source: tapas/web/resources/js/library/jquery.class.js @ 382

Last change on this file since 382 was 382, checked in by vmipsl, 12 years ago

clean

File size: 10.8 KB
Line 
1// Object Oriented Class
2jQuery.extend({
3    // define class core object
4    Class: function() {
5    },
6    // define interface core object
7    Interface: function() {
8    }
9});
10
11// define interface elements
12jQuery.extend(jQuery.Interface.prototype, {
13    attributes: [],
14    properties: [],
15    methods: []
16});
17
18// define class elements
19jQuery.extend(jQuery.Class, {
20    // this method create getter and setter property
21    createGetSet: function(object, name, element) {
22        object.prototype['get' + name] = function() {
23            return this[element];
24        };
25        object.prototype['set' + name] = function() {
26            this[element] = arguments[0];
27        };
28        if(!object.prototype[element])
29        {
30            object.prototype[element] = '';
31        }
32    },
33   
34    // this method create module in window object
35    createNamespace: function(name) {
36        var names = name.split('.'),
37            namespaceStr = 'window';
38        while (names.length) {
39            var name = names.shift();
40            namespaceStr += "['" + name + "']";
41        }
42        return namespaceStr;
43    },
44
45    // use this method for understanding interface elements is implemented in class
46    isIn: function(object, name) {
47        for (var item in object.prototype) {
48            if (item == name) {
49                if (jQuery.isArray(object.prototype[item]))
50                    return 0;
51                else if (jQuery.isFunction(object.prototype[item]))
52                    return 1;
53                else if (jQuery.isEmptyObject(object.prototype[item]) || jQuery.isPlainObject(object.prototype[item]))
54                    return 2;
55                else if (jQuery.isXMLDoc(object.prototype[item]))
56                    return 3;
57                else
58                    return 0;
59            }
60        }
61        return -1;
62    },
63   
64    // this method for create class object
65    create: function() {
66        var parent = null,
67            elements = null,
68            options = {
69                abstract: false,
70                getset: [],
71                implements: [],
72                module: '',
73                superClass: [], // for holding superclass initialize function
74                superRender: [] // for holding superclass initialize function
75            };
76           
77        // check for extending
78        if (jQuery.isFunction(arguments[0])) {
79            parent = arguments[0];
80            elements = arguments[1];
81            if (arguments[2]) {
82                jQuery.extend(options, arguments[2] || {});
83            }
84        }
85        else {
86            elements = arguments[0];
87            if (arguments[1]) {
88                jQuery.extend(options, arguments[1] || {});
89            }
90        }
91       
92        // create new class core
93        function handle() {
94            // check if class is abstracted
95            if (this.options.abstract) {
96                throw new Error("abstract classes cannot be instantiated");
97            }
98           
99            // execute constructor method
100            try {
101                this.initialize.apply(this, arguments);
102            } catch (ex) { }
103        }
104       
105        // extend class base methods in new class core
106        jQuery.extend(handle.prototype, jQuery.Class);
107       
108        // extend parent class methods in new class core
109        if (parent) {
110            // get parent initialize function
111            var superClass = parent.prototype.options.superClass || [];
112            var superRender = parent.prototype.options.superRender || [];
113           
114            // extend parent class methods in new class core
115            jQuery.extend(handle.prototype, parent.prototype);
116           
117            // save parent initialize function
118            superClass.push(parent.prototype.initialize);
119            options.superClass = superClass;
120           
121            try {
122                superRender.push(parent.prototype.render);
123                options.superRender = superRender;
124            } catch (ex) { }
125        }
126       
127        // extend user defined methods in new class core
128        jQuery.extend(handle.prototype, elements || {});
129        handle.prototype.options = options;
130
131        // define getter and setter functions
132        if (options.getset.length > 0) {
133            for (var i = 0; i < options.getset.length; i++) {
134                var name = options.getset[i][0],
135                    element = options.getset[i][1];
136                this.createGetSet(handle, name, element);
137            }
138        }
139       
140        // check for impelemented elements from interface
141        if (options.implements.length > 0) {
142            var attributesMustImplemented = [],
143                propertiesMustImplemented = [],
144                methodsMustImplemented = [];
145           
146            // extract elements from interface
147            for (var i = 0; i < options.implements.length; i++) {
148                jQuery.merge(attributesMustImplemented, options.implements[i].attributes);
149                jQuery.merge(propertiesMustImplemented, options.implements[i].properties);
150                jQuery.merge(methodsMustImplemented, options.implements[i].methods);
151            }
152           
153            var didNotImplemented = false,
154                msg = 'must be implemented';
155           
156            // check for attributes   
157            for (var i = 0; i < attributesMustImplemented.length; i++) {
158                var result = this.isIn(handle, attributesMustImplemented[i]);
159                if (result != 0 && result != 2) {
160                    didNotImplemented = true;
161                    msg = 'attribute: ' + attributesMustImplemented[i] + ', ' + msg;
162                }
163            }
164           
165            // check for properties
166            for (var i = 0; i < propertiesMustImplemented.length; i++) {
167                var resultGet = this.isIn(handle, 'get' + propertiesMustImplemented[i]),
168                    resultSet = this.isIn(handle, 'set' + propertiesMustImplemented[i]);
169                if (resultGet != 1) {
170                    didNotImplemented = true;
171                    msg = 'property: get' + propertiesMustImplemented[i] + ', ' + msg;
172                }
173                else if (resultSet != 1) {
174                    didNotImplemented = true;
175                    msg = 'property: set' + propertiesMustImplemented[i] + ', ' + msg;
176                }
177            }
178           
179            // check for methods
180            for (var i = 0; i < methodsMustImplemented.length; i++) {
181                var result = this.isIn(handle, methodsMustImplemented[i]);
182                if (result != 1) {
183                    didNotImplemented = true;
184                    msg = 'method: ' + methodsMustImplemented[i] + ', ' + msg;
185                }
186            }
187           
188            if (didNotImplemented) {
189                throw new Error(msg);
190            }
191        }
192       
193        // check if class is module type, create module
194        if (options.module != '') {
195            var names = options.module.split('.'),
196                name = names[0];
197            window[name] = new function() { };
198            for (var i = 1; i < names.length; i++) {
199                name += '.' + names[i];
200                eval(this.createNamespace(name) + ' = new function() {};');
201            }
202            eval('jQuery.extend(' + this.createNamespace(name) + ', handle.prototype);');
203        }
204       
205        return handle;
206    },
207   
208    // for add method to class in runtime
209    addMethods: function() {
210        if (arguments[0]) {
211            jQuery.extend(this.constructor.prototype, arguments[0]);
212        }
213    },
214   
215    // for add attribute to class in runtime
216    addAttributes: function() {
217        if (arguments[0]) {
218            jQuery.extend(this.constructor.prototype, arguments[0]);
219        }
220    },
221   
222    // for add property to class in runtime
223    addProperty: function() {
224        if (arguments[0]) {
225            jQuery.extend(this.constructor.prototype, arguments[0]);
226        }
227    },
228   
229    // this method is use to get value And set value of property
230    property: function() {
231        // get value section
232        if (arguments.length == 1 && this.constructor.prototype.hasOwnProperty('get' + arguments[0])) {
233            if (arguments[0]) {
234                if (jQuery.isFunction(this['get' + arguments[0]])) {
235                    return this['get' + arguments[0]]();
236                }
237                else {
238                    return this['get' + arguments[0]];
239                }
240            }
241        }
242       
243        //set value section
244        else if (this.constructor.prototype.hasOwnProperty('set' + arguments[0])) {
245            if (jQuery.isFunction(this['set' + arguments[0]])) {
246                for (var name in this) {
247                    if (this[name] == this['get' + arguments[0]]()) {
248                        if (!jQuery.isFunction(this[name])) {
249                            this[name] = arguments[1];
250                        }
251                    }
252                }
253                return this['get' + arguments[0]]();
254            }
255            else {
256                this[arguments[0]] = arguments[1];
257                return this['get' + arguments[0]];
258            }
259        }
260    },
261   
262    // check if two class is equal
263    equal: function() {
264        if (arguments[1]) {
265            return arguments[0].constructor.prototype == arguments[1].constructor.prototype;
266        }
267        else {
268            return this.constructor.prototype == arguments[0].constructor.prototype;
269        }
270    },
271   
272    // create fresh clone object from class object
273    clone: function() {
274        function handle() {
275            try {
276                this.initialize.apply(this, arguments);
277            } catch (ex) { }
278        }
279        if (arguments[0] == true) {
280            jQuery.extend(handle.prototype, this.constructor.prototype);
281            return handle;
282        }
283        else {
284            jQuery.extend(handle.constructor.prototype, this.constructor.prototype);
285            return handle;
286        }
287    },
288   
289    toString: function() {
290        return 'Design By Hassan Jodat Shandi';
291    },
292   
293    // this method use for call parent initialize method
294    parent: function() {
295        var method = arguments[0],
296            args = arguments[1],
297            superClass = this.options.superClass,
298            index = -1;
299        for (var i = 0; i < superClass.length; i++) {
300            if (superClass[i] == method)
301                index = i;
302        }
303        if (index == -1 && superClass[superClass.length - 1] != method) {
304            superClass[superClass.length - 1].apply(this, args);
305        }
306        else {
307            superClass[index - 1].apply(this, args);
308        }
309    }
310});
311
312var Class = $.Class;
Note: See TracBrowser for help on using the repository browser.