1 _kiwi
.model
.Applet
= _kiwi
.model
.Panel
.extend({
2 initialize: function (attributes
) {
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
});
11 // Holds the loaded applet
12 this.loaded_applet
= null;
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
) {
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());
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
);
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
);
36 // Keep a reference to this applet
37 this.loaded_applet
= applet_object
;
39 this.loaded_applet
.trigger('applet_loaded');
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
);
51 loadFromUrl: function(applet_url
, applet_name
) {
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());
62 // Load a new instance of this applet
63 that
.load(new _kiwi
.applets
[applet_name
]());
69 this.view
.$el
.remove();
72 this.view
= undefined;
74 // Call the applets dispose method if it has one
75 if (this.loaded_applet
&& this.loaded_applet
.dispose
) {
76 this.loaded_applet
.dispose();
82 isApplet: function () {
89 // Load an applet type once only. If it already exists, return that
90 loadOnce: function (applet_name
) {
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;
97 // Ignore if it doesn't have an applet loaded
98 if (!panel
.loaded_applet
) return;
100 if (panel
.loaded_applet
.get('_applet_name') === applet_name
) {
105 if (applet
) return applet
;
108 // If we didn't find an instance, load a new one up
109 return this.load(applet_name
);
113 load: function (applet_name
, options
) {
114 var applet
, applet_obj
;
116 options
= options
|| {};
118 applet_obj
= this.getApplet(applet_name
);
123 // Create the applet and load the content
124 applet
= new _kiwi
.model
.Applet();
125 applet
.load(new applet_obj({_applet_name
: applet_name
}));
127 // Add it into the tab list if needed (default)
129 _kiwi
.app
.applet_panels
.add(applet
);
136 getApplet: function (applet_name
) {
137 return _kiwi
.applets
[applet_name
] || null;
141 register: function (applet_name
, applet
) {
142 _kiwi
.applets
[applet_name
] = applet
;