From 7be79505559fc300049c14f8cf68ead9ad57eba0 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Thu, 26 Nov 2009 19:58:08 -0800 Subject: [PATCH] [NEW] Initial commit of Mojo extensions project. --- ext.js | 15 +++++ ext/pretty-print.js | 66 ++++++++++++++++++++++ ext/scene-assistant-base.js | 109 ++++++++++++++++++++++++++++++++++++ ext/typeof.js | 18 ++++++ 4 files changed, 208 insertions(+) create mode 100644 ext.js create mode 100644 ext/pretty-print.js create mode 100644 ext/scene-assistant-base.js create mode 100644 ext/typeof.js diff --git a/ext.js b/ext.js new file mode 100644 index 0000000..4b3fad8 --- /dev/null +++ b/ext.js @@ -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); + } +})(); \ No newline at end of file diff --git a/ext/pretty-print.js b/ext/pretty-print.js new file mode 100644 index 0000000..fe32d02 --- /dev/null +++ b/ext/pretty-print.js @@ -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; + } +}; diff --git a/ext/scene-assistant-base.js b/ext/scene-assistant-base.js new file mode 100644 index 0000000..225562b --- /dev/null +++ b/ext/scene-assistant-base.js @@ -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); diff --git a/ext/typeof.js b/ext/typeof.js new file mode 100644 index 0000000..afcbb77 --- /dev/null +++ b/ext/typeof.js @@ -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; +} +