Dialog.js 18 KB

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