您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

57 行
1.4 KiB

  1. frappe.ui.form.ControlBarcode = frappe.ui.form.ControlData.extend({
  2. make_wrapper() {
  3. // Create the elements for barcode area
  4. this._super();
  5. let $input_wrapper = this.$wrapper.find('.control-input-wrapper');
  6. this.barcode_area = $(`<div class="barcode-wrapper border"><svg height=80></svg></div>`);
  7. frappe.require("assets/frappe/js/lib/JsBarcode.all.min.js", () => {
  8. this.barcode_area.appendTo($input_wrapper);
  9. });
  10. },
  11. parse(value) {
  12. // Parse raw value
  13. return this.get_barcode_html(value);
  14. },
  15. set_formatted_input(value) {
  16. // Set values to display
  17. const svg = value;
  18. const barcode_value = $(svg).attr('data-barcode-value');
  19. this.$input.val(barcode_value);
  20. this.barcode_area.html(svg);
  21. },
  22. get_barcode_html(value) {
  23. // Get svg
  24. const svg = this.barcode_area.find('svg')[0];
  25. JsBarcode(svg, value, this.get_options(value));
  26. $(svg).attr('data-barcode-value', value);
  27. return this.barcode_area.html();
  28. },
  29. get_options(value) {
  30. // get JsBarcode options
  31. let options = JSON.parse('{ "height" : 40 }');
  32. if (this.isValidJson(this.df.options)) {
  33. options = JSON.parse(this.df.options);
  34. if (options.format && options.format === "EAN") {
  35. options.format = value.length == 8 ? "EAN8" : "EAN13";
  36. }
  37. }
  38. return options;
  39. },
  40. isValidJson(jsonData) {
  41. try {
  42. JSON.parse(jsonData);
  43. return true;
  44. } catch (e) {
  45. return false;
  46. }
  47. }
  48. });