1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC |
4
|
|
|
* @link http://2amigos.us |
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
6
|
|
|
*/ |
7
|
|
|
namespace dosamigos\tinymce; |
8
|
|
|
|
9
|
|
|
use yii\helpers\Html; |
10
|
|
|
use yii\helpers\Json; |
11
|
|
|
use yii\widgets\InputWidget; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* TinyMCE renders a tinyMCE js plugin for WYSIWYG editing. |
16
|
|
|
* |
17
|
|
|
* @author Antonio Ramirez <[email protected]> |
18
|
|
|
* @link http://www.ramirezcobos.com/ |
19
|
|
|
* @link http://www.2amigos.us/ |
20
|
|
|
*/ |
21
|
|
|
class TinyMce extends InputWidget |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string the language to use. Defaults to null (en). |
25
|
|
|
*/ |
26
|
|
|
public $language; |
27
|
|
|
/** |
28
|
|
|
* @var array the options for the TinyMCE JS plugin. |
29
|
|
|
* Please refer to the TinyMCE JS plugin Web page for possible options. |
30
|
|
|
* @see http://www.tinymce.com/wiki.php/Configuration |
31
|
|
|
*/ |
32
|
|
|
public $clientOptions = []; |
33
|
|
|
/** |
34
|
|
|
* @var bool whether to set the on change event for the editor. This is required to be able to validate data. |
35
|
|
|
* @see https://github.com/2amigos/yii2-tinymce-widget/issues/7 |
36
|
|
|
*/ |
37
|
|
|
public $triggerSaveOnBeforeValidateForm = true; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
3 |
|
public function run() |
43
|
|
|
{ |
44
|
3 |
|
if ($this->hasModel()) { |
45
|
2 |
|
echo Html::activeTextarea($this->model, $this->attribute, $this->options); |
46
|
2 |
|
} else { |
47
|
1 |
|
echo Html::textarea($this->name, $this->value, $this->options); |
48
|
|
|
} |
49
|
3 |
|
$this->registerClientScript(); |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Registers tinyMCE js plugin |
54
|
|
|
*/ |
55
|
3 |
|
protected function registerClientScript() |
56
|
|
|
{ |
57
|
3 |
|
$js = []; |
58
|
3 |
|
$view = $this->getView(); |
59
|
|
|
|
60
|
3 |
|
TinyMceAsset::register($view); |
61
|
|
|
|
62
|
3 |
|
$id = $this->options['id']; |
63
|
|
|
|
64
|
3 |
|
$this->clientOptions['selector'] = "#$id"; |
65
|
|
|
// @codeCoverageIgnoreStart |
66
|
|
|
if ($this->language !== null && $this->language !== 'en') { |
67
|
|
|
$langFile = "langs/{$this->language}.js"; |
68
|
|
|
$langAssetBundle = TinyMceLangAsset::register($view); |
69
|
|
|
$langAssetBundle->js[] = $langFile; |
70
|
|
|
$this->clientOptions['language_url'] = $langAssetBundle->baseUrl . "/{$langFile}"; |
71
|
|
|
$this->clientOptions['language'] = "{$this->language}";//Language fix. Without it EN language when add some plugins like codemirror |
72
|
|
|
} |
73
|
|
|
// @codeCoverageIgnoreEnd |
74
|
3 |
|
|
75
|
|
|
$options = Json::encode($this->clientOptions); |
76
|
3 |
|
|
77
|
3 |
|
$js[] = "tinymce.remove('#$id');tinymce.init($options);"; |
78
|
3 |
|
if ($this->triggerSaveOnBeforeValidateForm) { |
79
|
3 |
|
$js[] = "$('#{$id}').parents('form').on('beforeValidate', function() { tinymce.triggerSave(); });"; |
80
|
3 |
|
} |
81
|
3 |
|
$view->registerJs(implode("\n", $js)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|