index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. value: {
  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. value(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. this.$emit('input', true);
  130. }
  131. const props = merge({}, this, options);
  132. if (this._closeTimer) {
  133. clearTimeout(this._closeTimer);
  134. this._closeTimer = null;
  135. }
  136. clearTimeout(this._openTimer);
  137. const openDelay = Number(props.openDelay);
  138. if (openDelay > 0) {
  139. this._openTimer = setTimeout(() => {
  140. this._openTimer = null;
  141. this.doOpen(props);
  142. }, openDelay);
  143. } else {
  144. this.doOpen(props);
  145. }
  146. },
  147. doOpen(props) {
  148. if (this.$isServer) return;
  149. if (this.willOpen && !this.willOpen()) return;
  150. if (this.opened) return;
  151. this._opening = true;
  152. // 使用 vue-popup 的组件,如果需要和父组件通信显示的状态,应该使用 value,它是一个 prop,
  153. // 这样在父组件中用 v-model 即可;否则可以使用 visible,它是一个 data
  154. this.visible = true;
  155. this.$emit('input', true);
  156. const dom = getDOM(this.$el);
  157. const modal = props.modal;
  158. const zIndex = props.zIndex;
  159. if (zIndex) {
  160. PopupManager.zIndex = zIndex;
  161. }
  162. if (modal) {
  163. if (this._closing) {
  164. PopupManager.closeModal(this._popupId);
  165. this._closing = false;
  166. }
  167. PopupManager.openModal(this._popupId, PopupManager.nextZIndex(), this.modalAppendToBody ? undefined : dom, props.modalClass, props.modalFade);
  168. if (props.lockScroll) {
  169. if (!this.bodyOverflow) {
  170. this.bodyPaddingRight = document.body.style.paddingRight;
  171. this.bodyOverflow = document.body.style.overflow;
  172. }
  173. scrollBarWidth = getScrollBarWidth();
  174. let bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight;
  175. if (scrollBarWidth > 0 && bodyHasOverflow) {
  176. document.body.style.paddingRight = scrollBarWidth + 'px';
  177. }
  178. document.body.style.overflow = 'hidden';
  179. }
  180. }
  181. if (getComputedStyle(dom).position === 'static') {
  182. dom.style.position = 'absolute';
  183. }
  184. dom.style.zIndex = PopupManager.nextZIndex();
  185. this.opened = true;
  186. this.onOpen && this.onOpen();
  187. if (!this.transition) {
  188. this.doAfterOpen();
  189. }
  190. },
  191. doAfterOpen() {
  192. this._opening = false;
  193. },
  194. close() {
  195. if (this.willClose && !this.willClose()) return;
  196. if (this._openTimer !== null) {
  197. clearTimeout(this._openTimer);
  198. this._openTimer = null;
  199. }
  200. clearTimeout(this._closeTimer);
  201. const closeDelay = Number(this.closeDelay);
  202. if (closeDelay > 0) {
  203. this._closeTimer = setTimeout(() => {
  204. this._closeTimer = null;
  205. this.doClose();
  206. }, closeDelay);
  207. } else {
  208. this.doClose();
  209. }
  210. },
  211. doClose() {
  212. this.visible = false;
  213. this.$emit('input', false);
  214. this._closing = true;
  215. this.onClose && this.onClose();
  216. if (this.lockScroll) {
  217. setTimeout(() => {
  218. if (this.modal && this.bodyOverflow !== 'hidden') {
  219. document.body.style.overflow = this.bodyOverflow;
  220. document.body.style.paddingRight = this.bodyPaddingRight;
  221. }
  222. this.bodyOverflow = null;
  223. this.bodyPaddingRight = null;
  224. }, 200);
  225. }
  226. this.opened = false;
  227. if (!this.transition) {
  228. this.doAfterClose();
  229. }
  230. },
  231. doAfterClose() {
  232. PopupManager.closeModal(this._popupId);
  233. this._closing = false;
  234. }
  235. }
  236. };
  237. export {
  238. PopupManager
  239. };