Skip to content

Instantly share code, notes, and snippets.

@thisislawatts
Created May 8, 2014 08:29
Show Gist options
  • Save thisislawatts/8f7892a363911ca0b16a to your computer and use it in GitHub Desktop.
Save thisislawatts/8f7892a363911ca0b16a to your computer and use it in GitHub Desktop.
Resrc.js w/ Event emitter
/*!
* EventEmitter v4.2.7 - git.io/ee
* Oliver Caldwell
* MIT license
* @preserve
*/
(function(){"use strict";function t(){}function r(t,n){for(var e=t.length;e--;)if(t[e].listener===n)return e;return-1}function n(e){return function(){return this[e].apply(this,arguments)}}var e=t.prototype,i=this,s=i.EventEmitter;e.getListeners=function(n){var r,e,t=this._getEvents();if(n instanceof RegExp){r={};for(e in t)t.hasOwnProperty(e)&&n.test(e)&&(r[e]=t[e])}else r=t[n]||(t[n]=[]);return r},e.flattenListeners=function(t){var e,n=[];for(e=0;e<t.length;e+=1)n.push(t[e].listener);return n},e.getListenersAsObject=function(n){var e,t=this.getListeners(n);return t instanceof Array&&(e={},e[n]=t),e||t},e.addListener=function(i,e){var t,n=this.getListenersAsObject(i),s="object"==typeof e;for(t in n)n.hasOwnProperty(t)&&-1===r(n[t],e)&&n[t].push(s?e:{listener:e,once:!1});return this},e.on=n("addListener"),e.addOnceListener=function(e,t){return this.addListener(e,{listener:t,once:!0})},e.once=n("addOnceListener"),e.defineEvent=function(e){return this.getListeners(e),this},e.defineEvents=function(t){for(var e=0;e<t.length;e+=1)this.defineEvent(t[e]);return this},e.removeListener=function(i,s){var n,e,t=this.getListenersAsObject(i);for(e in t)t.hasOwnProperty(e)&&(n=r(t[e],s),-1!==n&&t[e].splice(n,1));return this},e.off=n("removeListener"),e.addListeners=function(e,t){return this.manipulateListeners(!1,e,t)},e.removeListeners=function(e,t){return this.manipulateListeners(!0,e,t)},e.manipulateListeners=function(r,t,i){var e,n,s=r?this.removeListener:this.addListener,o=r?this.removeListeners:this.addListeners;if("object"!=typeof t||t instanceof RegExp)for(e=i.length;e--;)s.call(this,t,i[e]);else for(e in t)t.hasOwnProperty(e)&&(n=t[e])&&("function"==typeof n?s.call(this,e,n):o.call(this,e,n));return this},e.removeEvent=function(e){var t,r=typeof e,n=this._getEvents();if("string"===r)delete n[e];else if(e instanceof RegExp)for(t in n)n.hasOwnProperty(t)&&e.test(t)&&delete n[t];else delete this._events;return this},e.removeAllListeners=n("removeEvent"),e.emitEvent=function(r,o){var e,i,t,s,n=this.getListenersAsObject(r);for(t in n)if(n.hasOwnProperty(t))for(i=n[t].length;i--;)e=n[t][i],e.once===!0&&this.removeListener(r,e.listener),s=e.listener.apply(this,o||[]),s===this._getOnceReturnValue()&&this.removeListener(r,e.listener);return this},e.trigger=n("emitEvent"),e.emit=function(e){var t=Array.prototype.slice.call(arguments,1);return this.emitEvent(e,t)},e.setOnceReturnValue=function(e){return this._onceReturnValue=e,this},e._getOnceReturnValue=function(){return this.hasOwnProperty("_onceReturnValue")?this._onceReturnValue:!0},e._getEvents=function(){return this._events||(this._events={})},t.noConflict=function(){return i.EventEmitter=s,t},"function"==typeof define&&define.amd?define(function(){return t}):"object"==typeof module&&module.exports?module.exports=t:this.EventEmitter=t}).call(this);
/**
* ReSRCit v0.6.2
* http://www.resrc.it
* Copyright (C) 2013 ReSRC Ltd.
*/
(function (e, t, n) {
"use strict";
var _this = e;
_this.ee = new EventEmitter();
function g(e, t) {
var n = {};
if (!t) return e;
for (var r in e) r in t ? n[r] = t[r] : n[r] = e[r];
return n
}
function y(e) {
return e == 1 ? "https://" : "http://"
}
function b(e, t, r) {
var i = [];
t == null && (t = n);
r == null && (r = "*");
var s = t.getElementsByTagName(r),
o = s.length,
u = new RegExp("(^|\\s)" + e + "(\\s|$)"),
a, f;
for (a = 0, f = 0; a < o; a++)
if (u.test(s[a].className)) {
i[f] = s[a];
f++
}
return i
}
function w(e, t) {
var n = new Image;
n.src = t;
n.onload = function () {
e.src = t;
}
}
function E(e, t) {
var n = new Image;
n.src = t;
n.onload = function () {
e.style.backgroundImage = "url(" + t + ")"
}
}
function S(e, r) {
var i;
e.currentStyle ? i = e.currentStyle.getAttribute("backgroundImage") : t.getComputedStyle && (i = n.defaultView.getComputedStyle(e, null).getPropertyValue(r));
return i
}
function x(e) {
var t = n.createElement("a");
t.href = e;
return t.hostname
}
function T(e) {
var t = function (e) {
var t = d,
n = [],
r;
while (r = t.exec(e)) n.push(r[1]);
return n
};
return t(e)
}
e.options = e.options || {};
var r = {
resrcOnLoad: !0,
resrcOnResize: !0,
resrcOnPinch: !1,
ssl: !1,
resrcClass: "resrc",
server: "app.resrc.it",
pixelRounding: 10
},
i = e.options,
s = g(r, i),
o = s.resrcOnLoad,
u = s.resrcOnResize,
a = s.resrcOnPinch,
f = s.server,
l = s.resrcClass,
c = y(s.ssl),
h = s.pixelRounding,
p = 100,
d = /\((.*?)\)/g,
v = 0,
m;
Array.prototype.indexOf || (Array.prototype.indexOf = function (e) {
for (var t = 0; t < this.length; t++)
if (this[t] === e) return t;
return -1
});
"filter" in Array.prototype || (Array.prototype.filter = function (e, t) {
var n = [],
r;
for (var i = 0, s = this.length; i < s; i++) i in this && e.call(t, r = this[i], i, this) && n.push(r);
return n
});
String.prototype.startsWith = function (e) {
return this.match("^" + e) == e
};
String.prototype.contains = function (e) {
return this.indexOf(e) >= 0
};
String.prototype.trimStart = function (e) {
var t = this;
while (t.startsWith(e)) t = t.substring(1);
return t
};
String.prototype.isNumber = function () {
return !isNaN(parseFloat(this)) && isFinite(this)
};
Array.prototype.getItenIndexPosition = function (e) {
for (var t = 0; t < this.length; t++)
if (e == "") {
if (this[t] == "") return t
} else {
var n = new RegExp(e, ["i"]),
r = n.exec(this[t]);
if (r != null) return t
}
return -1
};
Array.prototype.removeItems = function (e) {
for (var t = 0; t < this.length; t++)
if (e == "") {
if (this[t] == "") {
this.splice(t, 1);
t--
}
} else {
var n = new RegExp(e, ["i"]),
r = n.exec(this[t]);
if (r != null) {
this.splice(t, 1);
t--
}
}
return this
};
var N, C, k, L, A, O, M, _, D, P, H, B, j, F;
N = function (e) {
var t = e.getAttribute("data-resrc-gesture") || e.setAttribute("data-resrc-gesture", "true");
t || (e.addEventListener ? e.addEventListener("gestureend", O, !1) : e.attachEvent && e.attachEvent("gestureend", O))
}, C = function () {
o && k()
}, k = function () {
var e = b(l);
for (var t = 0; t < e.length; t++) L(e[t])
}, L = function (e, t) {
if (e !== null) {
t == null && (t = 1);
typeof e[0] == "undefined" && (e = [e]);
for (var n = 0; n < e.length; n++) {
var r = e[n];
if (typeof r == "undefined" || r == null) return;
var i = r.tagName.toLowerCase();
if (i == "img") {
var s = r.src;
if (s) w(r, A(r, s, t, function () {
a && N(e[n])
}));
else {
var o = r.getAttribute("data-src"),
src = A(r, o, t, function () {
a && N(r)
});
r.setAttribute("src", src )
_this.ee.emitEvent('resrc.img', [r] );
}
} else {
var u = S(r, "background-image").replace(/url\(('?"?)(.*?)\1\)/gi, "$2");
u != "none" && E(r, A(r, u, t, function () {}))
}
}
_this.ee.emitEvent('resrc.all', [e]);
}
}, A = function (e, t, n, r) {
function b(e) {
var t = /\((.*?)\)/g;
return t.test(e)
}
var i = M(e);
i *= n;
var s = t.replace(f, ""),
o, u, a;
if (s.contains("//")) {
var l = s.lastIndexOf("//");
o = s.substring(l + 2);
u = s.substring(0, l).toLowerCase();
u = u.replace("/http:", "").replace("http:", "").replace("/https:", "").replace("https:", "");
u = u.trimStart("/");
a = u.split("/");
a = a.removeItems("")
} else {
a = [];
o = s
}
var d = D(e),
v;
i < p ? v = "s=w" + i + ",pd" + d : v = "s=w" + Math.ceil(i / h) * h + ",pd" + d;
var m = a.getItenIndexPosition("s="),
g = a.getItenIndexPosition("o=.*");
m !== -1 ? a.splice(m, 1, v) : g !== -1 ? a.splice(g, 0, v) : a.push(v);
var y = a[a.getItenIndexPosition("o=.*")];
if (b(y)) {
var w = "o=" + T(y);
a.splice(g, 1, w);
a.join()
}
var E = c + f + "/" + a.join("/") + "/" + c + o;
e.setAttribute("data-resrc-width", i);
r(E);
return E
}, O = function () {
var e = Math.round(screen.width / t.innerWidth * 10) / 10;
w(event.target, A(event.target, event.target.src, e, function () {}))
}, M = function (e) {
e.alt = "";
if (e.parentNode == null) return H();
if (e.offsetWidth !== 0) return e.offsetWidth;
var t, n, r = {},
i = {
position: "absolute",
visibility: "hidden",
display: "block"
};
for (n in i) {
r[n] = e.style[n];
e.style[n] = i[n]
}
t = e.offsetWidth;
for (n in i) e.style[n] = r[n];
return t == 0 ? M(e.parentNode) : t
}, _ = function () {
return t.devicePixelRatio ? t.devicePixelRatio : 1
}, D = function (e) {
var t = e.getAttribute("data-resrc-dpi") || "",
n;
t != "" ? t.isNumber() ? n = parseFloat(t) : n = _() : n = _();
return n
}, F = function () {
t.addEventListener ? t.addEventListener("resize", P, !1) : t.attachEvent && t.attachEvent("onresize", P)
}, P = function () {
v !== H() && j()
}, H = function () {
return n.documentElement.clientWidth || n.body && n.body.clientWidth || 1024
}, B = function () {
k()
};
e.resrc = function (e, t) {
typeof e != "undefined" && L(e, t)
};
e.resrcAll = function () {
k()
};
j = function () {
t.clearTimeout(m);
m = t.setTimeout(B, 250)
};
u && F();
if (t.addEventListener) {
t.addEventListener("DOMContentLoaded", function () {
C();
t.removeEventListener("load", C, !1)
}, !1);
t.addEventListener("load", C, !1)
} else t.attachEvent && t.attachEvent("onload", C)
})(this.resrc = this.resrc || {}, this, document);
this.resrc.ee.addListener('resrc.img', function(e) {
console.log("Hello!", e)
});
this.resrc.ee.addListener('resrc.all', function(e) {
console.log("Everything processed!", e)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment