JsBarcode.js 6.8 KB

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