PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/packages/closure-library/src/main/resources/com/github/urmuzov/closuremaven/closurelibrarypackage/javascript/goog/ui/editor/toolbarfactory.js

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