forked from froala/yii2-froala-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFroalaEditorWidget.php
More file actions
113 lines (99 loc) · 3.24 KB
/
FroalaEditorWidget.php
File metadata and controls
113 lines (99 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace froala\froalaeditor;
use yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
class FroalaEditorWidget extends InputWidget
{
const PLUGIN_NAME = 'FroalaEditor';
/**
* @var array
* Plugins to be included, leave empty to load all plugins
* <pre>sample input:
* [
* //specify only needed forala plugins (local files)
* 'url',
* 'align',
* 'char_counter',
* ...
* //override default files for a specific plugin
* 'table' => [
* 'css' => '<new css file url>'
* ],
* //include custom plugin
* 'my_plugin' => [
* 'js' => '<js file url>' // required
* 'css' => '<css file url>' // optional
* ],
* ...
* ]
*/
public $clientPlugins;
/**
* Remove these plugins from this list plugins, this option overrides 'clientPlugins'
* @var array
*/
public $excludedPlugins = [];
/**
* FroalaEditor Options
* @var array
*/
public $clientOptions = [];
/**
* csrf cookie param
* @var string
*/
public $csrfCookieParam = '_csrfCookie';
/**
* @var boolean
*/
public $render = true;
/**
* @inheritdoc
*/
public function run()
{
if ($this->render) {
if ($this->hasModel()) {
echo Html::activeTextarea($this->model, $this->attribute, $this->options);
} else {
echo Html::textarea($this->name, $this->value, $this->options);
}
}
$this->registerClientScript();
}
/**
* register client scripts(css, javascript)
*/
public function registerClientScript()
{
$view = $this->getView();
$asset = FroalaEditorAsset::register($view);
$plugin_names = $asset->registerClientPlugins($this->clientPlugins, $this->excludedPlugins);
//theme
$themeType = isset($this->clientOptions['theme']) ? $this->clientOptions['theme'] : 'default';
if ($themeType != 'default') {
$view->registerCssFile("{$asset->baseUrl}/css/themes/{$themeType}.css", ['depends' => '\froala\froalaeditor\FroalaEditorAsset']);
}
//language
$langType = isset($this->clientOptions['language']) ? $this->clientOptions['language'] : 'en_gb';
if ($langType != 'es_gb') {
$view->registerJsFile("{$asset->baseUrl}/js/languages/{$langType}.js", ['depends' => '\froala\froalaeditor\FroalaEditorAsset']);
}
$id = $this->options['id'];
if (empty($this->clientPlugins)) {
$pluginsEnabled = false;
} else {
$pluginsEnabled = array_diff($plugin_names, $this->excludedPlugins ?: []);
}
if(!empty($pluginsEnabled)){
foreach($pluginsEnabled as $key =>$item){
$pluginsEnabled[$key] = lcfirst (yii\helpers\Inflector::camelize($item));
}
}
$jsOptions = array_merge($this->clientOptions, $pluginsEnabled ? ['pluginsEnabled' => $pluginsEnabled] : []);
$jsOptions = Json::encode($jsOptions);
$view->registerJs("new FroalaEditor('#$id',$jsOptions);");
}
}