-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
230 lines (199 loc) · 5.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* vue-awaited v1.0.1 | (c) Taras Batenkov and contributors | https://github.com/enkot/vue-awaited/blob/master/LICENSE */
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var obj;
var Vue = require('vue');
var DEFAULT_DELAY = 200;
var isVue3 = !!Vue.h;
var Lazily = ( obj = {
props: {
action: {
type: [String, Function, Promise],
required: false
},
lazy: {
type: Boolean,
default: true
},
delay: {
type: [Number, Boolean]
},
margin: {
type: String
},
threshold: {
type: Number
},
height: {
type: String
},
tag: {
type: String,
default: 'div'
},
watch: {
type: [Number, String, Array, Object, Function]
},
fetchOptions: {
type: Object,
default: function () {}
},
actionHandler: {
type: Function
}
},
data: function data() {
return {
resolved: false,
error: null,
data: null,
observer: null,
target: null,
observed: false,
isDelay: !!this.delay
}
},
mounted: function mounted() {
this.target = this.$el;
if (this.lazy) { this.observe(); }
},
created: function created() {
var this$1 = this;
if (!this.lazy) { this.run(); }
if (this.watch)
{ this.$watch(
isFunction(this.watch) ? this.watch : function () { return this$1.watch; },
this.run
); }
}
}, obj[isVue3 ? 'unmounted' : 'destroyed'] = function () {
this.unobserve();
}, obj.methods = {
run: function run() {
var this$1 = this;
this.observed = true;
if (!this.action) {
this.resolved = true;
return
}
this.resolved = false;
this.startDelay();
try {
var result = this.actionHandler
? this.actionHandler(this.action)
: this.getData(this.action);
if (isPromise(result))
{ return result
.then(function (data) { return (this$1.data = data); })
.catch(function (error) { return (this$1.error = error); })
.finally(function () { return (this$1.resolved = true); }) }
this.data = result;
} catch (error) {
this.error = error;
}
this.resolved = true;
},
getData: function getData(action) {
switch (typeof action) {
case 'string':
return fetch(
action,
Object.assign({}, this.fetchOptions)
).then(function (data) { return data.json(); })
case 'function':
return action()
default:
return action
}
},
runFunction: function runFunction() {},
observe: function observe() {
var this$1 = this;
this.observer = new IntersectionObserver(
function (entries) {
if (entries[0].intersectionRatio <= 0) { return }
this$1.unobserve();
this$1.run();
},
{
rootMargin: this.margin,
threshold: this.threshold
}
);
this.observer.observe(this.target);
},
unobserve: function unobserve() {
if (this.observer && this.target) {
this.observer.unobserve(this.target);
this.target = null;
}
},
startDelay: function startDelay() {
var this$1 = this;
if (this.delay) {
var delayTimer = setTimeout(
function () {
this$1.isDelay = false;
clearTimeout(delayTimer);
},
typeof +this.delay === 'number' ? this.delay : DEFAULT_DELAY
);
} else {
this.isDelay = false;
}
}
}, obj.render = function render(h) {
if (isVue3) { h = Vue.h; }
if (this.error) {
return getSlot(this, h, 'error', { error: this.error })
}
if (this.resolved) {
return getSlot(this, h, 'default', { data: this.data })
}
if (this.isDelay)
{ return h(
this.tag,
this.height
? {
style: {
height: this.height
}
}
: null
) }
return getSlot(this, h, 'pending', {
data: this.data,
observed: this.observed
})
}, obj );
function convertNodes(h, wrapperTag, nodes) {
if (nodes.length > 1 || !nodes[0].tag) { return h(wrapperTag, {}, nodes) }
return nodes[0]
}
function getSlot(vm, h, name, data) {
var slot = vm.$slots[name];
var scopedSlot = isVue3 ? vm.$slots[name] : vm.$scopedSlots[name];
var node = scopedSlot ? scopedSlot(data) : slot;
return Array.isArray(node) ? convertNodes(h, vm.tag, node) : node
}
function isPromise(value) {
return value && isFunction(value.then) && isFunction(value.catch)
}
function isFunction(value) {
return typeof value === 'function'
}
var index = {
install: function install(app, ref) {
if ( ref === void 0 ) ref = {};
var name = ref.name;
var props = ref.props;
if (props)
{ Object.keys(props).forEach(function (name) {
var prop = Lazily.props[name];
if (prop) { prop.default = props[name]; }
}); }
app.component(name || 'Lazily', Lazily);
}
};
exports.Lazily = Lazily;
exports.default = index;