Merge branch 'winston' of https://github.com/M2Ys4U/KiwiIRC into winston
[KiwiIRC.git] / client / src / models / applet.js
1 _kiwi.model.Applet = _kiwi.model.Panel.extend({
2 initialize: function (attributes) {
3 // Temporary name
4 var name = "applet_"+(new Date().getTime().toString()) + Math.ceil(Math.random()*100).toString();
5 this.view = new _kiwi.view.Applet({model: this, name: name});
6
7 this.set({
8 "name": name
9 }, {"silent": true});
10
11 // Holds the loaded applet
12 this.loaded_applet = null;
13 },
14
15
16 // Load an applet within this panel
17 load: function (applet_object, applet_name) {
18 if (typeof applet_object === 'object') {
19 // Make sure this is a valid Applet
20 if (applet_object.get || applet_object.extend) {
21
22 // Try find a title for the applet
23 this.set('title', applet_object.get('title') || _kiwi.global.i18n.translate('client_models_applet_unknown').fetch());
24
25 // Update the tabs title if the applet changes it
26 applet_object.bind('change:title', function (obj, new_value) {
27 this.set('title', new_value);
28 }, this);
29
30 // If this applet has a UI, add it now
31 this.view.$el.html('');
32 if (applet_object.view) {
33 this.view.$el.append(applet_object.view.$el);
34 }
35
36 // Keep a reference to this applet
37 this.loaded_applet = applet_object;
38
39 this.loaded_applet.trigger('applet_loaded');
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
51 loadFromUrl: function(applet_url, applet_name) {
52 var that = this;
53
54 this.view.$el.html(_kiwi.global.i18n.translate('client_models_applet_loading').fetch());
55 $script(applet_url, function () {
56 // Check if the applet loaded OK
57 if (!_kiwi.applets[applet_name]) {
58 that.view.$el.html(_kiwi.global.i18n.translate('client_models_applet_notfound').fetch());
59 return;
60 }
61
62 // Load a new instance of this applet
63 that.load(new _kiwi.applets[applet_name]());
64 });
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
82 isApplet: function () {
83 return true;
84 }
85 },
86
87
88 {
89 // Load an applet type once only. If it already exists, return that
90 loadOnce: function (applet_name) {
91
92 // See if we have an instance loaded already
93 var applet = _.find(_kiwi.app.panels('applets'), function(panel) {
94 // Ignore if it's not an applet
95 if (!panel.isApplet()) return;
96
97 // Ignore if it doesn't have an applet loaded
98 if (!panel.loaded_applet) return;
99
100 if (panel.loaded_applet.get('_applet_name') === applet_name) {
101 return true;
102 }
103 });
104
105 if (applet) return applet;
106
107
108 // If we didn't find an instance, load a new one up
109 return this.load(applet_name);
110 },
111
112
113 load: function (applet_name, options) {
114 var applet, applet_obj;
115
116 options = options || {};
117
118 applet_obj = this.getApplet(applet_name);
119
120 if (!applet_obj)
121 return;
122
123 // Create the applet and load the content
124 applet = new _kiwi.model.Applet();
125 applet.load(new applet_obj({_applet_name: applet_name}));
126
127 // Add it into the tab list if needed (default)
128 if (!options.no_tab)
129 _kiwi.app.applet_panels.add(applet);
130
131
132 return applet;
133 },
134
135
136 getApplet: function (applet_name) {
137 return _kiwi.applets[applet_name] || null;
138 },
139
140
141 register: function (applet_name, applet) {
142 _kiwi.applets[applet_name] = applet;
143 }
144 });