|
1 /** |
|
2 * Class.js |
|
3 * |
|
4 * Copyright 2003-2012, Moxiecode Systems AB, All rights reserved. |
|
5 */ |
|
6 |
|
7 /** |
|
8 * This utilitiy class is used for easier inheritage. |
|
9 * |
|
10 * Features: |
|
11 * * Exposed super functions: this._super(); |
|
12 * * Mixins |
|
13 * * Dummy functions |
|
14 * * Property functions: var value = object.value(); and object.value(newValue); |
|
15 * * Static functions |
|
16 * * Defaults settings |
|
17 */ |
|
18 define("tinymce/util/Class", [ |
|
19 "tinymce/util/Tools" |
|
20 ], function(Tools) { |
|
21 var each = Tools.each, extend = Tools.extend; |
|
22 |
|
23 var extendClass, initializing; |
|
24 |
|
25 function Class() { |
|
26 } |
|
27 |
|
28 // Provides classical inheritance, based on code made by John Resig |
|
29 Class.extend = extendClass = function(prop) { |
|
30 var self = this, _super = self.prototype, prototype, name, member; |
|
31 |
|
32 // The dummy class constructor |
|
33 function Class() { |
|
34 var i, mixins, mixin, self = this; |
|
35 |
|
36 // All construction is actually done in the init method |
|
37 if (!initializing) { |
|
38 // Run class constuctor |
|
39 if (self.init) { |
|
40 self.init.apply(self, arguments); |
|
41 } |
|
42 |
|
43 // Run mixin constructors |
|
44 mixins = self.Mixins; |
|
45 if (mixins) { |
|
46 i = mixins.length; |
|
47 while (i--) { |
|
48 mixin = mixins[i]; |
|
49 if (mixin.init) { |
|
50 mixin.init.apply(self, arguments); |
|
51 } |
|
52 } |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 // Dummy function, needs to be extended in order to provide functionality |
|
58 function dummy() { |
|
59 return this; |
|
60 } |
|
61 |
|
62 // Creates a overloaded method for the class |
|
63 // this enables you to use this._super(); to call the super function |
|
64 function createMethod(name, fn) { |
|
65 return function() { |
|
66 var self = this, tmp = self._super, ret; |
|
67 |
|
68 self._super = _super[name]; |
|
69 ret = fn.apply(self, arguments); |
|
70 self._super = tmp; |
|
71 |
|
72 return ret; |
|
73 }; |
|
74 } |
|
75 |
|
76 // Instantiate a base class (but only create the instance, |
|
77 // don't run the init constructor) |
|
78 initializing = true; |
|
79 |
|
80 /*eslint new-cap:0 */ |
|
81 prototype = new self(); |
|
82 initializing = false; |
|
83 |
|
84 // Add mixins |
|
85 if (prop.Mixins) { |
|
86 each(prop.Mixins, function(mixin) { |
|
87 mixin = mixin; |
|
88 |
|
89 for (var name in mixin) { |
|
90 if (name !== "init") { |
|
91 prop[name] = mixin[name]; |
|
92 } |
|
93 } |
|
94 }); |
|
95 |
|
96 if (_super.Mixins) { |
|
97 prop.Mixins = _super.Mixins.concat(prop.Mixins); |
|
98 } |
|
99 } |
|
100 |
|
101 // Generate dummy methods |
|
102 if (prop.Methods) { |
|
103 each(prop.Methods.split(','), function(name) { |
|
104 prop[name] = dummy; |
|
105 }); |
|
106 } |
|
107 |
|
108 // Generate property methods |
|
109 if (prop.Properties) { |
|
110 each(prop.Properties.split(','), function(name) { |
|
111 var fieldName = '_' + name; |
|
112 |
|
113 prop[name] = function(value) { |
|
114 var self = this, undef; |
|
115 |
|
116 // Set value |
|
117 if (value !== undef) { |
|
118 self[fieldName] = value; |
|
119 |
|
120 return self; |
|
121 } |
|
122 |
|
123 // Get value |
|
124 return self[fieldName]; |
|
125 }; |
|
126 }); |
|
127 } |
|
128 |
|
129 // Static functions |
|
130 if (prop.Statics) { |
|
131 each(prop.Statics, function(func, name) { |
|
132 Class[name] = func; |
|
133 }); |
|
134 } |
|
135 |
|
136 // Default settings |
|
137 if (prop.Defaults && _super.Defaults) { |
|
138 prop.Defaults = extend({}, _super.Defaults, prop.Defaults); |
|
139 } |
|
140 |
|
141 // Copy the properties over onto the new prototype |
|
142 for (name in prop) { |
|
143 member = prop[name]; |
|
144 |
|
145 if (typeof member == "function" && _super[name]) { |
|
146 prototype[name] = createMethod(name, member); |
|
147 } else { |
|
148 prototype[name] = member; |
|
149 } |
|
150 } |
|
151 |
|
152 // Populate our constructed prototype object |
|
153 Class.prototype = prototype; |
|
154 |
|
155 // Enforce the constructor to be what we expect |
|
156 Class.constructor = Class; |
|
157 |
|
158 // And make this class extendible |
|
159 Class.extend = extendClass; |
|
160 |
|
161 return Class; |
|
162 }; |
|
163 |
|
164 return Class; |
|
165 }); |