-
Notifications
You must be signed in to change notification settings - Fork 4
/
qrcode.html
65 lines (56 loc) · 1.5 KB
/
qrcode.html
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
<script src='../jquery.qrcode/dist/jquery.qrcode.js'></script>
<element name='qrcode'>
<template>
<div id='{{id}}' render='{{render}}' style='{{style}}' size='{{size}}'>
<content></content>
</div>
</template>
<script>
registerElement('qrcode', {
text: '',
init: function() {
var $ele = this.$ele;
if ($ele.attr('size') != '{{size}}') {
var size = $ele.attr('size');
$ele.css({
'width': size,
'height': size
});
}
this.render(this.$originEle.text());
},
render: function(txt) {
var $ele = this.$ele;
if (this.text !== txt) {
this.text = txt;
$ele.html('');
$ele.qrcode({
render: $ele.attr('render'),
background: '#fff',
size: parseInt($ele.attr('size'), 10),
text: toUtf8(txt)
});
}
}
});
function toUtf8(str) {
var out, i, len, c;
out = "";
len = str.length;
for (i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
</script>
</element>