JsBarcode icon indicating copy to clipboard operation
JsBarcode copied to clipboard

[Waiting for JsBarcode 4] New options from element

Open lindell opened this issue 7 years ago • 9 comments

Will allow barcode specific options to be specified through html parameters.

This does also force the use of some-option when you want to specify someOption which will break backwards compatibility.

Scheduled for JsBarcode 4 release.

lindell avatar Sep 28 '16 07:09 lindell

If I understand right, you forgot about conversion of flat option into boolean, in optionsFromStrings. It might looks like this:

const INT_OPTIONS = [
	"width",
	"height",
	"textMargin",
	"fontSize",
	"margin",
	"marginTop",
	"marginBottom",
	"marginLeft",
	"marginRight"
];

const BOOL_OPTIONS = [
	"displayValue",
	"flat"
];

// Convert string to integers/booleans where it should be
function optionsFromStrings(options) {
	Object.keys(options).forEach(key => {
		if (typeof options[key] === "string") { // not sure about this
			if (INT_OPTIONS.indexOf(key) !== -1) {
				options[key] = parseInt(options[key], 10);
			} else if (BOOL_OPTIONS.indexOf(key) !== -1) {
				options[key] = options[key] === "true";
			}
		}
	});
	return options;
}

export default optionsFromStrings;

SanichKotikov avatar May 17 '17 21:05 SanichKotikov

@SanichKotikov Indeed flat should be converted. But doing it that way would violate some important design principles. Primarly the dependency inversion principle. This means that every time an option is added to a barcode symbology the main library has to be updated which should be avoided.

But as you noted this is still an option that should be addressed. Maybe by allowing the symbologies to specify its options and the datatype to allow the main part of the library to convert it.

lindell avatar May 18 '17 19:05 lindell

I see. Good point about DI principle. I have a crazy idea https://jsfiddle.net/o6cbz9na/1/. If you need to add a new option, you will do it only in OPTIONS constant. :)

SanichKotikov avatar May 18 '17 20:05 SanichKotikov

That's all good! But the problem still remains that new (barcode specific) options does need to be edited in to work.

This implementation is much nicer for the library options and should probably be implemented for those 😄

lindell avatar May 19 '17 03:05 lindell

@lindell just a thought: why don't we apply flat automatically if displayValue = false?

SanichKotikov avatar Jun 16 '17 17:06 SanichKotikov

@lindell any news on this? It fixes a lot of stuff for options, desperately need it!! 😄

garygreen avatar Jul 04 '17 21:07 garygreen

@garygreen I think I will realease the this when #171 i done which is hopefully pretty soon.

But what do you want to do exactly? There is probably a pretty easy workaraound that is just not as pretty.

lindell avatar Jul 05 '17 04:07 lindell

Probably is an easy workaround, I guess I'll have to initialise the barcodes manually - still would like to give options in a <svg> element

garygreen avatar Jul 05 '17 09:07 garygreen

@garygreen You can already, with all options except the barcode specific ones. So basically the flat option for some of the EAN barcodes.

lindell avatar Jul 05 '17 16:07 lindell