YiiWheels
  • Package
  • Class
  • Tree

Packages

  • None
  • yiiwheels
    • behaviors
    • widgets
    • widgets
      • ace
      • box
      • datepicker
      • daterangepicker
      • datetimepicker
      • detail
      • fileupload
      • fileuploader
      • gallery
      • google
      • grid
        • behaviors
        • operations
      • highcharts
      • maskInput
      • maskmoney
      • modal
      • multiselect
      • rangeslider
      • redactor
      • select2
      • sparklines
      • switch
      • timeago
      • timepicker
      • toggle
      • typeahead

Classes

  • WhAceEditor
  1 <?php
  2 /**
  3  * WhAceEditor widget class
  4  *
  5  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  6  * @author Matt Tabin <amigo.tabin@gmail.com>
  7  * @copyright Copyright &copy; 2amigos.us 2013-
  8  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  9  * @package YiiWheels.widgets.ace
 10  * @uses Yiistrap.helpers.TbHtml
 11  */
 12 Yii::import('bootstrap.helpers.TbHtml');
 13 Yii::import('bootstrap.helpers.TbArray');
 14 
 15 class WhAceEditor extends CInputWidget
 16 {
 17     /**
 18      * @var string the theme
 19      */
 20     public $theme = 'clouds';
 21 
 22     /**
 23      * @var string the editor mode
 24      */
 25     public $mode = 'html';
 26 
 27     /**
 28      * @var array the options for the ace editor
 29      */
 30     public $pluginOptions = array();
 31 
 32     /**
 33      * @var string[] the JavaScript event handlers.
 34      */
 35     public $events = array();
 36 
 37     /**
 38      * Initializes the widget.
 39      */
 40     public function init()
 41     {
 42         if (empty($this->theme)) {
 43             throw new CException(Yii::t(
 44                 'zii',
 45                 '"{attribute}" cannot be empty.',
 46                 array('{attribute}' => 'theme')
 47             ));
 48         }
 49 
 50         if (empty($this->mode)) {
 51             throw new CException(Yii::t(
 52                 'zii',
 53                 '"{attribute}" cannot be empty.',
 54                 array('{attribute}' => 'mode')
 55             ));
 56         }
 57 
 58         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
 59     }
 60 
 61     /**
 62      * Runs the widget.
 63      */
 64     public function run()
 65     {
 66         $this->renderField();
 67         $this->registerClientScript();
 68     }
 69 
 70     /**
 71      * Renders field
 72      */
 73     public function renderField()
 74     {
 75         list($name, $id) = $this->resolveNameID();
 76 
 77         TbArray::defaultValue('id', $id, $this->htmlOptions);
 78         TbArray::defaultValue('name', $name, $this->htmlOptions);
 79 
 80         $tagOptions = $this->htmlOptions;
 81 
 82         $tagOptions['id'] = 'aceEditor_' . $tagOptions['id'];
 83 
 84         echo CHtml::openTag('div', $tagOptions);
 85         echo CHtml::closeTag('div');
 86 
 87         $this->htmlOptions['style'] = 'display:none';
 88 
 89         if ($this->hasModel()) {
 90             echo CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
 91         } else {
 92             echo CHtml::textArea($name, $this->value, $this->htmlOptions);
 93         }
 94 
 95         $this->htmlOptions = $tagOptions;
 96         if (!isset($this->htmlOptions['textareaId']))
 97             $this->htmlOptions['textareaId'] = $id;
 98     }
 99 
100     /**
101      * Registers required client script for bootstrap ace editor.
102      */
103     public function registerClientScript()
104     {
105         /* publish assets dir */
106         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
107         $assetsUrl = $this->getAssetsUrl($path);
108 
109         /* @var $cs CClientScript */
110         $cs = Yii::app()->getClientScript();
111 
112         $cs->registerScriptFile($assetsUrl . '/js/ace.js', CClientScript::POS_END);
113 
114         $id = TbArray::getValue('id', $this->htmlOptions, $this->getId());
115 
116         /* Global value that will hold the editor */
117         $cs->registerScript(uniqid(__CLASS__ . '#' . $id, true), 'var ' . $id . ';', CClientScript::POS_HEAD);
118 
119         ob_start();
120         /* initialize plugin */
121         $selector = TbArray::getValue('id', $this->htmlOptions, $this->getId());
122 
123         echo $selector . '= ace.edit("' . $id . '");' . PHP_EOL;
124         echo $selector . '.setTheme("ace/theme/' . $this->theme . '");' . PHP_EOL;
125         echo $selector . '.getSession().setMode('.(is_array($this->mode) ? CJavaScript::encode($this->mode) : '"ace/mode/'.$this->mode.'"').');' . PHP_EOL;
126         echo $selector . '.setValue($("#'.$this->htmlOptions['textareaId'].'").val());' . PHP_EOL;
127         echo $selector . '.getSession().on("change", function(){
128                 var theVal = ' . $selector . '.getSession().getValue();
129                 $("#'.$this->htmlOptions['textareaId'].'").val(theVal);
130             });';
131             
132         if (!empty($this->events) && is_array($this->events)) {
133             foreach ($this->events as $name => $handler) {
134                 $handler = ($handler instanceof CJavaScriptExpression)
135                     ? $handler
136                     : new CJavaScriptExpression($handler);
137 
138                 echo $id . ".getSession().on('{$name}', {$handler});" . PHP_EOL;
139             }
140         }
141         
142         if (!empty($this->pluginOptions))
143             echo $selector . '.setOptions('.CJavaScript::encode($this->pluginOptions).')';
144 
145         $cs->registerScript(uniqid(__CLASS__ . '#ReadyJS' . $id, true), ob_get_clean());
146     }
147 }
148 
YiiWheels API documentation generated by ApiGen 2.8.0