yh-hiprint 2.4.0 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HiprintDesigner.vue +3 -0
- package/designer.vue +54 -55
- package/libs/hiprint.bundle.js +11 -1
- package/libs/jsbarcode/JsBarcode.js +225 -225
- package/package.json +1 -1
package/HiprintDesigner.vue
CHANGED
|
@@ -842,8 +842,11 @@ function init() {
|
|
|
842
842
|
fontList: [
|
|
843
843
|
{title: '微软雅黑', value: 'Microsoft YaHei'},
|
|
844
844
|
{title: '黑体', value: 'STHeitiSC-Light'},
|
|
845
|
+
{title: '思源黑体', value: 'SourceHanSansCN-Normal'},
|
|
845
846
|
{title: '宋体', value: 'SimSun'},
|
|
847
|
+
{title: '华为楷体', value: 'STKaiti'},
|
|
846
848
|
{title: 'cursive', value: 'cursive'},
|
|
849
|
+
{title: 'Times New Roman', value: 'Times New Roman'},
|
|
847
850
|
],
|
|
848
851
|
dataMode: 1,
|
|
849
852
|
history: true,
|
package/designer.vue
CHANGED
|
@@ -265,14 +265,14 @@
|
|
|
265
265
|
</el-dialog>
|
|
266
266
|
</template>
|
|
267
267
|
<script setup>
|
|
268
|
-
import {
|
|
269
|
-
import {
|
|
270
|
-
import {
|
|
271
|
-
import {
|
|
272
|
-
import axios from
|
|
268
|
+
import {onMounted, ref, onActivated, onDeactivated, computed, watch, nextTick} from 'vue';
|
|
269
|
+
import {hiprint, defaultElementTypeProvider, fontSize, scale, zIndex, panel, usePaper, useScale, useDataSource} from 'yh-hiprint';
|
|
270
|
+
import {useRoute, onBeforeRouteUpdate} from 'vue-router/dist/vue-router';
|
|
271
|
+
import {ElMessageBox} from 'element-plus';
|
|
272
|
+
import axios from '@/libs/api.request';
|
|
273
273
|
|
|
274
274
|
const route = useRoute();
|
|
275
|
-
const {
|
|
275
|
+
const {paperType, paperWidth, paperHeight, setPaper} = usePaper();
|
|
276
276
|
function setPaperHandler(type) {
|
|
277
277
|
setPaper(type, () => {
|
|
278
278
|
hiprintTemplate.value.setPaper(paperWidth.value, paperHeight.value);
|
|
@@ -281,23 +281,23 @@ function setPaperHandler(type) {
|
|
|
281
281
|
|
|
282
282
|
const hiprintTemplate = ref(null);
|
|
283
283
|
const canvasRef = ref();
|
|
284
|
-
const {
|
|
284
|
+
const {scaleValue, scalePercentage, canZoomIn, canZoomOut, zoomIn, zoomOut} = useScale(() => {
|
|
285
285
|
hiprintTemplate.value.zoom(scaleValue.value);
|
|
286
286
|
canvasRef?.value.update();
|
|
287
287
|
});
|
|
288
288
|
|
|
289
289
|
const previewShow = ref(false);
|
|
290
|
-
const previewHtml = ref(
|
|
290
|
+
const previewHtml = ref('');
|
|
291
291
|
async function previewPrint() {
|
|
292
292
|
let {
|
|
293
|
-
data: {
|
|
293
|
+
data: {list, json},
|
|
294
294
|
} = await axios.request({
|
|
295
295
|
url: `/printTemplate/data/${detailData.value.code}`,
|
|
296
|
-
method:
|
|
297
|
-
type:
|
|
296
|
+
method: 'post',
|
|
297
|
+
type: 'json',
|
|
298
298
|
data: [
|
|
299
299
|
{
|
|
300
|
-
code:
|
|
300
|
+
code: '50101820',
|
|
301
301
|
},
|
|
302
302
|
],
|
|
303
303
|
});
|
|
@@ -323,12 +323,12 @@ async function previewPrint() {
|
|
|
323
323
|
} else {
|
|
324
324
|
list = [];
|
|
325
325
|
}
|
|
326
|
-
previewHtml.value = new hiprint.PrintTemplate({
|
|
326
|
+
previewHtml.value = new hiprint.PrintTemplate({template: JSON.parse(json)}).getHtml(list)[0].innerHTML;
|
|
327
327
|
previewShow.value = true;
|
|
328
328
|
} else {
|
|
329
329
|
ElMessage.warning({
|
|
330
|
-
title:
|
|
331
|
-
message:
|
|
330
|
+
title: '警告',
|
|
331
|
+
message: '模板配置不存在,请检查',
|
|
332
332
|
});
|
|
333
333
|
}
|
|
334
334
|
}
|
|
@@ -337,25 +337,25 @@ function saveConfig() {
|
|
|
337
337
|
let arr = [].concat(formCode.value);
|
|
338
338
|
let json = hiprintTemplate.value.getJson();
|
|
339
339
|
json.panels[0].printElements.forEach((item) => {
|
|
340
|
-
if (item.printElementType.type ===
|
|
340
|
+
if (item.printElementType.type === 'table') {
|
|
341
341
|
arr.push(item.options.field);
|
|
342
342
|
}
|
|
343
343
|
});
|
|
344
344
|
|
|
345
345
|
axios
|
|
346
346
|
.request({
|
|
347
|
-
url:
|
|
348
|
-
method:
|
|
349
|
-
type:
|
|
347
|
+
url: '/printTemplate/save',
|
|
348
|
+
method: 'post',
|
|
349
|
+
type: 'json',
|
|
350
350
|
data: {
|
|
351
351
|
id: detailData.value.id,
|
|
352
352
|
json: JSON.stringify(json),
|
|
353
|
-
dsIds: arr.join(
|
|
353
|
+
dsIds: arr.join(','),
|
|
354
354
|
},
|
|
355
355
|
})
|
|
356
356
|
.then((res) => {
|
|
357
357
|
ElMessage.success({
|
|
358
|
-
message:
|
|
358
|
+
message: '恭喜模板保存成功',
|
|
359
359
|
});
|
|
360
360
|
});
|
|
361
361
|
}
|
|
@@ -364,7 +364,7 @@ function clearPrint() {
|
|
|
364
364
|
hiprintTemplate.value?.clear();
|
|
365
365
|
}
|
|
366
366
|
|
|
367
|
-
const {
|
|
367
|
+
const {detailData, getDetail, listCode, dataSourceList, listColumns, formCode, formColumns, dataSourceForm, getDataSourceList} = useDataSource(axios);
|
|
368
368
|
|
|
369
369
|
watch(
|
|
370
370
|
() => formColumns.value,
|
|
@@ -387,15 +387,15 @@ watch(
|
|
|
387
387
|
);
|
|
388
388
|
let currentElementObj = ref(null);
|
|
389
389
|
let currentElementObjColumns = computed(() => {
|
|
390
|
-
if (currentElementObj.value?.printElementType.type ===
|
|
391
|
-
return currentElementObj.value.options.columns[0][
|
|
390
|
+
if (currentElementObj.value?.printElementType.type === 'table') {
|
|
391
|
+
return currentElementObj.value.options.columns[0]['columns'] || [];
|
|
392
392
|
} else {
|
|
393
393
|
return [];
|
|
394
394
|
}
|
|
395
395
|
});
|
|
396
396
|
|
|
397
397
|
function tableFieldChange(val) {
|
|
398
|
-
currentElementObj.value.updateOption(
|
|
398
|
+
currentElementObj.value.updateOption('field', val);
|
|
399
399
|
}
|
|
400
400
|
|
|
401
401
|
function tableColumnCancel() {
|
|
@@ -417,11 +417,11 @@ function init() {
|
|
|
417
417
|
options: [],
|
|
418
418
|
},
|
|
419
419
|
{
|
|
420
|
-
name:
|
|
420
|
+
name: '样式',
|
|
421
421
|
options: [
|
|
422
422
|
{
|
|
423
|
-
name:
|
|
424
|
-
after:
|
|
423
|
+
name: 'scale',
|
|
424
|
+
after: 'transform',
|
|
425
425
|
hidden: false,
|
|
426
426
|
},
|
|
427
427
|
],
|
|
@@ -429,16 +429,16 @@ function init() {
|
|
|
429
429
|
],
|
|
430
430
|
supportOptions: [
|
|
431
431
|
{
|
|
432
|
-
name:
|
|
432
|
+
name: 'styler',
|
|
433
433
|
hidden: true,
|
|
434
434
|
},
|
|
435
435
|
{
|
|
436
|
-
name:
|
|
437
|
-
after:
|
|
436
|
+
name: 'scale',
|
|
437
|
+
after: 'transform',
|
|
438
438
|
hidden: false,
|
|
439
439
|
},
|
|
440
440
|
{
|
|
441
|
-
name:
|
|
441
|
+
name: 'formatter',
|
|
442
442
|
hidden: true,
|
|
443
443
|
},
|
|
444
444
|
],
|
|
@@ -447,18 +447,18 @@ function init() {
|
|
|
447
447
|
tabs: [
|
|
448
448
|
{
|
|
449
449
|
replace: true,
|
|
450
|
-
name:
|
|
450
|
+
name: '基本',
|
|
451
451
|
options: [
|
|
452
452
|
{
|
|
453
|
-
name:
|
|
453
|
+
name: 'field',
|
|
454
454
|
hidden: false,
|
|
455
455
|
},
|
|
456
456
|
{
|
|
457
|
-
name:
|
|
457
|
+
name: 'src',
|
|
458
458
|
hidden: false,
|
|
459
459
|
},
|
|
460
460
|
{
|
|
461
|
-
name:
|
|
461
|
+
name: 'fit',
|
|
462
462
|
hidden: false,
|
|
463
463
|
},
|
|
464
464
|
],
|
|
@@ -466,31 +466,30 @@ function init() {
|
|
|
466
466
|
],
|
|
467
467
|
},
|
|
468
468
|
table: {
|
|
469
|
-
tabs: [{
|
|
469
|
+
tabs: [{options: [{name: 'field', hidden: true}]}],
|
|
470
470
|
},
|
|
471
471
|
});
|
|
472
472
|
|
|
473
|
-
hiprint.PrintElementTypeManager.buildByHtml(hiprintJQuery(
|
|
474
|
-
hiprintJQuery(
|
|
473
|
+
hiprint.PrintElementTypeManager.buildByHtml(hiprintJQuery('.ep-draggable-item'));
|
|
474
|
+
hiprintJQuery('#hiprint-printTemplate').empty();
|
|
475
475
|
let template = JSON.parse(detailData.value.json) || panel;
|
|
476
476
|
hiprintTemplate.value = new hiprint.PrintTemplate({
|
|
477
477
|
template,
|
|
478
478
|
onImageChooseClick: (target) => {
|
|
479
479
|
// 测试 3秒后修改图片地址值
|
|
480
480
|
setTimeout(() => {
|
|
481
|
-
target.refresh(
|
|
481
|
+
target.refresh('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtAAAAIIAQMAAAB99EudAAAABlBMVEUmf8vG2O41LStnAAABD0lEQVR42u3XQQqCQBSAYcWFS4/QUTpaHa2jdISWLUJjjMpclJoPGvq+1WsYfiJCZ4oCAAAAAAAAAAAAAAAAAHin6pL9c6H/fOzHbRrP0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0u/SY9LS0tLS0tLS0tLS0n+edm+UlpaWlpaWlpaWlpaW/tl0Ndyzbno7/+tPTJdd1wal69dNa6abx+Lq6TSeYtK7BX/Diek0XULSZZrakPRtV0i6Hu/KIt30q4fM0pvBqvR9mvsQkZaW9gyJT+f5lsnzjR54xAk8mAUeJyMPwYFH98ALx5Jr0kRLLndT7b64UX9QR/0eAAAAAAAAAAAAAAAAAAD/4gpryzr/bja4QgAAAABJRU5ErkJggg==', {
|
|
482
482
|
real: true,
|
|
483
483
|
});
|
|
484
484
|
}, 3000);
|
|
485
485
|
},
|
|
486
486
|
fontList: [
|
|
487
|
-
{
|
|
488
|
-
{
|
|
489
|
-
{
|
|
490
|
-
{
|
|
491
|
-
{
|
|
492
|
-
{
|
|
493
|
-
{ title: "cursive", value: "cursive" },
|
|
487
|
+
{title: '微软雅黑', value: 'Microsoft YaHei'},
|
|
488
|
+
{title: '黑体', value: 'STHeitiSC-Light'},
|
|
489
|
+
{title: '思源黑体', value: 'SourceHanSansCN-Normal'},
|
|
490
|
+
{title: '宋体', value: 'SimSun'},
|
|
491
|
+
{title: 'cursive', value: 'cursive'},
|
|
492
|
+
{title: 'Times New Roman', value: 'Times New Roman'},
|
|
494
493
|
],
|
|
495
494
|
dataMode: 1,
|
|
496
495
|
history: true,
|
|
@@ -501,11 +500,11 @@ function init() {
|
|
|
501
500
|
onUpdateError: (e) => {
|
|
502
501
|
// console.error(e);
|
|
503
502
|
},
|
|
504
|
-
settingContainer:
|
|
505
|
-
paginationContainer:
|
|
503
|
+
settingContainer: '#PrintElementOptionSetting',
|
|
504
|
+
paginationContainer: '.hiprint-printPagination',
|
|
506
505
|
});
|
|
507
506
|
|
|
508
|
-
hiprintTemplate.value.design(
|
|
507
|
+
hiprintTemplate.value.design('#hiprint-printTemplate', {grid: true});
|
|
509
508
|
scaleValue.value = hiprintTemplate.value.editingPanel.scale || 1;
|
|
510
509
|
window.ht = hiprintTemplate.value;
|
|
511
510
|
|
|
@@ -519,7 +518,7 @@ function init() {
|
|
|
519
518
|
})
|
|
520
519
|
);
|
|
521
520
|
}
|
|
522
|
-
hiprintJQuery(
|
|
521
|
+
hiprintJQuery('.hiprint-designer').on('mousedown', '.hiprint-printElement', (e) => {
|
|
523
522
|
let t = e.currentTarget;
|
|
524
523
|
let pes = hiprintTemplate.value.editingPanel.printElements;
|
|
525
524
|
let index = pes.map((item) => item.designTarget[0]).indexOf(t);
|
|
@@ -533,8 +532,8 @@ function init() {
|
|
|
533
532
|
console.log(error.message);
|
|
534
533
|
clearPrint();
|
|
535
534
|
hiprintTemplate.value = null;
|
|
536
|
-
if (error.message.indexOf(
|
|
537
|
-
ElMessageBox.alert(
|
|
535
|
+
if (error.message.indexOf('setTemplateId') > -1) {
|
|
536
|
+
ElMessageBox.alert('模板有错误,请返回列表手动修改或这清空模板!');
|
|
538
537
|
}
|
|
539
538
|
}
|
|
540
539
|
}
|
|
@@ -550,7 +549,7 @@ async function updateTemplate() {
|
|
|
550
549
|
if (detailData.value && detailData.value.json) {
|
|
551
550
|
let jsonObj = JSON.parse(detailData.value.json);
|
|
552
551
|
hiprintTemplate.value.update(jsonObj);
|
|
553
|
-
let {
|
|
552
|
+
let {width, height} = jsonObj.panels[0];
|
|
554
553
|
paperWidth.value = width;
|
|
555
554
|
paperHeight.value = height;
|
|
556
555
|
hiprintTemplate.value.setPaper(width, height);
|
|
@@ -567,7 +566,7 @@ async function updateTemplate() {
|
|
|
567
566
|
paperNumberLeft: 430,
|
|
568
567
|
paperNumberTop: 5,
|
|
569
568
|
printElements: [],
|
|
570
|
-
watermarkOptions: {
|
|
569
|
+
watermarkOptions: {content: '', rotate: 25, timestamp: false, format: 'YYYY-MM-DD HH:mm'},
|
|
571
570
|
},
|
|
572
571
|
],
|
|
573
572
|
});
|
package/libs/hiprint.bundle.js
CHANGED
|
@@ -2649,7 +2649,17 @@ var hiprint = (function (t) {
|
|
|
2649
2649
|
(n += " </select>\n </div>\n </div>"),
|
|
2650
2650
|
(this.target = hiprintJQuery(n));
|
|
2651
2651
|
} else {
|
|
2652
|
-
this.target = hiprintJQuery(
|
|
2652
|
+
this.target = hiprintJQuery(`<div class="hiprint-option-item">
|
|
2653
|
+
<div class="hiprint-option-item-label">字体</div>
|
|
2654
|
+
<div class="hiprint-option-item-field">
|
|
2655
|
+
<select class="auto-submit">
|
|
2656
|
+
<option value="" >默认</option>
|
|
2657
|
+
<option value="SimSun" >宋体</option>
|
|
2658
|
+
<option value="Microsoft YaHei" >微软雅黑</option>
|
|
2659
|
+
<option value="Times New Roman" >Times New Roman</option>
|
|
2660
|
+
</select>
|
|
2661
|
+
</div>
|
|
2662
|
+
</div>`);
|
|
2653
2663
|
}
|
|
2654
2664
|
return this.target;
|
|
2655
2665
|
}),
|
|
@@ -1,225 +1,225 @@
|
|
|
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;
|
|
158
|
-
var Encoder = barcodes[options.format.toUpperCase()];
|
|
159
|
-
var encoded = encode(text, Encoder, options);
|
|
160
|
-
|
|
161
|
-
render(renderProperty, encoded, options);
|
|
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 hiprintJQuery !== 'undefined') {
|
|
215
|
-
hiprintJQuery.fn.JsBarcode = function (content, options) {
|
|
216
|
-
var elementArray = [];
|
|
217
|
-
hiprintJQuery(this).each(function () {
|
|
218
|
-
elementArray.push(this);
|
|
219
|
-
});
|
|
220
|
-
return JsBarcode(elementArray, content, options);
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// Export to commonJS
|
|
225
|
-
export default JsBarcode;
|
|
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;
|
|
158
|
+
var Encoder = barcodes[options.format.toUpperCase()];
|
|
159
|
+
var encoded = encode(text, Encoder, options);
|
|
160
|
+
|
|
161
|
+
render(renderProperty, encoded, options);
|
|
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 hiprintJQuery !== 'undefined') {
|
|
215
|
+
hiprintJQuery.fn.JsBarcode = function (content, options) {
|
|
216
|
+
var elementArray = [];
|
|
217
|
+
hiprintJQuery(this).each(function () {
|
|
218
|
+
elementArray.push(this);
|
|
219
|
+
});
|
|
220
|
+
return JsBarcode(elementArray, content, options);
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Export to commonJS
|
|
225
|
+
export default JsBarcode;
|