Exemplo n.º 1
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if (is_null($this->timeFormat)) {
         $this->timeFormat = Yii::$app->getFormatter()->timeFormat;
         if (is_null($this->timeFormat)) {
             $this->timeFormat = 'medium';
         }
     }
     if (is_null($this->altTimeFormat)) {
         $this->altTimeFormat = 'HH:mm:ss';
     }
     Html::addCssClass($this->options, 'form-control');
     $this->altOptions['id'] = $this->options['id'] . '-alt';
     $this->clientOptions = array_merge(array_diff_assoc(['showButtonPanel' => $this->showButtonPanel], get_class_vars(__CLASS__)), $this->clientOptions, ['timeFormat' => FormatConverter::convertDatePhpOrIcuToJui($this->timeFormat, 'time'), 'altField' => '#' . $this->altOptions['id'], 'altFieldTimeOnly' => false, 'altTimeFormat' => FormatConverter::convertDatePhpOrIcuToJui($this->altTimeFormat, 'time')]);
     if (array_key_exists('readonly', $this->options) && $this->options['readonly']) {
         if (!array_key_exists('beforeShow', $this->clientOptions)) {
             $this->clientOptions['beforeShow'] = new JsExpression('function (input, inst) { return false; }');
         }
     }
 }
 /**
  * @param string $phpCharacter
  * @param string $juiCharacter
  * @dataProvider charactersDataProvider
  */
 public function testConvertTimePhpToJui($phpCharacter, $juiCharacter)
 {
     $this->assertEquals($juiCharacter, FormatConverter::convertTimePhpToJui($phpCharacter));
 }
    /**
     * @param int $mode
     * @param string $value
     * @dataProvider modeValueDataProvider
     */
    public function testWidgetClientOptions($mode, $value)
    {
        $actual = $this->getActual($mode, $value, ['clientOptions' => ['defaultDate' => -1, 'duration' => 'slow']]);
        list($id, $name) = $this->getIdName($mode);
        if (is_int($value) || is_string($value) && strlen($value) || $value instanceof DateTime || $value instanceof DateTimeInterface) {
            $formatter = Yii::$app->getFormatter();
            $encodedValueAttr = ' value="' . Html::encode($formatter->asDate($value) . ' ' . $formatter->asTime($value)) . '"';
            $encodedAltValueAttr = ' value="' . Html::encode($formatter->asDate($value, 'yyyy-MM-dd') . ' ' . $formatter->asTime($value, 'HH:mm:ss')) . '"';
        } elseif (!is_null($value)) {
            $encodedValueAttr = ' value="' . Html::encode($value) . '"';
            $encodedAltValueAttr = ' value="' . Html::encode($value) . '"';
        } else {
            $encodedValueAttr = '';
            $encodedAltValueAttr = '';
        }
        $expectedHtml = <<<EXPECTED_HTML
<input type="text" id="{$id}" class="form-control"{$encodedValueAttr}><input type="hidden" id="{$id}-alt" name="{$name}"{$encodedAltValueAttr}>
EXPECTED_HTML;
        $dateFormat = str_replace('\'', '\\u0027', FormatConverter::convertDateIcuToJui('medium'));
        $timeFormat = FormatConverter::convertDatePhpOrIcuToJui('medium', 'time');
        $expectedJs = <<<EXPECTED_JS
jQuery('#{$id}').datetimepicker({"defaultDate":-1,"duration":"slow","dateFormat":"{$dateFormat}","separator":" ","timeFormat":"{$timeFormat}","altField":"#{$id}-alt","altFieldTimeOnly":false,"altFormat":"yy-mm-dd","altSeparator":" ","altTimeFormat":"HH:mm:ss"});
EXPECTED_JS;
        $this->checkExpected($mode, $actual, $expectedHtml, $expectedJs);
    }