[NEW] Initial commit of Mojo extensions project.

This commit is contained in:
Sami Samhuri 2009-11-26 19:58:08 -08:00
commit 7be7950555
4 changed files with 208 additions and 0 deletions

15
ext.js Normal file
View file

@ -0,0 +1,15 @@
Mojo.Ext = {};
(function(){
var sources = [
'mojo-ext/ext/typeof.js',
'mojo-ext/ext/pretty-print.js',
'mojo-ext/ext/scene-assistant-base.js'
];
for (var i = 0; i < sources.length; ++i) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = sources[i];
document.getElementsByTagName('head')[0].appendChild(script);
}
})();

66
ext/pretty-print.js Normal file
View file

@ -0,0 +1,66 @@
// Pretty printer, handy for logging
Mojo.Log.pp = function(x, indent, key) {
if (indent === undefined) {
indent = 0;
}
var space = "";
for (var j = 0; j < indent; j++) {
space += " ";
}
switch (typeOf(x)) {
case 'object':
if (key) {
print(space + key + ': {');
}
else {
print(space + '{');
}
for (var a in x) {
Mojo.Log.pp(x[a], 1+indent, a);
}
print(space + "},");
break;
case 'string':
if (key) {
print(space + key + ': "' + x + '",');
}
else {
print(space + '"' + x + '",');
}
break;
case 'array':
if (key) {
print(space + key + ': [');
}
else {
print(space + '[');
}
for (var i = 0; i < x.length; ++i) {
Mojo.Log.pp(x[i], 1+indent);
}
print(space + '],');
break;
case 'null':
if (key) {
print(space + key + ': (null),');
}
else {
print(space + '(null),');
}
break;
default:
if (key) {
print(space + key + ": " + x + ',');
}
else {
print(space + x + ',');
}
break;
}
};

109
ext/scene-assistant-base.js Normal file
View file

@ -0,0 +1,109 @@
Mojo.Ext.SceneAssistantBase = Class.create({
setup: function() {
var sc = this.controller.stageController;
if (this.appMenuModel !== undefined) {
this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems:true}, this.appMenuModel);
}
else if (sc.appMenuModel !== undefined) {
this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems:true}, sc.appMenuModel);
}
},
setupListener: function(name, event, callback) {
if (this._registeredListeners === undefined) {
this._registeredListeners = {};
}
Mojo.Event.listen(this[name], event, callback);
this._registeredListeners[name] = {'event': event, 'callback': callback};
},
stopListeners: function() {
for (var name in this._registeredListeners) {
var listener = this._registeredListeners[name];
this.controller.stopListening(name, listener.event, listener.callback.bind(this));
}
},
initWidget: function(name, attributes, model) {
if (this[name + "Model"] === undefined) {
this[name + "Model"] = model;
}
this.controller.setupWidget(name, attributes, model);
if (this[name] === undefined) {
this[name] = this.controller.get(name);
}
return this[name];
},
initButton: function(name, label_or_model, callback, type_or_attributes) {
var model, attributes;
if (typeof label_or_model === 'string') {
model = {
"label" : label_or_model,
"buttonClass" : "",
"disabled" : false
};
}
else {
model = label_or_model;
}
if (type_or_attributes === undefined) {
attributes = {};
}
else if (typeof type_or_attributes === 'string') {
attributes = {
"type" : type_or_attributes
};
}
else {
attributes = type_or_attributes;
}
this.initWidget(name, attributes, model);
this.setupListener(name, Mojo.Event.tap, callback.bind(this));
return this[name];
},
initList: function(name, items_or_model, callback, itemTemplate_or_attributes) {
var model, attributes;
if (typeOf(items_or_model) === 'array') {
model = {
"listTitle" : 'List',
"items" : items_or_model
};
}
else {
model = items_or_model;
}
if (itemTemplate_or_attributes === undefined) {
attributes = {};
}
else if (typeof itemTemplate_or_attributes === 'string') {
attributes = {
"itemTemplate" : itemTemplate_or_attributes
};
}
else {
attributes = itemTemplate_or_attributes;
}
this.initWidget(name, attributes, model);
this.setupListener(name, Mojo.Event.listTap, callback.bind(this));
return this[name];
}
});
//
this.listModel = {listTitle:'Topic News', items:this.stories};
var listAttributes = {
itemTemplate: 'feed/listitem',
emptyTemplate: 'feed/emptylist',
dividerFunction: this.getDivider.bind(this),
dividerTemplate: 'feed/divider'
};
// Set up the attributes & model for the List widget:
this.controller.setupWidget('storyList', listAttributes, this.listModel);
this.storyList = this.controller.get('storyList');
this.listClickHandler = this.listClickHandler.bind(this);
Mojo.Event.listen(this.storyList, Mojo.Event.listTap, this.listClickHandler);

18
ext/typeof.js Normal file
View file

@ -0,0 +1,18 @@
// Provide a proper typeOf function since JavaScript's typeof is broken.
// (the native typeof operator returns 'object' for Objects, Arrays, and null)
function typeOf(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (typeof value.length === 'number' &&
!(value.propertyIsEnumerable('length')) &&
typeof value.splice === 'function') {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
}