Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 3.40 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2. This is a small class for replicating the most often-used (by me, at least) jQuery functionality in 2kb. Tested in newest browsers and IE8. Examples follow.
  3. Includes:
  4.   * Find elements
  5.   * Create element
  6.   * Update attributes for element
  7.   * Iterate through elements with callback
  8.   * Bind events with callback
  9.   * Empty children from element
  10.   * Load script
  11.   * Get and set element text
  12.   * Doc Ready
  13.  
  14. Other useful shortcuts built into js:
  15. parent = element.parentNode
  16. clone = element.cloneNode(true)
  17. next = element.nextSibling
  18. html = element.innerHTML
  19. addClass = element.classList.add("foo")
  20. removeClass = element.classList.remove("foo")
  21. toggleClass = element.classList.toggle("foo")
  22. */
  23.  
  24. var nilla = (function() {
  25.         var v = {};
  26.         //Add attrs and text to element
  27.         v.attrs = function(element, attrs) {
  28.                 for(var i in attrs) {
  29.                         if(i == 'html') {
  30.                                 element.innerHTML = attrs[i];
  31.                         } else {
  32.                           element.setAttribute(i, attrs[i]);   
  33.                         }
  34.                 }
  35.                 return element;
  36.         };
  37.         //Bind an event to elements
  38.         v.bind = function(els, e_type, callback) {
  39.                 if(typeof els == 'string') {
  40.                         els = v.find(els);
  41.                 }
  42.                 v.each(els, function(el) {
  43.                         if (!el.addEventListener) {
  44.                                 el.attachEvent('on'+e_type, callback);
  45.                         } else {
  46.                                 el.addEventListener(e_type, callback, false);
  47.                         }
  48.                 });
  49.                 return els;
  50.         };
  51.         //Make new element and all attributes and html
  52.         v.create = function(selector, attrs) {
  53.                 if(!attrs) attrs = {};
  54.                 var res = document.createElement(selector);
  55.                 v.attrs(res, attrs);
  56.                 return res;
  57.         };
  58.         //Iterate through elements and apply a callback function to each
  59.         v.each = function(els, callback) {
  60.                 if(typeof els == 'string') {
  61.                         els = v.find(els);
  62.                 }
  63.                 for(var i=0; i<els.length; i++){
  64.                   callback(els[i]);
  65.                 }
  66.                 //[].forEach.call(els, callback);
  67.                 return els;
  68.         };
  69.         //Remove all children from element
  70.         v.empty = function(element) {
  71.                 while(element.firstChild) {
  72.                         element.removeChild(element.firstChild);
  73.                 }
  74.                 return element;
  75.         };
  76.         //DOM selector
  77.         v.find = function(selector) {
  78.                 var res = [];
  79.                 if(selector.substring(0,1) == '#') {
  80.                         //Find by id
  81.                         res = [document.getElementById(selector.replace('#', ''))];
  82.                 } else {
  83.                         //Find by all
  84.                         res = document.querySelectorAll(selector);
  85.                 }
  86.                 return res;
  87.         };
  88.         //Return each last child of type from each parent element found
  89.         //NOT the same as jQuery's last-child support
  90.         v.lastOfType = function(parent, selector) {
  91.                 var res = [];
  92.                 v.each(parent, function(p) {
  93.                         var kids = p.querySelectorAll(selector);
  94.                         if(kids.length > 0) {
  95.                                 var index = kids.length - 1;
  96.                                 res.push(kids[index]);
  97.                         }
  98.                 });
  99.                 return res;
  100.         }
  101.         //Load script
  102.         v.load = function(script) {
  103.                 var scr = document.createElement('script');
  104.                 scr.src = script;
  105.                 document.body.appendChild(scr);
  106.         }
  107.         //Get and set text
  108.         v.text = function(element, text) {
  109.                 if(text) {
  110.                         if(element.textContent) {
  111.                                 element.textContent = text;
  112.                         } else {
  113.                                 element.innerText = text;
  114.                         }
  115.                 } else {
  116.                         return element.textContent || element.innerText;
  117.                 }
  118.         }
  119.         //Document Ready
  120.         v.docReady = function(callback) {
  121.                 if(document.addEventListener) {
  122.                         document.addEventListener('DOMContentLoaded', function() {
  123.                                 document.removeEventListener("DOMContentLoaded", arguments.callee, false);
  124.                                 callback();
  125.                         }, false);
  126.                 } else if(document.attachEvent) {
  127.                         document.attachEvent('onreadystatechange', function(){
  128.                                 if (document.readyState === "complete") {
  129.                                         document.detachEvent("onreadystatechange", arguments.callee);
  130.                                         callback();
  131.                                 }
  132.                         });
  133.                 }
  134.         };
  135.         return v;
  136. })();