2011-10-24 12:26:00 -04:00
|
|
|
/**
|
|
|
|
Kis JS Keep It Simple JS Library
|
|
|
|
Copyright Timothy J. Warren
|
|
|
|
License Public Domain
|
2012-02-23 12:42:33 -05:00
|
|
|
Version 0.5.0
|
2011-10-24 12:26:00 -04:00
|
|
|
*/
|
|
|
|
(function (){
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2011-11-02 19:12:58 -04:00
|
|
|
// Most functions rely on a string selector
|
|
|
|
// which returns html elements. This requires
|
|
|
|
// document.querySelectorAll or a custom
|
|
|
|
// selector engine. I choose to just use the
|
|
|
|
// browser feature, since it is present in
|
|
|
|
// IE 8+, and all other major browsers
|
|
|
|
if (typeof document.querySelector === "undefined")
|
2011-10-24 12:26:00 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:12:58 -04:00
|
|
|
var $_, $, dcopy, sel;
|
2011-10-24 12:26:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $_
|
|
|
|
*
|
|
|
|
* Constructor function
|
2011-11-01 18:56:27 -04:00
|
|
|
*
|
|
|
|
* @constuctor
|
|
|
|
* @namespace
|
|
|
|
* @param string selector
|
|
|
|
* @return object
|
2011-10-24 12:26:00 -04:00
|
|
|
*/
|
|
|
|
$_ = function(s)
|
|
|
|
{
|
|
|
|
//Have documentElement be default selector, just in case
|
|
|
|
if(typeof s === "undefined")
|
|
|
|
{
|
2011-10-31 11:56:59 -04:00
|
|
|
//Defines a "global" selector for that instance
|
2011-10-24 12:26:00 -04:00
|
|
|
sel = (typeof $_.el !== "undefined")
|
|
|
|
? $_.el
|
|
|
|
: document.documentElement;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sel = (typeof s !== "object") ? $(s) : s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the selector to the prototype
|
|
|
|
$_.prototype.el = sel;
|
|
|
|
|
|
|
|
// Make a copy before adding properties
|
|
|
|
var self = dcopy($_);
|
|
|
|
|
|
|
|
// Give sel to each extension.
|
|
|
|
for(var i in self)
|
|
|
|
{
|
|
|
|
if(typeof self[i] === "object")
|
|
|
|
{
|
|
|
|
self[i].el = sel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.el = sel;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
};
|
|
|
|
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* Simple DOM selector function
|
|
|
|
*
|
|
|
|
* @memberOf $_
|
|
|
|
* @param string selector
|
2011-11-03 16:04:35 -04:00
|
|
|
* @param object context
|
2011-11-01 18:56:27 -04:00
|
|
|
* @return object
|
|
|
|
* @type object
|
|
|
|
*/
|
|
|
|
$ = function (a, context)
|
|
|
|
{
|
|
|
|
var x, c;
|
|
|
|
|
|
|
|
if (typeof a != "string" || typeof a === "undefined"){ return a;}
|
|
|
|
|
|
|
|
//Check for a context of a specific element, otherwise, just run on the document
|
|
|
|
c = (context != null && context.nodeType === 1)
|
|
|
|
? context
|
|
|
|
: document;
|
|
|
|
|
|
|
|
//Pick the quickest method for each kind of selector
|
|
|
|
if (a.match(/^#([\w\-]+$)/))
|
|
|
|
{
|
|
|
|
return document.getElementById(a.split('#')[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = c.querySelectorAll(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Return the single object if applicable
|
|
|
|
return (x.length === 1) ? x[0] : x;
|
|
|
|
};
|
|
|
|
|
2011-10-24 12:26:00 -04:00
|
|
|
/**
|
|
|
|
* Deep copy/prototypical constructor function
|
2011-11-01 18:56:27 -04:00
|
|
|
*
|
|
|
|
* @param object obj
|
|
|
|
* @private
|
|
|
|
* @return object
|
|
|
|
* @type object
|
2011-10-24 12:26:00 -04:00
|
|
|
*/
|
|
|
|
dcopy = function(obj)
|
|
|
|
{
|
|
|
|
var type, F;
|
|
|
|
|
|
|
|
if(typeof obj === "undefined")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(typeof Object.create !== "undefined")
|
|
|
|
{
|
|
|
|
return Object.create(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
type = typeof obj;
|
|
|
|
|
|
|
|
if(type !== "object" && type !== "function")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2011-10-24 12:26:00 -04:00
|
|
|
F = function(){};
|
|
|
|
|
|
|
|
F.prototype = obj;
|
|
|
|
|
|
|
|
return new F();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* Adds the property `obj` to the $_ object, calling it `name`
|
|
|
|
*
|
|
|
|
* @param string name
|
|
|
|
* @param object obj
|
|
|
|
*/
|
2011-10-24 12:26:00 -04:00
|
|
|
$_.ext = function(name, obj)
|
|
|
|
{
|
|
|
|
obj.el = sel;
|
|
|
|
$_[name] = obj;
|
|
|
|
};
|
|
|
|
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* Iterates over a $_ object, applying a callback to each item
|
|
|
|
*
|
|
|
|
* @name $_.each
|
|
|
|
* @function
|
|
|
|
* @param function callback
|
|
|
|
*/
|
2011-10-24 12:26:00 -04:00
|
|
|
$_.ext('each', function (callback)
|
|
|
|
{
|
|
|
|
if(typeof sel.length !== "undefined" && sel !== window)
|
|
|
|
{
|
|
|
|
var len = sel.length;
|
|
|
|
|
|
|
|
if (len === 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var selx;
|
|
|
|
for (var x = 0; x < len; x++)
|
|
|
|
{
|
|
|
|
selx = (sel.item(x)) ? sel.item(x) : sel[x];
|
2011-11-02 19:12:58 -04:00
|
|
|
callback.call(selx, selx);
|
2011-10-24 12:26:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-02 19:12:58 -04:00
|
|
|
callback.call(sel, sel);
|
2011-10-24 12:26:00 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* Retrieves the type of the passed variable
|
|
|
|
*
|
|
|
|
* @param mixed obj
|
|
|
|
* @return string
|
|
|
|
* @type string
|
|
|
|
*/
|
2011-10-24 12:26:00 -04:00
|
|
|
$_.type = function(obj)
|
2011-11-02 19:12:58 -04:00
|
|
|
{
|
2011-10-24 12:26:00 -04:00
|
|
|
if((function() {return obj && (obj !== this)}).call(obj))
|
|
|
|
{
|
|
|
|
//fallback on 'typeof' for truthy primitive values
|
|
|
|
return (typeof obj).toLowerCase();
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:12:58 -04:00
|
|
|
//Strip x from [object x] and return
|
2011-10-24 12:26:00 -04:00
|
|
|
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
|
2011-10-31 11:56:59 -04:00
|
|
|
};
|
2011-10-24 12:26:00 -04:00
|
|
|
|
|
|
|
//Set global variables
|
|
|
|
$_ = window.$_ = window.$_ || $_;
|
|
|
|
$_.$ = $;
|
|
|
|
|
|
|
|
}());
|