match
Version:
Simplest way to create match memory games.
259 lines (257 loc) • 16.8 kB
JavaScript
"use strict";
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
!function (e) {
if ("object" == (typeof exports === "undefined" ? "undefined" : _typeof(exports)) && "undefined" != typeof module) module.exports = e();else if ("function" == typeof define && define.amd) define([], e);else {
var t;t = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof self ? self : this, t.Match = e();
}
}(function () {
return function e(t, n, i) {
function r(o, a) {
if (!n[o]) {
if (!t[o]) {
var u = "function" == typeof require && require;if (!a && u) return u(o, !0);if (s) return s(o, !0);var c = new Error("Cannot find module '" + o + "'");throw c.code = "MODULE_NOT_FOUND", c;
}var h = n[o] = { exports: {} };t[o][0].call(h.exports, function (e) {
var n = t[o][1][e];return r(n ? n : e);
}, h, h.exports, e, t, n, i);
}return n[o].exports;
}for (var s = "function" == typeof require && require, o = 0; o < i.length; o++) {
r(i[o]);
}return r;
}({ 1: [function (e, t) {
function n() {
this._events = this._events || {}, this._maxListeners = this._maxListeners || void 0;
}function i(e) {
return "function" == typeof e;
}function r(e) {
return "number" == typeof e;
}function s(e) {
return "object" == (typeof e === "undefined" ? "undefined" : _typeof(e)) && null !== e;
}function o(e) {
return void 0 === e;
}t.exports = n, n.EventEmitter = n, n.prototype._events = void 0, n.prototype._maxListeners = void 0, n.defaultMaxListeners = 10, n.prototype.setMaxListeners = function (e) {
if (!r(e) || 0 > e || isNaN(e)) throw TypeError("n must be a positive number");return this._maxListeners = e, this;
}, n.prototype.emit = function (e) {
var t, n, r, a, u, c;if (this._events || (this._events = {}), "error" === e && (!this._events.error || s(this._events.error) && !this._events.error.length)) {
if (t = arguments[1], t instanceof Error) throw t;throw TypeError('Uncaught, unspecified "error" event.');
}if (n = this._events[e], o(n)) return !1;if (i(n)) switch (arguments.length) {case 1:
n.call(this);break;case 2:
n.call(this, arguments[1]);break;case 3:
n.call(this, arguments[1], arguments[2]);break;default:
for (r = arguments.length, a = new Array(r - 1), u = 1; r > u; u++) {
a[u - 1] = arguments[u];
}n.apply(this, a);} else if (s(n)) {
for (r = arguments.length, a = new Array(r - 1), u = 1; r > u; u++) {
a[u - 1] = arguments[u];
}for (c = n.slice(), r = c.length, u = 0; r > u; u++) {
c[u].apply(this, a);
}
}return !0;
}, n.prototype.addListener = function (e, t) {
var r;if (!i(t)) throw TypeError("listener must be a function");if (this._events || (this._events = {}), this._events.newListener && this.emit("newListener", e, i(t.listener) ? t.listener : t), this._events[e] ? s(this._events[e]) ? this._events[e].push(t) : this._events[e] = [this._events[e], t] : this._events[e] = t, s(this._events[e]) && !this._events[e].warned) {
var r;r = o(this._maxListeners) ? n.defaultMaxListeners : this._maxListeners, r && r > 0 && this._events[e].length > r && (this._events[e].warned = !0, console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.", this._events[e].length), "function" == typeof console.trace && console.trace());
}return this;
}, n.prototype.on = n.prototype.addListener, n.prototype.once = function (e, t) {
function n() {
this.removeListener(e, n), r || (r = !0, t.apply(this, arguments));
}if (!i(t)) throw TypeError("listener must be a function");var r = !1;return n.listener = t, this.on(e, n), this;
}, n.prototype.removeListener = function (e, t) {
var n, r, o, a;if (!i(t)) throw TypeError("listener must be a function");if (!this._events || !this._events[e]) return this;if (n = this._events[e], o = n.length, r = -1, n === t || i(n.listener) && n.listener === t) delete this._events[e], this._events.removeListener && this.emit("removeListener", e, t);else if (s(n)) {
for (a = o; a-- > 0;) {
if (n[a] === t || n[a].listener && n[a].listener === t) {
r = a;break;
}
}if (0 > r) return this;1 === n.length ? (n.length = 0, delete this._events[e]) : n.splice(r, 1), this._events.removeListener && this.emit("removeListener", e, t);
}return this;
}, n.prototype.removeAllListeners = function (e) {
var t, n;if (!this._events) return this;if (!this._events.removeListener) return 0 === arguments.length ? this._events = {} : this._events[e] && delete this._events[e], this;if (0 === arguments.length) {
for (t in this._events) {
"removeListener" !== t && this.removeAllListeners(t);
}return this.removeAllListeners("removeListener"), this._events = {}, this;
}if (n = this._events[e], i(n)) this.removeListener(e, n);else for (; n.length;) {
this.removeListener(e, n[n.length - 1]);
}return delete this._events[e], this;
}, n.prototype.listeners = function (e) {
var t;return t = this._events && this._events[e] ? i(this._events[e]) ? [this._events[e]] : this._events[e].slice() : [];
}, n.listenerCount = function (e, t) {
var n;return n = e._events && e._events[t] ? i(e._events[t]) ? 1 : e._events[t].length : 0;
};
}, {}], 2: [function (e, t) {
function n() {
c = !1, o.length ? u = o.concat(u) : h = -1, u.length && i();
}function i() {
if (!c) {
var e = setTimeout(n);c = !0;for (var t = u.length; t;) {
for (o = u, u = []; ++h < t;) {
o[h].run();
}h = -1, t = u.length;
}o = null, c = !1, clearTimeout(e);
}
}function r(e, t) {
this.fun = e, this.array = t;
}function s() {}var o,
a = t.exports = {},
u = [],
c = !1,
h = -1;a.nextTick = function (e) {
var t = new Array(arguments.length - 1);if (arguments.length > 1) for (var n = 1; n < arguments.length; n++) {
t[n - 1] = arguments[n];
}u.push(new r(e, t)), 1 !== u.length || c || setTimeout(i, 0);
}, r.prototype.run = function () {
this.fun.apply(null, this.array);
}, a.title = "browser", a.browser = !0, a.env = {}, a.argv = [], a.version = "", a.versions = {}, a.on = s, a.addListener = s, a.once = s, a.off = s, a.removeListener = s, a.removeAllListeners = s, a.emit = s, a.binding = function () {
throw new Error("process.binding is not supported");
}, a.cwd = function () {
return "/";
}, a.chdir = function () {
throw new Error("process.chdir is not supported");
}, a.umask = function () {
return 0;
};
}, {}], 3: [function (e, t) {
function n(e, t, n) {
if (this.ev = new i(), this.data = t.data || n, this.found = {}, this.timestamps = [], this.flippedPairs = 0, this.active = [], this.options = u.deepMerge(t, { autoremove: !0, size: { x: 4, y: 4 }, classes: { active: "active" }, step: { x: 43, y: 43 } }), this.blocks_count = this.options.size.x * this.options.size.y, this.count = this.blocks_count / 2, this.blocks_count % 2) throw new Error("The number of blocks should be even.");if (this.ui = { container: s(e)[0], items: [], template: t.templateElm ? s(t.templateElm)[0].outerHTML : t.template }, !Array.isArray(this.data)) throw new Error("Data should be an array.");
}var i = e("events").EventEmitter,
r = e("shuffle-array"),
s = e("elm-select"),
o = e("barbe"),
a = e("iterate-object"),
u = e("ul");n.prototype.check = function (e, t) {
var n = e.getAttribute("data-pattern"),
i = t.getAttribute("data-pattern");return n === i ? (e._found = !0, t._found = !0, this.found[n] = [e, t], !0) : !1;
}, n.prototype.on = function () {
return this.ev.on.apply(this.ev, arguments), this;
}, n.prototype.emit = function () {
return this.ev.emit.apply(this.ev, arguments), this;
}, n.prototype.shuffle = function (e) {
return r(e || this.data);
}, n.prototype.clear = function () {
return this.ui.container.innerHTML = "", this;
}, n.prototype.deactivate = function (e) {
return this.active[this.active.indexOf(e)] = null, e._found ? this : (e.classList.remove(this.options.classes.active), this.emit("deactivate", e));
}, n.prototype.activate = function (e) {
if (e.classList.add(this.options.classes.active), this.active[0]) {
if (this.active[0] && this.active[1]) this.deactivate(this.active[0]), this.deactivate(this.active[1]), this.activate(e);else {
if (this.active[0] === e) return this;if (this.active[1] = e, this.emit("pair-flip", this.active[0], this.active[1]), this.check.apply(this, this.active)) return this.emit("activate", e), this.emit("success", this.active[0], this.active[1]), this.options.autoremove && (this.active[0].remove(), this.active[1].remove(), this.deactivate(this.active[0]), this.deactivate(this.active[1])), Object.keys(this.found).length === this.count && this.win(), this;
}
} else this.active[0] = e;return this.emit("activate", e);
}, n.prototype.clicked = function (e) {
var t = this;return e.addEventListener("click", function () {
t.activate(this);
}), this;
}, n.prototype.addHandlers = function () {
var e = this;return s.call(this, this.ui.container.children, this.clicked), e.on("pair-flip", function () {
++e.flippedPairs;
}), e;
}, n.prototype.win = function () {
return clearInterval(this.timer), this.timestamps[1] = new Date(), this.emit("win", this.timestamps[1] - this.timestamps[2]), this;
}, n.prototype.render = function (e) {
var t = this,
n = { x: 0, y: 0, cX: 0, cY: 0 };return e && t.clear(), t.shuffle(), t.items = t.data.slice(0, this.blocks_count / 2), a(t.items, function (e, n) {
for (var i, r, s, a = 0; 2 > a; ++a) {
i = document.createElement("div"), i.setAttribute("data-pattern", n), r = document.createElement("div"), r.classList.add("front"), s = document.createElement("div"), s.classList.add("back"), i.appendChild(r), i.appendChild(s), r.innerHTML = o(t.ui.template, e), t.ui.items.push({ element: i, data: e, duplicate: !!a }), t.emit("render", i, e, a);
}
}), t.shuffle(t.ui.items), t.ui.container.innerHTML = t.ui.items.map(function (e) {
return n.cY === t.options.size.y ? "" : n.cX === t.options.size.x && (n.y += t.options.step.y, n.x = 0, n.cX = 0, ++n.cY, n.cY === t.options.size.y) ? "" : (e.element.style.top = n.y + "px", e.element.style.left = n.x + "px", n.x += t.options.step.x, ++n.cX, e.element.outerHTML);
}).join(""), t.addHandlers();
}, n.prototype.start = function () {
var e = this;return e.render(!0), e.timestamps[0] = new Date(), e.timer = setInterval(function () {
e.emit("time", new Date() - e.timestamps[0]);
}, 1e3), e;
}, t.exports = n;
}, { barbe: 4, "elm-select": 6, events: 1, "iterate-object": 8, "shuffle-array": 9, ul: 10 }], 4: [function (e, t) {
function n(e, t, n) {
function r(n, i) {
Object.keys(n).forEach(function (o) {
return s = n[o], i.push(o), "object" == (typeof s === "undefined" ? "undefined" : _typeof(s)) ? r(s, i) : (e = e.replace(new RegExp(t[0] + i.join(".") + t[1], "g"), s), void i.pop());
});
}if (Array.isArray(t) || (n = t, t = ["{", "}"]), !n || n.constructor !== Object) return e;t = t.map(i);var s = null;return r(n, []), e;
}var i = e("regex-escape");t.exports = n;
}, { "regex-escape": 5 }], 5: [function (e, t) {
function n(e) {
return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}n.proto = function () {
return RegExp.escape = n, n;
}, t.exports = n;
}, {}], 6: [function (e, t) {
function n(e, t, r, s) {
var o = 0,
a = null;if ("string" == typeof e && (s = s ? n(s) : document, e = s.querySelectorAll(e)), i(e, NodeList) || i(e, HTMLCollection) || (e = [e]), "function" == typeof t) for (Array.isArray(r) || (r = [r]); o < e.length; ++o) {
a = [e[o]].concat(r), t.apply(this, a);
}return e;
}var i = e("typpy");t.exports = n;
}, { typpy: 7 }], 7: [function (e, t) {
function n(e, t) {
return 2 === arguments.length ? n.is(e, t) : n.get(e, !0);
}n.is = function (e, t) {
return n.get(e, "string" == typeof t) === t;
}, n.get = function (e, t) {
return "string" == typeof e ? t ? "string" : String : null === e ? t ? "null" : null : void 0 === e ? t ? "undefined" : void 0 : e !== e ? t ? "nan" : 0 / 0 : t ? e.constructor.name.toLowerCase() : e.constructor;
}, t.exports = n;
}, {}], 8: [function (e, t) {
function n(e, t) {
var n = 0,
i = [];if (Array.isArray(e)) for (; n < e.length && t(e[n], n, e) !== !1; ++n) {} else for (i = Object.keys(e); n < i.length && t(e[i[n]], i[n], e) !== !1; ++n) {}
}t.exports = n;
}, {}], 9: [function (e, t) {
"use strict";
function n(e, t) {
if (!Array.isArray(e)) throw new Error("shuffle expect an array as parameter.");t = t || {};var n,
i,
r = e,
s = e.length,
o = t.rng || Math.random;for (t.copy === !0 && (r = e.slice()); s;) {
n = Math.floor(o() * s), s -= 1, i = r[s], r[s] = r[n], r[n] = i;
}return r;
}n.pick = function (e, t) {
if (!Array.isArray(e)) throw new Error("shuffle.pick() expect an array as parameter.");t = t || {};var n = t.rng || Math.random,
i = t.picks || 1;if ("number" == typeof i && 1 !== i) {
for (var r, s = e.length, o = e.slice(), a = []; i;) {
r = Math.floor(n() * s), a.push(o[r]), o.splice(r, 1), s -= 1, i -= 1;
}return a;
}return e[Math.floor(n() * e.length)];
}, t.exports = n;
}, {}], 10: [function (e, t) {
(function (n) {
function i() {}var r = e("typpy"),
s = e("deffy");i.prototype.merge = function (e, t) {
var n = {},
i = null;t = s(t, {}), e = s(e, {});for (i in t) {
n[i] = t[i];
}for (i in e) {
void 0 !== e[i] && (n[i] = e[i]);
}return n;
}, i.prototype.deepMerge = function () {
for (var e, t, n = {}, i = [].splice.call(arguments, 0); i.length > 0;) {
if (e = i.splice(-1)[0], "object" === r(e)) for (t in e) {
e.hasOwnProperty(t) && ("object" === r(e[t]) ? n[t] = this.deepMerge(e[t], n[t] || {}) : void 0 !== e[t] && (n[t] = e[t]));
}
}return n;
}, i.prototype.clone = function (e) {
if (!e) return e;var t,
n,
i = this,
r = [Number, String, Boolean];if (r.forEach(function (n) {
e instanceof n && (t = n(e));
}), "undefined" == typeof t) if (Array.isArray(e)) t = [], e.forEach(function (e, n) {
t[n] = i.clone(e);
});else if ("object" == (typeof e === "undefined" ? "undefined" : _typeof(e))) {
if (e.prototype) t = e;else if (e instanceof Date) t = new Date(e);else {
t = {};for (n in e) {
t[n] = i.clone(e[n]);
}
}
} else t = e;return t;
}, i.prototype.HOME_DIR = n.env["win32" == n.platform ? "USERPROFILE" : "HOME"], i.prototype.home = function () {
return this.HOME_DIR;
}, t.exports = new i();
}).call(this, e("_process"));
}, { _process: 2, deffy: 11, typpy: 12 }], 11: [function (e, t) {
function n(e, t, n) {
return "function" == typeof t ? t(e) : (n = "boolean" === i(n) ? { empty: n } : { empty: !1 }, n.empty ? e || t : i(e) === i(t) ? e : t);
}var i = e("typpy");t.exports = n;
}, { typpy: 12 }], 12: [function (e, t) {
function n(e) {
return "string" == typeof e ? "string" : null === e ? "null" : void 0 === e ? "undefined" : e.constructor.name.toLowerCase();
}t.exports = n;
}, {}] }, {}, [3])(3);
});