This repository was archived by the owner on Mar 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 987
/
Copy pathtoolbar.js
300 lines (263 loc) · 10.1 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
296
297
298
299
300
/**
* Toolbar
*
* @param {Object} parent Reference to instance of Editor instance
* @param {Element} container Reference to the toolbar container element
*
* @example
* <div id="toolbar">
* <a data-wysihtml5-command="createLink">insert link</a>
* <a data-wysihtml5-command="formatBlock" data-wysihtml5-command-value="h1">insert h1</a>
* </div>
*
* <script>
* var toolbar = new wysihtml5.toolbar.Toolbar(editor, document.getElementById("toolbar"));
* </script>
*/
(function(wysihtml5) {
var CLASS_NAME_COMMAND_DISABLED = "wysihtml5-command-disabled",
CLASS_NAME_COMMANDS_DISABLED = "wysihtml5-commands-disabled",
CLASS_NAME_COMMAND_ACTIVE = "wysihtml5-command-active",
CLASS_NAME_ACTION_ACTIVE = "wysihtml5-action-active",
dom = wysihtml5.dom;
wysihtml5.toolbar.Toolbar = Base.extend(
/** @scope wysihtml5.toolbar.Toolbar.prototype */ {
constructor: function(editor, container) {
this.editor = editor;
this.container = typeof(container) === "string" ? document.getElementById(container) : container;
this.composer = editor.composer;
this._getLinks("command");
this._getLinks("action");
this._observe();
this.show();
var speechInputLinks = this.container.querySelectorAll("[data-wysihtml5-command=insertSpeech]"),
length = speechInputLinks.length,
i = 0;
for (; i<length; i++) {
new wysihtml5.toolbar.Speech(this, speechInputLinks[i]);
}
},
_getLinks: function(type) {
var links = this[type + "Links"] = wysihtml5.lang.array(this.container.querySelectorAll("[data-wysihtml5-" + type + "]")).get(),
length = links.length,
i = 0,
mapping = this[type + "Mapping"] = {},
link,
group,
name,
value,
dialog;
for (; i<length; i++) {
link = links[i];
name = link.getAttribute("data-wysihtml5-" + type);
value = link.getAttribute("data-wysihtml5-" + type + "-value");
group = this.container.querySelector("[data-wysihtml5-" + type + "-group='" + name + "']");
dialog = this._getDialog(link, name);
mapping[name + ":" + value] = {
link: link,
group: group,
name: name,
value: value,
dialog: dialog,
state: false
};
}
},
_getDialog: function(link, command) {
var that = this,
dialogElement = this.container.querySelector("[data-wysihtml5-dialog='" + command + "']"),
dialog,
caretBookmark;
if (dialogElement) {
dialog = new wysihtml5.toolbar.Dialog(link, dialogElement);
dialog.on("show", function() {
caretBookmark = that.composer.selection.getBookmark();
that.editor.fire("show:dialog", { command: command, dialogContainer: dialogElement, commandLink: link });
});
dialog.on("hide", function() {
that.editor.fire("hide:dialog", { command: command, dialogContainer: dialogElement, commandLink: link });
});
dialog.on("save", function(attributes) {
if (caretBookmark) {
that.composer.selection.setBookmark(caretBookmark);
}
that._execCommand(command, attributes);
that.editor.fire("save:dialog", { command: command, dialogContainer: dialogElement, commandLink: link });
});
dialog.on("cancel", function() {
that.editor.focus(false);
that.editor.fire("cancel:dialog", { command: command, dialogContainer: dialogElement, commandLink: link });
});
}
return dialog;
},
/**
* @example
* var toolbar = new wysihtml5.Toolbar();
* // Insert a <blockquote> element or wrap current selection in <blockquote>
* toolbar.execCommand("formatBlock", "blockquote");
*/
execCommand: function(command, commandValue) {
if (this.commandsDisabled) {
return;
}
var commandObj = this.commandMapping[command + ":" + commandValue];
// Show dialog when available
if (commandObj && commandObj.dialog && !commandObj.state) {
commandObj.dialog.show();
} else {
this._execCommand(command, commandValue);
}
},
_execCommand: function(command, commandValue) {
// Make sure that composer is focussed (false => don't move caret to the end)
this.editor.focus(false);
this.composer.commands.exec(command, commandValue);
this._updateLinkStates();
},
execAction: function(action) {
var editor = this.editor;
if (action === "change_view") {
if (editor.currentView === editor.textarea) {
editor.fire("change_view", "composer");
} else {
editor.fire("change_view", "textarea");
}
}
},
_observe: function() {
var that = this,
editor = this.editor,
container = this.container,
links = this.commandLinks.concat(this.actionLinks),
length = links.length,
i = 0;
for (; i<length; i++) {
// 'javascript:;' and unselectable=on Needed for IE, but done in all browsers to make sure that all get the same css applied
// (you know, a:link { ... } doesn't match anchors with missing href attribute)
if (links[i].nodeName === "A") {
dom.setAttributes({
href: "javascript:;",
unselectable: "on"
}).on(links[i]);
} else {
dom.setAttributes({ unselectable: "on" }).on(links[i]);
}
}
// Needed for opera and chrome
dom.delegate(container, "[data-wysihtml5-command], [data-wysihtml5-action]", "mousedown", function(event) { event.preventDefault(); });
dom.delegate(container, "[data-wysihtml5-command]", "click", function(event) {
var link = this,
command = link.getAttribute("data-wysihtml5-command"),
commandValue = link.getAttribute("data-wysihtml5-command-value");
that.execCommand(command, commandValue);
event.preventDefault();
});
dom.delegate(container, "[data-wysihtml5-action]", "click", function(event) {
var action = this.getAttribute("data-wysihtml5-action");
that.execAction(action);
event.preventDefault();
});
editor.on("focus:composer", function() {
that.bookmark = null;
clearInterval(that.interval);
that.interval = setInterval(function() { that._updateLinkStates(); }, 500);
});
editor.on("blur:composer", function() {
clearInterval(that.interval);
});
editor.on("destroy:composer", function() {
clearInterval(that.interval);
});
editor.on("change_view", function(currentView) {
// Set timeout needed in order to let the blur event fire first
setTimeout(function() {
that.commandsDisabled = (currentView !== "composer");
that._updateLinkStates();
if (that.commandsDisabled) {
dom.addClass(container, CLASS_NAME_COMMANDS_DISABLED);
} else {
dom.removeClass(container, CLASS_NAME_COMMANDS_DISABLED);
}
}, 0);
});
},
_updateLinkStates: function() {
var commandMapping = this.commandMapping,
actionMapping = this.actionMapping,
i,
state,
action,
command;
// every millisecond counts... this is executed quite often
for (i in commandMapping) {
command = commandMapping[i];
if (this.commandsDisabled) {
state = false;
dom.removeClass(command.link, CLASS_NAME_COMMAND_ACTIVE);
if (command.group) {
dom.removeClass(command.group, CLASS_NAME_COMMAND_ACTIVE);
}
if (command.dialog) {
command.dialog.hide();
}
} else {
state = this.composer.commands.state(command.name, command.value);
if (wysihtml5.lang.object(state).isArray()) {
// Grab first and only object/element in state array, otherwise convert state into boolean
// to avoid showing a dialog for multiple selected elements which may have different attributes
// eg. when two links with different href are selected, the state will be an array consisting of both link elements
// but the dialog interface can only update one
state = state.length === 1 ? state[0] : true;
}
dom.removeClass(command.link, CLASS_NAME_COMMAND_DISABLED);
if (command.group) {
dom.removeClass(command.group, CLASS_NAME_COMMAND_DISABLED);
}
}
if (command.state === state) {
continue;
}
command.state = state;
if (state) {
dom.addClass(command.link, CLASS_NAME_COMMAND_ACTIVE);
if (command.group) {
dom.addClass(command.group, CLASS_NAME_COMMAND_ACTIVE);
}
if (command.dialog) {
if (typeof(state) === "object") {
command.dialog.show(state);
} else {
command.dialog.hide();
}
}
} else {
dom.removeClass(command.link, CLASS_NAME_COMMAND_ACTIVE);
if (command.group) {
dom.removeClass(command.group, CLASS_NAME_COMMAND_ACTIVE);
}
if (command.dialog) {
command.dialog.hide();
}
}
}
for (i in actionMapping) {
action = actionMapping[i];
if (action.name === "change_view") {
action.state = this.editor.currentView === this.editor.textarea;
if (action.state) {
dom.addClass(action.link, CLASS_NAME_ACTION_ACTIVE);
} else {
dom.removeClass(action.link, CLASS_NAME_ACTION_ACTIVE);
}
}
}
},
show: function() {
this.container.style.display = "";
},
hide: function() {
this.container.style.display = "none";
}
});
})(wysihtml5);