index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import Vue from 'vue';
  2. import merge from 'element-ui/src/utils/merge';
  3. import PopupManager from 'element-ui/src/utils/popup/popup-manager';
  4. let idSeed = 1;
  5. const transitions = [];
  6. const hookTransition = (transition) => {
  7. if (transitions.indexOf(transition) !== -1) return;
  8. const getVueInstance = (element) => {
  9. let instance = element.__vue__;
  10. if (!instance) {
  11. const textNode = element.previousSibling;
  12. if (textNode.__vue__) {
  13. instance = textNode.__vue__;
  14. }
  15. }
  16. return instance;
  17. };
  18. Vue.transition(transition, {
  19. afterEnter(el) {
  20. const instance = getVueInstance(el);
  21. if (instance) {
  22. instance.doAfterOpen && instance.doAfterOpen();
  23. }
  24. },
  25. afterLeave(el) {
  26. const instance = getVueInstance(el);
  27. if (instance) {
  28. instance.doAfterClose && instance.doAfterClose();
  29. }
  30. }
  31. });
  32. };
  33. let scrollBarWidth;
  34. const getScrollBarWidth = () => {
  35. if (Vue.prototype.$isServer) return;
  36. if (scrollBarWidth !== undefined) return scrollBarWidth;
  37. const outer = document.createElement('div');
  38. outer.style.visibility = 'hidden';
  39. outer.style.width = '100px';
  40. outer.style.position = 'absolute';
  41. outer.style.top = '-9999px';
  42. document.body.appendChild(outer);
  43. const widthNoScroll = outer.offsetWidth;
  44. outer.style.overflow = 'scroll';
  45. const inner = document.createElement('div');
  46. inner.style.width = '100%';
  47. outer.appendChild(inner);
  48. const widthWithScroll = inner.offsetWidth;
  49. outer.parentNode.removeChild(outer);
  50. return widthNoScroll - widthWithScroll;
  51. };
  52. const getDOM = function(dom) {
  53. if (dom.nodeType === 3) {
  54. dom = dom.nextElementSibling || dom.nextSibling;
  55. getDOM(dom);
  56. }
  57. return dom;
  58. };
  59. export default {
  60. props: {
  61. value: {
  62. type: Boolean,
  63. default: false
  64. },
  65. transition: {
  66. type: String,
  67. default: ''
  68. },
  69. openDelay: {},
  70. closeDelay: {},
  71. zIndex: {},
  72. modal: {
  73. type: Boolean,
  74. default: false
  75. },
  76. modalFade: {
  77. type: Boolean,
  78. default: true
  79. },
  80. modalClass: {
  81. },
  82. modalAppendToBody: {
  83. type: Boolean,
  84. default: false
  85. },
  86. lockScroll: {
  87. type: Boolean,
  88. default: true
  89. },
  90. closeOnPressEscape: {
  91. type: Boolean,
  92. default: false
  93. },
  94. closeOnClickModal: {
  95. type: Boolean,
  96. default: false
  97. }
  98. },
  99. created() {
  100. if (this.transition) {
  101. hookTransition(this.transition);
  102. }
  103. },
  104. beforeMount() {
  105. this._popupId = 'popup-' + idSeed++;
  106. PopupManager.register(this._popupId, this);
  107. },
  108. beforeDestroy() {
  109. PopupManager.deregister(this._popupId);
  110. PopupManager.closeModal(this._popupId);
  111. if (this.modal && this.bodyOverflow !== null && this.bodyOverflow !== 'hidden') {
  112. document.body.style.overflow = this.bodyOverflow;
  113. document.body.style.paddingRight = this.bodyPaddingRight;
  114. }
  115. this.bodyOverflow = null;
  116. this.bodyPaddingRight = null;
  117. },
  118. data() {
  119. return {
  120. opened: false,
  121. bodyOverflow: null,
  122. bodyPaddingRight: null,
  123. rendered: false
  124. };
  125. },
  126. watch: {
  127. value(val) {
  128. if (val) {
  129. if (this._opening) return;
  130. if (!this.rendered) {
  131. this.rendered = true;
  132. Vue.nextTick(() => {
  133. this.open();
  134. });
  135. } else {
  136. this.open();
  137. }
  138. } else {
  139. this.close();
  140. }
  141. }
  142. },
  143. methods: {
  144. open(options) {
  145. if (!this.rendered) {
  146. this.rendered = true;
  147. this.$emit('input', true);
  148. }
  149. const props = merge({}, this, options);
  150. if (this._closeTimer) {
  151. clearTimeout(this._closeTimer);
  152. this._closeTimer = null;
  153. }
  154. clearTimeout(this._openTimer);
  155. const openDelay = Number(props.openDelay);
  156. if (openDelay > 0) {
  157. this._openTimer = setTimeout(() => {
  158. this._openTimer = null;
  159. this.doOpen(props);
  160. }, openDelay);
  161. } else {
  162. this.doOpen(props);
  163. }
  164. },
  165. doOpen(props) {
  166. if (this.$isServer) return;
  167. if (this.willOpen && !this.willOpen()) return;
  168. if (this.opened) return;
  169. this._opening = true;
  170. // 使用 vue-popup 的组件,如果需要和父组件通信显示的状态,应该使用 value,它是一个 prop,
  171. // 这样在父组件中用 v-model 即可;否则可以使用 visible,它是一个 data
  172. this.visible = true;
  173. this.$emit('input', true);
  174. const dom = getDOM(this.$el);
  175. const modal = props.modal;
  176. const zIndex = props.zIndex;
  177. if (zIndex) {
  178. PopupManager.zIndex = zIndex;
  179. }
  180. if (modal) {
  181. if (this._closing) {
  182. PopupManager.closeModal(this._popupId);
  183. this._closing = false;
  184. }
  185. PopupManager.openModal(this._popupId, PopupManager.nextZIndex(), this.modalAppendToBody ? undefined : dom, props.modalClass, props.modalFade);
  186. if (props.lockScroll) {
  187. if (!this.bodyOverflow) {
  188. this.bodyPaddingRight = document.body.style.paddingRight;
  189. this.bodyOverflow = document.body.style.overflow;
  190. }
  191. scrollBarWidth = getScrollBarWidth();
  192. let bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight;
  193. if (scrollBarWidth > 0 && bodyHasOverflow) {
  194. document.body.style.paddingRight = scrollBarWidth + 'px';
  195. }
  196. document.body.style.overflow = 'hidden';
  197. }
  198. }
  199. if (getComputedStyle(dom).position === 'static') {
  200. dom.style.position = 'absolute';
  201. }
  202. dom.style.zIndex = PopupManager.nextZIndex();
  203. this.opened = true;
  204. this.onOpen && this.onOpen();
  205. if (!this.transition) {
  206. this.doAfterOpen();
  207. }
  208. },
  209. doAfterOpen() {
  210. this._opening = false;
  211. },
  212. close() {
  213. if (this.willClose && !this.willClose()) return;
  214. if (this._openTimer !== null) {
  215. clearTimeout(this._openTimer);
  216. this._openTimer = null;
  217. }
  218. clearTimeout(this._closeTimer);
  219. const closeDelay = Number(this.closeDelay);
  220. if (closeDelay > 0) {
  221. this._closeTimer = setTimeout(() => {
  222. this._closeTimer = null;
  223. this.doClose();
  224. }, closeDelay);
  225. } else {
  226. this.doClose();
  227. }
  228. },
  229. doClose() {
  230. this.visible = false;
  231. this.$emit('input', false);
  232. this._closing = true;
  233. this.onClose && this.onClose();
  234. if (this.lockScroll) {
  235. setTimeout(() => {
  236. if (this.modal && this.bodyOverflow !== 'hidden') {
  237. document.body.style.overflow = this.bodyOverflow;
  238. document.body.style.paddingRight = this.bodyPaddingRight;
  239. }
  240. this.bodyOverflow = null;
  241. this.bodyPaddingRight = null;
  242. }, 200);
  243. }
  244. this.opened = false;
  245. if (!this.transition) {
  246. this.doAfterClose();
  247. }
  248. },
  249. doAfterClose() {
  250. PopupManager.closeModal(this._popupId);
  251. this._closing = false;
  252. }
  253. }
  254. };
  255. export { PopupManager };