ControlInput updating nick view properly
[KiwiIRC.git] / client / assets / dev / model_applet.js
CommitLineData
eaaf73b0 1_kiwi.model.Applet = _kiwi.model.Panel.extend({\r
19cc6364
D
2 // Used to determine if this is an applet panel. Applet panel tabs are treated\r
3 // differently than others\r
4 applet: true,\r
5\r
4baeda09 6\r
19cc6364
D
7 initialize: function (attributes) {\r
8 // Temporary name\r
9 var name = "applet_"+(new Date().getTime().toString()) + Math.ceil(Math.random()*100).toString();\r
eaaf73b0 10 this.view = new _kiwi.view.Applet({model: this, name: name});\r
19cc6364
D
11\r
12 this.set({\r
13 "name": name\r
14 }, {"silent": true});\r
51ae0eb8
D
15\r
16 // Holds the loaded applet\r
17 this.loaded_applet = null;\r
19cc6364
D
18 },\r
19\r
da848c4f 20\r
19cc6364
D
21 // Load an applet within this panel\r
22 load: function (applet_object, applet_name) {\r
23 if (typeof applet_object === 'object') {\r
4baeda09
D
24 // Make sure this is a valid Applet\r
25 if (applet_object.get || applet_object.extend) {\r
26\r
27 // Try find a title for the applet\r
28 this.set('title', applet_object.get('title') || 'Unknown Applet');\r
29\r
30 // Update the tabs title if the applet changes it\r
31 applet_object.bind('change:title', function (obj, new_value) {\r
32 this.set('title', new_value);\r
33 }, this);\r
34\r
35 // If this applet has a UI, add it now\r
36 this.view.$el.html('');\r
37 if (applet_object.view) {\r
38 this.view.$el.append(applet_object.view.$el);\r
39 }\r
40\r
41 // Keep a reference to this applet\r
42 this.loaded_applet = applet_object;\r
1aed0c0b
D
43\r
44 this.loaded_applet.trigger('applet_loaded');\r
4baeda09 45 }\r
19cc6364
D
46\r
47 } else if (typeof applet_object === 'string') {\r
48 // Treat this as a URL to an applet script and load it\r
49 this.loadFromUrl(applet_object, applet_name);\r
50 }\r
4baeda09
D
51\r
52 return this;\r
19cc6364
D
53 },\r
54\r
da848c4f 55\r
19cc6364
D
56 loadFromUrl: function(applet_url, applet_name) {\r
57 var that = this;\r
58\r
59 this.view.$el.html('Loading..');\r
60 $script(applet_url, function () {\r
61 // Check if the applet loaded OK\r
eaaf73b0 62 if (!_kiwi.applets[applet_name]) {\r
19cc6364
D
63 that.view.$el.html('Not found');\r
64 return;\r
65 }\r
66\r
67 // Load a new instance of this applet\r
eaaf73b0 68 that.load(new _kiwi.applets[applet_name]());\r
19cc6364 69 });\r
ea033d12
D
70 },\r
71\r
da848c4f 72\r
ea033d12
D
73 close: function () {\r
74 this.view.$el.remove();\r
75 this.destroy();\r
76 \r
77 this.view = undefined;\r
78\r
51ae0eb8
D
79 // Call the applets dispose method if it has one\r
80 if (this.loaded_applet && this.loaded_applet.dispose) {\r
81 this.loaded_applet.dispose();\r
82 }\r
83\r
ea033d12 84 this.closePanel();\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
114 load: function (applet_name) {\r
115 var applet;\r
116\r
117 // Find the applet within the registered applets\r
118 if (!_kiwi.applets[applet_name]) return;\r
119\r
120 // Create the applet and load the content\r
121 applet = new _kiwi.model.Applet();\r
122 applet.load(new _kiwi.applets[applet_name]({_applet_name: applet_name}));\r
123\r
124 // Add it into the tab list\r
c966123a 125 _kiwi.app.applet_panels.add(applet);\r
da848c4f
D
126\r
127\r
128 return applet;\r
129 },\r
130\r
131\r
132 register: function (applet_name, applet) {\r
133 _kiwi.applets[applet_name] = applet;\r
134 }\r
19cc6364 135});