]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * This base class can be used by derived classes to dynamically require Google API's.\r | |
3 | */\r | |
4 | Ext.define('Ext.ux.google.Api', {\r | |
5 | mixins: [\r | |
6 | 'Ext.mixin.Mashup'\r | |
7 | ],\r | |
8 | \r | |
9 | requiredScripts: [\r | |
10 | '//www.google.com/jsapi'\r | |
11 | ],\r | |
12 | \r | |
13 | statics: {\r | |
14 | loadedModules: {\r | |
15 | /*\r | |
16 | * feeds: [ callback1, callback2, .... ] transitions to -> feeds : true (when complete)\r | |
17 | */\r | |
18 | }\r | |
19 | },\r | |
20 | \r | |
21 | onClassExtended: function(cls, data, hooks) {\r | |
22 | var onBeforeClassCreated = hooks.onBeforeCreated,\r | |
23 | Api = this; // the Ext.ux.google.Api class\r | |
24 | \r | |
25 | hooks.onBeforeCreated = function(cls, data) {\r | |
26 | var me = this,\r | |
27 | apis = [],\r | |
28 | requiresGoogle = Ext.Array.from(data.requiresGoogle),\r | |
29 | loadedModules = Api.loadedModules,\r | |
30 | remaining = 0,\r | |
31 | callback = function () {\r | |
32 | if (! --remaining) {\r | |
33 | onBeforeClassCreated.call(me, cls, data, hooks);\r | |
34 | }\r | |
35 | Ext.env.Ready.unblock();\r | |
36 | },\r | |
37 | api, i, length;\r | |
38 | \r | |
39 | /*\r | |
40 | * requiresGoogle: [\r | |
41 | * 'feeds',\r | |
42 | * { api: 'feeds', version: '1.x',\r | |
43 | * callback : fn, nocss : true } //optionals\r | |
44 | * ]\r | |
45 | */\r | |
46 | length = requiresGoogle.length;\r | |
47 | \r | |
48 | for (i = 0; i < length; ++i) {\r | |
49 | if (Ext.isString(api = requiresGoogle[i])) {\r | |
50 | apis.push({ api: api });\r | |
51 | } else if(Ext.isObject(api)) {\r | |
52 | apis.push(Ext.apply({}, api));\r | |
53 | }\r | |
54 | }\r | |
55 | \r | |
56 | Ext.each(apis, function (api) {\r | |
57 | var name = api.api,\r | |
58 | version = String(api.version || '1.x'),\r | |
59 | module = loadedModules[name];\r | |
60 | \r | |
61 | if (!module) {\r | |
62 | ++remaining;\r | |
63 | Ext.env.Ready.block();\r | |
64 | loadedModules[name] = module = [ callback ].concat(api.callback || []);\r | |
65 | \r | |
66 | delete api.api;\r | |
67 | delete api.version;\r | |
68 | \r | |
69 | //TODO: window.google assertion?\r | |
70 | \r | |
71 | google.load(\r | |
72 | name,\r | |
73 | version,\r | |
74 | Ext.applyIf({\r | |
75 | callback : function () {\r | |
76 | loadedModules[name] = true;\r | |
77 | \r | |
78 | for (var n = module.length; n-- > 0; ) {\r | |
79 | module[n](); //iterate callbacks in reverse\r | |
80 | }\r | |
81 | }\r | |
82 | }, api)\r | |
83 | );\r | |
84 | \r | |
85 | } else if (module !== true) {\r | |
86 | module.push(callback);\r | |
87 | }\r | |
88 | });\r | |
89 | \r | |
90 | if (!remaining) {\r | |
91 | onBeforeClassCreated.call(me, cls, data, hooks);\r | |
92 | }\r | |
93 | };\r | |
94 | }\r | |
95 | });\r |