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 
 25 /*global ops, runtime*/
 26 
 27 /**
 28  * Sets the paragraph style name of the specified paragraph.
 29  * The supplied position argument is expected to be the first
 30  * step within the paragraph.
 31  *
 32  * @constructor
 33  * @implements ops.Operation
 34  */
 35 ops.OpSetParagraphStyle = function OpSetParagraphStyle() {
 36     "use strict";
 37 
 38     var memberid, timestamp, position, styleName,
 39         textns = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
 40 
 41     /**
 42      * @param {!ops.OpSetParagraphStyle.InitSpec} data
 43      */
 44     this.init = function (data) {
 45         memberid = data.memberid;
 46         timestamp = data.timestamp;
 47         position = data.position;
 48         styleName = data.styleName;
 49     };
 50 
 51     this.isEdit = true;
 52     this.group = undefined;
 53 
 54     /**
 55      * Returns true if the iterator is set to the first step within the paragraph
 56      *
 57      * @param {!ops.OdtDocument} odtDocument
 58      * @param {!Node} paragraphNode
 59      * @param {!core.PositionIterator} iterator
 60      * @return {!boolean}
 61      */
 62     function isFirstStep(odtDocument, paragraphNode, iterator) {
 63         var filters = [odtDocument.getPositionFilter()],
 64             container = iterator.container(),
 65             offset = iterator.unfilteredDomOffset(),
 66             stepIterator = odtDocument.createStepIterator(container, offset, filters, paragraphNode);
 67 
 68         return stepIterator.previousStep() === false;
 69     }
 70 
 71     /**
 72      * @param {!ops.Document} document
 73      */
 74     this.execute = function (document) {
 75         var odtDocument = /**@type{ops.OdtDocument}*/(document),
 76             iterator,
 77             paragraphNode;
 78 
 79         iterator = odtDocument.getIteratorAtPosition(position);
 80         paragraphNode = odtDocument.getParagraphElement(iterator.container());
 81         if (paragraphNode) {
 82             runtime.assert(isFirstStep(odtDocument, paragraphNode, iterator),
 83                 "SetParagraphStyle position should be the first position in the paragraph");
 84             if (styleName) {
 85                 paragraphNode.setAttributeNS(textns, 'text:style-name', styleName);
 86             } else {
 87                 paragraphNode.removeAttributeNS(textns, 'style-name');
 88             }
 89 
 90             odtDocument.getOdfCanvas().refreshSize();
 91             odtDocument.emit(ops.OdtDocument.signalParagraphChanged, {
 92                 paragraphElement: paragraphNode,
 93                 timeStamp: timestamp,
 94                 memberId: memberid
 95             });
 96 
 97             odtDocument.getOdfCanvas().rerenderAnnotations();
 98             return true;
 99         }
100         return false;
101     };
102 
103     /**
104      * @return {!ops.OpSetParagraphStyle.Spec}
105      */
106     this.spec = function () {
107         return {
108             optype: "SetParagraphStyle",
109             memberid: memberid,
110             timestamp: timestamp,
111             position: position,
112             styleName: styleName
113         };
114     };
115 };
116 /**@typedef{{
117     optype:string,
118     memberid:string,
119     timestamp:number,
120     position:number,
121     styleName:string
122 }}*/
123 ops.OpSetParagraphStyle.Spec;
124 /**@typedef{{
125     memberid:string,
126     timestamp:(number|undefined),
127     position:number,
128     styleName:string
129 }}*/
130 ops.OpSetParagraphStyle.InitSpec;
131