Skip to content

Instantly share code, notes, and snippets.

@gilesbradshaw
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilesbradshaw/086cd97a885a6c51770e to your computer and use it in GitHub Desktop.
Save gilesbradshaw/086cd97a885a6c51770e to your computer and use it in GitHub Desktop.
Component library
var __componentLibrary = function () {
//an event source lets to add and remove listeners, trigger events and get the last value triggered
var EventSource = function () {
var _this = this;
_this.events = {};
_this.values = {};
_this.addEventListener = function (name, handler, noInitialEvent) {
if (_this.events.hasOwnProperty(name))
_this.events[name].push(handler);
else
_this.events[name] = [handler];
if (!noInitialEvent) {
$(document).ready(function () {
if (this.get(name)) {
//handler(this.get(name));
handler.apply(_this, this.get(name));
}
}.bind(this));
}
return function () {
_this.removeEventListener(name, handler);
};
};
_this.removeEventListener = function (name, handler) {
if (!_this.events.hasOwnProperty(name))
return;
var index = _this.events[name].indexOf(handler);
if (index != -1)
_this.events[name].splice(index, 1);
};
_this.get = function (name) {
return _this.values[name];
};
_this.set = function (name, value) {
_this.values[name] = value;
};
_this.fireEvent = function (name, args) {
$(document).ready(function () {
if (!args || !args.length)
args = [];
_this.values[name] = args;
if (!_this.events.hasOwnProperty(name))
return;
var evs = _this.events[name], l = evs.length;
for (var i = 0; i < l; i++) {
evs[i].apply(null, args);
}
});
};
};
var windows = {
_default: function (container, id) {
var element = $("<div/>").attr("id", id).hide();
$(container).prepend(element);
return {
content: element,
show: function () {
element.show();
},
hide: function () {
element.hide();
}
};
}
}
var myLibrary = {
eventFactory: (function () {
var eventSources = {}
var eventSelectors = {}
return {
addEventSource: function (name) {
eventSources[name] = eventSources[name] || new EventSource();
return eventSources[name];
},
//selects between a number of different sources
eventSelector: function (name) {
if (eventSelectors[name])
return eventSelectors[name];
return eventSelectors[name] =
(function () {
var myEventSource = new EventSource();
eventSources[name] = myEventSource;
var handler = function () {
myEventSource.fireEvent(name, [].slice.call(arguments, 0));
};
var currentEventSource = undefined;
var currentEvent = undefined;
return {
setEventSource: function (eventSource, event) {
if (currentEventSource) {
eventSources[currentEventSource].removeEventListener(currentEvent, handler);
}
currentEventSource = eventSource;
currentEvent = event;
if (eventSource) {
if (eventSources[eventSource]) {
eventSources[eventSource].addEventListener(event, handler);
}
}
else {
myEventSource.fireEvent(name, []);
}
},
eventSource: myEventSource
};
})();
},
//takes a list of event sources + controlSelectors - if eventSource has messages and control is checked raises true else false
eventMultiplexor: function (name, config) {
if (eventSources[name])
return eventSources[name]
var mySource = new EventSource();
var refresher = function () {
mySource.fireEvent(name, []);
$(config).each(function (index, item) {
if (item.test()) {
var data = myLibrary.eventFactory.addEventSource(item.eventSource).get(item.event);
mySource.fireEvent(name, data);
}
});
};
$(config).each(function (index, item) {
myLibrary.eventFactory.addEventSource(item.eventSource).addEventListener(
item.event,
function () {
if (item.test()) {
mySource.fireEvent(name, [].slice.call(arguments, 0));
}
}
);
if (item.change) {
item.change(refresher);
}
});
mySource.listen = function (func) {
mySource.addEventListener(name, func);
};
mySource.unListen = function (func) {
mySource.removeEventListener(name, func);
};
refresher();
return eventSources[name] = mySource;
},
eventSources: eventSources
}
})(),
eventDisplayers: {
raise: {
wait: function (name) {
myLibrary.eventFactory.addEventSource(name).fireEvent("messages", [[
function () { return $("<div/>").append($("<img/>").attr("src", "/dealerweb/images/wait.gif")); }
]]);
}
},
cancel: function (name) {
myLibrary.eventFactory.addEventSource(name).fireEvent("messages");
},
//fireMessages
//gets messages out of the DOM fires them off then removes them
getMessagesFromAjaxResult: function (className, eventSource, event) {
__componentLibrary.eventFactory.addEventSource(eventSource).fireEvent(event, [$(className).children().toArray().map(function (e) { return $(e).text(); })]);
//$(className).remove();
},
//becomes inefficient for lots of messages
eventLister: function (container, id, eventSource, eventName) {
var lister;
var ret = function () {
if (!lister) {
lister = windows[myLibrary.defaultWindow](container, id);
}
__componentLibrary.eventFactory.addEventSource(eventSource).addEventListener(eventName, function (messages) {
if (messages && messages.length) {
lister.show();
var elements = messages.map(function (m) {
if ($.isFunction(m)) {
return $("<div/>").append(m());
}
else {
return $("<div/>").text(m);
}
});
lister.content.empty().append(
elements
);
}
else {
lister.content.html("");
lister.hide()
}
});
}
ret.lister = function (name) {
lister = windows[name](container, id);
return ret;
}
return ret;
}
},
addVariables: function (variables) {
myLibrary.variables = $.extend(true, {}, myLibrary.variables, variables);
},
addWindows:function(_windows) {
$.extend(windows,_windows);
},
defaultWindow:"_default",
//extension point for functions to be called from asp pages
pages: {}
};
return myLibrary;
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment