fe-qrcode.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * QR码生成器
  3. */
  4. baidu.qrcode = (function () {
  5. "use strict";
  6. /**
  7. * 二维码上携带的文字
  8. * @type {String}
  9. */
  10. var text = '';
  11. /**
  12. * 获取要用来生成二维码的文本数据
  13. * @private
  14. */
  15. var _createText = function () {
  16. $("#preview > img").attr("src", "");
  17. var current_tab = $("#tabs").tabs('option', 'selected');
  18. switch (current_tab) {
  19. case 0 :
  20. text = $.trim($("#tabs-1 #tab1_text").attr("value"));
  21. break;
  22. case 1 :
  23. text = "tel:" + $.trim($("#tabs-2 #tab2_telno").attr("value"));
  24. break;
  25. case 2 :
  26. text = $("#tabs-3 [name=tab3_type]:checked").attr("value") + ":" + $.trim($("#tabs-3 #tab3_telno").attr("value")) + ":" + $.trim($("#tabs-3 #tab3_message").attr("value"));
  27. break;
  28. case 3 :
  29. text = "mailto:" + $.trim($("#tabs-4 #tab4_email").attr("value"));
  30. break;
  31. case 4 :
  32. text = "BEGIN:VCARD\nVERSION:3.0\n";
  33. var v = $.trim($("#tabs-5 #tab5_FormattedName").attr("value"));
  34. text += v ? ("FN:" + v + "\n") : "";
  35. v = $.trim($("#tabs-5 #tab5_Telephone").attr("value"));
  36. text += v ? ("TEL:" + v + "\n") : "";
  37. v = $.trim($("#tabs-5 #tab5_Email").attr("value"));
  38. text += v ? ("EMAIL:" + v + "\n") : "";
  39. v = $.trim($("#tabs-5 #tab5_X-MSN").attr("value"));
  40. text += v ? ("X-MSN:" + v + "\n") : "";
  41. v = $.trim($("#tabs-5 #tab5_Organization").attr("value"));
  42. text += v ? ("ORG:" + v + "\n") : "";
  43. v = $.trim($("#tabs-5 #tab5_Title").attr("value"));
  44. text += v ? ("TITLE:" + v + "\n") : "";
  45. v = $.trim($("#tabs-5 #tab5_Address").attr("value"));
  46. text += v ? ("ADR:" + v + "\n") : "";
  47. v = $.trim($("#tabs-5 #tab5_URL").attr("value"));
  48. text += v ? ("URL:" + v + "\n") : "";
  49. text += "END:VCARD";
  50. break;
  51. }
  52. return text;
  53. };
  54. /**
  55. * 创建二维码生成的参数
  56. * @private
  57. */
  58. var _createOptions = function () {
  59. var width = $("#opt_width").val();
  60. var foreground = $('#opt_fc').val();
  61. var defaultOptions = {
  62. // render method: 'canvas', 'image' or 'div'
  63. render: 'canvas',
  64. // version range somewhere in 1 .. 40
  65. minVersion: 1,
  66. maxVersion: 40,
  67. // error correction level: 'L', 'M', 'Q' or 'H'
  68. ecLevel: 'L',
  69. // offset in pixel if drawn onto existing canvas
  70. left: 0,
  71. top: 0,
  72. // size in pixel
  73. size: +width || 200,
  74. // code color or image element
  75. fill: foreground,
  76. // background color or image element, null for transparent background
  77. background: null,
  78. // corner radius relative to module width: 0.0 .. 0.5
  79. radius: 0,
  80. // quiet zone in modules
  81. quiet: 0,
  82. text: text,
  83. // modes
  84. // 0: normal
  85. // 1: label strip
  86. // 2: label box
  87. // 3: image strip
  88. // 4: image box
  89. mode: 0,
  90. mSize: 0.15,
  91. mPosX: 0.5,
  92. mPosY: 0.5,
  93. label: 'FH',
  94. fontname: 'sans',
  95. fontcolor: '#f00',
  96. image: false
  97. };
  98. // 判断是否需要设置icon
  99. var iconType = $('input[name=qr_icon]:checked').val();
  100. if (iconType == 1) {
  101. defaultOptions.mode = 4;
  102. defaultOptions.image = $('#logo_default')[0];
  103. } else if (iconType == 2) {
  104. defaultOptions.mode = 4;
  105. defaultOptions.image = $('#logo')[0];
  106. }
  107. return defaultOptions;
  108. };
  109. /**
  110. * 创建二维码
  111. */
  112. var _createQrCode = function () {
  113. text = _createText();
  114. if (text) {
  115. $('#preview').html('').qrcode(_createOptions());
  116. } else {
  117. $('#preview').html('再在输入框里输入一些内容,就能生成二维码了哦~')
  118. }
  119. $('#fieldset_qr').show();
  120. };
  121. /**
  122. * 设置ICON
  123. * @param file
  124. * @private
  125. */
  126. var _drawIcon = function (file) {
  127. if (/image\//.test(file.type)) {
  128. var reader = new FileReader();
  129. reader.onload = function (evt) {
  130. $('#logo').attr('src', evt.target.result);
  131. _createQrCode();
  132. };
  133. reader.readAsDataURL(file);
  134. } else {
  135. alert('请选择图片文件!');
  136. }
  137. };
  138. /**
  139. * 可以拖拽一张图片上来,生成小icon
  140. * @private
  141. */
  142. var _dragAndDrop = function () {
  143. $(document).bind('drop', function (e) {
  144. e.preventDefault();
  145. e.stopPropagation();
  146. var files = e.originalEvent.dataTransfer.files;
  147. if (files.length) {
  148. _drawIcon(files[0]);
  149. }
  150. }).bind('dragover', function (e) {
  151. e.preventDefault();
  152. e.stopPropagation();
  153. });
  154. };
  155. /**
  156. * 绑定事件
  157. * @private
  158. */
  159. var _bindEvents = function () {
  160. var current_tab_id = 'tabs-1';
  161. $("#tabs").tabs()
  162. .bind("tabsselect", function (event, ui) {
  163. current_tab_id = ui.panel.id;
  164. })
  165. .tabs("select", 0);
  166. $("#confirm_button").button().click(function () {
  167. _createQrCode();
  168. });
  169. $("#opt_fc").colorpicker({
  170. fillcolor: true,
  171. success: function (obj, color) {
  172. _createQrCode();
  173. }
  174. });
  175. $('#remove_icon,#default_icon').click(function (e) {
  176. _createQrCode();
  177. });
  178. $('#upload_icon').click(function (e) {
  179. $('#file').trigger('click');
  180. });
  181. $('#file').change(function (e) {
  182. if (this.files.length) {
  183. _drawIcon(this.files[0]);
  184. this.value = '';
  185. }
  186. });
  187. _dragAndDrop();
  188. };
  189. var _init = function () {
  190. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  191. chrome.runtime.onMessage.addListener(function (request, sender, callback) {
  192. if (request.type == MSG_TYPE.TAB_CREATED_OR_UPDATED && request.event == 'qrcode') {
  193. if (request.content) {
  194. document.getElementById('tab1_text').value = (request.content);
  195. setTimeout(_createQrCode, 50);
  196. }
  197. }
  198. });
  199. $(function () {
  200. _bindEvents();
  201. $('#tab1_text').focus();
  202. });
  203. };
  204. return {
  205. init: _init
  206. };
  207. })();
  208. baidu.qrcode.init();