Dialog.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. MWF.widget = MWF.widget || {};
  2. MWF.widget.Dialog = MWF.DL = new Class({
  3. Implements: [Options, Events],
  4. Extends: MWF.widget.Common,
  5. options: {
  6. "style": "default",
  7. "title": "dialog",
  8. "width": "300",
  9. "height": "150",
  10. "top": "0",
  11. "left": "0",
  12. "fromTop": "0",
  13. "fromLeft": "0",
  14. "mark": true,
  15. "html": "",
  16. "text": "",
  17. "url": "",
  18. "content": null,
  19. "isMax": false,
  20. "isClose": true,
  21. "isResize": true,
  22. "isMove": true,
  23. "buttons": null,
  24. "buttonList": null,
  25. "maskNode" : null,
  26. "container": null
  27. },
  28. initialize: function(options){
  29. this.setOptions(options);
  30. this.path = MWF.defaultPath+"/widget/$Dialog/";
  31. this.cssPath = MWF.defaultPath+"/widget/$Dialog/"+this.options.style+"/css.wcss";
  32. this._loadCss();
  33. this.reStyle();
  34. // this.css.to.height = this.options.height;
  35. // this.css.to.width = this.options.width;
  36. // this.css.to.top = this.options.top;
  37. // this.css.to.left = this.options.left;
  38. // this.css.to.top = this.options.top;
  39. // this.css.from.top = this.options.fromTop;
  40. // this.css.from.left = this.options.fromLeft;
  41. this.fireEvent("queryLoad");
  42. this.getContentUrl();
  43. var request = new Request.HTML({
  44. url: this.contentUrl,
  45. method: "GET",
  46. async: false,
  47. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  48. this.node = responseTree[0];
  49. this.getDialogNode();
  50. this.fireEvent("postLoad");
  51. }.bind(this),
  52. onFailure: function(xhr){
  53. alert(xhr);
  54. }
  55. });
  56. request.send();
  57. },
  58. getContentUrl: function(){
  59. this.contentUrl = MWF.defaultPath+"/widget/$Dialog/"+this.options.style+"/dialog.html";
  60. },
  61. reStyle: function(options){
  62. if (options) this.setOptions(options);
  63. this.css.to.height = this.options.height+"px";
  64. this.css.to.width = this.options.width+"px";
  65. this.css.to.top = this.options.top+"px";
  66. this.css.to.left = this.options.left+"px";
  67. this.css.to.top = this.options.top+"px";
  68. this.css.from.top = this.options.fromTop+"px";
  69. this.css.from.left = this.options.fromLeft+"px";
  70. if (this.node) this.node.set("styles", this.css.from);
  71. },
  72. getParentSelect: function(node){
  73. var select = ""
  74. var pnode = node.getParent();
  75. while (!select && pnode){
  76. select = pnode.getStyle("-webkit-user-select");
  77. var pnode = pnode.getParent();
  78. }
  79. return select;
  80. },
  81. getDialogNode: function(){
  82. this.node.set("styles", this.css.from);
  83. this.node.inject(this.options.container || $(document.body));
  84. this.node.addEvent("selectstart", function(e){
  85. var select = e.target.getStyle("-webkit-user-select");
  86. if (!select) select = this.getParentSelect(e.target);
  87. if (!select){
  88. select = "none";
  89. }else{
  90. select = select.toString().toLowerCase();
  91. }
  92. var tag = e.target.tagName.toString().toLowerCase();
  93. if (select!="text" && select!="auto" && ["input", "textarea"].indexOf(tag)==-1) e.preventDefault();
  94. }.bind(this));
  95. this.title = this.node.getElement(".MWF_dialod_title");
  96. this.titleCenter = this.node.getElement(".MWF_dialod_title_center");
  97. this.titleRefresh = this.node.getElement(".MWF_dialod_title_refresh");
  98. this.titleText = this.node.getElement(".MWF_dialod_title_text");
  99. this.titleAction = this.node.getElement(".MWF_dialod_title_action");
  100. this.content = this.node.getElement(".MWF_dialod_content");
  101. this.bottom = this.node.getElement(".MWF_dialod_bottom");
  102. this.resizeNode = this.node.getElement(".MWF_dialod_bottom_resize");
  103. this.button = this.node.getElement(".MWF_dialod_button");
  104. if (this.title) this.setTitleEvent();
  105. if (this.titleRefresh) this.setTitleRefreshNode();
  106. // if (this.titleText) this.getTitle();
  107. if (this.content) this.getContent();
  108. if (this.titleAction) this.getAction();
  109. if (this.resizeNode) this.setResizeNode();
  110. // if (this.button) this.getButton();
  111. if (this.content) this.setContentSize();
  112. },
  113. setTitleRefreshNode: function(){
  114. this.titleRefresh.setStyles(this.css.titleRefresh);
  115. this.titleRefresh.set("title", MWF.LP.widget.refresh);
  116. },
  117. setTitleEvent: function(){
  118. this.title.addEvent("mousedown", function(){
  119. this.containerDrag = new Drag.Move(this.node);
  120. }.bind(this));
  121. this.title.addEvent("mouseup", function(){
  122. this.node.removeEvents("mousedown");
  123. this.title.addEvent("mousedown", function(){
  124. this.containerDrag = new Drag.Move(this.node);
  125. }.bind(this));
  126. }.bind(this));
  127. },
  128. setResizeNode: function(){
  129. //未实现................................
  130. },
  131. getAction: function(){
  132. //未实现................................
  133. },
  134. getButton: function(){
  135. for (i in this.options.buttons){
  136. var button = new Element("input", {
  137. "type": "button",
  138. "value": i,
  139. "styles": this.css.button,
  140. "events": {
  141. "click": this.options.buttons[i].bind(this)
  142. }
  143. }).inject(this.button);
  144. }
  145. if (this.options.buttonList){
  146. this.options.buttonList.each(function(bt){
  147. var button = new Element("input", {
  148. "type": "button",
  149. "value": bt.text,
  150. "styles": this.css.button,
  151. "events": {
  152. "click": bt.action.bind(this, this)
  153. }
  154. }).inject(this.button);
  155. }.bind(this));
  156. }
  157. },
  158. getContentSize: function(height, width){
  159. if (!height) height = this.options.height;
  160. if (!width) width = this.options.width;
  161. if (this.title){
  162. var h1 = this.title.getSize().y;
  163. var ptop1 = this.title.getStyle("padding-top").toFloat();
  164. var pbottom1 = this.title.getStyle("padding-bottom").toFloat();
  165. var mtop1 = this.title.getStyle("margin-top").toFloat();
  166. var mbottom1 = this.title.getStyle("margin-bottom").toFloat();
  167. height = height - h1 - ptop1 - pbottom1 - mtop1 - mbottom1;
  168. }
  169. if (this.bottom){
  170. var h2 = this.bottom.getSize().y;
  171. var ptop2 = this.bottom.getStyle("padding-top").toFloat();
  172. var pbottom2 = this.bottom.getStyle("padding-bottom").toFloat();
  173. var mtop2 = this.bottom.getStyle("margin-top").toFloat();
  174. var mbottom2 = this.bottom.getStyle("margin-bottom").toFloat();
  175. height = height - h2 - ptop2 - pbottom2 - mtop2 - mbottom2;
  176. }
  177. if (this.button){
  178. var h3 = this.button.getSize().y;
  179. var ptop3 = this.button.getStyle("padding-top").toFloat();
  180. var pbottom3 = this.button.getStyle("padding-bottom").toFloat();
  181. var mtop3 = this.button.getStyle("margin-top").toFloat();
  182. var mbottom3 = this.button.getStyle("margin-bottom").toFloat();
  183. height = height - h3 - ptop3 - pbottom3 - mtop3 - mbottom3;
  184. }
  185. var ptop4 = this.content.getStyle("padding-top").toFloat();
  186. var pbottom4 = this.content.getStyle("padding-bottom").toFloat();
  187. var mtop4 = this.content.getStyle("margin-top").toFloat();
  188. var mbottom4 = this.content.getStyle("margin-bottom").toFloat();
  189. height = height - ptop4 - pbottom4 - mtop4 - mbottom4;
  190. //if (this.content.getParent().getStyle("overflow-x")!="hidden" ) height = height-18;
  191. var pleft = this.content.getStyle("padding-left").toFloat();
  192. var pright = this.content.getStyle("padding-right").toFloat();
  193. var mleft = this.content.getStyle("margin-left").toFloat();
  194. var mright = this.content.getStyle("margin-right").toFloat();
  195. width = width-pleft-pright-mleft-mright;
  196. //if (this.content.getParent().getStyle("overflow-y")!="hidden" ) width = width-18;
  197. // var ptop5 = this.node.getStyle("padding-top").toFloat();
  198. // var pbottom5 = this.node.getStyle("padding-bottom").toFloat();
  199. // height = height - ptop5 - pbottom5;
  200. return {"height": height+"px", "width": width+"px"};
  201. },
  202. setContentSize: function(height, width){
  203. //this.content.setStyle("height", this.getContentSize(height));
  204. this.content.setStyles(this.getContentSize(height, width));
  205. this.content.setStyle("width", "auto");
  206. },
  207. getTitle: function(){
  208. this.titleText.set("text", this.options.title);
  209. },
  210. getContent: function(){
  211. this.content.setStyles(this.css.content);
  212. if (this.options.content){
  213. this.options.content.inject(this.content);
  214. }else if (this.options.url){
  215. this.content.set("load", {"method": "get", "async": false});
  216. $(this.content).load(this.options.url);
  217. /*
  218. var request = new Request.HTML({
  219. url: this.options.url,
  220. method: "GET",
  221. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  222. alert(responseHTML);
  223. this.content.set("html", responseHTML);
  224. }.bind(this),
  225. onFailure: function(xhr){
  226. alert("回退出现错误:"+xhr.status+" "+xhr.statusText);
  227. window.close();
  228. }
  229. });*/
  230. }else if (this.options.html){
  231. this.content.set("html", this.options.html);
  232. }else if (this.options.text){
  233. this.content.set("text", this.options.text);
  234. }
  235. // this.content.addEvent("selectstart", function(e){
  236. // e.preventDefault();
  237. // });
  238. },
  239. show: function(){
  240. if (this.options.mark) this._markShow();
  241. if (!this.morph){
  242. this.morph = new Fx.Morph(this.node, {duration: 200});
  243. }
  244. if (this.fireEvent("queryShow")){
  245. this.node.setStyle("display", "block");
  246. this.morph.start(this.css.to).chain(function(){
  247. if (this.titleText) this.getTitle();
  248. if (this.button) this.getButton();
  249. // this.content.setStyle("display", "block");
  250. this.fireEvent("postShow");
  251. }.bind(this));
  252. }
  253. },
  254. hide: function() {
  255. if (!this.morph){
  256. this.morph = new Fx.Morph(this.node, {duration: 200});
  257. }
  258. if (this.fireEvent("queryHide")){
  259. if (this.titleText) this.titleText.set("text", "");
  260. if (this.button) this.button.set("html", "");
  261. this.morph.start(this.css.from).chain(function(){
  262. this._markHide();
  263. this.node.setStyle("display", "none");
  264. this.fireEvent("postHide");
  265. }.bind(this));
  266. }
  267. },
  268. close: function(){
  269. if (!this.morph){
  270. this.morph = new Fx.Morph(this.node, {duration: 200});
  271. }
  272. if (this.fireEvent("queryClose")){
  273. this.morph.start(this.css.from).chain(function(){
  274. this._markHide();
  275. this.node.destroy();
  276. this.node = null;
  277. this.fireEvent("postClose");
  278. }.bind(this));
  279. }
  280. },
  281. _markShow: function(){
  282. if (this.options.mark){
  283. if (!this.markNode){
  284. var size = MWF.getMarkSize(this.options.maskNode);
  285. this.markNode = new Element("div", {
  286. styles: this.css.mark
  287. }).inject(this.options.container || $(document.body));
  288. this.markNode.set("styles", {
  289. "height": size.y,
  290. "width": size.x
  291. });
  292. }
  293. this.markNode.setStyle("display", "block");
  294. }
  295. },
  296. _markHide: function(){
  297. if (this.markNode){
  298. this.markNode.setStyle("display", "none");
  299. }
  300. }
  301. });