WhEditable::buildJsOptions PHP Method

buildJsOptions() public method

Builds javascript options
public buildJsOptions ( )
    public function buildJsOptions()
    {
        //normalize url from array
        $this->url = CHtml::normalizeUrl($this->url);
        $options = array('name' => $this->name, 'title' => CHtml::encode($this->title));
        //if value needed for autotext and it's not scalar --> add it to js options
        if ($this->_prepareToAutotext && !is_scalar($this->value)) {
            $options['value'] = $this->value;
        }
        //support of CSRF out of box, see https://github.com/vitalets/x-editable-yii/issues/38
        if (Yii::app()->request->enableCsrfValidation) {
            $csrfTokenName = Yii::app()->request->csrfTokenName;
            $csrfToken = Yii::app()->request->csrfToken;
            if (!isset($this->params[$csrfTokenName])) {
                $this->params[$csrfTokenName] = $csrfToken;
            }
        }
        //simple options set directly from config
        foreach (array('url', 'type', 'mode', 'placement', 'emptytext', 'params', 'inputclass', 'format', 'viewformat', 'template', 'combodate', 'select2', 'viewseparator', 'showbuttons', 'send') as $option) {
            if ($this->{$option} !== null) {
                $options[$option] = $this->{$option};
            }
        }
        if ($this->source) {
            //if source is array --> convert it to x-editable format.
            //Since 1.1.0 source as array with one element is NOT treated as Yii route!
            if (is_array($this->source)) {
                //if first elem is array assume it's normal x-editable format, so just pass it
                if (isset($this->source[0]) && is_array($this->source[0])) {
                    $options['source'] = $this->source;
                } else {
                    //else convert to x-editable source format {value: 1, text: 'abc'}
                    $options['source'] = array();
                    foreach ($this->source as $value => $text) {
                        $options['source'][] = array('value' => $value, 'text' => $text);
                    }
                }
            } else {
                //source is url string (or js function)
                $options['source'] = CHtml::normalizeUrl($this->source);
            }
        }
        //callbacks
        foreach (array('validate', 'success', 'display') as $method) {
            if (isset($this->{$method})) {
                $options[$method] = (strpos($this->{$method}, 'js:') !== 0 ? 'js:' : '') . $this->{$method};
            }
        }
        //merging options
        $this->options = CMap::mergeArray($this->options, $options);
        //i18n for `clear` in date and datetime
        if ($this->type == 'date' || $this->type == 'datetime') {
            if (!isset($this->options['clear'])) {
                $this->options['clear'] = Yii::t('EditableField.editable', 'x clear');
            }
        }
    }