-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
100 lines (93 loc) · 2.54 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
var JsBarcode = require('jsbarcode');
var VueBarcode = {
render: function (createElement) {
return createElement('div', [
createElement(this.elementTag, {
style: { display: this.valid ? undefined : 'none' },
'class': ['vue-barcode-element']
}),
createElement('div', {
style: { display: this.valid ? 'none' : undefined }
}, this.$slots.default),
]);
},
props: {
value: [String, Number],
format: [String],
width: [String, Number],
height: [String, Number],
displayValue: {
type: [String, Boolean],
default: true
},
text: [String, Number],
fontOptions : [String],
font: [String],
textAlign: [String],
textPosition: [String],
textMargin: [String, Number],
fontSize: [String, Number],
background: [String],
lineColor: [String],
margin: [String, Number],
marginTop: [String, Number],
marginBottom: [String, Number],
marginLeft: [String, Number],
marginRight: [String, Number],
flat: [Boolean],
ean128: [String, Boolean],
elementTag: {
type: String,
default: 'svg',
validator: function (value) {
return ['canvas', 'svg', 'img'].indexOf(value) !== -1
}
}
},
mounted: function(){
this.$watch('$props', render, { deep: true, immediate: true });
render.call(this);
},
data: function(){
return {valid: true};
}
};
function render(){
var that = this;
var settings = {
format: this.format,
width: this.width,
height: this.height,
displayValue: this.displayValue,
text: this.text,
fontOptions: this.fontOptions,
font: this.font,
textAlign: this.textAlign,
textPosition: this.textPosition,
textMargin: this.textMargin,
fontSize: this.fontSize,
background: this.background,
lineColor: this.lineColor,
margin: this.margin,
marginTop: this.marginTop,
marginBottom: this.marginBottom,
marginLeft: this.marginLeft,
marginRight: this.marginRight,
flat: this.flat,
ean128: this.ean128,
valid: function (valid) {
that.valid = valid;
},
elementTag: this.elementTag
};
removeUndefinedProps(settings);
JsBarcode(this.$el.querySelector('.vue-barcode-element'), String(this.value), settings);
}
function removeUndefinedProps(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop) && obj[prop] === undefined) {
delete obj[prop];
}
}
}
module.exports = VueBarcode;