Created
March 5, 2016 14:55
-
-
Save expalmer/2b231136a7c967008fb0 to your computer and use it in GitHub Desktop.
Todo List Vanilla JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function(context) { | |
'use strict'; | |
function $( selector, scope ) { | |
return $.qsa( selector, scope, true ); | |
} | |
$['qsa'] = function( selector, scope, first ) { | |
var e = ( scope || document).querySelectorAll( selector ); | |
return first ? e[0] : e; | |
}; | |
$['noop'] = function() {}; | |
$['each'] = function( array, cb ) { | |
var len = array.length; | |
var idx = -1; | |
while( ++idx < len ) { | |
cb.call(array, array[idx], idx, array); | |
} | |
}; | |
$['pluralization'] = function( value ) { | |
return +value === 1 ? "" : "s"; | |
}; | |
$['on'] = function (target, type, callback, useCapture) { | |
target.addEventListener(type, callback, !!useCapture); | |
}; | |
$['delegate'] = function (target, selector, type, handler) { | |
function dispatchEvent(event) { | |
var targetElement = event.target; | |
var potentialElements = $.qsa(selector, target); | |
var hasMatch = Array.prototype.indexOf.call(potentialElements, targetElement) >= 0; | |
if (hasMatch) { | |
handler.call(targetElement, event); | |
} | |
} | |
// https://developer.mozilla.org/en-US/docs/Web/Events/blur | |
var useCapture = type === 'blur' || type === 'focus'; | |
$.on(target, type, dispatchEvent, useCapture); | |
}; | |
$['parent'] = function (element, tagName) { | |
if (!element.parentNode) { | |
return; | |
} | |
if (element.parentNode.tagName.toLowerCase() === tagName.toLowerCase()) { | |
return element.parentNode; | |
} | |
return $.parent(element.parentNode, tagName); | |
}; | |
// classes | |
function splitStringToArray(className) { | |
return className.trim().split(/\s{1,}/g); | |
} | |
$['hasClass'] = function( e, c ) { | |
return splitStringToArray(e.className).indexOf(c) !== -1; | |
}; | |
$['addClass'] = function( e, c ) { | |
if( false === $.hasClass(e, c) ) { | |
var a = e.className; | |
a += " " + c; | |
e.className = a.trim(); | |
return true; | |
} | |
}; | |
$['removeClass'] = function( e, c ) { | |
var classes = splitStringToArray( e.className ); | |
var idx = classes.indexOf(c); | |
if( idx === -1 ) { | |
return false; | |
} | |
classes.splice(idx,1); | |
e.className = classes.join(' '); | |
}; | |
$['toggleClass'] = function( e, c ) { | |
if ( true !== $.addClass( e, c ) ) { | |
$.removeClass( e, c ); | |
} | |
}; | |
context.$ = $; | |
})(this); | |
// Prototype | |
NodeList.prototype.each = function( fn ) { | |
var len = this.length; | |
var idx = -1; | |
while( ++idx < len ) { | |
fn.call(this, this[idx], idx, this); | |
} | |
} | |
Array.prototype.some = function( fn ) { | |
var len = this.length; | |
var idx = -1; | |
while( ++idx < len ) { | |
if( fn( this[idx], idx, this ) ) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function(context) { | |
'use strict'; | |
var store = localStorage; | |
function Stores( key ) { | |
this.key = key; | |
if( !store[key] ) { | |
store[key] = JSON.stringify([]); | |
} | |
} | |
Stores.fn = Stores.prototype; | |
Stores.fn.find = function( id, cb ) { | |
var items = JSON.parse(store[this.key]); | |
var item = items | |
.filter(function(item) { | |
return id === item.id; | |
}); | |
cb.call(this, item[0] || {} ); | |
}; | |
Stores.fn.findAll = function( cb ) { | |
cb.call(this, JSON.parse( store[this.key] )); | |
}; | |
Stores.fn.save = function( item, cb, options ) { | |
var items = JSON.parse(store[this.key]); | |
// Update | |
if (item.id) { | |
items = items | |
.map(function( x ) { | |
if( x.id === item.id ) { | |
for (var prop in item ) { | |
x[prop] = item[prop]; | |
} | |
} | |
return x; | |
}); | |
// Insert | |
} else { | |
item.id = new Date().getTime(); | |
items.push(item); | |
} | |
store[this.key] = JSON.stringify(items); | |
cb.call(this, item); | |
}; | |
Stores.fn.destroy = function( id, cb ) { | |
var items = JSON.parse(store[this.key]); | |
items = items | |
.filter(function( x ) { | |
return x.id !== id; | |
}); | |
store[this.key] = JSON.stringify(items); | |
cb.call(this, true); | |
}; | |
Stores.fn.drop = function( cb ) { | |
store[this.key] = JSON.stringify([]); | |
this.findAll(cb); | |
}; | |
context.Stores = Stores; | |
})( this ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment