common.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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 = "0.3.17";
  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)/) ) ? true : false;
  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. COMMON.Class = {
  219. create: function(options) {
  220. var newClass = function() {
  221. this.initialize.apply(this, arguments);
  222. };
  223. COMMON.copyPrototype(newClass, options);
  224. return newClass;
  225. }
  226. };
  227. COMMON.copyPrototype = function (currentNS,props){
  228. if (!props){return currentNS;}
  229. if (!currentNS){return currentNS;}
  230. if ((typeof currentNS).toLowerCase()=="object"){
  231. for (var prop in props){
  232. currentNS[prop] = props[prop];
  233. }
  234. }
  235. if ((typeof currentNS).toLowerCase()=="function"){
  236. for (var prop in props){
  237. currentNS.prototype[prop] = props[prop];
  238. }
  239. }
  240. return currentNS;
  241. };
  242. if ((typeof JSON) == 'undefined'){
  243. COMMON.JSON = {};
  244. }else{
  245. COMMON.JSON = JSON;
  246. };
  247. (function(){
  248. var special = {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'};
  249. var escape = function(chr){
  250. return special[chr] || '\\u' + ('0000' + chr.charCodeAt(0).toString(16)).slice(-4);
  251. };
  252. COMMON.JSON.validate = function(string){
  253. 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, '');
  254. return (/^[\],:{}\s]*$/).test(string);
  255. };
  256. COMMON.JSON.encode = COMMON.JSON.stringify ? function(obj){
  257. return COMMON.JSON.stringify(obj);
  258. } : function(obj){
  259. if (obj && obj.toJSON) obj = obj.toJSON();
  260. switch (COMMON.typeOf(obj)){
  261. case 'string':
  262. return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"';
  263. case 'array':
  264. var string = [];
  265. for (var i=0; i<obj.length; i++){
  266. var json = COMMON.JSON.encode(obj[i]);
  267. if (json) string.push(json);
  268. }
  269. return '[' + string + ']';
  270. case 'object': case 'hash':
  271. var string = [];
  272. for (key in obj){
  273. var json = COMMON.JSON.encode(obj[key]);
  274. if (json) string.push(COMMON.JSON.encode(key) + ':' + json);
  275. }
  276. return '{' + string + '}';
  277. case 'number': case 'boolean': return '' + obj;
  278. case 'null': return 'null';
  279. }
  280. return null;
  281. };
  282. COMMON.JSON.decode = function(string, secure){
  283. if (!string || COMMON.typeOf(string) != 'string') return null;
  284. if (secure || COMMON.JSON.secure){
  285. if (COMMON.JSON.parse) return COMMON.JSON.parse(string);
  286. if (!COMMON.JSON.validate(string)) throw new Error('JSON could not decode the input; security is enabled and the value is not secure.');
  287. }
  288. return eval('(' + string + ')');
  289. };
  290. COMMON.JSON.get = function(url, callback, async){
  291. var jsonObj = null;
  292. var r = new COMMON.Request({
  293. url: url,
  294. async: (async==false) ? false : true,
  295. method: "get",
  296. onSuccess: function(responseText){
  297. jsonObj = COMMON.JSON.decode(responseText)
  298. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  299. callback(jsonObj);
  300. }else{
  301. COMMON.runCallback(callback, "onSuccess", [jsonObj]);
  302. }
  303. },
  304. onFailure: function(xhr){
  305. COMMON.runCallback(callback, "onRequestFailure", [xhr]);
  306. }
  307. });
  308. r.send();
  309. return jsonObj;
  310. };
  311. })();
  312. COMMON.XML = COMMON.Class.create({
  313. initialize: function(xml){
  314. this.xml = xml || null;
  315. this.request = null;
  316. },
  317. get: function(url, callback, async){
  318. var _self = this;
  319. this.request = new COMMON.Request({
  320. url: url,
  321. async: (async==false) ? false : true,
  322. method: "get",
  323. onSuccess: function(responseText, responseXML){
  324. _self.xml = responseXML
  325. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  326. callback.apply(_self, [_self]);
  327. }else{
  328. COMMON.runCallback(callback, "onSuccess", [_self]);
  329. }
  330. },
  331. onFailure: function(xhr){
  332. COMMON.runCallback(callback, "onRequestFailure", [_self]);
  333. }
  334. });
  335. this.request.send();
  336. return this;
  337. },
  338. queryNode: function(xpath){
  339. if (COMMON.Browser.Features.xpath){
  340. var xpathResult = this.xml.evaluate(xpath,this.xml,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  341. if (xpathResult.snapshotLength){
  342. return new COMMON.XML.Element(xpathResult.snapshotItem(0));
  343. }else{
  344. return null;
  345. }
  346. }else{
  347. try {
  348. return new COMMON.XML.Element(this.xml.selectSingleNode(xpath));
  349. }catch(e){
  350. try {
  351. xpath = xpath.replace(/\//g, ">");
  352. xpath = xpath.replace(/\\/g, ">");
  353. return new COMMON.XML.Element(this.xml.querySelector(xpath));
  354. }catch(e){
  355. return null;
  356. }
  357. }
  358. }
  359. },
  360. queryNodes: function(xpath){
  361. if (COMMON.Browser.Features.xpath){
  362. var xpathResult = this.xml.evaluate(xpath,this.xml,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  363. if (xpathResult.snapshotLength){
  364. return new COMMON.XML.Elements(xpathResult);
  365. }else{
  366. return null;
  367. }
  368. }else{
  369. try {
  370. return new COMMON.XML.Elements(this.xml.selectNodes(xpath));
  371. }catch(e){
  372. var nodes = [];
  373. xpath = xpath.replace(/\//g, ">");
  374. xpath = xpath.replace(/\\/g, ">");
  375. var firstNode = this.xml.querySelector(xpath);
  376. if (firstNode) nodes.push(firstNode);
  377. var nextNode = firstNode.nextSibling;
  378. while (nextNode){
  379. if (nextNode.tagName == xpath){
  380. nodes.push(nextNode);
  381. }
  382. nextNode = nextNode.nextSibling;
  383. }
  384. return new COMMON.XML.Elements(nodes);
  385. }
  386. }
  387. return null;
  388. }
  389. });
  390. COMMON.XML.Element = COMMON.Class.create({
  391. initialize: function(node){
  392. this.node = node;
  393. if (this.node){
  394. for (p in this.node){
  395. this[p] = this.node[p];
  396. }
  397. }
  398. },
  399. queryNode: function(xpath){
  400. if (COMMON.Browser.Features.xpath){
  401. var xpathResult = this.node.ownerDocument.evaluate(xpath,this.node,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  402. if (xpathResult.snapshotLength){
  403. return new COMMON.XML.Element(xpathResult.snapshotItem(0));
  404. }else{
  405. return null;
  406. }
  407. }else{
  408. try {
  409. return new COMMON.XML.Element(this.node.selectSingleNode(xpath));
  410. }catch(e){
  411. try {
  412. xpath = xpath.replace(/\//g, ">");
  413. xpath = xpath.replace(/\\/g, ">");
  414. return new COMMON.XML.Element(this.node.querySelector(xpath));
  415. }catch(e){
  416. return null;
  417. }
  418. }
  419. }
  420. },
  421. queryNodes: function(xpath){
  422. if (COMMON.Browser.Features.xpath){
  423. var xpathResult = this.node.ownerDocument.evaluate(xpath,this.node,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  424. if (xpathResult.snapshotLength){
  425. return new COMMON.XML.Elements(xpathResult);
  426. }else{
  427. return null;
  428. }
  429. }else{
  430. try {
  431. return new COMMON.XML.Elements(this.node.selectNodes(xpath));
  432. }catch(e){
  433. var nodes = [];
  434. xpath = xpath.replace(/\//g, ">");
  435. xpath = xpath.replace(/\\/g, ">");
  436. var firstNode = this.node.querySelector(xpath);
  437. if (firstNode){
  438. nodes.push(firstNode);
  439. var nextNode = firstNode.nextSibling;
  440. while (nextNode){
  441. if (nextNode.tagName == xpath){
  442. nodes.push(nextNode);
  443. }
  444. nextNode = nextNode.nextSibling;
  445. }
  446. }
  447. return new COMMON.XML.Elements(nodes);
  448. }
  449. }
  450. return null;
  451. },
  452. text: function(){
  453. try{
  454. var text = this.node.textContent;
  455. if (text){
  456. return text;
  457. }else{
  458. try{
  459. return this.node.text;
  460. }catch(e){
  461. return text;
  462. }
  463. }
  464. }catch(e){
  465. try{
  466. return this.node.text;
  467. }catch(e){
  468. return "";
  469. }
  470. }
  471. },
  472. attr: function(name){
  473. return this.node.getAttribute(name);
  474. }
  475. });
  476. COMMON.XML.Elements = COMMON.Class.create({
  477. initialize: function(nodes){
  478. this.nodes = nodes;
  479. },
  480. length: function(){
  481. if (this.nodes.snapshotLength){
  482. return this.nodes.snapshotLength;
  483. }else{
  484. return this.nodes.length;
  485. }
  486. },
  487. items: function(i){
  488. if (this.nodes.snapshotItem){
  489. return new COMMON.XML.Element(this.nodes.snapshotItem(i));
  490. }else{
  491. try {
  492. return new COMMON.XML.Element(this.nodes.item(i));
  493. }catch(e){
  494. try {
  495. return new COMMON.XML.Element(this.nodes[i]);
  496. }catch(e){
  497. return null;
  498. }
  499. }
  500. }
  501. },
  502. each: function(fun){
  503. if (this.length()){
  504. for (var i=0; i<this.length(); i++){
  505. fun.apply(this, [this.items(i), i]);
  506. }
  507. }
  508. }
  509. });
  510. COMMON.Request = function(o){
  511. this.url = o.url;
  512. if (!o.cache){
  513. if (this.url.indexOf("?")==-1){
  514. this.url = this.url + "?d=" + new String((new Date()).getTime());
  515. }else{
  516. this.url = this.url + "&d=" + new String((new Date()).getTime());
  517. }
  518. }
  519. this.async = (o.async==false) ? false : true;
  520. this.method = o.method;
  521. this.onSuccess = o.onSuccess;
  522. this.onFailure = o.onFailure;
  523. this.data = o.data;
  524. this.xhr = new COMMON.Browser.Request();
  525. this.headers = {
  526. 'X-Requested-With': 'XMLHttpRequest',
  527. 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
  528. };
  529. this.encoding = 'utf-8';
  530. };
  531. COMMON.Request.prototype.send = function(data){
  532. var xhr = this.xhr;
  533. //if (!this.async) alert(this.url);
  534. xhr.open(this.method.toUpperCase(), this.url, this.async);
  535. var req = this;
  536. if (this.method.toLowerCase() == 'post'){
  537. var encoding = (this.encoding) ? '; charset=' + this.encoding : '';
  538. this.headers['Content-type'] = 'application/x-www-form-urlencoded' + encoding;
  539. }
  540. for (key in this.headers){
  541. try {
  542. xhr.setRequestHeader(key, this.headers[key]);
  543. } catch (e){}
  544. }
  545. var onreadystatechange= function(){
  546. if (xhr.readyState != 4) return;
  547. //xhr.onreadystatechange = null;
  548. var status = xhr.status;
  549. status = (status == 1223) ? 204 : status;
  550. req.response = {text: xhr.responseText || '', xml: xhr.responseXML};
  551. if ((status >= 200 && status < 300))
  552. req.success(req.response.text, req.response.xml);
  553. else if ((status >= 300 && status < 400))
  554. req.redirect(req);
  555. else
  556. req.failure(xhr);
  557. };
  558. xhr.onreadystatechange = onreadystatechange;
  559. var rdata = data || this.data;
  560. xhr.send(rdata);
  561. if (!this.async) onreadystatechange();
  562. };
  563. COMMON.Request.prototype.processScripts = function(text){
  564. //if ((/(ecma|java)script/).test(this.getHeader('Content-type'))) return Browser.exec(text);
  565. return text;
  566. };
  567. COMMON.Request.prototype.success = function(text, xml){
  568. var t = this.processScripts(text);
  569. if (this.onSuccess) this.onSuccess(t, xml);
  570. };
  571. COMMON.Request.prototype.failure = function(xhr){
  572. if (this.onFailure) this.onFailure(xhr);
  573. };
  574. COMMON.Request.prototype.redirect = function(xhr){
  575. if (this.onRedirect) this.onRedirect(xhr);
  576. };
  577. COMMON.Request.prototype.getHeader = function(name){
  578. return this.xhr.getResponseHeader(name);
  579. };
  580. COMMON.Request.prototype.setHeader = function(name, value){
  581. this.headers[name] = value;
  582. return this;
  583. };
  584. COMMON.runCallback = function(callback, name, par){
  585. if (COMMON.typeOf(callback).toLowerCase()=='object'){
  586. if (callback[name]){
  587. callback[name].apply(callback, ((par) ? par : []));
  588. }
  589. }
  590. };
  591. COMMON.Request.prototype.getText = function(url){
  592. var v = "";
  593. var r = new COMMON.Request({
  594. "url": url,
  595. "async": false,
  596. "method": "get",
  597. "onSuccess": function(text){
  598. v = text;
  599. }
  600. }).send();;
  601. return v;
  602. };
  603. COMMON.AjaxModule = {
  604. "jquery": COMMON.contentPath+"/res/framework/jquery/jquery.min.js",
  605. "jquery-ui": COMMON.contentPath+"/res/framework/jquery/jquery-ui.custom/js/jquery-ui.custom.min.js",
  606. "mootools": COMMON.contentPath+"/res/framework/mootools/mootools-1.6.0.min.js",
  607. "mootools-more": COMMON.contentPath+"/res/framework/mootools/mootools-1.6.0.min.js",
  608. "mwf": COMMON.contentPath+"/res/mwf4/MWF.min.js",
  609. "ckeditor": COMMON.contentPath+"/res/framework/htmleditor/ckeditor451/ckeditor.js",
  610. "nicEdit": COMMON.contentPath+"/res/framework/htmleditor/nicEdit/nicEdit.js",
  611. "kindeditor": COMMON.contentPath+"/res/framework/htmleditor/kindeditor/kindeditor-all-min.js",
  612. "tinymce": COMMON.contentPath+"/res/framework/htmleditor/tinymce/tinymce.min.js",
  613. "bmap": "http://api.map.baidu.com/api?v=2.0&ak=fpzGqedB7e8CA8WB8jUgxlOx",
  614. "amap": "http://webapi.amap.com/maps?v=1.3&key=d7c7ce03a219fa4df4c7a30ba8e68888",
  615. "raphael": COMMON.contentPath+"/res/framework/raphael/raphael.js",
  616. "codemirror": COMMON.contentPath+"/res/framework/codemirror/lib/codemirror.js",
  617. "codemirror_javascript": COMMON.contentPath+"/res/framework/codemirror/mode/javascript/javascript.js",
  618. "d3": COMMON.contentPath+"/res/framework/d3/d3.js",
  619. "ace": COMMON.contentPath+"/res/framework/ace/src-min/ace.js",
  620. "ace-tools": COMMON.contentPath+"/res/framework/ace/src-min/ext-language_tools.js",
  621. "kity": COMMON.contentPath+"/res/framework/kityminder/kity/kity.min.min.js",
  622. "kityminder": COMMON.contentPath+"/res/framework/kityminder/core/dist/kityminder.core.js",
  623. //"loaded": [],
  624. //"loadedCss": [],
  625. init: function(){
  626. this["jquery"] = COMMON.contentPath+"/res/framework/jquery/jquery.min.js";
  627. this["jquery-ui"] = COMMON.contentPath+"/res/framework/jquery/jquery-ui.custom/js/jquery-ui.custom.min.js";
  628. this["mootools"] = COMMON.contentPath+"/res/framework/mootools/mootools-1.6.0.min.js";
  629. this["mootools-more"] = COMMON.contentPath+"/res/framework/mootools/mootools-1.6.0.min.js";
  630. this["mwf"] = COMMON.contentPath+"/res/mwf4/MWF.min.js";
  631. this["ckeditor"] = COMMON.contentPath+"/res/framework/htmleditor/ckeditor/ckeditor.js";
  632. this["nicEdit"] = COMMON.contentPath+"/res/framework/htmleditor/nicEdit/nicEdit.js";
  633. this["kindeditor"] = COMMON.contentPath+"/res/framework/htmleditor/kindeditor/kindeditor.js";
  634. this["tinymce"] = COMMON.contentPath+"/res/framework/htmleditor/tinymce/tinymce.full.js";
  635. this["bmap"] = "http://api.map.baidu.com/api?v=2.0&ak=fpzGqedB7e8CA8WB8jUgxlOx";
  636. this["amap"] = "http://webapi.amap.com/maps?v=1.3&key=d7c7ce03a219fa4df4c7a30ba8e68888";
  637. this["raphael"] = COMMON.contentPath+"/res/framework/raphael/raphael.js";
  638. this["codemirror"] = COMMON.contentPath+"/res/framework/codemirror/lib/codemirror.js";
  639. this["codemirror_javascript"] = COMMON.contentPath+"/res/framework/codemirror/mode/javascript/javascript.js";
  640. this["d3"] = COMMON.contentPath+"/res/framework/d3/d3.js";
  641. this["ace"] = COMMON.contentPath+"/res/framework/ace/src-min/ace.js";
  642. this["ace-tools"] = COMMON.contentPath+"/res/framework/ace/src-min/ext-language_tools.js";
  643. this["kity"] = COMMON.contentPath+"/res/framework/kityminder/kity/kity.min.js";
  644. this["kityminder"] = COMMON.contentPath+"/res/framework/kityminder/core/dist/kityminder.core.js";
  645. },
  646. "loaded": {},
  647. "loadedCss": {},
  648. loadDom: function(urls, callback, async, reload){
  649. if (COMMON.typeOf(urls)=="array"){
  650. var thisLoaded = [];
  651. urls.each(function(url){
  652. this.loadDomSingle(url, function(){
  653. thisLoaded.push(url);
  654. if (thisLoaded.length==urls.length){
  655. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  656. callback();
  657. }else{
  658. COMMON.runCallback(callback, "onSuccess");
  659. }
  660. }
  661. }, async, reload);
  662. }.bind(this));
  663. }
  664. if (COMMON.typeOf(urls)=="string"){
  665. this.loadDomSingle(urls, callback, async, reload);
  666. }
  667. },
  668. loadDomSingle: function(url, callback, async, reload){
  669. //var jsurl = this[url] || url;
  670. var jsurl = this[url];
  671. if (!jsurl){
  672. jsurl = url;
  673. if (!COMMON.debugger){
  674. jsurl = jsurl.replace(/\.js/, ".min.js");
  675. }
  676. }
  677. jsurl = (jsurl.indexOf("?")!=-1) ? jsurl+"&v="+COMMON.version : jsurl+"?v="+COMMON.version;
  678. if (!reload){
  679. if (this.loaded[key]){
  680. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  681. callback();
  682. }else{
  683. COMMON.runCallback(callback, "onSuccess");
  684. }
  685. return;
  686. };
  687. }
  688. var key = encodeURIComponent(url);
  689. var head = (document.head || document.getElementsByTagName("head")[0] || document.documentElement);
  690. var s = document.createElement('script');
  691. s.src = jsurl;
  692. head.appendChild(s);
  693. s.onload = s.onreadystatechange = function(_, isAbort) {
  694. if (isAbort || !s.readyState || s.readyState == "loaded" || s.readyState == "complete") {
  695. COMMON.AjaxModule.loaded[key] = true;
  696. s = s.onload = s.onreadystatechange = null;
  697. if (!isAbort)
  698. callback();
  699. }
  700. };
  701. },
  702. load: function(urls, callback, async, reload){
  703. if (COMMON.typeOf(urls)=="array"){
  704. var thisLoaded = [];
  705. urls.each(function(url){
  706. this.loadSingle(url, function(){
  707. thisLoaded.push(url);
  708. if (thisLoaded.length==urls.length){
  709. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  710. callback();
  711. }else{
  712. COMMON.runCallback(callback, "onSuccess");
  713. }
  714. }
  715. }, async, reload);
  716. }.bind(this));
  717. }
  718. if (COMMON.typeOf(urls)=="string"){
  719. this.loadSingle(urls, callback, async, reload);
  720. }
  721. },
  722. loadSingle: function(url, callback, async, reload){
  723. var jsurl = this[url];
  724. if (!jsurl){
  725. jsurl = url;
  726. if (!COMMON.debugger){
  727. jsurl = jsurl.replace(/\.js/, ".min.js");
  728. }
  729. }
  730. jsurl = (jsurl.indexOf("?")!=-1) ? jsurl+"&v="+COMMON.version : jsurl+"?v="+COMMON.version;
  731. var key = encodeURIComponent(url);
  732. if (!reload){
  733. if (this.loaded[key]){
  734. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  735. callback();
  736. }else{
  737. COMMON.runCallback(callback, "onSuccess");
  738. }
  739. return;
  740. }
  741. }
  742. var loadAsync = true;
  743. if (async===false){
  744. loadAsync = false;
  745. }
  746. var r = new COMMON.Request({
  747. url: jsurl,
  748. async: loadAsync,
  749. method: "get",
  750. cache: true,
  751. onSuccess: function(responseText, responseXML){
  752. var jsText = responseText;
  753. try{
  754. COMMON.Browser.exec(jsText);
  755. // COMMON.AjaxModule.loaded.push(url);
  756. COMMON.AjaxModule.loaded[key] = true;
  757. }catch (e){
  758. COMMON.runCallback(callback, "onFailure", e);
  759. return;
  760. }
  761. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  762. callback();
  763. }else{
  764. COMMON.runCallback(callback, "onSuccess");
  765. }
  766. },
  767. onFailure: function(xhr){
  768. COMMON.runCallback(callback, "onRequestFailure", xhr);
  769. }
  770. });
  771. r.send();
  772. },
  773. loadCss: function(urls, callback, async, reload, sourceDoc){
  774. if (COMMON.typeOf(urls)=="array"){
  775. var thisLoaded = [];
  776. urls.each(function(url){
  777. this.loadSingleCss(url, function(){
  778. thisLoaded.push(url);
  779. if (thisLoaded.length==urls.length){
  780. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  781. callback();
  782. }else{
  783. COMMON.runCallback(callback, "onSuccess");
  784. }
  785. }
  786. }, async, reload);
  787. }.bind(this));
  788. }
  789. if (COMMON.typeOf(urls)=="string"){
  790. this.loadSingleCss(urls, callback, async, reload);
  791. }
  792. },
  793. loadSingleCss: function(url, callback, async, reload, sourceDoc){
  794. var key = encodeURIComponent(url);
  795. if (!reload){
  796. if (this.loadedCss[key]){
  797. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  798. callback();
  799. }else{
  800. COMMON.runCallback(callback, "onSuccess");
  801. }
  802. return;
  803. };
  804. }
  805. var async = true;
  806. if (async===false){
  807. async = false;
  808. }
  809. var cssurl = this[url] || url;
  810. var r = new COMMON.Request({
  811. url: cssurl,
  812. async: async,
  813. method: "get",
  814. cache: true,
  815. onSuccess: function(responseText, responseXML){
  816. var cssText = responseText;
  817. try{
  818. if (cssText){
  819. var doc = sourceDoc || document;
  820. var style = doc.createElement("style");
  821. style.setAttribute("type", "text/css");
  822. var head = doc.getElementsByTagName("head")[0];
  823. head.appendChild(style);
  824. if(style.styleSheet){
  825. var setFunc = function(){
  826. style.styleSheet.cssText = cssText;
  827. };
  828. if(style.styleSheet.disabled){
  829. setTimeout(setFunc, 10);
  830. }else{
  831. setFunc();
  832. }
  833. }else{
  834. var cssTextNode = doc.createTextNode(cssText);
  835. style.appendChild(cssTextNode);
  836. }
  837. }
  838. COMMON.AjaxModule.loadedCss[key] = true;
  839. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  840. callback();
  841. }else{
  842. COMMON.runCallback(callback, "onSuccess");
  843. }
  844. }catch (e){
  845. COMMON.runCallback(callback, "onFailure", e);
  846. return;
  847. }
  848. },
  849. onFailure: function(xhr){
  850. COMMON.runCallback(callback, "onRequestFailure", xhr);
  851. }
  852. });
  853. r.send();
  854. }
  855. };
  856. COMMON.onReady = function(){
  857. return true;
  858. };
  859. COMMON.DOM = {
  860. ready: false,
  861. loaded: false,
  862. checks: [],
  863. shouldPoll: false,
  864. timer: null,
  865. testElement: document.createElement('div'),
  866. readys: [],
  867. domready: function(){
  868. clearTimeout(this.timer);
  869. if (COMMON.DOM.ready) return;
  870. COMMON.DOM.loaded = COMMON.DOM.ready = true;
  871. COMMON.DOM.removeListener(document, 'DOMContentLoaded', COMMON.DOM.domready);
  872. COMMON.DOM.removeListener(document, 'readystatechange', COMMON.DOM.check);
  873. COMMON.DOM.onReady();
  874. },
  875. check: function(){
  876. for (var i = COMMON.DOM.checks.length; i--;) if (COMMON.DOM.checks[i]()){
  877. COMMON.DOM.domready();
  878. return true;
  879. }
  880. return false;
  881. },
  882. poll: function(){
  883. clearTimeout(COMMON.DOM.timer);
  884. if (!COMMON.DOM.check()) COMMON.DOM.timer = setTimeout(COMMON.DOM.poll, 10);
  885. },
  886. /*<ltIE8>*/
  887. // doScroll technique by Diego Perini http://javascript.nwbox.com/IEContentLoaded/
  888. // testElement.doScroll() throws when the DOM is not ready, only in the top window
  889. doScrollWorks: function(){
  890. try {
  891. this.testElement.doScroll();
  892. return true;
  893. } catch (e){}
  894. return false;
  895. },
  896. /*</ltIE8>*/
  897. addListener: function(dom, type, fn){
  898. if (type == 'unload'){
  899. var old = fn, self = this;
  900. fn = function(){
  901. self.removeListener(dom, 'unload', fn);
  902. old();
  903. };
  904. }
  905. if (dom.addEventListener) dom.addEventListener(type, fn, !!arguments[2]);
  906. else dom.attachEvent('on' + type, fn);
  907. },
  908. removeListener: function(dom, type, fn){
  909. if (dom.removeEventListener) dom.removeEventListener(type, fn, !!arguments[2]);
  910. else dom.detachEvent('on' + type, fn);
  911. return this;
  912. },
  913. onReady: function(){
  914. for (var i=0; i<COMMON.DOM.readys.length; i++){
  915. COMMON.DOM.readys[i].apply(COMMON);
  916. }
  917. },
  918. addReady: function(fn){
  919. if (COMMON.DOM.loaded){
  920. if (fn) fn.apply(COMMON);
  921. }else{
  922. if (fn) COMMON.DOM.readys.push(fn);
  923. }
  924. return COMMON.DOM;
  925. }
  926. };
  927. (function(window, document){
  928. COMMON.DOM.addListener(document, 'DOMContentLoaded', COMMON.DOM.domready);
  929. /*<ltIE8>*/
  930. // If doScroll works already, it can't be used to determine domready
  931. // e.g. in an iframe
  932. if (COMMON.DOM.testElement.doScroll && !COMMON.DOM.doScrollWorks()){
  933. COMMON.DOM.checks.push(COMMON.DOM.doScrollWorks);
  934. COMMON.DOM.shouldPoll = true;
  935. }
  936. /*</ltIE8>*/
  937. if (document.readyState) COMMON.DOM.checks.push(function(){
  938. var state = document.readyState;
  939. return (state == 'loaded' || state == 'complete');
  940. });
  941. if ('onreadystatechange' in document) COMMON.DOM.addListener(document, 'readystatechange', COMMON.DOM.check);
  942. else COMMON.DOM.shouldPoll = true;
  943. if (COMMON.DOM.shouldPoll) COMMON.DOM.poll();
  944. })(window, document);
  945. (function(){
  946. Array.prototype.arrayIndexOf = function (o){
  947. for (var i=0; i<this.length; i++){
  948. if (this[i]==o){
  949. return i
  950. }
  951. }
  952. return null;
  953. };
  954. Array.prototype.arrayLastIndexOf = function (o){
  955. for (var i=this.length-1; i>=0; i--){
  956. if (this[i]==o){
  957. return i
  958. }
  959. }
  960. return null;
  961. };
  962. Array.prototype.arrayInStr = function (str){
  963. var returnArray = [];
  964. if (str.toString()!=""){
  965. for (var i=0; i<this.length; i++){
  966. if (this[i].toString().indexOf(str.toString())!=-1){
  967. returnArray.push(this[i])
  968. }
  969. }
  970. }
  971. return returnArray;
  972. };
  973. Array.prototype.isIntersection = function (arr){
  974. if (arr){
  975. for (var i=0; i<arr.length; i++){
  976. if (this.arrayIndexOf(arr[i])!=null){
  977. return true;
  978. }
  979. }
  980. }
  981. return false;
  982. };
  983. })();
  984. COMMON.getUserCN = function(name){
  985. var userName = name;
  986. userName = userName.substr(3, userName.length);
  987. userName = userName.substr(0,userName.indexOf("/"));
  988. return userName;
  989. };
  990. COMMON.getNowDateString = function (){
  991. var now = new Date();
  992. m = now.getMonth()+1;
  993. return now.getYear()+"年"+m+"月"+now.getDate()+"日 "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
  994. };