summaryrefslogtreecommitdiff
path: root/scripts/lib/dwb.js
blob: f0ee0b75cf8e90cb2347e508997d2c0879247e6d (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
(function() {
    var _modules = {};
    var _requires = {};
    var _callbacks = [];
    var _initialized = false;
    var _applyRequired = function(names, callback) 
    {
        if (names === null) 
            callback.call(this, _modules);
        else 
        {
            var modules = [];
            var name, detail;
            for (var j=0, m=names.length; j<m; j++) 
            {
                name = names[j];
                if (/^\w*!/.test(name)) 
                {
                    detail = name.split("!");
                    name = detail[0];
                    if (!_modules[name]) 
                        include(detail.slice(1).join("!"));
                }
                modules.push(_modules[name]);
            } 
            if (callback)
                callback.apply(this, modules);
        }
    };

    var _privProps = [];
    var _getPrivateIdx = function(object, key, identifier) 
    {
        var p;
        for (var i=0, l=_privProps.length; i<l; ++i) 
        {
            p = _privProps[i];
            if (p.object == object && p.key == key && p.identifier === identifier)
                return i;
        }
        return -1;
    };
    var _contexts;

    Object.defineProperties(this, { 
            "provide" : 
            { 
                value : function(name, module) 
                {
                    if (_modules[name]) 
                    {
                        io.debug({ 
                                offset : 1, arguments : arguments,
                                error : new Error("provide: Module " + 
                                                  name + " is already defined!")
                        });
                    }
                    else 
                        _modules[name] = module;
                }
            },
            "replace" : 
            {
                value : function(name, module) 
                {
                    if (! module && _modules[name] ) 
                    {
                        for (var key in _modules[name]) 
                            delete _modules[name][key];

                        delete _modules[name];
                    }
                    else 
                        _modules[name] = module;
                }
            },
            "require" : 
            {
                value : function(names, callback) 
                {
                    if (names !== null && ! (names instanceof Array)) 
                    {
                        io.debug({ 
                                error : new Error("require : invalid argument (" + 
                                                    JSON.stringify(names) + ")"), 
                                offset : 1, 
                                arguments : arguments 
                        });
                        return; 
                    }

                    if (!_initialized) 
                        _callbacks.push({callback : callback, names : names});
                    else 
                        _applyRequired(names, callback);
                }
            },
            // Called after all scripts have been loaded and executed
            // Immediately deleted from the global object, so it is not callable
            // from other scripts
            "_initAfter" : 
            { 
                value : function() 
                {
                    _initialized = true;
                    for (var i=0, l=_callbacks.length; i<l; i++) 
                        _applyRequired(_callbacks[i].names, _callbacks[i].callback);

                },
                configurable : true
            },
            "_initBefore" : 
            {
                value : function(contexts) 
                {
                    _contexts = contexts
                    Object.freeze(this);
                },
                configurable : true
            },
            //"_private" : 
            //{
            //    value : function() 
            //    {
            //        io.print(arguments);
            //    }
            //}
    });
    Object.defineProperties(GObject.prototype, {
            "setPrivate" : 
            { 
                value : function(key, value, identifier) 
                {
                    if (!(identifier instanceof Object) && !(identifier instanceof Function)) 
                        throw new Error("[setPrivate] identifier is not an Object or Function");

                    var i = _getPrivateIdx(this, key, identifier);
                    if (i === -1) 
                    {
                        if (value !== undefined && value !== null)
                            _privProps.push({ 
                                    object : this, 
                                    key : key, 
                                    identifier : identifier, 
                                    value : value 
                            });
                    }
                    else if (value !== null) 
                    {
                        _privProps[i].value = value;
                    }
                    else 
                    {
                        _privProps.splice(i);
                    }
                }
            },
            "getPrivate" : 
            { 
                value : function(key, identifier) 
                {
                    var i = _getPrivateIdx(this, key, identifier);
                    if (i !== -1) 
                        return _privProps[i].value;
                    return undefined;
                }
            },
            "notify" : 
            { 
                value : function(name, callback, after) 
                { 
                    return this.connect("notify::" + util.uncamelize(name), callback, after || false);
                }
            },
            "connectBlocked" : 
            { 
                value : function(name, callback, after) 
                { 
                    var sig = this.connect(name, (function() { 
                        this.blockSignal(sig);
                        var result = callback.apply(callback, arguments);
                        this.unblockSignal(sig);
                        return result;
                    }).bind(this));
                    return sig;
                }
            },
            "notifyBlocked" : 
            {
                value : function(name, callback, after) 
                {
                    return this.connectBlocked("notify::" + util.uncamelize(name), callback, after || false);
                }
            }
    });
    Object.defineProperties(Deferred.prototype, {
            "done" : {
                value : function(method) {
                    return this.then(method);
                }
            },
            "fail" : {
                value : function(method) {
                    return this.then(null, method);
                }
            },
            "always" : {
                value : function(method)
                {
                    return this.then(method, method);
                }
            }
    });
    Object.defineProperty(Deferred, "when", {
            value : function(value, callback, errback)
            {
                if (value instanceof Deferred)
                    return value.then(callback, errback);
                else 
                    return callback(value);
            }
    });
    if (!Function.prototype.debug) 
    {
        Object.defineProperty(Function.prototype, "debug", {
                value : function(scope)
                {
                    return function() {
                        try 
                        {
                            this.apply(this, arguments);
                        }
                        catch (e) 
                        { 
                            if (scope)
                                scope.debug(e);
                            else 
                                io.debug(e);
                        }
                    }.bind(this);
                }
        });
    }
})();
Object.preventExtensions(this);