1
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
export default fixOptions;
|
||||
|
||||
function fixOptions(options){
|
||||
// Fix the margins
|
||||
options.marginTop = options.marginTop || options.margin;
|
||||
options.marginBottom = options.marginBottom || options.margin;
|
||||
options.marginRight = options.marginRight || options.margin;
|
||||
options.marginLeft = options.marginLeft || options.margin;
|
||||
|
||||
return options;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import optionsFromStrings from "./optionsFromStrings.js";
|
||||
import defaults from "../options/defaults.js";
|
||||
|
||||
function getOptionsFromElement(element){
|
||||
var options = {};
|
||||
for(var property in defaults){
|
||||
if(defaults.hasOwnProperty(property)){
|
||||
// jsbarcode-*
|
||||
if(element.hasAttribute("jsbarcode-" + property.toLowerCase())){
|
||||
options[property] = element.getAttribute("jsbarcode-" + property.toLowerCase());
|
||||
}
|
||||
|
||||
// data-*
|
||||
if(element.hasAttribute("data-" + property.toLowerCase())){
|
||||
options[property] = element.getAttribute("data-" + property.toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
options["value"] = element.getAttribute("jsbarcode-value") || element.getAttribute("data-value");
|
||||
|
||||
// Since all atributes are string they need to be converted to integers
|
||||
options = optionsFromStrings(options);
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
export default getOptionsFromElement;
|
||||
@@ -0,0 +1,103 @@
|
||||
/* global HTMLImageElement */
|
||||
/* global HTMLCanvasElement */
|
||||
/* global SVGElement */
|
||||
|
||||
import getOptionsFromElement from "./getOptionsFromElement.js";
|
||||
import renderers from "../renderers";
|
||||
|
||||
import {InvalidElementException} from "../exceptions/exceptions.js";
|
||||
|
||||
// Takes an element and returns an object with information about how
|
||||
// it should be rendered
|
||||
// This could also return an array with these objects
|
||||
// {
|
||||
// element: The element that the renderer should draw on
|
||||
// renderer: The name of the renderer
|
||||
// afterRender (optional): If something has to done after the renderer
|
||||
// completed, calls afterRender (function)
|
||||
// options (optional): Options that can be defined in the element
|
||||
// }
|
||||
|
||||
function getRenderProperties(element){
|
||||
// If the element is a string, query select call again
|
||||
if(typeof element === "string"){
|
||||
return querySelectedRenderProperties(element);
|
||||
}
|
||||
// If element is array. Recursivly call with every object in the array
|
||||
else if(Array.isArray(element)){
|
||||
var returnArray = [];
|
||||
for(let i = 0; i < element.length; i++){
|
||||
returnArray.push(getRenderProperties(element[i]));
|
||||
}
|
||||
return returnArray;
|
||||
}
|
||||
// If element, render on canvas and set the uri as src
|
||||
else if(typeof HTMLCanvasElement !== 'undefined' && element instanceof HTMLImageElement){
|
||||
return newCanvasRenderProperties(element);
|
||||
}
|
||||
// If SVG
|
||||
else if(
|
||||
(element && element.nodeName && element.nodeName.toLowerCase() === 'svg') ||
|
||||
(typeof SVGElement !== 'undefined' && element instanceof SVGElement)
|
||||
){
|
||||
return {
|
||||
element: element,
|
||||
options: getOptionsFromElement(element),
|
||||
renderer: renderers.SVGRenderer
|
||||
};
|
||||
}
|
||||
// If canvas (in browser)
|
||||
else if(typeof HTMLCanvasElement !== 'undefined' && element instanceof HTMLCanvasElement){
|
||||
return {
|
||||
element: element,
|
||||
options: getOptionsFromElement(element),
|
||||
renderer: renderers.CanvasRenderer
|
||||
};
|
||||
}
|
||||
|
||||
// If canvas (in node)
|
||||
else if(element && element.getContext){
|
||||
return {
|
||||
element: element,
|
||||
renderer: renderers.CanvasRenderer
|
||||
};
|
||||
}
|
||||
else if(element && typeof element === 'object' && !element.nodeName) {
|
||||
return {
|
||||
element: element,
|
||||
renderer: renderers.ObjectRenderer
|
||||
};
|
||||
}
|
||||
else{
|
||||
throw new InvalidElementException();
|
||||
}
|
||||
}
|
||||
|
||||
function querySelectedRenderProperties(string){
|
||||
var selector = document.querySelectorAll(string);
|
||||
if(selector.length === 0){
|
||||
return undefined;
|
||||
}
|
||||
else{
|
||||
let returnArray = [];
|
||||
for(let i = 0; i < selector.length; i++){
|
||||
returnArray.push(getRenderProperties(selector[i]));
|
||||
}
|
||||
return returnArray;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function newCanvasRenderProperties(imgElement){
|
||||
var canvas = document.createElement('canvas');
|
||||
return {
|
||||
element: canvas,
|
||||
options: getOptionsFromElement(imgElement),
|
||||
renderer: renderers.CanvasRenderer,
|
||||
afterRender: function(){
|
||||
imgElement.setAttribute("src", canvas.toDataURL());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default getRenderProperties;
|
||||
@@ -0,0 +1,22 @@
|
||||
export default linearizeEncodings;
|
||||
|
||||
// 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]
|
||||
function linearizeEncodings(encodings){
|
||||
var linearEncodings = [];
|
||||
function nextLevel(encoded){
|
||||
if(Array.isArray(encoded)){
|
||||
for(let i = 0; i < encoded.length; i++){
|
||||
nextLevel(encoded[i]);
|
||||
}
|
||||
}
|
||||
else{
|
||||
encoded.text = encoded.text || "";
|
||||
encoded.data = encoded.data || "";
|
||||
linearEncodings.push(encoded);
|
||||
}
|
||||
}
|
||||
nextLevel(encodings);
|
||||
|
||||
return linearEncodings;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export default (old, replaceObj) => ({ ...old, ...replaceObj });
|
||||
@@ -0,0 +1,31 @@
|
||||
export default optionsFromStrings;
|
||||
|
||||
// Convert string to integers/booleans where it should be
|
||||
function optionsFromStrings(options){
|
||||
var intOptions = [
|
||||
"width",
|
||||
"height",
|
||||
"textMargin",
|
||||
"fontSize",
|
||||
"margin",
|
||||
"marginTop",
|
||||
"marginBottom",
|
||||
"marginLeft",
|
||||
"marginRight"
|
||||
];
|
||||
|
||||
for(var intOption in intOptions){
|
||||
if(intOptions.hasOwnProperty(intOption)){
|
||||
intOption = intOptions[intOption];
|
||||
if(typeof options[intOption] === "string"){
|
||||
options[intOption] = parseInt(options[intOption], 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof options["displayValue"] === "string"){
|
||||
options["displayValue"] = (options["displayValue"] != "false");
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
Reference in New Issue
Block a user