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