metro4
Version:
The front-end framework for Build responsive, mobile-first projects on the web with the first front-end component library in Metro Style
354 lines (303 loc) • 11.7 kB
JavaScript
$.fn.extend({
toggleAttr: function(a, v){
return this.each(function(){
var el = $(this);
if (v !== undefined) {
el.attr(a, v);
} else {
if (el.attr(a) !== undefined) {
el.removeAttr(a);
} else {
el.attr(a, ""+a);
}
}
});
},
clearClasses: function(){
return this.each(function(){
this.className = "";
});
},
fire: function(eventName, data){
return this.each(function(){
var e = jQuery.Event(eventName);
e.detail = data;
$(this).trigger(e);
});
}
});
Array.prototype.shuffle = function () {
var currentIndex = this.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = this[currentIndex];
this[currentIndex] = this[randomIndex];
this[randomIndex] = temporaryValue;
}
return this;
};
Array.prototype.clone = function () {
return this.slice(0);
};
Array.prototype.unique = function () {
var a = this.concat();
for (var i = 0; i < a.length; ++i) {
for (var j = i + 1; j < a.length; ++j) {
if (a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};
if (!Array.from) {
Array.from = function(val) {
var i, a = [];
if (val.length === undefined && typeof val === "object") {
return Object.values(val);
}
if (val.length !== undefined) {
for(i = 0; i < val.length; i++) {
a.push(val[i]);
}
return a;
}
throw new Error("Value can not be converted to array");
};
}
if (typeof Array.contains !== "function") {
Array.prototype.contains = function(val, from){
return this.indexOf(val, from) > -1;
}
}
/**
* Number.prototype.format(n, x, s, c)
*
* @param n: length of decimal
* @param x: length of whole part
* @param s: sections delimiter
* @param c: decimal delimiter
*/
Number.prototype.format = function(n, x, s, c) {
var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
num = this.toFixed(Math.max(0, ~~n));
return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
};
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
String.prototype.contains = function() {
return !!~String.prototype.indexOf.apply(this, arguments);
};
String.prototype.toDate = function(format, locale) {
var result;
var normalized, normalizedFormat, formatItems, dateItems, checkValue;
var monthIndex, dayIndex, yearIndex, hourIndex, minutesIndex, secondsIndex;
var year, month, day, hour, minute, second;
var parsedMonth;
locale = locale || "en-US";
var monthNameToNumber = function(month){
var d, months, index, i;
month = month.substr(0, 3);
if (
locale !== undefined
&& locale !== "en-US"
&& Locales !== undefined
&& Locales[locale] !== undefined
&& Locales[locale]['calendar'] !== undefined
&& Locales[locale]['calendar']['months'] !== undefined
) {
months = Locales[locale]['calendar']['months'];
for(i = 12; i < months.length; i++) {
if (months[i].toLowerCase() === month.toLowerCase()) {
index = i - 12;
break;
}
}
month = Locales["en-US"]['calendar']['months'][index];
}
d = Date.parse(month + " 1, 1972");
if(!isNaN(d)){
return new Date(d).getMonth() + 1;
}
return -1;
};
if (format === undefined || format === null || format === "") {
return new Date(this);
}
// normalized = this.replace(/[^a-zA-Z0-9%]/g, '-');
normalized = this.replace(/[\/,.:\s]/g, '-');
normalizedFormat= format.toLowerCase().replace(/[^a-zA-Z0-9%]/g, '-');
formatItems = normalizedFormat.split('-');
dateItems = normalized.split('-');
checkValue = normalized.replace(/\-/g,"");
if (checkValue.trim() === "") {
return "Invalid Date";
}
monthIndex = formatItems.indexOf("mm") > -1 ? formatItems.indexOf("mm") : formatItems.indexOf("%m");
dayIndex = formatItems.indexOf("dd") > -1 ? formatItems.indexOf("dd") : formatItems.indexOf("%d");
yearIndex = formatItems.indexOf("yyyy") > -1 ? formatItems.indexOf("yyyy") : formatItems.indexOf("yy") > -1 ? formatItems.indexOf("yy") : formatItems.indexOf("%y");
hourIndex = formatItems.indexOf("hh") > -1 ? formatItems.indexOf("hh") : formatItems.indexOf("%h");
minutesIndex = formatItems.indexOf("ii") > -1 ? formatItems.indexOf("ii") : formatItems.indexOf("mi") > -1 ? formatItems.indexOf("mi") : formatItems.indexOf("%i");
secondsIndex = formatItems.indexOf("ss") > -1 ? formatItems.indexOf("ss") : formatItems.indexOf("%s");
if (monthIndex > -1 && dateItems[monthIndex] !== "") {
if (isNaN(parseInt(dateItems[monthIndex]))) {
dateItems[monthIndex] = monthNameToNumber(dateItems[monthIndex]);
if (dateItems[monthIndex] === -1) {
return "Invalid Date";
}
} else {
parsedMonth = parseInt(dateItems[monthIndex]);
if (parsedMonth < 1 || parsedMonth > 12) {
return "Invalid Date";
}
}
} else {
return "Invalid Date";
}
year = yearIndex >-1 && dateItems[yearIndex] !== "" ? dateItems[yearIndex] : null;
month = monthIndex >-1 && dateItems[monthIndex] !== "" ? dateItems[monthIndex] : null;
day = dayIndex >-1 && dateItems[dayIndex] !== "" ? dateItems[dayIndex] : null;
hour = hourIndex >-1 && dateItems[hourIndex] !== "" ? dateItems[hourIndex] : null;
minute = minutesIndex>-1 && dateItems[minutesIndex] !== "" ? dateItems[minutesIndex] : null;
second = secondsIndex>-1 && dateItems[secondsIndex] !== "" ? dateItems[secondsIndex] : null;
result = new Date(year,month-1,day,hour,minute,second);
return result;
};
String.prototype.toArray = function(delimiter, type, format){
var str = this;
var a;
type = type || "string";
delimiter = delimiter || ",";
format = format === undefined || format === null ? false : format;
a = (""+str).split(delimiter);
return a.map(function(s){
var result;
switch (type) {
case "int":
case "integer": result = parseInt(s); break;
case "number":
case "float": result = parseFloat(s); break;
case "date": result = !format ? new Date(s) : s.toDate(format); break;
default: result = s.trim();
}
return result;
});
};
Date.prototype.getWeek = function (dowOffset) {
var nYear, nday, newYear, day, daynum, weeknum;
dowOffset = !Utils.isValue(dowOffset) ? METRO_WEEK_START : typeof dowOffset === 'number' ? parseInt(dowOffset) : 0;
newYear = new Date(this.getFullYear(),0,1);
day = newYear.getDay() - dowOffset;
day = (day >= 0 ? day : day + 7);
daynum = Math.floor((this.getTime() - newYear.getTime() -
(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
if(day < 4) {
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52) {
nYear = new Date(this.getFullYear() + 1,0,1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
weeknum = nday < 4 ? 1 : 53;
}
}
else {
weeknum = Math.floor((daynum+day-1)/7);
}
return weeknum;
};
Date.prototype.getYear = function(){
return this.getFullYear().toString().substr(-2);
};
Date.prototype.format = function(format, locale){
if (locale === undefined) {
locale = "en-US";
}
var cal = (Metro.locales !== undefined && Metro.locales[locale] !== undefined ? Metro.locales[locale] : Metro.locales["en-US"])['calendar'];
var date = this;
var nDay = date.getDay(),
nDate = date.getDate(),
nMonth = date.getMonth(),
nYear = date.getFullYear(),
nHour = date.getHours(),
aDays = cal['days'],
aMonths = cal['months'],
aDayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
isLeapYear = function() {
return (nYear%4===0 && nYear%100!==0) || nYear%400===0;
},
getThursday = function() {
var target = new Date(date);
target.setDate(nDate - ((nDay+6)%7) + 3);
return target;
},
zeroPad = function(nNum, nPad) {
return ('' + (Math.pow(10, nPad) + nNum)).slice(1);
};
return format.replace(/(%[a-z])/gi, function(sMatch) {
return {
'%a': aDays[nDay].slice(0,3),
'%A': aDays[nDay],
'%b': aMonths[nMonth].slice(0,3),
'%B': aMonths[nMonth],
'%c': date.toUTCString(),
'%C': Math.floor(nYear/100),
'%d': zeroPad(nDate, 2),
'dd': zeroPad(nDate, 2),
'%e': nDate,
'%F': date.toISOString().slice(0,10),
'%G': getThursday().getFullYear(),
'%g': ('' + getThursday().getFullYear()).slice(2),
'%H': zeroPad(nHour, 2),
// 'HH': zeroPad(nHour, 2),
'%I': zeroPad((nHour+11)%12 + 1, 2),
'%j': zeroPad(aDayCount[nMonth] + nDate + ((nMonth>1 && isLeapYear()) ? 1 : 0), 3),
'%k': '' + nHour,
'%l': (nHour+11)%12 + 1,
'%m': zeroPad(nMonth + 1, 2),
// 'mm': zeroPad(nMonth + 1, 2),
'%M': zeroPad(date.getMinutes(), 2),
// 'MM': zeroPad(date.getMinutes(), 2),
'%p': (nHour<12) ? 'AM' : 'PM',
'%P': (nHour<12) ? 'am' : 'pm',
'%s': Math.round(date.getTime()/1000),
// 'ss': Math.round(date.getTime()/1000),
'%S': zeroPad(date.getSeconds(), 2),
// 'SS': zeroPad(date.getSeconds(), 2),
'%u': nDay || 7,
'%V': (function() {
var target = getThursday(),
n1stThu = target.valueOf();
target.setMonth(0, 1);
var nJan1 = target.getDay();
if (nJan1!==4) target.setMonth(0, 1 + ((4-nJan1)+7)%7);
return zeroPad(1 + Math.ceil((n1stThu-target)/604800000), 2);
})(),
'%w': '' + nDay,
'%x': date.toLocaleDateString(),
'%X': date.toLocaleTimeString(),
'%y': ('' + nYear).slice(2),
// 'yy': ('' + nYear).slice(2),
'%Y': nYear,
// 'YYYY': nYear,
'%z': date.toTimeString().replace(/.+GMT([+-]\d+).+/, '$1'),
'%Z': date.toTimeString().replace(/.+\((.+?)\)$/, '$1')
}[sMatch] || sMatch;
});
};
Date.prototype.addHours = function(n) {
this.setTime(this.getTime() + (n*60*60*1000));
return this;
};
Date.prototype.addDays = function(n) {
this.setDate(this.getDate() + (n));
return this;
};
Date.prototype.addMonths = function(n) {
this.setMonth(this.getMonth() + (n));
return this;
};
Date.prototype.addYears = function(n) {
this.setFullYear(this.getFullYear() + (n));
return this;
};