simpleStyles.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * Copyright (C) 2012-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,require,gui,ops */
  25. define("webodf/editor/widgets/simpleStyles", [
  26. "webodf/editor/widgets/fontPicker",
  27. "dijit/form/ToggleButton",
  28. "dijit/form/NumberSpinner",
  29. "webodf/editor/EditorSession"],
  30. function (FontPicker, ToggleButton, NumberSpinner, EditorSession) {
  31. "use strict";
  32. var SimpleStyles = function(callback) {
  33. var self = this,
  34. editorSession,
  35. widget = {},
  36. directFormattingController,
  37. boldButton,
  38. italicButton,
  39. underlineButton,
  40. strikethroughButton,
  41. fontSizeSpinner,
  42. fontPicker,
  43. fontPickerWidget;
  44. boldButton = new ToggleButton({
  45. label: runtime.tr('Bold'),
  46. disabled: true,
  47. showLabel: false,
  48. checked: false,
  49. iconClass: "dijitEditorIcon dijitEditorIconBold",
  50. onChange: function (checked) {
  51. directFormattingController.setBold(checked);
  52. self.onToolDone();
  53. }
  54. });
  55. italicButton = new ToggleButton({
  56. label: runtime.tr('Italic'),
  57. disabled: true,
  58. showLabel: false,
  59. checked: false,
  60. iconClass: "dijitEditorIcon dijitEditorIconItalic",
  61. onChange: function (checked) {
  62. directFormattingController.setItalic(checked);
  63. self.onToolDone();
  64. }
  65. });
  66. underlineButton = new ToggleButton({
  67. label: runtime.tr('Underline'),
  68. disabled: true,
  69. showLabel: false,
  70. checked: false,
  71. iconClass: "dijitEditorIcon dijitEditorIconUnderline",
  72. onChange: function (checked) {
  73. directFormattingController.setHasUnderline(checked);
  74. self.onToolDone();
  75. }
  76. });
  77. strikethroughButton = new ToggleButton({
  78. label: runtime.tr('Strikethrough'),
  79. disabled: true,
  80. showLabel: false,
  81. checked: false,
  82. iconClass: "dijitEditorIcon dijitEditorIconStrikethrough",
  83. onChange: function (checked) {
  84. directFormattingController.setHasStrikethrough(checked);
  85. self.onToolDone();
  86. }
  87. });
  88. fontSizeSpinner = new NumberSpinner({
  89. label: runtime.tr('Size'),
  90. disabled: true,
  91. showLabel: false,
  92. value: 12,
  93. smallDelta: 1,
  94. constraints: {min:6, max:96},
  95. intermediateChanges: true,
  96. onChange: function (value) {
  97. directFormattingController.setFontSize(value);
  98. },
  99. onClick: function () {
  100. self.onToolDone();
  101. },
  102. onInput: function () {
  103. // Do not process any input in the text box;
  104. // even paste events will not be processed
  105. // so that no corrupt values can exist
  106. return false;
  107. }
  108. });
  109. fontPicker = new FontPicker(function () {});
  110. fontPickerWidget = fontPicker.widget();
  111. fontPickerWidget.setAttribute('disabled', true);
  112. fontPickerWidget.onChange = function(value) {
  113. directFormattingController.setFontName(value);
  114. self.onToolDone();
  115. };
  116. widget.children = [boldButton, italicButton, underlineButton, strikethroughButton, fontPickerWidget, fontSizeSpinner];
  117. widget.startup = function () {
  118. widget.children.forEach(function (element) {
  119. element.startup();
  120. });
  121. };
  122. widget.placeAt = function (container) {
  123. widget.children.forEach(function (element) {
  124. element.placeAt(container);
  125. });
  126. return widget;
  127. };
  128. function updateStyleButtons(changes) {
  129. // The 3rd parameter to set(...) is false to avoid firing onChange when setting the value programmatically.
  130. var updateCalls = {
  131. isBold: function(value) { boldButton.set('checked', value, false); },
  132. isItalic: function(value) { italicButton.set('checked', value, false); },
  133. hasUnderline: function(value) { underlineButton.set('checked', value, false); },
  134. hasStrikeThrough: function(value) { strikethroughButton.set('checked', value, false); },
  135. fontSize: function(value) {
  136. fontSizeSpinner.set('intermediateChanges', false); // Necessary due to https://bugs.dojotoolkit.org/ticket/11588
  137. fontSizeSpinner.set('value', value, false);
  138. fontSizeSpinner.set('intermediateChanges', true);
  139. },
  140. fontName: function(value) { fontPickerWidget.set('value', value, false); }
  141. };
  142. Object.keys(changes).forEach(function (key) {
  143. var updateCall = updateCalls[key];
  144. if (updateCall) {
  145. updateCall(changes[key]);
  146. }
  147. });
  148. }
  149. function enableStyleButtons(enabledFeatures) {
  150. widget.children.forEach(function (element) {
  151. element.setAttribute('disabled', !enabledFeatures.directTextStyling);
  152. });
  153. }
  154. function handleCursorMoved(cursor) {
  155. if (directFormattingController.enabledFeatures().directTextStyling) {
  156. var disabled = cursor.getSelectionType() === ops.OdtCursor.RegionSelection;
  157. enableStyleButtons({ directTextStyling: !disabled });
  158. }
  159. }
  160. this.setEditorSession = function(session) {
  161. if (editorSession) {
  162. editorSession.unsubscribe(EditorSession.signalCursorMoved, handleCursorMoved);
  163. directFormattingController.unsubscribe(gui.DirectFormattingController.textStylingChanged, updateStyleButtons);
  164. directFormattingController.unsubscribe(gui.DirectFormattingController.enabledChanged, enableStyleButtons);
  165. }
  166. editorSession = session;
  167. fontPicker.setEditorSession(editorSession);
  168. if (editorSession) {
  169. directFormattingController = editorSession.sessionController.getDirectFormattingController();
  170. editorSession.subscribe(EditorSession.signalCursorMoved, handleCursorMoved);
  171. directFormattingController.subscribe(gui.DirectFormattingController.textStylingChanged, updateStyleButtons);
  172. directFormattingController.subscribe(gui.DirectFormattingController.enabledChanged, enableStyleButtons);
  173. enableStyleButtons(directFormattingController.enabledFeatures());
  174. } else {
  175. enableStyleButtons({ directTextStyling: false});
  176. }
  177. updateStyleButtons({
  178. isBold: editorSession ? directFormattingController.isBold() : false,
  179. isItalic: editorSession ? directFormattingController.isItalic() : false,
  180. hasUnderline: editorSession ? directFormattingController.hasUnderline() : false,
  181. hasStrikeThrough: editorSession ? directFormattingController.hasStrikeThrough() : false,
  182. fontSize: editorSession ? directFormattingController.fontSize() : undefined,
  183. fontName: editorSession ? directFormattingController.fontName() : undefined
  184. });
  185. };
  186. this.onToolDone = function () {};
  187. callback(widget);
  188. };
  189. return SimpleStyles;
  190. });