Comparing version 0.1.8 to 0.1.9
@@ -21,3 +21,3 @@ var cwd = process.cwd(); | ||
"/**\n" + | ||
" *" + figlet + "\n" + | ||
" *" + (figlet + "\n").replace(/ +\n/g, '\n') + | ||
" *\n" + | ||
@@ -51,2 +51,1 @@ " * http://lighter.io/jymin\n" + | ||
} | ||
732
jymin.js
@@ -27,2 +27,3 @@ /** | ||
* https://github.com/zerious/jymin/blob/master/scripts/strings.js | ||
* https://github.com/zerious/jymin/blob/master/scripts/types.js | ||
* https://github.com/zerious/jymin/blob/master/scripts/url.js | ||
@@ -46,10 +47,10 @@ */ | ||
var getResponse = function ( | ||
url, // string*: The URL to request data from. | ||
data, // object: Data to post. The method is automagically "POST" if data is truey, otherwise "GET". | ||
onSuccess, // function: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure, // function: Callback to run on failure. `onFailure(response, request)`. | ||
evalJson // boolean: Whether to evaluate the response as JSON. | ||
url, // string: The URL to request data from. | ||
data, // object|: Data to post. The method is automagically "POST" if data is truey, otherwise "GET". | ||
onSuccess, // function|: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure, // function|: Callback to run on failure. `onFailure(response, request)`. | ||
evalJson // boolean|: Whether to evaluate the response as JSON. | ||
) { | ||
// If the optional data argument is omitted, shuffle it out. | ||
if (typeof data == 'function') { | ||
if (isFunction(data)) { | ||
evalJson = onFailure; | ||
@@ -91,4 +92,5 @@ onFailure = onSuccess; | ||
request.open(data ? 'POST' : 'GET', url, true); | ||
request.setRequestHeader('x-requested-with', 'XMLHttpRequest'); | ||
if (data) { | ||
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); | ||
request.setRequestHeader('content-type', 'application/x-www-form-urlencoded'); | ||
} | ||
@@ -105,9 +107,8 @@ request.send(data || null); | ||
var getJson = function ( | ||
url, // string*: The URL to request data from. | ||
onSuccess, // function: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure // function: Callback to run on failure. `onFailure(response, request)`. | ||
url, // string: The URL to request data from. | ||
onSuccess, // function|: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure // function|: Callback to run on failure. `onFailure(response, request)`. | ||
) { | ||
return getResponse(url, onSuccess, onFailure, true); | ||
}; | ||
var DEFAULT_ANIMATION_FRAME_COUNT = 40; | ||
@@ -120,40 +121,40 @@ var DEFAULT_ANIMATION_FRAME_DELAY = 20; | ||
var animate = function ( | ||
element, // string|DOMElement*: Element or ID of element to animate. | ||
styleTransitions, // object*: cssText values to animate through. | ||
onFinish, // function: Callback to execute when animation is complete. | ||
frameCount, // integer: Number of frames to animate through. (Default: 40) | ||
frameDelay, // integer: Number of milliseconds between frames. (Default: 20ms) | ||
frameIndex // integer: Index of the frame to start on. (Default: 0) | ||
element, // string|DOMElement: Element or ID of element to animate. | ||
styleTransitions, // object: cssText values to animate through. | ||
onFinish, // function|: Callback to execute when animation is complete. | ||
frameCount, // integer|: Number of frames to animate through. (Default: 40) | ||
frameDelay, // integer|: Number of milliseconds between frames. (Default: 20ms) | ||
frameIndex // integer|: Index of the frame to start on. (Default: 0) | ||
) { | ||
if (element = getElement(element)) { | ||
// Only allow one animation on an element at a time. | ||
stopAnimation(element); | ||
frameIndex = frameIndex || 0; | ||
frameCount = frameCount || DEFAULT_ANIMATION_FRAME_COUNT; | ||
frameDelay = frameDelay || DEFAULT_ANIMATION_FRAME_DELAY; | ||
var scale = Math.atan(1.5) * 2; | ||
var fraction = Math.atan(frameIndex / frameCount * 3 - 1.5) / scale + 0.5; | ||
var styles = {}; | ||
forIn(styleTransitions, function(transition, key) { | ||
var start = transition[0]; | ||
var end = transition[1]; | ||
var value; | ||
if (isNaN(start)) { | ||
value = frameIndex ? end : start; | ||
} | ||
else { | ||
value = (1 - fraction) * start + fraction * end; | ||
} | ||
styles[key] = value; | ||
}); | ||
extendStyle(element, styles); | ||
if (frameIndex < frameCount) { | ||
element.animation = setTimeout(function() { | ||
animate(element, styleTransitions, onFinish, frameCount, frameDelay, frameIndex + 1); | ||
}); | ||
} | ||
else if (onFinish) { | ||
onFinish(element); | ||
} | ||
if (element = getElement(element)) { | ||
// Only allow one animation on an element at a time. | ||
stopAnimation(element); | ||
frameIndex = frameIndex || 0; | ||
frameCount = frameCount || DEFAULT_ANIMATION_FRAME_COUNT; | ||
frameDelay = frameDelay || DEFAULT_ANIMATION_FRAME_DELAY; | ||
var scale = Math.atan(1.5) * 2; | ||
var fraction = Math.atan(frameIndex / frameCount * 3 - 1.5) / scale + 0.5; | ||
var styles = {}; | ||
forIn(styleTransitions, function(transition, key) { | ||
var start = transition[0]; | ||
var end = transition[1]; | ||
var value; | ||
if (isNaN(start)) { | ||
value = frameIndex ? end : start; | ||
} | ||
else { | ||
value = (1 - fraction) * start + fraction * end; | ||
} | ||
styles[key] = value; | ||
}); | ||
extendStyle(element, styles); | ||
if (frameIndex < frameCount) { | ||
element.animation = setTimeout(function() { | ||
animate(element, styleTransitions, onFinish, frameCount, frameDelay, frameIndex + 1); | ||
}); | ||
} | ||
else if (onFinish) { | ||
onFinish(element); | ||
} | ||
} | ||
}; | ||
@@ -165,9 +166,8 @@ | ||
var stopAnimation = function ( | ||
element // string|DOMElement*: Element or ID of element to cancel the animation on. | ||
element // string|DOMElement: Element or ID of element to cancel the animation on. | ||
) { | ||
if (element = getElement(element)) { | ||
clearTimeout(element.animation); | ||
} | ||
if (element = getElement(element)) { | ||
clearTimeout(element.animation); | ||
} | ||
}; | ||
/** | ||
@@ -178,6 +178,6 @@ * Iterate over an array, and call a function on each item. | ||
array, // Array*: The array to iterate over. | ||
callback // function*: The function to call on each item. `callback(item, index, array)` | ||
callback // Function*: The function to call on each item. `callback(item, index, array)` | ||
) { | ||
if (array) { | ||
for (var index = 0, length = array.length; index < length; index++) { | ||
for (var index = 0, length = getLength(array); index < length; index++) { | ||
var result = callback(array[index], index, array); | ||
@@ -195,4 +195,4 @@ if (result === false) { | ||
var forIn = function ( | ||
object, // object*: The object to iterate over. | ||
callback // function*: The function to call on each pair. `callback(value, key, object)` | ||
object, // Object*: The object to iterate over. | ||
callback // Function*: The function to call on each pair. `callback(value, key, object)` | ||
) { | ||
@@ -213,4 +213,4 @@ if (object) { | ||
var decorateObject = function ( | ||
object, // object*: The object to decorate. | ||
decorations // object*: The object to iterate over. | ||
object, // Object: The object to decorate. | ||
decorations // Object: The object to iterate over. | ||
) { | ||
@@ -226,2 +226,78 @@ if (object && decorations) { | ||
/** | ||
* Get the length of an array. | ||
* @return number: Array length. | ||
*/ | ||
var getLength = function ( | ||
array // Array|DomNodeCollection|String: The object to check for length. | ||
) { | ||
return isInstance(array) || isString(array) ? array.length : 0; | ||
}; | ||
/** | ||
* Get the first item in an array. | ||
* @return mixed: First item. | ||
*/ | ||
var getFirst = function ( | ||
array // Array: The array to get the | ||
) { | ||
return isInstance(array) ? array[0] : undefined; | ||
}; | ||
/** | ||
* Get the first item in an array. | ||
* @return mixed: First item. | ||
*/ | ||
var getLast = function ( | ||
array // Array: The array to get the | ||
) { | ||
return isInstance(array) ? array[getLength(array) - 1] : undefined; | ||
}; | ||
/** | ||
* Check for multiple array items. | ||
* @return boolean: true if the array has more than one item. | ||
*/ | ||
var hasMany = function ( | ||
array // Array: The array to check for item. | ||
) { | ||
return getLength(array) > 1; | ||
}; | ||
/** | ||
* Push an item into an array. | ||
* @return mixed: Pushed item. | ||
*/ | ||
var pushItem = function ( | ||
array, // Array: The array to push the item into. | ||
item // mixed: The item to push. | ||
) { | ||
if (isArray(array)) { | ||
array.push(item); | ||
} | ||
return item; | ||
}; | ||
/** | ||
* Push padding values onto an array up to a specified length. | ||
* @return number: The number of padding values that were added. | ||
*/ | ||
var padArray = function ( | ||
array, // Array: The array to check for items. | ||
padToLength, // number: The minimum number of items in the array. | ||
paddingValue // mixed|: The value to use as padding. | ||
) { | ||
var countAdded = 0; | ||
if (isArray(array)) { | ||
var startingLength = getLength(array); | ||
if (startingLength < length) { | ||
paddingValue = isDefined(paddingValue) ? paddingValue : ''; | ||
for (var index = startingLength; index < length; index++) { | ||
array.push(paddingValue); | ||
countAdded++; | ||
} | ||
} | ||
} | ||
return countAdded; | ||
}; | ||
/** | ||
* Return all cookies. | ||
@@ -249,3 +325,3 @@ * @return object: Cookie names and values. | ||
var getCookie = function ( | ||
name // string*: Name of the cookie. | ||
name // string: Name of the cookie. | ||
) { | ||
@@ -259,5 +335,5 @@ return getAllCookies()[name]; | ||
var setCookie = function ( | ||
name, // string*: Name of the cookie. | ||
value, // string*: Value to set. | ||
options // object: Name/value pairs for options including "maxage", "expires", "path", "domain" and "secure". | ||
name, // string: Name of the cookie. | ||
value, // string: Value to set. | ||
options // object|: Name/value pairs for options including "maxage", "expires", "path", "domain" and "secure". | ||
) { | ||
@@ -284,7 +360,6 @@ options = options || {}; | ||
var deleteCookie = function deleteCookie( | ||
name // string*: Name of the cookie. | ||
name // string: Name of the cookie. | ||
) { | ||
setCookie(name, null); | ||
}; | ||
/** | ||
@@ -310,4 +385,4 @@ * Get Unix epoch milliseconds from a date. | ||
) { | ||
// If the argument is not a string, just assume it's already an element reference, and return it. | ||
return typeof id == 'string' ? (parentElement || document).getElementById(id) : id; | ||
// If the argument is not a string, just assume it's already an element reference, and return it. | ||
return isString(id) ? (parentElement || document).getElementById(id) : id; | ||
}; | ||
@@ -322,4 +397,4 @@ | ||
) { | ||
parentElement = getElement(parentElement || document); | ||
return parentElement ? parentElement.getElementsByTagName(tagName || '*') : []; | ||
parentElement = getElement(parentElement || document); | ||
return parentElement ? parentElement.getElementsByTagName(tagName || '*') : []; | ||
}; | ||
@@ -334,27 +409,27 @@ | ||
) { | ||
tagAndClass = tagAndClass.split('.'); | ||
var tagName = (tagAndClass[0] || '*').toUpperCase(); | ||
var className = tagAndClass[1]; | ||
if (className) { | ||
parentElement = getElement(parentElement || document); | ||
var elements = []; | ||
if (parentElement.getElementsByClassName) { | ||
forEach(parentElement.getElementsByClassName(className), function(element) { | ||
if (element.tagName == tagName) { | ||
elements.push(element); | ||
} | ||
}); | ||
} | ||
else { | ||
forEach(getElementsByTagName(tagName), function(element) { | ||
if (hasClass(element, className)) { | ||
elements.push(element); | ||
} | ||
}); | ||
} | ||
tagAndClass = tagAndClass.split('.'); | ||
var tagName = (tagAndClass[0] || '*').toUpperCase(); | ||
var className = tagAndClass[1]; | ||
if (className) { | ||
parentElement = getElement(parentElement || document); | ||
var elements = []; | ||
if (parentElement.getElementsByClassName) { | ||
forEach(parentElement.getElementsByClassName(className), function(element) { | ||
if (element.tagName == tagName) { | ||
elements.push(element); | ||
} | ||
}); | ||
} | ||
else { | ||
elements = getElementsByTagName(tagName, parentElement); | ||
forEach(getElementsByTagName(tagName), function(element) { | ||
if (hasClass(element, className)) { | ||
elements.push(element); | ||
} | ||
}); | ||
} | ||
return elements; | ||
} | ||
else { | ||
elements = getElementsByTagName(tagName, parentElement); | ||
} | ||
return elements; | ||
}; | ||
@@ -369,10 +444,10 @@ | ||
) { | ||
var parentElement = (getElement(element) || {}).parentNode; | ||
// If a tag name is specified, keep walking up. | ||
if (tagName && parentElement) { | ||
if (parentElement.tagName != tagName) { | ||
parentElement = getParent(parentElement, tagName); | ||
} | ||
var parentElement = (getElement(element) || {}).parentNode; | ||
// If a tag name is specified, keep walking up. | ||
if (tagName && parentElement) { | ||
if (parentElement.tagName != tagName) { | ||
parentElement = getParent(parentElement, tagName); | ||
} | ||
return parentElement; | ||
} | ||
return parentElement; | ||
}; | ||
@@ -386,3 +461,3 @@ | ||
) { | ||
if (typeof tagIdentifier != 'string') { | ||
if (!isString(tagIdentifier)) { | ||
return tagIdentifier; | ||
@@ -463,6 +538,6 @@ } | ||
) { | ||
var parentElement = getParent(element); | ||
var wrapper = addElement(parentElement, tagIdentifier, element); | ||
insertElement(wrapper, element); | ||
return wrapper; | ||
var parentElement = getParent(element); | ||
var wrapper = addElement(parentElement, tagIdentifier, element); | ||
insertElement(wrapper, element); | ||
return wrapper; | ||
}; | ||
@@ -476,3 +551,3 @@ | ||
) { | ||
return getElement(parentElement).childNodes; | ||
return getElement(parentElement).childNodes; | ||
}; | ||
@@ -486,9 +561,9 @@ | ||
) { | ||
if (element = getElement(element)) { | ||
var index = 0; | ||
while (element = element.previousSibling) { | ||
++index; | ||
} | ||
return index; | ||
if (element = getElement(element)) { | ||
var index = 0; | ||
while (element = element.previousSibling) { | ||
++index; | ||
} | ||
return index; | ||
} | ||
}; | ||
@@ -504,13 +579,13 @@ | ||
) { | ||
// Ensure that we have elements, not just IDs. | ||
parentElement = getElement(parentElement); | ||
childElement = getElement(childElement); | ||
if (parentElement && childElement) { | ||
// If the beforeSibling value is a number, get the (future) sibling at that index. | ||
if (typeof beforeSibling == 'number') { | ||
beforeSibling = getChildren(parentElement)[beforeSibling]; | ||
} | ||
// Insert the element, optionally before an existing sibling. | ||
parentElement.insertBefore(childElement, beforeSibling || null); | ||
// Ensure that we have elements, not just IDs. | ||
parentElement = getElement(parentElement); | ||
childElement = getElement(childElement); | ||
if (parentElement && childElement) { | ||
// If the beforeSibling value is a number, get the (future) sibling at that index. | ||
if (isNumber(beforeSibling)) { | ||
beforeSibling = getChildren(parentElement)[beforeSibling]; | ||
} | ||
// Insert the element, optionally before an existing sibling. | ||
parentElement.insertBefore(childElement, beforeSibling || null); | ||
} | ||
}; | ||
@@ -524,10 +599,10 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Remove the element from its parent, provided that its parent still exists. | ||
var parentElement = getParent(element); | ||
if (parentElement) { | ||
parentElement.removeChild(element); | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Remove the element from its parent, provided that its parent still exists. | ||
var parentElement = getParent(element); | ||
if (parentElement) { | ||
parentElement.removeChild(element); | ||
} | ||
} | ||
}; | ||
@@ -541,3 +616,3 @@ | ||
) { | ||
setHtml(element, ''); | ||
setHtml(element, ''); | ||
}; | ||
@@ -551,6 +626,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.innerHTML; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.innerHTML; | ||
} | ||
}; | ||
@@ -565,7 +640,7 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerHTML. | ||
element.innerHTML = html; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerHTML. | ||
element.innerHTML = html; | ||
} | ||
}; | ||
@@ -579,6 +654,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.innerText; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.innerText; | ||
} | ||
}; | ||
@@ -593,7 +668,7 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerText. | ||
element.innerHTML = text; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerText. | ||
element.innerHTML = text; | ||
} | ||
}; | ||
@@ -607,6 +682,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.className; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.className; | ||
} | ||
}; | ||
@@ -621,7 +696,7 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerText. | ||
element.className = className; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerText. | ||
element.className = className; | ||
} | ||
}; | ||
@@ -635,6 +710,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.firstChild; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.firstChild; | ||
} | ||
}; | ||
@@ -648,6 +723,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.previousSibling; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.previousSibling; | ||
} | ||
}; | ||
@@ -661,6 +736,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.nextSibling; | ||
} | ||
} | ||
}; | ||
@@ -686,5 +761,5 @@ | ||
) { | ||
if (element = getElement(element)) { | ||
element.className += ' ' + className; | ||
} | ||
if (element = getElement(element)) { | ||
element.className += ' ' + className; | ||
} | ||
}; | ||
@@ -700,11 +775,11 @@ | ||
if (element = getElement(element)) { | ||
var tokens = getClass(element).split(/\s/); | ||
var ok = []; | ||
forEach(tokens, function (token) { | ||
if (token != className) { | ||
ok.push(token); | ||
} | ||
}); | ||
element.className = ok.join(' '); | ||
} | ||
var tokens = getClass(element).split(/\s/); | ||
var ok = []; | ||
forEach(tokens, function (token) { | ||
if (token != className) { | ||
ok.push(token); | ||
} | ||
}); | ||
element.className = ok.join(' '); | ||
} | ||
}; | ||
@@ -720,4 +795,4 @@ | ||
) { | ||
var method = flipOn ? addClass : removeClass; | ||
method(element, className); | ||
var method = flipOn ? addClass : removeClass; | ||
method(element, className); | ||
}; | ||
@@ -732,3 +807,3 @@ | ||
) { | ||
flipClass(element, className, !hasClass(element, className)); | ||
flipClass(element, className, !hasClass(element, className)); | ||
}; | ||
@@ -743,13 +818,13 @@ | ||
) { | ||
var head = getElementsByTagName('head')[0]; | ||
var script = addElement(0, 'script'); | ||
if (callback) { | ||
script.onload = callback; | ||
script.onreadystatechange = function() { | ||
if (isLoaded(script)) { | ||
callback(); | ||
} | ||
}; | ||
} | ||
script.src = src; | ||
var head = getElementsByTagName('head')[0]; | ||
var script = addElement(0, 'script'); | ||
if (callback) { | ||
script.onload = callback; | ||
script.onreadystatechange = function() { | ||
if (isLoaded(script)) { | ||
callback(); | ||
} | ||
}; | ||
} | ||
script.src = src; | ||
}; | ||
@@ -768,3 +843,4 @@ /** | ||
if (containsString(eventName, ' ')) { | ||
forEach(eventName.split(' '), function (singleEventName) { | ||
var eventNames = splitBySpaces(eventName); | ||
forEach(eventNames, function (singleEventName) { | ||
bind(element, singleEventName, eventHandler, customData, multiBindCustomData); | ||
@@ -949,3 +1025,3 @@ }); | ||
}; | ||
if (typeof delay == 'undefined') { | ||
if (isUndefined(delay)) { | ||
focus(); | ||
@@ -981,8 +1057,8 @@ } | ||
) { | ||
var isString = (typeof elementOrString == 'string'); | ||
var object = isString ? addTimeout : elementOrString; | ||
var key = isString ? elementOrString : 'T'; | ||
var usingString = isString(elementOrString); | ||
var object = usingString ? addTimeout : elementOrString; | ||
var key = usingString ? elementOrString : 'T'; | ||
clearTimeout(object[key]); | ||
if (callback) { | ||
if (typeof delay == 'undefined') { | ||
if (isUndefined(delay)) { | ||
delay = 9; | ||
@@ -1003,32 +1079,60 @@ } | ||
/** | ||
* Get or set the value of a form element. | ||
* Get the value of a form element. | ||
*/ | ||
var valueOf = function ( | ||
input, | ||
value | ||
var getValue = function ( | ||
input | ||
) { | ||
input = getElement(input); | ||
var type = input.type; | ||
var isCheckbox = type == 'checkbox'; | ||
var isRadio = type == 'radio'; | ||
var isSelect = /select/.test(type); | ||
if (typeof value == 'undefined') { | ||
value = input.value; | ||
if (isCheckbox) { | ||
return input.checked ? value : null; | ||
if (input) { | ||
var type = input.type[0]; | ||
var value = input.value; | ||
var checked = input.checked; | ||
var options = input.options; | ||
if (isBoolean(checked)) { | ||
value = checked ? value : null; | ||
} | ||
else if (isSelect) { | ||
return input.options[input.selectedIndex].value; | ||
else if (input.multiple) { | ||
value = []; | ||
forEach(options, function (option) { | ||
if (option.selected) { | ||
pushItem(value, option.value); | ||
} | ||
}); | ||
} | ||
else if (type == 's') { | ||
value = options[input.selectedIndex].value; | ||
} | ||
} | ||
else { | ||
if (isCheckbox) { | ||
return value; | ||
}; | ||
/** | ||
* Set the value of a form element. | ||
*/ | ||
var setValue = function ( | ||
input, | ||
value | ||
) { | ||
input = getElement(input); | ||
if (input) { | ||
var type = input.type[0]; | ||
if (type == 'c' || type == 'r') { | ||
input.checked = value ? true : false; | ||
} | ||
else if (isSelect) { | ||
forEach(input.options, function (option, index) { | ||
if (option.value == value) { | ||
input.selectedIndex = index; | ||
else if (type == 's') { | ||
var selected = {}; | ||
if (input.multiple) { | ||
if (!isArray(value)) { | ||
value = splitByCommas(value); | ||
} | ||
forEach(value, function (val) { | ||
selected[val] = true; | ||
}); | ||
} | ||
else { | ||
selected[value] = true; | ||
} | ||
value = isArray(value) ? value : [value]; | ||
forEach(input.options, function (option) { | ||
option.selected = !!selected[option.value]; | ||
}); | ||
@@ -1040,3 +1144,2 @@ } | ||
} | ||
return input.value; | ||
}; | ||
@@ -1091,21 +1194,48 @@ /** | ||
*/ | ||
var log = function ( | ||
message, | ||
object | ||
) { | ||
if (window.console && console.log) { | ||
// Prefix the first argument (hopefully a string) with the marker. | ||
if (typeof object == 'undefined') { | ||
console.log(message); | ||
} | ||
else { | ||
console.log(message, object); | ||
} | ||
} | ||
var error = function () { | ||
ifConsole('error', arguments); | ||
}; | ||
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var warn = function () { | ||
ifConsole('warn', arguments); | ||
}; | ||
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var info = function () { | ||
ifConsole('info', arguments); | ||
}; | ||
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var log = function () { | ||
ifConsole('log', arguments); | ||
}; | ||
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var trace = function () { | ||
ifConsole('trace', arguments); | ||
}; | ||
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var ifConsole = function (method, arguments) { | ||
var console = window.console; | ||
if (console && console[method]) { | ||
console[method].apply(console, arguments); | ||
} | ||
}; | ||
/** | ||
* If the argument is numeric, return a number, otherwise return zero. | ||
* @param {Object} n | ||
* @param Object n | ||
*/ | ||
var forceNumber = function ( | ||
var ensureNumber = function ( | ||
number, | ||
@@ -1118,10 +1248,9 @@ defaultNumber | ||
}; | ||
/** | ||
* Return true if it's a string. | ||
* Ensure a value is a string. | ||
*/ | ||
var isString = function ( | ||
object | ||
var ensureString = function ( | ||
value | ||
) { | ||
return typeof object == 'string'; | ||
return isString(value) ? value : '' + value; | ||
}; | ||
@@ -1136,3 +1265,3 @@ | ||
) { | ||
return ('' + string).indexOf(substring) > -1; | ||
return ensureString(string).indexOf(substring) > -1; | ||
}; | ||
@@ -1146,6 +1275,24 @@ | ||
) { | ||
return ('' + string).replace(/^\s+|\s+$/g, ''); | ||
return ensureString(string).replace(/^\s+|\s+$/g, ''); | ||
}; | ||
/** | ||
* Split a string by commas. | ||
*/ | ||
var splitByCommas = function ( | ||
string | ||
) { | ||
return ensureString(string).split(','); | ||
}; | ||
/** | ||
* Split a string by spaces. | ||
*/ | ||
var splitBySpaces = function ( | ||
string | ||
) { | ||
return ensureString(string).split(' '); | ||
}; | ||
/** | ||
* Return a string, with asterisks replaced by values from a replacements array. | ||
@@ -1157,7 +1304,7 @@ */ | ||
) { | ||
string = '' + string; | ||
forEach(replacements, function(replacement) { | ||
string = string.replace('*', replacement); | ||
}); | ||
return string; | ||
string = ensureString(string); | ||
forEach(replacements, function(replacement) { | ||
string = string.replace('*', replacement); | ||
}); | ||
return string; | ||
}; | ||
@@ -1171,3 +1318,3 @@ | ||
) { | ||
return ('' + string).replace(/[^a-z]/ig, ''); | ||
return ensureString(string).replace(/[^a-z]/ig, ''); | ||
}; | ||
@@ -1181,3 +1328,3 @@ | ||
) { | ||
return ('' + string).replace(/[^0-9]/g, ''); | ||
return ensureString(string).replace(/[^0-9]/g, ''); | ||
}; | ||
@@ -1204,7 +1351,91 @@ | ||
) { | ||
var match = new RegExp(browserName + '[ /](\\d+(\\.\\d+)?)', 'i').exec(navigator.userAgent); | ||
return match ? +match[1] : 0; | ||
var match = new RegExp(browserName + '[ /](\\d+(\\.\\d+)?)', 'i').exec(navigator.userAgent); | ||
return match ? +match[1] : 0; | ||
}; | ||
// Make an undefined value available. | ||
var undefined = window.undefined; | ||
/** | ||
* Return true if a variable is a given type. | ||
*/ | ||
var isType = function ( | ||
value, // mixed: The variable to check. | ||
type // string: The type we're checking for. | ||
) { | ||
return typeof value == type; | ||
}; | ||
/** | ||
* Return true if a variable is undefined. | ||
*/ | ||
var isUndefined = function ( | ||
value // mixed: The variable to check. | ||
) { | ||
return isType(value, 'undefined'); | ||
}; | ||
/** | ||
* Return true if a variable is boolean. | ||
*/ | ||
var isBoolean = function ( | ||
value // mixed: The variable to check. | ||
) { | ||
return isType(value, 'boolean'); | ||
}; | ||
/** | ||
* Return true if a variable is a number. | ||
*/ | ||
var isNumber = function ( | ||
value // mixed: The variable to check. | ||
) { | ||
return isType(value, 'number'); | ||
}; | ||
/** | ||
* Return true if a variable is a string. | ||
*/ | ||
var isString = function ( | ||
value // mixed: The variable to check. | ||
) { | ||
return isType(value, 'string'); | ||
}; | ||
/** | ||
* Return true if a variable is a function. | ||
*/ | ||
var isFunction = function ( | ||
value // mixed: The variable to check. | ||
) { | ||
return isType(value, 'function'); | ||
}; | ||
/** | ||
* Return true if a variable is an object. | ||
*/ | ||
var isObject = function ( | ||
value // mixed: The variable to check. | ||
) { | ||
return isType(value, 'object'); | ||
}; | ||
/** | ||
* Return true if a variable is an instance of a class. | ||
*/ | ||
var isInstance = function ( | ||
value, // mixed: The variable to check. | ||
protoClass // Class|: The class we'ere checking for. | ||
) { | ||
return value instanceof (protoClass || Object); | ||
}; | ||
/** | ||
* Return true if a variable is an array. | ||
*/ | ||
var isArray = function ( | ||
value // mixed: The variable to check. | ||
) { | ||
return isInstance(value, Array); | ||
}; | ||
/** | ||
* Get the current location host. | ||
@@ -1251,2 +1482,1 @@ */ | ||
}; | ||
@@ -15,3 +15,3 @@ { | ||
], | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"main": "chug/chug.js", | ||
@@ -18,0 +18,0 @@ "homepage": "http://lighter.io/jymin", |
@@ -13,10 +13,10 @@ /** | ||
var getResponse = function ( | ||
url, // string*: The URL to request data from. | ||
data, // object: Data to post. The method is automagically "POST" if data is truey, otherwise "GET". | ||
onSuccess, // function: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure, // function: Callback to run on failure. `onFailure(response, request)`. | ||
evalJson // boolean: Whether to evaluate the response as JSON. | ||
url, // string: The URL to request data from. | ||
data, // object|: Data to post. The method is automagically "POST" if data is truey, otherwise "GET". | ||
onSuccess, // function|: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure, // function|: Callback to run on failure. `onFailure(response, request)`. | ||
evalJson // boolean|: Whether to evaluate the response as JSON. | ||
) { | ||
// If the optional data argument is omitted, shuffle it out. | ||
if (typeof data == 'function') { | ||
if (isFunction(data)) { | ||
evalJson = onFailure; | ||
@@ -58,4 +58,5 @@ onFailure = onSuccess; | ||
request.open(data ? 'POST' : 'GET', url, true); | ||
request.setRequestHeader('x-requested-with', 'XMLHttpRequest'); | ||
if (data) { | ||
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); | ||
request.setRequestHeader('content-type', 'application/x-www-form-urlencoded'); | ||
} | ||
@@ -72,8 +73,7 @@ request.send(data || null); | ||
var getJson = function ( | ||
url, // string*: The URL to request data from. | ||
onSuccess, // function: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure // function: Callback to run on failure. `onFailure(response, request)`. | ||
url, // string: The URL to request data from. | ||
onSuccess, // function|: Callback to run on success. `onSuccess(response, request)`. | ||
onFailure // function|: Callback to run on failure. `onFailure(response, request)`. | ||
) { | ||
return getResponse(url, onSuccess, onFailure, true); | ||
}; | ||
@@ -8,40 +8,40 @@ var DEFAULT_ANIMATION_FRAME_COUNT = 40; | ||
var animate = function ( | ||
element, // string|DOMElement*: Element or ID of element to animate. | ||
styleTransitions, // object*: cssText values to animate through. | ||
onFinish, // function: Callback to execute when animation is complete. | ||
frameCount, // integer: Number of frames to animate through. (Default: 40) | ||
frameDelay, // integer: Number of milliseconds between frames. (Default: 20ms) | ||
frameIndex // integer: Index of the frame to start on. (Default: 0) | ||
element, // string|DOMElement: Element or ID of element to animate. | ||
styleTransitions, // object: cssText values to animate through. | ||
onFinish, // function|: Callback to execute when animation is complete. | ||
frameCount, // integer|: Number of frames to animate through. (Default: 40) | ||
frameDelay, // integer|: Number of milliseconds between frames. (Default: 20ms) | ||
frameIndex // integer|: Index of the frame to start on. (Default: 0) | ||
) { | ||
if (element = getElement(element)) { | ||
// Only allow one animation on an element at a time. | ||
stopAnimation(element); | ||
frameIndex = frameIndex || 0; | ||
frameCount = frameCount || DEFAULT_ANIMATION_FRAME_COUNT; | ||
frameDelay = frameDelay || DEFAULT_ANIMATION_FRAME_DELAY; | ||
var scale = Math.atan(1.5) * 2; | ||
var fraction = Math.atan(frameIndex / frameCount * 3 - 1.5) / scale + 0.5; | ||
var styles = {}; | ||
forIn(styleTransitions, function(transition, key) { | ||
var start = transition[0]; | ||
var end = transition[1]; | ||
var value; | ||
if (isNaN(start)) { | ||
value = frameIndex ? end : start; | ||
} | ||
else { | ||
value = (1 - fraction) * start + fraction * end; | ||
} | ||
styles[key] = value; | ||
}); | ||
extendStyle(element, styles); | ||
if (frameIndex < frameCount) { | ||
element.animation = setTimeout(function() { | ||
animate(element, styleTransitions, onFinish, frameCount, frameDelay, frameIndex + 1); | ||
}); | ||
} | ||
else if (onFinish) { | ||
onFinish(element); | ||
} | ||
if (element = getElement(element)) { | ||
// Only allow one animation on an element at a time. | ||
stopAnimation(element); | ||
frameIndex = frameIndex || 0; | ||
frameCount = frameCount || DEFAULT_ANIMATION_FRAME_COUNT; | ||
frameDelay = frameDelay || DEFAULT_ANIMATION_FRAME_DELAY; | ||
var scale = Math.atan(1.5) * 2; | ||
var fraction = Math.atan(frameIndex / frameCount * 3 - 1.5) / scale + 0.5; | ||
var styles = {}; | ||
forIn(styleTransitions, function(transition, key) { | ||
var start = transition[0]; | ||
var end = transition[1]; | ||
var value; | ||
if (isNaN(start)) { | ||
value = frameIndex ? end : start; | ||
} | ||
else { | ||
value = (1 - fraction) * start + fraction * end; | ||
} | ||
styles[key] = value; | ||
}); | ||
extendStyle(element, styles); | ||
if (frameIndex < frameCount) { | ||
element.animation = setTimeout(function() { | ||
animate(element, styleTransitions, onFinish, frameCount, frameDelay, frameIndex + 1); | ||
}); | ||
} | ||
else if (onFinish) { | ||
onFinish(element); | ||
} | ||
} | ||
}; | ||
@@ -53,8 +53,7 @@ | ||
var stopAnimation = function ( | ||
element // string|DOMElement*: Element or ID of element to cancel the animation on. | ||
element // string|DOMElement: Element or ID of element to cancel the animation on. | ||
) { | ||
if (element = getElement(element)) { | ||
clearTimeout(element.animation); | ||
} | ||
if (element = getElement(element)) { | ||
clearTimeout(element.animation); | ||
} | ||
}; | ||
@@ -6,6 +6,6 @@ /** | ||
array, // Array*: The array to iterate over. | ||
callback // function*: The function to call on each item. `callback(item, index, array)` | ||
callback // Function*: The function to call on each item. `callback(item, index, array)` | ||
) { | ||
if (array) { | ||
for (var index = 0, length = array.length; index < length; index++) { | ||
for (var index = 0, length = getLength(array); index < length; index++) { | ||
var result = callback(array[index], index, array); | ||
@@ -23,4 +23,4 @@ if (result === false) { | ||
var forIn = function ( | ||
object, // object*: The object to iterate over. | ||
callback // function*: The function to call on each pair. `callback(value, key, object)` | ||
object, // Object*: The object to iterate over. | ||
callback // Function*: The function to call on each pair. `callback(value, key, object)` | ||
) { | ||
@@ -41,4 +41,4 @@ if (object) { | ||
var decorateObject = function ( | ||
object, // object*: The object to decorate. | ||
decorations // object*: The object to iterate over. | ||
object, // Object: The object to decorate. | ||
decorations // Object: The object to iterate over. | ||
) { | ||
@@ -53,1 +53,77 @@ if (object && decorations) { | ||
/** | ||
* Get the length of an array. | ||
* @return number: Array length. | ||
*/ | ||
var getLength = function ( | ||
array // Array|DomNodeCollection|String: The object to check for length. | ||
) { | ||
return isInstance(array) || isString(array) ? array.length : 0; | ||
}; | ||
/** | ||
* Get the first item in an array. | ||
* @return mixed: First item. | ||
*/ | ||
var getFirst = function ( | ||
array // Array: The array to get the | ||
) { | ||
return isInstance(array) ? array[0] : undefined; | ||
}; | ||
/** | ||
* Get the first item in an array. | ||
* @return mixed: First item. | ||
*/ | ||
var getLast = function ( | ||
array // Array: The array to get the | ||
) { | ||
return isInstance(array) ? array[getLength(array) - 1] : undefined; | ||
}; | ||
/** | ||
* Check for multiple array items. | ||
* @return boolean: true if the array has more than one item. | ||
*/ | ||
var hasMany = function ( | ||
array // Array: The array to check for item. | ||
) { | ||
return getLength(array) > 1; | ||
}; | ||
/** | ||
* Push an item into an array. | ||
* @return mixed: Pushed item. | ||
*/ | ||
var pushItem = function ( | ||
array, // Array: The array to push the item into. | ||
item // mixed: The item to push. | ||
) { | ||
if (isArray(array)) { | ||
array.push(item); | ||
} | ||
return item; | ||
}; | ||
/** | ||
* Push padding values onto an array up to a specified length. | ||
* @return number: The number of padding values that were added. | ||
*/ | ||
var padArray = function ( | ||
array, // Array: The array to check for items. | ||
padToLength, // number: The minimum number of items in the array. | ||
paddingValue // mixed|: The value to use as padding. | ||
) { | ||
var countAdded = 0; | ||
if (isArray(array)) { | ||
var startingLength = getLength(array); | ||
if (startingLength < length) { | ||
paddingValue = isDefined(paddingValue) ? paddingValue : ''; | ||
for (var index = startingLength; index < length; index++) { | ||
array.push(paddingValue); | ||
countAdded++; | ||
} | ||
} | ||
} | ||
return countAdded; | ||
}; |
@@ -24,3 +24,3 @@ /** | ||
var getCookie = function ( | ||
name // string*: Name of the cookie. | ||
name // string: Name of the cookie. | ||
) { | ||
@@ -34,5 +34,5 @@ return getAllCookies()[name]; | ||
var setCookie = function ( | ||
name, // string*: Name of the cookie. | ||
value, // string*: Value to set. | ||
options // object: Name/value pairs for options including "maxage", "expires", "path", "domain" and "secure". | ||
name, // string: Name of the cookie. | ||
value, // string: Value to set. | ||
options // object|: Name/value pairs for options including "maxage", "expires", "path", "domain" and "secure". | ||
) { | ||
@@ -59,6 +59,5 @@ options = options || {}; | ||
var deleteCookie = function deleteCookie( | ||
name // string*: Name of the cookie. | ||
name // string: Name of the cookie. | ||
) { | ||
setCookie(name, null); | ||
}; | ||
@@ -10,4 +10,4 @@ /** | ||
) { | ||
// If the argument is not a string, just assume it's already an element reference, and return it. | ||
return typeof id == 'string' ? (parentElement || document).getElementById(id) : id; | ||
// If the argument is not a string, just assume it's already an element reference, and return it. | ||
return isString(id) ? (parentElement || document).getElementById(id) : id; | ||
}; | ||
@@ -22,4 +22,4 @@ | ||
) { | ||
parentElement = getElement(parentElement || document); | ||
return parentElement ? parentElement.getElementsByTagName(tagName || '*') : []; | ||
parentElement = getElement(parentElement || document); | ||
return parentElement ? parentElement.getElementsByTagName(tagName || '*') : []; | ||
}; | ||
@@ -34,27 +34,27 @@ | ||
) { | ||
tagAndClass = tagAndClass.split('.'); | ||
var tagName = (tagAndClass[0] || '*').toUpperCase(); | ||
var className = tagAndClass[1]; | ||
if (className) { | ||
parentElement = getElement(parentElement || document); | ||
var elements = []; | ||
if (parentElement.getElementsByClassName) { | ||
forEach(parentElement.getElementsByClassName(className), function(element) { | ||
if (element.tagName == tagName) { | ||
elements.push(element); | ||
} | ||
}); | ||
} | ||
else { | ||
forEach(getElementsByTagName(tagName), function(element) { | ||
if (hasClass(element, className)) { | ||
elements.push(element); | ||
} | ||
}); | ||
} | ||
tagAndClass = tagAndClass.split('.'); | ||
var tagName = (tagAndClass[0] || '*').toUpperCase(); | ||
var className = tagAndClass[1]; | ||
if (className) { | ||
parentElement = getElement(parentElement || document); | ||
var elements = []; | ||
if (parentElement.getElementsByClassName) { | ||
forEach(parentElement.getElementsByClassName(className), function(element) { | ||
if (element.tagName == tagName) { | ||
elements.push(element); | ||
} | ||
}); | ||
} | ||
else { | ||
elements = getElementsByTagName(tagName, parentElement); | ||
forEach(getElementsByTagName(tagName), function(element) { | ||
if (hasClass(element, className)) { | ||
elements.push(element); | ||
} | ||
}); | ||
} | ||
return elements; | ||
} | ||
else { | ||
elements = getElementsByTagName(tagName, parentElement); | ||
} | ||
return elements; | ||
}; | ||
@@ -69,10 +69,10 @@ | ||
) { | ||
var parentElement = (getElement(element) || {}).parentNode; | ||
// If a tag name is specified, keep walking up. | ||
if (tagName && parentElement) { | ||
if (parentElement.tagName != tagName) { | ||
parentElement = getParent(parentElement, tagName); | ||
} | ||
var parentElement = (getElement(element) || {}).parentNode; | ||
// If a tag name is specified, keep walking up. | ||
if (tagName && parentElement) { | ||
if (parentElement.tagName != tagName) { | ||
parentElement = getParent(parentElement, tagName); | ||
} | ||
return parentElement; | ||
} | ||
return parentElement; | ||
}; | ||
@@ -86,3 +86,3 @@ | ||
) { | ||
if (typeof tagIdentifier != 'string') { | ||
if (!isString(tagIdentifier)) { | ||
return tagIdentifier; | ||
@@ -163,6 +163,6 @@ } | ||
) { | ||
var parentElement = getParent(element); | ||
var wrapper = addElement(parentElement, tagIdentifier, element); | ||
insertElement(wrapper, element); | ||
return wrapper; | ||
var parentElement = getParent(element); | ||
var wrapper = addElement(parentElement, tagIdentifier, element); | ||
insertElement(wrapper, element); | ||
return wrapper; | ||
}; | ||
@@ -176,3 +176,3 @@ | ||
) { | ||
return getElement(parentElement).childNodes; | ||
return getElement(parentElement).childNodes; | ||
}; | ||
@@ -186,9 +186,9 @@ | ||
) { | ||
if (element = getElement(element)) { | ||
var index = 0; | ||
while (element = element.previousSibling) { | ||
++index; | ||
} | ||
return index; | ||
if (element = getElement(element)) { | ||
var index = 0; | ||
while (element = element.previousSibling) { | ||
++index; | ||
} | ||
return index; | ||
} | ||
}; | ||
@@ -204,13 +204,13 @@ | ||
) { | ||
// Ensure that we have elements, not just IDs. | ||
parentElement = getElement(parentElement); | ||
childElement = getElement(childElement); | ||
if (parentElement && childElement) { | ||
// If the beforeSibling value is a number, get the (future) sibling at that index. | ||
if (typeof beforeSibling == 'number') { | ||
beforeSibling = getChildren(parentElement)[beforeSibling]; | ||
} | ||
// Insert the element, optionally before an existing sibling. | ||
parentElement.insertBefore(childElement, beforeSibling || null); | ||
// Ensure that we have elements, not just IDs. | ||
parentElement = getElement(parentElement); | ||
childElement = getElement(childElement); | ||
if (parentElement && childElement) { | ||
// If the beforeSibling value is a number, get the (future) sibling at that index. | ||
if (isNumber(beforeSibling)) { | ||
beforeSibling = getChildren(parentElement)[beforeSibling]; | ||
} | ||
// Insert the element, optionally before an existing sibling. | ||
parentElement.insertBefore(childElement, beforeSibling || null); | ||
} | ||
}; | ||
@@ -224,10 +224,10 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Remove the element from its parent, provided that its parent still exists. | ||
var parentElement = getParent(element); | ||
if (parentElement) { | ||
parentElement.removeChild(element); | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Remove the element from its parent, provided that its parent still exists. | ||
var parentElement = getParent(element); | ||
if (parentElement) { | ||
parentElement.removeChild(element); | ||
} | ||
} | ||
}; | ||
@@ -241,3 +241,3 @@ | ||
) { | ||
setHtml(element, ''); | ||
setHtml(element, ''); | ||
}; | ||
@@ -251,6 +251,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.innerHTML; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.innerHTML; | ||
} | ||
}; | ||
@@ -265,7 +265,7 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerHTML. | ||
element.innerHTML = html; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerHTML. | ||
element.innerHTML = html; | ||
} | ||
}; | ||
@@ -279,6 +279,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.innerText; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.innerText; | ||
} | ||
}; | ||
@@ -293,7 +293,7 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerText. | ||
element.innerHTML = text; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerText. | ||
element.innerHTML = text; | ||
} | ||
}; | ||
@@ -307,6 +307,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.className; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.className; | ||
} | ||
}; | ||
@@ -321,7 +321,7 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerText. | ||
element.className = className; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Set the element's innerText. | ||
element.className = className; | ||
} | ||
}; | ||
@@ -335,6 +335,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.firstChild; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.firstChild; | ||
} | ||
}; | ||
@@ -348,6 +348,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.previousSibling; | ||
} | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.previousSibling; | ||
} | ||
}; | ||
@@ -361,6 +361,6 @@ | ||
) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
// Ensure that we have an element, not just an ID. | ||
if (element = getElement(element)) { | ||
return element.nextSibling; | ||
} | ||
} | ||
}; | ||
@@ -386,5 +386,5 @@ | ||
) { | ||
if (element = getElement(element)) { | ||
element.className += ' ' + className; | ||
} | ||
if (element = getElement(element)) { | ||
element.className += ' ' + className; | ||
} | ||
}; | ||
@@ -400,11 +400,11 @@ | ||
if (element = getElement(element)) { | ||
var tokens = getClass(element).split(/\s/); | ||
var ok = []; | ||
forEach(tokens, function (token) { | ||
if (token != className) { | ||
ok.push(token); | ||
} | ||
}); | ||
element.className = ok.join(' '); | ||
} | ||
var tokens = getClass(element).split(/\s/); | ||
var ok = []; | ||
forEach(tokens, function (token) { | ||
if (token != className) { | ||
ok.push(token); | ||
} | ||
}); | ||
element.className = ok.join(' '); | ||
} | ||
}; | ||
@@ -420,4 +420,4 @@ | ||
) { | ||
var method = flipOn ? addClass : removeClass; | ||
method(element, className); | ||
var method = flipOn ? addClass : removeClass; | ||
method(element, className); | ||
}; | ||
@@ -432,3 +432,3 @@ | ||
) { | ||
flipClass(element, className, !hasClass(element, className)); | ||
flipClass(element, className, !hasClass(element, className)); | ||
}; | ||
@@ -443,13 +443,13 @@ | ||
) { | ||
var head = getElementsByTagName('head')[0]; | ||
var script = addElement(0, 'script'); | ||
if (callback) { | ||
script.onload = callback; | ||
script.onreadystatechange = function() { | ||
if (isLoaded(script)) { | ||
callback(); | ||
} | ||
}; | ||
} | ||
script.src = src; | ||
var head = getElementsByTagName('head')[0]; | ||
var script = addElement(0, 'script'); | ||
if (callback) { | ||
script.onload = callback; | ||
script.onreadystatechange = function() { | ||
if (isLoaded(script)) { | ||
callback(); | ||
} | ||
}; | ||
} | ||
script.src = src; | ||
}; |
@@ -13,3 +13,4 @@ /** | ||
if (containsString(eventName, ' ')) { | ||
forEach(eventName.split(' '), function (singleEventName) { | ||
var eventNames = splitBySpaces(eventName); | ||
forEach(eventNames, function (singleEventName) { | ||
bind(element, singleEventName, eventHandler, customData, multiBindCustomData); | ||
@@ -194,3 +195,3 @@ }); | ||
}; | ||
if (typeof delay == 'undefined') { | ||
if (isUndefined(delay)) { | ||
focus(); | ||
@@ -226,8 +227,8 @@ } | ||
) { | ||
var isString = (typeof elementOrString == 'string'); | ||
var object = isString ? addTimeout : elementOrString; | ||
var key = isString ? elementOrString : 'T'; | ||
var usingString = isString(elementOrString); | ||
var object = usingString ? addTimeout : elementOrString; | ||
var key = usingString ? elementOrString : 'T'; | ||
clearTimeout(object[key]); | ||
if (callback) { | ||
if (typeof delay == 'undefined') { | ||
if (isUndefined(delay)) { | ||
delay = 9; | ||
@@ -234,0 +235,0 @@ } |
/** | ||
* Get or set the value of a form element. | ||
* Get the value of a form element. | ||
*/ | ||
var valueOf = function ( | ||
input, | ||
value | ||
var getValue = function ( | ||
input | ||
) { | ||
input = getElement(input); | ||
var type = input.type; | ||
var isCheckbox = type == 'checkbox'; | ||
var isRadio = type == 'radio'; | ||
var isSelect = /select/.test(type); | ||
if (typeof value == 'undefined') { | ||
value = input.value; | ||
if (isCheckbox) { | ||
return input.checked ? value : null; | ||
if (input) { | ||
var type = input.type[0]; | ||
var value = input.value; | ||
var checked = input.checked; | ||
var options = input.options; | ||
if (isBoolean(checked)) { | ||
value = checked ? value : null; | ||
} | ||
else if (isSelect) { | ||
return input.options[input.selectedIndex].value; | ||
else if (input.multiple) { | ||
value = []; | ||
forEach(options, function (option) { | ||
if (option.selected) { | ||
pushItem(value, option.value); | ||
} | ||
}); | ||
} | ||
else if (type == 's') { | ||
value = options[input.selectedIndex].value; | ||
} | ||
} | ||
else { | ||
if (isCheckbox) { | ||
return value; | ||
}; | ||
/** | ||
* Set the value of a form element. | ||
*/ | ||
var setValue = function ( | ||
input, | ||
value | ||
) { | ||
input = getElement(input); | ||
if (input) { | ||
var type = input.type[0]; | ||
if (type == 'c' || type == 'r') { | ||
input.checked = value ? true : false; | ||
} | ||
else if (isSelect) { | ||
forEach(input.options, function (option, index) { | ||
if (option.value == value) { | ||
input.selectedIndex = index; | ||
else if (type == 's') { | ||
var selected = {}; | ||
if (input.multiple) { | ||
if (!isArray(value)) { | ||
value = splitByCommas(value); | ||
} | ||
forEach(value, function (val) { | ||
selected[val] = true; | ||
}); | ||
} | ||
else { | ||
selected[value] = true; | ||
} | ||
value = isArray(value) ? value : [value]; | ||
forEach(input.options, function (option) { | ||
option.selected = !!selected[option.value]; | ||
}); | ||
@@ -38,3 +66,2 @@ } | ||
} | ||
return input.value; | ||
}; |
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var log = function ( | ||
message, | ||
object | ||
) { | ||
if (window.console && console.log) { | ||
// Prefix the first argument (hopefully a string) with the marker. | ||
if (typeof object == 'undefined') { | ||
console.log(message); | ||
} | ||
else { | ||
console.log(message, object); | ||
} | ||
} | ||
var error = function () { | ||
ifConsole('error', arguments); | ||
}; | ||
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var warn = function () { | ||
ifConsole('warn', arguments); | ||
}; | ||
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var info = function () { | ||
ifConsole('info', arguments); | ||
}; | ||
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var log = function () { | ||
ifConsole('log', arguments); | ||
}; | ||
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var trace = function () { | ||
ifConsole('trace', arguments); | ||
}; | ||
/** | ||
* Log values to the console, if it's available. | ||
*/ | ||
var ifConsole = function (method, arguments) { | ||
var console = window.console; | ||
if (console && console[method]) { | ||
console[method].apply(console, arguments); | ||
} | ||
}; |
/** | ||
* If the argument is numeric, return a number, otherwise return zero. | ||
* @param {Object} n | ||
* @param Object n | ||
*/ | ||
var forceNumber = function ( | ||
var ensureNumber = function ( | ||
number, | ||
@@ -13,2 +13,1 @@ defaultNumber | ||
}; | ||
/** | ||
* Return true if it's a string. | ||
* Ensure a value is a string. | ||
*/ | ||
var isString = function ( | ||
object | ||
var ensureString = function ( | ||
value | ||
) { | ||
return typeof object == 'string'; | ||
return isString(value) ? value : '' + value; | ||
}; | ||
@@ -17,3 +17,3 @@ | ||
) { | ||
return ('' + string).indexOf(substring) > -1; | ||
return ensureString(string).indexOf(substring) > -1; | ||
}; | ||
@@ -27,6 +27,24 @@ | ||
) { | ||
return ('' + string).replace(/^\s+|\s+$/g, ''); | ||
return ensureString(string).replace(/^\s+|\s+$/g, ''); | ||
}; | ||
/** | ||
* Split a string by commas. | ||
*/ | ||
var splitByCommas = function ( | ||
string | ||
) { | ||
return ensureString(string).split(','); | ||
}; | ||
/** | ||
* Split a string by spaces. | ||
*/ | ||
var splitBySpaces = function ( | ||
string | ||
) { | ||
return ensureString(string).split(' '); | ||
}; | ||
/** | ||
* Return a string, with asterisks replaced by values from a replacements array. | ||
@@ -38,7 +56,7 @@ */ | ||
) { | ||
string = '' + string; | ||
forEach(replacements, function(replacement) { | ||
string = string.replace('*', replacement); | ||
}); | ||
return string; | ||
string = ensureString(string); | ||
forEach(replacements, function(replacement) { | ||
string = string.replace('*', replacement); | ||
}); | ||
return string; | ||
}; | ||
@@ -52,3 +70,3 @@ | ||
) { | ||
return ('' + string).replace(/[^a-z]/ig, ''); | ||
return ensureString(string).replace(/[^a-z]/ig, ''); | ||
}; | ||
@@ -62,3 +80,3 @@ | ||
) { | ||
return ('' + string).replace(/[^0-9]/g, ''); | ||
return ensureString(string).replace(/[^0-9]/g, ''); | ||
}; | ||
@@ -85,5 +103,4 @@ | ||
) { | ||
var match = new RegExp(browserName + '[ /](\\d+(\\.\\d+)?)', 'i').exec(navigator.userAgent); | ||
return match ? +match[1] : 0; | ||
var match = new RegExp(browserName + '[ /](\\d+(\\.\\d+)?)', 'i').exec(navigator.userAgent); | ||
return match ? +match[1] : 0; | ||
}; | ||
@@ -43,2 +43,1 @@ /** | ||
}; | ||
79432
21
2845