1 /**
  2  * Copyright (C) 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, xmldom, odf, runtime*/
 26 
 27 /**
 28  * OpUpdateMember allows you to set and remove
 29  * certain properties.
 30  * 'fullName', 'color', and 'imageUrl' are not
 31  * removable, they will be filtered out of 
 32  * removedProperties if found.
 33  * @constructor
 34  * @implements ops.Operation
 35  */
 36 ops.OpUpdateMember = function OpUpdateMember() {
 37     "use strict";
 38 
 39     var /**@type{string}*/
 40         memberid,
 41         timestamp,
 42         /**@type{ops.MemberProperties}*/
 43         setProperties,
 44         removedProperties;
 45 
 46     /**
 47      * @param {!ops.OpUpdateMember.InitSpec} data
 48      */
 49     this.init = function (data) {
 50         memberid = data.memberid;
 51         timestamp = parseInt(data.timestamp, 10);
 52         setProperties = data.setProperties;
 53         removedProperties = data.removedProperties;
 54     };
 55 
 56     this.isEdit = false;
 57     this.group = undefined;
 58 
 59     /**
 60      * @param {!ops.OdtDocument} doc
 61      */
 62     function updateCreators(doc) {
 63         var xpath = xmldom.XPath,
 64             xp = "//dc:creator[@editinfo:memberid='" + memberid + "']",
 65             creators = xpath.getODFElementsWithXPath(doc.getRootNode(), xp, function (prefix) {
 66                 if (prefix === "editinfo") {
 67                     return "urn:webodf:names:editinfo";
 68                 }
 69                 return odf.Namespaces.lookupNamespaceURI(prefix);
 70             }),
 71             i;
 72 
 73         for (i = 0; i < creators.length; i += 1) {
 74             creators[i].textContent = setProperties.fullName;
 75         }
 76     }
 77 
 78     /**
 79      * @param {!ops.Document} document
 80      */
 81     this.execute = function (document) {
 82         var odtDocument = /**@type{ops.OdtDocument}*/(document),
 83             member = odtDocument.getMember(memberid);
 84         if (!member) {
 85             return false;
 86         }
 87 
 88         if (removedProperties) {
 89             member.removeProperties(removedProperties);
 90         }
 91         if (setProperties) {
 92             member.setProperties(setProperties);
 93             if (setProperties.fullName) {
 94                 updateCreators(odtDocument);
 95             }
 96         }
 97 
 98         odtDocument.emit(ops.Document.signalMemberUpdated, member);
 99         return true;
100     };
101 
102     /**
103      * @return {!ops.OpUpdateMember.Spec}
104      */
105     this.spec = function () {
106         return {
107             optype: "UpdateMember",
108             memberid: memberid,
109             timestamp: timestamp,
110             setProperties: setProperties,
111             removedProperties: removedProperties
112         };
113     };
114 };
115 /**@typedef{{
116     optype:string,
117     memberid:string,
118     timestamp:number,
119     setProperties:?ops.MemberProperties,
120     removedProperties:Object
121  }}*/
122 ops.OpUpdateMember.Spec;
123 /**@typedef{{
124     memberid:string,
125     timestamp:(number|undefined),
126     setProperties:?ops.MemberProperties,
127     removedProperties:Object
128  }}*/
129 ops.OpUpdateMember.InitSpec;
130