Readme formatting, and start of DOM .children function

This commit is contained in:
Timothy Warren 2011-07-26 15:29:30 -04:00
parent f8350e516a
commit 1c014459df
2 changed files with 339 additions and 259 deletions

View File

@ -16,13 +16,13 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
* Function: `$_(selector).module.function(params);` * Function: `$_(selector).module.function(params);`
## Official Modules: ## ## Official Modules: ##
**Global**: Core functions ###Global###: Core functions
properties: properties:
* el: The html object returned by the selector function. * el: The html object returned by the selector function.
functions: functions:
* each: For applying changes to every item matched by a selector * each: For applying changes to every item matched by a selector
Use: Use:
@ -39,21 +39,21 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
Adds 'zip' function to $_. Adds 'zip' function to $_.
**Ajax**: simple, jQuery-like ajax functions ###Ajax###: simple, jQuery-like ajax functions
functions: functions:
* Get: * Get:
Use: Use:
$_.get(url, data_object, callback); $_.get(url, data_object, callback);
* Post: * Post:
Use: Use:
$_.post(url, data_object, callback); $_.post(url, data_object, callback);
**QS**: querystring parsing and serialization for hashbang strings, and pushState urls ###QS###: querystring parsing and serialization for hashbang strings, and pushState urls
functions: functions:
* Parse: * Parse:
Use: Use:
@ -67,9 +67,9 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
Use: Use:
$_.qs.get(key); $_.qs.get(key);
**Store**: localstorage wrapper with automatic data serialization ###Store###: localstorage wrapper with automatic data serialization
functions: functions:
* Get: * Get:
Use: Use:
@ -84,9 +84,9 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
$_.store.getAll(); $_.store.getAll();
**Event**: wrapper for applying events to DOM objects ###Event###: wrapper for applying events to DOM objects
functions: functions:
*Add: *Add:
Use: Use:
@ -96,9 +96,9 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
Use: Use:
$_(selector).event.remove(event, callback); $_(selector).event.remove(event, callback);
**DOM**: Dom manipulation module ###DOM###: Dom manipulation module
function: functions:
*addClass: *addClass:
Use: Use:
@ -130,7 +130,7 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
*css: Sets css styles on the selected element(s) *css: Sets css styles on the selected element(s)
Use: Use:
Set: $_(selector).dom.css(property, value); Set: $_(selector).dom.css(property, value);
Get: $_(selector).dom.css(property);

View File

@ -1,11 +1,4 @@
/** /*
* Dom manipulation object
*
*/
(function (){
var d;
/*
* classList.js: Cross-browser full element.classList implementation. * classList.js: Cross-browser full element.classList implementation.
* 2011-06-15 * 2011-06-15
* *
@ -14,8 +7,8 @@
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/ */
if (typeof document !== "undefined" && !("classList" in document.createElement("a"))) if (typeof document !== "undefined" && !("classList" in document.createElement("a")))
{ {
(function (view){ (function (view){
var classListProp = "classList", var classListProp = "classList",
@ -157,9 +150,21 @@
} }
}(self)); }(self));
} }
// --------------------------------------------------------------------------
/**
* Dom manipulation object
*
*/
(function (){
var d, tag_reg, id_reg, class_reg;
tag_reg = /^([\w\-]+)$/;
id_reg = /#([\w\-]+$)/;
class_reg = /\.([\w\-]+)$/;
// --------------------------------------------------------------------------
//Private function for getting/setting attributes //Private function for getting/setting attributes
function _attr(sel, name, value) function _attr(sel, name, value)
@ -280,6 +285,62 @@
console.log("Property " + prop + " nor an equivalent seems to exist"); console.log("Property " + prop + " nor an equivalent seems to exist");
} }
function _sel_filter(filter, curr_sel)
{
var i,
len = curr_sel.length,
matches = [];
if(typeof filter !== "string")
{
return filter;
}
//Filter by tag
if(filter.match(tag_reg))
{
for(i=0;i<len;i++)
{
if(curr_sell[i].tagName.toLowerCase() == filter.toLowerCase())
{
matches.push(curr_sel[i]);
}
}
}
else if(filter.match(class_reg))
{
for(i=0;i<len;i++)
{
if(curr_sel[i].classList.contains(filter))
{
matches.push(curr_sel[i]);
}
}
}
else if(filter.match(id_reg))
{
return document.getElementById(filter);
}
else
{
console.log(filter+" is not a valid filter");
}
return (matches.length === 1) ? matches[0] : matches;
}
function _set_sel(sel)
{
for(var i in $_)
{
if(typeof $_[i] === "object")
{
$_[i].el = sel;
}
}
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
d = { d = {
@ -367,6 +428,25 @@
$_.each(function (e){ $_.each(function (e){
_css(e, prop, val); _css(e, prop, val);
}); });
},
children: function(filter)
{
var sel;
if(typeof sel === "undefined")
{
sel = this.el.children;
}
else
{
sel = _sel_filter(filter, this.el.children);
}
//Update the $_ object to reflect the new selector
_set_sel(sel);
//Return the $_ object for chaining
return $_
} }
}; };