scripts.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. (function ($) {
  2. 'use strict';
  3. var isOpera = Object.prototype.toString.call(window.opera) === '[object Opera]',
  4. guiValuePairs = [
  5. ["size", "px"],
  6. ["minversion", ""],
  7. ["quiet", " modules"],
  8. ["radius", "%"],
  9. ["msize", "%"],
  10. ["mposx", "%"],
  11. ["mposy", "%"]
  12. ],
  13. updateGui = function () {
  14. $.each(guiValuePairs, function (idx, pair) {
  15. var $label = $('label[for="' + pair[0] + '"]');
  16. $label.text($label.text().replace(/:.*/, ': ' + $('#' + pair[0]).val() + pair[1]));
  17. });
  18. },
  19. updateQrCode = function () {
  20. var options = {
  21. render: $("#render").val(),
  22. ecLevel: $("#eclevel").val(),
  23. minVersion: parseInt($("#minversion").val(), 10),
  24. fill: $("#fill").val(),
  25. background: $("#background").val(),
  26. // fill: $("#img-buffer")[0],
  27. text: $("#text").val(),
  28. size: parseInt($("#size").val(), 10),
  29. radius: parseInt($("#radius").val(), 10) * 0.01,
  30. quiet: parseInt($("#quiet").val(), 10),
  31. mode: parseInt($("#mode").val(), 10),
  32. mSize: parseInt($("#msize").val(), 10) * 0.01,
  33. mPosX: parseInt($("#mposx").val(), 10) * 0.01,
  34. mPosY: parseInt($("#mposy").val(), 10) * 0.01,
  35. label: $("#label").val(),
  36. fontname: $("#font").val(),
  37. fontcolor: $("#fontcolor").val(),
  38. image: $("#img-buffer")[0]
  39. };
  40. $("#container").empty().qrcode(options);
  41. },
  42. update = function () {
  43. updateGui();
  44. updateQrCode();
  45. },
  46. onImageInput = function () {
  47. var input = $("#image")[0];
  48. if (input.files && input.files[0]) {
  49. var reader = new FileReader();
  50. reader.onload = function (event) {
  51. $("#img-buffer").attr("src", event.target.result);
  52. $("#mode").val("4");
  53. setTimeout(update, 250);
  54. };
  55. reader.readAsDataURL(input.files[0]);
  56. }
  57. },
  58. download = function () {
  59. var data = $("#container canvas")[0].toDataURL('image/png');
  60. $("#download").attr("href", data);
  61. };
  62. $(function () {
  63. if (isOpera) {
  64. $('html').addClass('opera');
  65. $('#radius').prop('disabled', true);
  66. }
  67. $("#download").on("click", download);
  68. $("#image").on('change', onImageInput);
  69. $("input, textarea, select").on("input change", update);
  70. $(window).load(update);
  71. update();
  72. });
  73. }(jQuery));