|
| 1 | +/** |
| 2 | + * Copyright (C) 2010-2014 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 | + |
| 25 | +/*global core, ops, gui, odf, runtime*/ |
| 26 | + |
| 27 | +/** |
| 28 | + * @implements {core.Destroyable} |
| 29 | + * @param {!ops.Session} session |
| 30 | + * @param {!string} inputMemberId |
| 31 | + * @constructor |
| 32 | + */ |
| 33 | +gui.ListController = function ListController(session, inputMemberId) { |
| 34 | + "use strict"; |
| 35 | + var odtDocument = session.getOdtDocument(), |
| 36 | + odfUtils = odf.OdfUtils, |
| 37 | + eventNotifier = new core.EventNotifier([ |
| 38 | + gui.ListController.listStylingChanged |
| 39 | + ]), |
| 40 | + /**@type{!gui.ListStyleSummary}*/ |
| 41 | + lastSignalledSelectionInfo, |
| 42 | + /**@type{!core.LazyProperty.<!gui.ListStyleSummary>}*/ |
| 43 | + cachedSelectionInfo; |
| 44 | + |
| 45 | + /** |
| 46 | + * @param {!ops.OdtCursor|!string} cursorOrId |
| 47 | + * @return {undefined} |
| 48 | + */ |
| 49 | + function onCursorEvent(cursorOrId) { |
| 50 | + var cursorMemberId = (typeof cursorOrId === "string") |
| 51 | + ? cursorOrId : cursorOrId.getMemberId(); |
| 52 | + |
| 53 | + if (cursorMemberId === inputMemberId) { |
| 54 | + cachedSelectionInfo.reset(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return {undefined} |
| 60 | + */ |
| 61 | + function onParagraphStyleModified() { |
| 62 | + // this is reset on paragraph style change due to paragraph styles possibly linking to list styles |
| 63 | + // through the use of the style:list-style-name attribute |
| 64 | + // http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#attribute-style_list-style-name |
| 65 | + cachedSelectionInfo.reset(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param {!{paragraphElement:Element}} args |
| 70 | + * @return {undefined} |
| 71 | + */ |
| 72 | + function onParagraphChanged(args) { |
| 73 | + var cursor = odtDocument.getCursor(inputMemberId), |
| 74 | + p = args.paragraphElement; |
| 75 | + |
| 76 | + if (cursor && odfUtils.getParagraphElement(cursor.getNode()) === p) { |
| 77 | + cachedSelectionInfo.reset(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return {!gui.ListStyleSummary} |
| 83 | + */ |
| 84 | + function getSelectionInfo() { |
| 85 | + var cursor = odtDocument.getCursor(inputMemberId), |
| 86 | + cursorNode = cursor && cursor.getNode(); |
| 87 | + |
| 88 | + return new gui.ListStyleSummary(cursorNode, odtDocument.getRootNode(), odtDocument.getFormatting()); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return {undefined} |
| 93 | + */ |
| 94 | + function emitSelectionChanges() { |
| 95 | + var hasChanged = true, |
| 96 | + newStyleSummary = cachedSelectionInfo.value(); |
| 97 | + |
| 98 | + if (lastSignalledSelectionInfo) { |
| 99 | + hasChanged = lastSignalledSelectionInfo.isNumberedList !== newStyleSummary.isNumberedList || |
| 100 | + lastSignalledSelectionInfo.isBulletedList !== newStyleSummary.isBulletedList; |
| 101 | + } |
| 102 | + |
| 103 | + if (hasChanged) { |
| 104 | + lastSignalledSelectionInfo = newStyleSummary; |
| 105 | + eventNotifier.emit(gui.ListController.listStylingChanged, lastSignalledSelectionInfo); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param {!string} eventid |
| 111 | + * @param {!Function} cb |
| 112 | + * @return {undefined} |
| 113 | + */ |
| 114 | + this.subscribe = function (eventid, cb) { |
| 115 | + eventNotifier.subscribe(eventid, cb); |
| 116 | + }; |
| 117 | + |
| 118 | + /** |
| 119 | + * @param {!string} eventid |
| 120 | + * @param {!Function} cb |
| 121 | + * @return {undefined} |
| 122 | + */ |
| 123 | + this.unsubscribe = function (eventid, cb) { |
| 124 | + eventNotifier.unsubscribe(eventid, cb); |
| 125 | + }; |
| 126 | + |
| 127 | + /** |
| 128 | + * @param {!function(!Error=)} callback |
| 129 | + * @return {undefined} |
| 130 | + */ |
| 131 | + this.destroy = function (callback) { |
| 132 | + odtDocument.unsubscribe(ops.Document.signalCursorAdded, onCursorEvent); |
| 133 | + odtDocument.unsubscribe(ops.Document.signalCursorRemoved, onCursorEvent); |
| 134 | + odtDocument.unsubscribe(ops.Document.signalCursorMoved, onCursorEvent); |
| 135 | + odtDocument.unsubscribe(ops.OdtDocument.signalParagraphChanged, onParagraphChanged); |
| 136 | + odtDocument.unsubscribe(ops.OdtDocument.signalParagraphStyleModified, onParagraphStyleModified); |
| 137 | + odtDocument.unsubscribe(ops.OdtDocument.signalProcessingBatchEnd, emitSelectionChanges); |
| 138 | + callback(); |
| 139 | + }; |
| 140 | + |
| 141 | + /** |
| 142 | + * @return {undefined} |
| 143 | + */ |
| 144 | + function init() { |
| 145 | + odtDocument.subscribe(ops.Document.signalCursorAdded, onCursorEvent); |
| 146 | + odtDocument.subscribe(ops.Document.signalCursorRemoved, onCursorEvent); |
| 147 | + odtDocument.subscribe(ops.Document.signalCursorMoved, onCursorEvent); |
| 148 | + odtDocument.subscribe(ops.OdtDocument.signalParagraphChanged, onParagraphChanged); |
| 149 | + odtDocument.subscribe(ops.OdtDocument.signalParagraphStyleModified, onParagraphStyleModified); |
| 150 | + odtDocument.subscribe(ops.OdtDocument.signalProcessingBatchEnd, emitSelectionChanges); |
| 151 | + |
| 152 | + cachedSelectionInfo = new core.LazyProperty(getSelectionInfo); |
| 153 | + } |
| 154 | + |
| 155 | + init(); |
| 156 | +}; |
| 157 | + |
| 158 | +/**@const*/ |
| 159 | +gui.ListController.listStylingChanged = "listStyling/changed"; |
0 commit comments