Created
November 25, 2011 22:43
-
-
Save voidfiles/1394593 to your computer and use it in GitHub Desktop.
Flickrs actionQueue code isolated
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(F) { | |
var registered_ids = {}, | |
id_to_module_map = {}, | |
interim_actions = {}, | |
cleanup_actions = {}, | |
clicked_ids = {}, | |
queueing = true; | |
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) { | |
for (var key_id in registered_ids) { | |
if (registered_ids.hasOwnProperty(key_id) && actual_id.search(key_id) !== -1) { | |
return key_id; | |
} | |
} | |
return actual_id; | |
} | |
F.actionQueue = { | |
register: function(id, callbacks, required_module) { | |
if (id instanceof Array) { | |
for (var n = 0, len = id.length; n < len; n++) { | |
register_id(id[n], callbacks, required_module); | |
} | |
} else { | |
register_id(id, callbacks, required_module); | |
} | |
}, | |
queue_click: function(id) { | |
var id_key = rectify_id(id); | |
if (queueing && 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) { | |
return false; | |
} | |
return true; | |
}, | |
module_loaded: function(module_name, id_to_restrict_to) { | |
queueing = false; | |
for (var 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); | |
} | |
} | |
} | |
}; | |
}(F)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment