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 */
 26 
 27 /*
 28  * create specific operation instances.
 29  */
 30 
 31 
 32 /**
 33  * @constructor
 34  */
 35 ops.OperationFactory = function OperationFactory() {
 36     "use strict";
 37     var /**@type{!Object.<!string, !ops.OperationFactory.SpecConstructor>}*/
 38         specs;
 39 
 40     /**
 41      * @param {!function(new:ops.Operation)} Constructor
 42      * @return {!ops.OperationFactory.SpecConstructor}
 43      */
 44     /*jslint unparam:true*/
 45     function construct(Constructor) {
 46         return function(spec) {
 47             return new Constructor();
 48         };
 49     }
 50     /*jslint unparam:false*/
 51 
 52     /**
 53      * Registers an operation constructor with this operation factory
 54      * @param {!string} specName
 55      * @param {!ops.OperationFactory.SpecConstructor} specConstructor
 56      * @return {undefined}
 57      */
 58     this.register = function (specName, specConstructor) {
 59         specs[specName] = specConstructor;
 60     };
 61 
 62     /**
 63      * Create an instance of an operation based on the provided spec
 64      * @param {!{optype:string}} spec
 65      * @return {ops.Operation}
 66      */
 67     this.create = function (spec) {
 68         var /**@type{ops.Operation}*/
 69             op = null,
 70             constructor = specs[spec.optype];
 71         if (constructor) {
 72             op = constructor(spec);
 73             op.init(spec);
 74         }
 75         return op;
 76     };
 77 
 78     function init() {
 79         specs = {
 80             AddMember: construct(ops.OpAddMember),
 81             UpdateMember: construct(ops.OpUpdateMember),
 82             RemoveMember: construct(ops.OpRemoveMember),
 83             AddCursor: construct(ops.OpAddCursor),
 84             ApplyDirectStyling: construct(ops.OpApplyDirectStyling),
 85             SetBlob: construct(ops.OpSetBlob),
 86             RemoveBlob: construct(ops.OpRemoveBlob),
 87             InsertImage: construct(ops.OpInsertImage),
 88             InsertTable: construct(ops.OpInsertTable),
 89             InsertText: construct(ops.OpInsertText),
 90             RemoveText: construct(ops.OpRemoveText),
 91             MergeParagraph: construct(ops.OpMergeParagraph),
 92             SplitParagraph: construct(ops.OpSplitParagraph),
 93             SetParagraphStyle: construct(ops.OpSetParagraphStyle),
 94             UpdateParagraphStyle: construct(ops.OpUpdateParagraphStyle),
 95             AddStyle: construct(ops.OpAddStyle),
 96             RemoveStyle: construct(ops.OpRemoveStyle),
 97             MoveCursor: construct(ops.OpMoveCursor),
 98             RemoveCursor: construct(ops.OpRemoveCursor),
 99             AddAnnotation: construct(ops.OpAddAnnotation),
100             RemoveAnnotation: construct(ops.OpRemoveAnnotation),
101             UpdateMetadata: construct(ops.OpUpdateMetadata),
102             ApplyHyperlink: construct(ops.OpApplyHyperlink),
103             RemoveHyperlink: construct(ops.OpRemoveHyperlink)
104         };
105     }
106 
107     init();
108 };
109 
110 
111 /**
112  * @typedef {!function(!{optype:!string}):!ops.Operation}
113  */
114 ops.OperationFactory.SpecConstructor;