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 core, runtime, gui*/ 26 27 /** 28 * This class allows handling of 'constraints', which are modelled 29 * as a name (string) with a true/false state. 30 * It provides methods to register, set/get, and subscribe to constraint 31 * states. 32 * @constructor 33 */ 34 gui.SessionConstraints = function SessionConstraints() { 35 "use strict"; 36 37 var constraints = {}, 38 constraintNotifier = new core.EventNotifier(); 39 40 /** 41 * Register a constraint. 42 * The default state is `false`. Has no effect if the constraint 43 * already exists. 44 * @param {!string} constraint 45 * @return {undefined} 46 */ 47 function registerConstraint(constraint) { 48 if (!constraints.hasOwnProperty(constraint)) { 49 constraints[constraint] = false; 50 constraintNotifier.register(constraint); 51 } 52 } 53 this.registerConstraint = registerConstraint; 54 55 /** 56 * Subscribe to a constraint's state. 57 * @param {!string} constraint 58 * @param {!Function} callback the only argument of this 59 * callback is a boolean, indicating the new state of the 60 * constraint. 61 * @return {undefined} 62 */ 63 this.subscribe = function (constraint, callback) { 64 registerConstraint(constraint); 65 constraintNotifier.subscribe(constraint, callback); 66 }; 67 68 /** 69 * Unsubscribe a callback from a constraint's 70 * state. 71 * @param {!string} constraint 72 * @param {!Function} callback 73 * @return {undefined} 74 */ 75 this.unsubscribe = function (constraint, callback) { 76 constraintNotifier.unsubscribe(constraint, callback); 77 }; 78 79 /** 80 * Set the enabled/disabled state of a constraint 81 * @param {!string} constraint 82 * @param {!boolean} enabled true if enabled, false if disabled 83 * @return {undefined} 84 */ 85 this.setState = function (constraint, enabled) { 86 runtime.assert(constraints.hasOwnProperty(constraint) === true, "No such constraint"); 87 if (constraints[constraint] !== enabled) { 88 constraints[constraint] = enabled; 89 constraintNotifier.emit(constraint, enabled); 90 } 91 }; 92 93 /** 94 * Returns the enabled/disabled state of a constraint 95 * @param {!string} constraint 96 * @return {!boolean} 97 */ 98 this.getState = function (constraint) { 99 runtime.assert(constraints.hasOwnProperty(constraint) === true, "No such constraint"); 100 return constraints[constraint]; 101 }; 102 }; 103