Skip to content

Instantly share code, notes, and snippets.

@reedling78
Created February 1, 2014 19:46
Show Gist options
  • Save reedling78/8757671 to your computer and use it in GitHub Desktop.
Save reedling78/8757671 to your computer and use it in GitHub Desktop.
Helpers
(function() {
'use strict';
define([], function() {
return {
browserDetect: function() {
var returnVal = 'NOT IE';
var bName = window.navigator.userAgent;
var bVersion = window.navigator.appVersion;
if (bVersion.indexOf("MSIE 7.0") >= 1 || bVersion.indexOf("msie 7.0") >= 1) {
returnVal = 'msie 7';
}
if (bVersion.indexOf("MSIE 8.0") >= 1 || bVersion.indexOf("msie 8.0") >= 1) {
returnVal = 'msie 8';
}
if (bVersion.indexOf("MSIE 9.0") >= 1 || bVersion.indexOf("msie 9.0") >= 1) {
returnVal = 'msie 9';
}
return returnVal;
},
toObject: function(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
rv[arr[i].field] = arr[i].value;
return rv;
},
getTime: function(d) {
d = new Date(d);
var hour = (d.getHours() <= 12) ? d.getHours() : d.getHours() - 12,
minute = (d.getMinutes() < 10) ? '0' + d.getMinutes() : d.getMinutes(),
meridiem = (d.getHours() < 12) ? 'AM' : 'PM';
return '' + hour + ':' + minute + ' ' + meridiem;
},
getDate: function(d) {
d = new Date(d);
var month = d.getMonth() + 1,
date = d.getDate(),
year = d.getFullYear();
return '' + month + '/' + date + '/' + year;
},
getShortMonth: function(d) {
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"];
return monthNames[d.getMonth()];
},
getShortDay: function(d) {
var weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
return weekday[d.getDay()];
},
getLongMonth: function(d) {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return monthNames[d.getMonth()];
},
getLongDay: function(d) {
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return weekday[d.getDay()];
},
getOrdinal: function(d) {
var i = d.getDate();
var j = i % 10;
if (j == 1 && i != 11) {
return i + "st";
}
if (j == 2 && i != 12) {
return i + "nd";
}
if (j == 3 && i != 13) {
return i + "rd";
}
return i + "th";
},
tomorrow: function(dashs) {
var someDate = new Date();
var numberOfDaysToAdd = 1;
var someFormattedDate;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
if (dashs) {
someFormattedDate = mm + '-' + dd + '-' + y;
} else {
someFormattedDate = mm + '/' + dd + '/' + y;
}
return someFormattedDate;
},
formatMoney: function(x) {
return '$' + parseInt(x, 10).toFixed(2);
},
setCartAttr: function(el, model) {
el.setAttribute('action', 'addtocartajax');
el.setAttribute('sessionId', App.i.Models.APIFilter.get('sessionId'));
el.setAttribute('courseId', model.get('courseInfo').courseId);
el.setAttribute('price', model.get('purchase').price);
el.setAttribute('savedAmount', model.get('purchase').savingsAmount);
el.setAttribute('reservationString', model.get('reservationString'));
el.setAttribute('reservationTypeId', model.get('reservationTypeId'));
el.setAttribute('feeId', model.get('feeId'));
el.setAttribute('cancellationPeriod', model.get('info').cancellationPeriod);
el.setAttribute('teeTime', model.get('teeTime'));
el.setAttribute('teeTimeTypeId', model.get('teeTimeTypeId'));
el.setAttribute('regularPrice', model.get('purchase').regularPrice);
el.setAttribute('transactionFee', model.get('purchase').transactionFee);
el.setAttribute('currencyLocaleId', model.get('courseInfo').currencyLocaleId);
el.setAttribute('OriginatingContactID', App.i.Models.APIFilter.get('contactId'));
el.setAttribute('Utilization', model.get('info').utilization);
el.setAttribute('sku', model.get('sku'));
el.setAttribute('minPlayers', model.get('rules').minPlayers);
el.setAttribute('maxPlayers', model.get('rules').maxPlayers);
el.setAttribute('allowOne', model.get('rules').allowOne);
el.setAttribute('allowTwo', model.get('rules').allowTwo);
el.setAttribute('allowThree', model.get('rules').allowThree);
el.setAttribute('allowFour', model.get('rules').allowFour);
el.setAttribute('allowFive', model.get('rules').allowFive);
el.setAttribute('courseName', model.get('courseInfo').courseName);
if (model.get('courseInfo').hasCourseNote == undefined) {
el.setAttribute('hasCourseNote', model.get('hasCourseNote'));
el.setAttribute('courseNote', model.get('courseNote'));
} else {
el.setAttribute('hasCourseNote', model.get('courseInfo').hasCourseNote);
el.setAttribute('courseNote', model.get('courseInfo').courseNote);
}
el.setAttribute('rateIcons', JSON.stringify(model.get('rateIcons')));
},
timeIncrementer: function(startHour, endHour) {
//Used on the course details page to populate the time drop down box
var returnValue = [{
text: 'Time',
value: -1
}];
var minArray = [':00', ':30'];
var incrementer = startHour;
var hour, meridiem;
if (startHour < endHour) {
for (var i = startHour; i < (endHour + 1); i++) {
hour = (i <= 12) ? i : i - 12;
meridiem = (i < 12) ? 'AM' : 'PM';
for (var x = 0; x < minArray.length; x++) {
returnValue.push({
text: hour + minArray[x] + ' ' + meridiem,
value: hour + minArray[x] + ' ' + meridiem
});
};
}
}
return returnValue;
},
queryObj: function() {
var result = {}, keyValuePairs = location.search.slice(1).split('&');
keyValuePairs.forEach(function(keyValuePair) {
keyValuePair = keyValuePair.split('=');
result[keyValuePair[0]] = keyValuePair[1] || '';
});
return result;
},
getQueryString: function() {
var url = document.URL;
url = url.substring(url.indexOf('#')).replace('#', '&');
url = url.split("&");
var d = {}, a;
for (var i = 0; i < url.length; i++) {
if (url[i].indexOf('=') != -1) {
a = url[i].split('=');
d[a[0]] = a[1];
}
}
return d;
}
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment