dialog.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. ///import core
  2. ///import uicore
  3. ///import ui/mask.js
  4. ///import ui/button.js
  5. (function() {
  6. var utils = baidu.editor.utils,
  7. domUtils = baidu.editor.dom.domUtils,
  8. uiUtils = baidu.editor.ui.uiUtils,
  9. Mask = baidu.editor.ui.Mask,
  10. UIBase = baidu.editor.ui.UIBase,
  11. Button = baidu.editor.ui.Button,
  12. Dialog = (baidu.editor.ui.Dialog = function(options) {
  13. if (options.name) {
  14. var name = options.name;
  15. var cssRules = options.cssRules;
  16. if (!options.className) {
  17. options.className = "edui-for-" + name;
  18. }
  19. if (cssRules) {
  20. options.cssRules =
  21. ".edui-for-" + name + " .edui-dialog-content {" + cssRules + "}";
  22. }
  23. }
  24. this.initOptions(
  25. utils.extend(
  26. {
  27. autoReset: true,
  28. draggable: true,
  29. onok: function() {},
  30. oncancel: function() {},
  31. onclose: function(t, ok) {
  32. return ok ? this.onok() : this.oncancel();
  33. },
  34. //是否控制dialog中的scroll事件, 默认为不阻止
  35. holdScroll: false
  36. },
  37. options
  38. )
  39. );
  40. this.initDialog();
  41. });
  42. var modalMask;
  43. var dragMask;
  44. var activeDialog;
  45. Dialog.prototype = {
  46. draggable: false,
  47. uiName: "dialog",
  48. initDialog: function() {
  49. var me = this,
  50. theme = this.editor.options.theme;
  51. if (this.cssRules) {
  52. this.cssRules = ".edui-" + theme + " " + this.cssRules;
  53. utils.cssRule("edui-customize-" + this.name + "-style", this.cssRules);
  54. }
  55. this.initUIBase();
  56. this.modalMask =
  57. modalMask ||
  58. (modalMask = new Mask({
  59. className: "edui-dialog-modalmask",
  60. theme: theme,
  61. onclick: function() {
  62. activeDialog && activeDialog.close(false);
  63. }
  64. }));
  65. this.dragMask =
  66. dragMask ||
  67. (dragMask = new Mask({
  68. className: "edui-dialog-dragmask",
  69. theme: theme
  70. }));
  71. this.closeButton = new Button({
  72. className: "edui-dialog-closebutton",
  73. title: me.closeDialog,
  74. theme: theme,
  75. onclick: function() {
  76. me.close(false);
  77. }
  78. });
  79. this.fullscreen && this.initResizeEvent();
  80. if (this.buttons) {
  81. for (var i = 0; i < this.buttons.length; i++) {
  82. if (!(this.buttons[i] instanceof Button)) {
  83. this.buttons[i] = new Button(
  84. utils.extend(
  85. this.buttons[i],
  86. {
  87. editor: this.editor
  88. },
  89. true
  90. )
  91. );
  92. }
  93. }
  94. }
  95. },
  96. initResizeEvent: function() {
  97. var me = this;
  98. domUtils.on(window, "resize", function() {
  99. if (me._hidden || me._hidden === undefined) {
  100. return;
  101. }
  102. if (me.__resizeTimer) {
  103. window.clearTimeout(me.__resizeTimer);
  104. }
  105. me.__resizeTimer = window.setTimeout(function() {
  106. me.__resizeTimer = null;
  107. var dialogWrapNode = me.getDom(),
  108. contentNode = me.getDom("content"),
  109. wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  110. contentRect = UE.ui.uiUtils.getClientRect(contentNode),
  111. vpRect = uiUtils.getViewportRect();
  112. contentNode.style.width =
  113. vpRect.width - wrapRect.width + contentRect.width + "px";
  114. contentNode.style.height =
  115. vpRect.height - wrapRect.height + contentRect.height + "px";
  116. dialogWrapNode.style.width = vpRect.width + "px";
  117. dialogWrapNode.style.height = vpRect.height + "px";
  118. me.fireEvent("resize");
  119. }, 100);
  120. });
  121. },
  122. fitSize: function() {
  123. var popBodyEl = this.getDom("body");
  124. // if (!(baidu.editor.browser.ie && baidu.editor.browser.version == 7)) {
  125. // uiUtils.removeStyle(popBodyEl, 'width');
  126. // uiUtils.removeStyle(popBodyEl, 'height');
  127. // }
  128. var size = this.mesureSize();
  129. popBodyEl.style.width = size.width + "px";
  130. popBodyEl.style.height = size.height + "px";
  131. return size;
  132. },
  133. safeSetOffset: function(offset) {
  134. var me = this;
  135. var el = me.getDom();
  136. var vpRect = uiUtils.getViewportRect();
  137. var rect = uiUtils.getClientRect(el);
  138. var left = offset.left;
  139. if (left + rect.width > vpRect.right) {
  140. left = vpRect.right - rect.width;
  141. }
  142. var top = offset.top;
  143. if (top + rect.height > vpRect.bottom) {
  144. top = vpRect.bottom - rect.height;
  145. }
  146. el.style.left = Math.max(left, 0) + "px";
  147. el.style.top = Math.max(top, 0) + "px";
  148. },
  149. showAtCenter: function() {
  150. var vpRect = uiUtils.getViewportRect();
  151. if (!this.fullscreen) {
  152. this.getDom().style.display = "";
  153. var popSize = this.fitSize();
  154. var titleHeight = this.getDom("titlebar").offsetHeight | 0;
  155. var left = vpRect.width / 2 - popSize.width / 2;
  156. var top =
  157. vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight;
  158. var popEl = this.getDom();
  159. this.safeSetOffset({
  160. left: Math.max(left | 0, 0),
  161. top: Math.max(top | 0, 0)
  162. });
  163. if (!domUtils.hasClass(popEl, "edui-state-centered")) {
  164. popEl.className += " edui-state-centered";
  165. }
  166. } else {
  167. var dialogWrapNode = this.getDom(),
  168. contentNode = this.getDom("content");
  169. dialogWrapNode.style.display = "block";
  170. var wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  171. contentRect = UE.ui.uiUtils.getClientRect(contentNode);
  172. dialogWrapNode.style.left = "-100000px";
  173. contentNode.style.width =
  174. vpRect.width - wrapRect.width + contentRect.width + "px";
  175. contentNode.style.height =
  176. vpRect.height - wrapRect.height + contentRect.height + "px";
  177. dialogWrapNode.style.width = vpRect.width + "px";
  178. dialogWrapNode.style.height = vpRect.height + "px";
  179. dialogWrapNode.style.left = 0;
  180. //保存环境的overflow值
  181. this._originalContext = {
  182. html: {
  183. overflowX: document.documentElement.style.overflowX,
  184. overflowY: document.documentElement.style.overflowY
  185. },
  186. body: {
  187. overflowX: document.body.style.overflowX,
  188. overflowY: document.body.style.overflowY
  189. }
  190. };
  191. document.documentElement.style.overflowX = "hidden";
  192. document.documentElement.style.overflowY = "hidden";
  193. document.body.style.overflowX = "hidden";
  194. document.body.style.overflowY = "hidden";
  195. }
  196. this._show();
  197. },
  198. getContentHtml: function() {
  199. var contentHtml = "";
  200. if (typeof this.content == "string") {
  201. contentHtml = this.content;
  202. } else if (this.iframeUrl) {
  203. contentHtml =
  204. '<span id="' +
  205. this.id +
  206. '_contmask" class="dialogcontmask"></span><iframe id="' +
  207. this.id +
  208. '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="' +
  209. this.iframeUrl +
  210. '"></iframe>';
  211. }
  212. return contentHtml;
  213. },
  214. getHtmlTpl: function() {
  215. var footHtml = "";
  216. if (this.buttons) {
  217. var buff = [];
  218. for (var i = 0; i < this.buttons.length; i++) {
  219. buff[i] = this.buttons[i].renderHtml();
  220. }
  221. footHtml =
  222. '<div class="%%-foot">' +
  223. '<div id="##_buttons" class="%%-buttons">' +
  224. buff.join("") +
  225. "</div>" +
  226. "</div>";
  227. }
  228. return (
  229. '<div id="##" class="%%"><div ' +
  230. (!this.fullscreen
  231. ? 'class="%%"'
  232. : 'class="%%-wrap edui-dialog-fullscreen-flag"') +
  233. '><div id="##_body" class="%%-body">' +
  234. '<div class="%%-shadow"></div>' +
  235. '<div id="##_titlebar" class="%%-titlebar">' +
  236. '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
  237. '<span class="%%-caption">' +
  238. (this.title || "") +
  239. "</span>" +
  240. "</div>" +
  241. this.closeButton.renderHtml() +
  242. "</div>" +
  243. '<div id="##_content" class="%%-content">' +
  244. (this.autoReset ? "" : this.getContentHtml()) +
  245. "</div>" +
  246. footHtml +
  247. "</div></div></div>"
  248. );
  249. },
  250. postRender: function() {
  251. // todo: 保持居中/记住上次关闭位置选项
  252. if (!this.modalMask.getDom()) {
  253. this.modalMask.render();
  254. this.modalMask.hide();
  255. }
  256. if (!this.dragMask.getDom()) {
  257. this.dragMask.render();
  258. this.dragMask.hide();
  259. }
  260. var me = this;
  261. this.addListener("show", function() {
  262. me.modalMask.show(this.getDom().style.zIndex - 2);
  263. });
  264. this.addListener("hide", function() {
  265. me.modalMask.hide();
  266. });
  267. if (this.buttons) {
  268. for (var i = 0; i < this.buttons.length; i++) {
  269. this.buttons[i].postRender();
  270. }
  271. }
  272. domUtils.on(window, "resize", function() {
  273. setTimeout(function() {
  274. if (!me.isHidden()) {
  275. me.safeSetOffset(uiUtils.getClientRect(me.getDom()));
  276. }
  277. });
  278. });
  279. //hold住scroll事件,防止dialog的滚动影响页面
  280. // if( this.holdScroll ) {
  281. //
  282. // if( !me.iframeUrl ) {
  283. // domUtils.on( document.getElementById( me.id + "_iframe"), !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  284. // domUtils.preventDefault(e);
  285. // } );
  286. // } else {
  287. // me.addListener('dialogafterreset', function(){
  288. // window.setTimeout(function(){
  289. // var iframeWindow = document.getElementById( me.id + "_iframe").contentWindow;
  290. //
  291. // if( browser.ie ) {
  292. //
  293. // var timer = window.setInterval(function(){
  294. //
  295. // if( iframeWindow.document && iframeWindow.document.body ) {
  296. // window.clearInterval( timer );
  297. // timer = null;
  298. // domUtils.on( iframeWindow.document.body, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  299. // domUtils.preventDefault(e);
  300. // } );
  301. // }
  302. //
  303. // }, 100);
  304. //
  305. // } else {
  306. // domUtils.on( iframeWindow, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  307. // domUtils.preventDefault(e);
  308. // } );
  309. // }
  310. //
  311. // }, 1);
  312. // });
  313. // }
  314. //
  315. // }
  316. this._hide();
  317. },
  318. mesureSize: function() {
  319. var body = this.getDom("body");
  320. var width = uiUtils.getClientRect(this.getDom("content")).width;
  321. var dialogBodyStyle = body.style;
  322. dialogBodyStyle.width = width;
  323. return uiUtils.getClientRect(body);
  324. },
  325. _onTitlebarMouseDown: function(evt, el) {
  326. if (this.draggable) {
  327. var rect;
  328. var vpRect = uiUtils.getViewportRect();
  329. var me = this;
  330. uiUtils.startDrag(evt, {
  331. ondragstart: function() {
  332. rect = uiUtils.getClientRect(me.getDom());
  333. me.getDom("contmask").style.visibility = "visible";
  334. me.dragMask.show(me.getDom().style.zIndex - 1);
  335. },
  336. ondragmove: function(x, y) {
  337. var left = rect.left + x;
  338. var top = rect.top + y;
  339. me.safeSetOffset({
  340. left: left,
  341. top: top
  342. });
  343. },
  344. ondragstop: function() {
  345. me.getDom("contmask").style.visibility = "hidden";
  346. domUtils.removeClasses(me.getDom(), ["edui-state-centered"]);
  347. me.dragMask.hide();
  348. }
  349. });
  350. }
  351. },
  352. reset: function() {
  353. this.getDom("content").innerHTML = this.getContentHtml();
  354. this.fireEvent("dialogafterreset");
  355. },
  356. _show: function() {
  357. if (this._hidden) {
  358. this.getDom().style.display = "";
  359. //要高过编辑器的zindxe
  360. this.editor.container.style.zIndex &&
  361. (this.getDom().style.zIndex =
  362. this.editor.container.style.zIndex * 1 + 10);
  363. this._hidden = false;
  364. this.fireEvent("show");
  365. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  366. this.getDom().style.zIndex - 4;
  367. }
  368. },
  369. isHidden: function() {
  370. return this._hidden;
  371. },
  372. _hide: function() {
  373. if (!this._hidden) {
  374. var wrapNode = this.getDom();
  375. wrapNode.style.display = "none";
  376. wrapNode.style.zIndex = "";
  377. wrapNode.style.width = "";
  378. wrapNode.style.height = "";
  379. this._hidden = true;
  380. this.fireEvent("hide");
  381. }
  382. },
  383. open: function() {
  384. if (this.autoReset) {
  385. //有可能还没有渲染
  386. try {
  387. this.reset();
  388. } catch (e) {
  389. this.render();
  390. this.open();
  391. }
  392. }
  393. this.showAtCenter();
  394. if (this.iframeUrl) {
  395. try {
  396. this.getDom("iframe").focus();
  397. } catch (ex) {}
  398. }
  399. activeDialog = this;
  400. },
  401. _onCloseButtonClick: function(evt, el) {
  402. this.close(false);
  403. },
  404. close: function(ok) {
  405. if (this.fireEvent("close", ok) !== false) {
  406. //还原环境
  407. if (this.fullscreen) {
  408. document.documentElement.style.overflowX = this._originalContext.html.overflowX;
  409. document.documentElement.style.overflowY = this._originalContext.html.overflowY;
  410. document.body.style.overflowX = this._originalContext.body.overflowX;
  411. document.body.style.overflowY = this._originalContext.body.overflowY;
  412. delete this._originalContext;
  413. }
  414. this._hide();
  415. //销毁content
  416. var content = this.getDom("content");
  417. var iframe = this.getDom("iframe");
  418. if (content && iframe) {
  419. var doc = iframe.contentDocument || iframe.contentWindow.document;
  420. doc && (doc.body.innerHTML = "");
  421. domUtils.remove(content);
  422. }
  423. }
  424. }
  425. };
  426. utils.inherits(Dialog, UIBase);
  427. })();