Blame view

vendor/kartik-v/yii2-krajee-base/WidgetTrait.php 6.26 KB
583ea05f   andryeyev   + widget-select2
1
2
3
4
5
6
  <?php
  
  /**
   * @package   yii2-krajee-base
   * @author    Kartik Visweswaran <kartikv2@gmail.com>
   * @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2014 - 2015
280e92c2   Dmitryi   авторизация через...
7
   * @version   1.7.8
583ea05f   andryeyev   + widget-select2
8
9
10
11
12
13
14
15
16
17
18
   */
  
  namespace kartik\base;
  
  use yii\helpers\Json;
  use yii\web\JsExpression;
  use yii\web\View;
  
  /**
   * Trait used for Krajee widgets.
   *
280e92c2   Dmitryi   авторизация через...
19
20
21
22
23
24
25
26
   * @property array  $options
   * @property array  $pluginOptions
   * @property array  $_encOptions
   * @property string $_hashVar
   * @property string $_dataVar
   *
   * @method View getView()
   *
583ea05f   andryeyev   + widget-select2
27
28
29
30
31
32
33
34
35
36
37
38
39
40
   * @author Kartik Visweswaran <kartikv2@gmail.com>
   * @since 1.6.0
   */
  trait WidgetTrait
  {
      /**
       * Sets HTML5 data variable
       *
       * @param string $name the plugin name
       *
       * @return void
       */
      protected function setDataVar($name)
      {
280e92c2   Dmitryi   авторизация через...
41
          /** @noinspection PhpUndefinedFieldInspection */
583ea05f   andryeyev   + widget-select2
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
          $this->_dataVar = "data-krajee-{$name}";
      }
  
      /**
       * Adds an asset to the view
       *
       * @param View   $view The View object
       * @param string $file The asset file name
       * @param string $type The asset file type (css or js)
       * @param string $class The class name of the AssetBundle
       *
       * @return void
       */
      protected function addAsset($view, $file, $type, $class)
      {
          if ($type == 'css' || $type == 'js') {
              $asset = $view->getAssetManager();
              $bundle = $asset->bundles[$class];
              if ($type == 'css') {
                  $bundle->css[] = $file;
              } else {
                  $bundle->js[] = $file;
              }
              $asset->bundles[$class] = $bundle;
              $view->setAssetManager($asset);
          }
      }
  
      /**
       * Generates a hashed variable to store the pluginOptions. The following special data attributes
       * will also be setup for the input widget, that can be accessed through javascript :
       * - 'data-krajee-{name}' will store the hashed variable storing the plugin options. The {name}
       *   tag will represent the plugin name (e.g. select2, typeahead etc.) - Fixes issue #6.
       *
       * @param string $name the name of the plugin
       */
      protected function hashPluginOptions($name)
      {
          $this->_encOptions = empty($this->pluginOptions) ? '' : Json::htmlEncode($this->pluginOptions);
          $this->_hashVar = $name . '_' . hash('crc32', $this->_encOptions);
          $this->options['data-krajee-' . $name] = $this->_hashVar;
      }
  
      /**
       * Registers plugin options by storing it in a hashed javascript variable
       *
280e92c2   Dmitryi   авторизация через...
88
89
       * @param string $name the plugin name
       *
583ea05f   andryeyev   + widget-select2
90
91
92
93
       * @return void
       */
      protected function registerPluginOptions($name)
      {
583ea05f   andryeyev   + widget-select2
94
95
          $this->hashPluginOptions($name);
          $encOptions = empty($this->_encOptions) ? '{}' : $this->_encOptions;
280e92c2   Dmitryi   авторизация через...
96
          $this->registerWidgetJs("var {$this->_hashVar} = {$encOptions};\n", View::POS_HEAD);
583ea05f   andryeyev   + widget-select2
97
98
99
100
101
102
103
104
105
106
      }
  
      /**
       * Returns the plugin registration script
       *
       * @param string $name the name of the plugin
       * @param string $element the plugin target element
       * @param string $callback the javascript callback function to be called after plugin loads
       * @param string $callbackCon the javascript callback function to be passed to the plugin constructor
       *
280e92c2   Dmitryi   авторизация через...
107
       * @return string the generated plugin script
583ea05f   andryeyev   + widget-select2
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
       */
      protected function getPluginScript($name, $element = null, $callback = null, $callbackCon = null)
      {
          $id = $element == null ? "jQuery('#" . $this->options['id'] . "')" : $element;
          $script = '';
          if ($this->pluginOptions !== false) {
              $this->registerPluginOptions($name);
              $script = "{$id}.{$name}({$this->_hashVar})";
              if ($callbackCon != null) {
                  $script = "{$id}.{$name}({$this->_hashVar}, {$callbackCon})";
              }
              if ($callback != null) {
                  $script = "jQuery.when({$script}).done({$callback})";
              }
              $script .= ";\n";
          }
          if (!empty($this->pluginEvents)) {
              foreach ($this->pluginEvents as $event => $handler) {
                  $function = new JsExpression($handler);
                  $script .= "{$id}.on('{$event}', {$function});\n";
              }
          }
          return $script;
      }
  
      /**
       * Registers a specific plugin and the related events
       *
       * @param string $name the name of the plugin
       * @param string $element the plugin target element
       * @param string $callback the javascript callback function to be called after plugin loads
       * @param string $callbackCon the javascript callback function to be passed to the plugin constructor
       */
      protected function registerPlugin($name, $element = null, $callback = null, $callbackCon = null)
      {
          $script = $this->getPluginScript($name, $element, $callback, $callbackCon);
280e92c2   Dmitryi   авторизация через...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
          $this->registerWidgetJs($script);
      }
  
      /**
       * Registers a JS code block for the widget.
       *
       * @param string  $js the JS code block to be registered
       * @param integer $position the position at which the JS script tag should be inserted in a page. The possible
       *     values are:
       *      - [[POS_HEAD]]: in the head section
       *      - [[POS_BEGIN]]: at the beginning of the body section
       *      - [[POS_END]]: at the end of the body section
       *      - [[POS_LOAD]]: enclosed within jQuery(window).load(). Note that by using this position, the method will
       *     automatically register the jQuery js file.
       *      - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value. Note that by using
       *     this position, the method will automatically register the jQuery js file.
       * @param string  $key the key that identifies the JS code block. If null, it will use $js as the key. If two JS
       *     code blocks are registered with the same key, the latter will overwrite the former.
       */
      public function registerWidgetJs($js, $position = View::POS_READY, $key = null)
      {
          if (empty($js)) {
              return;
          }
          $view = $this->getView();
          $view->registerJs($js, $position, $key);
          if (!empty($this->pjaxContainerId) && ($position === View::POS_LOAD || $position === View::POS_READY)) {
              $pjax = 'jQuery("#' . $this->pjaxContainerId . '")';
              $view->registerJs("{$pjax}.on('pjax:complete',function(){ {$js} });");
583ea05f   andryeyev   + widget-select2
173
174
          }
      }
280e92c2   Dmitryi   авторизация через...
175
  }