From 7f57ed13c767a3cd7dae542fac6560caafaad022 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Fri, 20 Sep 2024 17:56:59 +0200 Subject: [PATCH] Removing ObjectsCommon API --- .../eu/ad5001/LogarithmPlotter/js/objects.mjs | 22 ++++++++ .../LogarithmPlotter/js/objs/autoload.mjs | 39 +++++++++----- .../LogarithmPlotter/js/objs/common.mjs | 53 ------------------- .../LogarithmPlotter/js/objs/function.mjs | 5 +- .../LogarithmPlotter/js/objs/gainbode.mjs | 6 +-- .../LogarithmPlotter/js/objs/phasebode.mjs | 10 ++-- .../ad5001/LogarithmPlotter/js/objs/point.mjs | 6 ++- .../LogarithmPlotter/js/objs/repartition.mjs | 6 ++- .../LogarithmPlotter/js/objs/sequence.mjs | 5 +- .../ad5001/LogarithmPlotter/js/objs/text.mjs | 5 +- .../LogarithmPlotter/js/objs/xcursor.mjs | 4 +- 11 files changed, 76 insertions(+), 85 deletions(-) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objects.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objects.mjs index c45b65e..039ba49 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objects.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objects.mjs @@ -36,6 +36,28 @@ class ObjectsAPI extends Module { this.currentObjectsByName = {} } + /** + * Creates a new name for an object, based on the allowedLetters. + * If variables with each of the allowedLetters is created, a subscript + * number is added to the name. + * @param {string} allowedLetters + * @param {string} prefix - Prefix to the name. + * @return {string} New unused name for a new object. + */ + getNewName(allowedLetters, prefix='') { + // Allows to get a new name, based on the allowed letters, + // as well as adding a sub number when needs be. + let newid = 0 + let ret + do { + let letter = allowedLetters[newid % allowedLetters.length] + let num = Math.floor((newid - (newid % allowedLetters.length)) / allowedLetters.length) + ret = prefix + letter + (num > 0 ? textsub(num-1) : '') + newid += 1 + } while(ret in this.currentObjectsByName) + return ret + } + /** * Renames an object from its old name to the new one. * @param {string} oldName - Current name of the object. diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/autoload.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/autoload.mjs index f29710b..dc84bb0 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/autoload.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/autoload.mjs @@ -16,7 +16,8 @@ * along with this program. If not, see . */ -import { API as ObjectsCommonAPI } from "common.mjs" +import Objects from "../objects.mjs" +import { DrawableObject } from "common.mjs" import Point from "point.mjs" import Text from "text.mjs" import Function from "function.mjs" @@ -28,15 +29,29 @@ import XCursor from "xcursor.mjs" import Sequence from "sequence.mjs" import RepartitionFunction from "repartition.mjs" +/** + * Registers the object obj in the object list. + * @param {DrawableObject} obj - Object to be registered. + */ +function registerObject(obj) { + // Registers an object to be used in LogarithmPlotter. + if(obj.prototype instanceof DrawableObject) { + if(!Objects.types[obj.type()]) + Objects.types[obj.type()] = obj + } else { + console.error("Could not register object " + (obj?.type() ?? obj.constructor.name) + ", as it isn't a DrawableObject.") + } +} + if(Object.keys(Modules.Objects.types).length === 0) { - ObjectsCommonAPI.registerObject(Point) - ObjectsCommonAPI.registerObject(Text) - ObjectsCommonAPI.registerObject(Function) - ObjectsCommonAPI.registerObject(GainBode) - ObjectsCommonAPI.registerObject(PhaseBode) - ObjectsCommonAPI.registerObject(SommeGainsBode) - ObjectsCommonAPI.registerObject(SommePhasesBode) - ObjectsCommonAPI.registerObject(XCursor) - ObjectsCommonAPI.registerObject(Sequence) - ObjectsCommonAPI.registerObject(RepartitionFunction) -} \ No newline at end of file + registerObject(Point) + registerObject(Text) + registerObject(Function) + registerObject(GainBode) + registerObject(PhaseBode) + registerObject(SommeGainsBode) + registerObject(SommePhasesBode) + registerObject(XCursor) + registerObject(Sequence) + registerObject(RepartitionFunction) +} diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.mjs index 173aef4..2c6270a 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.mjs @@ -24,59 +24,6 @@ import {ensureTypeSafety, serializesByPropertyType} from "../parameters.mjs" // This file contains the default data to be imported from all other objects -class ObjectsCommonAPI extends Module { - - constructor() { - super('ObjectsCommon', [ - Modules.Objects, - Modules.ExprParser, - Modules.Latex - ]) - } - - /** - * Creates a new name for an object, based on the allowedLetters. - * If variables with each of the allowedLetters is created, a subscript - * number is added to the name. - * @param {string} allowedLetters - * @param {string} prefix - Prefix to the name. - * @return {string} New unused name for a new object. - */ - getNewName(allowedLetters, prefix='') { - // Allows to get a new name, based on the allowed letters, - // as well as adding a sub number when needs be. - let newid = 0 - let ret - do { - let letter = allowedLetters[newid % allowedLetters.length] - let num = Math.floor((newid - (newid % allowedLetters.length)) / allowedLetters.length) - ret = prefix + letter + (num > 0 ? textsub(num-1) : '') - newid += 1 - } while(ret in Objects.currentObjectsByName) - return ret - } - - /** - * Registers the object obj in the object list. - * @param {DrawableObject} obj - Object to be registered. - */ - registerObject(obj) { - // Registers an object to be used in LogarithmPlotter. - // This function is called from autoload.mjs - if(obj.prototype instanceof DrawableObject) { - if(!Objects.types[obj.type()]) - Objects.types[obj.type()] = obj - } else { - console.error("Could not register object " + (obj.type()) + ", as it isn't a DrawableObject.") - } - } -} - -/** @type {ObjectsCommonAPI} */ -Modules.ObjectsCommon = Modules.ObjectsCommon || new ObjectsCommonAPI() - -export const API = Modules.ObjectsCommon - /** * Class to extend for every type of object that * can be drawn on the canvas. diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/function.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/function.mjs index fb9e93c..2448d50 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/function.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/function.mjs @@ -17,7 +17,8 @@ */ import { textsub } from "../utils.mjs" -import { API as Common, ExecutableObject } from "common.mjs" +import Objects from "../objects.mjs" +import { ExecutableObject } from "common.mjs" import { parseDomain, Expression, SpecialDomain } from "../mathlib.mjs" import * as P from "../parameters.mjs" import Latex from "../math/latex.mjs" @@ -50,7 +51,7 @@ export default class Function extends ExecutableObject { expression = 'x', definitionDomain = 'RPE', destinationDomain = 'R', displayMode = 'application', labelPosition = 'above', labelX = 1, drawPoints = true, drawDashedLines = true) { - if(name == null) name = Common.getNewName('fghjqlmnopqrstuvwabcde') + if(name == null) name = Objects.getNewName('fghjqlmnopqrstuvwabcde') super(name, visible, color, labelContent) if(typeof expression == 'number' || typeof expression == 'string') expression = new Expression(expression.toString()) this.expression = expression diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.mjs index 938aa33..f86d747 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.mjs @@ -21,7 +21,7 @@ import * as P from "../parameters.mjs" import Objects from "../objects.mjs" import Latex from "../math/latex.mjs" -import { API as Common, ExecutableObject } from "common.mjs" +import { ExecutableObject } from "common.mjs" import Function from "function.mjs" import { API as HistoryAPI } from "../history/common.mjs" @@ -43,7 +43,7 @@ export default class GainBode extends ExecutableObject { constructor(name = null, visible = true, color = null, labelContent = 'name + value', om_0 = '', pass = 'high', gain = '20', labelPosition = 'above', labelX = 1, omGraduation = false) { - if(name == null) name = Common.getNewName('G') + if(name == null) name = Objects.getNewName('G') if(name === 'G') name = 'G₀' // G is reserved for sum of BODE magnitudes (Somme gains Bode). super(name, visible, color, labelContent) if(typeof om_0 == "string") { @@ -51,7 +51,7 @@ export default class GainBode extends ExecutableObject { om_0 = Objects.currentObjectsByName[om_0] if(om_0 == null) { // Create new point - om_0 = Objects.createNewRegisteredObject('Point', [Common.getNewName('ω'), true, this.color, 'name']) + om_0 = Objects.createNewRegisteredObject('Point', [Objects.getNewName('ω'), true, this.color, 'name']) HistoryAPI.addToHistory(new CreateNewObject(om_0.name, 'Point', om_0.export())) om_0.update() labelPosition = 'below' diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.mjs index 97fdae2..dcdd615 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.mjs @@ -19,11 +19,11 @@ import { executeExpression, Expression } from "../mathlib.mjs" import * as P from "../parameters.mjs" import Objects from "../objects.mjs" -import Latex from "../math/latex.mjs" - -import { API as Common, ExecutableObject } from "common.mjs" import { API as HistoryAPI } from "../history/common.mjs" import { CreateNewObject } from "../historylib.mjs" +import Latex from "../math/latex.mjs" + +import { ExecutableObject } from "common.mjs" export default class PhaseBode extends ExecutableObject { @@ -40,7 +40,7 @@ export default class PhaseBode extends ExecutableObject { constructor(name = null, visible = true, color = null, labelContent = 'name + value', om_0 = '', phase = 90, unit = '°', labelPosition = 'above', labelX = 1) { - if(name == null) name = Common.getNewName('φ') + if(name == null) name = Objects.getNewName('φ') if(name === 'φ') name = 'φ₀' // φ is reserved for sum of BODE phases (Somme phases Bode). super(name, visible, color, labelContent) if(typeof phase == 'number' || typeof phase == 'string') phase = new Expression(phase.toString()) @@ -50,7 +50,7 @@ export default class PhaseBode extends ExecutableObject { om_0 = Objects.currentObjectsByName[om_0] if(om_0 == null) { // Create new point - om_0 = Objects.createNewRegisteredObject('Point', [Common.getNewName('ω'), this.color, 'name']) + om_0 = Objects.createNewRegisteredObject('Point', [Objects.getNewName('ω'), this.color, 'name']) om_0.labelPosition = this.phase.execute() >= 0 ? 'above' : 'below' HistoryAPI.history.addToHistory(new CreateNewObject(om_0.name, 'Point', om_0.export())) labelPosition = 'below' diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/point.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/point.mjs index f53eb72..66a493c 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/point.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/point.mjs @@ -18,8 +18,10 @@ import { Expression } from "../mathlib.mjs" import * as P from "../parameters.mjs" +import Objects from "../objects.mjs" import Latex from "../math/latex.mjs" -import { API as Common, DrawableObject } from "common.mjs" + +import { DrawableObject } from "common.mjs" export default class Point extends DrawableObject { @@ -36,7 +38,7 @@ export default class Point extends DrawableObject { constructor(name = null, visible = true, color = null, labelContent = 'name + value', x = 1, y = 0, labelPosition = 'above', pointStyle = '●') { - if(name == null) name = Common.getNewName('ABCDEFJKLMNOPQRSTUVW') + if(name == null) name = Objects.getNewName('ABCDEFJKLMNOPQRSTUVW') super(name, visible, color, labelContent) if(typeof x == 'number' || typeof x == 'string') x = new Expression(x.toString()) this.x = x diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/repartition.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/repartition.mjs index 2dd632d..89db16d 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/repartition.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/repartition.mjs @@ -16,10 +16,12 @@ * along with this program. If not, see . */ -import { API as Common, ExecutableObject } from "common.mjs" import * as P from "../parameters.mjs" +import Objects from "../objects.mjs" import Latex from "../math/latex.mjs" +import { ExecutableObject } from "common.mjs" + export default class RepartitionFunction extends ExecutableObject { static type(){return 'Repartition'} @@ -46,7 +48,7 @@ export default class RepartitionFunction extends ExecutableObject { constructor(name = null, visible = true, color = null, labelContent = 'name + value', probabilities = {'0': '0'}, labelPosition = 'above', labelX = 1) { - if(name == null) name = Common.getNewName('XYZUVW', "F_") + if(name == null) name = Objects.getNewName('XYZUVW', "F_") super(name, visible, color, labelContent) this.probabilities = probabilities this.labelPosition = labelPosition diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sequence.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sequence.mjs index 10cb154..11f1be7 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sequence.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sequence.mjs @@ -19,8 +19,9 @@ import { Sequence as MathSequence, Domain } from "../mathlib.mjs" import * as P from "../parameters.mjs" import Latex from "../math/latex.mjs" +import Objects from "../objects.mjs" -import { API as Common, ExecutableObject } from "common.mjs" +import { ExecutableObject } from "common.mjs" import Function from "function.mjs" @@ -45,7 +46,7 @@ export default class Sequence extends ExecutableObject { constructor(name = null, visible = true, color = null, labelContent = 'name + value', drawPoints = true, drawDashedLines = true, defaultExp = {1: "n"}, baseValues = {0: 0}, labelPosition = 'above', labelX = 1) { - if(name == null) name = Common.getNewName('uvwPSUVWabcde') + if(name == null) name = Objects.getNewName('uvwPSUVWabcde') super(name, visible, color, labelContent) this.drawPoints = drawPoints this.drawDashedLines = drawDashedLines diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/text.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/text.mjs index dfd0d27..fab2818 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/text.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/text.mjs @@ -18,9 +18,10 @@ import { Expression } from "../mathlib.mjs" import * as P from "../parameters.mjs" +import Objects from "../objects.mjs" import Latex from "../math/latex.mjs" -import { API as Common, DrawableObject } from "common.mjs" +import { DrawableObject } from "common.mjs" export default class Text extends DrawableObject { @@ -41,7 +42,7 @@ export default class Text extends DrawableObject { constructor(name = null, visible = true, color = null, labelContent = 'null', x = 1, y = 0, labelPosition = 'center', text = 'New text', disableLatex = false) { - if(name == null) name = Common.getNewName('t') + if(name == null) name = Objects.getNewName('t') super(name, visible, color, labelContent) if(typeof x == 'number' || typeof x == 'string') x = new Expression(x.toString()) this.x = x diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.mjs index 2e3c6e0..4ab8b3d 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.mjs @@ -21,7 +21,7 @@ import * as P from "../parameters.mjs" import Latex from "../math/latex.mjs" import Objects from "../objects.mjs" -import { API as Common, DrawableObject } from "common.mjs" +import { DrawableObject } from "common.mjs" export default class XCursor extends DrawableObject { @@ -45,7 +45,7 @@ export default class XCursor extends DrawableObject { constructor(name = null, visible = true, color = null, labelContent = 'name + value', x = 1, targetElement = null, labelPosition = 'left', approximate = true, rounding = 3, displayStyle = '— — — — — — —', targetValuePosition = 'Next to target') { - if(name == null) name = Common.getNewName('X') + if(name == null) name = Objects.getNewName('X') super(name, visible, color, labelContent) this.approximate = approximate this.rounding = rounding