/* common.js by tommy Object: COMMON.Browser: COMMON.Browser.ie - (boolean) True if the current browser is Internet Explorer. COMMON.Browser.firefox - (boolean) True if the current browser is Firefox. COMMON.Browser.safari - (boolean) True if the current browser is Safari. COMMON.Browser.chrome - (boolean) True if the current browser is Chrome. COMMON.Browser.opera - (boolean) True if the current browser is Opera. COMMON.Browser.Features.xpath - (boolean) True if the browser supports DOM queries using XPath. COMMON.Browser.Features.air - (boolean) True if the browser supports AIR. COMMON.Browser.Features.query - (boolean) True if the browser supports querySelectorAll. COMMON.Browser.Features.json - (boolean) True if the browser has a native JSON object. COMMON.Browser.Features.xhr - (boolean) True if the browser supports native XMLHTTP object. COMMON.Browser.Platform.mac - (boolean) True if the platform is Mac. COMMON.Browser.Platform.win - (boolean) True if the platform is Windows. COMMON.Browser.Platform.linux - (boolean) True if the platform is Linux. COMMON.Browser.Platform.ios - (boolean) True if the platform is iOS. COMMON.Browser.Platform.android - (boolean) True if the platform is Android COMMON.Browser.Platform.webos - (boolean) True if the platform is WebOS COMMON.Browser.Platform.other - (boolean) True if the platform is neither Mac, Windows, Linux, Android, WebOS nor iOS. COMMON.Browser.Platform.name - (string) The name of the platform. COMMON.Browser.Platform.isMobile - (boolean) True if the platform is mobile os. COMMON.Browser.Request - (object) The XMLHTTP object or equivalent. COMMON.Browser.exec(jsText) - (string) Executes the passed in string in the browser context. COMMON.Class: COMMON.Class.create(options) - Create new class. COMMON.JSON: COMMON.JSON.validate(jsonString) - (boolean) Validate a json String. COMMON.JSON.encode(object) - (string) transform a object(or array) to json String. COMMON.JSON.decode(jsonString) - (object) transform json String to object(or array). COMMON.JSON.get(url, callback, async) Arguments: url - The URL to json. callback - (object or function) If function, Fired when the Request is completed successfully; If object, {onSuccess: function(){}, onRequestFailure: function(){}}, "onSuccess" will fired when the Request is completed successfully, "onRequestFailure" will fired when the request failed; async - (boolean: defaults to true) If set to false, the requests will be synchronous and freeze the browser during request. Returns: object - json object or array COMMON.XML: COMMON.XML.get(url, callback, async) Arguments: url - The URL to XML. callback - (object or function) If function, Fired when the Request is completed successfully; If object, {onSuccess: function(){}, onRequestFailure: function(){}}, "onSuccess" will fired when the Request is completed successfully, "onRequestFailure" will fired when the request failed; async - (boolean: defaults to true) If set to false, the requests will be synchronous and freeze the browser during request. Returns: object - json object or array COMMON.AjaxModule COMMON.AjaxModule.load(url, callback, async, reload) Arguments: url - (string or array) The URL to json. callback - (object or function) If function, Fired when the Request is completed successfully; If object, {onSuccess: function(){}, onRequestFailure: function(){}}, "onSuccess" will fired when the Request is completed successfully, "onRequestFailure" will fired when the request failed; async - (boolean: defaults to true) If set to false, the requests will be synchronous and freeze the browser during request. reload - (boolean: defaults to false) If set to true, Even if the javascript had loaded, the javascript will be load. COMMON.DOM COMMON.DOM.addReady(fn) - add function, which executes when the DOM is loaded. Class: COMMON.Request: var options = { url: "", async: true, method: "get", data: "", onSuccess: function(responseText, responseXml){ //..... }, onFailure: function(xhr){ //......... } }; var r = new COMMON.Request(options).send(); Options: url - (string: defaults to null) The URL to request. async - (boolean: defaults to true) If set to false, the requests will be synchronous and freeze the browser during request. method - (string: defaults to 'post') The HTTP method for the request, can be either 'post' or 'get'. encoding - (string: defaults to 'utf-8') The encoding to be set in the request header. data - (mixed: defaults to '') The default data for Request:send, used when no data is given. Can be an Element, Object or String. If an Object is passed the Object:toQueryString method will be used to convert the object to a string. If an Element is passed the Element:toQueryString method will be used to convert the Element to a string. onSuccess(responseText, responseXML) - Fired when the Request is completed successfully. onFailure(xhr) - Fired when the request failed (error status code). Method: myRequest.send(data) - Opens the Request connection and sends the provided data with the specified options. myRequest.setHeader(name, value) - Add or modify a header for the request. myRequest.getHeader(name) - Returns the given response header or null if not found. Method: COMMON.typeOf(obj) Arguments: obj - (object) The object to inspect. Returns: ■ 'element' - (string) If object is a DOM element node. ■ 'elements' - (string) If object is an instance of Elements. ■ 'textnode' - (string) If object is a DOM text node. ■ 'whitespace' - (string) If object is a DOM whitespace node. ■ 'arguments' - (string) If object is an arguments object. ■ 'array' - (string) If object is an array. ■ 'object' - (string) If object is an object. ■ 'string' - (string) If object is a string. ■ 'number' - (string) If object is a number. ■ 'date' - (string) If object is a date. ■ 'boolean' - (string) If object is a boolean. ■ 'function' - (string) If object is a function. ■ 'regexp' - (string) If object is a regular expression. ■ 'class' - (string) If object is a Class (created with new Class or the extend of another class). ■ 'collection' - (string) If object is a native HTML elements collection, such as childNodes or getElementsByTagName. ■ 'window' - (string) If object is the window object. ■ 'document' - (string) If object is the document object. ■ 'domevent' - (string) If object is an event. ■ 'null' - (string) If object is undefined, null, NaN or none of the above. COMMON.copyPrototype(content, options) Arguments: content - (object or function) If object, options's property will by copy to content; If function, options's property will by copy to content's prototype. options - (object) */ COMMON = window.COMMON || {}; var href = window.location.href; if (href.indexOf("debugger")!==-1) COMMON["debugger"] = true; COMMON.contentPath = ""; COMMON.version = "1.0.6"; COMMON.setContentPath = function(path){ COMMON.contentPath = path; window.CKEDITOR_BASEPATH = path+"/res/framework/htmleditor/ckeditor/"; COMMON.AjaxModule.init(); }; Function.attempt = function(){ for (var i = 0, l = arguments.length; i < l; i++){ try { return arguments[i](); } catch (e){} } return null; }; (function(){ var document = this.document; var window = document.window = this; var ua = navigator.userAgent.toLowerCase(), platform = navigator.platform.toLowerCase(), UA = ua.match(/(opera|ie|firefox|chrome|version)[\s\/:]([\w\d\.]+)?.*?(safari|version[\s\/:]([\w\d\.]+)|$)/) || [null, 'unknown', 0], mode = UA[1] == 'ie' && document.documentMode; COMMON.Browser = { extend: Function.prototype.extend, name: (UA[1] == 'version') ? UA[3] : UA[1], version: mode || parseFloat((UA[1] == 'opera' && UA[4]) ? UA[4] : UA[2]), Platform: { name: ua.match(/ip(?:ad|od|hone)/) ? 'ios' : (ua.match(/(?:webos|android)/) || ua.match(/(?:windows\sphone)/) || platform.match(/mac|win|linux/) || ['other'])[0] }, Features: { xpath: !!(document.evaluate), air: !!(window.runtime), query: !!(document.querySelector), json: !!(window.JSON) }, Plugins: {} }; COMMON.Browser[COMMON.Browser.name] = true; COMMON.Browser[COMMON.Browser.name + parseInt(COMMON.Browser.version, 10)] = true; COMMON.Browser.Platform[COMMON.Browser.Platform.name] = true; //COMMON.Browser.Platform.isMobile = (COMMON.Browser.Platform.name.match(/(?:ios|webos|android|windows\sphone)/) || COMMON.Browser.chrome || COMMON.Browser.safari || COMMON.Browser.firefox) ? true : false; COMMON.Browser.Platform.isMobile = !!(COMMON.Browser.Platform.name.match(/(?:ios|webos|android|windows\sphone)/) ); // Request COMMON.Browser.Request = (function(){ var XMLHTTP = function(){ return new XMLHttpRequest(); }; var MSXML2 = function(){ return new ActiveXObject('MSXML2.XMLHTTP'); }; var MSXML = function(){ return new ActiveXObject('Microsoft.XMLHTTP'); }; return Function.attempt(function(){ XMLHTTP(); return XMLHTTP; }, function(){ MSXML2(); return MSXML2; }, function(){ MSXML(); return MSXML; }); })(); COMMON.Browser.Features.xhr = !!(COMMON.Browser.Request); // Flash detection var version = (Function.attempt(function(){ return navigator.plugins['Shockwave Flash'].description; }, function(){ return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version'); }) || '0 r0').match(/\d+/g); COMMON.Browser.Plugins.Flash = { version: Number(version[0] || '0.' + version[1]) || 0, build: Number(version[2]) || 0 }; // String scripts COMMON.Browser.exec = function(text){ if (!text) return text; if (window.execScript){ window.execScript(text); } else { var script = document.createElement('script'); script.setAttribute('type', 'text/javascript'); script.text = text; document.head.appendChild(script); document.head.removeChild(script); } return text; }; COMMON.typeOf = function(item){ if (item == null) return 'null'; if (item.$family != null) return item.$family(); if (item.constructor == window.Array) return "array"; if (item.nodeName){ if (item.nodeType == 1) return 'element'; if (item.nodeType == 3) return (/\S/).test(item.nodeValue) ? 'textnode' : 'whitespace'; } else if (typeof item.length == 'number'){ if (item.callee) return 'arguments'; //if ('item' in item) return 'collection'; } return typeof item; }; })(); if (COMMON.Browser.name=="ie" && COMMON.Browser.version<10){ COMMON["debugger"] = true; } COMMON.Class = { create: function(options) { var newClass = function() { this.initialize.apply(this, arguments); }; COMMON.copyPrototype(newClass, options); return newClass; } }; COMMON.copyPrototype = function (currentNS,props){ if (!props){return currentNS;} if (!currentNS){return currentNS;} if ((typeof currentNS).toLowerCase()=="object"){ for (var prop in props){ currentNS[prop] = props[prop]; } } if ((typeof currentNS).toLowerCase()=="function"){ for (var prop in props){ currentNS.prototype[prop] = props[prop]; } } return currentNS; }; if ((typeof JSON) == 'undefined'){ COMMON.JSON = {}; }else{ COMMON.JSON = JSON; } (function(){ var special = {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'}; var escape = function(chr){ return special[chr] || '\\u' + ('0000' + chr.charCodeAt(0).toString(16)).slice(-4); }; COMMON.JSON.validate = function(string){ string = string.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''); return (/^[\],:{}\s]*$/).test(string); }; COMMON.JSON.encode = COMMON.JSON.stringify ? function(obj){ return COMMON.JSON.stringify(obj); } : function(obj){ if (obj && obj.toJSON) obj = obj.toJSON(); switch (COMMON.typeOf(obj)){ case 'string': return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"'; case 'array': var string = []; for (var i=0; i"); xpath = xpath.replace(/\\/g, ">"); return new COMMON.XML.Element(this.xml.querySelector(xpath)); }catch(e){ return null; } } } }, queryNodes: function(xpath){ if (COMMON.Browser.Features.xpath){ var xpathResult = this.xml.evaluate(xpath,this.xml,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null); if (xpathResult.snapshotLength){ return new COMMON.XML.Elements(xpathResult); }else{ return null; } }else{ try { return new COMMON.XML.Elements(this.xml.selectNodes(xpath)); }catch(e){ var nodes = []; xpath = xpath.replace(/\//g, ">"); xpath = xpath.replace(/\\/g, ">"); var firstNode = this.xml.querySelector(xpath); if (firstNode) nodes.push(firstNode); var nextNode = firstNode.nextSibling; while (nextNode){ if (nextNode.tagName == xpath){ nodes.push(nextNode); } nextNode = nextNode.nextSibling; } return new COMMON.XML.Elements(nodes); } } } }); COMMON.XML.Element = COMMON.Class.create({ initialize: function(node){ this.node = node; if (this.node){ for (p in this.node){ this[p] = this.node[p]; } } }, queryNode: function(xpath){ if (COMMON.Browser.Features.xpath){ var xpathResult = this.node.ownerDocument.evaluate(xpath,this.node,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null); if (xpathResult.snapshotLength){ return new COMMON.XML.Element(xpathResult.snapshotItem(0)); }else{ return null; } }else{ try { return new COMMON.XML.Element(this.node.selectSingleNode(xpath)); }catch(e){ try { xpath = xpath.replace(/\//g, ">"); xpath = xpath.replace(/\\/g, ">"); return new COMMON.XML.Element(this.node.querySelector(xpath)); }catch(e){ return null; } } } }, queryNodes: function(xpath){ if (COMMON.Browser.Features.xpath){ var xpathResult = this.node.ownerDocument.evaluate(xpath,this.node,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null); if (xpathResult.snapshotLength){ return new COMMON.XML.Elements(xpathResult); }else{ return null; } }else{ try { return new COMMON.XML.Elements(this.node.selectNodes(xpath)); }catch(e){ var nodes = []; xpath = xpath.replace(/\//g, ">"); xpath = xpath.replace(/\\/g, ">"); var firstNode = this.node.querySelector(xpath); if (firstNode){ nodes.push(firstNode); var nextNode = firstNode.nextSibling; while (nextNode){ if (nextNode.tagName == xpath){ nodes.push(nextNode); } nextNode = nextNode.nextSibling; } } return new COMMON.XML.Elements(nodes); } } }, text: function(){ try{ var text = this.node.textContent; if (text){ return text; }else{ try{ return this.node.text; }catch(e){ return text; } } }catch(e){ try{ return this.node.text; }catch(e){ return ""; } } }, attr: function(name){ return this.node.getAttribute(name); } }); COMMON.XML.Elements = COMMON.Class.create({ initialize: function(nodes){ this.nodes = nodes; }, length: function(){ if (this.nodes.snapshotLength){ return this.nodes.snapshotLength; }else{ return this.nodes.length; } }, items: function(i){ if (this.nodes.snapshotItem){ return new COMMON.XML.Element(this.nodes.snapshotItem(i)); }else{ try { return new COMMON.XML.Element(this.nodes.item(i)); }catch(e){ try { return new COMMON.XML.Element(this.nodes[i]); }catch(e){ return null; } } } }, each: function(fun){ if (this.length()){ for (var i=0; i*/ // doScroll technique by Diego Perini http://javascript.nwbox.com/IEContentLoaded/ // testElement.doScroll() throws when the DOM is not ready, only in the top window doScrollWorks: function(){ try { this.testElement.doScroll(); return true; } catch (e){} return false; }, /**/ addListener: function(dom, type, fn){ if (type == 'unload'){ var old = fn, self = this; fn = function(){ self.removeListener(dom, 'unload', fn); old(); }; } if (dom.addEventListener) dom.addEventListener(type, fn, !!arguments[2]); else dom.attachEvent('on' + type, fn); }, removeListener: function(dom, type, fn){ if (dom.removeEventListener) dom.removeEventListener(type, fn, !!arguments[2]); else dom.detachEvent('on' + type, fn); return this; }, onReady: function(){ for (var i=0; i*/ // If doScroll works already, it can't be used to determine domready // e.g. in an iframe if (COMMON.DOM.testElement.doScroll && !COMMON.DOM.doScrollWorks()){ COMMON.DOM.checks.push(COMMON.DOM.doScrollWorks); COMMON.DOM.shouldPoll = true; } /**/ if (document.readyState) COMMON.DOM.checks.push(function(){ var state = document.readyState; return (state == 'loaded' || state == 'complete'); }); if ('onreadystatechange' in document) COMMON.DOM.addListener(document, 'readystatechange', COMMON.DOM.check); else COMMON.DOM.shouldPoll = true; if (COMMON.DOM.shouldPoll) COMMON.DOM.poll(); })(window, document); (function(){ Array.prototype.arrayIndexOf = function (o){ for (var i=0; i=0; i--){ if (this[i]==o){ return i } } return null; }; Array.prototype.arrayInStr = function (str){ var returnArray = []; if (str.toString()!=""){ for (var i=0; i