Skip to content
Browse files

reworked internals regarding module loading. Modules are now EventEmi…

…tter objects
  • Loading branch information...
1 parent c324c90 commit 97c24a6bafcbcfbd7f545c1dbc984c08e3407fca @rsms committed Apr 20, 2010
Showing with 94 additions and 73 deletions.
  1. +89 −67 client/lib/index.js
  2. +3 −4 client/lib/oui/anchor.js
  3. +2 −2 oui/builder/index.js
View
156 client/lib/index.js
@@ -6,12 +6,16 @@ window.oui = {};
if (window.OUI_DEBUG) window.oui.debug = true;
window.APP_VERSION = "{#APP_VERSION#}"; // todo: move to oui.app
+// -----------------------------------------------------------------------------
+// oui.mixin
+
// sophisticated client?
if ( typeof Object.getOwnPropertyNames === 'function'
&& typeof Object.getOwnPropertyDescriptor === 'function'
&& typeof Array.prototype.forEach === 'function')
{
window.oui.mixin = function(target) {
+ if (!target || typeof target !== 'object') return;
var i = 1, length = arguments.length, source;
for ( ; i < length; i++ ) {
// Only deal with defined values
@@ -48,6 +52,72 @@ else {
};
}
+// -----------------------------------------------------------------------------
+// oui.EventEmitter
+
+window.oui.EventEmitter = function() {};
+window.oui.mixin(window.oui.EventEmitter.prototype, {
+ addListener: function(type, once, listener) {
+ if (typeof once === 'function') {
+ listener = once;
+ once = false;
+ }
+ if (once) {
+ $(this.eventTarget || this).one(type, listener);
+ } else {
+ $(this.eventTarget || this).bind(type, listener);
+ }
+ return this;
+ },
+
+ on: function(type, once, listener) {
+ return this.addListener(type, once, listener);
+ },
+
+ one: function(type, listener) {
+ return this.addListener(type, true, listener);
+ },
+
+ removeListener: function(type, listener) {
+ $(this.eventTarget || this).unbind(type, listener);
+ return this;
+ },
+
+ emit: function(type /*[, arg[, ..]]*/) {
+ var args = [];
+ for (var i=1;i<arguments.length;i++)
+ args.push(arguments[i]);
+ return this.emitv(type, args);
+ },
+
+ emitv: function(type, args) {
+ $(this.eventTarget || this).triggerHandler(type, args);
+ return this;
+ },
+
+ // both emits an event and calls any functions on bound
+ // objects with the same name as <type>
+ trigger: function(type /*[, arg[, ..]]*/) {
+ var metaargs = [];
+ for (var i=1;i<arguments.length;i++)
+ metaargs.push(arguments[i]);
+ $(this.eventTarget || this).trigger(type, metaargs);
+ return this;
+ }
+});
+
+// -----------------------------------------------------------------------------
+// Module loading and defining
+
+var moduleHTML = function(module, query){
+ if (query) {
+ query = module.$html.find(query).clone();
+ query.ouiModule = module;
+ return query;
+ }
+ return module.$html;
+};
+
// Define a module
window.__defm = function(name, root, html, fun) {
if (arguments.length === 2) {
@@ -59,19 +129,19 @@ window.__defm = function(name, root, html, fun) {
html = null;
}
- if (!html) {
- html = function(query){
- if (!html._jQuery) {
- html._jQuery = jQuery('#'+name.replace(/\./g, '-'));
- }
- return query ? html._jQuery.find(query).clone() : html._jQuery;
- };
- }
-
- var module = {$html: html},
+ var k, v, module = new window.oui.EventEmitter(),
namep = name.split('.'),
curr = root, n, i = 0, L = namep.length-1;
+ module.id = name;
+
+ if (!html) {
+ module.__html = function(query){ return moduleHTML(module, query); };
+ $(function(){
+ module.$html = jQuery('#'+module.id.replace(/\./g, '-'));
+ });
+ }
+
for ( ; i<L; i++) {
n = namep[i];
if (curr[n] === undefined)
@@ -80,16 +150,16 @@ window.__defm = function(name, root, html, fun) {
}
// this, exports, __name, __html, __parent
- fun.call(module, module, name, html, curr);
+ fun.call(module, module, module.id, module.__html, curr);
var mname = namep[i], parent = curr[mname];
if (parent !== undefined) {
var t = typeof parent;
if (t === 'object' || t === 'function') {
window.oui.mixin(parent, module);
} else {
- for (var k in module) {
- console.warn('tried to overwrite module "'+name+'"');
+ for (k in module) {
+ console.warn('tried to overwrite module "'+module.id+'"');
break;
}
}
@@ -98,7 +168,9 @@ window.__defm = function(name, root, html, fun) {
}
};
+// -----------------------------------------------------------------------------
// oui module
+
__defm('oui', window, function(exports, __name, __html){
var EMPTYFUNC = function(){};
@@ -112,7 +184,8 @@ if (window.console === undefined || !window.oui.debug) {
group:EMPTYFUNC,
groupEnd:EMPTYFUNC,
assert:EMPTYFUNC,
- debug:EMPTYFUNC
+ debug:EMPTYFUNC,
+ info:EMPTYFUNC
};
}
else {
@@ -123,6 +196,7 @@ else {
if (console.groupEnd===undefined)console.groupEnd=EMPTYFUNC;
if (console.assert===undefined)console.assert=EMPTYFUNC;
if (console.debug===undefined)console.debug=EMPTYFUNC;
+ if (console.info===undefined)console.info=EMPTYFUNC;
if (Object.prototype.__defineGetter__) {
window.OUI_HELP = {
sections: {
@@ -167,7 +241,7 @@ else {
*
* Returns the prototype of ctor.
*/
-window.oui.inherits = function (ctor, superCtor, prototypeMixin) {
+exports.inherits = function (ctor, superCtor, prototypeMixin) {
var TempCtor = function(){};
TempCtor.prototype = superCtor.prototype;
ctor.super_ = superCtor;
@@ -196,58 +270,6 @@ exports.htmlesc = function(s, nl2br) {
return s;
};
-
-exports.EventEmitter = function() {};
-window.oui.mixin(exports.EventEmitter.prototype, {
- addListener: function(type, once, listener) {
- if (typeof once === 'function') {
- listener = once;
- once = false;
- }
- if (once) {
- $(this.eventTarget || this).one(type, listener);
- } else {
- $(this.eventTarget || this).bind(type, listener);
- }
- return this;
- },
-
- on: function(type, once, listener) {
- return this.addListener(type, once, listener);
- },
-
- one: function(type, listener) {
- return this.addListener(type, true, listener);
- },
-
- removeListener: function(type, listener) {
- $(this.eventTarget || this).unbind(type, listener);
- return this;
- },
-
- emit: function(type /*[, arg[, ..]]*/) {
- var args = [];
- for (var i=1;i<arguments.length;i++)
- args.push(arguments[i]);
- return this.emitv(type, args);
- },
-
- emitv: function(type, args) {
- $(this.eventTarget || this).triggerHandler(type, args);
- return this;
- },
-
- // both emits an event and calls any functions on bound
- // objects with the same name as <type>
- trigger: function(type /*[, arg[, ..]]*/) {
- var metaargs = [];
- for (var i=1;i<arguments.length;i++)
- metaargs.push(arguments[i]);
- $(this.eventTarget || this).trigger(type, metaargs);
- return this;
- }
-});
-
// capabilities
exports.capabilities = {
cors: (window.XMLHttpRequest &&
View
7 client/lib/oui/anchor.js
@@ -196,15 +196,14 @@ function _init() {
onHashChange();
}
});
- } else {
- exports._prevhash = '';
+ } //else {
setInterval(function(){
if (exports._prevhash !== document.location.hash){
exports._prevhash = document.location.hash;
onHashChange();
}
- }, 100);
- }
+ }, 500);
+ //}
//if (document.location.hash === '' || document.location.hash != exports._prevhash)
onHashChange();
return true;
View
4 oui/builder/index.js
@@ -278,9 +278,9 @@ mixin(Source.prototype, {
.replace(/-+/g,'_') // foo.bar-baz- => foo.bar_baz_
.replace(/\.index$/, '') // * removes ".index"
.replace(/^[_\.-]|[_\.-]$/g,'') // foo.bar_baz_ => foo.bar_baz
- .replace(/_+(\w)/g, function(str, m1){ // foo.bar_baz => foo.fooBaz
+ .replace(/_+(\w)/g, function(str, m1){ // foo.bar_baz => foo.barBaz
return m1.toUpperCase();
- })
+ })
);
},

0 comments on commit 97c24a6

Please sign in to comment.
Something went wrong with that request. Please try again.