Dialog.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. "title": bt.title,
  225. "styles": styles,
  226. "events": {
  227. "click": function(e){bt.action.call(this, this, e)}.bind(this)
  228. }
  229. }).inject(this.button);
  230. }.bind(this));
  231. }
  232. },
  233. getContentSize: function(height, width){
  234. var nodeHeight, nodeWidth;
  235. if (!height){
  236. if (this.options.contentHeight){
  237. nodeHeight = height = this.options.contentHeight.toFloat();
  238. }else{
  239. height = this.options.height.toFloat();
  240. }
  241. }
  242. if (!width){
  243. if (this.options.contentWidth){
  244. nodeWidth = width = this.options.contentWidth.toFloat();
  245. }else{
  246. width = this.options.width.toFloat();
  247. }
  248. }
  249. var offsetHeight = 0;
  250. var offsetWidth = 0;
  251. if (this.title){
  252. var h1 = this.title.getSize().y;
  253. var ptop1 = this.title.getStyle("padding-top").toFloat();
  254. var pbottom1 = this.title.getStyle("padding-bottom").toFloat();
  255. var mtop1 = this.title.getStyle("margin-top").toFloat();
  256. var mbottom1 = this.title.getStyle("margin-bottom").toFloat();
  257. offsetHeight += h1 + ptop1 + pbottom1 + mtop1 + mbottom1;
  258. }
  259. if (this.bottom){
  260. var h2 = this.bottom.getSize().y;
  261. var ptop2 = this.bottom.getStyle("padding-top").toFloat();
  262. var pbottom2 = this.bottom.getStyle("padding-bottom").toFloat();
  263. var mtop2 = this.bottom.getStyle("margin-top").toFloat();
  264. var mbottom2 = this.bottom.getStyle("margin-bottom").toFloat();
  265. offsetHeight += h2 + ptop2 + pbottom2 + mtop2 + mbottom2;
  266. }
  267. if (this.button){
  268. var h3 = this.button.getSize().y;
  269. var ptop3 = this.button.getStyle("padding-top").toFloat();
  270. var pbottom3 = this.button.getStyle("padding-bottom").toFloat();
  271. var mtop3 = this.button.getStyle("margin-top").toFloat();
  272. var mbottom3 = this.button.getStyle("margin-bottom").toFloat();
  273. offsetHeight += h3 + ptop3 + pbottom3 + mtop3 + mbottom3;
  274. }
  275. var ptop4 = this.content.getStyle("padding-top").toFloat();
  276. var pbottom4 = this.content.getStyle("padding-bottom").toFloat();
  277. var mtop4 = this.content.getStyle("margin-top").toFloat();
  278. var mbottom4 = this.content.getStyle("margin-bottom").toFloat();
  279. offsetHeight += ptop4 + pbottom4 + mtop4 + mbottom4;
  280. if (nodeHeight){
  281. nodeHeight = nodeHeight + offsetHeight+2;
  282. }else {
  283. height = height - offsetHeight;
  284. }
  285. //if (this.content.getParent().getStyle("overflow-x")!="hidden" ) height = height-18;
  286. var pleft = this.content.getStyle("padding-left").toFloat();
  287. var pright = this.content.getStyle("padding-right").toFloat();
  288. var mleft = this.content.getStyle("margin-left").toFloat();
  289. var mright = this.content.getStyle("margin-right").toFloat();
  290. offsetWidth = pleft+pright+mleft+mright;
  291. //width = width-pleft-pright-mleft-mright;
  292. //if (this.content.getParent().getStyle("overflow-y")!="hidden" ) width = width-18;
  293. if (nodeWidth){
  294. nodeWidth = nodeWidth+offsetWidth;
  295. }else{
  296. width = width-offsetWidth;
  297. }
  298. if (nodeHeight) {
  299. this.options.height = nodeHeight;
  300. this.options.contentHeight = null;
  301. this.options.fromTop = this.options.fromTop.toFloat()-offsetHeight/2;
  302. this.options.top = this.options.top.toFloat()-offsetHeight/2;
  303. this.css.to.height = nodeHeight+"px";
  304. this.css.to.top = this.options.top+"px";
  305. this.css.from.top = this.options.fromTop+"px";
  306. }
  307. if (nodeWidth){
  308. this.options.width = nodeWidth;
  309. this.options.contentWidth = null;
  310. this.options.fromLeft = this.options.fromLeft.toFloat()-offsetWidth/2;
  311. this.options.left = this.options.left.toFloat()-offsetWidth/2;
  312. this.css.to.width = nodeWidth+"px";
  313. this.css.to.left = this.options.left+"px";
  314. this.css.from.left = this.options.fromLeft+"px";
  315. }
  316. if (!height || height<0){
  317. this.content.setStyles({"overflow": "hidden", "height": "auto", "width": ""+width+"px"});
  318. height = this.content.getSize().y;
  319. var h = height + h1 + ptop1 + pbottom1 + mtop1 + mbottom1;
  320. h = h + h2 + ptop2 + pbottom2 + mtop2 + mbottom2;
  321. h = h + h3 + ptop3 + pbottom3 + mtop3 + mbottom3;
  322. h = h + ptop4 + pbottom4 + mtop4 + mbottom4;
  323. this.css.to.height = h;
  324. }
  325. // var ptop5 = this.node.getStyle("padding-top").toFloat();
  326. // var pbottom5 = this.node.getStyle("padding-bottom").toFloat();
  327. // height = height - ptop5 - pbottom5;
  328. return {"height": height+"px", "width": width+"px"};
  329. },
  330. setContentSize: function(height, width){
  331. //this.content.setStyle("height", this.getContentSize(height));
  332. // if (!this.options.height && !height){
  333. // this.content.setStyle("height", "auto");
  334. // this.content.setStyle("overflow", "hidden");
  335. // this.content.setStyle("width", "auto");
  336. // }else{
  337. this.content.setStyles(this.getContentSize(height, width));
  338. this.content.setStyle("width", "auto");
  339. //}
  340. },
  341. reCenter: function(){
  342. var size = this.node.getSize();
  343. var container = $(document.body);
  344. if (layout.desktop.currentApp){
  345. container = layout.desktop.currentApp.content;
  346. }else{
  347. if (this.options.container){
  348. if (this.options.container.getSize().y<$(document.body).getSize().y){
  349. container = this.options.container;
  350. }
  351. }
  352. }
  353. // if (this.options.container){
  354. // if (this.options.container.getSize().y<$(document.body).getSize().y){
  355. // container = this.options.container;
  356. // }
  357. // }
  358. var p = o2.getCenter(size, container, container);
  359. if (p.y<0) p.y = 0;
  360. this.options.top = p.y;
  361. this.options.left = p.x;
  362. this.css.to.top = this.options.top+"px";
  363. this.css.to.left = this.options.left+"px";
  364. this.node.setStyles({
  365. "top": this.css.to.top,
  366. "left": this.css.to.left
  367. });
  368. },
  369. getTitle: function(){
  370. this.titleText.set("text", this.options.title);
  371. },
  372. getContent: function(){
  373. this.content.setStyles(this.css.content);
  374. if (this.options.content){
  375. this.options.content.inject(this.content);
  376. }else if (this.options.url){
  377. this.content.set("load", {"method": "get", "async": false});
  378. $(this.content).load(this.options.url);
  379. /*
  380. var request = new Request.HTML({
  381. url: this.options.url,
  382. method: "GET",
  383. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  384. alert(responseHTML);
  385. this.content.set("html", responseHTML);
  386. }.bind(this),
  387. onFailure: function(xhr){
  388. alert("回退出现错误:"+xhr.status+" "+xhr.statusText);
  389. window.close();
  390. }
  391. });*/
  392. }else if (this.options.html){
  393. this.content.set("html", this.options.html);
  394. }else if (this.options.text){
  395. this.content.set("text", this.options.text);
  396. }
  397. // this.content.addEvent("selectstart", function(e){
  398. // e.preventDefault();
  399. // });
  400. },
  401. show: function(){
  402. if (this.options.mark) this._markShow();
  403. if (!this.morph){
  404. this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
  405. }
  406. if (this.fireEvent("queryShow")){
  407. this.node.setStyle("display", "block");
  408. // this.node.setStyles(t);
  409. // if (this.titleText) this.getTitle();
  410. // if (this.button) this.getButton();
  411. // // this.content.setStyle("display", "block");
  412. // //this.fireEvent("postShow");
  413. this.morph.start(this.css.to).chain(function(){
  414. if (this.titleText) this.getTitle();
  415. if (this.button) this.getButton();
  416. // this.content.setStyle("display", "block");
  417. this.fireEvent("postShow");
  418. }.bind(this));
  419. }
  420. },
  421. hide: function() {
  422. if (!this.morph){
  423. this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
  424. }
  425. if (this.fireEvent("queryHide")){
  426. if (this.titleText) this.titleText.set("text", "");
  427. if (this.button) this.button.set("html", "");
  428. this.morph.start(this.css.from).chain(function(){
  429. this._markHide();
  430. this.node.setStyle("display", "none");
  431. this.fireEvent("postHide");
  432. }.bind(this));
  433. }
  434. },
  435. close: function(){
  436. if (!this.morph){
  437. this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
  438. }
  439. if (this.fireEvent("queryClose")){
  440. this.morph.start(this.css.from).chain(function(){
  441. this._markHide();
  442. this.node.destroy();
  443. this.node = null;
  444. this.fireEvent("postClose");
  445. }.bind(this));
  446. }
  447. },
  448. _markShow: function(){
  449. if (this.options.mark){
  450. if (!this.markNode){
  451. var size = o2.getMarkSize(this.options.maskNode);
  452. this.markNode = new Element("div", {
  453. styles: this.css.mark
  454. }).inject(this.options.container || $(document.body));
  455. this.markNode.set("styles", {
  456. "height": size.y,
  457. "width": size.x
  458. });
  459. }
  460. this.markNode.setStyle("display", "block");
  461. }
  462. },
  463. _markHide: function(){
  464. if (this.markNode){
  465. this.markNode.setStyle("display", "none");
  466. this.markNode.destroy();
  467. this.markNode = null;
  468. }
  469. if (this.markNode_up){
  470. this.markNode_up.setStyle("display", "none");
  471. this.markNode_up.destroy();
  472. this.markNode_up = null;
  473. }
  474. }
  475. });