TTS Framework - JavaScript assets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
tts_js/core/core.js

381 lines
14 KiB

/*
* Miq
@copyright 2019 Edwin Martin
@link https://github.com/edwinm/miq/blob/master/miq.js
@license MIT
*
* Miq, the micro jQuery library
*/
var tts = function (arg, doc) {
doc = doc && doc.first || doc || document;
if (typeof arg == 'function') { /* $(function() {...}) */
if (doc.readyState == 'loading') {
doc.addEventListener('DOMContentLoaded', arg);
} else {
arg();
}
} else {
var ret = Object.create(tts.fn), match; /* $([domObject]) or $(ttsObject) */
if (typeof arg == 'object') {
if ('length' in arg) {
ret.length = arg.length;
for (var o = 0; o < arg.length; o++) {
ret[o] = arg[o];
}
} else { /* $(domObject) */
ret[0] = arg;
ret.length = 1;
}
} else if (!arg) { /* $() */
ret[0] = doc.createDocumentFragment();
ret.length = 1;
} else if ((match = arg.match(/<(.+)>/))) { /* $('<div>') */
ret[0] = doc.createElement(match[1]);
ret.length = 1;
} else { /* $('div.widget') */
var els = doc.querySelectorAll(arg);
ret.length = els.length;
for (var w = 0; w < els.length; w++) {
ret[w] = els[w];
}
}
return ret;
}
};
tts.version = "1.0";
tts.performanceData = pref_data;
tts.pageLoadTime = function () {
return pref_data.loadEventEnd - pref_data.navigationStart;
};
tts.connectTime = function () {
return pref_data.responseEnd - pref_data.requestStart;
};
tts.renderTime = function () {
return pref_data.domComplete - pref_data.domLoading;
};
tts.isNotSet = function (check) {
return (check === null || typeof check === "undefined" || check.length === 0) ? true : false;
};
tts.getValue = function (input, defaults) {
return (tts.isNotSet(input) === true) ? defaults : input;
};
tts.matches = ['matches', 'webkitMatchesSelector', 'mozMatchesSelector', 'msMatchesSelector'].filter(function (sel) {
return sel in document.documentElement;
})[0];
tts.fn = Object.create(Array.prototype, {
each: {value: function (callback) {
if ( ! tts.isFunction(callback) ) {
/*throw new Error("Callback should be a function");*/
}
var $this = {};
for (var index = 0; index < this.length; index++) {
$this = this[index];
$this.callback = callback;
$this.callback(index);
}
return this;
}},
first: {get: function () {
return this[0];
}},
eq: {value: function (i) {
return tts(this[i || 0]);
}},
on: {value: function (evt, fn) {
if (!tts.isFunction(fn))
tts.logger('Callback should be a function', 'warn');
var hasit = this[0].addEventListener;
for (var i = 0; i < this.length; i++) {
if (hasit) {
this[i].addEventListener(evt, fn);
} else {
this[i].attachEvent("on" + evt, fn);
}
}
return this;
}},
off: {value: function (evt, fn) {
if (!tts.isFunction(fn))
tts.logger('Callback should be a function', 'warn');
var hasit = this[0].removeEventListener;
for (var i = 0; i < this.length; i++) {
if (hasit) {
this[i].removeEventListener(evt, fn);
} else {
this[i].detachEvent("on" + evt, fn);
}
}
return this;
}},
trigger: {value: function (eventName) {
for (var i = 0; i < this.length; i++) {
var event = document.createEvent('Event');
event.initEvent(eventName, true, true);
this[i].dispatchEvent(event);
}
return this;
}},
toggleClass: {value: function (cls) {
for (var i = 0; i < this.length; i++) {
if (this[i].classList) {
this[i].classList.toggle(cls);
} else {
var classes = this[i].className.split(' ');
var existingIndex = classes.indexOf(cls);
if (existingIndex >= 0) {
classes.splice(existingIndex, 1);
} else {
classes.push(cls);
}
this[i].className = classes.join(' ');
}
}
return this;
}},
addClass: {value: function (cls) {
for (var i = 0; i < this.length; i++) {
if (!tts.fn.hasClass.call({first: this[i]}, cls)) {
this[i].className += ' ' + cls;
}
}
return this;
}},
removeClass: {value: function (cls) {
for (var i = 0; i < this.length; i++) {
this[i].className = this[i].className.replace(cls, '');
}
return this;
}},
hasClass: {value: function (cls) {
return this.first.className != '' && new RegExp('\\b' + cls + '\\b').test(this.first.className);
}},
prop: {value: function (property, value) {
if (typeof value == 'undefined') {
return this.first[property];
} else {
for (var i = 0; i < this.length; i++) {
this[i][property] = value;
}
return this;
}
}},
attr: {value: function (property, value) {
if (typeof value == 'undefined') {
return this.first.getAttribute(property);
} else {
for (var i = 0; i < this.length; i++) {
this[i].setAttribute(property, value);
}
return this;
}
}},
removeAttr: {value: function (property) {
for (var i = 0; i < this.length; i++) {
this[i].removeAttribute(property);
}
return this;
}},
val: {value: function (value) {
var el = this.first, prop = 'value';
switch (el.tagName) {
case 'SELECT':
prop = 'selectedIndex';
break;
case 'OPTION':
prop = 'selected';
break;
case 'INPUT':
if (el.type == 'checkbox' || el.type == 'radio') {
prop = 'checked';
}
break;
}
return this.prop(prop, value);
}},
append: {value: function (value) {
var t = this, v = tts(value), len = v.length;
for (var i = 0; i < len; i++) {
t.first.appendChild(v[i].first || v[i]);
}
return this;
}},
before: {value: function (value) {
this.first.parentElement.insertBefore(tts().append(value).first, this.first);
return this;
}},
parent: {value: function () {
return tts(this.first.parentNode);
}},
clone: {value: function () {
return tts(this.first.cloneNode(true));
}},
remove: {value: function () {
for (var i = 0; i < this.length; i++) {
this[i].parentNode.removeChild(this[i]);
}
return this;
}},
find: {value: function (value) {
return tts(value, this.first);
}},
closest: {value: function (selector) {
var el = this.first;
do {
if (el[tts.matches](selector)) {
return tts(el);
}
} while (el = el.parentElement);
return null;
}},
is: {value: function (selector) {
return tts(this.filter(function (el) {
return el[tts.matches](selector);
}));
}},
css: {value: function (property, value) {
if (typeof value == 'undefined') {
return this.first.style[property];
} else {
for (var i = 0; i < this.length; i++) {
this[i].style[property] = value;
}
return this;
}
}},
html: {value: function (value) {
return this.prop('innerHTML', value);
}},
text: {value: function (value) {
return this.prop('textContent', value);
}},
empty: {value: function () {
return this.prop('innerHTML', '');
}},
hide: {value: function () {
for (var i = 0; i < this.length; i++) {
this[i].style.display = 'none';
}
return this;
}},
show: {value: function (type) {
var display_type = (type) ? type : '';
for (var i = 0; i < this.length; i++) {
this[i].style.display = display_type;
}
return this;
}},
invisible: {value: function () {
for (var i = 0; i < this.length; i++) {
this[i].style.visibility = 'hidden';
}
return this;
}},
visible: {value: function (type) {
for (var i = 0; i < this.length; i++) {
this[i].style.visibility = "visible";
}
return this;
}},
fadeStop: {value: function () {
for (var i = 0; i < this.length; i++) {
this[i].style.opacity = 1;
this[i].style.filter = "alpha(opacity=1)";
this[i].style.display = "inline-block";
this[i].style.visibility = "visible";
}
}},
fadeIn: {value: function (ms, display) {
var tms = (typeof ms == 'undefined') ? 600 : ms, display_as = (typeof display == 'undefined') ? "inline-block" : display;
for (var i = 0; i < this.length; i++) {
var elem = this[i].style, opacity = {}, timer = {};
elem.opacity = 0;
elem.filter = "alpha(opacity=0)";
elem.display = display_as;
elem.visibility = "visible";
opacity.i = 0;
timer.i = setInterval(function () {
opacity.i += 50 / tms;
if (opacity.i >= 1) {
clearInterval(timer.i);
opacity.i = 1;
}
elem.opacity = opacity.i;
elem.filter = "alpha(opacity=" + opacity.i * 100 + ")";
}, 50);
}
}},
fadeOut: {value: function (ms) {
var tms = (typeof ms == 'undefined') ? 600 : ms;
for (var i = 0; i < this.length; i++) {
var opacity = {}, timer = {}, elem = this[i].style;
opacity.i = 1;
timer.i = setInterval(function () {
opacity.i -= 50 / tms;
if (opacity.i <= 0) {
clearInterval(timer.i);
opacity.i = 0;
elem.display = "none";
elem.visibility = "hidden";
}
elem.opacity = opacity.i;
elem.filter = "alpha(opacity=" + opacity.i * 100 + ")";
}, 50);
}
}}
});
tts.now = function () {
return Date.now();
};
tts.parse = function (string) {
return JSON.parse(string);
};
tts.map = function (array, callback, arg) {
return array.map(callback, arg);
};
tts.random = function (min, max) {
return Math.floor((Math.random() * max) + min);
};
tts.trim = function (string) {
return string.trim();
};
tts.inArray = function (item, array) {
return (array.indexOf(item) !== -1) ? true : false;
};
tts.isFunction = function (item) {
if (typeof item === 'function') {
return true;
}
var type = Object.prototype.toString.call(item);
return type === '[object Function]' || type === '[object GeneratorFunction]';
};
var arrayOfCalls = [];
tts.once = function (callback, id, parms) {
var fn = window[callback], found = (!tts.inArray(id, arrayOfCalls) && tts.isFunction(fn)) ? fn(parms) : false;
if (found !== false) {
arrayOfCalls.push(id);
}
return found;
};
tts.isEmptyObject = function (obj) {
return Object.keys(obj).length === 0;
};
tts.isEmpty = function (str) {
return (!str || 0 === str.length);
};
tts.isNumeric = function (n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
tts.extend = function (object1, object2) {
return Object.assign({}, object1, object2);
};
tts.when = function (arrayOfPromises) {
return new Promise(function (resolve, reject) {
Promise.all(arrayOfPromises).then(values => {
resolve(values);
}).catch(errors => {
reject(errors);
});
});
};