JsBarcode.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Import all the barcodes
  2. import barcodes from './barcodes/';
  3. // Help functions
  4. import merge from './help/merge.js';
  5. import linearizeEncodings from './help/linearizeEncodings.js';
  6. import fixOptions from './help/fixOptions.js';
  7. import getRenderProperties from './help/getRenderProperties.js';
  8. import optionsFromStrings from './help/optionsFromStrings.js';
  9. // Exceptions
  10. import ErrorHandler from './exceptions/ErrorHandler.js';
  11. import {InvalidInputException, NoElementException} from './exceptions/exceptions.js';
  12. // Default values
  13. import defaults from './options/defaults.js';
  14. // The protype of the object returned from the JsBarcode() call
  15. let API = function(){};
  16. // The first call of the library API
  17. // Will return an object with all barcodes calls and the data that is used
  18. // by the renderers
  19. let JsBarcode = function(element, text, options){
  20. var api = new API();
  21. if(typeof element === "undefined"){
  22. throw Error("No element to render on was provided.");
  23. }
  24. // Variables that will be pased through the API calls
  25. api._renderProperties = getRenderProperties(element);
  26. api._encodings = [];
  27. api._options = defaults;
  28. api._errorHandler = new ErrorHandler(api);
  29. // If text is set, use the simple syntax (render the barcode directly)
  30. if(typeof text !== "undefined"){
  31. options = options || {};
  32. if(!options.format){
  33. options.format = autoSelectBarcode();
  34. }
  35. api.options(options)[options.format](text, options).render();
  36. }
  37. return api;
  38. };
  39. // To make tests work TODO: remove
  40. JsBarcode.getModule = function(name){
  41. return barcodes[name];
  42. };
  43. // Register all barcodes
  44. for(var name in barcodes){
  45. if(barcodes.hasOwnProperty(name)){ // Security check if the propery is a prototype property
  46. registerBarcode(barcodes, name);
  47. }
  48. }
  49. function registerBarcode(barcodes, name){
  50. API.prototype[name] =
  51. API.prototype[name.toUpperCase()] =
  52. API.prototype[name.toLowerCase()] =
  53. function(text, options){
  54. var api = this;
  55. return api._errorHandler.wrapBarcodeCall(function(){
  56. // Ensure text is options.text
  57. options.text = typeof options.text === 'undefined' ? undefined : '' + options.text;
  58. var newOptions = merge(api._options, options);
  59. newOptions = optionsFromStrings(newOptions);
  60. var Encoder = barcodes[name];
  61. var encoded = encode(text, Encoder, newOptions);
  62. api._encodings.push(encoded);
  63. return api;
  64. });
  65. };
  66. }
  67. // encode() handles the Encoder call and builds the binary string to be rendered
  68. function encode(text, Encoder, options){
  69. // Ensure that text is a string
  70. text = "" + text;
  71. var encoder = new Encoder(text, options);
  72. // If the input is not valid for the encoder, throw error.
  73. // If the valid callback option is set, call it instead of throwing error
  74. if(!encoder.valid()){
  75. throw new InvalidInputException(encoder.constructor.name, text);
  76. }
  77. // Make a request for the binary data (and other infromation) that should be rendered
  78. var encoded = encoder.encode();
  79. // Encodings can be nestled like [[1-1, 1-2], 2, [3-1, 3-2]
  80. // Convert to [1-1, 1-2, 2, 3-1, 3-2]
  81. encoded = linearizeEncodings(encoded);
  82. // Merge
  83. for(let i = 0; i < encoded.length; i++){
  84. encoded[i].options = merge(options, encoded[i].options);
  85. }
  86. return encoded;
  87. }
  88. function autoSelectBarcode(){
  89. // If CODE128 exists. Use it
  90. if(barcodes["CODE128"]){
  91. return "CODE128";
  92. }
  93. // Else, take the first (probably only) barcode
  94. return Object.keys(barcodes)[0];
  95. }
  96. // Sets global encoder options
  97. // Added to the api by the JsBarcode function
  98. API.prototype.options = function(options){
  99. this._options = merge(this._options, options);
  100. return this;
  101. };
  102. // Will create a blank space (usually in between barcodes)
  103. API.prototype.blank = function(size){
  104. const zeroes = new Array(size + 1).join("0");
  105. this._encodings.push({data: zeroes});
  106. return this;
  107. };
  108. // Initialize JsBarcode on all HTML elements defined.
  109. API.prototype.init = function(){
  110. // Should do nothing if no elements where found
  111. if(!this._renderProperties){
  112. return;
  113. }
  114. // Make sure renderProperies is an array
  115. if(!Array.isArray(this._renderProperties)){
  116. this._renderProperties = [this._renderProperties];
  117. }
  118. var renderProperty;
  119. for(let i in this._renderProperties){
  120. renderProperty = this._renderProperties[i];
  121. var options = merge(this._options, renderProperty.options);
  122. if(options.format == "auto"){
  123. options.format = autoSelectBarcode();
  124. }
  125. this._errorHandler.wrapBarcodeCall(function(){
  126. var text = options.value;
  127. var Encoder = barcodes[options.format.toUpperCase()];
  128. var encoded = encode(text, Encoder, options);
  129. render(renderProperty, encoded, options);
  130. });
  131. }
  132. };
  133. // The render API call. Calls the real render function.
  134. API.prototype.render = function(){
  135. if(!this._renderProperties){
  136. throw new NoElementException();
  137. }
  138. if(Array.isArray(this._renderProperties)){
  139. for(var i = 0; i < this._renderProperties.length; i++){
  140. render(this._renderProperties[i], this._encodings, this._options);
  141. }
  142. }
  143. else{
  144. render(this._renderProperties, this._encodings, this._options);
  145. }
  146. return this;
  147. };
  148. API.prototype._defaults = defaults;
  149. // Prepares the encodings and calls the renderer
  150. function render(renderProperties, encodings, options){
  151. encodings = linearizeEncodings(encodings);
  152. for(let i = 0; i < encodings.length; i++){
  153. encodings[i].options = merge(options, encodings[i].options);
  154. fixOptions(encodings[i].options);
  155. }
  156. fixOptions(options);
  157. var Renderer = renderProperties.renderer;
  158. var renderer = new Renderer(renderProperties.element, encodings, options);
  159. renderer.render();
  160. if(renderProperties.afterRender){
  161. renderProperties.afterRender();
  162. }
  163. }
  164. // Export to browser
  165. if(typeof window !== "undefined"){
  166. window.JsBarcode = JsBarcode;
  167. }
  168. // Export to jQuery
  169. /*global jQuery */
  170. if (typeof jQuery !== 'undefined') {
  171. jQuery.fn.JsBarcode = function(content, options){
  172. var elementArray = [];
  173. jQuery(this).each(function() {
  174. elementArray.push(this);
  175. });
  176. return JsBarcode(elementArray, content, options);
  177. };
  178. }
  179. // Export to commonJS
  180. module.exports = JsBarcode;