Skip to content

Instantly share code, notes, and snippets.

@brandontrowe
Last active November 22, 2016 18:08
Show Gist options
  • Save brandontrowe/3ab6ab6229e5afa93991 to your computer and use it in GitHub Desktop.
Save brandontrowe/3ab6ab6229e5afa93991 to your computer and use it in GitHub Desktop.
$.fn.extend({
/**
* Outputs a timer to an element in the DOM
* @param {number} seconds - A positive
*/
countdownTimer: function (time) {
return this.each(function () {
var $this = $(this);
var timer = time;
var minutes;
var seconds;
var tick = setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
$this.text(minutes + ":" + seconds);
if (--timer < 0) {
clearInterval(tick);
}
}, 1000);
});
},
/*
* Grab the outer html of an element.
*/
outerHtml: function( value ) {
return ( value )
? this.before( value ).remove()
: jQuery( '<p>' ).append( this.eq(0).clone() ).html();
},
/*
* Toggle the existence of an attribute
*/
toggleAttr: function ( value, stateVal ) {
var justToggle = (stateVal === undefined);
return this.each(function () {
var self = $( this ),
reverse = !self.prop( value );
if ( justToggle ) {
self.prop( value, reverse );
} else {
self.prop( value, stateVal );
}
});
},
/**
* .jsonToHtml( method {settingsObj} )
* methods
* init: Initial setup.
* update: Updates the current HTML.
*
* @param {object} settings = {
* @param {string} htmlTmpl: HTML string with encoded object parameters. ex:{{name}},
* @param {string} ajaxPath: The path to pull down the JSON,
* @param {string} ajaxData: additional data to send along with the AJAX call
* }
*/
jsonToHtml: function( method ) {
var settings = {
htmlTmpl : '',
ajaxPath: '',
ajaxData: ''
};
var data = {};
var _setSettings = function (options) {$.extend(settings, options) };
var _buildItemHtml = function (obj) {
var tmpl = settings.htmlTmpl,
html = tmpl
.replace(/\{{(\w+)\}}/g, function(_,k){
return obj[k];
});
return html;
};
var _updateDisplay = function ( itemData ) {
var fullHtml = [];
var i;
var length;
for (i = 0, length = itemData.length; i < length; i++) {
fullHtml[i] = _buildItemHtml( itemData[i] );
}
$( data.that ).html( fullHtml.join('') )
.trigger('dp:init');
};
var _getItemData = function () {
return $.ajax({
type : 'POST',
dataType : 'json',
url : settings.ajaxPath,
data : settings.ajaxData
}).done(function ( json ) {
_updateDisplay( json );
}).fail(function ( jqXHR, textStatus ) {
console.log( textStatus );
});
};
var _init = function () {
_getItemData();
};
var methods = {
init : function (options) {
return this.each(function () {
if (options) { _setSettings(options); }
data.that = this;
_init();
});
},
update : function (options) {
return this.each(function () {
if (options) { _setSettings(options); }
data.that = this;
_init();
});
}
};
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}
$.error('Method ' + method + ' does not exist');
},
ensureLoad: function(handler) {
return this.each(function() {
if(this.complete) {
handler.call(this);
} else {
$(this).load(handler);
}
});
},
getTranslateValues: function() {
var translateMatrix = $(this).css('transform').match(/-?[\d\.]+/g);
return {
topLeft: parseInt(translateMatrix[0]),
topRight: parseInt(translateMatrix[1]),
bottomLeft: parseInt(translateMatrix[2]),
bottomLeft: parseInt(translateMatrix[3]),
x: parseInt(translateMatrix[4]),
y: parseInt(translateMatrix[5])
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment