-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKindEditor.php
81 lines (75 loc) · 1.96 KB
/
KindEditor.php
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
<?php
/**
* Usage:
* <?=\djfly\kindeditor\KindEditor::widget([
* 'id'=>'Article_content', # Textarea id
*
* # Additional Parameters (Check http://www.kindsoft.net/docs/option.html)
* 'items' => [
* 'width'=>'700px',
* 'height'=>'300px',
* 'themeType'=>'simple',
* 'allowFileManager'=>false,
* 'allowImageUpload'=>false,
* 'items'=>[
* 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
* 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
* 'insertunorderedlist', '|', 'emoticons', 'image', 'link',
* ]],
* ]); ?>
*/
/**
* KindEditor InputWidget.
*/
namespace djfly\kindeditor;
use Yii;
class KindEditor extends \yii\widgets\InputWidget
{
public $language = '';
/**
* @var array the kindeditor items configuration.
*/
public $items = array();
/**
* Initializes the widget.
*/
public function init()
{
// Prevents the extension from registering scripts and publishing assets when ran from the command line.
if (Yii::$app instanceof \yii\console\Application){
return;
}
parent::init();
Yii::setAlias('djfly/kindeditor', dirname(dirname(__DIR__)) . '/djfly/kindeditor');
}
/**
* Runs the widget.
*/
public function run()
{
$script = '';
$script = $script.'KindEditor.ready(function(K){var editor=K.create("textarea[id='.$this->options['id'].']", {'.$this->renderItems($this->items).'})});';
$this->getView()->registerJs($script);
KindEditorAsset::register($this->getView());
}
/**
* Renders the items.
* @param array $items the item configuration.
*/
protected function renderItems($items)
{
$script = '';
foreach ($items as $key => $item)
{
if(is_array($item))
{
$script = $script."'$key':[";
foreach ($item as $value)
$script = $script."'$value',";
$script = $script."],";
} else
$script = $script."'$key':'$item',";
}
return $script;
}
}