From 000fe8222bcbc6728a0f206b0546cfe3ad80bae8 Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Fri, 19 May 2017 11:54:33 +0200 Subject: [PATCH] Added check on mission var 'options', refactoring. (#3055) --- .../modules/components/shared/Label.js | 84 ++++++++++++------- 1 file changed, 55 insertions(+), 29 deletions(-) diff --git a/lib/network/modules/components/shared/Label.js b/lib/network/modules/components/shared/Label.js index becbf0d3..ea787b89 100644 --- a/lib/network/modules/components/shared/Label.js +++ b/lib/network/modules/components/shared/Label.js @@ -37,11 +37,7 @@ class Label { } static parseOptions(parentOptions, newOptions, allowDeletion = false) { - if (typeof newOptions.font === 'string') { - let newOptionsArray = newOptions.font.split(" "); - parentOptions.size = newOptionsArray[0].replace("px",''); - parentOptions.face = newOptionsArray[1]; - parentOptions.color = newOptionsArray[2]; + if (Label.parseFontString(parentOptions, newOptions.font)) { parentOptions.vadjust = 0; } else if (typeof newOptions.font === 'object') { @@ -51,6 +47,32 @@ class Label { parentOptions.vadjust = Number(parentOptions.vadjust); } + + /** + * If in-variable is a string, parse it as a font specifier. + * + * Note that following is not done here and have to be done after the call: + * - No number conversion (size) + * - Not all font options are set (vadjust, mod) + * + * @param inOptions {Object} font options to parse + * @param outOptions {Object} out-parameter, object in which to store the parse results (if any) + * + * @return true if font parsed as string, false otherwise + */ + static parseFontString(outOptions, inOptions) { + if (!inOptions || typeof inOptions !== 'string') return false; + + let newOptionsArray = inOptions.split(" "); + + outOptions.size = newOptionsArray[0].replace("px",''); + outOptions.face = newOptionsArray[1]; + outOptions.color = newOptionsArray[2]; + + return true; + } + + // set the width and height constraints based on 'nearest' value constrain(elementOptions, options, defaultOptions) { this.fontOptions.constrainWidth = false; @@ -141,13 +163,17 @@ class Label { if (!this.fontOptions.multi) return; /** - * Get property value from options.font[mod][property] if present. - * If mod not passed, use property value from options.font[property]. + * Resolve the font options path. + * If valid, return a reference to the object in question. + * Otherwise, just return null. * - * @return value if found, null otherwise. + * param 'mod' is optional. + * + * options {Object} base object to determine path from + * mod {string|undefined} if present, sub path for the mod-font */ - var getP = function(options, mod, property) { - if (!options.font) return null; + var pathP = function(options, mod) { + if (!options || !options.font) return null; var opt = options.font; @@ -156,7 +182,20 @@ class Label { opt = opt[mod]; } - if (opt.hasOwnProperty(property)) { + return opt; + }; + + + /** + * Get property value from options.font[mod][property] if present. + * If mod not passed, use property value from options.font[property]. + * + * @return value if found, null otherwise. + */ + var getP = function(options, mod, property) { + let opt = pathP(options, mod); + + if (opt && opt.hasOwnProperty(property)) { return opt[property]; } @@ -169,16 +208,7 @@ class Label { let modOptions = this.fontOptions[mod]; let modDefaults = defaultOptions.font[mod]; - let optionsFontMod; - if (options.font) { - optionsFontMod = options.font[mod]; - } - - if (typeof optionsFontMod === 'string') { - let modOptionsArray = optionsFontMod.split(" "); - modOptions.size = modOptionsArray[0].replace("px",""); - modOptions.face = modOptionsArray[1]; - modOptions.color = modOptionsArray[2]; + if (Label.parseFontString(modOptions, pathP(options, mod))) { modOptions.vadjust = this.fontOptions.vadjust; modOptions.mod = modDefaults.mod; } else { @@ -187,10 +217,8 @@ class Label { // much 'natural' versatility as we can get, so a simple global // change propagates in an expected way, even if not stictly logical. - // face: We want to capture any direct settings and overrides, but - // fall back to the base font if there aren't any. We make a - // special exception for mono, since we probably don't want to - // sync to a the base font face. + // 'face' has a special exception for mono, since we probably + // don't want to sync to the base font face. modOptions.face = getP(options , mod, 'face') || getP(groupOptions, mod, 'face') || @@ -199,7 +227,7 @@ class Label { this.fontOptions.face ; - // color: this is handled just like the face, except for specific default for 'mono' + // 'color' follows the standard flow modOptions.color = getP(options , mod, 'color') || getP(groupOptions, mod, 'color') || @@ -207,9 +235,7 @@ class Label { this.fontOptions.color ; - // mod: this is handled just like the face, except we never grab the - // base font's mod. We know they're in the defaultOptions, and unless - // we've been steered away from them, we use the default. + // 'mode' follows the standard flow modOptions.mod = getP(options , mod, 'mod') || getP(groupOptions, mod, 'mod') ||