Dialog.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. this.title.addEvent("mousedown", 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. "container": content
  148. });
  149. }.bind(this));
  150. this.title.addEvent("mouseup", function(){
  151. this.node.removeEvents("mousedown");
  152. this.title.addEvent("mousedown", function(){
  153. var content;
  154. if (layout.app) content = layout.app.content;
  155. if (layout.desktop.currentApp) content = layout.desktop.currentApp.content;
  156. this.containerDrag = new Drag.Move(this.node, {
  157. "container": content
  158. });
  159. }.bind(this));
  160. }.bind(this));
  161. },
  162. setResizeNode: function(){
  163. //未实现................................
  164. if (!this.options.isResize){
  165. if (this.resizeNode) this.resizeNode.hide();
  166. }else{
  167. if (this.resizeNode){
  168. this.node.makeResizable({
  169. "handle": this.resizeNode || this.bottom,
  170. "limit": {x:[200, null], y:[150, null]},
  171. "onDrag": function(){
  172. var size = this.node.getComputedSize();
  173. // this.css.to.width = size.totalWidth;
  174. // this.css.to.height = size.totalHeight;
  175. this.css.to.width = size.width;
  176. this.css.to.height = size.height;
  177. this.setContentSize(size.height, size.width);
  178. this.fireEvent("resize");
  179. }.bind(this),
  180. "onComplete": function(){
  181. this.fireEvent("resizeCompleted");
  182. }.bind(this)
  183. });
  184. }
  185. }
  186. },
  187. getAction: function(){
  188. //未完成................................
  189. if (this.options.isClose){
  190. this.closeAction = new Element("div", {"styles": this.css.closeAction}).inject(this.titleAction);
  191. this.closeAction.addEvent("click", this.close.bind(this));
  192. }
  193. },
  194. getButton: function(){
  195. for (i in this.options.buttons){
  196. var button = new Element("input", {
  197. "type": "button",
  198. "value": i,
  199. "styles": this.css.button,
  200. "events": {
  201. "click": this.options.buttons[i].bind(this)
  202. }
  203. }).inject(this.button);
  204. }
  205. if (this.options.buttonList){
  206. this.options.buttonList.each(function(bt){
  207. var styles = this.css.button;
  208. if( bt.type === "ok" && this.css.okButton )styles = this.css.okButton;
  209. if( bt.type === "cancel" && this.css.cancelButton )styles = this.css.cancelButton;
  210. if( bt.styles )styles = bt.styles;
  211. var button = new Element("input", {
  212. "type": "button",
  213. "value": bt.text,
  214. "styles": styles,
  215. "events": {
  216. "click": function(e){bt.action.call(this, this, e)}.bind(this)
  217. }
  218. }).inject(this.button);
  219. }.bind(this));
  220. }
  221. },
  222. getContentSize: function(height, width){
  223. var nodeHeight, nodeWidth;
  224. if (!height){
  225. if (this.options.contentHeight){
  226. nodeHeight = height = this.options.contentHeight.toFloat();
  227. }else{
  228. height = this.options.height.toFloat();
  229. }
  230. }
  231. if (!width){
  232. if (this.options.contentWidth){
  233. nodeWidth = width = this.options.contentWidth.toFloat();
  234. }else{
  235. width = this.options.width.toFloat();
  236. }
  237. }
  238. var offsetHeight = 0;
  239. var offsetWidth = 0;
  240. if (this.title){
  241. var h1 = this.title.getSize().y;
  242. var ptop1 = this.title.getStyle("padding-top").toFloat();
  243. var pbottom1 = this.title.getStyle("padding-bottom").toFloat();
  244. var mtop1 = this.title.getStyle("margin-top").toFloat();
  245. var mbottom1 = this.title.getStyle("margin-bottom").toFloat();
  246. offsetHeight += h1 + ptop1 + pbottom1 + mtop1 + mbottom1;
  247. }
  248. if (this.bottom){
  249. var h2 = this.bottom.getSize().y;
  250. var ptop2 = this.bottom.getStyle("padding-top").toFloat();
  251. var pbottom2 = this.bottom.getStyle("padding-bottom").toFloat();
  252. var mtop2 = this.bottom.getStyle("margin-top").toFloat();
  253. var mbottom2 = this.bottom.getStyle("margin-bottom").toFloat();
  254. offsetHeight += h2 + ptop2 + pbottom2 + mtop2 + mbottom2;
  255. }
  256. if (this.button){
  257. var h3 = this.button.getSize().y;
  258. var ptop3 = this.button.getStyle("padding-top").toFloat();
  259. var pbottom3 = this.button.getStyle("padding-bottom").toFloat();
  260. var mtop3 = this.button.getStyle("margin-top").toFloat();
  261. var mbottom3 = this.button.getStyle("margin-bottom").toFloat();
  262. offsetHeight += h3 + ptop3 + pbottom3 + mtop3 + mbottom3;
  263. }
  264. var ptop4 = this.content.getStyle("padding-top").toFloat();
  265. var pbottom4 = this.content.getStyle("padding-bottom").toFloat();
  266. var mtop4 = this.content.getStyle("margin-top").toFloat();
  267. var mbottom4 = this.content.getStyle("margin-bottom").toFloat();
  268. offsetHeight += ptop4 + pbottom4 + mtop4 + mbottom4;
  269. if (nodeHeight){
  270. nodeHeight = nodeHeight + offsetHeight+2;
  271. }else {
  272. height = height - offsetHeight;
  273. }
  274. //if (this.content.getParent().getStyle("overflow-x")!="hidden" ) height = height-18;
  275. var pleft = this.content.getStyle("padding-left").toFloat();
  276. var pright = this.content.getStyle("padding-right").toFloat();
  277. var mleft = this.content.getStyle("margin-left").toFloat();
  278. var mright = this.content.getStyle("margin-right").toFloat();
  279. offsetWidth = pleft+pright+mleft+mright;
  280. //width = width-pleft-pright-mleft-mright;
  281. //if (this.content.getParent().getStyle("overflow-y")!="hidden" ) width = width-18;
  282. if (nodeWidth){
  283. nodeWidth = nodeWidth+offsetWidth;
  284. }else{
  285. width = width-offsetWidth;
  286. }
  287. if (nodeHeight) {
  288. this.options.height = nodeHeight;
  289. this.options.contentHeight = null;
  290. this.options.fromTop = this.options.fromTop.toFloat()-offsetHeight/2;
  291. this.options.top = this.options.top.toFloat()-offsetHeight/2;
  292. this.css.to.height = nodeHeight+"px";
  293. this.css.to.top = this.options.top+"px";
  294. this.css.from.top = this.options.fromTop+"px";
  295. }
  296. if (nodeWidth){
  297. this.options.width = nodeWidth;
  298. this.options.contentWidth = null;
  299. this.options.fromLeft = this.options.fromLeft.toFloat()-offsetWidth/2;
  300. this.options.left = this.options.left.toFloat()-offsetWidth/2;
  301. this.css.to.width = nodeWidth+"px";
  302. this.css.to.left = this.options.left+"px";
  303. this.css.from.left = this.options.fromLeft+"px";
  304. }
  305. if (!height || height<0){
  306. this.content.setStyles({"overflow": "hidden", "height": "auto", "width": ""+width+"px"});
  307. height = this.content.getSize().y;
  308. var h = height + h1 + ptop1 + pbottom1 + mtop1 + mbottom1;
  309. h = h + h2 + ptop2 + pbottom2 + mtop2 + mbottom2;
  310. h = h + h3 + ptop3 + pbottom3 + mtop3 + mbottom3;
  311. h = h + ptop4 + pbottom4 + mtop4 + mbottom4;
  312. this.css.to.height = h;
  313. }
  314. // var ptop5 = this.node.getStyle("padding-top").toFloat();
  315. // var pbottom5 = this.node.getStyle("padding-bottom").toFloat();
  316. // height = height - ptop5 - pbottom5;
  317. return {"height": height+"px", "width": width+"px"};
  318. },
  319. setContentSize: function(height, width){
  320. //this.content.setStyle("height", this.getContentSize(height));
  321. // if (!this.options.height && !height){
  322. // this.content.setStyle("height", "auto");
  323. // this.content.setStyle("overflow", "hidden");
  324. // this.content.setStyle("width", "auto");
  325. // }else{
  326. this.content.setStyles(this.getContentSize(height, width));
  327. this.content.setStyle("width", "auto");
  328. //}
  329. },
  330. reCenter: function(){
  331. var size = this.node.getSize();
  332. var container = $(document.body);
  333. if (layout.desktop.currentApp){
  334. container = layout.desktop.currentApp.content;
  335. }else{
  336. if (this.options.container){
  337. if (this.options.container.getSize().y<$(document.body).getSize().y){
  338. container = this.options.container;
  339. }
  340. }
  341. }
  342. // if (this.options.container){
  343. // if (this.options.container.getSize().y<$(document.body).getSize().y){
  344. // container = this.options.container;
  345. // }
  346. // }
  347. var p = o2.getCenter(size, container, container);
  348. if (p.y<0) p.y = 0;
  349. this.options.top = p.y;
  350. this.options.left = p.x;
  351. this.css.to.top = this.options.top+"px";
  352. this.css.to.left = this.options.left+"px";
  353. this.node.setStyles({
  354. "top": this.css.to.top,
  355. "left": this.css.to.left
  356. });
  357. },
  358. getTitle: function(){
  359. this.titleText.set("text", this.options.title);
  360. },
  361. getContent: function(){
  362. this.content.setStyles(this.css.content);
  363. if (this.options.content){
  364. this.options.content.inject(this.content);
  365. }else if (this.options.url){
  366. this.content.set("load", {"method": "get", "async": false});
  367. $(this.content).load(this.options.url);
  368. /*
  369. var request = new Request.HTML({
  370. url: this.options.url,
  371. method: "GET",
  372. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  373. alert(responseHTML);
  374. this.content.set("html", responseHTML);
  375. }.bind(this),
  376. onFailure: function(xhr){
  377. alert("回退出现错误:"+xhr.status+" "+xhr.statusText);
  378. window.close();
  379. }
  380. });*/
  381. }else if (this.options.html){
  382. this.content.set("html", this.options.html);
  383. }else if (this.options.text){
  384. this.content.set("text", this.options.text);
  385. }
  386. // this.content.addEvent("selectstart", function(e){
  387. // e.preventDefault();
  388. // });
  389. },
  390. show: function(){
  391. if (this.options.mark) this._markShow();
  392. if (!this.morph){
  393. this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
  394. }
  395. if (this.fireEvent("queryShow")){
  396. this.node.setStyle("display", "block");
  397. // this.node.setStyles(t);
  398. // if (this.titleText) this.getTitle();
  399. // if (this.button) this.getButton();
  400. // // this.content.setStyle("display", "block");
  401. // //this.fireEvent("postShow");
  402. this.morph.start(this.css.to).chain(function(){
  403. if (this.titleText) this.getTitle();
  404. if (this.button) this.getButton();
  405. // this.content.setStyle("display", "block");
  406. this.fireEvent("postShow");
  407. }.bind(this));
  408. }
  409. },
  410. hide: function() {
  411. if (!this.morph){
  412. this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
  413. }
  414. if (this.fireEvent("queryHide")){
  415. if (this.titleText) this.titleText.set("text", "");
  416. if (this.button) this.button.set("html", "");
  417. this.morph.start(this.css.from).chain(function(){
  418. this._markHide();
  419. this.node.setStyle("display", "none");
  420. this.fireEvent("postHide");
  421. }.bind(this));
  422. }
  423. },
  424. close: function(){
  425. if (!this.morph){
  426. this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
  427. }
  428. if (this.fireEvent("queryClose")){
  429. this.morph.start(this.css.from).chain(function(){
  430. this._markHide();
  431. this.node.destroy();
  432. this.node = null;
  433. this.fireEvent("postClose");
  434. }.bind(this));
  435. }
  436. },
  437. _markShow: function(){
  438. if (this.options.mark){
  439. if (!this.markNode){
  440. var size = o2.getMarkSize(this.options.maskNode);
  441. this.markNode = new Element("div", {
  442. styles: this.css.mark
  443. }).inject(this.options.container || $(document.body));
  444. this.markNode.set("styles", {
  445. "height": size.y,
  446. "width": size.x
  447. });
  448. }
  449. this.markNode.setStyle("display", "block");
  450. }
  451. },
  452. _markHide: function(){
  453. if (this.markNode){
  454. this.markNode.setStyle("display", "none");
  455. }
  456. }
  457. });