1 <?php
2 3 4 5 6 7 8 9 10 11
12 Yii::import('bootstrap.helpers.TbHtml');
13 Yii::import('bootstrap.helpers.TbArray');
14
15 class WhAceEditor extends CInputWidget
16 {
17 18 19
20 public $theme = 'clouds';
21
22 23 24
25 public $mode = 'html';
26
27 28 29
30 public $pluginOptions = array();
31
32 33 34
35 public $events = array();
36
37 38 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 63
64 public function run()
65 {
66 $this->renderField();
67 $this->registerClientScript();
68 }
69
70 71 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 102
103 public function registerClientScript()
104 {
105
106 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
107 $assetsUrl = $this->getAssetsUrl($path);
108
109
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
117 $cs->registerScript(uniqid(__CLASS__ . '#' . $id, true), 'var ' . $id . ';', CClientScript::POS_HEAD);
118
119 ob_start();
120
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