Matt Snider JavaScript Resource

Understanding JavaScript and Frameworks

Wednesday, October 24, 2007

Core.js

/** * Copyright (c) 2007, Matt Snider, LLC All rights reserved. * Version: 1.0 */ /** * The Core Object and namespace for attaching additional Framework, Toolkit, and Business Objects, Functions, etc. * @namespace Core * @class Core * @dependencies DOM must be ready */ /** * W3C DOM Level 2 standard node types; for older browsers and IE */ if (null == document.ELEMENT_NODE) { document.ELEMENT_NODE = 1; document.ATTRIBUTE_NODE = 2; document.TEXT_NODE = 3; document.CDATA_SECTION_NODE = 4; document.ENTITY_REFERENCE_NODE = 5; document.ENTITY_NODE = 6; document.PROCESSING_INSTRUCTION_NODE = 7; document.COMMENT_NODE = 8; document.DOCUMENT_NODE = 9; document.DOCUMENT_TYPE_NODE = 10; document.DOCUMENT_FRAGMENT_NODE = 11; document.NOTATION_NODE = 12; } /** * manages commonwealth variables: version, browser, debug state, namespace */ window.Core = function() { // Private namespace var debugLevel = 0; // Public namespace return { /** * JavaScript Framework version */ VERSION: ‘1.0′, /** * Each HTML page on your site should have the same ID on the body tag, put that hear to speed up some DOM searches */ HTML_BODY_ID: ‘body’, /** * Object namespace placeholder for attaching page specific business logic */ Biz: {}, /** * Object namespace placeholder for attaching global constants; inner Function to create Client Singleton */ Constants: {}, /** * Object namespace for attaching client detection logic */ Client: function() { // Private namespace var versionSearchString = ”; /** * data to identify browsers */ var dataBrowser = [ {string: navigator.userAgent, subString: “OmniWeb”, versionSearch: “OmniWeb/”, identity: “OmniWeb”}, {string: navigator[”vendor”], subString: “Apple”, identity: “Safari”}, {prop: window[”opera”], identity: “Opera”}, {string: navigator[”vendor”], subString: “iCab”, identity: “iCab”}, {string: navigator[”vendor”], subString: “KDE”, identity: “Konqueror”}, {string: navigator.userAgent, subString: “Firefox”, identity: “Firefox”}, {string: navigator[”vendor”], subString: “Camino”, identity: “Camino”}, {string: navigator.userAgent, subString: “Netscape”, identity: “Netscape”}, // for newer Netscapes (6+) {string: navigator.userAgent, subString: “MSIE”, identity: “Explorer”, versionSearch: “MSIE”}, {string: navigator.userAgent, subString: “Gecko”, identity: “Mozilla”, versionSearch: “rv”}, {string: navigator.userAgent, subString: “Mozilla”, identity: “Netscape”, versionSearch: “Mozilla”} // for older Netscapes (4-) ]; /** * data to identify OS */ var dataOS = [ {string: navigator.platform, subString: “Win”, identity: “Windows”}, {string: navigator.platform, subString: “Mac”, identity: “Mac”}, {string: navigator.platform, subString: “Linux”, identity: “Linux”} ]; /** * Perform browser search on navigator * @param data {Array} Collection of browser detection Objects */ var searchString = function (data) { // iterate on the dataBrowser set; order is important or will match wrong versions for (var i=0; i<data.length; i++) { var dataString = data[i][”string”], dataProp = data[i][”prop”]; versionSearchString = data[i][”versionSearch”] || data[i][”identity”]; if (dataString) { if (dataString.indexOf(data[i][”subString”]) != -1) {return data[i][”identity”];} } else if (dataProp) { return data[i][”identity”]; } } }; /** * Perform browser version search on versionSearch or identity of the browser data Object * @param dataString {Array} Collection of browser detection Objects */ var searchVersion = function (dataString) { var index = dataString.indexOf(versionSearchString); return (-1 == index)? null: parseFloat(dataString.substring(index + versionSearchString.length + 1)); }; // Public namespace return { /** * The browser String as determined by searchString * @property browser * @type String */ browser: searchString(dataBrowser) || ‘An unknown browser’, /** * The version String as determined by searchVersion; must be called after searchString on dataBrowser * @property browser * @type String */ version: searchVersion(navigator.userAgent) || searchVersion(navigator.appVersion) || ‘an unknown version’, /** * The OS String as determined by searchString * @property browser * @type String */ os: searchString(dataOS) || ‘an unknown OS’, /** * Creates a cookie with @ name set to value that expires in n days * @param name {String} The name of the cookie * @param value {String} The value of the cookie * @param days {String} OPTIONAL: The number of days before cookie expires, otherwise expires at end of session */ createCookie: function(name, value, days) { var expires=null; if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); expires = “; expires=”+date.toGMTString(); } else {expires = “”;} document.cookie = name+”=”+value+expires+”; path=/”; }, /** * Expires the cookie * @param name {String} The name of the cookie */ eraseCookie: function(name) { this.createCookie(name, ”, -1); }, /** * X-browser method to find the BODY tag; lazy definition to improve repeat performance * * @method getBody * @return {HTMLElement} the BODY tag * @static */ getBody: function() { var bd = window.document.body || window.document.childNodes[0].childNodes[1] || window.document.getElementsByTagName(”body”)[0] || window.document; Mint.Client.getBody = function() {return bd;} return Mint.Client.getBody(); }, /** * X-browser method to find the scroll offset; lazy definition to improve repeat performance * * @method getScrollOffset * @return {Object} the scroll offset object {x, y} * @static */ getScrollOffset: function() { var de = document.documentElement, db = Mint.Client.getBody(); if (de && (de.scrollTop || de.scrollLeft)) { // compliant browser Mint.Client.getScrollOffset = function() {return {x: dd.scrollLeft, y: dd.scrollTop};} } else if (db) { // IE Mint.Client.getScrollOffset = function() {return {x: db.scrollLeft, y: db.scrollTop};} } else { // default, so calls to this method won’t fail Mint.Client.getScrollOffset = function() {return {x: 0, y: 0};} } return Mint.Client.getScrollOffset(); }, /** * Returns true if the browser variable is Explorer */ isIE: function() { return this.browser == “Explorer”; }, /** * Returns true if the browser variable is Opera */ isOpera: function() { return this.browser == “Opera”; }, /** * Retrieves the value of a cookie * @param name {String} The name of the cookie * @return {String} Value of the cookie or null */ readCookie: function(name) { var nameEQ = name + “=”; var ca = document.cookie.split(’;'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==’ ‘) {c = c.substring(1,c.length);} if (c.indexOf(nameEQ) == 0) {return c.substring(nameEQ.length,c.length);} } return null; } }; }(), /** * Object namespace placeholder for attaching debug constants and functions */ Debug: { ALL: 0, ERROR: 1, DEBUG: 2, PRODUCTION: 5 }, /** * Object namespace placeholder for attaching page specific widgets */ Widget: {}, /** * Object namespace placeholder for attaching page specific utility Objects and Functions */ Util: {}, /** * Placeholder function, used whenever an empty anonymous function is required */ emptyFunction: function() {}, /** * returns an image object with src, useful for image caching * @param src {String} The location of the image * @return {Image} A Javascript Image Object of the src */ getImage: function(src) { var img = new Image(); img.src = src; return img; }, /** * Returns the debug level of the application * @return {Integer} the debug level */ getDebugLevel: function() {return debugLevel;}, /** * Sets the debug level of the application * @param lvl {Integer} the debug level */ setDebugLevel: function(lvl) {debugLevel = lvl;} }; }(); /* Section: Type Detection Functions */ /** Function: $defined Returns true if the passed in value/Object is defined, that means it is not null or undefined. Arguments: o - the Object to inspect. Returns: {boolean} */ function $defined(o){ return (o != undefined && o != null); }; /** Function: $type Returns the type of Object that matches the element passed in. Arguments: obj - the Object to inspect. Example: var myString = ‘hello’; $type(myString); //returns “string” Returns: ‘element’ - if o is a DOM element node ‘textnode’ - if o is a DOM text node ‘whitespace’ - if o is a DOM whitespace node ‘arguments’ - if o is an arguments object ‘array’ - if o is an object ‘object’ - if o is an object ’string’ - if o is a string ‘number’ - if o is a number ‘boolean’ - if o is a boolean ‘function’ - if o is a function ‘regexp’ - if o is a regular expression ‘date’ - if o is a Date ‘class’ - if o is a Class. (created with new Class, or the extend of another class). ‘collection’ - if o is a native htmlelements collection, such as childNodes, getElementsByTagName .. etc. null - if the object is not defined or none of the above. */ function $type(o){ if (! $defined(o)) {return null;} if (o.htmlElement) {return ‘element’;} var type = typeof o; if (type == ‘object’ && o.nodeName) { switch (o.nodeType) { case 1: return ‘element’; case 3: return (/S/).test(o.nodeValue) ? ‘textnode’ : ‘whitespace’; } } if (type == ‘object’ || type == ‘function’) { switch (o.constructor) { case Array: return ‘array’; case RegExp: return ‘regexp’; case Class: return ‘class’; case Date: return ‘date’; // add additional Object types that you care about here } if (typeof o.length == ‘number’) { if (o.item) {return ‘collection’;} if (o.callee) {return ‘arguments’;} } } return type; }; /** Function: isType Returns true if the Object has the same type as supplied. Arguments: o - the Object to inspect. type - the String name for type Returns: {boolean} */ function isType(o, type) { return type == $type(o); }
posted by Matt Snider at 1:53 pm  

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress