Tools.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Copyright (C) 2013 KO GmbH <copyright@kogmbh.com>
  3. *
  4. * @licstart
  5. * This file is part of WebODF.
  6. *
  7. * WebODF is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU Affero General Public License (GNU AGPL)
  9. * as published by the Free Software Foundation, either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * WebODF is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with WebODF. If not, see <http://www.gnu.org/licenses/>.
  19. * @licend
  20. *
  21. * @source: http://www.webodf.org/
  22. * @source: https://github.com/kogmbh/WebODF/
  23. */
  24. /*global define,document,require,ops */
  25. define("webodf/editor/Tools", [
  26. "dojo/ready",
  27. "dijit/MenuItem",
  28. "dijit/DropDownMenu",
  29. "dijit/form/Button",
  30. "dijit/form/DropDownButton",
  31. "dijit/Toolbar",
  32. "webodf/editor/widgets/paragraphAlignment",
  33. "webodf/editor/widgets/simpleStyles",
  34. "webodf/editor/widgets/undoRedoMenu",
  35. "webodf/editor/widgets/toolbarWidgets/currentStyle",
  36. "webodf/editor/widgets/annotation",
  37. "webodf/editor/widgets/editHyperlinks",
  38. "webodf/editor/widgets/imageInserter",
  39. "webodf/editor/widgets/paragraphStylesDialog",
  40. "webodf/editor/widgets/zoomSlider",
  41. "webodf/editor/widgets/aboutDialog",
  42. "webodf/editor/EditorSession"],
  43. function (ready, MenuItem, DropDownMenu, Button, DropDownButton, Toolbar, ParagraphAlignment, SimpleStyles, UndoRedoMenu, CurrentStyle, AnnotationControl, EditHyperlinks, ImageInserter, ParagraphStylesDialog, ZoomSlider, AboutDialog, EditorSession) {
  44. "use strict";
  45. return function Tools(toolbarElementId, args) {
  46. var tr = runtime.tr,
  47. onToolDone = args.onToolDone,
  48. loadOdtFile = args.loadOdtFile,
  49. saveOdtFile = args.saveOdtFile,
  50. close = args.close,
  51. toolbar,
  52. loadButton, saveButton, closeButton, aboutButton,
  53. formatDropDownMenu, formatMenuButton,
  54. paragraphStylesMenuItem, paragraphStylesDialog, simpleStyles, currentStyle,
  55. zoomSlider,
  56. undoRedoMenu,
  57. editorSession,
  58. paragraphAlignment,
  59. imageInserter,
  60. annotationControl,
  61. editHyperlinks,
  62. aboutDialog,
  63. sessionSubscribers = [];
  64. /**
  65. * Creates a tool and installs it, if the enabled flag is set to true.
  66. * Only supports tool classes whose constructor has a single argument which
  67. * is a callback to pass the created widget object to.
  68. * @param {!function(new:Object, function(!Object):undefined)} Tool constructor method of the tool
  69. * @param {!boolean} enabled
  70. * @return {?Object}
  71. */
  72. function createTool(Tool, enabled) {
  73. var tool = null;
  74. if (enabled) {
  75. tool = new Tool(function (widget) {
  76. widget.placeAt(toolbar);
  77. widget.startup();
  78. });
  79. sessionSubscribers.push(tool);
  80. tool.onToolDone = onToolDone;
  81. }
  82. return tool;
  83. }
  84. function handleCursorMoved(cursor) {
  85. var disabled = cursor.getSelectionType() === ops.OdtCursor.RegionSelection;
  86. if (formatMenuButton) {
  87. formatMenuButton.setAttribute('disabled', disabled);
  88. }
  89. }
  90. function setEditorSession(session) {
  91. if (editorSession) {
  92. editorSession.unsubscribe(EditorSession.signalCursorMoved, handleCursorMoved);
  93. }
  94. editorSession = session;
  95. if (editorSession) {
  96. editorSession.subscribe(EditorSession.signalCursorMoved, handleCursorMoved);
  97. }
  98. sessionSubscribers.forEach(function (subscriber) {
  99. subscriber.setEditorSession(editorSession);
  100. });
  101. if (formatMenuButton) {
  102. formatMenuButton.setAttribute('disabled', !editorSession);
  103. }
  104. }
  105. this.setEditorSession = setEditorSession;
  106. /**
  107. * @param {!function(!Error=)} callback, passing an error object in case of error
  108. * @return {undefined}
  109. */
  110. this.destroy = function (callback) {
  111. // TODO:
  112. // 1. We don't want to use `document`
  113. // 2. We would like to avoid deleting all widgets
  114. // under document.body because this might interfere with
  115. // other apps that use the editor not-in-an-iframe,
  116. // but dojo always puts its dialogs below the body,
  117. // so this works for now. Perhaps will be obsoleted
  118. // once we move to a better widget toolkit
  119. var widgets = dijit.findWidgets(document.body);
  120. dojo.forEach(widgets, function(w) {
  121. w.destroyRecursive(false);
  122. });
  123. callback();
  124. };
  125. // init
  126. ready(function () {
  127. toolbar = new Toolbar({}, toolbarElementId);
  128. // About
  129. if (args.aboutEnabled) {
  130. aboutButton = new Button({
  131. label: tr('About WebODF Text Editor'),
  132. showLabel: false,
  133. iconClass: 'webodfeditor-dijitWebODFIcon',
  134. style: {
  135. float: 'left'
  136. }
  137. });
  138. aboutDialog = new AboutDialog(function (dialog) {
  139. aboutButton.onClick = function () {
  140. dialog.startup();
  141. dialog.show();
  142. };
  143. });
  144. aboutDialog.onToolDone = onToolDone;
  145. aboutButton.placeAt(toolbar);
  146. }
  147. // Undo/Redo
  148. undoRedoMenu = createTool(UndoRedoMenu, args.undoRedoEnabled);
  149. // Add annotation
  150. annotationControl = createTool(AnnotationControl, args.annotationsEnabled);
  151. // Simple Style Selector [B, I, U, S]
  152. simpleStyles = createTool(SimpleStyles, args.directTextStylingEnabled);
  153. // Paragraph direct alignment buttons
  154. paragraphAlignment = createTool(ParagraphAlignment, args.directParagraphStylingEnabled);
  155. // Paragraph Style Selector
  156. currentStyle = createTool(CurrentStyle, args.paragraphStyleSelectingEnabled);
  157. // Zoom Level Selector
  158. zoomSlider = createTool(ZoomSlider, args.zoomingEnabled);
  159. // Load
  160. if (loadOdtFile) {
  161. loadButton = new Button({
  162. label: tr('Open'),
  163. showLabel: false,
  164. iconClass: 'dijitIcon dijitIconFolderOpen',
  165. style: {
  166. float: 'left'
  167. },
  168. onClick: function () {
  169. loadOdtFile();
  170. }
  171. });
  172. loadButton.placeAt(toolbar);
  173. }
  174. // Save
  175. if (saveOdtFile) {
  176. saveButton = new Button({
  177. label: tr('Save'),
  178. showLabel: false,
  179. iconClass: 'dijitEditorIcon dijitEditorIconSave',
  180. style: {
  181. float: 'left'
  182. },
  183. onClick: function () {
  184. saveOdtFile();
  185. onToolDone();
  186. }
  187. });
  188. saveButton.placeAt(toolbar);
  189. }
  190. // Format menu
  191. if (args.paragraphStyleEditingEnabled) {
  192. formatDropDownMenu = new DropDownMenu({});
  193. paragraphStylesMenuItem = new MenuItem({
  194. label: tr("Paragraph...")
  195. });
  196. formatDropDownMenu.addChild(paragraphStylesMenuItem);
  197. paragraphStylesDialog = new ParagraphStylesDialog(function (dialog) {
  198. paragraphStylesMenuItem.onClick = function () {
  199. if (editorSession) {
  200. dialog.startup();
  201. dialog.show();
  202. }
  203. };
  204. });
  205. sessionSubscribers.push(paragraphStylesDialog);
  206. paragraphStylesDialog.onToolDone = onToolDone;
  207. formatMenuButton = new DropDownButton({
  208. dropDown: formatDropDownMenu,
  209. disabled: true,
  210. label: tr('Format'),
  211. iconClass: "dijitIconEditTask",
  212. style: {
  213. float: 'left'
  214. }
  215. });
  216. formatMenuButton.placeAt(toolbar);
  217. }
  218. // hyper links
  219. editHyperlinks = createTool(EditHyperlinks, args.hyperlinkEditingEnabled);
  220. // image insertion
  221. imageInserter = createTool(ImageInserter, args.imageInsertingEnabled);
  222. // close button
  223. if (close) {
  224. closeButton = new Button({
  225. label: tr('Close'),
  226. showLabel: false,
  227. iconClass: 'dijitEditorIcon dijitEditorIconCancel',
  228. style: {
  229. float: 'right'
  230. },
  231. onClick: function () {
  232. close();
  233. }
  234. });
  235. closeButton.placeAt(toolbar);
  236. }
  237. setEditorSession(editorSession);
  238. });
  239. };
  240. });