paragraphStyles.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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, runtime, ops */
  25. define("webodf/editor/widgets/paragraphStyles", [
  26. "dijit/form/Select",
  27. "dojox/html/entities",
  28. "webodf/editor/EditorSession"],
  29. function (Select, htmlEntities, EditorSession) {
  30. "use strict";
  31. /**
  32. * @constructor
  33. */
  34. var ParagraphStyles = function (callback) {
  35. var self = this,
  36. editorSession,
  37. select,
  38. defaultStyleUIId = ":default";
  39. this.widget = function () {
  40. return select;
  41. };
  42. /*
  43. * In this widget, we name the default style
  44. * (which is referred to as "" in webodf) as
  45. * ":default". The ":" is disallowed in an NCName, so this
  46. * avoids clashes with other styles.
  47. */
  48. this.value = function () {
  49. var value = select.get('value');
  50. if (value === defaultStyleUIId) {
  51. value = "";
  52. }
  53. return value;
  54. };
  55. this.setValue = function (value) {
  56. if (value === "") {
  57. value = defaultStyleUIId;
  58. }
  59. select.set('value', value, false);
  60. };
  61. // events
  62. this.onAdd = null;
  63. this.onRemove = null;
  64. /*jslint emptyblock: true*/
  65. this.onChange = function () {};
  66. /*jslint emptyblock: false*/
  67. function populateStyles() {
  68. var i, selectionList, availableStyles;
  69. // Populate the Default Style always
  70. selectionList = [{
  71. label: runtime.tr("Default Style"),
  72. value: defaultStyleUIId
  73. }];
  74. availableStyles = editorSession ? editorSession.getAvailableParagraphStyles() : [];
  75. for (i = 0; i < availableStyles.length; i += 1) {
  76. selectionList.push({
  77. label: htmlEntities.encode(availableStyles[i].displayName) || htmlEntities.encode(availableStyles[i].name),
  78. value: availableStyles[i].name
  79. });
  80. }
  81. select.removeOption(select.getOptions());
  82. select.addOption(selectionList);
  83. }
  84. function addStyle(styleInfo) {
  85. var stylens = "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
  86. newStyleElement;
  87. if (styleInfo.family !== 'paragraph') {
  88. return;
  89. }
  90. newStyleElement = editorSession.getParagraphStyleElement(styleInfo.name);
  91. select.addOption({
  92. label: htmlEntities.encode(newStyleElement.getAttributeNS(stylens, 'display-name')),
  93. value: styleInfo.name
  94. });
  95. if (self.onAdd) {
  96. self.onAdd(styleInfo.name);
  97. }
  98. }
  99. function removeStyle(styleInfo) {
  100. if (styleInfo.family !== 'paragraph') {
  101. return;
  102. }
  103. select.removeOption(styleInfo.name);
  104. if (self.onRemove) {
  105. self.onRemove(styleInfo.name);
  106. }
  107. }
  108. function handleCursorMoved(cursor) {
  109. var disabled = cursor.getSelectionType() === ops.OdtCursor.RegionSelection;
  110. select.setAttribute('disabled', disabled);
  111. }
  112. this.setEditorSession = function(session) {
  113. if (editorSession) {
  114. editorSession.unsubscribe(EditorSession.signalCommonStyleCreated, addStyle);
  115. editorSession.unsubscribe(EditorSession.signalCommonStyleDeleted, removeStyle);
  116. editorSession.unsubscribe(EditorSession.signalCursorMoved, handleCursorMoved);
  117. }
  118. editorSession = session;
  119. if (editorSession) {
  120. editorSession.subscribe(EditorSession.signalCommonStyleCreated, addStyle);
  121. editorSession.subscribe(EditorSession.signalCommonStyleDeleted, removeStyle);
  122. editorSession.subscribe(EditorSession.signalCursorMoved, handleCursorMoved);
  123. }
  124. select.setAttribute('disabled', !editorSession);
  125. populateStyles();
  126. };
  127. // init
  128. function init() {
  129. select = new Select({
  130. name: 'ParagraphStyles',
  131. maxHeight: 200,
  132. style: {
  133. width: '100px'
  134. }
  135. });
  136. // prevent browser translation service messing up ids
  137. select.domNode.setAttribute("translate", "no");
  138. select.domNode.classList.add("notranslate");
  139. select.dropDown.domNode.setAttribute("translate", "no");
  140. select.dropDown.domNode.classList.add("notranslate");
  141. populateStyles();
  142. // Call ParagraphStyles's onChange handler every time
  143. // the select's onchange is called, and pass the value
  144. // as reported by ParagraphStyles.value(), because we do not
  145. // want to expose the internal naming like ":default" outside this
  146. // class.
  147. select.onChange = function () {
  148. self.onChange(self.value());
  149. };
  150. return callback(self);
  151. }
  152. init();
  153. };
  154. return ParagraphStyles;
  155. });