Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 79 additions & 49 deletions src/$.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,81 @@
/*globals Element:true, NodeList:true*/
$ = (function (document, $) {
var element = Element.prototype,
nodeList = NodeList.prototype,
forEach = 'forEach',
trigger = 'trigger',
each = [][forEach],
dummy = document.createElement();

nodeList[forEach] = each;

element.on = function (event, fn) {
this.addEventListener(event, fn, false);
return this;
};

nodeList.on = function (event, fn) {
each.call(this, function (el) {
el.on(event, fn);
});
return this;
};

element.trigger = function (type, data) {
var event = document.createEvent('HTMLEvents');
event.initEvent(type, true, true);
event.data = data || {};
event.eventName = type;
event.target = this;
this.dispatchEvent(event);
return this;
};

nodeList.trigger = function (event) {
each.call(this, function (el) {
el[trigger](event);
});
return this;
};

$ = function (s) {
var r = document.querySelectorAll(s || '☺'),
length = r.length;
return length == 1 ? r[0] : !length ? nodeList : r;
};

$.on = element.on.bind(dummy);
$.trigger = element[trigger].bind(dummy);

return $;
$ = (function(document, $) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you restore the 2 white space - otherwise I see a 100% file change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@remy FYI you can also view any commit without whitespace differences by appending ?w=1 to the URL. E.g. https://github.com/remy/min.js/pull/8/files?w=1

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I've been doing that - but integrating the change also means reversing out the white space changes - fun times :)

var element = Element.prototype,
nodeList = NodeList.prototype,
forEach = 'forEach',
trigger = 'trigger',
each = [][forEach],
dummy = document.createElement();

nodeList[forEach] = each;

element.on = function(event, fn) {
if (!this.handlers) {
this.handlers = []
}
this.addEventListener(event, fn, false);
this.handlers.push(fn);
return this;
};

element.off = function(event, fn) {
if (!fn) {
this.handlers[forEach](function(handler) {
this.removeEventListener(event, handler, false);
}.bind(this));
}else {
this.removeEventListener(event, fn, false);
}
return this;
};

nodeList.on = function(event, fn) {
each.call(this, function(el) {
el.on(event, fn);
this.handlers.push(fn);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicates the handlers list - as we're using el.on - which also pushes on to the handlers.

});
return this;
};

nodeList.off = function(event, fn) {
if(!fn) {
this.handlers[forEach](function(handler) {
this.off(event, handler, false);
}.bind(this));
}else {
each.call(this, function(el) {
el.off(event, fn);
});
}
return this;
};

element.trigger = function(type, data) {
var event = document.createEvent('HTMLEvents');
event.initEvent(type, true, true);
event.data = data || {};
event.eventName = type;
event.target = this;
this.dispatchEvent(event);
return this;
};

nodeList.trigger = function(event) {
each.call(this, function(el) {
el[trigger](event);
});
return this;
};

$ = function(s) {
var r = document.querySelectorAll(s || '☺'),
length = r.length;
return length == 1 ? r[0] : !length ? nodeList : r;
};

$.on = element.on.bind(dummy);
$.off = element.off.bind(dummy);
$.trigger = element[trigger].bind(dummy);

return $;
})(document);