Blame view

vendor/kartik-v/yii2-krajee-base/AssetBundle.php 3.29 KB
583ea05f   andryeyev   + widget-select2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  <?php
  
  /**
   * @package   yii2-krajee-base
   * @author    Kartik Visweswaran <kartikv2@gmail.com>
   * @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2014 - 2015
   * @version   1.7.7
   */
  
  namespace kartik\base;
  
  /**
   * Base asset bundle for all widgets
   *
   * @author Kartik Visweswaran <kartikv2@gmail.com>
   * @since 1.0
   */
  class AssetBundle extends \yii\web\AssetBundle
  {
      const EMPTY_ASSET = 'N0/@$$3T$';
      const EMPTY_PATH = 'N0/P@T#';
      const KRAJEE_ASSET = 'K3/@$$3T$';
      const KRAJEE_PATH = 'K3/P@T#';
      
      public $js = self::KRAJEE_ASSET;
      public $css = self::KRAJEE_ASSET;
      public $sourcePath = self::KRAJEE_PATH;    
      public $depends = [
          'yii\web\JqueryAsset',
          'yii\web\YiiAsset',
          'yii\bootstrap\BootstrapAsset',
      ];
  
      /**
       * @inheritdoc
       */
      public function init() {
          parent::init();
          if ($this->js === self::KRAJEE_ASSET) {
              $this->js = [];
          }
          if ($this->css === self::KRAJEE_ASSET) {
              $this->css = [];
          }
          if ($this->sourcePath === self::KRAJEE_PATH) {
              $this->sourcePath = null;
          }
      }
      
      /**
       * Adds a language JS locale file
       *
       * @param string $lang the ISO language code
       * @param string $prefix the language locale file name prefix
       * @param string $dir the language file directory relative to source path
       * @param bool $min whether to auto use minified version
       *
       * @return AssetBundle instance
       */
      public function addLanguage($lang = '', $prefix = '', $dir = null, $min = false)
      {
          if (empty($lang) || substr($lang, 0, 2) == 'en') {
              return $this;
          }
          $ext = $min ? (YII_DEBUG ? ".min.js" : ".js") : ".js";       
          $file = "{$prefix}{$lang}{$ext}";
          if ($dir === null) {
              $dir = 'js';
          } elseif ($dir === "/") {
              $dir = '';
          }
          $path = $this->sourcePath . '/' . $dir;
          if (!Config::fileExists("{$path}/{$file}")) {
              $lang = Config::getLang($lang);
              $file = "{$prefix}{$lang}{$ext}";
          }
          if (Config::fileExists("{$path}/{$file}")) {
             $this->js[] = empty($dir) ? $file : "{$dir}/{$file}";
          }
          return $this;
      }
      
      /**
       * Set up CSS and JS asset arrays based on the base-file names
       *
       * @param string $type whether 'css' or 'js'
       * @param array  $files the list of 'css' or 'js' basefile names
       */
      protected function setupAssets($type, $files = [])
      {
          if ($this->$type === self::KRAJEE_ASSET) {
              $srcFiles = [];
              $minFiles = [];
              foreach ($files as $file) {
                  $srcFiles[] = "{$file}.{$type}";
                  $minFiles[] = "{$file}.min.{$type}";
              }
              $this->$type = YII_DEBUG ? $srcFiles : $minFiles;
          } elseif ($this->$type === self::EMPTY_ASSET) {
              $this->$type = [];
          }
      }
  
      /**
       * Sets the source path if empty
       *
       * @param string $path the path to be set
       */
      protected function setSourcePath($path)
      {
          if ($this->sourcePath === self::KRAJEE_PATH) {
              $this->sourcePath = $path;
          } elseif ($this->sourcePath === self::EMPTY_PATH) {
              $this->sourcePath = null;
          }
      }
  }