aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/gate.js
blob: 0910cc46a965055d353a34b5f8b531b7e7fb32a7 (plain)
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
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */
/**
	@module montage/core/gate
    @requires montage/core/core
    @requires montage/core/logger
*/
var Montage = require("montage").Montage,
    logger = require("core/logger").logger("gate");
/**
 @class module:montage/core/gate.Gate
 @extends module:montage/core/core.Montage
 */
var Gate = exports.Gate = Montage.create(Montage,/** @lends module:montage/core/gate.Gate# */ {
/**
    @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,
                fieldValue,
                oldCount = this.count;

            table = (!table ? this.table = {} : table);

            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 if (value) {
                logger.error(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;
        }
    }
});