Last active
August 29, 2015 14:26
-
-
Save mholtzhausen/7a7381577c8d503210af to your computer and use it in GitHub Desktop.
copy, extend, eventlistener, object watcher < 200ln
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
'use strict'; | |
var Staple=(function(){ | |
var staple=function(){ | |
return 'version 1.0'; | |
}; | |
staple.copy= function (obj) { | |
return JSON.parse(JSON.stringify({object: obj})).object; | |
}; | |
staple.extend= function () { | |
var ret = {}; | |
if (arguments.length < 1) throw new Error('no objects passed'); | |
for (var idx = 0; idx < arguments.length; idx++) { | |
var arg = arguments[idx]; | |
if (arg === null || 'object' != typeof arg || Array.isArray(arg)) throw new Error('argument ' + idx + ' is not an object'); | |
for (var pidx in arg) { | |
var prop = arg[pidx]; | |
if (Array.isArray(prop)) throw new Error('argument ' + idx + '[' + pidx + '] is not an array'); | |
if (typeof(prop) == 'object') { | |
if (Object.keys(ext).indexOf(pidx) >= 0) { | |
ret[pidx] = staple.extend(ext[idx], prop); | |
} else { | |
ret[pidx] = staple.extend(prop); | |
} | |
} else { | |
ret[pidx] = prop; | |
} | |
} | |
} | |
return ret; | |
}; | |
staple.query= function (query, context) { | |
if('object' == typeof query && !Array.isArray(query))query=Array.prototype.slice.call(query); | |
if(query instanceof HTMLElement) return [query]; | |
query = Array.isArray(query) ? query : query.split(' '); | |
var currentQuery=query.shift(); | |
if(currentQuery instanceof HTMLElement) return staple.query(query).concat(currentQuery); | |
if(currentQuery=='')return []; | |
if (!context)context = document; | |
if(!Array.isArray(context))context=[context]; | |
var result = []; | |
for(var ctx=0; ctx<context.length; ctx++){ | |
var nodelist=context[ctx].querySelectorAll(currentQuery); | |
var nodeArray=Array.prototype.slice.call(nodelist); | |
result=result.concat(nodeArray); | |
} | |
if(query.length > 0){ | |
return staple.query(query,result); | |
}else{ | |
return result; | |
} | |
}; | |
staple.eventListener= function (obj, config) { | |
return (function (obj) { | |
var cfg = staple.extend({ | |
'on': 'on', | |
'trigger': 'trigger', | |
'list': 'list' | |
}, config || {}); | |
var l = {}; | |
var pvt = {}; | |
obj[cfg.on] = function (evt, cb) { | |
evt = ('' + evt).split(' '); | |
for (var edx in evt) { | |
if (typeof( l[evt[edx]] ) != 'object') { | |
l[evt[edx]] = []; | |
} | |
l[evt[edx]].push(cb); | |
} | |
}; | |
obj[cfg.trigger] = function (evt, data) { | |
var success = true; | |
var evt = evt.split(' '); | |
for (var edx in evt) { | |
if (typeof( l[evt[edx]] == 'object' )) { | |
for (var idx in l[evt[edx]]) { | |
if (typeof( l[evt[edx]][idx] ) == 'function') { | |
success = success && (l[evt[edx]][idx].apply({}, [data, evt[edx]]) !== false); | |
} | |
} | |
} | |
} | |
return success; | |
}; | |
obj[cfg.list] = function () { | |
return Object.keys(l); | |
}; | |
return obj; | |
})(obj || {}); | |
}; | |
staple.observe= function (init) { | |
return (function (init) { | |
var data = staple.extend({}, init); | |
var evt = staple.eventListener(); | |
var main = function () { | |
var argc = arguments.length; | |
if (argc == 1) { | |
console.log('here'); | |
if (Object.keys(data).indexOf(arguments[0]) >= 0) { | |
return data[arguments[0]]; | |
} | |
} else if (argc == 2) { | |
var undefined; | |
var orig = main(arguments[0]); | |
data[arguments[0]] = staple.copy(arguments[1]); | |
evt.trigger(arguments[0], {before: orig, after: main(arguments[0])}); | |
return main(arguments[0]); | |
} else { | |
throw new Error('Too many arguments'); | |
} | |
}; | |
main.all = function () { | |
return staple.copy(data); | |
}; | |
main.clear = function () { | |
data = {}; | |
}; | |
main.update = function (obj) { | |
if ('object' == typeof obj && !Array.isArray(obj)) { | |
for (var key in obj) { | |
main(key, obj[key]); | |
} | |
} else { | |
throw new Error('Takes an Object as parameter') | |
} | |
}; | |
main.import = function (obj, keys) { | |
if (!Array.isArray(keys) && typeof(keys) !== 'object') keys = keys.split(','); | |
if (Array.isArray(keys)) { | |
for (var idx = 0; idx < keys.length; idx++) { | |
if (typeof(obj[keys[idx]]) != 'undefined') { | |
main(keys[idx], obj[keys[idx]]); | |
} | |
} | |
} else { | |
for (var idx in keys) { | |
var current = keys[idx] ? keys[idx] : idx; | |
if (typeof(obj[current]) != 'undefined') { | |
main(idx, obj[current]); | |
} | |
} | |
} | |
}; | |
main.watch = function (key, cb) { | |
evt.on(key, cb); | |
}; | |
main.data = data; | |
return main; | |
})(init || {}); | |
}; | |
staple.bound= function (init) { | |
return (function (init) { | |
var data = staple.extend({}, init); | |
var changes = []; | |
var evt = staple.eventListener(); | |
var main = function () { | |
var argc = arguments.length; | |
if (argc == 1) { | |
if (Object.keys(data).indexOf(arguments[0]) >= 0) { | |
return data[arguments[0]]; | |
} | |
} else if (argc == 2) { | |
var undefined; | |
var orig = main(arguments[0]); | |
data[arguments[0]] = staple.copy(arguments[1]); | |
if (orig !== arguments[1]) { | |
if (changes.indexOf(arguments[0]) < 0) changes.push(arguments[0]) | |
} | |
return main; | |
} else { | |
throw new Error('Too many arguments'); | |
} | |
}; | |
main.eventListener=evt; | |
main.all=function(){ | |
return Staple.copy(data); | |
}; | |
main.apply = function () { | |
for (var idx = 0; idx < changes.length; idx++) { | |
evt.trigger(changes[idx], data[changes[idx]]); | |
} | |
changes = []; | |
}; | |
main.render = function () { | |
var watched = evt.list(); | |
for (var idx = 0; idx < watched.length; idx++) { | |
evt.trigger(watched[idx], data[watched[idx]]); | |
} | |
}; | |
main.bind = function (elem) { | |
elem=staple.query(elem); | |
return (function (elem, bound) { | |
var bind = {}; | |
bind.html = function (prop, middleware) { | |
evt.on(prop, (function (bound, elem, prop, middleware) { | |
return function (data, prop) { | |
var val = data; | |
if (!Array.isArray(middleware)) middleware = [middleware]; | |
for (var idx = 0; idx < middleware.length; idx++) { | |
if ('function' == typeof middleware[idx]) val = middleware[idx](val); | |
} | |
elem.forEach(function(item){ | |
item.innerHTML=val; | |
}); | |
}; | |
})(main, elem, prop, middleware)); | |
return bind; | |
}; | |
bind.value = function (prop, middleware) { | |
evt.on(prop, (function (bound, elem, prop, middleware) { | |
return function (data, prop) { | |
var val = data; | |
if (!Array.isArray(middleware)) middleware = [middleware]; | |
for (var idx = 0; idx < middleware.length; idx++) { | |
if ('function' == typeof middleware[idx]) val = middleware[idx](val); | |
} | |
elem.forEach(function(item){ | |
item.value = val; | |
}); | |
}; | |
})(main, elem, prop, middleware)); | |
return bind; | |
}; | |
bind.attr = function (attribute, prop, middleware) { | |
evt.on(prop, (function (bound, elem, prop, middleware, attribute) { | |
return function (data, prop) { | |
var val = data; | |
if (!Array.isArray(middleware)) middleware = [middleware]; | |
for (var idx = 0; idx < middleware.length; idx++) { | |
if ('function' == typeof middleware[idx]) val = middleware[idx](val); | |
} | |
elem.forEach(function(item){ | |
item.setAttribute(attribute, val); | |
}); | |
}; | |
})(main, elem, prop, middleware, attribute)); | |
return bind; | |
}; | |
return bind; | |
})(elem, main) | |
}; | |
return main; | |
})(init || {}) | |
}; | |
return staple; | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment