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, ops, odf*/
 26 
 27 /**
 28  * An editing session and what belongs to it.
 29  * @constructor
 30  * @implements {core.Destroyable}
 31  * @param {!odf.OdfCanvas} odfCanvas
 32  */
 33 ops.Session = function Session(odfCanvas) {
 34     "use strict";
 35     var self = this,
 36         /**@type{!ops.OperationFactory}*/
 37         operationFactory = new ops.OperationFactory(),
 38         /**@type{!ops.OdtDocument}*/
 39         odtDocument = new ops.OdtDocument(odfCanvas),
 40         /**@type{?ops.OperationRouter}*/
 41         operationRouter = null;
 42 
 43     /**
 44      * Forward the router's batch start signal on to the document
 45      * @param {*} args
 46      * @return {undefined}
 47      */
 48     function forwardBatchStart(args) {
 49         odtDocument.emit(ops.OdtDocument.signalProcessingBatchStart, args);
 50     }
 51 
 52     /**
 53      * Forward the router's batch end signal on to the document
 54      * @param {*} args
 55      * @return {undefined}
 56      */
 57     function forwardBatchEnd(args) {
 58         odtDocument.emit(ops.OdtDocument.signalProcessingBatchEnd, args);
 59     }
 60 
 61     /**
 62      * @param {!ops.OperationFactory} opFactory
 63      */
 64     this.setOperationFactory = function (opFactory) {
 65         operationFactory = opFactory;
 66         if (operationRouter) {
 67             operationRouter.setOperationFactory(operationFactory);
 68         }
 69     };
 70 
 71     /**
 72      * @param {!ops.OperationRouter} opRouter
 73      * @return {undefined}
 74      */
 75     this.setOperationRouter = function (opRouter) {
 76         if (operationRouter) {
 77             operationRouter.unsubscribe(ops.OperationRouter.signalProcessingBatchStart, forwardBatchStart);
 78             operationRouter.unsubscribe(ops.OperationRouter.signalProcessingBatchEnd, forwardBatchEnd);
 79         }
 80         operationRouter = opRouter;
 81         operationRouter.subscribe(ops.OperationRouter.signalProcessingBatchStart, forwardBatchStart);
 82         operationRouter.subscribe(ops.OperationRouter.signalProcessingBatchEnd, forwardBatchEnd);
 83         opRouter.setPlaybackFunction(function (op) {
 84             odtDocument.emit(ops.OdtDocument.signalOperationStart, op);
 85             if (op.execute(odtDocument)) {
 86                 odtDocument.emit(ops.OdtDocument.signalOperationEnd, op);
 87                 return true;
 88             }
 89             return false;
 90         });
 91         opRouter.setOperationFactory(operationFactory);
 92     };
 93 
 94     /**
 95      * @return {!ops.OperationFactory}
 96      */
 97     this.getOperationFactory = function () {
 98         return operationFactory;
 99     };
100 
101     /**
102      * @return {!ops.OdtDocument}
103      */
104     this.getOdtDocument = function () {
105         return odtDocument;
106     };
107 
108     /**
109      * Controller sends operations to this method.
110      *
111      * @param {!Array.<!ops.Operation>} ops
112      * @return {undefined}
113      */
114     this.enqueue = function (ops) {
115         operationRouter.push(ops);
116     };
117 
118     /**
119      * @param {!function(!Object=)} callback, passing an error object in case of error
120      * @return {undefined}
121      */
122     this.close = function (callback) {
123         operationRouter.close(function (err) {
124             if (err) {
125                 callback(err);
126             } else {
127                 odtDocument.close(callback);
128             }
129         });
130     };
131 
132     /**
133      * @param {!function(!Error=)} callback, passing an error object in case of error
134      * @return {undefined}
135      */
136     this.destroy = function (callback) {
137         /*
138         operationRouter.destroy(function(err) {
139             if (err) {
140                 callback(err);
141             } else {
142                 memberModel.destroy(function(err) {
143                     if (err) {
144                         callback(err);
145                     } else {
146                         */
147                         odtDocument.destroy(callback);
148                         /*
149                     }
150                 });
151             }
152         });
153         */
154     };
155 
156     /**
157      * @return {undefined}
158      */
159     function init() {
160         self.setOperationRouter(new ops.TrivialOperationRouter());
161     }
162     init();
163 };
164 
165 // vim:expandtab
166