Merge branch 'development' of https://github.com/crtaylor123/KiwiIRC into crtaylor123...
[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
ea033d12 79 this.closePanel();\r
425efe7a
JA
80 },\r
81\r
82 isApplet: function () {\r
83 return true;\r
19cc6364 84 }\r
da848c4f
D
85},\r
86\r
87\r
88{\r
89 // Load an applet type once only. If it already exists, return that\r
90 loadOnce: function (applet_name) {\r
91\r
92 // See if we have an instance loaded already\r
c966123a 93 var applet = _.find(_kiwi.app.panels('applets'), function(panel) {\r
da848c4f
D
94 // Ignore if it's not an applet\r
95 if (!panel.isApplet()) return;\r
96\r
97 // Ignore if it doesn't have an applet loaded\r
98 if (!panel.loaded_applet) return;\r
99\r
100 if (panel.loaded_applet.get('_applet_name') === applet_name) {\r
101 return true;\r
102 }\r
103 });\r
104\r
105 if (applet) return applet;\r
106\r
107\r
108 // If we didn't find an instance, load a new one up\r
109 return this.load(applet_name);\r
110 },\r
111\r
112\r
17f187e6
D
113 load: function (applet_name, options) {\r
114 var applet, applet_obj;\r
115\r
116 options = options || {};\r
117\r
118 applet_obj = this.getApplet(applet_name);\r
da848c4f 119\r
17f187e6
D
120 if (!applet_obj)\r
121 return;\r
da848c4f
D
122\r
123 // Create the applet and load the content\r
124 applet = new _kiwi.model.Applet();\r
17f187e6 125 applet.load(new applet_obj({_applet_name: applet_name}));\r
da848c4f 126\r
17f187e6
D
127 // Add it into the tab list if needed (default)\r
128 if (!options.no_tab)\r
129 _kiwi.app.applet_panels.add(applet);\r
da848c4f
D
130\r
131\r
132 return applet;\r
133 },\r
134\r
135\r
17f187e6
D
136 getApplet: function (applet_name) {\r
137 return _kiwi.applets[applet_name] || null;\r
138 },\r
139\r
140\r
da848c4f
D
141 register: function (applet_name, applet) {\r
142 _kiwi.applets[applet_name] = applet;\r
143 }\r
19cc6364 144});