Blame view

vendor/kartik-v/yii2-krajee-base/Html5Input.php 6.85 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
19
20
21
22
23
24
25
   */
  
  namespace kartik\base;
  
  use Yii;
  use yii\helpers\Html;
  use yii\helpers\ArrayHelper;
  
  /**
   * Html5Input widget is a widget encapsulating the HTML 5 inputs.
   *
   * @author Kartik Visweswaran <kartikv2@gmail.com>
   * @since 1.0
   * @see http://twitter.github.com/typeahead.js/examples
   */
  class Html5Input extends InputWidget
  {
      /**
583ea05f   andryeyev   + widget-select2
26
27
28
       * @var string the HTML 5 input type
       */
      public $type;
280e92c2   Dmitryi   авторизация через...
29
  
583ea05f   andryeyev   + widget-select2
30
31
32
33
      /**
       * @var string the width in 'px' or '%' of the HTML5 input container
       */
      public $width;
280e92c2   Dmitryi   авторизация через...
34
  
583ea05f   andryeyev   + widget-select2
35
36
37
38
      /**
       * @var array the HTML attributes for the widget container
       */
      public $containerOptions = [];
280e92c2   Dmitryi   авторизация через...
39
  
583ea05f   andryeyev   + widget-select2
40
41
42
43
      /**
       * @var array the HTML attributes for the HTML-5 input.
       */
      public $html5Options = [];
280e92c2   Dmitryi   авторизация через...
44
  
583ea05f   andryeyev   + widget-select2
45
46
47
48
      /**
       * @var array the HTML attributes for the HTML-5 input container
       */
      public $html5Container = [];
280e92c2   Dmitryi   авторизация через...
49
  
583ea05f   andryeyev   + widget-select2
50
51
52
53
54
      /**
       * @var string|boolean the message shown for unsupported browser. If set to false
       * will not be displayed
       */
      public $noSupport;
280e92c2   Dmitryi   авторизация через...
55
  
583ea05f   andryeyev   + widget-select2
56
57
58
59
      /**
       * @var string array the HTML attributes for container displaying unsupported browser message
       */
      public $noSupportOptions = [];
280e92c2   Dmitryi   авторизация через...
60
  
583ea05f   andryeyev   + widget-select2
61
62
63
64
      /**
       * @var string one of the SIZE modifiers 'lg', 'md', 'sm', 'xs'
       */
      public $size;
280e92c2   Dmitryi   авторизация через...
65
  
583ea05f   andryeyev   + widget-select2
66
67
      /**
       * @var array the addon content
280e92c2   Dmitryi   авторизация через...
68
       * - prepend: array/string the prepend addon content. If set as an array, the following options can be set:
583ea05f   andryeyev   + widget-select2
69
70
71
       *   - content: string the prepend addon content
       *   - asButton: boolean whether the addon is a button
       *   - options: array the HTML attributes for the prepend addon
280e92c2   Dmitryi   авторизация через...
72
       * - append: array/string the append addon content. If set as an array, the following options can be set:
583ea05f   andryeyev   + widget-select2
73
74
75
       *   - content: string the append addon content
       *   - asButton: boolean whether the addon is a button
       *   - options: array the HTML attributes for the append addon
280e92c2   Dmitryi   авторизация через...
76
77
       * - preCaption: array/string the addon content placed before the caption. If set as an array, the following
       *     options can be set:
583ea05f   andryeyev   + widget-select2
78
79
80
81
82
83
       *   - content: string the append addon content
       *   - asButton: boolean whether the addon is a button
       *   - options: array the HTML attributes for the append addon     */
      public $addon = [];
  
      /**
280e92c2   Dmitryi   авторизация через...
84
85
86
87
88
89
90
91
92
       * @var array the special inputs which need captions
       */
      private static $_specialInputs = [
          'color',
          'range'
      ];
  
      /**
       * @inheritdoc
583ea05f   andryeyev   + widget-select2
93
       */
280e92c2   Dmitryi   авторизация через...
94
      public function run()
583ea05f   andryeyev   + widget-select2
95
      {
583ea05f   andryeyev   + widget-select2
96
97
98
          $this->initInput();
      }
  
280e92c2   Dmitryi   авторизация через...
99
100
101
      /**
       * Initializes the input
       */
583ea05f   andryeyev   + widget-select2
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
      protected function initInput()
      {
          $this->initDisability($this->html5Options);
          if (in_array($this->type, self::$_specialInputs)) {
              $this->html5Options['id'] = $this->options['id'] . '-source';
              $this->registerAssets();
              echo $this->renderInput();
          } else {
              ArrayHelper::merge($this->options, $this->html5Options);
              if (isset($this->size)) {
                  Html::addCssClass($this->options, ['class' => 'input-' . $this->size]);
              }
              echo $this->getHtml5Input();
          }
      }
  
      /**
       * Registers the needed assets
       */
      public function registerAssets()
      {
          $view = $this->getView();
          Html5InputAsset::register($view);
          $caption = 'jQuery("#' . $this->options['id'] . '")';
          $input = 'jQuery("#' . $this->html5Options['id'] . '")';
          $js = "{$caption}.on('change',function(){{$input}.val(this.value)});\n" .
              "{$input}.on('input change',function(e){{$caption}.val(this.value);if(e.type=='change'){{$caption}.trigger('change');}});";
280e92c2   Dmitryi   авторизация через...
129
          $this->registerWidgetJs($js);
583ea05f   andryeyev   + widget-select2
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
      }
  
      /**
       * Renders the special HTML5 input
       * Mainly useful for the color and range inputs
       */
      protected function renderInput()
      {
          Html::addCssClass($this->options, 'form-control');
          $size = isset($this->size) ? ' input-group-' . $this->size : '';
          Html::addCssClass($this->containerOptions, 'input-group input-group-html5' . $size);
          if (isset($this->width) && ($this->width > 0)) {
              Html::addCssStyle($this->html5Container, 'width:' . $this->width);
          }
          Html::addCssClass($this->html5Container, 'input-group-addon addon-' . $this->type);
          $caption = $this->getInput('textInput');
          $value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value;
          $input = Html::input($this->type, $this->html5Options['id'], $value, $this->html5Options);
          $prepend = static::getAddonContent(ArrayHelper::getValue($this->addon, 'prepend', ''));
          $append = static::getAddonContent(ArrayHelper::getValue($this->addon, 'append', ''));
          $preCaption = static::getAddonContent(ArrayHelper::getValue($this->addon, 'preCaption', ''));
          $prepend .= Html::tag('span', $input, $this->html5Container);
          $content = Html::tag('div', $prepend . $preCaption . $caption . $append, $this->containerOptions);
          Html::addCssClass($this->noSupportOptions, 'alert alert-warning');
          if ($this->noSupport === false) {
              $message = '';
          } else {
              $noSupport = !empty($this->noSupport) ? $this->noSupport :
280e92c2   Dmitryi   авторизация через...
158
159
160
161
162
                  Yii::t(
                      'kvbase',
                      'It is recommended you use an upgraded browser to display the {type} control properly.',
                      ['type' => $this->type]
                  );
583ea05f   andryeyev   + widget-select2
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
              $message = "\n<br>" . Html::tag('div', $noSupport, $this->noSupportOptions);
          }
          return "<!--[if lt IE 10]>\n{$caption}{$message}\n<![endif]--><![if gt IE 9]>\n{$content}\n<![endif]>";
      }
  
      /**
       * Parses and returns addon content
       *
       * @param string /array $addon the addon parameter
       *
       * @return string
       */
      protected static function getAddonContent($addon)
      {
          if (is_array($addon)) {
              $content = ArrayHelper::getValue($addon, 'content', '');
              $options = ArrayHelper::getValue($addon, 'options', []);
              if (ArrayHelper::getValue($addon, 'asButton', false) == true) {
                  Html::addCssClass($options, 'input-group-btn');
                  return Html::tag('div', $content, $options);
              } else {
                  Html::addCssClass($options, 'input-group-addon');
                  return Html::tag('span', $content, $options);
              }
          }
          return $addon;
      }
  
      /**
       * Gets the HTML5 input
       * return string
       */
      protected function getHtml5Input()
      {
          if ($this->hasModel()) {
              return Html::activeInput($this->type, $this->model, $this->attribute, $this->options);
          }
          return Html::input($this->type, $this->name, $this->value, $this->options);
      }
  }