index.js 6.0 KB

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