Merge with development branch (New server codebase)
[KiwiIRC.git] / client / assets / dev / model_applet.js
1 kiwi.model.Applet = kiwi.model.Panel.extend({
2 // Used to determine if this is an applet panel. Applet panel tabs are treated
3 // differently than others
4 applet: true,
5
6
7 initialize: function (attributes) {
8 // Temporary name
9 var name = "applet_"+(new Date().getTime().toString()) + Math.ceil(Math.random()*100).toString();
10 this.view = new kiwi.view.Applet({model: this, name: name});
11
12 this.set({
13 "name": name
14 }, {"silent": true});
15
16 // Holds the loaded applet
17 this.loaded_applet = null;
18 },
19
20 // Load an applet within this panel
21 load: function (applet_object, applet_name) {
22 if (typeof applet_object === 'object') {
23 // Make sure this is a valid Applet
24 if (applet_object.get || applet_object.extend) {
25
26 // Try find a title for the applet
27 this.set('title', applet_object.get('title') || 'Unknown Applet');
28
29 // Update the tabs title if the applet changes it
30 applet_object.bind('change:title', function (obj, new_value) {
31 this.set('title', new_value);
32 }, this);
33
34 // If this applet has a UI, add it now
35 this.view.$el.html('');
36 if (applet_object.view) {
37 this.view.$el.append(applet_object.view.$el);
38 }
39
40 // Keep a reference to this applet
41 this.loaded_applet = applet_object;
42 }
43
44 } else if (typeof applet_object === 'string') {
45 // Treat this as a URL to an applet script and load it
46 this.loadFromUrl(applet_object, applet_name);
47 }
48
49 return this;
50 },
51
52 loadFromUrl: function(applet_url, applet_name) {
53 var that = this;
54
55 this.view.$el.html('Loading..');
56 $script(applet_url, function () {
57 // Check if the applet loaded OK
58 if (!kiwi.applets[applet_name]) {
59 that.view.$el.html('Not found');
60 return;
61 }
62
63 // Load a new instance of this applet
64 that.load(new kiwi.applets[applet_name]());
65 });
66 },
67
68 close: function () {
69 this.view.$el.remove();
70 this.destroy();
71
72 this.view = undefined;
73
74 // Call the applets dispose method if it has one
75 if (this.loaded_applet && this.loaded_applet.dispose) {
76 this.loaded_applet.dispose();
77 }
78
79 this.closePanel();
80 }
81 });