2016-02-09 20:07:01 -05:00
|
|
|
var AnimeClient = (function(w) {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// ! Base
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function matches(elm, selector) {
|
|
|
|
let matches = (elm.document || elm.ownerDocument).querySelectorAll(selector),
|
|
|
|
i = matches.length;
|
|
|
|
while (--i >= 0 && matches.item(i) !== elm);
|
|
|
|
return i > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const _ = {
|
|
|
|
/**
|
|
|
|
* Placeholder function
|
|
|
|
*/
|
|
|
|
noop: () => {},
|
|
|
|
/**
|
|
|
|
* DOM selector
|
|
|
|
*
|
|
|
|
* @param {string} selector - The dom selector string
|
|
|
|
* @param {object} context
|
|
|
|
* @return {array} - array of dom elements
|
|
|
|
*/
|
|
|
|
$(selector, context) {
|
2017-02-16 11:47:54 -05:00
|
|
|
if (typeof selector !== 'string') {
|
2016-02-09 20:07:01 -05:00
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
context = (context != null && context.nodeType === 1)
|
|
|
|
? context
|
|
|
|
: document;
|
|
|
|
|
|
|
|
let elements = [];
|
|
|
|
if (selector.match(/^#([\w]+$)/)) {
|
|
|
|
elements.push(document.getElementById(selector.split('#')[1]));
|
|
|
|
} else {
|
2016-02-16 12:07:01 -05:00
|
|
|
elements = [].slice.apply(context.querySelectorAll(selector));
|
2016-02-09 20:07:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return elements;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Scroll to the top of the Page
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
scrollToTop() {
|
|
|
|
w.scroll(0,0);
|
|
|
|
},
|
2016-02-16 12:07:01 -05:00
|
|
|
/**
|
|
|
|
* Hide the selected element
|
|
|
|
*
|
|
|
|
* @param {string|Element} sel - the selector of the element to hide
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
hide(sel) {
|
|
|
|
sel.setAttribute('hidden', 'hidden');
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* UnHide the selected element
|
|
|
|
*
|
|
|
|
* @param {string|Element} sel - the selector of the element to hide
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
show(sel) {
|
|
|
|
sel.removeAttribute('hidden');
|
|
|
|
},
|
2016-02-09 20:07:01 -05:00
|
|
|
/**
|
|
|
|
* Display a message box
|
|
|
|
*
|
|
|
|
* @param {String} type - message type: info, error, success
|
|
|
|
* @param {String} message - the message itself
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
showMessage(type, message) {
|
2016-02-10 12:25:13 -05:00
|
|
|
let template =
|
2017-02-16 11:47:54 -05:00
|
|
|
`<div class='message ${type}'>
|
|
|
|
<span class='icon'></span>
|
2016-02-09 20:07:01 -05:00
|
|
|
${message}
|
2017-02-16 11:47:54 -05:00
|
|
|
<span class='close'></span>
|
2016-02-09 20:07:01 -05:00
|
|
|
</div>`;
|
|
|
|
|
2016-02-10 17:50:07 -05:00
|
|
|
let sel = AnimeClient.$('.message');
|
|
|
|
if (sel[0] !== undefined) {
|
|
|
|
sel[0].remove();
|
2016-02-09 20:07:01 -05:00
|
|
|
}
|
2016-02-10 17:50:07 -05:00
|
|
|
|
|
|
|
_.$('header')[0].insertAdjacentHTML('beforeend', template);
|
2016-02-09 20:07:01 -05:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Finds the closest parent element matching the passed selector
|
|
|
|
*
|
|
|
|
* @param {DOMElement} current - the current DOMElement
|
|
|
|
* @param {string} parentSelector - selector for the parent element
|
|
|
|
* @return {DOMElement|null} - the parent element
|
|
|
|
*/
|
|
|
|
closestParent(current, parentSelector) {
|
|
|
|
if (Element.prototype.closest !== undefined) {
|
|
|
|
return current.closest(parentSelector);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (current !== document.documentElement) {
|
|
|
|
if (matches(current, parentSelector)) {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = current.parentElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Generate a full url from a relative path
|
|
|
|
*
|
|
|
|
* @param {String} path - url path
|
|
|
|
* @return {String} - full url
|
|
|
|
*/
|
|
|
|
url(path) {
|
|
|
|
let uri = `//${document.location.host}`;
|
|
|
|
uri += (path.charAt(0) === '/') ? path : `/${path}`;
|
|
|
|
|
|
|
|
return uri;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Throttle execution of a function
|
|
|
|
*
|
|
|
|
* @see https://remysharp.com/2010/07/21/throttling-function-calls
|
|
|
|
* @see https://jsfiddle.net/jonathansampson/m7G64/
|
|
|
|
* @param {Number} interval - the minimum throttle time in ms
|
|
|
|
* @param {Function} fn - the function to throttle
|
|
|
|
* @param {Object} scope - the 'this' object for the function
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
throttle(interval, fn, scope) {
|
|
|
|
var wait = false;
|
|
|
|
return function () {
|
|
|
|
var context = scope || this;
|
|
|
|
var args = arguments;
|
|
|
|
|
|
|
|
if ( ! wait) {
|
|
|
|
fn.apply(context, args);
|
|
|
|
wait = true;
|
|
|
|
setTimeout(function() {
|
|
|
|
wait = false;
|
|
|
|
}, interval);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// ! Events
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function addEvent(sel, event, listener) {
|
|
|
|
// Recurse!
|
|
|
|
if (! event.match(/^([\w\-]+)$/)) {
|
|
|
|
event.split(' ').forEach((evt) => {
|
|
|
|
addEvent(sel, evt, listener);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sel.addEventListener(event, listener, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function delegateEvent(sel, target, event, listener) {
|
|
|
|
// Attach the listener to the parent
|
|
|
|
addEvent(sel, event, (e) => {
|
|
|
|
// Get live version of the target selector
|
|
|
|
_.$(target, sel).forEach((element) => {
|
|
|
|
if(e.target == element) {
|
|
|
|
listener.call(element, e);
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-16 12:07:01 -05:00
|
|
|
/**
|
|
|
|
* Add an event listener
|
|
|
|
*
|
|
|
|
* @param {string|element} sel - the parent selector to bind to
|
|
|
|
* @param {string} event - event name(s) to bind
|
|
|
|
* @param {string|element} [target] - the element to directly bind the event to
|
|
|
|
* @param {function} listener - event listener callback
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-02-09 20:07:01 -05:00
|
|
|
_.on = function (sel, event, target, listener) {
|
|
|
|
if (arguments.length === 3) {
|
|
|
|
listener = target;
|
|
|
|
_.$(sel).forEach((el) => {
|
|
|
|
addEvent(el, event, listener);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_.$(sel).forEach((el) => {
|
|
|
|
delegateEvent(el, target, event, listener);
|
|
|
|
});
|
|
|
|
}
|
2017-02-15 14:08:15 -05:00
|
|
|
};
|
2016-02-09 20:07:01 -05:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// ! Ajax
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Url encoding for non-get requests
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* @returns {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function ajaxSerialize(data) {
|
|
|
|
let pairs = [];
|
|
|
|
|
|
|
|
Object.keys(data).forEach((name) => {
|
|
|
|
let value = data[name].toString();
|
|
|
|
|
|
|
|
name = encodeURIComponent(name);
|
|
|
|
value = encodeURIComponent(value);
|
|
|
|
|
|
|
|
pairs.push(`${name}=${value}`);
|
|
|
|
});
|
|
|
|
|
2017-02-16 11:47:54 -05:00
|
|
|
return pairs.join('&');
|
2017-02-16 13:22:26 -05:00
|
|
|
}
|
2016-02-09 20:07:01 -05:00
|
|
|
|
2016-02-16 12:07:01 -05:00
|
|
|
/**
|
|
|
|
* Make an ajax request
|
|
|
|
*
|
|
|
|
* Config:{
|
|
|
|
* data: // data to send with the request
|
|
|
|
* type: // http verb of the request, defaults to GET
|
|
|
|
* success: // success callback
|
2017-01-06 21:39:01 -05:00
|
|
|
* error: // error callback
|
2016-02-16 12:07:01 -05:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @param {string} url - the url to request
|
|
|
|
* @param {Object} config - the configuration object
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-02-09 20:07:01 -05:00
|
|
|
_.ajax = function(url, config) {
|
|
|
|
// Set some sane defaults
|
|
|
|
config = config || {};
|
|
|
|
config.data = config.data || {};
|
|
|
|
config.type = config.type || 'GET';
|
|
|
|
config.dataType = config.dataType || '';
|
|
|
|
config.success = config.success || _.noop;
|
2017-01-06 21:39:01 -05:00
|
|
|
config.mimeType = config.mimeType || 'application/x-www-form-urlencoded';
|
2016-02-09 20:07:01 -05:00
|
|
|
config.error = config.error || _.noop;
|
|
|
|
|
|
|
|
let request = new XMLHttpRequest();
|
|
|
|
let method = String(config.type).toUpperCase();
|
|
|
|
|
2017-02-16 11:47:54 -05:00
|
|
|
if (method === 'GET') {
|
2016-02-09 20:07:01 -05:00
|
|
|
url += (url.match(/\?/))
|
|
|
|
? ajaxSerialize(config.data)
|
|
|
|
: `?${ajaxSerialize(config.data)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
request.open(method, url);
|
|
|
|
|
|
|
|
request.onreadystatechange = () => {
|
|
|
|
if (request.readyState === 4) {
|
|
|
|
let responseText = '';
|
|
|
|
|
2017-02-16 11:47:54 -05:00
|
|
|
if (request.responseType === 'json') {
|
2016-02-09 20:07:01 -05:00
|
|
|
responseText = JSON.parse(request.responseText);
|
|
|
|
} else {
|
|
|
|
responseText = request.responseText;
|
|
|
|
}
|
|
|
|
|
2018-08-09 11:31:15 -04:00
|
|
|
if (request.status > 299) {
|
2016-02-10 12:25:13 -05:00
|
|
|
config.error.call(null, request.status, responseText, request.response);
|
2016-02-09 20:07:01 -05:00
|
|
|
} else {
|
|
|
|
config.success.call(null, responseText, request.status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-06 21:39:01 -05:00
|
|
|
if (config.dataType === 'json') {
|
|
|
|
config.data = JSON.stringify(config.data);
|
|
|
|
config.mimeType = 'application/json';
|
|
|
|
} else {
|
|
|
|
config.data = ajaxSerialize(config.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
request.setRequestHeader('Content-Type', config.mimeType);
|
|
|
|
|
2016-02-09 20:07:01 -05:00
|
|
|
switch (method) {
|
2017-02-16 11:47:54 -05:00
|
|
|
case 'GET':
|
2016-02-09 20:07:01 -05:00
|
|
|
request.send(null);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-01-06 21:39:01 -05:00
|
|
|
request.send(config.data);
|
2016-02-09 20:07:01 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_.get = function(url, data, callback) {
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
callback = data;
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.ajax(url, {
|
2017-02-16 13:22:26 -05:00
|
|
|
data,
|
2016-02-09 20:07:01 -05:00
|
|
|
success: callback
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// Export
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return _;
|
|
|
|
})(window);
|