JsTemplate/Template.js
[gnome.introspection-doc-generator] / Object.js
1 //<script type="text/javascript">
2
3 /**
4  * Similar to Prototype / jQuery API
5  * implements standard object/class constructor tools
6  * 
7  * 
8  */
9  
10 //usage:
11 //imports['Object.js'].load(Object);
12  
13
14 if (imports) {
15     load = false; // declare global for gnome langs.
16 }
17 (function() {
18
19
20     object = {
21         /**
22          * Copies all the properties of config to obj.
23          * 
24          * Pretty much the same as JQuery/Prototype..
25          * @param {Object} obj The receiver of the properties
26          * @param {Object} config The source of the properties
27          * @param {Object} defaults A different object that will also be applied for default values
28          * @return {Object} returns obj
29          * @member Object extend
30          */
31
32          
33         extend : function(o, c, defaults){
34             if(defaults){
35                 // no "this" reference for friendly out of scope calls
36                 Object.extend(o, defaults);
37             }
38             if(o && c && typeof c == 'object'){
39                 for(var p in c){
40                     o[p] = c[p];
41                 }
42             }
43             return o;
44         },
45         
46         
47         /**
48          * Copies all the properties of config to obj, if the do not exist.
49          * @param {Object} obj The receiver of the properties
50          * @param {Object} config The source of the properties
51          * @return {Object} returns obj
52          * @member Object extendIf
53          */
54
55          
56         extendIf : function(o, c){
57             
58             if(!o || !c || typeof c != 'object'){
59                 return o;
60             }
61             for(var p in c){
62                 if (typeof(o[p]) != 'undefined') {
63                     continue;
64                 }
65                 o[p] = c[p];
66             }
67             return o;
68         },
69         
70         
71         /**
72          * Extends one class with another class and optionally overrides members with the passed literal. This class
73          * also adds the function "override()" to the class that can be used to override
74          * members on an instance.
75          * 
76          * usage: 
77          * MyObject = Object.define(
78          *     function(...) {
79          *          ....
80          *     },
81          *     parentClass, // or Object
82          *     {
83          *        ... methods and properties.
84          *     }
85          * });
86          * @param {Function} constructor The class inheriting the functionality
87          * @param {Object} superclass The class being extended
88          * @param {Object} overrides (optional) A literal with members
89          * @return {Function} constructor (eg. class
90          * @method define
91          */
92         define : function(){
93             // inline overrides
94             var io = function(o){
95                 for(var m in o){
96                     this[m] = o[m];
97                 }
98             };
99             return function(sb, sp, overrides) {
100                 if (typeof(sp) == 'undefined') {
101                     // error condition - try and dump..
102                     throw "Missing superclass: when applying: " + sb
103                 }
104                 
105                 var F = function(){}, sbp, spp = sp.prototype;
106                 F.prototype = spp;
107                 sbp = sb.prototype = new F();
108                 sbp.constructor=sb;
109                 sb.superclass=spp;
110                 
111                 // extends Object.
112                 if(spp.constructor == Object.prototype.constructor){
113                     spp.constructor=sp;
114                 }
115                 
116                 sb.override = function(o){
117                     Object.extend(sb.prototype, o);
118                 };
119                 sbp.override = io;
120                 Object.extend(sb.prototype, overrides);
121                 return sb;
122             };
123         }(),
124         
125         
126         /**
127          * returns a list of keys of the object.
128          * @param {Object} obj object to inspect
129          * @return {Array} returns list of kyes
130          * @member Object keys
131          */
132         keys : function(o)
133         {
134             var ret = [];
135             for(var i in o) {
136                 ret.push[i];
137             }
138             return ret;
139         }
140  
141         
142     };
143     
144     
145     if (imports) {
146         load = function(ar) {
147             Object = ar;
148             imports.lang.copyPropertiesNoOverwrite( object,ar);
149              
150         };
151     } else {
152         // non imports version.
153         
154         for(i in object) {
155             if (!Object[i]) {
156                 Object[i] = object[i];
157             }
158         }
159     }
160 })();