-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvue-dummy.cjs.js
80 lines (64 loc) · 2.45 KB
/
vue-dummy.cjs.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
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var Dummy = _interopDefault(require('dummyjs'));
var Plugin = function () {};
Plugin.install = function (Vue, options) {
if (Plugin.installed) {
return;
}
var directive = function (el, binding) {
if(!el) {
return;
}
var args = binding.arg // v-dummy:args
|| Object.keys(binding.modifiers).join(',') // v-dummy.args
|| (typeof binding.value == 'string' ? binding.value : binding.expression)
|| '';
var nodeName = el.nodeName.toLowerCase();
if(nodeName === 'img') {
el.src = Dummy.src(args, el);
} else if(nodeName === 'table') {
var tableRow = function () { return ("<tr><td>" + (Dummy.text(3)) + "</td><td>" + (Dummy.text(3)) + "</td><td>" + (Dummy.text(3)) + "</td></tr>"); };
el.innerHTML = "<thead>" + (tableRow().replace(/td>/g, 'th>')) + "</thead><tbody>" + (tableRow()) + (tableRow()) + (tableRow()) + "</tbody>";
} else if(nodeName === 'ul' || nodeName === 'ol') {
el.innerHTML += "<li>" + (Dummy.text(3)) + "</li><li>" + (Dummy.text(3)) + "</li><li>" + (Dummy.text(3)) + "</li>";
} else {
el.innerHTML += Dummy.text(args);
}
};
Vue.directive('dummy', {
// called when the bound element has been inserted into its parent node
inserted: directive
});
Vue.directive('dummy-self', {
inserted: function (el, binding) {
el.outerHTML = Dummy.text(typeof binding.value == 'string' ? binding.value : binding.expression);
}
});
var componentProps = 'i,img,image,t,txt,text,w,words'.split(',');
var componentPropsObj = componentProps.reduce(function (c, v, i) { c[v] = true; return c }, {});
Vue.component('dummy', {
render: function (createElement) {
var this$1 = this;
var value = '';
var renderImage = false;
for (var i = 0; i < componentProps.length; i++) {
if(typeof this$1[componentProps[i]] !== 'undefined') {
value = this$1[componentProps[i]] + '';
renderImage = componentProps[i][0] === 'i' || value.indexOf('x') > 0;
}
}
return createElement(renderImage ? 'img' : 'span', {
directives: [{
name: renderImage ? 'dummy' : 'dummy-self',
value: value
}]
});
},
props: componentPropsObj
});
};
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(Plugin);
}
module.exports = Plugin;