2013-12-04 17:24:04 +00:00
|
|
|
define([
|
|
|
|
"util/dom2"
|
|
|
|
], function() {
|
2013-08-22 06:35:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
A module for translating UI interactions into events for the rest of the application. Basically it's a pubsub with a dom layer. Listens for menu events, as well as offering an API for firing and subscribing to arbitrary events.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
var commands = {};
|
|
|
|
|
2013-11-25 01:53:55 +00:00
|
|
|
//commands can pass a callback, although most don't respond that way
|
|
|
|
var fire = function(command, argument, callback) {
|
2013-08-22 06:35:47 +00:00
|
|
|
if (!commands[command]) return;
|
2013-09-02 01:26:22 +00:00
|
|
|
var registry = commands[command].slice();
|
|
|
|
registry.forEach(function(entry) {
|
2013-12-02 23:17:46 +00:00
|
|
|
var result = entry.callback.apply(entry.scope || null, argument instanceof Array ? argument : [argument], callback);
|
2013-09-02 01:26:22 +00:00
|
|
|
});
|
2013-08-22 06:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var register = function(command, listener, scope) {
|
|
|
|
if (!commands[command]) {
|
|
|
|
commands[command] = [];
|
|
|
|
}
|
|
|
|
commands[command].push({
|
|
|
|
callback: listener,
|
|
|
|
scope: scope
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//delegate for all elements that have a command attribute
|
|
|
|
//may want to add more listeners for other UI elements (select)
|
|
|
|
document.body.on("click", function(e) {
|
2013-08-23 23:03:46 +00:00
|
|
|
//cancel on inputs, selectboxes
|
|
|
|
if (["input", "select"].indexOf(e.target.tagName.toLowerCase()) >= 0) return;
|
2013-08-22 06:35:47 +00:00
|
|
|
//delegate all items with a command attribute
|
|
|
|
if (e.target.hasAttribute("command")) {
|
|
|
|
var command = e.target.getAttribute("command");
|
|
|
|
var arg = e.target.getAttribute("argument");
|
|
|
|
fire(command, arg);
|
2013-11-06 17:55:11 +00:00
|
|
|
e.preventDefault();
|
2013-08-22 06:35:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-23 23:03:46 +00:00
|
|
|
document.body.on("change", function(e) {
|
|
|
|
if (e.target.hasAttribute("command")) {
|
|
|
|
var command = e.target.getAttribute("command");
|
|
|
|
var arg = e.target.value;
|
|
|
|
fire(command, arg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-12-16 16:51:56 +00:00
|
|
|
var facade = {
|
2013-08-22 06:35:47 +00:00
|
|
|
fire: fire,
|
2013-12-16 16:51:56 +00:00
|
|
|
on: register,
|
|
|
|
list: []
|
2013-08-22 06:35:47 +00:00
|
|
|
};
|
2013-12-16 16:51:56 +00:00
|
|
|
|
|
|
|
//this is kind of a hack, since it updates as async
|
|
|
|
//we'll have to investigate a Require plugin if this is needed at startup
|
|
|
|
//alternatively, we could have init wait for the loaded event
|
|
|
|
var listRequest = new XMLHttpRequest();
|
|
|
|
listRequest.open("GET", "config/commands.json");
|
|
|
|
listRequest.onerror = listRequest.onLoad = function() {
|
|
|
|
if (listRequest.responseText) {
|
|
|
|
var list = JSON.parse(listRequest.responseText);
|
|
|
|
facade.list.push.apply(facade.list, list);
|
|
|
|
fire("command:loaded-list");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return facade;
|
2013-08-22 06:35:47 +00:00
|
|
|
|
|
|
|
});
|