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 runtime, odf, ops*/
 26 
 27 /**
 28  * @constructor
 29  * @implements ops.Operation
 30  */
 31 ops.OpAddStyle = function OpAddStyle() {
 32     "use strict";
 33 
 34     var memberid, timestamp,
 35         styleName, styleFamily, isAutomaticStyle,
 36         /**@type{!odf.Formatting.StyleData}*/setProperties,
 37         /** @const */stylens = odf.Namespaces.stylens;
 38 
 39     /**
 40      * @param {!ops.OpAddStyle.InitSpec} data
 41      */
 42     this.init = function (data) {
 43         memberid = data.memberid;
 44         timestamp = data.timestamp;
 45         styleName = data.styleName;
 46         styleFamily = data.styleFamily;
 47         // Input is either from an xml doc or potentially a manually created op
 48         // This means isAutomaticStyles is either a raw string of 'true', or a a native bool
 49         // Can't use Boolean(...) as Boolean('false') === true
 50         isAutomaticStyle = data.isAutomaticStyle === 'true' || data.isAutomaticStyle === true;
 51         setProperties = data.setProperties;
 52     };
 53 
 54     this.isEdit = true;
 55     this.group = undefined;
 56 
 57     /**
 58      * @param {!ops.Document} document
 59      */
 60     this.execute = function (document) {
 61         var odtDocument = /**@type{ops.OdtDocument}*/(document),
 62             odfContainer = odtDocument.getOdfCanvas().odfContainer(),
 63             formatting = odtDocument.getFormatting(),
 64             dom = odtDocument.getDOMDocument(),
 65             styleNode = dom.createElementNS(stylens, 'style:style');
 66 
 67         if (!styleNode) {
 68             return false;
 69         }
 70 
 71         if (setProperties) {
 72             formatting.updateStyle(styleNode, setProperties);
 73         }
 74 
 75         styleNode.setAttributeNS(stylens, 'style:family', styleFamily);
 76         styleNode.setAttributeNS(stylens, 'style:name', styleName);
 77 
 78         if (isAutomaticStyle) {
 79             odfContainer.rootElement.automaticStyles.appendChild(styleNode);
 80         } else {
 81             odfContainer.rootElement.styles.appendChild(styleNode);
 82         }
 83 
 84         odtDocument.getOdfCanvas().refreshCSS();
 85         if (!isAutomaticStyle) {
 86             odtDocument.emit(ops.OdtDocument.signalCommonStyleCreated, {name: styleName, family: styleFamily});
 87         }
 88         return true;
 89     };
 90 
 91     /**
 92      * @return {!ops.OpAddStyle.Spec}
 93      */
 94     this.spec = function () {
 95         return {
 96             optype: "AddStyle",
 97             memberid: memberid,
 98             timestamp: timestamp,
 99             styleName: styleName,
100             styleFamily: styleFamily,
101             isAutomaticStyle: isAutomaticStyle,
102             setProperties: setProperties
103         };
104     };
105 };
106 /**@typedef{{
107     optype:string,
108     memberid:string,
109     timestamp:number,
110     styleName:string,
111     styleFamily:string,
112     isAutomaticStyle:boolean,
113     setProperties:odf.Formatting.StyleData
114 }}*/
115 ops.OpAddStyle.Spec;
116 /**@typedef{{
117     memberid:string,
118     timestamp:(number|undefined),
119     styleName:string,
120     styleFamily:string,
121     isAutomaticStyle:(boolean|string),
122     setProperties:odf.Formatting.StyleData
123 }}*/
124 ops.OpAddStyle.InitSpec;
125