common.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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.25";
  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. "JSONTemplate": COMMON.contentPath+"/res/framework/mootools/plugin/Template.js",
  624. //"loaded": [],
  625. //"loadedCss": [],
  626. init: function(){
  627. this["jquery"] = COMMON.contentPath+"/res/framework/jquery/jquery.min.js";
  628. this["jquery-ui"] = COMMON.contentPath+"/res/framework/jquery/jquery-ui.custom/js/jquery-ui.custom.min.js";
  629. this["mootools"] = COMMON.contentPath+"/res/framework/mootools/mootools-1.6.0.min.js";
  630. this["mootools-more"] = COMMON.contentPath+"/res/framework/mootools/mootools-1.6.0.min.js";
  631. this["mwf"] = COMMON.contentPath+"/res/mwf4/MWF.min.js";
  632. this["ckeditor"] = COMMON.contentPath+"/res/framework/htmleditor/ckeditor451/ckeditor.js";
  633. this["nicEdit"] = COMMON.contentPath+"/res/framework/htmleditor/nicEdit/nicEdit.js";
  634. this["kindeditor"] = COMMON.contentPath+"/res/framework/htmleditor/kindeditor/kindeditor.js";
  635. this["tinymce"] = COMMON.contentPath+"/res/framework/htmleditor/tinymce/tinymce.full.js";
  636. this["bmap"] = "http://api.map.baidu.com/api?v=2.0&ak=fpzGqedB7e8CA8WB8jUgxlOx";
  637. this["amap"] = "http://webapi.amap.com/maps?v=1.3&key=d7c7ce03a219fa4df4c7a30ba8e68888";
  638. this["raphael"] = COMMON.contentPath+"/res/framework/raphael/raphael.js";
  639. this["codemirror"] = COMMON.contentPath+"/res/framework/codemirror/lib/codemirror.js";
  640. this["codemirror_javascript"] = COMMON.contentPath+"/res/framework/codemirror/mode/javascript/javascript.js";
  641. this["d3"] = COMMON.contentPath+"/res/framework/d3/d3.js";
  642. this["ace"] = COMMON.contentPath+"/res/framework/ace/src-min/ace.js";
  643. this["ace-tools"] = COMMON.contentPath+"/res/framework/ace/src-min/ext-language_tools.js";
  644. this["kity"] = COMMON.contentPath+"/res/framework/kityminder/kity/kity.min.js";
  645. this["kityminder"] = COMMON.contentPath+"/res/framework/kityminder/core/dist/kityminder.core.js";
  646. this["JSONTemplate"] = COMMON.contentPath+"/res/framework/mootools/plugin/Template.js";
  647. },
  648. "loaded": {},
  649. "loadedCss": {},
  650. loadDom: function(urls, callback, async, reload){
  651. if (COMMON.typeOf(urls)==="array"){
  652. var thisLoaded = [];
  653. urls.each(function(url){
  654. this.loadDomSingle(url, function(){
  655. thisLoaded.push(url);
  656. if (thisLoaded.length===urls.length){
  657. if (COMMON.typeOf(callback).toLowerCase() === 'function'){
  658. callback();
  659. }else{
  660. COMMON.runCallback(callback, "onSuccess");
  661. }
  662. }
  663. }, async, reload);
  664. }.bind(this));
  665. }
  666. if (COMMON.typeOf(urls)==="string"){
  667. this.loadDomSingle(urls, callback, async, reload);
  668. }
  669. },
  670. loadDomSingle: function(url, callback, async, reload){
  671. //var jsurl = this[url] || url;
  672. var jsurl = this[url];
  673. if (!jsurl){
  674. jsurl = url;
  675. if (!COMMON["debugger"]){
  676. jsurl = jsurl.replace(/\.js/, ".min.js");
  677. }
  678. }
  679. jsurl = (jsurl.indexOf("?")!==-1) ? jsurl+"&v="+COMMON.version : jsurl+"?v="+COMMON.version;
  680. if (!reload){
  681. if (this.loaded[key]){
  682. if (COMMON.typeOf(callback).toLowerCase() === 'function'){
  683. callback();
  684. }else{
  685. COMMON.runCallback(callback, "onSuccess");
  686. }
  687. return;
  688. }
  689. }
  690. var key = encodeURIComponent(url);
  691. var head = (document.head || document.getElementsByTagName("head")[0] || document.documentElement);
  692. var s = document.createElement('script');
  693. s.src = jsurl;
  694. head.appendChild(s);
  695. s.onload = s.onreadystatechange = function(_, isAbort) {
  696. if (isAbort || !s.readyState || s.readyState === "loaded" || s.readyState === "complete") {
  697. COMMON.AjaxModule.loaded[key] = true;
  698. s = s.onload = s.onreadystatechange = null;
  699. if (!isAbort)
  700. callback();
  701. }
  702. };
  703. },
  704. load: function(urls, callback, async, reload){
  705. if (COMMON.typeOf(urls)==="array"){
  706. var thisLoaded = [];
  707. urls.each(function(url){
  708. this.loadSingle(url, function(){
  709. thisLoaded.push(url);
  710. if (thisLoaded.length===urls.length){
  711. if (COMMON.typeOf(callback).toLowerCase() === 'function'){
  712. callback();
  713. }else{
  714. COMMON.runCallback(callback, "onSuccess");
  715. }
  716. }
  717. }, async, reload);
  718. }.bind(this));
  719. }
  720. if (COMMON.typeOf(urls)==="string"){
  721. this.loadSingle(urls, callback, async, reload);
  722. }
  723. },
  724. loadSingle: function(url, callback, async, reload){
  725. var jsurl = this[url];
  726. if (!jsurl){
  727. jsurl = url;
  728. if (!COMMON["debugger"]){
  729. jsurl = jsurl.replace(/\.js/, ".min.js");
  730. }
  731. }
  732. jsurl = (jsurl.indexOf("?")!=-1) ? jsurl+"&v="+COMMON.version : jsurl+"?v="+COMMON.version;
  733. var key = encodeURIComponent(url);
  734. if (!reload){
  735. if (this.loaded[key]){
  736. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  737. callback();
  738. }else{
  739. COMMON.runCallback(callback, "onSuccess");
  740. }
  741. return;
  742. }
  743. }
  744. var loadAsync = true;
  745. if (async===false){
  746. loadAsync = false;
  747. }
  748. var r = new COMMON.Request({
  749. url: jsurl,
  750. async: loadAsync,
  751. method: "get",
  752. cache: true,
  753. onSuccess: function(responseText, responseXML){
  754. var jsText = responseText;
  755. try{
  756. COMMON.Browser.exec(jsText);
  757. // COMMON.AjaxModule.loaded.push(url);
  758. COMMON.AjaxModule.loaded[key] = true;
  759. }catch (e){
  760. COMMON.runCallback(callback, "onFailure", e);
  761. return;
  762. }
  763. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  764. callback();
  765. }else{
  766. COMMON.runCallback(callback, "onSuccess");
  767. }
  768. },
  769. onFailure: function(xhr){
  770. COMMON.runCallback(callback, "onRequestFailure", xhr);
  771. }
  772. });
  773. r.send();
  774. },
  775. loadCss: function(urls, callback, async, reload, sourceDoc){
  776. if (COMMON.typeOf(urls)=="array"){
  777. var thisLoaded = [];
  778. urls.each(function(url){
  779. this.loadSingleCss(url, function(){
  780. thisLoaded.push(url);
  781. if (thisLoaded.length==urls.length){
  782. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  783. callback();
  784. }else{
  785. COMMON.runCallback(callback, "onSuccess");
  786. }
  787. }
  788. }, async, reload);
  789. }.bind(this));
  790. }
  791. if (COMMON.typeOf(urls)=="string"){
  792. this.loadSingleCss(urls, callback, async, reload);
  793. }
  794. },
  795. loadSingleCss: function(url, callback, async, reload, sourceDoc){
  796. var key = encodeURIComponent(url);
  797. if (!reload){
  798. if (this.loadedCss[key]){
  799. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  800. callback();
  801. }else{
  802. COMMON.runCallback(callback, "onSuccess");
  803. }
  804. return;
  805. };
  806. }
  807. var async = true;
  808. if (async===false){
  809. async = false;
  810. }
  811. var cssurl = this[url] || url;
  812. var r = new COMMON.Request({
  813. url: cssurl,
  814. async: async,
  815. method: "get",
  816. cache: true,
  817. onSuccess: function(responseText, responseXML){
  818. var cssText = responseText;
  819. try{
  820. if (cssText){
  821. var doc = sourceDoc || document;
  822. var style = doc.createElement("style");
  823. style.setAttribute("type", "text/css");
  824. var head = doc.getElementsByTagName("head")[0];
  825. head.appendChild(style);
  826. if(style.styleSheet){
  827. var setFunc = function(){
  828. style.styleSheet.cssText = cssText;
  829. };
  830. if(style.styleSheet.disabled){
  831. setTimeout(setFunc, 10);
  832. }else{
  833. setFunc();
  834. }
  835. }else{
  836. var cssTextNode = doc.createTextNode(cssText);
  837. style.appendChild(cssTextNode);
  838. }
  839. }
  840. COMMON.AjaxModule.loadedCss[key] = true;
  841. if (COMMON.typeOf(callback).toLowerCase() == 'function'){
  842. callback();
  843. }else{
  844. COMMON.runCallback(callback, "onSuccess");
  845. }
  846. }catch (e){
  847. COMMON.runCallback(callback, "onFailure", e);
  848. return;
  849. }
  850. },
  851. onFailure: function(xhr){
  852. COMMON.runCallback(callback, "onRequestFailure", xhr);
  853. }
  854. });
  855. r.send();
  856. }
  857. };
  858. COMMON.onReady = function(){
  859. return true;
  860. };
  861. COMMON.DOM = {
  862. ready: false,
  863. loaded: false,
  864. checks: [],
  865. shouldPoll: false,
  866. timer: null,
  867. testElement: document.createElement('div'),
  868. readys: [],
  869. domready: function(){
  870. clearTimeout(this.timer);
  871. if (COMMON.DOM.ready) return;
  872. COMMON.DOM.loaded = COMMON.DOM.ready = true;
  873. COMMON.DOM.removeListener(document, 'DOMContentLoaded', COMMON.DOM.domready);
  874. COMMON.DOM.removeListener(document, 'readystatechange', COMMON.DOM.check);
  875. COMMON.DOM.onReady();
  876. },
  877. check: function(){
  878. for (var i = COMMON.DOM.checks.length; i--;) if (COMMON.DOM.checks[i]()){
  879. COMMON.DOM.domready();
  880. return true;
  881. }
  882. return false;
  883. },
  884. poll: function(){
  885. clearTimeout(COMMON.DOM.timer);
  886. if (!COMMON.DOM.check()) COMMON.DOM.timer = setTimeout(COMMON.DOM.poll, 10);
  887. },
  888. /*<ltIE8>*/
  889. // doScroll technique by Diego Perini http://javascript.nwbox.com/IEContentLoaded/
  890. // testElement.doScroll() throws when the DOM is not ready, only in the top window
  891. doScrollWorks: function(){
  892. try {
  893. this.testElement.doScroll();
  894. return true;
  895. } catch (e){}
  896. return false;
  897. },
  898. /*</ltIE8>*/
  899. addListener: function(dom, type, fn){
  900. if (type == 'unload'){
  901. var old = fn, self = this;
  902. fn = function(){
  903. self.removeListener(dom, 'unload', fn);
  904. old();
  905. };
  906. }
  907. if (dom.addEventListener) dom.addEventListener(type, fn, !!arguments[2]);
  908. else dom.attachEvent('on' + type, fn);
  909. },
  910. removeListener: function(dom, type, fn){
  911. if (dom.removeEventListener) dom.removeEventListener(type, fn, !!arguments[2]);
  912. else dom.detachEvent('on' + type, fn);
  913. return this;
  914. },
  915. onReady: function(){
  916. for (var i=0; i<COMMON.DOM.readys.length; i++){
  917. COMMON.DOM.readys[i].apply(COMMON);
  918. }
  919. },
  920. addReady: function(fn){
  921. if (COMMON.DOM.loaded){
  922. if (fn) fn.apply(COMMON);
  923. }else{
  924. if (fn) COMMON.DOM.readys.push(fn);
  925. }
  926. return COMMON.DOM;
  927. }
  928. };
  929. (function(window, document){
  930. COMMON.DOM.addListener(document, 'DOMContentLoaded', COMMON.DOM.domready);
  931. /*<ltIE8>*/
  932. // If doScroll works already, it can't be used to determine domready
  933. // e.g. in an iframe
  934. if (COMMON.DOM.testElement.doScroll && !COMMON.DOM.doScrollWorks()){
  935. COMMON.DOM.checks.push(COMMON.DOM.doScrollWorks);
  936. COMMON.DOM.shouldPoll = true;
  937. }
  938. /*</ltIE8>*/
  939. if (document.readyState) COMMON.DOM.checks.push(function(){
  940. var state = document.readyState;
  941. return (state == 'loaded' || state == 'complete');
  942. });
  943. if ('onreadystatechange' in document) COMMON.DOM.addListener(document, 'readystatechange', COMMON.DOM.check);
  944. else COMMON.DOM.shouldPoll = true;
  945. if (COMMON.DOM.shouldPoll) COMMON.DOM.poll();
  946. })(window, document);
  947. (function(){
  948. Array.prototype.arrayIndexOf = function (o){
  949. for (var i=0; i<this.length; i++){
  950. if (this[i]==o){
  951. return i
  952. }
  953. }
  954. return null;
  955. };
  956. Array.prototype.arrayLastIndexOf = function (o){
  957. for (var i=this.length-1; i>=0; i--){
  958. if (this[i]==o){
  959. return i
  960. }
  961. }
  962. return null;
  963. };
  964. Array.prototype.arrayInStr = function (str){
  965. var returnArray = [];
  966. if (str.toString()!=""){
  967. for (var i=0; i<this.length; i++){
  968. if (this[i].toString().indexOf(str.toString())!=-1){
  969. returnArray.push(this[i])
  970. }
  971. }
  972. }
  973. return returnArray;
  974. };
  975. Array.prototype.isIntersection = function (arr){
  976. if (arr){
  977. for (var i=0; i<arr.length; i++){
  978. if (this.arrayIndexOf(arr[i])!=null){
  979. return true;
  980. }
  981. }
  982. }
  983. return false;
  984. };
  985. })();
  986. COMMON.getUserCN = function(name){
  987. var userName = name;
  988. userName = userName.substr(3, userName.length);
  989. userName = userName.substr(0,userName.indexOf("/"));
  990. return userName;
  991. };
  992. COMMON.getNowDateString = function (){
  993. var now = new Date();
  994. m = now.getMonth()+1;
  995. return now.getYear()+"年"+m+"月"+now.getDate()+"日 "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
  996. };