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
|
|
|
var newOptions = merge(api._options, options);
|
70
|
|
|
newOptions = optionsFromStrings(newOptions);
|
71
|
|
|
var Encoder = barcodes[name];
|
72
|
|
|
var encoded = encode(text, Encoder, newOptions);
|
73
|
|
|
api._encodings.push(encoded);
|
74
|
|
|
|
75
|
|
|
return api;
|
76
|
|
|
});
|
77
|
|
|
};
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
// encode() handles the Encoder call and builds the binary string to be rendered
|
81
|
|
|
function encode(text, Encoder, options){
|
82
|
|
|
// Ensure that text is a string
|
83
|
|
|
text = "" + text;
|
84
|
|
|
|
85
|
|
|
var encoder = new Encoder(text, options);
|
86
|
|
|
|
87
|
|
|
// If the input is not valid for the encoder, throw error.
|
88
|
|
|
// If the valid callback option is set, call it instead of throwing error
|
89
|
|
|
if(!encoder.valid()){
|
90
|
|
|
throw new InvalidInputException(encoder.constructor.name, text);
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
// Make a request for the binary data (and other infromation) that should be rendered
|
94
|
|
|
var encoded = encoder.encode();
|
95
|
|
|
|
96
|
|
|
// Encodings can be nestled like [[1-1, 1-2], 2, [3-1, 3-2]
|
97
|
|
|
// Convert to [1-1, 1-2, 2, 3-1, 3-2]
|
98
|
|
|
encoded = linearizeEncodings(encoded);
|
99
|
|
|
|
100
|
|
|
// Merge
|
101
|
|
|
for(let i = 0; i < encoded.length; i++){
|
102
|
|
|
encoded[i].options = merge(options, encoded[i].options);
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
return encoded;
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
function autoSelectBarcode(){
|
109
|
|
|
// If CODE128 exists. Use it
|
110
|
|
|
if(barcodes["CODE128"]){
|
111
|
|
|
return "CODE128";
|
112
|
|
|
}
|
113
|
|
|
|
114
|
|
|
// Else, take the first (probably only) barcode
|
115
|
|
|
return Object.keys(barcodes)[0];
|
116
|
|
|
}
|
117
|
|
|
|
118
|
|
|
// Sets global encoder options
|
119
|
|
|
// Added to the api by the JsBarcode function
|
120
|
|
|
API.prototype.options = function(options){
|
121
|
|
|
this._options = merge(this._options, options);
|
122
|
|
|
return this;
|
123
|
|
|
};
|
124
|
|
|
|
125
|
|
|
// Will create a blank space (usually in between barcodes)
|
126
|
|
|
API.prototype.blank = function(size){
|
127
|
|
|
var zeroes = "0".repeat(size);
|
128
|
|
|
this._encodings.push({data: zeroes});
|
129
|
|
|
return this;
|
130
|
|
|
};
|
131
|
|
|
|
132
|
|
|
// Initialize JsBarcode on all HTML elements defined.
|
133
|
|
|
API.prototype.init = function(){
|
134
|
|
|
// Should do nothing if no elements where found
|
135
|
|
|
if(!this._renderProperties){
|
136
|
|
|
return;
|
137
|
|
|
}
|
138
|
|
|
|
139
|
|
|
// Make sure renderProperies is an array
|
140
|
|
|
if(!Array.isArray(this._renderProperties)){
|
141
|
|
|
this._renderProperties = [this._renderProperties];
|
142
|
|
|
}
|
143
|
|
|
|
144
|
|
|
var renderProperty;
|
145
|
|
|
for(let i in this._renderProperties){
|
146
|
|
|
renderProperty = this._renderProperties[i];
|
147
|
|
|
var options = merge(this._options, renderProperty.options);
|
148
|
|
|
|
149
|
|
|
if(options.format == "auto"){
|
150
|
|
|
options.format = autoSelectBarcode();
|
151
|
|
|
}
|
152
|
|
|
|
153
|
|
|
this._errorHandler.wrapBarcodeCall(function(){
|
154
|
|
|
var text = options.value;
|
|
|
|
|
155
|
|
|
var Encoder = barcodes[options.format.toUpperCase()];
|
156
|
|
|
var encoded = encode(text, Encoder, options);
|
157
|
|
|
|
158
|
|
|
render(renderProperty, encoded, options);
|
|
|
|
|
159
|
|
|
});
|
160
|
|
|
}
|
161
|
|
|
};
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
// The render API call. Calls the real render function.
|
165
|
|
|
API.prototype.render = function(){
|
166
|
|
|
if(!this._renderProperties){
|
167
|
|
|
throw new NoElementException();
|
168
|
|
|
}
|
169
|
|
|
|
170
|
|
|
if(Array.isArray(this._renderProperties)){
|
171
|
|
|
for(var i = 0; i < this._renderProperties.length; i++){
|
172
|
|
|
render(this._renderProperties[i], this._encodings, this._options);
|
173
|
|
|
}
|
174
|
|
|
}
|
175
|
|
|
else{
|
176
|
|
|
render(this._renderProperties, this._encodings, this._options);
|
177
|
|
|
}
|
178
|
|
|
|
179
|
|
|
return this;
|
180
|
|
|
};
|
181
|
|
|
|
182
|
|
|
API.prototype._defaults = defaults;
|
183
|
|
|
|
184
|
|
|
// Prepares the encodings and calls the renderer
|
185
|
|
|
function render(renderProperties, encodings, options){
|
186
|
|
|
encodings = linearizeEncodings(encodings);
|
187
|
|
|
|
188
|
|
|
for(let i = 0; i < encodings.length; i++){
|
189
|
|
|
encodings[i].options = merge(options, encodings[i].options);
|
190
|
|
|
fixOptions(encodings[i].options);
|
191
|
|
|
}
|
192
|
|
|
|
193
|
|
|
fixOptions(options);
|
194
|
|
|
|
195
|
|
|
var Renderer = renderProperties.renderer;
|
196
|
|
|
var renderer = new Renderer(renderProperties.element, encodings, options);
|
197
|
|
|
renderer.render();
|
198
|
|
|
|
199
|
|
|
if(renderProperties.afterRender){
|
200
|
|
|
renderProperties.afterRender();
|
201
|
|
|
}
|
202
|
|
|
}
|
203
|
|
|
|
204
|
|
|
// Export to browser
|
205
|
|
|
if(typeof window !== "undefined"){
|
206
|
|
|
window.JsBarcode = JsBarcode;
|
207
|
|
|
}
|
208
|
|
|
|
209
|
|
|
// Export to jQuery
|
210
|
|
|
/*global jQuery */
|
211
|
|
|
if (typeof jQuery !== 'undefined') {
|
212
|
|
|
jQuery.fn.JsBarcode = function(content, options){
|
213
|
|
|
var elementArray = [];
|
214
|
|
|
jQuery(this).each(function() {
|
215
|
|
|
elementArray.push(this);
|
216
|
|
|
});
|
217
|
|
|
return JsBarcode(elementArray, content, options);
|
218
|
|
|
};
|
219
|
|
|
}
|
220
|
|
|
|
221
|
|
|
// Export to commonJS
|
222
|
|
|
module.exports = JsBarcode;
|
223
|
|
|
|