Created
November 27, 2011 19:42
-
-
Save voidfiles/1398040 to your computer and use it in GitHub Desktop.
actionQueue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(A) { | |
var registered_ids = {}, | |
id_to_module_map = {}, | |
interim_actions = {}, | |
cleanup_actions = {}, | |
clicked_ids = {}, | |
queueing = {}; | |
function register_id(id, callbacks, required_module) { | |
id = id.replace('*', '.*'); | |
registered_ids[id] = true; | |
id_to_module_map[id] = required_module; | |
interim_actions[id] = callbacks.interim; | |
cleanup_actions[id] = callbacks.cleanup; | |
} | |
function rectify_id(actual_id) { | |
var key_id; // this was a jslint clean up | |
for (key_id in registered_ids) { | |
if (registered_ids.hasOwnProperty(key_id) && actual_id.search(key_id) !== -1) { | |
return key_id; | |
} | |
} | |
return actual_id; | |
} | |
A.actionQueue = { | |
register: function(id, callbacks, required_module) { | |
var n, | |
len; // this was a jslint cleanup | |
if (id instanceof Array) { | |
for (n = 0, len = id.length; n < len; n++) { | |
register_id(id[n], callbacks, required_module); | |
} | |
} else { | |
register_id(id, callbacks, required_module); | |
} | |
/* this is to handle multiple modules ending at there own time */ | |
if(typeof queueing[required_module] === 'undefined'){ | |
queueing[required_module] = true; | |
} | |
}, | |
queue_click: function(id) { | |
var id_key = rectify_id(id), | |
required_module = id_to_module_map[id]; | |
if (queueing[required_module] === true && id && id_key && registered_ids[id_key]) { | |
clicked_ids[id_key] = true; | |
if (typeof interim_actions[id_key] === 'function') { | |
interim_actions[id_key].apply(this, arguments); | |
} | |
return false; | |
} else if (queueing[required_module] === true) { | |
return false; | |
} | |
return true; | |
}, | |
module_loaded: function(module_name, id_to_restrict_to) { | |
var id; // jslint cleanup | |
queueing[module_name] = false; // better handling of multiple modules | |
for (id in id_to_module_map) { | |
if (id_to_module_map.hasOwnProperty(id) && clicked_ids[id] && id_to_module_map[id] === module_name) { | |
cleanup_actions[id](id_to_restrict_to ? id_to_restrict_to : id); | |
} | |
} | |
}, | |
refresh_module: function(){ | |
/* I need to add this hook in here for testing purposes. */ | |
registered_ids = {}; | |
id_to_module_map = {}; | |
interim_actions = {}; | |
cleanup_actions = {}; | |
clicked_ids = {}; | |
queueing = {}; | |
} | |
}; | |
}(A)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment