Dialog.js 18 KB

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