|
1 /* Copyright 2009 the Melange authors. |
|
2 * |
|
3 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 * you may not use this file except in compliance with the License. |
|
5 * You may obtain a copy of the License at |
|
6 * |
|
7 * http://www.apache.org/licenses/LICENSE-2.0 |
|
8 * |
|
9 * Unless required by applicable law or agreed to in writing, software |
|
10 * distributed under the License is distributed on an "AS IS" BASIS, |
|
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 * See the License for the specific language governing permissions and |
|
13 * limitations under the License. |
|
14 */ |
|
15 /** |
|
16 * @author <a href="mailto:fadinlight@gmail.com">Mario Ferraro</a> |
|
17 */ |
|
18 |
|
19 (function () { |
|
20 /** @lends melange */ |
|
21 |
|
22 /** General melange package. |
|
23 * @name melange |
|
24 * @namespace melange |
|
25 */ |
|
26 var melange = window.melange = function () { |
|
27 return new melange(); |
|
28 }; |
|
29 |
|
30 if (window.jQuery === undefined) { |
|
31 throw new Error("jQuery package must be loaded exposing jQuery namespace"); |
|
32 } |
|
33 |
|
34 if (window.JSON === undefined) { |
|
35 throw new Error("json2 package must be loaded exposing JSON namespace"); |
|
36 } |
|
37 |
|
38 /** Shortcut to current package. |
|
39 * @private |
|
40 */ |
|
41 var $m = melange; |
|
42 |
|
43 /** Contains general configuration for melange package. |
|
44 * @variable |
|
45 * @public |
|
46 * @name melange.config |
|
47 */ |
|
48 $m.config = {}; |
|
49 |
|
50 (function () { |
|
51 var configuration = |
|
52 jQuery("script[melangeConfig][src$='melange.js']").attr("melangeConfig"); |
|
53 if (configuration) { |
|
54 var configuration_object = JSON.parse("{ " + configuration + " }"); |
|
55 jQuery.extend($m.config, configuration_object); |
|
56 } |
|
57 }()); |
|
58 |
|
59 /** Shortcut to clone objects using jQuery. |
|
60 * @function |
|
61 * @public |
|
62 * @name melange.clone |
|
63 * @param {Object} object the object to clone |
|
64 * @returns {Object} a new, cloned object |
|
65 */ |
|
66 $m.clone = function (object) { |
|
67 // clone object, see |
|
68 // http://stackoverflow.com/questions/122102/ |
|
69 // what-is-the-most-efficent-way-to-clone-a-javascript-object |
|
70 return jQuery.extend(true, {}, object); |
|
71 }; |
|
72 |
|
73 /** Set melange general options. |
|
74 * @function |
|
75 * @public |
|
76 * @name melange.setOptions |
|
77 * @param {Object} options Options to set/unset |
|
78 */ |
|
79 $m.setOptions = function (options) { |
|
80 switch (options.debug) { |
|
81 case true: |
|
82 $m.logging.setDebug(); |
|
83 break; |
|
84 case false: |
|
85 $m.logging.unsetDebug(); |
|
86 break; |
|
87 default: |
|
88 $m.logging.setDebug(); |
|
89 } |
|
90 if (options.debugLevel) { |
|
91 $m.logging.setDebugLevel(options.debugLevel); |
|
92 } |
|
93 }; |
|
94 |
|
95 /** Facility to load google API. |
|
96 * @function |
|
97 * @public |
|
98 * @name melange.loadGoogleApi |
|
99 * @param {String} modulename Google Ajax module to load |
|
100 * @param {String|Number} moduleversion Google Ajax module version to load |
|
101 * @param {Object} settings Google Ajax settings for the module |
|
102 * @param {Function} callback to be called as soon as module is loaded |
|
103 */ |
|
104 $m.loadGoogleApi = function (modulename, moduleversion, settings, callback) { |
|
105 |
|
106 if (!modulename || !moduleversion) { |
|
107 throw new TypeError("modulename must be defined"); |
|
108 } |
|
109 |
|
110 /** Options to be sent to google.load constructor |
|
111 * @private |
|
112 * @name melange.loadGoogleApi.options |
|
113 */ |
|
114 var options = { |
|
115 name : modulename, |
|
116 version : moduleversion, |
|
117 settings : settings |
|
118 }; |
|
119 jQuery.extend(options.settings, {callback: callback}); |
|
120 google.load(options.name, options.version, options.settings); |
|
121 }; |
|
122 |
|
123 (function () { |
|
124 /** @lends melange.error */ |
|
125 |
|
126 /** Package that handles melange errors |
|
127 * @namespace melange.error |
|
128 */ |
|
129 melange.error = window.melange.error = function () { |
|
130 return new melange.error(); |
|
131 }; |
|
132 |
|
133 /** Shortcut to current package. |
|
134 * @property |
|
135 * @private |
|
136 */ |
|
137 var $m = melange.error; |
|
138 |
|
139 /** List of default custom error types to be created. |
|
140 * @property |
|
141 * @private |
|
142 */ |
|
143 var error_types = [ |
|
144 "DependencyNotSatisfied", |
|
145 "notImplementedByChildClass" |
|
146 ]; |
|
147 |
|
148 /** Create errors |
|
149 * @function |
|
150 * @public |
|
151 * @name melange.error.createErrors |
|
152 * @param {String[]} error_types Array of strings with errors names |
|
153 */ |
|
154 $m.createErrors = function (error_types) { |
|
155 jQuery.each(error_types, function () { |
|
156 melange.error[this] = Error; |
|
157 }); |
|
158 }; |
|
159 |
|
160 $m.createErrors(error_types); |
|
161 }()); |
|
162 |
|
163 (function () { |
|
164 /** @lends melange.logging */ |
|
165 |
|
166 /** Package that contains all log related functions. |
|
167 * @name melange.logging |
|
168 * @namespace melange.logging |
|
169 */ |
|
170 melange.logging = window.melange.logging = function () { |
|
171 return new melange.logging(); |
|
172 }; |
|
173 |
|
174 /** Shortcut to current package. |
|
175 * @property |
|
176 * @private |
|
177 */ |
|
178 var $m = melange.logging; |
|
179 /** @private */ |
|
180 var debug = false; |
|
181 /** @private */ |
|
182 var current_debug_level = 5; |
|
183 |
|
184 /** Set debug logging on. |
|
185 * @function |
|
186 * @public |
|
187 * @name melange.logging.setDebug |
|
188 */ |
|
189 $m.setDebug = function () { |
|
190 debug = true; |
|
191 }; |
|
192 |
|
193 /** Set debug logging off. |
|
194 * @function |
|
195 * @public |
|
196 * @name melange.logging.unsetDebug |
|
197 */ |
|
198 $m.unsetDebug = function () { |
|
199 debug = false; |
|
200 }; |
|
201 |
|
202 /** Check if debug is active. |
|
203 * @function |
|
204 * @public |
|
205 * @name melange.logging.isDebug |
|
206 * @returns {boolean} true if debug is on, false otherwise |
|
207 */ |
|
208 $m.isDebug = function () { |
|
209 return debug ? true : false; |
|
210 }; |
|
211 |
|
212 /** Set the current debug level. |
|
213 * @function |
|
214 * @public |
|
215 * @name melange.logging.setDebugLevel |
|
216 * @param level The log level to set |
|
217 * @throws {TypeError} if the parameter given is not a number |
|
218 */ |
|
219 $m.setDebugLevel = function (level) { |
|
220 if (isNaN(level)) { |
|
221 throw new melange.error.TypeError( |
|
222 "melange.logging.setDebugLevel: parameter must be a number" |
|
223 ); |
|
224 } |
|
225 if (level <= 0) { |
|
226 level = 1; |
|
227 } |
|
228 if (level >= 6) { |
|
229 level = 5; |
|
230 } |
|
231 current_debug_level = level; |
|
232 }; |
|
233 |
|
234 /** Get the current debug level. |
|
235 * @function |
|
236 * @public |
|
237 * @name melange.logging.getDebugLevel |
|
238 * @returns {Number} The current debug level |
|
239 */ |
|
240 $m.getDebugLevel = function () { |
|
241 return current_debug_level; |
|
242 }; |
|
243 |
|
244 /** A decorator for logging. |
|
245 * @function |
|
246 * @public |
|
247 * @name melange.logging.debugDecorator |
|
248 * @param {Object} object_to_decorate The Function/Object to decorate |
|
249 * @returns {Object} Same object,decorated with log(level,message) func |
|
250 */ |
|
251 $m.debugDecorator = function (object_to_decorate) { |
|
252 /** Function to handle output of logs. |
|
253 * @function |
|
254 * @name melange.logging.debugDecorator.log |
|
255 * @param level The log level |
|
256 * @param message The string |
|
257 */ |
|
258 object_to_decorate.log = function (level, message) { |
|
259 if (melange.logging.isDebug() && current_debug_level >= level) { |
|
260 console.debug(message); |
|
261 } |
|
262 }; |
|
263 return object_to_decorate; |
|
264 }; |
|
265 }()); |
|
266 |
|
267 (function () { |
|
268 /** @lends melange.templates */ |
|
269 |
|
270 /** Package that provides basic templates functions |
|
271 * @name melange.templates |
|
272 * @namespace melange.templates |
|
273 */ |
|
274 melange.templates = window.melange.templates = function () { |
|
275 return new melange.templates(); |
|
276 }; |
|
277 |
|
278 /** Shortcut to current package |
|
279 * @private |
|
280 */ |
|
281 var $m = melange.logging.debugDecorator(melange.templates); |
|
282 |
|
283 melange.error.createErrors([ |
|
284 ]); |
|
285 |
|
286 /** Parent prototype for all templates |
|
287 * @class |
|
288 * @constructor |
|
289 * @name melange.templates._baseTemplate |
|
290 * @public |
|
291 */ |
|
292 $m._baseTemplate = function () { |
|
293 this.context = {}; |
|
294 var configuration = jQuery("script[melangeContext]")[0]; |
|
295 if (configuration !== undefined) { |
|
296 configuration = jQuery(configuration).attr("melangeContext"); |
|
297 if (configuration) { |
|
298 /* FIXME: json2 doesn't parse the object if they have k:"v" instead |
|
299 of "k":"v", but this is not what gviz outputs, we need to change |
|
300 gviz source or hack json2 source or use another method. eval() |
|
301 must be not used anyway |
|
302 */ |
|
303 /*jslint evil: true,undef: false */ |
|
304 eval("var configuration_object = " + configuration); |
|
305 jQuery.extend(this.context, configuration_object); |
|
306 /*jslint evil: false,undef: true */ |
|
307 } |
|
308 } |
|
309 }; |
|
310 }()); |
|
311 }()); |
|
312 window.melange = window.melange.logging.debugDecorator(window.melange); |