Я создал простой виджет в своем текущем проекте yii2. Просто он создает список опций выбора для всех тем jui и позволяет пользователю изменять тему и сохранять ее в виде файлов cookie.Как создать многоразовый виджет в Yii2
Этот виджет нуждается в двух файлах javascript, - они зарегистрированы в run() - одним из них является плагин jquery cookie. Я спрашиваю о том, как сохранить целостность этого виджета и его js-файлов, чтобы упростить повторное использование в других проектах Yii2 без необходимости, maunally, копировать все необходимые js-файлы?
<?php
namespace common\libs;
use yii;
use yii\base\Widget;
use yii\web\View;
use yii\web\JqueryAsset;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of JuiThemeSelectWidget
*
* @author Said Bakr
*/
class JuiThemeSelectWidget extends Widget
{
private $list;
private $script;
private static $juiThemeSelectId = 'JuiThemesList';
public $themeListId;
public $label;
public function init() {
parent::init();
if ($this->themeListId) self::$juiThemeSelectId = $this->themeListId;
$this->list = $this->createSelectList($this->getThemesList());
$this->makeScript();
}
public static function getThemesList()
{
$themesPath = dirname(Yii::$app->basePath).DIRECTORY_SEPARATOR."vendor".DIRECTORY_SEPARATOR."bower".DIRECTORY_SEPARATOR."jquery-ui".DIRECTORY_SEPARATOR."themes";
$output = [];
foreach (scandir($themesPath) as $item){
if (is_dir($themesPath.DIRECTORY_SEPARATOR.$item) && ($item != '.' && $item !='..')) $output[] = $item;
}
return $output;
}
public static function createSelectList($items)
{
$juiThemeSelectId = self::$juiThemeSelectId;
$output = '';
$output .= "<select id=\"$juiThemeSelectId\">"."\n";
foreach ($items as $item){
$output .= "<option value='$item'>$item</option>\n";
}
$output .= "</select>\n";
return $output;
}
/**
* Making the client-side script for the list */
private function makeScript()
{
$t = self::$juiThemeSelectId;
$this->script = <<<EOD
<script>
var juiThemeSelectId = "$t"
</script>
EOD;
}
public function run() {
parent::run();
$this->getView()->registerJsFile('/myjs/jquery.cookie.js', ['depends' => [JqueryAsset::className()]]);
$this->getView()->registerJsFile('/myjs/JuiThemeSelect.js', ['depends' => [JqueryAsset::className()]]);
return "$this->label $this->list \n $this->script";
}
}
В этом случае я думаю, вы должны просто опубликовать его на GitHub/Bitbucket в качестве расширения. – arogachev
@arogachev Не удалось ли это сделать локально? Есть ли способ заставить композитора производить его локально? – SaidbakR
Возможно, Satis является опцией https://github.com/composer/satis. – arogachev