paragraphStyles.js 5.4 KB

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