currentStyle.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/toolbarWidgets/currentStyle",
  26. ["webodf/editor/EditorSession"],
  27. function (EditorSession) {
  28. "use strict";
  29. return function CurrentStyle(callback) {
  30. var self = this,
  31. editorSession,
  32. paragraphStyles;
  33. function selectParagraphStyle(info) {
  34. if (paragraphStyles) {
  35. if (info.type === 'style') {
  36. paragraphStyles.setValue(info.styleName);
  37. }
  38. }
  39. }
  40. function setParagraphStyle() {
  41. if (editorSession) {
  42. editorSession.setCurrentParagraphStyle(paragraphStyles.value());
  43. }
  44. self.onToolDone();
  45. }
  46. function makeWidget(callback) {
  47. require(["webodf/editor/widgets/paragraphStyles"], function (ParagraphStyles) {
  48. var p = new ParagraphStyles(function (pStyles) {
  49. paragraphStyles = pStyles;
  50. paragraphStyles.widget().onChange = setParagraphStyle;
  51. paragraphStyles.setEditorSession(editorSession);
  52. return callback(paragraphStyles.widget());
  53. });
  54. return p; // make sure p is not unused
  55. });
  56. }
  57. this.setEditorSession = function (session) {
  58. if (editorSession) {
  59. editorSession.unsubscribe(EditorSession.signalParagraphChanged, selectParagraphStyle);
  60. }
  61. editorSession = session;
  62. if (paragraphStyles) {
  63. paragraphStyles.setEditorSession(editorSession);
  64. }
  65. if (editorSession) {
  66. editorSession.subscribe(EditorSession.signalParagraphChanged, selectParagraphStyle);
  67. // TODO: selectParagraphStyle(editorSession.getCurrentParagraphStyle());
  68. }
  69. };
  70. /*jslint emptyblock: true*/
  71. this.onToolDone = function () {};
  72. /*jslint emptyblock: false*/
  73. makeWidget(function (widget) {
  74. return callback(widget);
  75. });
  76. };
  77. });