making it librejs compliant
[KiwiIRC.git] / client / src / models / applet.js
CommitLineData
eaaf73b0 1_kiwi.model.Applet = _kiwi.model.Panel.extend({\r
19cc6364
D
2 initialize: function (attributes) {\r
3 // Temporary name\r
4 var name = "applet_"+(new Date().getTime().toString()) + Math.ceil(Math.random()*100).toString();\r
eaaf73b0 5 this.view = new _kiwi.view.Applet({model: this, name: name});\r
19cc6364
D
6\r
7 this.set({\r
8 "name": name\r
9 }, {"silent": true});\r
51ae0eb8
D
10\r
11 // Holds the loaded applet\r
12 this.loaded_applet = null;\r
19cc6364
D
13 },\r
14\r
da848c4f 15\r
19cc6364
D
16 // Load an applet within this panel\r
17 load: function (applet_object, applet_name) {\r
18 if (typeof applet_object === 'object') {\r
4baeda09
D
19 // Make sure this is a valid Applet\r
20 if (applet_object.get || applet_object.extend) {\r
21\r
22 // Try find a title for the applet\r
247dd7ac 23 this.set('title', applet_object.get('title') || _kiwi.global.i18n.translate('client_models_applet_unknown').fetch());\r
4baeda09
D
24\r
25 // Update the tabs title if the applet changes it\r
26 applet_object.bind('change:title', function (obj, new_value) {\r
27 this.set('title', new_value);\r
28 }, this);\r
29\r
30 // If this applet has a UI, add it now\r
31 this.view.$el.html('');\r
32 if (applet_object.view) {\r
33 this.view.$el.append(applet_object.view.$el);\r
34 }\r
35\r
36 // Keep a reference to this applet\r
37 this.loaded_applet = applet_object;\r
1aed0c0b
D
38\r
39 this.loaded_applet.trigger('applet_loaded');\r
4baeda09 40 }\r
19cc6364
D
41\r
42 } else if (typeof applet_object === 'string') {\r
43 // Treat this as a URL to an applet script and load it\r
44 this.loadFromUrl(applet_object, applet_name);\r
45 }\r
4baeda09
D
46\r
47 return this;\r
19cc6364
D
48 },\r
49\r
da848c4f 50\r
19cc6364
D
51 loadFromUrl: function(applet_url, applet_name) {\r
52 var that = this;\r
53\r
247dd7ac 54 this.view.$el.html(_kiwi.global.i18n.translate('client_models_applet_loading').fetch());\r
19cc6364
D
55 $script(applet_url, function () {\r
56 // Check if the applet loaded OK\r
eaaf73b0 57 if (!_kiwi.applets[applet_name]) {\r
247dd7ac 58 that.view.$el.html(_kiwi.global.i18n.translate('client_models_applet_notfound').fetch());\r
19cc6364
D
59 return;\r
60 }\r
61\r
62 // Load a new instance of this applet\r
eaaf73b0 63 that.load(new _kiwi.applets[applet_name]());\r
19cc6364 64 });\r
ea033d12
D
65 },\r
66\r
da848c4f 67\r
ea033d12
D
68 close: function () {\r
69 this.view.$el.remove();\r
70 this.destroy();\r
17f187e6 71\r
ea033d12
D
72 this.view = undefined;\r
73\r
51ae0eb8
D
74 // Call the applets dispose method if it has one\r
75 if (this.loaded_applet && this.loaded_applet.dispose) {\r
76 this.loaded_applet.dispose();\r
77 }\r
78\r
c891916b
D
79 // Call the inherited close()\r
80 this.constructor.__super__.close.apply(this, arguments);\r
425efe7a
JA
81 },\r
82\r
83 isApplet: function () {\r
84 return true;\r
19cc6364 85 }\r
da848c4f
D
86},\r
87\r
88\r
89{\r
90 // Load an applet type once only. If it already exists, return that\r
91 loadOnce: function (applet_name) {\r
92\r
93 // See if we have an instance loaded already\r
c966123a 94 var applet = _.find(_kiwi.app.panels('applets'), function(panel) {\r
da848c4f
D
95 // Ignore if it's not an applet\r
96 if (!panel.isApplet()) return;\r
97\r
98 // Ignore if it doesn't have an applet loaded\r
99 if (!panel.loaded_applet) return;\r
100\r
101 if (panel.loaded_applet.get('_applet_name') === applet_name) {\r
102 return true;\r
103 }\r
104 });\r
105\r
106 if (applet) return applet;\r
107\r
108\r
109 // If we didn't find an instance, load a new one up\r
110 return this.load(applet_name);\r
111 },\r
112\r
113\r
17f187e6
D
114 load: function (applet_name, options) {\r
115 var applet, applet_obj;\r
116\r
117 options = options || {};\r
118\r
119 applet_obj = this.getApplet(applet_name);\r
da848c4f 120\r
17f187e6
D
121 if (!applet_obj)\r
122 return;\r
da848c4f
D
123\r
124 // Create the applet and load the content\r
125 applet = new _kiwi.model.Applet();\r
17f187e6 126 applet.load(new applet_obj({_applet_name: applet_name}));\r
da848c4f 127\r
17f187e6
D
128 // Add it into the tab list if needed (default)\r
129 if (!options.no_tab)\r
130 _kiwi.app.applet_panels.add(applet);\r
da848c4f
D
131\r
132\r
133 return applet;\r
134 },\r
135\r
136\r
17f187e6
D
137 getApplet: function (applet_name) {\r
138 return _kiwi.applets[applet_name] || null;\r
139 },\r
140\r
141\r
da848c4f
D
142 register: function (applet_name, applet) {\r
143 _kiwi.applets[applet_name] = applet;\r
144 }\r
19cc6364 145});