Dialog.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. o2.widget = o2.widget || {};
  2. o2.widget.Dialog = o2.DL = new Class({
  3. Implements: [Options, Events],
  4. Extends: o2.widget.Common,
  5. options: {
  6. "style": "default",
  7. "title": "dialog",
  8. "width": "300",
  9. "height": "150",
  10. "contentWidth": null,
  11. "contentHeight": null,
  12. "top": "0",
  13. "left": "0",
  14. "fromTop": "0",
  15. "fromLeft": "0",
  16. "mark": true,
  17. "html": "",
  18. "text": "",
  19. "url": "",
  20. "content": null,
  21. "lp": null,
  22. "isMax": false,
  23. "isClose": false,
  24. "isResize": true,
  25. "isMove": true,
  26. "isTitle": true,
  27. "buttons": null,
  28. "buttonList": null,
  29. "maskNode" : null,
  30. "transition": null,
  31. "duration": 200,
  32. "container": null
  33. },
  34. initialize: function(options){
  35. this.setOptions(options);
  36. this.path = o2.session.path+"/widget/$Dialog/";
  37. this.cssPath = o2.session.path+"/widget/$Dialog/"+this.options.style+"/css.wcss";
  38. this._loadCss();
  39. this.reStyle();
  40. // this.css.to.height = this.options.height;
  41. // this.css.to.width = this.options.width;
  42. // this.css.to.top = this.options.top;
  43. // this.css.to.left = this.options.left;
  44. // this.css.to.top = this.options.top;
  45. // this.css.from.top = this.options.fromTop;
  46. // this.css.from.left = this.options.fromLeft;
  47. this.fireEvent("queryLoad");
  48. this.getContentUrl();
  49. var request = new Request.HTML({
  50. url: this.contentUrl,
  51. method: "GET",
  52. async: false,
  53. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  54. this.node = responseTree[0];
  55. this.getDialogNode();
  56. this.fireEvent("postLoad");
  57. }.bind(this),
  58. onFailure: function(xhr){
  59. alert(xhr);
  60. }
  61. });
  62. request.send();
  63. },
  64. getContentUrl: function(){
  65. this.contentUrl = o2.session.path+"/widget/$Dialog/"+this.options.style+"/dialog.html";
  66. },
  67. reStyle: function(options){
  68. if (options) this.setOptions(options);
  69. this.css.to.height = this.options.height+"px";
  70. this.css.to.width = this.options.width+"px";
  71. this.css.to.top = this.options.top+"px";
  72. this.css.to.left = this.options.left+"px";
  73. //this.css.to.top = this.options.top+"px";
  74. this.css.from.top = this.options.fromTop+"px";
  75. this.css.from.left = this.options.fromLeft+"px";
  76. if (this.node) this.node.set("styles", this.css.from);
  77. },
  78. getParentSelect: function(node){
  79. var select = "";
  80. var pnode = node.getParent();
  81. while (!select && pnode){
  82. select = pnode.getStyle("-webkit-user-select");
  83. var pnode = pnode.getParent();
  84. }
  85. return select;
  86. },
  87. getDialogNode: function(){
  88. this.node.set("styles", this.css.from);
  89. this.node.inject(this.options.container || $(document.body));
  90. this.node.addEvent("selectstart", function(e){
  91. var select = e.target.getStyle("-webkit-user-select");
  92. if (!select) select = this.getParentSelect(e.target);
  93. if (!select){
  94. select = "none";
  95. }else{
  96. select = select.toString().toLowerCase();
  97. }
  98. var tag = e.target.tagName.toString().toLowerCase();
  99. if (select!="text" && select!="auto" && ["input", "textarea"].indexOf(tag)==-1) e.preventDefault();
  100. }.bind(this));
  101. this.title = this.node.getElement(".MWF_dialod_title");
  102. this.titleCenter = this.node.getElement(".MWF_dialod_title_center");
  103. this.titleRefresh = this.node.getElement(".MWF_dialod_title_refresh");
  104. this.titleText = this.node.getElement(".MWF_dialod_title_text");
  105. this.titleAction = this.node.getElement(".MWF_dialod_title_action");
  106. this.under = this.node.getElement(".MWF_dialod_under");
  107. this.content = this.node.getElement(".MWF_dialod_content");
  108. this.bottom = this.node.getElement(".MWF_dialod_bottom");
  109. this.resizeNode = this.node.getElement(".MWF_dialod_bottom_resize");
  110. this.button = this.node.getElement(".MWF_dialod_button");
  111. if (!this.options.isTitle) {
  112. this.title.destroy();
  113. this.title = null;
  114. this.titleCenter = null;
  115. this.titleRefresh = null;
  116. this.titleText = null;
  117. this.titleAction = null;
  118. }
  119. if (this.title) this.title.setStyles(this.css.MWF_dialod_title);
  120. if (this.titleCenter) this.titleCenter.setStyles(this.css.MWF_dialod_title_center);
  121. if (this.titleRefresh) this.titleRefresh.setStyles(this.css.MWF_dialod_title_refresh);
  122. if (this.titleText) this.titleText.setStyles(this.css.MWF_dialod_title_text);
  123. if (this.titleAction) this.titleAction.setStyles(this.css.MWF_dialod_title_action);
  124. if (this.under) this.under.setStyles(this.css.MWF_dialod_under);
  125. if (this.content) this.content.setStyles(this.css.MWF_dialod_content);
  126. if (this.bottom) this.bottom.setStyles(this.css.MWF_dialod_bottom);
  127. if (this.resizeNode) this.resizeNode.setStyles(this.css.MWF_dialod_bottom_resize);
  128. if (this.button) this.button.setStyles(this.css.MWF_dialod_button);
  129. if (this.title) this.setTitleEvent();
  130. if (this.titleRefresh) this.setTitleRefreshNode();
  131. // if (this.titleText) this.getTitle();
  132. if (this.content) this.getContent();
  133. if (this.titleAction) this.getAction();
  134. if (this.resizeNode) this.setResizeNode();
  135. // if (this.button) this.getButton();
  136. if (this.content) this.setContentSize();
  137. },
  138. setTitleRefreshNode: function(){
  139. this.titleRefresh.setStyles(this.css.titleRefresh);
  140. this.titleRefresh.set("title", o2.LP.widget.refresh);
  141. },
  142. setTitleEvent: function(){
  143. var content;
  144. if (layout.app) content = layout.app.content;
  145. if (layout.desktop.currentApp) content = layout.desktop.currentApp.content;
  146. this.containerDrag = new Drag.Move(this.node, {
  147. "handle": this.title,
  148. "container": this.options.container || this.markNode || content,
  149. "snap": 5
  150. });
  151. // this.title.addEvent("mousedown", function(e){
  152. // var content;
  153. // if (layout.app) content = layout.app.content;
  154. // if (layout.desktop.currentApp) content = layout.desktop.currentApp.content;
  155. // this.containerDrag = new Drag.Move(this.node, {
  156. // "container": content
  157. // });
  158. // this.containerDrag.start();
  159. // }.bind(this));
  160. // this.title.addEvent("mouseup", function(){
  161. // this.node.removeEvents("mousedown");
  162. // this.title.addEvent("mousedown", function(){
  163. // var content;
  164. // if (layout.app) content = layout.app.content;
  165. // if (layout.desktop.currentApp) content = layout.desktop.currentApp.content;
  166. // this.containerDrag = new Drag.Move(this.node, {
  167. // "container": content
  168. // });
  169. // this.containerDrag.start();
  170. // }.bind(this));
  171. // }.bind(this));
  172. },
  173. setResizeNode: function(){
  174. //未实现................................
  175. if (!this.options.isResize){
  176. if (this.resizeNode) this.resizeNode.hide();
  177. }else{
  178. if (this.resizeNode){
  179. this.node.makeResizable({
  180. "handle": this.resizeNode || this.bottom,
  181. "limit": {x:[200, null], y:[150, null]},
  182. "onDrag": function(){
  183. var size = this.node.getComputedSize();
  184. // this.css.to.width = size.totalWidth;
  185. // this.css.to.height = size.totalHeight;
  186. this.css.to.width = size.width;
  187. this.css.to.height = size.height;
  188. this.setContentSize(size.height, size.width);
  189. this.fireEvent("resize");
  190. }.bind(this),
  191. "onComplete": function(){
  192. this.fireEvent("resizeCompleted");
  193. }.bind(this)
  194. });
  195. }
  196. }
  197. },
  198. getAction: function(){
  199. //未完成................................
  200. if (this.options.isClose){
  201. this.closeAction = new Element("div", {"styles": this.css.closeAction}).inject(this.titleAction);
  202. this.closeAction.addEvent("click", this.close.bind(this));
  203. }
  204. },
  205. getButton: function(){
  206. for (i in this.options.buttons){
  207. var button = new Element("input", {
  208. "type": "button",
  209. "value": i,
  210. "class": "mainColor_bg",
  211. "styles": this.css.button,
  212. "events": {
  213. "click": this.options.buttons[i].bind(this)
  214. }
  215. }).inject(this.button);
  216. }
  217. if (this.options.buttonList){
  218. this.options.buttonList.each(function(bt){
  219. var styles = this.css.button;
  220. if( bt.type === "ok" && this.css.okButton )styles = this.css.okButton;
  221. if( bt.type === "cancel" && this.css.cancelButton )styles = this.css.cancelButton;
  222. if( bt.styles )styles = bt.styles;
  223. var button = new Element("input", {
  224. "type": "button",
  225. "value": bt.text,
  226. "title": bt.title,
  227. "class": (bt.type!=="cancel") ? "mainColor_bg" : "",
  228. "styles": styles,
  229. "events": {
  230. "click": function(e){bt.action.call(this, this, e)}.bind(this)
  231. }
  232. }).inject(this.button);
  233. }.bind(this));
  234. }
  235. },
  236. getContentSize: function(height, width){
  237. var nodeHeight, nodeWidth;
  238. if (!height){
  239. if (this.options.contentHeight){
  240. nodeHeight = height = this.options.contentHeight.toFloat();
  241. }else{
  242. height = this.options.height.toFloat();
  243. }
  244. }
  245. if (!width){
  246. if (this.options.contentWidth){
  247. nodeWidth = width = this.options.contentWidth.toFloat();
  248. }else{
  249. width = this.options.width.toFloat();
  250. }
  251. }
  252. var offsetHeight = 0;
  253. var offsetWidth = 0;
  254. if (this.title){
  255. var h1 = this.title.getSize().y;
  256. var ptop1 = this.title.getStyle("padding-top").toFloat();
  257. var pbottom1 = this.title.getStyle("padding-bottom").toFloat();
  258. var mtop1 = this.title.getStyle("margin-top").toFloat();
  259. var mbottom1 = this.title.getStyle("margin-bottom").toFloat();
  260. offsetHeight += h1 + ptop1 + pbottom1 + mtop1 + mbottom1;
  261. }
  262. if (this.bottom){
  263. var h2 = this.bottom.getSize().y;
  264. var ptop2 = this.bottom.getStyle("padding-top").toFloat();
  265. var pbottom2 = this.bottom.getStyle("padding-bottom").toFloat();
  266. var mtop2 = this.bottom.getStyle("margin-top").toFloat();
  267. var mbottom2 = this.bottom.getStyle("margin-bottom").toFloat();
  268. offsetHeight += h2 + ptop2 + pbottom2 + mtop2 + mbottom2;
  269. }
  270. if (this.button){
  271. var h3 = this.button.getSize().y;
  272. var ptop3 = this.button.getStyle("padding-top").toFloat();
  273. var pbottom3 = this.button.getStyle("padding-bottom").toFloat();
  274. var mtop3 = this.button.getStyle("margin-top").toFloat();
  275. var mbottom3 = this.button.getStyle("margin-bottom").toFloat();
  276. offsetHeight += h3 + ptop3 + pbottom3 + mtop3 + mbottom3;
  277. }
  278. var ptop4 = this.content.getStyle("padding-top").toFloat();
  279. var pbottom4 = this.content.getStyle("padding-bottom").toFloat();
  280. var mtop4 = this.content.getStyle("margin-top").toFloat();
  281. var mbottom4 = this.content.getStyle("margin-bottom").toFloat();
  282. offsetHeight += ptop4 + pbottom4 + mtop4 + mbottom4;
  283. var maxHeight = 0;
  284. if (nodeHeight){
  285. nodeHeight = nodeHeight + offsetHeight+2;
  286. }else {
  287. height = height - offsetHeight;
  288. if( height < 0 ){
  289. maxHeight = (this.options.container || $(document.body)).getSize().y;
  290. maxHeight = maxHeight - offsetHeight - 10;
  291. }
  292. }
  293. //if (this.content.getParent().getStyle("overflow-x")!="hidden" ) height = height-18;
  294. var pleft = this.content.getStyle("padding-left").toFloat();
  295. var pright = this.content.getStyle("padding-right").toFloat();
  296. var mleft = this.content.getStyle("margin-left").toFloat();
  297. var mright = this.content.getStyle("margin-right").toFloat();
  298. offsetWidth = pleft+pright+mleft+mright;
  299. //width = width-pleft-pright-mleft-mright;
  300. //if (this.content.getParent().getStyle("overflow-y")!="hidden" ) width = width-18;
  301. if (nodeWidth){
  302. nodeWidth = nodeWidth+offsetWidth;
  303. }else{
  304. width = width-offsetWidth;
  305. }
  306. if (nodeHeight) {
  307. this.options.height = nodeHeight;
  308. this.options.contentHeight = null;
  309. this.options.fromTop = this.options.fromTop.toFloat()-offsetHeight/2;
  310. this.options.top = this.options.top.toFloat()-offsetHeight/2;
  311. this.css.to.height = nodeHeight+"px";
  312. this.css.to.top = this.options.top+"px";
  313. this.css.from.top = this.options.fromTop+"px";
  314. }
  315. if (nodeWidth){
  316. this.options.width = nodeWidth;
  317. this.options.contentWidth = null;
  318. this.options.fromLeft = this.options.fromLeft.toFloat()-offsetWidth/2;
  319. this.options.left = this.options.left.toFloat()-offsetWidth/2;
  320. this.css.to.width = nodeWidth+"px";
  321. this.css.to.left = this.options.left+"px";
  322. this.css.from.left = this.options.fromLeft+"px";
  323. }
  324. if (!height || height<0){
  325. this.content.setStyles({"overflow": "hidden", "height": "auto", "width": ""+width+"px"});
  326. if( maxHeight )this.content.setStyles({"max-height": maxHeight+"px"});
  327. height = this.content.getSize().y;
  328. var h = height + h1 + ptop1 + pbottom1 + mtop1 + mbottom1;
  329. h = h + h2 + ptop2 + pbottom2 + mtop2 + mbottom2;
  330. h = h + h3 + ptop3 + pbottom3 + mtop3 + mbottom3;
  331. h = h + ptop4 + pbottom4 + mtop4 + mbottom4;
  332. this.css.to.height = h;
  333. }
  334. // var ptop5 = this.node.getStyle("padding-top").toFloat();
  335. // var pbottom5 = this.node.getStyle("padding-bottom").toFloat();
  336. // height = height - ptop5 - pbottom5;
  337. return {"height": height+"px", "width": width+"px"};
  338. },
  339. setContentSize: function(height, width){
  340. //this.content.setStyle("height", this.getContentSize(height));
  341. // if (!this.options.height && !height){
  342. // this.content.setStyle("height", "auto");
  343. // this.content.setStyle("overflow", "hidden");
  344. // this.content.setStyle("width", "auto");
  345. // }else{
  346. this.content.setStyles(this.getContentSize(height, width));
  347. this.content.setStyle("width", "auto");
  348. //}
  349. },
  350. reCenter: function(){
  351. var size = this.node.getSize();
  352. var container = $(document.body);
  353. if (layout.desktop.currentApp){
  354. container = layout.desktop.currentApp.content;
  355. }else{
  356. if (this.options.container){
  357. if (this.options.container.getSize().y<$(document.body).getSize().y){
  358. container = this.options.container;
  359. }
  360. }
  361. }
  362. // if (this.options.container){
  363. // if (this.options.container.getSize().y<$(document.body).getSize().y){
  364. // container = this.options.container;
  365. // }
  366. // }
  367. var p = o2.getCenter(size, container, container);
  368. if (p.y<0) p.y = 0;
  369. this.options.top = p.y;
  370. this.options.left = p.x;
  371. this.css.to.top = this.options.top+"px";
  372. this.css.to.left = this.options.left+"px";
  373. this.node.setStyles({
  374. "top": this.css.to.top,
  375. "left": this.css.to.left
  376. });
  377. },
  378. getTitle: function(){
  379. this.titleText.set("text", this.options.title);
  380. },
  381. getContent: function(){
  382. this.content.setStyles(this.css.content);
  383. if (this.options.content){
  384. this.options.content.inject(this.content);
  385. }else if (this.options.url){
  386. this.content.set("load", {"method": "get", "async": false});
  387. $(this.content).loadHtml(this.options.url, {"bind": {"lp": this.options.lp}});
  388. /*
  389. var request = new Request.HTML({
  390. url: this.options.url,
  391. method: "GET",
  392. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  393. alert(responseHTML);
  394. this.content.set("html", responseHTML);
  395. }.bind(this),
  396. onFailure: function(xhr){
  397. alert("回退出现错误:"+xhr.status+" "+xhr.statusText);
  398. window.close();
  399. }
  400. });*/
  401. }else if (this.options.html){
  402. this.content.set("html", this.options.html);
  403. }else if (this.options.text){
  404. this.content.set("text", this.options.text);
  405. }
  406. // this.content.addEvent("selectstart", function(e){
  407. // e.preventDefault();
  408. // });
  409. },
  410. show: function(){
  411. if (this.options.mark) this._markShow();
  412. if (!this.morph){
  413. this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
  414. }
  415. if (this.fireEvent("queryShow")){
  416. this.node.setStyle("display", "block");
  417. // this.node.setStyles(t);
  418. // if (this.titleText) this.getTitle();
  419. // if (this.button) this.getButton();
  420. // // this.content.setStyle("display", "block");
  421. // //this.fireEvent("postShow");
  422. var pn = this.node.getOffsetParent() || this.node.getParent();
  423. var p = pn.getPosition();
  424. var scrollParent = this.getScrollableParent(this.node);
  425. var h = this.css.to.height.toInt();
  426. var y = this.css.to.top.toInt();
  427. y = y+p.y;
  428. var windowHeight = window.innerHeight.toInt();
  429. if( layout.mobile ){
  430. var toolbar = $(document.body).getElement(".o2_form_mobile_actions");
  431. if(toolbar)windowHeight = windowHeight - toolbar.getSize().y;
  432. }
  433. var ih = windowHeight + scrollParent.getScroll().y;
  434. if (h+y> ih){
  435. y = ih-p.y-h-20;
  436. if (y<0) y=0;
  437. this.css.to.top = ""+y+"px";
  438. }
  439. this.morph.start(this.css.to).chain(function(){
  440. if (this.titleText) this.getTitle();
  441. if (this.button) this.getButton();
  442. // this.content.setStyle("display", "block");
  443. this.fireEvent("postShow");
  444. }.bind(this));
  445. }
  446. },
  447. getScrollableParent: function (ele) {
  448. if( !ele || ele === document.body )return document.body;
  449. if( this.isScrollable(ele) ){
  450. return ele;
  451. }else{
  452. return this.getScrollableParent(ele.parentNode);
  453. }
  454. },
  455. isScrollable: function (ele) {
  456. var hasScrollableContent = ele.scrollHeight > ele.clientHeight;
  457. var overflowYStyle = window.getComputedStyle(ele).overflowY;
  458. var isOverflowHidden = overflowYStyle.indexOf('hidden') !== -1;
  459. return hasScrollableContent && !isOverflowHidden;
  460. },
  461. hide: function() {
  462. if (!this.morph){
  463. this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
  464. }
  465. if (this.fireEvent("queryHide")){
  466. if (this.titleText) this.titleText.set("text", "");
  467. if (this.button) this.button.set("html", "");
  468. this.morph.start(this.css.from).chain(function(){
  469. this._markHide();
  470. this.node.setStyle("display", "none");
  471. this.fireEvent("postHide");
  472. }.bind(this));
  473. }
  474. },
  475. close: function(){
  476. if (!this.morph){
  477. this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
  478. }
  479. if (this.fireEvent("queryClose")){
  480. this.morph.start(this.css.from).chain(function(){
  481. this._markHide();
  482. this.node.destroy();
  483. this.node = null;
  484. this.fireEvent("postClose");
  485. }.bind(this));
  486. }
  487. },
  488. _markShow: function(){
  489. if (this.options.mark){
  490. if (!this.markNode){
  491. var size = o2.getMarkSize(this.options.maskNode);
  492. this.markNode = new Element("div", {
  493. styles: this.css.mark
  494. }).inject(this.options.container || $(document.body));
  495. this.markNode.set("styles", {
  496. "height": size.y,
  497. "width": size.x
  498. });
  499. }
  500. this.markNode.setStyle("display", "block");
  501. }
  502. },
  503. _markHide: function(){
  504. if (this.markNode){
  505. this.markNode.setStyle("display", "none");
  506. this.markNode.destroy();
  507. this.markNode = null;
  508. }
  509. if (this.markNode_up){
  510. this.markNode_up.setStyle("display", "none");
  511. this.markNode_up.destroy();
  512. this.markNode_up = null;
  513. }
  514. }
  515. });