Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/ui/editor/toolbarfactory.js
4067 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Generic factory functions for creating the building blocks for
9
* an editor toolbar.
10
*/
11
12
goog.provide('goog.ui.editor.ToolbarFactory');
13
14
goog.require('goog.dom');
15
goog.require('goog.dom.TagName');
16
goog.require('goog.string');
17
goog.require('goog.style');
18
goog.require('goog.ui.Component');
19
goog.require('goog.ui.Container');
20
goog.require('goog.ui.Option');
21
goog.require('goog.ui.Toolbar');
22
goog.require('goog.ui.ToolbarButton');
23
goog.require('goog.ui.ToolbarColorMenuButton');
24
goog.require('goog.ui.ToolbarMenuButton');
25
goog.require('goog.ui.ToolbarRenderer');
26
goog.require('goog.ui.ToolbarSelect');
27
goog.requireType('goog.ui.Button');
28
goog.requireType('goog.ui.ButtonRenderer');
29
goog.requireType('goog.ui.ColorMenuButton');
30
goog.requireType('goog.ui.ColorMenuButtonRenderer');
31
goog.requireType('goog.ui.Control');
32
goog.requireType('goog.ui.ControlContent');
33
goog.requireType('goog.ui.MenuButton');
34
goog.requireType('goog.ui.MenuButtonRenderer');
35
goog.requireType('goog.ui.Select');
36
37
38
/**
39
* Takes a font spec (e.g. "Arial, Helvetica, sans-serif") and returns the
40
* primary font name, normalized to lowercase (e.g. "arial").
41
* @param {string} fontSpec Font specification.
42
* @return {string} The primary font name, in lowercase.
43
*/
44
goog.ui.editor.ToolbarFactory.getPrimaryFont = function(fontSpec) {
45
'use strict';
46
const i = fontSpec.indexOf(',');
47
const fontName =
48
(i != -1 ? fontSpec.substring(0, i) : fontSpec).toLowerCase();
49
// Strip leading/trailing quotes from the font name (bug 1050118).
50
return goog.string.stripQuotes(fontName, '"\'');
51
};
52
53
54
/**
55
* Bulk-adds fonts to the given font menu button. The argument must be an
56
* array of font descriptor objects, each of which must have the following
57
* attributes:
58
* <ul>
59
* <li>`caption` - Caption to show in the font menu (e.g. 'Tahoma')
60
* <li>`value` - Value for the corresponding 'font-family' CSS style
61
* (e.g. 'Tahoma, Arial, sans-serif')
62
* </ul>
63
* @param {!goog.ui.Select} button Font menu button.
64
* @param {!Array<{caption: string, value: string}>} fonts Array of
65
* font descriptors.
66
*/
67
goog.ui.editor.ToolbarFactory.addFonts = function(button, fonts) {
68
'use strict';
69
fonts.forEach(function(font) {
70
'use strict';
71
goog.ui.editor.ToolbarFactory.addFont(button, font.caption, font.value);
72
});
73
};
74
75
76
/**
77
* Adds a menu item to the given font menu button. The first font listed in
78
* the `value` argument is considered the font ID, so adding two items
79
* whose CSS style starts with the same font may lead to unpredictable results.
80
* @param {!goog.ui.Select} button Font menu button.
81
* @param {string} caption Caption to show for the font menu.
82
* @param {string} value Value for the corresponding 'font-family' CSS style.
83
*/
84
goog.ui.editor.ToolbarFactory.addFont = function(button, caption, value) {
85
'use strict';
86
// The font ID is the first font listed in the CSS style, normalized to
87
// lowercase.
88
const id = goog.ui.editor.ToolbarFactory.getPrimaryFont(value);
89
90
// Construct the option, and add it to the button.
91
const option = new goog.ui.Option(caption, value, button.getDomHelper());
92
option.setId(id);
93
button.addItem(option);
94
95
// Captions are shown in their own font.
96
option.getContentElement().style.fontFamily = value;
97
};
98
99
100
/**
101
* Bulk-adds font sizes to the given font size menu button. The argument must
102
* be an array of font size descriptor objects, each of which must have the
103
* following attributes:
104
* <ul>
105
* <li>`caption` - Caption to show in the font size menu (e.g. 'Huge')
106
* <li>`value` - Value for the corresponding HTML font size (e.g. 6)
107
* </ul>
108
* @param {!goog.ui.Select} button Font size menu button.
109
* @param {!Array<{caption: string, value:number}>} sizes Array of font
110
* size descriptors.
111
*/
112
goog.ui.editor.ToolbarFactory.addFontSizes = function(button, sizes) {
113
'use strict';
114
sizes.forEach(function(size) {
115
'use strict';
116
goog.ui.editor.ToolbarFactory.addFontSize(button, size.caption, size.value);
117
});
118
};
119
120
121
/**
122
* Adds a menu item to the given font size menu button. The `value`
123
* argument must be a legacy HTML font size in the 0-7 range.
124
* @param {!goog.ui.Select} button Font size menu button.
125
* @param {string} caption Caption to show in the font size menu.
126
* @param {number} value Value for the corresponding HTML font size.
127
* @suppress {strictMissingProperties} Part of the go/strict_warnings_migration
128
*/
129
goog.ui.editor.ToolbarFactory.addFontSize = function(button, caption, value) {
130
'use strict';
131
// Construct the option, and add it to the button.
132
const option = new goog.ui.Option(caption, value, button.getDomHelper());
133
button.addItem(option);
134
135
// Adjust the font size of the menu item and the height of the checkbox
136
// element after they've been rendered by addItem(). Captions are shown in
137
// the corresponding font size, and lining up the checkbox is tricky.
138
const content = option.getContentElement();
139
content.style.fontSize =
140
goog.ui.editor.ToolbarFactory.getPxFromLegacySize(value) + 'px';
141
content.firstChild.style.height = '1.1em';
142
};
143
144
145
/**
146
* Converts a legacy font size specification into an equivalent pixel size.
147
* For example, {@code &lt;font size="6"&gt;} is {@code font-size: 32px;}, etc.
148
* @param {number} fontSize Legacy font size spec in the 0-7 range.
149
* @return {number} Equivalent pixel size.
150
*/
151
goog.ui.editor.ToolbarFactory.getPxFromLegacySize = function(fontSize) {
152
'use strict';
153
return goog.ui.editor.ToolbarFactory.LEGACY_SIZE_TO_PX_MAP_[fontSize] || 10;
154
};
155
156
157
/**
158
* Converts a pixel font size specification into an equivalent legacy size.
159
* For example, {@code font-size: 32px;} is {@code &lt;font size="6"&gt;}, etc.
160
* If the given pixel size doesn't exactly match one of the legacy sizes, -1 is
161
* returned.
162
* @param {number} px Pixel font size.
163
* @return {number} Equivalent legacy size spec in the 0-7 range, or -1 if none
164
* exists.
165
*/
166
goog.ui.editor.ToolbarFactory.getLegacySizeFromPx = function(px) {
167
'use strict';
168
// Use lastIndexOf to get the largest legacy size matching the pixel size
169
// (most notably returning 1 instead of 0 for 10px).
170
return goog.ui.editor.ToolbarFactory.LEGACY_SIZE_TO_PX_MAP_.lastIndexOf(px);
171
};
172
173
174
/**
175
* Map of legacy font sizes (0-7) to equivalent pixel sizes.
176
* @type {!Array<number>}
177
* @private
178
*/
179
goog.ui.editor.ToolbarFactory.LEGACY_SIZE_TO_PX_MAP_ =
180
[10, 10, 13, 16, 18, 24, 32, 48];
181
182
183
/**
184
* Bulk-adds format options to the given "Format block" menu button. The
185
* argument must be an array of format option descriptor objects, each of
186
* which must have the following attributes:
187
* <ul>
188
* <li>`caption` - Caption to show in the menu (e.g. 'Minor heading')
189
* <li>`command` - Corresponding {@link goog.dom.TagName} (e.g.
190
* 'H4')
191
* </ul>
192
* @param {!goog.ui.Select} button "Format block" menu button.
193
* @param {!Array<{caption: string, command: !goog.dom.TagName}>} formats Array
194
* of format option descriptors.
195
*/
196
goog.ui.editor.ToolbarFactory.addFormatOptions = function(button, formats) {
197
'use strict';
198
formats.forEach(function(format) {
199
'use strict';
200
goog.ui.editor.ToolbarFactory.addFormatOption(
201
button, format.caption, format.command);
202
});
203
};
204
205
206
/**
207
* Adds a menu item to the given "Format block" menu button.
208
* @param {!goog.ui.Select} button "Format block" menu button.
209
* @param {string} caption Caption to show in the menu.
210
* @param {!goog.dom.TagName} tag Corresponding block format tag.
211
*/
212
goog.ui.editor.ToolbarFactory.addFormatOption = function(button, caption, tag) {
213
'use strict';
214
// Construct the option, and add it to the button.
215
// TODO(attila): Create boring but functional menu item for now...
216
const buttonDom = button.getDomHelper();
217
const option = new goog.ui.Option(
218
buttonDom.createDom(goog.dom.TagName.DIV, null, caption), tag, buttonDom);
219
option.setId(String(tag));
220
button.addItem(option);
221
};
222
223
224
/**
225
* Creates a {@link goog.ui.Toolbar} containing the specified set of
226
* toolbar buttons, and renders it into the given parent element. Each
227
* item in the `items` array must a {@link goog.ui.Control}.
228
* @param {!Array<goog.ui.Control>} items Toolbar items; each must
229
* be a {@link goog.ui.Control}.
230
* @param {!Element} elem Toolbar parent element.
231
* @param {boolean=} opt_isRightToLeft Whether the editor chrome is
232
* right-to-left; defaults to the directionality of the toolbar parent
233
* element.
234
* @return {!goog.ui.Toolbar} Editor toolbar, rendered into the given parent
235
* element.
236
*/
237
goog.ui.editor.ToolbarFactory.makeToolbar = function(
238
items, elem, opt_isRightToLeft) {
239
'use strict';
240
const domHelper = goog.dom.getDomHelper(elem);
241
242
// Create an empty horizontal toolbar using the default renderer.
243
const toolbar = new goog.ui.Toolbar(
244
goog.ui.ToolbarRenderer.getInstance(),
245
goog.ui.Container.Orientation.HORIZONTAL, domHelper);
246
247
// Optimization: Explicitly test for the directionality of the parent
248
// element here, so we can set it for both the toolbar and its children,
249
// saving a lot of expensive calls to goog.style.isRightToLeft() during
250
// rendering.
251
const isRightToLeft = opt_isRightToLeft || goog.style.isRightToLeft(elem);
252
toolbar.setRightToLeft(isRightToLeft);
253
254
// Optimization: Set the toolbar to non-focusable before it is rendered,
255
// to avoid creating unnecessary keyboard event handler objects.
256
toolbar.setFocusable(false);
257
258
for (let i = 0, button; button = items[i]; i++) {
259
// Optimization: Set the button to non-focusable before it is rendered,
260
// to avoid creating unnecessary keyboard event handler objects. Also set
261
// the directionality of the button explicitly, to avoid expensive calls
262
// to goog.style.isRightToLeft() during rendering.
263
button.setSupportedState(goog.ui.Component.State.FOCUSED, false);
264
button.setRightToLeft(isRightToLeft);
265
toolbar.addChild(button, true);
266
}
267
268
toolbar.render(elem);
269
return toolbar;
270
};
271
272
273
/**
274
* Creates a toolbar button with the given ID, tooltip, and caption. Applies
275
* any custom CSS class names to the button's caption element.
276
* @param {string} id Button ID; must equal a {@link goog.editor.Command} for
277
* built-in buttons, anything else for custom buttons.
278
* @param {string} tooltip Tooltip to be shown on hover.
279
* @param {goog.ui.ControlContent} caption Button caption.
280
* @param {string=} opt_classNames CSS class name(s) to apply to the caption
281
* element.
282
* @param {goog.ui.ButtonRenderer=} opt_renderer Button renderer; defaults to
283
* {@link goog.ui.ToolbarButtonRenderer} if unspecified.
284
* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
285
* creation; defaults to the current document if unspecified.
286
* @return {!goog.ui.Button} A toolbar button.
287
*/
288
goog.ui.editor.ToolbarFactory.makeButton = function(
289
id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
290
'use strict';
291
const button = new goog.ui.ToolbarButton(
292
goog.ui.editor.ToolbarFactory.createContent_(
293
caption, opt_classNames, opt_domHelper),
294
opt_renderer, opt_domHelper);
295
button.setId(id);
296
button.setTooltip(tooltip);
297
return button;
298
};
299
300
301
/**
302
* Creates a toggle button with the given ID, tooltip, and caption. Applies
303
* any custom CSS class names to the button's caption element. The button
304
* returned has checkbox-like toggle semantics.
305
* @param {string} id Button ID; must equal a {@link goog.editor.Command} for
306
* built-in buttons, anything else for custom buttons.
307
* @param {string} tooltip Tooltip to be shown on hover.
308
* @param {goog.ui.ControlContent} caption Button caption.
309
* @param {string=} opt_classNames CSS class name(s) to apply to the caption
310
* element.
311
* @param {goog.ui.ButtonRenderer=} opt_renderer Button renderer; defaults to
312
* {@link goog.ui.ToolbarButtonRenderer} if unspecified.
313
* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
314
* creation; defaults to the current document if unspecified.
315
* @return {!goog.ui.Button} A toggle button.
316
*/
317
goog.ui.editor.ToolbarFactory.makeToggleButton = function(
318
id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
319
'use strict';
320
const button = goog.ui.editor.ToolbarFactory.makeButton(
321
id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);
322
button.setSupportedState(goog.ui.Component.State.CHECKED, true);
323
return button;
324
};
325
326
327
/**
328
* Creates a menu button with the given ID, tooltip, and caption. Applies
329
* any custom CSS class names to the button's caption element. The button
330
* returned doesn't have an actual menu attached; use {@link
331
* goog.ui.MenuButton#setMenu} to attach a {@link goog.ui.Menu} to the
332
* button.
333
* @param {string} id Button ID; must equal a {@link goog.editor.Command} for
334
* built-in buttons, anything else for custom buttons.
335
* @param {string} tooltip Tooltip to be shown on hover.
336
* @param {goog.ui.ControlContent} caption Button caption.
337
* @param {string=} opt_classNames CSS class name(s) to apply to the caption
338
* element.
339
* @param {goog.ui.ButtonRenderer=} opt_renderer Button renderer; defaults to
340
* {@link goog.ui.ToolbarMenuButtonRenderer} if unspecified.
341
* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
342
* creation; defaults to the current document if unspecified.
343
* @return {!goog.ui.MenuButton} A menu button.
344
*/
345
goog.ui.editor.ToolbarFactory.makeMenuButton = function(
346
id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
347
'use strict';
348
const button = new goog.ui.ToolbarMenuButton(
349
goog.ui.editor.ToolbarFactory.createContent_(
350
caption, opt_classNames, opt_domHelper),
351
null, opt_renderer, opt_domHelper);
352
button.setId(id);
353
button.setTooltip(tooltip);
354
return button;
355
};
356
357
358
/**
359
* Creates a select button with the given ID, tooltip, and caption. Applies
360
* any custom CSS class names to the button's root element. The button
361
* returned doesn't have an actual menu attached; use {@link
362
* goog.ui.Select#setMenu} to attach a {@link goog.ui.Menu} containing
363
* {@link goog.ui.Option}s to the select button.
364
* @param {string} id Button ID; must equal a {@link goog.editor.Command} for
365
* built-in buttons, anything else for custom buttons.
366
* @param {string} tooltip Tooltip to be shown on hover.
367
* @param {goog.ui.ControlContent} caption Button caption; used as the
368
* default caption when nothing is selected.
369
* @param {string=} opt_classNames CSS class name(s) to apply to the button's
370
* root element.
371
* @param {goog.ui.MenuButtonRenderer=} opt_renderer Button renderer;
372
* defaults to {@link goog.ui.ToolbarMenuButtonRenderer} if unspecified.
373
* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
374
* creation; defaults to the current document if unspecified.
375
* @return {!goog.ui.Select} A select button.
376
*/
377
goog.ui.editor.ToolbarFactory.makeSelectButton = function(
378
id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
379
'use strict';
380
const button =
381
new goog.ui.ToolbarSelect(null, null, opt_renderer, opt_domHelper);
382
if (opt_classNames) {
383
// Unlike the other button types, for goog.ui.Select buttons we apply the
384
// extra class names to the root element, because for select buttons the
385
// caption isn't stable (as it changes each time the selection changes).
386
opt_classNames.split(/\s+/).forEach(button.addClassName, button);
387
}
388
button.addClassName(goog.getCssName('goog-toolbar-select'));
389
button.setDefaultCaption(caption);
390
button.setId(id);
391
button.setTooltip(tooltip);
392
return button;
393
};
394
395
396
/**
397
* Creates a color menu button with the given ID, tooltip, and caption.
398
* Applies any custom CSS class names to the button's caption element. The
399
* button is created with a default color menu containing standard color
400
* palettes.
401
* @param {string} id Button ID; must equal a {@link goog.editor.Command} for
402
* built-in toolbar buttons, but can be anything else for custom buttons.
403
* @param {string} tooltip Tooltip to be shown on hover.
404
* @param {goog.ui.ControlContent} caption Button caption.
405
* @param {string=} opt_classNames CSS class name(s) to apply to the caption
406
* element.
407
* @param {goog.ui.ColorMenuButtonRenderer=} opt_renderer Button renderer;
408
* defaults to {@link goog.ui.ToolbarColorMenuButtonRenderer}
409
* if unspecified.
410
* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
411
* creation; defaults to the current document if unspecified.
412
* @return {!goog.ui.ColorMenuButton} A color menu button.
413
*/
414
goog.ui.editor.ToolbarFactory.makeColorMenuButton = function(
415
id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
416
'use strict';
417
const button = new goog.ui.ToolbarColorMenuButton(
418
goog.ui.editor.ToolbarFactory.createContent_(
419
caption, opt_classNames, opt_domHelper),
420
null, opt_renderer, opt_domHelper);
421
button.setId(id);
422
button.setTooltip(tooltip);
423
return button;
424
};
425
426
427
/**
428
* Creates a new DIV that wraps a button caption, optionally applying CSS
429
* class names to it. Used as a helper function in button factory methods.
430
* @param {goog.ui.ControlContent} caption Button caption.
431
* @param {string=} opt_classNames CSS class name(s) to apply to the DIV that
432
* wraps the caption (if any).
433
* @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
434
* creation; defaults to the current document if unspecified.
435
* @return {!Element} DIV that wraps the caption.
436
* @private
437
*/
438
goog.ui.editor.ToolbarFactory.createContent_ = function(
439
caption, opt_classNames, opt_domHelper) {
440
'use strict';
441
return (opt_domHelper || goog.dom.getDomHelper())
442
.createDom(goog.dom.TagName.DIV, opt_classNames, caption);
443
};
444
445