Dialog.js 16 KB

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