1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | 1× 1× 1× 1× 1132× 1132× 1132× 4× 4× 4× 16× 4× 3739× 3739× 3739× 1813× 1926× 1496× 1495× 1× 430× 3739× 3739× 824× 2915× 1137× 1961× 1961× 1954× 1098× 1137× 1137× | /** * @module montage/core/gate * @requires montage/core/core * @requires montage/core/logger */ var Montage = require("./core").Montage, logger = require("./logger").logger("gate"), Map = require("collections/map"); /** * @class Gate * @extends Montage */ var Gate = exports.Gate = Montage.specialize(/** @lends Gate.prototype # */ { /** * @function * @returns {Gate} A new Gate instance. */ init: { enumerable: false, value: function () { this.reset(); return this; } }, /** * @function * @param {string} delegate The delegate to be initialized. * @returns itself */ initWithDelegate: { enumerable: false, value: function (delegate) { this.reset(); this.delegate = delegate; return this; } }, /** * @function * @param {string} propertyDescriptor The propertyDescriptor to be initialized. * @returns itself */ initWithDescriptor: { enumerable: false, value: function (propertyDescriptor) { var fieldName; this.reset(); for (fieldName in propertyDescriptor) { this.setField(fieldName, propertyDescriptor[fieldName].value); } return this; } }, /** * @type {Property} * @default {number} 0 */ count: { value: 0 }, /** * @type {Property} * @default {string} null */ table: { value: null }, /** * @function * @param {Array} aFieldName The aFieldName array. * @returns !table or table[aFieldName] */ getField: { enumerable: false, value: function (aFieldName) { var table = this.table; return !table || table[aFieldName]; } }, /** * @function * @param {Array} aFieldName The aFieldName array. * @param {number} value The count on the array. */ setField: { enumerable: false, value: function (aFieldName, value) { var table = this.table || (this.table = new Map()), fieldValue, oldCount = this.count; fieldValue = table[aFieldName]; if (typeof fieldValue === "undefined" && !value) { // new field this.count++; } else if (typeof fieldValue !== "undefined" && fieldValue !== value) { if (value) { this.count--; } else { this.count++; } } else Iif (value && logger.isDebug) { logger.debug(this, aFieldName + " was not set before."); } table[aFieldName] = !!value; if (this.count === 0 && oldCount > 0) { this.callDelegateMethod(true); } else if (oldCount === 0 && this.count > 0) { this.callDelegateMethod(false); } } }, /** * @function * @param {Array} aFieldName The aFieldName array to be removed. */ removeField: { enumerable: false, value: function (aFieldName) { var table = this.table, fieldValue = table[aFieldName]; if (typeof fieldValue !== "undefined" && !fieldValue) { // if the value was false decrement the count this.count--; } delete table[aFieldName]; } }, /** * @type {Property} * @default {string} null */ delegate: { enumerable: false, value: null }, /** * @function * @param {number} value The value to be called. */ callDelegateMethod: { value: function (value) { var delegateMethod; if (this.delegate && typeof (delegateMethod = this.delegate["gateDidBecome" + (value ? "True" : "False")]) === "function") { delegateMethod.call(this.delegate, this); } }, enumerable: false }, /** * @type {Function} * @returns this.count === 0 */ value: { enumerable: false, get: function () { return this.count === 0; } }, /** * @function */ reset: { enumerable: false, value: function () { this.table = {}; this.count = 0; } }, /** * @function * @returns {string} result */ toString: { value: function () { var fieldNames = this._fields, i, iField, result = ""; for (i = 0; (iField = fieldNames[i]); i++) { result += iField + "[" + (this._value & fieldNames[iField]) + "], "; } return result; } } }); |