dialog.js 9.7 KB

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