index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  6. var _popperjs = require("@vant/popperjs");
  7. var _utils = require("../utils");
  8. var _constant = require("../utils/constant");
  9. var _clickOutside = require("../mixins/click-outside");
  10. var _icon = _interopRequireDefault(require("../icon"));
  11. var _popup = _interopRequireDefault(require("../popup"));
  12. // Mixins
  13. // Components
  14. var _createNamespace = (0, _utils.createNamespace)('popover'),
  15. createComponent = _createNamespace[0],
  16. bem = _createNamespace[1];
  17. var _default2 = createComponent({
  18. mixins: [(0, _clickOutside.ClickOutsideMixin)({
  19. event: 'touchstart',
  20. method: 'onClickOutside'
  21. })],
  22. props: {
  23. value: Boolean,
  24. trigger: String,
  25. overlay: Boolean,
  26. offset: {
  27. type: Array,
  28. default: function _default() {
  29. return [0, 8];
  30. }
  31. },
  32. theme: {
  33. type: String,
  34. default: 'light'
  35. },
  36. actions: {
  37. type: Array,
  38. default: function _default() {
  39. return [];
  40. }
  41. },
  42. placement: {
  43. type: String,
  44. default: 'bottom'
  45. },
  46. getContainer: {
  47. type: [String, Function],
  48. default: 'body'
  49. },
  50. closeOnClickAction: {
  51. type: Boolean,
  52. default: true
  53. }
  54. },
  55. watch: {
  56. value: 'updateLocation',
  57. placement: 'updateLocation'
  58. },
  59. mounted: function mounted() {
  60. this.updateLocation();
  61. },
  62. beforeDestroy: function beforeDestroy() {
  63. if (this.popper) {
  64. this.popper.destroy();
  65. this.popper = null;
  66. }
  67. },
  68. methods: {
  69. createPopper: function createPopper() {
  70. return (0, _popperjs.createPopper)(this.$refs.wrapper, this.$refs.popover.$el, {
  71. placement: this.placement,
  72. modifiers: [{
  73. name: 'computeStyles',
  74. options: {
  75. adaptive: false,
  76. gpuAcceleration: false
  77. }
  78. }, (0, _extends2.default)({}, _popperjs.offsetModifier, {
  79. options: {
  80. offset: this.offset
  81. }
  82. })]
  83. });
  84. },
  85. updateLocation: function updateLocation() {
  86. var _this = this;
  87. this.$nextTick(function () {
  88. if (!_this.value) {
  89. return;
  90. }
  91. if (!_this.popper) {
  92. _this.popper = _this.createPopper();
  93. } else {
  94. _this.popper.setOptions({
  95. placement: _this.placement
  96. });
  97. }
  98. });
  99. },
  100. renderAction: function renderAction(action, index) {
  101. var _this2 = this;
  102. var h = this.$createElement;
  103. var icon = action.icon,
  104. text = action.text,
  105. disabled = action.disabled,
  106. className = action.className;
  107. return h("div", {
  108. "attrs": {
  109. "role": "menuitem"
  110. },
  111. "class": [bem('action', {
  112. disabled: disabled,
  113. 'with-icon': icon
  114. }), className],
  115. "on": {
  116. "click": function click() {
  117. return _this2.onClickAction(action, index);
  118. }
  119. }
  120. }, [icon && h(_icon.default, {
  121. "attrs": {
  122. "name": icon
  123. },
  124. "class": bem('action-icon')
  125. }), h("div", {
  126. "class": [bem('action-text'), _constant.BORDER_BOTTOM]
  127. }, [text])]);
  128. },
  129. onToggle: function onToggle(value) {
  130. this.$emit('input', value);
  131. },
  132. onClickWrapper: function onClickWrapper() {
  133. if (this.trigger === 'click') {
  134. this.onToggle(!this.value);
  135. }
  136. },
  137. onTouchstart: function onTouchstart(event) {
  138. event.stopPropagation();
  139. this.$emit('touchstart', event);
  140. },
  141. onClickAction: function onClickAction(action, index) {
  142. if (action.disabled) {
  143. return;
  144. }
  145. this.$emit('select', action, index);
  146. if (this.closeOnClickAction) {
  147. this.$emit('input', false);
  148. }
  149. },
  150. onClickOutside: function onClickOutside() {
  151. this.$emit('input', false);
  152. },
  153. onOpen: function onOpen() {
  154. this.$emit('open');
  155. },
  156. /* istanbul ignore next */
  157. onOpened: function onOpened() {
  158. this.$emit('opened');
  159. },
  160. onClose: function onClose() {
  161. this.$emit('close');
  162. },
  163. /* istanbul ignore next */
  164. onClosed: function onClosed() {
  165. this.$emit('closed');
  166. }
  167. },
  168. render: function render() {
  169. var h = arguments[0];
  170. return h("span", {
  171. "ref": "wrapper",
  172. "class": bem('wrapper'),
  173. "on": {
  174. "click": this.onClickWrapper
  175. }
  176. }, [h(_popup.default, {
  177. "ref": "popover",
  178. "attrs": {
  179. "value": this.value,
  180. "overlay": this.overlay,
  181. "position": null,
  182. "transition": "van-popover-zoom",
  183. "lockScroll": false,
  184. "getContainer": this.getContainer
  185. },
  186. "class": bem([this.theme]),
  187. "on": {
  188. "open": this.onOpen,
  189. "close": this.onClose,
  190. "input": this.onToggle,
  191. "opened": this.onOpened,
  192. "closed": this.onClosed
  193. },
  194. "nativeOn": {
  195. "touchstart": this.onTouchstart
  196. }
  197. }, [h("div", {
  198. "class": bem('arrow')
  199. }), h("div", {
  200. "class": bem('content'),
  201. "attrs": {
  202. "role": "menu"
  203. }
  204. }, [this.slots('default') || this.actions.map(this.renderAction)])]), this.slots('reference')]);
  205. }
  206. });
  207. exports.default = _default2;