Skip to content

Commit

Permalink
Fixed #86, started docs for #87, added new method $.each(), with docs…
Browse files Browse the repository at this point in the history
… (needs examples)
  • Loading branch information
LeaVerou committed Dec 12, 2015
1 parent 7ccc59c commit b921eee
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 64 deletions.
14 changes: 8 additions & 6 deletions bliss._.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ 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) {
var listeners = this[_].bliss.listeners = this[_].bliss.listeners || {};

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});
}
}
Expand All @@ -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);
Expand Down
75 changes: 44 additions & 31 deletions bliss.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -590,9 +603,7 @@ $.add = function (methods, on, noOverwrite) {
}
}
}

})(method, methods[method]);
}
});
};

$.add($.Array.prototype, {element: false});
Expand Down Expand Up @@ -668,17 +679,18 @@ 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) {
var listeners = this[_].bliss.listeners = this[_].bliss.listeners || {};

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});
}
}
Expand All @@ -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);
Expand Down
Loading

0 comments on commit b921eee

Please sign in to comment.