paragraphAlignment.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 */
  25. define("webodf/editor/widgets/paragraphAlignment", [
  26. "dijit/form/ToggleButton",
  27. "dijit/form/Button",
  28. "webodf/editor/EditorSession"],
  29. function (ToggleButton, Button, EditorSession) {
  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. ];
  113. widget.startup = function () {
  114. widget.children.forEach(function (element) {
  115. element.startup();
  116. });
  117. };
  118. widget.placeAt = function (container) {
  119. widget.children.forEach(function (element) {
  120. element.placeAt(container);
  121. });
  122. return widget;
  123. };
  124. function updateStyleButtons(changes) {
  125. var buttons = {
  126. isAlignedLeft: justifyLeft,
  127. isAlignedCenter: justifyCenter,
  128. isAlignedRight: justifyRight,
  129. isAlignedJustified: justifyFull
  130. };
  131. Object.keys(changes).forEach(function (key) {
  132. var button = buttons[key];
  133. if (button) {
  134. // The 3rd parameter to set(...) is false to avoid firing onChange when setting the value programmatically.
  135. button.set('checked', changes[key], false);
  136. }
  137. });
  138. }
  139. function enableStyleButtons(enabledFeatures) {
  140. widget.children.forEach(function (element) {
  141. element.setAttribute('disabled', !enabledFeatures.directParagraphStyling);
  142. });
  143. }
  144. this.setEditorSession = function (session) {
  145. if (editorSession) {
  146. directFormattingController.unsubscribe(gui.DirectFormattingController.paragraphStylingChanged, updateStyleButtons);
  147. directFormattingController.unsubscribe(gui.DirectFormattingController.enabledChanged, enableStyleButtons);
  148. }
  149. editorSession = session;
  150. if (editorSession) {
  151. directFormattingController = editorSession.sessionController.getDirectFormattingController();
  152. directFormattingController.subscribe(gui.DirectFormattingController.paragraphStylingChanged, updateStyleButtons);
  153. directFormattingController.subscribe(gui.DirectFormattingController.enabledChanged, enableStyleButtons);
  154. enableStyleButtons(directFormattingController.enabledFeatures());
  155. } else {
  156. enableStyleButtons({directParagraphStyling: false});
  157. }
  158. updateStyleButtons({
  159. isAlignedLeft: editorSession ? directFormattingController.isAlignedLeft() : false,
  160. isAlignedCenter: editorSession ? directFormattingController.isAlignedCenter() : false,
  161. isAlignedRight: editorSession ? directFormattingController.isAlignedRight() : false,
  162. isAlignedJustified: editorSession ? directFormattingController.isAlignedJustified() : false
  163. });
  164. };
  165. this.onToolDone = function () {};
  166. callback(widget);
  167. };
  168. return ParagraphAlignment;
  169. });