o2.more.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /** ***** BEGIN LICENSE BLOCK *****
  2. * |------------------------------------------------------------------------------|
  3. * | O2OA 活力办公 创意无限 o2.more.js |
  4. * |------------------------------------------------------------------------------|
  5. * | Distributed under the AGPL license: |
  6. * |------------------------------------------------------------------------------|
  7. * | Copyright © 2018, o2oa.net, o2server.io O2 Team |
  8. * | All rights reserved. |
  9. * |------------------------------------------------------------------------------|
  10. *
  11. * This file is part of O2OA.
  12. *
  13. * O2OA is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * O2OA is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with Foobar. If not, see <https://www.gnu.org/licenses/>.
  25. *
  26. * ***** END LICENSE BLOCK ******/
  27. (function (){
  28. o2.getCenterPosition = function(el, width, height){
  29. var elPositon = $(el).getPosition();
  30. var elSize = $(el).getSize();
  31. var node = $("layout");
  32. var size = (node) ? $(node).getSize() : $(document.body).getSize();
  33. var top = (elPositon.y+elSize.y)/2 - (height/2);
  34. var left = (elPositon.x+elSize.x)/2-(width/2);
  35. if ((left+width)>size.x){
  36. left = size.x-width-10;
  37. }
  38. if ((top+height)>size.y){
  39. top = size.y-height-10;
  40. }
  41. return {"x": left, "y": top};
  42. };
  43. o2.getMarkSize = function(node){
  44. var size;
  45. if (!node){
  46. size = $(document.body).getSize();
  47. var winSize = $(window).getSize();
  48. var height = size.y;
  49. var width = size.x;
  50. if (height<winSize.y) height = winSize.y;
  51. if (width<winSize.x) width = winSize.x;
  52. return {x: size.x, y: height};
  53. }else{
  54. size = $(node).getSize();
  55. return {x: size.x, y: size.y};
  56. }
  57. };
  58. o2.json = function(jsonString, fun){
  59. var obj = JSON.decode(jsonString);
  60. var p = fun.split(".");
  61. var tmp = obj;
  62. p.each(function(item){
  63. if (item.indexOf("[")!==-1){
  64. var x = item.split("[");
  65. var i = parseInt(x[1].substr(0, x[1].indexOf("]")));
  66. tmp = tmp[x[0]][i];
  67. }else{
  68. tmp = tmp[item];
  69. }
  70. });
  71. return tmp;
  72. };
  73. o2.getHTMLTemplate = function(url, callback, async){
  74. var loadAsync = (async !== false);
  75. var res = new Request.HTML({
  76. url: url,
  77. async: loadAsync,
  78. method: "get",
  79. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  80. o2.runCallback(callback, "success", [responseTree, responseElements, responseHTML, responseJavaScript]);
  81. }.bind(this),
  82. onFailure: function(xhr){
  83. o2.runCallback(callback, "requestFailure", [xhr]);
  84. }
  85. });
  86. res.send();
  87. };
  88. o2.getRequestText = function(url, callback, async){
  89. var loadAsync = (async !== false);
  90. url = (url.indexOf("?")!==-1) ? url+"&v="+o2.version.v : url+"?v="+o2.version.v;
  91. var res = new Request({
  92. url: url,
  93. async: loadAsync,
  94. method: "get",
  95. onSuccess: function(responseText, responseXML){
  96. o2.runCallback(callback, "success",[responseText, responseXML]);
  97. }.bind(this),
  98. onFailure: function(xhr){
  99. o2.runCallback(callback, "requestFailure",[xhr]);
  100. }
  101. });
  102. res.send();
  103. };
  104. o2.encodeJsonString = function(str){
  105. var tmp = [str];
  106. var dataStr = (JSON.encode(tmp));
  107. return dataStr.substr(2, dataStr.length-4);
  108. };
  109. o2.decodeJsonString = function(str){
  110. var tmp = "[\""+str+"\"]";
  111. var dataObj = (JSON.decode(tmp));
  112. return dataObj[0];
  113. };
  114. o2.getTextSize = function(text, styles){
  115. var tmpSpan = new Element("span", {
  116. "text": text,
  117. "styles": styles
  118. }).inject($(document.body));
  119. var size = tmpSpan.getSize();
  120. tmpSpan.destroy();
  121. return size;
  122. };
  123. o2.getCenter = function(size, target, offset){
  124. if (!target) target = document.body;
  125. var targetSize = target.getSize();
  126. var targetPosition = target.getPosition(offset);
  127. var targetScroll = target.getScroll();
  128. var x = targetSize.x/2;
  129. var y = targetSize.y/2;
  130. x = x-(size.x/2);
  131. y = y-(size.y/2);
  132. x = x+targetPosition.x;
  133. y = y+targetPosition.y;
  134. x = x+targetScroll.x;
  135. y = y+targetScroll.y;
  136. return {"x": x, "y": y};
  137. };
  138. o2.getEPointer = function(e){
  139. var x = 0;
  140. var y = 0;
  141. if (typeOf(e)=="element"){
  142. var position = e.getPosition(this.content);
  143. x = position.x;
  144. y = position.y;
  145. }else{
  146. if (Browser.name=="firefox"){
  147. x = parseFloat(e.event.clientX || e.event.x);
  148. y = parseFloat(e.event.clientY || e.event.y);
  149. }else{
  150. x = parseFloat(e.event.x);
  151. y = parseFloat(e.event.y);
  152. }
  153. if (e.target){
  154. var position = e.target.getPosition(this.content);
  155. x = position.x;
  156. y = position.y;
  157. }
  158. // }
  159. }
  160. return {"x": x, "y": y};
  161. };
  162. o2.getParent = function(node, tag){
  163. var pNode = node.parentElement;
  164. while(pNode && pNode.tagName.toString().toLowerCase() !== tag.toString().toLowerCase()){
  165. pNode = pNode.parentElement;
  166. }
  167. return pNode;
  168. };
  169. o2.getOffset = function(evt){
  170. if (Browser.name==="firefox"){
  171. return {
  172. "offsetX": evt.layerX,
  173. "offsetY": evt.layerY
  174. };
  175. }else{
  176. return {
  177. "offsetX": evt.offsetX,
  178. "offsetY": evt.offsetY
  179. }
  180. }
  181. };
  182. if (String.implement) String.implement({
  183. "getAllIndexOf": function(str){
  184. var idxs= [];
  185. var idx = this.indexOf(str);
  186. while (idx !== -1){
  187. idxs.push(idx);
  188. idx = this.indexOf(str, idx+1);
  189. }
  190. return idxs;
  191. }
  192. });
  193. if (Array.implement) Array.implement({
  194. "trim": function(){
  195. var arr = [];
  196. this.each(function(v){
  197. if (v) arr.push(v);
  198. });
  199. return arr;
  200. },
  201. "isIntersect": function(arr){
  202. return this.some(function(item){ return (arr.indexOf(item)!==-1); })
  203. }
  204. });
  205. if (window.Element && Element.implement) Element.implement({
  206. "isIntoView": function() {
  207. // var pNode = this.getParent();
  208. // while (pNode && ((pNode.getScrollSize().y-(pNode.getComputedSize().height+1)<=0) || pNode.getStyle("overflow")==="visible")) pNode = pNode.getParent();
  209. //
  210. var pNode = this.getParentSrcollNode();
  211. if (!pNode) pNode = document.body;
  212. var size = pNode.getSize();
  213. var srcoll = pNode.getScroll();
  214. var p = (pNode == window) ? {"x":0, "y": 0} : this.getPosition(pNode);
  215. var nodeSize = this.getSize();
  216. //return (p.x-srcoll.x>=0 && p.y-srcoll.y>=0) && (p.x+nodeSize.x<size.x+srcoll.x && p.y+nodeSize.y<size.y+srcoll.y);
  217. return (p.x-srcoll.x>=0 && p.y>=0) && (p.x+nodeSize.x<size.x+srcoll.x && p.y+nodeSize.y<size.y)
  218. },
  219. "appendHTML": function(html, where){
  220. if (this.insertAdjacentHTML){
  221. var whereText = "beforeEnd";
  222. if (where==="before") whereText = "beforeBegin";
  223. if (where==="after") whereText = "afterEnd";
  224. if (where==="bottom") whereText = "beforeEnd";
  225. if (where==="top") whereText = "afterBegin";
  226. this.insertAdjacentHTML(whereText, html);
  227. }else {
  228. if (where==="bottom") this.innerHTML = this.innerHTML+html;
  229. if (where==="top") this.innerHTML = html+this.innerHTML;
  230. }
  231. },
  232. "positionTo": function(x,y){
  233. var left = x.toFloat();
  234. var top = y.toFloat();
  235. var offsetNode = this.getOffsetParent();
  236. if (offsetNode){
  237. var offsetPosition = offsetNode.getPosition();
  238. left = left-offsetPosition.x;
  239. top = top-offsetPosition.y;
  240. }
  241. this.setStyles({"top": top, "left": left});
  242. return this;
  243. },
  244. "getBorder": function(){
  245. var positions = ["top", "left", "right", "bottom"];
  246. var styles = ["color", "style", "width"];
  247. var obj = {};
  248. positions.each(function (position){
  249. styles.each(function(style){
  250. var key = "border-"+position+"-"+style;
  251. obj[key] = this.getStyle(key);
  252. }.bind(this));
  253. }.bind(this));
  254. return obj;
  255. },
  256. "isOutside": function(e){
  257. var elementCoords = this.getCoordinates();
  258. var targetCoords = this.getCoordinates();
  259. if(((e.page.x < elementCoords.left || e.page.x > (elementCoords.left + elementCoords.width)) ||
  260. (e.page.y < elementCoords.top || e.page.y > (elementCoords.top + elementCoords.height))) &&
  261. ((e.page.x < targetCoords.left || e.page.x > (targetCoords.left + targetCoords.width)) ||
  262. (e.page.y < targetCoords.top || e.page.y > (targetCoords.top + targetCoords.height))) ) return true;
  263. return false;
  264. },
  265. "getAbsolutePosition":function(){
  266. var styleLeft = 0;
  267. var styleTop = 0;
  268. var node = this;
  269. styleLeft = node.offsetLeft;
  270. styleTop = node.offsetTop;
  271. node = node.parentElement;
  272. while (node && node.tagName.toString().toLowerCase()!=="body"){
  273. styleLeft += node.offsetLeft;
  274. styleTop += node.offsetTop;
  275. node = node.offsetParent;
  276. }
  277. return {x: styleLeft, y: styleTop};
  278. },
  279. "tweenScroll": function(to, time){
  280. if (!this.tweenScrollQueue){
  281. this.tweenScrollQueue = [];
  282. }
  283. if (this.tweenScrollQueue.length){
  284. this.tweenScrollQueue.push(to);
  285. }else{
  286. this.tweenScrollQueue.push(to);
  287. this.doTweenScrollQueue(time);
  288. }
  289. },
  290. "doTweenScrollQueue": function(time){
  291. if (this.tweenScrollQueue.length){
  292. var i = this.tweenScrollQueue.length;
  293. var to = this.tweenScrollQueue[this.tweenScrollQueue.length-1];
  294. var scroll = this.getScroll();
  295. var dy = to - scroll.y;
  296. var step = dy/time;
  297. var count = 0;
  298. var move = 0;
  299. var id = window.setInterval(function(){
  300. this.scrollTo(0, scroll.y+count*step);
  301. count++;
  302. if (count>time){
  303. window.clearInterval(id);
  304. for (var x=1; x<=i; x++) this.tweenScrollQueue.shift();
  305. if (this.tweenScrollQueue.length) this.doTweenScrollQueue(time);
  306. }
  307. }.bind(this), 1);
  308. }
  309. },
  310. "isPointIn": function(px, py, offX, offY, el){
  311. if (!offX) offX = 0;
  312. if (!offY) offY = 0;
  313. var position = this.getPosition(el);
  314. var size = this.getSize();
  315. return (position.x-offX<=px && position.x+size.x+offX>=px && position.y-offY<=py && position.y+size.y+offY>=py);
  316. },
  317. "isInPointInRect": function(sx, sy, ex, ey){
  318. var position = this.getPosition();
  319. var size = this.getSize();
  320. var p1 = {"x": position.x, "y": position.y};
  321. var p2 = {"x": position.x+size.x, "y": position.y};
  322. var p3 = {"x": position.x+size.x, "y": position.y+size.y};
  323. var p4 = {"x": position.x, "y": position.y+size.y};
  324. var sp = {"x": Math.min(sx, ex), "y": Math.min(sy, ey)};
  325. var ep = {"x": Math.max(sx, ex), "y": Math.max(sy, ey)};
  326. if (p1.x>=sp.x && p1.y>=sp.y && p1.x<=ep.x && p1.y<=ep.y) return true;
  327. if (p2.x>=sp.x && p2.y>=sp.y && p2.x<=ep.x && p2.y<=ep.y) return true;
  328. if (p3.x>=sp.x && p3.y>=sp.y && p3.x<=ep.x && p3.y<=ep.y) return true;
  329. if (p4.x>=sp.x && p4.y>=sp.y && p4.x<=ep.x && p4.y<=ep.y) return true;
  330. if (p3.x>=sp.x && p3.y>=sp.y && p1.x<=sp.x && p1.y<=sp.y) return true;
  331. if (p3.x>=ep.x && p3.y>=ep.y && p1.x<=ep.x && p1.y<=ep.y) return true;
  332. if (p1.x<=sp.x && p2.x>=sp.x && p1.y>=sp.y && p4.y<=ep.y) return true;
  333. if (p1.y<=sp.y && p4.y>=sp.y && p1.x>=sp.x && p2.x<=ep.x) return true;
  334. return false;
  335. },
  336. "isOverlap": function(node){
  337. var p = node.getPosition();
  338. var s = node.getSize();
  339. return this.isInPointInRect(p.x, p.y, p.x+s.x, p.y+s.y);
  340. },
  341. "getUsefulSize": function(){
  342. var size = this.getSize();
  343. var borderLeft = this.getStyle("border-left").toInt();
  344. var borderBottom = this.getStyle("border-bottom").toInt();
  345. var borderTop = this.getStyle("border-top").toInt();
  346. var borderRight = this.getStyle("border-right").toInt();
  347. var paddingLeft = this.getStyle("padding-left").toInt();
  348. var paddingBottom = this.getStyle("padding-bottom").toInt();
  349. var paddingTop = this.getStyle("padding-top").toInt();
  350. var paddingRight = this.getStyle("padding-right").toInt();
  351. var x = size.x-paddingLeft-paddingRight;
  352. var y = size.y-paddingTop-paddingBottom;
  353. return {"x": x, "y": y};
  354. },
  355. "clearStyles": function(isChild){
  356. this.removeProperty("style");
  357. if (isChild){
  358. var subNode = this.getFirst();
  359. while (subNode){
  360. subNode.clearStyles(isChild);
  361. subNode = subNode.getNext();
  362. }
  363. }
  364. },
  365. "maskIf": function(styles, click){
  366. var style = {
  367. "background-color": "#666666",
  368. "opacity": 0.4,
  369. "z-index":100
  370. };
  371. if (styles){
  372. style = Object.merge(style, styles);
  373. }
  374. var position = this.getPosition(this.getOffsetParent());
  375. this.mask({
  376. "destroyOnHide": true,
  377. "style": style,
  378. "useIframeShim": true,
  379. "iframeShimOptions": {"browsers": true},
  380. "onShow": function(){
  381. this.shim.shim.setStyles({
  382. "opacity": 0,
  383. "top": ""+position.y+"px",
  384. "left": ""+position.x+"px"
  385. });
  386. },
  387. "onClick": click
  388. });
  389. },
  390. "scrollIn": function(where){
  391. var wh = (where) ? where.toString().toLowerCase() : "center";
  392. if (Browser.name=="ie" || Browser.name=="safari"){
  393. var scrollNode = this.getParentSrcollNode();
  394. var scrollFx = new Fx.Scroll(scrollNode);
  395. var scroll = scrollNode.getScroll();
  396. var size = scrollNode.getSize();
  397. var thisSize = this.getComputedSize();
  398. var p = this.getPosition(scrollNode);
  399. if (wh=="start"){
  400. var top = 0;
  401. scrollFx.start(scroll.x, p.y-top+scroll.y);
  402. }else if (wh=="end"){
  403. var bottom = size.y-thisSize.totalHeight;
  404. scrollFx.start(scroll.x, p.y-bottom+scroll.y);
  405. }else{
  406. var center = size.y/2-thisSize.totalHeight/2;
  407. scrollFx.start(scroll.x, p.y-center+scroll.y);
  408. }
  409. }else{
  410. if (wh!=="start" && wh!=="end") wh = "center"
  411. this.scrollIntoView({"behavior": "smooth", "block": wh, "inline": "nearest"});
  412. }
  413. },
  414. scrollToNode: function(el, where){
  415. var scrollSize = this.getScrollSize();
  416. if (!scrollSize.y) return true;
  417. var wh = (where) ? where.toString().toLowerCase() : "bottom";
  418. var node = $(el);
  419. var size = node.getComputedSize();
  420. var p = node.getPosition(this);
  421. var thisSize = this.getComputedSize();
  422. var scroll = this.getScroll();
  423. if (wh==="top"){
  424. var n = (p.y-thisSize.computedTop);
  425. if (n<0) this.scrollTo(scroll.x, scroll.y+n);
  426. n = (size.totalHeight+p.y-thisSize.computedTop)-thisSize.height;
  427. if (n>0) this.scrollTo(scroll.x, scroll.y+n);
  428. }else{
  429. var n = (size.totalHeight+p.y-thisSize.computedTop)-thisSize.height;
  430. if (n>0) this.scrollTo(scroll.x, scroll.y+n);
  431. n = p.y-thisSize.computedTop;
  432. if (n<0) this.scrollTo(scroll.x, scroll.y+n);
  433. }
  434. },
  435. "getInnerStyles": function(){
  436. var styles = {};
  437. style = this.get("style");
  438. if (style){
  439. var styleArr = style.split(/\s*\;\s*/g);
  440. styleArr.each(function(s){
  441. if (s){
  442. var sarr = s.split(/\s*\:\s*/g);
  443. styles[sarr[0]] = (sarr.length>1) ? sarr[1]: ""
  444. }
  445. }.bind(this));
  446. }
  447. return styles;
  448. },
  449. "getInnerProperties": function(){
  450. var properties = {};
  451. if (this.attributes.length){
  452. for (var i=0; i<this.attributes.length; i++){
  453. properties[this.attributes[i].nodeName] = this.attributes[i].nodeValue;
  454. }
  455. }
  456. return properties;
  457. },
  458. "getZIndex": function(){
  459. var n = this;
  460. var i=0;
  461. while (n){
  462. if (n.getStyle("position")==="absolute"){
  463. var idx = n.getStyle("z-index");
  464. i = (idx && idx.toFloat()>i) ? idx.toFloat()+1 : 0;
  465. break;
  466. }
  467. n = n.getParent();
  468. }
  469. return i;
  470. },
  471. "getParentSrcollNode": function(){
  472. var node = this.getParent();
  473. while (node && (node.getScrollSize().y-2<=node.getSize().y || (node.getStyle("overflow")!=="auto" && node.getStyle("overflow-y")!=="auto"))){
  474. node = node.getParent();
  475. }
  476. return node || null;
  477. },
  478. "getEdgeHeight": function(notMargin){
  479. var h = 0;
  480. h += (this.getStyle("border-top-width").toFloat() || 0)+ (this.getStyle("border-bottom-width").toFloat() || 0);
  481. h += (this.getStyle("padding-top").toFloat() || 0)+ (this.getStyle("padding-bottom").toFloat() || 0);
  482. if (!notMargin) h += (this.getStyle("margin-top").toFloat() || 0)+ (this.getStyle("margin-bottom").toFloat() || 0);
  483. return h;
  484. },
  485. "getEdgeWidth": function(notMargin){
  486. var h = 0;
  487. h += (this.getStyle("border-left-width").toFloat() || 0)+ (this.getStyle("border-right-width").toFloat() || 0);
  488. h += (this.getStyle("padding-left").toFloat() || 0)+ (this.getStyle("padding-right").toFloat() || 0);
  489. if (!notMargin) h += (this.getStyle("margin-left").toFloat() || 0)+ (this.getStyle("margin-right").toFloat() || 0);
  490. return h;
  491. }
  492. });
  493. Object.copy = function(from, to){
  494. Object.each(from, function(value, key){
  495. switch (typeOf(value)){
  496. case "object":
  497. if (!to[key]) to[key]={};
  498. Object.copy(value, to[key]);
  499. break;
  500. default:
  501. to[key] = value;
  502. }
  503. });
  504. };
  505. if (window.JSON) JSON.format = JSON.encode;
  506. if (window.Slick) {
  507. Slick.definePseudo('src', function (value) {
  508. return Element.get(this, "src").indexOf(value) !== -1;
  509. });
  510. Slick.definePseudo('srcarr', function (value) {
  511. var vList = value.split(",");
  512. var src = Element.get(this, "src");
  513. var flag = false;
  514. for (var i = 0; i < vList.length; i++) {
  515. if (src.indexOf(vList[i]) !== -1) {
  516. flag = true;
  517. break;
  518. }
  519. }
  520. return flag;
  521. });
  522. Slick.definePseudo('ahref', function (value) {
  523. var href = Element.get(this, "href");
  524. if (!href) href = "";
  525. href = href.toString().toLowerCase();
  526. return (href.indexOf(value) !== -1);
  527. });
  528. Slick.definePseudo('rowspanBefore', function (line) {
  529. var tr = MWF.getParent(this, "tr");
  530. var rowspan = this.get("rowspan").toInt() || 1;
  531. var currentRowIndex = tr.rowIndex.toInt();
  532. return rowspan > 1 && currentRowIndex < line.toInt() && currentRowIndex + rowspan - 1 >= line;
  533. });
  534. Slick.definePseudo('rowspan', function () {
  535. var rowspan = this.get("rowspan").toInt() || 1;
  536. return rowspan > 1;
  537. });
  538. Slick.definePseudo('colspanBefore', function (col) {
  539. var tr = MWF.getParent(this, "tr");
  540. var colspan = this.get("colspan").toInt() || 1;
  541. var currentColIndex = this.cellIndex.toInt();
  542. return colspan > 1 && currentColIndex < col.toInt() && currentColIndex + colspan - 1 >= col.toInt();
  543. });
  544. Slick.definePseudo('colspan', function () {
  545. var colspan = this.get("colspan").toInt() || 1;
  546. return colspan > 1;
  547. });
  548. }
  549. o2.common = o2.common || {};
  550. o2.common.encodeHtml = function(str){
  551. str = str.toString();
  552. str = str.replace(/\&/g, "&amp;");
  553. str = str.replace(/>/g, "&gt;");
  554. str = str.replace(/</g, "&lt;");
  555. return str.replace(/\"/g, "&quot;");
  556. };
  557. o2.common.getResponseTextPost = function(path, body, contentType){
  558. var returnText = "";
  559. var options = {
  560. url: path,
  561. async: false,
  562. data: body,
  563. method: "post",
  564. onSuccess: function(esponseTree, responseElements, responseHTML, responseJavaScript){
  565. returnText = responseHTML;
  566. }
  567. };
  568. var r = new Request.HTML(options);
  569. r.send();
  570. return returnText;
  571. };
  572. o2.common.getResponseText = function(path){
  573. var returnText = "";
  574. var options = {
  575. url: path,
  576. async: false,
  577. method: "get",
  578. onSuccess: function(esponseTree, responseElements, responseHTML, responseJavaScript){
  579. returnText = responseHTML;
  580. }
  581. };
  582. var r = new Request.HTML(options);
  583. r.send();
  584. return returnText;
  585. };
  586. o2.common.toDate = function(str){
  587. var tmpArr = str.split(" ");
  588. if (!tmpArr[1]) tmpArr.push("0:0:0");
  589. var dateArr = tmpArr[0].split("-");
  590. var timeArr = tmpArr[1].split(":");
  591. return new Date(dateArr[0],parseInt(dateArr[1])-1,dateArr[2],timeArr[0],timeArr[1],timeArr[2]);
  592. };
  593. o2.common.toDate = function(str){
  594. var tmpArr = str.split(" ");
  595. if (!tmpArr[1]) tmpArr.push("0:0:0");
  596. var dateArr = tmpArr[0].split("-");
  597. var timeArr = tmpArr[1].split(":");
  598. return new Date(dateArr[0],parseInt(dateArr[1])-1,dateArr[2],timeArr[0],timeArr[1],timeArr[2]);
  599. };
  600. o2.grayscale = function(src, width, height, callback){
  601. try {
  602. var canvas = document.createElement('canvas');
  603. var ctx = canvas.getContext('2d');
  604. var imgObj = new Image();
  605. imgObj.src = src;
  606. canvas.width = width || imgObj.width;
  607. canvas.height = height || imgObj.height;
  608. ctx.drawImage(imgObj, 0, 0);
  609. var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
  610. for(var y = 0; y < imgPixels.height; y++){
  611. for(var x = 0; x < imgPixels.width; x++){
  612. var i = (y * 4) * imgPixels.width + x * 4;
  613. var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
  614. imgPixels.data[i] = avg;
  615. imgPixels.data[i + 1] = avg;
  616. imgPixels.data[i + 2] = avg;
  617. }
  618. }
  619. ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
  620. var src1 = canvas.toDataURL();
  621. //var blob = canvas.toBlob();
  622. canvas.destroy();
  623. return {"status": "success", "src": src1};
  624. }catch(e){
  625. return {"status": "error", "src": src}
  626. }
  627. };
  628. o2.eventPosition = function(e){
  629. var x = 0;
  630. var y = 0;
  631. if (Browser.name=="firefox"){
  632. x = parseFloat(e.event.clientX || e.event.x);
  633. y = parseFloat(e.event.clientY || e.event.y);
  634. }else{
  635. x = parseFloat(e.event.x);
  636. y = parseFloat(e.event.y);
  637. }
  638. return {"x": x, "y": y};
  639. };
  640. if (window.Browser){
  641. if (Browser.name==="ie" && Browser.version<9){
  642. Browser.ieuns = true;
  643. }else if(Browser.name==="ie" && Browser.version<10){
  644. Browser.iecomp = true;
  645. }
  646. if (Browser.iecomp){
  647. o2.load("ie_adapter", null, false);
  648. o2.session.isDebugger = true;
  649. //layout["debugger"] = true;
  650. }
  651. o2.session.isMobile = (["mac", "win", "linux"].indexOf(Browser.Platform.name)===-1);
  652. }
  653. })();
  654. o2.more = true;