qrindex.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. !function () {
  2. !function () {
  3. // function shimDataset() {
  4. // // IE 10- does not support dataset
  5. // var datasets = [];
  6. // Object.defineProperty(HTMLElement.prototype, 'dataset', {
  7. // get: function () {
  8. // var dataset, ele = this;
  9. // forEach(datasets, function (item) {
  10. // if (item.ele === ele) {
  11. // dataset = item.dataset;
  12. // return false;
  13. // }
  14. // });
  15. // if (!dataset) {
  16. // dataset = {};
  17. // datasets.push({ele: ele, dataset: dataset});
  18. // forEach(ele.attributes, function (attr) {
  19. // var name = attr.name;
  20. // if (/^data-/.test(name)) {
  21. // name = name.slice(5).replace(/-(\w)/g, function (m, g) {
  22. // return g.toUpperCase();
  23. // });
  24. // dataset[name] = attr.value;
  25. // }
  26. // });
  27. // }
  28. // return dataset;
  29. // },
  30. // });
  31. // }
  32. function shimClassList() {
  33. // IE 9- does not support classList
  34. function getClassList(el) {
  35. function get() {
  36. return el.className.trim().split(/\s+/);
  37. }
  38. function set(list) {
  39. el.className = list.join(' ');
  40. }
  41. function add() {
  42. var list = get();
  43. forEach(arguments, function (arg) {
  44. list.indexOf(arg) < 0 && list.push(arg);
  45. });
  46. set(list);
  47. }
  48. function remove() {
  49. var list = get();
  50. forEach(arguments, function (arg) {
  51. var i = list.indexOf(arg);
  52. if (~i) list.splice(i, 1);
  53. });
  54. set(list);
  55. }
  56. return {
  57. add: add,
  58. remove: remove,
  59. };
  60. }
  61. var classLists = [];
  62. Object.defineProperty(HTMLElement.prototype, 'classList', {
  63. get: function () {
  64. var classList, ele = this;
  65. forEach(classLists, function (item) {
  66. if (item.ele === ele) {
  67. classList = item.classList;
  68. return false;
  69. }
  70. });
  71. if (!classList) {
  72. classList = getClassList(ele);
  73. classLists.push({ele: ele, classList: classList});
  74. }
  75. return classList;
  76. },
  77. });
  78. }
  79. // if (!document.body.dataset) shimDataset();
  80. if (!document.body.classList) shimClassList();
  81. }();
  82. function $(selector) {
  83. return document.querySelector(selector);
  84. }
  85. function forEach(arr, cb) {
  86. for (var i = 0; i < arr.length; i ++)
  87. if (cb.call(arr, arr[i], i) === false) break;
  88. }
  89. function setLogoType(el) {
  90. if (logoTab.head) logoTab.head.classList.remove('active');
  91. logoTab.head = el;
  92. logoTab.type = el.getAttribute('data-type');
  93. el.classList.add('active');
  94. forEach(logoTabs, function (el) {
  95. el.classList[el.getAttribute('data-type') === logoTab.type ? 'add' : 'remove']('active');
  96. });
  97. }
  98. function showImage(img, file) {
  99. if (!file) return;
  100. var reader = new FileReader;
  101. reader.onload = function () {
  102. img.src = this.result;
  103. };
  104. reader.readAsDataURL(file);
  105. }
  106. var logoTabs = document.querySelectorAll('.logo-body>.tab');
  107. var logoHeader = $('.logo-header');
  108. var cbLogo = $('#cblogo');
  109. var logoImg = $('#logoImg');
  110. var effectImg = $('#effect-img');
  111. var logoTab = {};
  112. setLogoType($('.logo-header>[data-type]'));
  113. logoHeader.addEventListener('click', function (e) {
  114. var type = e.target.getAttribute('data-type');
  115. if (type) setLogoType(e.target);
  116. }, false);
  117. $('#fimg').addEventListener('change', function (e) {
  118. showImage(logoImg, e.target.files[0]);
  119. }, false);
  120. $('#effect-file').addEventListener('change', function (e) {
  121. showImage(effectImg, e.target.files[0]);
  122. }, false);
  123. var q = $('#qrcanvas');
  124. var canvas;
  125. $('#qrgen').onclick = function () {
  126. var colorIn = $('#colorIn').value;
  127. var colorOut = $('#colorOut').value;
  128. var colorFore = $('#colorFore').value;
  129. var colorBack = $('#colorBack').value;
  130. var options = {
  131. cellSize: Number($('#cellSize').value),
  132. foreground: [
  133. // foreground color
  134. {style: colorFore},
  135. // outer squares of the positioner
  136. {row: 0, rows: 7, col: 0, cols: 7, style: colorOut},
  137. {row: -7, rows: 7, col: 0, cols: 7, style: colorOut},
  138. {row: 0, rows: 7, col: -7, cols: 7, style: colorOut},
  139. // inner squares of the positioner
  140. {row: 2, rows: 3, col: 2, cols: 3, style: colorIn},
  141. {row: -5, rows: 3, col: 2, cols: 3, style: colorIn},
  142. {row: 2, rows: 3, col: -5, cols: 3, style: colorIn},
  143. ],
  144. background: colorBack,
  145. data: $('#qrtext').value,
  146. typeNumber: Number($('#typeNumber').value),
  147. };
  148. //q.innerHTML='';
  149. if (cbLogo.checked) {
  150. options.logo = {
  151. clearEdges: Number($('#qrclearedges').value),
  152. size: $('#logoSize').value / 100,
  153. margin: Number($('#logoMargin').value),
  154. };
  155. if (logoTab.type == 'image')
  156. options.logo.image = logoImg;
  157. else {
  158. options.logo.text = $('#logoText').value;
  159. var font = $('#logoFont').value;
  160. if (font) options.logo.fontFace = font;
  161. options.logo.color = $('#logoColor').value;
  162. var style = '';
  163. if ($('#logoItalic').checked) style += 'italic ';
  164. if ($('#logoBold').checked) style += 'bold ';
  165. options.logo.fontStyle = style;
  166. }
  167. }
  168. var effect = $('[name=effect-type]:checked').value;
  169. if (effect !== 'none') {
  170. options.effect = {key: effect, value: $('#effect-value').value / 100};
  171. if (effect === 'image') {
  172. options.background = [colorBack, effectImg];
  173. }
  174. }
  175. options.reuseCanvas = canvas;
  176. canvas = qrgen.canvas(options);
  177. q.appendChild(canvas);
  178. };
  179. }();