kis-js/src/core.js

159 lines
3.1 KiB
JavaScript
Raw Normal View History

/**
Kis JS Keep It Simple JS Library
Copyright Timothy J. Warren
License Public Domain
Version 0.9.0
*/
(function (undefined){
"use strict";
/**
* Current selector object
*
* @memberOf $_
* @name el
*/
var sel;
2012-03-29 11:58:32 -04:00
/**
* $_
*
* Constructor function
*
* @constructor
* @namespace $_
* @param {string} selector - The dom selector string
* @param {Object} [context] - Context of the dom selector string
* @return {Object}
*/
var $_ = function(s, context)
{
// Have documentElement be default selector, just in case
if (s === undefined)
{
// Defines a "global" selector for that instance
sel = ($_.el !== undefined)
? $_.el
: document.documentElement;
}
else
{
sel = $(s, context);
}
2012-03-29 11:58:32 -04:00
// Add the selector to the prototype
$_.prototype.el = sel;
2012-04-06 10:40:53 -04:00
// Use the $_ object as it's own prototype
var self = Object.create($_);
// Give sel to each extension.
2012-03-29 11:58:32 -04:00
for(var i in self)
{
if(typeof self[i] === "object")
{
self[i].el = sel;
2012-03-29 11:58:32 -04:00
}
}
self.el = sel;
2012-03-29 11:58:32 -04:00
return self;
};
2012-03-29 11:58:32 -04:00
/**
* Simple DOM selector function
*
* @memberOf $_
* @param {string} selector
* @param {Object} [context]
* @return {Object}
*/
var $ = function (selector, context)
{
var elements;
2012-03-29 11:58:32 -04:00
if (typeof selector != "string" || selector === undefined){ return selector;}
2012-03-29 11:58:32 -04:00
//Check for a context of a specific element, otherwise, just run on the document
context = (context != null && context.nodeType === 1)
2012-03-29 11:58:32 -04:00
? context
: document;
2012-03-29 11:58:32 -04:00
//Pick the quickest method for each kind of selector
if (selector.match(/^#([\w\-]+$)/))
{
return document.getElementById(selector.split('#')[1]);
}
else
{
elements = context.querySelectorAll(selector);
}
2012-03-29 11:58:32 -04:00
//Return the single object if applicable
return (elements.length === 1) ? elements[0] : elements;
};
2012-03-29 11:58:32 -04:00
/**
* Adds the property `obj` to the $_ object, calling it `name`
2012-03-29 11:58:32 -04:00
*
* @memberOf $_
* @function ext
* @example $_.ext('foo', {});
* @param {string} name - name of the module
* @param {object} obj - the object to add
*/
$_.ext = function(name, obj)
{
obj.el = sel;
$_[name] = obj;
};
2012-03-29 11:58:32 -04:00
/**
* Iterates over a $_ object, applying a callback to each item
*
* @memberOf $_
* @function each
* @example $_('form input').each(function(item) { alert(item) });
* @param {function} callback - iteration callback
*/
$_.ext('each', function(callback)
{
if(sel.length !== undefined && sel !== window)
{
[].forEach.call(sel, callback);
}
else
{
2011-11-02 19:12:58 -04:00
callback.call(sel, sel);
}
});
2012-03-29 11:58:32 -04:00
/**
* Retrieves the type of the passed variable
*
* @memberOf $_
* @function type
* @example $_.type([]); // Returns 'array'
* @param {*} obj
* @return {string}
*/
var type = function(obj)
2012-03-29 11:58:32 -04:00
{
if((function() {return obj && (obj !== this)}).call(obj))
{
//fallback on 'typeof' for truthy primitive values
return (typeof obj).toLowerCase();
}
2012-03-29 11:58:32 -04:00
//Strip x from [object x] and return
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
};
//Set global variables
$_ = window.$_ = window.$_ || $_;
$_.$ = $;
$_.type = type;
}());