PageRenderTime 90ms CodeModel.GetById 80ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/models/active_object.js

https://github.com/ajanthanm/cssfilterlab
JavaScript | 169 lines | 132 code | 22 blank | 15 comment | 20 complexity | f9c0cf8e7fc7ce6ef654e04350ebfb93 MD5 | raw file
  1/*
  2 * Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
  3 * 
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 * 
  8 *     http://www.apache.org/licenses/LICENSE-2.0
  9 * 
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17(function() {
 18
 19    function ActiveObject(source, delegate) {
 20        ActiveObject.$super.call(this);
 21        this.delegate = delegate;
 22        this.properties = {};
 23        if (source)
 24            this.loadFromJSON(source);
 25    }
 26
 27    function ActiveCollection(source, delegate) {
 28        ActiveCollection.$super.call(this, source, delegate);
 29    }
 30
 31    Global.Utils.extend(ActiveObject).from(Global.EventDispatcher);
 32    Global.Utils.extend(ActiveCollection).from(ActiveObject);
 33
 34    $.extend(ActiveObject.prototype, {
 35        loadFromJSON: function(source) {
 36            var self = this;
 37            $.each(source, function(key, val) {
 38                if (!self.properties.hasOwnProperty(key))
 39                    self.injectSetterMethod(key);
 40                self.properties[key] = self.wrapValue(val);
 41            });
 42        },
 43
 44        injectSetterMethod: function(key) {
 45            Object.defineProperty(this, key, {
 46                get: this.get.bind(this, key),
 47                set: this.set.bind(this, key),
 48                enumerable: true,
 49                configurable: true
 50            });
 51        },
 52
 53        wrapValue: function(val) {
 54            if (val !== null && typeof(val) == "object") {
 55                if (val instanceof Array)
 56                    val = new ActiveCollection(val, this);
 57                else if (!(val instanceof ActiveObject))
 58                    val = new ActiveObject(val, this);
 59            } 
 60            return val;
 61        },
 62
 63        toJSON: function() {
 64            var result = {};
 65            $.each(this.properties, function(key, value) {
 66                if (value && value instanceof ActiveObject)
 67                    value = value.toJSON();
 68                result[key] = value;
 69            });
 70            return result;
 71        },
 72
 73        fireRootValueChanged: function() {
 74            var root = this;
 75            while (root.delegate)
 76                root = root.delegate;
 77            root.fire("rootValueChanged");
 78        },
 79
 80        set: function(key, newValue) {
 81            var oldValue = this.properties[key],
 82                hadValue = this.properties.hasOwnProperty(key);
 83            if (!hadValue)
 84                this.injectSetterMethod(key);
 85            this.properties[key] = newValue = this.wrapValue(newValue);
 86            this.fire("propertyChanged." + key, [newValue, oldValue]);
 87            this.fire("propertyChanged", [key, newValue, oldValue]);
 88            if (!hadValue)
 89                this.fire("propertyAdded", [key, newValue]);
 90            this.fireRootValueChanged();
 91            return newValue;
 92        },
 93
 94        get: function(key) {
 95            return this.properties[key];
 96        },
 97
 98        remove: function(key) {
 99            var oldValue = this.properties[key],
100                hadValue = this.properties.hasOwnProperty(key);
101            if (!hadValue)
102                return;
103            delete this.properties[key];
104            this.fire("propertyChanged." + key, [null, oldValue]);
105            this.fire("propertyChanged", [key, null, oldValue]);
106            this.fire("propertyRemoved", [key, oldValue]);
107            this.fireRootValueChanged();
108            return oldValue;
109        },
110
111        bind: function(key, fn) {
112            fn(this.get(key), null);
113            return this.on("propertyChanged." + key, fn);
114        },
115
116        unbind: function(key, fn) {
117            return this.off("propertyChanged." + key, fn);
118        },
119
120        oneWayBind: function(key, el, fn) {
121            this.bind(key, function(newValue, oldValue) {
122                var value = el[fn].call(el);
123                if (oldValue != null && value == newValue)
124                    return;
125                el[fn].call(el, newValue);
126            });
127            return this;
128        },
129
130        twoWayBind: function(key, el, fn, events) {
131            var self = this;
132            this.oneWayBind(key, el, fn);
133            el.bind(events, function() {
134                var value = el[fn].call(el);
135                self.set(key, value);
136            });
137            return this;
138        },
139
140        bindToTextInput: function(key, el) {
141            return this.twoWayBind(key, el, "val", "change keyup blur");
142        },
143
144        bindToSelectInput: function(key, el) {
145            return this.twoWayBind(key, el, "val", "change keyup click blur");
146        }
147    });
148
149    $.extend(ActiveCollection.prototype, {
150        loadFromJSON: function(source) {
151            this.$super.loadFromJSON.call(this, source);
152            this.set("length", source.length);
153        },
154
155        toJSON: function() {
156            var result = [];
157            $.each(this.properties, function(key, value) {
158                if (value && value instanceof ActiveObject)
159                    value = value.toJSON();
160                result.push(value);
161            });
162            return result;
163        }
164    });
165
166    Global.ActiveObject = ActiveObject;
167    Global.ActiveCollection = ActiveCollection;
168
169})();