Skip to content

Instantly share code, notes, and snippets.

@qbunt
Created May 15, 2013 02:48
Show Gist options
  • Save qbunt/5581333 to your computer and use it in GitHub Desktop.
Save qbunt/5581333 to your computer and use it in GitHub Desktop.
utils
/**
* User: jeremy.bunting
* Date: 4/25/13
* Time: 11:45 AM
*/
var utils = (function () {
var queryString = decodeURIComponent(window.location.search);
var parseQueryString, getQueryParam, environment, truncateWithEllipsis, setCookie, getCookie, deleteCookie, destroyView, loadTemplate;
parseQueryString = function () {
var query = {};
queryString.replace(/\b([^&=]*)=([^&=]*)\b/g, function (m, a, d) {
if (typeof query[a] != 'undefined') {
query[a] += ',' + d;
} else {
query[a] = d;
}
});
return query;
};
environment = function () {
// NOTE these breakpoints are taken directly from the _queries.scss stylesheet, they're identical
if (Modernizr.mq('only screen and (max-width: 480px)') == true) {
return 'mobile'
}
if (Modernizr.mq('only screen and (min-width: 481px) and (max-width: 1024px)') == true) {
// return 'tablet'
return 'desktop'
}
if (Modernizr.mq('only screen and (max-width: 1382px)') == true) {
return 'desktop'
}
return 'desktop'
};
truncateWithEllipsis = function (str, limit) {
if (str.length >= limit) {
return jQuery.trim(str).substring(0, limit).split(" ").slice(0, -1).join(" ") + "...";
} else {
return str;
}
},
setCookie = function (c_name, value, exdays) {
var exdate = new Date();
var time = exdate.getTime();
time += 3600 * 1000;
exdate.setTime(time);
//exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
};
getCookie = function (c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
};
deleteCookie = function (name) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};
getQueryParam = function (parameter) {
var p = escape(unescape(parameter));
var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?", "i");
var match = regex.exec(queryString);
var value = null;
if (match != null) {
value = match[1];
}
return value;
};
destroyView = function (view) {
// completely unbind and destroy the view
view.undelegateEvents();
view.$el.removeData().unbind();
//Remove view from DOM
view.remove();
Backbone.View.prototype.remove.call(view);
};
loadTemplate = function (views, callback) {
// loads an array of Mustache templates, applies as a property of a matched view (must name match)
var deferreds = [];
$.each(views, function (index, view) {
deferreds.push($.get(config.appURL + 'media/app/scripts/templates/' + view + '.mustache', function (data) {
app.Views[view].prototype.template = data;
}));
});
$.when.apply($, deferreds).done(callback);
};
countUpVert = function(renderEl, renderVal) {
// renderEl.addClass('blur');
var height = renderEl.height();
console.log(renderVal);
var numDigits = parseFloat(renderVal).toFixed(0).length;
console.log('numDigits', numDigits);
//add spans
if ($('.digit', renderEl).length == 0) {
if (numDigits == 5) {
renderEl.append($('<span/>').addClass('digit-4 digit')).append($('<span/>').addClass('digit-3 digit')).append($('<span/>').addClass('comma').html('<br/>,')).append($('<span/>').addClass('digit-2 digit')).append($('<span/>').addClass('digit-1 digit')).append($('<span/>').addClass('digit-0 digit'));
} else if (numDigits == 4) {
renderEl.append($('<span/>').addClass('digit-3 digit')).append($('<span/>').addClass('comma').html('<br/>,')).append($('<span/>').addClass('digit-2 digit')).append($('<span/>').addClass('digit-1 digit')).append($('<span/>').addClass('digit-0 digit'));
} else if (numDigits == 3) {
renderEl.append($('<span/>').addClass('digit-2 digit')).append($('<span/>').addClass('digit-1 digit')).append($('<span/>').addClass('digit-0 digit'));
} else if (numDigits == 2) {
renderEl.append($('<span/>').addClass('digit-1 digit')).append($('<span/>').addClass('digit-0 digit'));
} else if (numDigits == 1) {
renderEl.append($('<span/>').addClass('digit-0 digit'));
}
}
var countArray = renderVal.toString().split('').reverse();
renderEl.find('.digit').html('<br/>0<br/>1<br/>2<br/>3<br/>4<br/>5<br/>6<br/>7<br/>8<br/>9');
_.each(countArray, function(val, index, list){
var delayTime = "0.1"+index;
TweenMax.to(renderEl.find('.digit-' + index), 1.5, {css:{'margin-top':(-1 * ((val * height) + height))}, ease:Bounce.easeOut, onComplete:function(){
// renderEl.removeClass('blur');
}, delay:delayTime
} );
if (index % 3 == 0) {
if (index < countArray.length) {
renderEl.find('.digit-' + index).next('.comma').animate({
marginTop: height * -1
}, 'slow');
} else {
renderEl.find('.digit-' + index).next('.comma').animate({
marginTop: 0
}, 'slow');
}
}
});
}
return {
parseQueryString: parseQueryString,
getQueryParam: getQueryParam,
setCookie: setCookie,
getCookie: getCookie,
deleteCookie: deleteCookie,
destroyView: destroyView,
environment: environment,
truncateWithEllipsis: truncateWithEllipsis,
loadTemplate: loadTemplate,
countUp: countUpVert
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment