paragraphAlignment.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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, require, ops, gui, runtime */
  25. define("webodf/editor/widgets/paragraphAlignment", [
  26. "dijit/form/ToggleButton",
  27. "dijit/form/Button",
  28. "webodf/editor/EditorSession"],
  29. function (ToggleButton, Button) {
  30. "use strict";
  31. var ParagraphAlignment = function (callback) {
  32. var self = this,
  33. editorSession,
  34. widget = {},
  35. directFormattingController,
  36. justifyLeft,
  37. justifyCenter,
  38. justifyRight,
  39. justifyFull,
  40. indent,
  41. outdent;
  42. justifyLeft = new ToggleButton({
  43. label: runtime.tr('Align Left'),
  44. disabled: true,
  45. showLabel: false,
  46. checked: false,
  47. iconClass: "dijitEditorIcon dijitEditorIconJustifyLeft",
  48. onChange: function () {
  49. directFormattingController.alignParagraphLeft();
  50. self.onToolDone();
  51. }
  52. });
  53. justifyCenter = new ToggleButton({
  54. label: runtime.tr('Center'),
  55. disabled: true,
  56. showLabel: false,
  57. checked: false,
  58. iconClass: "dijitEditorIcon dijitEditorIconJustifyCenter",
  59. onChange: function () {
  60. directFormattingController.alignParagraphCenter();
  61. self.onToolDone();
  62. }
  63. });
  64. justifyRight = new ToggleButton({
  65. label: runtime.tr('Align Right'),
  66. disabled: true,
  67. showLabel: false,
  68. checked: false,
  69. iconClass: "dijitEditorIcon dijitEditorIconJustifyRight",
  70. onChange: function () {
  71. directFormattingController.alignParagraphRight();
  72. self.onToolDone();
  73. }
  74. });
  75. justifyFull = new ToggleButton({
  76. label: runtime.tr('Justify'),
  77. disabled: true,
  78. showLabel: false,
  79. checked: false,
  80. iconClass: "dijitEditorIcon dijitEditorIconJustifyFull",
  81. onChange: function () {
  82. directFormattingController.alignParagraphJustified();
  83. self.onToolDone();
  84. }
  85. });
  86. outdent = new Button({
  87. label: runtime.tr('Decrease Indent'),
  88. disabled: true,
  89. showLabel: false,
  90. iconClass: "dijitEditorIcon dijitEditorIconOutdent",
  91. onClick: function () {
  92. directFormattingController.outdent();
  93. self.onToolDone();
  94. }
  95. });
  96. indent = new Button({
  97. label: runtime.tr('Increase Indent'),
  98. disabled: true,
  99. showLabel: false,
  100. iconClass: "dijitEditorIcon dijitEditorIconIndent",
  101. onClick: function () {
  102. directFormattingController.indent();
  103. self.onToolDone();
  104. }
  105. });
  106. widget.children = [justifyLeft,
  107. justifyCenter,
  108. justifyRight,
  109. justifyFull,
  110. outdent,
  111. indent ];
  112. widget.startup = function () {
  113. widget.children.forEach(function (element) {
  114. element.startup();
  115. });
  116. };
  117. widget.placeAt = function (container) {
  118. widget.children.forEach(function (element) {
  119. element.placeAt(container);
  120. });
  121. return widget;
  122. };
  123. function updateStyleButtons(changes) {
  124. var buttons = {
  125. isAlignedLeft: justifyLeft,
  126. isAlignedCenter: justifyCenter,
  127. isAlignedRight: justifyRight,
  128. isAlignedJustified: justifyFull
  129. };
  130. Object.keys(changes).forEach(function (key) {
  131. var button = buttons[key];
  132. if (button) {
  133. // The 3rd parameter to set(...) is false to avoid firing onChange when setting the value programmatically.
  134. button.set('checked', changes[key], false);
  135. }
  136. });
  137. }
  138. function enableStyleButtons(enabledFeatures) {
  139. widget.children.forEach(function (element) {
  140. element.setAttribute('disabled', !enabledFeatures.directParagraphStyling);
  141. });
  142. }
  143. this.setEditorSession = function (session) {
  144. if (editorSession) {
  145. directFormattingController.unsubscribe(gui.DirectFormattingController.paragraphStylingChanged, updateStyleButtons);
  146. directFormattingController.unsubscribe(gui.DirectFormattingController.enabledChanged, enableStyleButtons);
  147. }
  148. editorSession = session;
  149. if (editorSession) {
  150. directFormattingController = editorSession.sessionController.getDirectFormattingController();
  151. directFormattingController.subscribe(gui.DirectFormattingController.paragraphStylingChanged, updateStyleButtons);
  152. directFormattingController.subscribe(gui.DirectFormattingController.enabledChanged, enableStyleButtons);
  153. enableStyleButtons(directFormattingController.enabledFeatures());
  154. } else {
  155. enableStyleButtons({directParagraphStyling: false});
  156. }
  157. updateStyleButtons({
  158. isAlignedLeft: editorSession ? directFormattingController.isAlignedLeft() : false,
  159. isAlignedCenter: editorSession ? directFormattingController.isAlignedCenter() : false,
  160. isAlignedRight: editorSession ? directFormattingController.isAlignedRight() : false,
  161. isAlignedJustified: editorSession ? directFormattingController.isAlignedJustified() : false
  162. });
  163. };
  164. /*jslint emptyblock: true*/
  165. this.onToolDone = function () {};
  166. /*jslint emptyblock: false*/
  167. callback(widget);
  168. };
  169. return ParagraphAlignment;
  170. });