dialog.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. this.initOptions(utils.extend({
  14. autoReset: true,
  15. draggable: true,
  16. onok: function (){},
  17. oncancel: function (){},
  18. onclose: function (t, ok){
  19. return ok ? this.onok() : this.oncancel();
  20. }
  21. },options));
  22. this.initDialog();
  23. };
  24. var modalMask;
  25. var dragMask;
  26. Dialog.prototype = {
  27. draggable: false,
  28. uiName: 'dialog',
  29. initDialog: function (){
  30. var me = this;
  31. this.initUIBase();
  32. this.modalMask = (modalMask || (modalMask = new Mask({
  33. className: 'edui-dialog-modalmask'
  34. })));
  35. this.dragMask = (dragMask || (dragMask = new Mask({
  36. className: 'edui-dialog-dragmask'
  37. })));
  38. this.closeButton = new Button({
  39. className: 'edui-dialog-closebutton',
  40. title: '关闭对话框',
  41. onclick: function (){
  42. me.close(false);
  43. }
  44. });
  45. if (this.buttons) {
  46. for (var i=0; i<this.buttons.length; i++) {
  47. if (!(this.buttons[i] instanceof Button)) {
  48. this.buttons[i] = new Button(this.buttons[i]);
  49. }
  50. }
  51. }
  52. },
  53. fitSize: function (){
  54. var popBodyEl = this.getDom('body');
  55. // if (!(baidu.editor.browser.ie && baidu.editor.browser.version == 7)) {
  56. // uiUtils.removeStyle(popBodyEl, 'width');
  57. // uiUtils.removeStyle(popBodyEl, 'height');
  58. // }
  59. var size = this.mesureSize();
  60. popBodyEl.style.width = size.width + 'px';
  61. popBodyEl.style.height = size.height + 'px';
  62. return size;
  63. },
  64. safeSetOffset: function (offset){
  65. var me = this;
  66. var el = me.getDom();
  67. var vpRect = uiUtils.getViewportRect();
  68. var rect = uiUtils.getClientRect(el);
  69. var left = offset.left;
  70. if (left + rect.width > vpRect.right) {
  71. left = vpRect.right - rect.width;
  72. }
  73. var top = offset.top;
  74. if (top + rect.height > vpRect.bottom) {
  75. top = vpRect.bottom - rect.height;
  76. }
  77. el.style.left = Math.max(left, 0) + 'px';
  78. el.style.top = Math.max(top, 0) + 'px';
  79. },
  80. showAtCenter: function (){
  81. this.getDom().style.display = '';
  82. var vpRect = uiUtils.getViewportRect();
  83. var popSize = this.fitSize();
  84. var titleHeight = this.getDom('titlebar').offsetHeight | 0;
  85. var left = vpRect.width / 2 - popSize.width / 2;
  86. var top = vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight;
  87. var popEl = this.getDom();
  88. this.safeSetOffset({
  89. left: Math.max(left | 0, 0),
  90. top: Math.max(top | 0, 0)
  91. });
  92. if (!domUtils.hasClass(popEl, 'edui-state-centered')) {
  93. popEl.className += ' edui-state-centered';
  94. }
  95. this._show();
  96. },
  97. getContentHtml: function (){
  98. var contentHtml = '';
  99. if (typeof this.content == 'string') {
  100. contentHtml = this.content;
  101. } else if (this.iframeUrl) {
  102. contentHtml = '<span id="'+ this.id +'_contmask" class="dialogcontmask"></span><iframe id="'+ this.id +
  103. '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="'+ this.iframeUrl +'"></iframe>';
  104. }
  105. return contentHtml;
  106. },
  107. getHtmlTpl: function (){
  108. var footHtml = '';
  109. if (this.buttons) {
  110. var buff = [];
  111. for (var i=0; i<this.buttons.length; i++) {
  112. buff[i] = this.buttons[i].renderHtml();
  113. }
  114. footHtml = '<div class="%%-foot">' +
  115. '<div id="##_buttons" class="%%-buttons">' + buff.join('') + '</div>' +
  116. '</div>';
  117. }
  118. return '<div id="##" class="%%"><div class="%%-wrap"><div id="##_body" class="%%-body">' +
  119. '<div class="%%-shadow"></div>' +
  120. '<div id="##_titlebar" class="%%-titlebar">' +
  121. '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
  122. '<span class="%%-caption">' + (this.title || '') + '</span>' +
  123. '</div>' +
  124. this.closeButton.renderHtml() +
  125. '</div>' +
  126. '<div id="##_content" class="%%-content">'+ ( this.autoReset ? '' : this.getContentHtml()) +'</div>' +
  127. footHtml +
  128. '</div></div></div>';
  129. },
  130. postRender: function (){
  131. // todo: 保持居中/记住上次关闭位置选项
  132. if (!this.modalMask.getDom()) {
  133. this.modalMask.render();
  134. this.modalMask.hide();
  135. }
  136. if (!this.dragMask.getDom()) {
  137. this.dragMask.render();
  138. this.dragMask.hide();
  139. }
  140. var me = this;
  141. this.addListener('show', function (){
  142. me.modalMask.show(this.getDom().style.zIndex - 2);
  143. });
  144. this.addListener('hide', function (){
  145. me.modalMask.hide();
  146. });
  147. if (this.buttons) {
  148. for (var i=0; i<this.buttons.length; i++) {
  149. this.buttons[i].postRender();
  150. }
  151. }
  152. domUtils.on(window, 'resize', function (){
  153. setTimeout(function (){
  154. if (!me.isHidden()) {
  155. me.safeSetOffset(uiUtils.getClientRect(me.getDom()));
  156. }
  157. });
  158. });
  159. this._hide();
  160. },
  161. mesureSize: function (){
  162. var body = this.getDom('body');
  163. var width = uiUtils.getClientRect(this.getDom('content')).width;
  164. var dialogBodyStyle = body.style;
  165. dialogBodyStyle.width = width;
  166. return uiUtils.getClientRect(body);
  167. },
  168. _onTitlebarMouseDown: function (evt, el){
  169. if (this.draggable) {
  170. var rect;
  171. var vpRect = uiUtils.getViewportRect();
  172. var me = this;
  173. uiUtils.startDrag(evt, {
  174. ondragstart: function (){
  175. rect = uiUtils.getClientRect(me.getDom());
  176. me.getDom('contmask').style.visibility = 'visible';
  177. me.dragMask.show(me.getDom().style.zIndex - 1);
  178. },
  179. ondragmove: function (x, y){
  180. var left = rect.left + x;
  181. var top = rect.top + y;
  182. me.safeSetOffset({
  183. left: left,
  184. top: top
  185. });
  186. },
  187. ondragstop: function (){
  188. me.getDom('contmask').style.visibility = 'hidden';
  189. domUtils.removeClasses(me.getDom(), ['edui-state-centered']);
  190. me.dragMask.hide();
  191. }
  192. });
  193. }
  194. },
  195. reset: function (){
  196. this.getDom('content').innerHTML = this.getContentHtml();
  197. },
  198. _show: function (){
  199. if (this._hidden) {
  200. this.getDom().style.display = '';
  201. //要高过编辑器的zindxe
  202. this.editor.container.style.zIndex && (this.getDom().style.zIndex = this.editor.container.style.zIndex * 1 + 10);
  203. this._hidden = false;
  204. this.fireEvent('show');
  205. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = this.getDom().style.zIndex - 4;
  206. }
  207. },
  208. isHidden: function (){
  209. return this._hidden;
  210. },
  211. _hide: function (){
  212. if (!this._hidden) {
  213. this.getDom().style.display = 'none';
  214. this.getDom().style.zIndex = '';
  215. this._hidden = true;
  216. this.fireEvent('hide');
  217. }
  218. },
  219. open: function (){
  220. if (this.autoReset) {
  221. //有可能还没有渲染
  222. try{
  223. this.reset();
  224. }catch(e){
  225. this.render();
  226. this.open()
  227. }
  228. }
  229. this.showAtCenter();
  230. if (this.iframeUrl) {
  231. try {
  232. this.getDom('iframe').focus();
  233. } catch(ex){}
  234. }
  235. },
  236. _onCloseButtonClick: function (evt, el){
  237. this.close(false);
  238. },
  239. close: function (ok){
  240. if (this.fireEvent('close', ok) !== false) {
  241. this._hide();
  242. }
  243. }
  244. };
  245. utils.inherits(Dialog, UIBase);
  246. })();