1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Editor;
13
14 use Brickrouge\Document;
15 use Brickrouge\Element;
16 use Brickrouge\Form;
17 use Brickrouge\Group;
18 use Brickrouge\Text;
19
20 21 22
23 class TabbableEditorElement extends Element implements EditorElement
24 {
25 26 27 28 29
30 static protected function add_assets(Document $document)
31 {
32 parent::add_assets($document);
33
34 $document->css->add('assets/editor.css');
35 $document->js->add('assets/editor.js');
36 }
37
38 public function __construct(array $attributes=array())
39 {
40 parent::__construct
41 (
42 'div', $attributes + array
43 (
44 Element::WIDGET_CONSTRUCTOR => 'TabbableEditor',
45
46 'class' => 'editor editor--tabbable'
47 )
48 );
49 }
50
51 52 53 54 55
56 protected function alter_dataset(array $dataset)
57 {
58 return parent::alter_dataset(array_merge($dataset, array(
59
60 'control-name' => $this['name']
61
62 )));
63 }
64
65 protected function render_inner_html()
66 {
67 $value = $this['value'];
68 $panes = $value ? array_values((array) $this['value']) : array
69 (
70 array
71 (
72 'title' => 'New tab',
73 'editor_id' => 'rte',
74 'serialized_content' => null
75 )
76 );
77
78 foreach ($panes as $id => &$pane)
79 {
80 $pane['name'] = $this['name'] . '[' . $id . ']';
81 }
82
83 $nav = $this->render_tabbable_nav($panes);
84 $content = $this->render_tabbable_content($panes);
85
86 return <<<EOT
87 <div class="tabbable" tabindex="0">
88
89 $nav
90 $content
91
92 </div>
93 EOT;
94 }
95
96 protected function render_tabbable_nav(array $panes)
97 {
98 $first = key($panes);
99 $html = '';
100
101 foreach ($panes as $i => $pane)
102 {
103 $tab = static::create_tab($pane);
104
105 if (!$i)
106 {
107 $tab->add_class('active');
108 }
109
110 $html .= $tab;
111 }
112
113 $html .= '<li><a href="#" title="Nouvel onglet" data-create="tab">+</a></li>';
114
115 return '<ul class="nav nav-tabs">' . $html . '</ul>';
116 }
117
118 protected function render_tabbable_content(array $panes)
119 {
120 $html = '';
121
122 foreach ($panes as $i => $pane)
123 {
124 $element = static::create_pane($pane);
125
126 if (!$i)
127 {
128 $element->add_class('active');
129 }
130
131 $html .= $element;
132 }
133
134 return '<div class="tab-content widget-bordered">' . $html . '</div>';
135 }
136
137 138 139 140 141 142 143 144 145
146 static public function create_tab(array $pane)
147 {
148 return new Element
149 (
150 'li', array
151 (
152 Element::CHILDREN => array
153 (
154 new Element
155 (
156 'a', array
157 (
158 Element::INNER_HTML => '<span class="title" data-recieves="title">' . $pane['title'] . '</span><span class="close" data-removes="tab">×</span>',
159
160 'href' => '#',
161 'data-toggle' => 'tab',
162 'tabindex' => -1
163 )
164 )
165 )
166 )
167 );
168 }
169
170 171 172 173 174 175 176 177 178
179 static public function create_pane(array $pane)
180 {
181 global $core;
182
183 $editor_id = $pane['editor_id'];
184 $name = $pane['name'];
185 $editor = $core->editors[$editor_id];
186 $value = null;
187
188 if (!empty($pane['content']))
189 {
190 $value = $pane['content'];
191 }
192 else if (!empty($pane['serialized_content']))
193 {
194 $value = $editor->unserialize($pane['serialized_content']);
195 }
196
197 $content = new Group
198 (
199 array
200 (
201 Element::CHILDREN => array
202 (
203 'title' => new Text
204 (
205 array
206 (
207 Element::REQUIRED => true,
208
209 'name' => "{$name}[title]",
210 'value' => $pane['title'],
211
212 'data-provides' => 'title'
213 )
214 ),
215
216 'content' => new MultiEditorElement
217 (
218 $editor_id, array
219 (
220 MultiEditorElement::SELECTOR_NAME => "{$name}[editor_id]",
221
222 'name' => "{$name}[content]",
223 'value' => $value,
224
225 'data-provides' => 'content'
226 )
227 )
228 )
229 )
230 );
231
232 return new Element
233 (
234 'div', array
235 (
236 Element::INNER_HTML => $content,
237
238 'class' => 'tab-pane'
239 )
240 );
241 }
242 }