Skip to content

Instantly share code, notes, and snippets.

@mikewest
Created April 14, 2010 12:31
Show Gist options
  • Save mikewest/365751 to your computer and use it in GitHub Desktop.
Save mikewest/365751 to your computer and use it in GitHub Desktop.
/*jslint onevar: false, white: false, browser: true, laxbreak: true, undef: true, nomen: false, eqeqeq: true, plusplus: false, bitwise: true, regexp: true, newcap: true, immed: true */
/*global SDE: true, jQuery: false, console: false */
/**
* The SDE object is the single global object (that should be) created by the
* code written for Sueddeutsche.de. It acts as a namespace for the various
* helper functions, classes, and modules that we write, and ought be included
* everywhere on the site.
*
* @module sde
* @title SDE Global Object
*/
if ( typeof SDE === "undefined" || !SDE ) {
/**
* The SDE global namespace object.
*
* @class SDE
* @static
*/
var SDE = {};
}
/**
* Boolean value specifying "production" status. If `true`, disables
* potentially problematic functionality (like logging).
*
* @property is_production
* @default true
*/
SDE.is_production = false;
/**
* Logging method: wrapper for `console.log`
*
* <pre><code>
* SDE.log( 'OMG! Something terrible happened!' );
* SDE.log( 'This is an object: %o', object );
* </code></pre>
*
* @method log
* @static
*
* @return {Boolean} True if message was logged successfully.
*/
SDE.log = function ( ) {
if ( !SDE.is_production && typeof console !== 'undefined' && typeof console.log !== 'undefined' && typeof console.log.apply !== 'undefined' ) {
return ( console.log.apply( console, arguments ) === 'undefined' );
} else {
return false;
}
};
/**
* Generate a (more or less) unique ID
*
* @method uid
* @static
* @param {Int} length The length of the UID. Defaults to 16 characters.
*
* @return {String} The generated ID.
*/
SDE.uid = function ( length ) {
var uid = '';
if ( !length || length <= 0 ) {
length = 16;
}
for (var i = 0; i < length; i++) {
uid += String.fromCharCode(
Math.floor( Math.random() * 26 ) + 97
);
}
return uid;
};
/**
* Returns the requested namespace under SDE, creating it if necessary.
* Copied almost completely from YUI, both in concept and implementation.
*
* <pre><code>
* SDE.namespace( 'spiele' ); // returns a reference to `SDE.spiele`
* SDE.namespace( 'SDE.spiele' ); // same as above
* SDE.namespace( 'spiele.duel' ); // also works with multiple packages
* </code></pre>
*
* @method namespace
* @static
*
* @param {String} n The namespace to (potentially) create and return
* @return {Object} A reference to the requested namespace object
*/
SDE.namespace = function ( n ) {
var obj = SDE, i;
n = n.split( '.' );
for ( i = ( n[0] === 'SDE' ) ? 1 : 0; i < n.length; i++ ) {
obj[ n[ i ] ] = obj[ n[ i ] ] || {};
obj = obj[ n[ i ] ];
}
return obj;
};
if ( typeof jQuery !== "undefined" && jQuery ) {
SDE.$ = jQuery;
jQuery.noConflict();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment