Dialog.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. "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 = o2.session.path+"/widget/$Dialog/";
  31. this.cssPath = o2.session.path+"/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 = o2.session.path+"/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.under = this.node.getElement(".MWF_dialod_under");
  101. this.content = this.node.getElement(".MWF_dialod_content");
  102. this.bottom = this.node.getElement(".MWF_dialod_bottom");
  103. this.resizeNode = this.node.getElement(".MWF_dialod_bottom_resize");
  104. this.button = this.node.getElement(".MWF_dialod_button");
  105. if (this.title) this.title.setStyles(this.css.MWF_dialod_title);
  106. if (this.titleCenter) this.titleCenter.setStyles(this.css.MWF_dialod_title_center);
  107. if (this.titleRefresh) this.titleRefresh.setStyles(this.css.MWF_dialod_title_refresh);
  108. if (this.titleText) this.titleText.setStyles(this.css.MWF_dialod_title_text);
  109. if (this.titleAction) this.titleAction.setStyles(this.css.MWF_dialod_title_action);
  110. if (this.under) this.under.setStyles(this.css.MWF_dialod_under);
  111. if (this.content) this.content.setStyles(this.css.MWF_dialod_content);
  112. if (this.bottom) this.bottom.setStyles(this.css.MWF_dialod_bottom);
  113. if (this.resizeNode) this.resizeNode.setStyles(this.css.MWF_dialod_bottom_resize);
  114. if (this.button) this.button.setStyles(this.css.MWF_dialod_button);
  115. if (this.title) this.setTitleEvent();
  116. if (this.titleRefresh) this.setTitleRefreshNode();
  117. // if (this.titleText) this.getTitle();
  118. if (this.content) this.getContent();
  119. if (this.titleAction) this.getAction();
  120. if (this.resizeNode) this.setResizeNode();
  121. // if (this.button) this.getButton();
  122. if (this.content) this.setContentSize();
  123. },
  124. setTitleRefreshNode: function(){
  125. this.titleRefresh.setStyles(this.css.titleRefresh);
  126. this.titleRefresh.set("title", o2.LP.widget.refresh);
  127. },
  128. setTitleEvent: function(){
  129. this.title.addEvent("mousedown", function(){
  130. this.containerDrag = new Drag.Move(this.node);
  131. }.bind(this));
  132. this.title.addEvent("mouseup", function(){
  133. this.node.removeEvents("mousedown");
  134. this.title.addEvent("mousedown", function(){
  135. this.containerDrag = new Drag.Move(this.node);
  136. }.bind(this));
  137. }.bind(this));
  138. },
  139. setResizeNode: function(){
  140. //未实现................................
  141. },
  142. getAction: function(){
  143. //未实现................................
  144. },
  145. getButton: function(){
  146. for (i in this.options.buttons){
  147. var button = new Element("input", {
  148. "type": "button",
  149. "value": i,
  150. "styles": this.css.button,
  151. "events": {
  152. "click": this.options.buttons[i].bind(this)
  153. }
  154. }).inject(this.button);
  155. }
  156. if (this.options.buttonList){
  157. this.options.buttonList.each(function(bt){
  158. var button = new Element("input", {
  159. "type": "button",
  160. "value": bt.text,
  161. "styles": this.css.button,
  162. "events": {
  163. "click": bt.action.bind(this, this)
  164. }
  165. }).inject(this.button);
  166. }.bind(this));
  167. }
  168. },
  169. getContentSize: function(height, width){
  170. if (!height) height = this.options.height;
  171. if (!width) width = this.options.width;
  172. if (this.title){
  173. var h1 = this.title.getSize().y;
  174. var ptop1 = this.title.getStyle("padding-top").toFloat();
  175. var pbottom1 = this.title.getStyle("padding-bottom").toFloat();
  176. var mtop1 = this.title.getStyle("margin-top").toFloat();
  177. var mbottom1 = this.title.getStyle("margin-bottom").toFloat();
  178. height = height - h1 - ptop1 - pbottom1 - mtop1 - mbottom1;
  179. }
  180. if (this.bottom){
  181. var h2 = this.bottom.getSize().y;
  182. var ptop2 = this.bottom.getStyle("padding-top").toFloat();
  183. var pbottom2 = this.bottom.getStyle("padding-bottom").toFloat();
  184. var mtop2 = this.bottom.getStyle("margin-top").toFloat();
  185. var mbottom2 = this.bottom.getStyle("margin-bottom").toFloat();
  186. height = height - h2 - ptop2 - pbottom2 - mtop2 - mbottom2;
  187. }
  188. if (this.button){
  189. var h3 = this.button.getSize().y;
  190. var ptop3 = this.button.getStyle("padding-top").toFloat();
  191. var pbottom3 = this.button.getStyle("padding-bottom").toFloat();
  192. var mtop3 = this.button.getStyle("margin-top").toFloat();
  193. var mbottom3 = this.button.getStyle("margin-bottom").toFloat();
  194. height = height - h3 - ptop3 - pbottom3 - mtop3 - mbottom3;
  195. }
  196. var ptop4 = this.content.getStyle("padding-top").toFloat();
  197. var pbottom4 = this.content.getStyle("padding-bottom").toFloat();
  198. var mtop4 = this.content.getStyle("margin-top").toFloat();
  199. var mbottom4 = this.content.getStyle("margin-bottom").toFloat();
  200. height = height - ptop4 - pbottom4 - mtop4 - mbottom4;
  201. //if (this.content.getParent().getStyle("overflow-x")!="hidden" ) height = height-18;
  202. var pleft = this.content.getStyle("padding-left").toFloat();
  203. var pright = this.content.getStyle("padding-right").toFloat();
  204. var mleft = this.content.getStyle("margin-left").toFloat();
  205. var mright = this.content.getStyle("margin-right").toFloat();
  206. width = width-pleft-pright-mleft-mright;
  207. //if (this.content.getParent().getStyle("overflow-y")!="hidden" ) width = width-18;
  208. if (!height || height<0){
  209. this.content.setStyles({"overflow": "hidden", "height": "auto", "width": ""+width+"px"});
  210. height = this.content.getSize().y;
  211. var h = height + h1 + ptop1 + pbottom1 + mtop1 + mbottom1;
  212. h = h + h2 + ptop2 + pbottom2 + mtop2 + mbottom2;
  213. h = h + h3 + ptop3 + pbottom3 + mtop3 + mbottom3;
  214. h = h + ptop4 + pbottom4 + mtop4 + mbottom4;
  215. this.css.to.height = h;
  216. }
  217. // var ptop5 = this.node.getStyle("padding-top").toFloat();
  218. // var pbottom5 = this.node.getStyle("padding-bottom").toFloat();
  219. // height = height - ptop5 - pbottom5;
  220. return {"height": height+"px", "width": width+"px"};
  221. },
  222. setContentSize: function(height, width){
  223. //this.content.setStyle("height", this.getContentSize(height));
  224. // if (!this.options.height && !height){
  225. // this.content.setStyle("height", "auto");
  226. // this.content.setStyle("overflow", "hidden");
  227. // this.content.setStyle("width", "auto");
  228. // }else{
  229. this.content.setStyles(this.getContentSize(height, width));
  230. this.content.setStyle("width", "auto");
  231. //}
  232. },
  233. getTitle: function(){
  234. this.titleText.set("text", this.options.title);
  235. },
  236. getContent: function(){
  237. this.content.setStyles(this.css.content);
  238. if (this.options.content){
  239. this.options.content.inject(this.content);
  240. }else if (this.options.url){
  241. this.content.set("load", {"method": "get", "async": false});
  242. $(this.content).load(this.options.url);
  243. /*
  244. var request = new Request.HTML({
  245. url: this.options.url,
  246. method: "GET",
  247. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  248. alert(responseHTML);
  249. this.content.set("html", responseHTML);
  250. }.bind(this),
  251. onFailure: function(xhr){
  252. alert("回退出现错误:"+xhr.status+" "+xhr.statusText);
  253. window.close();
  254. }
  255. });*/
  256. }else if (this.options.html){
  257. this.content.set("html", this.options.html);
  258. }else if (this.options.text){
  259. this.content.set("text", this.options.text);
  260. }
  261. // this.content.addEvent("selectstart", function(e){
  262. // e.preventDefault();
  263. // });
  264. },
  265. show: function(){
  266. if (this.options.mark) this._markShow();
  267. if (!this.morph){
  268. this.morph = new Fx.Morph(this.node, {duration: 200});
  269. }
  270. if (this.fireEvent("queryShow")){
  271. this.node.setStyle("display", "block");
  272. // this.node.setStyles(t);
  273. // if (this.titleText) this.getTitle();
  274. // if (this.button) this.getButton();
  275. // // this.content.setStyle("display", "block");
  276. // //this.fireEvent("postShow");
  277. this.morph.start(this.css.to).chain(function(){
  278. if (this.titleText) this.getTitle();
  279. if (this.button) this.getButton();
  280. // this.content.setStyle("display", "block");
  281. this.fireEvent("postShow");
  282. }.bind(this));
  283. }
  284. },
  285. hide: function() {
  286. if (!this.morph){
  287. this.morph = new Fx.Morph(this.node, {duration: 200});
  288. }
  289. if (this.fireEvent("queryHide")){
  290. if (this.titleText) this.titleText.set("text", "");
  291. if (this.button) this.button.set("html", "");
  292. this.morph.start(this.css.from).chain(function(){
  293. this._markHide();
  294. this.node.setStyle("display", "none");
  295. this.fireEvent("postHide");
  296. }.bind(this));
  297. }
  298. },
  299. close: function(){
  300. if (!this.morph){
  301. this.morph = new Fx.Morph(this.node, {duration: 200});
  302. }
  303. if (this.fireEvent("queryClose")){
  304. this.morph.start(this.css.from).chain(function(){
  305. this._markHide();
  306. this.node.destroy();
  307. this.node = null;
  308. this.fireEvent("postClose");
  309. }.bind(this));
  310. }
  311. },
  312. _markShow: function(){
  313. if (this.options.mark){
  314. if (!this.markNode){
  315. var size = o2.getMarkSize(this.options.maskNode);
  316. this.markNode = new Element("div", {
  317. styles: this.css.mark
  318. }).inject(this.options.container || $(document.body));
  319. this.markNode.set("styles", {
  320. "height": size.y,
  321. "width": size.x
  322. });
  323. }
  324. this.markNode.setStyle("display", "block");
  325. }
  326. },
  327. _markHide: function(){
  328. if (this.markNode){
  329. this.markNode.setStyle("display", "none");
  330. }
  331. }
  332. });