kis-js/src/core.js

210 lines
3.6 KiB
JavaScript
Raw Normal View History

/**
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
*/
(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")
{
return;
}
2011-11-02 19:12:58 -04:00
var $_, $, dcopy, sel;
/**
* $_
*
* Constructor function
*
* @constuctor
* @namespace
* @param string selector
* @return object
*/
$_ = function(s)
{
//Have documentElement be default selector, just in case
if(typeof s === "undefined")
{
//Defines a "global" selector for that instance
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;
};
/**
* Simple DOM selector function
*
* @memberOf $_
* @param string selector
2011-11-03 16:04:35 -04:00
* @param object context
* @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;
};
/**
* Deep copy/prototypical constructor function
*
* @param object obj
* @private
* @return object
* @type object
*/
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;
}
/**
* @private
*/
F = function(){};
F.prototype = obj;
return new F();
};
/**
* Adds the property `obj` to the $_ object, calling it `name`
*
* @param string name
* @param object obj
*/
$_.ext = function(name, obj)
{
obj.el = sel;
$_[name] = obj;
};
/**
* Iterates over a $_ object, applying a callback to each item
*
* @name $_.each
* @function
* @param function callback
*/
$_.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);
}
}
else
{
2011-11-02 19:12:58 -04:00
callback.call(sel, sel);
}
});
/**
* Retrieves the type of the passed variable
*
* @param mixed obj
* @return string
* @type string
*/
$_.type = function(obj)
2011-11-02 19:12:58 -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
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
};
//Set global variables
$_ = window.$_ = window.$_ || $_;
$_.$ = $;
}());