836c1d1ea70b8a8c103e8e1df96bb48935d6ce2d
[KiwiIRC.git] / client_backbone / 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 loaded_applet: null,
7
8 initialize: function (attributes) {
9 // Temporary name
10 var name = "applet_"+(new Date().getTime().toString()) + Math.ceil(Math.random()*100).toString();
11 this.view = new kiwi.view.Applet({model: this, name: name});
12
13 this.set({
14 "name": name
15 }, {"silent": true});
16 },
17
18 // Load an applet within this panel
19 load: function (applet_object, applet_name) {
20 if (typeof applet_object === 'object') {
21 // Make sure this is a valid Applet
22 if (applet_object.get || applet_object.extend) {
23
24 // Try find a title for the applet
25 this.set('title', applet_object.get('title') || 'Unknown Applet');
26
27 // Update the tabs title if the applet changes it
28 applet_object.bind('change:title', function (obj, new_value) {
29 this.set('title', new_value);
30 }, this);
31
32 // If this applet has a UI, add it now
33 this.view.$el.html('');
34 if (applet_object.view) {
35 this.view.$el.append(applet_object.view.$el);
36 }
37
38 // Keep a reference to this applet
39 this.loaded_applet = applet_object;
40 }
41
42 } else if (typeof applet_object === 'string') {
43 // Treat this as a URL to an applet script and load it
44 this.loadFromUrl(applet_object, applet_name);
45 }
46
47 return this;
48 },
49
50 loadFromUrl: function(applet_url, applet_name) {
51 var that = this;
52
53 this.view.$el.html('Loading..');
54 $script(applet_url, function () {
55 // Check if the applet loaded OK
56 if (!kiwi.applets[applet_name]) {
57 that.view.$el.html('Not found');
58 return;
59 }
60
61 // Load a new instance of this applet
62 that.load(new kiwi.applets[applet_name]());
63 });
64 },
65
66 close: function () {
67 this.view.$el.remove();
68 this.destroy();
69
70 this.view = undefined;
71
72 this.closePanel();
73 }
74 });