Skip to content
This repository has been archived by the owner on Jul 14, 2019. It is now read-only.

Commit

Permalink
Name was changed to proper FluidPlayer, additional sourceOptions were…
Browse files Browse the repository at this point in the history
… implemented. Source is now being checked before use. Script options were added for configuration.
  • Loading branch information
mklemarczyk committed Jul 26, 2018
1 parent d91042b commit 3f83b55
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
18 changes: 3 additions & 15 deletions src/helpers/HtmlVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,18 @@
* @author Maciej Klemarczyk <[email protected]>
* @since 1.0
*/
class FluidPlayerWidget extends \yii\helpers\BaseHtml
class HtmlVideo extends \yii\helpers\BaseHtml
{
public static function beginForm($id, $options = [])
public static function beginVideo($id, $options = [])
{
$options['id'] = $id;
return static::beginTag('video', $options);
}

public static function source($src, $type = 'SD', $options = [])
public static function source($src, $options = [])
{
$options['src'] = Url::to($src);

if (isset($options['srcset']) && is_array($options['srcset'])) {
$srcset = [];
foreach ($options['srcset'] as $descriptor => $url) {
$srcset[] = Url::to($url) . ' ' . $descriptor;
}
$options['srcset'] = implode(',', $srcset);
}

if (!isset($options['alt'])) {
$options['alt'] = '';
}

return static::tag('source', '', $options);
}

Expand Down
1 change: 1 addition & 0 deletions src/models/VideoSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class VideoSource extends Model
public $title;
public $type;
public $quality;
public $sourceOptions;

public function rules()
{
Expand Down
28 changes: 20 additions & 8 deletions src/widgets/FluidPalyer.php → src/widgets/FluidPlayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace insma\player\widgets;

use yii\base\Widget;
use yii\helpers\Json;
use yii\web\View;

use insma\player\helpers\HtmlVideo;
Expand All @@ -30,18 +31,25 @@ class FluidPlayer extends Widget
*/
public $videoOptions;

/**
* @var string
*/
public $playerOptions;

public function init()
{
parent::init();
$this->registerAssetBundle()
$this->registerAssetBundle();
$this->registerPlayerScript();
}

public function run()
{
$htmlView = HtmlVideo::beginVideo($this->id, $videoOptions);
foreach ($sources as $key => $value) {
$htmlView .= self::prepareSourceTag($value);
$htmlView = HtmlVideo::beginVideo($this->id, $this->videoOptions);
foreach ($this->sources as $key => $value) {
if ($value instanceof VideoSource && $value->validate()) {
$htmlView .= self::prepareSourceTag($value);
}
}
$htmlView .= HtmlVideo::endVideo();
return $htmlView;
Expand All @@ -51,25 +59,29 @@ protected function prepareSourceTag($source)
{
$options = [];

$options['source'] = $source->source;
if (is_array($source->sourceOptions)) {
$options = $source->sourceOptions;
}

$options['title'] = $source->title;
$options['type'] = $source->type;
$options['quality'] = $source->quality;

return HtmlVideo::source($src, $options);
return HtmlVideo::source($source->source, $options);
}

/**
* Register assetBundle
*/
protected function registerAssetBundle()
{
FluidPalyerAsset::register($this->getView());
FluidPlayerAsset::register($this->getView());
}

protected function registerPlayerScript()
{
$jsScript = '';
$options = Json::encode($this->playerOptions);
$jsScript = 'fluidPlayer("' . $this->id . '",' . $options . ');';
$this->getView()->registerJs($jsScript, View::POS_READY, $this->id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author Maciej Klemarczyk <[email protected]>
* @since 1.0
*/
class FluidPalyerAsset extends \yii\web\AssetBundle
class FluidPlayerAsset extends \yii\web\AssetBundle
{
public $sourcePath = '@vendor/insma/fluid-player';

Expand Down

0 comments on commit 3f83b55

Please sign in to comment.