index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.PopupManager = undefined;
  4. var _vue = require('vue');
  5. var _vue2 = _interopRequireDefault(_vue);
  6. var _merge = require('element-ui/lib/utils/merge');
  7. var _merge2 = _interopRequireDefault(_merge);
  8. var _popupManager = require('element-ui/lib/utils/popup/popup-manager');
  9. var _popupManager2 = _interopRequireDefault(_popupManager);
  10. var _scrollbarWidth = require('../scrollbar-width');
  11. var _scrollbarWidth2 = _interopRequireDefault(_scrollbarWidth);
  12. var _dom = require('../dom');
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. var idSeed = 1;
  15. var scrollBarWidth = void 0;
  16. var getDOM = function getDOM(dom) {
  17. if (dom.nodeType === 3) {
  18. dom = dom.nextElementSibling || dom.nextSibling;
  19. getDOM(dom);
  20. }
  21. return dom;
  22. };
  23. exports.default = {
  24. props: {
  25. visible: {
  26. type: Boolean,
  27. default: false
  28. },
  29. openDelay: {},
  30. closeDelay: {},
  31. zIndex: {},
  32. modal: {
  33. type: Boolean,
  34. default: false
  35. },
  36. modalFade: {
  37. type: Boolean,
  38. default: true
  39. },
  40. modalClass: {},
  41. modalAppendToBody: {
  42. type: Boolean,
  43. default: false
  44. },
  45. lockScroll: {
  46. type: Boolean,
  47. default: true
  48. },
  49. closeOnPressEscape: {
  50. type: Boolean,
  51. default: false
  52. },
  53. closeOnClickModal: {
  54. type: Boolean,
  55. default: false
  56. }
  57. },
  58. beforeMount: function beforeMount() {
  59. this._popupId = 'popup-' + idSeed++;
  60. _popupManager2.default.register(this._popupId, this);
  61. },
  62. beforeDestroy: function beforeDestroy() {
  63. _popupManager2.default.deregister(this._popupId);
  64. _popupManager2.default.closeModal(this._popupId);
  65. this.restoreBodyStyle();
  66. },
  67. data: function data() {
  68. return {
  69. opened: false,
  70. bodyPaddingRight: null,
  71. computedBodyPaddingRight: 0,
  72. withoutHiddenClass: true,
  73. rendered: false
  74. };
  75. },
  76. watch: {
  77. visible: function visible(val) {
  78. var _this = this;
  79. if (val) {
  80. if (this._opening) return;
  81. if (!this.rendered) {
  82. this.rendered = true;
  83. _vue2.default.nextTick(function () {
  84. _this.open();
  85. });
  86. } else {
  87. this.open();
  88. }
  89. } else {
  90. this.close();
  91. }
  92. }
  93. },
  94. methods: {
  95. open: function open(options) {
  96. var _this2 = this;
  97. if (!this.rendered) {
  98. this.rendered = true;
  99. }
  100. var props = (0, _merge2.default)({}, this.$props || this, options);
  101. if (this._closeTimer) {
  102. clearTimeout(this._closeTimer);
  103. this._closeTimer = null;
  104. }
  105. clearTimeout(this._openTimer);
  106. var openDelay = Number(props.openDelay);
  107. if (openDelay > 0) {
  108. this._openTimer = setTimeout(function () {
  109. _this2._openTimer = null;
  110. _this2.doOpen(props);
  111. }, openDelay);
  112. } else {
  113. this.doOpen(props);
  114. }
  115. },
  116. doOpen: function doOpen(props) {
  117. if (this.$isServer) return;
  118. if (this.willOpen && !this.willOpen()) return;
  119. if (this.opened) return;
  120. this._opening = true;
  121. var dom = getDOM(this.$el);
  122. var modal = props.modal;
  123. var zIndex = props.zIndex;
  124. if (zIndex) {
  125. _popupManager2.default.zIndex = zIndex;
  126. }
  127. if (modal) {
  128. if (this._closing) {
  129. _popupManager2.default.closeModal(this._popupId);
  130. this._closing = false;
  131. }
  132. _popupManager2.default.openModal(this._popupId, _popupManager2.default.nextZIndex(), this.modalAppendToBody ? undefined : dom, props.modalClass, props.modalFade);
  133. if (props.lockScroll) {
  134. this.withoutHiddenClass = !(0, _dom.hasClass)(document.body, 'el-popup-parent--hidden');
  135. if (this.withoutHiddenClass) {
  136. this.bodyPaddingRight = document.body.style.paddingRight;
  137. this.computedBodyPaddingRight = parseInt((0, _dom.getStyle)(document.body, 'paddingRight'), 10);
  138. }
  139. scrollBarWidth = (0, _scrollbarWidth2.default)();
  140. var bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight;
  141. var bodyOverflowY = (0, _dom.getStyle)(document.body, 'overflowY');
  142. if (scrollBarWidth > 0 && (bodyHasOverflow || bodyOverflowY === 'scroll') && this.withoutHiddenClass) {
  143. document.body.style.paddingRight = this.computedBodyPaddingRight + scrollBarWidth + 'px';
  144. }
  145. (0, _dom.addClass)(document.body, 'el-popup-parent--hidden');
  146. }
  147. }
  148. if (getComputedStyle(dom).position === 'static') {
  149. dom.style.position = 'absolute';
  150. }
  151. dom.style.zIndex = _popupManager2.default.nextZIndex();
  152. this.opened = true;
  153. this.onOpen && this.onOpen();
  154. this.doAfterOpen();
  155. },
  156. doAfterOpen: function doAfterOpen() {
  157. this._opening = false;
  158. },
  159. close: function close() {
  160. var _this3 = this;
  161. if (this.willClose && !this.willClose()) return;
  162. if (this._openTimer !== null) {
  163. clearTimeout(this._openTimer);
  164. this._openTimer = null;
  165. }
  166. clearTimeout(this._closeTimer);
  167. var closeDelay = Number(this.closeDelay);
  168. if (closeDelay > 0) {
  169. this._closeTimer = setTimeout(function () {
  170. _this3._closeTimer = null;
  171. _this3.doClose();
  172. }, closeDelay);
  173. } else {
  174. this.doClose();
  175. }
  176. },
  177. doClose: function doClose() {
  178. this._closing = true;
  179. this.onClose && this.onClose();
  180. if (this.lockScroll) {
  181. setTimeout(this.restoreBodyStyle, 200);
  182. }
  183. this.opened = false;
  184. this.doAfterClose();
  185. },
  186. doAfterClose: function doAfterClose() {
  187. _popupManager2.default.closeModal(this._popupId);
  188. this._closing = false;
  189. },
  190. restoreBodyStyle: function restoreBodyStyle() {
  191. if (this.modal && this.withoutHiddenClass) {
  192. document.body.style.paddingRight = this.bodyPaddingRight;
  193. (0, _dom.removeClass)(document.body, 'el-popup-parent--hidden');
  194. }
  195. this.withoutHiddenClass = true;
  196. }
  197. }
  198. };
  199. exports.PopupManager = _popupManager2.default;