popup.js 8.1 KB

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