New IRC connection - don't send redundant con_num to client on connection error
[KiwiIRC.git] / client / assets / src / models / 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
21 // Load an applet within this panel
22 load: function (applet_object, applet_name) {
23 if (typeof applet_object === 'object') {
24 // Make sure this is a valid Applet
25 if (applet_object.get || applet_object.extend) {
26
27 // Try find a title for the applet
28 this.set('title', applet_object.get('title') || _kiwi.global.i18n.translate('client_models_applet_unknown').fetch());
29
30 // Update the tabs title if the applet changes it
31 applet_object.bind('change:title', function (obj, new_value) {
32 this.set('title', new_value);
33 }, this);
34
35 // If this applet has a UI, add it now
36 this.view.$el.html('');
37 if (applet_object.view) {
38 this.view.$el.append(applet_object.view.$el);
39 }
40
41 // Keep a reference to this applet
42 this.loaded_applet = applet_object;
43
44 this.loaded_applet.trigger('applet_loaded');
45 }
46
47 } else if (typeof applet_object === 'string') {
48 // Treat this as a URL to an applet script and load it
49 this.loadFromUrl(applet_object, applet_name);
50 }
51
52 return this;
53 },
54
55
56 loadFromUrl: function(applet_url, applet_name) {
57 var that = this;
58
59 this.view.$el.html(_kiwi.global.i18n.translate('client_models_applet_loading').fetch());
60 $script(applet_url, function () {
61 // Check if the applet loaded OK
62 if (!_kiwi.applets[applet_name]) {
63 that.view.$el.html(_kiwi.global.i18n.translate('client_models_applet_notfound').fetch());
64 return;
65 }
66
67 // Load a new instance of this applet
68 that.load(new _kiwi.applets[applet_name]());
69 });
70 },
71
72
73 close: function () {
74 this.view.$el.remove();
75 this.destroy();
76
77 this.view = undefined;
78
79 // Call the applets dispose method if it has one
80 if (this.loaded_applet && this.loaded_applet.dispose) {
81 this.loaded_applet.dispose();
82 }
83
84 this.closePanel();
85 }
86 },
87
88
89 {
90 // Load an applet type once only. If it already exists, return that
91 loadOnce: function (applet_name) {
92
93 // See if we have an instance loaded already
94 var applet = _.find(_kiwi.app.panels('applets'), function(panel) {
95 // Ignore if it's not an applet
96 if (!panel.isApplet()) return;
97
98 // Ignore if it doesn't have an applet loaded
99 if (!panel.loaded_applet) return;
100
101 if (panel.loaded_applet.get('_applet_name') === applet_name) {
102 return true;
103 }
104 });
105
106 if (applet) return applet;
107
108
109 // If we didn't find an instance, load a new one up
110 return this.load(applet_name);
111 },
112
113
114 load: function (applet_name) {
115 var applet;
116
117 // Find the applet within the registered applets
118 if (!_kiwi.applets[applet_name]) return;
119
120 // Create the applet and load the content
121 applet = new _kiwi.model.Applet();
122 applet.load(new _kiwi.applets[applet_name]({_applet_name: applet_name}));
123
124 // Add it into the tab list
125 _kiwi.app.applet_panels.add(applet);
126
127
128 return applet;
129 },
130
131
132 register: function (applet_name, applet) {
133 _kiwi.applets[applet_name] = applet;
134 }
135 });