Merge branch 'ResidentBiscuit-rb_ui_work' into development
[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 // Call the inherited close()
80 this.constructor.__super__.close.apply(this, arguments);
81 },
82
83 isApplet: function () {
84 return true;
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, options) {
115 var applet, applet_obj;
116
117 options = options || {};
118
119 applet_obj = this.getApplet(applet_name);
120
121 if (!applet_obj)
122 return;
123
124 // Create the applet and load the content
125 applet = new _kiwi.model.Applet();
126 applet.load(new applet_obj({_applet_name: applet_name}));
127
128 // Add it into the tab list if needed (default)
129 if (!options.no_tab)
130 _kiwi.app.applet_panels.add(applet);
131
132
133 return applet;
134 },
135
136
137 getApplet: function (applet_name) {
138 return _kiwi.applets[applet_name] || null;
139 },
140
141
142 register: function (applet_name, applet) {
143 _kiwi.applets[applet_name] = applet;
144 }
145 });