Removing ObjectsCommon API

This commit is contained in:
Adsooi 2024-09-20 17:56:59 +02:00
parent 325eef57e2
commit 7f57ed13c7
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
11 changed files with 76 additions and 85 deletions

View file

@ -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.

View file

@ -16,7 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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"
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)
/**
* 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) {
registerObject(Point)
registerObject(Text)
registerObject(Function)
registerObject(GainBode)
registerObject(PhaseBode)
registerObject(SommeGainsBode)
registerObject(SommePhasesBode)
registerObject(XCursor)
registerObject(Sequence)
registerObject(RepartitionFunction)
}

View file

@ -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.

View file

@ -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

View file

@ -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'

View file

@ -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'

View file

@ -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

View file

@ -16,10 +16,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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

View file

@ -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

View file

@ -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

View file

@ -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