popup.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ///import core
  2. ///import uicore
  3. (function () {
  4. var utils = baidu.editor.utils,
  5. uiUtils = baidu.editor.ui.uiUtils,
  6. domUtils = baidu.editor.dom.domUtils,
  7. UIBase = baidu.editor.ui.UIBase,
  8. Popup = baidu.editor.ui.Popup = function (options){
  9. this.initOptions(options);
  10. this.initPopup();
  11. };
  12. var allPopups = [];
  13. function closeAllPopup( el ){
  14. var newAll = [];
  15. for ( var i = 0; i < allPopups.length; i++ ) {
  16. var pop = allPopups[i];
  17. if (!pop.isHidden()) {
  18. if (pop.queryAutoHide(el) !== false) {
  19. pop.hide();
  20. }
  21. }
  22. }
  23. }
  24. Popup.postHide = closeAllPopup;
  25. var ANCHOR_CLASSES = ['edui-anchor-topleft','edui-anchor-topright',
  26. 'edui-anchor-bottomleft','edui-anchor-bottomright'];
  27. Popup.prototype = {
  28. SHADOW_RADIUS: 5,
  29. content: null,
  30. _hidden: false,
  31. autoRender: true,
  32. canSideLeft: true,
  33. canSideUp: true,
  34. initPopup: function (){
  35. this.initUIBase();
  36. allPopups.push( this );
  37. },
  38. getHtmlTpl: function (){
  39. return '<div id="##" class="edui-popup %%">' +
  40. ' <div id="##_body" class="edui-popup-body">' +
  41. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: white;" frameborder="0" width="100%" height="100%" src="javascript:"></iframe>' +
  42. ' <div class="edui-shadow"></div>' +
  43. ' <div id="##_content" class="edui-popup-content">' +
  44. this.getContentHtmlTpl() +
  45. ' </div>' +
  46. ' </div>' +
  47. '</div>';
  48. },
  49. getContentHtmlTpl: function (){
  50. if(this.content){
  51. if (typeof this.content == 'string') {
  52. return this.content;
  53. }
  54. return this.content.renderHtml();
  55. }else{
  56. return ''
  57. }
  58. },
  59. _UIBase_postRender: UIBase.prototype.postRender,
  60. postRender: function (){
  61. if (this.content instanceof UIBase) {
  62. this.content.postRender();
  63. }
  64. this.fireEvent('postRenderAfter');
  65. this.hide(true);
  66. this._UIBase_postRender();
  67. },
  68. _doAutoRender: function (){
  69. if (!this.getDom() && this.autoRender) {
  70. this.render();
  71. }
  72. },
  73. mesureSize: function (){
  74. var box = this.getDom('content');
  75. return uiUtils.getClientRect(box);
  76. },
  77. fitSize: function (){
  78. var popBodyEl = this.getDom('body');
  79. popBodyEl.style.width = '';
  80. popBodyEl.style.height = '';
  81. var size = this.mesureSize();
  82. popBodyEl.style.width = size.width + 'px';
  83. popBodyEl.style.height = size.height + 'px';
  84. return size;
  85. },
  86. showAnchor: function ( element, hoz ){
  87. this.showAnchorRect( uiUtils.getClientRect( element ), hoz );
  88. },
  89. showAnchorRect: function ( rect, hoz, adj ){
  90. this._doAutoRender();
  91. var vpRect = uiUtils.getViewportRect();
  92. this._show();
  93. var popSize = this.fitSize();
  94. var sideLeft, sideUp, left, top;
  95. if (hoz) {
  96. sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
  97. sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
  98. left = (sideLeft ? rect.left - popSize.width : rect.right);
  99. top = (sideUp ? rect.bottom - popSize.height : rect.top);
  100. } else {
  101. sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
  102. sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
  103. left = (sideLeft ? rect.right - popSize.width : rect.left);
  104. top = (sideUp ? rect.top - popSize.height : rect.bottom);
  105. }
  106. var popEl = this.getDom();
  107. uiUtils.setViewportOffset(popEl, {
  108. left: left,
  109. top: top
  110. });
  111. domUtils.removeClasses(popEl, ANCHOR_CLASSES);
  112. popEl.className += ' ' + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)];
  113. if(this.editor){
  114. popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10;
  115. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = popEl.style.zIndex - 1;
  116. }
  117. },
  118. showAt: function (offset) {
  119. var left = offset.left;
  120. var top = offset.top;
  121. var rect = {
  122. left: left,
  123. top: top,
  124. right: left,
  125. bottom: top,
  126. height: 0,
  127. width: 0
  128. };
  129. this.showAnchorRect(rect, false, true);
  130. },
  131. _show: function (){
  132. if (this._hidden) {
  133. var box = this.getDom();
  134. box.style.display = '';
  135. this._hidden = false;
  136. // if (box.setActive) {
  137. // box.setActive();
  138. // }
  139. this.fireEvent('show');
  140. }
  141. },
  142. isHidden: function (){
  143. return this._hidden;
  144. },
  145. show: function (){
  146. this._doAutoRender();
  147. this._show();
  148. },
  149. hide: function (notNofity){
  150. if (!this._hidden && this.getDom()) {
  151. // this.getDom().style.visibility = 'hidden';
  152. this.getDom().style.display = 'none';
  153. this._hidden = true;
  154. if (!notNofity) {
  155. this.fireEvent('hide');
  156. }
  157. }
  158. },
  159. queryAutoHide: function (el){
  160. return !el || !uiUtils.contains(this.getDom(), el);
  161. }
  162. };
  163. utils.inherits(Popup, UIBase);
  164. domUtils.on( document, 'mousedown', function ( evt ) {
  165. var el = evt.target || evt.srcElement;
  166. closeAllPopup( el );
  167. } );
  168. domUtils.on( window, 'scroll', function () {
  169. closeAllPopup();
  170. } );
  171. // var lastVpRect = uiUtils.getViewportRect();
  172. // domUtils.on( window, 'resize', function () {
  173. // var vpRect = uiUtils.getViewportRect();
  174. // if (vpRect.width != lastVpRect.width || vpRect.height != lastVpRect.height) {
  175. // closeAllPopup();
  176. // }
  177. // } );
  178. })();