Debian Codegraph @

closure-library/closure/goog/ui/editor/defaulttoolbar.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Factory functions for creating a default editing toolbar.
17 *
18 * @author [email protected] (Attila Bodis)
19 * @see ../../demos/editor/editor.html
20 */
21
22 goog.provide('goog.ui.editor.ButtonDescriptor');
23 goog.provide('goog.ui.editor.DefaultToolbar');
24
25 goog.require('goog.asserts');
26 goog.require('goog.dom');
27 goog.require('goog.dom.TagName');
28 goog.require('goog.dom.classlist');
29 goog.require('goog.editor.Command');
30 goog.require('goog.style');
31 goog.require('goog.ui.editor.ToolbarFactory');
32 goog.require('goog.ui.editor.messages');
33 goog.require('goog.userAgent');
34
35 // Font menu creation.
36
37
38 /** @desc Font menu item caption for the default sans-serif font. */
39 goog.ui.editor.DefaultToolbar.MSG_FONT_NORMAL = goog.getMsg('Normal');
40
41
42 /** @desc Font menu item caption for the default serif font. */
43 goog.ui.editor.DefaultToolbar.MSG_FONT_NORMAL_SERIF =
44 goog.getMsg('Normal / serif');
45
46
47 /**
48 * Common font descriptors for all locales. Each descriptor has the following
49 * attributes:
50 * <ul>
51 * <li>{@code caption} - Caption to show in the font menu (e.g. 'Tahoma')
52 * <li>{@code value} - Value for the corresponding 'font-family' CSS style
53 * (e.g. 'Tahoma, Arial, sans-serif')
54 * </ul>
55 * @type {!Array<{caption:string, value:string}>}
56 * @private
57 */
58 goog.ui.editor.DefaultToolbar.FONTS_ = [
59 {
60 caption: goog.ui.editor.DefaultToolbar.MSG_FONT_NORMAL,
61 value: 'arial,sans-serif'
62 },
63 {
64 caption: goog.ui.editor.DefaultToolbar.MSG_FONT_NORMAL_SERIF,
65 value: 'times new roman,serif'
66 },
67 {caption: 'Courier New', value: 'courier new,monospace'},
68 {caption: 'Georgia', value: 'georgia,serif'},
69 {caption: 'Trebuchet', value: 'trebuchet ms,sans-serif'},
70 {caption: 'Verdana', value: 'verdana,sans-serif'}
71 ];
72
73
74 /**
75 * Locale-specific font descriptors. The object is a map of locale strings to
76 * arrays of font descriptors.
77 * @type {!Object<!Array<{caption:string, value:string}>>}
78 * @private
79 */
80 goog.ui.editor.DefaultToolbar.I18N_FONTS_ = {
81 'ja': [
82 {
83 caption: '\uff2d\uff33 \uff30\u30b4\u30b7\u30c3\u30af',
84 value: 'ms pgothic,sans-serif'
85 },
86 {caption: '\uff2d\uff33 \uff30\u660e\u671d', value: 'ms pmincho,serif'}, {
87 caption: '\uff2d\uff33 \u30b4\u30b7\u30c3\u30af',
88 value: 'ms gothic,monospace'
89 }
90 ],
91 'ko': [
92 {caption: '\uad74\ub9bc', value: 'gulim,sans-serif'},
93 {caption: '\ubc14\ud0d5', value: 'batang,serif'},
94 {caption: '\uad74\ub9bc\uccb4', value: 'gulimche,monospace'}
95 ],
96 'zh-tw': [
97 {caption: '\u65b0\u7d30\u660e\u9ad4', value: 'pmingliu,serif'},
98 {caption: '\u7d30\u660e\u9ad4', value: 'mingliu,serif'}
99 ],
100 'zh-cn': [
101 {caption: '\u5b8b\u4f53', value: 'simsun,serif'},
102 {caption: '\u9ed1\u4f53', value: 'simhei,sans-serif'},
103 {caption: 'MS Song', value: 'ms song,monospace'}
104 ]
105 };
106
107
108 /**
109 * Default locale for font names.
110 * @type {string}
111 * @private
112 */
113 goog.ui.editor.DefaultToolbar.locale_ = 'en-us';
114
115
116 /**
117 * Sets the locale for the font names. If not set, defaults to 'en-us'.
118 * Used only for default creation of font names name. Must be set
119 * before font name menu is created.
120 * @param {string} locale Locale to use for the toolbar font names.
121 */
122 goog.ui.editor.DefaultToolbar.setLocale = function(locale) {
123 goog.ui.editor.DefaultToolbar.locale_ = locale;
124 };
125
126
127 /**
128 * Initializes the given font menu button by adding default fonts to the menu.
129 * If goog.ui.editor.DefaultToolbar.setLocale was called to specify a locale
130 * for which locale-specific default fonts exist, those are added before
131 * common fonts.
132 * @param {!goog.ui.Select} button Font menu button.
133 */
134 goog.ui.editor.DefaultToolbar.addDefaultFonts = function(button) {
135 // Normalize locale to lowercase, with a hyphen (see bug 1036165).
136 var locale =
137 goog.ui.editor.DefaultToolbar.locale_.replace(/_/, '-').toLowerCase();
138 // Add locale-specific default fonts, if any.
139 var fontlist = [];
140
141 if (locale in goog.ui.editor.DefaultToolbar.I18N_FONTS_) {
142 fontlist = goog.ui.editor.DefaultToolbar.I18N_FONTS_[locale];
143 }
144 if (fontlist.length) {
145 goog.ui.editor.ToolbarFactory.addFonts(button, fontlist);
146 }
147 // Add locale-independent default fonts.
148 goog.ui.editor.ToolbarFactory.addFonts(
149 button, goog.ui.editor.DefaultToolbar.FONTS_);
150 };
151
152
153 // Font size menu creation.
154
155
156 /** @desc Font size menu item caption for the 'Small' size. */
157 goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_SMALL = goog.getMsg('Small');
158
159
160 /** @desc Font size menu item caption for the 'Normal' size. */
161 goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_NORMAL = goog.getMsg('Normal');
162
163
164 /** @desc Font size menu item caption for the 'Large' size. */
165 goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_LARGE = goog.getMsg('Large');
166
167
168 /** @desc Font size menu item caption for the 'Huge' size. */
169 goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_HUGE = goog.getMsg('Huge');
170
171
172 /**
173 * Font size descriptors, each with the following attributes:
174 * <ul>
175 * <li>{@code caption} - Caption to show in the font size menu (e.g. 'Huge')
176 * <li>{@code value} - Value for the corresponding HTML font size (e.g. 6)
177 * </ul>
178 * @type {!Array<{caption:string, value:number}>}
179 * @private
180 */
181 goog.ui.editor.DefaultToolbar.FONT_SIZES_ = [
182 {caption: goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_SMALL, value: 1},
183 {caption: goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_NORMAL, value: 2},
184 {caption: goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_LARGE, value: 4},
185 {caption: goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_HUGE, value: 6}
186 ];
187
188
189 /**
190 * Initializes the given font size menu button by adding default font sizes to
191 * it.
192 * @param {!goog.ui.Select} button Font size menu button.
193 */
194 goog.ui.editor.DefaultToolbar.addDefaultFontSizes = function(button) {
195 goog.ui.editor.ToolbarFactory.addFontSizes(
196 button, goog.ui.editor.DefaultToolbar.FONT_SIZES_);
197 };
198
199
200 // Header format menu creation.
201
202
203 /** @desc Caption for "Heading" block format option. */
204 goog.ui.editor.DefaultToolbar.MSG_FORMAT_HEADING = goog.getMsg('Heading');
205
206
207 /** @desc Caption for "Subheading" block format option. */
208 goog.ui.editor.DefaultToolbar.MSG_FORMAT_SUBHEADING = goog.getMsg('Subheading');
209
210
211 /** @desc Caption for "Minor heading" block format option. */
212 goog.ui.editor.DefaultToolbar.MSG_FORMAT_MINOR_HEADING =
213 goog.getMsg('Minor heading');
214
215
216 /** @desc Caption for "Normal" block format option. */
217 goog.ui.editor.DefaultToolbar.MSG_FORMAT_NORMAL = goog.getMsg('Normal');
218
219
220 /**
221 * Format option descriptors, each with the following attributes:
222 * <ul>
223 * <li>{@code caption} - Caption to show in the menu (e.g. 'Minor heading')
224 * <li>{@code command} - Corresponding {@link goog.dom.TagName} (e.g.
225 * 'H4')
226 * </ul>
227 * @type {!Array<{caption: string, command: !goog.dom.TagName}>}
228 * @private
229 */
230 goog.ui.editor.DefaultToolbar.FORMAT_OPTIONS_ = [
231 {
232 caption: goog.ui.editor.DefaultToolbar.MSG_FORMAT_HEADING,
233 command: goog.dom.TagName.H2
234 },
235 {
236 caption: goog.ui.editor.DefaultToolbar.MSG_FORMAT_SUBHEADING,
237 command: goog.dom.TagName.H3
238 },
239 {
240 caption: goog.ui.editor.DefaultToolbar.MSG_FORMAT_MINOR_HEADING,
241 command: goog.dom.TagName.H4
242 },
243 {
244 caption: goog.ui.editor.DefaultToolbar.MSG_FORMAT_NORMAL,
245 command: goog.dom.TagName.P
246 }
247 ];
248
249
250 /**
251 * Initializes the given "Format block" menu button by adding default format
252 * options to the menu.
253 * @param {!goog.ui.Select} button "Format block" menu button.
254 */
255 goog.ui.editor.DefaultToolbar.addDefaultFormatOptions = function(button) {
256 goog.ui.editor.ToolbarFactory.addFormatOptions(
257 button, goog.ui.editor.DefaultToolbar.FORMAT_OPTIONS_);
258 };
259
260
261 /**
262 * Creates a {@link goog.ui.Toolbar} containing a default set of editor
263 * toolbar buttons, and renders it into the given parent element.
264 * @param {!Element} elem Toolbar parent element.
265 * @param {boolean=} opt_isRightToLeft Whether the editor chrome is
266 * right-to-left; defaults to the directionality of the toolbar parent
267 * element.
268 * @return {!goog.ui.Toolbar} Default editor toolbar, rendered into the given
269 * parent element.
270 * @see goog.ui.editor.DefaultToolbar.DEFAULT_BUTTONS
271 */
272 goog.ui.editor.DefaultToolbar.makeDefaultToolbar = function(
273 elem, opt_isRightToLeft) {
274 var isRightToLeft = opt_isRightToLeft || goog.style.isRightToLeft(elem);
275 var buttons = isRightToLeft ?
276 goog.ui.editor.DefaultToolbar.DEFAULT_BUTTONS_RTL :
277 goog.ui.editor.DefaultToolbar.DEFAULT_BUTTONS;
278 return goog.ui.editor.DefaultToolbar.makeToolbar(
279 buttons, elem, opt_isRightToLeft);
280 };
281
282
283 /**
284 * Creates a {@link goog.ui.Toolbar} containing the specified set of
285 * toolbar buttons, and renders it into the given parent element. Each
286 * item in the {@code items} array must either be a
287 * {@link goog.editor.Command} (to create a built-in button) or a subclass
288 * of {@link goog.ui.Control} (to create a custom control).
289 * @param {!Array<string|goog.ui.Control>} items Toolbar items; each must
290 * be a {@link goog.editor.Command} or a {@link goog.ui.Control}.
291 * @param {!Element} elem Toolbar parent element.
292 * @param {boolean=} opt_isRightToLeft Whether the editor chrome is
293 * right-to-left; defaults to the directionality of the toolbar parent
294 * element.
295 * @return {!goog.ui.Toolbar} Editor toolbar, rendered into the given parent
296 * element.
297 */
298 goog.ui.editor.DefaultToolbar.makeToolbar = function(
299 items, elem, opt_isRightToLeft) {
300 var domHelper = goog.dom.getDomHelper(elem);
301 var controls = [];
302
303 for (var i = 0, button; button = items[i]; i++) {
304 if (goog.isString(button)) {
305 button = goog.ui.editor.DefaultToolbar.makeBuiltInToolbarButton(
306 button, domHelper);
307 }
308 if (button) {
309 controls.push(button);
310 }
311 }
312
313 return goog.ui.editor.ToolbarFactory.makeToolbar(
314 controls, elem, opt_isRightToLeft);
315 };
316
317
318 /**
319 * Creates an instance of a subclass of {@link goog.ui.Button} for the given
320 * {@link goog.editor.Command}, or null if no built-in button exists for the
321 * command. Note that this function is only intended to create built-in
322 * buttons; please don't try to hack it!
323 * @param {string} command Editor command ID.
324 * @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
325 * creation; defaults to the current document if unspecified.
326 * @return {goog.ui.Button} Toolbar button (null if no built-in button exists
327 * for the command).
328 */
329 goog.ui.editor.DefaultToolbar.makeBuiltInToolbarButton = function(
330 command, opt_domHelper) {
331 var button;
332 var descriptor = goog.ui.editor.DefaultToolbar.buttons_[command];
333 if (descriptor) {
334 // Default the factory method to makeToggleButton, since most built-in
335 // toolbar buttons are toggle buttons. See also
336 // goog.ui.editor.DefaultToolbar.button_list_.
337 var factory =
338 descriptor.factory || goog.ui.editor.ToolbarFactory.makeToggleButton;
339 var id = descriptor.command;
340 var tooltip = descriptor.tooltip;
341 var caption = descriptor.caption;
342 var classNames = descriptor.classes;
343 // Default the DOM helper to the one for the current document.
344 var domHelper = opt_domHelper || goog.dom.getDomHelper();
345 // Instantiate the button based on the descriptor.
346 button = factory(id, tooltip, caption, classNames, null, domHelper);
347 // If this button's state should be queried when updating the toolbar,
348 // set the button object's queryable property to true.
349 if (descriptor.queryable) {
350 button.queryable = true;
351 }
352 }
353 return button;
354 };
355
356
357 /**
358 * A set of built-in buttons to display in the default editor toolbar.
359 * @type {!Array<string>}
360 */
361 goog.ui.editor.DefaultToolbar.DEFAULT_BUTTONS = [
362 goog.editor.Command.IMAGE, goog.editor.Command.LINK, goog.editor.Command.BOLD,
363 goog.editor.Command.ITALIC, goog.editor.Command.UNORDERED_LIST,
364 goog.editor.Command.FONT_COLOR, goog.editor.Command.FONT_FACE,
365 goog.editor.Command.FONT_SIZE, goog.editor.Command.JUSTIFY_LEFT,
366 goog.editor.Command.JUSTIFY_CENTER, goog.editor.Command.JUSTIFY_RIGHT,
367 goog.editor.Command.EDIT_HTML
368 ];
369
370
371 /**
372 * A set of built-in buttons to display in the default editor toolbar when
373 * the editor chrome is right-to-left (BiDi mode only).
374 * @type {!Array<string>}
375 */
376 goog.ui.editor.DefaultToolbar.DEFAULT_BUTTONS_RTL = [
377 goog.editor.Command.IMAGE, goog.editor.Command.LINK, goog.editor.Command.BOLD,
378 goog.editor.Command.ITALIC, goog.editor.Command.UNORDERED_LIST,
379 goog.editor.Command.FONT_COLOR, goog.editor.Command.FONT_FACE,
380 goog.editor.Command.FONT_SIZE, goog.editor.Command.JUSTIFY_RIGHT,
381 goog.editor.Command.JUSTIFY_CENTER, goog.editor.Command.JUSTIFY_LEFT,
382 goog.editor.Command.DIR_RTL, goog.editor.Command.DIR_LTR,
383 goog.editor.Command.EDIT_HTML
384 ];
385
386
387 /**
388 * Creates a toolbar button with the given ID, tooltip, and caption. Applies
389 * any custom CSS class names to the button's caption element. This button
390 * is designed to be used as the RTL button.
391 * @param {string} id Button ID; must equal a {@link goog.editor.Command} for
392 * built-in buttons, anything else for custom buttons.
393 * @param {string} tooltip Tooltip to be shown on hover.
394 * @param {goog.ui.ControlContent} caption Button caption.
395 * @param {string=} opt_classNames CSS class name(s) to apply to the caption
396 * element.
397 * @param {goog.ui.ButtonRenderer=} opt_renderer Button renderer; defaults to
398 * {@link goog.ui.ToolbarButtonRenderer} if unspecified.
399 * @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
400 * creation; defaults to the current document if unspecified.
401 * @return {!goog.ui.Button} A toolbar button.
402 * @private
403 */
404 goog.ui.editor.DefaultToolbar.rtlButtonFactory_ = function(
405 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
406 var button = goog.ui.editor.ToolbarFactory.makeToggleButton(
407 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);
408 button.updateFromValue = function(value) {
409 // Enable/disable right-to-left text editing mode in the toolbar.
410 var isRtl = !!value;
411 // Enable/disable a marker class on the toolbar's root element; the rest is
412 // done using CSS scoping in editortoolbar.css. This changes
413 // direction-senitive toolbar icons (like indent/outdent)
414 goog.dom.classlist.enable(
415 goog.asserts.assert(button.getParent().getElement()),
416 goog.getCssName('tr-rtl-mode'), isRtl);
417 button.setChecked(isRtl);
418 };
419 return button;
420 };
421
422
423 /**
424 * Creates a toolbar button with the given ID, tooltip, and caption. Applies
425 * any custom CSS class names to the button's caption element. Designed to
426 * be used to create undo and redo buttons.
427 * @param {string} id Button ID; must equal a {@link goog.editor.Command} for
428 * built-in buttons, anything else for custom buttons.
429 * @param {string} tooltip Tooltip to be shown on hover.
430 * @param {goog.ui.ControlContent} caption Button caption.
431 * @param {string=} opt_classNames CSS class name(s) to apply to the caption
432 * element.
433 * @param {goog.ui.ButtonRenderer=} opt_renderer Button renderer; defaults to
434 * {@link goog.ui.ToolbarButtonRenderer} if unspecified.
435 * @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
436 * creation; defaults to the current document if unspecified.
437 * @return {!goog.ui.Button} A toolbar button.
438 * @private
439 */
440 goog.ui.editor.DefaultToolbar.undoRedoButtonFactory_ = function(
441 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
442 var button = goog.ui.editor.ToolbarFactory.makeButton(
443 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);
444 button.updateFromValue = function(value) { button.setEnabled(value); };
445 return button;
446 };
447
448
449 /**
450 * Creates a toolbar button with the given ID, tooltip, and caption. Applies
451 * any custom CSS class names to the button's caption element. Used to create
452 * a font face button, filled with default fonts.
453 * @param {string} id Button ID; must equal a {@link goog.editor.Command} for
454 * built-in buttons, anything else for custom buttons.
455 * @param {string} tooltip Tooltip to be shown on hover.
456 * @param {goog.ui.ControlContent} caption Button caption.
457 * @param {string=} opt_classNames CSS class name(s) to apply to the caption
458 * element.
459 * @param {goog.ui.MenuButtonRenderer=} opt_renderer Button renderer; defaults
460 * to {@link goog.ui.ToolbarMenuButtonRenderer} if unspecified.
461 * @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
462 * creation; defaults to the current document if unspecified.
463 * @return {!goog.ui.Button} A toolbar button.
464 * @private
465 */
466 goog.ui.editor.DefaultToolbar.fontFaceFactory_ = function(
467 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
468 var button = goog.ui.editor.ToolbarFactory.makeSelectButton(
469 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);
470 goog.ui.editor.DefaultToolbar.addDefaultFonts(button);
471 button.setDefaultCaption(goog.ui.editor.DefaultToolbar.MSG_FONT_NORMAL);
472 // Font options don't have keyboard accelerators.
473 goog.dom.classlist.add(
474 goog.asserts.assert(button.getMenu().getContentElement()),
475 goog.getCssName('goog-menu-noaccel'));
476
477 // How to update this button's state.
478 button.updateFromValue = function(value) {
479 // Normalize value to null or a non-empty string (sometimes we get
480 // the empty string, sometimes we get false...), extract the substring
481 // up to the first comma to get the primary font name, and normalize
482 // to lowercase. This allows us to map a font spec like "Arial,
483 // Helvetica, sans-serif" to a font menu item.
484 // TODO (attila): Try to make this more robust.
485 var item = null;
486 if (value && value.length > 0) {
487 item = /** @type {goog.ui.MenuItem} */ (
488 button.getMenu().getChild(
489 goog.ui.editor.ToolbarFactory.getPrimaryFont(value)));
490 }
491 var selectedItem = button.getSelectedItem();
492 if (item != selectedItem) {
493 button.setSelectedItem(item);
494 }
495 };
496 return button;
497 };
498
499
500 /**
501 * Creates a toolbar button with the given ID, tooltip, and caption. Applies
502 * any custom CSS class names to the button's caption element. Use to create a
503 * font size button, filled with default font sizes.
504 * @param {string} id Button ID; must equal a {@link goog.editor.Command} for
505 * built-in buttons, anything else for custom buttons.
506 * @param {string} tooltip Tooltip to be shown on hover.
507 * @param {goog.ui.ControlContent} caption Button caption.
508 * @param {string=} opt_classNames CSS class name(s) to apply to the caption
509 * element.
510 * @param {goog.ui.MenuButtonRenderer=} opt_renderer Button renderer; defaults
511 * to {@link goog.ui.ToolbarMebuButtonRenderer} if unspecified.
512 * @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
513 * creation; defaults to the current document if unspecified.
514 * @return {!goog.ui.Button} A toolbar button.
515 * @private
516 */
517 goog.ui.editor.DefaultToolbar.fontSizeFactory_ = function(
518 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
519 var button = goog.ui.editor.ToolbarFactory.makeSelectButton(
520 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);
521 goog.ui.editor.DefaultToolbar.addDefaultFontSizes(button);
522 button.setDefaultCaption(goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_NORMAL);
523 // Font size options don't have keyboard accelerators.
524 goog.dom.classlist.add(
525 goog.asserts.assert(button.getMenu().getContentElement()),
526 goog.getCssName('goog-menu-noaccel'));
527 // How to update this button's state.
528 button.updateFromValue = function(value) {
529 // Webkit pre-534.7 returns a string like '32px' instead of the equivalent
530 // integer, so normalize that first.
531 // NOTE(user): Gecko returns "6" so can't just normalize all
532 // strings, only ones ending in "px".
533 if (goog.isString(value) && goog.style.getLengthUnits(value) == 'px') {
534 value = goog.ui.editor.ToolbarFactory.getLegacySizeFromPx(
535 parseInt(value, 10));
536 }
537 // Normalize value to null or a positive integer (sometimes we get
538 // the empty string, sometimes we get false, or -1 if the above
539 // normalization didn't match to a particular 0-7 size)
540 value = value > 0 ? value : null;
541 if (value != button.getValue()) {
542 button.setValue(value);
543 }
544 };
545 return button;
546 };
547
548
549 /**
550 * Function to update the state of a color menu button.
551 * @param {goog.ui.ToolbarColorMenuButton} button The button to which the
552 * color menu is attached.
553 * @param {number} color Color value to update to.
554 * @private
555 */
556 goog.ui.editor.DefaultToolbar.colorUpdateFromValue_ = function(button, color) {
557 var value = color;
558
559 try {
560 if (goog.userAgent.IE) {
561 // IE returns a number that, converted to hex, is a BGR color.
562 // Convert from decimal to BGR to RGB.
563 var hex = '000000' + value.toString(16);
564 var bgr = hex.substr(hex.length - 6, 6);
565 value =
566 '#' + bgr.substring(4, 6) + bgr.substring(2, 4) + bgr.substring(0, 2);
567 }
568 if (value != button.getValue()) {
569 button.setValue(/** @type {string} */ (value));
570 }
571 } catch (ex) {
572 // TODO(attila): Find out when/why this happens.
573 }
574 };
575
576
577 /**
578 * Creates a toolbar button with the given ID, tooltip, and caption. Applies
579 * any custom CSS class names to the button's caption element. Use to create
580 * a font color button.
581 * @param {string} id Button ID; must equal a {@link goog.editor.Command} for
582 * built-in buttons, anything else for custom buttons.
583 * @param {string} tooltip Tooltip to be shown on hover.
584 * @param {goog.ui.ControlContent} caption Button caption.
585 * @param {string=} opt_classNames CSS class name(s) to apply to the caption
586 * element.
587 * @param {goog.ui.ColorMenuButtonRenderer=} opt_renderer Button renderer;
588 * defaults to {@link goog.ui.ToolbarColorMenuButtonRenderer} if
589 * unspecified.
590 * @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
591 * creation; defaults to the current document if unspecified.
592 * @return {!goog.ui.Button} A toolbar button.
593 * @private
594 */
595 goog.ui.editor.DefaultToolbar.fontColorFactory_ = function(
596 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
597 var button = goog.ui.editor.ToolbarFactory.makeColorMenuButton(
598 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);
599 // Initialize default foreground color.
600 button.setSelectedColor('#000');
601 button.updateFromValue =
602 goog.partial(goog.ui.editor.DefaultToolbar.colorUpdateFromValue_, button);
603 return button;
604 };
605
606
607 /**
608 * Creates a toolbar button with the given ID, tooltip, and caption. Applies
609 * any custom CSS class names to the button's caption element. Use to create
610 * a font background color button.
611 * @param {string} id Button ID; must equal a {@link goog.editor.Command} for
612 * built-in buttons, anything else for custom buttons.
613 * @param {string} tooltip Tooltip to be shown on hover.
614 * @param {goog.ui.ControlContent} caption Button caption.
615 * @param {string=} opt_classNames CSS class name(s) to apply to the caption
616 * element.
617 * @param {goog.ui.ColorMenuButtonRenderer=} opt_renderer Button renderer;
618 * defaults to {@link goog.ui.ToolbarColorMenuButtonRenderer} if
619 * unspecified.
620 * @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
621 * creation; defaults to the current document if unspecified.
622 * @return {!goog.ui.Button} A toolbar button.
623 * @private
624 */
625 goog.ui.editor.DefaultToolbar.backgroundColorFactory_ = function(
626 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
627 var button = goog.ui.editor.ToolbarFactory.makeColorMenuButton(
628 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);
629 // Initialize default background color.
630 button.setSelectedColor('#FFF');
631 button.updateFromValue =
632 goog.partial(goog.ui.editor.DefaultToolbar.colorUpdateFromValue_, button);
633 return button;
634 };
635
636
637 /**
638 * Creates a toolbar button with the given ID, tooltip, and caption. Applies
639 * any custom CSS class names to the button's caption element. Use to create
640 * the format menu, prefilled with default formats.
641 * @param {string} id Button ID; must equal a {@link goog.editor.Command} for
642 * built-in buttons, anything else for custom buttons.
643 * @param {string} tooltip Tooltip to be shown on hover.
644 * @param {goog.ui.ControlContent} caption Button caption.
645 * @param {string=} opt_classNames CSS class name(s) to apply to the caption
646 * element.
647 * @param {goog.ui.MenuButtonRenderer=} opt_renderer Button renderer;
648 * defaults to
649 * {@link goog.ui.ToolbarMenuButtonRenderer} if unspecified.
650 * @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for DOM
651 * creation; defaults to the current document if unspecified.
652 * @return {!goog.ui.Button} A toolbar button.
653 * @private
654 */
655 goog.ui.editor.DefaultToolbar.formatBlockFactory_ = function(
656 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper) {
657 var button = goog.ui.editor.ToolbarFactory.makeSelectButton(
658 id, tooltip, caption, opt_classNames, opt_renderer, opt_domHelper);
659 goog.ui.editor.DefaultToolbar.addDefaultFormatOptions(button);
660 button.setDefaultCaption(goog.ui.editor.DefaultToolbar.MSG_FORMAT_NORMAL);
661 // Format options don't have keyboard accelerators.
662 goog.dom.classlist.add(
663 goog.asserts.assert(button.getMenu().getContentElement()),
664 goog.getCssName('goog-menu-noaccel'));
665 // How to update this button.
666 button.updateFromValue = function(value) {
667 // Normalize value to null or a nonempty string (sometimes we get
668 // the empty string, sometimes we get false...)
669 value = value && value.length > 0 ? value : null;
670 if (value != button.getValue()) {
671 button.setValue(value);
672 }
673 };
674 return button;
675 };
676
677
678 // Messages used for tooltips and captions.
679
680
681 /** @desc Format menu tooltip. */
682 goog.ui.editor.DefaultToolbar.MSG_FORMAT_BLOCK_TITLE = goog.getMsg('Format');
683
684
685 /** @desc Format menu caption. */
686 goog.ui.editor.DefaultToolbar.MSG_FORMAT_BLOCK_CAPTION = goog.getMsg('Format');
687
688
689 /** @desc Undo button tooltip. */
690 goog.ui.editor.DefaultToolbar.MSG_UNDO_TITLE = goog.getMsg('Undo');
691
692
693 /** @desc Redo button tooltip. */
694 goog.ui.editor.DefaultToolbar.MSG_REDO_TITLE = goog.getMsg('Redo');
695
696
697 /** @desc Font menu tooltip. */
698 goog.ui.editor.DefaultToolbar.MSG_FONT_FACE_TITLE = goog.getMsg('Font');
699
700
701 /** @desc Font size menu tooltip. */
702 goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_TITLE = goog.getMsg('Font size');
703
704
705 /** @desc Text foreground color menu tooltip. */
706 goog.ui.editor.DefaultToolbar.MSG_FONT_COLOR_TITLE = goog.getMsg('Text color');
707
708
709 /** @desc Bold button tooltip. */
710 goog.ui.editor.DefaultToolbar.MSG_BOLD_TITLE = goog.getMsg('Bold');
711
712
713 /** @desc Italic button tooltip. */
714 goog.ui.editor.DefaultToolbar.MSG_ITALIC_TITLE = goog.getMsg('Italic');
715
716
717 /** @desc Underline button tooltip. */
718 goog.ui.editor.DefaultToolbar.MSG_UNDERLINE_TITLE = goog.getMsg('Underline');
719
720
721 /** @desc Text background color menu tooltip. */
722 goog.ui.editor.DefaultToolbar.MSG_BACKGROUND_COLOR_TITLE =
723 goog.getMsg('Text background color');
724
725
726 /** @desc Link button tooltip. */
727 goog.ui.editor.DefaultToolbar.MSG_LINK_TITLE =
728 goog.getMsg('Add or remove link');
729
730
731 /** @desc Numbered list button tooltip. */
732 goog.ui.editor.DefaultToolbar.MSG_ORDERED_LIST_TITLE =
733 goog.getMsg('Numbered list');
734
735
736 /** @desc Bullet list button tooltip. */
737 goog.ui.editor.DefaultToolbar.MSG_UNORDERED_LIST_TITLE =
738 goog.getMsg('Bullet list');
739
740
741 /** @desc Outdent button tooltip. */
742 goog.ui.editor.DefaultToolbar.MSG_OUTDENT_TITLE =
743 goog.getMsg('Decrease indent');
744
745
746 /** @desc Indent button tooltip. */
747 goog.ui.editor.DefaultToolbar.MSG_INDENT_TITLE = goog.getMsg('Increase indent');
748
749
750 /** @desc Align left button tooltip. */
751 goog.ui.editor.DefaultToolbar.MSG_ALIGN_LEFT_TITLE = goog.getMsg('Align left');
752
753
754 /** @desc Align center button tooltip. */
755 goog.ui.editor.DefaultToolbar.MSG_ALIGN_CENTER_TITLE =
756 goog.getMsg('Align center');
757
758
759 /** @desc Align right button tooltip. */
760 goog.ui.editor.DefaultToolbar.MSG_ALIGN_RIGHT_TITLE =
761 goog.getMsg('Align right');
762
763
764 /** @desc Justify button tooltip. */
765 goog.ui.editor.DefaultToolbar.MSG_JUSTIFY_TITLE = goog.getMsg('Justify');
766
767
768 /** @desc Remove formatting button tooltip. */
769 goog.ui.editor.DefaultToolbar.MSG_REMOVE_FORMAT_TITLE =
770 goog.getMsg('Remove formatting');
771
772
773 /** @desc Insert image button tooltip. */
774 goog.ui.editor.DefaultToolbar.MSG_IMAGE_TITLE = goog.getMsg('Insert image');
775
776
777 /** @desc Strike through button tooltip. */
778 goog.ui.editor.DefaultToolbar.MSG_STRIKE_THROUGH_TITLE =
779 goog.getMsg('Strikethrough');
780
781
782 /** @desc Left-to-right button tooltip. */
783 goog.ui.editor.DefaultToolbar.MSG_DIR_LTR_TITLE = goog.getMsg('Left-to-right');
784
785
786 /** @desc Right-to-left button tooltip. */
787 goog.ui.editor.DefaultToolbar.MSG_DIR_RTL_TITLE = goog.getMsg('Right-to-left');
788
789
790 /** @desc Blockquote button tooltip. */
791 goog.ui.editor.DefaultToolbar.MSG_BLOCKQUOTE_TITLE = goog.getMsg('Quote');
792
793
794 /** @desc Edit HTML button tooltip. */
795 goog.ui.editor.DefaultToolbar.MSG_EDIT_HTML_TITLE =
796 goog.getMsg('Edit HTML source');
797
798
799 /** @desc Subscript button tooltip. */
800 goog.ui.editor.DefaultToolbar.MSG_SUBSCRIPT = goog.getMsg('Subscript');
801
802
803 /** @desc Superscript button tooltip. */
804 goog.ui.editor.DefaultToolbar.MSG_SUPERSCRIPT = goog.getMsg('Superscript');
805
806
807 /** @desc Edit HTML button caption. */
808 goog.ui.editor.DefaultToolbar.MSG_EDIT_HTML_CAPTION = goog.getMsg('Edit HTML');
809
810
811 /**
812 * Map of {@code goog.editor.Command}s to toolbar button descriptor objects,
813 * each of which has the following attributes:
814 * <ul>
815 * <li>{@code command} - The command corresponding to the
816 * button (mandatory)
817 * <li>{@code tooltip} - Tooltip text (optional); if unspecified, the button
818 * has no hover text
819 * <li>{@code caption} - Caption to display on the button (optional); if
820 * unspecified, the button has no text caption
821 * <li>{@code classes} - CSS class name(s) to be applied to the button's
822 * element when rendered (optional); if unspecified, defaults to
823 * 'tr-icon'
824 * plus 'tr-' followed by the command ID, but without any leading '+'
825 * character (e.g. if the command ID is '+undo', then {@code classes}
826 * defaults to 'tr-icon tr-undo')
827 * <li>{@code factory} - factory function used to create the button, which
828 * must accept {@code id}, {@code tooltip}, {@code caption}, and
829 * {@code classes} as arguments, and must return an instance of
830 * {@link goog.ui.Button} or an appropriate subclass (optional); if
831 * unspecified, defaults to
832 * {@link goog.ui.editor.DefaultToolbar.makeToggleButton},
833 * since most built-in toolbar buttons are toggle buttons
834 * <li>(@code queryable} - Whether the button's state should be queried
835 * when updating the toolbar (optional).
836 * </ul>
837 * Note that this object is only used for creating toolbar buttons for
838 * built-in editor commands; custom buttons aren't listed here. Please don't
839 * try to hack this!
840 * @private {!Object<!goog.ui.editor.ButtonDescriptor>}.
841 */
842 goog.ui.editor.DefaultToolbar.buttons_ = {};
843
844
845 /**
846 * @typedef {{command: string, tooltip: ?string,
847 * caption: ?goog.ui.ControlContent, classes: ?string,
848 * factory: ?function(string, string, goog.ui.ControlContent, ?string,
849 * goog.ui.ButtonRenderer, goog.dom.DomHelper):goog.ui.Button,
850 * queryable:?boolean}}
851 */
852 goog.ui.editor.ButtonDescriptor;
853
854
855 /**
856 * Built-in toolbar button descriptors. See
857 * {@link goog.ui.editor.DefaultToolbar.buttons_} for details on button
858 * descriptor objects. This array is processed at JS parse time; each item is
859 * inserted into {@link goog.ui.editor.DefaultToolbar.buttons_}, and the array
860 * itself is deleted and (hopefully) garbage-collected.
861 * @private {Array<!goog.ui.editor.ButtonDescriptor>}
862 */
863 goog.ui.editor.DefaultToolbar.button_list_ = [
864 {
865 command: goog.editor.Command.UNDO,
866 tooltip: goog.ui.editor.DefaultToolbar.MSG_UNDO_TITLE,
867 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-undo'),
868 factory: goog.ui.editor.DefaultToolbar.undoRedoButtonFactory_,
869 queryable: true
870 },
871 {
872 command: goog.editor.Command.REDO,
873 tooltip: goog.ui.editor.DefaultToolbar.MSG_REDO_TITLE,
874 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-redo'),
875 factory: goog.ui.editor.DefaultToolbar.undoRedoButtonFactory_,
876 queryable: true
877 },
878 {
879 command: goog.editor.Command.FONT_FACE,
880 tooltip: goog.ui.editor.DefaultToolbar.MSG_FONT_FACE_TITLE,
881 classes: goog.getCssName('tr-fontName'),
882 factory: goog.ui.editor.DefaultToolbar.fontFaceFactory_,
883 queryable: true
884 },
885 {
886 command: goog.editor.Command.FONT_SIZE,
887 tooltip: goog.ui.editor.DefaultToolbar.MSG_FONT_SIZE_TITLE,
888 classes: goog.getCssName('tr-fontSize'),
889 factory: goog.ui.editor.DefaultToolbar.fontSizeFactory_,
890 queryable: true
891 },
892 {
893 command: goog.editor.Command.BOLD,
894 tooltip: goog.ui.editor.DefaultToolbar.MSG_BOLD_TITLE,
895 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-bold'),
896 queryable: true
897 },
898 {
899 command: goog.editor.Command.ITALIC,
900 tooltip: goog.ui.editor.DefaultToolbar.MSG_ITALIC_TITLE,
901 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-italic'),
902 queryable: true
903 },
904 {
905 command: goog.editor.Command.UNDERLINE,
906 tooltip: goog.ui.editor.DefaultToolbar.MSG_UNDERLINE_TITLE,
907 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-underline'),
908 queryable: true
909 },
910 {
911 command: goog.editor.Command.FONT_COLOR,
912 tooltip: goog.ui.editor.DefaultToolbar.MSG_FONT_COLOR_TITLE,
913 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-foreColor'),
914 factory: goog.ui.editor.DefaultToolbar.fontColorFactory_,
915 queryable: true
916 },
917 {
918 command: goog.editor.Command.BACKGROUND_COLOR,
919 tooltip: goog.ui.editor.DefaultToolbar.MSG_BACKGROUND_COLOR_TITLE,
920 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-backColor'),
921 factory: goog.ui.editor.DefaultToolbar.backgroundColorFactory_,
922 queryable: true
923 },
924 {
925 command: goog.editor.Command.LINK,
926 tooltip: goog.ui.editor.DefaultToolbar.MSG_LINK_TITLE,
927 caption: goog.ui.editor.messages.MSG_LINK_CAPTION,
928 classes: goog.getCssName('tr-link'),
929 queryable: true
930 },
931 {
932 command: goog.editor.Command.ORDERED_LIST,
933 tooltip: goog.ui.editor.DefaultToolbar.MSG_ORDERED_LIST_TITLE,
934 classes: goog.getCssName('tr-icon') + ' ' +
935 goog.getCssName('tr-insertOrderedList'),
936 queryable: true
937 },
938 {
939 command: goog.editor.Command.UNORDERED_LIST,
940 tooltip: goog.ui.editor.DefaultToolbar.MSG_UNORDERED_LIST_TITLE,
941 classes: goog.getCssName('tr-icon') + ' ' +
942 goog.getCssName('tr-insertUnorderedList'),
943 queryable: true
944 },
945 {
946 command: goog.editor.Command.OUTDENT,
947 tooltip: goog.ui.editor.DefaultToolbar.MSG_OUTDENT_TITLE,
948 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-outdent'),
949 factory: goog.ui.editor.ToolbarFactory.makeButton
950 },
951 {
952 command: goog.editor.Command.INDENT,
953 tooltip: goog.ui.editor.DefaultToolbar.MSG_INDENT_TITLE,
954 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-indent'),
955 factory: goog.ui.editor.ToolbarFactory.makeButton
956 },
957 {
958 command: goog.editor.Command.JUSTIFY_LEFT,
959 tooltip: goog.ui.editor.DefaultToolbar.MSG_ALIGN_LEFT_TITLE,
960 classes:
961 goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-justifyLeft'),
962 queryable: true
963 },
964 {
965 command: goog.editor.Command.JUSTIFY_CENTER,
966 tooltip: goog.ui.editor.DefaultToolbar.MSG_ALIGN_CENTER_TITLE,
967 classes:
968 goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-justifyCenter'),
969 queryable: true
970 },
971 {
972 command: goog.editor.Command.JUSTIFY_RIGHT,
973 tooltip: goog.ui.editor.DefaultToolbar.MSG_ALIGN_RIGHT_TITLE,
974 classes:
975 goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-justifyRight'),
976 queryable: true
977 },
978 {
979 command: goog.editor.Command.JUSTIFY_FULL,
980 tooltip: goog.ui.editor.DefaultToolbar.MSG_JUSTIFY_TITLE,
981 classes:
982 goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-justifyFull'),
983 queryable: true
984 },
985 {
986 command: goog.editor.Command.REMOVE_FORMAT,
987 tooltip: goog.ui.editor.DefaultToolbar.MSG_REMOVE_FORMAT_TITLE,
988 classes:
989 goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-removeFormat'),
990 factory: goog.ui.editor.ToolbarFactory.makeButton
991 },
992 {
993 command: goog.editor.Command.IMAGE,
994 tooltip: goog.ui.editor.DefaultToolbar.MSG_IMAGE_TITLE,
995 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-image'),
996 factory: goog.ui.editor.ToolbarFactory.makeButton
997 },
998 {
999 command: goog.editor.Command.STRIKE_THROUGH,
1000 tooltip: goog.ui.editor.DefaultToolbar.MSG_STRIKE_THROUGH_TITLE,
1001 classes:
1002 goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-strikeThrough'),
1003 queryable: true
1004 },
1005 {
1006 command: goog.editor.Command.SUBSCRIPT,
1007 tooltip: goog.ui.editor.DefaultToolbar.MSG_SUBSCRIPT,
1008 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-subscript'),
1009 queryable: true
1010 },
1011 {
1012 command: goog.editor.Command.SUPERSCRIPT,
1013 tooltip: goog.ui.editor.DefaultToolbar.MSG_SUPERSCRIPT,
1014 classes:
1015 goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-superscript'),
1016 queryable: true
1017 },
1018 {
1019 command: goog.editor.Command.DIR_LTR,
1020 tooltip: goog.ui.editor.DefaultToolbar.MSG_DIR_LTR_TITLE,
1021 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-ltr'),
1022 queryable: true
1023 },
1024 {
1025 command: goog.editor.Command.DIR_RTL,
1026 tooltip: goog.ui.editor.DefaultToolbar.MSG_DIR_RTL_TITLE,
1027 classes: goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-rtl'),
1028 factory: goog.ui.editor.DefaultToolbar.rtlButtonFactory_,
1029 queryable: true
1030 },
1031 {
1032 command: goog.editor.Command.BLOCKQUOTE,
1033 tooltip: goog.ui.editor.DefaultToolbar.MSG_BLOCKQUOTE_TITLE,
1034 classes:
1035 goog.getCssName('tr-icon') + ' ' + goog.getCssName('tr-BLOCKQUOTE'),
1036 queryable: true
1037 },
1038 {
1039 command: goog.editor.Command.FORMAT_BLOCK,
1040 tooltip: goog.ui.editor.DefaultToolbar.MSG_FORMAT_BLOCK_TITLE,
1041 caption: goog.ui.editor.DefaultToolbar.MSG_FORMAT_BLOCK_CAPTION,
1042 classes: goog.getCssName('tr-formatBlock'),
1043 factory: goog.ui.editor.DefaultToolbar.formatBlockFactory_,
1044 queryable: true
1045 },
1046 {
1047 command: goog.editor.Command.EDIT_HTML,
1048 tooltip: goog.ui.editor.DefaultToolbar.MSG_EDIT_HTML_TITLE,
1049 caption: goog.ui.editor.DefaultToolbar.MSG_EDIT_HTML_CAPTION,
1050 classes: goog.getCssName('tr-editHtml'),
1051 factory: goog.ui.editor.ToolbarFactory.makeButton
1052 }
1053 ];
1054
1055
1056 (function() {
1057 // Create the goog.ui.editor.DefaultToolbar.buttons_ map from
1058 // goog.ui.editor.DefaultToolbar.button_list_.
1059 for (var i = 0, button;
1060 button = goog.ui.editor.DefaultToolbar.button_list_[i]; i++) {
1061 goog.ui.editor.DefaultToolbar.buttons_[button.command] = button;
1062 }
1063
1064 // goog.ui.editor.DefaultToolbar.button_list_ is no longer needed
1065 // once the map is ready.
1066 goog.ui.editor.DefaultToolbar.button_list_ = null;
1067 })();