Skip to content

Commit

Permalink
First version of event code. Import build files for datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Palmer committed Dec 28, 2007
1 parent 9467e1a commit 80f725e
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 5 deletions.
1 change: 1 addition & 0 deletions build/datasource/datasource-min.js
@@ -0,0 +1 @@
MICROSIS.util.DataSource=Class.create({initialize:function(A,B){},})
26 changes: 26 additions & 0 deletions 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) {

}
});
2 changes: 1 addition & 1 deletion build/microsis/microsis-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/templates/microsis/file.tmpl
Expand Up @@ -4,7 +4,7 @@
<head>
<meta http-equiv="content-type" content="text/html; charset={+(JsDoc.opt.e||'utf-8')+}">
<meta name="generator" content="JsDoc Toolkit 1.0">
<title>JsDoc: {+data.overview.name+}</title>
<title>Microsis: {+data.overview.name+}</title>
<link rel=stylesheet href="default.css" type="text/css" media=screen>
</head>

Expand Down
3 changes: 1 addition & 2 deletions lib/templates/microsis/index.html
Expand Up @@ -3,8 +3,7 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>JsDoc</title>
<meta name="generator" content="JsDoc Toolkit 1.0">
<title>Microsis</title>
</head>
<frameset cols="200,*">
<frame name="menu" src="file_list.html">
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/microsis/splash.html
@@ -1,6 +1,6 @@
<html>
<head>
<title>JsDoc</title>
<title>Microsis</title>
</head>
<body>
</body>
Expand Down
65 changes: 65 additions & 0 deletions 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++;
}
});
183 changes: 183 additions & 0 deletions 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);
}
};
3 changes: 3 additions & 0 deletions src/microsis/microsis.js
Expand Up @@ -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;
}
})();

0 comments on commit 80f725e

Please sign in to comment.