1 | 'use strict';
|
2 |
|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
4 |
|
5 | var throttle = _interopDefault(require('throttleit'));
|
6 | var inViewport = _interopDefault(require('element-in-view'));
|
7 |
|
8 | var triggers = ['scroll', 'resize'];
|
9 |
|
10 | var MugenScroll = {
|
11 | name: 'mugen-scroll',
|
12 | props: {
|
13 | handler: {
|
14 | type: Function,
|
15 | required: true
|
16 | },
|
17 | shouldHandle: {
|
18 | type: Boolean,
|
19 | default: true
|
20 | },
|
21 | threshold: {
|
22 | type: Number,
|
23 | default: 0
|
24 | },
|
25 | handleOnMount: {
|
26 | type: Boolean,
|
27 | default: true
|
28 | },
|
29 | scrollContainer: {
|
30 | type: String
|
31 | }
|
32 | },
|
33 | mounted: function mounted() {
|
34 | this.checkInView();
|
35 | },
|
36 | methods: {
|
37 | checkInView: function checkInView() {
|
38 | var this$1 = this;
|
39 |
|
40 | var execute = function () {
|
41 |
|
42 | if (!this$1.$refs.scroll) {
|
43 | return
|
44 | }
|
45 |
|
46 | var inView = inViewport(this$1.$refs.scroll, {
|
47 | threshold: this$1.threshold
|
48 | });
|
49 | if (this$1.shouldHandle && inView) {
|
50 | this$1.handler();
|
51 | }
|
52 | };
|
53 |
|
54 |
|
55 | if (this.handleOnMount) {
|
56 | execute();
|
57 | }
|
58 |
|
59 | if (this.scrollContainer) {
|
60 | var parent = this;
|
61 | while ((parent = parent.$parent) && !this._scrollContainer) {
|
62 | this$1._scrollContainer = parent.$refs[this$1.scrollContainer];
|
63 | }
|
64 |
|
65 | if (this._scrollContainer && this._scrollContainer.$el) {
|
66 | this._scrollContainer = this._scrollContainer.$el;
|
67 | }
|
68 | }
|
69 |
|
70 | this._scrollContainer = this._scrollContainer || window;
|
71 |
|
72 |
|
73 | this.check = throttle(execute, 200);
|
74 | triggers.forEach(function (event) { return this$1._scrollContainer.addEventListener(event, this$1.check); });
|
75 | }
|
76 | },
|
77 | render: function render(h) {
|
78 | return h('div', {
|
79 | staticClass: 'mugen-scroll',
|
80 | ref: 'scroll'
|
81 | }, this.$slots.default)
|
82 | },
|
83 | beforeDestroy: function beforeDestroy() {
|
84 | var this$1 = this;
|
85 |
|
86 | triggers.forEach(function (event) { return this$1._scrollContainer.removeEventListener(event, this$1.check); });
|
87 | }
|
88 | };
|
89 |
|
90 | if (typeof window !== 'undefined' && window.Vue) {
|
91 | window.Vue.component(MugenScroll.name, MugenScroll);
|
92 | }
|
93 |
|
94 | module.exports = MugenScroll;
|