demo.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. var Popup = new baidu.editor.ui.Popup();
  2. var uiUtils = baidu.editor.ui.uiUtils();
  3. var popEl = this.getDom();
  4. uiUtils.setViewportOffset(popEl, {
  5. left: 20,
  6. top: top
  7. });
  8. (function () {
  9. var utils = baidu.editor.utils,
  10. uiUtils = baidu.editor.ui.uiUtils,
  11. domUtils = baidu.editor.dom.domUtils,
  12. UIBase = baidu.editor.ui.UIBase,
  13. Popup = baidu.editor.ui.Popup = function (options){
  14. this.initOptions(options);
  15. this.initPopup();
  16. };
  17. var allPopups = [];
  18. function closeAllPopup( evt,el ){
  19. for ( var i = 0; i < allPopups.length; i++ ) {
  20. var pop = allPopups[i];
  21. if (!pop.isHidden()) {
  22. if (pop.queryAutoHide(el) !== false) {
  23. if(evt&&/scroll/ig.test(evt.type)&&pop.className=="edui-wordpastepop") return;
  24. pop.hide();
  25. }
  26. }
  27. }
  28. if(allPopups.length)
  29. pop.editor.fireEvent("afterhidepop");
  30. }
  31. Popup.postHide = closeAllPopup;
  32. var ANCHOR_CLASSES = ['edui-anchor-topleft','edui-anchor-topright',
  33. 'edui-anchor-bottomleft','edui-anchor-bottomright'];
  34. Popup.prototype = {
  35. SHADOW_RADIUS: 5,
  36. content: null,
  37. _hidden: false,
  38. autoRender: true,
  39. canSideLeft: true,
  40. canSideUp: true,
  41. initPopup: function (){
  42. this.initUIBase();
  43. allPopups.push( this );
  44. },
  45. getHtmlTpl: function (){
  46. return '<div id="##" class="edui-popup %%" onmousedown="return false;">' +
  47. ' <div id="##_body" class="edui-popup-body">' +
  48. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  49. ' <div class="edui-shadow"></div>' +
  50. ' <div id="##_content" class="edui-popup-content">' +
  51. this.getContentHtmlTpl() +
  52. ' </div>' +
  53. ' </div>' +
  54. '</div>';
  55. },
  56. getContentHtmlTpl: function (){
  57. if(this.content){
  58. if (typeof this.content == 'string') {
  59. return this.content;
  60. }
  61. return this.content.renderHtml();
  62. }else{
  63. return ''
  64. }
  65. },
  66. _UIBase_postRender: UIBase.prototype.postRender,
  67. postRender: function (){
  68. if (this.content instanceof UIBase) {
  69. this.content.postRender();
  70. }
  71. //捕获鼠标滚轮
  72. if( this.captureWheel && !this.captured ) {
  73. this.captured = true;
  74. var winHeight = ( document.documentElement.clientHeight || document.body.clientHeight ) - 80,
  75. _height = this.getDom().offsetHeight,
  76. _top = uiUtils.getClientRect( this.combox.getDom() ).top,
  77. content = this.getDom('content'),
  78. ifr = this.getDom('body').getElementsByTagName('iframe'),
  79. me = this;
  80. ifr.length && ( ifr = ifr[0] );
  81. while( _top + _height > winHeight ) {
  82. _height -= 30;
  83. }
  84. content.style.height = _height + 'px';
  85. //同步更改iframe高度
  86. ifr && ( ifr.style.height = _height + 'px' );
  87. //阻止在combox上的鼠标滚轮事件, 防止用户的正常操作被误解
  88. if( window.XMLHttpRequest ) {
  89. domUtils.on( content, ( 'onmousewheel' in document.body ) ? 'mousewheel' :'DOMMouseScroll' , function(e){
  90. if(e.preventDefault) {
  91. e.preventDefault();
  92. } else {
  93. e.returnValue = false;
  94. }
  95. if( e.wheelDelta ) {
  96. content.scrollTop -= ( e.wheelDelta / 120 )*60;
  97. } else {
  98. content.scrollTop -= ( e.detail / -3 )*60;
  99. }
  100. });
  101. } else {
  102. //ie6
  103. domUtils.on( this.getDom(), 'mousewheel' , function(e){
  104. e.returnValue = false;
  105. me.getDom('content').scrollTop -= ( e.wheelDelta / 120 )*60;
  106. });
  107. }
  108. }
  109. this.fireEvent('postRenderAfter');
  110. this.hide(true);
  111. this._UIBase_postRender();
  112. },
  113. _doAutoRender: function (){
  114. if (!this.getDom() && this.autoRender) {
  115. this.render();
  116. }
  117. },
  118. mesureSize: function (){
  119. var box = this.getDom('content');
  120. return uiUtils.getClientRect(box);
  121. },
  122. fitSize: function (){
  123. if( this.captureWheel && this.sized ) {
  124. return this.__size;
  125. }
  126. this.sized = true;
  127. var popBodyEl = this.getDom('body');
  128. popBodyEl.style.width = '';
  129. popBodyEl.style.height = '';
  130. var size = this.mesureSize();
  131. if( this.captureWheel ) {
  132. popBodyEl.style.width = -(-20 -size.width) + 'px';
  133. var height = parseInt( this.getDom('content').style.height, 10 );
  134. !window.isNaN( height ) && ( size.height = height );
  135. } else {
  136. popBodyEl.style.width = size.width + 'px';
  137. }
  138. popBodyEl.style.height = size.height + 'px';
  139. this.__size = size;
  140. this.captureWheel && (this.getDom('content').style.overflow = 'auto');
  141. return size;
  142. },
  143. showAnchor: function ( element, hoz ){
  144. this.showAnchorRect( uiUtils.getClientRect( element ), hoz );
  145. },
  146. showAnchorRect: function ( rect, hoz, adj ){
  147. this._doAutoRender();
  148. var vpRect = uiUtils.getViewportRect();
  149. this.getDom().style.visibility = 'hidden';
  150. this._show();
  151. var popSize = this.fitSize();
  152. var sideLeft, sideUp, left, top;
  153. if (hoz) {
  154. sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
  155. sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
  156. left = (sideLeft ? rect.left - popSize.width : rect.right);
  157. top = (sideUp ? rect.bottom - popSize.height : rect.top);
  158. } else {
  159. sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
  160. sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
  161. left = (sideLeft ? rect.right - popSize.width : rect.left);
  162. top = (sideUp ? rect.top - popSize.height : rect.bottom);
  163. }
  164. var popEl = this.getDom();
  165. uiUtils.setViewportOffset(popEl, {
  166. left: 20,
  167. top: top
  168. });
  169. domUtils.removeClasses(popEl, ANCHOR_CLASSES);
  170. popEl.className += ' ' + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)];
  171. if(this.editor){
  172. popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10;
  173. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = popEl.style.zIndex - 1;
  174. }
  175. this.getDom().style.visibility = 'uset';
  176. },
  177. showAt: function (offset) {
  178. var left = offset.left;
  179. var top = offset.top;
  180. var rect = {
  181. left: left,
  182. top: top,
  183. right: left,
  184. bottom: top,
  185. height: 0,
  186. width: 0
  187. };
  188. this.showAnchorRect(rect, false, true);
  189. },
  190. _show: function (){
  191. if (this._hidden) {
  192. var box = this.getDom();
  193. box.style.display = '';
  194. this._hidden = false;
  195. // if (box.setActive) {
  196. // box.setActive();
  197. // }
  198. this.fireEvent('show');
  199. }
  200. },
  201. isHidden: function (){
  202. return this._hidden;
  203. },
  204. show: function (){
  205. this._doAutoRender();
  206. this._show();
  207. },
  208. hide: function (notNofity){
  209. if (!this._hidden && this.getDom()) {
  210. this.getDom().style.display = 'none';
  211. this._hidden = true;
  212. if (!notNofity) {
  213. this.fireEvent('hide');
  214. }
  215. }
  216. },
  217. queryAutoHide: function (el){
  218. return !el || !uiUtils.contains(this.getDom(), el);
  219. }
  220. };
  221. utils.inherits(Popup, UIBase);
  222. domUtils.on( document, 'mousedown', function ( evt ) {
  223. var el = evt.target || evt.srcElement;
  224. closeAllPopup( evt,el );
  225. } );
  226. domUtils.on( window, 'scroll', function (evt,el) {
  227. closeAllPopup( evt,el );
  228. } );
  229. })();