-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
toolbar.js
295 lines (262 loc) · 6.77 KB
/
toolbar.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* @fileoverview Implements toolbar
* @author NHN FE Development Lab <dl_javascript@nhn.com>
*/
import $ from 'jquery';
import util from 'tui-code-snippet';
import UIController from './uicontroller';
import Button from './button';
import ToolbarItem from './toolbarItem';
import ToolbarDivider from './toolbarDivider';
import ToolbarItemFactory from './toolbarItemFactory';
/**
* Class Toolbar
* @param {EventManager} eventManager - event manager
* @param {ToolbarItem[]} [items=[]] - toolbar items
*/
class Toolbar extends UIController {
/**
* items
* @type {Array}
* @private
*/
_items = [];
/**
* event manager
* @type {EventManager}
* @private
*/
_eventManager;
constructor(eventManager, items = []) {
super({
tagName: 'div',
className: 'tui-editor-defaultUI-toolbar'
});
this._eventManager = eventManager;
this.setItems(items);
this._initEvent(eventManager);
}
/**
* init event
* @param {EventManager} eventManager - event manager
* @private
* @override
*/
_initEvent(eventManager) {
eventManager.listen('stateChange', ev => {
this._items.forEach(item => {
if (item._state) {
if (ev[item._state]) {
item.$el.addClass('active');
} else {
item.$el.removeClass('active');
}
}
});
});
eventManager.listen('changePreviewTabPreview', () => this.disableAllButton());
eventManager.listen('changePreviewTabWrite', () => this.enableAllButton());
eventManager.listen('changeMode', () => this.enableAllButton());
}
/**
* disable all toolbar button
*/
disableAllButton() {
this._items.forEach(item => {
if (item instanceof Button) {
item.disable();
}
});
}
/**
* enable all toolbar button
*/
enableAllButton() {
this._items.forEach(item => {
if (item instanceof Button) {
item.enable();
}
});
}
/**
* get toolbar items
* @returns {ToolbarItem[]} - toolbar items
*/
getItems() {
return this._items.slice(0);
}
/**
* get toolbar item at given index
* @param {number} index - item index
* @returns {ToolbarItem} - toolbar item at the index
*/
getItem(index) {
return this._items[index];
}
/**
* set toolbar items
* @param {ToolbarItem[]} items - toolbar items
*/
setItems(items) {
this.removeAllItems();
items.forEach(this.addItem.bind(this));
}
/**
* add toolbar item
* @param {ToolbarItem|string|object} item - toolbar item
*/
addItem(item) {
this.insertItem(this._items.length, item);
}
/**
* insert toolbar item
* @param {number} index - index at given item inserted
* @param {ToolbarItem|string|object} item - toolbar item
*/
insertItem(index, item) {
if (util.isString(item)) {
item = ToolbarItemFactory.create(item);
} else if (util.isString(item.type)) {
item = ToolbarItemFactory.create(item.type, item.options);
}
const children = this.$el.children();
if (index >= 0 && index < children.length) {
item.$el.insertBefore(children.eq(index));
this._items.splice(index, 0, item);
} else {
item.$el.appendTo(this.$el);
this._items.push(item);
}
item.onCommandHandler = (e, commandName) => this._eventManager.emit('command', commandName);
item.onEventHandler = (e, eventName) => this._eventManager.emit(eventName);
item.on('command', item.onCommandHandler);
item.on('event', item.onEventHandler);
}
/**
* get index of given item
* @param {ToolbarItem} item - toolbar item
* @returns {number} - index of given toolbar item
*/
indexOfItem(item) {
let index;
if (item instanceof ToolbarItem) {
index = this._items.indexOf(item);
} else if (util.isString(item)) {
const itemName = item;
index = this._items.map(itemToTest => itemToTest.getName()).indexOf(itemName);
}
return index;
}
/**
* remove an item
* @param {ToolbarItem|number} item - an toolbar item or index of the item to remove
* @param {boolean} destroy - destroy item or not
* @returns {ToolbarItem|undefined} - removed item
*/
removeItem(item, destroy = true) {
let index;
let removedItem;
if (item instanceof ToolbarItem) {
index = this.indexOfItem(item);
} else {
index = item;
}
if (index >= 0) {
removedItem = this._items.splice(index, 1)[0];
}
if (removedItem) {
if (destroy) {
removedItem.destroy();
} else {
removedItem.off('command', removedItem.onCommandHandler);
removedItem.off('event', removedItem.onEventHandler);
removedItem.$el.detach();
}
}
return removedItem;
}
/**
* remove all toolbar items
*/
removeAllItems() {
while (this._items && this._items.length > 0) {
this.removeItem(0);
}
}
/**
* destroy instance
* @override
*/
destroy() {
this.removeAllItems();
super.destroy();
}
/**
* add button
* @param {Button} button - button instance
* @param {Number} [index] - location the button will be placed
* @deprecated
*/
addButton(button, index) {
if (util.isArray(button)) {
let arrayIndex = button.length - 1;
for (; arrayIndex >= 0; arrayIndex -= 1) {
if (util.isNumber(index)) {
this._addButton(button[arrayIndex], index);
} else {
this._addButton(button);
}
}
} else {
this._addButton(button, index);
}
}
/**
* _addButton
* @param {Button} button - button instance
* @param {Number} index - location the button will be placed
* @private
* @deprecated
*/
_addButton(button, index) {
const $btn = this._setButton(button, index).$el;
if (util.isNumber(index)) {
this.$el.find(`.${Button.className}`).eq(index - 1).before($btn);
} else {
this.$el.append($btn);
}
}
/**
* add divider
* @returns {jQuery} - created divider jquery element
* @deprecated
*/
addDivider() {
const $el = $(`<div class="${ToolbarDivider.className}"></div>`);
this.$el.append($el);
return $el;
}
/**
* _setButton
* @param {Button} button - button instance
* @param {Number} index - location the button will be placed
* @returns {Button} - button instance
* @private
* @deprecated
*/
_setButton(button, index) {
const ev = this._eventManager;
if (!(button instanceof Button)) {
button = new Button(button);
}
button.on('command', (e, commandName) => ev.emit('command', commandName));
button.on('event', (e, eventName) => ev.emit(eventName));
if (util.isNumber(index)) {
this._items.splice(index, 0, button);
} else {
this._items.push(button);
}
return button;
}
}
export default Toolbar;