From 80f725e7710a3d09914dc9726f0daacdbbcba213 Mon Sep 17 00:00:00 2001 From: Justin Palmer Date: Fri, 28 Dec 2007 00:44:37 -0800 Subject: [PATCH] First version of event code. Import build files for datasource --- build/datasource/datasource-min.js | 1 + build/datasource/datasource.js | 26 ++++ build/microsis/microsis-min.js | 2 +- lib/templates/microsis/file.tmpl | 2 +- lib/templates/microsis/index.html | 3 +- lib/templates/microsis/splash.html | 2 +- src/datasource/datasource.js | 65 ++++++++++ src/event/event.js | 183 +++++++++++++++++++++++++++++ src/microsis/microsis.js | 3 + 9 files changed, 282 insertions(+), 5 deletions(-) create mode 100644 build/datasource/datasource-min.js create mode 100644 build/datasource/datasource.js create mode 100644 src/datasource/datasource.js create mode 100644 src/event/event.js diff --git a/build/datasource/datasource-min.js b/build/datasource/datasource-min.js new file mode 100644 index 0000000..f4b20df --- /dev/null +++ b/build/datasource/datasource-min.js @@ -0,0 +1 @@ +MICROSIS.util.DataSource=Class.create({initialize:function(A,B){},}) \ No newline at end of file diff --git a/build/datasource/datasource.js b/build/datasource/datasource.js new file mode 100644 index 0000000..18a1331 --- /dev/null +++ b/build/datasource/datasource.js @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2007 Justin Palmer. All Rights Reserved + * Licensed under BSD license + * Version 0.1.0 + */ + +/** + * A powerful interface for accessing different types of data. + * Data can come from be local such as Arrays and JSON or returned + * remoteley from an XHR request. + * @namespace MICROSIS.util + * @requires microsis + * @title Datasource Utility + */ + +/** + * @class DataSource + * @constructor + * @param oLiveData {Object} Pointer to a live database + * @param oConfigs {Object} (optional) Configuration hash + */ +MICROSIS.util.DataSource = Class.create({ + initialize: function(oLiveData, oConfigs) { + + } +}); \ No newline at end of file diff --git a/build/microsis/microsis-min.js b/build/microsis/microsis-min.js index fb9786a..690a080 100644 --- a/build/microsis/microsis-min.js +++ b/build/microsis/microsis-min.js @@ -1 +1 @@ -var fook="wee";if(typeof MICROSIS=="undefined"||!MICROSIS){var MICROSIS={}}MICROSIS.namespace=function(){var A=$A(arguments),C=MICROSIS,B;A.each(function(D){B=D.split(".");B.without("MICROSIS").each(function(E){C[E]=C[E]||{};C=C[E]})});return C};(function(){MICROSIS.namespace("util","widget")})() \ No newline at end of file +if(typeof MICROSIS=="undefined"||!MICROSIS){var MICROSIS={}}MICROSIS.namespace=function(){var A=$A(arguments),C=MICROSIS,B;A.each(function(D){B=D.split(".");B.without("MICROSIS").each(function(E){C[E]=C[E]||{};C=C[E]})});return C};(function(){MICROSIS.namespace("util","widget")})() \ No newline at end of file diff --git a/lib/templates/microsis/file.tmpl b/lib/templates/microsis/file.tmpl index 2cecfa0..14740e7 100644 --- a/lib/templates/microsis/file.tmpl +++ b/lib/templates/microsis/file.tmpl @@ -4,7 +4,7 @@ - JsDoc: {+data.overview.name+} + Microsis: {+data.overview.name+} diff --git a/lib/templates/microsis/index.html b/lib/templates/microsis/index.html index 3df8e5b..ade2cab 100644 --- a/lib/templates/microsis/index.html +++ b/lib/templates/microsis/index.html @@ -3,8 +3,7 @@ - JsDoc - + Microsis diff --git a/lib/templates/microsis/splash.html b/lib/templates/microsis/splash.html index 8c74951..1ad3531 100644 --- a/lib/templates/microsis/splash.html +++ b/lib/templates/microsis/splash.html @@ -1,6 +1,6 @@  - JsDoc + Microsis diff --git a/src/datasource/datasource.js b/src/datasource/datasource.js new file mode 100644 index 0000000..3e6d6eb --- /dev/null +++ b/src/datasource/datasource.js @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2007 Justin Palmer. All Rights Reserved + * Licensed under BSD license + * Version 0.1.0 + */ + +/** + * A powerful interface for accessing different types of data. + * Data can come from be local such as Arrays and JSON or returned + * remoteley from an XHR request. + * @namespace MICROSIS.util + * @requires microsis + * @title Datasource Utility + */ + +/** + * @class DataSource + * @constructor + * @param oLiveData {Object} Pointer to a live database + * @param oConfigs {Object} (optional) Configuration hash + */ +MICROSIS.util.DataSource = Class.create({ + + initialize: function(oLiveData, oConfigs) { + if(oConfigs && (oConfigs.constructor == Object)) { + Object.extend(this, oConfigs); + } + + if(!oLiveData) return; + + if(oLiveData.nodeType && oLiveData.nodeType == 9) { + this.dataType = MICROSIS.util.DataSource.TYPE_XML; + } else if(Object.isArray(oLiveData)) { + this.dataType = MICROSIS.util.DataSource.TYPE_JSARRAY; + } else if(Object.isString(oLiveData)) { + this.dataType = MICROSIS.util.DataSource.TYPE_XHR; + } else if(Object.isFunction(oLiveData)) { + this.dataType = MICROSIS.util.DataSource.TYPE_JSFUNCTION; + } else if(oliveData.nodeName && (oLiveData.nodeName.toLowerCase() == "table")) { + this.dataType = MICROSIS.util.DataSource.TYPE_HTMLTABLE; + } else if(Object.isObject(oLiveData)) { + this.dataType = MICROSIS.util.DataSource.TYPE_JSON; + } else { + this.dataType = MICROSIS.util.DataSource.TYPE_UNKNOWN; + } + + this.liveData = oLiveData; + this._oQueue = {interval: null, conn: null, request: []}; + + // Validate and initialize public configuration + var maxCacheEntries = this.maxCacheEntries; + if(!Object.isNumber(maxCacheEntries) || (maxCacheEntries < 0)) + maxCacheEntries = 0; + + // Initialize local cache + if(maxCacheEntries > 0 && !this._aCache) + this._aCache = []; + + // Initialized interval tracker + this._aIntervals = []; + + this._sName = "DataSource instance" + MICROSIS.util.DataSource._nIndex; + MICROSIS.util.DataSource._nIndex++; + } +}); \ No newline at end of file diff --git a/src/event/event.js b/src/event/event.js new file mode 100644 index 0000000..7057c31 --- /dev/null +++ b/src/event/event.js @@ -0,0 +1,183 @@ +/** + * Copyright (c) 2007 Justin Palmer. All Rights Reserved + * Licensed under BSD license + * Version 0.1.0 + */ + +/** + * The CustomEvent object allows you to create and subscribe to interesting + * moments within your code. You can define events in one object and any number + * of different objects can subscribe to the event, including the object the event + * was created in. + * + * @param {String} name The name of the custom event that is passed to a subscribers + * callback. + * @param {Object} scope The context the custom event is invoked from. Defaults to + * the window object. + * @namespace MICROSIS.util + * @class CustomEvent + * @constructor + */ +MICROSIS.util.CustomEvent = Class.create({ + + initialize: function(name, scope) { + this.name = name; + this.scope = scope || window; + this.subscribers = []; + this.lastError = null; + }, + + subscribe: function(method, obj) { + if(!method) { + throw new Error("Invalid callback for " + this.name); + } + this.subscribers.push(new MICROSIS.util.Subscriber(method, obj)); + }, + + unsubscribe: function(method, obj) { + if(!method) + return this.cancelSubscriptions(); + var nullified = false; + this.subscribers.each(function(subscriber, index) { + if(subscriber && subscriber.isComposedOf(method, obj)) { + this._unset(index); + nullified = true; + } + }.bind(this)); + return nullified; + }, + + fire: function() { + var length = this.subscribers.length, + args = $A(arguments), + ret = true, rebuild = false; + + if(isNaN(length)) + return true; + + if(length == 0) + rebuild = true; + + this.subscribers.each(function(subscriber) { + try { + console.log("ARGS INTERNAL", args); + ret = subscriber.method.call(this.scope, args.first(), subscriber.obj); + } catch(e) { + this.lastError = e; + } + + if(ret === false) + return false; + + }.bind(this)); + + if(rebuild) this.subscribers = this.subscribers.compact(); + + return true + }, + + _unset: function(index) { + var sub = this.subscribers[index]; + if(sub) { + delete sub.method; + delete sub.obj; + } + this.subscribers[index] = null; + } + +}); + +MICROSIS.util.Subscriber = Class.create({ + initialize: function(method, obj) { + this.method = method; + this.obj = obj; + }, + + isComposedOf: function(method, obj) { + if(obj) return (this.method == method && this.obj == obj); + else return (this.method == method); + } +}); + +MICROSIS.util.EventProvider = { + __events: null, + __subscribers: null, + + subscribe: function(name, method, obj) { + this.__events = this.__events || {}; + var customEvent = this.__events[name]; + + if(customEvent) { + customEvent.subscribe(method, obj); + } else { + var subscribers = this.__subscribers = this.__subscribers || {}; + if(!subscribers[name]) + subscribers[name] = []; + + subscribers[name].push({ + method: method, + obj: obj + }); + } + }, + + unsubscribe: function(name, method, obj) { + this.__events = this.__events || {}; + var events = this.__events; + if(name) { + var customEvent = events[name]; + if(customEvent) { + return customEvent.unsubscribe(method, obj); + } + } else { + var ret = true; + var hasOwnProp = !!Object.prototype.hasOwnProperty, yes = null; + for(var prop in events) { + if(hasOwnProp) { + yes = events.hasOwnProperty(prop); + } else { + yes = !Object.isUndefined(events[prop]) && + events.constructor.prototype[prop] !== events[prop]; + } + + if(yes) { + ret = ret && events[prop].unsubscribe(method, obj); + } + } + return ret; + } + return false; + }, + + cancelSubscriptions: function(name) { + return this.unsubscribe(name); + }, + + createEvent: function(name, options) { + var events = this.__events = this.__events || {}; + var subscribers = this.__subscribers = this.__subscribers || {}; + var opts = options || {}; + + if(Object.isUndefined(events[name])) { + var scope = opts.scope || this; + var customEvent = new MICROSIS.util.CustomEvent(name, scope); + events[name] = customEvent; + var sub = subscribers[name]; + if(sub) { + sub.each(function(subscriber) { + customEvent.subscribe(subscriber.method, subscriber.obj); + }); + } + } + return events[name]; + }, + + fireEvent: function(name) { + var args = $A(arguments); + var events = this.__events = this.__events || {}; + var customEvent = events[name]; + if(!customEvent) return null; + + return customEvent.fire.apply(customEvent, args); + } +}; diff --git a/src/microsis/microsis.js b/src/microsis/microsis.js index 1508cb5..2f88ae8 100644 --- a/src/microsis/microsis.js +++ b/src/microsis/microsis.js @@ -50,4 +50,7 @@ MICROSIS.namespace = function() { (function() { MICROSIS.namespace('util', 'widget'); + Object.isObject = function(o) { + return (o && (typeof o == "object" || Object.isFunction(o))) || false; + } })();