में एक पुन: प्रयोज्य विजेट कैसे बनाएं I yii2 की वर्तमान प्रोजेक्ट में मैंने एक साधारण विजेट बनाया है। बस, यह सभी जुई विषयों के लिए एक चयन विकल्प सूची बनाता है और उपयोगकर्ता को थीम बदलने और कुकीज़ के माध्यम से इसे सहेजने की अनुमति देता है।Yii2
इस विजेट को दो जावास्क्रिप्ट फ़ाइलों की आवश्यकता है, - वे रन() में पंजीकृत हैं - उनमें से एक jquery कुकीज़ प्लगइन है। मैं इस विजेट और इसकी जेएस फाइलों की अखंडता को बचाने के तरीके के बारे में पूछता हूं ताकि अन्य वाईआई 2 परियोजनाओं में बिना किसी आवश्यकता के, फिर से, सभी जरूरी जेएस फाइलों की प्रतिलिपि बनाने के लिए इसे आसान बनाया जा सके?
<?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";
}
}
इस मामले में मुझे लगता है कि आपको इसे गिटहब/बिटबकेट पर विस्तार के रूप में प्रकाशित करना चाहिए। – arogachev
@arogachev यह स्थानीय रूप से नहीं किया जा सका? क्या संगीतकार इसे स्थानीय रूप से उत्पादित करने का कोई तरीका है? – SaidbakR
शायद सैटिस एक विकल्प है https://github.com/composer/satis। – arogachev