From b921eee7bbb2ca852205215edf46db5333fd3e07 Mon Sep 17 00:00:00 2001 From: Lea Verou Date: Fri, 11 Dec 2015 19:38:10 -0500 Subject: [PATCH] Fixed #86, started docs for #87, added new method $.each(), with docs (needs examples) --- bliss._.js | 14 +++++---- bliss.js | 75 ++++++++++++++++++++++++++++-------------------- bliss.min.js | 2 +- bliss.shy.js | 61 +++++++++++++++++++++++---------------- bliss.shy.min.js | 2 +- docs.html | 42 +++++++++++++++++++++++++++ 6 files changed, 132 insertions(+), 64 deletions(-) diff --git a/bliss._.js b/bliss._.js index 0dc33fe..236d785 100644 --- a/bliss._.js +++ b/bliss._.js @@ -57,9 +57,10 @@ Object.defineProperty(Array.prototype, _, { if (self.EventTarget && "addEventListener" in EventTarget.prototype) { var addEventListener = EventTarget.prototype.addEventListener, removeEventListener = EventTarget.prototype.removeEventListener, - filter = function(callback, capture, l){ - return !(l.callback === callback && l.capture == capture); - }; + equal = function(callback, capture, l){ + return l.callback === callback && l.capture == capture; + }, + notEqual = function() { return !equal.apply(this, arguments); }; EventTarget.prototype.addEventListener = function(type, callback, capture) { if (this[_] && callback) { @@ -67,7 +68,7 @@ if (self.EventTarget && "addEventListener" in EventTarget.prototype) { listeners[type] = listeners[type] || []; - if (listeners[type].filter(filter.bind(null, callback, capture)).length === 0) { + if (listeners[type].filter(equal.bind(null, callback, capture)).length === 0) { listeners[type].push({callback: callback, capture: capture}); } } @@ -79,8 +80,9 @@ if (self.EventTarget && "addEventListener" in EventTarget.prototype) { if (this[_] && callback) { var listeners = this[_].bliss.listeners = this[_].bliss.listeners || {}; - listeners[type] = listeners[type] || []; - listeners[type] = listeners[type].filter(filter.bind(null, callback, capture)); + if (listeners[type]) { + listeners[type] = listeners[type].filter(notEqual.bind(null, callback, capture)); + } } return removeEventListener.call(this, type, callback, capture); diff --git a/bliss.js b/bliss.js index 644119e..c670648 100644 --- a/bliss.js +++ b/bliss.js @@ -94,6 +94,16 @@ extend($, { return $.set(document.createElement(tag || "div"), o); }, + each: function(obj, callback, ret) { + ret = ret || {}; + + for (var property in obj) { + ret[property] = callback.call(obj, property, obj[property]); + } + + return ret; + }, + ready: function(context) { context = context || document; @@ -445,15 +455,23 @@ $.setProps = { val[arguments[0]] = arguments[1]; } - for (var events in val) { - events.split(/\s+/).forEach(function (event) { - var me = this; - this.addEventListener(event, function callback() { - me.removeEventListener(event, callback); - return val[events].apply(this, arguments); + var me = this; + + $.each(val, function(events, callback){ + events = events.split(/\s+/); + + var once = function() { + events.forEach(function(event){ + me.removeEventListener(event, once); }); - }, this); - } + + return callback.apply(me, arguments); + }; + + events.forEach(function (event) { + me.addEventListener(event, once); + }); + }); }, // Event delegation @@ -472,17 +490,15 @@ $.setProps = { var element = this; - for (var type in val) { - (function (type, callbacks) { - element.addEventListener(type, function(evt) { - for (var selector in callbacks) { - if (evt.target.matches(selector)) { // Do ancestors count? - callbacks[selector].call(this, evt); - } + $.each(val, function (type, callbacks) { + element.addEventListener(type, function(evt) { + for (var selector in callbacks) { + if (evt.target.matches(selector)) { // Do ancestors count? + callbacks[selector].call(this, evt); } - }); - })(type, val[type]); - } + } + }); + }); }, // Set the contents as a string, an element, an object to create an element or an array of these @@ -556,10 +572,7 @@ $.add = function (methods, on, noOverwrite) { methods[arguments[0]] = arguments[1]; } - for (var method in methods) { - // In the future, we should use let instead of a closure - (function(method, callback){ - + $.each(methods, function(method, callback){ if ($.type(callback) == "function") { if (on.element && (!(method in $.Element.prototype) || !noOverwrite)) { $.Element.prototype[method] = function () { @@ -590,9 +603,7 @@ $.add = function (methods, on, noOverwrite) { } } } - - })(method, methods[method]); - } + }); }; $.add($.Array.prototype, {element: false}); @@ -668,9 +679,10 @@ Object.defineProperty(Array.prototype, _, { if (self.EventTarget && "addEventListener" in EventTarget.prototype) { var addEventListener = EventTarget.prototype.addEventListener, removeEventListener = EventTarget.prototype.removeEventListener, - filter = function(callback, capture, l){ - return !(l.callback === callback && l.capture == capture); - }; + equal = function(callback, capture, l){ + return l.callback === callback && l.capture == capture; + }, + notEqual = function() { return !equal.apply(this, arguments); }; EventTarget.prototype.addEventListener = function(type, callback, capture) { if (this[_] && callback) { @@ -678,7 +690,7 @@ if (self.EventTarget && "addEventListener" in EventTarget.prototype) { listeners[type] = listeners[type] || []; - if (listeners[type].filter(filter.bind(null, callback, capture)).length === 0) { + if (listeners[type].filter(equal.bind(null, callback, capture)).length === 0) { listeners[type].push({callback: callback, capture: capture}); } } @@ -690,8 +702,9 @@ if (self.EventTarget && "addEventListener" in EventTarget.prototype) { if (this[_] && callback) { var listeners = this[_].bliss.listeners = this[_].bliss.listeners || {}; - listeners[type] = listeners[type] || []; - listeners[type] = listeners[type].filter(filter.bind(null, callback, capture)); + if (listeners[type]) { + listeners[type] = listeners[type].filter(notEqual.bind(null, callback, capture)); + } } return removeEventListener.call(this, type, callback, capture); diff --git a/bliss.min.js b/bliss.min.js index 2927a35..a18ca75 100644 --- a/bliss.min.js +++ b/bliss.min.js @@ -1 +1 @@ -!function(){"use strict";function t(t,n,r){for(var s in n){if(r){var i=e.type(r);if("own"===r&&!n.hasOwnProperty(s)||"array"===i&&-1===r.indexOf(s)||"regexp"===i&&!r.test(s)||"function"===i&&!r.call(n,s))continue}var o=Object.getOwnPropertyDescriptor(n,s);!o||o.writable&&o.configurable&&o.enumerable&&!o.get&&!o.set?t[s]=n[s]:(delete t[s],Object.defineProperty(t,s,o))}return t}var e=self.Bliss=t(function(t,n){return"string"===e.type(t)?(n||document).querySelector(t):t||null},self.Bliss);t(e,{extend:t,property:e.property||"_",sources:{},$:function(t,e){return t instanceof Node||t instanceof Window?[t]:Array.prototype.slice.call("string"==typeof t?(e||document).querySelectorAll(t):t||[])},type:function(t){if(null===t)return"null";if(void 0===t)return"undefined";var e=(Object.prototype.toString.call(t).match(/^\[object\s+(.*?)\]$/)[1]||"").toLowerCase();return"number"==e&&isNaN(t)?"nan":e},defined:function(){for(var t=0;t=3)Object.defineProperty(t,n,{get:function(){return delete this[n],this[n]=r.call(this)},configurable:!0,enumerable:!0});else if(2===arguments.length)for(var s in n)e.lazy(t,s,n[s]);return t},live:function(t,n,r){if(arguments.length>=3)Object.defineProperty(t,n,{get:function(){var t=this["_"+n],e=r.get&&r.get.call(this,t);return void 0!==e?e:t},set:function(t){var e=this["_"+n],s=r.set&&r.set.call(this,t,e);this["_"+n]=void 0!==s?s:t},configurable:r.configurable,enumerable:r.enumerable});else if(2===arguments.length)for(var s in n)e.live(t,s,n[s]);return t}},include:function(){var t=arguments[arguments.length-1],n=2===arguments.length?arguments[0]:!1,r=document.createElement("script");return n?Promise.resolve():new Promise(function(n,s){e.set(r,{async:!0,onload:function(){n(),e.remove(r)},onerror:function(){s()},src:t,inside:document.head})})},fetch:function(t,n){if(!t)throw new TypeError("URL parameter is mandatory and cannot be "+t);t=new URL(t,location),n=n||{},n.data=n.data||"",n.method=n.method||"GET",n.headers=n.headers||{};var r=new XMLHttpRequest;"string"!==e.type(n.data)&&(n.data=Object.keys(n.data).map(function(t){return t+"="+encodeURIComponent(n.data[t])}).join("&")),"GET"===n.method&&n.data&&(t.search+=n.data),document.body.setAttribute("data-loading",t),r.open(n.method,t,!n.sync);for(var s in n)if(s in r)try{r[s]=n[s]}catch(i){self.console&&console.error(i)}"GET"===n.method||n.headers["Content-type"]||n.headers["Content-Type"]||r.setRequestHeader("Content-type","application/x-www-form-urlencoded");for(var o in n.headers)r.setRequestHeader(o,n.headers[o]);return new Promise(function(t,e){r.onload=function(){document.body.removeAttribute("data-loading"),0===r.status||r.status>=200&&r.status<300||304===r.status?t(r):e(Error(r.statusText))},r.onerror=function(){document.body.removeAttribute("data-loading"),e(Error("Network Error"))},r.send("GET"===n.method?null:n.data)})}});var n=e.property;e.Element=function(t){this.subject=t,this.data={},this.bliss={}},e.Element.prototype={set:function(t){"string"===e.type(arguments[0])&&(t={},t[arguments[0]]=arguments[1]);for(var n in t)n in e.setProps?e.setProps[n].call(this,t[n]):n in this?this[n]=t[n]:(this.setAttribute||console.log(this),this.setAttribute(n,t[n]))},transition:function(t,n){return n=+n||400,new Promise(function(r,s){if("transition"in this.style){var i=e.extend({},this.style,/^transition(Duration|Property)$/);e.style(this,{transitionDuration:(n||400)+"ms",transitionProperty:Object.keys(t).join(", ")}),e.once(this,"transitionend",function(){clearTimeout(o),e.style(this,i),r(this)});var o=setTimeout(r,n+50,this);e.style(this,t)}else e.style(this,t),r(this)}.bind(this))},fire:function(t,n){var r=document.createEvent("HTMLEvents");r.initEvent(t,!0,!0),this.dispatchEvent(e.extend(r,n))}},e.setProps={style:function(t){e.extend(this.style,t)},attributes:function(t){for(var e in t)this.setAttribute(e,t[e])},properties:function(t){e.extend(this,t)},events:function(t){if(t&&t.addEventListener){var e=this;if(t[n]&&t[n].bliss){var r=t[n].bliss.listeners;for(var s in r)r[s].forEach(function(t){e.addEventListener(s,t.callback,t.capture)})}for(var i in t)0===i.indexOf("on")&&(this[i]=t[i])}else for(var o in t)o.split(/\s+/).forEach(function(e){this.addEventListener(e,t[o])},this)},once:function(t){2==arguments.length&&(t={},t[arguments[0]]=arguments[1]);for(var e in t)e.split(/\s+/).forEach(function(n){var r=this;this.addEventListener(n,function s(){return r.removeEventListener(n,s),t[e].apply(this,arguments)})},this)},delegate:function(t){3===arguments.length?(t={},t[arguments[0]]={},t[arguments[0]][arguments[1]]=arguments[2]):2===arguments.length&&(t={},t[arguments[0]]=arguments[1]);var e=this;for(var n in t)!function(t,n){e.addEventListener(t,function(t){for(var e in n)t.target.matches(e)&&n[e].call(this,t)})}(n,t[n])},contents:function(t){(t||0===t)&&(Array.isArray(t)?t:[t]).forEach(function(t){var n=e.type(t);/^(string|number)$/.test(n)?t=document.createTextNode(t+""):"object"===n&&(t=e.create(t)),t instanceof Node&&this.appendChild(t)},this)},inside:function(t){t.appendChild(this)},before:function(t){t.parentNode.insertBefore(this,t)},after:function(t){t.parentNode.insertBefore(this,t.nextSibling)},start:function(t){t.insertBefore(this,t.firstChild)},around:function(t){t.parentNode&&e.before(this,t),(/^template$/i.test(this.nodeName)?this.content||this:this).appendChild(t)}},e.Array=function(t){this.subject=t},e.Array.prototype={all:function(t){var e=$$(arguments).slice(1);return this[t].apply(this,e)}},e.add=function(t,n,r){n=e.extend({$:!0,element:!0,array:!0},n),"string"===e.type(arguments[0])&&(t={},t[arguments[0]]=arguments[1]);for(var s in t)!function(t,s){"function"==e.type(s)&&(!n.element||t in e.Element.prototype&&r||(e.Element.prototype[t]=function(){return this.subject&&e.defined(s.apply(this.subject,arguments),this.subject)}),!n.array||t in e.Array.prototype&&r||(e.Array.prototype[t]=function(){var t=arguments;return this.subject.map(function(n){return n&&e.defined(s.apply(n,t),n)})}),n.$&&(e.sources[t]=e[t]=s,(n.array||n.element)&&(e[t]=function(){var r=[].slice.apply(arguments),s=r.shift(),i=n.array&&Array.isArray(s)?"Array":"Element";return e[i].prototype[t].apply({subject:s},r)})))}(s,t[s])},e.add(e.Array.prototype,{element:!1}),e.add(e.Element.prototype),e.add(e.setProps),e.add(e.classProps,{element:!1,array:!1});var r=document.createElement("_");e.add(e.extend({},HTMLElement.prototype,function(t){return"function"===e.type(r[t])}),null,!0)}(),function(t){"use strict";if(Bliss&&!Bliss.shy){var e=Bliss.property;if(t.add({clone:function(){var e=this.cloneNode(!0),n=t.$("*",e).concat(e);return t.$("*",this).concat(this).forEach(function(e,r,s){t.events(n[r],e)}),e}},{array:!1}),Object.defineProperty(Node.prototype,e,{get:function i(){return Object.defineProperty(Node.prototype,e,{get:void 0}),Object.defineProperty(this,e,{value:new t.Element(this)}),Object.defineProperty(Node.prototype,e,{get:i}),this[e]},configurable:!0}),Object.defineProperty(Array.prototype,e,{get:function(){return Object.defineProperty(this,e,{value:new t.Array(this)}),this[e]},configurable:!0}),self.EventTarget&&"addEventListener"in EventTarget.prototype){var n=EventTarget.prototype.addEventListener,r=EventTarget.prototype.removeEventListener,s=function(t,e,n){return!(n.callback===t&&n.capture==e)};EventTarget.prototype.addEventListener=function(t,r,i){if(this[e]&&r){var o=this[e].bliss.listeners=this[e].bliss.listeners||{};o[t]=o[t]||[],0===o[t].filter(s.bind(null,r,i)).length&&o[t].push({callback:r,capture:i})}return n.call(this,t,r,i)},EventTarget.prototype.removeEventListener=function(t,n,i){if(this[e]&&n){var o=this[e].bliss.listeners=this[e].bliss.listeners||{};o[t]=o[t]||[],o[t]=o[t].filter(s.bind(null,n,i))}return r.call(this,t,n,i)}}self.$=self.$||t,self.$$=self.$$||t.$}}(Bliss); \ No newline at end of file +!function(){"use strict";function t(t,n,r){for(var s in n){if(r){var i=e.type(r);if("own"===r&&!n.hasOwnProperty(s)||"array"===i&&-1===r.indexOf(s)||"regexp"===i&&!r.test(s)||"function"===i&&!r.call(n,s))continue}var o=Object.getOwnPropertyDescriptor(n,s);!o||o.writable&&o.configurable&&o.enumerable&&!o.get&&!o.set?t[s]=n[s]:(delete t[s],Object.defineProperty(t,s,o))}return t}var e=self.Bliss=t(function(t,n){return"string"===e.type(t)?(n||document).querySelector(t):t||null},self.Bliss);t(e,{extend:t,property:e.property||"_",sources:{},$:function(t,e){return t instanceof Node||t instanceof Window?[t]:Array.prototype.slice.call("string"==typeof t?(e||document).querySelectorAll(t):t||[])},type:function(t){if(null===t)return"null";if(void 0===t)return"undefined";var e=(Object.prototype.toString.call(t).match(/^\[object\s+(.*?)\]$/)[1]||"").toLowerCase();return"number"==e&&isNaN(t)?"nan":e},defined:function(){for(var t=0;t=3)Object.defineProperty(t,n,{get:function(){return delete this[n],this[n]=r.call(this)},configurable:!0,enumerable:!0});else if(2===arguments.length)for(var s in n)e.lazy(t,s,n[s]);return t},live:function(t,n,r){if(arguments.length>=3)Object.defineProperty(t,n,{get:function(){var t=this["_"+n],e=r.get&&r.get.call(this,t);return void 0!==e?e:t},set:function(t){var e=this["_"+n],s=r.set&&r.set.call(this,t,e);this["_"+n]=void 0!==s?s:t},configurable:r.configurable,enumerable:r.enumerable});else if(2===arguments.length)for(var s in n)e.live(t,s,n[s]);return t}},include:function(){var t=arguments[arguments.length-1],n=2===arguments.length?arguments[0]:!1,r=document.createElement("script");return n?Promise.resolve():new Promise(function(n,s){e.set(r,{async:!0,onload:function(){n(),e.remove(r)},onerror:function(){s()},src:t,inside:document.head})})},fetch:function(t,n){if(!t)throw new TypeError("URL parameter is mandatory and cannot be "+t);t=new URL(t,location),n=n||{},n.data=n.data||"",n.method=n.method||"GET",n.headers=n.headers||{};var r=new XMLHttpRequest;"string"!==e.type(n.data)&&(n.data=Object.keys(n.data).map(function(t){return t+"="+encodeURIComponent(n.data[t])}).join("&")),"GET"===n.method&&n.data&&(t.search+=n.data),document.body.setAttribute("data-loading",t),r.open(n.method,t,!n.sync);for(var s in n)if(s in r)try{r[s]=n[s]}catch(i){self.console&&console.error(i)}"GET"===n.method||n.headers["Content-type"]||n.headers["Content-Type"]||r.setRequestHeader("Content-type","application/x-www-form-urlencoded");for(var o in n.headers)r.setRequestHeader(o,n.headers[o]);return new Promise(function(t,e){r.onload=function(){document.body.removeAttribute("data-loading"),0===r.status||r.status>=200&&r.status<300||304===r.status?t(r):e(Error(r.statusText))},r.onerror=function(){document.body.removeAttribute("data-loading"),e(Error("Network Error"))},r.send("GET"===n.method?null:n.data)})}});var n=e.property;e.Element=function(t){this.subject=t,this.data={},this.bliss={}},e.Element.prototype={set:function(t){"string"===e.type(arguments[0])&&(t={},t[arguments[0]]=arguments[1]);for(var n in t)n in e.setProps?e.setProps[n].call(this,t[n]):n in this?this[n]=t[n]:(this.setAttribute||console.log(this),this.setAttribute(n,t[n]))},transition:function(t,n){return n=+n||400,new Promise(function(r,s){if("transition"in this.style){var i=e.extend({},this.style,/^transition(Duration|Property)$/);e.style(this,{transitionDuration:(n||400)+"ms",transitionProperty:Object.keys(t).join(", ")}),e.once(this,"transitionend",function(){clearTimeout(o),e.style(this,i),r(this)});var o=setTimeout(r,n+50,this);e.style(this,t)}else e.style(this,t),r(this)}.bind(this))},fire:function(t,n){var r=document.createEvent("HTMLEvents");r.initEvent(t,!0,!0),this.dispatchEvent(e.extend(r,n))}},e.setProps={style:function(t){e.extend(this.style,t)},attributes:function(t){for(var e in t)this.setAttribute(e,t[e])},properties:function(t){e.extend(this,t)},events:function(t){if(t&&t.addEventListener){var e=this;if(t[n]&&t[n].bliss){var r=t[n].bliss.listeners;for(var s in r)r[s].forEach(function(t){e.addEventListener(s,t.callback,t.capture)})}for(var i in t)0===i.indexOf("on")&&(this[i]=t[i])}else for(var o in t)o.split(/\s+/).forEach(function(e){this.addEventListener(e,t[o])},this)},once:function(t){2==arguments.length&&(t={},t[arguments[0]]=arguments[1]);var n=this;e.each(t,function(t,e){t=t.split(/\s+/);var r=function(){return t.forEach(function(t){n.removeEventListener(t,r)}),e.apply(n,arguments)};t.forEach(function(t){n.addEventListener(t,r)})})},delegate:function(t){3===arguments.length?(t={},t[arguments[0]]={},t[arguments[0]][arguments[1]]=arguments[2]):2===arguments.length&&(t={},t[arguments[0]]=arguments[1]);var n=this;e.each(t,function(t,e){n.addEventListener(t,function(t){for(var n in e)t.target.matches(n)&&e[n].call(this,t)})})},contents:function(t){(t||0===t)&&(Array.isArray(t)?t:[t]).forEach(function(t){var n=e.type(t);/^(string|number)$/.test(n)?t=document.createTextNode(t+""):"object"===n&&(t=e.create(t)),t instanceof Node&&this.appendChild(t)},this)},inside:function(t){t.appendChild(this)},before:function(t){t.parentNode.insertBefore(this,t)},after:function(t){t.parentNode.insertBefore(this,t.nextSibling)},start:function(t){t.insertBefore(this,t.firstChild)},around:function(t){t.parentNode&&e.before(this,t),(/^template$/i.test(this.nodeName)?this.content||this:this).appendChild(t)}},e.Array=function(t){this.subject=t},e.Array.prototype={all:function(t){var e=$$(arguments).slice(1);return this[t].apply(this,e)}},e.add=function(t,n,r){n=e.extend({$:!0,element:!0,array:!0},n),"string"===e.type(arguments[0])&&(t={},t[arguments[0]]=arguments[1]),e.each(t,function(t,s){"function"==e.type(s)&&(!n.element||t in e.Element.prototype&&r||(e.Element.prototype[t]=function(){return this.subject&&e.defined(s.apply(this.subject,arguments),this.subject)}),!n.array||t in e.Array.prototype&&r||(e.Array.prototype[t]=function(){var t=arguments;return this.subject.map(function(n){return n&&e.defined(s.apply(n,t),n)})}),n.$&&(e.sources[t]=e[t]=s,(n.array||n.element)&&(e[t]=function(){var r=[].slice.apply(arguments),s=r.shift(),i=n.array&&Array.isArray(s)?"Array":"Element";return e[i].prototype[t].apply({subject:s},r)})))})},e.add(e.Array.prototype,{element:!1}),e.add(e.Element.prototype),e.add(e.setProps),e.add(e.classProps,{element:!1,array:!1});var r=document.createElement("_");e.add(e.extend({},HTMLElement.prototype,function(t){return"function"===e.type(r[t])}),null,!0)}(),function(t){"use strict";if(Bliss&&!Bliss.shy){var e=Bliss.property;if(t.add({clone:function(){var e=this.cloneNode(!0),n=t.$("*",e).concat(e);return t.$("*",this).concat(this).forEach(function(e,r,s){t.events(n[r],e)}),e}},{array:!1}),Object.defineProperty(Node.prototype,e,{get:function o(){return Object.defineProperty(Node.prototype,e,{get:void 0}),Object.defineProperty(this,e,{value:new t.Element(this)}),Object.defineProperty(Node.prototype,e,{get:o}),this[e]},configurable:!0}),Object.defineProperty(Array.prototype,e,{get:function(){return Object.defineProperty(this,e,{value:new t.Array(this)}),this[e]},configurable:!0}),self.EventTarget&&"addEventListener"in EventTarget.prototype){var n=EventTarget.prototype.addEventListener,r=EventTarget.prototype.removeEventListener,s=function(t,e,n){return n.callback===t&&n.capture==e},i=function(){return!s.apply(this,arguments)};EventTarget.prototype.addEventListener=function(t,r,i){if(this[e]&&r){var o=this[e].bliss.listeners=this[e].bliss.listeners||{};o[t]=o[t]||[],0===o[t].filter(s.bind(null,r,i)).length&&o[t].push({callback:r,capture:i})}return n.call(this,t,r,i)},EventTarget.prototype.removeEventListener=function(t,n,s){if(this[e]&&n){var o=this[e].bliss.listeners=this[e].bliss.listeners||{};o[t]&&(o[t]=o[t].filter(i.bind(null,n,s)))}return r.call(this,t,n,s)}}self.$=self.$||t,self.$$=self.$$||t.$}}(Bliss); \ No newline at end of file diff --git a/bliss.shy.js b/bliss.shy.js index 6b93f6d..0908504 100644 --- a/bliss.shy.js +++ b/bliss.shy.js @@ -94,6 +94,16 @@ extend($, { return $.set(document.createElement(tag || "div"), o); }, + each: function(obj, callback, ret) { + ret = ret || {}; + + for (var property in obj) { + ret[property] = callback.call(obj, property, obj[property]); + } + + return ret; + }, + ready: function(context) { context = context || document; @@ -445,15 +455,23 @@ $.setProps = { val[arguments[0]] = arguments[1]; } - for (var events in val) { - events.split(/\s+/).forEach(function (event) { - var me = this; - this.addEventListener(event, function callback() { - me.removeEventListener(event, callback); - return val[events].apply(this, arguments); + var me = this; + + $.each(val, function(events, callback){ + events = events.split(/\s+/); + + var once = function() { + events.forEach(function(event){ + me.removeEventListener(event, once); }); - }, this); - } + + return callback.apply(me, arguments); + }; + + events.forEach(function (event) { + me.addEventListener(event, once); + }); + }); }, // Event delegation @@ -472,17 +490,15 @@ $.setProps = { var element = this; - for (var type in val) { - (function (type, callbacks) { - element.addEventListener(type, function(evt) { - for (var selector in callbacks) { - if (evt.target.matches(selector)) { // Do ancestors count? - callbacks[selector].call(this, evt); - } + $.each(val, function (type, callbacks) { + element.addEventListener(type, function(evt) { + for (var selector in callbacks) { + if (evt.target.matches(selector)) { // Do ancestors count? + callbacks[selector].call(this, evt); } - }); - })(type, val[type]); - } + } + }); + }); }, // Set the contents as a string, an element, an object to create an element or an array of these @@ -556,10 +572,7 @@ $.add = function (methods, on, noOverwrite) { methods[arguments[0]] = arguments[1]; } - for (var method in methods) { - // In the future, we should use let instead of a closure - (function(method, callback){ - + $.each(methods, function(method, callback){ if ($.type(callback) == "function") { if (on.element && (!(method in $.Element.prototype) || !noOverwrite)) { $.Element.prototype[method] = function () { @@ -590,9 +603,7 @@ $.add = function (methods, on, noOverwrite) { } } } - - })(method, methods[method]); - } + }); }; $.add($.Array.prototype, {element: false}); diff --git a/bliss.shy.min.js b/bliss.shy.min.js index e9da38c..396a029 100644 --- a/bliss.shy.min.js +++ b/bliss.shy.min.js @@ -1 +1 @@ -!function(){"use strict";function t(t,n,r){for(var s in n){if(r){var i=e.type(r);if("own"===r&&!n.hasOwnProperty(s)||"array"===i&&-1===r.indexOf(s)||"regexp"===i&&!r.test(s)||"function"===i&&!r.call(n,s))continue}var o=Object.getOwnPropertyDescriptor(n,s);!o||o.writable&&o.configurable&&o.enumerable&&!o.get&&!o.set?t[s]=n[s]:(delete t[s],Object.defineProperty(t,s,o))}return t}var e=self.Bliss=t(function(t,n){return"string"===e.type(t)?(n||document).querySelector(t):t||null},self.Bliss);t(e,{extend:t,property:e.property||"_",sources:{},$:function(t,e){return t instanceof Node||t instanceof Window?[t]:Array.prototype.slice.call("string"==typeof t?(e||document).querySelectorAll(t):t||[])},type:function(t){if(null===t)return"null";if(void 0===t)return"undefined";var e=(Object.prototype.toString.call(t).match(/^\[object\s+(.*?)\]$/)[1]||"").toLowerCase();return"number"==e&&isNaN(t)?"nan":e},defined:function(){for(var t=0;t=3)Object.defineProperty(t,n,{get:function(){return delete this[n],this[n]=r.call(this)},configurable:!0,enumerable:!0});else if(2===arguments.length)for(var s in n)e.lazy(t,s,n[s]);return t},live:function(t,n,r){if(arguments.length>=3)Object.defineProperty(t,n,{get:function(){var t=this["_"+n],e=r.get&&r.get.call(this,t);return void 0!==e?e:t},set:function(t){var e=this["_"+n],s=r.set&&r.set.call(this,t,e);this["_"+n]=void 0!==s?s:t},configurable:r.configurable,enumerable:r.enumerable});else if(2===arguments.length)for(var s in n)e.live(t,s,n[s]);return t}},include:function(){var t=arguments[arguments.length-1],n=2===arguments.length?arguments[0]:!1,r=document.createElement("script");return n?Promise.resolve():new Promise(function(n,s){e.set(r,{async:!0,onload:function(){n(),e.remove(r)},onerror:function(){s()},src:t,inside:document.head})})},fetch:function(t,n){if(!t)throw new TypeError("URL parameter is mandatory and cannot be "+t);t=new URL(t,location),n=n||{},n.data=n.data||"",n.method=n.method||"GET",n.headers=n.headers||{};var r=new XMLHttpRequest;"string"!==e.type(n.data)&&(n.data=Object.keys(n.data).map(function(t){return t+"="+encodeURIComponent(n.data[t])}).join("&")),"GET"===n.method&&n.data&&(t.search+=n.data),document.body.setAttribute("data-loading",t),r.open(n.method,t,!n.sync);for(var s in n)if(s in r)try{r[s]=n[s]}catch(i){self.console&&console.error(i)}"GET"===n.method||n.headers["Content-type"]||n.headers["Content-Type"]||r.setRequestHeader("Content-type","application/x-www-form-urlencoded");for(var o in n.headers)r.setRequestHeader(o,n.headers[o]);return new Promise(function(t,e){r.onload=function(){document.body.removeAttribute("data-loading"),0===r.status||r.status>=200&&r.status<300||304===r.status?t(r):e(Error(r.statusText))},r.onerror=function(){document.body.removeAttribute("data-loading"),e(Error("Network Error"))},r.send("GET"===n.method?null:n.data)})}});var n=e.property;e.Element=function(t){this.subject=t,this.data={},this.bliss={}},e.Element.prototype={set:function(t){"string"===e.type(arguments[0])&&(t={},t[arguments[0]]=arguments[1]);for(var n in t)n in e.setProps?e.setProps[n].call(this,t[n]):n in this?this[n]=t[n]:(this.setAttribute||console.log(this),this.setAttribute(n,t[n]))},transition:function(t,n){return n=+n||400,new Promise(function(r,s){if("transition"in this.style){var i=e.extend({},this.style,/^transition(Duration|Property)$/);e.style(this,{transitionDuration:(n||400)+"ms",transitionProperty:Object.keys(t).join(", ")}),e.once(this,"transitionend",function(){clearTimeout(o),e.style(this,i),r(this)});var o=setTimeout(r,n+50,this);e.style(this,t)}else e.style(this,t),r(this)}.bind(this))},fire:function(t,n){var r=document.createEvent("HTMLEvents");r.initEvent(t,!0,!0),this.dispatchEvent(e.extend(r,n))}},e.setProps={style:function(t){e.extend(this.style,t)},attributes:function(t){for(var e in t)this.setAttribute(e,t[e])},properties:function(t){e.extend(this,t)},events:function(t){if(t&&t.addEventListener){var e=this;if(t[n]&&t[n].bliss){var r=t[n].bliss.listeners;for(var s in r)r[s].forEach(function(t){e.addEventListener(s,t.callback,t.capture)})}for(var i in t)0===i.indexOf("on")&&(this[i]=t[i])}else for(var o in t)o.split(/\s+/).forEach(function(e){this.addEventListener(e,t[o])},this)},once:function(t){2==arguments.length&&(t={},t[arguments[0]]=arguments[1]);for(var e in t)e.split(/\s+/).forEach(function(n){var r=this;this.addEventListener(n,function s(){return r.removeEventListener(n,s),t[e].apply(this,arguments)})},this)},delegate:function(t){3===arguments.length?(t={},t[arguments[0]]={},t[arguments[0]][arguments[1]]=arguments[2]):2===arguments.length&&(t={},t[arguments[0]]=arguments[1]);var e=this;for(var n in t)!function(t,n){e.addEventListener(t,function(t){for(var e in n)t.target.matches(e)&&n[e].call(this,t)})}(n,t[n])},contents:function(t){(t||0===t)&&(Array.isArray(t)?t:[t]).forEach(function(t){var n=e.type(t);/^(string|number)$/.test(n)?t=document.createTextNode(t+""):"object"===n&&(t=e.create(t)),t instanceof Node&&this.appendChild(t)},this)},inside:function(t){t.appendChild(this)},before:function(t){t.parentNode.insertBefore(this,t)},after:function(t){t.parentNode.insertBefore(this,t.nextSibling)},start:function(t){t.insertBefore(this,t.firstChild)},around:function(t){t.parentNode&&e.before(this,t),(/^template$/i.test(this.nodeName)?this.content||this:this).appendChild(t)}},e.Array=function(t){this.subject=t},e.Array.prototype={all:function(t){var e=$$(arguments).slice(1);return this[t].apply(this,e)}},e.add=function(t,n,r){n=e.extend({$:!0,element:!0,array:!0},n),"string"===e.type(arguments[0])&&(t={},t[arguments[0]]=arguments[1]);for(var s in t)!function(t,s){"function"==e.type(s)&&(!n.element||t in e.Element.prototype&&r||(e.Element.prototype[t]=function(){return this.subject&&e.defined(s.apply(this.subject,arguments),this.subject)}),!n.array||t in e.Array.prototype&&r||(e.Array.prototype[t]=function(){var t=arguments;return this.subject.map(function(n){return n&&e.defined(s.apply(n,t),n)})}),n.$&&(e.sources[t]=e[t]=s,(n.array||n.element)&&(e[t]=function(){var r=[].slice.apply(arguments),s=r.shift(),i=n.array&&Array.isArray(s)?"Array":"Element";return e[i].prototype[t].apply({subject:s},r)})))}(s,t[s])},e.add(e.Array.prototype,{element:!1}),e.add(e.Element.prototype),e.add(e.setProps),e.add(e.classProps,{element:!1,array:!1});var r=document.createElement("_");e.add(e.extend({},HTMLElement.prototype,function(t){return"function"===e.type(r[t])}),null,!0)}(); \ No newline at end of file +!function(){"use strict";function t(t,n,r){for(var s in n){if(r){var i=e.type(r);if("own"===r&&!n.hasOwnProperty(s)||"array"===i&&-1===r.indexOf(s)||"regexp"===i&&!r.test(s)||"function"===i&&!r.call(n,s))continue}var o=Object.getOwnPropertyDescriptor(n,s);!o||o.writable&&o.configurable&&o.enumerable&&!o.get&&!o.set?t[s]=n[s]:(delete t[s],Object.defineProperty(t,s,o))}return t}var e=self.Bliss=t(function(t,n){return"string"===e.type(t)?(n||document).querySelector(t):t||null},self.Bliss);t(e,{extend:t,property:e.property||"_",sources:{},$:function(t,e){return t instanceof Node||t instanceof Window?[t]:Array.prototype.slice.call("string"==typeof t?(e||document).querySelectorAll(t):t||[])},type:function(t){if(null===t)return"null";if(void 0===t)return"undefined";var e=(Object.prototype.toString.call(t).match(/^\[object\s+(.*?)\]$/)[1]||"").toLowerCase();return"number"==e&&isNaN(t)?"nan":e},defined:function(){for(var t=0;t=3)Object.defineProperty(t,n,{get:function(){return delete this[n],this[n]=r.call(this)},configurable:!0,enumerable:!0});else if(2===arguments.length)for(var s in n)e.lazy(t,s,n[s]);return t},live:function(t,n,r){if(arguments.length>=3)Object.defineProperty(t,n,{get:function(){var t=this["_"+n],e=r.get&&r.get.call(this,t);return void 0!==e?e:t},set:function(t){var e=this["_"+n],s=r.set&&r.set.call(this,t,e);this["_"+n]=void 0!==s?s:t},configurable:r.configurable,enumerable:r.enumerable});else if(2===arguments.length)for(var s in n)e.live(t,s,n[s]);return t}},include:function(){var t=arguments[arguments.length-1],n=2===arguments.length?arguments[0]:!1,r=document.createElement("script");return n?Promise.resolve():new Promise(function(n,s){e.set(r,{async:!0,onload:function(){n(),e.remove(r)},onerror:function(){s()},src:t,inside:document.head})})},fetch:function(t,n){if(!t)throw new TypeError("URL parameter is mandatory and cannot be "+t);t=new URL(t,location),n=n||{},n.data=n.data||"",n.method=n.method||"GET",n.headers=n.headers||{};var r=new XMLHttpRequest;"string"!==e.type(n.data)&&(n.data=Object.keys(n.data).map(function(t){return t+"="+encodeURIComponent(n.data[t])}).join("&")),"GET"===n.method&&n.data&&(t.search+=n.data),document.body.setAttribute("data-loading",t),r.open(n.method,t,!n.sync);for(var s in n)if(s in r)try{r[s]=n[s]}catch(i){self.console&&console.error(i)}"GET"===n.method||n.headers["Content-type"]||n.headers["Content-Type"]||r.setRequestHeader("Content-type","application/x-www-form-urlencoded");for(var o in n.headers)r.setRequestHeader(o,n.headers[o]);return new Promise(function(t,e){r.onload=function(){document.body.removeAttribute("data-loading"),0===r.status||r.status>=200&&r.status<300||304===r.status?t(r):e(Error(r.statusText))},r.onerror=function(){document.body.removeAttribute("data-loading"),e(Error("Network Error"))},r.send("GET"===n.method?null:n.data)})}});var n=e.property;e.Element=function(t){this.subject=t,this.data={},this.bliss={}},e.Element.prototype={set:function(t){"string"===e.type(arguments[0])&&(t={},t[arguments[0]]=arguments[1]);for(var n in t)n in e.setProps?e.setProps[n].call(this,t[n]):n in this?this[n]=t[n]:(this.setAttribute||console.log(this),this.setAttribute(n,t[n]))},transition:function(t,n){return n=+n||400,new Promise(function(r,s){if("transition"in this.style){var i=e.extend({},this.style,/^transition(Duration|Property)$/);e.style(this,{transitionDuration:(n||400)+"ms",transitionProperty:Object.keys(t).join(", ")}),e.once(this,"transitionend",function(){clearTimeout(o),e.style(this,i),r(this)});var o=setTimeout(r,n+50,this);e.style(this,t)}else e.style(this,t),r(this)}.bind(this))},fire:function(t,n){var r=document.createEvent("HTMLEvents");r.initEvent(t,!0,!0),this.dispatchEvent(e.extend(r,n))}},e.setProps={style:function(t){e.extend(this.style,t)},attributes:function(t){for(var e in t)this.setAttribute(e,t[e])},properties:function(t){e.extend(this,t)},events:function(t){if(t&&t.addEventListener){var e=this;if(t[n]&&t[n].bliss){var r=t[n].bliss.listeners;for(var s in r)r[s].forEach(function(t){e.addEventListener(s,t.callback,t.capture)})}for(var i in t)0===i.indexOf("on")&&(this[i]=t[i])}else for(var o in t)o.split(/\s+/).forEach(function(e){this.addEventListener(e,t[o])},this)},once:function(t){2==arguments.length&&(t={},t[arguments[0]]=arguments[1]);var n=this;e.each(t,function(t,e){t=t.split(/\s+/);var r=function(){return t.forEach(function(t){n.removeEventListener(t,r)}),e.apply(n,arguments)};t.forEach(function(t){n.addEventListener(t,r)})})},delegate:function(t){3===arguments.length?(t={},t[arguments[0]]={},t[arguments[0]][arguments[1]]=arguments[2]):2===arguments.length&&(t={},t[arguments[0]]=arguments[1]);var n=this;e.each(t,function(t,e){n.addEventListener(t,function(t){for(var n in e)t.target.matches(n)&&e[n].call(this,t)})})},contents:function(t){(t||0===t)&&(Array.isArray(t)?t:[t]).forEach(function(t){var n=e.type(t);/^(string|number)$/.test(n)?t=document.createTextNode(t+""):"object"===n&&(t=e.create(t)),t instanceof Node&&this.appendChild(t)},this)},inside:function(t){t.appendChild(this)},before:function(t){t.parentNode.insertBefore(this,t)},after:function(t){t.parentNode.insertBefore(this,t.nextSibling)},start:function(t){t.insertBefore(this,t.firstChild)},around:function(t){t.parentNode&&e.before(this,t),(/^template$/i.test(this.nodeName)?this.content||this:this).appendChild(t)}},e.Array=function(t){this.subject=t},e.Array.prototype={all:function(t){var e=$$(arguments).slice(1);return this[t].apply(this,e)}},e.add=function(t,n,r){n=e.extend({$:!0,element:!0,array:!0},n),"string"===e.type(arguments[0])&&(t={},t[arguments[0]]=arguments[1]),e.each(t,function(t,s){"function"==e.type(s)&&(!n.element||t in e.Element.prototype&&r||(e.Element.prototype[t]=function(){return this.subject&&e.defined(s.apply(this.subject,arguments),this.subject)}),!n.array||t in e.Array.prototype&&r||(e.Array.prototype[t]=function(){var t=arguments;return this.subject.map(function(n){return n&&e.defined(s.apply(n,t),n)})}),n.$&&(e.sources[t]=e[t]=s,(n.array||n.element)&&(e[t]=function(){var r=[].slice.apply(arguments),s=r.shift(),i=n.array&&Array.isArray(s)?"Array":"Element";return e[i].prototype[t].apply({subject:s},r)})))})},e.add(e.Array.prototype,{element:!1}),e.add(e.Element.prototype),e.add(e.setProps),e.add(e.classProps,{element:!1,array:!1});var r=document.createElement("_");e.add(e.extend({},HTMLElement.prototype,function(t){return"function"===e.type(r[t])}),null,!0)}(); \ No newline at end of file diff --git a/docs.html b/docs.html index c4d1b63..179f912 100644 --- a/docs.html +++ b/docs.html @@ -829,6 +829,25 @@

fire

jQuery.fn.trigger +
+

once

+ +

Set event listeners that will be fired once per callback per element.

+ +
subject = $.once(subject, handlers)
+
subject = subject._.once(handlers)
+
subject = subject = subject._.set({once: handlers})
+ +
+
subject
+
The element(s) to set the event listeners on.
+ +
handlers
+
An object whose keys are the events and the values the listeners. You can include multiple event types by space-separating them.
+
+ + jQuery.fn.one +

ready

@@ -929,6 +948,29 @@

Class

+
+

each

+ +

Loop over properties of an object and map them onto another object.

+ +
var ret = $.each(obj, callback [, ret]);
+ +
+
obj
+
The object to loop over
+ +
callback
+
The function to be executed for every property. It will be called with obj as its context and the property and value as its arguments.
+ +
ret
+
The returned object. If not provided, a new object will be created. Provide the same object as obj to overwrite.
+
+ +
    +
  • Inherited properties will be included in the loop.
  • +
+
+

extend