1 /** The minplayer namespace. */ 2 var minplayer = minplayer || {}; 3 4 /** 5 * @constructor 6 * @class This is a class used to keep track of flag states 7 * which is used to control the busy cursor, big play button, among other 8 * items in which multiple components can have an interest in hiding or 9 * showing a single element on the screen. 10 * 11 * <p> 12 * <strong>Usage:</strong> 13 * <pre><code> 14 * // Declare a flags variable. 15 * var flags = new minplayer.flags(); 16 * 17 * // Set the flag based on two components interested in the flag. 18 * flags.setFlag("component1", true); 19 * flags.setFlag("component2", true); 20 * 21 * // Print out the value of the flags. ( Prints 3 ) 22 * console.log(flags.flags); 23 * 24 * // Now unset a single components flag. 25 * flags.setFlag("component1", false); 26 * 27 * // Print out the value of the flags. 28 * console.log(flags.flags); 29 * 30 * // Unset the other components flag. 31 * flags.setFlag("component2", false); 32 * 33 * // Print out the value of the flags. 34 * console.log(flags.flags); 35 * </code></pre> 36 * </p> 37 */ 38 minplayer.flags = function() { 39 40 /** The flag. */ 41 this.flag = 0; 42 43 /** Id map to reference id with the flag index. */ 44 this.ids = {}; 45 46 /** The number of flags. */ 47 this.numFlags = 0; 48 }; 49 50 /** 51 * Sets a flag based on boolean logic operators. 52 * 53 * @param {string} id The id of the controller interested in this flag. 54 * @param {boolean} value The value of this flag ( true or false ). 55 */ 56 minplayer.flags.prototype.setFlag = function(id, value) { 57 58 // Define this id if it isn't present. 59 if (!this.ids.hasOwnProperty(id)) { 60 this.ids[id] = this.numFlags; 61 this.numFlags++; 62 } 63 64 // Use binary operations to keep track of the flag state 65 if (value) { 66 this.flag |= (1 << this.ids[id]); 67 } 68 else { 69 this.flag &= ~(1 << this.ids[id]); 70 } 71 }; 72