Skip to content

Commit

Permalink
put it through the JSLint wash
Browse files Browse the repository at this point in the history
  • Loading branch information
paulca committed Jul 15, 2011
1 parent 2570bde commit b6b4583
Showing 1 changed file with 77 additions and 59 deletions.
136 changes: 77 additions & 59 deletions whenever.js
@@ -1,8 +1,11 @@
var whenever = function(element){
var whenever = function (element) {
"use strict";
var choose_element, choose_function, binding, is, given, then;

// Decide what element to bind to
// if the element has been defined, use that
// otherwise, use an anchor containing the text
var choose_element = function(element){
choose_element = function (element) {
if(typeof whenever.definitions[element] === 'string')
{
return whenever.definitions[element];
Expand All @@ -20,153 +23,166 @@ var whenever = function(element){
// choose a function from a collection of functions
// returns the function to call as the first argument
// and the arguments to call it with as the second
var choose_function = function(string_to_match, collection){
var args = [];
var function_to_call;
choose_function = function (string_to_match, collection) {
var args, function_to_call, potential_match, matches;
args = [];
if(typeof collection[string_to_match] === 'function')
{
function_to_call = collection[string_to_match];
}
else
{
for(var potential_match in collection)
for(potential_match in collection)
{
var matches = string_to_match.match(new RegExp(potential_match))
if(matches)
if(collection.hasOwnProperty(potential_match))
{
matches.shift();
function_to_call = collection[potential_match];
args = matches;
matches = string_to_match.match(new RegExp(potential_match));
if(matches)
{
matches.shift();
function_to_call = collection[potential_match];
args = matches;
}
}
}
}
if(typeof function_to_call === 'function')
{
return function(){
return function () {
return function_to_call.apply(this, args);
}
};
}
else
{
throw("'" + string_to_match + "' was not found");
}
}
};

// A private object to store the element, the event and the action
var binding = {
binding = {
selector: choose_element(element),
conditions: [],
actions: [],
action: undefined
};
var is, given, then;

// store the event and return an object for chaining
is = function(event){
is = function (event) {
if(whenever.translations[event])
{
binding.event = whenever.translations[event]
binding.event = whenever.translations[event];
}
else
{
throw(event + ' does not map to an event')
throw(event + ' does not map to an event');
}
return {
and: given,
given: given,
then: then
}
}
};
};

// store a predicate along the chain
given = function(condition){
given = function (condition) {

binding.conditions.push(condition)
binding.conditions.push(condition);

// and and then are before we've called then
return {
and: given,
then: then
}
}
};
};

// call the binding function with condition as appropriate
then = function(action){
then = function (action) {
var function_to_apply;

// add the action to the chain of actions
binding.actions.push(action)
binding.actions.push(action);

// unbind the previous 'then' or 'and'
if(typeof binding.action === 'function' && binding.event != 'ready' && binding.event != 'load')
if(typeof binding.action === 'function'
&& binding.event !== 'ready'
&& binding.event !== 'load')
{
whenever.unbind_function_to_event(
binding.selector,
binding.event,
binding.action
)
);
}

// create a function that runs the condition before running the actions
function_to_apply = function(){
var out;
for(var i = 0; i<binding.conditions.length; i++)
function_to_apply = function () {
var i, out;
for(i = 0; i<binding.conditions.length; i = i+1)
{
if(choose_function(binding.conditions[i], whenever.conditions).apply(this) === false)
if(choose_function (binding.conditions[i], whenever.conditions)
.apply(this) === false)
{
return false
return false;
}
}
if(binding.event != 'ready' && binding.event != 'load')
if(binding.event !== 'ready' && binding.event !== 'load')
{
for(var i = 0; i<binding.actions.length; i++)
for(i = 0; i<binding.actions.length; i=i+1)
{
out = choose_function(binding.actions[i], whenever.actions).apply(this);
out = choose_function (binding.actions[i], whenever.actions)
.apply(this);
}
}
else
{
out = choose_function(action, whenever.actions).apply(this);
out = choose_function (action, whenever.actions).apply(this);
}

// return the result of the last function in the chain
return out;
}
};

// save the function to check for later
binding.action = function_to_apply
binding.action = function_to_apply;

// bind it!
whenever.bind_function_to_event(
binding.selector,
binding.event,
binding.action
)
);

// only and left
return {
and: then
}
}
};
};

// is follows whenever
return {
is: is
}
};
};

// extend object so we can build up definitions more than once
whenever.definer = function(){}
whenever.definer.prototype.add = function(object){
for(var label in object)
whenever.definer = function () {
"use strict";
};
whenever.definer.prototype.add = function (object) {
"use strict";
var label;
for(label in object)
{
this[label] = object[label];
if(object.hasOwnProperty(label))
{
this[label] = object[label];
}
}
};

whenever.definitions = new whenever.definer;
whenever.actions = new whenever.definer;
whenever.conditions = new whenever.definer;
whenever.definitions = new whenever.definer();
whenever.actions = new whenever.definer();
whenever.conditions = new whenever.definer();

whenever.translations = {
'blurred': 'blur',
Expand All @@ -180,21 +196,23 @@ whenever.translations = {
'changed': 'change'
};

whenever.unbind_function_to_event = function(selector, event, action){
return jQuery(document).ready(function(){
whenever.unbind_function_to_event = function (selector, event, action) {
"use strict";
return jQuery(document).ready(function () {
jQuery(document).undelegate(selector, event, action);
})
}
});
};

whenever.bind_function_to_event = function(selector, event, action){
return jQuery(document).ready(function(){
whenever.bind_function_to_event = function (selector, event, action) {
"use strict";
return jQuery(document).ready(function () {
if(event === 'ready' || event === 'load')
{
action.apply(selector)
action.apply(selector);
}
else
{
return jQuery(document).delegate(selector, event, action);
}
});
}
};

0 comments on commit b6b4583

Please sign in to comment.