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
7 initialize: function (attributes
) {
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
});
16 // Holds the loaded applet
17 this.loaded_applet
= null;
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
) {
27 // Try find a title for the applet
28 this.set('title', applet_object
.get('title') || 'Unknown Applet');
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
);
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
);
41 // Keep a reference to this applet
42 this.loaded_applet
= applet_object
;
44 this.loaded_applet
.trigger('applet_loaded');
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
);
56 loadFromUrl: function(applet_url
, applet_name
) {
59 this.view
.$el
.html('Loading..');
60 $script(applet_url
, function () {
61 // Check if the applet loaded OK
62 if (!_kiwi
.applets
[applet_name
]) {
63 that
.view
.$el
.html('Not found');
67 // Load a new instance of this applet
68 that
.load(new _kiwi
.applets
[applet_name
]());
74 this.view
.$el
.remove();
77 this.view
= undefined;
79 // Call the applets dispose method if it has one
80 if (this.loaded_applet
&& this.loaded_applet
.dispose
) {
81 this.loaded_applet
.dispose();
90 // Load an applet type once only. If it already exists, return that
91 loadOnce: function (applet_name
) {
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;
98 // Ignore if it doesn't have an applet loaded
99 if (!panel
.loaded_applet
) return;
101 if (panel
.loaded_applet
.get('_applet_name') === applet_name
) {
106 if (applet
) return applet
;
109 // If we didn't find an instance, load a new one up
110 return this.load(applet_name
);
114 load: function (applet_name
) {
117 // Find the applet within the registered applets
118 if (!_kiwi
.applets
[applet_name
]) return;
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
}));
124 // Add it into the tab list
125 _kiwi
.app
.applet_panels
.add(applet
);
132 register: function (applet_name
, applet
) {
133 _kiwi
.applets
[applet_name
] = applet
;