/**
* Essential JavaScript Utilities
*
* @author Davey Jacobson <djacobson@usarugby.org>
* @namespace UTIL
* @version 0.0.54
* @changelog {2017-06-15} - 0.0.1 Release
* @changelog {2017-08-31} - 0.0.54 Added global vars,
* namespace( `BIN` ),
* methods( `BIN#_jstz` `UTIL#platform`, `UTIL#userTimezone` )
*/
(function( $, win, doc ) {
win = window||win;
doc = document||doc;
/**
* Shorthand Global Variables
*/
var tool = {},
$win = $(win),
$doc = $(doc),
$html = $(doc.documentElement),
$body = $(doc.body),
$head = $(doc.head),
/**
* Conditional check if environment is `dev` or `live`
*
* @param {Object} options Override default opts.
* @return {Boolean}
*/
devEnvChk = function( options ) {
var defaults = {
serverName: 'localhost',
serverPort: null,
serverTld : 'dev',
get domain() {
if ( this.serverPort !== null ) {
return this.serverName + '.' + this.serverTld + ':' + this.serverPort;
} else {
return this.serverName + '.' + this.serverTld; // assumes your development domain is `localhost.dev`
}
}
};
var opts = $.extend( defaults, options );
return location.href.match( new RegExp( opts.domain ), 'g' ) ? true : false;
},
/** Attach `devEnvChk` to variable `$dev` */
$dev = devEnvChk() ? '' : '.min';
/**
* @namespace BIN
*/
var BIN = {
/**
* Load external dependency: JSTZ
*
* @name jsTimezoneDetect
* @author Jon Nylander
* @version 1.0.6
* @see {@link http://pellepim.bitbucket.org/jstz/|JSTZ}
*/
_jstz: function( callback ) {
if ( typeof jstz === undefined ) {
$.getScript( 'https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz' + $dev + '.js', callback );
}
else {
// Written long due to `UTIL` not being defined until later.
if ( callback && typeof(callback) === 'function' ) {
callback();
}
}
}
};
/**
* @namespace UTIL
*/
var UTIL = {
/**
* Get var/val type
*
* @memberof UTIL
* @method is#Method
*/
is: {
fn: function( x ) {
return typeof(x) === 'function';
},
obj: function( x ) {
return typeof(x) === 'object';
},
str: function( x ) {
return typeof(x) === 'string';
}
},
/**
* Check for various platforms
*
* @memberof UTIL
* @method platform#Method
*/
platform: {
userAgent: function() {
return navigator.userAgent;
},
Windows: function( check ) {
check = (this.userAgent().match(/Win(dows\sNT)?(64)?/)) ? true : false;
return check;
},
MacOS: function( check ) {
check = (this.userAgent().match(/Mac(intosh)?(\sOS)?/)) ? true : false;
return check;
},
Linux: function( check ) {
check = (this.userAgent().match(/Linux/)) ? true : false;
return check;
}
},
/**
* Get user timezone. Save/Use during session.
*
* @memberof UTIL
* @method userTimezone
*/
userTimezone: function() {
BIN._jstz(function() {
var tz = jstz.determine() || 'UTC';
if ( !sessionStorage.timezone ) {
sessionStorage.setItem( 'timezone', tz.name() );
}
tool.timezone = sessionStorage.timezone;
});
return tool.timezone;
}
};
/**
* Methods to be fired globally.
*/
$doc.ready(function() {
// Client-side UI
win.tool = tool;
// User Timezone
UTIL.userTimezone();
});
}( jQuery, window, document ));
Just a basic object of common (but essential) JavaScript utility functions that I use for all of my major projects. Quick, simple and a HUGE timesaver when developing.
New methods will be added soon...
New methods will be added soon...
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.