common.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. common.js by tommy
  3. Object:
  4. COMMON.Browser:
  5. COMMON.Browser.ie - (boolean) True if the current browser is Internet Explorer.
  6. COMMON.Browser.firefox - (boolean) True if the current browser is Firefox.
  7. COMMON.Browser.safari - (boolean) True if the current browser is Safari.
  8. COMMON.Browser.chrome - (boolean) True if the current browser is Chrome.
  9. COMMON.Browser.opera - (boolean) True if the current browser is Opera.
  10. COMMON.Browser.Features.xpath - (boolean) True if the browser supports DOM queries using XPath.
  11. COMMON.Browser.Features.air - (boolean) True if the browser supports AIR.
  12. COMMON.Browser.Features.query - (boolean) True if the browser supports querySelectorAll.
  13. COMMON.Browser.Features.json - (boolean) True if the browser has a native JSON object.
  14. COMMON.Browser.Features.xhr - (boolean) True if the browser supports native XMLHTTP object.
  15. COMMON.Browser.Platform.mac - (boolean) True if the platform is Mac.
  16. COMMON.Browser.Platform.win - (boolean) True if the platform is Windows.
  17. COMMON.Browser.Platform.linux - (boolean) True if the platform is Linux.
  18. COMMON.Browser.Platform.ios - (boolean) True if the platform is iOS.
  19. COMMON.Browser.Platform.android - (boolean) True if the platform is Android
  20. COMMON.Browser.Platform.webos - (boolean) True if the platform is WebOS
  21. COMMON.Browser.Platform.other - (boolean) True if the platform is neither Mac, Windows, Linux, Android, WebOS nor iOS.
  22. COMMON.Browser.Platform.name - (string) The name of the platform.
  23. COMMON.Browser.Platform.isMobile - (boolean) True if the platform is mobile os.
  24. COMMON.Browser.Request - (object) The XMLHTTP object or equivalent.
  25. COMMON.Browser.exec(jsText) - (string) Executes the passed in string in the browser context.
  26. COMMON.Class:
  27. COMMON.Class.create(options) - Create new class.
  28. COMMON.JSON:
  29. COMMON.JSON.validate(jsonString) - (boolean) Validate a json String.
  30. COMMON.JSON.encode(object) - (string) transform a object(or array) to json String.
  31. COMMON.JSON.decode(jsonString) - (object) transform json String to object(or array).
  32. COMMON.JSON.get(url, callback, async)
  33. Arguments:
  34. url - The URL to json.
  35. 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;
  36. async - (boolean: defaults to true) If set to false, the requests will be synchronous and freeze the browser during request.
  37. Returns:
  38. object - json object or array
  39. COMMON.XML:
  40. COMMON.XML.get(url, callback, async)
  41. Arguments:
  42. url - The URL to XML.
  43. 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;
  44. async - (boolean: defaults to true) If set to false, the requests will be synchronous and freeze the browser during request.
  45. Returns:
  46. object - json object or array
  47. COMMON.AjaxModule
  48. COMMON.AjaxModule.load(url, callback, async, reload)
  49. Arguments:
  50. url - (string or array) The URL to json.
  51. 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;
  52. async - (boolean: defaults to true) If set to false, the requests will be synchronous and freeze the browser during request.
  53. reload - (boolean: defaults to false) If set to true, Even if the javascript had loaded, the javascript will be load.
  54. COMMON.DOM
  55. COMMON.DOM.addReady(fn) - add function, which executes when the DOM is loaded.
  56. Class:
  57. COMMON.Request:
  58. var options = {
  59. url: "",
  60. async: true,
  61. method: "get",
  62. data: "",
  63. onSuccess: function(responseText, responseXml){
  64. //.....
  65. },
  66. onFailure: function(xhr){
  67. //.........
  68. }
  69. };
  70. var r = new COMMON.Request(options).send();
  71. Options:
  72. url - (string: defaults to null) The URL to request.
  73. async - (boolean: defaults to true) If set to false, the requests will be synchronous and freeze the browser during request.
  74. method - (string: defaults to 'post') The HTTP method for the request, can be either 'post' or 'get'.
  75. encoding - (string: defaults to 'utf-8') The encoding to be set in the request header.
  76. 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.
  77. onSuccess(responseText, responseXML) - Fired when the Request is completed successfully.
  78. onFailure(xhr) - Fired when the request failed (error status code).
  79. Method:
  80. myRequest.send(data) - Opens the Request connection and sends the provided data with the specified options.
  81. myRequest.setHeader(name, value) - Add or modify a header for the request.
  82. myRequest.getHeader(name) - Returns the given response header or null if not found.
  83. Method:
  84. COMMON.typeOf(obj)
  85. Arguments:
  86. obj - (object) The object to inspect.
  87. Returns:
  88. ■ 'element' - (string) If object is a DOM element node.
  89. ■ 'elements' - (string) If object is an instance of Elements.
  90. ■ 'textnode' - (string) If object is a DOM text node.
  91. ■ 'whitespace' - (string) If object is a DOM whitespace node.
  92. ■ 'arguments' - (string) If object is an arguments object.
  93. ■ 'array' - (string) If object is an array.
  94. ■ 'object' - (string) If object is an object.
  95. ■ 'string' - (string) If object is a string.
  96. ■ 'number' - (string) If object is a number.
  97. ■ 'date' - (string) If object is a date.
  98. ■ 'boolean' - (string) If object is a boolean.
  99. ■ 'function' - (string) If object is a function.
  100. ■ 'regexp' - (string) If object is a regular expression.
  101. ■ 'class' - (string) If object is a Class (created with new Class or the extend of another class).
  102. ■ 'collection' - (string) If object is a native HTML elements collection, such as childNodes or getElementsByTagName.
  103. ■ 'window' - (string) If object is the window object.
  104. ■ 'document' - (string) If object is the document object.
  105. ■ 'domevent' - (string) If object is an event.
  106. ■ 'null' - (string) If object is undefined, null, NaN or none of the above.
  107. COMMON.copyPrototype(content, options)
  108. Arguments:
  109. 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.
  110. options - (object)
  111. */
  112. COMMON = window.COMMON || {};
  113. var href = window.location.href;
  114. if (href.indexOf("debugger")!==-1) COMMON["debugger"] = true;
  115. COMMON.contentPath = "";
  116. COMMON.version = "1.0.6";
  117. COMMON.setContentPath = function(path){
  118. COMMON.contentPath = path;
  119. window.CKEDITOR_BASEPATH = path+"/res/framework/htmleditor/ckeditor/";
  120. COMMON.AjaxModule.init();
  121. };
  122. Function.attempt = function(){
  123. for (var i = 0, l = arguments.length; i < l; i++){
  124. try {
  125. return arguments[i]();
  126. } catch (e){}
  127. }
  128. return null;
  129. };
  130. (function(){
  131. var document = this.document;
  132. var window = document.window = this;
  133. var ua = navigator.userAgent.toLowerCase(),
  134. platform = navigator.platform.toLowerCase(),
  135. UA = ua.match(/(opera|ie|firefox|chrome|version)[\s\/:]([\w\d\.]+)?.*?(safari|version[\s\/:]([\w\d\.]+)|$)/) || [null, 'unknown', 0],
  136. mode = UA[1] == 'ie' && document.documentMode;
  137. COMMON.Browser = {
  138. extend: Function.prototype.extend,
  139. name: (UA[1] == 'version') ? UA[3] : UA[1],
  140. version: mode || parseFloat((UA[1] == 'opera' && UA[4]) ? UA[4] : UA[2]),
  141. Platform: {
  142. name: ua.match(/ip(?:ad|od|hone)/) ? 'ios' : (ua.match(/(?:webos|android)/) || ua.match(/(?:windows\sphone)/) || platform.match(/mac|win|linux/) || ['other'])[0]
  143. },
  144. Features: {
  145. xpath: !!(document.evaluate),
  146. air: !!(window.runtime),
  147. query: !!(document.querySelector),
  148. json: !!(window.JSON)
  149. },
  150. Plugins: {}
  151. };
  152. COMMON.Browser[COMMON.Browser.name] = true;
  153. COMMON.Browser[COMMON.Browser.name + parseInt(COMMON.Browser.version, 10)] = true;
  154. COMMON.Browser.Platform[COMMON.Browser.Platform.name] = true;
  155. //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;
  156. COMMON.Browser.Platform.isMobile = !!(COMMON.Browser.Platform.name.match(/(?:ios|webos|android|windows\sphone)/) );
  157. // Request
  158. COMMON.Browser.Request = (function(){
  159. var XMLHTTP = function(){
  160. return new XMLHttpRequest();
  161. };
  162. var MSXML2 = function(){
  163. return new ActiveXObject('MSXML2.XMLHTTP');
  164. };
  165. var MSXML = function(){
  166. return new ActiveXObject('Microsoft.XMLHTTP');
  167. };
  168. return Function.attempt(function(){
  169. XMLHTTP();
  170. return XMLHTTP;
  171. }, function(){
  172. MSXML2();
  173. return MSXML2;
  174. }, function(){
  175. MSXML();
  176. return MSXML;
  177. });
  178. })();
  179. COMMON.Browser.Features.xhr = !!(COMMON.Browser.Request);
  180. // Flash detection
  181. var version = (Function.attempt(function(){
  182. return navigator.plugins['Shockwave Flash'].description;
  183. }, function(){
  184. return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version');
  185. }) || '0 r0').match(/\d+/g);
  186. COMMON.Browser.Plugins.Flash = {
  187. version: Number(version[0] || '0.' + version[1]) || 0,
  188. build: Number(version[2]) || 0
  189. };
  190. // String scripts
  191. COMMON.Browser.exec = function(text){
  192. if (!text) return text;
  193. if (window.execScript){
  194. window.execScript(text);
  195. } else {
  196. var script = document.createElement('script');
  197. script.setAttribute('type', 'text/javascript');
  198. script.text = text;
  199. document.head.appendChild(script);
  200. document.head.removeChild(script);
  201. }
  202. return text;
  203. };
  204. COMMON.typeOf = function(item){
  205. if (item == null) return 'null';
  206. if (item.$family != null) return item.$family();
  207. if (item.constructor == window.Array) return "array";
  208. if (item.nodeName){
  209. if (item.nodeType == 1) return 'element';
  210. if (item.nodeType == 3) return (/\S/).test(item.nodeValue) ? 'textnode' : 'whitespace';
  211. } else if (typeof item.length == 'number'){
  212. if (item.callee) return 'arguments';
  213. //if ('item' in item) return 'collection';
  214. }
  215. return typeof item;
  216. };
  217. })();
  218. if (COMMON.Browser.name=="ie" && COMMON.Browser.version<10){
  219. COMMON["debugger"] = true;
  220. }
  221. COMMON.Class = {
  222. create: function(options) {
  223. var newClass = function() {
  224. this.initialize.apply(this, arguments);
  225. };
  226. COMMON.copyPrototype(newClass, options);
  227. return newClass;
  228. }
  229. };
  230. COMMON.copyPrototype = function (currentNS,props){
  231. if (!props){return currentNS;}
  232. if (!currentNS){return currentNS;}
  233. if ((typeof currentNS).toLowerCase()=="object"){
  234. for (var prop in props){
  235. currentNS[prop] = props[prop];
  236. }
  237. }
  238. if ((typeof currentNS).toLowerCase()=="function"){
  239. for (var prop in props){
  240. currentNS.prototype[prop] = props[prop];
  241. }
  242. }
  243. return currentNS;
  244. };
  245. if ((typeof JSON) == 'undefined'){
  246. COMMON.JSON = {};
  247. }else{
  248. COMMON.JSON = JSON;
  249. }
  250. (function(){
  251. var special = {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'};
  252. var escape = function(chr){
  253. return special[chr] || '\\u' + ('0000' + chr.charCodeAt(0).toString(16)).slice(-4);
  254. };
  255. COMMON.JSON.validate = function(string){
  256. 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, '');
  257. return (/^[\],:{}\s]*$/).test(string);
  258. };
  259. COMMON.JSON.encode = COMMON.JSON.stringify ? function(obj){
  260. return COMMON.JSON.stringify(obj);
  261. } : function(obj){
  262. if (obj && obj.toJSON) obj = obj.toJSON();
  263. switch (COMMON.typeOf(obj)){
  264. case 'string':
  265. return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"';
  266. case 'array':
  267. var string = [];
  268. for (var i=0; i<obj.length; i++){
  269. var json = COMMON.JSON.encode(obj[i]);
  270. if (json) string.push(json);
  271. }
  272. return '[' + string + ']';
  273. case 'object': case 'hash':
  274. var string = [];
  275. for (key in obj){
  276. var json = COMMON.JSON.encode(obj[key]);
  277. if (json) string.push(COMMON.JSON.encode(key) + ':' + json);
  278. }
  279. return '{' + string + '}';
  280. case 'number': case 'boolean': return '' + obj;
  281. case 'null': return 'null';
  282. }
  283. return null;
  284. };
  285. COMMON.JSON.decode = function(string, secure){
  286. if (!string || COMMON.typeOf(string) != 'string') return null;
  287. if (secure || COMMON.JSON.secure){
  288. if (COMMON.JSON.parse) return COMMON.JSON.parse(string);
  289. if (!COMMON.JSON.validate(string)) throw new Error('JSON could not decode the input; security is enabled and the value is not secure.');
  290. }
  291. return eval('(' + string + ')');
  292. };
  293. COMMON.JSON.get = function(url, callback, async){
  294. var jsonObj = null;
  295. var r = new COMMON.Request({
  296. url: url,
  297. async: (async==false) ? false : true,
  298. method: "get",
  299. onSuccess: function(responseText){
  300. jsonObj = COMMON.JSON.decode(responseText);
  301. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  302. callback(jsonObj);
  303. }else{
  304. COMMON.runCallback(callback, "onSuccess", [jsonObj]);
  305. }
  306. },
  307. onFailure: function(xhr){
  308. COMMON.runCallback(callback, "onRequestFailure", [xhr]);
  309. }
  310. });
  311. r.send();
  312. return jsonObj;
  313. };
  314. })();
  315. COMMON.XML = COMMON.Class.create({
  316. initialize: function(xml){
  317. this.xml = xml || null;
  318. this.request = null;
  319. },
  320. get: function(url, callback, async){
  321. var _self = this;
  322. this.request = new COMMON.Request({
  323. url: url,
  324. async: (async!==false),
  325. method: "get",
  326. onSuccess: function(responseText, responseXML){
  327. _self.xml = responseXML;
  328. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  329. callback.apply(_self, [_self]);
  330. }else{
  331. COMMON.runCallback(callback, "onSuccess", [_self]);
  332. }
  333. },
  334. onFailure: function(xhr){
  335. COMMON.runCallback(callback, "onRequestFailure", [_self]);
  336. }
  337. });
  338. this.request.send();
  339. return this;
  340. },
  341. queryNode: function(xpath){
  342. if (COMMON.Browser.Features.xpath){
  343. var xpathResult = this.xml.evaluate(xpath,this.xml,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  344. if (xpathResult.snapshotLength){
  345. return new COMMON.XML.Element(xpathResult.snapshotItem(0));
  346. }else{
  347. return null;
  348. }
  349. }else{
  350. try {
  351. return new COMMON.XML.Element(this.xml.selectSingleNode(xpath));
  352. }catch(e){
  353. try {
  354. xpath = xpath.replace(/\//g, ">");
  355. xpath = xpath.replace(/\\/g, ">");
  356. return new COMMON.XML.Element(this.xml.querySelector(xpath));
  357. }catch(e){
  358. return null;
  359. }
  360. }
  361. }
  362. },
  363. queryNodes: function(xpath){
  364. if (COMMON.Browser.Features.xpath){
  365. var xpathResult = this.xml.evaluate(xpath,this.xml,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  366. if (xpathResult.snapshotLength){
  367. return new COMMON.XML.Elements(xpathResult);
  368. }else{
  369. return null;
  370. }
  371. }else{
  372. try {
  373. return new COMMON.XML.Elements(this.xml.selectNodes(xpath));
  374. }catch(e){
  375. var nodes = [];
  376. xpath = xpath.replace(/\//g, ">");
  377. xpath = xpath.replace(/\\/g, ">");
  378. var firstNode = this.xml.querySelector(xpath);
  379. if (firstNode) nodes.push(firstNode);
  380. var nextNode = firstNode.nextSibling;
  381. while (nextNode){
  382. if (nextNode.tagName == xpath){
  383. nodes.push(nextNode);
  384. }
  385. nextNode = nextNode.nextSibling;
  386. }
  387. return new COMMON.XML.Elements(nodes);
  388. }
  389. }
  390. }
  391. });
  392. COMMON.XML.Element = COMMON.Class.create({
  393. initialize: function(node){
  394. this.node = node;
  395. if (this.node){
  396. for (p in this.node){
  397. this[p] = this.node[p];
  398. }
  399. }
  400. },
  401. queryNode: function(xpath){
  402. if (COMMON.Browser.Features.xpath){
  403. var xpathResult = this.node.ownerDocument.evaluate(xpath,this.node,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  404. if (xpathResult.snapshotLength){
  405. return new COMMON.XML.Element(xpathResult.snapshotItem(0));
  406. }else{
  407. return null;
  408. }
  409. }else{
  410. try {
  411. return new COMMON.XML.Element(this.node.selectSingleNode(xpath));
  412. }catch(e){
  413. try {
  414. xpath = xpath.replace(/\//g, ">");
  415. xpath = xpath.replace(/\\/g, ">");
  416. return new COMMON.XML.Element(this.node.querySelector(xpath));
  417. }catch(e){
  418. return null;
  419. }
  420. }
  421. }
  422. },
  423. queryNodes: function(xpath){
  424. if (COMMON.Browser.Features.xpath){
  425. var xpathResult = this.node.ownerDocument.evaluate(xpath,this.node,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  426. if (xpathResult.snapshotLength){
  427. return new COMMON.XML.Elements(xpathResult);
  428. }else{
  429. return null;
  430. }
  431. }else{
  432. try {
  433. return new COMMON.XML.Elements(this.node.selectNodes(xpath));
  434. }catch(e){
  435. var nodes = [];
  436. xpath = xpath.replace(/\//g, ">");
  437. xpath = xpath.replace(/\\/g, ">");
  438. var firstNode = this.node.querySelector(xpath);
  439. if (firstNode){
  440. nodes.push(firstNode);
  441. var nextNode = firstNode.nextSibling;
  442. while (nextNode){
  443. if (nextNode.tagName == xpath){
  444. nodes.push(nextNode);
  445. }
  446. nextNode = nextNode.nextSibling;
  447. }
  448. }
  449. return new COMMON.XML.Elements(nodes);
  450. }
  451. }
  452. },
  453. text: function(){
  454. try{
  455. var text = this.node.textContent;
  456. if (text){
  457. return text;
  458. }else{
  459. try{
  460. return this.node.text;
  461. }catch(e){
  462. return text;
  463. }
  464. }
  465. }catch(e){
  466. try{
  467. return this.node.text;
  468. }catch(e){
  469. return "";
  470. }
  471. }
  472. },
  473. attr: function(name){
  474. return this.node.getAttribute(name);
  475. }
  476. });
  477. COMMON.XML.Elements = COMMON.Class.create({
  478. initialize: function(nodes){
  479. this.nodes = nodes;
  480. },
  481. length: function(){
  482. if (this.nodes.snapshotLength){
  483. return this.nodes.snapshotLength;
  484. }else{
  485. return this.nodes.length;
  486. }
  487. },
  488. items: function(i){
  489. if (this.nodes.snapshotItem){
  490. return new COMMON.XML.Element(this.nodes.snapshotItem(i));
  491. }else{
  492. try {
  493. return new COMMON.XML.Element(this.nodes.item(i));
  494. }catch(e){
  495. try {
  496. return new COMMON.XML.Element(this.nodes[i]);
  497. }catch(e){
  498. return null;
  499. }
  500. }
  501. }
  502. },
  503. each: function(fun){
  504. if (this.length()){
  505. for (var i=0; i<this.length(); i++){
  506. fun.apply(this, [this.items(i), i]);
  507. }
  508. }
  509. }
  510. });
  511. COMMON.Request = function(o){
  512. this.url = o.url;
  513. if (!o.cache){
  514. if (this.url.indexOf("?")==-1){
  515. this.url = this.url + "?d=" + new String((new Date()).getTime());
  516. }else{
  517. this.url = this.url + "&d=" + new String((new Date()).getTime());
  518. }
  519. }
  520. this.async = (o.async!==false);
  521. this.method = o.method;
  522. this.onSuccess = o.onSuccess;
  523. this.onFailure = o.onFailure;
  524. this.data = o.data;
  525. this.xhr = new COMMON.Browser.Request();
  526. this.headers = {
  527. 'X-Requested-With': 'XMLHttpRequest',
  528. 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
  529. };
  530. this.encoding = 'utf-8';
  531. };
  532. COMMON.Request.prototype.send = function(data){
  533. var xhr = this.xhr;
  534. //if (!this.async) alert(this.url);
  535. xhr.open(this.method.toUpperCase(), this.url, this.async);
  536. var req = this;
  537. if (this.method.toLowerCase() == 'post'){
  538. var encoding = (this.encoding) ? '; charset=' + this.encoding : '';
  539. this.headers['Content-type'] = 'application/x-www-form-urlencoded' + encoding;
  540. }
  541. for (key in this.headers){
  542. try {
  543. xhr.setRequestHeader(key, this.headers[key]);
  544. } catch (e){}
  545. }
  546. var onreadystatechange= function(){
  547. if (xhr.readyState != 4) return;
  548. //xhr.onreadystatechange = null;
  549. var status = xhr.status;
  550. status = (status == 1223) ? 204 : status;
  551. req.response = {text: xhr.responseText || '', xml: xhr.responseXML};
  552. if ((status >= 200 && status < 300))
  553. req.success(req.response.text, req.response.xml);
  554. else if ((status >= 300 && status < 400))
  555. req.redirect(req);
  556. else
  557. req.failure(xhr);
  558. req.async = true;
  559. };
  560. xhr.onreadystatechange = onreadystatechange;
  561. var rdata = data || this.data;
  562. xhr.send(rdata);
  563. //if (!this.async) onreadystatechange();
  564. };
  565. COMMON.Request.prototype.processScripts = function(text){
  566. //if ((/(ecma|java)script/).test(this.getHeader('Content-type'))) return Browser.exec(text);
  567. return text;
  568. };
  569. COMMON.Request.prototype.success = function(text, xml){
  570. var t = this.processScripts(text);
  571. if (this.onSuccess) this.onSuccess(t, xml);
  572. };
  573. COMMON.Request.prototype.failure = function(xhr){
  574. if (this.onFailure) this.onFailure(xhr);
  575. };
  576. COMMON.Request.prototype.redirect = function(xhr){
  577. if (this.onRedirect) this.onRedirect(xhr);
  578. };
  579. COMMON.Request.prototype.getHeader = function(name){
  580. return this.xhr.getResponseHeader(name);
  581. };
  582. COMMON.Request.prototype.setHeader = function(name, value){
  583. this.headers[name] = value;
  584. return this;
  585. };
  586. COMMON.runCallback = function(callback, name, par){
  587. if (COMMON.typeOf(callback).toLowerCase()=='object'){
  588. if (callback[name]){
  589. callback[name].apply(callback, ((par) ? par : []));
  590. }
  591. }
  592. };
  593. COMMON.Request.prototype.getText = function(url){
  594. var v = "";
  595. var r = new COMMON.Request({
  596. "url": url,
  597. "async": false,
  598. "method": "get",
  599. "onSuccess": function(text){
  600. v = text;
  601. }
  602. }).send();
  603. return v;
  604. };
  605. COMMON.AjaxModule = {
  606. "jquery": COMMON.contentPath+"/res/framework/jquery/jquery.min.js",
  607. "jquery-ui": COMMON.contentPath+"/res/framework/jquery/jquery-ui.custom/js/jquery-ui.custom.min.js",
  608. "mootools": COMMON.contentPath+"/res/framework/mootools/mootools-1.6.0.js",
  609. "mootools-more": COMMON.contentPath+"/res/framework/mootools/mootools-1.6.0.js",
  610. "mwf": COMMON.contentPath+"/res/mwf4/MWF.min.js",
  611. "ckeditor": COMMON.contentPath+"/res/framework/htmleditor/ckeditor451/ckeditor.js",
  612. "nicEdit": COMMON.contentPath+"/res/framework/htmleditor/nicEdit/nicEdit.js",
  613. "kindeditor": COMMON.contentPath+"/res/framework/htmleditor/kindeditor/kindeditor-all-min.js",
  614. "tinymce": COMMON.contentPath+"/res/framework/htmleditor/tinymce/tinymce.min.js",
  615. "bmap": "http://api.map.baidu.com/api?v=2.0&ak=fpzGqedB7e8CA8WB8jUgxlOx",
  616. "amap": "http://webapi.amap.com/maps?v=1.3&key=d7c7ce03a219fa4df4c7a30ba8e68888",
  617. "raphael": COMMON.contentPath+"/res/framework/raphael/raphael.js",
  618. "codemirror": COMMON.contentPath+"/res/framework/codemirror/lib/codemirror.js",
  619. "codemirror_javascript": COMMON.contentPath+"/res/framework/codemirror/mode/javascript/javascript.js",
  620. "d3": COMMON.contentPath+"/res/framework/d3/d3.min.js",
  621. "ace": COMMON.contentPath+"/res/framework/ace/src-noconflict/ace.js",
  622. "ace-tools": COMMON.contentPath+"/res/framework/ace/src-noconflict/ext-language_tools.js",
  623. "kity": COMMON.contentPath+"/res/framework/kityminder/kity/kity.min.js",
  624. "kityminder": COMMON.contentPath+"/res/framework/kityminder/core/dist/kityminder.core.js",
  625. "JSONTemplate": COMMON.contentPath+"/res/framework/mootools/plugin/Template.js",
  626. "ie_adapter": COMMON.contentPath+"/res/mwf4/ie_adapter.js",
  627. //"loaded": [],
  628. //"loadedCss": [],
  629. init: function(){
  630. this["jquery"] = COMMON.contentPath+"/res/framework/jquery/jquery.min.js";
  631. this["jquery-ui"] = COMMON.contentPath+"/res/framework/jquery/jquery-ui.custom/js/jquery-ui.custom.min.js";
  632. this["mootools"] = COMMON.contentPath+"/res/framework/mootools/mootools-1.6.0.js";
  633. this["mootools-more"] = COMMON.contentPath+"/res/framework/mootools/mootools-1.6.0.js";
  634. this["mwf"] = COMMON.contentPath+"/res/mwf4/MWF.js";
  635. this["ckeditor"] = COMMON.contentPath+"/res/framework/htmleditor/ckeditor451/ckeditor.js";
  636. this["nicEdit"] = COMMON.contentPath+"/res/framework/htmleditor/nicEdit/nicEdit.js";
  637. this["kindeditor"] = COMMON.contentPath+"/res/framework/htmleditor/kindeditor/kindeditor.js";
  638. this["tinymce"] = COMMON.contentPath+"/res/framework/htmleditor/tinymce/tinymce.full.js";
  639. this["bmap"] = "http://api.map.baidu.com/api?v=2.0&ak=fpzGqedB7e8CA8WB8jUgxlOx";
  640. this["amap"] = "http://webapi.amap.com/maps?v=1.3&key=d7c7ce03a219fa4df4c7a30ba8e68888";
  641. this["raphael"] = COMMON.contentPath+"/res/framework/raphael/raphael.js";
  642. this["codemirror"] = COMMON.contentPath+"/res/framework/codemirror/lib/codemirror.js";
  643. this["codemirror_javascript"] = COMMON.contentPath+"/res/framework/codemirror/mode/javascript/javascript.js";
  644. this["d3"] = COMMON.contentPath+"/res/framework/d3/d3.min.js";
  645. this["ace"] = COMMON.contentPath+"/res/framework/ace/src-noconflict/ace.js";
  646. this["ace-tools"] = COMMON.contentPath+"/res/framework/ace/src-noconflict/ext-language_tools.js";
  647. this["kity"] = COMMON.contentPath+"/res/framework/kityminder/kity/kity.min.js";
  648. this["kityminder"] = COMMON.contentPath+"/res/framework/kityminder/core/dist/kityminder.core.js";
  649. this["JSONTemplate"] = COMMON.contentPath+"/res/framework/mootools/plugin/Template.js";
  650. this["ie_adapter"] = COMMON.contentPath+"/res/mwf4/ie_adapter.js";
  651. },
  652. "loaded": {},
  653. "loadedCss": {},
  654. loadDom: function(urls, callback, async, reload){
  655. if (COMMON.typeOf(urls)==="array"){
  656. var thisLoaded = [];
  657. urls.each(function(url){
  658. this.loadDomSingle(url, function(){
  659. thisLoaded.push(url);
  660. if (thisLoaded.length===urls.length){
  661. if (COMMON.typeOf(callback).toLowerCase() === 'function'){
  662. callback();
  663. }else{
  664. COMMON.runCallback(callback, "onSuccess");
  665. }
  666. }
  667. }, async, reload);
  668. }.bind(this));
  669. }
  670. if (COMMON.typeOf(urls)==="string"){
  671. this.loadDomSingle(urls, callback, async, reload);
  672. }
  673. },
  674. loadDomSingle: function(url, callback, async, reload){
  675. //var jsurl = this[url] || url;
  676. var jsurl = this[url];
  677. if (!jsurl){
  678. jsurl = url;
  679. if (!COMMON["debugger"]){
  680. if (jsurl.indexOf("framework")===-1) jsurl = jsurl.replace(/\.js/, ".min.js");
  681. }
  682. }
  683. jsurl = (jsurl.indexOf("?")!==-1) ? jsurl+"&v="+COMMON.version : jsurl+"?v="+COMMON.version;
  684. if (!reload){
  685. if (this.loaded[key]){
  686. if (COMMON.typeOf(callback).toLowerCase() === 'function'){
  687. callback();
  688. }else{
  689. COMMON.runCallback(callback, "onSuccess");
  690. }
  691. return;
  692. }
  693. }
  694. var key = encodeURIComponent(url);
  695. var head = (document.head || document.getElementsByTagName("head")[0] || document.documentElement);
  696. var s = document.createElement('script');
  697. s.src = jsurl;
  698. head.appendChild(s);
  699. s.onload = s.onreadystatechange = function(_, isAbort) {
  700. if (isAbort || !s.readyState || s.readyState === "loaded" || s.readyState === "complete") {
  701. COMMON.AjaxModule.loaded[key] = true;
  702. s = s.onload = s.onreadystatechange = null;
  703. if (!isAbort)
  704. callback();
  705. }
  706. };
  707. },
  708. load: function(urls, callback, async, reload){
  709. if (COMMON.typeOf(urls)==="array"){
  710. var thisLoaded = [];
  711. urls.each(function(url){
  712. this.loadSingle(url, function(){
  713. thisLoaded.push(url);
  714. if (thisLoaded.length===urls.length){
  715. if (COMMON.typeOf(callback).toLowerCase() === 'function'){
  716. callback();
  717. }else{
  718. COMMON.runCallback(callback, "onSuccess");
  719. }
  720. }
  721. }, async, reload);
  722. }.bind(this));
  723. }
  724. if (COMMON.typeOf(urls)==="string"){
  725. this.loadSingle(urls, callback, async, reload);
  726. }
  727. },
  728. loadSingle: function(url, callback, async, reload){
  729. var jsurl = this[url];
  730. if (!jsurl){
  731. jsurl = url;
  732. if (!COMMON["debugger"]){
  733. jsurl = jsurl.replace(/\.js/, ".min.js");
  734. }
  735. }
  736. jsurl = (jsurl.indexOf("?")!=-1) ? jsurl+"&v="+COMMON.version : jsurl+"?v="+COMMON.version;
  737. var key = encodeURIComponent(url);
  738. if (!reload){
  739. if (this.loaded[key]){
  740. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  741. callback();
  742. }else{
  743. COMMON.runCallback(callback, "onSuccess");
  744. }
  745. return;
  746. }
  747. }
  748. var loadAsync = true;
  749. if (async===false){
  750. loadAsync = false;
  751. }
  752. var r = new COMMON.Request({
  753. url: jsurl,
  754. async: loadAsync,
  755. method: "get",
  756. cache: true,
  757. onSuccess: function(responseText, responseXML){
  758. var jsText = responseText;
  759. try{
  760. COMMON.Browser.exec(jsText);
  761. // COMMON.AjaxModule.loaded.push(url);
  762. COMMON.AjaxModule.loaded[key] = true;
  763. }catch (e){
  764. COMMON.runCallback(callback, "onFailure", e);
  765. return;
  766. }
  767. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  768. callback();
  769. }else{
  770. COMMON.runCallback(callback, "onSuccess");
  771. }
  772. },
  773. onFailure: function(xhr){
  774. COMMON.runCallback(callback, "onRequestFailure", xhr);
  775. }
  776. });
  777. r.send();
  778. },
  779. loadCss: function(urls, callback, async, reload, sourceDoc){
  780. if (COMMON.typeOf(urls)=="array"){
  781. var thisLoaded = [];
  782. urls.each(function(url){
  783. this.loadSingleCss(url, function(){
  784. thisLoaded.push(url);
  785. if (thisLoaded.length==urls.length){
  786. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  787. callback();
  788. }else{
  789. COMMON.runCallback(callback, "onSuccess");
  790. }
  791. }
  792. }, async, reload);
  793. }.bind(this));
  794. }
  795. if (COMMON.typeOf(urls)=="string"){
  796. this.loadSingleCss(urls, callback, async, reload);
  797. }
  798. },
  799. loadSingleCss: function(url, callback, async, reload, sourceDoc){
  800. var key = encodeURIComponent(url);
  801. if (!reload){
  802. if (this.loadedCss[key]){
  803. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  804. callback();
  805. }else{
  806. COMMON.runCallback(callback, "onSuccess");
  807. }
  808. return;
  809. };
  810. }
  811. var async = true;
  812. if (async===false){
  813. async = false;
  814. }
  815. var cssurl = this[url] || url;
  816. var r = new COMMON.Request({
  817. url: cssurl,
  818. async: async,
  819. method: "get",
  820. cache: true,
  821. onSuccess: function(responseText, responseXML){
  822. var cssText = responseText;
  823. try{
  824. if (cssText){
  825. var doc = sourceDoc || document;
  826. var style = doc.createElement("style");
  827. style.setAttribute("type", "text/css");
  828. var head = doc.getElementsByTagName("head")[0];
  829. head.appendChild(style);
  830. if(style.styleSheet){
  831. var setFunc = function(){
  832. style.styleSheet.cssText = cssText;
  833. };
  834. if(style.styleSheet.disabled){
  835. setTimeout(setFunc, 10);
  836. }else{
  837. setFunc();
  838. }
  839. }else{
  840. var cssTextNode = doc.createTextNode(cssText);
  841. style.appendChild(cssTextNode);
  842. }
  843. }
  844. COMMON.AjaxModule.loadedCss[key] = true;
  845. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  846. callback();
  847. }else{
  848. COMMON.runCallback(callback, "onSuccess");
  849. }
  850. }catch (e){
  851. COMMON.runCallback(callback, "onFailure", e);
  852. return;
  853. }
  854. },
  855. onFailure: function(xhr){
  856. COMMON.runCallback(callback, "onRequestFailure", xhr);
  857. }
  858. });
  859. r.send();
  860. }
  861. };
  862. COMMON.onReady = function(){
  863. return true;
  864. };
  865. COMMON.DOM = {
  866. ready: false,
  867. loaded: false,
  868. checks: [],
  869. shouldPoll: false,
  870. timer: null,
  871. testElement: document.createElement('div'),
  872. readys: [],
  873. domready: function(){
  874. clearTimeout(this.timer);
  875. if (COMMON.DOM.ready) return;
  876. COMMON.DOM.loaded = COMMON.DOM.ready = true;
  877. COMMON.DOM.removeListener(document, 'DOMContentLoaded', COMMON.DOM.domready);
  878. COMMON.DOM.removeListener(document, 'readystatechange', COMMON.DOM.check);
  879. COMMON.DOM.onReady();
  880. },
  881. check: function(){
  882. for (var i = COMMON.DOM.checks.length; i--;) if (COMMON.DOM.checks[i]()){
  883. COMMON.DOM.domready();
  884. return true;
  885. }
  886. return false;
  887. },
  888. poll: function(){
  889. clearTimeout(COMMON.DOM.timer);
  890. if (!COMMON.DOM.check()) COMMON.DOM.timer = setTimeout(COMMON.DOM.poll, 10);
  891. },
  892. /*<ltIE8>*/
  893. // doScroll technique by Diego Perini http://javascript.nwbox.com/IEContentLoaded/
  894. // testElement.doScroll() throws when the DOM is not ready, only in the top window
  895. doScrollWorks: function(){
  896. try {
  897. this.testElement.doScroll();
  898. return true;
  899. } catch (e){}
  900. return false;
  901. },
  902. /*</ltIE8>*/
  903. addListener: function(dom, type, fn){
  904. if (type == 'unload'){
  905. var old = fn, self = this;
  906. fn = function(){
  907. self.removeListener(dom, 'unload', fn);
  908. old();
  909. };
  910. }
  911. if (dom.addEventListener) dom.addEventListener(type, fn, !!arguments[2]);
  912. else dom.attachEvent('on' + type, fn);
  913. },
  914. removeListener: function(dom, type, fn){
  915. if (dom.removeEventListener) dom.removeEventListener(type, fn, !!arguments[2]);
  916. else dom.detachEvent('on' + type, fn);
  917. return this;
  918. },
  919. onReady: function(){
  920. for (var i=0; i<COMMON.DOM.readys.length; i++){
  921. COMMON.DOM.readys[i].apply(COMMON);
  922. }
  923. },
  924. addReady: function(fn){
  925. if (COMMON.DOM.loaded){
  926. if (fn) fn.apply(COMMON);
  927. }else{
  928. if (fn) COMMON.DOM.readys.push(fn);
  929. }
  930. return COMMON.DOM;
  931. }
  932. };
  933. (function(window, document){
  934. COMMON.DOM.addListener(document, 'DOMContentLoaded', COMMON.DOM.domready);
  935. /*<ltIE8>*/
  936. // If doScroll works already, it can't be used to determine domready
  937. // e.g. in an iframe
  938. if (COMMON.DOM.testElement.doScroll && !COMMON.DOM.doScrollWorks()){
  939. COMMON.DOM.checks.push(COMMON.DOM.doScrollWorks);
  940. COMMON.DOM.shouldPoll = true;
  941. }
  942. /*</ltIE8>*/
  943. if (document.readyState) COMMON.DOM.checks.push(function(){
  944. var state = document.readyState;
  945. return (state == 'loaded' || state == 'complete');
  946. });
  947. if ('onreadystatechange' in document) COMMON.DOM.addListener(document, 'readystatechange', COMMON.DOM.check);
  948. else COMMON.DOM.shouldPoll = true;
  949. if (COMMON.DOM.shouldPoll) COMMON.DOM.poll();
  950. })(window, document);
  951. (function(){
  952. Array.prototype.arrayIndexOf = function (o){
  953. for (var i=0; i<this.length; i++){
  954. if (this[i]==o){
  955. return i
  956. }
  957. }
  958. return null;
  959. };
  960. Array.prototype.arrayLastIndexOf = function (o){
  961. for (var i=this.length-1; i>=0; i--){
  962. if (this[i]==o){
  963. return i
  964. }
  965. }
  966. return null;
  967. };
  968. Array.prototype.arrayInStr = function (str){
  969. var returnArray = [];
  970. if (str.toString()!=""){
  971. for (var i=0; i<this.length; i++){
  972. if (this[i].toString().indexOf(str.toString())!=-1){
  973. returnArray.push(this[i])
  974. }
  975. }
  976. }
  977. return returnArray;
  978. };
  979. Array.prototype.isIntersection = function (arr){
  980. if (arr){
  981. for (var i=0; i<arr.length; i++){
  982. if (this.arrayIndexOf(arr[i])!=null){
  983. return true;
  984. }
  985. }
  986. }
  987. return false;
  988. };
  989. })();
  990. COMMON.getUserCN = function(name){
  991. var userName = name;
  992. userName = userName.substr(3, userName.length);
  993. userName = userName.substr(0,userName.indexOf("/"));
  994. return userName;
  995. };
  996. COMMON.getNowDateString = function (){
  997. var now = new Date();
  998. m = now.getMonth()+1;
  999. return now.getYear()+"年"+m+"月"+now.getDate()+"日 "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
  1000. };