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