1 /**
  2  * Copyright (C) 2012 KO GmbH <aditya.bhatt@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, runtime*/
 26 
 27 /**
 28  * @constructor
 29  * @implements {core.Destroyable}
 30  * @param {!Element} container
 31  * @param {!ops.OdtDocument} odtDocument
 32  */
 33 ops.EditInfo = function EditInfo(container, odtDocument) {
 34     "use strict";
 35     var /**@type {!Element}*/
 36         editInfoNode,
 37         /**@type {!Object.<!string,{time:!Date}>}*/
 38         editHistory = {};
 39 
 40     /**
 41      * @return {!Array.<{memberid:!string,time:!Date}>}
 42      */
 43     function sortEdits() {
 44         var /**@type {!Array.<{memberid:!string,time:!Date}>}*/
 45             arr = [],
 46             /**@type{string}*/
 47             memberid;
 48         for (memberid in editHistory) {
 49             if (editHistory.hasOwnProperty(memberid)) {
 50                 arr.push({
 51                     'memberid': memberid,
 52                     'time': editHistory[memberid].time
 53                 });
 54             }
 55         }
 56 
 57         arr.sort(function (a, b) {
 58             return a.time - b.time;
 59         });
 60 
 61         return arr;
 62     }
 63 
 64     /**
 65      * @return {!Element}
 66      */
 67     this.getNode = function () {
 68         return editInfoNode;
 69     };
 70 
 71     /**
 72      * @return {!ops.OdtDocument}
 73      */
 74     this.getOdtDocument = function () {
 75         return odtDocument;
 76     };
 77 
 78     /**
 79      * @return {!Object.<!string,{time:!Date}>}
 80      */
 81     this.getEdits = function () {
 82         return editHistory;
 83     };
 84 
 85     /**
 86      * Returns the sorted list of memberid/time pairs, with oldest first.
 87      * @return {!Array.<{memberid:!string,time:!Date}>}
 88      */
 89     this.getSortedEdits = function () {
 90         return sortEdits();
 91     };
 92 
 93     /**
 94      * @param {!string} memberid
 95      * @param {!Date} timestamp
 96      * @return {undefined}
 97      */
 98     this.addEdit = function (memberid, timestamp) {
 99         // log the edit time for this memberid
100         editHistory[memberid] = {
101             time: timestamp
102         };
103     };
104 
105     /**
106      * @return {undefined}
107      */
108     this.clearEdits = function () {
109         editHistory = {};
110     };
111 
112     /**
113      * @param {!function(!Error=)} callback, passing an error object in case of error
114      * @return {undefined}
115      */
116     this.destroy = function (callback) {
117         // TODO: have EditInfo cleaned up if the paragraph is deleted, not happening right now
118         // workaround: check if the container is still in the DOM
119         if (container.parentNode) {
120             container.removeChild(editInfoNode);
121         }
122         callback();
123     };
124 
125     function init() {
126         var editInfons = 'urn:webodf:names:editinfo',
127             dom = odtDocument.getDOMDocument();
128 
129         editInfoNode = dom.createElementNS(editInfons, 'editinfo');
130         container.insertBefore(editInfoNode, container.firstChild);
131     }
132 
133     init();
134 };
135