Dialog.js 16 KB

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