diff --git a/.vscode/settings.json b/.vscode/settings.json index 8b8a3a138ae..a795c06cde1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -147,7 +147,7 @@ "xmemory0": "cpp" }, "typescript.preferences.quoteStyle": "single", - "typescript.preferences.importModuleSpecifier": "non-relative" + "typescript.preferences.importModuleSpecifier": "project-relative" , "emeraldwalk.runonsave": { "commands": [ diff --git a/fbw-a32nx/mach.config.js b/fbw-a32nx/mach.config.js index d8c95714bc6..f2a026bed07 100644 --- a/fbw-a32nx/mach.config.js +++ b/fbw-a32nx/mach.config.js @@ -30,12 +30,13 @@ module.exports = { typecheckingPlugin(), ], instruments: [ - msfsAvionicsInstrument('PFD'), - msfsAvionicsInstrument('ND'), - msfsAvionicsInstrument('EWD'), msfsAvionicsInstrument('Clock'), - msfsAvionicsInstrument('OANC'), + msfsAvionicsInstrument('EWD'), msfsAvionicsInstrument('FCU'), + msfsAvionicsInstrument('MCDU', 'McduBaseInstrument.ts'), + msfsAvionicsInstrument('ND'), + msfsAvionicsInstrument('OANC'), + msfsAvionicsInstrument('PFD'), reactInstrument('SD'), reactInstrument('DCDU'), diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/SimObjects/AirPlanes/FlyByWire_A320_NEO/panel/panel.cfg b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/SimObjects/AirPlanes/FlyByWire_A320_NEO/panel/panel.cfg index e032e4583c7..ca72db31df6 100644 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/SimObjects/AirPlanes/FlyByWire_A320_NEO/panel/panel.cfg +++ b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/SimObjects/AirPlanes/FlyByWire_A320_NEO/panel/panel.cfg @@ -65,7 +65,7 @@ size_mm = 1024,1024 pixel_size = 1024,1024 texture = $FMC -htmlgauge00 = Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU.html, 0,0,1024,1024 +htmlgauge00 = A32NX/MCDU/mcdu.html, 0,0,1024,1024 [VCockpit09] size_mm = 1280,640 diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/CSS/A32NX_Display_Common.css b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/CSS/A32NX_Display_Common.css deleted file mode 100644 index e2d57ed4438..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/CSS/A32NX_Display_Common.css +++ /dev/null @@ -1,185 +0,0 @@ -@font-face { - font-family: "ECAMFontRegular"; - src: url("/Fonts/fbw-a32nx/ECAMFontRegular.ttf") format("truetype"); - font-weight: normal; - font-style: normal; -} - -:root { - --displayBackground: rgba(13, 20, 35, 1); - --displaySeparator: #3c3c3c; - --displaySeparatorLight: #aaaaaa; - - --displayWhite: #ffffff; - --displayGrey: #787878; - --displayDarkGrey: #b3b3b3; - --displayLightGrey: lightgray; - --displayAmber: #e68000; - --displayCyan: #00ffff; - --displayGreen: #00ff00; - --displayMagenta: #ff94ff; - --displayRed: #ff0000; - --displayYellow: #ffff00; - - --mcduWhite: #ffffff; - --mcduLightGrey: #666666; - --mcduGrey: #787878; - --mcduGreen: #00ff00; - --mcduAmber: #ff9a00; - --mcduCyan: #00ffff; - --mcduYellow: #ffff00; - --mcduRed: #ff0000; - --mcduMagenta: #ff94ff; -} - -div { - font-family: "ECAMFontRegular" !important; -} - -text { - font-family: "ECAMFontRegular" !important; -} - -.Separator { - stroke: var(--displaySeparator); - stroke-width: 4; - fill: none; -} - -/* change from PageTitle */ -.PageTitle { - fill: var(--displayWhite); - font-size: 25px; - text-decoration: underline; -} -#PageTitle { - fill: var(--displayWhite); - font-size: 25px; - text-decoration: underline; -} -.ECAMTitle { - fill: var(--displayWhite); - font-size: 25px; - text-decoration: underline; -} - -.normalText { - fill: var(--displayWhite); -} -.unitText { - fill: var(--displayCyan); -} -.valueText { - fill: var(--displayGreen); -} -.warningText { - fill: var(--displayAmber); -} -.dangerText { - fill: var(--displayRed); -} - -.text-xs { - font-size: 14px; -} -.text-s { - font-size: 16px; -} -.text-m { - font-size: 18px; -} -.text-l { - font-size: 20px; -} -.text-xl { - font-size: 22px; -} -.text-xxl { - font-size: 24px; -} - -.greenShapeFill { - stroke: var(--displayGreen); - fill: var(--displayGreen); -} -.amberShapeFill { - stroke: var(--displayAmber); - fill: var(--displayAmber); -} -.redShapeFill { - stroke: var(--displayRed); - fill: var(--displayRed); -} -.greyShapeFill { - stroke: var(--displayGrey); - fill: var(--displayGrey); -} -.greenShapeOutline { - stroke: var(--displayGreen); - fill: none; -} -.amberShapeOutline { - stroke: var(--displayAmber); - fill: none; -} -.redShapeOutline { - stroke: var(--displayRed); - fill: none; -} -.greyShapeOutline { - stroke: var(--displayGrey); - fill: none; -} - -:root strokeExtraThin { - stroke-width: 1px; -} -:root strokeThin { - stroke-width: 2px; -} -:root strokeRegular { - stroke-width: 3px; -} -:root strokeThick { - stroke-width: 4px; -} -:root strokeExtraThick { - stroke-width: 5px; -} -:root strokeSuperThick { - stroke-width: 6px; -} - -.engineering-mode-overlay { - position: absolute; - left: 0%; - top: 0%; - width: 100%; - height: 100%; - border: none; - z-index: 999; - background-image: url(/css/thalesTest.svg); - background-size: cover; -} - -.engineering-mode-overlay text { - font-size: 21px; - text-anchor: middle; - color: black; -} - -.engineering-mode-overlay text.start { - font-size: 21px; - text-anchor: start; - color: black; -} - -.maintenance-mode-overlay { - position: absolute; - left: 0%; - top: 0%; - width: 100%; - height: 100%; - border: none; - z-index: 990; -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/JS/fbw-a32nx/A32NX_Util.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/JS/fbw-a32nx/A32NX_Util.js deleted file mode 100644 index a7a1712a486..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/JS/fbw-a32nx/A32NX_Util.js +++ /dev/null @@ -1,417 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -const A32NX_Util = {}; - -let nxNotificationsListener; - -A32NX_Util.createDeltaTimeCalculator = (startTime = Date.now()) => { - let lastTime = startTime; - - return () => { - const nowTime = Date.now(); - const deltaTime = nowTime - lastTime; - lastTime = nowTime; - - return deltaTime; - }; -}; - -A32NX_Util.createFrameCounter = (interval = 5) => { - let count = 0; - return () => { - const c = count++; - if (c == interval) { - count = 0; - } - return c; - }; -}; - -A32NX_Util.createMachine = (machineDef) => { - const machine = { - value: machineDef.init, - action(event) { - const currStateDef = machineDef[machine.value]; - const destTransition = currStateDef.transitions[event]; - if (!destTransition) { - return; - } - const destState = destTransition.target; - - machine.value = destState; - }, - setState(newState) { - const valid = machineDef[newState]; - if (valid) { - machine.value = newState; - } - } - }; - return machine; -}; - -/** - * Compute a true heading from a magnetic heading - * @param {Number} heading true heading - * @param {Number=} magVar falls back to current aircraft position magvar - * @returns magnetic heading - */ -A32NX_Util.trueToMagnetic = (heading, magVar) => { - return (720 + heading - (magVar || SimVar.GetSimVarValue("MAGVAR", "degree"))) % 360; -}; - -/** - * Compute a magnetic heading from a true heading - * @param {Number} heading magnetic heading - * @param {Number=} magVar falls back to current aircraft position magvar - * @returns true heading - */ -A32NX_Util.magneticToTrue = (heading, magVar) => { - return (720 + heading + (magVar || SimVar.GetSimVarValue("MAGVAR", "degree"))) % 360; -}; - -/** - * Takes a LatLongAlt or LatLong and returns a vector of spherical co-ordinates - * @param {(LatLong | LatLongAlt)} ll - */ -A32NX_Util.latLonToSpherical = (ll) => { - return [ - Math.cos(ll.lat * Avionics.Utils.DEG2RAD) * Math.cos(ll.long * Avionics.Utils.DEG2RAD), - Math.cos(ll.lat * Avionics.Utils.DEG2RAD) * Math.sin(ll.long * Avionics.Utils.DEG2RAD), - Math.sin(ll.lat * Avionics.Utils.DEG2RAD) - ]; -}; - -/** - * Takes a vector of spherical co-ordinates and returns a LatLong - * @param {[x: number, y: number, z: number]} s - * @returns {LatLong} - */ -A32NX_Util.sphericalToLatLon = (s) => { - return new LatLong(Math.asin(s[2]) * Avionics.Utils.RAD2DEG, Math.atan2(s[1], s[0]) * Avionics.Utils.RAD2DEG); -}; - -/** - * Computes the intersection point of two (true) bearings on a great circle - * @param {(LatLong | LatLongAlt)} latlon1 - * @param {number} brg1 - * @param {(LatLong | LatLongAlt)} latlon2 - * @param {number} brg2 - * @returns {LatLong} - */ -A32NX_Util.greatCircleIntersection = (latlon1, brg1, latlon2, brg2) => { - // c.f. https://blog.mbedded.ninja/mathematics/geometry/spherical-geometry/finding-the-intersection-of-two-arcs-that-lie-on-a-sphere/ - const Pa11 = A32NX_Util.latLonToSpherical(latlon1); - const latlon12 = Avionics.Utils.bearingDistanceToCoordinates(brg1 % 360, 100, latlon1.lat, latlon1.long); - const Pa12 = A32NX_Util.latLonToSpherical(latlon12); - const Pa21 = A32NX_Util.latLonToSpherical(latlon2); - const latlon22 = Avionics.Utils.bearingDistanceToCoordinates(brg2 % 360, 100, latlon2.lat, latlon2.long); - const Pa22 = A32NX_Util.latLonToSpherical(latlon22); - - const N1 = math.cross(Pa11, Pa12); - const N2 = math.cross(Pa21, Pa22); - - const L = math.cross(N1, N2); - const l = math.norm(L); - - const I1 = math.divide(L, l); - const I2 = math.multiply(I1, -1); - - const s1 = A32NX_Util.sphericalToLatLon(I1); - const s2 = A32NX_Util.sphericalToLatLon(I2); - - const brgTos1 = Avionics.Utils.computeGreatCircleHeading(latlon1, s1); - const brgTos2 = Avionics.Utils.computeGreatCircleHeading(latlon1, s2); - - const delta1 = Math.abs(brg1 - brgTos1); - const delta2 = Math.abs(brg1 - brgTos2); - - return delta1 < delta2 ? s1 : s2; -}; - -A32NX_Util.bothGreatCircleIntersections = (latlon1, brg1, latlon2, brg2) => { - // c.f. https://blog.mbedded.ninja/mathematics/geometry/spherical-geometry/finding-the-intersection-of-two-arcs-that-lie-on-a-sphere/ - const Pa11 = A32NX_Util.latLonToSpherical(latlon1); - const latlon12 = Avionics.Utils.bearingDistanceToCoordinates(brg1 % 360, 100, latlon1.lat, latlon1.long); - const Pa12 = A32NX_Util.latLonToSpherical(latlon12); - const Pa21 = A32NX_Util.latLonToSpherical(latlon2); - const latlon22 = Avionics.Utils.bearingDistanceToCoordinates(brg2 % 360, 100, latlon2.lat, latlon2.long); - const Pa22 = A32NX_Util.latLonToSpherical(latlon22); - - const N1 = math.cross(Pa11, Pa12); - const N2 = math.cross(Pa21, Pa22); - - const L = math.cross(N1, N2); - const l = math.norm(L); - - const I1 = math.divide(L, l); - const I2 = math.multiply(I1, -1); - - const s1 = A32NX_Util.sphericalToLatLon(I1); - const s2 = A32NX_Util.sphericalToLatLon(I2); - - return [s1, s2]; -}; - -/** - * Returns the ISA temperature for a given altitude - * @param alt {number} altitude in ft - * @returns {number} ISA temp in C° - */ -A32NX_Util.getIsaTemp = (alt = Simplane.getAltitude()) => { - return Math.min(alt, 36089) * -0.0019812 + 15; -}; - -/** - * Returns the deviation from ISA temperature and OAT at given altitude - * @param alt {number} altitude in ft - * @returns {number} ISA temp deviation from OAT in C° - */ -A32NX_Util.getIsaTempDeviation = (alt = Simplane.getAltitude(), sat = Simplane.getAmbientTemperature()) => { - return sat - A32NX_Util.getIsaTemp(alt); -}; - -/** -* Get the magvar to use for radials from a wp. - * @param {VhfNavaid} facility The waypoint. -*/ -A32NX_Util.getRadialMagVar = (facility) => { - if (facility.subSectionCode === 0 /* VhfNavaid */) { - if (facility.stationDeclination !== undefined) { - return facility.stationDeclination; - } - } - - return Facilities.getMagVar(facility.location.lat, facility.location.long); -}; - -/** - * Utility class to throttle instrument updates - */ -class UpdateThrottler { - - /** - * @param {number} intervalMs Interval between updates, in milliseconds - */ - constructor(intervalMs) { - this.intervalMs = intervalMs; - this.currentTime = 0; - this.lastUpdateTime = 0; - - // Take a random offset to space out updates from different instruments among different - // frames as much as possible. - this.refreshOffset = Math.floor(Math.random() * intervalMs); - this.refreshNumber = 0; - } - - /** - * Checks whether the instrument should be updated in the current frame according to the - * configured update interval. - * - * @param {number} deltaTime - * @param {boolean} [forceUpdate = false] - True if you want to force an update during this frame. - * @returns -1 if the instrument should not update, or the time elapsed since the last - * update in milliseconds - */ - canUpdate(deltaTime, forceUpdate = false) { - this.currentTime += deltaTime; - const number = Math.floor((this.currentTime + this.refreshOffset) / this.intervalMs); - const update = number > this.refreshNumber; - this.refreshNumber = number; - if (update || forceUpdate) { - const accumulatedDelta = this.currentTime - this.lastUpdateTime; - this.lastUpdateTime = this.currentTime; - return accumulatedDelta; - } else { - return -1; - } - } -} - -A32NX_Util.UpdateThrottler = UpdateThrottler; - -/** - * NotificationParams class container for popups to package popup metadata - */ -class NotificationParams { - constructor() { - this.__Type = "SNotificationParams"; - this.buttons = []; - this.style = "normal"; - this.displayGlobalPopup = true; - } -} - -/** - * NXPopUp utility class to create a pop-up UI element - */ -class NXPopUp { - constructor() { - this.params = new NotificationParams(); - this.popupListener; - this.params.title = "A32NX POPUP"; - this.params.time = new Date().getTime(); - this.params.id = this.params.title + "_" + this.params.time; - this.params.contentData = "Default Message"; - this.params.style = "small"; - this.params.buttons.push(new NotificationButton("TT:MENU.YES", "A32NX_POP_" + this.params.id + "_YES")); - this.params.buttons.push(new NotificationButton("TT:MENU.NO", "A32NX_POP_" + this.params.id + "_NO")); - } - - _showPopUp(params) { - try { - Coherent.trigger("SHOW_POP_UP", params); - } catch (e) { - console.error(e); - } - } - - /** - * Show popup with given or already initiated parameters - * @param {string} title Title for popup - will show in menu bar - * @param {string} message Popup message - * @param {string} style Style/Type of popup. Valid types are small|normal|big|big-help - * @param {function} callbackYes Callback function -> YES button is clicked. - * @param {function} callbackNo Callback function -> NO button is clicked. - */ - showPopUp(title, message, style, callbackYes, callbackNo) { - if (title) { - this.params.title = title; - } - if (message) { - this.params.contentData = message; - } - if (style) { - this.params.style = style; - } - if (callbackYes) { - const yes = (typeof callbackYes === "function") ? callbackYes : () => callbackYes; - Coherent.on(`A32NX_POP_${this.params.id}_YES`, () => { - Coherent.off(`A32NX_POP_${this.params.id}_YES`, null, null); - yes(); - }); - } - if (callbackNo) { - const no = (typeof callbackNo === "function") ? callbackNo : () => callbackNo; - Coherent.on(`A32NX_POP_${this.params.id}_NO`, () => { - Coherent.off(`A32NX_POP_${this.params.id}_NO`, null, null); - no(); - }); - } - - if (!this.popupListener) { - this.popupListener = RegisterViewListener("JS_LISTENER_POPUP", this._showPopUp.bind(null, this.params)); - } else { - this._showPopUp(); - } - } -} - -/** - * NXNotif utility class to create a notification event and element - */ - -class NXNotifManager { - - constructor() { - Coherent.on("keyIntercepted", (key) => this.registerIntercepts(key)); - Coherent.call("INTERCEPT_KEY_EVENT", "PAUSE_TOGGLE", 0); - Coherent.call("INTERCEPT_KEY_EVENT", "PAUSE_ON", 0); - Coherent.call("INTERCEPT_KEY_EVENT", "PAUSE_OFF", 0); - Coherent.call("INTERCEPT_KEY_EVENT", "PAUSE_SET", 0); - this.notifications = []; - } - - registerIntercepts(key) { - switch (key) { - case "PAUSE_TOGGLE": - case "PAUSE_ON": - case "PAUSE_OFF": - case "PAUSE_SET": - this.notifications.forEach((notif) => { - notif.hideNotification(); - }); - this.notifications.length = 0; - break; - default: - break; - } - } - - showNotification(params = {}) { - const notif = new NXNotif(); - notif.showNotification(params); - this.notifications.push(notif); - } -} - -class NXNotif { - constructor() { - const title = "A32NX ALERT"; - this.time = new Date().getTime(); - this.params = { - id: `${title}_${this.time}`, - title, - type: "MESSAGE", - theme: "GAMEPLAY", - image: "IMAGE_NOTIFICATION", - description: "Default Message", - timeout: 10000, - time: this.time, - }; - } - - setData(params = {}) { - if (params.title) { - this.params.title = params.title; - this.params.id = `${params.title}_${new Date().getTime()}`; - } - if (params.type) { - this.params.type = params.type; - } - if (params.theme) { - this.params.theme = params.theme; - } - if (params.image) { - this.params.image = params.image; - } - if (params.message) { - this.params.description = params.message; - } - if (params.timeout) { - this.params.timeout = params.timeout; - } - } - - /** - * Show notification with given or already initiated parametrs. - * @param {string} params.title Title for notification - will show as the message header - * @param {string} params.type Type of Notification - Valid types are MESSAGE|SUBTITLES - * @param {string} params.theme Theme of Notification. Valid types are TIPS|GAMEPLAY|SYSTEM - * @param {string} params.image Notification image. Valid types are IMAGE_NOTIFICATION|IMAGE_SCORE - * @param {string} params.message Notification message - * @param {number} params.timeout Time in ms before notification message will disappear - */ - showNotification(params = {}) { - this.setData(params); - - if (!nxNotificationsListener) { - nxNotificationsListener = RegisterViewListener("JS_LISTENER_NOTIFICATIONS"); - } - nxNotificationsListener.triggerToAllSubscribers("SendNewNotification", this.params); - setTimeout(() => { - this.hideNotification(); - }, this.params.timeout); - } - - // TODO FIXME: May break in the future, check every update - hideNotification() { - nxNotificationsListener.triggerToAllSubscribers("HideNotification", this.params.type, null, this.params.id); - } -} - -A32NX_Util.meterToFeet = (meterValue) => { - return meterValue / 0.3048; -}; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_ATSU.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_ATSU.js deleted file mode 100644 index 1c1f7ede411..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_ATSU.js +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -function translateAtsuMessageType(type) { - switch (type) { - case AtsuCommon.AtsuMessageType.Freetext: - return "FREETEXT"; - case AtsuCommon.AtsuMessageType.METAR: - return "METAR"; - case AtsuCommon.AtsuMessageType.TAF: - return "TAF"; - case AtsuCommon.AtsuMessageType.ATIS: - return "ATIS"; - default: - return "UNKNOWN"; - } -} - -function fetchTimeValue() { - let timeValue = SimVar.GetGlobalVarValue("ZULU TIME", "seconds"); - if (timeValue) { - const seconds = Number.parseInt(timeValue); - const displayTime = Utils.SecondsToDisplayTime(seconds, true, true, false); - timeValue = displayTime.toString(); - return timeValue.substring(0, 5); - } - return null; -} - -/** - * Converts lbs to kg - * @param {string | number} value - */ -const lbsToKg = (value) => { - return (+value * 0.4535934).toString(); -}; - -/** - * Fetch SimBrief OFP data and store on FMCMainDisplay object - * @param {FMCMainDisplay} mcdu FMCMainDisplay - * @param {() => void} updateView - * @return {Promise} - */ -const getSimBriefOfp = (mcdu, updateView, callback = () => {}) => { - const navigraphUsername = NXDataStore.get("NAVIGRAPH_USERNAME", ""); - const overrideSimBriefUserID = NXDataStore.get('CONFIG_OVERRIDE_SIMBRIEF_USERID', ''); - - if (!navigraphUsername && !overrideSimBriefUserID) { - mcdu.setScratchpadMessage(NXFictionalMessages.noNavigraphUser); - throw new Error("No Navigraph username provided"); - } - - mcdu.simbrief["sendStatus"] = "REQUESTING"; - - updateView(); - - return Fmgc.SimBriefUplinkAdapter.downloadOfpForUserID(navigraphUsername, overrideSimBriefUserID) - .then(data => { - mcdu.simbrief["units"] = data.units; - mcdu.simbrief["route"] = data.route; - mcdu.simbrief["cruiseAltitude"] = data.cruiseAltitude; - mcdu.simbrief["originIcao"] = data.origin.icao; - mcdu.simbrief["originTransAlt"] = parseInt(data.origin.transAlt, 10); - mcdu.simbrief["originTransLevel"] = parseInt(data.origin.transLevel, 10); - mcdu.simbrief["destinationIcao"] = data.destination.icao; - mcdu.simbrief["destinationTransAlt"] = parseInt(data.destination.transAlt, 10); - mcdu.simbrief["destinationTransLevel"] = parseInt(data.destination.transLevel, 10); - mcdu.simbrief["blockFuel"] = mcdu.simbrief["units"] === 'kgs' ? data.fuel.planRamp : lbsToKg(data.fuel.planRamp); - mcdu.simbrief["payload"] = mcdu.simbrief["units"] === 'kgs' ? data.weights.payload : lbsToKg(data.weights.payload); - mcdu.simbrief["estZfw"] = mcdu.simbrief["units"] === 'kgs' ? data.weights.estZeroFuelWeight : lbsToKg(data.weights.estZeroFuelWeight); - mcdu.simbrief["paxCount"] = data.weights.passengerCount; - mcdu.simbrief["bagCount"] = data.weights.bagCount; - mcdu.simbrief["paxWeight"] = data.weights.passengerWeight; - mcdu.simbrief["bagWeight"] = data.weights.bagWeight; - mcdu.simbrief["freight"] = data.weights.freight; - mcdu.simbrief["cargo"] = data.weights.cargo; - mcdu.simbrief["costIndex"] = data.costIndex; - mcdu.simbrief["navlog"] = data.navlog; - mcdu.simbrief["callsign"] = data.flightNumber; - let alternate = data.alternate; - if (Array.isArray(data.alternate)) { - alternate = data.alternate[0]; - } - mcdu.simbrief["alternateIcao"] = alternate.icao_code; - mcdu.simbrief["alternateTransAlt"] = parseInt(alternate.transAlt, 10); - mcdu.simbrief["alternateTransLevel"] = parseInt(alternate.transLevel, 10); - mcdu.simbrief["alternateAvgWindDir"] = parseInt(alternate.averageWindDirection, 10); - mcdu.simbrief["alternateAvgWindSpd"] = parseInt(alternate.averageWindSpeed, 10); - mcdu.simbrief["avgTropopause"] = data.averageTropopause; - mcdu.simbrief["ete"] = data.times.estTimeEnroute; - mcdu.simbrief["blockTime"] = data.times.estBlock; - mcdu.simbrief["outTime"] = data.times.estOut; - mcdu.simbrief["onTime"] = data.times.estOn; - mcdu.simbrief["inTime"] = data.times.estIn; - mcdu.simbrief["offTime"] = data.times.estOff; - mcdu.simbrief["taxiFuel"] = mcdu.simbrief["units"] === 'kgs' ? data.fuel.taxi : lbsToKg(data.fuel.taxi); - mcdu.simbrief["tripFuel"] = mcdu.simbrief["units"] === 'kgs' ? data.fuel.enrouteBurn : lbsToKg(data.fuel.enrouteBurn); - mcdu.simbrief["sendStatus"] = "DONE"; - - callback(); - - updateView(); - - return data; - }) - .catch(_err => { - console.log(_err.message); - - mcdu.simbrief["sendStatus"] = "READY"; - updateView(); - }); -}; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Core.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Core.js deleted file mode 100644 index 2378ceacdf6..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Core.js +++ /dev/null @@ -1,92 +0,0 @@ -const ENABLE_TOTAL_UPDATE_TIME_TRACING = false; - -class A32NX_Core { - constructor() { - this.modules = [ - { - name: 'Refuel', - module: new A32NX_Refuel(), - updateInterval: 150, - }, - { - name: 'LocalVars', - module: new A32NX_LocalVarUpdater(), - updateInterval: 50, - }, - { - name: 'FADEC #1', - module: new A32NX_FADEC(1), - updateInterval: 100, - }, - { - name: 'FADEC #2', - module: new A32NX_FADEC(2), - updateInterval: 100, - }, - { - name: 'FWC', - module: new A32NX_FWC(2), - updateInterval: 50, - }, - { - name: 'GPWS', - module: new A32NX_GPWS(this), - updateInterval: 75, - }, - { - name: 'Speeds', - module: new A32NX_Speeds(), - updateInterval: 500, - }, - ]; - this.moduleThrottlers = {}; - for (const moduleDefinition of this.modules) { - this.moduleThrottlers[moduleDefinition.name] = new UpdateThrottler(moduleDefinition.updateInterval); - } - - this.soundManager = new A32NX_SoundManager(); - this.tipsManager = A32NX_TipsManager.instance; - } - - init(startTime) { - this.getDeltaTime = A32NX_Util.createDeltaTimeCalculator(startTime); - this.modules.forEach((moduleDefinition) => { - if (typeof moduleDefinition.module.init === 'function') { - moduleDefinition.module.init(); - } - }); - - this.isInit = true; - } - - update() { - if (!this.isInit) { - return; - } - - const startTime = ENABLE_TOTAL_UPDATE_TIME_TRACING ? Date.now() : 0; - - const deltaTime = this.getDeltaTime(); - - this.soundManager.update(deltaTime); - this.tipsManager.update(deltaTime); - - let updatedModules = 0; - this.modules.forEach((moduleDefinition) => { - const moduleDeltaTime = this.moduleThrottlers[moduleDefinition.name].canUpdate(deltaTime); - - if (moduleDeltaTime !== -1) { - moduleDefinition.module.update(moduleDeltaTime, this); - updatedModules++; - } - }); - - if (ENABLE_TOTAL_UPDATE_TIME_TRACING) { - const endTime = Date.now(); - - const updateTime = endTime - startTime; - - console.warn(`NXCore update took: ${updateTime.toFixed(2)}ms (${updatedModules} modules updated)`); - } - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_DMC.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_DMC.js deleted file mode 100644 index e5174cac63c..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_DMC.js +++ /dev/null @@ -1,50 +0,0 @@ -function updateDisplayDMC(displayName, displayElement, maintElement) { - const dmcSwitchingKnob = SimVar.GetSimVarValue("L:A32NX_EIS_DMC_SWITCHING_KNOB", "Enum"); - const dmc3displayTest = SimVar.GetSimVarValue("L:A32NX_DMC_DISPLAYTEST:3", "Enum"); - let dmc1displayTest = SimVar.GetSimVarValue("L:A32NX_DMC_DISPLAYTEST:1", "Enum"); - let dmc2displayTest = SimVar.GetSimVarValue("L:A32NX_DMC_DISPLAYTEST:2", "Enum"); - - if (dmcSwitchingKnob == 0) { - dmc1displayTest = dmc3displayTest; - } else if (dmcSwitchingKnob == 2) { - dmc2displayTest = dmc3displayTest; - } - - let testActive = false; - switch (displayName) { - case "PFD1": - case "MFD1": - case "EICAS1": - testActive = dmc1displayTest == 2 ? 1 : 0; - maintMode = dmc1displayTest == 1 ? 1 : 0; - break; - case "PFD2": - case "MFD2": - case "EICAS2": - testActive = dmc2displayTest == 2 ? 1 : 0; - maintMode = dmc2displayTest == 1 ? 1 : 0; - break; - } - - const displayState = displayElement.getAttribute("display"); - if (testActive) { - if (displayState != "block") { - displayElement.setAttribute("display", "block"); - } - } else { - if (displayState != "none") { - displayElement.setAttribute("display", "none"); - } - } - - const maintState = maintElement.getAttribute("display"); - if (maintMode) { - if (maintState != "block") { - maintElement.setAttribute("display", "block"); - } - } else { - if (maintState != "none") { - maintElement.setAttribute("display", "none"); - } - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_FADEC.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_FADEC.js deleted file mode 100644 index a6369335644..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_FADEC.js +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class A32NX_FADEC { - constructor(engine) { - this.engine = engine; - this.fadecTimer = -1; - this.dcEssPoweredInPreviousUpdate = false; - this.lastActiveIgniterAutostart = 0; // 0 = A, 1 = B - } - - init() { - this.updateSimVars(); - } - - update(deltaTime) { - const dcEssIsPowered = this.isDcEssPowered(); - const ignitionState = SimVar.GetSimVarValue("L:XMLVAR_ENG_MODE_SEL", "Enum") === 2; - const engineState = SimVar.GetSimVarValue(`L:A32NX_ENGINE_STATE:${this.engine}`, "Number"); - const n2Percent = SimVar.GetSimVarValue(`L:A32NX_ENGINE_N2:${this.engine}`, "Number"); - - if ((this.dcEssPoweredInPreviousUpdate !== dcEssIsPowered && dcEssIsPowered === 1) || - (this.lastEngineState !== engineState && engineState === 4)) { - this.fadecTimer = 5 * 60; - } - if ((this.lastEngineState === 2 || this.lastEngineState === 3) && engineState !== 2 && engineState !== 3) { - this.lastActiveIgniterAutostart ^= 1; // toggles Igniter - } - - this.igniting = ignitionState && (engineState === 2 || engineState === 3) && n2Percent > 25 && n2Percent < 55; - - if (this.lastIgnitionState !== ignitionState && !ignitionState) { - this.fadecTimer = Math.max(30, this.fadecTimer); - } - this.fadecTimer -= deltaTime / 1000; - this.updateSimVars(); - this.dcEssPoweredInPreviousUpdate = dcEssIsPowered; - } - - updateSimVars() { - this.lastIgnitionState = SimVar.GetSimVarValue("L:XMLVAR_ENG_MODE_SEL", "Enum") === 2; - this.lastEngineState = SimVar.GetSimVarValue(`L:A32NX_ENGINE_STATE:${this.engine}`,"Number"); - SimVar.SetSimVarValue(`L:A32NX_FADEC_POWERED_ENG${this.engine}`, "Bool", this.isPowered() ? 1 : 0); - SimVar.SetSimVarValue(`L:A32NX_FADEC_IGNITER_A_ACTIVE_ENG${this.engine}`, "Bool", this.igniting && this.lastActiveIgniterAutostart === 0 ? 1 : 0); - SimVar.SetSimVarValue(`L:A32NX_FADEC_IGNITER_B_ACTIVE_ENG${this.engine}`, "Bool", this.igniting && this.lastActiveIgniterAutostart === 1 ? 1 : 0); - } - - isPowered() { - if (SimVar.GetSimVarValue(`L:A32NX_FIRE_BUTTON_ENG${this.engine}`, "Bool") === 1) { - return false; - } - if (SimVar.GetSimVarValue(`TURB ENG N2:${this.engine}`, "Percent") > 15) { - return true; - } - if (SimVar.GetSimVarValue("L:XMLVAR_ENG_MODE_SEL", "Enum") !== 1) { - return true; - } - if (SimVar.GetSimVarValue(`L:A32NX_OVHD_FADEC_${this.engine}`, "Bool")) { - return true; - } - if (this.fadecTimer > 0) { - return true; - } - return false; - } - - isDcEssPowered() { - // This will have to be revisited when implementing the FADEC. One shouldn't consider this reference - // to DC ESS valuable: it might be powered by multiple buses or related to other things altogether. - return SimVar.GetSimVarValue("L:A32NX_ELEC_DC_ESS_BUS_IS_POWERED", "Bool"); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_FWC.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_FWC.js deleted file mode 100644 index bedeb91c7b5..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_FWC.js +++ /dev/null @@ -1,383 +0,0 @@ -class A32NX_FWC { - constructor() { - // momentary - this.toConfigTest = null; // WTOCT - - // persistent - this.flightPhase = null; - this.ldgMemo = null; - this.toMemo = null; - - // ESDL 1. 0. 60 - this.gndMemo = new NXLogic_ConfirmNode(1); // outptuts ZGND - - // ESDL 1. 0. 60 - this.eng1OrTwoRunningConf = new NXLogic_ConfirmNode(30); - - // ESDL 1. 0. 73 - this.speedAbove80KtsMemo = new NXLogic_MemoryNode(true); - - // ESDL 1. 0. 79 / ESDL 1. 0. 80 - this.mctMemo = new NXLogic_ConfirmNode(60, false); - - // ESDL 1. 0.100 - this.firePBOutConf = new NXLogic_ConfirmNode(0.2); // CONF01 - this.firePBOutMemo = new NXLogic_TriggeredMonostableNode(2); // MTRIG 05 - this.firePBClear10 = new NXLogic_MemoryNode(false); - this.phase110Memo = new NXLogic_TriggeredMonostableNode(300); // MTRIG 03 - this.phase8GroundMemo = new NXLogic_TriggeredMonostableNode(2); // MTRIG 06 - this.ac80KtsMemo = new NXLogic_TriggeredMonostableNode(2); // MTRIG 04 - this.prevPhase9InvertMemo = new NXLogic_TriggeredMonostableNode(3, false); // MTRIG 02 - this.eng1Or2TOPowerInvertMemo = new NXLogic_TriggeredMonostableNode(1, false); // MTRIG 01 - this.phase9Nvm = new NXLogic_MemoryNode(true, true); - this.prevPhase9 = false; - - // ESDL 1. 0.110 - this.groundImmediateMemo = new NXLogic_TriggeredMonostableNode(2); // MTRIG 03 - this.phase5Memo = new NXLogic_TriggeredMonostableNode(120); // MTRIG 01 - this.phase67Memo = new NXLogic_TriggeredMonostableNode(180); // MTRIG 02 - - // ESDL 1. 0.180 - this.memoTo_conf01 = new NXLogic_ConfirmNode(120, true); // CONF 01 - this.memoTo_memo = new NXLogic_MemoryNode(false); - - // ESDL 1. 0.190 - this.memoLdgMemo_conf01 = new NXLogic_ConfirmNode(1, true); // CONF 01 - this.memoLdgMemo_inhibit = new NXLogic_MemoryNode(false); - this.memoLdgMemo_conf02 = new NXLogic_ConfirmNode(10, true); // CONF 01 - this.memoLdgMemo_below2000ft = new NXLogic_MemoryNode(true); - - // ESDL 1. 0.310 - this.memoToInhibit_conf01 = new NXLogic_ConfirmNode(3, true); // CONF 01 - - // ESDL 1. 0.320 - this.memoLdgInhibit_conf01 = new NXLogic_ConfirmNode(3, true); // CONF 01 - - // altitude warning - this.previousTargetAltitude = NaN; - this._wasBellowThreshold = false; - this._wasAboveThreshold = false; - this._wasInRange = false; - this._wasReach200ft = false; - } - - update(_deltaTime, _core) { - this._updateFlightPhase(_deltaTime); - this._updateButtons(_deltaTime); - this._updateTakeoffMemo(_deltaTime); - this._updateLandingMemo(_deltaTime); - this._updateAltitudeWarning(); - } - - _updateButtons(_deltaTime) { - this.toConfigTest = SimVar.GetSimVarValue('L:A32NX_FWS_TO_CONFIG_TEST', 'boolean'); - } - - _updateFlightPhase(_deltaTime) { - const radioHeight1 = Arinc429Word.fromSimVarValue("L:A32NX_RA_1_RADIO_ALTITUDE"); - const radioHeight2 = Arinc429Word.fromSimVarValue("L:A32NX_RA_2_RADIO_ALTITUDE"); - const radioHeight = radioHeight1.isFailureWarning() || radioHeight1.isNoComputedData() ? radioHeight2 : radioHeight1; - const eng1N1 = SimVar.GetSimVarValue("ENG N1 RPM:1", "Percent"); - const eng2N1 = SimVar.GetSimVarValue("ENG N1 RPM:2", "Percent"); - // TODO find a better source for the following value ("core speed at or above idle") - // Note that N1 starts below idle on spawn on the runway, so this should be below 16 to not jump back to phase 1 - const oneEngRunning = ( - eng1N1 > 15 || eng2N1 > 15 - ); - const eng1Or2Running = this.eng1OrTwoRunningConf.write(oneEngRunning, _deltaTime); - const engOneAndTwoNotRunning = !eng1Or2Running; - const hFail = radioHeight1.isFailureWarning() && radioHeight2.isFailureWarning(); - const adcTestInhib = false; - - // ESLD 1.0.60 - const groundImmediate = Simplane.getIsGrounded(); - const ground = this.gndMemo.write(groundImmediate, _deltaTime); - - // ESLD 1.0.73 - const ias = SimVar.GetSimVarValue("AIRSPEED INDICATED", "knots"); - const acSpeedAbove80kts = this.speedAbove80KtsMemo.write(ias > 83, ias < 77); - - // ESLD 1.0.90 - const hAbv1500 = radioHeight.isNoComputedData() || radioHeight.value > 1500; - const hAbv800 = radioHeight.isNoComputedData() || radioHeight.value > 800; - - // ESLD 1.0.79 + 1.0.80 - const eng1TLA = SimVar.GetSimVarValue("L:A32NX_AUTOTHRUST_TLA:1", "number"); - const eng1TLAFTO = SimVar.GetSimVarValue("L:A32NX_AIRLINER_TO_FLEX_TEMP", "number") !== 0; // is a flex temp is set? - const eng1MCT = eng1TLA > 33.3 && eng1TLA < 36.7; - const eng1TLAFullPwr = eng1TLA > 43.3; - const eng2TLA = SimVar.GetSimVarValue("L:A32NX_AUTOTHRUST_TLA:2", "number"); - const eng2TLAFTO = eng1TLAFTO; // until we have proper FADECs - const eng2MCT = eng2TLA > 33.3 && eng2TLA < 36.7; - const eng2TLAFullPwr = eng2TLA > 43.3; - const eng1OrEng2SupMCT = !(eng1TLA < 36.7) || !(eng2TLA < 36.7); - const eng1AndEng2MCL = eng1TLA > 22.9 && eng2TLA > 22.9; - const eng1Or2TOPowerSignal = ( - (eng1TLAFTO && eng1MCT) || - (eng2TLAFTO && eng2MCT) || - (eng1OrEng2SupMCT || eng1OrEng2SupMCT) || - (eng1TLAFullPwr || eng2TLAFullPwr) - ); - const eng1Or2TOPower = ( - eng1Or2TOPowerSignal || - (this.mctMemo.write(eng1Or2TOPowerSignal, _deltaTime) && !hAbv1500 && eng1AndEng2MCL) - ); - - // ESLD 1.0.100 - const eng1FirePbOut = SimVar.GetSimVarValue("L:A32NX_FIRE_BUTTON_ENG1", "Bool"); - const eng1FirePbMemo = this.firePBOutMemo.write( - this.firePBOutConf.write(eng1FirePbOut, _deltaTime), - _deltaTime - ); - const resetFirePbClear10 = eng1FirePbMemo && ground; - - const phase8 = ( - (this.phase8GroundMemo.write(groundImmediate, _deltaTime) || groundImmediate) && - !eng1Or2TOPower && - acSpeedAbove80kts - ); - - const phase34Cond = ground && eng1Or2TOPower; - const phase3 = !acSpeedAbove80kts && eng1Or2Running && phase34Cond; - const phase4 = acSpeedAbove80kts && phase34Cond; - - const setPhase9Nvm = phase3 || phase8; - const resetPhase9Nvm = ( - ( - !this.ac80KtsMemo.write(!acSpeedAbove80kts, _deltaTime) && - ( - (ground && this.prevPhase9InvertMemo.write(this.prevPhase9, _deltaTime)) || - resetFirePbClear10 || - (ground && this.eng1Or2TOPowerInvertMemo.write(eng1Or2TOPower, _deltaTime)) - ) && - !this.prevPhase9 - ) || adcTestInhib - ); - const phase9Nvm = this.phase9Nvm.write(setPhase9Nvm, resetPhase9Nvm); // S* / R (NVM) - const phase29Cond = ground && !eng1Or2TOPower && !acSpeedAbove80kts; - const phase9 = oneEngRunning && phase9Nvm && phase29Cond; - const phase2 = phase29Cond && !phase9Nvm && eng1Or2Running; - - const phase110MemoA = this.firePBClear10.write(phase9, resetFirePbClear10); // S / R* - const phase110Cond = !phase9 && engOneAndTwoNotRunning && groundImmediate; - const phase110Memo = this.phase110Memo.write(phase110MemoA && phase110Cond, _deltaTime); // MTRIG 03 - const phase1 = phase110Cond && !phase110Memo; - const phase10 = phase110Cond && phase110Memo; - - this.prevPhase9 = phase9; - - // ESLD 1.0.110 - const ground2sMemorized = this.groundImmediateMemo.write(groundImmediate, _deltaTime) || groundImmediate; - const phase5Cond = !hAbv1500 && eng1Or2TOPower && !hFail && !ground2sMemorized; - const phase5 = this.phase5Memo.write(phase5Cond, _deltaTime) && phase5Cond; - - const phase67Cond = ( - !ground2sMemorized && - !hFail && - !eng1Or2TOPower && - !hAbv1500 && - !hAbv800 - ); - const phase67Memo = this.phase67Memo.write(phase67Cond, _deltaTime) && phase67Cond; - - const phase6 = !phase5 && !ground2sMemorized && !phase67Memo; - const phase7 = phase67Memo && !phase8; - - /*** End of ESLD logic ***/ - - // consolidate into single variable (just to be safe) - const phases = [phase1, phase2, phase3, phase4, phase5, phase6, phase7, phase8, phase9, phase10]; - - if (this.flightPhase === null && phases.indexOf(true) !== -1) { - // if we aren't initialized, just grab the first one that is valid - this._setFlightPhase(phases.indexOf(true) + 1); - console.log(`FWC flight phase: ${this.flightPhase}`); - return; - } - - const activePhases = phases.map((x, i) => [x, i + 1]).filter(y => !!y[0]).map(z => z[1]); - - // the usual and easy case: only one flight phase is valid - if (activePhases.length === 1) { - if (activePhases[0] !== this.flightPhase) { - console.log(`FWC flight phase: ${this.flightPhase} => ${activePhases[0]}`); - this._setFlightPhase(activePhases[0]); - } - return; - } - - // the mixed case => warn - if (activePhases.length > 1) { - console.warn(`Multiple FWC flight phases are valid: ${activePhases.join(", ")}`); - if (activePhases.indexOf(this.flightPhase) !== -1) { - // if the currently active one is present, keep it - console.warn(`Remaining in FWC flight phase ${this.flightPhase}`); - return; - } - // pick the earliest one - this._setFlightPhase(activePhases[0]); - console.log(`Resolving by switching FWC flight phase: ${this.flightPhase} => ${activePhases[0]}`); - return; - } - - // otherwise, no flight phase is valid => warn - console.warn("No valid FWC flight phase"); - if (this.flightPhase === null) { - this._setFlightPhase(null); - } - } - - _setFlightPhase(flightPhase) { - if (flightPhase === this.flightPhase) { - return; - } - - // update flight phase - this.flightPhase = flightPhase; - SimVar.SetSimVarValue("L:A32NX_FWC_FLIGHT_PHASE", "Enum", this.flightPhase || 0); - } - - _updateTakeoffMemo(_deltaTime) { - /// FWC ESLD 1.0.180 - const setFlightPhaseMemo = this.flightPhase === 2 && this.toConfigTest; - const resetFlightPhaseMemo = ( - this.flightPhase === 10 || - this.flightPhase === 3 || - this.flightPhase === 1 || - this.flightPhase === 6 - ); - const flightPhaseMemo = this.memoTo_memo.write(setFlightPhaseMemo, resetFlightPhaseMemo); - - const eng1NotRunning = SimVar.GetSimVarValue("ENG N1 RPM:1", "Percent") < 15; - const eng2NotRunning = SimVar.GetSimVarValue("ENG N1 RPM:2", "Percent") < 15; - const toTimerElapsed = this.memoTo_conf01.write(!eng1NotRunning && !eng2NotRunning, _deltaTime); - - this.toMemo = flightPhaseMemo || (this.flightPhase === 2 && toTimerElapsed); - SimVar.SetSimVarValue("L:A32NX_FWC_TOMEMO", "Bool", this.toMemo); - } - - _updateLandingMemo(_deltaTime) { - const radioHeight1 = Arinc429Word.fromSimVarValue("L:A32NX_RA_1_RADIO_ALTITUDE"); - const radioHeight2 = Arinc429Word.fromSimVarValue("L:A32NX_RA_2_RADIO_ALTITUDE"); - const radioHeight1Invalid = radioHeight1.isFailureWarning() || radioHeight1.isNoComputedData(); - const radioHeight2Invalid = radioHeight2.isFailureWarning() || radioHeight2.isNoComputedData(); - const gearDownlocked = SimVar.GetSimVarValue("GEAR TOTAL PCT EXTENDED", "percent") > 0.95; - - // FWC ESLD 1.0.190 - const setBelow2000ft = (radioHeight1.value < 2000 && !radioHeight1Invalid) || (radioHeight2.value < 2000 && !radioHeight2Invalid); - const resetBelow2000ft = (radioHeight1.value > 2200 || radioHeight1Invalid) && (radioHeight2.value > 2200 || radioHeight2Invalid); - const memo2 = this.memoLdgMemo_below2000ft.write(setBelow2000ft, resetBelow2000ft); - - const setInhibitMemo = this.memoLdgMemo_conf01.write(resetBelow2000ft && !radioHeight1Invalid && !radioHeight2Invalid, _deltaTime); - const resetInhibitMemo = !(this.flightPhase === 7 || this.flightPhase === 8 || this.flightPhase === 6); - const memo1 = this.memoLdgMemo_inhibit.write(setInhibitMemo, resetInhibitMemo); - - const showInApproach = memo1 && memo2 && this.flightPhase === 6; - - const invalidRadioMemo = this.memoLdgMemo_conf02.write(radioHeight1Invalid && radioHeight2Invalid && gearDownlocked && this.flightPhase === 6); - - this.ldgMemo = showInApproach || invalidRadioMemo || this.flightPhase === 8 || this.flightPhase === 7; - SimVar.SetSimVarValue("L:A32NX_FWC_LDGMEMO", "Bool", this.ldgMemo); - } - - _updateAltitudeWarning() { - const indicatedAltitude = Simplane.getAltitude(); - const shortAlert = SimVar.GetSimVarValue("L:A32NX_ALT_DEVIATION_SHORT", "Bool"); - if (shortAlert === 1) { - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION_SHORT", "Bool", false); - } - - const warningPressed = SimVar.GetSimVarValue("L:PUSH_AUTOPILOT_MASTERAWARN_L", "Bool") || SimVar.GetSimVarValue("L:PUSH_AUTOPILOT_MASTERAWARN_R", "Bool"); - if (warningPressed) { - this._wasBellowThreshold = false; - this._wasAboveThreshold = false; - this._wasInRange = false; - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION", "Bool", false); - return; - } - - if (Simplane.getIsGrounded()) { - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION", "Bool", false); - } - - // Use FCU displayed value - const currentAltitudeConstraint = SimVar.GetSimVarValue("L:A32NX_FG_ALTITUDE_CONSTRAINT", "feet"); - const currentFCUAltitude = SimVar.GetSimVarValue("AUTOPILOT ALTITUDE LOCK VAR:3", "feet"); - const targetAltitude = currentAltitudeConstraint && !this.hasAltitudeConstraint() ? currentAltitudeConstraint : currentFCUAltitude; - - // Exit when selected altitude is being changed - if (this.previousTargetAltitude !== targetAltitude) { - this.previousTargetAltitude = targetAltitude; - this._wasBellowThreshold = false; - this._wasAboveThreshold = false; - this._wasInRange = false; - this._wasReach200ft = false; - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION_SHORT", "Bool", false); - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION", "Bool", false); - return; - } - - // Exit when: - // - Landing gear down & slats extended - // - Glide slope captured - // - Landing locked down - - const landingGearIsDown = SimVar.GetSimVarValue("L:A32NX_FLAPS_HANDLE_INDEX", "Enum") >= 1 && SimVar.GetSimVarValue("L:A32NX_GEAR_HANDLE_POSITION", "Percent over 100") > 0.5; - const verticalMode = SimVar.GetSimVarValue("L:A32NX_FMA_VERTICAL_MODE", "Number"); - const glideSlopeCaptured = verticalMode >= 30 && verticalMode <= 34; - const landingGearIsLockedDown = SimVar.GetSimVarValue("GEAR POSITION:0", "Enum") > 0.9; - const isTcasResolutionAdvisoryActive = SimVar.GetSimVarValue("L:A32NX_TCAS_STATE", "Enum") > 1; - if (landingGearIsDown || glideSlopeCaptured || landingGearIsLockedDown || isTcasResolutionAdvisoryActive) { - this._wasBellowThreshold = false; - this._wasAboveThreshold = false; - this._wasInRange = false; - this._wasReach200ft = false; - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION_SHORT", "Bool", false); - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION", "Bool", false); - return; - } - - const delta = Math.abs(indicatedAltitude - targetAltitude); - - if (delta < 200) { - this._wasBellowThreshold = true; - this._wasAboveThreshold = false; - this._wasReach200ft = true; - } - if (750 < delta) { - this._wasAboveThreshold = true; - this._wasBellowThreshold = false; - } - if (200 <= delta && delta <= 750) { - this._wasInRange = true; - } - - if (this._wasBellowThreshold && this._wasReach200ft) { - if (delta >= 200) { - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION", "Bool", true); - } else if (delta < 200) { - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION", "Bool", false); - } - } else if (this._wasAboveThreshold && delta <= 750 && !this._wasReach200ft) { - if (!SimVar.GetSimVarValue("L:A32NX_AUTOPILOT_1_ACTIVE", "Bool") && !SimVar.GetSimVarValue("L:A32NX_AUTOPILOT_2_ACTIVE", "Bool")) { - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION", "Bool", false); - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION_SHORT", "Bool", true); - } - } else if (750 < delta && this._wasInRange && !this._wasReach200ft) { - if (750 < delta) { - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION", "Bool", true); - } else if (delta >= 750) { - SimVar.SetSimVarValue("L:A32NX_ALT_DEVIATION", "Bool", false); - } - } - } - - hasAltitudeConstraint() { - if (this.aircraft == Aircraft.A320_NEO) { - if (Simplane.getAutoPilotAltitudeManaged() && SimVar.GetSimVarValue("L:AP_CURRENT_TARGET_ALTITUDE_IS_CONSTRAINT", "number") != 0) { - return false; - } - } - return true; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_FuelPred.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_FuelPred.js deleted file mode 100644 index a08796d5e82..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_FuelPred.js +++ /dev/null @@ -1,245 +0,0 @@ -// DO NOT TOUCH THESE VALUES -const airDistanceCoeff = math.bignumber(math.matrix([ - [-5.64E-02,1.00E+00,-3.30E-07,1.38E-10,-8.55E-15,-3.73E-18,4.77E-22], - [-1.06E-03,-2.22E-03,-8.92E-10,1.31E-12,-4.30E-16,4.02E-20,0], - [9.93E-05,4.89E-06,1.75E-11,-6.27E-15,6.91E-19,0,0], - [1.12E-07,-1.11E-08,-1.14E-13,1.73E-17,0,0,0], - [-1.30E-08,3.04E-11,-2.14E-16,0,0,0,0], - [-4.07E-12,-5.50E-14,0,0,0,0,0], - [3.74E-13,0,0,0,0,0,0] -])); - -// DO NOT TOUCH THESE VALUES -const fuelConsumedCoeff = math.bignumber(math.matrix([ - [4.069435e+02, 1.080068e+01, 1.868617e-03, 4.469823e-06, -1.075694e-09, 1.699993e-13], - [-1.429171e+01, -8.078463e-02, -7.557951e-05, -6.795487e-09, -1.238178e-12, 0], - [1.984013e-01, 6.804436e-04, 2.789351e-07, 2.353046e-11, 0, 0], - [-1.330668e-03, -2.251760e-06, -3.946554e-10, 0, 0, 0], - [3.930031e-06, 2.634909e-09, 0, 0, 0, 0], - [-4.209321e-09, 0, 0, 0, 0, 0] -])); - -// DO NOT TOUCH THESE VALUES -const timeCoeff = math.bignumber(math.matrix([ - [-2.307264e+02, 1.161741e+00, -1.208222e-03, 1.002013e-07, -2.440974e-11, 4.213891e-15], - [4.151808e+00, -1.124149e-02, 9.688891e-06, -1.392537e-10, -3.745942e-14, 0], - [-2.846925e-02, 5.221070e-05, -2.839090e-08, 5.961502e-13, 0, 0], - [8.892639e-05, -1.087864e-07, 2.572222e-11, 0, 0, 0], - [-1.236801e-07, 8.777364e-11, 0, 0, 0, 0], - [5.521856e-11, 0, 0, 0, 0, 0] -])); - -// DO NOT TOUCH THESE VALUES -const correctionsCoef = math.bignumber(math.matrix ([ - [-4.502431e+00, -2.212160e-03, 1.379723e-05, 9.071250e-08, 3.291840e-12, 3.007572e-18], - [-1.410121e-01, 7.319389e-04, -1.299149e-06, -5.614996e-10, -1.371330e-14, 0], - [3.467151e-03, -1.438481e-06, 7.152032e-09, 9.475944e-13, 0, 0], - [-2.559041e-05, -4.887061e-09, -1.067236e-11, 0, 0, 0], - [7.616725e-08, 1.345230e-11, 0, 0, 0, 0], - [-7.977101e-11, 0, 0, 0, 0, 0] -])); - -// DO NOT TOUCH THESE VALUES -const altTimeCoef = math.bignumber(math.matrix([ - [-2.491288e+01, 4.715493e-01, -8.365416e-04, -2.578474e-06, 2.125971e-08, -3.165746e-11], - [2.537249e-01, -1.867867e-03, 1.154863e-05, -6.299859e-08, 1.098651e-10, 0], - [1.299638e-04, 5.221100e-06, 3.780907e-08, -1.382036e-10, 0, 0], - [-1.363711e-05, -3.246849e-08, 1.174097e-10, 0, 0, 0], - [5.570762e-08, -3.605170e-11, 0, 0, 0, 0], - [-5.290598e-11, 0, 0, 0, 0, 0] -])); - -// DO NOT TOUCH THESE VALUES -const altFuelConsumedCoef = math.bignumber(math.matrix([ - [-1.150449e+03, 2.328350e+01, -2.914237e-02, -6.834285e-05, 6.611919e-07, -1.041647e-09], - [1.122890e+01, -2.179675e-01, 3.322086e-04, -1.966203e-06, 3.776331e-09, 0], - [3.397620e-02, 1.309511e-03, 1.089408e-06, -5.094745e-09, 0, 0], - [-8.409842e-04, -4.082921e-06, 4.695926e-09, 0, 0, 0], - [3.119312e-06, 1.397091e-09, 0, 0, 0, 0], - [-3.065377e-09, 0, 0, 0, 0, 0] -])); - -// DO NOT TOUCH THESE VALUES -const altCorrectionsCoeff = math.bignumber(math.matrix([ - [5.735300e+01, -1.087438e-01, 2.945632e-04, -1.440854e-06, 4.636839e-09, -5.967608e-12], - [-1.495235e+00, 1.909434e-03, -1.015931e-07, -7.037200e-09, 1.818587e-11, 0], - [1.484228e-02, -8.755315e-06, 7.469694e-09, -2.930156e-11, 0, 0], - [-7.065761e-05, 1.559773e-08, 2.511840e-11, 0, 0, 0], - [1.596208e-07, -2.479681e-11, 0, 0, 0, 0], - [-1.354973e-10, 0, 0, 0, 0, 0] -])); - -// DO NOT TOUCH THESE VALUES -const holdingFFCoeff = math.bignumber(math.matrix([ - [-7.241814e+01, 1.547344e+02, -9.771374e+00, 2.825355e-01, -4.163005e-03, 3.112997e-05, -9.425687e-08], - [-8.776689e+01, 4.591613e+00, -9.195936e-02, 9.173242e-04, -4.938582e-06, 1.249467e-08, 0], - [8.290402e-01, -3.535182e-02, 5.086226e-04, -2.918997e-06, 5.495734e-09, 0, 0], - [-3.263924e-03, 1.145225e-04, -1.177681e-06, 3.652267e-09, 0, 0, 0], - [5.285811e-06, -1.484045e-07, 8.385686e-10, 0, 0, 0, 0], - [-2.100748e-09, 5.800337e-11, 0, 0, 0, 0, 0], - [-1.558294e-12, 0, 0, 0, 0, 0, 0] -])); - -const userAltTimeCoeff = math.bignumber(math.matrix([ - [1.934198e+01, -3.211068e-03, 7.848773e-06, -9.051067e-09, 3.631462e-12, -4.555530e-16], - [-3.851766e-01, 6.104416e-04, 7.078771e-08, -2.693042e-11, 1.890995e-15, 0], - [2.633289e-03, -4.659318e-06, 5.933422e-11, 1.599828e-14, 0, 0], - [-7.320044e-06, 1.295341e-08, -1.857516e-13, 0, 0, 0], - [6.762639e-09, -1.259232e-11, 0, 0, 0, 0], - [9.144145e-13, 0, 0, 0, 0, 0] -])); - -/** - * @param {number}value - the value to build the matrix from - * @returns {math.matrix} return a 7x7 matrix for A predictors - */ -const _buildAMatrix7 = (value) => { - return math.bignumber(math.matrix([ - [1, value ** 1, value ** 2, value ** 3, value ** 4, value ** 5, value ** 6], - [0,0,0,0,0,0,0], - [0,0,0,0,0,0,0], - [0,0,0,0,0,0,0], - [0,0,0,0,0,0,0], - [0,0,0,0,0,0,0], - [0,0,0,0,0,0,0], - ])); -}; - -/** - * @param {number}value - the value to build the matrix from - * @returns {math.matrix} return a 6x6 matrix for A predictors - */ -const _buildAMatrix6 = (value) => { - return math.bignumber(math.matrix([ - [1, value ** 1, value ** 2, value ** 3, value ** 4, value ** 5], - [0,0,0,0,0,0], - [0,0,0,0,0,0], - [0,0,0,0,0,0], - [0,0,0,0,0,0], - [0,0,0,0,0,0], - ])); -}; - -/** - * @param {number}value - the value to build the matrix from - * @returns {math.matrix} return a 7x7 matrix for B predictors - */ -const _buildBMatrix7 = (value) => { - return math.bignumber(math.matrix([ - [1,0,0,0,0,0,0], - [value ** 1,0,0,0,0,0,0], - [value ** 2,0,0,0,0,0,0], - [value ** 3,0,0,0,0,0,0], - [value ** 4,0,0,0,0,0,0], - [value ** 5,0,0,0,0,0,0], - [value ** 6,0,0,0,0,0,0], - ])); -}; - -/** - * @param {number}value - the value to build the matrix from - * @returns {math.matrix} return a 6x6 matrix for B predictors - */ -const _buildBMatrix6 = (value) => { - return math.bignumber(math.matrix([ - [1,0,0,0,0,0], - [value ** 1,0,0,0,0,0], - [value ** 2,0,0,0,0,0], - [value ** 3,0,0,0,0,0], - [value ** 4,0,0,0,0,0], - [value ** 5,0,0,0,0,0], - ])); -}; -//TODO Refactor this when you have time -class A32NX_FuelPred { - - /** - * Computes a flight time when a user inputs they're own weight for alternate fuel - * @param {number} fuel - fuel in kg e.g 1200KG - * @param {number} flightLevel - Flight Level in raw form e.g FL120 = 120 - * @return {number} predicted flight time - */ - static computeUserAltTime(fuel, flightLevel) { - const fuelMatrix = _buildAMatrix6(fuel); - const flightLevelMatrix = _buildBMatrix6(flightLevel); - const mmOfFuelFL = math.multiply(flightLevelMatrix, fuelMatrix); - return (Math.round(math.sum(math.dotMultiply(userAltTimeCoeff, mmOfFuelFL)))); - } - - /** - * Computes Air Distance in NM using computed polynomial coefficients - * @param {number} groundDistance - ground distance in NM e.g 200 - * @param {number} windComponent - wind in KTs, HD should be identified with a negative number - * e.g HD150 == -150 vice versa for tailwind - * @returns {number} computedAirDistance in NM - */ - static computeAirDistance(groundDistance, windComponent) { - const groundMatrix = _buildAMatrix7(groundDistance); - const windMatrix = _buildBMatrix7(windComponent); - - const mmOfGroundWind = math.multiply(windMatrix, groundMatrix); - return (Math.round(math.sum(math.dotMultiply(airDistanceCoeff,mmOfGroundWind)))); - } - - /** - * - * @param {number} weight - ZFW weight of the aircraft in padded form e.g 53,000KG = 53 - * @param {number} flightLevel - Flight level in padded form without any alpha chracters e.g FL250 = 250 - * @return {number} predicted fuel flow for one engine per hour e.g result = 600, then 600kg for 30 minutes of holding - */ - static computeHoldingTrackFF(weight, flightLevel) { - const weightMatrix = _buildAMatrix7(weight); - const flightLevelMatrix = _buildBMatrix7(flightLevel); - const mmOfWeightFL = math.multiply(flightLevelMatrix, weightMatrix); - return (Math.round(math.sum(math.dotMultiply(holdingFFCoeff,mmOfWeightFL)))); - } - - /** - * Computes time, fuel and corrections needed for a trip or alternate //TODO work on a new method name - * @param {number} airDistance - air distance in NM e.g 200 - * @param {number} flightLevel - cruising flight level e.g FL290 == 290 - * @param {computations} computation - ENUM of either TIME, FUEL or CORRECTIONS - * @param {boolean} alternate - States whether this computations is for an alternate destination or not - * @returns {number} fuel consumed in KG - */ - static computeNumbers(airDistance, flightLevel, computation, alternate) { - const airDistanceMatrix = _buildAMatrix6(airDistance); - const flightLevelMatrix = _buildBMatrix6(flightLevel); - const mmOfDistFL = math.multiply(flightLevelMatrix, airDistanceMatrix); - //TODO Create logic for handling 200NM and FL390 = 0 - switch (computation) { - case this.computations.FUEL: - return (Math.round(math.sum(math.dotMultiply((alternate ? altFuelConsumedCoef : fuelConsumedCoeff), mmOfDistFL)))); - case this.computations.TIME: - return (Math.round(math.sum(math.dotMultiply((alternate ? altTimeCoef : timeCoeff), mmOfDistFL)))); - case this.computations.CORRECTIONS: - return (Math.round(math.sum(math.dotMultiply((alternate ? altCorrectionsCoeff : correctionsCoef), mmOfDistFL)))); - } - } - - constructor() { - } -} - -A32NX_FuelPred.refWeight = 55; - -A32NX_FuelPred.computations = { - TIME:"time", - FUEL:"fuel", - CORRECTIONS:"corrections" -}; - -A32NX_FuelPred.correction = { - LOW_AIR_CONDITIONING : -0.005, - ENGINE_ANTI_ICE_ON : 0.02, - TOTAL_ANTI_ICE_ON : 0.05, -}; - -A32NX_FuelPred.altCorrection = { - LOW_AIR_CONDITIONING: -0.05, - ENGINE_ANTI_ICE_ON: 0.02, - TOTAL_ANTI_ICE_ON: 0.09, - LOW_AIR_CONDITIONING_HIGH_FL: -0.005, - ENGINE_ANTI_ICE_ON_HIGH_FL : 0.015, - TOTAL_ANTI_ICE_ON_HIGH_FL : 0.07 -}; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_GPWS.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_GPWS.js deleted file mode 100644 index 397fa8d4ca5..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_GPWS.js +++ /dev/null @@ -1,1025 +0,0 @@ -// Note the master copy of these flags is contained in `fbw-a32nx\src\systems\shared\src\AutoCallOuts.ts` -// Please do not edit here unless copying from there. - -/** Bit flags for the radio auto call outs (for CONFIG_A32NX_FWC_RADIO_AUTO_CALL_OUT_PINS). */ -const A32NXRadioAutoCallOutFlags = Object.freeze({ - TwoThousandFiveHundred: 1 << 0, - TwentyFiveHundred: 1 << 1, - TwoThousand: 1 << 2, - OneThousand: 1 << 3, - FiveHundred: 1 << 4, - FourHundred: 1 << 5, - ThreeHundred: 1 << 6, - TwoHundred: 1 << 7, - OneHundred: 1 << 8, - Fifty: 1 << 9, - Forty: 1 << 10, - Thirty: 1 << 11, - Twenty: 1 << 12, - Ten: 1 << 13, - Five: 1 << 14, - FiveHundredGlide: 1 << 15, -}); - -/** The default (Airbus basic configuration) radio altitude auto call outs. */ -const DEFAULT_RADIO_AUTO_CALL_OUTS = A32NXRadioAutoCallOutFlags.TwoThousandFiveHundred | A32NXRadioAutoCallOutFlags.OneThousand | A32NXRadioAutoCallOutFlags.FourHundred - | A32NXRadioAutoCallOutFlags.Fifty | A32NXRadioAutoCallOutFlags.Forty | A32NXRadioAutoCallOutFlags.Thirty | A32NXRadioAutoCallOutFlags.Twenty - | A32NXRadioAutoCallOutFlags.Ten | A32NXRadioAutoCallOutFlags.Five; - -class A32NX_GPWS { - constructor(_core) { - console.log('A32NX_GPWS constructed'); - this.core = _core; - - this.autoCallOutPins = DEFAULT_RADIO_AUTO_CALL_OUTS; - - this.minimumsState = 0; - - this.Mode3MaxBaroAlt = NaN; - - this.Mode4MaxRAAlt = 0; - - this.Mode2BoundaryLeaveAlt = NaN; - this.Mode2NumTerrain = 0; - this.Mode2NumFramesInBoundary = 0; - - this.RadioAltRate = NaN; - this.prevRadioAlt = NaN; - this.prevRadioAlt2 = NaN; - - this.modes = [ - // Mode 1 - { - // 0: no warning, 1: "sink rate", 2 "pull up" - current: 0, - previous: 0, - type: [ - {}, - { sound: soundList.sink_rate, soundPeriod: 1.1, gpwsLight: true }, - { gpwsLight: true, pullUp: true } - ] - }, - // Mode 2 is currently inactive. - { - // 0: no warning, 1: "terrain", 2: "pull up" - current: 0, - previous: 0, - type: [{}, { gpwsLight: true }, { gpwsLight: true, pullUp: true }], - }, - // Mode 3 - { - // 0: no warning, 1: "don't sink" - current: 0, - previous: 0, - type: [{}, { sound: soundList.dont_sink, soundPeriod: 1.1, gpwsLight: true }] - }, - // Mode 4 - { - // 0: no warning, 1: "too low gear", 2: "too low flaps", 3: "too low terrain" - current: 0, - previous: 0, - type: [ - {}, - { sound: soundList.too_low_gear, soundPeriod: 1.1, gpwsLight: true }, - { sound: soundList.too_low_flaps, soundPeriod: 1.1, gpwsLight: true }, - { sound: soundList.too_low_terrain, soundPeriod: 1.1, gpwsLight: true } - ] - }, - // Mode 5, not all warnings are fully implemented - { - // 0: no warning, 1: "glideslope", 2: "hard glideslope" (louder) - current: 0, - previous: 0, - type: [ - {}, - {}, - {}, - ], - onChange: (current, _) => { - this.setGlideSlopeWarning(current >= 1); - } - } - ]; - - this.PrevShouldPullUpPlay = 0; - - this.AltCallState = A32NX_Util.createMachine(AltCallStateMachine); - this.AltCallState.setState("ground"); - this.RetardState = A32NX_Util.createMachine(RetardStateMachine); - this.RetardState.setState("landed"); - - this.isAirVsGroundMode = SimVar.GetSimVarValue("L:A32NX_GPWS_GROUND_STATE", "Bool") !== 1; - this.airborneFor5s = new NXLogic_ConfirmNode(5); - this.airborneFor10s = new NXLogic_ConfirmNode(10); - - this.isApproachVsTakeoffState = SimVar.GetSimVarValue("L:A32NX_GPWS_APPROACH_STATE", "Bool") === 1; - - this.isOverflightDetected = new NXLogic_TriggeredMonostableNode(60, false); - // Only relevant if alternate mode 4b is enabled - this.isMode4aInhibited = false; - - // PIN PROGs - this.isAudioDeclutterEnabled = false; - this.isAlternateMode4bEnabled = false; - this.isTerrainClearanceFloorEnabled = false; - this.isTerrainAwarenessEnabled = false; - - this.egpwsAlertDiscreteWord1 = Arinc429Word.empty(); - this.egpwsAlertDiscreteWord2 = Arinc429Word.empty(); - } - - gpwsUpdateDiscreteWords() { - this.egpwsAlertDiscreteWord1.ssm = Arinc429Word.SignStatusMatrix.NormalOperation; - this.egpwsAlertDiscreteWord1.setBitValue(11, this.modes[0].current === 1); - this.egpwsAlertDiscreteWord1.setBitValue(12, this.modes[0].current === 2); - this.egpwsAlertDiscreteWord1.setBitValue(13, this.modes[1].current === 1); - this.egpwsAlertDiscreteWord1.setBitValue(12, this.modes[1].current === 2); - this.egpwsAlertDiscreteWord1.setBitValue(14, this.modes[2].current === 1); - this.egpwsAlertDiscreteWord1.setBitValue(15, this.modes[3].current === 1); - this.egpwsAlertDiscreteWord1.setBitValue(16, this.modes[3].current === 2); - this.egpwsAlertDiscreteWord1.setBitValue(17, this.modes[3].current === 3); - this.egpwsAlertDiscreteWord1.setBitValue(18, this.modes[4].current === 1); - Arinc429Word.toSimVarValue('L:A32NX_EGPWS_ALERT_1_DISCRETE_WORD_1', this.egpwsAlertDiscreteWord1.value, this.egpwsAlertDiscreteWord1.ssm); - Arinc429Word.toSimVarValue('L:A32NX_EGPWS_ALERT_2_DISCRETE_WORD_1', this.egpwsAlertDiscreteWord1.value, this.egpwsAlertDiscreteWord1.ssm); - - this.egpwsAlertDiscreteWord2.ssm = Arinc429Word.SignStatusMatrix.NormalOperation; - this.egpwsAlertDiscreteWord2.setBitValue(14, false); - Arinc429Word.toSimVarValue('L:A32NX_EGPWS_ALERT_1_DISCRETE_WORD_2', this.egpwsAlertDiscreteWord2.value, this.egpwsAlertDiscreteWord2.ssm); - Arinc429Word.toSimVarValue('L:A32NX_EGPWS_ALERT_2_DISCRETE_WORD_2', this.egpwsAlertDiscreteWord2.value, this.egpwsAlertDiscreteWord2.ssm); - } - - setGlideSlopeWarning(state) { - SimVar.SetSimVarValue('L:A32NX_GPWS_GS_Warning_Active', 'Bool', state ? 1 : 0); // Still need this for XML - this.egpwsAlertDiscreteWord2.setBitValue(11, state); - Arinc429Word.toSimVarValue('L:A32NX_EGPWS_ALERT_1_DISCRETE_WORD_2', this.egpwsAlertDiscreteWord2.value, this.egpwsAlertDiscreteWord2.ssm); - Arinc429Word.toSimVarValue('L:A32NX_EGPWS_ALERT_2_DISCRETE_WORD_2', this.egpwsAlertDiscreteWord2.value, this.egpwsAlertDiscreteWord2.ssm); - } - - setGpwsWarning(state) { - SimVar.SetSimVarValue('L:A32NX_GPWS_Warning_Active', 'Bool', state ? 1 : 0); // Still need this for XML - this.egpwsAlertDiscreteWord2.setBitValue(12, state); - this.egpwsAlertDiscreteWord2.setBitValue(13, state); - Arinc429Word.toSimVarValue('L:A32NX_EGPWS_ALERT_1_DISCRETE_WORD_2', this.egpwsAlertDiscreteWord2.value, this.egpwsAlertDiscreteWord2.ssm); - Arinc429Word.toSimVarValue('L:A32NX_EGPWS_ALERT_2_DISCRETE_WORD_2', this.egpwsAlertDiscreteWord2.value, this.egpwsAlertDiscreteWord2.ssm); - } - - init() { - console.log('A32NX_GPWS init'); - - this.setGlideSlopeWarning(false); - this.setGpwsWarning(false); - - NXDataStore.getAndSubscribe('CONFIG_A32NX_FWC_RADIO_AUTO_CALL_OUT_PINS', (k, v) => k === 'CONFIG_A32NX_FWC_RADIO_AUTO_CALL_OUT_PINS' && (this.autoCallOutPins = v), DEFAULT_RADIO_AUTO_CALL_OUTS); - } - - update(deltaTime, _core) { - this.gpws(deltaTime); - } - - gpws(deltaTime) { - // EGPWS receives ADR1 only - const baroAlt = Arinc429Word.fromSimVarValue("L:A32NX_ADIRS_ADR_1_BARO_CORRECTED_ALTITUDE_1"); - const computedAirspeed = Arinc429Word.fromSimVarValue("L:A32NX_ADIRS_ADR_1_COMPUTED_AIRSPEED"); - const pitch = Arinc429Word.fromSimVarValue("L:A32NX_ADIRS_IR_1_PITCH"); - const inertialVs = Arinc429Word.fromSimVarValue("L:A32NX_ADIRS_IR_1_VERTICAL_SPEED"); - const barometricVs = Arinc429Word.fromSimVarValue("L:A32NX_ADIRS_ADR_1_BAROMETRIC_VERTICAL_SPEED"); - const radioAlt1 = Arinc429Word.fromSimVarValue("L:A32NX_RA_1_RADIO_ALTITUDE"); - const radioAlt2 = Arinc429Word.fromSimVarValue("L:A32NX_RA_2_RADIO_ALTITUDE"); - const radioAlt = radioAlt1.isFailureWarning() || radioAlt1.isNoComputedData() ? radioAlt2 : radioAlt1; - const radioAltValid = radioAlt.isNormalOperation(); - const isOnGround = !this.isAirVsGroundMode; - - const isGpwsSysOff = SimVar.GetSimVarValue("L:A32NX_GPWS_SYS_OFF", "Bool") === 1; - const isTerrModeOff = SimVar.GetSimVarValue("L:A32NX_GPWS_TERR_OFF", "Bool") === 1; - const isFlapModeOff = SimVar.GetSimVarValue("L:A32NX_GPWS_FLAP_OFF", "Bool") === 1; - const isLdgFlap3On = SimVar.GetSimVarValue("L:A32NX_GPWS_FLAPS3", "Bool") === 1; - - const sfccPositionWord = Arinc429Word.fromSimVarValue("L:A32NX_SFCC_SLAT_FLAP_ACTUAL_POSITION_WORD"); - const isFlapsFull = sfccPositionWord.bitValueOr(22, false); - const isFlaps3 = sfccPositionWord.bitValueOr(21, false) && !isFlapsFull; - - const areFlapsInLandingConfig = !sfccPositionWord.isNormalOperation() || isFlapModeOff || (isLdgFlap3On ? isFlaps3 : isFlapsFull); - const isGearDownLocked = SimVar.GetSimVarValue("L:A32NX_LGCIU_1_LEFT_GEAR_DOWNLOCKED", "Bool") === 1; - - // TODO only use this in the air? - const isNavAccuracyHigh = SimVar.GetSimVarValue("L:A32NX_FMGC_L_NAV_ACCURACY_HIGH", "Bool") === 1; - const isTcfOperational = this.isTerrainClearanceFloorOperational(isTerrModeOff, radioAlt, isNavAccuracyHigh); - const isTafOperational = this.isTerrainAwarenessOperational(isTerrModeOff); - - this.UpdateAltState(radioAltValid ? radioAlt.value : NaN); - this.differentiate_radioalt(radioAltValid ? radioAlt.value : NaN, deltaTime); - - const mda = SimVar.GetSimVarValue("L:AIRLINER_MINIMUM_DESCENT_ALTITUDE", "feet"); - const dh = SimVar.GetSimVarValue("L:AIRLINER_DECISION_HEIGHT", "feet"); - - this.update_maxRA(radioAlt, isOnGround); - - const isOverflightDetected = this.updateOverflightState(deltaTime); - this.updateMode4aInhibited(isGearDownLocked, areFlapsInLandingConfig); - - this.updateAirGroundState(deltaTime, computedAirspeed, radioAlt, pitch); - this.updateApproachTakeoffState(computedAirspeed, radioAlt, isGearDownLocked, areFlapsInLandingConfig, isTcfOperational, isTafOperational, isOverflightDetected); - - if (!isGpwsSysOff && radioAltValid && radioAlt.value >= 10 && radioAlt.value <= 2450 - ) { //Activate between 10 - 2450 radio alt unless SYS is off - const altRate = this.selectAltitudeRate(inertialVs, barometricVs); - - this.GPWSMode1(this.modes[0], radioAlt.value, altRate); - //Mode 2 is disabled because of an issue with the terrain height simvar which causes false warnings very frequently. See PR#1742 for more info - //this.GPWSMode2(this.modes[1], radioAlt, computedAirspeed, areFlapsInLandingConfig, isGearDownLocked); - this.GPWSMode3(this.modes[2], baroAlt, radioAlt.value, altRate, areFlapsInLandingConfig, isGearDownLocked); - this.GPWSMode4(this.modes[3], radioAlt.value, computedAirspeed, areFlapsInLandingConfig, isGearDownLocked, isTcfOperational, isTafOperational, isOverflightDetected); - this.GPWSMode5(this.modes[4], radioAlt.value); - - } else { - this.modes.forEach((mode) => { - mode.current = 0; - }); - - this.Mode3MaxBaroAlt = NaN; - - this.setGlideSlopeWarning(false); - this.setGpwsWarning(false); - } - - this.GPWSComputeLightsAndCallouts(); - this.gpwsUpdateDiscreteWords(); - - if ((mda !== 0 || (dh !== -1 && dh !== -2) && this.isApproachVsTakeoffState)) { - let minimumsDA; //MDA or DH - let minimumsIA; //radio or baro altitude - if (dh >= 0) { - minimumsDA = dh; - minimumsIA = radioAlt.isNormalOperation() || radioAlt.isFunctionalTest() ? radioAlt.value : NaN; - } else { - minimumsDA = mda; - minimumsIA = baroAlt.isNormalOperation() || baroAlt.isFunctionalTest() ? baroAlt.value : NaN; - } - if (isFinite(minimumsDA) && isFinite(minimumsIA)) { - this.gpws_minimums(minimumsDA, minimumsIA); - } - } - } - - /** - * Takes the derivative of the radio altimeter. Using central difference, to prevent high frequency noise - * @param radioAlt - in feet - * @param deltaTime - in milliseconds - */ - differentiate_radioalt(radioAlt, deltaTime) { - if (!isNaN(this.prevRadioAlt2) && !isNaN(radioAlt)) { - this.RadioAltRate = (radioAlt - this.prevRadioAlt2) / (deltaTime / 1000 / 60) / 2; - this.prevRadioAlt2 = this.prevRadioAlt; - this.prevRadioAlt = radioAlt; - } else if (!isNaN(this.prevRadioAlt) && !isNaN(radioAlt)) { - this.prevRadioAlt2 = this.prevRadioAlt; - this.prevRadioAlt = radioAlt; - } else { - this.prevRadioAlt2 = radioAlt; - } - } - - update_maxRA(radioAlt, isOnGround) { - // on ground check is to get around the fact that radio alt is set to around 300 while loading - if (isOnGround || this.isApproachVsTakeoffState) { - this.Mode4MaxRAAlt = 0; - } else if (radioAlt.isNormalOperation()) { - this.Mode4MaxRAAlt = Math.max(this.Mode4MaxRAAlt, 0.75 * radioAlt.value); - } - } - - gpws_minimums(minimumsDA, minimumsIA) { - let over100Above = false; - let overMinimums = false; - - if (minimumsDA <= 90) { - overMinimums = minimumsIA >= minimumsDA + 15; - over100Above = minimumsIA >= minimumsDA + 115; - } else { - overMinimums = minimumsIA >= minimumsDA + 5; - over100Above = minimumsIA >= minimumsDA + 105; - } - if (this.minimumsState === 0 && overMinimums) { - this.minimumsState = 1; - } else if (this.minimumsState === 1 && over100Above) { - this.minimumsState = 2; - } else if (this.minimumsState === 2 && !over100Above) { - this.core.soundManager.tryPlaySound(soundList.hundred_above); - this.minimumsState = 1; - } else if (this.minimumsState === 1 && !overMinimums) { - this.core.soundManager.tryPlaySound(soundList.minimums); - this.minimumsState = 0; - } - } - - GPWSComputeLightsAndCallouts() { - this.modes.forEach((mode) => { - if (mode.current === mode.previous) { - return; - } - - const previousType = mode.type[mode.previous]; - this.core.soundManager.removePeriodicSound(previousType.sound); - - const currentType = mode.type[mode.current]; - this.core.soundManager.addPeriodicSound(currentType.sound, currentType.soundPeriod); - - if (mode.onChange) { - mode.onChange(mode.current, mode.previous); - } - - mode.previous = mode.current; - }); - - const activeTypes = this.modes.map((mode) => mode.type[mode.current]); - - const shouldPullUpPlay = activeTypes.some((type) => type.pullUp); - if (shouldPullUpPlay !== this.PrevShouldPullUpPlay) { - if (shouldPullUpPlay) { - this.core.soundManager.addPeriodicSound(soundList.pull_up, 1.1); - } else { - this.core.soundManager.removePeriodicSound(soundList.pull_up); - } - this.PrevShouldPullUpPlay = shouldPullUpPlay; - } - - const illuminateGpwsLight = activeTypes.some((type) => type.gpwsLight); - this.setGpwsWarning(illuminateGpwsLight); - } - - /** - * Compute the GPWS Mode 1 state. - * @param mode - The mode object which stores the state. - * @param radioAlt - Radio altitude in feet - * @param vSpeed - Vertical speed, in feet/min, NaN if not available - */ - GPWSMode1(mode, radioAlt, vSpeed) { - const sinkrate = -vSpeed; - - if (!Number.isFinite(sinkrate) || sinkrate <= 1000) { - mode.current = 0; - return; - } - - const maxSinkrateAlt = 0.61 * sinkrate - 600; - const maxPullUpAlt = sinkrate < 1700 ? 1.3 * sinkrate - 1940 : 0.4 * sinkrate - 410; - - if (radioAlt <= maxPullUpAlt) { - mode.current = 2; - } else if (radioAlt <= maxSinkrateAlt) { - mode.current = 1; - } else { - mode.current = 0; - } - } - - /** - * Compute the GPWS Mode 2 state. - * @param mode - The mode object which stores the state. - * @param radioAlt - Radio altitude in feet - * @param computedAirspeed - Arinc429 value of the computed airspeed in knots. - * @param areFlapsInLandingConfig - If flaps is in landing config - * @param gearExtended - If the gear is deployed - */ - GPWSMode2(mode, radioAlt, computedAirspeed, areFlapsInLandingConfig, gearExtended) { - if (!computedAirspeed.isNormalOperation()) { - return false; - } - - let IsInBoundary = false; - const UpperBoundaryRate = -this.RadioAltRate < 3500 ? 0.7937 * -this.RadioAltRate - 1557.5 : 0.19166 * -this.RadioAltRate + 610; - const UpperBoundarySpeed = Math.max(1650, Math.min(2450, 8.8888 * computedAirspeed.value - 305.555)); - - if (!areFlapsInLandingConfig && -this.RadioAltRate > 2000) { - if (radioAlt < UpperBoundarySpeed && radioAlt < UpperBoundaryRate) { - this.Mode2NumFramesInBoundary += 1; - } else { - this.Mode2NumFramesInBoundary = 0; - } - } else if (areFlapsInLandingConfig && -this.RadioAltRate > 2000) { - if (radioAlt < 775 && radioAlt < UpperBoundaryRate && -this.RadioAltRate < 10000) { - this.Mode2NumFramesInBoundary += 1; - } else { - this.Mode2NumFramesInBoundary = 0; - } - } - // This is to prevent very quick changes in radio alt rate triggering the alarm. The derivative is sadly pretty jittery. - if (this.Mode2NumFramesInBoundary > 5) { - IsInBoundary = true; - } - - if (IsInBoundary) { - this.Mode2BoundaryLeaveAlt = -1; - if (this.Mode2NumTerrain < 2 || gearExtended) { - if (this.core.soundManager.tryPlaySound(soundList.too_low_terrain)) { // too low terrain is not correct, but no "terrain" call yet - this.Mode2NumTerrain += 1; - } - mode.current = 1; - } else if (!gearExtended) { - mode.current = 2; - } - } else if (this.Mode2BoundaryLeaveAlt === -1) { - this.Mode2BoundaryLeaveAlt = radioAlt; - } else if (this.Mode2BoundaryLeaveAlt + 300 > radioAlt) { - mode.current = 1; - this.core.soundManager.tryPlaySound(soundList.too_low_terrain); - } else if (this.Mode2BoundaryLeaveAlt + 300 <= radioAlt) { - mode.current = 0; - this.Mode2NumTerrain = 0; - this.Mode2BoundaryLeaveAlt = NaN; - } - } - - /** - * Compute the GPWS Mode 3 state. - * @param {*} mode - The mode object which stores the state. - * @param {*} baroAlt - Arinc429 value of the barometric altitude in feet. - * @param {*} radioAlt - Radio altitude in feet - * @param {*} altRate - Altitude rate in feet per minute - * @param {*} areFlapsInLandingConfig - True if flaps is in landing config - * @param {*} isGearDownLocked - True if the gear is down and locked - */ - GPWSMode3(mode, baroAlt, radioAlt, altRate, areFlapsInLandingConfig, isGearDownLocked) { - if ((isGearDownLocked && areFlapsInLandingConfig) || this.isApproachVsTakeoffState || radioAlt > 1500 || radioAlt < 10 || !baroAlt.isNormalOperation()) { - this.Mode3MaxBaroAlt = NaN; - mode.current = 0; - return; - } - - const maxAltLoss = 0.09 * radioAlt + 7.1; - const hasPositiveAltRate = Number.isFinite(altRate) && altRate > 0; - - if (baroAlt.value > this.Mode3MaxBaroAlt || isNaN(this.Mode3MaxBaroAlt)) { - this.Mode3MaxBaroAlt = baroAlt.value; - mode.current = 0; - } else if (!hasPositiveAltRate && (this.Mode3MaxBaroAlt - baroAlt.value) > maxAltLoss) { - mode.current = 1; - } else { - mode.current = 0; - } - } - - /** - * Compute the GPWS Mode 4 state. - * @param mode - The mode object which stores the state. - * @param radioAlt - Radio altitude in feet - * @param computedAirspeed - ARINC value of the computed airspeed in knots. - * @param areFlapsInLandingConfig - If flaps is in landing config - * @param gearExtended - If the gear is extended - * @param tcfOperational - If the terrain clearance floor is operational - * @param tafOperational - If the terrain awareness floor is operational - * @param isOverflightDetected - If an overflight is detected - * @constructor - */ - GPWSMode4(mode, radioAlt, computedAirspeed, areFlapsInLandingConfig, gearExtended, tcfOperational, tafOperational, isOverflightDetected) { - mode.current = 0; - - if (!computedAirspeed.isNormalOperation() || radioAlt < 30 || radioAlt > 1000 || !this.isAirVsGroundMode) { - return; - } - - const isMode4cEnabled = !this.isApproachVsTakeoffState && (!areFlapsInLandingConfig || !gearExtended) && this.isAirVsGroundMode; - - // Mode 4 A - if (this.isApproachVsTakeoffState && !gearExtended && !this.isMode4aInhibited) { - const boundary = this.mode4aUpperBoundary(computedAirspeed.value, areFlapsInLandingConfig, tcfOperational, tafOperational, isOverflightDetected); - - if (computedAirspeed.value < 190 && radioAlt < 500) { - mode.current = 1; // TOO LOW GEAR - } else if (computedAirspeed.value >= 190 && radioAlt < boundary) { - mode.current = areFlapsInLandingConfig ? 1 : 3; // TOO LOW GEAR or TOO LOW TERRAIN - } - // Mode 4 B - } else if (this.isApproachVsTakeoffState && (!areFlapsInLandingConfig || !gearExtended)) { - // Normal 4b mode, flaps down, what is the boundary? - const boundary = this.mode4bUpperBoundary(computedAirspeed.value, areFlapsInLandingConfig, tcfOperational, tafOperational, isOverflightDetected); - - if (computedAirspeed.value < 159 && radioAlt < 245) { - mode.current = !gearExtended ? 1 : 2; // TOO LOW GEAR or TOO LOW FLAPS - } else if (computedAirspeed.value >= 159 && radioAlt < boundary) { - mode.current = this.isMode4aInhibited ? 1 : 3; // TOO LOW GEAR or TOO LOW TERRAIN - } - // Mode 4 C - } else if (isMode4cEnabled) { - const maximumFloor = this.mode4cUpperBoundary(computedAirspeed.value); - const maximumFilterValue = Math.min(maximumFloor, this.Mode4MaxRAAlt); - - if (radioAlt < maximumFilterValue) { - mode.current = 3; - } - } - } - - /** - * Compute the GPWS Mode 5 state. - * @param mode - The mode object which stores the state. - * @param - radioAlt Radio altitude in feet - * @constructor - */ - GPWSMode5(mode, radioAlt) { - if (radioAlt > 1000 || radioAlt < 30 || SimVar.GetSimVarValue("L:A32NX_GPWS_GS_OFF", "Bool")) { - mode.current = 0; - return; - } - - // FIXME add backcourse inhibit - if (!SimVar.GetSimVarValue('L:A32NX_RADIO_RECEIVER_GS_IS_VALID', 'number')) { - mode.current = 0; - return; - } - const error = SimVar.GetSimVarValue('L:A32NX_RADIO_RECEIVER_GS_DEVIATION', 'number'); - const dots = -error * 2.5; //According to the FCOM, one dot is approx. 0.4 degrees. 1/0.4 = 2.5 - - const minAltForWarning = dots < 2.9 ? -75 * dots + 247.5 : 30; - const minAltForHardWarning = dots < 3.8 ? -66.66 * dots + 283.33 : 30; - - if (dots > 2 && radioAlt > minAltForHardWarning && radioAlt < 350) { - mode.current = 2; - } else if (dots > 1.3 && radioAlt > minAltForWarning) { - mode.current = 1; - } else { - mode.current = 0; - } - } - - UpdateAltState(radioAlt) { - if (isNaN(radioAlt)) { - return; - } - switch (this.AltCallState.value) { - case "ground": - if (radioAlt > 6) { - this.AltCallState.action("up"); - } - break; - case "over5": - if (radioAlt > 12) { - this.AltCallState.action("up"); - } else if (radioAlt <= 6) { - if (this.RetardState.value !== "retardPlaying" && (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Five)) { - this.core.soundManager.tryPlaySound(soundList.alt_5); - } - this.AltCallState.action("down"); - } - break; - case "over10": - if (radioAlt > 22) { - this.AltCallState.action("up"); - } else if (radioAlt <= 12) { - if (this.RetardState.value !== "retardPlaying" && (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Ten)) { - this.core.soundManager.tryPlaySound(soundList.alt_10); - } - this.AltCallState.action("down"); - } - break; - case "over20": - if (radioAlt > 32) { - this.AltCallState.action("up"); - } else if (radioAlt <= 22) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Twenty) { - this.core.soundManager.tryPlaySound(soundList.alt_20); - } - this.AltCallState.action("down"); - } - break; - case "over30": - if (radioAlt > 42) { - this.AltCallState.action("up"); - } else if (radioAlt <= 32) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Thirty) { - this.core.soundManager.tryPlaySound(soundList.alt_30); - } - this.AltCallState.action("down"); - } - break; - case "over40": - if (radioAlt > 53) { - this.AltCallState.action("up"); - } else if (radioAlt <= 42) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Forty) { - this.core.soundManager.tryPlaySound(soundList.alt_40); - } - this.AltCallState.action("down"); - } - break; - case "over50": - if (radioAlt > 110) { - this.AltCallState.action("up"); - } else if (radioAlt <= 53) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Fifty) { - this.core.soundManager.tryPlaySound(soundList.alt_50); - } - this.AltCallState.action("down"); - } - break; - case "over100": - if (radioAlt > 210) { - this.AltCallState.action("up"); - } else if (radioAlt <= 110) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.OneHundred) { - this.core.soundManager.tryPlaySound(soundList.alt_100); - } - this.AltCallState.action("down"); - } - break; - case "over200": - if (radioAlt > 310) { - this.AltCallState.action("up"); - } else if (radioAlt <= 210) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.TwoHundred) { - this.core.soundManager.tryPlaySound(soundList.alt_200); - } - this.AltCallState.action("down"); - } - break; - case "over300": - if (radioAlt > 410) { - this.AltCallState.action("up"); - } else if (radioAlt <= 310) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.ThreeHundred) { - this.core.soundManager.tryPlaySound(soundList.alt_300); - } - this.AltCallState.action("down"); - } - break; - case "over400": - if (radioAlt > 513) { - this.AltCallState.action("up"); - } else if (radioAlt <= 410) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.FourHundred) { - this.core.soundManager.tryPlaySound(soundList.alt_400); - } - this.AltCallState.action("down"); - } - break; - case "over500": - if (radioAlt > 1020) { - this.AltCallState.action("up"); - } else if (radioAlt <= 513) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.FiveHundred) { - this.core.soundManager.tryPlaySound(soundList.alt_500); - } - this.AltCallState.action("down"); - } - break; - case "over1000": - if (radioAlt > 2020) { - this.AltCallState.action("up"); - } else if (radioAlt <= 1020) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.OneThousand) { - this.core.soundManager.tryPlaySound(soundList.alt_1000); - } - this.AltCallState.action("down"); - } - break; - case "over2000": - if (radioAlt > 2530) { - this.AltCallState.action("up"); - } else if (radioAlt <= 2020) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.TwoThousand) { - this.core.soundManager.tryPlaySound(soundList.alt_2000); - } - this.AltCallState.action("down"); - } - break; - case "over2500": - if (radioAlt <= 2530) { - if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.TwoThousandFiveHundred) { - this.core.soundManager.tryPlaySound(soundList.alt_2500); - } else if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.TwentyFiveHundred) { - this.core.soundManager.tryPlaySound(soundList.alt_2500b); - } - this.AltCallState.action("down"); - } - break; - } - - switch (this.RetardState.value) { - case "overRetard": - if (radioAlt < 20) { - if (!SimVar.GetSimVarValue("L:A32NX_AUTOPILOT_ACTIVE", "Bool")) { - this.RetardState.action("play"); - this.core.soundManager.addPeriodicSound(soundList.retard, 1.1); - } else if (radioAlt < 10) { - this.RetardState.action("play"); - this.core.soundManager.addPeriodicSound(soundList.retard, 1.1); - } - } - break; - case "retardPlaying": - if (SimVar.GetSimVarValue("L:A32NX_AUTOTHRUST_TLA:1", "number") < 2.6 || SimVar.GetSimVarValue("L:A32NX_AUTOTHRUST_TLA:2", "number") < 2.6) { - this.RetardState.action("land"); - this.core.soundManager.removePeriodicSound(soundList.retard); - } else if (SimVar.GetSimVarValue("L:A32NX_FMGC_FLIGHT_PHASE", "Enum") === FmgcFlightPhases.GOAROUND || radioAlt > 20) { - this.RetardState.action("go_around"); - this.core.soundManager.removePeriodicSound(soundList.retard); - } - break; - case "landed": - if (radioAlt > 20) { - this.RetardState.action("up"); - } - break; - } - } - - updateAirGroundState(deltaTime, computedAirspeed, radioAlt, pitchAngle) { - if (!computedAirspeed.isNormalOperation() || !radioAlt.isNormalOperation()) { - // Stay in current state - return; - } - - this.airborneFor5s.write(computedAirspeed.value > 90 && radioAlt.value > 25 && pitchAngle.isNormalOperation() && pitchAngle.value > 5, deltaTime); - this.airborneFor10s.write(computedAirspeed.value > 90 && radioAlt.value > 25, deltaTime); - - if (this.isAirVsGroundMode) { - if (radioAlt.value < 25) { - this.isAirVsGroundMode = false; - SimVar.SetSimVarValue("L:A32NX_GPWS_GROUND_STATE", "Bool", 1); - } - } else { - if (this.airborneFor5s.read() || this.airborneFor5s.read()) { - this.isAirVsGroundMode = true; - SimVar.SetSimVarValue("L:A32NX_GPWS_GROUND_STATE", "Bool", 0); - } - } - } - - updateApproachTakeoffState(computedAirspeed, radioAlt, isGearDown, areFlapsInLandingConfig, tcfOperational, tafOperational, isOverflightDetected) { - // TODO: what do we do if computedAirspeed is not NO? - if (!computedAirspeed.isNormalOperation()) { - return; - } - - if (this.isApproachVsTakeoffState) { - // Switch to TO if we pass below 245 ft mode 4b floor without an alert (i.e gear down and flaps in landing config) - if (radioAlt.isNormalOperation() && radioAlt.value < 245 && isGearDown && areFlapsInLandingConfig) { - this.isApproachVsTakeoffState = false; - SimVar.SetSimVarValue("L:A32NX_GPWS_APPROACH_STATE", "Bool", 0); - } - } else { - const isFirstAlgorithmSatisfied = false; - // - 4C filter value exceeds 4A alert boundary - const isSecondAlgorithmSatisfied = this.Mode4MaxRAAlt > this.mode4aUpperBoundary(computedAirspeed.value, areFlapsInLandingConfig, tcfOperational, tafOperational, isOverflightDetected); - - if ((isFirstAlgorithmSatisfied || !this.isAudioDeclutterEnabled) && isSecondAlgorithmSatisfied) { - this.isApproachVsTakeoffState = true; - SimVar.SetSimVarValue("L:A32NX_GPWS_APPROACH_STATE", "Bool", 1); - } - } - } - - mode4aUpperBoundary(computedAirspeed, areFlapsInLandingConfig, tcfOperational, tafOperational, isOverflightDetected) { - let expandedBoundary = 1000; - if (areFlapsInLandingConfig || tcfOperational || tafOperational) { - expandedBoundary = 500; - } else if (isOverflightDetected) { - expandedBoundary = 800; - } - - return Math.max(500, Math.min(expandedBoundary, 8.333 * computedAirspeed - 1083.33)); - } - - mode4bUpperBoundary(computedAirspeed, areFlapsInLandingConfig, tcfOperational, tafOperational, isOverflightDetected) { - let expandedBoundary = 1000; - if (areFlapsInLandingConfig || tcfOperational || tafOperational) { - expandedBoundary = 245; - } else if (isOverflightDetected) { - expandedBoundary = 800; - } - - return Math.max(245, Math.min(expandedBoundary, 8.333 * computedAirspeed - 1083.33)); - } - - mode4cUpperBoundary(computedAirspeed) { - return Math.max(500, Math.min(1000, 8.3333 * computedAirspeed.value - 1083.33)); - } - - updateOverflightState(deltaTime) { - // Need -2200 ft/s to trigger an overflight state - return this.isOverflightDetected.write(this.RadioAltRate < -2200 * 60, deltaTime); - } - - isTerrainClearanceFloorOperational(terrPbOff, radioAlt, fmcNavAccuracyHigh) { - // TODO when ground speed is below 60 kts, always consider fms nav accuracy high - // where does GS come from? - return this.isTerrainAwarenessEnabled && !terrPbOff && radioAlt.isNormalOperation() && fmcNavAccuracyHigh; - } - - isTerrainAwarenessOperational(terrPbOff) { - // TODO replace placeholders - const doesTerrainAwarenessUseGeometricAltitude = true; - const isGeometricAltitudeVfomValid = true; - const isGeometricAltitudeHilValid = true; - const geometricAltitudeVfom = 0; - const isRaimFailureDetected = false; - - return this.isTerrainAwarenessEnabled - && !terrPbOff && - !doesTerrainAwarenessUseGeometricAltitude && - isGeometricAltitudeVfomValid && - isGeometricAltitudeHilValid && - !isRaimFailureDetected && - geometricAltitudeVfom < 200; - } - - updateMode4aInhibited(isGearDownLocked, isFlapsInLandingConfig) { - if (this.isMode4aInhibited) { - if (!this.isAirVsGroundMode || !this.isApproachVsTakeoffState) { - // Reset - this.isMode4aInhibited = false; - } - } else if (this.isAlternateMode4bEnabled) { - if (isGearDownLocked || isFlapsInLandingConfig) { - this.isMode4aInhibited = true; - } - } - } - - selectAltitudeRate(inertialVs, baroVs) { - if (inertialVs.isNormalOperation()) { - return inertialVs.value; - } else if (Number.isFinite(this.RadioAltRate)) { - return this.RadioAltRate; - } else if (baroVs.isNormalOperation()) { - return baroVs.value; - } - - return NaN; - } -} - -const RetardStateMachine = { - overRetard: { - transitions: { - play: { - target: "retardPlaying" - } - } - }, - retardPlaying: { - transitions: { - land: { - target: "landed" - }, - go_around: { - target: "overRetard" - } - } - }, - landed: { - transitions: { - up: { - target: "overRetard" - } - } - } -}; - -const AltCallStateMachine = { - init: "ground", - over2500: { - transitions: { - down: { - target: "over2000" - } - } - }, - over2000: { - transitions: { - down: { - target: "over1000" - }, - up: { - target: "over2500", - } - } - }, - over1000: { - transitions: { - down: { - target: "over500" - }, - up: { - target: "over2000" - } - } - }, - over500: { - transitions: { - down: { - target: "over400" - }, - up: { - target: "over1000" - } - } - }, - over400: { - transitions: { - down: { - target: "over300" - }, - up: { - target: "over500" - } - } - }, - over300: { - transitions: { - down: { - target: "over200" - }, - up: { - target: "over400" - } - } - }, - over200: { - transitions: { - down: { - target: "over100" - }, - up: { - target: "over300" - } - } - }, - over100: { - transitions: { - down: { - target: "over50" - }, - up: { - target: "over200" - } - } - }, - over50: { - transitions: { - down: { - target: "over40" - }, - up: { - target: "over100" - } - } - }, - over40: { - transitions: { - down: { - target: "over30" - }, - up: { - target: "over50" - } - } - }, - over30: { - transitions: { - down: { - target: "over20" - }, - up: { - target: "over40" - } - } - }, - over20: { - transitions: { - down: { - target: "over10" - }, - up: { - target: "over30" - } - } - }, - over10: { - transitions: { - down: { - target: "over5" - }, - up: { - target: "over20" - } - } - }, - over5: { - transitions: { - down: { - target: "ground" - }, - up: { - target: "over10" - } - } - }, - ground: { - transitions: { - up: { - target: "over5" - } - } - } -}; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_LocalVarUpdater.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_LocalVarUpdater.js deleted file mode 100644 index c3386c7ab73..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_LocalVarUpdater.js +++ /dev/null @@ -1,105 +0,0 @@ -// Use this to create and sync local simvars that are derived from other simvars. -// To create and sync a new local simvar, you need to add a selector and an updater. -// The selector calculates the new value based on other simvars and some logic. -// The updater compares the new value from the selector with the current value from the local simvar, -// and then updates the local simvar if it changed. - -const FLAPS_IN_MOTION_MIN_DELTA = 0.1; - -class A32NX_LocalVarUpdater { - constructor() { - // Initial data for deltas - this.lastFlapsPosition = SimVar.GetSimVarValue("L:A32NX_LEFT_FLAPS_POSITION_PERCENT", "Percent"); - // track which compartment has gotten temperature initialization - this.initializedCabinTemp = { - "CKPT":false, - "FWD":false, - "AFT":false - }; - - this.updaters = [ - { - varName: "L:A32NX_NO_SMOKING_MEMO", - type: "Bool", - selector: this._noSmokingMemoSelector, - refreshInterval: 1000, - }, - { - varName: "L:A32NX_FLAPS_IN_MOTION", - type: "Bool", - selector: this._flapsInMotionSelector.bind(this), - refreshInterval: 50, - }, - { - varName: "L:A32NX_SLIDES_ARMED", - type: "Bool", - selector: this._areSlidesArmed.bind(this), - refreshInterval: 100, - }, - // New updaters go here... - ]; - - this.updaterThrottlers = {}; - this.updaters.forEach((updater) => { - this.updaterThrottlers[updater.varName] = new UpdateThrottler(updater.refreshInterval); - }); - } - - update(deltaTime) { - this.updaters.forEach(updater => this._runUpdater(deltaTime, updater)); - } - - _runUpdater(deltaTime, {varName, type, selector, identifier = null}) { - const selectorDeltaTime = this.updaterThrottlers[varName].canUpdate(deltaTime); - - if (selectorDeltaTime === -1) { - return; - } - - const newValue = selector(selectorDeltaTime, identifier); - const currentValue = SimVar.GetSimVarValue(varName, type); - - if (newValue !== currentValue) { - SimVar.SetSimVarValue(varName, type, newValue); - } - } - - _noSmokingMemoSelector() { - const gearPercent = SimVar.GetSimVarValue("L:A32NX_GEAR_CENTER_POSITION", "Percent"); - const noSmokingSwitch = SimVar.GetSimVarValue("L:XMLVAR_SWITCH_OVHD_INTLT_NOSMOKING_Position", "Position"); - - // Switch is ON - if (noSmokingSwitch === 0) { - return true; - } - - // Switch is AUTO and gear more than 50% down - if (noSmokingSwitch === 1 && gearPercent > 50) { - return true; - } - - return false; - } - - _flapsInMotionSelector() { - const currentFlapsPosition = SimVar.GetSimVarValue("L:A32NX_LEFT_FLAPS_POSITION_PERCENT", "Percent"); - const lastFlapsPosition = this.lastFlapsPosition; - - this.lastFlapsPosition = currentFlapsPosition; - - return Math.abs(lastFlapsPosition - currentFlapsPosition) > FLAPS_IN_MOTION_MIN_DELTA; - } - - _areSlidesArmed() { - - return !SimVar.GetSimVarValue('SIM ON GROUND', 'bool') || - SimVar.GetSimVarValue('ON ANY RUNWAY', 'bool') || - (SimVar.GetSimVarValue('LIGHT BEACON ON', 'bool') && - SimVar.GetSimVarValue('INTERACTIVE POINT OPEN:0', 'percent') < 5 && // Pilot side front door for ramp/stairs - SimVar.GetSimVarValue('INTERACTIVE POINT OPEN:3', 'percent') < 5 && // Rear door, FO side for catering - SimVar.GetSimVarValue('L:A32NX_FWD_DOOR_CARGO_LOCKED', 'bool') // Cargo door FO side - ); - } - - // New selectors go here... -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_PayloadManager.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_PayloadManager.js deleted file mode 100644 index bebc158c024..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_PayloadManager.js +++ /dev/null @@ -1,133 +0,0 @@ -/* eslint-disable no-undef */ -class A32NX_PayloadConstructor { - constructor() { - this.paxStations = { - rows1_6: { - name: 'ROWS [1-6]', - seats: 36, - weight: Math.round(NXUnits.kgToUser(3024)), - stationIndex: 0 + 1, - position: 20.5, - simVar: "A32NX_PAX_A" - }, - rows7_13: { - name: 'ROWS [7-13]', - seats: 42, - weight: Math.round(NXUnits.kgToUser(3530)), - stationIndex: 1 + 1, - position: 1.5, - simVar: "A32NX_PAX_B" - }, - rows14_21: { - name: 'ROWS [14-21]', - seats: 48, - weight: Math.round(NXUnits.kgToUser(4032)), - stationIndex: 2 + 1, - position: -16.6, - simVar: "A32NX_PAX_C" - }, - rows22_29: { - name: 'ROWS [22-29]', - seats: 48, - weight: Math.round(NXUnits.kgToUser(4032)), - stationIndex: 3 + 1, - position: -35.6, - simVar: "A32NX_PAX_D" - }, - }; - - this.cargoStations = { - fwdBag: { - name: 'FWD BAGGAGE/CONTAINER', - weight: Math.round(NXUnits.kgToUser(3402)), - load: 0, - stationIndex: 4 + 1, - position: 17.3, - visible: true, - simVar: 'A32NX_CARGO_FWD_BAGGAGE_CONTAINER', - }, - aftCont: { - name: 'AFT CONTAINER', - weight: Math.round(NXUnits.kgToUser(2426)), - load: 0, - stationIndex: 5 + 1, - position: -24.1, - visible: true, - simVar: 'A32NX_CARGO_AFT_CONTAINER', - }, - aftBag: { - name: 'AFT BAGGAGE', - weight: Math.round(NXUnits.kgToUser(2110)), - load: 0, - stationIndex: 6 + 1, - position: -34.1, - visible: true, - simVar: 'A32NX_CARGO_AFT_BAGGAGE', - }, - aftBulk: { - name: 'AFT BULK/LOOSE', - weight: Math.round(NXUnits.kgToUser(1497)), - load: 0, - stationIndex: 7 + 1, - position: -42.4, - visible: true, - simVar: 'A32NX_CARGO_AFT_BULK_LOOSE', - }, - }; - } -} - -const payloadConstruct = new A32NX_PayloadConstructor(); -const paxStations = payloadConstruct.paxStations; -const cargoStations = payloadConstruct.cargoStations; -const MAX_SEAT_AVAILABLE = 174; - -/** - * Calculate %MAC ZWFCG of all stations - */ -function getZfwcg() { - - const leMacZ = -5.383; // Accurate to 3 decimals, replaces debug weight values - const macSize = 13.464; // Accurate to 3 decimals, replaces debug weight values - - const emptyWeight = (SimVar.GetSimVarValue("EMPTY WEIGHT", getUserUnit())); - const emptyPosition = -9.42; // Value from flight_model.cfg - const emptyMoment = emptyPosition * emptyWeight; - const PAX_WEIGHT = SimVar.GetSimVarValue("L:A32NX_WB_PER_PAX_WEIGHT", "Number"); - - const paxTotalMass = Object.values(paxStations).map((station) => new BitFlags(SimVar.GetSimVarValue(`L:${station.simVar}`, "Number")).getTotalBits() * PAX_WEIGHT).reduce((acc, cur) => acc + cur, 0); - const paxTotalMoment = Object.values(paxStations).map((station) => new BitFlags(SimVar.GetSimVarValue(`L:${station.simVar}`, "Number")).getTotalBits() * PAX_WEIGHT * station.position).reduce((acc, cur) => acc + cur, 0); - - const cargoTotalMass = Object.values(cargoStations).map((station) => SimVar.GetSimVarValue(`PAYLOAD STATION WEIGHT:${station.stationIndex}`, getUserUnit())).reduce((acc, cur) => acc + cur, 0); - const cargoTotalMoment = Object.values(cargoStations).map((station) => (SimVar.GetSimVarValue(`PAYLOAD STATION WEIGHT:${station.stationIndex}`, getUserUnit()) * station.position)).reduce((acc, cur) => acc + cur, 0); - - const totalMass = emptyWeight + paxTotalMass + cargoTotalMass; - const totalMoment = emptyMoment + paxTotalMoment + cargoTotalMoment; - - const cgPosition = totalMoment / totalMass; - const cgPositionToLemac = cgPosition - leMacZ; - const cgPercentMac = -100 * (cgPositionToLemac / macSize); - - return cgPercentMac; -} - -function getTotalCargo() { - const cargoTotalMass = Object.values(cargoStations).filter((station) => station.visible).map((station) => SimVar.GetSimVarValue(`PAYLOAD STATION WEIGHT:${station.stationIndex}`, getUserUnit())).reduce((acc, cur) => acc + cur, 0); - return cargoTotalMass; -} - -function getTotalPayload() { - const paxTotalMass = Object.values(paxStations).map((station) => SimVar.GetSimVarValue(`PAYLOAD STATION WEIGHT:${station.stationIndex}`, getUserUnit())).reduce((acc, cur) => acc + cur, 0); - const cargoTotalMass = getTotalCargo(); - return paxTotalMass + cargoTotalMass; -} - -function getZfw() { - const emptyWeight = (SimVar.GetSimVarValue("EMPTY WEIGHT", getUserUnit())); - return emptyWeight + getTotalPayload(); -} - -function getUserUnit() { - const defaultUnit = (NXUnits.userWeightUnit() == "KG") ? "Kilograms" : "Pounds"; - return defaultUnit; -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Refuel.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Refuel.js deleted file mode 100644 index 84e907678ba..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Refuel.js +++ /dev/null @@ -1,178 +0,0 @@ -// Copyright (c) 2021-2024 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -const WING_FUELRATE_GAL_SEC = 4.01; -const CENTER_MODIFIER = 0.4528; - -class A32NX_Refuel { - constructor() {} - - init() { - const totalFuelGallons = 6267; - const fuelWeight = SimVar.GetSimVarValue("FUEL WEIGHT PER GALLON", "kilograms"); - const centerCurrentSimVar = SimVar.GetSimVarValue("FUEL TANK CENTER QUANTITY", "Gallons"); - const LInnCurrentSimVar = SimVar.GetSimVarValue("FUEL TANK LEFT MAIN QUANTITY", "Gallons"); - const LOutCurrentSimVar = SimVar.GetSimVarValue("FUEL TANK LEFT AUX QUANTITY", "Gallons"); - const RInnCurrentSimVar = SimVar.GetSimVarValue("FUEL TANK RIGHT MAIN QUANTITY", "Gallons"); - const ROutCurrentSimVar = SimVar.GetSimVarValue("FUEL TANK RIGHT AUX QUANTITY", "Gallons"); - const total = Math.round(Math.max((LInnCurrentSimVar + (LOutCurrentSimVar) + (RInnCurrentSimVar) + (ROutCurrentSimVar) + (centerCurrentSimVar)), 0)); - const totalConverted = Math.round(NXUnits.kgToUser(total * fuelWeight)); - SimVar.SetSimVarValue("L:A32NX_REFUEL_STARTED_BY_USR", "Bool", false); - SimVar.SetSimVarValue("L:A32NX_FUEL_TOTAL_DESIRED", "Number", total); - SimVar.SetSimVarValue("L:A32NX_FUEL_DESIRED", "Number", totalConverted); // TODO this looks sus... should not be user units in simvars - SimVar.SetSimVarValue("L:A32NX_FUEL_DESIRED_PERCENT", "Number", Math.round((total / totalFuelGallons) * 100)); - SimVar.SetSimVarValue("L:A32NX_FUEL_CENTER_DESIRED", "Number", centerCurrentSimVar); - SimVar.SetSimVarValue("L:A32NX_FUEL_LEFT_MAIN_DESIRED", "Number", LInnCurrentSimVar); - SimVar.SetSimVarValue("L:A32NX_FUEL_LEFT_AUX_DESIRED", "Number", LOutCurrentSimVar); - SimVar.SetSimVarValue("L:A32NX_FUEL_RIGHT_MAIN_DESIRED", "Number", RInnCurrentSimVar); - SimVar.SetSimVarValue("L:A32NX_FUEL_RIGHT_AUX_DESIRED", "Number", ROutCurrentSimVar); - } - - defuelTank(multiplier) { - return -WING_FUELRATE_GAL_SEC * multiplier; - } - refuelTank(multiplier) { - return WING_FUELRATE_GAL_SEC * multiplier; - } - - update(_deltaTime) { - const refuelStartedByUser = SimVar.GetSimVarValue("L:A32NX_REFUEL_STARTED_BY_USR", "Bool"); - const gsxFuelHose = SimVar.GetSimVarValue("L:FSDT_GSX_FUELHOSE_CONNECTED", "Number"); - if (!refuelStartedByUser && gsxFuelHose == 0) { - return; - } - if (!refuelStartedByUser && gsxFuelHose == 1) { - SimVar.SetSimVarValue("L:A32NX_REFUEL_STARTED_BY_USR", "Bool", true); - return; - } - const busDC2 = SimVar.GetSimVarValue("L:A32NX_ELEC_DC_2_BUS_IS_POWERED", "Bool"); - const busDCHot1 = SimVar.GetSimVarValue("L:A32NX_ELEC_DC_HOT_1_BUS_IS_POWERED", "Bool"); - const gs = SimVar.GetSimVarValue("GPS GROUND SPEED", "knots"); - const onGround = SimVar.GetSimVarValue("SIM ON GROUND", "Bool"); - const eng1Running = SimVar.GetSimVarValue("ENG COMBUSTION:1", "Bool"); - const eng2Running = SimVar.GetSimVarValue("ENG COMBUSTION:2", "Bool"); - const refuelRate = NXDataStore.get("REFUEL_RATE_SETTING", "0"); // default = real - if (refuelRate !== '2') { - if (!onGround || eng1Running || eng2Running || gs > 0.1 || (!busDC2 && !busDCHot1)) { - return; - } - } - const centerTargetSimVar = SimVar.GetSimVarValue("L:A32NX_FUEL_CENTER_DESIRED", "Number"); - const LInnTargetSimVar = SimVar.GetSimVarValue("L:A32NX_FUEL_LEFT_MAIN_DESIRED", "Number"); - const LOutTargetSimVar = SimVar.GetSimVarValue("L:A32NX_FUEL_LEFT_AUX_DESIRED", "Number"); - const RInnTargetSimVar = SimVar.GetSimVarValue("L:A32NX_FUEL_RIGHT_MAIN_DESIRED", "Number"); - const ROutTargetSimVar = SimVar.GetSimVarValue("L:A32NX_FUEL_RIGHT_AUX_DESIRED", "Number"); - const centerCurrentSimVar = SimVar.GetSimVarValue("FUEL TANK CENTER QUANTITY", "Gallons"); - const LInnCurrentSimVar = SimVar.GetSimVarValue("FUEL TANK LEFT MAIN QUANTITY", "Gallons"); - const LOutCurrentSimVar = SimVar.GetSimVarValue("FUEL TANK LEFT AUX QUANTITY", "Gallons"); - const RInnCurrentSimVar = SimVar.GetSimVarValue("FUEL TANK RIGHT MAIN QUANTITY", "Gallons"); - const ROutCurrentSimVar = SimVar.GetSimVarValue("FUEL TANK RIGHT AUX QUANTITY", "Gallons"); - let centerCurrent = centerCurrentSimVar; - let LInnCurrent = LInnCurrentSimVar; - let LOutCurrent = LOutCurrentSimVar; - let RInnCurrent = RInnCurrentSimVar; - let ROutCurrent = ROutCurrentSimVar; - const centerTarget = centerTargetSimVar; - const LInnTarget = LInnTargetSimVar; - const LOutTarget = LOutTargetSimVar; - const RInnTarget = RInnTargetSimVar; - const ROutTarget = ROutTargetSimVar; - if (refuelRate == '2') { // instant - SimVar.SetSimVarValue("FUEL TANK CENTER QUANTITY", "Gallons", centerTarget); - SimVar.SetSimVarValue("FUEL TANK LEFT MAIN QUANTITY", "Gallons", LInnTarget); - SimVar.SetSimVarValue("FUEL TANK LEFT AUX QUANTITY", "Gallons", LOutTarget); - SimVar.SetSimVarValue("FUEL TANK RIGHT MAIN QUANTITY", "Gallons", RInnTarget); - SimVar.SetSimVarValue("FUEL TANK RIGHT AUX QUANTITY", "Gallons", ROutTarget); - } else { - let multiplier = 1; - if (refuelRate == '1') { // fast - multiplier = 5; - } - multiplier *= _deltaTime / 1000; - //DEFUELING (center tank first, then main, then aux) - if (centerCurrent > centerTarget) { - centerCurrent += this.defuelTank(multiplier) * CENTER_MODIFIER; - if (centerCurrent < centerTarget) { - centerCurrent = centerTarget; - } - SimVar.SetSimVarValue("FUEL TANK CENTER QUANTITY", "Gallons", centerCurrent); - } - if (LInnCurrent > LInnTarget || RInnCurrent > RInnTarget) { - LInnCurrent += this.defuelTank(multiplier) / 2; - RInnCurrent += this.defuelTank(multiplier) / 2; - if (LInnCurrent < LInnTarget) { - LInnCurrent = LInnTarget; - } - if (RInnCurrent < RInnTarget) { - RInnCurrent = RInnTarget; - } - SimVar.SetSimVarValue("FUEL TANK RIGHT MAIN QUANTITY", "Gallons", RInnCurrent); - SimVar.SetSimVarValue("FUEL TANK LEFT MAIN QUANTITY", "Gallons", LInnCurrent); - if (LInnCurrent != LInnTarget || RInnCurrent != RInnTarget) { - return; - } - } - if (LOutCurrent > LOutTarget || ROutCurrent > ROutTarget) { - LOutCurrent += this.defuelTank(multiplier) / 2; - ROutCurrent += this.defuelTank(multiplier) / 2; - if (LOutCurrent < LOutTarget) { - LOutCurrent = LOutTarget; - } - if (ROutCurrent < ROutTarget) { - ROutCurrent = ROutTarget; - } - SimVar.SetSimVarValue("FUEL TANK RIGHT AUX QUANTITY", "Gallons", ROutCurrent); - SimVar.SetSimVarValue("FUEL TANK LEFT AUX QUANTITY", "Gallons", LOutCurrent); - if (LOutCurrent != LOutTarget || ROutCurrent != ROutTarget) { - return; - } - } - // REFUELING (aux first, then main, then center tank) - if (centerCurrent < centerTarget) { - centerCurrent += this.refuelTank(multiplier) * CENTER_MODIFIER; - if (centerCurrent > centerTarget) { - centerCurrent = centerTarget; - } - SimVar.SetSimVarValue("FUEL TANK CENTER QUANTITY", "Gallons", centerCurrent); - } - if (LOutCurrent < LOutTarget || ROutCurrent < ROutTarget) { - LOutCurrent += this.refuelTank(multiplier) / 2; - ROutCurrent += this.refuelTank(multiplier) / 2; - if (LOutCurrent > LOutTarget) { - LOutCurrent = LOutTarget; - } - if (ROutCurrent > ROutTarget) { - ROutCurrent = ROutTarget; - } - SimVar.SetSimVarValue("FUEL TANK RIGHT AUX QUANTITY", "Gallons", ROutCurrent); - SimVar.SetSimVarValue("FUEL TANK LEFT AUX QUANTITY", "Gallons", LOutCurrent); - if (LOutCurrent != LOutTarget || ROutCurrent != ROutTarget) { - return; - } - } - if (LInnCurrent < LInnTarget || RInnCurrent < RInnTarget) { - LInnCurrent += this.refuelTank(multiplier) / 2; - RInnCurrent += this.refuelTank(multiplier) / 2; - if (LInnCurrent > LInnTarget) { - LInnCurrent = LInnTarget; - } - if (RInnCurrent > RInnTarget) { - RInnCurrent = RInnTarget; - } - SimVar.SetSimVarValue("FUEL TANK RIGHT MAIN QUANTITY", "Gallons", RInnCurrent); - SimVar.SetSimVarValue("FUEL TANK LEFT MAIN QUANTITY", "Gallons", LInnCurrent); - if (LInnCurrent != LInnTarget || RInnCurrent != RInnTarget) { - return; - } - } - - } - - if (centerCurrent == centerTarget && LInnCurrent == LInnTarget && LOutCurrent == LOutTarget && RInnCurrent == RInnTarget && ROutCurrent == ROutTarget) { - // DONE FUELING - SimVar.SetSimVarValue("L:A32NX_REFUEL_STARTED_BY_USR", "Bool", false); - SimVar.SetSimVarValue("L:FSDT_GSX_FUELHOSE_CONNECTED", "Number", 0); - } - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_SoundManager.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_SoundManager.js deleted file mode 100644 index 038dd069446..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_SoundManager.js +++ /dev/null @@ -1,190 +0,0 @@ -class PeriodicSound { - constructor(sound, period) { - this.sound = sound; - this.period = period; - this.timeSinceLastPlayed = NaN; - } -} - -class A32NX_SoundManager { - constructor() { - this.periodicList = []; - - this.playingSound = null; - this.playingSoundRemaining = NaN; - } - - addPeriodicSound(sound, period = NaN) { - if (!sound) { - return; - } - - let useLengthForPeriod = false; - if (period < sound.length) { - console.error("A32NXSoundManager ERROR: Sound period can't be smaller than sound length. Using sound length instead."); - useLengthForPeriod = true; - } - - let found = false; - this.periodicList.forEach((element) => { - if (element.sound.name === sound.name) { - found = true; - } - }); - - if (!found) { - this.periodicList.push(new PeriodicSound(sound, useLengthForPeriod ? sound.length : period)); - } - } - - removePeriodicSound(sound) { - if (!sound) { - return; - } - - for (let i = 0; i < this.periodicList.length; i++) { - if (this.periodicList[i].sound.name === sound.name) { - this.periodicList.splice(i, 1); - } - } - } - - tryPlaySound(sound, retry = false, repeatOnce = false) { - if (this.playingSound === null) { - this.playingSound = sound; - this.playingSoundRemaining = sound.length; - - Coherent.call("PLAY_INSTRUMENT_SOUND", sound.name).catch(console.error); - if (repeatOnce) { - this.soundQueue.push(sound); - } - return true; - } else if (retry) { - this.soundQueue.push(sound); - if (repeatOnce) { - this.soundQueue.push(sound); - } - return false; - } - return false; - } - - update(deltaTime, _core) { - if (this.playingSoundRemaining <= 0) { - this.playingSound = null; - this.playingSoundRemaining = NaN; - } else if (this.playingSoundRemaining > 0) { - this.playingSoundRemaining -= deltaTime / 1000; - } - - this.periodicList.forEach((element) => { - if (isNaN(element.timeSinceLastPlayed) || element.timeSinceLastPlayed >= element.period) { - if (this.tryPlaySound(element.sound)) { - element.timeSinceLastPlayed = 0; - } - } else { - element.timeSinceLastPlayed += deltaTime / 1000; - } - }); - } -} - -// many lengths are approximate until we can get them accuratly (when boris re-makes them and we have the sources) -const soundList = { - pull_up: { - name: "aural_pullup_new", - length: 0.9 - }, - sink_rate: { - name: "aural_sink_rate_new", - length: 0.9 - }, - dont_sink:{ - name: "aural_dontsink_new", - length: 0.9 - }, - too_low_gear:{ - name: "aural_too_low_gear", - length: 0.8 - }, - too_low_flaps:{ - name: "aural_too_low_flaps", - length: 0.8 - }, - too_low_terrain: { - name: "aural_too_low_terrain", - length: 0.9 - }, - minimums: { - name: "aural_minimumnew", - length: 0.67 - }, - hundred_above: { - name: "aural_100above", - length: 0.72 - }, - retard: { - name: "new_retard", - length: 0.9 - }, - alt_2500: { - name: "new_2500", - length: 1.1 - }, - alt_2500b: { - name: "new_2_500", - length: 1.047, - }, - alt_2000: { - name: "new_2000", - length: 0.72, - }, - alt_1000: { - name: "new_1000", - length: 0.9 - }, - alt_500: { - name: "new_500", - length: 0.6 - }, - alt_400: { - name: "new_400", - length: 0.6 - }, - alt_300: { - name: "new_300", - length: 0.6 - }, - alt_200: { - name: "new_200", - length: 0.6 - }, - alt_100: { - name: "new_100", - length: 0.6 - }, - alt_50: { - name: "new_50", - length: 0.4 - }, - alt_40: { - name: "new_40", - length: 0.4 - }, - alt_30: { - name: "new_30", - length: 0.4 - }, - alt_20: { - name: "new_20", - length: 0.4 - }, - alt_10: { - name: "new_10", - length: 0.3 - }, - alt_5: { - name: "new_5", - length: 0.3 - } -}; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Speeds.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Speeds.js deleted file mode 100644 index d6c043cc206..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Speeds.js +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Calculates and shares Vs, Vls, F, S and GD. - */ -class A32NX_Speeds { - constructor() { - console.log('A32NX_VSPEEDS constructed'); - } - - init() { - console.log('A32NX_VSPEEDS init'); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_VS", "number", 0); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_VLS", "number", 0); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_F", "number", 0); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_S", "number", 0); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_GD", "number", 0); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_LANDING_CONF3", "boolean", 0); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_VMAX", "number", 0); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_VFEN", "number", 0); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_ALPHA_PROTECTION_CALC", "number", 0); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_ALPHA_MAX_CALC", "number", 0); - this.lastGw = 50; - this.lastFhi = -1; - this.curFhi = -1; - this.ldgPos = -1; - this.alt = -1; - this.cgw = 0; - this.isTo = false; - } - - update() { - const fp = SimVar.GetSimVarValue("L:A32NX_FMGC_FLIGHT_PHASE", "Enum"); - let fhi = SimVar.GetSimVarValue("L:A32NX_FLAPS_HANDLE_INDEX", "Enum"); - /** Using true fhi for comparison */ - const isTo = fhi === SimVar.GetSimVarValue("L:A32NX_TO_CONFIG_FLAPS", "number"); - /** Change fhi to differentiate between 1 and 1 + F */ - if (fhi === 1 && SimVar.GetSimVarValue("L:A32NX_FLAPS_CONF_INDEX", "Enum") === 1) { - fhi = 5; - } - const fmGW = parseFloat((SimVar.GetSimVarValue("L:A32NX_FM_GROSS_WEIGHT", "Number")).toFixed(1)); - const gw = (fmGW === 0) ? 64.3 : fmGW; // MZFW = 64300KG - const ldg = Math.round(SimVar.GetSimVarValue("GEAR POSITION:0", "Enum")); - const alt = this.round(Simplane.getAltitude()); - - if (fhi === this.lastFhi && gw === this.lastGw && ldg === this.ldgPos && alt === this.alt && isTo === this.isTo) { - return; - } - - /** During Take Off allow to change this.isTo - * Otherwise if we are in take off config and change the fhi, we no longer are in take off config */ - if (fp === FmgcFlightPhases.TAKEOFF && Simplane.getAltitudeAboveGround() < 1.5) { - this.isTo = isTo; - } else if (this.isTo && this.lastFhi !== fhi) { - this.isTo = false; - } - - this.lastFhi = fhi; - this.lastGw = gw; - this.cgw = Math.ceil(((gw > 80 ? 80 : gw) - 40) / 5); - this.ldgPos = ldg; - this.alt = alt; - - const speeds = new NXSpeeds(gw, this.lastFhi, ldg, this.isTo); - speeds.compensateForMachEffect(alt); - - SimVar.SetSimVarValue("L:A32NX_SPEEDS_VS", "number", speeds.vs); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_VLS", "number", speeds.vls); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_F", "number", speeds.f); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_S", "number", speeds.s); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_GD", "number", speeds.gd); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_VMAX", "number", speeds.vmax); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_VFEN", "number", speeds.vfeN); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_ALPHA_PROTECTION_CALC", "number", speeds.vs * 1.1); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_ALPHA_MAX_CALC", "number", speeds.vs * 1.03); - } - - /** - * Math.round(x / r) * r - * @param x {number} number to be rounded - * @param r {number} precision - * @returns {number} rounded number - */ - round(x, r = 100) { - return Math.round(x / r) * r; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_TipsManager.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_TipsManager.js deleted file mode 100644 index 9b16500ffc6..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_TipsManager.js +++ /dev/null @@ -1,81 +0,0 @@ -class A32NX_TipsManager { - /* private */constructor() { - this.notif = new NXNotifManager(); - this.checkThrottleCalibration(); - this.updateThrottler = new UpdateThrottler(15000); - this.wasAnyAssistanceActive = false; - } - - static get instance() { - if (!__tipsManager) { - __tipsManager = new A32NX_TipsManager(); - } - return __tipsManager; - } - - update(deltaTime) { - if (this.updateThrottler.canUpdate(deltaTime) !== -1) { - this.checkAssistenceConfiguration(); - } - } - - checkThrottleCalibration() { - let once = false; - let input = Math.round(SimVar.GetSimVarValue("L:A32NX_THROTTLE_MAPPING_INPUT:1", "Number") * 100) / 100; - - const throttleConfig = SimVar.GetSimVarValue("L:A32NX_THROTTLE_MAPPING_LOADED_CONFIG:1", "Boolean"); - - if (!throttleConfig) { - const checkThrottle = setInterval(() => { - if (SimVar.GetSimVarValue("L:A32NX_THROTTLE_MAPPING_LOADED_CONFIG:1", "Boolean")) { - clearInterval(checkThrottle); - return; - } - if (input === Math.round(SimVar.GetSimVarValue("L:A32NX_THROTTLE_MAPPING_INPUT:1", "Number") * 100) / 100) { - const fwcFlightPhase = SimVar.GetSimVarValue("L:A32NX_FWC_FLIGHT_PHASE", "Enum"); - const atPhase = SimVar.GetSimVarValue("L:A32NX_AUTOTHRUST_MODE_MESSAGE", "Enum"); - const clbLow = Math.round(SimVar.GetSimVarValue("L:A32NX_THROTTLE_MAPPING_CLIMB_LOW:1", "Number") * 100) / 100; - const clbHigh = Math.round(SimVar.GetSimVarValue("L:A32NX_THROTTLE_MAPPING_CLIMB_HIGH:1", "Number") * 100) / 100; - - // If thrust lever is within 0.03 range of the default CLB detent limits in CRZ and A/THR MODE is LVR CLB - if ((input >= (clbLow - 0.03) && input <= (clbHigh + 0.03)) && fwcFlightPhase === 6 && atPhase === 3) { - (!once) ? once = true : this.notif.showNotification({message: "Please calibrate your throttles in the flyPad tablet (EFB). Potentially inaccurate throttle calibration has been detected.", timeout: 60000}); - } else { - once = false; - } - } else { - input = Math.round(SimVar.GetSimVarValue("L:A32NX_THROTTLE_MAPPING_INPUT:1", "Number") * 100) / 100; - } - }, 300000); - } - } - - checkAssistenceConfiguration() { - // only check when actually flying, otherwise return - if (SimVar.GetSimVarValue("L:A32NX_IS_READY", "Number") !== 1) { - this.wasAnyAssistanceActive = false; - return; - } - - // determine if any assistance is active - const assistanceAiControls = SimVar.GetSimVarValue("AI CONTROLS", "Bool"); - const assistanceTakeOffEnabled = SimVar.GetSimVarValue("ASSISTANCE TAKEOFF ENABLED", "Bool"); - const assistanceLandingEnabled = SimVar.GetSimVarValue("ASSISTANCE LANDING ENABLED", "Bool"); - const assistanceAutotrimActive = SimVar.GetSimVarValue("AI AUTOTRIM ACTIVE", "Bool"); - const isAnyAssistanceActive = (assistanceAiControls || assistanceTakeOffEnabled || assistanceLandingEnabled || assistanceAutotrimActive); - - // show popup when an enabled assistance is detected and it was not active before - if (!this.wasAnyAssistanceActive && isAnyAssistanceActive) { - this.notif.showNotification({message: "Ensure you have turned off all assistance functions:\n\n• AUTO-RUDDER\n• ASSISTED YOKE\n• ASSISTED LANDING\n• ASSISTED TAKEOFF\n• AI ANTI-STALL PROTECTION\n• AI AUTO-TRIM\n• ASSISTED CONTROLLER SENSITIVITY\n\nThey cause serious incompatibility!", timeout: 15000}); - } - - // remember if any assistance was active - this.wasAnyAssistanceActive = isAnyAssistanceActive; - } - - showNavRadioTuningTip() { - this.notif.showNotification({message: "Navigation radio tuning is not possible while the FMGC controls the radios:\n\n• tune via the MCDU RADIO NAV page, or\n• press the NAV button on the RMP to enable manual tuning.", timeout: 15000}); - } -} - -let __tipsManager; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/Adirs.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/Adirs.js deleted file mode 100644 index c7935841b31..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/Adirs.js +++ /dev/null @@ -1,78 +0,0 @@ -class ADIRS { - static getNdSupplier(displayIndex, knobValue) { - const adirs3ToCaptain = 0; - const adirs3ToFO = 2; - - if (this.isCaptainSide(displayIndex)) { - return knobValue === adirs3ToCaptain ? 3 : 1; - } - return knobValue === adirs3ToFO ? 3 : 2; - } - - static getNdInertialReferenceSource(displayIndex) { - return ADIRS.getNdSupplier(displayIndex, SimVar.GetSimVarValue('L:A32NX_ATT_HDG_SWITCHING_KNOB', 'Enum')); - } - - static getNdAirDataReferenceSource(displayIndex) { - return ADIRS.getNdSupplier(displayIndex, SimVar.GetSimVarValue('L:A32NX_AIR_DATA_SWITCHING_KNOB', 'Enum')); - } - - static isCaptainSide(displayIndex) { - return displayIndex === 1; - } - - static mapNotAvailable(displayIndex) { - const inertialReferenceSource = ADIRS.getNdInertialReferenceSource(displayIndex); - return !Arinc429Word.fromSimVarValue(`L:A32NX_ADIRS_IR_${inertialReferenceSource}_LATITUDE`).isNormalOperation() || - !Arinc429Word.fromSimVarValue(`L:A32NX_ADIRS_IR_${inertialReferenceSource}_LONGITUDE`).isNormalOperation(); - } - - static getLatitude() { - return ADIRS.getFromAnyAdiru('IR', 'LATITUDE'); - } - - static getLongitude() { - return ADIRS.getFromAnyAdiru('IR', 'LONGITUDE'); - } - - static getTrueTrack() { - return ADIRS.getFromAnyAdiru('IR', 'TRUE_TRACK'); - } - - static getTrueAirspeed() { - return ADIRS.getFromAnyAdiru('ADR', 'TRUE_AIRSPEED'); - } - - static getCalibratedAirspeed() { - return ADIRS.getFromAnyAdiru('ADR', 'COMPUTED_AIRSPEED'); - } - - static getGroundSpeed() { - return ADIRS.getFromAnyAdiru('IR', 'GROUND_SPEED'); - } - - // FIXME there should be baro corrected altitude 1 (capt) and 2 (f/o) - static getBaroCorrectedAltitude() { - return ADIRS.getFromAnyAdiru('ADR', 'ALTITUDE'); - } - - /** - * - * @param {'IR' | 'ADR'} type IR or ADR - * @param {string} param the name of the param - * @returns {Arinc429Word} - */ - static getFromAnyAdiru(type, param) { - // In the real aircraft, FMGC 1 is supplied by ADIRU 1, and FMGC 2 by ADIRU 2. When any is unavailable - // the FMGC switches to ADIRU 3. If only one ADIRU is available, both FMGCs use the same ADIRU. - // As we don't have a split FMGC, we'll just use the following code for now. - const fromAdiru = 1; - const toAdiru = 3; - for (let adiruNumber = fromAdiru; adiruNumber <= toAdiru; adiruNumber++) { - const arincWord = Arinc429Word.fromSimVarValue(`L:A32NX_ADIRS_${type}_${adiruNumber}_${param}`); - if (arincWord.isNormalOperation() || adiruNumber === toAdiru) { - return arincWord; - } - } - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/DisplayUnit.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/DisplayUnit.js deleted file mode 100644 index 549d395a640..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/DisplayUnit.js +++ /dev/null @@ -1,101 +0,0 @@ -class DisplayUnit { - constructor(rootElement, isPoweredFn, getSelfTestTimeInSecondsFn, potentiometerId, selfTestElement) { - this.rootElement = rootElement; - this.selfTest = new DisplayUnitSelfTest(selfTestElement, getSelfTestTimeInSecondsFn); - this.isPowered = isPoweredFn; - this.potentiometerId = potentiometerId; - this.offDurationTimerActive = SimVar.GetSimVarValue('L:A32NX_COLD_AND_DARK_SPAWN', 'Bool'); - this.previouslyOff = false; - // Start with a state where turning on the display unit within 10 seconds after starting the flight - // will trigger the self test. - this.offDurationInMilliseconds = this.offDurationTimerActive ? DisplayUnitSelfTest.RequiredAfterBeingOffForMilliseconds : 0; - if (this.offDurationTimerActive) { - this.selfTest.execute(this.offDurationInMilliseconds); - } - } - - isJustNowTurnedOn() { - return this.previouslyOff && this.isOn(); - } - - isOn() { - const brightness = SimVar.GetSimVarValue(`LIGHT POTENTIOMETER:${this.potentiometerId}`, "number"); - return brightness >= 0.01 && this.isPowered(); - } - - update(deltaTime) { - const isOn = this.isOn(); - - if (this.isJustNowTurnedOn()) { - this.selfTest.execute(this.offDurationInMilliseconds); - } - - if (!isOn) { - this.selfTest.interrupt(); - } else { - this.selfTest.update(deltaTime); - } - - if (this.offDurationTimerActive) { - if (isOn) { - this.offDurationInMilliseconds = 0; - } else { - this.offDurationInMilliseconds += deltaTime; - } - } else { - // on non c&d spawn in - if (isOn) { - this.offDurationTimerActive = true; // normal ops - } - } - - this.rootElement.style.display = isOn ? "block" : "none"; - - this.previouslyOff = !isOn; - } -} - -class DisplayUnitSelfTest { - constructor(element, getSelfTestTimeInSecondsFn) { - this.element = element; - this.getSelfTestTimeInSeconds = getSelfTestTimeInSecondsFn; - this.maxTimer = this.getSelfTestTimeInSeconds() * 1000; - this.init = false; - - this.remainingTestDurationInMilliseconds = 0; - } - - static get RequiredAfterBeingOffForMilliseconds() { - return 10000; - } - - isRequired(offDurationInMilliseconds) { - return offDurationInMilliseconds >= DisplayUnitSelfTest.RequiredAfterBeingOffForMilliseconds; - } - - execute(offDurationInMilliseconds) { - if (this.isRequired(offDurationInMilliseconds)) { - this.remainingTestDurationInMilliseconds = this.maxTimer; - } - } - - interrupt() { - if (this.remainingTestDurationInMilliseconds > 0 && this.remainingTestDurationInMilliseconds < this.maxTimer) { - this.remainingTestDurationInMilliseconds = this.maxTimer; - } - } - - update(deltaTime) { - if (this.remainingTestDurationInMilliseconds > 0) { - if (this.init) { - this.remainingTestDurationInMilliseconds -= deltaTime; - } else { - this.init = true; - } - this.element.style.visibility = "visible"; - } else { - - this.element.style.visibility = "hidden"; - } - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/FMGC/A32NX_MessageQueue.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/FMGC/A32NX_MessageQueue.js deleted file mode 100644 index c8f01565659..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/FMGC/A32NX_MessageQueue.js +++ /dev/null @@ -1,79 +0,0 @@ -class A32NX_MessageQueue { - constructor(fmgc) { - this._fmgc = fmgc; - this._queue = []; - } - - /** - * Fmgc messages enter the queue via this void - * @param message {TypeIIMessage} - */ - addMessage(message) { - if (message.isResolved(this._fmgc)) { - this.updateDisplayedMessage(); - return; - } - - this._addToQueueOrUpdateQueuePosition(message); - this.updateDisplayedMessage(); - } - - removeMessage(value) { - for (let i = 0; i < this._queue.length; i++) { - const message = this._queue[i]; - if (message.text === value) { - message.onClear(this._fmgc); - this._queue.splice(i, 1); - if (i === 0) { - if (this._fmgc.fmgcScratchpad) { - this._fmgc.fmgcScratchpad.removeMessage(value); - } - this.updateDisplayedMessage(); - } - break; - } - } - } - - resetQueue() { - this._queue = []; - } - - updateDisplayedMessage() { - if (this._queue.length > 0) { - const message = this._queue[0]; - if (message.isResolved(this._fmgc)) { - this._queue.splice(0, 1); - return this.updateDisplayedMessage(); - } - - if (this._fmgc.fmgcScratchpad) { - this._fmgc.fmgcScratchpad.setMessage(message); - } - } - } - - _addToQueueOrUpdateQueuePosition(message) { - for (let i = 0; i < this._queue.length; i++) { - if (this._queue[i].text === message.text) { - if (i !== 0) { - this._queue.unshift(this._queue[i]); - this._queue.splice(i + 1, 1); - } - return; - } - } - - for (let i = 0; i < this._queue.length; i++) { - if (this._queue[i].isResolved(this._fmgc)) { - this._queue.splice(i, 1); - } - } - - this._queue.unshift(message); - - if (this._queue.length > 5) { - this._queue.splice(5, 1); - } - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/README.md b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/README.md deleted file mode 100644 index 8ed019b1d23..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# A32NX Core - -This is where we want to put logic that should not be tied to a screen. - -It is currently tied to A320_Neo_CDU_MainDisplay.js update, but if we find a better place for it we will move it. - -We will be figuring out how we want to structure things in this folder as we go. - -Please reach out to the core team before starting work on changes to any of these files while we figure things out. diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/math.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/math.js deleted file mode 100644 index 09e58f9507f..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/math.js +++ /dev/null @@ -1,62176 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -/* eslint no-use-before-define: 0 */ -/** - * math.js - * https://github.com/josdejong/mathjs - * - * Math.js is an extensive math library for JavaScript and Node.js, - * It features real and complex numbers, units, matrices, a large set of - * mathematical functions, and a flexible expression parser. - * - * @version 7.5.0 - * @date 2020-10-07 - * - * @license - * Copyright (C) 2013-2020 Jos de Jong - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy - * of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -(function webpackUniversalModuleDefinition(root, factory) { - if (typeof exports === 'object' && typeof module === 'object') { - module.exports = factory(); - } else if (typeof define === 'function' && define.amd) { - define([], factory); - } else if (typeof exports === 'object') { - exports["math"] = factory(); - } else { - root["math"] = factory(); - } -})(this, function () { - return /******/ (function (modules) { // webpackBootstrap - /******/ // The module cache - /******/ var installedModules = {}; - /******/ - /******/ // The require function - /******/ function __webpack_require__(moduleId) { - /******/ - /******/ // Check if module is in cache - /******/ if (installedModules[moduleId]) { - /******/ return installedModules[moduleId].exports; - /******/ } - /******/ // Create a new module (and put it into the cache) - /******/ var module = installedModules[moduleId] = { - /******/ i: moduleId, - /******/ l: false, - /******/ exports: {} - /******/ }; - /******/ - /******/ // Execute the module function - /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); - /******/ - /******/ // Flag the module as loaded - /******/ module.l = true; - /******/ - /******/ // Return the exports of the module - /******/ return module.exports; - /******/ } - /******/ - /******/ - /******/ // expose the modules object (__webpack_modules__) - /******/ __webpack_require__.m = modules; - /******/ - /******/ // expose the module cache - /******/ __webpack_require__.c = installedModules; - /******/ - /******/ // define getter function for harmony exports - /******/ __webpack_require__.d = function (exports, name, getter) { - /******/ if (!__webpack_require__.o(exports, name)) { - /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); - /******/ } - /******/ }; - /******/ - /******/ // define __esModule on exports - /******/ __webpack_require__.r = function (exports) { - /******/ if (typeof Symbol !== 'undefined' && Symbol.toStringTag) { - /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); - /******/ } - /******/ Object.defineProperty(exports, '__esModule', { value: true }); - /******/ }; - /******/ - /******/ // create a fake namespace object - /******/ // mode & 1: value is a module id, require it - /******/ // mode & 2: merge all properties of value into the ns - /******/ // mode & 4: return value when already ns object - /******/ // mode & 8|1: behave like require - /******/ __webpack_require__.t = function (value, mode) { - /******/ if (mode & 1) { - value = __webpack_require__(value); - } - /******/ if (mode & 8) { - return value; - } - /******/ if ((mode & 4) && typeof value === 'object' && value && value.__esModule) { - return value; - } - /******/ var ns = Object.create(null); - /******/ __webpack_require__.r(ns); - /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); - /******/ if (mode & 2 && typeof value != 'string') { - for (var key in value) { - __webpack_require__.d(ns, key, function (key) { - return value[key]; - }.bind(null, key)); - } - } - /******/ return ns; - /******/ }; - /******/ - /******/ // getDefaultExport function for compatibility with non-harmony modules - /******/ __webpack_require__.n = function (module) { - /******/ var getter = module && module.__esModule ? - /******/ function getDefault() { - return module['default']; - } : - /******/ function getModuleExports() { - return module; - }; - /******/ __webpack_require__.d(getter, 'a', getter); - /******/ return getter; - /******/ }; - /******/ - /******/ // Object.prototype.hasOwnProperty.call - /******/ __webpack_require__.o = function (object, property) { - return Object.prototype.hasOwnProperty.call(object, property); - }; - /******/ - /******/ // __webpack_public_path__ - /******/ __webpack_require__.p = ""; - /******/ - /******/ - /******/ // Load entry module and return exports - /******/ return __webpack_require__(__webpack_require__.s = 18); - /******/ }) - /************************************************************************/ - /******/ ([ - /* 0 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function () { - return factory; - }); - /* unused harmony export sortFactories */ - /* unused harmony export create */ - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function () { - return isFactory; - }); - /* unused harmony export assertDependencies */ - /* unused harmony export isOptionalDependency */ - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function () { - return stripOptionalNotation; - }); - /* harmony import */ var _array__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2); - /* harmony import */ var _object__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3); - - /** - * Create a factory function, which can be used to inject dependencies. - * - * The created functions are memoized, a consecutive call of the factory - * with the exact same inputs will return the same function instance. - * The memoized cache is exposed on `factory.cache` and can be cleared - * if needed. - * - * Example: - * - * const name = 'log' - * const dependencies = ['config', 'typed', 'divideScalar', 'Complex'] - * - * export const createLog = factory(name, dependencies, ({ typed, config, divideScalar, Complex }) => { - * // ... create the function log here and return it - * } - * - * @param {string} name Name of the function to be created - * @param {string[]} dependencies The names of all required dependencies - * @param {function} create Callback function called with an object with all dependencies - * @param {Object} [meta] Optional object with meta information that will be attached - * to the created factory function as property `meta`. - * @returns {function} - */ - - function factory(name, dependencies, create, meta) { - function assertAndCreate(scope) { - // we only pass the requested dependencies to the factory function - // to prevent functions to rely on dependencies that are not explicitly - // requested. - var deps = Object(_object__WEBPACK_IMPORTED_MODULE_1__[/* pickShallow */ "j"])(scope, dependencies.map(stripOptionalNotation)); - assertDependencies(name, dependencies, scope); - return create(deps); - } - - assertAndCreate.isFactory = true; - assertAndCreate.fn = name; - assertAndCreate.dependencies = dependencies.slice().sort(); - - if (meta) { - assertAndCreate.meta = meta; - } - - return assertAndCreate; - } - /** - * Sort all factories such that when loading in order, the dependencies are resolved. - * - * @param {Array} factories - * @returns {Array} Returns a new array with the sorted factories. - */ - - function sortFactories(factories) { - var factoriesByName = {}; - factories.forEach(function (factory) { - factoriesByName[factory.fn] = factory; - }); - - function containsDependency(factory, dependency) { - // TODO: detect circular references - if (isFactory(factory)) { - if (Object(_array__WEBPACK_IMPORTED_MODULE_0__[/* contains */ "b"])(factory.dependencies, dependency.fn || dependency.name)) { - return true; - } - - if (factory.dependencies.some(function (d) { - return containsDependency(factoriesByName[d], dependency); - })) { - return true; - } - } - - return false; - } - - var sorted = []; - - function addFactory(factory) { - var index = 0; - - while (index < sorted.length && !containsDependency(sorted[index], factory)) { - index++; - } - - sorted.splice(index, 0, factory); - } // sort regular factory functions - - factories.filter(isFactory).forEach(addFactory); // sort legacy factory functions AFTER the regular factory functions - - factories.filter(function (factory) { - return !isFactory(factory); - }).forEach(addFactory); - return sorted; - } // TODO: comment or cleanup if unused in the end - - function create(factories) { - var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - sortFactories(factories).forEach(function (factory) { - return factory(scope); - }); - return scope; - } - /** - * Test whether an object is a factory. This is the case when it has - * properties name, dependencies, and a function create. - * @param {*} obj - * @returns {boolean} - */ - - function isFactory(obj) { - return typeof obj === 'function' && typeof obj.fn === 'string' && Array.isArray(obj.dependencies); - } - /** - * Assert that all dependencies of a list with dependencies are available in the provided scope. - * - * Will throw an exception when there are dependencies missing. - * - * @param {string} name Name for the function to be created. Used to generate a useful error message - * @param {string[]} dependencies - * @param {Object} scope - */ - - function assertDependencies(name, dependencies, scope) { - var allDefined = dependencies.filter(function (dependency) { - return !isOptionalDependency(dependency); - }) // filter optionals - .every(function (dependency) { - return scope[dependency] !== undefined; - }); - - if (!allDefined) { - var missingDependencies = dependencies.filter(function (dependency) { - return scope[dependency] === undefined; - }); // TODO: create a custom error class for this, a MathjsError or something like that - - throw new Error("Cannot create function \"".concat(name, "\", ") + "some dependencies are missing: ".concat(missingDependencies.map(function (d) { - return "\"".concat(d, "\""); - }).join(', '), ".")); - } - } - function isOptionalDependency(dependency) { - return dependency && dependency[0] === '?'; - } - function stripOptionalNotation(dependency) { - return dependency && dependency[0] === '?' ? dependency.slice(1) : dependency; - } - - /***/ }), - /* 1 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "y", function () { - return isNumber; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "e", function () { - return isBigNumber; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "j", function () { - return isComplex; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "o", function () { - return isFraction; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "L", function () { - return isUnit; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "I", function () { - return isString; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function () { - return isArray; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "v", function () { - return isMatrix; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "i", function () { - return isCollection; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "n", function () { - return isDenseMatrix; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "H", function () { - return isSparseMatrix; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "D", function () { - return isRange; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "t", function () { - return isIndex; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "g", function () { - return isBoolean; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "G", function () { - return isResultSet; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "s", function () { - return isHelp; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "p", function () { - return isFunction; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "m", function () { - return isDate; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "F", function () { - return isRegExp; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "z", function () { - return isObject; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "x", function () { - return isNull; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "K", function () { - return isUndefined; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function () { - return isAccessorNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function () { - return isArrayNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "d", function () { - return isAssignmentNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "f", function () { - return isBlockNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "k", function () { - return isConditionalNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "l", function () { - return isConstantNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "q", function () { - return isFunctionAssignmentNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "r", function () { - return isFunctionNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "u", function () { - return isIndexNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "w", function () { - return isNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "A", function () { - return isObjectNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "B", function () { - return isOperatorNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "C", function () { - return isParenthesisNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "E", function () { - return isRangeNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "J", function () { - return isSymbolNode; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "h", function () { - return isChain; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "M", function () { - return typeOf; - }); - function _typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - _typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - _typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return _typeof(obj); - } - - // type checks for all known types - // - // note that: - // - // - check by duck-typing on a property like `isUnit`, instead of checking instanceof. - // instanceof cannot be used because that would not allow to pass data from - // one instance of math.js to another since each has it's own instance of Unit. - // - check the `isUnit` property via the constructor, so there will be no - // matches for "fake" instances like plain objects with a property `isUnit`. - // That is important for security reasons. - // - It must not be possible to override the type checks used internally, - // for security reasons, so these functions are not exposed in the expression - // parser. - function isNumber(x) { - return typeof x === 'number'; - } - function isBigNumber(x) { - return x && x.constructor.prototype.isBigNumber === true || false; - } - function isComplex(x) { - return x && _typeof(x) === 'object' && Object.getPrototypeOf(x).isComplex === true || false; - } - function isFraction(x) { - return x && _typeof(x) === 'object' && Object.getPrototypeOf(x).isFraction === true || false; - } - function isUnit(x) { - return x && x.constructor.prototype.isUnit === true || false; - } - function isString(x) { - return typeof x === 'string'; - } - var isArray = Array.isArray; - function isMatrix(x) { - return x && x.constructor.prototype.isMatrix === true || false; - } - /** - * Test whether a value is a collection: an Array or Matrix - * @param {*} x - * @returns {boolean} isCollection - */ - - function isCollection(x) { - return Array.isArray(x) || isMatrix(x); - } - function isDenseMatrix(x) { - return x && x.isDenseMatrix && x.constructor.prototype.isMatrix === true || false; - } - function isSparseMatrix(x) { - return x && x.isSparseMatrix && x.constructor.prototype.isMatrix === true || false; - } - function isRange(x) { - return x && x.constructor.prototype.isRange === true || false; - } - function isIndex(x) { - return x && x.constructor.prototype.isIndex === true || false; - } - function isBoolean(x) { - return typeof x === 'boolean'; - } - function isResultSet(x) { - return x && x.constructor.prototype.isResultSet === true || false; - } - function isHelp(x) { - return x && x.constructor.prototype.isHelp === true || false; - } - function isFunction(x) { - return typeof x === 'function'; - } - function isDate(x) { - return x instanceof Date; - } - function isRegExp(x) { - return x instanceof RegExp; - } - function isObject(x) { - return !!(x && _typeof(x) === 'object' && x.constructor === Object && !isComplex(x) && !isFraction(x)); - } - function isNull(x) { - return x === null; - } - function isUndefined(x) { - return x === undefined; - } - function isAccessorNode(x) { - return x && x.isAccessorNode === true && x.constructor.prototype.isNode === true || false; - } - function isArrayNode(x) { - return x && x.isArrayNode === true && x.constructor.prototype.isNode === true || false; - } - function isAssignmentNode(x) { - return x && x.isAssignmentNode === true && x.constructor.prototype.isNode === true || false; - } - function isBlockNode(x) { - return x && x.isBlockNode === true && x.constructor.prototype.isNode === true || false; - } - function isConditionalNode(x) { - return x && x.isConditionalNode === true && x.constructor.prototype.isNode === true || false; - } - function isConstantNode(x) { - return x && x.isConstantNode === true && x.constructor.prototype.isNode === true || false; - } - function isFunctionAssignmentNode(x) { - return x && x.isFunctionAssignmentNode === true && x.constructor.prototype.isNode === true || false; - } - function isFunctionNode(x) { - return x && x.isFunctionNode === true && x.constructor.prototype.isNode === true || false; - } - function isIndexNode(x) { - return x && x.isIndexNode === true && x.constructor.prototype.isNode === true || false; - } - function isNode(x) { - return x && x.isNode === true && x.constructor.prototype.isNode === true || false; - } - function isObjectNode(x) { - return x && x.isObjectNode === true && x.constructor.prototype.isNode === true || false; - } - function isOperatorNode(x) { - return x && x.isOperatorNode === true && x.constructor.prototype.isNode === true || false; - } - function isParenthesisNode(x) { - return x && x.isParenthesisNode === true && x.constructor.prototype.isNode === true || false; - } - function isRangeNode(x) { - return x && x.isRangeNode === true && x.constructor.prototype.isNode === true || false; - } - function isSymbolNode(x) { - return x && x.isSymbolNode === true && x.constructor.prototype.isNode === true || false; - } - function isChain(x) { - return x && x.constructor.prototype.isChain === true || false; - } - function typeOf(x) { - var t = _typeof(x); - - if (t === 'object') { - // JavaScript types - if (x === null) { - return 'null'; - } - if (Array.isArray(x)) { - return 'Array'; - } - if (x instanceof Date) { - return 'Date'; - } - if (x instanceof RegExp) { - return 'RegExp'; - } // math.js types - - if (isBigNumber(x)) { - return 'BigNumber'; - } - if (isComplex(x)) { - return 'Complex'; - } - if (isFraction(x)) { - return 'Fraction'; - } - if (isMatrix(x)) { - return 'Matrix'; - } - if (isUnit(x)) { - return 'Unit'; - } - if (isIndex(x)) { - return 'Index'; - } - if (isRange(x)) { - return 'Range'; - } - if (isResultSet(x)) { - return 'ResultSet'; - } - if (isNode(x)) { - return x.type; - } - if (isChain(x)) { - return 'Chain'; - } - if (isHelp(x)) { - return 'Help'; - } - return 'Object'; - } - - if (t === 'function') { - return 'Function'; - } - return t; // can be 'string', 'number', 'boolean', ... - } - - /***/ }), - /* 2 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function () { - return arraySize; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "p", function () { - return validate; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "q", function () { - return validateIndex; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "m", function () { - return resize; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "l", function () { - return reshape; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "n", function () { - return squeeze; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "o", function () { - return unsqueeze; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "e", function () { - return flatten; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "k", function () { - return map; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "f", function () { - return forEach; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function () { - return filter; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "d", function () { - return filterRegExp; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "j", function () { - return join; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "i", function () { - return identify; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "g", function () { - return generalize; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "h", function () { - return getArrayDataType; - }); - /* unused harmony export last */ - /* unused harmony export initial */ - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function () { - return contains; - }); - /* harmony import */ var _number__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(4); - /* harmony import */ var _is__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1); - /* harmony import */ var _string__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5); - /* harmony import */ var _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(6); - /* harmony import */ var _error_IndexError__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(9); - - /** - * Calculate the size of a multi dimensional array. - * This function checks the size of the first entry, it does not validate - * whether all dimensions match. (use function `validate` for that) - * @param {Array} x - * @Return {Number[]} size - */ - - function arraySize(x) { - var s = []; - - while (Array.isArray(x)) { - s.push(x.length); - x = x[0]; - } - - return s; - } - /** - * Recursively validate whether each element in a multi dimensional array - * has a size corresponding to the provided size array. - * @param {Array} array Array to be validated - * @param {number[]} size Array with the size of each dimension - * @param {number} dim Current dimension - * @throws DimensionError - * @private - */ - - function _validate(array, size, dim) { - var i; - var len = array.length; - - if (len !== size[dim]) { - throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](len, size[dim]); - } - - if (dim < size.length - 1) { - // recursively validate each child array - var dimNext = dim + 1; - - for (i = 0; i < len; i++) { - var child = array[i]; - - if (!Array.isArray(child)) { - throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](size.length - 1, size.length, '<'); - } - - _validate(array[i], size, dimNext); - } - } else { - // last dimension. none of the childs may be an array - for (i = 0; i < len; i++) { - if (Array.isArray(array[i])) { - throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](size.length + 1, size.length, '>'); - } - } - } - } - /** - * Validate whether each element in a multi dimensional array has - * a size corresponding to the provided size array. - * @param {Array} array Array to be validated - * @param {number[]} size Array with the size of each dimension - * @throws DimensionError - */ - - function validate(array, size) { - var isScalar = size.length === 0; - - if (isScalar) { - // scalar - if (Array.isArray(array)) { - throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](array.length, 0); - } - } else { - // array - _validate(array, size, 0); - } - } - /** - * Test whether index is an integer number with index >= 0 and index < length - * when length is provided - * @param {number} index Zero-based index - * @param {number} [length] Length of the array - */ - - function validateIndex(index, length) { - if (!Object(_is__WEBPACK_IMPORTED_MODULE_1__[/* isNumber */ "y"])(index) || !Object(_number__WEBPACK_IMPORTED_MODULE_0__[/* isInteger */ "i"])(index)) { - throw new TypeError('Index must be an integer (value: ' + index + ')'); - } - - if (index < 0 || typeof length === 'number' && index >= length) { - throw new _error_IndexError__WEBPACK_IMPORTED_MODULE_4__[/* IndexError */ "a"](index, length); - } - } - /** - * Resize a multi dimensional array. The resized array is returned. - * @param {Array} array Array to be resized - * @param {Array.} size Array with the size of each dimension - * @param {*} [defaultValue=0] Value to be filled in in new entries, - * zero by default. Specify for example `null`, - * to clearly see entries that are not explicitly - * set. - * @return {Array} array The resized array - */ - - function resize(array, size, defaultValue) { - // TODO: add support for scalars, having size=[] ? - // check the type of the arguments - if (!Array.isArray(array) || !Array.isArray(size)) { - throw new TypeError('Array expected'); - } - - if (size.length === 0) { - throw new Error('Resizing to scalar is not supported'); - } // check whether size contains positive integers - - size.forEach(function (value) { - if (!Object(_is__WEBPACK_IMPORTED_MODULE_1__[/* isNumber */ "y"])(value) || !Object(_number__WEBPACK_IMPORTED_MODULE_0__[/* isInteger */ "i"])(value) || value < 0) { - throw new TypeError('Invalid size, must contain positive integers ' + '(size: ' + Object(_string__WEBPACK_IMPORTED_MODULE_2__[/* format */ "d"])(size) + ')'); - } - }); // recursively resize the array - - var _defaultValue = defaultValue !== undefined ? defaultValue : 0; - - _resize(array, size, 0, _defaultValue); - - return array; - } - /** - * Recursively resize a multi dimensional array - * @param {Array} array Array to be resized - * @param {number[]} size Array with the size of each dimension - * @param {number} dim Current dimension - * @param {*} [defaultValue] Value to be filled in in new entries, - * undefined by default. - * @private - */ - - function _resize(array, size, dim, defaultValue) { - var i; - var elem; - var oldLen = array.length; - var newLen = size[dim]; - var minLen = Math.min(oldLen, newLen); // apply new length - - array.length = newLen; - - if (dim < size.length - 1) { - // non-last dimension - var dimNext = dim + 1; // resize existing child arrays - - for (i = 0; i < minLen; i++) { - // resize child array - elem = array[i]; - - if (!Array.isArray(elem)) { - elem = [elem]; // add a dimension - - array[i] = elem; - } - - _resize(elem, size, dimNext, defaultValue); - } // create new child arrays - - for (i = minLen; i < newLen; i++) { - // get child array - elem = []; - array[i] = elem; // resize new child array - - _resize(elem, size, dimNext, defaultValue); - } - } else { - // last dimension - // remove dimensions of existing values - for (i = 0; i < minLen; i++) { - while (Array.isArray(array[i])) { - array[i] = array[i][0]; - } - } // fill new elements with the default value - - for (i = minLen; i < newLen; i++) { - array[i] = defaultValue; - } - } - } - /** - * Re-shape a multi dimensional array to fit the specified dimensions - * @param {Array} array Array to be reshaped - * @param {Array.} sizes List of sizes for each dimension - * @returns {Array} Array whose data has been formatted to fit the - * specified dimensions - * - * @throws {DimensionError} If the product of the new dimension sizes does - * not equal that of the old ones - */ - - function reshape(array, sizes) { - var flatArray = flatten(array); - var newArray; - - function product(arr) { - return arr.reduce(function (prev, curr) { - return prev * curr; - }); - } - - if (!Array.isArray(array) || !Array.isArray(sizes)) { - throw new TypeError('Array expected'); - } - - if (sizes.length === 0) { - throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](0, product(arraySize(array)), '!='); - } - - var totalSize = 1; - - for (var sizeIndex = 0; sizeIndex < sizes.length; sizeIndex++) { - totalSize *= sizes[sizeIndex]; - } - - if (flatArray.length !== totalSize) { - throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](product(sizes), product(arraySize(array)), '!='); - } - - try { - newArray = _reshape(flatArray, sizes); - } catch (e) { - if (e instanceof _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"]) { - throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](product(sizes), product(arraySize(array)), '!='); - } - - throw e; - } - - return newArray; - } - /** - * Iteratively re-shape a multi dimensional array to fit the specified dimensions - * @param {Array} array Array to be reshaped - * @param {Array.} sizes List of sizes for each dimension - * @returns {Array} Array whose data has been formatted to fit the - * specified dimensions - */ - - function _reshape(array, sizes) { - // testing if there are enough elements for the requested shape - var tmpArray = array; - var tmpArray2; // for each dimensions starting by the last one and ignoring the first one - - for (var sizeIndex = sizes.length - 1; sizeIndex > 0; sizeIndex--) { - var size = sizes[sizeIndex]; - tmpArray2 = []; // aggregate the elements of the current tmpArray in elements of the requested size - - var length = tmpArray.length / size; - - for (var i = 0; i < length; i++) { - tmpArray2.push(tmpArray.slice(i * size, (i + 1) * size)); - } // set it as the new tmpArray for the next loop turn or for return - - tmpArray = tmpArray2; - } - - return tmpArray; - } - /** - * Squeeze a multi dimensional array - * @param {Array} array - * @param {Array} [size] - * @returns {Array} returns the array itself - */ - - function squeeze(array, size) { - var s = size || arraySize(array); // squeeze outer dimensions - - while (Array.isArray(array) && array.length === 1) { - array = array[0]; - s.shift(); - } // find the first dimension to be squeezed - - var dims = s.length; - - while (s[dims - 1] === 1) { - dims--; - } // squeeze inner dimensions - - if (dims < s.length) { - array = _squeeze(array, dims, 0); - s.length = dims; - } - - return array; - } - /** - * Recursively squeeze a multi dimensional array - * @param {Array} array - * @param {number} dims Required number of dimensions - * @param {number} dim Current dimension - * @returns {Array | *} Returns the squeezed array - * @private - */ - - function _squeeze(array, dims, dim) { - var i, ii; - - if (dim < dims) { - var next = dim + 1; - - for (i = 0, ii = array.length; i < ii; i++) { - array[i] = _squeeze(array[i], dims, next); - } - } else { - while (Array.isArray(array)) { - array = array[0]; - } - } - - return array; - } - /** - * Unsqueeze a multi dimensional array: add dimensions when missing - * - * Paramter `size` will be mutated to match the new, unqueezed matrix size. - * - * @param {Array} array - * @param {number} dims Desired number of dimensions of the array - * @param {number} [outer] Number of outer dimensions to be added - * @param {Array} [size] Current size of array. - * @returns {Array} returns the array itself - * @private - */ - - function unsqueeze(array, dims, outer, size) { - var s = size || arraySize(array); // unsqueeze outer dimensions - - if (outer) { - for (var i = 0; i < outer; i++) { - array = [array]; - s.unshift(1); - } - } // unsqueeze inner dimensions - - array = _unsqueeze(array, dims, 0); - - while (s.length < dims) { - s.push(1); - } - - return array; - } - /** - * Recursively unsqueeze a multi dimensional array - * @param {Array} array - * @param {number} dims Required number of dimensions - * @param {number} dim Current dimension - * @returns {Array | *} Returns the squeezed array - * @private - */ - - function _unsqueeze(array, dims, dim) { - var i, ii; - - if (Array.isArray(array)) { - var next = dim + 1; - - for (i = 0, ii = array.length; i < ii; i++) { - array[i] = _unsqueeze(array[i], dims, next); - } - } else { - for (var d = dim; d < dims; d++) { - array = [array]; - } - } - - return array; - } - /** - * Flatten a multi dimensional array, put all elements in a one dimensional - * array - * @param {Array} array A multi dimensional array - * @return {Array} The flattened array (1 dimensional) - */ - - function flatten(array) { - if (!Array.isArray(array)) { - // if not an array, return as is - return array; - } - - var flat = []; - array.forEach(function callback(value) { - if (Array.isArray(value)) { - value.forEach(callback); // traverse through sub-arrays recursively - } else { - flat.push(value); - } - }); - return flat; - } - /** - * A safe map - * @param {Array} array - * @param {function} callback - */ - - function map(array, callback) { - return Array.prototype.map.call(array, callback); - } - /** - * A safe forEach - * @param {Array} array - * @param {function} callback - */ - - function forEach(array, callback) { - Array.prototype.forEach.call(array, callback); - } - /** - * A safe filter - * @param {Array} array - * @param {function} callback - */ - - function filter(array, callback) { - if (arraySize(array).length !== 1) { - throw new Error('Only one dimensional matrices supported'); - } - - return Array.prototype.filter.call(array, callback); - } - /** - * Filter values in a callback given a regular expression - * @param {Array} array - * @param {RegExp} regexp - * @return {Array} Returns the filtered array - * @private - */ - - function filterRegExp(array, regexp) { - if (arraySize(array).length !== 1) { - throw new Error('Only one dimensional matrices supported'); - } - - return Array.prototype.filter.call(array, function (entry) { - return regexp.test(entry); - }); - } - /** - * A safe join - * @param {Array} array - * @param {string} separator - */ - - function join(array, separator) { - return Array.prototype.join.call(array, separator); - } - /** - * Assign a numeric identifier to every element of a sorted array - * @param {Array} a An array - * @return {Array} An array of objects containing the original value and its identifier - */ - - function identify(a) { - if (!Array.isArray(a)) { - throw new TypeError('Array input expected'); - } - - if (a.length === 0) { - return a; - } - - var b = []; - var count = 0; - b[0] = { - value: a[0], - identifier: 0 - }; - - for (var i = 1; i < a.length; i++) { - if (a[i] === a[i - 1]) { - count++; - } else { - count = 0; - } - - b.push({ - value: a[i], - identifier: count - }); - } - - return b; - } - /** - * Remove the numeric identifier from the elements - * @param {array} a An array - * @return {array} An array of values without identifiers - */ - - function generalize(a) { - if (!Array.isArray(a)) { - throw new TypeError('Array input expected'); - } - - if (a.length === 0) { - return a; - } - - var b = []; - - for (var i = 0; i < a.length; i++) { - b.push(a[i].value); - } - - return b; - } - /** - * Check the datatype of a given object - * This is a low level implementation that should only be used by - * parent Matrix classes such as SparseMatrix or DenseMatrix - * This method does not validate Array Matrix shape - * @param {Array} array - * @param {function} typeOf Callback function to use to determine the type of a value - * @return string - */ - - function getArrayDataType(array, typeOf) { - var type; // to hold type info - - var length = 0; // to hold length value to ensure it has consistent sizes - - for (var i = 0; i < array.length; i++) { - var item = array[i]; - var isArray = Array.isArray(item); // Saving the target matrix row size - - if (i === 0 && isArray) { - length = item.length; - } // If the current item is an array but the length does not equal the targetVectorSize - - if (isArray && item.length !== length) { - return undefined; - } - - var itemType = isArray ? getArrayDataType(item, typeOf) // recurse into a nested array - : typeOf(item); - - if (type === undefined) { - type = itemType; // first item - } else if (type !== itemType) { - return 'mixed'; - } else {// we're good, everything has the same type so far - } - } - - return type; - } - /** - * Return the last item from an array - * @param array - * @returns {*} - */ - - function last(array) { - return array[array.length - 1]; - } - /** - * Get all but the last element of array. - */ - - function initial(array) { - return array.slice(0, array.length - 1); - } - /** - * Test whether an array or string contains an item - * @param {Array | string} array - * @param {*} item - * @return {boolean} - */ - - function contains(array, item) { - return array.indexOf(item) !== -1; - } - - /***/ }), - /* 3 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function () { - return clone; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "i", function () { - return mapObject; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "e", function () { - return extend; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function () { - return deepExtend; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "d", function () { - return deepStrictEqual; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function () { - return deepFlatten; - }); - /* unused harmony export canDefineProperty */ - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "h", function () { - return lazy; - }); - /* unused harmony export traverse */ - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "f", function () { - return hasOwnProperty; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "g", function () { - return isLegacyFactory; - }); - /* unused harmony export get */ - /* unused harmony export set */ - /* unused harmony export pick */ - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "j", function () { - return pickShallow; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "k", function () { - return values; - }); - /* harmony import */ var _is__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1); - function _typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - _typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - _typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return _typeof(obj); - } - - /** - * Clone an object - * - * clone(x) - * - * Can clone any primitive type, array, and object. - * If x has a function clone, this function will be invoked to clone the object. - * - * @param {*} x - * @return {*} clone - */ - - function clone(x) { - var type = _typeof(x); // immutable primitive types - - if (type === 'number' || type === 'string' || type === 'boolean' || x === null || x === undefined) { - return x; - } // use clone function of the object when available - - if (typeof x.clone === 'function') { - return x.clone(); - } // array - - if (Array.isArray(x)) { - return x.map(function (value) { - return clone(value); - }); - } - - if (x instanceof Date) { - return new Date(x.valueOf()); - } - if (Object(_is__WEBPACK_IMPORTED_MODULE_0__[/* isBigNumber */ "e"])(x)) { - return x; - } // bignumbers are immutable - - if (x instanceof RegExp) { - throw new TypeError('Cannot clone ' + x); - } // TODO: clone a RegExp - // object - - return mapObject(x, clone); - } - /** - * Apply map to all properties of an object - * @param {Object} object - * @param {function} callback - * @return {Object} Returns a copy of the object with mapped properties - */ - - function mapObject(object, callback) { - var clone = {}; - - for (var key in object) { - if (hasOwnProperty(object, key)) { - clone[key] = callback(object[key]); - } - } - - return clone; - } - /** - * Extend object a with the properties of object b - * @param {Object} a - * @param {Object} b - * @return {Object} a - */ - - function extend(a, b) { - for (var prop in b) { - if (hasOwnProperty(b, prop)) { - a[prop] = b[prop]; - } - } - - return a; - } - /** - * Deep extend an object a with the properties of object b - * @param {Object} a - * @param {Object} b - * @returns {Object} - */ - - function deepExtend(a, b) { - // TODO: add support for Arrays to deepExtend - if (Array.isArray(b)) { - throw new TypeError('Arrays are not supported by deepExtend'); - } - - for (var prop in b) { - if (hasOwnProperty(b, prop)) { - if (b[prop] && b[prop].constructor === Object) { - if (a[prop] === undefined) { - a[prop] = {}; - } - - if (a[prop] && a[prop].constructor === Object) { - deepExtend(a[prop], b[prop]); - } else { - a[prop] = b[prop]; - } - } else if (Array.isArray(b[prop])) { - throw new TypeError('Arrays are not supported by deepExtend'); - } else { - a[prop] = b[prop]; - } - } - } - - return a; - } - /** - * Deep test equality of all fields in two pairs of arrays or objects. - * Compares values and functions strictly (ie. 2 is not the same as '2'). - * @param {Array | Object} a - * @param {Array | Object} b - * @returns {boolean} - */ - - function deepStrictEqual(a, b) { - var prop, i, len; - - if (Array.isArray(a)) { - if (!Array.isArray(b)) { - return false; - } - - if (a.length !== b.length) { - return false; - } - - for (i = 0, len = a.length; i < len; i++) { - if (!deepStrictEqual(a[i], b[i])) { - return false; - } - } - - return true; - } else if (typeof a === 'function') { - return a === b; - } else if (a instanceof Object) { - if (Array.isArray(b) || !(b instanceof Object)) { - return false; - } - - for (prop in a) { - // noinspection JSUnfilteredForInLoop - if (!(prop in b) || !deepStrictEqual(a[prop], b[prop])) { - return false; - } - } - - for (prop in b) { - // noinspection JSUnfilteredForInLoop - if (!(prop in a) || !deepStrictEqual(a[prop], b[prop])) { - return false; - } - } - - return true; - } else { - return a === b; - } - } - /** - * Recursively flatten a nested object. - * @param {Object} nestedObject - * @return {Object} Returns the flattened object - */ - - function deepFlatten(nestedObject) { - var flattenedObject = {}; - - _deepFlatten(nestedObject, flattenedObject); - - return flattenedObject; - } // helper function used by deepFlatten - - function _deepFlatten(nestedObject, flattenedObject) { - for (var prop in nestedObject) { - if (hasOwnProperty(nestedObject, prop)) { - var value = nestedObject[prop]; - - if (_typeof(value) === 'object' && value !== null) { - _deepFlatten(value, flattenedObject); - } else { - flattenedObject[prop] = value; - } - } - } - } - /** - * Test whether the current JavaScript engine supports Object.defineProperty - * @returns {boolean} returns true if supported - */ - - function canDefineProperty() { - // test needed for broken IE8 implementation - try { - if (Object.defineProperty) { - Object.defineProperty({}, 'x', { - get: function get() {} - }); - return true; - } - } catch (e) {} - - return false; - } - /** - * Attach a lazy loading property to a constant. - * The given function `fn` is called once when the property is first requested. - * - * @param {Object} object Object where to add the property - * @param {string} prop Property name - * @param {Function} valueResolver Function returning the property value. Called - * without arguments. - */ - - function lazy(object, prop, valueResolver) { - var _uninitialized = true; - - var _value; - - Object.defineProperty(object, prop, { - get: function get() { - if (_uninitialized) { - _value = valueResolver(); - _uninitialized = false; - } - - return _value; - }, - set: function set(value) { - _value = value; - _uninitialized = false; - }, - configurable: true, - enumerable: true - }); - } - /** - * Traverse a path into an object. - * When a namespace is missing, it will be created - * @param {Object} object - * @param {string | string[]} path A dot separated string like 'name.space' - * @return {Object} Returns the object at the end of the path - */ - - function traverse(object, path) { - if (path && typeof path === 'string') { - return traverse(object, path.split('.')); - } - - var obj = object; - - if (path) { - for (var i = 0; i < path.length; i++) { - var key = path[i]; - - if (!(key in obj)) { - obj[key] = {}; - } - - obj = obj[key]; - } - } - - return obj; - } - /** - * A safe hasOwnProperty - * @param {Object} object - * @param {string} property - */ - - function hasOwnProperty(object, property) { - return object && Object.hasOwnProperty.call(object, property); - } - /** - * Test whether an object is a factory. a factory has fields: - * - * - factory: function (type: Object, config: Object, load: function, typed: function [, math: Object]) (required) - * - name: string (optional) - * - path: string A dot separated path (optional) - * - math: boolean If true (false by default), the math namespace is passed - * as fifth argument of the factory function - * - * @param {*} object - * @returns {boolean} - */ - - function isLegacyFactory(object) { - return object && typeof object.factory === 'function'; - } - /** - * Get a nested property from an object - * @param {Object} object - * @param {string | string[]} path - * @returns {Object} - */ - - function get(object, path) { - if (typeof path === 'string') { - if (isPath(path)) { - return get(object, path.split('.')); - } else { - return object[path]; - } - } - - var child = object; - - for (var i = 0; i < path.length; i++) { - var key = path[i]; - child = child ? child[key] : undefined; - } - - return child; - } - /** - * Set a nested property in an object - * Mutates the object itself - * If the path doesn't exist, it will be created - * @param {Object} object - * @param {string | string[]} path - * @param {*} value - * @returns {Object} - */ - - function set(object, path, value) { - if (typeof path === 'string') { - if (isPath(path)) { - return set(object, path.split('.'), value); - } else { - object[path] = value; - return object; - } - } - - var child = object; - - for (var i = 0; i < path.length - 1; i++) { - var key = path[i]; - - if (child[key] === undefined) { - child[key] = {}; - } - - child = child[key]; - } - - if (path.length > 0) { - var lastKey = path[path.length - 1]; - child[lastKey] = value; - } - - return object; - } - /** - * Create an object composed of the picked object properties - * @param {Object} object - * @param {string[]} properties - * @param {function} [transform] Optional value to transform a value when picking it - * @return {Object} - */ - - function pick(object, properties, transform) { - var copy = {}; - - for (var i = 0; i < properties.length; i++) { - var key = properties[i]; - var value = get(object, key); - - if (value !== undefined) { - set(copy, key, transform ? transform(value, key) : value); - } - } - - return copy; - } - /** - * Shallow version of pick, creating an object composed of the picked object properties - * but not for nested properties - * @param {Object} object - * @param {string[]} properties - * @return {Object} - */ - - function pickShallow(object, properties) { - var copy = {}; - - for (var i = 0; i < properties.length; i++) { - var key = properties[i]; - var value = object[key]; - - if (value !== undefined) { - copy[key] = value; - } - } - - return copy; - } - function values(object) { - return Object.keys(object).map(function (key) { - return object[key]; - }); - } // helper function to test whether a string contains a path like 'user.name' - - function isPath(str) { - return str.indexOf('.') !== -1; - } - - /***/ }), - /* 4 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "i", function () { - return isInteger; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "n", function () { - return sign; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "l", function () { - return log2; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "j", function () { - return log10; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "k", function () { - return log1p; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "d", function () { - return cbrt; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "g", function () { - return expm1; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "h", function () { - return format; - }); - /* unused harmony export splitNumber */ - /* unused harmony export toEngineering */ - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "q", function () { - return toFixed; - }); - /* unused harmony export toExponential */ - /* unused harmony export toPrecision */ - /* unused harmony export roundDigits */ - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "f", function () { - return digits; - }); - /* unused harmony export DBL_EPSILON */ - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "m", function () { - return nearlyEqual; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function () { - return acosh; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function () { - return asinh; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function () { - return atanh; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "e", function () { - return cosh; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "o", function () { - return sinh; - }); - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "p", function () { - return tanh; - }); - /* harmony import */ var _is__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1); - - /** - * @typedef {{sign: '+' | '-' | '', coefficients: number[], exponent: number}} SplitValue - */ - - /** - * Check if a number is integer - * @param {number | boolean} value - * @return {boolean} isInteger - */ - - function isInteger(value) { - if (typeof value === 'boolean') { - return true; - } - - return isFinite(value) ? value === Math.round(value) : false; // Note: we use ==, not ===, as we can have Booleans as well - } - /** - * Calculate the sign of a number - * @param {number} x - * @returns {number} - */ - - var sign = /* #__PURE__ */Math.sign || function (x) { - if (x > 0) { - return 1; - } else if (x < 0) { - return -1; - } else { - return 0; - } - }; - /** - * Calculate the base-2 logarithm of a number - * @param {number} x - * @returns {number} - */ - - var log2 = /* #__PURE__ */Math.log2 || function log2(x) { - return Math.log(x) / Math.LN2; - }; - /** - * Calculate the base-10 logarithm of a number - * @param {number} x - * @returns {number} - */ - - var log10 = /* #__PURE__ */Math.log10 || function log10(x) { - return Math.log(x) / Math.LN10; - }; - /** - * Calculate the natural logarithm of a number + 1 - * @param {number} x - * @returns {number} - */ - - var log1p = /* #__PURE__ */Math.log1p || function (x) { - return Math.log(x + 1); - }; - /** - * Calculate cubic root for a number - * - * Code from es6-shim.js: - * https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1564-L1577 - * - * @param {number} x - * @returns {number} Returns the cubic root of x - */ - - var cbrt = /* #__PURE__ */Math.cbrt || function cbrt(x) { - if (x === 0) { - return x; - } - - var negate = x < 0; - var result; - - if (negate) { - x = -x; - } - - if (isFinite(x)) { - result = Math.exp(Math.log(x) / 3); // from https://en.wikipedia.org/wiki/Cube_root#Numerical_methods - - result = (x / (result * result) + 2 * result) / 3; - } else { - result = x; - } - - return negate ? -result : result; - }; - /** - * Calculates exponentiation minus 1 - * @param {number} x - * @return {number} res - */ - - var expm1 = /* #__PURE__ */Math.expm1 || function expm1(x) { - return x >= 2e-4 || x <= -2e-4 ? Math.exp(x) - 1 : x + x * x / 2 + x * x * x / 6; - }; - /** - * Convert a number to a formatted string representation. - * - * Syntax: - * - * format(value) - * format(value, options) - * format(value, precision) - * format(value, fn) - * - * Where: - * - * {number} value The value to be formatted - * {Object} options An object with formatting options. Available options: - * {string} notation - * Number notation. Choose from: - * 'fixed' Always use regular number notation. - * For example '123.40' and '14000000' - * 'exponential' Always use exponential notation. - * For example '1.234e+2' and '1.4e+7' - * 'engineering' Always use engineering notation. - * For example '123.4e+0' and '14.0e+6' - * 'auto' (default) Regular number notation for numbers - * having an absolute value between - * `lowerExp` and `upperExp` bounds, and - * uses exponential notation elsewhere. - * Lower bound is included, upper bound - * is excluded. - * For example '123.4' and '1.4e7'. - * {number} precision A number between 0 and 16 to round - * the digits of the number. - * In case of notations 'exponential', - * 'engineering', and 'auto', - * `precision` defines the total - * number of significant digits returned. - * In case of notation 'fixed', - * `precision` defines the number of - * significant digits after the decimal - * point. - * `precision` is undefined by default, - * not rounding any digits. - * {number} lowerExp Exponent determining the lower boundary - * for formatting a value with an exponent - * when `notation='auto`. - * Default value is `-3`. - * {number} upperExp Exponent determining the upper boundary - * for formatting a value with an exponent - * when `notation='auto`. - * Default value is `5`. - * {Function} fn A custom formatting function. Can be used to override the - * built-in notations. Function `fn` is called with `value` as - * parameter and must return a string. Is useful for example to - * format all values inside a matrix in a particular way. - * - * Examples: - * - * format(6.4) // '6.4' - * format(1240000) // '1.24e6' - * format(1/3) // '0.3333333333333333' - * format(1/3, 3) // '0.333' - * format(21385, 2) // '21000' - * format(12.071, {notation: 'fixed'}) // '12' - * format(2.3, {notation: 'fixed', precision: 2}) // '2.30' - * format(52.8, {notation: 'exponential'}) // '5.28e+1' - * format(12345678, {notation: 'engineering'}) // '12.345678e+6' - * - * @param {number} value - * @param {Object | Function | number} [options] - * @return {string} str The formatted value - */ - - function format(value, options) { - if (typeof options === 'function') { - // handle format(value, fn) - return options(value); - } // handle special cases - - if (value === Infinity) { - return 'Infinity'; - } else if (value === -Infinity) { - return '-Infinity'; - } else if (isNaN(value)) { - return 'NaN'; - } // default values for options - - var notation = 'auto'; - var precision; - - if (options) { - // determine notation from options - if (options.notation) { - notation = options.notation; - } // determine precision from options - - if (Object(_is__WEBPACK_IMPORTED_MODULE_0__[/* isNumber */ "y"])(options)) { - precision = options; - } else if (Object(_is__WEBPACK_IMPORTED_MODULE_0__[/* isNumber */ "y"])(options.precision)) { - precision = options.precision; - } - } // handle the various notations - - switch (notation) { - case 'fixed': - return toFixed(value, precision); - - case 'exponential': - return toExponential(value, precision); - - case 'engineering': - return toEngineering(value, precision); - - case 'auto': - // remove trailing zeros after the decimal point - return toPrecision(value, precision, options && options).replace(/((\.\d*?)(0+))($|e)/, function () { - var digits = arguments[2]; - var e = arguments[4]; - return digits !== '.' ? digits + e : e; - }); - - default: - throw new Error('Unknown notation "' + notation + '". ' + 'Choose "auto", "exponential", or "fixed".'); - } - } - /** - * Split a number into sign, coefficients, and exponent - * @param {number | string} value - * @return {SplitValue} - * Returns an object containing sign, coefficients, and exponent - */ - - function splitNumber(value) { - // parse the input value - var match = String(value).toLowerCase().match(/^0*?(-?)(\d+\.?\d*)(e([+-]?\d+))?$/); - - if (!match) { - throw new SyntaxError('Invalid number ' + value); - } - - var sign = match[1]; - var digits = match[2]; - var exponent = parseFloat(match[4] || '0'); - var dot = digits.indexOf('.'); - exponent += dot !== -1 ? dot - 1 : digits.length - 1; - var coefficients = digits.replace('.', '') // remove the dot (must be removed before removing leading zeros) - .replace(/^0*/, function (zeros) { - // remove leading zeros, add their count to the exponent - exponent -= zeros.length; - return ''; - }).replace(/0*$/, '') // remove trailing zeros - .split('').map(function (d) { - return parseInt(d); - }); - - if (coefficients.length === 0) { - coefficients.push(0); - exponent++; - } - - return { - sign: sign, - coefficients: coefficients, - exponent: exponent - }; - } - /** - * Format a number in engineering notation. Like '1.23e+6', '2.3e+0', '3.500e-3' - * @param {number | string} value - * @param {number} [precision] Optional number of significant figures to return. - */ - - function toEngineering(value, precision) { - if (isNaN(value) || !isFinite(value)) { - return String(value); - } - - var split = splitNumber(value); - var rounded = roundDigits(split, precision); - var e = rounded.exponent; - var c = rounded.coefficients; // find nearest lower multiple of 3 for exponent - - var newExp = e % 3 === 0 ? e : e < 0 ? e - 3 - e % 3 : e - e % 3; - - if (Object(_is__WEBPACK_IMPORTED_MODULE_0__[/* isNumber */ "y"])(precision)) { - // add zeroes to give correct sig figs - while (precision > c.length || e - newExp + 1 > c.length) { - c.push(0); - } - } else { - // concatenate coefficients with necessary zeros - // add zeros if necessary (for example: 1e+8 -> 100e+6) - var missingZeros = Math.abs(e - newExp) - (c.length - 1); - - for (var i = 0; i < missingZeros; i++) { - c.push(0); - } - } // find difference in exponents - - var expDiff = Math.abs(e - newExp); - var decimalIdx = 1; // push decimal index over by expDiff times - - while (expDiff > 0) { - decimalIdx++; - expDiff--; - } // if all coefficient values are zero after the decimal point and precision is unset, don't add a decimal value. - // otherwise concat with the rest of the coefficients - - var decimals = c.slice(decimalIdx).join(''); - var decimalVal = Object(_is__WEBPACK_IMPORTED_MODULE_0__[/* isNumber */ "y"])(precision) && decimals.length || decimals.match(/[1-9]/) ? '.' + decimals : ''; - var str = c.slice(0, decimalIdx).join('') + decimalVal + 'e' + (e >= 0 ? '+' : '') + newExp.toString(); - return rounded.sign + str; - } - /** - * Format a number with fixed notation. - * @param {number | string} value - * @param {number} [precision=undefined] Optional number of decimals after the - * decimal point. null by default. - */ - - function toFixed(value, precision) { - if (isNaN(value) || !isFinite(value)) { - return String(value); - } - - var splitValue = splitNumber(value); - var rounded = typeof precision === 'number' ? roundDigits(splitValue, splitValue.exponent + 1 + precision) : splitValue; - var c = rounded.coefficients; - var p = rounded.exponent + 1; // exponent may have changed - // append zeros if needed - - var pp = p + (precision || 0); - - if (c.length < pp) { - c = c.concat(zeros(pp - c.length)); - } // prepend zeros if needed - - if (p < 0) { - c = zeros(-p + 1).concat(c); - p = 1; - } // insert a dot if needed - - if (p < c.length) { - c.splice(p, 0, p === 0 ? '0.' : '.'); - } - - return rounded.sign + c.join(''); - } - /** - * Format a number in exponential notation. Like '1.23e+5', '2.3e+0', '3.500e-3' - * @param {number | string} value - * @param {number} [precision] Number of digits in formatted output. - * If not provided, the maximum available digits - * is used. - */ - - function toExponential(value, precision) { - if (isNaN(value) || !isFinite(value)) { - return String(value); - } // round if needed, else create a clone - - var split = splitNumber(value); - var rounded = precision ? roundDigits(split, precision) : split; - var c = rounded.coefficients; - var e = rounded.exponent; // append zeros if needed - - if (c.length < precision) { - c = c.concat(zeros(precision - c.length)); - } // format as `C.CCCe+EEE` or `C.CCCe-EEE` - - var first = c.shift(); - return rounded.sign + first + (c.length > 0 ? '.' + c.join('') : '') + 'e' + (e >= 0 ? '+' : '') + e; - } - /** - * Format a number with a certain precision - * @param {number | string} value - * @param {number} [precision=undefined] Optional number of digits. - * @param {{lowerExp: number | undefined, upperExp: number | undefined}} [options] - * By default: - * lowerExp = -3 (incl) - * upper = +5 (excl) - * @return {string} - */ - - function toPrecision(value, precision, options) { - if (isNaN(value) || !isFinite(value)) { - return String(value); - } // determine lower and upper bound for exponential notation. - - var lowerExp = options && options.lowerExp !== undefined ? options.lowerExp : -3; - var upperExp = options && options.upperExp !== undefined ? options.upperExp : 5; - var split = splitNumber(value); - var rounded = precision ? roundDigits(split, precision) : split; - - if (rounded.exponent < lowerExp || rounded.exponent >= upperExp) { - // exponential notation - return toExponential(value, precision); - } else { - var c = rounded.coefficients; - var e = rounded.exponent; // append trailing zeros - - if (c.length < precision) { - c = c.concat(zeros(precision - c.length)); - } // append trailing zeros - // TODO: simplify the next statement - - c = c.concat(zeros(e - c.length + 1 + (c.length < precision ? precision - c.length : 0))); // prepend zeros - - c = zeros(-e).concat(c); - var dot = e > 0 ? e : 0; - - if (dot < c.length - 1) { - c.splice(dot + 1, 0, '.'); - } - - return rounded.sign + c.join(''); - } - } - /** - * Round the number of digits of a number * - * @param {SplitValue} split A value split with .splitNumber(value) - * @param {number} precision A positive integer - * @return {SplitValue} - * Returns an object containing sign, coefficients, and exponent - * with rounded digits - */ - - function roundDigits(split, precision) { - // create a clone - var rounded = { - sign: split.sign, - coefficients: split.coefficients, - exponent: split.exponent - }; - var c = rounded.coefficients; // prepend zeros if needed - - while (precision <= 0) { - c.unshift(0); - rounded.exponent++; - precision++; - } - - if (c.length > precision) { - var removed = c.splice(precision, c.length - precision); - - if (removed[0] >= 5) { - var i = precision - 1; - c[i]++; - - while (c[i] === 10) { - c.pop(); - - if (i === 0) { - c.unshift(0); - rounded.exponent++; - i++; - } - - i--; - c[i]++; - } - } - } - - return rounded; - } - /** - * Create an array filled with zeros. - * @param {number} length - * @return {Array} - */ - - function zeros(length) { - var arr = []; - - for (var i = 0; i < length; i++) { - arr.push(0); - } - - return arr; - } - /** - * Count the number of significant digits of a number. - * - * For example: - * 2.34 returns 3 - * 0.0034 returns 2 - * 120.5e+30 returns 4 - * - * @param {number} value - * @return {number} digits Number of significant digits - */ - - function digits(value) { - return value.toExponential().replace(/e.*$/, '') // remove exponential notation - .replace(/^0\.?0*|\./, '') // remove decimal point and leading zeros - .length; - } - /** - * Minimum number added to one that makes the result different than one - */ - - var DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16; - /** - * Compares two floating point numbers. - * @param {number} x First value to compare - * @param {number} y Second value to compare - * @param {number} [epsilon] The maximum relative difference between x and y - * If epsilon is undefined or null, the function will - * test whether x and y are exactly equal. - * @return {boolean} whether the two numbers are nearly equal - */ - - function nearlyEqual(x, y, epsilon) { - // if epsilon is null or undefined, test whether x and y are exactly equal - if (epsilon === null || epsilon === undefined) { - return x === y; - } - - if (x === y) { - return true; - } // NaN - - if (isNaN(x) || isNaN(y)) { - return false; - } // at this point x and y should be finite - - if (isFinite(x) && isFinite(y)) { - // check numbers are very close, needed when comparing numbers near zero - var diff = Math.abs(x - y); - - if (diff < DBL_EPSILON) { - return true; - } else { - // use relative error - return diff <= Math.max(Math.abs(x), Math.abs(y)) * epsilon; - } - } // Infinite and Number or negative Infinite and positive Infinite cases - - return false; - } - /** - * Calculate the hyperbolic arccos of a number - * @param {number} x - * @return {number} - */ - - var acosh = Math.acosh || function (x) { - return Math.log(Math.sqrt(x * x - 1) + x); - }; - var asinh = Math.asinh || function (x) { - return Math.log(Math.sqrt(x * x + 1) + x); - }; - /** - * Calculate the hyperbolic arctangent of a number - * @param {number} x - * @return {number} - */ - - var atanh = Math.atanh || function (x) { - return Math.log((1 + x) / (1 - x)) / 2; - }; - /** - * Calculate the hyperbolic cosine of a number - * @param {number} x - * @returns {number} - */ - - var cosh = Math.cosh || function (x) { - return (Math.exp(x) + Math.exp(-x)) / 2; - }; - /** - * Calculate the hyperbolic sine of a number - * @param {number} x - * @returns {number} - */ - - var sinh = Math.sinh || function (x) { - return (Math.exp(x) - Math.exp(-x)) / 2; - }; - /** - * Calculate the hyperbolic tangent of a number - * @param {number} x - * @returns {number} - */ - - var tanh = Math.tanh || function (x) { - var e = Math.exp(2 * x); - return (e - 1) / (e + 1); - }; - - /***/ }), - /* 5 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - - // EXPORTS - __webpack_require__.d(__webpack_exports__, "b", function () { - return /* binding */ endsWith; - }); - __webpack_require__.d(__webpack_exports__, "d", function () { - return /* binding */ string_format; - }); - __webpack_require__.d(__webpack_exports__, "e", function () { - return /* binding */ stringify; - }); - __webpack_require__.d(__webpack_exports__, "c", function () { - return /* binding */ string_escape; - }); - __webpack_require__.d(__webpack_exports__, "a", function () { - return /* binding */ compareText; - }); - - // EXTERNAL MODULE: ./src/utils/is.js - var is = __webpack_require__(1); - - // EXTERNAL MODULE: ./src/utils/number.js - var number = __webpack_require__(4); - - // CONCATENATED MODULE: ./src/utils/bignumber/formatter.js - /** - * Convert a BigNumber to a formatted string representation. - * - * Syntax: - * - * format(value) - * format(value, options) - * format(value, precision) - * format(value, fn) - * - * Where: - * - * {number} value The value to be formatted - * {Object} options An object with formatting options. Available options: - * {string} notation - * Number notation. Choose from: - * 'fixed' Always use regular number notation. - * For example '123.40' and '14000000' - * 'exponential' Always use exponential notation. - * For example '1.234e+2' and '1.4e+7' - * 'auto' (default) Regular number notation for numbers - * having an absolute value between - * `lower` and `upper` bounds, and uses - * exponential notation elsewhere. - * Lower bound is included, upper bound - * is excluded. - * For example '123.4' and '1.4e7'. - * {number} precision A number between 0 and 16 to round - * the digits of the number. - * In case of notations 'exponential', - * 'engineering', and 'auto', - * `precision` defines the total - * number of significant digits returned. - * In case of notation 'fixed', - * `precision` defines the number of - * significant digits after the decimal - * point. - * `precision` is undefined by default. - * {number} lowerExp Exponent determining the lower boundary - * for formatting a value with an exponent - * when `notation='auto`. - * Default value is `-3`. - * {number} upperExp Exponent determining the upper boundary - * for formatting a value with an exponent - * when `notation='auto`. - * Default value is `5`. - * {Function} fn A custom formatting function. Can be used to override the - * built-in notations. Function `fn` is called with `value` as - * parameter and must return a string. Is useful for example to - * format all values inside a matrix in a particular way. - * - * Examples: - * - * format(6.4) // '6.4' - * format(1240000) // '1.24e6' - * format(1/3) // '0.3333333333333333' - * format(1/3, 3) // '0.333' - * format(21385, 2) // '21000' - * format(12e8, {notation: 'fixed'}) // returns '1200000000' - * format(2.3, {notation: 'fixed', precision: 4}) // returns '2.3000' - * format(52.8, {notation: 'exponential'}) // returns '5.28e+1' - * format(12400, {notation: 'engineering'}) // returns '12.400e+3' - * - * @param {BigNumber} value - * @param {Object | Function | number} [options] - * @return {string} str The formatted value - */ - function format(value, options) { - if (typeof options === 'function') { - // handle format(value, fn) - return options(value); - } // handle special cases - - if (!value.isFinite()) { - return value.isNaN() ? 'NaN' : value.gt(0) ? 'Infinity' : '-Infinity'; - } // default values for options - - var notation = 'auto'; - var precision; - - if (options !== undefined) { - // determine notation from options - if (options.notation) { - notation = options.notation; - } // determine precision from options - - if (typeof options === 'number') { - precision = options; - } else if (options.precision) { - precision = options.precision; - } - } // handle the various notations - - switch (notation) { - case 'fixed': - return toFixed(value, precision); - - case 'exponential': - return toExponential(value, precision); - - case 'engineering': - return toEngineering(value, precision); - - case 'auto': - { - // determine lower and upper bound for exponential notation. - // TODO: implement support for upper and lower to be BigNumbers themselves - var lowerExp = options && options.lowerExp !== undefined ? options.lowerExp : -3; - var upperExp = options && options.upperExp !== undefined ? options.upperExp : 5; // handle special case zero - - if (value.isZero()) { - return '0'; - } // determine whether or not to output exponential notation - - var str; - var rounded = value.toSignificantDigits(precision); - var exp = rounded.e; - - if (exp >= lowerExp && exp < upperExp) { - // normal number notation - str = rounded.toFixed(); - } else { - // exponential notation - str = toExponential(value, precision); - } // remove trailing zeros after the decimal point - - return str.replace(/((\.\d*?)(0+))($|e)/, function () { - var digits = arguments[2]; - var e = arguments[4]; - return digits !== '.' ? digits + e : e; - }); - } - - default: - throw new Error('Unknown notation "' + notation + '". ' + 'Choose "auto", "exponential", or "fixed".'); - } - } - /** - * Format a BigNumber in engineering notation. Like '1.23e+6', '2.3e+0', '3.500e-3' - * @param {BigNumber | string} value - * @param {number} [precision] Optional number of significant figures to return. - */ - - function toEngineering(value, precision) { - // find nearest lower multiple of 3 for exponent - var e = value.e; - var newExp = e % 3 === 0 ? e : e < 0 ? e - 3 - e % 3 : e - e % 3; // find difference in exponents, and calculate the value without exponent - - var valueWithoutExp = value.mul(Math.pow(10, -newExp)); - var valueStr = valueWithoutExp.toPrecision(precision); - - if (valueStr.indexOf('e') !== -1) { - valueStr = valueWithoutExp.toString(); - } - - return valueStr + 'e' + (e >= 0 ? '+' : '') + newExp.toString(); - } - /** - * Format a number in exponential notation. Like '1.23e+5', '2.3e+0', '3.500e-3' - * @param {BigNumber} value - * @param {number} [precision] Number of digits in formatted output. - * If not provided, the maximum available digits - * is used. - * @returns {string} str - */ - - function toExponential(value, precision) { - if (precision !== undefined) { - return value.toExponential(precision - 1); // Note the offset of one - } else { - return value.toExponential(); - } - } - /** - * Format a number with fixed notation. - * @param {BigNumber} value - * @param {number} [precision=undefined] Optional number of decimals after the - * decimal point. Undefined by default. - */ - - function toFixed(value, precision) { - return value.toFixed(precision); - } - // CONCATENATED MODULE: ./src/utils/string.js - function _typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - _typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - _typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return _typeof(obj); - } - - /** - * Check if a text ends with a certain string. - * @param {string} text - * @param {string} search - */ - - function endsWith(text, search) { - var start = text.length - search.length; - var end = text.length; - return text.substring(start, end) === search; - } - /** - * Format a value of any type into a string. - * - * Usage: - * math.format(value) - * math.format(value, precision) - * - * When value is a function: - * - * - When the function has a property `syntax`, it returns this - * syntax description. - * - In other cases, a string `'function'` is returned. - * - * When `value` is an Object: - * - * - When the object contains a property `format` being a function, this - * function is invoked as `value.format(options)` and the result is returned. - * - When the object has its own `toString` method, this method is invoked - * and the result is returned. - * - In other cases the function will loop over all object properties and - * return JSON object notation like '{"a": 2, "b": 3}'. - * - * Example usage: - * math.format(2/7) // '0.2857142857142857' - * math.format(math.pi, 3) // '3.14' - * math.format(new Complex(2, 3)) // '2 + 3i' - * math.format('hello') // '"hello"' - * - * @param {*} value Value to be stringified - * @param {Object | number | Function} [options] Formatting options. See - * lib/utils/number:format for a - * description of the available - * options. - * @return {string} str - */ - - function string_format(value, options) { - if (typeof value === 'number') { - return Object(number["h" /* format */])(value, options); - } - - if (Object(is["e" /* isBigNumber */])(value)) { - return format(value, options); - } // note: we use unsafe duck-typing here to check for Fractions, this is - // ok here since we're only invoking toString or concatenating its values - - if (looksLikeFraction(value)) { - if (!options || options.fraction !== 'decimal') { - // output as ratio, like '1/3' - return value.s * value.n + '/' + value.d; - } else { - // output as decimal, like '0.(3)' - return value.toString(); - } - } - - if (Array.isArray(value)) { - return formatArray(value, options); - } - - if (Object(is["I" /* isString */])(value)) { - return '"' + value + '"'; - } - - if (typeof value === 'function') { - return value.syntax ? String(value.syntax) : 'function'; - } - - if (value && _typeof(value) === 'object') { - if (typeof value.format === 'function') { - return value.format(options); - } else if (value && value.toString(options) !== {}.toString()) { - // this object has a non-native toString method, use that one - return value.toString(options); - } else { - var entries = Object.keys(value).map(function (key) { - return '"' + key + '": ' + string_format(value[key], options); - }); - return '{' + entries.join(', ') + '}'; - } - } - - return String(value); - } - /** - * Stringify a value into a string enclosed in double quotes. - * Unescaped double quotes and backslashes inside the value are escaped. - * @param {*} value - * @return {string} - */ - - function stringify(value) { - var text = String(value); - var escaped = ''; - var i = 0; - - while (i < text.length) { - var c = text.charAt(i); - - if (c === '\\') { - escaped += c; - i++; - c = text.charAt(i); - - if (c === '' || '"\\/bfnrtu'.indexOf(c) === -1) { - escaped += '\\'; // no valid escape character -> escape it - } - - escaped += c; - } else if (c === '"') { - escaped += '\\"'; - } else { - escaped += c; - } - - i++; - } - - return '"' + escaped + '"'; - } - /** - * Escape special HTML characters - * @param {*} value - * @return {string} - */ - - function string_escape(value) { - var text = String(value); - text = text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(//g, '>'); - return text; - } - /** - * Recursively format an n-dimensional matrix - * Example output: "[[1, 2], [3, 4]]" - * @param {Array} array - * @param {Object | number | Function} [options] Formatting options. See - * lib/utils/number:format for a - * description of the available - * options. - * @returns {string} str - */ - - function formatArray(array, options) { - if (Array.isArray(array)) { - var str = '['; - var len = array.length; - - for (var i = 0; i < len; i++) { - if (i !== 0) { - str += ', '; - } - - str += formatArray(array[i], options); - } - - str += ']'; - return str; - } else { - return string_format(array, options); - } - } - /** - * Check whether a value looks like a Fraction (unsafe duck-type check) - * @param {*} value - * @return {boolean} - */ - - function looksLikeFraction(value) { - return value && _typeof(value) === 'object' && typeof value.s === 'number' && typeof value.n === 'number' && typeof value.d === 'number' || false; - } - /** - * Compare two strings - * @param {string} x - * @param {string} y - * @returns {number} - */ - - function compareText(x, y) { - // we don't want to convert numbers to string, only accept string input - if (!Object(is["I" /* isString */])(x)) { - throw new TypeError('Unexpected type of argument in function compareText ' + '(expected: string or Array or Matrix, actual: ' + Object(is["M" /* typeOf */])(x) + ', index: 0)'); - } - - if (!Object(is["I" /* isString */])(y)) { - throw new TypeError('Unexpected type of argument in function compareText ' + '(expected: string or Array or Matrix, actual: ' + Object(is["M" /* typeOf */])(y) + ', index: 1)'); - } - - return x === y ? 0 : x > y ? 1 : -1; - } - - /***/ }), - /* 6 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function () { - return DimensionError; - }); - /** - * Create a range error with the message: - * 'Dimension mismatch ( != )' - * @param {number | number[]} actual The actual size - * @param {number | number[]} expected The expected size - * @param {string} [relation='!='] Optional relation between actual - * and expected size: '!=', '<', etc. - * @extends RangeError - */ - function DimensionError(actual, expected, relation) { - if (!(this instanceof DimensionError)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this.actual = actual; - this.expected = expected; - this.relation = relation; - this.message = 'Dimension mismatch (' + (Array.isArray(actual) ? '[' + actual.join(', ') + ']' : actual) + ' ' + (this.relation || '!=') + ' ' + (Array.isArray(expected) ? '[' + expected.join(', ') + ']' : expected) + ')'; - this.stack = new Error().stack; - } - DimensionError.prototype = new RangeError(); - DimensionError.prototype.constructor = RangeError; - DimensionError.prototype.name = 'DimensionError'; - DimensionError.prototype.isDimensionError = true; - - /***/ }), - /* 7 */, - /* 8 */ - /***/ (function (module, exports, __webpack_require__) { - - var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/** - * @license Complex.js v2.0.11 11/02/2016 - * - * Copyright (c) 2016, Robert Eisele (robert@xarg.org) - * Dual licensed under the MIT or GPL Version 2 licenses. - **/ - - /** - * - * This class allows the manipulation of complex numbers. - * You can pass a complex number in different formats. Either as object, double, string or two integer parameters. - * - * Object form - * { re: , im: } - * { arg: , abs: } - * { phi: , r: } - * - * Array / Vector form - * [ real, imaginary ] - * - * Double form - * 99.3 - Single double value - * - * String form - * '23.1337' - Simple real number - * '15+3i' - a simple complex number - * '3-i' - a simple complex number - * - * Example: - * - * var c = new Complex('99.3+8i'); - * c.mul({r: 3, i: 9}).div(4.9).sub(3, 2); - * - */ - - (function (root) { - - 'use strict'; - - var cosh = function (x) { - return (Math.exp(x) + Math.exp(-x)) * 0.5; - }; - - var sinh = function (x) { - return (Math.exp(x) - Math.exp(-x)) * 0.5; - }; - - /** - * Calculates cos(x) - 1 using Taylor series if x is small. - * - * @param {number} x - * @returns {number} cos(x) - 1 - */ - - var cosm1 = function (x) { - var limit = Math.PI / 4; - if (x < -limit || x > limit) { - return (Math.cos(x) - 1.0); - } - - var xx = x * x; - return xx * - (-0.5 + xx * - (1 / 24 + xx * - (-1 / 720 + xx * - (1 / 40320 + xx * - (-1 / 3628800 + xx * - (1 / 4790014600 + xx * - (-1 / 87178291200 + xx * - (1 / 20922789888000) - ) - ) - ) - ) - ) - ) - ); - }; - - var hypot = function (x, y) { - - var a = Math.abs(x); - var b = Math.abs(y); - - if (a < 3000 && b < 3000) { - return Math.sqrt(a * a + b * b); - } - - if (a < b) { - a = b; - b = x / y; - } else { - b = y / x; - } - return a * Math.sqrt(1 + b * b); - }; - - var parser_exit = function () { - throw SyntaxError('Invalid Param'); - }; - - /** - * Calculates log(sqrt(a^2+b^2)) in a way to avoid overflows - * - * @param {number} a - * @param {number} b - * @returns {number} - */ - function logHypot(a, b) { - - var _a = Math.abs(a); - var _b = Math.abs(b); - - if (a === 0) { - return Math.log(_b); - } - - if (b === 0) { - return Math.log(_a); - } - - if (_a < 3000 && _b < 3000) { - return Math.log(a * a + b * b) * 0.5; - } - - /* I got 4 ideas to compute this property without overflow: - * - * Testing 1000000 times with random samples for a,b ∈ [1, 1000000000] against a big decimal library to get an error estimate - * - * 1. Only eliminate the square root: (OVERALL ERROR: 3.9122483030951116e-11) - - Math.log(a * a + b * b) / 2 - - * - * - * 2. Try to use the non-overflowing pythagoras: (OVERALL ERROR: 8.889760039210159e-10) - - var fn = function(a, b) { - a = Math.abs(a); - b = Math.abs(b); - var t = Math.min(a, b); - a = Math.max(a, b); - t = t / a; - - return Math.log(a) + Math.log(1 + t * t) / 2; - }; - - * 3. Abuse the identity cos(atan(y/x) = x / sqrt(x^2+y^2): (OVERALL ERROR: 3.4780178737037204e-10) - - Math.log(a / Math.cos(Math.atan2(b, a))) - - * 4. Use 3. and apply log rules: (OVERALL ERROR: 1.2014087502620896e-9) - - Math.log(a) - Math.log(Math.cos(Math.atan2(b, a))) - - */ - - return Math.log(a / Math.cos(Math.atan2(b, a))); - } - - var parse = function (a, b) { - - var z = {'re': 0, 'im': 0}; - - if (a === undefined || a === null) { - z['re'] = - z['im'] = 0; - } else if (b !== undefined) { - z['re'] = a; - z['im'] = b; - } else { - switch (typeof a) { - - case 'object': - - if ('im' in a && 're' in a) { - z['re'] = a['re']; - z['im'] = a['im']; - } else if ('abs' in a && 'arg' in a) { - if (!Number.isFinite(a['abs']) && Number.isFinite(a['arg'])) { - return Complex['INFINITY']; - } - z['re'] = a['abs'] * Math.cos(a['arg']); - z['im'] = a['abs'] * Math.sin(a['arg']); - } else if ('r' in a && 'phi' in a) { - if (!Number.isFinite(a['r']) && Number.isFinite(a['phi'])) { - return Complex['INFINITY']; - } - z['re'] = a['r'] * Math.cos(a['phi']); - z['im'] = a['r'] * Math.sin(a['phi']); - } else if (a.length === 2) { // Quick array check - z['re'] = a[0]; - z['im'] = a[1]; - } else { - parser_exit(); - } - break; - - case 'string': - - z['im'] = /* void */ - z['re'] = 0; - - var tokens = a.match(/\d+\.?\d*e[+-]?\d+|\d+\.?\d*|\.\d+|./g); - var plus = 1; - var minus = 0; - - if (tokens === null) { - parser_exit(); - } - - for (var i = 0; i < tokens.length; i++) { - - var c = tokens[i]; - - if (c === ' ' || c === '\t' || c === '\n') { - /* void */ - } else if (c === '+') { - plus++; - } else if (c === '-') { - minus++; - } else if (c === 'i' || c === 'I') { - - if (plus + minus === 0) { - parser_exit(); - } - - if (tokens[i + 1] !== ' ' && !isNaN(tokens[i + 1])) { - z['im'] += parseFloat((minus % 2 ? '-' : '') + tokens[i + 1]); - i++; - } else { - z['im'] += parseFloat((minus % 2 ? '-' : '') + '1'); - } - plus = minus = 0; - - } else { - - if (plus + minus === 0 || isNaN(c)) { - parser_exit(); - } - - if (tokens[i + 1] === 'i' || tokens[i + 1] === 'I') { - z['im'] += parseFloat((minus % 2 ? '-' : '') + c); - i++; - } else { - z['re'] += parseFloat((minus % 2 ? '-' : '') + c); - } - plus = minus = 0; - } - } - - // Still something on the stack - if (plus + minus > 0) { - parser_exit(); - } - break; - - case 'number': - z['im'] = 0; - z['re'] = a; - break; - - default: - parser_exit(); - } - } - - if (isNaN(z['re']) || isNaN(z['im'])) { - // If a calculation is NaN, we treat it as NaN and don't throw - //parser_exit(); - } - - return z; - }; - - /** - * @constructor - * @returns {Complex} - */ - function Complex(a, b) { - - if (!(this instanceof Complex)) { - return new Complex(a, b); - } - - var z = parse(a, b); - - this['re'] = z['re']; - this['im'] = z['im']; - } - - Complex.prototype = { - - 're': 0, - 'im': 0, - - /** - * Calculates the sign of a complex number, which is a normalized complex - * - * @returns {Complex} - */ - 'sign': function () { - - var abs = this['abs'](); - - return new Complex( - this['re'] / abs, - this['im'] / abs); - }, - - /** - * Adds two complex numbers - * - * @returns {Complex} - */ - 'add': function (a, b) { - - var z = new Complex(a, b); - - // Infinity + Infinity = NaN - if (this['isInfinite']() && z['isInfinite']()) { - return Complex['NAN']; - } - - // Infinity + z = Infinity { where z != Infinity } - if (this['isInfinite']() || z['isInfinite']()) { - return Complex['INFINITY']; - } - - return new Complex( - this['re'] + z['re'], - this['im'] + z['im']); - }, - - /** - * Subtracts two complex numbers - * - * @returns {Complex} - */ - 'sub': function (a, b) { - - var z = new Complex(a, b); - - // Infinity - Infinity = NaN - if (this['isInfinite']() && z['isInfinite']()) { - return Complex['NAN']; - } - - // Infinity - z = Infinity { where z != Infinity } - if (this['isInfinite']() || z['isInfinite']()) { - return Complex['INFINITY']; - } - - return new Complex( - this['re'] - z['re'], - this['im'] - z['im']); - }, - - /** - * Multiplies two complex numbers - * - * @returns {Complex} - */ - 'mul': function (a, b) { - - var z = new Complex(a, b); - - // Infinity * 0 = NaN - if ((this['isInfinite']() && z['isZero']()) || (this['isZero']() && z['isInfinite']())) { - return Complex['NAN']; - } - - // Infinity * z = Infinity { where z != 0 } - if (this['isInfinite']() || z['isInfinite']()) { - return Complex['INFINITY']; - } - - // Short circuit for real values - if (z['im'] === 0 && this['im'] === 0) { - return new Complex(this['re'] * z['re'], 0); - } - - return new Complex( - this['re'] * z['re'] - this['im'] * z['im'], - this['re'] * z['im'] + this['im'] * z['re']); - }, - - /** - * Divides two complex numbers - * - * @returns {Complex} - */ - 'div': function (a, b) { - - var z = new Complex(a, b); - - // 0 / 0 = NaN and Infinity / Infinity = NaN - if ((this['isZero']() && z['isZero']()) || (this['isInfinite']() && z['isInfinite']())) { - return Complex['NAN']; - } - - // Infinity / 0 = Infinity - if (this['isInfinite']() || z['isZero']()) { - return Complex['INFINITY']; - } - - // 0 / Infinity = 0 - if (this['isZero']() || z['isInfinite']()) { - return Complex['ZERO']; - } - - a = this['re']; - b = this['im']; - - var c = z['re']; - var d = z['im']; - var t, x; - - if (0 === d) { - // Divisor is real - return new Complex(a / c, b / c); - } - - if (Math.abs(c) < Math.abs(d)) { - - x = c / d; - t = c * x + d; - - return new Complex( - (a * x + b) / t, - (b * x - a) / t); - - } else { - - x = d / c; - t = d * x + c; - - return new Complex( - (a + b * x) / t, - (b - a * x) / t); - } - }, - - /** - * Calculate the power of two complex numbers - * - * @returns {Complex} - */ - 'pow': function (a, b) { - - var z = new Complex(a, b); - - a = this['re']; - b = this['im']; - - if (z['isZero']()) { - return Complex['ONE']; - } - - // If the exponent is real - if (z['im'] === 0) { - - if (b === 0 && a >= 0) { - - return new Complex(Math.pow(a, z['re']), 0); - - } else if (a === 0) { // If base is fully imaginary - - switch ((z['re'] % 4 + 4) % 4) { - case 0: - return new Complex(Math.pow(b, z['re']), 0); - case 1: - return new Complex(0, Math.pow(b, z['re'])); - case 2: - return new Complex(-Math.pow(b, z['re']), 0); - case 3: - return new Complex(0, -Math.pow(b, z['re'])); - } - } - } - - /* I couldn't find a good formula, so here is a derivation and optimization - * - * z_1^z_2 = (a + bi)^(c + di) - * = exp((c + di) * log(a + bi) - * = pow(a^2 + b^2, (c + di) / 2) * exp(i(c + di)atan2(b, a)) - * =>... - * Re = (pow(a^2 + b^2, c / 2) * exp(-d * atan2(b, a))) * cos(d * log(a^2 + b^2) / 2 + c * atan2(b, a)) - * Im = (pow(a^2 + b^2, c / 2) * exp(-d * atan2(b, a))) * sin(d * log(a^2 + b^2) / 2 + c * atan2(b, a)) - * - * =>... - * Re = exp(c * log(sqrt(a^2 + b^2)) - d * atan2(b, a)) * cos(d * log(sqrt(a^2 + b^2)) + c * atan2(b, a)) - * Im = exp(c * log(sqrt(a^2 + b^2)) - d * atan2(b, a)) * sin(d * log(sqrt(a^2 + b^2)) + c * atan2(b, a)) - * - * => - * Re = exp(c * logsq2 - d * arg(z_1)) * cos(d * logsq2 + c * arg(z_1)) - * Im = exp(c * logsq2 - d * arg(z_1)) * sin(d * logsq2 + c * arg(z_1)) - * - */ - - if (a === 0 && b === 0 && z['re'] > 0 && z['im'] >= 0) { - return Complex['ZERO']; - } - - var arg = Math.atan2(b, a); - var loh = logHypot(a, b); - - a = Math.exp(z['re'] * loh - z['im'] * arg); - b = z['im'] * loh + z['re'] * arg; - return new Complex( - a * Math.cos(b), - a * Math.sin(b)); - }, - - /** - * Calculate the complex square root - * - * @returns {Complex} - */ - 'sqrt': function () { - - var a = this['re']; - var b = this['im']; - var r = this['abs'](); - - var re, im; - - if (a >= 0) { - - if (b === 0) { - return new Complex(Math.sqrt(a), 0); - } - - re = 0.5 * Math.sqrt(2.0 * (r + a)); - } else { - re = Math.abs(b) / Math.sqrt(2 * (r - a)); - } - - if (a <= 0) { - im = 0.5 * Math.sqrt(2.0 * (r - a)); - } else { - im = Math.abs(b) / Math.sqrt(2 * (r + a)); - } - - return new Complex(re, b < 0 ? -im : im); - }, - - /** - * Calculate the complex exponent - * - * @returns {Complex} - */ - 'exp': function () { - - var tmp = Math.exp(this['re']); - - if (this['im'] === 0) { - //return new Complex(tmp, 0); - } - return new Complex( - tmp * Math.cos(this['im']), - tmp * Math.sin(this['im'])); - }, - - /** - * Calculate the complex exponent and subtracts one. - * - * This may be more accurate than `Complex(x).exp().sub(1)` if - * `x` is small. - * - * @returns {Complex} - */ - 'expm1': function () { - - /** - * exp(a + i*b) - 1 - = exp(a) * (cos(b) + j*sin(b)) - 1 - = expm1(a)*cos(b) + cosm1(b) + j*exp(a)*sin(b) - */ - - var a = this['re']; - var b = this['im']; - - return new Complex( - Math.expm1(a) * Math.cos(b) + cosm1(b), - Math.exp(a) * Math.sin(b)); - }, - - /** - * Calculate the natural log - * - * @returns {Complex} - */ - 'log': function () { - - var a = this['re']; - var b = this['im']; - - if (b === 0 && a > 0) { - //return new Complex(Math.log(a), 0); - } - - return new Complex( - logHypot(a, b), - Math.atan2(b, a)); - }, - - /** - * Calculate the magnitude of the complex number - * - * @returns {number} - */ - 'abs': function () { - - return hypot(this['re'], this['im']); - }, - - /** - * Calculate the angle of the complex number - * - * @returns {number} - */ - 'arg': function () { - - return Math.atan2(this['im'], this['re']); - }, - - /** - * Calculate the sine of the complex number - * - * @returns {Complex} - */ - 'sin': function () { - - // sin(c) = (e^b - e^(-b)) / (2i) - - var a = this['re']; - var b = this['im']; - - return new Complex( - Math.sin(a) * cosh(b), - Math.cos(a) * sinh(b)); - }, - - /** - * Calculate the cosine - * - * @returns {Complex} - */ - 'cos': function () { - - // cos(z) = (e^b + e^(-b)) / 2 - - var a = this['re']; - var b = this['im']; - - return new Complex( - Math.cos(a) * cosh(b), - -Math.sin(a) * sinh(b)); - }, - - /** - * Calculate the tangent - * - * @returns {Complex} - */ - 'tan': function () { - - // tan(c) = (e^(ci) - e^(-ci)) / (i(e^(ci) + e^(-ci))) - - var a = 2 * this['re']; - var b = 2 * this['im']; - var d = Math.cos(a) + cosh(b); - - return new Complex( - Math.sin(a) / d, - sinh(b) / d); - }, - - /** - * Calculate the cotangent - * - * @returns {Complex} - */ - 'cot': function () { - - // cot(c) = i(e^(ci) + e^(-ci)) / (e^(ci) - e^(-ci)) - - var a = 2 * this['re']; - var b = 2 * this['im']; - var d = Math.cos(a) - cosh(b); - - return new Complex( - -Math.sin(a) / d, - sinh(b) / d); - }, - - /** - * Calculate the secant - * - * @returns {Complex} - */ - 'sec': function () { - - // sec(c) = 2 / (e^(ci) + e^(-ci)) - - var a = this['re']; - var b = this['im']; - var d = 0.5 * cosh(2 * b) + 0.5 * Math.cos(2 * a); - - return new Complex( - Math.cos(a) * cosh(b) / d, - Math.sin(a) * sinh(b) / d); - }, - - /** - * Calculate the cosecans - * - * @returns {Complex} - */ - 'csc': function () { - - // csc(c) = 2i / (e^(ci) - e^(-ci)) - - var a = this['re']; - var b = this['im']; - var d = 0.5 * cosh(2 * b) - 0.5 * Math.cos(2 * a); - - return new Complex( - Math.sin(a) * cosh(b) / d, - -Math.cos(a) * sinh(b) / d); - }, - - /** - * Calculate the complex arcus sinus - * - * @returns {Complex} - */ - 'asin': function () { - - // asin(c) = -i * log(ci + sqrt(1 - c^2)) - - var a = this['re']; - var b = this['im']; - - var t1 = new Complex( - b * b - a * a + 1, - -2 * a * b)['sqrt'](); - - var t2 = new Complex( - t1['re'] - b, - t1['im'] + a)['log'](); - - return new Complex(t2['im'], -t2['re']); - }, - - /** - * Calculate the complex arcus cosinus - * - * @returns {Complex} - */ - 'acos': function () { - - // acos(c) = i * log(c - i * sqrt(1 - c^2)) - - var a = this['re']; - var b = this['im']; - - var t1 = new Complex( - b * b - a * a + 1, - -2 * a * b)['sqrt'](); - - var t2 = new Complex( - t1['re'] - b, - t1['im'] + a)['log'](); - - return new Complex(Math.PI / 2 - t2['im'], t2['re']); - }, - - /** - * Calculate the complex arcus tangent - * - * @returns {Complex} - */ - 'atan': function () { - - // atan(c) = i / 2 log((i + x) / (i - x)) - - var a = this['re']; - var b = this['im']; - - if (a === 0) { - - if (b === 1) { - return new Complex(0, Infinity); - } - - if (b === -1) { - return new Complex(0, -Infinity); - } - } - - var d = a * a + (1.0 - b) * (1.0 - b); - - var t1 = new Complex( - (1 - b * b - a * a) / d, - -2 * a / d).log(); - - return new Complex(-0.5 * t1['im'], 0.5 * t1['re']); - }, - - /** - * Calculate the complex arcus cotangent - * - * @returns {Complex} - */ - 'acot': function () { - - // acot(c) = i / 2 log((c - i) / (c + i)) - - var a = this['re']; - var b = this['im']; - - if (b === 0) { - return new Complex(Math.atan2(1, a), 0); - } - - var d = a * a + b * b; - return (d !== 0) - ? new Complex( - a / d, - -b / d).atan() - : new Complex( - (a !== 0) ? a / 0 : 0, - (b !== 0) ? -b / 0 : 0).atan(); - }, - - /** - * Calculate the complex arcus secant - * - * @returns {Complex} - */ - 'asec': function () { - - // asec(c) = -i * log(1 / c + sqrt(1 - i / c^2)) - - var a = this['re']; - var b = this['im']; - - if (a === 0 && b === 0) { - return new Complex(0, Infinity); - } - - var d = a * a + b * b; - return (d !== 0) - ? new Complex( - a / d, - -b / d).acos() - : new Complex( - (a !== 0) ? a / 0 : 0, - (b !== 0) ? -b / 0 : 0).acos(); - }, - - /** - * Calculate the complex arcus cosecans - * - * @returns {Complex} - */ - 'acsc': function () { - - // acsc(c) = -i * log(i / c + sqrt(1 - 1 / c^2)) - - var a = this['re']; - var b = this['im']; - - if (a === 0 && b === 0) { - return new Complex(Math.PI / 2, Infinity); - } - - var d = a * a + b * b; - return (d !== 0) - ? new Complex( - a / d, - -b / d).asin() - : new Complex( - (a !== 0) ? a / 0 : 0, - (b !== 0) ? -b / 0 : 0).asin(); - }, - - /** - * Calculate the complex sinh - * - * @returns {Complex} - */ - 'sinh': function () { - - // sinh(c) = (e^c - e^-c) / 2 - - var a = this['re']; - var b = this['im']; - - return new Complex( - sinh(a) * Math.cos(b), - cosh(a) * Math.sin(b)); - }, - - /** - * Calculate the complex cosh - * - * @returns {Complex} - */ - 'cosh': function () { - - // cosh(c) = (e^c + e^-c) / 2 - - var a = this['re']; - var b = this['im']; - - return new Complex( - cosh(a) * Math.cos(b), - sinh(a) * Math.sin(b)); - }, - - /** - * Calculate the complex tanh - * - * @returns {Complex} - */ - 'tanh': function () { - - // tanh(c) = (e^c - e^-c) / (e^c + e^-c) - - var a = 2 * this['re']; - var b = 2 * this['im']; - var d = cosh(a) + Math.cos(b); - - return new Complex( - sinh(a) / d, - Math.sin(b) / d); - }, - - /** - * Calculate the complex coth - * - * @returns {Complex} - */ - 'coth': function () { - - // coth(c) = (e^c + e^-c) / (e^c - e^-c) - - var a = 2 * this['re']; - var b = 2 * this['im']; - var d = cosh(a) - Math.cos(b); - - return new Complex( - sinh(a) / d, - -Math.sin(b) / d); - }, - - /** - * Calculate the complex coth - * - * @returns {Complex} - */ - 'csch': function () { - - // csch(c) = 2 / (e^c - e^-c) - - var a = this['re']; - var b = this['im']; - var d = Math.cos(2 * b) - cosh(2 * a); - - return new Complex( - -2 * sinh(a) * Math.cos(b) / d, - 2 * cosh(a) * Math.sin(b) / d); - }, - - /** - * Calculate the complex sech - * - * @returns {Complex} - */ - 'sech': function () { - - // sech(c) = 2 / (e^c + e^-c) - - var a = this['re']; - var b = this['im']; - var d = Math.cos(2 * b) + cosh(2 * a); - - return new Complex( - 2 * cosh(a) * Math.cos(b) / d, - -2 * sinh(a) * Math.sin(b) / d); - }, - - /** - * Calculate the complex asinh - * - * @returns {Complex} - */ - 'asinh': function () { - - // asinh(c) = log(c + sqrt(c^2 + 1)) - - var tmp = this['im']; - this['im'] = -this['re']; - this['re'] = tmp; - var res = this['asin'](); - - this['re'] = -this['im']; - this['im'] = tmp; - tmp = res['re']; - - res['re'] = -res['im']; - res['im'] = tmp; - return res; - }, - - /** - * Calculate the complex asinh - * - * @returns {Complex} - */ - 'acosh': function () { - - // acosh(c) = log(c + sqrt(c^2 - 1)) - - var res = this['acos'](); - if (res['im'] <= 0) { - var tmp = res['re']; - res['re'] = -res['im']; - res['im'] = tmp; - } else { - var tmp = res['im']; - res['im'] = -res['re']; - res['re'] = tmp; - } - return res; - }, - - /** - * Calculate the complex atanh - * - * @returns {Complex} - */ - 'atanh': function () { - - // atanh(c) = log((1+c) / (1-c)) / 2 - - var a = this['re']; - var b = this['im']; - - var noIM = a > 1 && b === 0; - var oneMinus = 1 - a; - var onePlus = 1 + a; - var d = oneMinus * oneMinus + b * b; - - var x = (d !== 0) - ? new Complex( - (onePlus * oneMinus - b * b) / d, - (b * oneMinus + onePlus * b) / d) - : new Complex( - (a !== -1) ? (a / 0) : 0, - (b !== 0) ? (b / 0) : 0); - - var temp = x['re']; - x['re'] = logHypot(x['re'], x['im']) / 2; - x['im'] = Math.atan2(x['im'], temp) / 2; - if (noIM) { - x['im'] = -x['im']; - } - return x; - }, - - /** - * Calculate the complex acoth - * - * @returns {Complex} - */ - 'acoth': function () { - - // acoth(c) = log((c+1) / (c-1)) / 2 - - var a = this['re']; - var b = this['im']; - - if (a === 0 && b === 0) { - return new Complex(0, Math.PI / 2); - } - - var d = a * a + b * b; - return (d !== 0) - ? new Complex( - a / d, - -b / d).atanh() - : new Complex( - (a !== 0) ? a / 0 : 0, - (b !== 0) ? -b / 0 : 0).atanh(); - }, - - /** - * Calculate the complex acsch - * - * @returns {Complex} - */ - 'acsch': function () { - - // acsch(c) = log((1+sqrt(1+c^2))/c) - - var a = this['re']; - var b = this['im']; - - if (b === 0) { - - return new Complex( - (a !== 0) - ? Math.log(a + Math.sqrt(a * a + 1)) - : Infinity, 0); - } - - var d = a * a + b * b; - return (d !== 0) - ? new Complex( - a / d, - -b / d).asinh() - : new Complex( - (a !== 0) ? a / 0 : 0, - (b !== 0) ? -b / 0 : 0).asinh(); - }, - - /** - * Calculate the complex asech - * - * @returns {Complex} - */ - 'asech': function () { - - // asech(c) = log((1+sqrt(1-c^2))/c) - - var a = this['re']; - var b = this['im']; - - if (this['isZero']()) { - return Complex['INFINITY']; - } - - var d = a * a + b * b; - return (d !== 0) - ? new Complex( - a / d, - -b / d).acosh() - : new Complex( - (a !== 0) ? a / 0 : 0, - (b !== 0) ? -b / 0 : 0).acosh(); - }, - - /** - * Calculate the complex inverse 1/z - * - * @returns {Complex} - */ - 'inverse': function () { - - // 1 / 0 = Infinity and 1 / Infinity = 0 - if (this['isZero']()) { - return Complex['INFINITY']; - } - - if (this['isInfinite']()) { - return Complex['ZERO']; - } - - var a = this['re']; - var b = this['im']; - - var d = a * a + b * b; - - return new Complex(a / d, -b / d); - }, - - /** - * Returns the complex conjugate - * - * @returns {Complex} - */ - 'conjugate': function () { - - return new Complex(this['re'], -this['im']); - }, - - /** - * Gets the negated complex number - * - * @returns {Complex} - */ - 'neg': function () { - - return new Complex(-this['re'], -this['im']); - }, - - /** - * Ceils the actual complex number - * - * @returns {Complex} - */ - 'ceil': function (places) { - - places = Math.pow(10, places || 0); - - return new Complex( - Math.ceil(this['re'] * places) / places, - Math.ceil(this['im'] * places) / places); - }, - - /** - * Floors the actual complex number - * - * @returns {Complex} - */ - 'floor': function (places) { - - places = Math.pow(10, places || 0); - - return new Complex( - Math.floor(this['re'] * places) / places, - Math.floor(this['im'] * places) / places); - }, - - /** - * Ceils the actual complex number - * - * @returns {Complex} - */ - 'round': function (places) { - - places = Math.pow(10, places || 0); - - return new Complex( - Math.round(this['re'] * places) / places, - Math.round(this['im'] * places) / places); - }, - - /** - * Compares two complex numbers - * - * **Note:** new Complex(Infinity).equals(Infinity) === false - * - * @returns {boolean} - */ - 'equals': function (a, b) { - - var z = new Complex(a, b); - - return Math.abs(z['re'] - this['re']) <= Complex['EPSILON'] && - Math.abs(z['im'] - this['im']) <= Complex['EPSILON']; - }, - - /** - * Clones the actual object - * - * @returns {Complex} - */ - 'clone': function () { - - return new Complex(this['re'], this['im']); - }, - - /** - * Gets a string of the actual complex number - * - * @returns {string} - */ - 'toString': function () { - - var a = this['re']; - var b = this['im']; - var ret = ''; - - if (this['isNaN']()) { - return 'NaN'; - } - - if (this['isZero']()) { - return '0'; - } - - if (this['isInfinite']()) { - return 'Infinity'; - } - - if (a !== 0) { - ret += a; - } - - if (b !== 0) { - - if (a !== 0) { - ret += b < 0 ? ' - ' : ' + '; - } else if (b < 0) { - ret += '-'; - } - - b = Math.abs(b); - - if (1 !== b) { - ret += b; - } - ret += 'i'; - } - - if (!ret) { - return '0'; - } - - return ret; - }, - - /** - * Returns the actual number as a vector - * - * @returns {Array} - */ - 'toVector': function () { - - return [this['re'], this['im']]; - }, - - /** - * Returns the actual real value of the current object - * - * @returns {number|null} - */ - 'valueOf': function () { - - if (this['im'] === 0) { - return this['re']; - } - return null; - }, - - /** - * Determines whether a complex number is not on the Riemann sphere. - * - * @returns {boolean} - */ - 'isNaN': function () { - return isNaN(this['re']) || isNaN(this['im']); - }, - - /** - * Determines whether or not a complex number is at the zero pole of the - * Riemann sphere. - * - * @returns {boolean} - */ - 'isZero': function () { - return ( - (this['re'] === 0 || this['re'] === -0) && - (this['im'] === 0 || this['im'] === -0) - ); - }, - - /** - * Determines whether a complex number is not at the infinity pole of the - * Riemann sphere. - * - * @returns {boolean} - */ - 'isFinite': function () { - return isFinite(this['re']) && isFinite(this['im']); - }, - - /** - * Determines whether or not a complex number is at the infinity pole of the - * Riemann sphere. - * - * @returns {boolean} - */ - 'isInfinite': function () { - return !(this['isNaN']() || this['isFinite']()); - } - }; - - Complex['ZERO'] = new Complex(0, 0); - Complex['ONE'] = new Complex(1, 0); - Complex['I'] = new Complex(0, 1); - Complex['PI'] = new Complex(Math.PI, 0); - Complex['E'] = new Complex(Math.E, 0); - Complex['INFINITY'] = new Complex(Infinity, Infinity); - Complex['NAN'] = new Complex(NaN, NaN); - Complex['EPSILON'] = 1e-16; - - if (true) { - !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = (function () { - return Complex; - }).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), - __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); - } else {} - - })(this); - - /***/ }), - /* 9 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function () { - return IndexError; - }); - /** - * Create a range error with the message: - * 'Index out of range (index < min)' - * 'Index out of range (index < max)' - * - * @param {number} index The actual index - * @param {number} [min=0] Minimum index (included) - * @param {number} [max] Maximum index (excluded) - * @extends RangeError - */ - function IndexError(index, min, max) { - if (!(this instanceof IndexError)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this.index = index; - - if (arguments.length < 3) { - this.min = 0; - this.max = min; - } else { - this.min = min; - this.max = max; - } - - if (this.min !== undefined && this.index < this.min) { - this.message = 'Index out of range (' + this.index + ' < ' + this.min + ')'; - } else if (this.max !== undefined && this.index >= this.max) { - this.message = 'Index out of range (' + this.index + ' > ' + (this.max - 1) + ')'; - } else { - this.message = 'Index out of range (' + this.index + ')'; - } - - this.stack = new Error().stack; - } - IndexError.prototype = new RangeError(); - IndexError.prototype.constructor = RangeError; - IndexError.prototype.name = 'IndexError'; - IndexError.prototype.isIndexError = true; - - /***/ }), - /* 10 */ - /***/ (function (module, exports, __webpack_require__) { - - var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/** - * @license Fraction.js v4.0.12 09/09/2015 - * http://www.xarg.org/2014/03/rational-numbers-in-javascript/ - * - * Copyright (c) 2015, Robert Eisele (robert@xarg.org) - * Dual licensed under the MIT or GPL Version 2 licenses. - **/ - - /** - * - * This class offers the possibility to calculate fractions. - * You can pass a fraction in different formats. Either as array, as double, as string or as an integer. - * - * Array/Object form - * [ 0 => , 1 => ] - * [ n => , d => ] - * - * Integer form - * - Single integer value - * - * Double form - * - Single double value - * - * String form - * 123.456 - a simple double - * 123/456 - a string fraction - * 123.'456' - a double with repeating decimal places - * 123.(456) - synonym - * 123.45'6' - a double with repeating last place - * 123.45(6) - synonym - * - * Example: - * - * var f = new Fraction("9.4'31'"); - * f.mul([-4, 3]).div(4.9); - * - */ - - (function (root) { - - "use strict"; - - // Maximum search depth for cyclic rational numbers. 2000 should be more than enough. - // Example: 1/7 = 0.(142857) has 6 repeating decimal places. - // If MAX_CYCLE_LEN gets reduced, long cycles will not be detected and toString() only gets the first 10 digits - var MAX_CYCLE_LEN = 2000; - - // Parsed data to avoid calling "new" all the time - var P = { - "s": 1, - "n": 0, - "d": 1 - }; - - function createError(name) { - - function errorConstructor() { - var temp = Error.apply(this, arguments); - temp['name'] = this['name'] = name; - this['stack'] = temp['stack']; - this['message'] = temp['message']; - } - - /** - * Error constructor - * - * @constructor - */ - function IntermediateInheritor() {} - IntermediateInheritor.prototype = Error.prototype; - errorConstructor.prototype = new IntermediateInheritor(); - - return errorConstructor; - } - - var DivisionByZero = Fraction['DivisionByZero'] = createError('DivisionByZero'); - var InvalidParameter = Fraction['InvalidParameter'] = createError('InvalidParameter'); - - function assign(n, s) { - - if (isNaN(n = parseInt(n, 10))) { - throwInvalidParam(); - } - return n * s; - } - - function throwInvalidParam() { - throw new InvalidParameter(); - } - - var parse = function (p1, p2) { - - var n = 0, d = 1, s = 1; - var v = 0, w = 0, x = 0, y = 1, z = 1; - - var A = 0, B = 1; - var C = 1, D = 1; - - var N = 10000000; - var M; - - if (p1 === undefined || p1 === null) { - /* void */ - } else if (p2 !== undefined) { - n = p1; - d = p2; - s = n * d; - } else { - switch (typeof p1) { - - case "object": - { - if ("d" in p1 && "n" in p1) { - n = p1["n"]; - d = p1["d"]; - if ("s" in p1) { - n *= p1["s"]; - } - } else if (0 in p1) { - n = p1[0]; - if (1 in p1) { - d = p1[1]; - } - } else { - throwInvalidParam(); - } - s = n * d; - break; - } - case "number": - { - if (p1 < 0) { - s = p1; - p1 = -p1; - } - - if (p1 % 1 === 0) { - n = p1; - } else if (p1 > 0) { // check for != 0, scale would become NaN (log(0)), which converges really slow - - if (p1 >= 1) { - z = Math.pow(10, Math.floor(1 + Math.log(p1) / Math.LN10)); - p1 /= z; - } - - // Using Farey Sequences - // http://www.johndcook.com/blog/2010/10/20/best-rational-approximation/ - - while (B <= N && D <= N) { - M = (A + C) / (B + D); - - if (p1 === M) { - if (B + D <= N) { - n = A + C; - d = B + D; - } else if (D > B) { - n = C; - d = D; - } else { - n = A; - d = B; - } - break; - - } else { - - if (p1 > M) { - A += C; - B += D; - } else { - C += A; - D += B; - } - - if (B > N) { - n = C; - d = D; - } else { - n = A; - d = B; - } - } - } - n *= z; - } else if (isNaN(p1) || isNaN(p2)) { - d = n = NaN; - } - break; - } - case "string": - { - B = p1.match(/\d+|./g); - - if (B === null) { - throwInvalidParam(); - } - - if (B[A] === '-') {// Check for minus sign at the beginning - s = -1; - A++; - } else if (B[A] === '+') {// Check for plus sign at the beginning - A++; - } - - if (B.length === A + 1) { // Check if it's just a simple number "1234" - w = assign(B[A++], s); - } else if (B[A + 1] === '.' || B[A] === '.') { // Check if it's a decimal number - - if (B[A] !== '.') { // Handle 0.5 and .5 - v = assign(B[A++], s); - } - A++; - - // Check for decimal places - if (A + 1 === B.length || B[A + 1] === '(' && B[A + 3] === ')' || B[A + 1] === "'" && B[A + 3] === "'") { - w = assign(B[A], s); - y = Math.pow(10, B[A].length); - A++; - } - - // Check for repeating places - if (B[A] === '(' && B[A + 2] === ')' || B[A] === "'" && B[A + 2] === "'") { - x = assign(B[A + 1], s); - z = Math.pow(10, B[A + 1].length) - 1; - A += 3; - } - - } else if (B[A + 1] === '/' || B[A + 1] === ':') { // Check for a simple fraction "123/456" or "123:456" - w = assign(B[A], s); - y = assign(B[A + 2], 1); - A += 3; - } else if (B[A + 3] === '/' && B[A + 1] === ' ') { // Check for a complex fraction "123 1/2" - v = assign(B[A], s); - w = assign(B[A + 2], s); - y = assign(B[A + 4], 1); - A += 5; - } - - if (B.length <= A) { // Check for more tokens on the stack - d = y * z; - s = /* void */ - n = x + d * v + z * w; - break; - } - - /* Fall through on error */ - } - default: - throwInvalidParam(); - } - } - - if (d === 0) { - throw new DivisionByZero(); - } - - P["s"] = s < 0 ? -1 : 1; - P["n"] = Math.abs(n); - P["d"] = Math.abs(d); - }; - - function modpow(b, e, m) { - - var r = 1; - for (; e > 0; b = (b * b) % m, e >>= 1) { - - if (e & 1) { - r = (r * b) % m; - } - } - return r; - } - - function cycleLen(n, d) { - - for (; d % 2 === 0; - d /= 2) { - } - - for (; d % 5 === 0; - d /= 5) { - } - - if (d === 1) // Catch non-cyclic numbers - { - return 0; - } - - // If we would like to compute really large numbers quicker, we could make use of Fermat's little theorem: - // 10^(d-1) % d == 1 - // However, we don't need such large numbers and MAX_CYCLE_LEN should be the capstone, - // as we want to translate the numbers to strings. - - var rem = 10 % d; - var t = 1; - - for (; rem !== 1; t++) { - rem = rem * 10 % d; - - if (t > MAX_CYCLE_LEN) { - return 0; - } // Returning 0 here means that we don't print it as a cyclic number. It's likely that the answer is `d-1` - } - return t; - } - - function cycleStart(n, d, len) { - - var rem1 = 1; - var rem2 = modpow(10, len, d); - - for (var t = 0; t < 300; t++) { // s < ~log10(Number.MAX_VALUE) - // Solve 10^s == 10^(s+t) (mod d) - - if (rem1 === rem2) { - return t; - } - - rem1 = rem1 * 10 % d; - rem2 = rem2 * 10 % d; - } - return 0; - } - - function gcd(a, b) { - - if (!a) { - return b; - } - if (!b) { - return a; - } - - while (1) { - a %= b; - if (!a) { - return b; - } - b %= a; - if (!b) { - return a; - } - } - }; - - /** - * Module constructor - * - * @constructor - * @param {number|Fraction=} a - * @param {number=} b - */ - function Fraction(a, b) { - - if (!(this instanceof Fraction)) { - return new Fraction(a, b); - } - - parse(a, b); - - if (Fraction['REDUCE']) { - a = gcd(P["d"], P["n"]); // Abuse a - } else { - a = 1; - } - - this["s"] = P["s"]; - this["n"] = P["n"] / a; - this["d"] = P["d"] / a; - } - - /** - * Boolean global variable to be able to disable automatic reduction of the fraction - * - */ - Fraction['REDUCE'] = 1; - - Fraction.prototype = { - - "s": 1, - "n": 0, - "d": 1, - - /** - * Calculates the absolute value - * - * Ex: new Fraction(-4).abs() => 4 - **/ - "abs": function () { - - return new Fraction(this["n"], this["d"]); - }, - - /** - * Inverts the sign of the current fraction - * - * Ex: new Fraction(-4).neg() => 4 - **/ - "neg": function () { - - return new Fraction(-this["s"] * this["n"], this["d"]); - }, - - /** - * Adds two rational numbers - * - * Ex: new Fraction({n: 2, d: 3}).add("14.9") => 467 / 30 - **/ - "add": function (a, b) { - - parse(a, b); - return new Fraction( - this["s"] * this["n"] * P["d"] + P["s"] * this["d"] * P["n"], - this["d"] * P["d"] - ); - }, - - /** - * Subtracts two rational numbers - * - * Ex: new Fraction({n: 2, d: 3}).add("14.9") => -427 / 30 - **/ - "sub": function (a, b) { - - parse(a, b); - return new Fraction( - this["s"] * this["n"] * P["d"] - P["s"] * this["d"] * P["n"], - this["d"] * P["d"] - ); - }, - - /** - * Multiplies two rational numbers - * - * Ex: new Fraction("-17.(345)").mul(3) => 5776 / 111 - **/ - "mul": function (a, b) { - - parse(a, b); - return new Fraction( - this["s"] * P["s"] * this["n"] * P["n"], - this["d"] * P["d"] - ); - }, - - /** - * Divides two rational numbers - * - * Ex: new Fraction("-17.(345)").inverse().div(3) - **/ - "div": function (a, b) { - - parse(a, b); - return new Fraction( - this["s"] * P["s"] * this["n"] * P["d"], - this["d"] * P["n"] - ); - }, - - /** - * Clones the actual object - * - * Ex: new Fraction("-17.(345)").clone() - **/ - "clone": function () { - return new Fraction(this); - }, - - /** - * Calculates the modulo of two rational numbers - a more precise fmod - * - * Ex: new Fraction('4.(3)').mod([7, 8]) => (13/3) % (7/8) = (5/6) - **/ - "mod": function (a, b) { - - if (isNaN(this['n']) || isNaN(this['d'])) { - return new Fraction(NaN); - } - - if (a === undefined) { - return new Fraction(this["s"] * this["n"] % this["d"], 1); - } - - parse(a, b); - if (0 === P["n"] && 0 === this["d"]) { - Fraction(0, 0); // Throw DivisionByZero - } - - /* - * First silly attempt, kinda slow - * - return that["sub"]({ - "n": num["n"] * Math.floor((this.n / this.d) / (num.n / num.d)), - "d": num["d"], - "s": this["s"] - });*/ - - /* - * New attempt: a1 / b1 = a2 / b2 * q + r - * => b2 * a1 = a2 * b1 * q + b1 * b2 * r - * => (b2 * a1 % a2 * b1) / (b1 * b2) - */ - return new Fraction( - this["s"] * (P["d"] * this["n"]) % (P["n"] * this["d"]), - P["d"] * this["d"] - ); - }, - - /** - * Calculates the fractional gcd of two rational numbers - * - * Ex: new Fraction(5,8).gcd(3,7) => 1/56 - */ - "gcd": function (a, b) { - - parse(a, b); - - // gcd(a / b, c / d) = gcd(a, c) / lcm(b, d) - - return new Fraction(gcd(P["n"], this["n"]) * gcd(P["d"], this["d"]), P["d"] * this["d"]); - }, - - /** - * Calculates the fractional lcm of two rational numbers - * - * Ex: new Fraction(5,8).lcm(3,7) => 15 - */ - "lcm": function (a, b) { - - parse(a, b); - - // lcm(a / b, c / d) = lcm(a, c) / gcd(b, d) - - if (P["n"] === 0 && this["n"] === 0) { - return new Fraction; - } - return new Fraction(P["n"] * this["n"], gcd(P["n"], this["n"]) * gcd(P["d"], this["d"])); - }, - - /** - * Calculates the ceil of a rational number - * - * Ex: new Fraction('4.(3)').ceil() => (5 / 1) - **/ - "ceil": function (places) { - - places = Math.pow(10, places || 0); - - if (isNaN(this["n"]) || isNaN(this["d"])) { - return new Fraction(NaN); - } - return new Fraction(Math.ceil(places * this["s"] * this["n"] / this["d"]), places); - }, - - /** - * Calculates the floor of a rational number - * - * Ex: new Fraction('4.(3)').floor() => (4 / 1) - **/ - "floor": function (places) { - - places = Math.pow(10, places || 0); - - if (isNaN(this["n"]) || isNaN(this["d"])) { - return new Fraction(NaN); - } - return new Fraction(Math.floor(places * this["s"] * this["n"] / this["d"]), places); - }, - - /** - * Rounds a rational numbers - * - * Ex: new Fraction('4.(3)').round() => (4 / 1) - **/ - "round": function (places) { - - places = Math.pow(10, places || 0); - - if (isNaN(this["n"]) || isNaN(this["d"])) { - return new Fraction(NaN); - } - return new Fraction(Math.round(places * this["s"] * this["n"] / this["d"]), places); - }, - - /** - * Gets the inverse of the fraction, means numerator and denumerator are exchanged - * - * Ex: new Fraction([-3, 4]).inverse() => -4 / 3 - **/ - "inverse": function () { - - return new Fraction(this["s"] * this["d"], this["n"]); - }, - - /** - * Calculates the fraction to some integer exponent - * - * Ex: new Fraction(-1,2).pow(-3) => -8 - */ - "pow": function (m) { - - if (m < 0) { - return new Fraction(Math.pow(this['s'] * this["d"], -m), Math.pow(this["n"], -m)); - } else { - return new Fraction(Math.pow(this['s'] * this["n"], m), Math.pow(this["d"], m)); - } - }, - - /** - * Check if two rational numbers are the same - * - * Ex: new Fraction(19.6).equals([98, 5]); - **/ - "equals": function (a, b) { - - parse(a, b); - return this["s"] * this["n"] * P["d"] === P["s"] * P["n"] * this["d"]; // Same as compare() === 0 - }, - - /** - * Check if two rational numbers are the same - * - * Ex: new Fraction(19.6).equals([98, 5]); - **/ - "compare": function (a, b) { - - parse(a, b); - var t = (this["s"] * this["n"] * P["d"] - P["s"] * P["n"] * this["d"]); - return (0 < t) - (t < 0); - }, - - "simplify": function (eps) { - - // First naive implementation, needs improvement - - if (isNaN(this['n']) || isNaN(this['d'])) { - return this; - } - - var cont = this['abs']()['toContinued'](); - - eps = eps || 0.001; - - function rec(a) { - if (a.length === 1) { - return new Fraction(a[0]); - } - return rec(a.slice(1))['inverse']()['add'](a[0]); - } - - for (var i = 0; i < cont.length; i++) { - var tmp = rec(cont.slice(0, i + 1)); - if (tmp['sub'](this['abs']())['abs']().valueOf() < eps) { - return tmp['mul'](this['s']); - } - } - return this; - }, - - /** - * Check if two rational numbers are divisible - * - * Ex: new Fraction(19.6).divisible(1.5); - */ - "divisible": function (a, b) { - - parse(a, b); - return !(!(P["n"] * this["d"]) || ((this["n"] * P["d"]) % (P["n"] * this["d"]))); - }, - - /** - * Returns a decimal representation of the fraction - * - * Ex: new Fraction("100.'91823'").valueOf() => 100.91823918239183 - **/ - 'valueOf': function () { - - return this["s"] * this["n"] / this["d"]; - }, - - /** - * Returns a string-fraction representation of a Fraction object - * - * Ex: new Fraction("1.'3'").toFraction() => "4 1/3" - **/ - 'toFraction': function (excludeWhole) { - - var whole, str = ""; - var n = this["n"]; - var d = this["d"]; - if (this["s"] < 0) { - str += '-'; - } - - if (d === 1) { - str += n; - } else { - - if (excludeWhole && (whole = Math.floor(n / d)) > 0) { - str += whole; - str += " "; - n %= d; - } - - str += n; - str += '/'; - str += d; - } - return str; - }, - - /** - * Returns a latex representation of a Fraction object - * - * Ex: new Fraction("1.'3'").toLatex() => "\frac{4}{3}" - **/ - 'toLatex': function (excludeWhole) { - - var whole, str = ""; - var n = this["n"]; - var d = this["d"]; - if (this["s"] < 0) { - str += '-'; - } - - if (d === 1) { - str += n; - } else { - - if (excludeWhole && (whole = Math.floor(n / d)) > 0) { - str += whole; - n %= d; - } - - str += "\\frac{"; - str += n; - str += '}{'; - str += d; - str += '}'; - } - return str; - }, - - /** - * Returns an array of continued fraction elements - * - * Ex: new Fraction("7/8").toContinued() => [0,1,7] - */ - 'toContinued': function () { - - var t; - var a = this['n']; - var b = this['d']; - var res = []; - - if (isNaN(this['n']) || isNaN(this['d'])) { - return res; - } - - do { - res.push(Math.floor(a / b)); - t = a % b; - a = b; - b = t; - } while (a !== 1); - - return res; - }, - - /** - * Creates a string representation of a fraction with all digits - * - * Ex: new Fraction("100.'91823'").toString() => "100.(91823)" - **/ - 'toString': function (dec) { - - var g; - var N = this["n"]; - var D = this["d"]; - - if (isNaN(N) || isNaN(D)) { - return "NaN"; - } - - if (!Fraction['REDUCE']) { - g = gcd(N, D); - N /= g; - D /= g; - } - - dec = dec || 15; // 15 = decimal places when no repitation - - var cycLen = cycleLen(N, D); // Cycle length - var cycOff = cycleStart(N, D, cycLen); // Cycle start - - var str = this['s'] === -1 ? "-" : ""; - - str += N / D | 0; - - N %= D; - N *= 10; - - if (N) { - str += "."; - } - - if (cycLen) { - - for (var i = cycOff; i--;) { - str += N / D | 0; - N %= D; - N *= 10; - } - str += "("; - for (var i = cycLen; i--;) { - str += N / D | 0; - N %= D; - N *= 10; - } - str += ")"; - } else { - for (var i = dec; N && i--;) { - str += N / D | 0; - N %= D; - N *= 10; - } - } - return str; - } - }; - - if (true) { - !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = (function () { - return Fraction; - }).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), - __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); - } else {} - - })(this); - - /***/ }), - /* 11 */ - /***/ (function (module, exports) { - - /* - * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license - * Author: Jim Palmer (based on chunking idea from Dave Koelle) - */ - /*jshint unused:false */ - module.exports = function naturalSort(a, b) { - "use strict"; - var re = /(^([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)?$|^0x[0-9a-f]+$|\d+)/gi, - sre = /(^[ ]*|[ ]*$)/g, - dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, - hre = /^0x[0-9a-f]+$/i, - ore = /^0/, - i = function (s) { - return naturalSort.insensitive && ('' + s).toLowerCase() || '' + s; - }, - // convert all to strings strip whitespace - x = i(a).replace(sre, '') || '', - y = i(b).replace(sre, '') || '', - // chunk/tokenize - xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), - yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), - // numeric, hex or date detection - xD = parseInt(x.match(hre), 16) || (xN.length !== 1 && x.match(dre) && Date.parse(x)), - yD = parseInt(y.match(hre), 16) || xD && y.match(dre) && Date.parse(y) || null, - oFxNcL, oFyNcL; - // first try and sort Hex codes or Dates - if (yD) { - if (xD < yD) { - return -1; - } else if (xD > yD) { - return 1; - } - } - // natural sorting through split numeric strings and default strings - for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) { - // find floats not starting with '0', string or 0 if not defined (Clint Priest) - oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0; - oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0; - // handle numeric vs string comparison - number < string - (Kyle Adams) - if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { - return (isNaN(oFxNcL)) ? 1 : -1; - } - // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2' - else if (typeof oFxNcL !== typeof oFyNcL) { - oFxNcL += ''; - oFyNcL += ''; - } - if (oFxNcL < oFyNcL) { - return -1; - } - if (oFxNcL > oFyNcL) { - return 1; - } - } - return 0; - }; - - /***/ }), - /* 12 */ - /***/ (function (module, exports, __webpack_require__) { - - var __WEBPACK_AMD_DEFINE_RESULT__; ;(function (globalScope) { - 'use strict'; - - /* - * decimal.js v10.2.1 - * An arbitrary-precision Decimal type for JavaScript. - * https://github.com/MikeMcl/decimal.js - * Copyright (c) 2020 Michael Mclaughlin - * MIT Licence - */ - - // ----------------------------------- EDITABLE DEFAULTS ------------------------------------ // - - // The maximum exponent magnitude. - // The limit on the value of `toExpNeg`, `toExpPos`, `minE` and `maxE`. - var EXP_LIMIT = 9e15, // 0 to 9e15 - - // The limit on the value of `precision`, and on the value of the first argument to - // `toDecimalPlaces`, `toExponential`, `toFixed`, `toPrecision` and `toSignificantDigits`. - MAX_DIGITS = 1e9, // 0 to 1e9 - - // Base conversion alphabet. - NUMERALS = '0123456789abcdef', - - // The natural logarithm of 10 (1025 digits). - LN10 = '2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058', - - // Pi (1025 digits). - PI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789', - - // The initial configuration properties of the Decimal constructor. - DEFAULTS = { - - // These values must be integers within the stated ranges (inclusive). - // Most of these values can be changed at run-time using the `Decimal.config` method. - - // The maximum number of significant digits of the result of a calculation or base conversion. - // E.g. `Decimal.config({ precision: 20 });` - precision: 20, // 1 to MAX_DIGITS - - // The rounding mode used when rounding to `precision`. - // - // ROUND_UP 0 Away from zero. - // ROUND_DOWN 1 Towards zero. - // ROUND_CEIL 2 Towards +Infinity. - // ROUND_FLOOR 3 Towards -Infinity. - // ROUND_HALF_UP 4 Towards nearest neighbour. If equidistant, up. - // ROUND_HALF_DOWN 5 Towards nearest neighbour. If equidistant, down. - // ROUND_HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour. - // ROUND_HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity. - // ROUND_HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity. - // - // E.g. - // `Decimal.rounding = 4;` - // `Decimal.rounding = Decimal.ROUND_HALF_UP;` - rounding: 4, // 0 to 8 - - // The modulo mode used when calculating the modulus: a mod n. - // The quotient (q = a / n) is calculated according to the corresponding rounding mode. - // The remainder (r) is calculated as: r = a - n * q. - // - // UP 0 The remainder is positive if the dividend is negative, else is negative. - // DOWN 1 The remainder has the same sign as the dividend (JavaScript %). - // FLOOR 3 The remainder has the same sign as the divisor (Python %). - // HALF_EVEN 6 The IEEE 754 remainder function. - // EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). Always positive. - // - // Truncated division (1), floored division (3), the IEEE 754 remainder (6), and Euclidian - // division (9) are commonly used for the modulus operation. The other rounding modes can also - // be used, but they may not give useful results. - modulo: 1, // 0 to 9 - - // The exponent value at and beneath which `toString` returns exponential notation. - // JavaScript numbers: -7 - toExpNeg: -7, // 0 to -EXP_LIMIT - - // The exponent value at and above which `toString` returns exponential notation. - // JavaScript numbers: 21 - toExpPos: 21, // 0 to EXP_LIMIT - - // The minimum exponent value, beneath which underflow to zero occurs. - // JavaScript numbers: -324 (5e-324) - minE: -EXP_LIMIT, // -1 to -EXP_LIMIT - - // The maximum exponent value, above which overflow to Infinity occurs. - // JavaScript numbers: 308 (1.7976931348623157e+308) - maxE: EXP_LIMIT, // 1 to EXP_LIMIT - - // Whether to use cryptographically-secure random number generation, if available. - crypto: false // true/false - }, - - // ----------------------------------- END OF EDITABLE DEFAULTS ------------------------------- // - - Decimal, inexact, noConflict, quadrant, - external = true, - - decimalError = '[DecimalError] ', - invalidArgument = decimalError + 'Invalid argument: ', - precisionLimitExceeded = decimalError + 'Precision limit exceeded', - cryptoUnavailable = decimalError + 'crypto unavailable', - - mathfloor = Math.floor, - mathpow = Math.pow, - - isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i, - isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i, - isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i, - isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i, - - BASE = 1e7, - LOG_BASE = 7, - MAX_SAFE_INTEGER = 9007199254740991, - - LN10_PRECISION = LN10.length - 1, - PI_PRECISION = PI.length - 1, - - // Decimal.prototype object - P = { name: '[object Decimal]' }; - - // Decimal prototype methods - - /* - * absoluteValue abs - * ceil - * comparedTo cmp - * cosine cos - * cubeRoot cbrt - * decimalPlaces dp - * dividedBy div - * dividedToIntegerBy divToInt - * equals eq - * floor - * greaterThan gt - * greaterThanOrEqualTo gte - * hyperbolicCosine cosh - * hyperbolicSine sinh - * hyperbolicTangent tanh - * inverseCosine acos - * inverseHyperbolicCosine acosh - * inverseHyperbolicSine asinh - * inverseHyperbolicTangent atanh - * inverseSine asin - * inverseTangent atan - * isFinite - * isInteger isInt - * isNaN - * isNegative isNeg - * isPositive isPos - * isZero - * lessThan lt - * lessThanOrEqualTo lte - * logarithm log - * [maximum] [max] - * [minimum] [min] - * minus sub - * modulo mod - * naturalExponential exp - * naturalLogarithm ln - * negated neg - * plus add - * precision sd - * round - * sine sin - * squareRoot sqrt - * tangent tan - * times mul - * toBinary - * toDecimalPlaces toDP - * toExponential - * toFixed - * toFraction - * toHexadecimal toHex - * toNearest - * toNumber - * toOctal - * toPower pow - * toPrecision - * toSignificantDigits toSD - * toString - * truncated trunc - * valueOf toJSON - */ - - /* - * Return a new Decimal whose value is the absolute value of this Decimal. - * - */ - P.absoluteValue = P.abs = function () { - var x = new this.constructor(this); - if (x.s < 0) { - x.s = 1; - } - return finalise(x); - }; - - /* - * Return a new Decimal whose value is the value of this Decimal rounded to a whole number in the - * direction of positive Infinity. - * - */ - P.ceil = function () { - return finalise(new this.constructor(this), this.e + 1, 2); - }; - - /* - * Return - * 1 if the value of this Decimal is greater than the value of `y`, - * -1 if the value of this Decimal is less than the value of `y`, - * 0 if they have the same value, - * NaN if the value of either Decimal is NaN. - * - */ - P.comparedTo = P.cmp = function (y) { - var i, j, xdL, ydL, - x = this, - xd = x.d, - yd = (y = new x.constructor(y)).d, - xs = x.s, - ys = y.s; - - // Either NaN or ±Infinity? - if (!xd || !yd) { - return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1; - } - - // Either zero? - if (!xd[0] || !yd[0]) { - return xd[0] ? xs : yd[0] ? -ys : 0; - } - - // Signs differ? - if (xs !== ys) { - return xs; - } - - // Compare exponents. - if (x.e !== y.e) { - return x.e > y.e ^ xs < 0 ? 1 : -1; - } - - xdL = xd.length; - ydL = yd.length; - - // Compare digit by digit. - for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) { - if (xd[i] !== yd[i]) { - return xd[i] > yd[i] ^ xs < 0 ? 1 : -1; - } - } - - // Compare lengths. - return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1; - }; - - /* - * Return a new Decimal whose value is the cosine of the value in radians of this Decimal. - * - * Domain: [-Infinity, Infinity] - * Range: [-1, 1] - * - * cos(0) = 1 - * cos(-0) = 1 - * cos(Infinity) = NaN - * cos(-Infinity) = NaN - * cos(NaN) = NaN - * - */ - P.cosine = P.cos = function () { - var pr, rm, - x = this, - Ctor = x.constructor; - - if (!x.d) { - return new Ctor(NaN); - } - - // cos(0) = cos(-0) = 1 - if (!x.d[0]) { - return new Ctor(1); - } - - pr = Ctor.precision; - rm = Ctor.rounding; - Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE; - Ctor.rounding = 1; - - x = cosine(Ctor, toLessThanHalfPi(Ctor, x)); - - Ctor.precision = pr; - Ctor.rounding = rm; - - return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true); - }; - - /* - * - * Return a new Decimal whose value is the cube root of the value of this Decimal, rounded to - * `precision` significant digits using rounding mode `rounding`. - * - * cbrt(0) = 0 - * cbrt(-0) = -0 - * cbrt(1) = 1 - * cbrt(-1) = -1 - * cbrt(N) = N - * cbrt(-I) = -I - * cbrt(I) = I - * - * Math.cbrt(x) = (x < 0 ? -Math.pow(-x, 1/3) : Math.pow(x, 1/3)) - * - */ - P.cubeRoot = P.cbrt = function () { - var e, m, n, r, rep, s, sd, t, t3, t3plusx, - x = this, - Ctor = x.constructor; - - if (!x.isFinite() || x.isZero()) { - return new Ctor(x); - } - external = false; - - // Initial estimate. - s = x.s * mathpow(x.s * x, 1 / 3); - - // Math.cbrt underflow/overflow? - // Pass x to Math.pow as integer, then adjust the exponent of the result. - if (!s || Math.abs(s) == 1 / 0) { - n = digitsToString(x.d); - e = x.e; - - // Adjust n exponent so it is a multiple of 3 away from x exponent. - if (s = (e - n.length + 1) % 3) { - n += (s == 1 || s == -2 ? '0' : '00'); - } - s = mathpow(n, 1 / 3); - - // Rarely, e may be one less than the result exponent value. - e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2)); - - if (s == 1 / 0) { - n = '5e' + e; - } else { - n = s.toExponential(); - n = n.slice(0, n.indexOf('e') + 1) + e; - } - - r = new Ctor(n); - r.s = x.s; - } else { - r = new Ctor(s.toString()); - } - - sd = (e = Ctor.precision) + 3; - - // Halley's method. - // TODO? Compare Newton's method. - for (;;) { - t = r; - t3 = t.times(t).times(t); - t3plusx = t3.plus(x); - r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1); - - // TODO? Replace with for-loop and checkRoundingDigits. - if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) { - n = n.slice(sd - 3, sd + 1); - - // The 4th rounding digit may be in error by -1 so if the 4 rounding digits are 9999 or 4999 - // , i.e. approaching a rounding boundary, continue the iteration. - if (n == '9999' || !rep && n == '4999') { - - // On the first iteration only, check to see if rounding up gives the exact result as the - // nines may infinitely repeat. - if (!rep) { - finalise(t, e + 1, 0); - - if (t.times(t).times(t).eq(x)) { - r = t; - break; - } - } - - sd += 4; - rep = 1; - } else { - - // If the rounding digits are null, 0{0,4} or 50{0,3}, check for an exact result. - // If not, then there are further digits and m will be truthy. - if (!+n || !+n.slice(1) && n.charAt(0) == '5') { - - // Truncate to the first rounding digit. - finalise(r, e + 1, 1); - m = !r.times(r).times(r).eq(x); - } - - break; - } - } - } - - external = true; - - return finalise(r, e, Ctor.rounding, m); - }; - - /* - * Return the number of decimal places of the value of this Decimal. - * - */ - P.decimalPlaces = P.dp = function () { - var w, - d = this.d, - n = NaN; - - if (d) { - w = d.length - 1; - n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE; - - // Subtract the number of trailing zeros of the last word. - w = d[w]; - if (w) { - for (; w % 10 == 0; w /= 10) { - n--; - } - } - if (n < 0) { - n = 0; - } - } - - return n; - }; - - /* - * n / 0 = I - * n / N = N - * n / I = 0 - * 0 / n = 0 - * 0 / 0 = N - * 0 / N = N - * 0 / I = 0 - * N / n = N - * N / 0 = N - * N / N = N - * N / I = N - * I / n = I - * I / 0 = I - * I / N = N - * I / I = N - * - * Return a new Decimal whose value is the value of this Decimal divided by `y`, rounded to - * `precision` significant digits using rounding mode `rounding`. - * - */ - P.dividedBy = P.div = function (y) { - return divide(this, new this.constructor(y)); - }; - - /* - * Return a new Decimal whose value is the integer part of dividing the value of this Decimal - * by the value of `y`, rounded to `precision` significant digits using rounding mode `rounding`. - * - */ - P.dividedToIntegerBy = P.divToInt = function (y) { - var x = this, - Ctor = x.constructor; - return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding); - }; - - /* - * Return true if the value of this Decimal is equal to the value of `y`, otherwise return false. - * - */ - P.equals = P.eq = function (y) { - return this.cmp(y) === 0; - }; - - /* - * Return a new Decimal whose value is the value of this Decimal rounded to a whole number in the - * direction of negative Infinity. - * - */ - P.floor = function () { - return finalise(new this.constructor(this), this.e + 1, 3); - }; - - /* - * Return true if the value of this Decimal is greater than the value of `y`, otherwise return - * false. - * - */ - P.greaterThan = P.gt = function (y) { - return this.cmp(y) > 0; - }; - - /* - * Return true if the value of this Decimal is greater than or equal to the value of `y`, - * otherwise return false. - * - */ - P.greaterThanOrEqualTo = P.gte = function (y) { - var k = this.cmp(y); - return k == 1 || k === 0; - }; - - /* - * Return a new Decimal whose value is the hyperbolic cosine of the value in radians of this - * Decimal. - * - * Domain: [-Infinity, Infinity] - * Range: [1, Infinity] - * - * cosh(x) = 1 + x^2/2! + x^4/4! + x^6/6! + ... - * - * cosh(0) = 1 - * cosh(-0) = 1 - * cosh(Infinity) = Infinity - * cosh(-Infinity) = Infinity - * cosh(NaN) = NaN - * - * x time taken (ms) result - * 1000 9 9.8503555700852349694e+433 - * 10000 25 4.4034091128314607936e+4342 - * 100000 171 1.4033316802130615897e+43429 - * 1000000 3817 1.5166076984010437725e+434294 - * 10000000 abandoned after 2 minute wait - * - * TODO? Compare performance of cosh(x) = 0.5 * (exp(x) + exp(-x)) - * - */ - P.hyperbolicCosine = P.cosh = function () { - var k, n, pr, rm, len, - x = this, - Ctor = x.constructor, - one = new Ctor(1); - - if (!x.isFinite()) { - return new Ctor(x.s ? 1 / 0 : NaN); - } - if (x.isZero()) { - return one; - } - - pr = Ctor.precision; - rm = Ctor.rounding; - Ctor.precision = pr + Math.max(x.e, x.sd()) + 4; - Ctor.rounding = 1; - len = x.d.length; - - // Argument reduction: cos(4x) = 1 - 8cos^2(x) + 8cos^4(x) + 1 - // i.e. cos(x) = 1 - cos^2(x/4)(8 - 8cos^2(x/4)) - - // Estimate the optimum number of times to use the argument reduction. - // TODO? Estimation reused from cosine() and may not be optimal here. - if (len < 32) { - k = Math.ceil(len / 3); - n = (1 / tinyPow(4, k)).toString(); - } else { - k = 16; - n = '2.3283064365386962890625e-10'; - } - - x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true); - - // Reverse argument reduction - var cosh2_x, - i = k, - d8 = new Ctor(8); - for (; i--;) { - cosh2_x = x.times(x); - x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8)))); - } - - return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true); - }; - - /* - * Return a new Decimal whose value is the hyperbolic sine of the value in radians of this - * Decimal. - * - * Domain: [-Infinity, Infinity] - * Range: [-Infinity, Infinity] - * - * sinh(x) = x + x^3/3! + x^5/5! + x^7/7! + ... - * - * sinh(0) = 0 - * sinh(-0) = -0 - * sinh(Infinity) = Infinity - * sinh(-Infinity) = -Infinity - * sinh(NaN) = NaN - * - * x time taken (ms) - * 10 2 ms - * 100 5 ms - * 1000 14 ms - * 10000 82 ms - * 100000 886 ms 1.4033316802130615897e+43429 - * 200000 2613 ms - * 300000 5407 ms - * 400000 8824 ms - * 500000 13026 ms 8.7080643612718084129e+217146 - * 1000000 48543 ms - * - * TODO? Compare performance of sinh(x) = 0.5 * (exp(x) - exp(-x)) - * - */ - P.hyperbolicSine = P.sinh = function () { - var k, pr, rm, len, - x = this, - Ctor = x.constructor; - - if (!x.isFinite() || x.isZero()) { - return new Ctor(x); - } - - pr = Ctor.precision; - rm = Ctor.rounding; - Ctor.precision = pr + Math.max(x.e, x.sd()) + 4; - Ctor.rounding = 1; - len = x.d.length; - - if (len < 3) { - x = taylorSeries(Ctor, 2, x, x, true); - } else { - - // Alternative argument reduction: sinh(3x) = sinh(x)(3 + 4sinh^2(x)) - // i.e. sinh(x) = sinh(x/3)(3 + 4sinh^2(x/3)) - // 3 multiplications and 1 addition - - // Argument reduction: sinh(5x) = sinh(x)(5 + sinh^2(x)(20 + 16sinh^2(x))) - // i.e. sinh(x) = sinh(x/5)(5 + sinh^2(x/5)(20 + 16sinh^2(x/5))) - // 4 multiplications and 2 additions - - // Estimate the optimum number of times to use the argument reduction. - k = 1.4 * Math.sqrt(len); - k = k > 16 ? 16 : k | 0; - - x = x.times(1 / tinyPow(5, k)); - x = taylorSeries(Ctor, 2, x, x, true); - - // Reverse argument reduction - var sinh2_x, - d5 = new Ctor(5), - d16 = new Ctor(16), - d20 = new Ctor(20); - for (; k--;) { - sinh2_x = x.times(x); - x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20)))); - } - } - - Ctor.precision = pr; - Ctor.rounding = rm; - - return finalise(x, pr, rm, true); - }; - - /* - * Return a new Decimal whose value is the hyperbolic tangent of the value in radians of this - * Decimal. - * - * Domain: [-Infinity, Infinity] - * Range: [-1, 1] - * - * tanh(x) = sinh(x) / cosh(x) - * - * tanh(0) = 0 - * tanh(-0) = -0 - * tanh(Infinity) = 1 - * tanh(-Infinity) = -1 - * tanh(NaN) = NaN - * - */ - P.hyperbolicTangent = P.tanh = function () { - var pr, rm, - x = this, - Ctor = x.constructor; - - if (!x.isFinite()) { - return new Ctor(x.s); - } - if (x.isZero()) { - return new Ctor(x); - } - - pr = Ctor.precision; - rm = Ctor.rounding; - Ctor.precision = pr + 7; - Ctor.rounding = 1; - - return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm); - }; - - /* - * Return a new Decimal whose value is the arccosine (inverse cosine) in radians of the value of - * this Decimal. - * - * Domain: [-1, 1] - * Range: [0, pi] - * - * acos(x) = pi/2 - asin(x) - * - * acos(0) = pi/2 - * acos(-0) = pi/2 - * acos(1) = 0 - * acos(-1) = pi - * acos(1/2) = pi/3 - * acos(-1/2) = 2*pi/3 - * acos(|x| > 1) = NaN - * acos(NaN) = NaN - * - */ - P.inverseCosine = P.acos = function () { - var halfPi, - x = this, - Ctor = x.constructor, - k = x.abs().cmp(1), - pr = Ctor.precision, - rm = Ctor.rounding; - - if (k !== -1) { - return k === 0 - // |x| is 1 - ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) - // |x| > 1 or x is NaN - : new Ctor(NaN); - } - - if (x.isZero()) { - return getPi(Ctor, pr + 4, rm).times(0.5); - } - - // TODO? Special case acos(0.5) = pi/3 and acos(-0.5) = 2*pi/3 - - Ctor.precision = pr + 6; - Ctor.rounding = 1; - - x = x.asin(); - halfPi = getPi(Ctor, pr + 4, rm).times(0.5); - - Ctor.precision = pr; - Ctor.rounding = rm; - - return halfPi.minus(x); - }; - - /* - * Return a new Decimal whose value is the inverse of the hyperbolic cosine in radians of the - * value of this Decimal. - * - * Domain: [1, Infinity] - * Range: [0, Infinity] - * - * acosh(x) = ln(x + sqrt(x^2 - 1)) - * - * acosh(x < 1) = NaN - * acosh(NaN) = NaN - * acosh(Infinity) = Infinity - * acosh(-Infinity) = NaN - * acosh(0) = NaN - * acosh(-0) = NaN - * acosh(1) = 0 - * acosh(-1) = NaN - * - */ - P.inverseHyperbolicCosine = P.acosh = function () { - var pr, rm, - x = this, - Ctor = x.constructor; - - if (x.lte(1)) { - return new Ctor(x.eq(1) ? 0 : NaN); - } - if (!x.isFinite()) { - return new Ctor(x); - } - - pr = Ctor.precision; - rm = Ctor.rounding; - Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4; - Ctor.rounding = 1; - external = false; - - x = x.times(x).minus(1).sqrt().plus(x); - - external = true; - Ctor.precision = pr; - Ctor.rounding = rm; - - return x.ln(); - }; - - /* - * Return a new Decimal whose value is the inverse of the hyperbolic sine in radians of the value - * of this Decimal. - * - * Domain: [-Infinity, Infinity] - * Range: [-Infinity, Infinity] - * - * asinh(x) = ln(x + sqrt(x^2 + 1)) - * - * asinh(NaN) = NaN - * asinh(Infinity) = Infinity - * asinh(-Infinity) = -Infinity - * asinh(0) = 0 - * asinh(-0) = -0 - * - */ - P.inverseHyperbolicSine = P.asinh = function () { - var pr, rm, - x = this, - Ctor = x.constructor; - - if (!x.isFinite() || x.isZero()) { - return new Ctor(x); - } - - pr = Ctor.precision; - rm = Ctor.rounding; - Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6; - Ctor.rounding = 1; - external = false; - - x = x.times(x).plus(1).sqrt().plus(x); - - external = true; - Ctor.precision = pr; - Ctor.rounding = rm; - - return x.ln(); - }; - - /* - * Return a new Decimal whose value is the inverse of the hyperbolic tangent in radians of the - * value of this Decimal. - * - * Domain: [-1, 1] - * Range: [-Infinity, Infinity] - * - * atanh(x) = 0.5 * ln((1 + x) / (1 - x)) - * - * atanh(|x| > 1) = NaN - * atanh(NaN) = NaN - * atanh(Infinity) = NaN - * atanh(-Infinity) = NaN - * atanh(0) = 0 - * atanh(-0) = -0 - * atanh(1) = Infinity - * atanh(-1) = -Infinity - * - */ - P.inverseHyperbolicTangent = P.atanh = function () { - var pr, rm, wpr, xsd, - x = this, - Ctor = x.constructor; - - if (!x.isFinite()) { - return new Ctor(NaN); - } - if (x.e >= 0) { - return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN); - } - - pr = Ctor.precision; - rm = Ctor.rounding; - xsd = x.sd(); - - if (Math.max(xsd, pr) < 2 * -x.e - 1) { - return finalise(new Ctor(x), pr, rm, true); - } - - Ctor.precision = wpr = xsd - x.e; - - x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1); - - Ctor.precision = pr + 4; - Ctor.rounding = 1; - - x = x.ln(); - - Ctor.precision = pr; - Ctor.rounding = rm; - - return x.times(0.5); - }; - - /* - * Return a new Decimal whose value is the arcsine (inverse sine) in radians of the value of this - * Decimal. - * - * Domain: [-Infinity, Infinity] - * Range: [-pi/2, pi/2] - * - * asin(x) = 2*atan(x/(1 + sqrt(1 - x^2))) - * - * asin(0) = 0 - * asin(-0) = -0 - * asin(1/2) = pi/6 - * asin(-1/2) = -pi/6 - * asin(1) = pi/2 - * asin(-1) = -pi/2 - * asin(|x| > 1) = NaN - * asin(NaN) = NaN - * - * TODO? Compare performance of Taylor series. - * - */ - P.inverseSine = P.asin = function () { - var halfPi, k, - pr, rm, - x = this, - Ctor = x.constructor; - - if (x.isZero()) { - return new Ctor(x); - } - - k = x.abs().cmp(1); - pr = Ctor.precision; - rm = Ctor.rounding; - - if (k !== -1) { - - // |x| is 1 - if (k === 0) { - halfPi = getPi(Ctor, pr + 4, rm).times(0.5); - halfPi.s = x.s; - return halfPi; - } - - // |x| > 1 or x is NaN - return new Ctor(NaN); - } - - // TODO? Special case asin(1/2) = pi/6 and asin(-1/2) = -pi/6 - - Ctor.precision = pr + 6; - Ctor.rounding = 1; - - x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan(); - - Ctor.precision = pr; - Ctor.rounding = rm; - - return x.times(2); - }; - - /* - * Return a new Decimal whose value is the arctangent (inverse tangent) in radians of the value - * of this Decimal. - * - * Domain: [-Infinity, Infinity] - * Range: [-pi/2, pi/2] - * - * atan(x) = x - x^3/3 + x^5/5 - x^7/7 + ... - * - * atan(0) = 0 - * atan(-0) = -0 - * atan(1) = pi/4 - * atan(-1) = -pi/4 - * atan(Infinity) = pi/2 - * atan(-Infinity) = -pi/2 - * atan(NaN) = NaN - * - */ - P.inverseTangent = P.atan = function () { - var i, j, k, n, px, t, r, wpr, x2, - x = this, - Ctor = x.constructor, - pr = Ctor.precision, - rm = Ctor.rounding; - - if (!x.isFinite()) { - if (!x.s) { - return new Ctor(NaN); - } - if (pr + 4 <= PI_PRECISION) { - r = getPi(Ctor, pr + 4, rm).times(0.5); - r.s = x.s; - return r; - } - } else if (x.isZero()) { - return new Ctor(x); - } else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) { - r = getPi(Ctor, pr + 4, rm).times(0.25); - r.s = x.s; - return r; - } - - Ctor.precision = wpr = pr + 10; - Ctor.rounding = 1; - - // TODO? if (x >= 1 && pr <= PI_PRECISION) atan(x) = halfPi * x.s - atan(1 / x); - - // Argument reduction - // Ensure |x| < 0.42 - // atan(x) = 2 * atan(x / (1 + sqrt(1 + x^2))) - - k = Math.min(28, wpr / LOG_BASE + 2 | 0); - - for (i = k; i; --i) { - x = x.div(x.times(x).plus(1).sqrt().plus(1)); - } - - external = false; - - j = Math.ceil(wpr / LOG_BASE); - n = 1; - x2 = x.times(x); - r = new Ctor(x); - px = x; - - // atan(x) = x - x^3/3 + x^5/5 - x^7/7 + ... - for (; i !== -1;) { - px = px.times(x2); - t = r.minus(px.div(n += 2)); - - px = px.times(x2); - r = t.plus(px.div(n += 2)); - - if (r.d[j] !== void 0) { - for (i = j; r.d[i] === t.d[i] && i--;) { - ; - } - } - } - - if (k) { - r = r.times(2 << (k - 1)); - } - - external = true; - - return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true); - }; - - /* - * Return true if the value of this Decimal is a finite number, otherwise return false. - * - */ - P.isFinite = function () { - return !!this.d; - }; - - /* - * Return true if the value of this Decimal is an integer, otherwise return false. - * - */ - P.isInteger = P.isInt = function () { - return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2; - }; - - /* - * Return true if the value of this Decimal is NaN, otherwise return false. - * - */ - P.isNaN = function () { - return !this.s; - }; - - /* - * Return true if the value of this Decimal is negative, otherwise return false. - * - */ - P.isNegative = P.isNeg = function () { - return this.s < 0; - }; - - /* - * Return true if the value of this Decimal is positive, otherwise return false. - * - */ - P.isPositive = P.isPos = function () { - return this.s > 0; - }; - - /* - * Return true if the value of this Decimal is 0 or -0, otherwise return false. - * - */ - P.isZero = function () { - return !!this.d && this.d[0] === 0; - }; - - /* - * Return true if the value of this Decimal is less than `y`, otherwise return false. - * - */ - P.lessThan = P.lt = function (y) { - return this.cmp(y) < 0; - }; - - /* - * Return true if the value of this Decimal is less than or equal to `y`, otherwise return false. - * - */ - P.lessThanOrEqualTo = P.lte = function (y) { - return this.cmp(y) < 1; - }; - - /* - * Return the logarithm of the value of this Decimal to the specified base, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - * If no base is specified, return log[10](arg). - * - * log[base](arg) = ln(arg) / ln(base) - * - * The result will always be correctly rounded if the base of the log is 10, and 'almost always' - * otherwise: - * - * Depending on the rounding mode, the result may be incorrectly rounded if the first fifteen - * rounding digits are [49]99999999999999 or [50]00000000000000. In that case, the maximum error - * between the result and the correctly rounded result will be one ulp (unit in the last place). - * - * log[-b](a) = NaN - * log[0](a) = NaN - * log[1](a) = NaN - * log[NaN](a) = NaN - * log[Infinity](a) = NaN - * log[b](0) = -Infinity - * log[b](-0) = -Infinity - * log[b](-a) = NaN - * log[b](1) = 0 - * log[b](Infinity) = Infinity - * log[b](NaN) = NaN - * - * [base] {number|string|Decimal} The base of the logarithm. - * - */ - P.logarithm = P.log = function (base) { - var isBase10, d, denominator, k, inf, num, sd, r, - arg = this, - Ctor = arg.constructor, - pr = Ctor.precision, - rm = Ctor.rounding, - guard = 5; - - // Default base is 10. - if (base == null) { - base = new Ctor(10); - isBase10 = true; - } else { - base = new Ctor(base); - d = base.d; - - // Return NaN if base is negative, or non-finite, or is 0 or 1. - if (base.s < 0 || !d || !d[0] || base.eq(1)) { - return new Ctor(NaN); - } - - isBase10 = base.eq(10); - } - - d = arg.d; - - // Is arg negative, non-finite, 0 or 1? - if (arg.s < 0 || !d || !d[0] || arg.eq(1)) { - return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0); - } - - // The result will have a non-terminating decimal expansion if base is 10 and arg is not an - // integer power of 10. - if (isBase10) { - if (d.length > 1) { - inf = true; - } else { - for (k = d[0]; k % 10 === 0;) { - k /= 10; - } - inf = k !== 1; - } - } - - external = false; - sd = pr + guard; - num = naturalLogarithm(arg, sd); - denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd); - - // The result will have 5 rounding digits. - r = divide(num, denominator, sd, 1); - - // If at a rounding boundary, i.e. the result's rounding digits are [49]9999 or [50]0000, - // calculate 10 further digits. - // - // If the result is known to have an infinite decimal expansion, repeat this until it is clear - // that the result is above or below the boundary. Otherwise, if after calculating the 10 - // further digits, the last 14 are nines, round up and assume the result is exact. - // Also assume the result is exact if the last 14 are zero. - // - // Example of a result that will be incorrectly rounded: - // log[1048576](4503599627370502) = 2.60000000000000009610279511444746... - // The above result correctly rounded using ROUND_CEIL to 1 decimal place should be 2.7, but it - // will be given as 2.6 as there are 15 zeros immediately after the requested decimal place, so - // the exact result would be assumed to be 2.6, which rounded using ROUND_CEIL to 1 decimal - // place is still 2.6. - if (checkRoundingDigits(r.d, k = pr, rm)) { - - do { - sd += 10; - num = naturalLogarithm(arg, sd); - denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd); - r = divide(num, denominator, sd, 1); - - if (!inf) { - - // Check for 14 nines from the 2nd rounding digit, as the first may be 4. - if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) { - r = finalise(r, pr + 1, 0); - } - - break; - } - } while (checkRoundingDigits(r.d, k += 10, rm)); - } - - external = true; - - return finalise(r, pr, rm); - }; - - /* - * Return a new Decimal whose value is the maximum of the arguments and the value of this Decimal. - * - * arguments {number|string|Decimal} - * - P.max = function () { - Array.prototype.push.call(arguments, this); - return maxOrMin(this.constructor, arguments, 'lt'); - }; - */ - - /* - * Return a new Decimal whose value is the minimum of the arguments and the value of this Decimal. - * - * arguments {number|string|Decimal} - * - P.min = function () { - Array.prototype.push.call(arguments, this); - return maxOrMin(this.constructor, arguments, 'gt'); - }; - */ - - /* - * n - 0 = n - * n - N = N - * n - I = -I - * 0 - n = -n - * 0 - 0 = 0 - * 0 - N = N - * 0 - I = -I - * N - n = N - * N - 0 = N - * N - N = N - * N - I = N - * I - n = I - * I - 0 = I - * I - N = N - * I - I = N - * - * Return a new Decimal whose value is the value of this Decimal minus `y`, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - */ - P.minus = P.sub = function (y) { - var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd, - x = this, - Ctor = x.constructor; - - y = new Ctor(y); - - // If either is not finite... - if (!x.d || !y.d) { - - // Return NaN if either is NaN. - if (!x.s || !y.s) { - y = new Ctor(NaN); - } - - // Return y negated if x is finite and y is ±Infinity. - else if (x.d) { - y.s = -y.s; - } - - // Return x if y is finite and x is ±Infinity. - // Return x if both are ±Infinity with different signs. - // Return NaN if both are ±Infinity with the same sign. - else { - y = new Ctor(y.d || x.s !== y.s ? x : NaN); - } - - return y; - } - - // If signs differ... - if (x.s != y.s) { - y.s = -y.s; - return x.plus(y); - } - - xd = x.d; - yd = y.d; - pr = Ctor.precision; - rm = Ctor.rounding; - - // If either is zero... - if (!xd[0] || !yd[0]) { - - // Return y negated if x is zero and y is non-zero. - if (yd[0]) { - y.s = -y.s; - } - - // Return x if y is zero and x is non-zero. - else if (xd[0]) { - y = new Ctor(x); - } - - // Return zero if both are zero. - // From IEEE 754 (2008) 6.3: 0 - 0 = -0 - -0 = -0 when rounding to -Infinity. - else { - return new Ctor(rm === 3 ? -0 : 0); - } - - return external ? finalise(y, pr, rm) : y; - } - - // x and y are finite, non-zero numbers with the same sign. - - // Calculate base 1e7 exponents. - e = mathfloor(y.e / LOG_BASE); - xe = mathfloor(x.e / LOG_BASE); - - xd = xd.slice(); - k = xe - e; - - // If base 1e7 exponents differ... - if (k) { - xLTy = k < 0; - - if (xLTy) { - d = xd; - k = -k; - len = yd.length; - } else { - d = yd; - e = xe; - len = xd.length; - } - - // Numbers with massively different exponents would result in a very high number of - // zeros needing to be prepended, but this can be avoided while still ensuring correct - // rounding by limiting the number of zeros to `Math.ceil(pr / LOG_BASE) + 2`. - i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2; - - if (k > i) { - k = i; - d.length = 1; - } - - // Prepend zeros to equalise exponents. - d.reverse(); - for (i = k; i--;) { - d.push(0); - } - d.reverse(); - - // Base 1e7 exponents equal. - } else { - - // Check digits to determine which is the bigger number. - - i = xd.length; - len = yd.length; - xLTy = i < len; - if (xLTy) { - len = i; - } - - for (i = 0; i < len; i++) { - if (xd[i] != yd[i]) { - xLTy = xd[i] < yd[i]; - break; - } - } - - k = 0; - } - - if (xLTy) { - d = xd; - xd = yd; - yd = d; - y.s = -y.s; - } - - len = xd.length; - - // Append zeros to `xd` if shorter. - // Don't add zeros to `yd` if shorter as subtraction only needs to start at `yd` length. - for (i = yd.length - len; i > 0; --i) { - xd[len++] = 0; - } - - // Subtract yd from xd. - for (i = yd.length; i > k;) { - - if (xd[--i] < yd[i]) { - for (j = i; j && xd[--j] === 0;) { - xd[j] = BASE - 1; - } - --xd[j]; - xd[i] += BASE; - } - - xd[i] -= yd[i]; - } - - // Remove trailing zeros. - for (; xd[--len] === 0;) { - xd.pop(); - } - - // Remove leading zeros and adjust exponent accordingly. - for (; xd[0] === 0; xd.shift()) { - --e; - } - - // Zero? - if (!xd[0]) { - return new Ctor(rm === 3 ? -0 : 0); - } - - y.d = xd; - y.e = getBase10Exponent(xd, e); - - return external ? finalise(y, pr, rm) : y; - }; - - /* - * n % 0 = N - * n % N = N - * n % I = n - * 0 % n = 0 - * -0 % n = -0 - * 0 % 0 = N - * 0 % N = N - * 0 % I = 0 - * N % n = N - * N % 0 = N - * N % N = N - * N % I = N - * I % n = N - * I % 0 = N - * I % N = N - * I % I = N - * - * Return a new Decimal whose value is the value of this Decimal modulo `y`, rounded to - * `precision` significant digits using rounding mode `rounding`. - * - * The result depends on the modulo mode. - * - */ - P.modulo = P.mod = function (y) { - var q, - x = this, - Ctor = x.constructor; - - y = new Ctor(y); - - // Return NaN if x is ±Infinity or NaN, or y is NaN or ±0. - if (!x.d || !y.s || y.d && !y.d[0]) { - return new Ctor(NaN); - } - - // Return x if y is ±Infinity or x is ±0. - if (!y.d || x.d && !x.d[0]) { - return finalise(new Ctor(x), Ctor.precision, Ctor.rounding); - } - - // Prevent rounding of intermediate calculations. - external = false; - - if (Ctor.modulo == 9) { - - // Euclidian division: q = sign(y) * floor(x / abs(y)) - // result = x - q * y where 0 <= result < abs(y) - q = divide(x, y.abs(), 0, 3, 1); - q.s *= y.s; - } else { - q = divide(x, y, 0, Ctor.modulo, 1); - } - - q = q.times(y); - - external = true; - - return x.minus(q); - }; - - /* - * Return a new Decimal whose value is the natural exponential of the value of this Decimal, - * i.e. the base e raised to the power the value of this Decimal, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - */ - P.naturalExponential = P.exp = function () { - return naturalExponential(this); - }; - - /* - * Return a new Decimal whose value is the natural logarithm of the value of this Decimal, - * rounded to `precision` significant digits using rounding mode `rounding`. - * - */ - P.naturalLogarithm = P.ln = function () { - return naturalLogarithm(this); - }; - - /* - * Return a new Decimal whose value is the value of this Decimal negated, i.e. as if multiplied by - * -1. - * - */ - P.negated = P.neg = function () { - var x = new this.constructor(this); - x.s = -x.s; - return finalise(x); - }; - - /* - * n + 0 = n - * n + N = N - * n + I = I - * 0 + n = n - * 0 + 0 = 0 - * 0 + N = N - * 0 + I = I - * N + n = N - * N + 0 = N - * N + N = N - * N + I = N - * I + n = I - * I + 0 = I - * I + N = N - * I + I = I - * - * Return a new Decimal whose value is the value of this Decimal plus `y`, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - */ - P.plus = P.add = function (y) { - var carry, d, e, i, k, len, pr, rm, xd, yd, - x = this, - Ctor = x.constructor; - - y = new Ctor(y); - - // If either is not finite... - if (!x.d || !y.d) { - - // Return NaN if either is NaN. - if (!x.s || !y.s) { - y = new Ctor(NaN); - } - - // Return x if y is finite and x is ±Infinity. - // Return x if both are ±Infinity with the same sign. - // Return NaN if both are ±Infinity with different signs. - // Return y if x is finite and y is ±Infinity. - else if (!x.d) { - y = new Ctor(y.d || x.s === y.s ? x : NaN); - } - - return y; - } - - // If signs differ... - if (x.s != y.s) { - y.s = -y.s; - return x.minus(y); - } - - xd = x.d; - yd = y.d; - pr = Ctor.precision; - rm = Ctor.rounding; - - // If either is zero... - if (!xd[0] || !yd[0]) { - - // Return x if y is zero. - // Return y if y is non-zero. - if (!yd[0]) { - y = new Ctor(x); - } - - return external ? finalise(y, pr, rm) : y; - } - - // x and y are finite, non-zero numbers with the same sign. - - // Calculate base 1e7 exponents. - k = mathfloor(x.e / LOG_BASE); - e = mathfloor(y.e / LOG_BASE); - - xd = xd.slice(); - i = k - e; - - // If base 1e7 exponents differ... - if (i) { - - if (i < 0) { - d = xd; - i = -i; - len = yd.length; - } else { - d = yd; - e = k; - len = xd.length; - } - - // Limit number of zeros prepended to max(ceil(pr / LOG_BASE), len) + 1. - k = Math.ceil(pr / LOG_BASE); - len = k > len ? k + 1 : len + 1; - - if (i > len) { - i = len; - d.length = 1; - } - - // Prepend zeros to equalise exponents. Note: Faster to use reverse then do unshifts. - d.reverse(); - for (; i--;) { - d.push(0); - } - d.reverse(); - } - - len = xd.length; - i = yd.length; - - // If yd is longer than xd, swap xd and yd so xd points to the longer array. - if (len - i < 0) { - i = len; - d = yd; - yd = xd; - xd = d; - } - - // Only start adding at yd.length - 1 as the further digits of xd can be left as they are. - for (carry = 0; i;) { - carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0; - xd[i] %= BASE; - } - - if (carry) { - xd.unshift(carry); - ++e; - } - - // Remove trailing zeros. - // No need to check for zero, as +x + +y != 0 && -x + -y != 0 - for (len = xd.length; xd[--len] == 0;) { - xd.pop(); - } - - y.d = xd; - y.e = getBase10Exponent(xd, e); - - return external ? finalise(y, pr, rm) : y; - }; - - /* - * Return the number of significant digits of the value of this Decimal. - * - * [z] {boolean|number} Whether to count integer-part trailing zeros: true, false, 1 or 0. - * - */ - P.precision = P.sd = function (z) { - var k, - x = this; - - if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) { - throw Error(invalidArgument + z); - } - - if (x.d) { - k = getPrecision(x.d); - if (z && x.e + 1 > k) { - k = x.e + 1; - } - } else { - k = NaN; - } - - return k; - }; - - /* - * Return a new Decimal whose value is the value of this Decimal rounded to a whole number using - * rounding mode `rounding`. - * - */ - P.round = function () { - var x = this, - Ctor = x.constructor; - - return finalise(new Ctor(x), x.e + 1, Ctor.rounding); - }; - - /* - * Return a new Decimal whose value is the sine of the value in radians of this Decimal. - * - * Domain: [-Infinity, Infinity] - * Range: [-1, 1] - * - * sin(x) = x - x^3/3! + x^5/5! - ... - * - * sin(0) = 0 - * sin(-0) = -0 - * sin(Infinity) = NaN - * sin(-Infinity) = NaN - * sin(NaN) = NaN - * - */ - P.sine = P.sin = function () { - var pr, rm, - x = this, - Ctor = x.constructor; - - if (!x.isFinite()) { - return new Ctor(NaN); - } - if (x.isZero()) { - return new Ctor(x); - } - - pr = Ctor.precision; - rm = Ctor.rounding; - Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE; - Ctor.rounding = 1; - - x = sine(Ctor, toLessThanHalfPi(Ctor, x)); - - Ctor.precision = pr; - Ctor.rounding = rm; - - return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true); - }; - - /* - * Return a new Decimal whose value is the square root of this Decimal, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - * sqrt(-n) = N - * sqrt(N) = N - * sqrt(-I) = N - * sqrt(I) = I - * sqrt(0) = 0 - * sqrt(-0) = -0 - * - */ - P.squareRoot = P.sqrt = function () { - var m, n, sd, r, rep, t, - x = this, - d = x.d, - e = x.e, - s = x.s, - Ctor = x.constructor; - - // Negative/NaN/Infinity/zero? - if (s !== 1 || !d || !d[0]) { - return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0); - } - - external = false; - - // Initial estimate. - s = Math.sqrt(+x); - - // Math.sqrt underflow/overflow? - // Pass x to Math.sqrt as integer, then adjust the exponent of the result. - if (s == 0 || s == 1 / 0) { - n = digitsToString(d); - - if ((n.length + e) % 2 == 0) { - n += '0'; - } - s = Math.sqrt(n); - e = mathfloor((e + 1) / 2) - (e < 0 || e % 2); - - if (s == 1 / 0) { - n = '5e' + e; - } else { - n = s.toExponential(); - n = n.slice(0, n.indexOf('e') + 1) + e; - } - - r = new Ctor(n); - } else { - r = new Ctor(s.toString()); - } - - sd = (e = Ctor.precision) + 3; - - // Newton-Raphson iteration. - for (;;) { - t = r; - r = t.plus(divide(x, t, sd + 2, 1)).times(0.5); - - // TODO? Replace with for-loop and checkRoundingDigits. - if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) { - n = n.slice(sd - 3, sd + 1); - - // The 4th rounding digit may be in error by -1 so if the 4 rounding digits are 9999 or - // 4999, i.e. approaching a rounding boundary, continue the iteration. - if (n == '9999' || !rep && n == '4999') { - - // On the first iteration only, check to see if rounding up gives the exact result as the - // nines may infinitely repeat. - if (!rep) { - finalise(t, e + 1, 0); - - if (t.times(t).eq(x)) { - r = t; - break; - } - } - - sd += 4; - rep = 1; - } else { - - // If the rounding digits are null, 0{0,4} or 50{0,3}, check for an exact result. - // If not, then there are further digits and m will be truthy. - if (!+n || !+n.slice(1) && n.charAt(0) == '5') { - - // Truncate to the first rounding digit. - finalise(r, e + 1, 1); - m = !r.times(r).eq(x); - } - - break; - } - } - } - - external = true; - - return finalise(r, e, Ctor.rounding, m); - }; - - /* - * Return a new Decimal whose value is the tangent of the value in radians of this Decimal. - * - * Domain: [-Infinity, Infinity] - * Range: [-Infinity, Infinity] - * - * tan(0) = 0 - * tan(-0) = -0 - * tan(Infinity) = NaN - * tan(-Infinity) = NaN - * tan(NaN) = NaN - * - */ - P.tangent = P.tan = function () { - var pr, rm, - x = this, - Ctor = x.constructor; - - if (!x.isFinite()) { - return new Ctor(NaN); - } - if (x.isZero()) { - return new Ctor(x); - } - - pr = Ctor.precision; - rm = Ctor.rounding; - Ctor.precision = pr + 10; - Ctor.rounding = 1; - - x = x.sin(); - x.s = 1; - x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0); - - Ctor.precision = pr; - Ctor.rounding = rm; - - return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true); - }; - - /* - * n * 0 = 0 - * n * N = N - * n * I = I - * 0 * n = 0 - * 0 * 0 = 0 - * 0 * N = N - * 0 * I = N - * N * n = N - * N * 0 = N - * N * N = N - * N * I = N - * I * n = I - * I * 0 = N - * I * N = N - * I * I = I - * - * Return a new Decimal whose value is this Decimal times `y`, rounded to `precision` significant - * digits using rounding mode `rounding`. - * - */ - P.times = P.mul = function (y) { - var carry, e, i, k, r, rL, t, xdL, ydL, - x = this, - Ctor = x.constructor, - xd = x.d, - yd = (y = new Ctor(y)).d; - - y.s *= x.s; - - // If either is NaN, ±Infinity or ±0... - if (!xd || !xd[0] || !yd || !yd[0]) { - - return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd - - // Return NaN if either is NaN. - // Return NaN if x is ±0 and y is ±Infinity, or y is ±0 and x is ±Infinity. - ? NaN - - // Return ±Infinity if either is ±Infinity. - // Return ±0 if either is ±0. - : !xd || !yd ? y.s / 0 : y.s * 0); - } - - e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE); - xdL = xd.length; - ydL = yd.length; - - // Ensure xd points to the longer array. - if (xdL < ydL) { - r = xd; - xd = yd; - yd = r; - rL = xdL; - xdL = ydL; - ydL = rL; - } - - // Initialise the result array with zeros. - r = []; - rL = xdL + ydL; - for (i = rL; i--;) { - r.push(0); - } - - // Multiply! - for (i = ydL; --i >= 0;) { - carry = 0; - for (k = xdL + i; k > i;) { - t = r[k] + yd[i] * xd[k - i - 1] + carry; - r[k--] = t % BASE | 0; - carry = t / BASE | 0; - } - - r[k] = (r[k] + carry) % BASE | 0; - } - - // Remove trailing zeros. - for (; !r[--rL];) { - r.pop(); - } - - if (carry) { - ++e; - } else { - r.shift(); - } - - y.d = r; - y.e = getBase10Exponent(r, e); - - return external ? finalise(y, Ctor.precision, Ctor.rounding) : y; - }; - - /* - * Return a string representing the value of this Decimal in base 2, round to `sd` significant - * digits using rounding mode `rm`. - * - * If the optional `sd` argument is present then return binary exponential notation. - * - * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive. - * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. - * - */ - P.toBinary = function (sd, rm) { - return toStringBinary(this, 2, sd, rm); - }; - - /* - * Return a new Decimal whose value is the value of this Decimal rounded to a maximum of `dp` - * decimal places using rounding mode `rm` or `rounding` if `rm` is omitted. - * - * If `dp` is omitted, return a new Decimal whose value is the value of this Decimal. - * - * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive. - * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. - * - */ - P.toDecimalPlaces = P.toDP = function (dp, rm) { - var x = this, - Ctor = x.constructor; - - x = new Ctor(x); - if (dp === void 0) { - return x; - } - - checkInt32(dp, 0, MAX_DIGITS); - - if (rm === void 0) { - rm = Ctor.rounding; - } else { - checkInt32(rm, 0, 8); - } - - return finalise(x, dp + x.e + 1, rm); - }; - - /* - * Return a string representing the value of this Decimal in exponential notation rounded to - * `dp` fixed decimal places using rounding mode `rounding`. - * - * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive. - * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. - * - */ - P.toExponential = function (dp, rm) { - var str, - x = this, - Ctor = x.constructor; - - if (dp === void 0) { - str = finiteToString(x, true); - } else { - checkInt32(dp, 0, MAX_DIGITS); - - if (rm === void 0) { - rm = Ctor.rounding; - } else { - checkInt32(rm, 0, 8); - } - - x = finalise(new Ctor(x), dp + 1, rm); - str = finiteToString(x, true, dp + 1); - } - - return x.isNeg() && !x.isZero() ? '-' + str : str; - }; - - /* - * Return a string representing the value of this Decimal in normal (fixed-point) notation to - * `dp` fixed decimal places and rounded using rounding mode `rm` or `rounding` if `rm` is - * omitted. - * - * As with JavaScript numbers, (-0).toFixed(0) is '0', but e.g. (-0.00001).toFixed(0) is '-0'. - * - * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive. - * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. - * - * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'. - * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'. - * (-0).toFixed(3) is '0.000'. - * (-0.5).toFixed(0) is '-0'. - * - */ - P.toFixed = function (dp, rm) { - var str, y, - x = this, - Ctor = x.constructor; - - if (dp === void 0) { - str = finiteToString(x); - } else { - checkInt32(dp, 0, MAX_DIGITS); - - if (rm === void 0) { - rm = Ctor.rounding; - } else { - checkInt32(rm, 0, 8); - } - - y = finalise(new Ctor(x), dp + x.e + 1, rm); - str = finiteToString(y, false, dp + y.e + 1); - } - - // To determine whether to add the minus sign look at the value before it was rounded, - // i.e. look at `x` rather than `y`. - return x.isNeg() && !x.isZero() ? '-' + str : str; - }; - - /* - * Return an array representing the value of this Decimal as a simple fraction with an integer - * numerator and an integer denominator. - * - * The denominator will be a positive non-zero value less than or equal to the specified maximum - * denominator. If a maximum denominator is not specified, the denominator will be the lowest - * value necessary to represent the number exactly. - * - * [maxD] {number|string|Decimal} Maximum denominator. Integer >= 1 and < Infinity. - * - */ - P.toFraction = function (maxD) { - var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r, - x = this, - xd = x.d, - Ctor = x.constructor; - - if (!xd) { - return new Ctor(x); - } - - n1 = d0 = new Ctor(1); - d1 = n0 = new Ctor(0); - - d = new Ctor(d1); - e = d.e = getPrecision(xd) - x.e - 1; - k = e % LOG_BASE; - d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k); - - if (maxD == null) { - - // d is 10**e, the minimum max-denominator needed. - maxD = e > 0 ? d : n1; - } else { - n = new Ctor(maxD); - if (!n.isInt() || n.lt(n1)) { - throw Error(invalidArgument + n); - } - maxD = n.gt(d) ? (e > 0 ? d : n1) : n; - } - - external = false; - n = new Ctor(digitsToString(xd)); - pr = Ctor.precision; - Ctor.precision = e = xd.length * LOG_BASE * 2; - - for (;;) { - q = divide(n, d, 0, 1, 1); - d2 = d0.plus(q.times(d1)); - if (d2.cmp(maxD) == 1) { - break; - } - d0 = d1; - d1 = d2; - d2 = n1; - n1 = n0.plus(q.times(d2)); - n0 = d2; - d2 = d; - d = n.minus(q.times(d2)); - n = d2; - } - - d2 = divide(maxD.minus(d0), d1, 0, 1, 1); - n0 = n0.plus(d2.times(n1)); - d0 = d0.plus(d2.times(d1)); - n0.s = n1.s = x.s; - - // Determine which fraction is closer to x, n0/d0 or n1/d1? - r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1 - ? [n1, d1] : [n0, d0]; - - Ctor.precision = pr; - external = true; - - return r; - }; - - /* - * Return a string representing the value of this Decimal in base 16, round to `sd` significant - * digits using rounding mode `rm`. - * - * If the optional `sd` argument is present then return binary exponential notation. - * - * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive. - * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. - * - */ - P.toHexadecimal = P.toHex = function (sd, rm) { - return toStringBinary(this, 16, sd, rm); - }; - - /* - * Returns a new Decimal whose value is the nearest multiple of `y` in the direction of rounding - * mode `rm`, or `Decimal.rounding` if `rm` is omitted, to the value of this Decimal. - * - * The return value will always have the same sign as this Decimal, unless either this Decimal - * or `y` is NaN, in which case the return value will be also be NaN. - * - * The return value is not affected by the value of `precision`. - * - * y {number|string|Decimal} The magnitude to round to a multiple of. - * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. - * - * 'toNearest() rounding mode not an integer: {rm}' - * 'toNearest() rounding mode out of range: {rm}' - * - */ - P.toNearest = function (y, rm) { - var x = this, - Ctor = x.constructor; - - x = new Ctor(x); - - if (y == null) { - - // If x is not finite, return x. - if (!x.d) { - return x; - } - - y = new Ctor(1); - rm = Ctor.rounding; - } else { - y = new Ctor(y); - if (rm === void 0) { - rm = Ctor.rounding; - } else { - checkInt32(rm, 0, 8); - } - - // If x is not finite, return x if y is not NaN, else NaN. - if (!x.d) { - return y.s ? x : y; - } - - // If y is not finite, return Infinity with the sign of x if y is Infinity, else NaN. - if (!y.d) { - if (y.s) { - y.s = x.s; - } - return y; - } - } - - // If y is not zero, calculate the nearest multiple of y to x. - if (y.d[0]) { - external = false; - x = divide(x, y, 0, rm, 1).times(y); - external = true; - finalise(x); - - // If y is zero, return zero with the sign of x. - } else { - y.s = x.s; - x = y; - } - - return x; - }; - - /* - * Return the value of this Decimal converted to a number primitive. - * Zero keeps its sign. - * - */ - P.toNumber = function () { - return +this; - }; - - /* - * Return a string representing the value of this Decimal in base 8, round to `sd` significant - * digits using rounding mode `rm`. - * - * If the optional `sd` argument is present then return binary exponential notation. - * - * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive. - * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. - * - */ - P.toOctal = function (sd, rm) { - return toStringBinary(this, 8, sd, rm); - }; - - /* - * Return a new Decimal whose value is the value of this Decimal raised to the power `y`, rounded - * to `precision` significant digits using rounding mode `rounding`. - * - * ECMAScript compliant. - * - * pow(x, NaN) = NaN - * pow(x, ±0) = 1 - - * pow(NaN, non-zero) = NaN - * pow(abs(x) > 1, +Infinity) = +Infinity - * pow(abs(x) > 1, -Infinity) = +0 - * pow(abs(x) == 1, ±Infinity) = NaN - * pow(abs(x) < 1, +Infinity) = +0 - * pow(abs(x) < 1, -Infinity) = +Infinity - * pow(+Infinity, y > 0) = +Infinity - * pow(+Infinity, y < 0) = +0 - * pow(-Infinity, odd integer > 0) = -Infinity - * pow(-Infinity, even integer > 0) = +Infinity - * pow(-Infinity, odd integer < 0) = -0 - * pow(-Infinity, even integer < 0) = +0 - * pow(+0, y > 0) = +0 - * pow(+0, y < 0) = +Infinity - * pow(-0, odd integer > 0) = -0 - * pow(-0, even integer > 0) = +0 - * pow(-0, odd integer < 0) = -Infinity - * pow(-0, even integer < 0) = +Infinity - * pow(finite x < 0, finite non-integer) = NaN - * - * For non-integer or very large exponents pow(x, y) is calculated using - * - * x^y = exp(y*ln(x)) - * - * Assuming the first 15 rounding digits are each equally likely to be any digit 0-9, the - * probability of an incorrectly rounded result - * P([49]9{14} | [50]0{14}) = 2 * 0.2 * 10^-14 = 4e-15 = 1/2.5e+14 - * i.e. 1 in 250,000,000,000,000 - * - * If a result is incorrectly rounded the maximum error will be 1 ulp (unit in last place). - * - * y {number|string|Decimal} The power to which to raise this Decimal. - * - */ - P.toPower = P.pow = function (y) { - var e, k, pr, r, rm, s, - x = this, - Ctor = x.constructor, - yn = +(y = new Ctor(y)); - - // Either ±Infinity, NaN or ±0? - if (!x.d || !y.d || !x.d[0] || !y.d[0]) { - return new Ctor(mathpow(+x, yn)); - } - - x = new Ctor(x); - - if (x.eq(1)) { - return x; - } - - pr = Ctor.precision; - rm = Ctor.rounding; - - if (y.eq(1)) { - return finalise(x, pr, rm); - } - - // y exponent - e = mathfloor(y.e / LOG_BASE); - - // If y is a small integer use the 'exponentiation by squaring' algorithm. - if (e >= y.d.length - 1 && (k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) { - r = intPow(Ctor, x, k, pr); - return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm); - } - - s = x.s; - - // if x is negative - if (s < 0) { - - // if y is not an integer - if (e < y.d.length - 1) { - return new Ctor(NaN); - } - - // Result is positive if x is negative and the last digit of integer y is even. - if ((y.d[e] & 1) == 0) { - s = 1; - } - - // if x.eq(-1) - if (x.e == 0 && x.d[0] == 1 && x.d.length == 1) { - x.s = s; - return x; - } - } - - // Estimate result exponent. - // x^y = 10^e, where e = y * log10(x) - // log10(x) = log10(x_significand) + x_exponent - // log10(x_significand) = ln(x_significand) / ln(10) - k = mathpow(+x, yn); - e = k == 0 || !isFinite(k) - ? mathfloor(yn * (Math.log('0.' + digitsToString(x.d)) / Math.LN10 + x.e + 1)) - : new Ctor(k + '').e; - - // Exponent estimate may be incorrect e.g. x: 0.999999999999999999, y: 2.29, e: 0, r.e: -1. - - // Overflow/underflow? - if (e > Ctor.maxE + 1 || e < Ctor.minE - 1) { - return new Ctor(e > 0 ? s / 0 : 0); - } - - external = false; - Ctor.rounding = x.s = 1; - - // Estimate the extra guard digits needed to ensure five correct rounding digits from - // naturalLogarithm(x). Example of failure without these extra digits (precision: 10): - // new Decimal(2.32456).pow('2087987436534566.46411') - // should be 1.162377823e+764914905173815, but is 1.162355823e+764914905173815 - k = Math.min(12, (e + '').length); - - // r = x^y = exp(y*ln(x)) - r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr); - - // r may be Infinity, e.g. (0.9999999999999999).pow(-1e+40) - if (r.d) { - - // Truncate to the required precision plus five rounding digits. - r = finalise(r, pr + 5, 1); - - // If the rounding digits are [49]9999 or [50]0000 increase the precision by 10 and recalculate - // the result. - if (checkRoundingDigits(r.d, pr, rm)) { - e = pr + 10; - - // Truncate to the increased precision plus five rounding digits. - r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1); - - // Check for 14 nines from the 2nd rounding digit (the first rounding digit may be 4 or 9). - if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 1e14) { - r = finalise(r, pr + 1, 0); - } - } - } - - r.s = s; - external = true; - Ctor.rounding = rm; - - return finalise(r, pr, rm); - }; - - /* - * Return a string representing the value of this Decimal rounded to `sd` significant digits - * using rounding mode `rounding`. - * - * Return exponential notation if `sd` is less than the number of digits necessary to represent - * the integer part of the value in normal notation. - * - * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive. - * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. - * - */ - P.toPrecision = function (sd, rm) { - var str, - x = this, - Ctor = x.constructor; - - if (sd === void 0) { - str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos); - } else { - checkInt32(sd, 1, MAX_DIGITS); - - if (rm === void 0) { - rm = Ctor.rounding; - } else { - checkInt32(rm, 0, 8); - } - - x = finalise(new Ctor(x), sd, rm); - str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd); - } - - return x.isNeg() && !x.isZero() ? '-' + str : str; - }; - - /* - * Return a new Decimal whose value is the value of this Decimal rounded to a maximum of `sd` - * significant digits using rounding mode `rm`, or to `precision` and `rounding` respectively if - * omitted. - * - * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive. - * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. - * - * 'toSD() digits out of range: {sd}' - * 'toSD() digits not an integer: {sd}' - * 'toSD() rounding mode not an integer: {rm}' - * 'toSD() rounding mode out of range: {rm}' - * - */ - P.toSignificantDigits = P.toSD = function (sd, rm) { - var x = this, - Ctor = x.constructor; - - if (sd === void 0) { - sd = Ctor.precision; - rm = Ctor.rounding; - } else { - checkInt32(sd, 1, MAX_DIGITS); - - if (rm === void 0) { - rm = Ctor.rounding; - } else { - checkInt32(rm, 0, 8); - } - } - - return finalise(new Ctor(x), sd, rm); - }; - - /* - * Return a string representing the value of this Decimal. - * - * Return exponential notation if this Decimal has a positive exponent equal to or greater than - * `toExpPos`, or a negative exponent equal to or less than `toExpNeg`. - * - */ - P.toString = function () { - var x = this, - Ctor = x.constructor, - str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos); - - return x.isNeg() && !x.isZero() ? '-' + str : str; - }; - - /* - * Return a new Decimal whose value is the value of this Decimal truncated to a whole number. - * - */ - P.truncated = P.trunc = function () { - return finalise(new this.constructor(this), this.e + 1, 1); - }; - - /* - * Return a string representing the value of this Decimal. - * Unlike `toString`, negative zero will include the minus sign. - * - */ - P.valueOf = P.toJSON = function () { - var x = this, - Ctor = x.constructor, - str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos); - - return x.isNeg() ? '-' + str : str; - }; - - /* - // Add aliases to match BigDecimal method names. - // P.add = P.plus; - P.subtract = P.minus; - P.multiply = P.times; - P.divide = P.div; - P.remainder = P.mod; - P.compareTo = P.cmp; - P.negate = P.neg; - */ - - // Helper functions for Decimal.prototype (P) and/or Decimal methods, and their callers. - - /* - * digitsToString P.cubeRoot, P.logarithm, P.squareRoot, P.toFraction, P.toPower, - * finiteToString, naturalExponential, naturalLogarithm - * checkInt32 P.toDecimalPlaces, P.toExponential, P.toFixed, P.toNearest, - * P.toPrecision, P.toSignificantDigits, toStringBinary, random - * checkRoundingDigits P.logarithm, P.toPower, naturalExponential, naturalLogarithm - * convertBase toStringBinary, parseOther - * cos P.cos - * divide P.atanh, P.cubeRoot, P.dividedBy, P.dividedToIntegerBy, - * P.logarithm, P.modulo, P.squareRoot, P.tan, P.tanh, P.toFraction, - * P.toNearest, toStringBinary, naturalExponential, naturalLogarithm, - * taylorSeries, atan2, parseOther - * finalise P.absoluteValue, P.atan, P.atanh, P.ceil, P.cos, P.cosh, - * P.cubeRoot, P.dividedToIntegerBy, P.floor, P.logarithm, P.minus, - * P.modulo, P.negated, P.plus, P.round, P.sin, P.sinh, P.squareRoot, - * P.tan, P.times, P.toDecimalPlaces, P.toExponential, P.toFixed, - * P.toNearest, P.toPower, P.toPrecision, P.toSignificantDigits, - * P.truncated, divide, getLn10, getPi, naturalExponential, - * naturalLogarithm, ceil, floor, round, trunc - * finiteToString P.toExponential, P.toFixed, P.toPrecision, P.toString, P.valueOf, - * toStringBinary - * getBase10Exponent P.minus, P.plus, P.times, parseOther - * getLn10 P.logarithm, naturalLogarithm - * getPi P.acos, P.asin, P.atan, toLessThanHalfPi, atan2 - * getPrecision P.precision, P.toFraction - * getZeroString digitsToString, finiteToString - * intPow P.toPower, parseOther - * isOdd toLessThanHalfPi - * maxOrMin max, min - * naturalExponential P.naturalExponential, P.toPower - * naturalLogarithm P.acosh, P.asinh, P.atanh, P.logarithm, P.naturalLogarithm, - * P.toPower, naturalExponential - * nonFiniteToString finiteToString, toStringBinary - * parseDecimal Decimal - * parseOther Decimal - * sin P.sin - * taylorSeries P.cosh, P.sinh, cos, sin - * toLessThanHalfPi P.cos, P.sin - * toStringBinary P.toBinary, P.toHexadecimal, P.toOctal - * truncate intPow - * - * Throws: P.logarithm, P.precision, P.toFraction, checkInt32, getLn10, getPi, - * naturalLogarithm, config, parseOther, random, Decimal - */ - - function digitsToString(d) { - var i, k, ws, - indexOfLastWord = d.length - 1, - str = '', - w = d[0]; - - if (indexOfLastWord > 0) { - str += w; - for (i = 1; i < indexOfLastWord; i++) { - ws = d[i] + ''; - k = LOG_BASE - ws.length; - if (k) { - str += getZeroString(k); - } - str += ws; - } - - w = d[i]; - ws = w + ''; - k = LOG_BASE - ws.length; - if (k) { - str += getZeroString(k); - } - } else if (w === 0) { - return '0'; - } - - // Remove trailing zeros of last w. - for (; w % 10 === 0;) { - w /= 10; - } - - return str + w; - } - - function checkInt32(i, min, max) { - if (i !== ~~i || i < min || i > max) { - throw Error(invalidArgument + i); - } - } - - /* - * Check 5 rounding digits if `repeating` is null, 4 otherwise. - * `repeating == null` if caller is `log` or `pow`, - * `repeating != null` if caller is `naturalLogarithm` or `naturalExponential`. - */ - function checkRoundingDigits(d, i, rm, repeating) { - var di, k, r, rd; - - // Get the length of the first word of the array d. - for (k = d[0]; k >= 10; k /= 10) { - --i; - } - - // Is the rounding digit in the first word of d? - if (--i < 0) { - i += LOG_BASE; - di = 0; - } else { - di = Math.ceil((i + 1) / LOG_BASE); - i %= LOG_BASE; - } - - // i is the index (0 - 6) of the rounding digit. - // E.g. if within the word 3487563 the first rounding digit is 5, - // then i = 4, k = 1000, rd = 3487563 % 1000 = 563 - k = mathpow(10, LOG_BASE - i); - rd = d[di] % k | 0; - - if (repeating == null) { - if (i < 3) { - if (i == 0) { - rd = rd / 100 | 0; - } else if (i == 1) { - rd = rd / 10 | 0; - } - r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 50000 || rd == 0; - } else { - r = (rm < 4 && rd + 1 == k || rm > 3 && rd + 1 == k / 2) && - (d[di + 1] / k / 100 | 0) == mathpow(10, i - 2) - 1 || - (rd == k / 2 || rd == 0) && (d[di + 1] / k / 100 | 0) == 0; - } - } else { - if (i < 4) { - if (i == 0) { - rd = rd / 1000 | 0; - } else if (i == 1) { - rd = rd / 100 | 0; - } else if (i == 2) { - rd = rd / 10 | 0; - } - r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999; - } else { - r = ((repeating || rm < 4) && rd + 1 == k || - (!repeating && rm > 3) && rd + 1 == k / 2) && - (d[di + 1] / k / 1000 | 0) == mathpow(10, i - 3) - 1; - } - } - - return r; - } - - // Convert string of `baseIn` to an array of numbers of `baseOut`. - // Eg. convertBase('255', 10, 16) returns [15, 15]. - // Eg. convertBase('ff', 16, 10) returns [2, 5, 5]. - function convertBase(str, baseIn, baseOut) { - var j, - arr = [0], - arrL, - i = 0, - strL = str.length; - - for (; i < strL;) { - for (arrL = arr.length; arrL--;) { - arr[arrL] *= baseIn; - } - arr[0] += NUMERALS.indexOf(str.charAt(i++)); - for (j = 0; j < arr.length; j++) { - if (arr[j] > baseOut - 1) { - if (arr[j + 1] === void 0) { - arr[j + 1] = 0; - } - arr[j + 1] += arr[j] / baseOut | 0; - arr[j] %= baseOut; - } - } - } - - return arr.reverse(); - } - - /* - * cos(x) = 1 - x^2/2! + x^4/4! - ... - * |x| < pi/2 - * - */ - function cosine(Ctor, x) { - var k, y, - len = x.d.length; - - // Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1 - // i.e. cos(x) = 8*(cos^4(x/4) - cos^2(x/4)) + 1 - - // Estimate the optimum number of times to use the argument reduction. - if (len < 32) { - k = Math.ceil(len / 3); - y = (1 / tinyPow(4, k)).toString(); - } else { - k = 16; - y = '2.3283064365386962890625e-10'; - } - - Ctor.precision += k; - - x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1)); - - // Reverse argument reduction - for (var i = k; i--;) { - var cos2x = x.times(x); - x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1); - } - - Ctor.precision -= k; - - return x; - } - - /* - * Perform division in the specified base. - */ - var divide = (function () { - - // Assumes non-zero x and k, and hence non-zero result. - function multiplyInteger(x, k, base) { - var temp, - carry = 0, - i = x.length; - - for (x = x.slice(); i--;) { - temp = x[i] * k + carry; - x[i] = temp % base | 0; - carry = temp / base | 0; - } - - if (carry) { - x.unshift(carry); - } - - return x; - } - - function compare(a, b, aL, bL) { - var i, r; - - if (aL != bL) { - r = aL > bL ? 1 : -1; - } else { - for (i = r = 0; i < aL; i++) { - if (a[i] != b[i]) { - r = a[i] > b[i] ? 1 : -1; - break; - } - } - } - - return r; - } - - function subtract(a, b, aL, base) { - var i = 0; - - // Subtract b from a. - for (; aL--;) { - a[aL] -= i; - i = a[aL] < b[aL] ? 1 : 0; - a[aL] = i * base + a[aL] - b[aL]; - } - - // Remove leading zeros. - for (; !a[0] && a.length > 1;) { - a.shift(); - } - } - - return function (x, y, pr, rm, dp, base) { - var cmp, e, i, k, logBase, more, prod, prodL, q, qd, rem, remL, rem0, sd, t, xi, xL, yd0, - yL, yz, - Ctor = x.constructor, - sign = x.s == y.s ? 1 : -1, - xd = x.d, - yd = y.d; - - // Either NaN, Infinity or 0? - if (!xd || !xd[0] || !yd || !yd[0]) { - - return new Ctor(// Return NaN if either NaN, or both Infinity or 0. - !x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN : - - // Return ±0 if x is 0 or y is ±Infinity, or return ±Infinity as y is 0. - xd && xd[0] == 0 || !yd ? sign * 0 : sign / 0); - } - - if (base) { - logBase = 1; - e = x.e - y.e; - } else { - base = BASE; - logBase = LOG_BASE; - e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase); - } - - yL = yd.length; - xL = xd.length; - q = new Ctor(sign); - qd = q.d = []; - - // Result exponent may be one less than e. - // The digit array of a Decimal from toStringBinary may have trailing zeros. - for (i = 0; yd[i] == (xd[i] || 0); i++) { - ; - } - - if (yd[i] > (xd[i] || 0)) { - e--; - } - - if (pr == null) { - sd = pr = Ctor.precision; - rm = Ctor.rounding; - } else if (dp) { - sd = pr + (x.e - y.e) + 1; - } else { - sd = pr; - } - - if (sd < 0) { - qd.push(1); - more = true; - } else { - - // Convert precision in number of base 10 digits to base 1e7 digits. - sd = sd / logBase + 2 | 0; - i = 0; - - // divisor < 1e7 - if (yL == 1) { - k = 0; - yd = yd[0]; - sd++; - - // k is the carry. - for (; (i < xL || k) && sd--; i++) { - t = k * base + (xd[i] || 0); - qd[i] = t / yd | 0; - k = t % yd | 0; - } - - more = k || i < xL; - - // divisor >= 1e7 - } else { - - // Normalise xd and yd so highest order digit of yd is >= base/2 - k = base / (yd[0] + 1) | 0; - - if (k > 1) { - yd = multiplyInteger(yd, k, base); - xd = multiplyInteger(xd, k, base); - yL = yd.length; - xL = xd.length; - } - - xi = yL; - rem = xd.slice(0, yL); - remL = rem.length; - - // Add zeros to make remainder as long as divisor. - for (; remL < yL;) { - rem[remL++] = 0; - } - - yz = yd.slice(); - yz.unshift(0); - yd0 = yd[0]; - - if (yd[1] >= base / 2) { - ++yd0; - } - - do { - k = 0; - - // Compare divisor and remainder. - cmp = compare(yd, rem, yL, remL); - - // If divisor < remainder. - if (cmp < 0) { - - // Calculate trial digit, k. - rem0 = rem[0]; - if (yL != remL) { - rem0 = rem0 * base + (rem[1] || 0); - } - - // k will be how many times the divisor goes into the current remainder. - k = rem0 / yd0 | 0; - - // Algorithm: - // 1. product = divisor * trial digit (k) - // 2. if product > remainder: product -= divisor, k-- - // 3. remainder -= product - // 4. if product was < remainder at 2: - // 5. compare new remainder and divisor - // 6. If remainder > divisor: remainder -= divisor, k++ - - if (k > 1) { - if (k >= base) { - k = base - 1; - } - - // product = divisor * trial digit. - prod = multiplyInteger(yd, k, base); - prodL = prod.length; - remL = rem.length; - - // Compare product and remainder. - cmp = compare(prod, rem, prodL, remL); - - // product > remainder. - if (cmp == 1) { - k--; - - // Subtract divisor from product. - subtract(prod, yL < prodL ? yz : yd, prodL, base); - } - } else { - - // cmp is -1. - // If k is 0, there is no need to compare yd and rem again below, so change cmp to 1 - // to avoid it. If k is 1 there is a need to compare yd and rem again below. - if (k == 0) { - cmp = k = 1; - } - prod = yd.slice(); - } - - prodL = prod.length; - if (prodL < remL) { - prod.unshift(0); - } - - // Subtract product from remainder. - subtract(rem, prod, remL, base); - - // If product was < previous remainder. - if (cmp == -1) { - remL = rem.length; - - // Compare divisor and new remainder. - cmp = compare(yd, rem, yL, remL); - - // If divisor < new remainder, subtract divisor from remainder. - if (cmp < 1) { - k++; - - // Subtract divisor from remainder. - subtract(rem, yL < remL ? yz : yd, remL, base); - } - } - - remL = rem.length; - } else if (cmp === 0) { - k++; - rem = [0]; - } // if cmp === 1, k will be 0 - - // Add the next digit, k, to the result array. - qd[i++] = k; - - // Update the remainder. - if (cmp && rem[0]) { - rem[remL++] = xd[xi] || 0; - } else { - rem = [xd[xi]]; - remL = 1; - } - - } while ((xi++ < xL || rem[0] !== void 0) && sd--); - - more = rem[0] !== void 0; - } - - // Leading zero? - if (!qd[0]) { - qd.shift(); - } - } - - // logBase is 1 when divide is being used for base conversion. - if (logBase == 1) { - q.e = e; - inexact = more; - } else { - - // To calculate q.e, first get the number of digits of qd[0]. - for (i = 1, k = qd[0]; k >= 10; k /= 10) { - i++; - } - q.e = i + e * logBase - 1; - - finalise(q, dp ? pr + q.e + 1 : pr, rm, more); - } - - return q; - }; - })(); - - /* - * Round `x` to `sd` significant digits using rounding mode `rm`. - * Check for over/under-flow. - */ - function finalise(x, sd, rm, isTruncated) { - var digits, i, j, k, rd, roundUp, w, xd, xdi, - Ctor = x.constructor; - - // Don't round if sd is null or undefined. - out: if (sd != null) { - xd = x.d; - - // Infinity/NaN. - if (!xd) { - return x; - } - - // rd: the rounding digit, i.e. the digit after the digit that may be rounded up. - // w: the word of xd containing rd, a base 1e7 number. - // xdi: the index of w within xd. - // digits: the number of digits of w. - // i: what would be the index of rd within w if all the numbers were 7 digits long (i.e. if - // they had leading zeros) - // j: if > 0, the actual index of rd within w (if < 0, rd is a leading zero). - - // Get the length of the first word of the digits array xd. - for (digits = 1, k = xd[0]; k >= 10; k /= 10) { - digits++; - } - i = sd - digits; - - // Is the rounding digit in the first word of xd? - if (i < 0) { - i += LOG_BASE; - j = sd; - w = xd[xdi = 0]; - - // Get the rounding digit at index j of w. - rd = w / mathpow(10, digits - j - 1) % 10 | 0; - } else { - xdi = Math.ceil((i + 1) / LOG_BASE); - k = xd.length; - if (xdi >= k) { - if (isTruncated) { - - // Needed by `naturalExponential`, `naturalLogarithm` and `squareRoot`. - for (; k++ <= xdi;) { - xd.push(0); - } - w = rd = 0; - digits = 1; - i %= LOG_BASE; - j = i - LOG_BASE + 1; - } else { - break out; - } - } else { - w = k = xd[xdi]; - - // Get the number of digits of w. - for (digits = 1; k >= 10; k /= 10) { - digits++; - } - - // Get the index of rd within w. - i %= LOG_BASE; - - // Get the index of rd within w, adjusted for leading zeros. - // The number of leading zeros of w is given by LOG_BASE - digits. - j = i - LOG_BASE + digits; - - // Get the rounding digit at index j of w. - rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0; - } - } - - // Are there any non-zero digits after the rounding digit? - isTruncated = isTruncated || sd < 0 || - xd[xdi + 1] !== void 0 || (j < 0 ? w : w % mathpow(10, digits - j - 1)); - - // The expression `w % mathpow(10, digits - j - 1)` returns all the digits of w to the right - // of the digit at (left-to-right) index j, e.g. if w is 908714 and j is 2, the expression - // will give 714. - - roundUp = rm < 4 - ? (rd || isTruncated) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) - : rd > 5 || rd == 5 && (rm == 4 || isTruncated || rm == 6 && - - // Check whether the digit to the left of the rounding digit is odd. - ((i > 0 ? j > 0 ? w / mathpow(10, digits - j) : 0 : xd[xdi - 1]) % 10) & 1 || - rm == (x.s < 0 ? 8 : 7)); - - if (sd < 1 || !xd[0]) { - xd.length = 0; - if (roundUp) { - - // Convert sd to decimal places. - sd -= x.e + 1; - - // 1, 0.1, 0.01, 0.001, 0.0001 etc. - xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE); - x.e = -sd || 0; - } else { - - // Zero. - xd[0] = x.e = 0; - } - - return x; - } - - // Remove excess digits. - if (i == 0) { - xd.length = xdi; - k = 1; - xdi--; - } else { - xd.length = xdi + 1; - k = mathpow(10, LOG_BASE - i); - - // E.g. 56700 becomes 56000 if 7 is the rounding digit. - // j > 0 means i > number of leading zeros of w. - xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0; - } - - if (roundUp) { - for (;;) { - - // Is the digit to be rounded up in the first word of xd? - if (xdi == 0) { - - // i will be the length of xd[0] before k is added. - for (i = 1, j = xd[0]; j >= 10; j /= 10) { - i++; - } - j = xd[0] += k; - for (k = 1; j >= 10; j /= 10) { - k++; - } - - // if i != k the length has increased. - if (i != k) { - x.e++; - if (xd[0] == BASE) { - xd[0] = 1; - } - } - - break; - } else { - xd[xdi] += k; - if (xd[xdi] != BASE) { - break; - } - xd[xdi--] = 0; - k = 1; - } - } - } - - // Remove trailing zeros. - for (i = xd.length; xd[--i] === 0;) { - xd.pop(); - } - } - - if (external) { - - // Overflow? - if (x.e > Ctor.maxE) { - - // Infinity. - x.d = null; - x.e = NaN; - - // Underflow? - } else if (x.e < Ctor.minE) { - - // Zero. - x.e = 0; - x.d = [0]; - // Ctor.underflow = true; - } // else Ctor.underflow = false; - } - - return x; - } - - function finiteToString(x, isExp, sd) { - if (!x.isFinite()) { - return nonFiniteToString(x); - } - var k, - e = x.e, - str = digitsToString(x.d), - len = str.length; - - if (isExp) { - if (sd && (k = sd - len) > 0) { - str = str.charAt(0) + '.' + str.slice(1) + getZeroString(k); - } else if (len > 1) { - str = str.charAt(0) + '.' + str.slice(1); - } - - str = str + (x.e < 0 ? 'e' : 'e+') + x.e; - } else if (e < 0) { - str = '0.' + getZeroString(-e - 1) + str; - if (sd && (k = sd - len) > 0) { - str += getZeroString(k); - } - } else if (e >= len) { - str += getZeroString(e + 1 - len); - if (sd && (k = sd - e - 1) > 0) { - str = str + '.' + getZeroString(k); - } - } else { - if ((k = e + 1) < len) { - str = str.slice(0, k) + '.' + str.slice(k); - } - if (sd && (k = sd - len) > 0) { - if (e + 1 === len) { - str += '.'; - } - str += getZeroString(k); - } - } - - return str; - } - - // Calculate the base 10 exponent from the base 1e7 exponent. - function getBase10Exponent(digits, e) { - var w = digits[0]; - - // Add the number of digits of the first word of the digits array. - for (e *= LOG_BASE; w >= 10; w /= 10) { - e++; - } - return e; - } - - function getLn10(Ctor, sd, pr) { - if (sd > LN10_PRECISION) { - - // Reset global state in case the exception is caught. - external = true; - if (pr) { - Ctor.precision = pr; - } - throw Error(precisionLimitExceeded); - } - return finalise(new Ctor(LN10), sd, 1, true); - } - - function getPi(Ctor, sd, rm) { - if (sd > PI_PRECISION) { - throw Error(precisionLimitExceeded); - } - return finalise(new Ctor(PI), sd, rm, true); - } - - function getPrecision(digits) { - var w = digits.length - 1, - len = w * LOG_BASE + 1; - - w = digits[w]; - - // If non-zero... - if (w) { - - // Subtract the number of trailing zeros of the last word. - for (; w % 10 == 0; w /= 10) { - len--; - } - - // Add the number of digits of the first word. - for (w = digits[0]; w >= 10; w /= 10) { - len++; - } - } - - return len; - } - - function getZeroString(k) { - var zs = ''; - for (; k--;) { - zs += '0'; - } - return zs; - } - - /* - * Return a new Decimal whose value is the value of Decimal `x` to the power `n`, where `n` is an - * integer of type number. - * - * Implements 'exponentiation by squaring'. Called by `pow` and `parseOther`. - * - */ - function intPow(Ctor, x, n, pr) { - var isTruncated, - r = new Ctor(1), - - // Max n of 9007199254740991 takes 53 loop iterations. - // Maximum digits array length; leaves [28, 34] guard digits. - k = Math.ceil(pr / LOG_BASE + 4); - - external = false; - - for (;;) { - if (n % 2) { - r = r.times(x); - if (truncate(r.d, k)) { - isTruncated = true; - } - } - - n = mathfloor(n / 2); - if (n === 0) { - - // To ensure correct rounding when r.d is truncated, increment the last word if it is zero. - n = r.d.length - 1; - if (isTruncated && r.d[n] === 0) { - ++r.d[n]; - } - break; - } - - x = x.times(x); - truncate(x.d, k); - } - - external = true; - - return r; - } - - function isOdd(n) { - return n.d[n.d.length - 1] & 1; - } - - /* - * Handle `max` and `min`. `ltgt` is 'lt' or 'gt'. - */ - function maxOrMin(Ctor, args, ltgt) { - var y, - x = new Ctor(args[0]), - i = 0; - - for (; ++i < args.length;) { - y = new Ctor(args[i]); - if (!y.s) { - x = y; - break; - } else if (x[ltgt](y)) { - x = y; - } - } - - return x; - } - - /* - * Return a new Decimal whose value is the natural exponential of `x` rounded to `sd` significant - * digits. - * - * Taylor/Maclaurin series. - * - * exp(x) = x^0/0! + x^1/1! + x^2/2! + x^3/3! + ... - * - * Argument reduction: - * Repeat x = x / 32, k += 5, until |x| < 0.1 - * exp(x) = exp(x / 2^k)^(2^k) - * - * Previously, the argument was initially reduced by - * exp(x) = exp(r) * 10^k where r = x - k * ln10, k = floor(x / ln10) - * to first put r in the range [0, ln10], before dividing by 32 until |x| < 0.1, but this was - * found to be slower than just dividing repeatedly by 32 as above. - * - * Max integer argument: exp('20723265836946413') = 6.3e+9000000000000000 - * Min integer argument: exp('-20723265836946411') = 1.2e-9000000000000000 - * (Math object integer min/max: Math.exp(709) = 8.2e+307, Math.exp(-745) = 5e-324) - * - * exp(Infinity) = Infinity - * exp(-Infinity) = 0 - * exp(NaN) = NaN - * exp(±0) = 1 - * - * exp(x) is non-terminating for any finite, non-zero x. - * - * The result will always be correctly rounded. - * - */ - function naturalExponential(x, sd) { - var denominator, guard, j, pow, sum, t, wpr, - rep = 0, - i = 0, - k = 0, - Ctor = x.constructor, - rm = Ctor.rounding, - pr = Ctor.precision; - - // 0/NaN/Infinity? - if (!x.d || !x.d[0] || x.e > 17) { - - return new Ctor(x.d - ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0 - : x.s ? x.s < 0 ? 0 : x : 0 / 0); - } - - if (sd == null) { - external = false; - wpr = pr; - } else { - wpr = sd; - } - - t = new Ctor(0.03125); - - // while abs(x) >= 0.1 - while (x.e > -2) { - - // x = x / 2^5 - x = x.times(t); - k += 5; - } - - // Use 2 * log10(2^k) + 5 (empirically derived) to estimate the increase in precision - // necessary to ensure the first 4 rounding digits are correct. - guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0; - wpr += guard; - denominator = pow = sum = new Ctor(1); - Ctor.precision = wpr; - - for (;;) { - pow = finalise(pow.times(x), wpr, 1); - denominator = denominator.times(++i); - t = sum.plus(divide(pow, denominator, wpr, 1)); - - if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) { - j = k; - while (j--) { - sum = finalise(sum.times(sum), wpr, 1); - } - - // Check to see if the first 4 rounding digits are [49]999. - // If so, repeat the summation with a higher precision, otherwise - // e.g. with precision: 18, rounding: 1 - // exp(18.404272462595034083567793919843761) = 98372560.1229999999 (should be 98372560.123) - // `wpr - guard` is the index of first rounding digit. - if (sd == null) { - - if (rep < 3 && checkRoundingDigits(sum.d, wpr - guard, rm, rep)) { - Ctor.precision = wpr += 10; - denominator = pow = t = new Ctor(1); - i = 0; - rep++; - } else { - return finalise(sum, Ctor.precision = pr, rm, external = true); - } - } else { - Ctor.precision = pr; - return sum; - } - } - - sum = t; - } - } - - /* - * Return a new Decimal whose value is the natural logarithm of `x` rounded to `sd` significant - * digits. - * - * ln(-n) = NaN - * ln(0) = -Infinity - * ln(-0) = -Infinity - * ln(1) = 0 - * ln(Infinity) = Infinity - * ln(-Infinity) = NaN - * ln(NaN) = NaN - * - * ln(n) (n != 1) is non-terminating. - * - */ - function naturalLogarithm(y, sd) { - var c, c0, denominator, e, numerator, rep, sum, t, wpr, x1, x2, - n = 1, - guard = 10, - x = y, - xd = x.d, - Ctor = x.constructor, - rm = Ctor.rounding, - pr = Ctor.precision; - - // Is x negative or Infinity, NaN, 0 or 1? - if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) { - return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x); - } - - if (sd == null) { - external = false; - wpr = pr; - } else { - wpr = sd; - } - - Ctor.precision = wpr += guard; - c = digitsToString(xd); - c0 = c.charAt(0); - - if (Math.abs(e = x.e) < 1.5e15) { - - // Argument reduction. - // The series converges faster the closer the argument is to 1, so using - // ln(a^b) = b * ln(a), ln(a) = ln(a^b) / b - // multiply the argument by itself until the leading digits of the significand are 7, 8, 9, - // 10, 11, 12 or 13, recording the number of multiplications so the sum of the series can - // later be divided by this number, then separate out the power of 10 using - // ln(a*10^b) = ln(a) + b*ln(10). - - // max n is 21 (gives 0.9, 1.0 or 1.1) (9e15 / 21 = 4.2e14). - //while (c0 < 9 && c0 != 1 || c0 == 1 && c.charAt(1) > 1) { - // max n is 6 (gives 0.7 - 1.3) - while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) { - x = x.times(y); - c = digitsToString(x.d); - c0 = c.charAt(0); - n++; - } - - e = x.e; - - if (c0 > 1) { - x = new Ctor('0.' + c); - e++; - } else { - x = new Ctor(c0 + '.' + c.slice(1)); - } - } else { - - // The argument reduction method above may result in overflow if the argument y is a massive - // number with exponent >= 1500000000000000 (9e15 / 6 = 1.5e15), so instead recall this - // function using ln(x*10^e) = ln(x) + e*ln(10). - t = getLn10(Ctor, wpr + 2, pr).times(e + ''); - x = naturalLogarithm(new Ctor(c0 + '.' + c.slice(1)), wpr - guard).plus(t); - Ctor.precision = pr; - - return sd == null ? finalise(x, pr, rm, external = true) : x; - } - - // x1 is x reduced to a value near 1. - x1 = x; - - // Taylor series. - // ln(y) = ln((1 + x)/(1 - x)) = 2(x + x^3/3 + x^5/5 + x^7/7 + ...) - // where x = (y - 1)/(y + 1) (|x| < 1) - sum = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1); - x2 = finalise(x.times(x), wpr, 1); - denominator = 3; - - for (;;) { - numerator = finalise(numerator.times(x2), wpr, 1); - t = sum.plus(divide(numerator, new Ctor(denominator), wpr, 1)); - - if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) { - sum = sum.times(2); - - // Reverse the argument reduction. Check that e is not 0 because, besides preventing an - // unnecessary calculation, -0 + 0 = +0 and to ensure correct rounding -0 needs to stay -0. - if (e !== 0) { - sum = sum.plus(getLn10(Ctor, wpr + 2, pr).times(e + '')); - } - sum = divide(sum, new Ctor(n), wpr, 1); - - // Is rm > 3 and the first 4 rounding digits 4999, or rm < 4 (or the summation has - // been repeated previously) and the first 4 rounding digits 9999? - // If so, restart the summation with a higher precision, otherwise - // e.g. with precision: 12, rounding: 1 - // ln(135520028.6126091714265381533) = 18.7246299999 when it should be 18.72463. - // `wpr - guard` is the index of first rounding digit. - if (sd == null) { - if (checkRoundingDigits(sum.d, wpr - guard, rm, rep)) { - Ctor.precision = wpr += guard; - t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1); - x2 = finalise(x.times(x), wpr, 1); - denominator = rep = 1; - } else { - return finalise(sum, Ctor.precision = pr, rm, external = true); - } - } else { - Ctor.precision = pr; - return sum; - } - } - - sum = t; - denominator += 2; - } - } - - // ±Infinity, NaN. - function nonFiniteToString(x) { - // Unsigned. - return String(x.s * x.s / 0); - } - - /* - * Parse the value of a new Decimal `x` from string `str`. - */ - function parseDecimal(x, str) { - var e, i, len; - - // Decimal point? - if ((e = str.indexOf('.')) > -1) { - str = str.replace('.', ''); - } - - // Exponential form? - if ((i = str.search(/e/i)) > 0) { - - // Determine exponent. - if (e < 0) { - e = i; - } - e += +str.slice(i + 1); - str = str.substring(0, i); - } else if (e < 0) { - - // Integer. - e = str.length; - } - - // Determine leading zeros. - for (i = 0; str.charCodeAt(i) === 48; i++) { - ; - } - - // Determine trailing zeros. - for (len = str.length; str.charCodeAt(len - 1) === 48; --len) { - ; - } - str = str.slice(i, len); - - if (str) { - len -= i; - x.e = e = e - i - 1; - x.d = []; - - // Transform base - - // e is the base 10 exponent. - // i is where to slice str to get the first word of the digits array. - i = (e + 1) % LOG_BASE; - if (e < 0) { - i += LOG_BASE; - } - - if (i < len) { - if (i) { - x.d.push(+str.slice(0, i)); - } - for (len -= LOG_BASE; i < len;) { - x.d.push(+str.slice(i, i += LOG_BASE)); - } - str = str.slice(i); - i = LOG_BASE - str.length; - } else { - i -= len; - } - - for (; i--;) { - str += '0'; - } - x.d.push(+str); - - if (external) { - - // Overflow? - if (x.e > x.constructor.maxE) { - - // Infinity. - x.d = null; - x.e = NaN; - - // Underflow? - } else if (x.e < x.constructor.minE) { - - // Zero. - x.e = 0; - x.d = [0]; - // x.constructor.underflow = true; - } // else x.constructor.underflow = false; - } - } else { - - // Zero. - x.e = 0; - x.d = [0]; - } - - return x; - } - - /* - * Parse the value of a new Decimal `x` from a string `str`, which is not a decimal value. - */ - function parseOther(x, str) { - var base, Ctor, divisor, i, isFloat, len, p, xd, xe; - - if (str === 'Infinity' || str === 'NaN') { - if (!+str) { - x.s = NaN; - } - x.e = NaN; - x.d = null; - return x; - } - - if (isHex.test(str)) { - base = 16; - str = str.toLowerCase(); - } else if (isBinary.test(str)) { - base = 2; - } else if (isOctal.test(str)) { - base = 8; - } else { - throw Error(invalidArgument + str); - } - - // Is there a binary exponent part? - i = str.search(/p/i); - - if (i > 0) { - p = +str.slice(i + 1); - str = str.substring(2, i); - } else { - str = str.slice(2); - } - - // Convert `str` as an integer then divide the result by `base` raised to a power such that the - // fraction part will be restored. - i = str.indexOf('.'); - isFloat = i >= 0; - Ctor = x.constructor; - - if (isFloat) { - str = str.replace('.', ''); - len = str.length; - i = len - i; - - // log[10](16) = 1.2041... , log[10](88) = 1.9444.... - divisor = intPow(Ctor, new Ctor(base), i, i * 2); - } - - xd = convertBase(str, base, BASE); - xe = xd.length - 1; - - // Remove trailing zeros. - for (i = xe; xd[i] === 0; --i) { - xd.pop(); - } - if (i < 0) { - return new Ctor(x.s * 0); - } - x.e = getBase10Exponent(xd, xe); - x.d = xd; - external = false; - - // At what precision to perform the division to ensure exact conversion? - // maxDecimalIntegerPartDigitCount = ceil(log[10](b) * otherBaseIntegerPartDigitCount) - // log[10](2) = 0.30103, log[10](8) = 0.90309, log[10](16) = 1.20412 - // E.g. ceil(1.2 * 3) = 4, so up to 4 decimal digits are needed to represent 3 hex int digits. - // maxDecimalFractionPartDigitCount = {Hex:4|Oct:3|Bin:1} * otherBaseFractionPartDigitCount - // Therefore using 4 * the number of digits of str will always be enough. - if (isFloat) { - x = divide(x, divisor, len * 4); - } - - // Multiply by the binary exponent part if present. - if (p) { - x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p)); - } - external = true; - - return x; - } - - /* - * sin(x) = x - x^3/3! + x^5/5! - ... - * |x| < pi/2 - * - */ - function sine(Ctor, x) { - var k, - len = x.d.length; - - if (len < 3) { - return taylorSeries(Ctor, 2, x, x); - } - - // Argument reduction: sin(5x) = 16*sin^5(x) - 20*sin^3(x) + 5*sin(x) - // i.e. sin(x) = 16*sin^5(x/5) - 20*sin^3(x/5) + 5*sin(x/5) - // and sin(x) = sin(x/5)(5 + sin^2(x/5)(16sin^2(x/5) - 20)) - - // Estimate the optimum number of times to use the argument reduction. - k = 1.4 * Math.sqrt(len); - k = k > 16 ? 16 : k | 0; - - x = x.times(1 / tinyPow(5, k)); - x = taylorSeries(Ctor, 2, x, x); - - // Reverse argument reduction - var sin2_x, - d5 = new Ctor(5), - d16 = new Ctor(16), - d20 = new Ctor(20); - for (; k--;) { - sin2_x = x.times(x); - x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20)))); - } - - return x; - } - - // Calculate Taylor series for `cos`, `cosh`, `sin` and `sinh`. - function taylorSeries(Ctor, n, x, y, isHyperbolic) { - var j, t, u, x2, - i = 1, - pr = Ctor.precision, - k = Math.ceil(pr / LOG_BASE); - - external = false; - x2 = x.times(x); - u = new Ctor(y); - - for (;;) { - t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1); - u = isHyperbolic ? y.plus(t) : y.minus(t); - y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1); - t = u.plus(y); - - if (t.d[k] !== void 0) { - for (j = k; t.d[j] === u.d[j] && j--;) { - ; - } - if (j == -1) { - break; - } - } - - j = u; - u = y; - y = t; - t = j; - i++; - } - - external = true; - t.d.length = k + 1; - - return t; - } - - // Exponent e must be positive and non-zero. - function tinyPow(b, e) { - var n = b; - while (--e) { - n *= b; - } - return n; - } - - // Return the absolute value of `x` reduced to less than or equal to half pi. - function toLessThanHalfPi(Ctor, x) { - var t, - isNeg = x.s < 0, - pi = getPi(Ctor, Ctor.precision, 1), - halfPi = pi.times(0.5); - - x = x.abs(); - - if (x.lte(halfPi)) { - quadrant = isNeg ? 4 : 1; - return x; - } - - t = x.divToInt(pi); - - if (t.isZero()) { - quadrant = isNeg ? 3 : 2; - } else { - x = x.minus(t.times(pi)); - - // 0 <= x < pi - if (x.lte(halfPi)) { - quadrant = isOdd(t) ? (isNeg ? 2 : 3) : (isNeg ? 4 : 1); - return x; - } - - quadrant = isOdd(t) ? (isNeg ? 1 : 4) : (isNeg ? 3 : 2); - } - - return x.minus(pi).abs(); - } - - /* - * Return the value of Decimal `x` as a string in base `baseOut`. - * - * If the optional `sd` argument is present include a binary exponent suffix. - */ - function toStringBinary(x, baseOut, sd, rm) { - var base, e, i, k, len, roundUp, str, xd, y, - Ctor = x.constructor, - isExp = sd !== void 0; - - if (isExp) { - checkInt32(sd, 1, MAX_DIGITS); - if (rm === void 0) { - rm = Ctor.rounding; - } else { - checkInt32(rm, 0, 8); - } - } else { - sd = Ctor.precision; - rm = Ctor.rounding; - } - - if (!x.isFinite()) { - str = nonFiniteToString(x); - } else { - str = finiteToString(x); - i = str.indexOf('.'); - - // Use exponential notation according to `toExpPos` and `toExpNeg`? No, but if required: - // maxBinaryExponent = floor((decimalExponent + 1) * log[2](10)) - // minBinaryExponent = floor(decimalExponent * log[2](10)) - // log[2](10) = 3.321928094887362347870319429489390175864 - - if (isExp) { - base = 2; - if (baseOut == 16) { - sd = sd * 4 - 3; - } else if (baseOut == 8) { - sd = sd * 3 - 2; - } - } else { - base = baseOut; - } - - // Convert the number as an integer then divide the result by its base raised to a power such - // that the fraction part will be restored. - - // Non-integer. - if (i >= 0) { - str = str.replace('.', ''); - y = new Ctor(1); - y.e = str.length - i; - y.d = convertBase(finiteToString(y), 10, base); - y.e = y.d.length; - } - - xd = convertBase(str, 10, base); - e = len = xd.length; - - // Remove trailing zeros. - for (; xd[--len] == 0;) { - xd.pop(); - } - - if (!xd[0]) { - str = isExp ? '0p+0' : '0'; - } else { - if (i < 0) { - e--; - } else { - x = new Ctor(x); - x.d = xd; - x.e = e; - x = divide(x, y, sd, rm, 0, base); - xd = x.d; - e = x.e; - roundUp = inexact; - } - - // The rounding digit, i.e. the digit after the digit that may be rounded up. - i = xd[sd]; - k = base / 2; - roundUp = roundUp || xd[sd + 1] !== void 0; - - roundUp = rm < 4 - ? (i !== void 0 || roundUp) && (rm === 0 || rm === (x.s < 0 ? 3 : 2)) - : i > k || i === k && (rm === 4 || roundUp || rm === 6 && xd[sd - 1] & 1 || - rm === (x.s < 0 ? 8 : 7)); - - xd.length = sd; - - if (roundUp) { - - // Rounding up may mean the previous digit has to be rounded up and so on. - for (; ++xd[--sd] > base - 1;) { - xd[sd] = 0; - if (!sd) { - ++e; - xd.unshift(1); - } - } - } - - // Determine trailing zeros. - for (len = xd.length; !xd[len - 1]; --len) { - ; - } - - // E.g. [4, 11, 15] becomes 4bf. - for (i = 0, str = ''; i < len; i++) { - str += NUMERALS.charAt(xd[i]); - } - - // Add binary exponent suffix? - if (isExp) { - if (len > 1) { - if (baseOut == 16 || baseOut == 8) { - i = baseOut == 16 ? 4 : 3; - for (--len; len % i; len++) { - str += '0'; - } - xd = convertBase(str, base, baseOut); - for (len = xd.length; !xd[len - 1]; --len) { - ; - } - - // xd[0] will always be be 1 - for (i = 1, str = '1.'; i < len; i++) { - str += NUMERALS.charAt(xd[i]); - } - } else { - str = str.charAt(0) + '.' + str.slice(1); - } - } - - str = str + (e < 0 ? 'p' : 'p+') + e; - } else if (e < 0) { - for (; ++e;) { - str = '0' + str; - } - str = '0.' + str; - } else { - if (++e > len) { - for (e -= len; e--;) { - str += '0'; - } - } else if (e < len) { - str = str.slice(0, e) + '.' + str.slice(e); - } - } - } - - str = (baseOut == 16 ? '0x' : baseOut == 2 ? '0b' : baseOut == 8 ? '0o' : '') + str; - } - - return x.s < 0 ? '-' + str : str; - } - - // Does not strip trailing zeros. - function truncate(arr, len) { - if (arr.length > len) { - arr.length = len; - return true; - } - } - - // Decimal methods - - /* - * abs - * acos - * acosh - * add - * asin - * asinh - * atan - * atanh - * atan2 - * cbrt - * ceil - * clone - * config - * cos - * cosh - * div - * exp - * floor - * hypot - * ln - * log - * log2 - * log10 - * max - * min - * mod - * mul - * pow - * random - * round - * set - * sign - * sin - * sinh - * sqrt - * sub - * tan - * tanh - * trunc - */ - - /* - * Return a new Decimal whose value is the absolute value of `x`. - * - * x {number|string|Decimal} - * - */ - function abs(x) { - return new this(x).abs(); - } - - /* - * Return a new Decimal whose value is the arccosine in radians of `x`. - * - * x {number|string|Decimal} - * - */ - function acos(x) { - return new this(x).acos(); - } - - /* - * Return a new Decimal whose value is the inverse of the hyperbolic cosine of `x`, rounded to - * `precision` significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} A value in radians. - * - */ - function acosh(x) { - return new this(x).acosh(); - } - - /* - * Return a new Decimal whose value is the sum of `x` and `y`, rounded to `precision` significant - * digits using rounding mode `rounding`. - * - * x {number|string|Decimal} - * y {number|string|Decimal} - * - */ - function add(x, y) { - return new this(x).plus(y); - } - - /* - * Return a new Decimal whose value is the arcsine in radians of `x`, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} - * - */ - function asin(x) { - return new this(x).asin(); - } - - /* - * Return a new Decimal whose value is the inverse of the hyperbolic sine of `x`, rounded to - * `precision` significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} A value in radians. - * - */ - function asinh(x) { - return new this(x).asinh(); - } - - /* - * Return a new Decimal whose value is the arctangent in radians of `x`, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} - * - */ - function atan(x) { - return new this(x).atan(); - } - - /* - * Return a new Decimal whose value is the inverse of the hyperbolic tangent of `x`, rounded to - * `precision` significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} A value in radians. - * - */ - function atanh(x) { - return new this(x).atanh(); - } - - /* - * Return a new Decimal whose value is the arctangent in radians of `y/x` in the range -pi to pi - * (inclusive), rounded to `precision` significant digits using rounding mode `rounding`. - * - * Domain: [-Infinity, Infinity] - * Range: [-pi, pi] - * - * y {number|string|Decimal} The y-coordinate. - * x {number|string|Decimal} The x-coordinate. - * - * atan2(±0, -0) = ±pi - * atan2(±0, +0) = ±0 - * atan2(±0, -x) = ±pi for x > 0 - * atan2(±0, x) = ±0 for x > 0 - * atan2(-y, ±0) = -pi/2 for y > 0 - * atan2(y, ±0) = pi/2 for y > 0 - * atan2(±y, -Infinity) = ±pi for finite y > 0 - * atan2(±y, +Infinity) = ±0 for finite y > 0 - * atan2(±Infinity, x) = ±pi/2 for finite x - * atan2(±Infinity, -Infinity) = ±3*pi/4 - * atan2(±Infinity, +Infinity) = ±pi/4 - * atan2(NaN, x) = NaN - * atan2(y, NaN) = NaN - * - */ - function atan2(y, x) { - y = new this(y); - x = new this(x); - var r, - pr = this.precision, - rm = this.rounding, - wpr = pr + 4; - - // Either NaN - if (!y.s || !x.s) { - r = new this(NaN); - - // Both ±Infinity - } else if (!y.d && !x.d) { - r = getPi(this, wpr, 1).times(x.s > 0 ? 0.25 : 0.75); - r.s = y.s; - - // x is ±Infinity or y is ±0 - } else if (!x.d || y.isZero()) { - r = x.s < 0 ? getPi(this, pr, rm) : new this(0); - r.s = y.s; - - // y is ±Infinity or x is ±0 - } else if (!y.d || x.isZero()) { - r = getPi(this, wpr, 1).times(0.5); - r.s = y.s; - - // Both non-zero and finite - } else if (x.s < 0) { - this.precision = wpr; - this.rounding = 1; - r = this.atan(divide(y, x, wpr, 1)); - x = getPi(this, wpr, 1); - this.precision = pr; - this.rounding = rm; - r = y.s < 0 ? r.minus(x) : r.plus(x); - } else { - r = this.atan(divide(y, x, wpr, 1)); - } - - return r; - } - - /* - * Return a new Decimal whose value is the cube root of `x`, rounded to `precision` significant - * digits using rounding mode `rounding`. - * - * x {number|string|Decimal} - * - */ - function cbrt(x) { - return new this(x).cbrt(); - } - - /* - * Return a new Decimal whose value is `x` rounded to an integer using `ROUND_CEIL`. - * - * x {number|string|Decimal} - * - */ - function ceil(x) { - return finalise(x = new this(x), x.e + 1, 2); - } - - /* - * Configure global settings for a Decimal constructor. - * - * `obj` is an object with one or more of the following properties, - * - * precision {number} - * rounding {number} - * toExpNeg {number} - * toExpPos {number} - * maxE {number} - * minE {number} - * modulo {number} - * crypto {boolean|number} - * defaults {true} - * - * E.g. Decimal.config({ precision: 20, rounding: 4 }) - * - */ - function config(obj) { - if (!obj || typeof obj !== 'object') { - throw Error(decimalError + 'Object expected'); - } - var i, p, v, - useDefaults = obj.defaults === true, - ps = [ - 'precision', 1, MAX_DIGITS, - 'rounding', 0, 8, - 'toExpNeg', -EXP_LIMIT, 0, - 'toExpPos', 0, EXP_LIMIT, - 'maxE', 0, EXP_LIMIT, - 'minE', -EXP_LIMIT, 0, - 'modulo', 0, 9 - ]; - - for (i = 0; i < ps.length; i += 3) { - if (p = ps[i], useDefaults) { - this[p] = DEFAULTS[p]; - } - if ((v = obj[p]) !== void 0) { - if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) { - this[p] = v; - } else { - throw Error(invalidArgument + p + ': ' + v); - } - } - } - - if (p = 'crypto', useDefaults) { - this[p] = DEFAULTS[p]; - } - if ((v = obj[p]) !== void 0) { - if (v === true || v === false || v === 0 || v === 1) { - if (v) { - if (typeof crypto != 'undefined' && crypto && - (crypto.getRandomValues || crypto.randomBytes)) { - this[p] = true; - } else { - throw Error(cryptoUnavailable); - } - } else { - this[p] = false; - } - } else { - throw Error(invalidArgument + p + ': ' + v); - } - } - - return this; - } - - /* - * Return a new Decimal whose value is the cosine of `x`, rounded to `precision` significant - * digits using rounding mode `rounding`. - * - * x {number|string|Decimal} A value in radians. - * - */ - function cos(x) { - return new this(x).cos(); - } - - /* - * Return a new Decimal whose value is the hyperbolic cosine of `x`, rounded to precision - * significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} A value in radians. - * - */ - function cosh(x) { - return new this(x).cosh(); - } - - /* - * Create and return a Decimal constructor with the same configuration properties as this Decimal - * constructor. - * - */ - function clone(obj) { - var i, p, ps; - - /* - * The Decimal constructor and exported function. - * Return a new Decimal instance. - * - * v {number|string|Decimal} A numeric value. - * - */ - function Decimal(v) { - var e, i, t, - x = this; - - // Decimal called without new. - if (!(x instanceof Decimal)) { - return new Decimal(v); - } - - // Retain a reference to this Decimal constructor, and shadow Decimal.prototype.constructor - // which points to Object. - x.constructor = Decimal; - - // Duplicate. - if (v instanceof Decimal) { - x.s = v.s; - - if (external) { - if (!v.d || v.e > Decimal.maxE) { - - // Infinity. - x.e = NaN; - x.d = null; - } else if (v.e < Decimal.minE) { - - // Zero. - x.e = 0; - x.d = [0]; - } else { - x.e = v.e; - x.d = v.d.slice(); - } - } else { - x.e = v.e; - x.d = v.d ? v.d.slice() : v.d; - } - - return; - } - - t = typeof v; - - if (t === 'number') { - if (v === 0) { - x.s = 1 / v < 0 ? -1 : 1; - x.e = 0; - x.d = [0]; - return; - } - - if (v < 0) { - v = -v; - x.s = -1; - } else { - x.s = 1; - } - - // Fast path for small integers. - if (v === ~~v && v < 1e7) { - for (e = 0, i = v; i >= 10; i /= 10) { - e++; - } - - if (external) { - if (e > Decimal.maxE) { - x.e = NaN; - x.d = null; - } else if (e < Decimal.minE) { - x.e = 0; - x.d = [0]; - } else { - x.e = e; - x.d = [v]; - } - } else { - x.e = e; - x.d = [v]; - } - - return; - - // Infinity, NaN. - } else if (v * 0 !== 0) { - if (!v) { - x.s = NaN; - } - x.e = NaN; - x.d = null; - return; - } - - return parseDecimal(x, v.toString()); - - } else if (t !== 'string') { - throw Error(invalidArgument + v); - } - - // Minus sign? - if ((i = v.charCodeAt(0)) === 45) { - v = v.slice(1); - x.s = -1; - } else { - // Plus sign? - if (i === 43) { - v = v.slice(1); - } - x.s = 1; - } - - return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v); - } - - Decimal.prototype = P; - - Decimal.ROUND_UP = 0; - Decimal.ROUND_DOWN = 1; - Decimal.ROUND_CEIL = 2; - Decimal.ROUND_FLOOR = 3; - Decimal.ROUND_HALF_UP = 4; - Decimal.ROUND_HALF_DOWN = 5; - Decimal.ROUND_HALF_EVEN = 6; - Decimal.ROUND_HALF_CEIL = 7; - Decimal.ROUND_HALF_FLOOR = 8; - Decimal.EUCLID = 9; - - Decimal.config = Decimal.set = config; - Decimal.clone = clone; - Decimal.isDecimal = isDecimalInstance; - - Decimal.abs = abs; - Decimal.acos = acos; - Decimal.acosh = acosh; // ES6 - Decimal.add = add; - Decimal.asin = asin; - Decimal.asinh = asinh; // ES6 - Decimal.atan = atan; - Decimal.atanh = atanh; // ES6 - Decimal.atan2 = atan2; - Decimal.cbrt = cbrt; // ES6 - Decimal.ceil = ceil; - Decimal.cos = cos; - Decimal.cosh = cosh; // ES6 - Decimal.div = div; - Decimal.exp = exp; - Decimal.floor = floor; - Decimal.hypot = hypot; // ES6 - Decimal.ln = ln; - Decimal.log = log; - Decimal.log10 = log10; // ES6 - Decimal.log2 = log2; // ES6 - Decimal.max = max; - Decimal.min = min; - Decimal.mod = mod; - Decimal.mul = mul; - Decimal.pow = pow; - Decimal.random = random; - Decimal.round = round; - Decimal.sign = sign; // ES6 - Decimal.sin = sin; - Decimal.sinh = sinh; // ES6 - Decimal.sqrt = sqrt; - Decimal.sub = sub; - Decimal.tan = tan; - Decimal.tanh = tanh; // ES6 - Decimal.trunc = trunc; // ES6 - - if (obj === void 0) { - obj = {}; - } - if (obj) { - if (obj.defaults !== true) { - ps = ['precision', 'rounding', 'toExpNeg', 'toExpPos', 'maxE', 'minE', 'modulo', 'crypto']; - for (i = 0; i < ps.length;) { - if (!obj.hasOwnProperty(p = ps[i++])) { - obj[p] = this[p]; - } - } - } - } - - Decimal.config(obj); - - return Decimal; - } - - /* - * Return a new Decimal whose value is `x` divided by `y`, rounded to `precision` significant - * digits using rounding mode `rounding`. - * - * x {number|string|Decimal} - * y {number|string|Decimal} - * - */ - function div(x, y) { - return new this(x).div(y); - } - - /* - * Return a new Decimal whose value is the natural exponential of `x`, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} The power to which to raise the base of the natural log. - * - */ - function exp(x) { - return new this(x).exp(); - } - - /* - * Return a new Decimal whose value is `x` round to an integer using `ROUND_FLOOR`. - * - * x {number|string|Decimal} - * - */ - function floor(x) { - return finalise(x = new this(x), x.e + 1, 3); - } - - /* - * Return a new Decimal whose value is the square root of the sum of the squares of the arguments, - * rounded to `precision` significant digits using rounding mode `rounding`. - * - * hypot(a, b, ...) = sqrt(a^2 + b^2 + ...) - * - * arguments {number|string|Decimal} - * - */ - function hypot() { - var i, n, - t = new this(0); - - external = false; - - for (i = 0; i < arguments.length;) { - n = new this(arguments[i++]); - if (!n.d) { - if (n.s) { - external = true; - return new this(1 / 0); - } - t = n; - } else if (t.d) { - t = t.plus(n.times(n)); - } - } - - external = true; - - return t.sqrt(); - } - - /* - * Return true if object is a Decimal instance (where Decimal is any Decimal constructor), - * otherwise return false. - * - */ - function isDecimalInstance(obj) { - return obj instanceof Decimal || obj && obj.name === '[object Decimal]' || false; - } - - /* - * Return a new Decimal whose value is the natural logarithm of `x`, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} - * - */ - function ln(x) { - return new this(x).ln(); - } - - /* - * Return a new Decimal whose value is the log of `x` to the base `y`, or to base 10 if no base - * is specified, rounded to `precision` significant digits using rounding mode `rounding`. - * - * log[y](x) - * - * x {number|string|Decimal} The argument of the logarithm. - * y {number|string|Decimal} The base of the logarithm. - * - */ - function log(x, y) { - return new this(x).log(y); - } - - /* - * Return a new Decimal whose value is the base 2 logarithm of `x`, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} - * - */ - function log2(x) { - return new this(x).log(2); - } - - /* - * Return a new Decimal whose value is the base 10 logarithm of `x`, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} - * - */ - function log10(x) { - return new this(x).log(10); - } - - /* - * Return a new Decimal whose value is the maximum of the arguments. - * - * arguments {number|string|Decimal} - * - */ - function max() { - return maxOrMin(this, arguments, 'lt'); - } - - /* - * Return a new Decimal whose value is the minimum of the arguments. - * - * arguments {number|string|Decimal} - * - */ - function min() { - return maxOrMin(this, arguments, 'gt'); - } - - /* - * Return a new Decimal whose value is `x` modulo `y`, rounded to `precision` significant digits - * using rounding mode `rounding`. - * - * x {number|string|Decimal} - * y {number|string|Decimal} - * - */ - function mod(x, y) { - return new this(x).mod(y); - } - - /* - * Return a new Decimal whose value is `x` multiplied by `y`, rounded to `precision` significant - * digits using rounding mode `rounding`. - * - * x {number|string|Decimal} - * y {number|string|Decimal} - * - */ - function mul(x, y) { - return new this(x).mul(y); - } - - /* - * Return a new Decimal whose value is `x` raised to the power `y`, rounded to precision - * significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} The base. - * y {number|string|Decimal} The exponent. - * - */ - function pow(x, y) { - return new this(x).pow(y); - } - - /* - * Returns a new Decimal with a random value equal to or greater than 0 and less than 1, and with - * `sd`, or `Decimal.precision` if `sd` is omitted, significant digits (or less if trailing zeros - * are produced). - * - * [sd] {number} Significant digits. Integer, 0 to MAX_DIGITS inclusive. - * - */ - function random(sd) { - var d, e, k, n, - i = 0, - r = new this(1), - rd = []; - - if (sd === void 0) { - sd = this.precision; - } else { - checkInt32(sd, 1, MAX_DIGITS); - } - - k = Math.ceil(sd / LOG_BASE); - - if (!this.crypto) { - for (; i < k;) { - rd[i++] = Math.random() * 1e7 | 0; - } - - // Browsers supporting crypto.getRandomValues. - } else if (crypto.getRandomValues) { - d = crypto.getRandomValues(new Uint32Array(k)); - - for (; i < k;) { - n = d[i]; - - // 0 <= n < 4294967296 - // Probability n >= 4.29e9, is 4967296 / 4294967296 = 0.00116 (1 in 865). - if (n >= 4.29e9) { - d[i] = crypto.getRandomValues(new Uint32Array(1))[0]; - } else { - - // 0 <= n <= 4289999999 - // 0 <= (n % 1e7) <= 9999999 - rd[i++] = n % 1e7; - } - } - - // Node.js supporting crypto.randomBytes. - } else if (crypto.randomBytes) { - - // buffer - d = crypto.randomBytes(k *= 4); - - for (; i < k;) { - - // 0 <= n < 2147483648 - n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 0x7f) << 24); - - // Probability n >= 2.14e9, is 7483648 / 2147483648 = 0.0035 (1 in 286). - if (n >= 2.14e9) { - crypto.randomBytes(4).copy(d, i); - } else { - - // 0 <= n <= 2139999999 - // 0 <= (n % 1e7) <= 9999999 - rd.push(n % 1e7); - i += 4; - } - } - - i = k / 4; - } else { - throw Error(cryptoUnavailable); - } - - k = rd[--i]; - sd %= LOG_BASE; - - // Convert trailing digits to zeros according to sd. - if (k && sd) { - n = mathpow(10, LOG_BASE - sd); - rd[i] = (k / n | 0) * n; - } - - // Remove trailing words which are zero. - for (; rd[i] === 0; i--) { - rd.pop(); - } - - // Zero? - if (i < 0) { - e = 0; - rd = [0]; - } else { - e = -1; - - // Remove leading words which are zero and adjust exponent accordingly. - for (; rd[0] === 0; e -= LOG_BASE) { - rd.shift(); - } - - // Count the digits of the first word of rd to determine leading zeros. - for (k = 1, n = rd[0]; n >= 10; n /= 10) { - k++; - } - - // Adjust the exponent for leading zeros of the first word of rd. - if (k < LOG_BASE) { - e -= LOG_BASE - k; - } - } - - r.e = e; - r.d = rd; - - return r; - } - - /* - * Return a new Decimal whose value is `x` rounded to an integer using rounding mode `rounding`. - * - * To emulate `Math.round`, set rounding to 7 (ROUND_HALF_CEIL). - * - * x {number|string|Decimal} - * - */ - function round(x) { - return finalise(x = new this(x), x.e + 1, this.rounding); - } - - /* - * Return - * 1 if x > 0, - * -1 if x < 0, - * 0 if x is 0, - * -0 if x is -0, - * NaN otherwise - * - * x {number|string|Decimal} - * - */ - function sign(x) { - x = new this(x); - return x.d ? (x.d[0] ? x.s : 0 * x.s) : x.s || NaN; - } - - /* - * Return a new Decimal whose value is the sine of `x`, rounded to `precision` significant digits - * using rounding mode `rounding`. - * - * x {number|string|Decimal} A value in radians. - * - */ - function sin(x) { - return new this(x).sin(); - } - - /* - * Return a new Decimal whose value is the hyperbolic sine of `x`, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} A value in radians. - * - */ - function sinh(x) { - return new this(x).sinh(); - } - - /* - * Return a new Decimal whose value is the square root of `x`, rounded to `precision` significant - * digits using rounding mode `rounding`. - * - * x {number|string|Decimal} - * - */ - function sqrt(x) { - return new this(x).sqrt(); - } - - /* - * Return a new Decimal whose value is `x` minus `y`, rounded to `precision` significant digits - * using rounding mode `rounding`. - * - * x {number|string|Decimal} - * y {number|string|Decimal} - * - */ - function sub(x, y) { - return new this(x).sub(y); - } - - /* - * Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant - * digits using rounding mode `rounding`. - * - * x {number|string|Decimal} A value in radians. - * - */ - function tan(x) { - return new this(x).tan(); - } - - /* - * Return a new Decimal whose value is the hyperbolic tangent of `x`, rounded to `precision` - * significant digits using rounding mode `rounding`. - * - * x {number|string|Decimal} A value in radians. - * - */ - function tanh(x) { - return new this(x).tanh(); - } - - /* - * Return a new Decimal whose value is `x` truncated to an integer. - * - * x {number|string|Decimal} - * - */ - function trunc(x) { - return finalise(x = new this(x), x.e + 1, 1); - } - - // Create and configure initial Decimal constructor. - Decimal = clone(DEFAULTS); - - Decimal['default'] = Decimal.Decimal = Decimal; - - // Create the internal constants from their string values. - LN10 = new Decimal(LN10); - PI = new Decimal(PI); - - // Export. - - // AMD. - if (true) { - !(__WEBPACK_AMD_DEFINE_RESULT__ = (function () { - return Decimal; - }).call(exports, __webpack_require__, exports, module), - __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); - - // Node and other environments that support module.exports. - } else {} - })(this); - - /***/ }), - /* 13 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function () { - return ArgumentsError; - }); - /** - * Create a syntax error with the message: - * 'Wrong number of arguments in function ( provided, - expected)' - * @param {string} fn Function name - * @param {number} count Actual argument count - * @param {number} min Minimum required argument count - * @param {number} [max] Maximum required argument count - * @extends Error - */ - function ArgumentsError(fn, count, min, max) { - if (!(this instanceof ArgumentsError)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this.fn = fn; - this.count = count; - this.min = min; - this.max = max; - this.message = 'Wrong number of arguments in function ' + fn + ' (' + count + ' provided, ' + min + (max !== undefined && max !== null ? '-' + max : '') + ' expected)'; - this.stack = new Error().stack; - } - ArgumentsError.prototype = new Error(); - ArgumentsError.prototype.constructor = Error; - ArgumentsError.prototype.name = 'ArgumentsError'; - ArgumentsError.prototype.isArgumentsError = true; - - /***/ }), - /* 14 */ - /***/ (function (module, exports, __webpack_require__) { - - "use strict"; - var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/** - * typed-function - * - * Type checking for JavaScript functions - * - * https://github.com/josdejong/typed-function - */ - - (function (root, factory) { - if (true) { - // AMD. Register as an anonymous module. - !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory), - __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ? - (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__), - __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); - } else {} - }(this, function () { - - function ok() { - return true; - } - - function notOk() { - return false; - } - - function undef() { - return undefined; - } - - /** - * @typedef {{ - * params: Param[], - * fn: function - * }} Signature - * - * @typedef {{ - * types: Type[], - * restParam: boolean - * }} Param - * - * @typedef {{ - * name: string, - * typeIndex: number, - * test: function, - * conversion?: ConversionDef, - * conversionIndex: number, - * }} Type - * - * @typedef {{ - * from: string, - * to: string, - * convert: function (*) : * - * }} ConversionDef - * - * @typedef {{ - * name: string, - * test: function(*) : boolean - * }} TypeDef - */ - - // create a new instance of typed-function - function create() { - // data type tests - var _types = [ - { name: 'number', test: function (x) { - return typeof x === 'number'; - } }, - { name: 'string', test: function (x) { - return typeof x === 'string'; - } }, - { name: 'boolean', test: function (x) { - return typeof x === 'boolean'; - } }, - { name: 'Function', test: function (x) { - return typeof x === 'function'; - } }, - { name: 'Array', test: Array.isArray }, - { name: 'Date', test: function (x) { - return x instanceof Date; - } }, - { name: 'RegExp', test: function (x) { - return x instanceof RegExp; - } }, - { name: 'Object', test: function (x) { - return typeof x === 'object' && x !== null && x.constructor === Object; - }}, - { name: 'null', test: function (x) { - return x === null; - } }, - { name: 'undefined', test: function (x) { - return x === undefined; - } } - ]; - - var anyType = { - name: 'any', - test: ok - }; - - // types which need to be ignored - var _ignore = []; - - // type conversions - var _conversions = []; - - // This is a temporary object, will be replaced with a typed function at the end - var typed = { - types: _types, - conversions: _conversions, - ignore: _ignore - }; - - /** - * Find the test function for a type - * @param {String} typeName - * @return {TypeDef} Returns the type definition when found, - * Throws a TypeError otherwise - */ - function findTypeByName(typeName) { - var entry = findInArray(typed.types, function (entry) { - return entry.name === typeName; - }); - - if (entry) { - return entry; - } - - if (typeName === 'any') { // special baked-in case 'any' - return anyType; - } - - var hint = findInArray(typed.types, function (entry) { - return entry.name.toLowerCase() === typeName.toLowerCase(); - }); - - throw new TypeError('Unknown type "' + typeName + '"' + - (hint ? ('. Did you mean "' + hint.name + '"?') : '')); - } - - /** - * Find the index of a type definition. Handles special case 'any' - * @param {TypeDef} type - * @return {number} - */ - function findTypeIndex(type) { - if (type === anyType) { - return 999; - } - - return typed.types.indexOf(type); - } - - /** - * Find a type that matches a value. - * @param {*} value - * @return {string} Returns the name of the first type for which - * the type test matches the value. - */ - function findTypeName(value) { - var entry = findInArray(typed.types, function (entry) { - return entry.test(value); - }); - - if (entry) { - return entry.name; - } - - throw new TypeError('Value has unknown type. Value: ' + value); - } - - /** - * Find a specific signature from a (composed) typed function, for example: - * - * typed.find(fn, ['number', 'string']) - * typed.find(fn, 'number, string') - * - * Function find only only works for exact matches. - * - * @param {Function} fn A typed-function - * @param {string | string[]} signature Signature to be found, can be - * an array or a comma separated string. - * @return {Function} Returns the matching signature, or - * throws an error when no signature - * is found. - */ - function find(fn, signature) { - if (!fn.signatures) { - throw new TypeError('Function is no typed-function'); - } - - // normalize input - var arr; - if (typeof signature === 'string') { - arr = signature.split(','); - for (var i = 0; i < arr.length; i++) { - arr[i] = arr[i].trim(); - } - } else if (Array.isArray(signature)) { - arr = signature; - } else { - throw new TypeError('String array or a comma separated string expected'); - } - - var str = arr.join(','); - - // find an exact match - var match = fn.signatures[str]; - if (match) { - return match; - } - - // TODO: extend find to match non-exact signatures - - throw new TypeError('Signature not found (signature: ' + (fn.name || 'unnamed') + '(' + arr.join(', ') + '))'); - } - - /** - * Convert a given value to another data type. - * @param {*} value - * @param {string} type - */ - function convert(value, type) { - var from = findTypeName(value); - - // check conversion is needed - if (type === from) { - return value; - } - - for (var i = 0; i < typed.conversions.length; i++) { - var conversion = typed.conversions[i]; - if (conversion.from === from && conversion.to === type) { - return conversion.convert(value); - } - } - - throw new Error('Cannot convert from ' + from + ' to ' + type); - } - - /** - * Stringify parameters in a normalized way - * @param {Param[]} params - * @return {string} - */ - function stringifyParams(params) { - return params - .map(function (param) { - var typeNames = param.types.map(getTypeName); - - return (param.restParam ? '...' : '') + typeNames.join('|'); - }) - .join(','); - } - - /** - * Parse a parameter, like "...number | boolean" - * @param {string} param - * @param {ConversionDef[]} conversions - * @return {Param} param - */ - function parseParam(param, conversions) { - var restParam = param.indexOf('...') === 0; - var types = (!restParam) - ? param - : (param.length > 3) - ? param.slice(3) - : 'any'; - - var typeNames = types.split('|').map(trim) - .filter(notEmpty) - .filter(notIgnore); - - var matchingConversions = filterConversions(conversions, typeNames); - - var exactTypes = typeNames.map(function (typeName) { - var type = findTypeByName(typeName); - - return { - name: typeName, - typeIndex: findTypeIndex(type), - test: type.test, - conversion: null, - conversionIndex: -1 - }; - }); - - var convertibleTypes = matchingConversions.map(function (conversion) { - var type = findTypeByName(conversion.from); - - return { - name: conversion.from, - typeIndex: findTypeIndex(type), - test: type.test, - conversion: conversion, - conversionIndex: conversions.indexOf(conversion) - }; - }); - - return { - types: exactTypes.concat(convertibleTypes), - restParam: restParam - }; - } - - /** - * Parse a signature with comma separated parameters, - * like "number | boolean, ...string" - * @param {string} signature - * @param {function} fn - * @param {ConversionDef[]} conversions - * @return {Signature | null} signature - */ - function parseSignature(signature, fn, conversions) { - var params = []; - - if (signature.trim() !== '') { - params = signature - .split(',') - .map(trim) - .map(function (param, index, array) { - var parsedParam = parseParam(param, conversions); - - if (parsedParam.restParam && (index !== array.length - 1)) { - throw new SyntaxError('Unexpected rest parameter "' + param + '": ' + - 'only allowed for the last parameter'); - } - - return parsedParam; - }); - } - - if (params.some(isInvalidParam)) { - // invalid signature: at least one parameter has no types - // (they may have been filtered) - return null; - } - - return { - params: params, - fn: fn - }; - } - - /** - * Test whether a set of params contains a restParam - * @param {Param[]} params - * @return {boolean} Returns true when the last parameter is a restParam - */ - function hasRestParam(params) { - var param = last(params); - return param ? param.restParam : false; - } - - /** - * Test whether a parameter contains conversions - * @param {Param} param - * @return {boolean} Returns true when at least one of the parameters - * contains a conversion. - */ - function hasConversions(param) { - return param.types.some(function (type) { - return type.conversion != null; - }); - } - - /** - * Create a type test for a single parameter, which can have one or multiple - * types. - * @param {Param} param - * @return {function(x: *) : boolean} Returns a test function - */ - function compileTest(param) { - if (!param || param.types.length === 0) { - // nothing to do - return ok; - } else if (param.types.length === 1) { - return findTypeByName(param.types[0].name).test; - } else if (param.types.length === 2) { - var test0 = findTypeByName(param.types[0].name).test; - var test1 = findTypeByName(param.types[1].name).test; - return function or(x) { - return test0(x) || test1(x); - }; - } else { // param.types.length > 2 - var tests = param.types.map(function (type) { - return findTypeByName(type.name).test; - }); - return function or(x) { - for (var i = 0; i < tests.length; i++) { - if (tests[i](x)) { - return true; - } - } - return false; - }; - } - } - - /** - * Create a test for all parameters of a signature - * @param {Param[]} params - * @return {function(args: Array<*>) : boolean} - */ - function compileTests(params) { - var tests, test0, test1; - - if (hasRestParam(params)) { - // variable arguments like '...number' - tests = initial(params).map(compileTest); - var varIndex = tests.length; - var lastTest = compileTest(last(params)); - var testRestParam = function (args) { - for (var i = varIndex; i < args.length; i++) { - if (!lastTest(args[i])) { - return false; - } - } - return true; - }; - - return function testArgs(args) { - for (var i = 0; i < tests.length; i++) { - if (!tests[i](args[i])) { - return false; - } - } - return testRestParam(args) && (args.length >= varIndex + 1); - }; - } else { - // no variable arguments - if (params.length === 0) { - return function testArgs(args) { - return args.length === 0; - }; - } else if (params.length === 1) { - test0 = compileTest(params[0]); - return function testArgs(args) { - return test0(args[0]) && args.length === 1; - }; - } else if (params.length === 2) { - test0 = compileTest(params[0]); - test1 = compileTest(params[1]); - return function testArgs(args) { - return test0(args[0]) && test1(args[1]) && args.length === 2; - }; - } else { // arguments.length > 2 - tests = params.map(compileTest); - return function testArgs(args) { - for (var i = 0; i < tests.length; i++) { - if (!tests[i](args[i])) { - return false; - } - } - return args.length === tests.length; - }; - } - } - } - - /** - * Find the parameter at a specific index of a signature. - * Handles rest parameters. - * @param {Signature} signature - * @param {number} index - * @return {Param | null} Returns the matching parameter when found, - * null otherwise. - */ - function getParamAtIndex(signature, index) { - return index < signature.params.length - ? signature.params[index] - : hasRestParam(signature.params) - ? last(signature.params) - : null; - } - - /** - * Get all type names of a parameter - * @param {Signature} signature - * @param {number} index - * @param {boolean} excludeConversions - * @return {string[]} Returns an array with type names - */ - function getExpectedTypeNames(signature, index, excludeConversions) { - var param = getParamAtIndex(signature, index); - var types = param - ? excludeConversions - ? param.types.filter(isExactType) - : param.types - : []; - - return types.map(getTypeName); - } - - /** - * Returns the name of a type - * @param {Type} type - * @return {string} Returns the type name - */ - function getTypeName(type) { - return type.name; - } - - /** - * Test whether a type is an exact type or conversion - * @param {Type} type - * @return {boolean} Returns true when - */ - function isExactType(type) { - return type.conversion === null || type.conversion === undefined; - } - - /** - * Helper function for creating error messages: create an array with - * all available types on a specific argument index. - * @param {Signature[]} signatures - * @param {number} index - * @return {string[]} Returns an array with available types - */ - function mergeExpectedParams(signatures, index) { - var typeNames = uniq(flatMap(signatures, function (signature) { - return getExpectedTypeNames(signature, index, false); - })); - - return (typeNames.indexOf('any') !== -1) ? ['any'] : typeNames; - } - - /** - * Create - * @param {string} name The name of the function - * @param {array.<*>} args The actual arguments passed to the function - * @param {Signature[]} signatures A list with available signatures - * @return {TypeError} Returns a type error with additional data - * attached to it in the property `data` - */ - function createError(name, args, signatures) { - var err, expected; - var _name = name || 'unnamed'; - - // test for wrong type at some index - var matchingSignatures = signatures; - var index; - for (index = 0; index < args.length; index++) { - var nextMatchingDefs = matchingSignatures.filter(function (signature) { - var test = compileTest(getParamAtIndex(signature, index)); - return (index < signature.params.length || hasRestParam(signature.params)) && - test(args[index]); - }); - - if (nextMatchingDefs.length === 0) { - // no matching signatures anymore, throw error "wrong type" - expected = mergeExpectedParams(matchingSignatures, index); - if (expected.length > 0) { - var actualType = findTypeName(args[index]); - - err = new TypeError('Unexpected type of argument in function ' + _name + - ' (expected: ' + expected.join(' or ') + - ', actual: ' + actualType + ', index: ' + index + ')'); - err.data = { - category: 'wrongType', - fn: _name, - index: index, - actual: actualType, - expected: expected - }; - return err; - } - } else { - matchingSignatures = nextMatchingDefs; - } - } - - // test for too few arguments - var lengths = matchingSignatures.map(function (signature) { - return hasRestParam(signature.params) ? Infinity : signature.params.length; - }); - if (args.length < Math.min.apply(null, lengths)) { - expected = mergeExpectedParams(matchingSignatures, index); - err = new TypeError('Too few arguments in function ' + _name + - ' (expected: ' + expected.join(' or ') + - ', index: ' + args.length + ')'); - err.data = { - category: 'tooFewArgs', - fn: _name, - index: args.length, - expected: expected - }; - return err; - } - - // test for too many arguments - var maxLength = Math.max.apply(null, lengths); - if (args.length > maxLength) { - err = new TypeError('Too many arguments in function ' + _name + - ' (expected: ' + maxLength + ', actual: ' + args.length + ')'); - err.data = { - category: 'tooManyArgs', - fn: _name, - index: args.length, - expectedLength: maxLength - }; - return err; - } - - err = new TypeError('Arguments of type "' + args.join(', ') + - '" do not match any of the defined signatures of function ' + _name + '.'); - err.data = { - category: 'mismatch', - actual: args.map(findTypeName) - }; - return err; - } - - /** - * Find the lowest index of all exact types of a parameter (no conversions) - * @param {Param} param - * @return {number} Returns the index of the lowest type in typed.types - */ - function getLowestTypeIndex(param) { - var min = 999; - - for (var i = 0; i < param.types.length; i++) { - if (isExactType(param.types[i])) { - min = Math.min(min, param.types[i].typeIndex); - } - } - - return min; - } - - /** - * Find the lowest index of the conversion of all types of the parameter - * having a conversion - * @param {Param} param - * @return {number} Returns the lowest index of the conversions of this type - */ - function getLowestConversionIndex(param) { - var min = 999; - - for (var i = 0; i < param.types.length; i++) { - if (!isExactType(param.types[i])) { - min = Math.min(min, param.types[i].conversionIndex); - } - } - - return min; - } - - /** - * Compare two params - * @param {Param} param1 - * @param {Param} param2 - * @return {number} returns a negative number when param1 must get a lower - * index than param2, a positive number when the opposite, - * or zero when both are equal - */ - function compareParams(param1, param2) { - var c; - - // compare having a rest parameter or not - c = param1.restParam - param2.restParam; - if (c !== 0) { - return c; - } - - // compare having conversions or not - c = hasConversions(param1) - hasConversions(param2); - if (c !== 0) { - return c; - } - - // compare the index of the types - c = getLowestTypeIndex(param1) - getLowestTypeIndex(param2); - if (c !== 0) { - return c; - } - - // compare the index of any conversion - return getLowestConversionIndex(param1) - getLowestConversionIndex(param2); - } - - /** - * Compare two signatures - * @param {Signature} signature1 - * @param {Signature} signature2 - * @return {number} returns a negative number when param1 must get a lower - * index than param2, a positive number when the opposite, - * or zero when both are equal - */ - function compareSignatures(signature1, signature2) { - var len = Math.min(signature1.params.length, signature2.params.length); - var i; - var c; - - // compare whether the params have conversions at all or not - c = signature1.params.some(hasConversions) - signature2.params.some(hasConversions); - if (c !== 0) { - return c; - } - - // next compare whether the params have conversions one by one - for (i = 0; i < len; i++) { - c = hasConversions(signature1.params[i]) - hasConversions(signature2.params[i]); - if (c !== 0) { - return c; - } - } - - // compare the types of the params one by one - for (i = 0; i < len; i++) { - c = compareParams(signature1.params[i], signature2.params[i]); - if (c !== 0) { - return c; - } - } - - // compare the number of params - return signature1.params.length - signature2.params.length; - } - - /** - * Get params containing all types that can be converted to the defined types. - * - * @param {ConversionDef[]} conversions - * @param {string[]} typeNames - * @return {ConversionDef[]} Returns the conversions that are available - * for every type (if any) - */ - function filterConversions(conversions, typeNames) { - var matches = {}; - - conversions.forEach(function (conversion) { - if (typeNames.indexOf(conversion.from) === -1 && - typeNames.indexOf(conversion.to) !== -1 && - !matches[conversion.from]) { - matches[conversion.from] = conversion; - } - }); - - return Object.keys(matches).map(function (from) { - return matches[from]; - }); - } - - /** - * Preprocess arguments before calling the original function: - * - if needed convert the parameters - * - in case of rest parameters, move the rest parameters into an Array - * @param {Param[]} params - * @param {function} fn - * @return {function} Returns a wrapped function - */ - function compileArgsPreprocessing(params, fn) { - var fnConvert = fn; - - // TODO: can we make this wrapper function smarter/simpler? - - if (params.some(hasConversions)) { - var restParam = hasRestParam(params); - var compiledConversions = params.map(compileArgConversion); - - fnConvert = function convertArgs() { - var args = []; - var last = restParam ? arguments.length - 1 : arguments.length; - for (var i = 0; i < last; i++) { - args[i] = compiledConversions[i](arguments[i]); - } - if (restParam) { - args[last] = arguments[last].map(compiledConversions[last]); - } - - return fn.apply(this, args); - }; - } - - var fnPreprocess = fnConvert; - if (hasRestParam(params)) { - var offset = params.length - 1; - - fnPreprocess = function preprocessRestParams() { - return fnConvert.apply(this, - slice(arguments, 0, offset).concat([slice(arguments, offset)])); - }; - } - - return fnPreprocess; - } - - /** - * Compile conversion for a parameter to the right type - * @param {Param} param - * @return {function} Returns the wrapped function that will convert arguments - * - */ - function compileArgConversion(param) { - var test0, test1, conversion0, conversion1; - var tests = []; - var conversions = []; - - param.types.forEach(function (type) { - if (type.conversion) { - tests.push(findTypeByName(type.conversion.from).test); - conversions.push(type.conversion.convert); - } - }); - - // create optimized conversion functions depending on the number of conversions - switch (conversions.length) { - case 0: - return function convertArg(arg) { - return arg; - }; - - case 1: - test0 = tests[0]; - conversion0 = conversions[0]; - return function convertArg(arg) { - if (test0(arg)) { - return conversion0(arg); - } - return arg; - }; - - case 2: - test0 = tests[0]; - test1 = tests[1]; - conversion0 = conversions[0]; - conversion1 = conversions[1]; - return function convertArg(arg) { - if (test0(arg)) { - return conversion0(arg); - } - if (test1(arg)) { - return conversion1(arg); - } - return arg; - }; - - default: - return function convertArg(arg) { - for (var i = 0; i < conversions.length; i++) { - if (tests[i](arg)) { - return conversions[i](arg); - } - } - return arg; - }; - } - } - - /** - * Convert an array with signatures into a map with signatures, - * where signatures with union types are split into separate signatures - * - * Throws an error when there are conflicting types - * - * @param {Signature[]} signatures - * @return {Object.} Returns a map with signatures - * as key and the original function - * of this signature as value. - */ - function createSignaturesMap(signatures) { - var signaturesMap = {}; - signatures.forEach(function (signature) { - if (!signature.params.some(hasConversions)) { - splitParams(signature.params, true).forEach(function (params) { - signaturesMap[stringifyParams(params)] = signature.fn; - }); - } - }); - - return signaturesMap; - } - - /** - * Split params with union types in to separate params. - * - * For example: - * - * splitParams([['Array', 'Object'], ['string', 'RegExp']) - * // returns: - * // [ - * // ['Array', 'string'], - * // ['Array', 'RegExp'], - * // ['Object', 'string'], - * // ['Object', 'RegExp'] - * // ] - * - * @param {Param[]} params - * @param {boolean} ignoreConversionTypes - * @return {Param[]} - */ - function splitParams(params, ignoreConversionTypes) { - function _splitParams(params, index, types) { - if (index < params.length) { - var param = params[index]; - var filteredTypes = ignoreConversionTypes - ? param.types.filter(isExactType) - : param.types; - var typeGroups; - - if (param.restParam) { - // split the types of a rest parameter in two: - // one with only exact types, and one with exact types and conversions - var exactTypes = filteredTypes.filter(isExactType); - typeGroups = exactTypes.length < filteredTypes.length - ? [exactTypes, filteredTypes] - : [filteredTypes]; - - } else { - // split all the types of a regular parameter into one type per group - typeGroups = filteredTypes.map(function (type) { - return [type]; - }); - } - - // recurse over the groups with types - return flatMap(typeGroups, function (typeGroup) { - return _splitParams(params, index + 1, types.concat([typeGroup])); - }); - - } else { - // we've reached the end of the parameters. Now build a new Param - var splittedParams = types.map(function (type, typeIndex) { - return { - types: type, - restParam: (typeIndex === params.length - 1) && hasRestParam(params) - }; - }); - - return [splittedParams]; - } - } - - return _splitParams(params, 0, []); - } - - /** - * Test whether two signatures have a conflicting signature - * @param {Signature} signature1 - * @param {Signature} signature2 - * @return {boolean} Returns true when the signatures conflict, false otherwise. - */ - function hasConflictingParams(signature1, signature2) { - var ii = Math.max(signature1.params.length, signature2.params.length); - - for (var i = 0; i < ii; i++) { - var typesNames1 = getExpectedTypeNames(signature1, i, true); - var typesNames2 = getExpectedTypeNames(signature2, i, true); - - if (!hasOverlap(typesNames1, typesNames2)) { - return false; - } - } - - var len1 = signature1.params.length; - var len2 = signature2.params.length; - var restParam1 = hasRestParam(signature1.params); - var restParam2 = hasRestParam(signature2.params); - - return restParam1 - ? restParam2 ? (len1 === len2) : (len2 >= len1) - : restParam2 ? (len1 >= len2) : (len1 === len2); - } - - /** - * Create a typed function - * @param {String} name The name for the typed function - * @param {Object.} signaturesMap - * An object with one or - * multiple signatures as key, and the - * function corresponding to the - * signature as value. - * @return {function} Returns the created typed function. - */ - function createTypedFunction(name, signaturesMap) { - if (Object.keys(signaturesMap).length === 0) { - throw new SyntaxError('No signatures provided'); - } - - // parse the signatures, and check for conflicts - var parsedSignatures = []; - Object.keys(signaturesMap) - .map(function (signature) { - return parseSignature(signature, signaturesMap[signature], typed.conversions); - }) - .filter(notNull) - .forEach(function (parsedSignature) { - // check whether this parameter conflicts with already parsed signatures - var conflictingSignature = findInArray(parsedSignatures, function (s) { - return hasConflictingParams(s, parsedSignature); - }); - if (conflictingSignature) { - throw new TypeError('Conflicting signatures "' + - stringifyParams(conflictingSignature.params) + '" and "' + - stringifyParams(parsedSignature.params) + '".'); - } - - parsedSignatures.push(parsedSignature); - }); - - // split and filter the types of the signatures, and then order them - var signatures = flatMap(parsedSignatures, function (parsedSignature) { - var params = parsedSignature ? splitParams(parsedSignature.params, false) : []; - - return params.map(function (params) { - return { - params: params, - fn: parsedSignature.fn - }; - }); - }).filter(notNull); - - signatures.sort(compareSignatures); - - // we create a highly optimized checks for the first couple of signatures with max 2 arguments - var ok0 = signatures[0] && signatures[0].params.length <= 2 && !hasRestParam(signatures[0].params); - var ok1 = signatures[1] && signatures[1].params.length <= 2 && !hasRestParam(signatures[1].params); - var ok2 = signatures[2] && signatures[2].params.length <= 2 && !hasRestParam(signatures[2].params); - var ok3 = signatures[3] && signatures[3].params.length <= 2 && !hasRestParam(signatures[3].params); - var ok4 = signatures[4] && signatures[4].params.length <= 2 && !hasRestParam(signatures[4].params); - var ok5 = signatures[5] && signatures[5].params.length <= 2 && !hasRestParam(signatures[5].params); - var allOk = ok0 && ok1 && ok2 && ok3 && ok4 && ok5; - - // compile the tests - var tests = signatures.map(function (signature) { - return compileTests(signature.params); - }); - - var test00 = ok0 ? compileTest(signatures[0].params[0]) : notOk; - var test10 = ok1 ? compileTest(signatures[1].params[0]) : notOk; - var test20 = ok2 ? compileTest(signatures[2].params[0]) : notOk; - var test30 = ok3 ? compileTest(signatures[3].params[0]) : notOk; - var test40 = ok4 ? compileTest(signatures[4].params[0]) : notOk; - var test50 = ok5 ? compileTest(signatures[5].params[0]) : notOk; - - var test01 = ok0 ? compileTest(signatures[0].params[1]) : notOk; - var test11 = ok1 ? compileTest(signatures[1].params[1]) : notOk; - var test21 = ok2 ? compileTest(signatures[2].params[1]) : notOk; - var test31 = ok3 ? compileTest(signatures[3].params[1]) : notOk; - var test41 = ok4 ? compileTest(signatures[4].params[1]) : notOk; - var test51 = ok5 ? compileTest(signatures[5].params[1]) : notOk; - - // compile the functions - var fns = signatures.map(function (signature) { - return compileArgsPreprocessing(signature.params, signature.fn); - }); - - var fn0 = ok0 ? fns[0] : undef; - var fn1 = ok1 ? fns[1] : undef; - var fn2 = ok2 ? fns[2] : undef; - var fn3 = ok3 ? fns[3] : undef; - var fn4 = ok4 ? fns[4] : undef; - var fn5 = ok5 ? fns[5] : undef; - - var len0 = ok0 ? signatures[0].params.length : -1; - var len1 = ok1 ? signatures[1].params.length : -1; - var len2 = ok2 ? signatures[2].params.length : -1; - var len3 = ok3 ? signatures[3].params.length : -1; - var len4 = ok4 ? signatures[4].params.length : -1; - var len5 = ok5 ? signatures[5].params.length : -1; - - // simple and generic, but also slow - var iStart = allOk ? 6 : 0; - var iEnd = signatures.length; - var generic = function generic() { - 'use strict'; - - for (var i = iStart; i < iEnd; i++) { - if (tests[i](arguments)) { - return fns[i].apply(this, arguments); - } - } - - throw createError(name, arguments, signatures); - }; - - // create the typed function - // fast, specialized version. Falls back to the slower, generic one if needed - var fn = function fn(arg0, arg1) { - 'use strict'; - - if (arguments.length === len0 && test00(arg0) && test01(arg1)) { - return fn0.apply(fn, arguments); - } - if (arguments.length === len1 && test10(arg0) && test11(arg1)) { - return fn1.apply(fn, arguments); - } - if (arguments.length === len2 && test20(arg0) && test21(arg1)) { - return fn2.apply(fn, arguments); - } - if (arguments.length === len3 && test30(arg0) && test31(arg1)) { - return fn3.apply(fn, arguments); - } - if (arguments.length === len4 && test40(arg0) && test41(arg1)) { - return fn4.apply(fn, arguments); - } - if (arguments.length === len5 && test50(arg0) && test51(arg1)) { - return fn5.apply(fn, arguments); - } - - return generic.apply(fn, arguments); - }; - - // attach name the typed function - try { - Object.defineProperty(fn, 'name', {value: name}); - } catch (err) { - // old browsers do not support Object.defineProperty and some don't support setting the name property - // the function name is not essential for the functioning, it's mostly useful for debugging, - // so it's fine to have unnamed functions. - } - - // attach signatures to the function - fn.signatures = createSignaturesMap(signatures); - - return fn; - } - - /** - * Test whether a type should be NOT be ignored - * @param {string} typeName - * @return {boolean} - */ - function notIgnore(typeName) { - return typed.ignore.indexOf(typeName) === -1; - } - - /** - * trim a string - * @param {string} str - * @return {string} - */ - function trim(str) { - return str.trim(); - } - - /** - * Test whether a string is not empty - * @param {string} str - * @return {boolean} - */ - function notEmpty(str) { - return !!str; - } - - /** - * test whether a value is not strict equal to null - * @param {*} value - * @return {boolean} - */ - function notNull(value) { - return value !== null; - } - - /** - * Test whether a parameter has no types defined - * @param {Param} param - * @return {boolean} - */ - function isInvalidParam(param) { - return param.types.length === 0; - } - - /** - * Return all but the last items of an array - * @param {Array} arr - * @return {Array} - */ - function initial(arr) { - return arr.slice(0, arr.length - 1); - } - - /** - * return the last item of an array - * @param {Array} arr - * @return {*} - */ - function last(arr) { - return arr[arr.length - 1]; - } - - /** - * Slice an array or function Arguments - * @param {Array | Arguments | IArguments} arr - * @param {number} start - * @param {number} [end] - * @return {Array} - */ - function slice(arr, start, end) { - return Array.prototype.slice.call(arr, start, end); - } - - /** - * Test whether an array contains some item - * @param {Array} array - * @param {*} item - * @return {boolean} Returns true if array contains item, false if not. - */ - function contains(array, item) { - return array.indexOf(item) !== -1; - } - - /** - * Test whether two arrays have overlapping items - * @param {Array} array1 - * @param {Array} array2 - * @return {boolean} Returns true when at least one item exists in both arrays - */ - function hasOverlap(array1, array2) { - for (var i = 0; i < array1.length; i++) { - if (contains(array2, array1[i])) { - return true; - } - } - - return false; - } - - /** - * Return the first item from an array for which test(arr[i]) returns true - * @param {Array} arr - * @param {function} test - * @return {* | undefined} Returns the first matching item - * or undefined when there is no match - */ - function findInArray(arr, test) { - for (var i = 0; i < arr.length; i++) { - if (test(arr[i])) { - return arr[i]; - } - } - return undefined; - } - - /** - * Filter unique items of an array with strings - * @param {string[]} arr - * @return {string[]} - */ - function uniq(arr) { - var entries = {}; - for (var i = 0; i < arr.length; i++) { - entries[arr[i]] = true; - } - return Object.keys(entries); - } - - /** - * Flat map the result invoking a callback for every item in an array. - * https://gist.github.com/samgiles/762ee337dff48623e729 - * @param {Array} arr - * @param {function} callback - * @return {Array} - */ - function flatMap(arr, callback) { - return Array.prototype.concat.apply([], arr.map(callback)); - } - - /** - * Retrieve the function name from a set of typed functions, - * and check whether the name of all functions match (if given) - * @param {function[]} fns - */ - function getName(fns) { - var name = ''; - - for (var i = 0; i < fns.length; i++) { - var fn = fns[i]; - - // check whether the names are the same when defined - if ((typeof fn.signatures === 'object' || typeof fn.signature === 'string') && fn.name !== '') { - if (name === '') { - name = fn.name; - } else if (name !== fn.name) { - var err = new Error('Function names do not match (expected: ' + name + ', actual: ' + fn.name + ')'); - err.data = { - actual: fn.name, - expected: name - }; - throw err; - } - } - } - - return name; - } - - // extract and merge all signatures of a list with typed functions - function extractSignatures(fns) { - var err; - var signaturesMap = {}; - - function validateUnique(_signature, _fn) { - if (signaturesMap.hasOwnProperty(_signature) && _fn !== signaturesMap[_signature]) { - err = new Error('Signature "' + _signature + '" is defined twice'); - err.data = {signature: _signature}; - throw err; - // else: both signatures point to the same function, that's fine - } - } - - for (var i = 0; i < fns.length; i++) { - var fn = fns[i]; - - // test whether this is a typed-function - if (typeof fn.signatures === 'object') { - // merge the signatures - for (var signature in fn.signatures) { - if (fn.signatures.hasOwnProperty(signature)) { - validateUnique(signature, fn.signatures[signature]); - signaturesMap[signature] = fn.signatures[signature]; - } - } - } else if (typeof fn.signature === 'string') { - validateUnique(fn.signature, fn); - signaturesMap[fn.signature] = fn; - } else { - err = new TypeError('Function is no typed-function (index: ' + i + ')'); - err.data = {index: i}; - throw err; - } - } - - return signaturesMap; - } - - typed = createTypedFunction('typed', { - 'string, Object': createTypedFunction, - 'Object': function (signaturesMap) { - // find existing name - var fns = []; - for (var signature in signaturesMap) { - if (signaturesMap.hasOwnProperty(signature)) { - fns.push(signaturesMap[signature]); - } - } - var name = getName(fns); - return createTypedFunction(name, signaturesMap); - }, - '...Function': function (fns) { - return createTypedFunction(getName(fns), extractSignatures(fns)); - }, - 'string, ...Function': function (name, fns) { - return createTypedFunction(name, extractSignatures(fns)); - } - }); - - typed.create = create; - typed.types = _types; - typed.conversions = _conversions; - typed.ignore = _ignore; - typed.convert = convert; - typed.find = find; - - /** - * add a type - * @param {{name: string, test: function}} type - * @param {boolean} [beforeObjectTest=true] - * If true, the new test will be inserted before - * the test with name 'Object' (if any), since - * tests for Object match Array and classes too. - */ - typed.addType = function (type, beforeObjectTest) { - if (!type || typeof type.name !== 'string' || typeof type.test !== 'function') { - throw new TypeError('Object with properties {name: string, test: function} expected'); - } - - if (beforeObjectTest !== false) { - for (var i = 0; i < typed.types.length; i++) { - if (typed.types[i].name === 'Object') { - typed.types.splice(i, 0, type); - return; - } - } - } - - typed.types.push(type); - }; - - // add a conversion - typed.addConversion = function (conversion) { - if (!conversion - || typeof conversion.from !== 'string' - || typeof conversion.to !== 'string' - || typeof conversion.convert !== 'function') { - throw new TypeError('Object with properties {from: string, to: string, convert: function} expected'); - } - - typed.conversions.push(conversion); - }; - - return typed; - } - - return create(); - })); - - /***/ }), - /* 15 */ - /***/ (function (module, exports, __webpack_require__) { - - "use strict"; - /* WEBPACK VAR INJECTION */(function (global) { - - var width = 256;// each RC4 output is 0 <= x < 256 - var chunks = 6;// at least six RC4 outputs for each double - var digits = 52;// there are 52 significant digits in a double - var pool = [];// pool: entropy pool starts empty - var GLOBAL = typeof global === 'undefined' ? window : global; - - // - // The following constants are related to IEEE 754 limits. - // - var startdenom = Math.pow(width, chunks), - significance = Math.pow(2, digits), - overflow = significance * 2, - mask = width - 1; - - var oldRandom = Math.random; - - // - // seedrandom() - // This is the seedrandom function described above. - // - module.exports = function (seed, options) { - if (options && options.global === true) { - options.global = false; - Math.random = module.exports(seed, options); - options.global = true; - return Math.random; - } - var use_entropy = (options && options.entropy) || false; - var key = []; - - // Flatten the seed string or build one from local entropy if needed. - var shortseed = mixkey(flatten( - use_entropy ? [seed, tostring(pool)] : - 0 in arguments ? seed : autoseed(), 3), key); - - // Use the seed to initialize an ARC4 generator. - var arc4 = new ARC4(key); - - // Mix the randomness into accumulated entropy. - mixkey(tostring(arc4.S), pool); - - // Override Math.random - - // This function returns a random double in [0, 1) that contains - // randomness in every bit of the mantissa of the IEEE 754 value. - - return function () { // Closure to return a random double: - var n = arc4.g(chunks), // Start with a numerator n < 2 ^ 48 - d = startdenom, // and denominator d = 2 ^ 48. - x = 0; // and no 'extra last byte'. - while (n < significance) { // Fill up all significant digits by - n = (n + x) * width; // shifting numerator and - d *= width; // denominator and generating a - x = arc4.g(1); // new least-significant-byte. - } - while (n >= overflow) { // To avoid rounding up, before adding - n /= 2; // last byte, shift everything - d /= 2; // right using integer Math until - x >>>= 1; // we have exactly the desired bits. - } - return (n + x) / d; // Form the number within [0, 1). - }; - }; - - module.exports.resetGlobal = function () { - Math.random = oldRandom; - }; - - // - // ARC4 - // - // An ARC4 implementation. The constructor takes a key in the form of - // an array of at most (width) integers that should be 0 <= x < (width). - // - // The g(count) method returns a pseudorandom integer that concatenates - // the next (count) outputs from ARC4. Its return value is a number x - // that is in the range 0 <= x < (width ^ count). - // - /** @constructor */ - function ARC4(key) { - var t, keylen = key.length, - me = this, i = 0, j = me.i = me.j = 0, s = me.S = []; - - // The empty key [] is treated as [0]. - if (!keylen) { - key = [keylen++]; - } - - // Set up S using the standard key scheduling algorithm. - while (i < width) { - s[i] = i++; - } - for (i = 0; i < width; i++) { - s[i] = s[j = mask & (j + key[i % keylen] + (t = s[i]))]; - s[j] = t; - } - - // The "g" method returns the next (count) outputs as one number. - (me.g = function (count) { - // Using instance members instead of closure state nearly doubles speed. - var t, r = 0, - i = me.i, j = me.j, s = me.S; - while (count--) { - t = s[i = mask & (i + 1)]; - r = r * width + s[mask & ((s[i] = s[j = mask & (j + t)]) + (s[j] = t))]; - } - me.i = i; me.j = j; - return r; - // For robust unpredictability discard an initial batch of values. - // See http://www.rsa.com/rsalabs/node.asp?id=2009 - })(width); - } - - // - // flatten() - // Converts an object tree to nested arrays of strings. - // - function flatten(obj, depth) { - var result = [], typ = (typeof obj)[0], prop; - if (depth && typ == 'o') { - for (prop in obj) { - try { - result.push(flatten(obj[prop], depth - 1)); - } catch (e) {} - } - } - return (result.length ? result : typ == 's' ? obj : obj + '\0'); - } - - // - // mixkey() - // Mixes a string seed into a key that is an array of integers, and - // returns a shortened string seed that is equivalent to the result key. - // - function mixkey(seed, key) { - var stringseed = seed + '', smear, j = 0; - while (j < stringseed.length) { - key[mask & j] = - mask & ((smear ^= key[mask & j] * 19) + stringseed.charCodeAt(j++)); - } - return tostring(key); - } - - // - // autoseed() - // Returns an object for autoseeding, using window.crypto if available. - // - /** @param {Uint8Array=} seed */ - function autoseed(seed) { - try { - GLOBAL.crypto.getRandomValues(seed = new Uint8Array(width)); - return tostring(seed); - } catch (e) { - return [+new Date, GLOBAL, GLOBAL.navigator && GLOBAL.navigator.plugins, - GLOBAL.screen, tostring(pool)]; - } - } - - // - // tostring() - // Converts an array of charcodes to a string - // - function tostring(a) { - return String.fromCharCode.apply(0, a); - } - - // - // When seedrandom.js is loaded, we immediately mix a few bits - // from the built-in RNG into the entropy pool. Because we do - // not want to intefere with determinstic PRNG state later, - // seedrandom will not call Math.random on its own again after - // initialization. - // - mixkey(Math.random(), pool); - - /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(19))); - - /***/ }), - /* 16 */ - /***/ (function (module, exports, __webpack_require__) { - - "use strict"; - - // Map the characters to escape to their escaped values. The list is derived - // from http://www.cespedes.org/blog/85/how-to-escape-latex-special-characters - - var _extends = Object.assign || function (target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i]; for (var key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } return target; - }; - - var defaultEscapes = { - "{": "\\{", - "}": "\\}", - "\\": "\\textbackslash{}", - "#": "\\#", - $: "\\$", - "%": "\\%", - "&": "\\&", - "^": "\\textasciicircum{}", - _: "\\_", - "~": "\\textasciitilde{}" - }; - var formatEscapes = { - "\u2013": "\\--", - "\u2014": "\\---", - " ": "~", - "\t": "\\qquad{}", - "\r\n": "\\newline{}", - "\n": "\\newline{}" - }; - - var defaultEscapeMapFn = function defaultEscapeMapFn(defaultEscapes, formatEscapes) { - return _extends({}, defaultEscapes, formatEscapes); - }; - - /** - * Escape a string to be used in LaTeX documents. - * @param {string} str the string to be escaped. - * @param {boolean} params.preserveFormatting whether formatting escapes should - * be performed (default: false). - * @param {function} params.escapeMapFn the function to modify the escape maps. - * @return {string} the escaped string, ready to be used in LaTeX. - */ - module.exports = function (str) { - var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, - _ref$preserveFormatti = _ref.preserveFormatting, - preserveFormatting = _ref$preserveFormatti === undefined ? false : _ref$preserveFormatti, - _ref$escapeMapFn = _ref.escapeMapFn, - escapeMapFn = _ref$escapeMapFn === undefined ? defaultEscapeMapFn : _ref$escapeMapFn; - - var runningStr = String(str); - var result = ""; - - var escapes = escapeMapFn(_extends({}, defaultEscapes), preserveFormatting ? _extends({}, formatEscapes) : {}); - var escapeKeys = Object.keys(escapes); // as it is reused later on - - // Algorithm: Go through the string character by character, if it matches - // with one of the special characters then we'll replace it with the escaped - // version. - - var _loop = function _loop() { - var specialCharFound = false; - escapeKeys.forEach(function (key, index) { - if (specialCharFound) { - return; - } - if (runningStr.length >= key.length && runningStr.slice(0, key.length) === key) { - result += escapes[escapeKeys[index]]; - runningStr = runningStr.slice(key.length, runningStr.length); - specialCharFound = true; - } - }); - if (!specialCharFound) { - result += runningStr.slice(0, 1); - runningStr = runningStr.slice(1, runningStr.length); - } - }; - - while (runningStr) { - _loop(); - } - return result; - }; - - /***/ }), - /* 17 */ - /***/ (function (module, exports) { - - function E() { - // Keep this empty so it's easier to inherit from - // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3) - } - - E.prototype = { - on: function (name, callback, ctx) { - var e = this.e || (this.e = {}); - - (e[name] || (e[name] = [])).push({ - fn: callback, - ctx: ctx - }); - - return this; - }, - - once: function (name, callback, ctx) { - var self = this; - function listener() { - self.off(name, listener); - callback.apply(ctx, arguments); - }; - - listener._ = callback; - return this.on(name, listener, ctx); - }, - - emit: function (name) { - var data = [].slice.call(arguments, 1); - var evtArr = ((this.e || (this.e = {}))[name] || []).slice(); - var i = 0; - var len = evtArr.length; - - for (i; i < len; i++) { - evtArr[i].fn.apply(evtArr[i].ctx, data); - } - - return this; - }, - - off: function (name, callback) { - var e = this.e || (this.e = {}); - var evts = e[name]; - var liveEvents = []; - - if (evts && callback) { - for (var i = 0, len = evts.length; i < len; i++) { - if (evts[i].fn !== callback && evts[i].fn._ !== callback) { - liveEvents.push(evts[i]); - } - } - } - - // Remove event from queue to prevent memory leak - // Suggested by https://github.com/lazd - // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910 - - (liveEvents.length) - ? e[name] = liveEvents - : delete e[name]; - - return this; - } - }; - - module.exports = E; - module.exports.TinyEmitter = E; - - /***/ }), - /* 18 */ - /***/ (function (module, exports, __webpack_require__) { - - var all = __webpack_require__(20); - - var _require = __webpack_require__(21), - create = _require.create; - - var defaultInstance = create(all); // TODO: not nice having to revert to CommonJS, find an ES6 solution - - module.exports = /* #__PURE__ */defaultInstance; - - /***/ }), - /* 19 */ - /***/ (function (module, exports) { - - var g; - - // This works in non-strict mode - g = (function () { - return this; - })(); - - try { - // This works if eval is allowed (see CSP) - g = g || new Function("return this")(); - } catch (e) { - // This works if the window reference is available - if (typeof window === "object") { - g = window; - } - } - - // g can still be undefined, but nothing to do about it... - // We return undefined, instead of nothing here, so it's - // easier to handle this case. if(!global) { ...} - - module.exports = g; - - /***/ }), - /* 20 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - // ESM COMPAT FLAG - __webpack_require__.r(__webpack_exports__); - - // EXPORTS - __webpack_require__.d(__webpack_exports__, "createTyped", function () { - return /* reexport */ typed_createTyped; - }); - __webpack_require__.d(__webpack_exports__, "createResultSet", function () { - return /* reexport */ createResultSet; - }); - __webpack_require__.d(__webpack_exports__, "createBigNumberClass", function () { - return /* reexport */ createBigNumberClass; - }); - __webpack_require__.d(__webpack_exports__, "createComplexClass", function () { - return /* reexport */ createComplexClass; - }); - __webpack_require__.d(__webpack_exports__, "createFractionClass", function () { - return /* reexport */ createFractionClass; - }); - __webpack_require__.d(__webpack_exports__, "createRangeClass", function () { - return /* reexport */ createRangeClass; - }); - __webpack_require__.d(__webpack_exports__, "createMatrixClass", function () { - return /* reexport */ createMatrixClass; - }); - __webpack_require__.d(__webpack_exports__, "createDenseMatrixClass", function () { - return /* reexport */ createDenseMatrixClass; - }); - __webpack_require__.d(__webpack_exports__, "createClone", function () { - return /* reexport */ createClone; - }); - __webpack_require__.d(__webpack_exports__, "createIsInteger", function () { - return /* reexport */ createIsInteger; - }); - __webpack_require__.d(__webpack_exports__, "createIsNegative", function () { - return /* reexport */ createIsNegative; - }); - __webpack_require__.d(__webpack_exports__, "createIsNumeric", function () { - return /* reexport */ createIsNumeric; - }); - __webpack_require__.d(__webpack_exports__, "createHasNumericValue", function () { - return /* reexport */ createHasNumericValue; - }); - __webpack_require__.d(__webpack_exports__, "createIsPositive", function () { - return /* reexport */ createIsPositive; - }); - __webpack_require__.d(__webpack_exports__, "createIsZero", function () { - return /* reexport */ createIsZero; - }); - __webpack_require__.d(__webpack_exports__, "createIsNaN", function () { - return /* reexport */ createIsNaN; - }); - __webpack_require__.d(__webpack_exports__, "createTypeOf", function () { - return /* reexport */ createTypeOf; - }); - __webpack_require__.d(__webpack_exports__, "createEqualScalar", function () { - return /* reexport */ createEqualScalar; - }); - __webpack_require__.d(__webpack_exports__, "createSparseMatrixClass", function () { - return /* reexport */ createSparseMatrixClass; - }); - __webpack_require__.d(__webpack_exports__, "createNumber", function () { - return /* reexport */ createNumber; - }); - __webpack_require__.d(__webpack_exports__, "createString", function () { - return /* reexport */ createString; - }); - __webpack_require__.d(__webpack_exports__, "createBoolean", function () { - return /* reexport */ createBoolean; - }); - __webpack_require__.d(__webpack_exports__, "createBignumber", function () { - return /* reexport */ createBignumber; - }); - __webpack_require__.d(__webpack_exports__, "createComplex", function () { - return /* reexport */ createComplex; - }); - __webpack_require__.d(__webpack_exports__, "createFraction", function () { - return /* reexport */ createFraction; - }); - __webpack_require__.d(__webpack_exports__, "createMatrix", function () { - return /* reexport */ createMatrix; - }); - __webpack_require__.d(__webpack_exports__, "createSplitUnit", function () { - return /* reexport */ createSplitUnit; - }); - __webpack_require__.d(__webpack_exports__, "createUnaryMinus", function () { - return /* reexport */ createUnaryMinus; - }); - __webpack_require__.d(__webpack_exports__, "createUnaryPlus", function () { - return /* reexport */ createUnaryPlus; - }); - __webpack_require__.d(__webpack_exports__, "createAbs", function () { - return /* reexport */ createAbs; - }); - __webpack_require__.d(__webpack_exports__, "createApply", function () { - return /* reexport */ createApply; - }); - __webpack_require__.d(__webpack_exports__, "createAddScalar", function () { - return /* reexport */ createAddScalar; - }); - __webpack_require__.d(__webpack_exports__, "createCbrt", function () { - return /* reexport */ createCbrt; - }); - __webpack_require__.d(__webpack_exports__, "createCeil", function () { - return /* reexport */ createCeil; - }); - __webpack_require__.d(__webpack_exports__, "createCube", function () { - return /* reexport */ createCube; - }); - __webpack_require__.d(__webpack_exports__, "createExp", function () { - return /* reexport */ createExp; - }); - __webpack_require__.d(__webpack_exports__, "createExpm1", function () { - return /* reexport */ createExpm1; - }); - __webpack_require__.d(__webpack_exports__, "createFix", function () { - return /* reexport */ createFix; - }); - __webpack_require__.d(__webpack_exports__, "createFloor", function () { - return /* reexport */ createFloor; - }); - __webpack_require__.d(__webpack_exports__, "createGcd", function () { - return /* reexport */ createGcd; - }); - __webpack_require__.d(__webpack_exports__, "createLcm", function () { - return /* reexport */ createLcm; - }); - __webpack_require__.d(__webpack_exports__, "createLog10", function () { - return /* reexport */ createLog10; - }); - __webpack_require__.d(__webpack_exports__, "createLog2", function () { - return /* reexport */ createLog2; - }); - __webpack_require__.d(__webpack_exports__, "createMod", function () { - return /* reexport */ createMod; - }); - __webpack_require__.d(__webpack_exports__, "createMultiplyScalar", function () { - return /* reexport */ createMultiplyScalar; - }); - __webpack_require__.d(__webpack_exports__, "createMultiply", function () { - return /* reexport */ createMultiply; - }); - __webpack_require__.d(__webpack_exports__, "createNthRoot", function () { - return /* reexport */ createNthRoot; - }); - __webpack_require__.d(__webpack_exports__, "createSign", function () { - return /* reexport */ createSign; - }); - __webpack_require__.d(__webpack_exports__, "createSqrt", function () { - return /* reexport */ createSqrt; - }); - __webpack_require__.d(__webpack_exports__, "createSquare", function () { - return /* reexport */ createSquare; - }); - __webpack_require__.d(__webpack_exports__, "createSubtract", function () { - return /* reexport */ createSubtract; - }); - __webpack_require__.d(__webpack_exports__, "createXgcd", function () { - return /* reexport */ createXgcd; - }); - __webpack_require__.d(__webpack_exports__, "createDotMultiply", function () { - return /* reexport */ createDotMultiply; - }); - __webpack_require__.d(__webpack_exports__, "createBitAnd", function () { - return /* reexport */ createBitAnd; - }); - __webpack_require__.d(__webpack_exports__, "createBitNot", function () { - return /* reexport */ createBitNot; - }); - __webpack_require__.d(__webpack_exports__, "createBitOr", function () { - return /* reexport */ createBitOr; - }); - __webpack_require__.d(__webpack_exports__, "createBitXor", function () { - return /* reexport */ createBitXor; - }); - __webpack_require__.d(__webpack_exports__, "createArg", function () { - return /* reexport */ createArg; - }); - __webpack_require__.d(__webpack_exports__, "createConj", function () { - return /* reexport */ createConj; - }); - __webpack_require__.d(__webpack_exports__, "createIm", function () { - return /* reexport */ createIm; - }); - __webpack_require__.d(__webpack_exports__, "createRe", function () { - return /* reexport */ createRe; - }); - __webpack_require__.d(__webpack_exports__, "createNot", function () { - return /* reexport */ createNot; - }); - __webpack_require__.d(__webpack_exports__, "createOr", function () { - return /* reexport */ createOr; - }); - __webpack_require__.d(__webpack_exports__, "createXor", function () { - return /* reexport */ createXor; - }); - __webpack_require__.d(__webpack_exports__, "createConcat", function () { - return /* reexport */ createConcat; - }); - __webpack_require__.d(__webpack_exports__, "createColumn", function () { - return /* reexport */ createColumn; - }); - __webpack_require__.d(__webpack_exports__, "createCross", function () { - return /* reexport */ createCross; - }); - __webpack_require__.d(__webpack_exports__, "createDiag", function () { - return /* reexport */ createDiag; - }); - __webpack_require__.d(__webpack_exports__, "createFilter", function () { - return /* reexport */ createFilter; - }); - __webpack_require__.d(__webpack_exports__, "createFlatten", function () { - return /* reexport */ createFlatten; - }); - __webpack_require__.d(__webpack_exports__, "createForEach", function () { - return /* reexport */ createForEach; - }); - __webpack_require__.d(__webpack_exports__, "createGetMatrixDataType", function () { - return /* reexport */ createGetMatrixDataType; - }); - __webpack_require__.d(__webpack_exports__, "createIdentity", function () { - return /* reexport */ createIdentity; - }); - __webpack_require__.d(__webpack_exports__, "createKron", function () { - return /* reexport */ createKron; - }); - __webpack_require__.d(__webpack_exports__, "createMap", function () { - return /* reexport */ createMap; - }); - __webpack_require__.d(__webpack_exports__, "createDiff", function () { - return /* reexport */ createDiff; - }); - __webpack_require__.d(__webpack_exports__, "createOnes", function () { - return /* reexport */ createOnes; - }); - __webpack_require__.d(__webpack_exports__, "createRange", function () { - return /* reexport */ range_createRange; - }); - __webpack_require__.d(__webpack_exports__, "createReshape", function () { - return /* reexport */ createReshape; - }); - __webpack_require__.d(__webpack_exports__, "createResize", function () { - return /* reexport */ createResize; - }); - __webpack_require__.d(__webpack_exports__, "createRotationMatrix", function () { - return /* reexport */ createRotationMatrix; - }); - __webpack_require__.d(__webpack_exports__, "createRow", function () { - return /* reexport */ createRow; - }); - __webpack_require__.d(__webpack_exports__, "createSize", function () { - return /* reexport */ createSize; - }); - __webpack_require__.d(__webpack_exports__, "createSqueeze", function () { - return /* reexport */ createSqueeze; - }); - __webpack_require__.d(__webpack_exports__, "createSubset", function () { - return /* reexport */ createSubset; - }); - __webpack_require__.d(__webpack_exports__, "createTranspose", function () { - return /* reexport */ createTranspose; - }); - __webpack_require__.d(__webpack_exports__, "createCtranspose", function () { - return /* reexport */ createCtranspose; - }); - __webpack_require__.d(__webpack_exports__, "createZeros", function () { - return /* reexport */ createZeros; - }); - __webpack_require__.d(__webpack_exports__, "createErf", function () { - return /* reexport */ createErf; - }); - __webpack_require__.d(__webpack_exports__, "createMode", function () { - return /* reexport */ createMode; - }); - __webpack_require__.d(__webpack_exports__, "createProd", function () { - return /* reexport */ createProd; - }); - __webpack_require__.d(__webpack_exports__, "createFormat", function () { - return /* reexport */ createFormat; - }); - __webpack_require__.d(__webpack_exports__, "createBin", function () { - return /* reexport */ createBin; - }); - __webpack_require__.d(__webpack_exports__, "createOct", function () { - return /* reexport */ createOct; - }); - __webpack_require__.d(__webpack_exports__, "createHex", function () { - return /* reexport */ createHex; - }); - __webpack_require__.d(__webpack_exports__, "createPrint", function () { - return /* reexport */ createPrint; - }); - __webpack_require__.d(__webpack_exports__, "createTo", function () { - return /* reexport */ createTo; - }); - __webpack_require__.d(__webpack_exports__, "createIsPrime", function () { - return /* reexport */ createIsPrime; - }); - __webpack_require__.d(__webpack_exports__, "createNumeric", function () { - return /* reexport */ createNumeric; - }); - __webpack_require__.d(__webpack_exports__, "createDivideScalar", function () { - return /* reexport */ createDivideScalar; - }); - __webpack_require__.d(__webpack_exports__, "createPow", function () { - return /* reexport */ createPow; - }); - __webpack_require__.d(__webpack_exports__, "createRound", function () { - return /* reexport */ createRound; - }); - __webpack_require__.d(__webpack_exports__, "createLog", function () { - return /* reexport */ createLog; - }); - __webpack_require__.d(__webpack_exports__, "createLog1p", function () { - return /* reexport */ createLog1p; - }); - __webpack_require__.d(__webpack_exports__, "createNthRoots", function () { - return /* reexport */ createNthRoots; - }); - __webpack_require__.d(__webpack_exports__, "createDotPow", function () { - return /* reexport */ createDotPow; - }); - __webpack_require__.d(__webpack_exports__, "createDotDivide", function () { - return /* reexport */ createDotDivide; - }); - __webpack_require__.d(__webpack_exports__, "createLsolve", function () { - return /* reexport */ createLsolve; - }); - __webpack_require__.d(__webpack_exports__, "createUsolve", function () { - return /* reexport */ createUsolve; - }); - __webpack_require__.d(__webpack_exports__, "createLsolveAll", function () { - return /* reexport */ createLsolveAll; - }); - __webpack_require__.d(__webpack_exports__, "createUsolveAll", function () { - return /* reexport */ createUsolveAll; - }); - __webpack_require__.d(__webpack_exports__, "createLeftShift", function () { - return /* reexport */ createLeftShift; - }); - __webpack_require__.d(__webpack_exports__, "createRightArithShift", function () { - return /* reexport */ createRightArithShift; - }); - __webpack_require__.d(__webpack_exports__, "createRightLogShift", function () { - return /* reexport */ createRightLogShift; - }); - __webpack_require__.d(__webpack_exports__, "createAnd", function () { - return /* reexport */ createAnd; - }); - __webpack_require__.d(__webpack_exports__, "createCompare", function () { - return /* reexport */ createCompare; - }); - __webpack_require__.d(__webpack_exports__, "createCompareNatural", function () { - return /* reexport */ createCompareNatural; - }); - __webpack_require__.d(__webpack_exports__, "createCompareText", function () { - return /* reexport */ createCompareText; - }); - __webpack_require__.d(__webpack_exports__, "createEqual", function () { - return /* reexport */ createEqual; - }); - __webpack_require__.d(__webpack_exports__, "createEqualText", function () { - return /* reexport */ createEqualText; - }); - __webpack_require__.d(__webpack_exports__, "createSmaller", function () { - return /* reexport */ createSmaller; - }); - __webpack_require__.d(__webpack_exports__, "createSmallerEq", function () { - return /* reexport */ createSmallerEq; - }); - __webpack_require__.d(__webpack_exports__, "createLarger", function () { - return /* reexport */ createLarger; - }); - __webpack_require__.d(__webpack_exports__, "createLargerEq", function () { - return /* reexport */ createLargerEq; - }); - __webpack_require__.d(__webpack_exports__, "createDeepEqual", function () { - return /* reexport */ createDeepEqual; - }); - __webpack_require__.d(__webpack_exports__, "createUnequal", function () { - return /* reexport */ createUnequal; - }); - __webpack_require__.d(__webpack_exports__, "createPartitionSelect", function () { - return /* reexport */ createPartitionSelect; - }); - __webpack_require__.d(__webpack_exports__, "createSort", function () { - return /* reexport */ createSort; - }); - __webpack_require__.d(__webpack_exports__, "createMax", function () { - return /* reexport */ createMax; - }); - __webpack_require__.d(__webpack_exports__, "createMin", function () { - return /* reexport */ createMin; - }); - __webpack_require__.d(__webpack_exports__, "createImmutableDenseMatrixClass", function () { - return /* reexport */ createImmutableDenseMatrixClass; - }); - __webpack_require__.d(__webpack_exports__, "createIndexClass", function () { - return /* reexport */ createIndexClass; - }); - __webpack_require__.d(__webpack_exports__, "createFibonacciHeapClass", function () { - return /* reexport */ createFibonacciHeapClass; - }); - __webpack_require__.d(__webpack_exports__, "createSpaClass", function () { - return /* reexport */ createSpaClass; - }); - __webpack_require__.d(__webpack_exports__, "createUnitClass", function () { - return /* reexport */ createUnitClass; - }); - __webpack_require__.d(__webpack_exports__, "createUnitFunction", function () { - return /* reexport */ createUnitFunction; - }); - __webpack_require__.d(__webpack_exports__, "createSparse", function () { - return /* reexport */ createSparse; - }); - __webpack_require__.d(__webpack_exports__, "createCreateUnit", function () { - return /* reexport */ createCreateUnit; - }); - __webpack_require__.d(__webpack_exports__, "createAcos", function () { - return /* reexport */ createAcos; - }); - __webpack_require__.d(__webpack_exports__, "createAcosh", function () { - return /* reexport */ createAcosh; - }); - __webpack_require__.d(__webpack_exports__, "createAcot", function () { - return /* reexport */ createAcot; - }); - __webpack_require__.d(__webpack_exports__, "createAcoth", function () { - return /* reexport */ createAcoth; - }); - __webpack_require__.d(__webpack_exports__, "createAcsc", function () { - return /* reexport */ createAcsc; - }); - __webpack_require__.d(__webpack_exports__, "createAcsch", function () { - return /* reexport */ createAcsch; - }); - __webpack_require__.d(__webpack_exports__, "createAsec", function () { - return /* reexport */ createAsec; - }); - __webpack_require__.d(__webpack_exports__, "createAsech", function () { - return /* reexport */ createAsech; - }); - __webpack_require__.d(__webpack_exports__, "createAsin", function () { - return /* reexport */ createAsin; - }); - __webpack_require__.d(__webpack_exports__, "createAsinh", function () { - return /* reexport */ createAsinh; - }); - __webpack_require__.d(__webpack_exports__, "createAtan", function () { - return /* reexport */ createAtan; - }); - __webpack_require__.d(__webpack_exports__, "createAtan2", function () { - return /* reexport */ createAtan2; - }); - __webpack_require__.d(__webpack_exports__, "createAtanh", function () { - return /* reexport */ createAtanh; - }); - __webpack_require__.d(__webpack_exports__, "createCos", function () { - return /* reexport */ createCos; - }); - __webpack_require__.d(__webpack_exports__, "createCosh", function () { - return /* reexport */ createCosh; - }); - __webpack_require__.d(__webpack_exports__, "createCot", function () { - return /* reexport */ createCot; - }); - __webpack_require__.d(__webpack_exports__, "createCoth", function () { - return /* reexport */ createCoth; - }); - __webpack_require__.d(__webpack_exports__, "createCsc", function () { - return /* reexport */ createCsc; - }); - __webpack_require__.d(__webpack_exports__, "createCsch", function () { - return /* reexport */ createCsch; - }); - __webpack_require__.d(__webpack_exports__, "createSec", function () { - return /* reexport */ createSec; - }); - __webpack_require__.d(__webpack_exports__, "createSech", function () { - return /* reexport */ createSech; - }); - __webpack_require__.d(__webpack_exports__, "createSin", function () { - return /* reexport */ createSin; - }); - __webpack_require__.d(__webpack_exports__, "createSinh", function () { - return /* reexport */ createSinh; - }); - __webpack_require__.d(__webpack_exports__, "createTan", function () { - return /* reexport */ createTan; - }); - __webpack_require__.d(__webpack_exports__, "createTanh", function () { - return /* reexport */ createTanh; - }); - __webpack_require__.d(__webpack_exports__, "createSetCartesian", function () { - return /* reexport */ createSetCartesian; - }); - __webpack_require__.d(__webpack_exports__, "createSetDifference", function () { - return /* reexport */ createSetDifference; - }); - __webpack_require__.d(__webpack_exports__, "createSetDistinct", function () { - return /* reexport */ createSetDistinct; - }); - __webpack_require__.d(__webpack_exports__, "createSetIntersect", function () { - return /* reexport */ createSetIntersect; - }); - __webpack_require__.d(__webpack_exports__, "createSetIsSubset", function () { - return /* reexport */ createSetIsSubset; - }); - __webpack_require__.d(__webpack_exports__, "createSetMultiplicity", function () { - return /* reexport */ createSetMultiplicity; - }); - __webpack_require__.d(__webpack_exports__, "createSetPowerset", function () { - return /* reexport */ createSetPowerset; - }); - __webpack_require__.d(__webpack_exports__, "createSetSize", function () { - return /* reexport */ createSetSize; - }); - __webpack_require__.d(__webpack_exports__, "createSetSymDifference", function () { - return /* reexport */ createSetSymDifference; - }); - __webpack_require__.d(__webpack_exports__, "createSetUnion", function () { - return /* reexport */ createSetUnion; - }); - __webpack_require__.d(__webpack_exports__, "createAdd", function () { - return /* reexport */ createAdd; - }); - __webpack_require__.d(__webpack_exports__, "createHypot", function () { - return /* reexport */ createHypot; - }); - __webpack_require__.d(__webpack_exports__, "createNorm", function () { - return /* reexport */ createNorm; - }); - __webpack_require__.d(__webpack_exports__, "createDot", function () { - return /* reexport */ createDot; - }); - __webpack_require__.d(__webpack_exports__, "createTrace", function () { - return /* reexport */ createTrace; - }); - __webpack_require__.d(__webpack_exports__, "createIndex", function () { - return /* reexport */ createIndex; - }); - __webpack_require__.d(__webpack_exports__, "createNode", function () { - return /* reexport */ createNode; - }); - __webpack_require__.d(__webpack_exports__, "createAccessorNode", function () { - return /* reexport */ createAccessorNode; - }); - __webpack_require__.d(__webpack_exports__, "createArrayNode", function () { - return /* reexport */ createArrayNode; - }); - __webpack_require__.d(__webpack_exports__, "createAssignmentNode", function () { - return /* reexport */ createAssignmentNode; - }); - __webpack_require__.d(__webpack_exports__, "createBlockNode", function () { - return /* reexport */ createBlockNode; - }); - __webpack_require__.d(__webpack_exports__, "createConditionalNode", function () { - return /* reexport */ createConditionalNode; - }); - __webpack_require__.d(__webpack_exports__, "createConstantNode", function () { - return /* reexport */ ConstantNode_createConstantNode; - }); - __webpack_require__.d(__webpack_exports__, "createFunctionAssignmentNode", function () { - return /* reexport */ createFunctionAssignmentNode; - }); - __webpack_require__.d(__webpack_exports__, "createIndexNode", function () { - return /* reexport */ createIndexNode; - }); - __webpack_require__.d(__webpack_exports__, "createObjectNode", function () { - return /* reexport */ createObjectNode; - }); - __webpack_require__.d(__webpack_exports__, "createOperatorNode", function () { - return /* reexport */ createOperatorNode; - }); - __webpack_require__.d(__webpack_exports__, "createParenthesisNode", function () { - return /* reexport */ createParenthesisNode; - }); - __webpack_require__.d(__webpack_exports__, "createRangeNode", function () { - return /* reexport */ createRangeNode; - }); - __webpack_require__.d(__webpack_exports__, "createRelationalNode", function () { - return /* reexport */ createRelationalNode; - }); - __webpack_require__.d(__webpack_exports__, "createSymbolNode", function () { - return /* reexport */ createSymbolNode; - }); - __webpack_require__.d(__webpack_exports__, "createFunctionNode", function () { - return /* reexport */ createFunctionNode; - }); - __webpack_require__.d(__webpack_exports__, "createParse", function () { - return /* reexport */ createParse; - }); - __webpack_require__.d(__webpack_exports__, "createCompile", function () { - return /* reexport */ createCompile; - }); - __webpack_require__.d(__webpack_exports__, "createEvaluate", function () { - return /* reexport */ createEvaluate; - }); - __webpack_require__.d(__webpack_exports__, "createParserClass", function () { - return /* reexport */ createParserClass; - }); - __webpack_require__.d(__webpack_exports__, "createParser", function () { - return /* reexport */ createParser; - }); - __webpack_require__.d(__webpack_exports__, "createLup", function () { - return /* reexport */ createLup; - }); - __webpack_require__.d(__webpack_exports__, "createQr", function () { - return /* reexport */ createQr; - }); - __webpack_require__.d(__webpack_exports__, "createSlu", function () { - return /* reexport */ createSlu; - }); - __webpack_require__.d(__webpack_exports__, "createLusolve", function () { - return /* reexport */ createLusolve; - }); - __webpack_require__.d(__webpack_exports__, "createHelpClass", function () { - return /* reexport */ createHelpClass; - }); - __webpack_require__.d(__webpack_exports__, "createChainClass", function () { - return /* reexport */ createChainClass; - }); - __webpack_require__.d(__webpack_exports__, "createHelp", function () { - return /* reexport */ createHelp; - }); - __webpack_require__.d(__webpack_exports__, "createChain", function () { - return /* reexport */ createChain; - }); - __webpack_require__.d(__webpack_exports__, "createDet", function () { - return /* reexport */ createDet; - }); - __webpack_require__.d(__webpack_exports__, "createInv", function () { - return /* reexport */ createInv; - }); - __webpack_require__.d(__webpack_exports__, "createEigs", function () { - return /* reexport */ createEigs; - }); - __webpack_require__.d(__webpack_exports__, "createExpm", function () { - return /* reexport */ createExpm; - }); - __webpack_require__.d(__webpack_exports__, "createSqrtm", function () { - return /* reexport */ createSqrtm; - }); - __webpack_require__.d(__webpack_exports__, "createDivide", function () { - return /* reexport */ createDivide; - }); - __webpack_require__.d(__webpack_exports__, "createDistance", function () { - return /* reexport */ createDistance; - }); - __webpack_require__.d(__webpack_exports__, "createIntersect", function () { - return /* reexport */ createIntersect; - }); - __webpack_require__.d(__webpack_exports__, "createSum", function () { - return /* reexport */ createSum; - }); - __webpack_require__.d(__webpack_exports__, "createMean", function () { - return /* reexport */ createMean; - }); - __webpack_require__.d(__webpack_exports__, "createMedian", function () { - return /* reexport */ createMedian; - }); - __webpack_require__.d(__webpack_exports__, "createMad", function () { - return /* reexport */ createMad; - }); - __webpack_require__.d(__webpack_exports__, "createVariance", function () { - return /* reexport */ createVariance; - }); - __webpack_require__.d(__webpack_exports__, "createQuantileSeq", function () { - return /* reexport */ createQuantileSeq; - }); - __webpack_require__.d(__webpack_exports__, "createStd", function () { - return /* reexport */ createStd; - }); - __webpack_require__.d(__webpack_exports__, "createCombinations", function () { - return /* reexport */ createCombinations; - }); - __webpack_require__.d(__webpack_exports__, "createCombinationsWithRep", function () { - return /* reexport */ createCombinationsWithRep; - }); - __webpack_require__.d(__webpack_exports__, "createGamma", function () { - return /* reexport */ createGamma; - }); - __webpack_require__.d(__webpack_exports__, "createFactorial", function () { - return /* reexport */ createFactorial; - }); - __webpack_require__.d(__webpack_exports__, "createKldivergence", function () { - return /* reexport */ createKldivergence; - }); - __webpack_require__.d(__webpack_exports__, "createMultinomial", function () { - return /* reexport */ createMultinomial; - }); - __webpack_require__.d(__webpack_exports__, "createPermutations", function () { - return /* reexport */ createPermutations; - }); - __webpack_require__.d(__webpack_exports__, "createPickRandom", function () { - return /* reexport */ createPickRandom; - }); - __webpack_require__.d(__webpack_exports__, "createRandom", function () { - return /* reexport */ createRandom; - }); - __webpack_require__.d(__webpack_exports__, "createRandomInt", function () { - return /* reexport */ createRandomInt; - }); - __webpack_require__.d(__webpack_exports__, "createStirlingS2", function () { - return /* reexport */ createStirlingS2; - }); - __webpack_require__.d(__webpack_exports__, "createBellNumbers", function () { - return /* reexport */ createBellNumbers; - }); - __webpack_require__.d(__webpack_exports__, "createCatalan", function () { - return /* reexport */ createCatalan; - }); - __webpack_require__.d(__webpack_exports__, "createComposition", function () { - return /* reexport */ createComposition; - }); - __webpack_require__.d(__webpack_exports__, "createSimplify", function () { - return /* reexport */ createSimplify; - }); - __webpack_require__.d(__webpack_exports__, "createDerivative", function () { - return /* reexport */ createDerivative; - }); - __webpack_require__.d(__webpack_exports__, "createRationalize", function () { - return /* reexport */ createRationalize; - }); - __webpack_require__.d(__webpack_exports__, "createReviver", function () { - return /* reexport */ createReviver; - }); - __webpack_require__.d(__webpack_exports__, "createReplacer", function () { - return /* reexport */ createReplacer; - }); - __webpack_require__.d(__webpack_exports__, "createE", function () { - return /* reexport */ createE; - }); - __webpack_require__.d(__webpack_exports__, "createUppercaseE", function () { - return /* reexport */ createUppercaseE; - }); - __webpack_require__.d(__webpack_exports__, "createFalse", function () { - return /* reexport */ createFalse; - }); - __webpack_require__.d(__webpack_exports__, "createI", function () { - return /* reexport */ createI; - }); - __webpack_require__.d(__webpack_exports__, "createInfinity", function () { - return /* reexport */ createInfinity; - }); - __webpack_require__.d(__webpack_exports__, "createLN10", function () { - return /* reexport */ createLN10; - }); - __webpack_require__.d(__webpack_exports__, "createLN2", function () { - return /* reexport */ createLN2; - }); - __webpack_require__.d(__webpack_exports__, "createLOG10E", function () { - return /* reexport */ createLOG10E; - }); - __webpack_require__.d(__webpack_exports__, "createLOG2E", function () { - return /* reexport */ createLOG2E; - }); - __webpack_require__.d(__webpack_exports__, "createNaN", function () { - return /* reexport */ createNaN; - }); - __webpack_require__.d(__webpack_exports__, "createNull", function () { - return /* reexport */ createNull; - }); - __webpack_require__.d(__webpack_exports__, "createPhi", function () { - return /* reexport */ createPhi; - }); - __webpack_require__.d(__webpack_exports__, "createPi", function () { - return /* reexport */ createPi; - }); - __webpack_require__.d(__webpack_exports__, "createUppercasePi", function () { - return /* reexport */ createUppercasePi; - }); - __webpack_require__.d(__webpack_exports__, "createSQRT1_2", function () { - return /* reexport */ createSQRT1_2; - }); - __webpack_require__.d(__webpack_exports__, "createSQRT2", function () { - return /* reexport */ createSQRT2; - }); - __webpack_require__.d(__webpack_exports__, "createTau", function () { - return /* reexport */ createTau; - }); - __webpack_require__.d(__webpack_exports__, "createTrue", function () { - return /* reexport */ createTrue; - }); - __webpack_require__.d(__webpack_exports__, "createVersion", function () { - return /* reexport */ createVersion; - }); - __webpack_require__.d(__webpack_exports__, "createAtomicMass", function () { - return /* reexport */ createAtomicMass; - }); - __webpack_require__.d(__webpack_exports__, "createAvogadro", function () { - return /* reexport */ createAvogadro; - }); - __webpack_require__.d(__webpack_exports__, "createBohrMagneton", function () { - return /* reexport */ createBohrMagneton; - }); - __webpack_require__.d(__webpack_exports__, "createBohrRadius", function () { - return /* reexport */ createBohrRadius; - }); - __webpack_require__.d(__webpack_exports__, "createBoltzmann", function () { - return /* reexport */ createBoltzmann; - }); - __webpack_require__.d(__webpack_exports__, "createClassicalElectronRadius", function () { - return /* reexport */ createClassicalElectronRadius; - }); - __webpack_require__.d(__webpack_exports__, "createConductanceQuantum", function () { - return /* reexport */ createConductanceQuantum; - }); - __webpack_require__.d(__webpack_exports__, "createCoulomb", function () { - return /* reexport */ createCoulomb; - }); - __webpack_require__.d(__webpack_exports__, "createDeuteronMass", function () { - return /* reexport */ createDeuteronMass; - }); - __webpack_require__.d(__webpack_exports__, "createEfimovFactor", function () { - return /* reexport */ createEfimovFactor; - }); - __webpack_require__.d(__webpack_exports__, "createElectricConstant", function () { - return /* reexport */ createElectricConstant; - }); - __webpack_require__.d(__webpack_exports__, "createElectronMass", function () { - return /* reexport */ createElectronMass; - }); - __webpack_require__.d(__webpack_exports__, "createElementaryCharge", function () { - return /* reexport */ createElementaryCharge; - }); - __webpack_require__.d(__webpack_exports__, "createFaraday", function () { - return /* reexport */ createFaraday; - }); - __webpack_require__.d(__webpack_exports__, "createFermiCoupling", function () { - return /* reexport */ createFermiCoupling; - }); - __webpack_require__.d(__webpack_exports__, "createFineStructure", function () { - return /* reexport */ createFineStructure; - }); - __webpack_require__.d(__webpack_exports__, "createFirstRadiation", function () { - return /* reexport */ createFirstRadiation; - }); - __webpack_require__.d(__webpack_exports__, "createGasConstant", function () { - return /* reexport */ createGasConstant; - }); - __webpack_require__.d(__webpack_exports__, "createGravitationConstant", function () { - return /* reexport */ createGravitationConstant; - }); - __webpack_require__.d(__webpack_exports__, "createGravity", function () { - return /* reexport */ createGravity; - }); - __webpack_require__.d(__webpack_exports__, "createHartreeEnergy", function () { - return /* reexport */ createHartreeEnergy; - }); - __webpack_require__.d(__webpack_exports__, "createInverseConductanceQuantum", function () { - return /* reexport */ createInverseConductanceQuantum; - }); - __webpack_require__.d(__webpack_exports__, "createKlitzing", function () { - return /* reexport */ createKlitzing; - }); - __webpack_require__.d(__webpack_exports__, "createLoschmidt", function () { - return /* reexport */ createLoschmidt; - }); - __webpack_require__.d(__webpack_exports__, "createMagneticConstant", function () { - return /* reexport */ createMagneticConstant; - }); - __webpack_require__.d(__webpack_exports__, "createMagneticFluxQuantum", function () { - return /* reexport */ createMagneticFluxQuantum; - }); - __webpack_require__.d(__webpack_exports__, "createMolarMass", function () { - return /* reexport */ createMolarMass; - }); - __webpack_require__.d(__webpack_exports__, "createMolarMassC12", function () { - return /* reexport */ createMolarMassC12; - }); - __webpack_require__.d(__webpack_exports__, "createMolarPlanckConstant", function () { - return /* reexport */ createMolarPlanckConstant; - }); - __webpack_require__.d(__webpack_exports__, "createMolarVolume", function () { - return /* reexport */ createMolarVolume; - }); - __webpack_require__.d(__webpack_exports__, "createNeutronMass", function () { - return /* reexport */ createNeutronMass; - }); - __webpack_require__.d(__webpack_exports__, "createNuclearMagneton", function () { - return /* reexport */ createNuclearMagneton; - }); - __webpack_require__.d(__webpack_exports__, "createPlanckCharge", function () { - return /* reexport */ createPlanckCharge; - }); - __webpack_require__.d(__webpack_exports__, "createPlanckConstant", function () { - return /* reexport */ createPlanckConstant; - }); - __webpack_require__.d(__webpack_exports__, "createPlanckLength", function () { - return /* reexport */ createPlanckLength; - }); - __webpack_require__.d(__webpack_exports__, "createPlanckMass", function () { - return /* reexport */ createPlanckMass; - }); - __webpack_require__.d(__webpack_exports__, "createPlanckTemperature", function () { - return /* reexport */ createPlanckTemperature; - }); - __webpack_require__.d(__webpack_exports__, "createPlanckTime", function () { - return /* reexport */ createPlanckTime; - }); - __webpack_require__.d(__webpack_exports__, "createProtonMass", function () { - return /* reexport */ createProtonMass; - }); - __webpack_require__.d(__webpack_exports__, "createQuantumOfCirculation", function () { - return /* reexport */ createQuantumOfCirculation; - }); - __webpack_require__.d(__webpack_exports__, "createReducedPlanckConstant", function () { - return /* reexport */ createReducedPlanckConstant; - }); - __webpack_require__.d(__webpack_exports__, "createRydberg", function () { - return /* reexport */ createRydberg; - }); - __webpack_require__.d(__webpack_exports__, "createSackurTetrode", function () { - return /* reexport */ createSackurTetrode; - }); - __webpack_require__.d(__webpack_exports__, "createSecondRadiation", function () { - return /* reexport */ createSecondRadiation; - }); - __webpack_require__.d(__webpack_exports__, "createSpeedOfLight", function () { - return /* reexport */ createSpeedOfLight; - }); - __webpack_require__.d(__webpack_exports__, "createStefanBoltzmann", function () { - return /* reexport */ createStefanBoltzmann; - }); - __webpack_require__.d(__webpack_exports__, "createThomsonCrossSection", function () { - return /* reexport */ createThomsonCrossSection; - }); - __webpack_require__.d(__webpack_exports__, "createVacuumImpedance", function () { - return /* reexport */ createVacuumImpedance; - }); - __webpack_require__.d(__webpack_exports__, "createWeakMixingAngle", function () { - return /* reexport */ createWeakMixingAngle; - }); - __webpack_require__.d(__webpack_exports__, "createWienDisplacement", function () { - return /* reexport */ createWienDisplacement; - }); - __webpack_require__.d(__webpack_exports__, "createApplyTransform", function () { - return /* reexport */ createApplyTransform; - }); - __webpack_require__.d(__webpack_exports__, "createColumnTransform", function () { - return /* reexport */ createColumnTransform; - }); - __webpack_require__.d(__webpack_exports__, "createFilterTransform", function () { - return /* reexport */ createFilterTransform; - }); - __webpack_require__.d(__webpack_exports__, "createForEachTransform", function () { - return /* reexport */ createForEachTransform; - }); - __webpack_require__.d(__webpack_exports__, "createIndexTransform", function () { - return /* reexport */ createIndexTransform; - }); - __webpack_require__.d(__webpack_exports__, "createMapTransform", function () { - return /* reexport */ createMapTransform; - }); - __webpack_require__.d(__webpack_exports__, "createMaxTransform", function () { - return /* reexport */ createMaxTransform; - }); - __webpack_require__.d(__webpack_exports__, "createMeanTransform", function () { - return /* reexport */ createMeanTransform; - }); - __webpack_require__.d(__webpack_exports__, "createMinTransform", function () { - return /* reexport */ createMinTransform; - }); - __webpack_require__.d(__webpack_exports__, "createRangeTransform", function () { - return /* reexport */ createRangeTransform; - }); - __webpack_require__.d(__webpack_exports__, "createRowTransform", function () { - return /* reexport */ createRowTransform; - }); - __webpack_require__.d(__webpack_exports__, "createSubsetTransform", function () { - return /* reexport */ createSubsetTransform; - }); - __webpack_require__.d(__webpack_exports__, "createConcatTransform", function () { - return /* reexport */ createConcatTransform; - }); - __webpack_require__.d(__webpack_exports__, "createDiffTransform", function () { - return /* reexport */ createDiffTransform; - }); - __webpack_require__.d(__webpack_exports__, "createStdTransform", function () { - return /* reexport */ createStdTransform; - }); - __webpack_require__.d(__webpack_exports__, "createSumTransform", function () { - return /* reexport */ createSumTransform; - }); - __webpack_require__.d(__webpack_exports__, "createVarianceTransform", function () { - return /* reexport */ createVarianceTransform; - }); - - // EXTERNAL MODULE: ./src/utils/is.js - var is = __webpack_require__(1); - - // EXTERNAL MODULE: ./node_modules/typed-function/typed-function.js - var typed_function = __webpack_require__(14); - var typed_function_default = /*#__PURE__*/__webpack_require__.n(typed_function); - - // EXTERNAL MODULE: ./src/utils/number.js - var utils_number = __webpack_require__(4); - - // EXTERNAL MODULE: ./src/utils/factory.js - var factory = __webpack_require__(0); - - // CONCATENATED MODULE: ./src/core/function/typed.js - /** - * Create a typed-function which checks the types of the arguments and - * can match them against multiple provided signatures. The typed-function - * automatically converts inputs in order to find a matching signature. - * Typed functions throw informative errors in case of wrong input arguments. - * - * See the library [typed-function](https://github.com/josdejong/typed-function) - * for detailed documentation. - * - * Syntax: - * - * math.typed(name, signatures) : function - * math.typed(signatures) : function - * - * Examples: - * - * // create a typed function with multiple types per argument (type union) - * const fn2 = typed({ - * 'number | boolean': function (b) { - * return 'b is a number or boolean' - * }, - * 'string, number | boolean': function (a, b) { - * return 'a is a string, b is a number or boolean' - * } - * }) - * - * // create a typed function with an any type argument - * const log = typed({ - * 'string, any': function (event, data) { - * console.log('event: ' + event + ', data: ' + JSON.stringify(data)) - * } - * }) - * - * @param {string} [name] Optional name for the typed-function - * @param {Object} signatures Object with one or multiple function signatures - * @returns {function} The created typed-function. - */ - - // returns a new instance of typed-function - - var _createTyped2 = function _createTyped() { - // initially, return the original instance of typed-function - // consecutively, return a new instance from typed.create. - _createTyped2 = typed_function_default.a.create; - return typed_function_default.a; - }; - - var typed_dependencies = ['?BigNumber', '?Complex', '?DenseMatrix', '?Fraction']; - /** - * Factory function for creating a new typed instance - * @param {Object} dependencies Object with data types like Complex and BigNumber - * @returns {Function} - */ - - var typed_createTyped = /* #__PURE__ */Object(factory["a" /* factory */])('typed', typed_dependencies, function createTyped(_ref) { - var BigNumber = _ref.BigNumber, - Complex = _ref.Complex, - DenseMatrix = _ref.DenseMatrix, - Fraction = _ref.Fraction; - - // TODO: typed-function must be able to silently ignore signatures with unknown data types - // get a new instance of typed-function - var typed = _createTyped2(); // define all types. The order of the types determines in which order function - // arguments are type-checked (so for performance it's important to put the - // most used types first). - - typed.types = [{ - name: 'number', - test: is["y" /* isNumber */] - }, { - name: 'Complex', - test: is["j" /* isComplex */] - }, { - name: 'BigNumber', - test: is["e" /* isBigNumber */] - }, { - name: 'Fraction', - test: is["o" /* isFraction */] - }, { - name: 'Unit', - test: is["L" /* isUnit */] - }, { - name: 'string', - test: is["I" /* isString */] - }, { - name: 'Chain', - test: is["h" /* isChain */] - }, { - name: 'Array', - test: is["b" /* isArray */] - }, { - name: 'Matrix', - test: is["v" /* isMatrix */] - }, { - name: 'DenseMatrix', - test: is["n" /* isDenseMatrix */] - }, { - name: 'SparseMatrix', - test: is["H" /* isSparseMatrix */] - }, { - name: 'Range', - test: is["D" /* isRange */] - }, { - name: 'Index', - test: is["t" /* isIndex */] - }, { - name: 'boolean', - test: is["g" /* isBoolean */] - }, { - name: 'ResultSet', - test: is["G" /* isResultSet */] - }, { - name: 'Help', - test: is["s" /* isHelp */] - }, { - name: 'function', - test: is["p" /* isFunction */] - }, { - name: 'Date', - test: is["m" /* isDate */] - }, { - name: 'RegExp', - test: is["F" /* isRegExp */] - }, { - name: 'null', - test: is["x" /* isNull */] - }, { - name: 'undefined', - test: is["K" /* isUndefined */] - }, { - name: 'AccessorNode', - test: is["a" /* isAccessorNode */] - }, { - name: 'ArrayNode', - test: is["c" /* isArrayNode */] - }, { - name: 'AssignmentNode', - test: is["d" /* isAssignmentNode */] - }, { - name: 'BlockNode', - test: is["f" /* isBlockNode */] - }, { - name: 'ConditionalNode', - test: is["k" /* isConditionalNode */] - }, { - name: 'ConstantNode', - test: is["l" /* isConstantNode */] - }, { - name: 'FunctionNode', - test: is["r" /* isFunctionNode */] - }, { - name: 'FunctionAssignmentNode', - test: is["q" /* isFunctionAssignmentNode */] - }, { - name: 'IndexNode', - test: is["u" /* isIndexNode */] - }, { - name: 'Node', - test: is["w" /* isNode */] - }, { - name: 'ObjectNode', - test: is["A" /* isObjectNode */] - }, { - name: 'OperatorNode', - test: is["B" /* isOperatorNode */] - }, { - name: 'ParenthesisNode', - test: is["C" /* isParenthesisNode */] - }, { - name: 'RangeNode', - test: is["E" /* isRangeNode */] - }, { - name: 'SymbolNode', - test: is["J" /* isSymbolNode */] - }, { - name: 'Object', - test: is["z" /* isObject */] - } // order 'Object' last, it matches on other classes too - ]; - typed.conversions = [{ - from: 'number', - to: 'BigNumber', - convert: function convert(x) { - if (!BigNumber) { - throwNoBignumber(x); - } // note: conversion from number to BigNumber can fail if x has >15 digits - - if (Object(utils_number["f" /* digits */])(x) > 15) { - throw new TypeError('Cannot implicitly convert a number with >15 significant digits to BigNumber ' + '(value: ' + x + '). ' + 'Use function bignumber(x) to convert to BigNumber.'); - } - - return new BigNumber(x); - } - }, { - from: 'number', - to: 'Complex', - convert: function convert(x) { - if (!Complex) { - throwNoComplex(x); - } - - return new Complex(x, 0); - } - }, { - from: 'number', - to: 'string', - convert: function convert(x) { - return x + ''; - } - }, { - from: 'BigNumber', - to: 'Complex', - convert: function convert(x) { - if (!Complex) { - throwNoComplex(x); - } - - return new Complex(x.toNumber(), 0); - } - }, { - from: 'Fraction', - to: 'BigNumber', - convert: function convert(x) { - throw new TypeError('Cannot implicitly convert a Fraction to BigNumber or vice versa. ' + 'Use function bignumber(x) to convert to BigNumber or fraction(x) to convert to Fraction.'); - } - }, { - from: 'Fraction', - to: 'Complex', - convert: function convert(x) { - if (!Complex) { - throwNoComplex(x); - } - - return new Complex(x.valueOf(), 0); - } - }, { - from: 'number', - to: 'Fraction', - convert: function convert(x) { - if (!Fraction) { - throwNoFraction(x); - } - - var f = new Fraction(x); - - if (f.valueOf() !== x) { - throw new TypeError('Cannot implicitly convert a number to a Fraction when there will be a loss of precision ' + '(value: ' + x + '). ' + 'Use function fraction(x) to convert to Fraction.'); - } - - return f; - } - }, { - // FIXME: add conversion from Fraction to number, for example for `sqrt(fraction(1,3))` - // from: 'Fraction', - // to: 'number', - // convert: function (x) { - // return x.valueOf() - // } - // }, { - from: 'string', - to: 'number', - convert: function convert(x) { - var n = Number(x); - - if (isNaN(n)) { - throw new Error('Cannot convert "' + x + '" to a number'); - } - - return n; - } - }, { - from: 'string', - to: 'BigNumber', - convert: function convert(x) { - if (!BigNumber) { - throwNoBignumber(x); - } - - try { - return new BigNumber(x); - } catch (err) { - throw new Error('Cannot convert "' + x + '" to BigNumber'); - } - } - }, { - from: 'string', - to: 'Fraction', - convert: function convert(x) { - if (!Fraction) { - throwNoFraction(x); - } - - try { - return new Fraction(x); - } catch (err) { - throw new Error('Cannot convert "' + x + '" to Fraction'); - } - } - }, { - from: 'string', - to: 'Complex', - convert: function convert(x) { - if (!Complex) { - throwNoComplex(x); - } - - try { - return new Complex(x); - } catch (err) { - throw new Error('Cannot convert "' + x + '" to Complex'); - } - } - }, { - from: 'boolean', - to: 'number', - convert: function convert(x) { - return +x; - } - }, { - from: 'boolean', - to: 'BigNumber', - convert: function convert(x) { - if (!BigNumber) { - throwNoBignumber(x); - } - - return new BigNumber(+x); - } - }, { - from: 'boolean', - to: 'Fraction', - convert: function convert(x) { - if (!Fraction) { - throwNoFraction(x); - } - - return new Fraction(+x); - } - }, { - from: 'boolean', - to: 'string', - convert: function convert(x) { - return String(x); - } - }, { - from: 'Array', - to: 'Matrix', - convert: function convert(array) { - if (!DenseMatrix) { - throwNoMatrix(); - } - - return new DenseMatrix(array); - } - }, { - from: 'Matrix', - to: 'Array', - convert: function convert(matrix) { - return matrix.valueOf(); - } - }]; - return typed; - }); - - function throwNoBignumber(x) { - throw new Error("Cannot convert value ".concat(x, " into a BigNumber: no class 'BigNumber' provided")); - } - - function throwNoComplex(x) { - throw new Error("Cannot convert value ".concat(x, " into a Complex number: no class 'Complex' provided")); - } - - function throwNoMatrix() { - throw new Error('Cannot convert array into a Matrix: no class \'DenseMatrix\' provided'); - } - - function throwNoFraction(x) { - throw new Error("Cannot convert value ".concat(x, " into a Fraction, no class 'Fraction' provided.")); - } - // CONCATENATED MODULE: ./src/type/resultset/ResultSet.js - - var ResultSet_name = 'ResultSet'; - var ResultSet_dependencies = []; - var createResultSet = /* #__PURE__ */Object(factory["a" /* factory */])(ResultSet_name, ResultSet_dependencies, function () { - /** - * A ResultSet contains a list or results - * @class ResultSet - * @param {Array} entries - * @constructor ResultSet - */ - function ResultSet(entries) { - if (!(this instanceof ResultSet)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this.entries = entries || []; - } - /** - * Attach type information - */ - - ResultSet.prototype.type = 'ResultSet'; - ResultSet.prototype.isResultSet = true; - /** - * Returns the array with results hold by this ResultSet - * @memberof ResultSet - * @returns {Array} entries - */ - - ResultSet.prototype.valueOf = function () { - return this.entries; - }; - /** - * Returns the stringified results of the ResultSet - * @memberof ResultSet - * @returns {string} string - */ - - ResultSet.prototype.toString = function () { - return '[' + this.entries.join(', ') + ']'; - }; - /** - * Get a JSON representation of the ResultSet - * @memberof ResultSet - * @returns {Object} Returns a JSON object structured as: - * `{"mathjs": "ResultSet", "entries": [...]}` - */ - - ResultSet.prototype.toJSON = function () { - return { - mathjs: 'ResultSet', - entries: this.entries - }; - }; - /** - * Instantiate a ResultSet from a JSON object - * @memberof ResultSet - * @param {Object} json A JSON object structured as: - * `{"mathjs": "ResultSet", "entries": [...]}` - * @return {ResultSet} - */ - - ResultSet.fromJSON = function (json) { - return new ResultSet(json.entries); - }; - - return ResultSet; - }, { - isClass: true - }); - // EXTERNAL MODULE: ./node_modules/decimal.js/decimal.js - var decimal = __webpack_require__(12); - var decimal_default = /*#__PURE__*/__webpack_require__.n(decimal); - - // CONCATENATED MODULE: ./src/type/bignumber/BigNumber.js - - var BigNumber_name = 'BigNumber'; - var BigNumber_dependencies = ['?on', 'config']; - var createBigNumberClass = /* #__PURE__ */Object(factory["a" /* factory */])(BigNumber_name, BigNumber_dependencies, function (_ref) { - var on = _ref.on, - config = _ref.config; - var EUCLID = 9; // Use euclidian division for mod calculation - - var BigNumber = decimal_default.a.clone({ - precision: config.precision, - modulo: EUCLID - }); - /** - * Attach type information - */ - - BigNumber.prototype.type = 'BigNumber'; - BigNumber.prototype.isBigNumber = true; - /** - * Get a JSON representation of a BigNumber containing - * type information - * @returns {Object} Returns a JSON object structured as: - * `{"mathjs": "BigNumber", "value": "0.2"}` - */ - - BigNumber.prototype.toJSON = function () { - return { - mathjs: 'BigNumber', - value: this.toString() - }; - }; - /** - * Instantiate a BigNumber from a JSON object - * @param {Object} json a JSON object structured as: - * `{"mathjs": "BigNumber", "value": "0.2"}` - * @return {BigNumber} - */ - - BigNumber.fromJSON = function (json) { - return new BigNumber(json.value); - }; - - if (on) { - // listen for changed in the configuration, automatically apply changed precision - on('config', function (curr, prev) { - if (curr.precision !== prev.precision) { - BigNumber.config({ - precision: curr.precision - }); - } - }); - } - - return BigNumber; - }, { - isClass: true - }); - // EXTERNAL MODULE: ./node_modules/complex.js/complex.js - var complex_js_complex = __webpack_require__(8); - var complex_default = /*#__PURE__*/__webpack_require__.n(complex_js_complex); - - // CONCATENATED MODULE: ./src/type/complex/Complex.js - function _typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - _typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - _typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return _typeof(obj); - } - - var Complex_name = 'Complex'; - var Complex_dependencies = []; - var createComplexClass = /* #__PURE__ */Object(factory["a" /* factory */])(Complex_name, Complex_dependencies, function () { - /** - * Attach type information - */ - complex_default.a.prototype.type = 'Complex'; - complex_default.a.prototype.isComplex = true; - /** - * Get a JSON representation of the complex number - * @returns {Object} Returns a JSON object structured as: - * `{"mathjs": "Complex", "re": 2, "im": 3}` - */ - - complex_default.a.prototype.toJSON = function () { - return { - mathjs: 'Complex', - re: this.re, - im: this.im - }; - }; - /* - * Return the value of the complex number in polar notation - * The angle phi will be set in the interval of [-pi, pi]. - * @return {{r: number, phi: number}} Returns and object with properties r and phi. - */ - - complex_default.a.prototype.toPolar = function () { - return { - r: this.abs(), - phi: this.arg() - }; - }; - /** - * Get a string representation of the complex number, - * with optional formatting options. - * @param {Object | number | Function} [options] Formatting options. See - * lib/utils/number:format for a - * description of the available - * options. - * @return {string} str - */ - - complex_default.a.prototype.format = function (options) { - var str = ''; - var im = this.im; - var re = this.re; - var strRe = Object(utils_number["h" /* format */])(this.re, options); - var strIm = Object(utils_number["h" /* format */])(this.im, options); // round either re or im when smaller than the configured precision - - var precision = Object(is["y" /* isNumber */])(options) ? options : options ? options.precision : null; - - if (precision !== null) { - var epsilon = Math.pow(10, -precision); - - if (Math.abs(re / im) < epsilon) { - re = 0; - } - - if (Math.abs(im / re) < epsilon) { - im = 0; - } - } - - if (im === 0) { - // real value - str = strRe; - } else if (re === 0) { - // purely complex value - if (im === 1) { - str = 'i'; - } else if (im === -1) { - str = '-i'; - } else { - str = strIm + 'i'; - } - } else { - // complex value - if (im < 0) { - if (im === -1) { - str = strRe + ' - i'; - } else { - str = strRe + ' - ' + strIm.substring(1) + 'i'; - } - } else { - if (im === 1) { - str = strRe + ' + i'; - } else { - str = strRe + ' + ' + strIm + 'i'; - } - } - } - - return str; - }; - /** - * Create a complex number from polar coordinates - * - * Usage: - * - * Complex.fromPolar(r: number, phi: number) : Complex - * Complex.fromPolar({r: number, phi: number}) : Complex - * - * @param {*} args... - * @return {Complex} - */ - - complex_default.a.fromPolar = function (args) { - switch (arguments.length) { - case 1: - { - var arg = arguments[0]; - - if (_typeof(arg) === 'object') { - return complex_default()(arg); - } else { - throw new TypeError('Input has to be an object with r and phi keys.'); - } - } - - case 2: - { - var r = arguments[0]; - var phi = arguments[1]; - - if (Object(is["y" /* isNumber */])(r)) { - if (Object(is["L" /* isUnit */])(phi) && phi.hasBase('ANGLE')) { - // convert unit to a number in radians - phi = phi.toNumber('rad'); - } - - if (Object(is["y" /* isNumber */])(phi)) { - return new complex_default.a({ - r: r, - phi: phi - }); - } - - throw new TypeError('Phi is not a number nor an angle unit.'); - } else { - throw new TypeError('Radius r is not a number.'); - } - } - - default: - throw new SyntaxError('Wrong number of arguments in function fromPolar'); - } - }; - - complex_default.a.prototype.valueOf = complex_default.a.prototype.toString; - /** - * Create a Complex number from a JSON object - * @param {Object} json A JSON Object structured as - * {"mathjs": "Complex", "re": 2, "im": 3} - * All properties are optional, default values - * for `re` and `im` are 0. - * @return {Complex} Returns a new Complex number - */ - - complex_default.a.fromJSON = function (json) { - return new complex_default.a(json); - }; - /** - * Compare two complex numbers, `a` and `b`: - * - * - Returns 1 when the real part of `a` is larger than the real part of `b` - * - Returns -1 when the real part of `a` is smaller than the real part of `b` - * - Returns 1 when the real parts are equal - * and the imaginary part of `a` is larger than the imaginary part of `b` - * - Returns -1 when the real parts are equal - * and the imaginary part of `a` is smaller than the imaginary part of `b` - * - Returns 0 when both real and imaginary parts are equal. - * - * @params {Complex} a - * @params {Complex} b - * @returns {number} Returns the comparison result: -1, 0, or 1 - */ - - complex_default.a.compare = function (a, b) { - if (a.re > b.re) { - return 1; - } - - if (a.re < b.re) { - return -1; - } - - if (a.im > b.im) { - return 1; - } - - if (a.im < b.im) { - return -1; - } - - return 0; - }; - - return complex_default.a; - }, { - isClass: true - }); - // EXTERNAL MODULE: ./node_modules/fraction.js/fraction.js - var fraction_js_fraction = __webpack_require__(10); - var fraction_default = /*#__PURE__*/__webpack_require__.n(fraction_js_fraction); - - // CONCATENATED MODULE: ./src/type/fraction/Fraction.js - - var Fraction_name = 'Fraction'; - var Fraction_dependencies = []; - var createFractionClass = /* #__PURE__ */Object(factory["a" /* factory */])(Fraction_name, Fraction_dependencies, function () { - /** - * Attach type information - */ - fraction_default.a.prototype.type = 'Fraction'; - fraction_default.a.prototype.isFraction = true; - /** - * Get a JSON representation of a Fraction containing type information - * @returns {Object} Returns a JSON object structured as: - * `{"mathjs": "Fraction", "n": 3, "d": 8}` - */ - - fraction_default.a.prototype.toJSON = function () { - return { - mathjs: 'Fraction', - n: this.s * this.n, - d: this.d - }; - }; - /** - * Instantiate a Fraction from a JSON object - * @param {Object} json a JSON object structured as: - * `{"mathjs": "Fraction", "n": 3, "d": 8}` - * @return {BigNumber} - */ - - fraction_default.a.fromJSON = function (json) { - return new fraction_default.a(json); - }; - - return fraction_default.a; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/type/matrix/Range.js - - var Range_name = 'Range'; - var Range_dependencies = []; - var createRangeClass = /* #__PURE__ */Object(factory["a" /* factory */])(Range_name, Range_dependencies, function () { - /** - * Create a range. A range has a start, step, and end, and contains functions - * to iterate over the range. - * - * A range can be constructed as: - * - * const range = new Range(start, end) - * const range = new Range(start, end, step) - * - * To get the result of the range: - * range.forEach(function (x) { - * console.log(x) - * }) - * range.map(function (x) { - * return math.sin(x) - * }) - * range.toArray() - * - * Example usage: - * - * const c = new Range(2, 6) // 2:1:5 - * c.toArray() // [2, 3, 4, 5] - * const d = new Range(2, -3, -1) // 2:-1:-2 - * d.toArray() // [2, 1, 0, -1, -2] - * - * @class Range - * @constructor Range - * @param {number} start included lower bound - * @param {number} end excluded upper bound - * @param {number} [step] step size, default value is 1 - */ - function Range(start, end, step) { - if (!(this instanceof Range)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - var hasStart = start !== null && start !== undefined; - var hasEnd = end !== null && end !== undefined; - var hasStep = step !== null && step !== undefined; - - if (hasStart) { - if (Object(is["e" /* isBigNumber */])(start)) { - start = start.toNumber(); - } else if (typeof start !== 'number') { - throw new TypeError('Parameter start must be a number'); - } - } - - if (hasEnd) { - if (Object(is["e" /* isBigNumber */])(end)) { - end = end.toNumber(); - } else if (typeof end !== 'number') { - throw new TypeError('Parameter end must be a number'); - } - } - - if (hasStep) { - if (Object(is["e" /* isBigNumber */])(step)) { - step = step.toNumber(); - } else if (typeof step !== 'number') { - throw new TypeError('Parameter step must be a number'); - } - } - - this.start = hasStart ? parseFloat(start) : 0; - this.end = hasEnd ? parseFloat(end) : 0; - this.step = hasStep ? parseFloat(step) : 1; - } - /** - * Attach type information - */ - - Range.prototype.type = 'Range'; - Range.prototype.isRange = true; - /** - * Parse a string into a range, - * The string contains the start, optional step, and end, separated by a colon. - * If the string does not contain a valid range, null is returned. - * For example str='0:2:11'. - * @memberof Range - * @param {string} str - * @return {Range | null} range - */ - - Range.parse = function (str) { - if (typeof str !== 'string') { - return null; - } - - var args = str.split(':'); - var nums = args.map(function (arg) { - return parseFloat(arg); - }); - var invalid = nums.some(function (num) { - return isNaN(num); - }); - - if (invalid) { - return null; - } - - switch (nums.length) { - case 2: - return new Range(nums[0], nums[1]); - - case 3: - return new Range(nums[0], nums[2], nums[1]); - - default: - return null; - } - }; - /** - * Create a clone of the range - * @return {Range} clone - */ - - Range.prototype.clone = function () { - return new Range(this.start, this.end, this.step); - }; - /** - * Retrieve the size of the range. - * Returns an array containing one number, the number of elements in the range. - * @memberof Range - * @returns {number[]} size - */ - - Range.prototype.size = function () { - var len = 0; - var start = this.start; - var step = this.step; - var end = this.end; - var diff = end - start; - - if (Object(utils_number["n" /* sign */])(step) === Object(utils_number["n" /* sign */])(diff)) { - len = Math.ceil(diff / step); - } else if (diff === 0) { - len = 0; - } - - if (isNaN(len)) { - len = 0; - } - - return [len]; - }; - /** - * Calculate the minimum value in the range - * @memberof Range - * @return {number | undefined} min - */ - - Range.prototype.min = function () { - var size = this.size()[0]; - - if (size > 0) { - if (this.step > 0) { - // positive step - return this.start; - } else { - // negative step - return this.start + (size - 1) * this.step; - } - } else { - return undefined; - } - }; - /** - * Calculate the maximum value in the range - * @memberof Range - * @return {number | undefined} max - */ - - Range.prototype.max = function () { - var size = this.size()[0]; - - if (size > 0) { - if (this.step > 0) { - // positive step - return this.start + (size - 1) * this.step; - } else { - // negative step - return this.start; - } - } else { - return undefined; - } - }; - /** - * Execute a callback function for each value in the range. - * @memberof Range - * @param {function} callback The callback method is invoked with three - * parameters: the value of the element, the index - * of the element, and the Range being traversed. - */ - - Range.prototype.forEach = function (callback) { - var x = this.start; - var step = this.step; - var end = this.end; - var i = 0; - - if (step > 0) { - while (x < end) { - callback(x, [i], this); - x += step; - i++; - } - } else if (step < 0) { - while (x > end) { - callback(x, [i], this); - x += step; - i++; - } - } - }; - /** - * Execute a callback function for each value in the Range, and return the - * results as an array - * @memberof Range - * @param {function} callback The callback method is invoked with three - * parameters: the value of the element, the index - * of the element, and the Matrix being traversed. - * @returns {Array} array - */ - - Range.prototype.map = function (callback) { - var array = []; - this.forEach(function (value, index, obj) { - array[index[0]] = callback(value, index, obj); - }); - return array; - }; - /** - * Create an Array with a copy of the Ranges data - * @memberof Range - * @returns {Array} array - */ - - Range.prototype.toArray = function () { - var array = []; - this.forEach(function (value, index) { - array[index[0]] = value; - }); - return array; - }; - /** - * Get the primitive value of the Range, a one dimensional array - * @memberof Range - * @returns {Array} array - */ - - Range.prototype.valueOf = function () { - // TODO: implement a caching mechanism for range.valueOf() - return this.toArray(); - }; - /** - * Get a string representation of the range, with optional formatting options. - * Output is formatted as 'start:step:end', for example '2:6' or '0:0.2:11' - * @memberof Range - * @param {Object | number | function} [options] Formatting options. See - * lib/utils/number:format for a - * description of the available - * options. - * @returns {string} str - */ - - Range.prototype.format = function (options) { - var str = Object(utils_number["h" /* format */])(this.start, options); - - if (this.step !== 1) { - str += ':' + Object(utils_number["h" /* format */])(this.step, options); - } - - str += ':' + Object(utils_number["h" /* format */])(this.end, options); - return str; - }; - /** - * Get a string representation of the range. - * @memberof Range - * @returns {string} - */ - - Range.prototype.toString = function () { - return this.format(); - }; - /** - * Get a JSON representation of the range - * @memberof Range - * @returns {Object} Returns a JSON object structured as: - * `{"mathjs": "Range", "start": 2, "end": 4, "step": 1}` - */ - - Range.prototype.toJSON = function () { - return { - mathjs: 'Range', - start: this.start, - end: this.end, - step: this.step - }; - }; - /** - * Instantiate a Range from a JSON object - * @memberof Range - * @param {Object} json A JSON object structured as: - * `{"mathjs": "Range", "start": 2, "end": 4, "step": 1}` - * @return {Range} - */ - - Range.fromJSON = function (json) { - return new Range(json.start, json.end, json.step); - }; - - return Range; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/type/matrix/Matrix.js - - var Matrix_name = 'Matrix'; - var Matrix_dependencies = []; - var createMatrixClass = /* #__PURE__ */Object(factory["a" /* factory */])(Matrix_name, Matrix_dependencies, function () { - /** - * @constructor Matrix - * - * A Matrix is a wrapper around an Array. A matrix can hold a multi dimensional - * array. A matrix can be constructed as: - * - * let matrix = math.matrix(data) - * - * Matrix contains the functions to resize, get and set values, get the size, - * clone the matrix and to convert the matrix to a vector, array, or scalar. - * Furthermore, one can iterate over the matrix using map and forEach. - * The internal Array of the Matrix can be accessed using the function valueOf. - * - * Example usage: - * - * let matrix = math.matrix([[1, 2], [3, 4]]) - * matix.size() // [2, 2] - * matrix.resize([3, 2], 5) - * matrix.valueOf() // [[1, 2], [3, 4], [5, 5]] - * matrix.subset([1,2]) // 3 (indexes are zero-based) - * - */ - function Matrix() { - if (!(this instanceof Matrix)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - } - /** - * Attach type information - */ - - Matrix.prototype.type = 'Matrix'; - Matrix.prototype.isMatrix = true; - /** - * Get the storage format used by the matrix. - * - * Usage: - * const format = matrix.storage() // retrieve storage format - * - * @return {string} The storage format. - */ - - Matrix.prototype.storage = function () { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke storage on a Matrix interface'); - }; - /** - * Get the datatype of the data stored in the matrix. - * - * Usage: - * const format = matrix.datatype() // retrieve matrix datatype - * - * @return {string} The datatype. - */ - - Matrix.prototype.datatype = function () { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke datatype on a Matrix interface'); - }; - /** - * Create a new Matrix With the type of the current matrix instance - * @param {Array | Object} data - * @param {string} [datatype] - */ - - Matrix.prototype.create = function (data, datatype) { - throw new Error('Cannot invoke create on a Matrix interface'); - }; - /** - * Get a subset of the matrix, or replace a subset of the matrix. - * - * Usage: - * const subset = matrix.subset(index) // retrieve subset - * const value = matrix.subset(index, replacement) // replace subset - * - * @param {Index} index - * @param {Array | Matrix | *} [replacement] - * @param {*} [defaultValue=0] Default value, filled in on new entries when - * the matrix is resized. If not provided, - * new matrix elements will be filled with zeros. - */ - - Matrix.prototype.subset = function (index, replacement, defaultValue) { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke subset on a Matrix interface'); - }; - /** - * Get a single element from the matrix. - * @param {number[]} index Zero-based index - * @return {*} value - */ - - Matrix.prototype.get = function (index) { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke get on a Matrix interface'); - }; - /** - * Replace a single element in the matrix. - * @param {number[]} index Zero-based index - * @param {*} value - * @param {*} [defaultValue] Default value, filled in on new entries when - * the matrix is resized. If not provided, - * new matrix elements will be left undefined. - * @return {Matrix} self - */ - - Matrix.prototype.set = function (index, value, defaultValue) { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke set on a Matrix interface'); - }; - /** - * Resize the matrix to the given size. Returns a copy of the matrix when - * `copy=true`, otherwise return the matrix itself (resize in place). - * - * @param {number[]} size The new size the matrix should have. - * @param {*} [defaultValue=0] Default value, filled in on new entries. - * If not provided, the matrix elements will - * be filled with zeros. - * @param {boolean} [copy] Return a resized copy of the matrix - * - * @return {Matrix} The resized matrix - */ - - Matrix.prototype.resize = function (size, defaultValue) { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke resize on a Matrix interface'); - }; - /** - * Reshape the matrix to the given size. Returns a copy of the matrix when - * `copy=true`, otherwise return the matrix itself (reshape in place). - * - * @param {number[]} size The new size the matrix should have. - * @param {boolean} [copy] Return a reshaped copy of the matrix - * - * @return {Matrix} The reshaped matrix - */ - - Matrix.prototype.reshape = function (size, defaultValue) { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke reshape on a Matrix interface'); - }; - /** - * Create a clone of the matrix - * @return {Matrix} clone - */ - - Matrix.prototype.clone = function () { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke clone on a Matrix interface'); - }; - /** - * Retrieve the size of the matrix. - * @returns {number[]} size - */ - - Matrix.prototype.size = function () { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke size on a Matrix interface'); - }; - /** - * Create a new matrix with the results of the callback function executed on - * each entry of the matrix. - * @param {Function} callback The callback function is invoked with three - * parameters: the value of the element, the index - * of the element, and the Matrix being traversed. - * @param {boolean} [skipZeros] Invoke callback function for non-zero values only. - * - * @return {Matrix} matrix - */ - - Matrix.prototype.map = function (callback, skipZeros) { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke map on a Matrix interface'); - }; - /** - * Execute a callback function on each entry of the matrix. - * @param {Function} callback The callback function is invoked with three - * parameters: the value of the element, the index - * of the element, and the Matrix being traversed. - */ - - Matrix.prototype.forEach = function (callback) { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke forEach on a Matrix interface'); - }; - /** - * Create an Array with a copy of the data of the Matrix - * @returns {Array} array - */ - - Matrix.prototype.toArray = function () { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke toArray on a Matrix interface'); - }; - /** - * Get the primitive value of the Matrix: a multidimensional array - * @returns {Array} array - */ - - Matrix.prototype.valueOf = function () { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke valueOf on a Matrix interface'); - }; - /** - * Get a string representation of the matrix, with optional formatting options. - * @param {Object | number | Function} [options] Formatting options. See - * lib/utils/number:format for a - * description of the available - * options. - * @returns {string} str - */ - - Matrix.prototype.format = function (options) { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke format on a Matrix interface'); - }; - /** - * Get a string representation of the matrix - * @returns {string} str - */ - - Matrix.prototype.toString = function () { - // must be implemented by each of the Matrix implementations - throw new Error('Cannot invoke toString on a Matrix interface'); - }; - - return Matrix; - }, { - isClass: true - }); - // EXTERNAL MODULE: ./src/utils/array.js - var utils_array = __webpack_require__(2); - - // EXTERNAL MODULE: ./src/utils/string.js + 1 modules - var utils_string = __webpack_require__(5); - - // EXTERNAL MODULE: ./src/utils/object.js - var utils_object = __webpack_require__(3); - - // EXTERNAL MODULE: ./src/error/DimensionError.js - var DimensionError = __webpack_require__(6); - - // CONCATENATED MODULE: ./src/type/matrix/DenseMatrix.js - - var DenseMatrix_name = 'DenseMatrix'; - var DenseMatrix_dependencies = ['Matrix']; - var createDenseMatrixClass = /* #__PURE__ */Object(factory["a" /* factory */])(DenseMatrix_name, DenseMatrix_dependencies, function (_ref) { - var Matrix = _ref.Matrix; - - /** - * Dense Matrix implementation. A regular, dense matrix, supporting multi-dimensional matrices. This is the default matrix type. - * @class DenseMatrix - */ - function DenseMatrix(data, datatype) { - if (!(this instanceof DenseMatrix)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - if (datatype && !Object(is["I" /* isString */])(datatype)) { - throw new Error('Invalid datatype: ' + datatype); - } - - if (Object(is["v" /* isMatrix */])(data)) { - // check data is a DenseMatrix - if (data.type === 'DenseMatrix') { - // clone data & size - this._data = Object(utils_object["a" /* clone */])(data._data); - this._size = Object(utils_object["a" /* clone */])(data._size); - this._datatype = datatype || data._datatype; - } else { - // build data from existing matrix - this._data = data.toArray(); - this._size = data.size(); - this._datatype = datatype || data._datatype; - } - } else if (data && Object(is["b" /* isArray */])(data.data) && Object(is["b" /* isArray */])(data.size)) { - // initialize fields from JSON representation - this._data = data.data; - this._size = data.size; // verify the dimensions of the array - - Object(utils_array["p" /* validate */])(this._data, this._size); - this._datatype = datatype || data.datatype; - } else if (Object(is["b" /* isArray */])(data)) { - // replace nested Matrices with Arrays - this._data = preprocess(data); // get the dimensions of the array - - this._size = Object(utils_array["a" /* arraySize */])(this._data); // verify the dimensions of the array, TODO: compute size while processing array - - Object(utils_array["p" /* validate */])(this._data, this._size); // data type unknown - - this._datatype = datatype; - } else if (data) { - // unsupported type - throw new TypeError('Unsupported type of data (' + Object(is["M" /* typeOf */])(data) + ')'); - } else { - // nothing provided - this._data = []; - this._size = [0]; - this._datatype = datatype; - } - } - - DenseMatrix.prototype = new Matrix(); - /** - * Create a new DenseMatrix - */ - - DenseMatrix.prototype.createDenseMatrix = function (data, datatype) { - return new DenseMatrix(data, datatype); - }; - /** - * Attach type information - */ - - DenseMatrix.prototype.type = 'DenseMatrix'; - DenseMatrix.prototype.isDenseMatrix = true; - /** - * Get the matrix type - * - * Usage: - * const matrixType = matrix.getDataType() // retrieves the matrix type - * - * @memberOf DenseMatrix - * @return {string} type information; if multiple types are found from the Matrix, it will return "mixed" - */ - - DenseMatrix.prototype.getDataType = function () { - return Object(utils_array["h" /* getArrayDataType */])(this._data, is["M" /* typeOf */]); - }; - /** - * Get the storage format used by the matrix. - * - * Usage: - * const format = matrix.storage() // retrieve storage format - * - * @memberof DenseMatrix - * @return {string} The storage format. - */ - - DenseMatrix.prototype.storage = function () { - return 'dense'; - }; - /** - * Get the datatype of the data stored in the matrix. - * - * Usage: - * const format = matrix.datatype() // retrieve matrix datatype - * - * @memberof DenseMatrix - * @return {string} The datatype. - */ - - DenseMatrix.prototype.datatype = function () { - return this._datatype; - }; - /** - * Create a new DenseMatrix - * @memberof DenseMatrix - * @param {Array} data - * @param {string} [datatype] - */ - - DenseMatrix.prototype.create = function (data, datatype) { - return new DenseMatrix(data, datatype); - }; - /** - * Get a subset of the matrix, or replace a subset of the matrix. - * - * Usage: - * const subset = matrix.subset(index) // retrieve subset - * const value = matrix.subset(index, replacement) // replace subset - * - * @memberof DenseMatrix - * @param {Index} index - * @param {Array | Matrix | *} [replacement] - * @param {*} [defaultValue=0] Default value, filled in on new entries when - * the matrix is resized. If not provided, - * new matrix elements will be filled with zeros. - */ - - DenseMatrix.prototype.subset = function (index, replacement, defaultValue) { - switch (arguments.length) { - case 1: - return _get(this, index); - // intentional fall through - - case 2: - case 3: - return _set(this, index, replacement, defaultValue); - - default: - throw new SyntaxError('Wrong number of arguments'); - } - }; - /** - * Get a single element from the matrix. - * @memberof DenseMatrix - * @param {number[]} index Zero-based index - * @return {*} value - */ - - DenseMatrix.prototype.get = function (index) { - if (!Object(is["b" /* isArray */])(index)) { - throw new TypeError('Array expected'); - } - - if (index.length !== this._size.length) { - throw new DimensionError["a" /* DimensionError */](index.length, this._size.length); - } // check index - - for (var x = 0; x < index.length; x++) { - Object(utils_array["q" /* validateIndex */])(index[x], this._size[x]); - } - - var data = this._data; - - for (var i = 0, ii = index.length; i < ii; i++) { - var indexI = index[i]; - Object(utils_array["q" /* validateIndex */])(indexI, data.length); - data = data[indexI]; - } - - return data; - }; - /** - * Replace a single element in the matrix. - * @memberof DenseMatrix - * @param {number[]} index Zero-based index - * @param {*} value - * @param {*} [defaultValue] Default value, filled in on new entries when - * the matrix is resized. If not provided, - * new matrix elements will be left undefined. - * @return {DenseMatrix} self - */ - - DenseMatrix.prototype.set = function (index, value, defaultValue) { - if (!Object(is["b" /* isArray */])(index)) { - throw new TypeError('Array expected'); - } - - if (index.length < this._size.length) { - throw new DimensionError["a" /* DimensionError */](index.length, this._size.length, '<'); - } - - var i, ii, indexI; // enlarge matrix when needed - - var size = index.map(function (i) { - return i + 1; - }); - - _fit(this, size, defaultValue); // traverse over the dimensions - - var data = this._data; - - for (i = 0, ii = index.length - 1; i < ii; i++) { - indexI = index[i]; - Object(utils_array["q" /* validateIndex */])(indexI, data.length); - data = data[indexI]; - } // set new value - - indexI = index[index.length - 1]; - Object(utils_array["q" /* validateIndex */])(indexI, data.length); - data[indexI] = value; - return this; - }; - /** - * Get a submatrix of this matrix - * @memberof DenseMatrix - * @param {DenseMatrix} matrix - * @param {Index} index Zero-based index - * @private - */ - - function _get(matrix, index) { - if (!Object(is["t" /* isIndex */])(index)) { - throw new TypeError('Invalid index'); - } - - var isScalar = index.isScalar(); - - if (isScalar) { - // return a scalar - return matrix.get(index.min()); - } else { - // validate dimensions - var size = index.size(); - - if (size.length !== matrix._size.length) { - throw new DimensionError["a" /* DimensionError */](size.length, matrix._size.length); - } // validate if any of the ranges in the index is out of range - - var min = index.min(); - var max = index.max(); - - for (var i = 0, ii = matrix._size.length; i < ii; i++) { - Object(utils_array["q" /* validateIndex */])(min[i], matrix._size[i]); - Object(utils_array["q" /* validateIndex */])(max[i], matrix._size[i]); - } // retrieve submatrix - // TODO: more efficient when creating an empty matrix and setting _data and _size manually - - return new DenseMatrix(_getSubmatrix(matrix._data, index, size.length, 0), matrix._datatype); - } - } - /** - * Recursively get a submatrix of a multi dimensional matrix. - * Index is not checked for correct number or length of dimensions. - * @memberof DenseMatrix - * @param {Array} data - * @param {Index} index - * @param {number} dims Total number of dimensions - * @param {number} dim Current dimension - * @return {Array} submatrix - * @private - */ - - function _getSubmatrix(data, index, dims, dim) { - var last = dim === dims - 1; - var range = index.dimension(dim); - - if (last) { - return range.map(function (i) { - Object(utils_array["q" /* validateIndex */])(i, data.length); - return data[i]; - }).valueOf(); - } else { - return range.map(function (i) { - Object(utils_array["q" /* validateIndex */])(i, data.length); - var child = data[i]; - return _getSubmatrix(child, index, dims, dim + 1); - }).valueOf(); - } - } - /** - * Replace a submatrix in this matrix - * Indexes are zero-based. - * @memberof DenseMatrix - * @param {DenseMatrix} matrix - * @param {Index} index - * @param {DenseMatrix | Array | *} submatrix - * @param {*} defaultValue Default value, filled in on new entries when - * the matrix is resized. - * @return {DenseMatrix} matrix - * @private - */ - - function _set(matrix, index, submatrix, defaultValue) { - if (!index || index.isIndex !== true) { - throw new TypeError('Invalid index'); - } // get index size and check whether the index contains a single value - - var iSize = index.size(); - var isScalar = index.isScalar(); // calculate the size of the submatrix, and convert it into an Array if needed - - var sSize; - - if (Object(is["v" /* isMatrix */])(submatrix)) { - sSize = submatrix.size(); - submatrix = submatrix.valueOf(); - } else { - sSize = Object(utils_array["a" /* arraySize */])(submatrix); - } - - if (isScalar) { - // set a scalar - // check whether submatrix is a scalar - if (sSize.length !== 0) { - throw new TypeError('Scalar expected'); - } - - matrix.set(index.min(), submatrix, defaultValue); - } else { - // set a submatrix - // validate dimensions - if (iSize.length < matrix._size.length) { - throw new DimensionError["a" /* DimensionError */](iSize.length, matrix._size.length, '<'); - } - - if (sSize.length < iSize.length) { - // calculate number of missing outer dimensions - var i = 0; - var outer = 0; - - while (iSize[i] === 1 && sSize[i] === 1) { - i++; - } - - while (iSize[i] === 1) { - outer++; - i++; - } // unsqueeze both outer and inner dimensions - - submatrix = Object(utils_array["o" /* unsqueeze */])(submatrix, iSize.length, outer, sSize); - } // check whether the size of the submatrix matches the index size - - if (!Object(utils_object["d" /* deepStrictEqual */])(iSize, sSize)) { - throw new DimensionError["a" /* DimensionError */](iSize, sSize, '>'); - } // enlarge matrix when needed - - var size = index.max().map(function (i) { - return i + 1; - }); - - _fit(matrix, size, defaultValue); // insert the sub matrix - - var dims = iSize.length; - var dim = 0; - - _setSubmatrix(matrix._data, index, submatrix, dims, dim); - } - - return matrix; - } - /** - * Replace a submatrix of a multi dimensional matrix. - * @memberof DenseMatrix - * @param {Array} data - * @param {Index} index - * @param {Array} submatrix - * @param {number} dims Total number of dimensions - * @param {number} dim - * @private - */ - - function _setSubmatrix(data, index, submatrix, dims, dim) { - var last = dim === dims - 1; - var range = index.dimension(dim); - - if (last) { - range.forEach(function (dataIndex, subIndex) { - Object(utils_array["q" /* validateIndex */])(dataIndex); - data[dataIndex] = submatrix[subIndex[0]]; - }); - } else { - range.forEach(function (dataIndex, subIndex) { - Object(utils_array["q" /* validateIndex */])(dataIndex); - - _setSubmatrix(data[dataIndex], index, submatrix[subIndex[0]], dims, dim + 1); - }); - } - } - /** - * Resize the matrix to the given size. Returns a copy of the matrix when - * `copy=true`, otherwise return the matrix itself (resize in place). - * - * @memberof DenseMatrix - * @param {number[] || Matrix} size The new size the matrix should have. - * @param {*} [defaultValue=0] Default value, filled in on new entries. - * If not provided, the matrix elements will - * be filled with zeros. - * @param {boolean} [copy] Return a resized copy of the matrix - * - * @return {Matrix} The resized matrix - */ - - DenseMatrix.prototype.resize = function (size, defaultValue, copy) { - // validate arguments - if (!Object(is["i" /* isCollection */])(size)) { - throw new TypeError('Array or Matrix expected'); - } // SparseMatrix input is always 2d, flatten this into 1d if it's indeed a vector - - var sizeArray = size.valueOf().map(function (value) { - return Array.isArray(value) && value.length === 1 ? value[0] : value; - }); // matrix to resize - - var m = copy ? this.clone() : this; // resize matrix - - return _resize(m, sizeArray, defaultValue); - }; - - function _resize(matrix, size, defaultValue) { - // check size - if (size.length === 0) { - // first value in matrix - var v = matrix._data; // go deep - - while (Object(is["b" /* isArray */])(v)) { - v = v[0]; - } - - return v; - } // resize matrix - - matrix._size = size.slice(0); // copy the array - - matrix._data = Object(utils_array["m" /* resize */])(matrix._data, matrix._size, defaultValue); // return matrix - - return matrix; - } - /** - * Reshape the matrix to the given size. Returns a copy of the matrix when - * `copy=true`, otherwise return the matrix itself (reshape in place). - * - * NOTE: This might be better suited to copy by default, instead of modifying - * in place. For now, it operates in place to remain consistent with - * resize(). - * - * @memberof DenseMatrix - * @param {number[]} size The new size the matrix should have. - * @param {boolean} [copy] Return a reshaped copy of the matrix - * - * @return {Matrix} The reshaped matrix - */ - - DenseMatrix.prototype.reshape = function (size, copy) { - var m = copy ? this.clone() : this; - m._data = Object(utils_array["l" /* reshape */])(m._data, size); - m._size = size.slice(0); - return m; - }; - /** - * Enlarge the matrix when it is smaller than given size. - * If the matrix is larger or equal sized, nothing is done. - * @memberof DenseMatrix - * @param {DenseMatrix} matrix The matrix to be resized - * @param {number[]} size - * @param {*} defaultValue Default value, filled in on new entries. - * @private - */ - - function _fit(matrix, size, defaultValue) { - var // copy the array - newSize = matrix._size.slice(0); - - var changed = false; // add dimensions when needed - - while (newSize.length < size.length) { - newSize.push(0); - changed = true; - } // enlarge size when needed - - for (var i = 0, ii = size.length; i < ii; i++) { - if (size[i] > newSize[i]) { - newSize[i] = size[i]; - changed = true; - } - } - - if (changed) { - // resize only when size is changed - _resize(matrix, newSize, defaultValue); - } - } - /** - * Create a clone of the matrix - * @memberof DenseMatrix - * @return {DenseMatrix} clone - */ - - DenseMatrix.prototype.clone = function () { - var m = new DenseMatrix({ - data: Object(utils_object["a" /* clone */])(this._data), - size: Object(utils_object["a" /* clone */])(this._size), - datatype: this._datatype - }); - return m; - }; - /** - * Retrieve the size of the matrix. - * @memberof DenseMatrix - * @returns {number[]} size - */ - - DenseMatrix.prototype.size = function () { - return this._size.slice(0); // return a clone of _size - }; - /** - * Create a new matrix with the results of the callback function executed on - * each entry of the matrix. - * @memberof DenseMatrix - * @param {Function} callback The callback function is invoked with three - * parameters: the value of the element, the index - * of the element, and the Matrix being traversed. - * - * @return {DenseMatrix} matrix - */ - - DenseMatrix.prototype.map = function (callback) { - // matrix instance - var me = this; - - var recurse = function recurse(value, index) { - if (Object(is["b" /* isArray */])(value)) { - return value.map(function (child, i) { - return recurse(child, index.concat(i)); - }); - } else { - return callback(value, index, me); - } - }; // determine the new datatype when the original matrix has datatype defined - // TODO: should be done in matrix constructor instead - - var data = recurse(this._data, []); - var datatype = this._datatype !== undefined ? Object(utils_array["h" /* getArrayDataType */])(data, is["M" /* typeOf */]) : undefined; - return new DenseMatrix(data, datatype); - }; - /** - * Execute a callback function on each entry of the matrix. - * @memberof DenseMatrix - * @param {Function} callback The callback function is invoked with three - * parameters: the value of the element, the index - * of the element, and the Matrix being traversed. - */ - - DenseMatrix.prototype.forEach = function (callback) { - // matrix instance - var me = this; - - var recurse = function recurse(value, index) { - if (Object(is["b" /* isArray */])(value)) { - value.forEach(function (child, i) { - recurse(child, index.concat(i)); - }); - } else { - callback(value, index, me); - } - }; - - recurse(this._data, []); - }; - /** - * Create an Array with a copy of the data of the DenseMatrix - * @memberof DenseMatrix - * @returns {Array} array - */ - - DenseMatrix.prototype.toArray = function () { - return Object(utils_object["a" /* clone */])(this._data); - }; - /** - * Get the primitive value of the DenseMatrix: a multidimensional array - * @memberof DenseMatrix - * @returns {Array} array - */ - - DenseMatrix.prototype.valueOf = function () { - return this._data; - }; - /** - * Get a string representation of the matrix, with optional formatting options. - * @memberof DenseMatrix - * @param {Object | number | Function} [options] Formatting options. See - * lib/utils/number:format for a - * description of the available - * options. - * @returns {string} str - */ - - DenseMatrix.prototype.format = function (options) { - return Object(utils_string["d" /* format */])(this._data, options); - }; - /** - * Get a string representation of the matrix - * @memberof DenseMatrix - * @returns {string} str - */ - - DenseMatrix.prototype.toString = function () { - return Object(utils_string["d" /* format */])(this._data); - }; - /** - * Get a JSON representation of the matrix - * @memberof DenseMatrix - * @returns {Object} - */ - - DenseMatrix.prototype.toJSON = function () { - return { - mathjs: 'DenseMatrix', - data: this._data, - size: this._size, - datatype: this._datatype - }; - }; - /** - * Get the kth Matrix diagonal. - * - * @memberof DenseMatrix - * @param {number | BigNumber} [k=0] The kth diagonal where the vector will retrieved. - * - * @returns {Matrix} The matrix with the diagonal values. - */ - - DenseMatrix.prototype.diagonal = function (k) { - // validate k if any - if (k) { - // convert BigNumber to a number - if (Object(is["e" /* isBigNumber */])(k)) { - k = k.toNumber(); - } // is must be an integer - - if (!Object(is["y" /* isNumber */])(k) || !Object(utils_number["i" /* isInteger */])(k)) { - throw new TypeError('The parameter k must be an integer number'); - } - } else { - // default value - k = 0; - } - - var kSuper = k > 0 ? k : 0; - var kSub = k < 0 ? -k : 0; // rows & columns - - var rows = this._size[0]; - var columns = this._size[1]; // number diagonal values - - var n = Math.min(rows - kSub, columns - kSuper); // x is a matrix get diagonal from matrix - - var data = []; // loop rows - - for (var i = 0; i < n; i++) { - data[i] = this._data[i + kSub][i + kSuper]; - } // create DenseMatrix - - return new DenseMatrix({ - data: data, - size: [n], - datatype: this._datatype - }); - }; - /** - * Create a diagonal matrix. - * - * @memberof DenseMatrix - * @param {Array} size The matrix size. - * @param {number | Matrix | Array } value The values for the diagonal. - * @param {number | BigNumber} [k=0] The kth diagonal where the vector will be filled in. - * @param {number} [defaultValue] The default value for non-diagonal - * @param {string} [datatype] The datatype for the diagonal - * - * @returns {DenseMatrix} - */ - - DenseMatrix.diagonal = function (size, value, k, defaultValue) { - if (!Object(is["b" /* isArray */])(size)) { - throw new TypeError('Array expected, size parameter'); - } - - if (size.length !== 2) { - throw new Error('Only two dimensions matrix are supported'); - } // map size & validate - - size = size.map(function (s) { - // check it is a big number - if (Object(is["e" /* isBigNumber */])(s)) { - // convert it - s = s.toNumber(); - } // validate arguments - - if (!Object(is["y" /* isNumber */])(s) || !Object(utils_number["i" /* isInteger */])(s) || s < 1) { - throw new Error('Size values must be positive integers'); - } - - return s; - }); // validate k if any - - if (k) { - // convert BigNumber to a number - if (Object(is["e" /* isBigNumber */])(k)) { - k = k.toNumber(); - } // is must be an integer - - if (!Object(is["y" /* isNumber */])(k) || !Object(utils_number["i" /* isInteger */])(k)) { - throw new TypeError('The parameter k must be an integer number'); - } - } else { - // default value - k = 0; - } - - var kSuper = k > 0 ? k : 0; - var kSub = k < 0 ? -k : 0; // rows and columns - - var rows = size[0]; - var columns = size[1]; // number of non-zero items - - var n = Math.min(rows - kSub, columns - kSuper); // value extraction function - - var _value; // check value - - if (Object(is["b" /* isArray */])(value)) { - // validate array - if (value.length !== n) { - // number of values in array must be n - throw new Error('Invalid value array length'); - } // define function - - _value = function _value(i) { - // return value @ i - return value[i]; - }; - } else if (Object(is["v" /* isMatrix */])(value)) { - // matrix size - var ms = value.size(); // validate matrix - - if (ms.length !== 1 || ms[0] !== n) { - // number of values in array must be n - throw new Error('Invalid matrix length'); - } // define function - - _value = function _value(i) { - // return value @ i - return value.get([i]); - }; - } else { - // define function - _value = function _value() { - // return value - return value; - }; - } // discover default value if needed - - if (!defaultValue) { - // check first value in array - defaultValue = Object(is["e" /* isBigNumber */])(_value(0)) ? _value(0).mul(0) // trick to create a BigNumber with value zero - : 0; - } // empty array - - var data = []; // check we need to resize array - - if (size.length > 0) { - // resize array - data = Object(utils_array["m" /* resize */])(data, size, defaultValue); // fill diagonal - - for (var d = 0; d < n; d++) { - data[d + kSub][d + kSuper] = _value(d); - } - } // create DenseMatrix - - return new DenseMatrix({ - data: data, - size: [rows, columns] - }); - }; - /** - * Generate a matrix from a JSON object - * @memberof DenseMatrix - * @param {Object} json An object structured like - * `{"mathjs": "DenseMatrix", data: [], size: []}`, - * where mathjs is optional - * @returns {DenseMatrix} - */ - - DenseMatrix.fromJSON = function (json) { - return new DenseMatrix(json); - }; - /** - * Swap rows i and j in Matrix. - * - * @memberof DenseMatrix - * @param {number} i Matrix row index 1 - * @param {number} j Matrix row index 2 - * - * @return {Matrix} The matrix reference - */ - - DenseMatrix.prototype.swapRows = function (i, j) { - // check index - if (!Object(is["y" /* isNumber */])(i) || !Object(utils_number["i" /* isInteger */])(i) || !Object(is["y" /* isNumber */])(j) || !Object(utils_number["i" /* isInteger */])(j)) { - throw new Error('Row index must be positive integers'); - } // check dimensions - - if (this._size.length !== 2) { - throw new Error('Only two dimensional matrix is supported'); - } // validate index - - Object(utils_array["q" /* validateIndex */])(i, this._size[0]); - Object(utils_array["q" /* validateIndex */])(j, this._size[0]); // swap rows - - DenseMatrix._swapRows(i, j, this._data); // return current instance - - return this; - }; - /** - * Swap rows i and j in Dense Matrix data structure. - * - * @param {number} i Matrix row index 1 - * @param {number} j Matrix row index 2 - * @param {Array} data Matrix data - */ - - DenseMatrix._swapRows = function (i, j, data) { - // swap values i <-> j - var vi = data[i]; - data[i] = data[j]; - data[j] = vi; - }; - /** - * Preprocess data, which can be an Array or DenseMatrix with nested Arrays and - * Matrices. Replaces all nested Matrices with Arrays - * @memberof DenseMatrix - * @param {Array} data - * @return {Array} data - */ - - function preprocess(data) { - for (var i = 0, ii = data.length; i < ii; i++) { - var elem = data[i]; - - if (Object(is["b" /* isArray */])(elem)) { - data[i] = preprocess(elem); - } else if (elem && elem.isMatrix === true) { - data[i] = preprocess(elem.valueOf()); - } - } - - return data; - } - - return DenseMatrix; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/function/utils/clone.js - - var clone_name = 'clone'; - var clone_dependencies = ['typed']; - var createClone = /* #__PURE__ */Object(factory["a" /* factory */])(clone_name, clone_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Clone an object. - * - * Syntax: - * - * math.clone(x) - * - * Examples: - * - * math.clone(3.5) // returns number 3.5 - * math.clone(math.complex('2-4i') // returns Complex 2 - 4i - * math.clone(math.unit(45, 'deg')) // returns Unit 45 deg - * math.clone([[1, 2], [3, 4]]) // returns Array [[1, 2], [3, 4]] - * math.clone("hello world") // returns string "hello world" - * - * @param {*} x Object to be cloned - * @return {*} A clone of object x - */ - return typed(clone_name, { - any: utils_object["a" /* clone */] - }); - }); - // EXTERNAL MODULE: ./src/error/IndexError.js - var IndexError = __webpack_require__(9); - - // CONCATENATED MODULE: ./src/utils/collection.js - - /** - * Test whether an array contains collections - * @param {Array} array - * @returns {boolean} Returns true when the array contains one or multiple - * collections (Arrays or Matrices). Returns false otherwise. - */ - - function containsCollections(array) { - for (var i = 0; i < array.length; i++) { - if (Object(is["i" /* isCollection */])(array[i])) { - return true; - } - } - - return false; - } - /** - * Recursively loop over all elements in a given multi dimensional array - * and invoke the callback on each of the elements. - * @param {Array | Matrix} array - * @param {Function} callback The callback method is invoked with one - * parameter: the current element in the array - */ - - function deepForEach(array, callback) { - if (Object(is["v" /* isMatrix */])(array)) { - array = array.valueOf(); - } - - for (var i = 0, ii = array.length; i < ii; i++) { - var value = array[i]; - - if (Array.isArray(value)) { - deepForEach(value, callback); - } else { - callback(value); - } - } - } - /** - * Execute the callback function element wise for each element in array and any - * nested array - * Returns an array with the results - * @param {Array | Matrix} array - * @param {Function} callback The callback is called with two parameters: - * value1 and value2, which contain the current - * element of both arrays. - * @param {boolean} [skipZeros] Invoke callback function for non-zero values only. - * - * @return {Array | Matrix} res - */ - - function deepMap(array, callback, skipZeros) { - if (array && typeof array.map === 'function') { - // TODO: replace array.map with a for loop to improve performance - return array.map(function (x) { - return deepMap(x, callback, skipZeros); - }); - } else { - return callback(array); - } - } - /** - * Reduce a given matrix or array to a new matrix or - * array with one less dimension, applying the given - * callback in the selected dimension. - * @param {Array | Matrix} mat - * @param {number} dim - * @param {Function} callback - * @return {Array | Matrix} res - */ - - function reduce(mat, dim, callback) { - var size = Array.isArray(mat) ? Object(utils_array["a" /* arraySize */])(mat) : mat.size(); - - if (dim < 0 || dim >= size.length) { - // TODO: would be more clear when throwing a DimensionError here - throw new IndexError["a" /* IndexError */](dim, size.length); - } - - if (Object(is["v" /* isMatrix */])(mat)) { - return mat.create(_reduce(mat.valueOf(), dim, callback)); - } else { - return _reduce(mat, dim, callback); - } - } - /** - * Recursively reduce a matrix - * @param {Array} mat - * @param {number} dim - * @param {Function} callback - * @returns {Array} ret - * @private - */ - - function _reduce(mat, dim, callback) { - var i, ret, val, tran; - - if (dim <= 0) { - if (!Array.isArray(mat[0])) { - val = mat[0]; - - for (i = 1; i < mat.length; i++) { - val = callback(val, mat[i]); - } - - return val; - } else { - tran = _switch(mat); - ret = []; - - for (i = 0; i < tran.length; i++) { - ret[i] = _reduce(tran[i], dim - 1, callback); - } - - return ret; - } - } else { - ret = []; - - for (i = 0; i < mat.length; i++) { - ret[i] = _reduce(mat[i], dim - 1, callback); - } - - return ret; - } - } - /** - * Transpose a matrix - * @param {Array} mat - * @returns {Array} ret - * @private - */ - - function _switch(mat) { - var I = mat.length; - var J = mat[0].length; - var i, j; - var ret = []; - - for (j = 0; j < J; j++) { - var tmp = []; - - for (i = 0; i < I; i++) { - tmp.push(mat[i][j]); - } - - ret.push(tmp); - } - - return ret; - } // TODO: document function scatter - - function scatter(a, j, w, x, u, mark, cindex, f, inverse, update, value) { - // a arrays - var avalues = a._values; - var aindex = a._index; - var aptr = a._ptr; // vars - - var k, k0, k1, i; // check we need to process values (pattern matrix) - - if (x) { - // values in j - for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { - // row - i = aindex[k]; // check value exists in current j - - if (w[i] !== mark) { - // i is new entry in j - w[i] = mark; // add i to pattern of C - - cindex.push(i); // x(i) = A, check we need to call function this time - - if (update) { - // copy value to workspace calling callback function - x[i] = inverse ? f(avalues[k], value) : f(value, avalues[k]); // function was called on current row - - u[i] = mark; - } else { - // copy value to workspace - x[i] = avalues[k]; - } - } else { - // i exists in C already - x[i] = inverse ? f(avalues[k], x[i]) : f(x[i], avalues[k]); // function was called on current row - - u[i] = mark; - } - } - } else { - // values in j - for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { - // row - i = aindex[k]; // check value exists in current j - - if (w[i] !== mark) { - // i is new entry in j - w[i] = mark; // add i to pattern of C - - cindex.push(i); - } else { - // indicate function was called on current row - u[i] = mark; - } - } - } - } - // CONCATENATED MODULE: ./src/function/utils/isInteger.js - - var isInteger_name = 'isInteger'; - var isInteger_dependencies = ['typed']; - var createIsInteger = /* #__PURE__ */Object(factory["a" /* factory */])(isInteger_name, isInteger_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Test whether a value is an integer number. - * The function supports `number`, `BigNumber`, and `Fraction`. - * - * The function is evaluated element-wise in case of Array or Matrix input. - * - * Syntax: - * - * math.isInteger(x) - * - * Examples: - * - * math.isInteger(2) // returns true - * math.isInteger(0) // returns true - * math.isInteger(0.5) // returns false - * math.isInteger(math.bignumber(500)) // returns true - * math.isInteger(math.fraction(4)) // returns true - * math.isInteger('3') // returns true - * math.isInteger([3, 0.5, -2]) // returns [true, false, true] - * math.isInteger(math.complex('2-4i') // throws an error - * - * See also: - * - * isNumeric, isPositive, isNegative, isZero - * - * @param {number | BigNumber | Fraction | Array | Matrix} x Value to be tested - * @return {boolean} Returns true when `x` contains a numeric, integer value. - * Throws an error in case of an unknown data type. - */ - return typed(isInteger_name, { - number: utils_number["i" /* isInteger */], - // TODO: what to do with isInteger(add(0.1, 0.2)) ? - BigNumber: function BigNumber(x) { - return x.isInt(); - }, - Fraction: function Fraction(x) { - return x.d === 1 && isFinite(x.n); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/plain/number/utils.js - - var utils_n1 = 'number'; - function isIntegerNumber(x) { - return Object(utils_number["i" /* isInteger */])(x); - } - isIntegerNumber.signature = utils_n1; - function isNegativeNumber(x) { - return x < 0; - } - isNegativeNumber.signature = utils_n1; - function isPositiveNumber(x) { - return x > 0; - } - isPositiveNumber.signature = utils_n1; - function isZeroNumber(x) { - return x === 0; - } - isZeroNumber.signature = utils_n1; - function isNaNNumber(x) { - return Number.isNaN(x); - } - isNaNNumber.signature = utils_n1; - // CONCATENATED MODULE: ./src/function/utils/isNegative.js - - var isNegative_name = 'isNegative'; - var isNegative_dependencies = ['typed']; - var createIsNegative = /* #__PURE__ */Object(factory["a" /* factory */])(isNegative_name, isNegative_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Test whether a value is negative: smaller than zero. - * The function supports types `number`, `BigNumber`, `Fraction`, and `Unit`. - * - * The function is evaluated element-wise in case of Array or Matrix input. - * - * Syntax: - * - * math.isNegative(x) - * - * Examples: - * - * math.isNegative(3) // returns false - * math.isNegative(-2) // returns true - * math.isNegative(0) // returns false - * math.isNegative(-0) // returns false - * math.isNegative(math.bignumber(2)) // returns false - * math.isNegative(math.fraction(-2, 5)) // returns true - * math.isNegative('-2') // returns true - * math.isNegative([2, 0, -3]') // returns [false, false, true] - * - * See also: - * - * isNumeric, isPositive, isZero, isInteger - * - * @param {number | BigNumber | Fraction | Unit | Array | Matrix} x Value to be tested - * @return {boolean} Returns true when `x` is larger than zero. - * Throws an error in case of an unknown data type. - */ - return typed(isNegative_name, { - number: isNegativeNumber, - BigNumber: function BigNumber(x) { - return x.isNeg() && !x.isZero() && !x.isNaN(); - }, - Fraction: function Fraction(x) { - return x.s < 0; // It's enough to decide on the sign - }, - Unit: function Unit(x) { - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/utils/isNumeric.js - - var isNumeric_name = 'isNumeric'; - var isNumeric_dependencies = ['typed']; - var createIsNumeric = /* #__PURE__ */Object(factory["a" /* factory */])(isNumeric_name, isNumeric_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Test whether a value is an numeric value. - * - * The function is evaluated element-wise in case of Array or Matrix input. - * - * Syntax: - * - * math.isNumeric(x) - * - * Examples: - * - * math.isNumeric(2) // returns true - * math.isNumeric('2') // returns false - * math.hasNumericValue('2') // returns true - * math.isNumeric(0) // returns true - * math.isNumeric(math.bignumber(500)) // returns true - * math.isNumeric(math.fraction(4)) // returns true - * math.isNumeric(math.complex('2-4i') // returns false - * math.isNumeric([2.3, 'foo', false]) // returns [true, false, true] - * - * See also: - * - * isZero, isPositive, isNegative, isInteger, hasNumericValue - * - * @param {*} x Value to be tested - * @return {boolean} Returns true when `x` is a `number`, `BigNumber`, - * `Fraction`, or `boolean`. Returns false for other types. - * Throws an error in case of unknown types. - */ - return typed(isNumeric_name, { - 'number | BigNumber | Fraction | boolean': function numberBigNumberFractionBoolean() { - return true; - }, - 'Complex | Unit | string | null | undefined | Node': function ComplexUnitStringNullUndefinedNode() { - return false; - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/utils/hasNumericValue.js - - var hasNumericValue_name = 'hasNumericValue'; - var hasNumericValue_dependencies = ['typed', 'isNumeric']; - var createHasNumericValue = /* #__PURE__ */Object(factory["a" /* factory */])(hasNumericValue_name, hasNumericValue_dependencies, function (_ref) { - var typed = _ref.typed, - isNumeric = _ref.isNumeric; - - /** - * Test whether a value is an numeric value. - * - * In case of a string, true is returned if the string contains a numeric value. - * - * Syntax: - * - * math.hasNumericValue(x) - * - * Examples: - * - * math.hasNumericValue(2) // returns true - * math.hasNumericValue('2') // returns true - * math.isNumeric('2') // returns false - * math.hasNumericValue(0) // returns true - * math.hasNumericValue(math.bignumber(500)) // returns true - * math.hasNumericValue(math.fraction(4)) // returns true - * math.hasNumericValue(math.complex('2-4i') // returns false - * math.hasNumericValue([2.3, 'foo', false]) // returns [true, false, true] - * - * See also: - * - * isZero, isPositive, isNegative, isInteger, isNumeric - * - * @param {*} x Value to be tested - * @return {boolean} Returns true when `x` is a `number`, `BigNumber`, - * `Fraction`, `Boolean`, or a `String` containing number. Returns false for other types. - * Throws an error in case of unknown types. - */ - return typed(hasNumericValue_name, { - string: function string(x) { - return x.trim().length > 0 && !isNaN(Number(x)); - }, - any: function any(x) { - return isNumeric(x); - } - }); - }); - // CONCATENATED MODULE: ./src/function/utils/isPositive.js - - var isPositive_name = 'isPositive'; - var isPositive_dependencies = ['typed']; - var createIsPositive = /* #__PURE__ */Object(factory["a" /* factory */])(isPositive_name, isPositive_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Test whether a value is positive: larger than zero. - * The function supports types `number`, `BigNumber`, `Fraction`, and `Unit`. - * - * The function is evaluated element-wise in case of Array or Matrix input. - * - * Syntax: - * - * math.isPositive(x) - * - * Examples: - * - * math.isPositive(3) // returns true - * math.isPositive(-2) // returns false - * math.isPositive(0) // returns false - * math.isPositive(-0) // returns false - * math.isPositive(0.5) // returns true - * math.isPositive(math.bignumber(2)) // returns true - * math.isPositive(math.fraction(-2, 5)) // returns false - * math.isPositive(math.fraction(1,3)) // returns false - * math.isPositive('2') // returns true - * math.isPositive([2, 0, -3]) // returns [true, false, false] - * - * See also: - * - * isNumeric, isZero, isNegative, isInteger - * - * @param {number | BigNumber | Fraction | Unit | Array | Matrix} x Value to be tested - * @return {boolean} Returns true when `x` is larger than zero. - * Throws an error in case of an unknown data type. - */ - return typed(isPositive_name, { - number: isPositiveNumber, - BigNumber: function BigNumber(x) { - return !x.isNeg() && !x.isZero() && !x.isNaN(); - }, - Fraction: function Fraction(x) { - return x.s > 0 && x.n > 0; - }, - Unit: function Unit(x) { - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/utils/isZero.js - - var isZero_name = 'isZero'; - var isZero_dependencies = ['typed']; - var createIsZero = /* #__PURE__ */Object(factory["a" /* factory */])(isZero_name, isZero_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Test whether a value is zero. - * The function can check for zero for types `number`, `BigNumber`, `Fraction`, - * `Complex`, and `Unit`. - * - * The function is evaluated element-wise in case of Array or Matrix input. - * - * Syntax: - * - * math.isZero(x) - * - * Examples: - * - * math.isZero(0) // returns true - * math.isZero(2) // returns false - * math.isZero(0.5) // returns false - * math.isZero(math.bignumber(0)) // returns true - * math.isZero(math.fraction(0)) // returns true - * math.isZero(math.fraction(1,3)) // returns false - * math.isZero(math.complex('2 - 4i') // returns false - * math.isZero(math.complex('0i') // returns true - * math.isZero('0') // returns true - * math.isZero('2') // returns false - * math.isZero([2, 0, -3]') // returns [false, true, false] - * - * See also: - * - * isNumeric, isPositive, isNegative, isInteger - * - * @param {number | BigNumber | Complex | Fraction | Unit | Array | Matrix} x Value to be tested - * @return {boolean} Returns true when `x` is zero. - * Throws an error in case of an unknown data type. - */ - return typed(isZero_name, { - number: isZeroNumber, - BigNumber: function BigNumber(x) { - return x.isZero(); - }, - Complex: function Complex(x) { - return x.re === 0 && x.im === 0; - }, - Fraction: function Fraction(x) { - return x.d === 1 && x.n === 0; - }, - Unit: function Unit(x) { - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/utils/isNaN.js - - var isNaN_name = 'isNaN'; - var isNaN_dependencies = ['typed']; - var createIsNaN = /* #__PURE__ */Object(factory["a" /* factory */])(isNaN_name, isNaN_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Test whether a value is NaN (not a number). - * The function supports types `number`, `BigNumber`, `Fraction`, `Unit` and `Complex`. - * - * The function is evaluated element-wise in case of Array or Matrix input. - * - * Syntax: - * - * math.isNaN(x) - * - * Examples: - * - * math.isNaN(3) // returns false - * math.isNaN(NaN) // returns true - * math.isNaN(0) // returns false - * math.isNaN(math.bignumber(NaN)) // returns true - * math.isNaN(math.bignumber(0)) // returns false - * math.isNaN(math.fraction(-2, 5)) // returns false - * math.isNaN('-2') // returns false - * math.isNaN([2, 0, -3, NaN]') // returns [false, false, false, true] - * - * See also: - * - * isNumeric, isNegative, isPositive, isZero, isInteger - * - * @param {number | BigNumber | Fraction | Unit | Array | Matrix} x Value to be tested - * @return {boolean} Returns true when `x` is NaN. - * Throws an error in case of an unknown data type. - */ - return typed(isNaN_name, { - number: isNaNNumber, - BigNumber: function BigNumber(x) { - return x.isNaN(); - }, - Fraction: function Fraction(x) { - return false; - }, - Complex: function Complex(x) { - return x.isNaN(); - }, - Unit: function Unit(x) { - return Number.isNaN(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, Number.isNaN); - } - }); - }); - // CONCATENATED MODULE: ./src/function/utils/typeOf.js - - var typeOf_name = 'typeOf'; - var typeOf_dependencies = ['typed']; - var createTypeOf = /* #__PURE__ */Object(factory["a" /* factory */])(typeOf_name, typeOf_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Determine the type of a variable. - * - * Function `typeOf` recognizes the following types of objects: - * - * Object | Returns | Example - * ---------------------- | ------------- | ------------------------------------------ - * null | `'null'` | `math.typeOf(null)` - * number | `'number'` | `math.typeOf(3.5)` - * boolean | `'boolean'` | `math.typeOf(true)` - * string | `'string'` | `math.typeOf('hello world')` - * Array | `'Array'` | `math.typeOf([1, 2, 3])` - * Date | `'Date'` | `math.typeOf(new Date())` - * Function | `'Function'` | `math.typeOf(function () {})` - * Object | `'Object'` | `math.typeOf({a: 2, b: 3})` - * RegExp | `'RegExp'` | `math.typeOf(/a regexp/)` - * undefined | `'undefined'` | `math.typeOf(undefined)` - * math.BigNumber | `'BigNumber'` | `math.typeOf(math.bignumber('2.3e500'))` - * math.Chain | `'Chain'` | `math.typeOf(math.chain(2))` - * math.Complex | `'Complex'` | `math.typeOf(math.complex(2, 3))` - * math.Fraction | `'Fraction'` | `math.typeOf(math.fraction(1, 3))` - * math.Help | `'Help'` | `math.typeOf(math.help('sqrt'))` - * math.Help | `'Help'` | `math.typeOf(math.help('sqrt'))` - * math.Index | `'Index'` | `math.typeOf(math.index(1, 3))` - * math.Matrix | `'Matrix'` | `math.typeOf(math.matrix([[1,2], [3, 4]]))` - * math.Range | `'Range'` | `math.typeOf(math.range(0, 10))` - * math.ResultSet | `'ResultSet'` | `math.typeOf(math.evaluate('a=2\nb=3'))` - * math.Unit | `'Unit'` | `math.typeOf(math.unit('45 deg'))` - * math.AccessorNode | `'AccessorNode'` | `math.typeOf(math.parse('A[2]'))` - * math.ArrayNode | `'ArrayNode'` | `math.typeOf(math.parse('[1,2,3]'))` - * math.AssignmentNode | `'AssignmentNode'` | `math.typeOf(math.parse('x=2'))` - * math.BlockNode | `'BlockNode'` | `math.typeOf(math.parse('a=2; b=3'))` - * math.ConditionalNode | `'ConditionalNode'` | `math.typeOf(math.parse('x<0 ? -x : x'))` - * math.ConstantNode | `'ConstantNode'` | `math.typeOf(math.parse('2.3'))` - * math.FunctionAssignmentNode | `'FunctionAssignmentNode'` | `math.typeOf(math.parse('f(x)=x^2'))` - * math.FunctionNode | `'FunctionNode'` | `math.typeOf(math.parse('sqrt(4)'))` - * math.IndexNode | `'IndexNode'` | `math.typeOf(math.parse('A[2]').index)` - * math.ObjectNode | `'ObjectNode'` | `math.typeOf(math.parse('{a:2}'))` - * math.ParenthesisNode | `'ParenthesisNode'` | `math.typeOf(math.parse('(2+3)'))` - * math.RangeNode | `'RangeNode'` | `math.typeOf(math.parse('1:10'))` - * math.SymbolNode | `'SymbolNode'` | `math.typeOf(math.parse('x'))` - * - * Syntax: - * - * math.typeOf(x) - * - * Examples: - * - * math.typeOf(3.5) // returns 'number' - * math.typeOf(math.complex('2-4i')) // returns 'Complex' - * math.typeOf(math.unit('45 deg')) // returns 'Unit' - * math.typeOf('hello world') // returns 'string' - * - * @param {*} x The variable for which to test the type. - * @return {string} Returns the name of the type. Primitive types are lower case, - * non-primitive types are upper-camel-case. - * For example 'number', 'string', 'Array', 'Date'. - */ - return typed(typeOf_name, { - any: is["M" /* typeOf */] - }); - }); - // CONCATENATED MODULE: ./src/utils/bignumber/nearlyEqual.js - /** - * Compares two BigNumbers. - * @param {BigNumber} x First value to compare - * @param {BigNumber} y Second value to compare - * @param {number} [epsilon] The maximum relative difference between x and y - * If epsilon is undefined or null, the function will - * test whether x and y are exactly equal. - * @return {boolean} whether the two numbers are nearly equal - */ - function nearlyEqual(x, y, epsilon) { - // if epsilon is null or undefined, test whether x and y are exactly equal - if (epsilon === null || epsilon === undefined) { - return x.eq(y); - } // use "==" operator, handles infinities - - if (x.eq(y)) { - return true; - } // NaN - - if (x.isNaN() || y.isNaN()) { - return false; - } // at this point x and y should be finite - - if (x.isFinite() && y.isFinite()) { - // check numbers are very close, needed when comparing numbers near zero - var diff = x.minus(y).abs(); - - if (diff.isZero()) { - return true; - } else { - // use relative error - var max = x.constructor.max(x.abs(), y.abs()); - return diff.lte(max.times(epsilon)); - } - } // Infinite and Number or negative Infinite and positive Infinite cases - - return false; - } - // CONCATENATED MODULE: ./src/utils/complex.js - - /** - * Test whether two complex values are equal provided a given epsilon. - * Does not use or change the global Complex.EPSILON setting - * @param {Complex} x - * @param {Complex} y - * @param {number} epsilon - * @returns {boolean} - */ - - function complexEquals(x, y, epsilon) { - return Object(utils_number["m" /* nearlyEqual */])(x.re, y.re, epsilon) && Object(utils_number["m" /* nearlyEqual */])(x.im, y.im, epsilon); - } - // CONCATENATED MODULE: ./src/function/relational/equalScalar.js - - var equalScalar_name = 'equalScalar'; - var equalScalar_dependencies = ['typed', 'config']; - var createEqualScalar = /* #__PURE__ */Object(factory["a" /* factory */])(equalScalar_name, equalScalar_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config; - - /** - * Test whether two scalar values are nearly equal. - * - * @param {number | BigNumber | Fraction | boolean | Complex | Unit} x First value to compare - * @param {number | BigNumber | Fraction | boolean | Complex} y Second value to compare - * @return {boolean} Returns true when the compared values are equal, else returns false - * @private - */ - return typed(equalScalar_name, { - 'boolean, boolean': function booleanBoolean(x, y) { - return x === y; - }, - 'number, number': function numberNumber(x, y) { - return Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return x.eq(y) || nearlyEqual(x, y, config.epsilon); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.equals(y); - }, - 'Complex, Complex': function ComplexComplex(x, y) { - return complexEquals(x, y, config.epsilon); - }, - 'Unit, Unit': function UnitUnit(x, y) { - if (!x.equalBase(y)) { - throw new Error('Cannot compare units with different base'); - } - - return this(x.value, y.value); - } - }); - }); - var createEqualScalarNumber = Object(factory["a" /* factory */])(equalScalar_name, ['typed', 'config'], function (_ref2) { - var typed = _ref2.typed, - config = _ref2.config; - return typed(equalScalar_name, { - 'number, number': function numberNumber(x, y) { - return Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon); - } - }); - }); - // CONCATENATED MODULE: ./src/type/matrix/SparseMatrix.js - - var SparseMatrix_name = 'SparseMatrix'; - var SparseMatrix_dependencies = ['typed', 'equalScalar', 'Matrix']; - var createSparseMatrixClass = /* #__PURE__ */Object(factory["a" /* factory */])(SparseMatrix_name, SparseMatrix_dependencies, function (_ref) { - var typed = _ref.typed, - equalScalar = _ref.equalScalar, - Matrix = _ref.Matrix; - - /** - * Sparse Matrix implementation. This type implements a Compressed Column Storage format - * for sparse matrices. - * @class SparseMatrix - */ - function SparseMatrix(data, datatype) { - if (!(this instanceof SparseMatrix)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - if (datatype && !Object(is["I" /* isString */])(datatype)) { - throw new Error('Invalid datatype: ' + datatype); - } - - if (Object(is["v" /* isMatrix */])(data)) { - // create from matrix - _createFromMatrix(this, data, datatype); - } else if (data && Object(is["b" /* isArray */])(data.index) && Object(is["b" /* isArray */])(data.ptr) && Object(is["b" /* isArray */])(data.size)) { - // initialize fields - this._values = data.values; - this._index = data.index; - this._ptr = data.ptr; - this._size = data.size; - this._datatype = datatype || data.datatype; - } else if (Object(is["b" /* isArray */])(data)) { - // create from array - _createFromArray(this, data, datatype); - } else if (data) { - // unsupported type - throw new TypeError('Unsupported type of data (' + Object(is["M" /* typeOf */])(data) + ')'); - } else { - // nothing provided - this._values = []; - this._index = []; - this._ptr = [0]; - this._size = [0, 0]; - this._datatype = datatype; - } - } - - function _createFromMatrix(matrix, source, datatype) { - // check matrix type - if (source.type === 'SparseMatrix') { - // clone arrays - matrix._values = source._values ? Object(utils_object["a" /* clone */])(source._values) : undefined; - matrix._index = Object(utils_object["a" /* clone */])(source._index); - matrix._ptr = Object(utils_object["a" /* clone */])(source._ptr); - matrix._size = Object(utils_object["a" /* clone */])(source._size); - matrix._datatype = datatype || source._datatype; - } else { - // build from matrix data - _createFromArray(matrix, source.valueOf(), datatype || source._datatype); - } - } - - function _createFromArray(matrix, data, datatype) { - // initialize fields - matrix._values = []; - matrix._index = []; - matrix._ptr = []; - matrix._datatype = datatype; // discover rows & columns, do not use math.size() to avoid looping array twice - - var rows = data.length; - var columns = 0; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; - - if (Object(is["I" /* isString */])(datatype)) { - // find signature that matches (datatype, datatype) - eq = typed.find(equalScalar, [datatype, datatype]) || equalScalar; // convert 0 to the same datatype - - zero = typed.convert(0, datatype); - } // check we have rows (empty array) - - if (rows > 0) { - // column index - var j = 0; - - do { - // store pointer to values index - matrix._ptr.push(matrix._index.length); // loop rows - - for (var i = 0; i < rows; i++) { - // current row - var row = data[i]; // check row is an array - - if (Object(is["b" /* isArray */])(row)) { - // update columns if needed (only on first column) - if (j === 0 && columns < row.length) { - columns = row.length; - } // check row has column - - if (j < row.length) { - // value - var v = row[j]; // check value != 0 - - if (!eq(v, zero)) { - // store value - matrix._values.push(v); // index - - matrix._index.push(i); - } - } - } else { - // update columns if needed (only on first column) - if (j === 0 && columns < 1) { - columns = 1; - } // check value != 0 (row is a scalar) - - if (!eq(row, zero)) { - // store value - matrix._values.push(row); // index - - matrix._index.push(i); - } - } - } // increment index - - j++; - } while (j < columns); - } // store number of values in ptr - - matrix._ptr.push(matrix._index.length); // size - - matrix._size = [rows, columns]; - } - - SparseMatrix.prototype = new Matrix(); - /** - * Create a new SparseMatrix - */ - - SparseMatrix.prototype.createSparseMatrix = function (data, datatype) { - return new SparseMatrix(data, datatype); - }; - /** - * Attach type information - */ - - SparseMatrix.prototype.type = 'SparseMatrix'; - SparseMatrix.prototype.isSparseMatrix = true; - /** - * Get the matrix type - * - * Usage: - * const matrixType = matrix.getDataType() // retrieves the matrix type - * - * @memberOf SparseMatrix - * @return {string} type information; if multiple types are found from the Matrix, it will return "mixed" - */ - - SparseMatrix.prototype.getDataType = function () { - return Object(utils_array["h" /* getArrayDataType */])(this._values, is["M" /* typeOf */]); - }; - /** - * Get the storage format used by the matrix. - * - * Usage: - * const format = matrix.storage() // retrieve storage format - * - * @memberof SparseMatrix - * @return {string} The storage format. - */ - - SparseMatrix.prototype.storage = function () { - return 'sparse'; - }; - /** - * Get the datatype of the data stored in the matrix. - * - * Usage: - * const format = matrix.datatype() // retrieve matrix datatype - * - * @memberof SparseMatrix - * @return {string} The datatype. - */ - - SparseMatrix.prototype.datatype = function () { - return this._datatype; - }; - /** - * Create a new SparseMatrix - * @memberof SparseMatrix - * @param {Array} data - * @param {string} [datatype] - */ - - SparseMatrix.prototype.create = function (data, datatype) { - return new SparseMatrix(data, datatype); - }; - /** - * Get the matrix density. - * - * Usage: - * const density = matrix.density() // retrieve matrix density - * - * @memberof SparseMatrix - * @return {number} The matrix density. - */ - - SparseMatrix.prototype.density = function () { - // rows & columns - var rows = this._size[0]; - var columns = this._size[1]; // calculate density - - return rows !== 0 && columns !== 0 ? this._index.length / (rows * columns) : 0; - }; - /** - * Get a subset of the matrix, or replace a subset of the matrix. - * - * Usage: - * const subset = matrix.subset(index) // retrieve subset - * const value = matrix.subset(index, replacement) // replace subset - * - * @memberof SparseMatrix - * @param {Index} index - * @param {Array | Matrix | *} [replacement] - * @param {*} [defaultValue=0] Default value, filled in on new entries when - * the matrix is resized. If not provided, - * new matrix elements will be filled with zeros. - */ - - SparseMatrix.prototype.subset = function (index, replacement, defaultValue) { - // check it is a pattern matrix - if (!this._values) { - throw new Error('Cannot invoke subset on a Pattern only matrix'); - } // check arguments - - switch (arguments.length) { - case 1: - return _getsubset(this, index); - // intentional fall through - - case 2: - case 3: - return _setsubset(this, index, replacement, defaultValue); - - default: - throw new SyntaxError('Wrong number of arguments'); - } - }; - - function _getsubset(matrix, idx) { - // check idx - if (!Object(is["t" /* isIndex */])(idx)) { - throw new TypeError('Invalid index'); - } - - var isScalar = idx.isScalar(); - - if (isScalar) { - // return a scalar - return matrix.get(idx.min()); - } // validate dimensions - - var size = idx.size(); - - if (size.length !== matrix._size.length) { - throw new DimensionError["a" /* DimensionError */](size.length, matrix._size.length); - } // vars - - var i, ii, k, kk; // validate if any of the ranges in the index is out of range - - var min = idx.min(); - var max = idx.max(); - - for (i = 0, ii = matrix._size.length; i < ii; i++) { - Object(utils_array["q" /* validateIndex */])(min[i], matrix._size[i]); - Object(utils_array["q" /* validateIndex */])(max[i], matrix._size[i]); - } // matrix arrays - - var mvalues = matrix._values; - var mindex = matrix._index; - var mptr = matrix._ptr; // rows & columns dimensions for result matrix - - var rows = idx.dimension(0); - var columns = idx.dimension(1); // workspace & permutation vector - - var w = []; - var pv = []; // loop rows in resulting matrix - - rows.forEach(function (i, r) { - // update permutation vector - pv[i] = r[0]; // mark i in workspace - - w[i] = true; - }); // result matrix arrays - - var values = mvalues ? [] : undefined; - var index = []; - var ptr = []; // loop columns in result matrix - - columns.forEach(function (j) { - // update ptr - ptr.push(index.length); // loop values in column j - - for (k = mptr[j], kk = mptr[j + 1]; k < kk; k++) { - // row - i = mindex[k]; // check row is in result matrix - - if (w[i] === true) { - // push index - index.push(pv[i]); // check we need to process values - - if (values) { - values.push(mvalues[k]); - } - } - } - }); // update ptr - - ptr.push(index.length); // return matrix - - return new SparseMatrix({ - values: values, - index: index, - ptr: ptr, - size: size, - datatype: matrix._datatype - }); - } - - function _setsubset(matrix, index, submatrix, defaultValue) { - // check index - if (!index || index.isIndex !== true) { - throw new TypeError('Invalid index'); - } // get index size and check whether the index contains a single value - - var iSize = index.size(); - var isScalar = index.isScalar(); // calculate the size of the submatrix, and convert it into an Array if needed - - var sSize; - - if (Object(is["v" /* isMatrix */])(submatrix)) { - // submatrix size - sSize = submatrix.size(); // use array representation - - submatrix = submatrix.toArray(); - } else { - // get submatrix size (array, scalar) - sSize = Object(utils_array["a" /* arraySize */])(submatrix); - } // check index is a scalar - - if (isScalar) { - // verify submatrix is a scalar - if (sSize.length !== 0) { - throw new TypeError('Scalar expected'); - } // set value - - matrix.set(index.min(), submatrix, defaultValue); - } else { - // validate dimensions, index size must be one or two dimensions - if (iSize.length !== 1 && iSize.length !== 2) { - throw new DimensionError["a" /* DimensionError */](iSize.length, matrix._size.length, '<'); - } // check submatrix and index have the same dimensions - - if (sSize.length < iSize.length) { - // calculate number of missing outer dimensions - var i = 0; - var outer = 0; - - while (iSize[i] === 1 && sSize[i] === 1) { - i++; - } - - while (iSize[i] === 1) { - outer++; - i++; - } // unsqueeze both outer and inner dimensions - - submatrix = Object(utils_array["o" /* unsqueeze */])(submatrix, iSize.length, outer, sSize); - } // check whether the size of the submatrix matches the index size - - if (!Object(utils_object["d" /* deepStrictEqual */])(iSize, sSize)) { - throw new DimensionError["a" /* DimensionError */](iSize, sSize, '>'); - } // offsets - - var x0 = index.min()[0]; - var y0 = index.min()[1]; // submatrix rows and columns - - var m = sSize[0]; - var n = sSize[1]; // loop submatrix - - for (var x = 0; x < m; x++) { - // loop columns - for (var y = 0; y < n; y++) { - // value at i, j - var v = submatrix[x][y]; // invoke set (zero value will remove entry from matrix) - - matrix.set([x + x0, y + y0], v, defaultValue); - } - } - } - - return matrix; - } - /** - * Get a single element from the matrix. - * @memberof SparseMatrix - * @param {number[]} index Zero-based index - * @return {*} value - */ - - SparseMatrix.prototype.get = function (index) { - if (!Object(is["b" /* isArray */])(index)) { - throw new TypeError('Array expected'); - } - - if (index.length !== this._size.length) { - throw new DimensionError["a" /* DimensionError */](index.length, this._size.length); - } // check it is a pattern matrix - - if (!this._values) { - throw new Error('Cannot invoke get on a Pattern only matrix'); - } // row and column - - var i = index[0]; - var j = index[1]; // check i, j are valid - - Object(utils_array["q" /* validateIndex */])(i, this._size[0]); - Object(utils_array["q" /* validateIndex */])(j, this._size[1]); // find value index - - var k = _getValueIndex(i, this._ptr[j], this._ptr[j + 1], this._index); // check k is prior to next column k and it is in the correct row - - if (k < this._ptr[j + 1] && this._index[k] === i) { - return this._values[k]; - } - - return 0; - }; - /** - * Replace a single element in the matrix. - * @memberof SparseMatrix - * @param {number[]} index Zero-based index - * @param {*} v - * @param {*} [defaultValue] Default value, filled in on new entries when - * the matrix is resized. If not provided, - * new matrix elements will be set to zero. - * @return {SparseMatrix} self - */ - - SparseMatrix.prototype.set = function (index, v, defaultValue) { - if (!Object(is["b" /* isArray */])(index)) { - throw new TypeError('Array expected'); - } - - if (index.length !== this._size.length) { - throw new DimensionError["a" /* DimensionError */](index.length, this._size.length); - } // check it is a pattern matrix - - if (!this._values) { - throw new Error('Cannot invoke set on a Pattern only matrix'); - } // row and column - - var i = index[0]; - var j = index[1]; // rows & columns - - var rows = this._size[0]; - var columns = this._size[1]; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; - - if (Object(is["I" /* isString */])(this._datatype)) { - // find signature that matches (datatype, datatype) - eq = typed.find(equalScalar, [this._datatype, this._datatype]) || equalScalar; // convert 0 to the same datatype - - zero = typed.convert(0, this._datatype); - } // check we need to resize matrix - - if (i > rows - 1 || j > columns - 1) { - // resize matrix - _resize(this, Math.max(i + 1, rows), Math.max(j + 1, columns), defaultValue); // update rows & columns - - rows = this._size[0]; - columns = this._size[1]; - } // check i, j are valid - - Object(utils_array["q" /* validateIndex */])(i, rows); - Object(utils_array["q" /* validateIndex */])(j, columns); // find value index - - var k = _getValueIndex(i, this._ptr[j], this._ptr[j + 1], this._index); // check k is prior to next column k and it is in the correct row - - if (k < this._ptr[j + 1] && this._index[k] === i) { - // check value != 0 - if (!eq(v, zero)) { - // update value - this._values[k] = v; - } else { - // remove value from matrix - _remove(k, j, this._values, this._index, this._ptr); - } - } else { - // insert value @ (i, j) - _insert(k, i, j, v, this._values, this._index, this._ptr); - } - - return this; - }; - - function _getValueIndex(i, top, bottom, index) { - // check row is on the bottom side - if (bottom - top === 0) { - return bottom; - } // loop rows [top, bottom[ - - for (var r = top; r < bottom; r++) { - // check we found value index - if (index[r] === i) { - return r; - } - } // we did not find row - - return top; - } - - function _remove(k, j, values, index, ptr) { - // remove value @ k - values.splice(k, 1); - index.splice(k, 1); // update pointers - - for (var x = j + 1; x < ptr.length; x++) { - ptr[x]--; - } - } - - function _insert(k, i, j, v, values, index, ptr) { - // insert value - values.splice(k, 0, v); // update row for k - - index.splice(k, 0, i); // update column pointers - - for (var x = j + 1; x < ptr.length; x++) { - ptr[x]++; - } - } - /** - * Resize the matrix to the given size. Returns a copy of the matrix when - * `copy=true`, otherwise return the matrix itself (resize in place). - * - * @memberof SparseMatrix - * @param {number[] | Matrix} size The new size the matrix should have. - * @param {*} [defaultValue=0] Default value, filled in on new entries. - * If not provided, the matrix elements will - * be filled with zeros. - * @param {boolean} [copy] Return a resized copy of the matrix - * - * @return {Matrix} The resized matrix - */ - - SparseMatrix.prototype.resize = function (size, defaultValue, copy) { - // validate arguments - if (!Object(is["i" /* isCollection */])(size)) { - throw new TypeError('Array or Matrix expected'); - } // SparseMatrix input is always 2d, flatten this into 1d if it's indeed a vector - - var sizeArray = size.valueOf().map(function (value) { - return Array.isArray(value) && value.length === 1 ? value[0] : value; - }); - - if (sizeArray.length !== 2) { - throw new Error('Only two dimensions matrix are supported'); - } // check sizes - - sizeArray.forEach(function (value) { - if (!Object(is["y" /* isNumber */])(value) || !Object(utils_number["i" /* isInteger */])(value) || value < 0) { - throw new TypeError('Invalid size, must contain positive integers ' + '(size: ' + Object(utils_string["d" /* format */])(sizeArray) + ')'); - } - }); // matrix to resize - - var m = copy ? this.clone() : this; // resize matrix - - return _resize(m, sizeArray[0], sizeArray[1], defaultValue); - }; - - function _resize(matrix, rows, columns, defaultValue) { - // value to insert at the time of growing matrix - var value = defaultValue || 0; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; - - if (Object(is["I" /* isString */])(matrix._datatype)) { - // find signature that matches (datatype, datatype) - eq = typed.find(equalScalar, [matrix._datatype, matrix._datatype]) || equalScalar; // convert 0 to the same datatype - - zero = typed.convert(0, matrix._datatype); // convert value to the same datatype - - value = typed.convert(value, matrix._datatype); - } // should we insert the value? - - var ins = !eq(value, zero); // old columns and rows - - var r = matrix._size[0]; - var c = matrix._size[1]; - var i, j, k; // check we need to increase columns - - if (columns > c) { - // loop new columns - for (j = c; j < columns; j++) { - // update matrix._ptr for current column - matrix._ptr[j] = matrix._values.length; // check we need to insert matrix._values - - if (ins) { - // loop rows - for (i = 0; i < r; i++) { - // add new matrix._values - matrix._values.push(value); // update matrix._index - - matrix._index.push(i); - } - } - } // store number of matrix._values in matrix._ptr - - matrix._ptr[columns] = matrix._values.length; - } else if (columns < c) { - // truncate matrix._ptr - matrix._ptr.splice(columns + 1, c - columns); // truncate matrix._values and matrix._index - - matrix._values.splice(matrix._ptr[columns], matrix._values.length); - - matrix._index.splice(matrix._ptr[columns], matrix._index.length); - } // update columns - - c = columns; // check we need to increase rows - - if (rows > r) { - // check we have to insert values - if (ins) { - // inserts - var n = 0; // loop columns - - for (j = 0; j < c; j++) { - // update matrix._ptr for current column - matrix._ptr[j] = matrix._ptr[j] + n; // where to insert matrix._values - - k = matrix._ptr[j + 1] + n; // pointer - - var p = 0; // loop new rows, initialize pointer - - for (i = r; i < rows; i++, p++) { - // add value - matrix._values.splice(k + p, 0, value); // update matrix._index - - matrix._index.splice(k + p, 0, i); // increment inserts - - n++; - } - } // store number of matrix._values in matrix._ptr - - matrix._ptr[c] = matrix._values.length; - } - } else if (rows < r) { - // deletes - var d = 0; // loop columns - - for (j = 0; j < c; j++) { - // update matrix._ptr for current column - matrix._ptr[j] = matrix._ptr[j] - d; // where matrix._values start for next column - - var k0 = matrix._ptr[j]; - var k1 = matrix._ptr[j + 1] - d; // loop matrix._index - - for (k = k0; k < k1; k++) { - // row - i = matrix._index[k]; // check we need to delete value and matrix._index - - if (i > rows - 1) { - // remove value - matrix._values.splice(k, 1); // remove item from matrix._index - - matrix._index.splice(k, 1); // increase deletes - - d++; - } - } - } // update matrix._ptr for current column - - matrix._ptr[j] = matrix._values.length; - } // update matrix._size - - matrix._size[0] = rows; - matrix._size[1] = columns; // return matrix - - return matrix; - } - /** - * Reshape the matrix to the given size. Returns a copy of the matrix when - * `copy=true`, otherwise return the matrix itself (reshape in place). - * - * NOTE: This might be better suited to copy by default, instead of modifying - * in place. For now, it operates in place to remain consistent with - * resize(). - * - * @memberof SparseMatrix - * @param {number[]} size The new size the matrix should have. - * @param {boolean} [copy] Return a reshaped copy of the matrix - * - * @return {Matrix} The reshaped matrix - */ - - SparseMatrix.prototype.reshape = function (size, copy) { - // validate arguments - if (!Object(is["b" /* isArray */])(size)) { - throw new TypeError('Array expected'); - } - - if (size.length !== 2) { - throw new Error('Sparse matrices can only be reshaped in two dimensions'); - } // check sizes - - size.forEach(function (value) { - if (!Object(is["y" /* isNumber */])(value) || !Object(utils_number["i" /* isInteger */])(value) || value < 0) { - throw new TypeError('Invalid size, must contain positive integers ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - }); // m * n must not change - - if (this._size[0] * this._size[1] !== size[0] * size[1]) { - throw new Error('Reshaping sparse matrix will result in the wrong number of elements'); - } // matrix to reshape - - var m = copy ? this.clone() : this; // return unchanged if the same shape - - if (this._size[0] === size[0] && this._size[1] === size[1]) { - return m; - } // Convert to COO format (generate a column index) - - var colIndex = []; - - for (var i = 0; i < m._ptr.length; i++) { - for (var j = 0; j < m._ptr[i + 1] - m._ptr[i]; j++) { - colIndex.push(i); - } - } // Clone the values array - - var values = m._values.slice(); // Clone the row index array - - var rowIndex = m._index.slice(); // Transform the (row, column) indices - - for (var _i = 0; _i < m._index.length; _i++) { - var r1 = rowIndex[_i]; - var c1 = colIndex[_i]; - var flat = r1 * m._size[1] + c1; - colIndex[_i] = flat % size[1]; - rowIndex[_i] = Math.floor(flat / size[1]); - } // Now reshaping is supposed to preserve the row-major order, BUT these sparse matrices are stored - // in column-major order, so we have to reorder the value array now. One option is to use a multisort, - // sorting several arrays based on some other array. - // OR, we could easily just: - // 1. Remove all values from the matrix - - m._values.length = 0; - m._index.length = 0; - m._ptr.length = size[1] + 1; - m._size = size.slice(); - - for (var _i2 = 0; _i2 < m._ptr.length; _i2++) { - m._ptr[_i2] = 0; - } // 2. Re-insert all elements in the proper order (simplified code from SparseMatrix.prototype.set) - // This step is probably the most time-consuming - - for (var h = 0; h < values.length; h++) { - var _i3 = rowIndex[h]; - var _j = colIndex[h]; - var v = values[h]; - - var k = _getValueIndex(_i3, m._ptr[_j], m._ptr[_j + 1], m._index); - - _insert(k, _i3, _j, v, m._values, m._index, m._ptr); - } // The value indices are inserted out of order, but apparently that's... still OK? - - return m; - }; - /** - * Create a clone of the matrix - * @memberof SparseMatrix - * @return {SparseMatrix} clone - */ - - SparseMatrix.prototype.clone = function () { - var m = new SparseMatrix({ - values: this._values ? Object(utils_object["a" /* clone */])(this._values) : undefined, - index: Object(utils_object["a" /* clone */])(this._index), - ptr: Object(utils_object["a" /* clone */])(this._ptr), - size: Object(utils_object["a" /* clone */])(this._size), - datatype: this._datatype - }); - return m; - }; - /** - * Retrieve the size of the matrix. - * @memberof SparseMatrix - * @returns {number[]} size - */ - - SparseMatrix.prototype.size = function () { - return this._size.slice(0); // copy the Array - }; - /** - * Create a new matrix with the results of the callback function executed on - * each entry of the matrix. - * @memberof SparseMatrix - * @param {Function} callback The callback function is invoked with three - * parameters: the value of the element, the index - * of the element, and the Matrix being traversed. - * @param {boolean} [skipZeros] Invoke callback function for non-zero values only. - * - * @return {SparseMatrix} matrix - */ - - SparseMatrix.prototype.map = function (callback, skipZeros) { - // check it is a pattern matrix - if (!this._values) { - throw new Error('Cannot invoke map on a Pattern only matrix'); - } // matrix instance - - var me = this; // rows and columns - - var rows = this._size[0]; - var columns = this._size[1]; // invoke callback - - var invoke = function invoke(v, i, j) { - // invoke callback - return callback(v, [i, j], me); - }; // invoke _map - - return _map(this, 0, rows - 1, 0, columns - 1, invoke, skipZeros); - }; - /** - * Create a new matrix with the results of the callback function executed on the interval - * [minRow..maxRow, minColumn..maxColumn]. - */ - - function _map(matrix, minRow, maxRow, minColumn, maxColumn, callback, skipZeros) { - // result arrays - var values = []; - var index = []; - var ptr = []; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; - - if (Object(is["I" /* isString */])(matrix._datatype)) { - // find signature that matches (datatype, datatype) - eq = typed.find(equalScalar, [matrix._datatype, matrix._datatype]) || equalScalar; // convert 0 to the same datatype - - zero = typed.convert(0, matrix._datatype); - } // invoke callback - - var invoke = function invoke(v, x, y) { - // invoke callback - v = callback(v, x, y); // check value != 0 - - if (!eq(v, zero)) { - // store value - values.push(v); // index - - index.push(x); - } - }; // loop columns - - for (var j = minColumn; j <= maxColumn; j++) { - // store pointer to values index - ptr.push(values.length); // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] - - var k0 = matrix._ptr[j]; - var k1 = matrix._ptr[j + 1]; - - if (skipZeros) { - // loop k within [k0, k1[ - for (var k = k0; k < k1; k++) { - // row index - var i = matrix._index[k]; // check i is in range - - if (i >= minRow && i <= maxRow) { - // value @ k - invoke(matrix._values[k], i - minRow, j - minColumn); - } - } - } else { - // create a cache holding all defined values - var _values = {}; - - for (var _k = k0; _k < k1; _k++) { - var _i4 = matrix._index[_k]; - _values[_i4] = matrix._values[_k]; - } // loop over all rows (indexes can be unordered so we can't use that), - // and either read the value or zero - - for (var _i5 = minRow; _i5 <= maxRow; _i5++) { - var value = _i5 in _values ? _values[_i5] : 0; - invoke(value, _i5 - minRow, j - minColumn); - } - } - } // store number of values in ptr - - ptr.push(values.length); // return sparse matrix - - return new SparseMatrix({ - values: values, - index: index, - ptr: ptr, - size: [maxRow - minRow + 1, maxColumn - minColumn + 1] - }); - } - /** - * Execute a callback function on each entry of the matrix. - * @memberof SparseMatrix - * @param {Function} callback The callback function is invoked with three - * parameters: the value of the element, the index - * of the element, and the Matrix being traversed. - * @param {boolean} [skipZeros] Invoke callback function for non-zero values only. - */ - - SparseMatrix.prototype.forEach = function (callback, skipZeros) { - // check it is a pattern matrix - if (!this._values) { - throw new Error('Cannot invoke forEach on a Pattern only matrix'); - } // matrix instance - - var me = this; // rows and columns - - var rows = this._size[0]; - var columns = this._size[1]; // loop columns - - for (var j = 0; j < columns; j++) { - // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] - var k0 = this._ptr[j]; - var k1 = this._ptr[j + 1]; - - if (skipZeros) { - // loop k within [k0, k1[ - for (var k = k0; k < k1; k++) { - // row index - var i = this._index[k]; // value @ k - - callback(this._values[k], [i, j], me); - } - } else { - // create a cache holding all defined values - var values = {}; - - for (var _k2 = k0; _k2 < k1; _k2++) { - var _i6 = this._index[_k2]; - values[_i6] = this._values[_k2]; - } // loop over all rows (indexes can be unordered so we can't use that), - // and either read the value or zero - - for (var _i7 = 0; _i7 < rows; _i7++) { - var value = _i7 in values ? values[_i7] : 0; - callback(value, [_i7, j], me); - } - } - } - }; - /** - * Create an Array with a copy of the data of the SparseMatrix - * @memberof SparseMatrix - * @returns {Array} array - */ - - SparseMatrix.prototype.toArray = function () { - return _toArray(this._values, this._index, this._ptr, this._size, true); - }; - /** - * Get the primitive value of the SparseMatrix: a two dimensions array - * @memberof SparseMatrix - * @returns {Array} array - */ - - SparseMatrix.prototype.valueOf = function () { - return _toArray(this._values, this._index, this._ptr, this._size, false); - }; - - function _toArray(values, index, ptr, size, copy) { - // rows and columns - var rows = size[0]; - var columns = size[1]; // result - - var a = []; // vars - - var i, j; // initialize array - - for (i = 0; i < rows; i++) { - a[i] = []; - - for (j = 0; j < columns; j++) { - a[i][j] = 0; - } - } // loop columns - - for (j = 0; j < columns; j++) { - // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] - var k0 = ptr[j]; - var k1 = ptr[j + 1]; // loop k within [k0, k1[ - - for (var k = k0; k < k1; k++) { - // row index - i = index[k]; // set value (use one for pattern matrix) - - a[i][j] = values ? copy ? Object(utils_object["a" /* clone */])(values[k]) : values[k] : 1; - } - } - - return a; - } - /** - * Get a string representation of the matrix, with optional formatting options. - * @memberof SparseMatrix - * @param {Object | number | Function} [options] Formatting options. See - * lib/utils/number:format for a - * description of the available - * options. - * @returns {string} str - */ - - SparseMatrix.prototype.format = function (options) { - // rows and columns - var rows = this._size[0]; - var columns = this._size[1]; // density - - var density = this.density(); // rows & columns - - var str = 'Sparse Matrix [' + Object(utils_string["d" /* format */])(rows, options) + ' x ' + Object(utils_string["d" /* format */])(columns, options) + '] density: ' + Object(utils_string["d" /* format */])(density, options) + '\n'; // loop columns - - for (var j = 0; j < columns; j++) { - // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] - var k0 = this._ptr[j]; - var k1 = this._ptr[j + 1]; // loop k within [k0, k1[ - - for (var k = k0; k < k1; k++) { - // row index - var i = this._index[k]; // append value - - str += '\n (' + Object(utils_string["d" /* format */])(i, options) + ', ' + Object(utils_string["d" /* format */])(j, options) + ') ==> ' + (this._values ? Object(utils_string["d" /* format */])(this._values[k], options) : 'X'); - } - } - - return str; - }; - /** - * Get a string representation of the matrix - * @memberof SparseMatrix - * @returns {string} str - */ - - SparseMatrix.prototype.toString = function () { - return Object(utils_string["d" /* format */])(this.toArray()); - }; - /** - * Get a JSON representation of the matrix - * @memberof SparseMatrix - * @returns {Object} - */ - - SparseMatrix.prototype.toJSON = function () { - return { - mathjs: 'SparseMatrix', - values: this._values, - index: this._index, - ptr: this._ptr, - size: this._size, - datatype: this._datatype - }; - }; - /** - * Get the kth Matrix diagonal. - * - * @memberof SparseMatrix - * @param {number | BigNumber} [k=0] The kth diagonal where the vector will retrieved. - * - * @returns {Matrix} The matrix vector with the diagonal values. - */ - - SparseMatrix.prototype.diagonal = function (k) { - // validate k if any - if (k) { - // convert BigNumber to a number - if (Object(is["e" /* isBigNumber */])(k)) { - k = k.toNumber(); - } // is must be an integer - - if (!Object(is["y" /* isNumber */])(k) || !Object(utils_number["i" /* isInteger */])(k)) { - throw new TypeError('The parameter k must be an integer number'); - } - } else { - // default value - k = 0; - } - - var kSuper = k > 0 ? k : 0; - var kSub = k < 0 ? -k : 0; // rows & columns - - var rows = this._size[0]; - var columns = this._size[1]; // number diagonal values - - var n = Math.min(rows - kSub, columns - kSuper); // diagonal arrays - - var values = []; - var index = []; - var ptr = []; // initial ptr value - - ptr[0] = 0; // loop columns - - for (var j = kSuper; j < columns && values.length < n; j++) { - // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] - var k0 = this._ptr[j]; - var k1 = this._ptr[j + 1]; // loop x within [k0, k1[ - - for (var x = k0; x < k1; x++) { - // row index - var i = this._index[x]; // check row - - if (i === j - kSuper + kSub) { - // value on this column - values.push(this._values[x]); // store row - - index[values.length - 1] = i - kSub; // exit loop - - break; - } - } - } // close ptr - - ptr.push(values.length); // return matrix - - return new SparseMatrix({ - values: values, - index: index, - ptr: ptr, - size: [n, 1] - }); - }; - /** - * Generate a matrix from a JSON object - * @memberof SparseMatrix - * @param {Object} json An object structured like - * `{"mathjs": "SparseMatrix", "values": [], "index": [], "ptr": [], "size": []}`, - * where mathjs is optional - * @returns {SparseMatrix} - */ - - SparseMatrix.fromJSON = function (json) { - return new SparseMatrix(json); - }; - /** - * Create a diagonal matrix. - * - * @memberof SparseMatrix - * @param {Array} size The matrix size. - * @param {number | Array | Matrix } value The values for the diagonal. - * @param {number | BigNumber} [k=0] The kth diagonal where the vector will be filled in. - * @param {number} [defaultValue] The default value for non-diagonal - * @param {string} [datatype] The Matrix datatype, values must be of this datatype. - * - * @returns {SparseMatrix} - */ - - SparseMatrix.diagonal = function (size, value, k, defaultValue, datatype) { - if (!Object(is["b" /* isArray */])(size)) { - throw new TypeError('Array expected, size parameter'); - } - - if (size.length !== 2) { - throw new Error('Only two dimensions matrix are supported'); - } // map size & validate - - size = size.map(function (s) { - // check it is a big number - if (Object(is["e" /* isBigNumber */])(s)) { - // convert it - s = s.toNumber(); - } // validate arguments - - if (!Object(is["y" /* isNumber */])(s) || !Object(utils_number["i" /* isInteger */])(s) || s < 1) { - throw new Error('Size values must be positive integers'); - } - - return s; - }); // validate k if any - - if (k) { - // convert BigNumber to a number - if (Object(is["e" /* isBigNumber */])(k)) { - k = k.toNumber(); - } // is must be an integer - - if (!Object(is["y" /* isNumber */])(k) || !Object(utils_number["i" /* isInteger */])(k)) { - throw new TypeError('The parameter k must be an integer number'); - } - } else { - // default value - k = 0; - } // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; - - if (Object(is["I" /* isString */])(datatype)) { - // find signature that matches (datatype, datatype) - eq = typed.find(equalScalar, [datatype, datatype]) || equalScalar; // convert 0 to the same datatype - - zero = typed.convert(0, datatype); - } - - var kSuper = k > 0 ? k : 0; - var kSub = k < 0 ? -k : 0; // rows and columns - - var rows = size[0]; - var columns = size[1]; // number of non-zero items - - var n = Math.min(rows - kSub, columns - kSuper); // value extraction function - - var _value; // check value - - if (Object(is["b" /* isArray */])(value)) { - // validate array - if (value.length !== n) { - // number of values in array must be n - throw new Error('Invalid value array length'); - } // define function - - _value = function _value(i) { - // return value @ i - return value[i]; - }; - } else if (Object(is["v" /* isMatrix */])(value)) { - // matrix size - var ms = value.size(); // validate matrix - - if (ms.length !== 1 || ms[0] !== n) { - // number of values in array must be n - throw new Error('Invalid matrix length'); - } // define function - - _value = function _value(i) { - // return value @ i - return value.get([i]); - }; - } else { - // define function - _value = function _value() { - // return value - return value; - }; - } // create arrays - - var values = []; - var index = []; - var ptr = []; // loop items - - for (var j = 0; j < columns; j++) { - // number of rows with value - ptr.push(values.length); // diagonal index - - var i = j - kSuper; // check we need to set diagonal value - - if (i >= 0 && i < n) { - // get value @ i - var v = _value(i); // check for zero - - if (!eq(v, zero)) { - // column - index.push(i + kSub); // add value - - values.push(v); - } - } - } // last value should be number of values - - ptr.push(values.length); // create SparseMatrix - - return new SparseMatrix({ - values: values, - index: index, - ptr: ptr, - size: [rows, columns] - }); - }; - /** - * Swap rows i and j in Matrix. - * - * @memberof SparseMatrix - * @param {number} i Matrix row index 1 - * @param {number} j Matrix row index 2 - * - * @return {Matrix} The matrix reference - */ - - SparseMatrix.prototype.swapRows = function (i, j) { - // check index - if (!Object(is["y" /* isNumber */])(i) || !Object(utils_number["i" /* isInteger */])(i) || !Object(is["y" /* isNumber */])(j) || !Object(utils_number["i" /* isInteger */])(j)) { - throw new Error('Row index must be positive integers'); - } // check dimensions - - if (this._size.length !== 2) { - throw new Error('Only two dimensional matrix is supported'); - } // validate index - - Object(utils_array["q" /* validateIndex */])(i, this._size[0]); - Object(utils_array["q" /* validateIndex */])(j, this._size[0]); // swap rows - - SparseMatrix._swapRows(i, j, this._size[1], this._values, this._index, this._ptr); // return current instance - - return this; - }; - /** - * Loop rows with data in column j. - * - * @param {number} j Column - * @param {Array} values Matrix values - * @param {Array} index Matrix row indeces - * @param {Array} ptr Matrix column pointers - * @param {Function} callback Callback function invoked for every row in column j - */ - - SparseMatrix._forEachRow = function (j, values, index, ptr, callback) { - // indeces for column j - var k0 = ptr[j]; - var k1 = ptr[j + 1]; // loop - - for (var k = k0; k < k1; k++) { - // invoke callback - callback(index[k], values[k]); - } - }; - /** - * Swap rows x and y in Sparse Matrix data structures. - * - * @param {number} x Matrix row index 1 - * @param {number} y Matrix row index 2 - * @param {number} columns Number of columns in matrix - * @param {Array} values Matrix values - * @param {Array} index Matrix row indeces - * @param {Array} ptr Matrix column pointers - */ - - SparseMatrix._swapRows = function (x, y, columns, values, index, ptr) { - // loop columns - for (var j = 0; j < columns; j++) { - // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] - var k0 = ptr[j]; - var k1 = ptr[j + 1]; // find value index @ x - - var kx = _getValueIndex(x, k0, k1, index); // find value index @ x - - var ky = _getValueIndex(y, k0, k1, index); // check both rows exist in matrix - - if (kx < k1 && ky < k1 && index[kx] === x && index[ky] === y) { - // swap values (check for pattern matrix) - if (values) { - var v = values[kx]; - values[kx] = values[ky]; - values[ky] = v; - } // next column - - continue; - } // check x row exist & no y row - - if (kx < k1 && index[kx] === x && (ky >= k1 || index[ky] !== y)) { - // value @ x (check for pattern matrix) - var vx = values ? values[kx] : undefined; // insert value @ y - - index.splice(ky, 0, y); - - if (values) { - values.splice(ky, 0, vx); - } // remove value @ x (adjust array index if needed) - - index.splice(ky <= kx ? kx + 1 : kx, 1); - - if (values) { - values.splice(ky <= kx ? kx + 1 : kx, 1); - } // next column - - continue; - } // check y row exist & no x row - - if (ky < k1 && index[ky] === y && (kx >= k1 || index[kx] !== x)) { - // value @ y (check for pattern matrix) - var vy = values ? values[ky] : undefined; // insert value @ x - - index.splice(kx, 0, x); - - if (values) { - values.splice(kx, 0, vy); - } // remove value @ y (adjust array index if needed) - - index.splice(kx <= ky ? ky + 1 : ky, 1); - - if (values) { - values.splice(kx <= ky ? ky + 1 : ky, 1); - } - } - } - }; - - return SparseMatrix; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/type/number.js - - var number_name = 'number'; - var number_dependencies = ['typed']; - var createNumber = /* #__PURE__ */Object(factory["a" /* factory */])(number_name, number_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Create a number or convert a string, boolean, or unit to a number. - * When value is a matrix, all elements will be converted to number. - * - * Syntax: - * - * math.number(value) - * math.number(unit, valuelessUnit) - * - * Examples: - * - * math.number(2) // returns number 2 - * math.number('7.2') // returns number 7.2 - * math.number(true) // returns number 1 - * math.number([true, false, true, true]) // returns [1, 0, 1, 1] - * math.number(math.unit('52cm'), 'm') // returns 0.52 - * - * See also: - * - * bignumber, boolean, complex, index, matrix, string, unit - * - * @param {string | number | BigNumber | Fraction | boolean | Array | Matrix | Unit | null} [value] Value to be converted - * @param {Unit | string} [valuelessUnit] A valueless unit, used to convert a unit to a number - * @return {number | Array | Matrix} The created number - */ - var number = typed('number', { - '': function _() { - return 0; - }, - number: function number(x) { - return x; - }, - string: function string(x) { - if (x === 'NaN') { - return NaN; - } - var num = Number(x); - - if (isNaN(num)) { - throw new SyntaxError('String "' + x + '" is no valid number'); - } - - if (['0b', '0o', '0x'].includes(x.substring(0, 2))) { - if (num > Math.pow(2, 32) - 1) { - throw new SyntaxError("String \"".concat(x, "\" is out of range")); - } - - if (num & 0x80000000) { - num = -1 * ~(num - 1); - } - } - - return num; - }, - BigNumber: function BigNumber(x) { - return x.toNumber(); - }, - Fraction: function Fraction(x) { - return x.valueOf(); - }, - Unit: function Unit(x) { - throw new Error('Second argument with valueless unit expected'); - }, - "null": function _null(x) { - return 0; - }, - 'Unit, string | Unit': function UnitStringUnit(unit, valuelessUnit) { - return unit.toNumber(valuelessUnit); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); // reviver function to parse a JSON object like: - // - // {"mathjs":"number","value":"2.3"} - // - // into a number 2.3 - - number.fromJSON = function (json) { - return parseFloat(json.value); - }; - - return number; - }); - // CONCATENATED MODULE: ./src/type/string.js - - var string_name = 'string'; - var string_dependencies = ['typed']; - var createString = /* #__PURE__ */Object(factory["a" /* factory */])(string_name, string_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Create a string or convert any object into a string. - * Elements of Arrays and Matrices are processed element wise. - * - * Syntax: - * - * math.string(value) - * - * Examples: - * - * math.string(4.2) // returns string '4.2' - * math.string(math.complex(3, 2) // returns string '3 + 2i' - * - * const u = math.unit(5, 'km') - * math.string(u.to('m')) // returns string '5000 m' - * - * math.string([true, false]) // returns ['true', 'false'] - * - * See also: - * - * bignumber, boolean, complex, index, matrix, number, unit - * - * @param {* | Array | Matrix | null} [value] A value to convert to a string - * @return {string | Array | Matrix} The created string - */ - return typed(string_name, { - '': function _() { - return ''; - }, - number: utils_number["h" /* format */], - "null": function _null(x) { - return 'null'; - }, - "boolean": function boolean(x) { - return x + ''; - }, - string: function string(x) { - return x; - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - }, - any: function any(x) { - return String(x); - } - }); - }); - // CONCATENATED MODULE: ./src/type/boolean.js - - var boolean_name = 'boolean'; - var boolean_dependencies = ['typed']; - var createBoolean = /* #__PURE__ */Object(factory["a" /* factory */])(boolean_name, boolean_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Create a boolean or convert a string or number to a boolean. - * In case of a number, `true` is returned for non-zero numbers, and `false` in - * case of zero. - * Strings can be `'true'` or `'false'`, or can contain a number. - * When value is a matrix, all elements will be converted to boolean. - * - * Syntax: - * - * math.boolean(x) - * - * Examples: - * - * math.boolean(0) // returns false - * math.boolean(1) // returns true - * math.boolean(-3) // returns true - * math.boolean('true') // returns true - * math.boolean('false') // returns false - * math.boolean([1, 0, 1, 1]) // returns [true, false, true, true] - * - * See also: - * - * bignumber, complex, index, matrix, string, unit - * - * @param {string | number | boolean | Array | Matrix | null} value A value of any type - * @return {boolean | Array | Matrix} The boolean value - */ - return typed(boolean_name, { - '': function _() { - return false; - }, - "boolean": function boolean(x) { - return x; - }, - number: function number(x) { - return !!x; - }, - "null": function _null(x) { - return false; - }, - BigNumber: function BigNumber(x) { - return !x.isZero(); - }, - string: function string(x) { - // try case insensitive - var lcase = x.toLowerCase(); - - if (lcase === 'true') { - return true; - } else if (lcase === 'false') { - return false; - } // test whether value is a valid number - - var num = Number(x); - - if (x !== '' && !isNaN(num)) { - return !!num; - } - - throw new Error('Cannot convert "' + x + '" to a boolean'); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/type/bignumber/function/bignumber.js - - var bignumber_name = 'bignumber'; - var bignumber_dependencies = ['typed', 'BigNumber']; - var createBignumber = /* #__PURE__ */Object(factory["a" /* factory */])(bignumber_name, bignumber_dependencies, function (_ref) { - var typed = _ref.typed, - BigNumber = _ref.BigNumber; - - /** - * Create a BigNumber, which can store numbers with arbitrary precision. - * When a matrix is provided, all elements will be converted to BigNumber. - * - * Syntax: - * - * math.bignumber(x) - * - * Examples: - * - * 0.1 + 0.2 // returns number 0.30000000000000004 - * math.bignumber(0.1) + math.bignumber(0.2) // returns BigNumber 0.3 - * - * - * 7.2e500 // returns number Infinity - * math.bignumber('7.2e500') // returns BigNumber 7.2e500 - * - * See also: - * - * boolean, complex, index, matrix, string, unit - * - * @param {number | string | Fraction | BigNumber | Array | Matrix | boolean | null} [value] Value for the big number, - * 0 by default. - * @returns {BigNumber} The created bignumber - */ - return typed('bignumber', { - '': function _() { - return new BigNumber(0); - }, - number: function number(x) { - // convert to string to prevent errors in case of >15 digits - return new BigNumber(x + ''); - }, - string: function string(x) { - return new BigNumber(x); - }, - BigNumber: function BigNumber(x) { - // we assume a BigNumber is immutable - return x; - }, - Fraction: function Fraction(x) { - return new BigNumber(x.n).div(x.d).times(x.s); - }, - "null": function _null(x) { - return new BigNumber(0); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/type/complex/function/complex.js - - var complex_name = 'complex'; - var complex_dependencies = ['typed', 'Complex']; - var createComplex = /* #__PURE__ */Object(factory["a" /* factory */])(complex_name, complex_dependencies, function (_ref) { - var typed = _ref.typed, - Complex = _ref.Complex; - - /** - * Create a complex value or convert a value to a complex value. - * - * Syntax: - * - * math.complex() // creates a complex value with zero - * // as real and imaginary part. - * math.complex(re : number, im : string) // creates a complex value with provided - * // values for real and imaginary part. - * math.complex(re : number) // creates a complex value with provided - * // real value and zero imaginary part. - * math.complex(complex : Complex) // clones the provided complex value. - * math.complex(arg : string) // parses a string into a complex value. - * math.complex(array : Array) // converts the elements of the array - * // or matrix element wise into a - * // complex value. - * math.complex({re: number, im: number}) // creates a complex value with provided - * // values for real an imaginary part. - * math.complex({r: number, phi: number}) // creates a complex value with provided - * // polar coordinates - * - * Examples: - * - * const a = math.complex(3, -4) // a = Complex 3 - 4i - * a.re = 5 // a = Complex 5 - 4i - * const i = a.im // Number -4 - * const b = math.complex('2 + 6i') // Complex 2 + 6i - * const c = math.complex() // Complex 0 + 0i - * const d = math.add(a, b) // Complex 5 + 2i - * - * See also: - * - * bignumber, boolean, index, matrix, number, string, unit - * - * @param {* | Array | Matrix} [args] - * Arguments specifying the real and imaginary part of the complex number - * @return {Complex | Array | Matrix} Returns a complex value - */ - return typed('complex', { - '': function _() { - return Complex.ZERO; - }, - number: function number(x) { - return new Complex(x, 0); - }, - 'number, number': function numberNumber(re, im) { - return new Complex(re, im); - }, - // TODO: this signature should be redundant - 'BigNumber, BigNumber': function BigNumberBigNumber(re, im) { - return new Complex(re.toNumber(), im.toNumber()); - }, - Fraction: function Fraction(x) { - return new Complex(x.valueOf(), 0); - }, - Complex: function Complex(x) { - return x.clone(); - }, - string: function string(x) { - return Complex(x); // for example '2 + 3i' - }, - "null": function _null(x) { - return Complex(0); - }, - Object: function Object(x) { - if ('re' in x && 'im' in x) { - return new Complex(x.re, x.im); - } - - if ('r' in x && 'phi' in x || 'abs' in x && 'arg' in x) { - return new Complex(x); - } - - throw new Error('Expected object with properties (re and im) or (r and phi) or (abs and arg)'); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/type/fraction/function/fraction.js - - var fraction_name = 'fraction'; - var fraction_dependencies = ['typed', 'Fraction']; - var createFraction = /* #__PURE__ */Object(factory["a" /* factory */])(fraction_name, fraction_dependencies, function (_ref) { - var typed = _ref.typed, - Fraction = _ref.Fraction; - - /** - * Create a fraction convert a value to a fraction. - * - * Syntax: - * math.fraction(numerator, denominator) - * math.fraction({n: numerator, d: denominator}) - * math.fraction(matrix: Array | Matrix) Turn all matrix entries - * into fractions - * - * Examples: - * - * math.fraction(1, 3) - * math.fraction('2/3') - * math.fraction({n: 2, d: 3}) - * math.fraction([0.2, 0.25, 1.25]) - * - * See also: - * - * bignumber, number, string, unit - * - * @param {number | string | Fraction | BigNumber | Array | Matrix} [args] - * Arguments specifying the numerator and denominator of - * the fraction - * @return {Fraction | Array | Matrix} Returns a fraction - */ - return typed('fraction', { - number: function number(x) { - if (!isFinite(x) || isNaN(x)) { - throw new Error(x + ' cannot be represented as a fraction'); - } - - return new Fraction(x); - }, - string: function string(x) { - return new Fraction(x); - }, - 'number, number': function numberNumber(numerator, denominator) { - return new Fraction(numerator, denominator); - }, - "null": function _null(x) { - return new Fraction(0); - }, - BigNumber: function BigNumber(x) { - return new Fraction(x.toString()); - }, - Fraction: function Fraction(x) { - return x; // fractions are immutable - }, - Object: function Object(x) { - return new Fraction(x); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/type/matrix/function/matrix.js - - var matrix_name = 'matrix'; - var matrix_dependencies = ['typed', 'Matrix', 'DenseMatrix', 'SparseMatrix']; - var createMatrix = /* #__PURE__ */Object(factory["a" /* factory */])(matrix_name, matrix_dependencies, function (_ref) { - var typed = _ref.typed, - Matrix = _ref.Matrix, - DenseMatrix = _ref.DenseMatrix, - SparseMatrix = _ref.SparseMatrix; - - /** - * Create a Matrix. The function creates a new `math.Matrix` object from - * an `Array`. A Matrix has utility functions to manipulate the data in the - * matrix, like getting the size and getting or setting values in the matrix. - * Supported storage formats are 'dense' and 'sparse'. - * - * Syntax: - * - * math.matrix() // creates an empty matrix using default storage format (dense). - * math.matrix(data) // creates a matrix with initial data using default storage format (dense). - * math.matrix('dense') // creates an empty matrix using the given storage format. - * math.matrix(data, 'dense') // creates a matrix with initial data using the given storage format. - * math.matrix(data, 'sparse') // creates a sparse matrix with initial data. - * math.matrix(data, 'sparse', 'number') // creates a sparse matrix with initial data, number data type. - * - * Examples: - * - * let m = math.matrix([[1, 2], [3, 4]]) - * m.size() // Array [2, 2] - * m.resize([3, 2], 5) - * m.valueOf() // Array [[1, 2], [3, 4], [5, 5]] - * m.get([1, 0]) // number 3 - * - * See also: - * - * bignumber, boolean, complex, index, number, string, unit, sparse - * - * @param {Array | Matrix} [data] A multi dimensional array - * @param {string} [format] The Matrix storage format - * - * @return {Matrix} The created matrix - */ - return typed(matrix_name, { - '': function _() { - return _create([]); - }, - string: function string(format) { - return _create([], format); - }, - 'string, string': function stringString(format, datatype) { - return _create([], format, datatype); - }, - Array: function Array(data) { - return _create(data); - }, - Matrix: function Matrix(data) { - return _create(data, data.storage()); - }, - 'Array | Matrix, string': _create, - 'Array | Matrix, string, string': _create - }); - /** - * Create a new Matrix with given storage format - * @param {Array} data - * @param {string} [format] - * @param {string} [datatype] - * @returns {Matrix} Returns a new Matrix - * @private - */ - - function _create(data, format, datatype) { - // get storage format constructor - if (format === 'dense' || format === 'default' || format === undefined) { - return new DenseMatrix(data, datatype); - } - - if (format === 'sparse') { - return new SparseMatrix(data, datatype); - } - - throw new TypeError('Unknown matrix type ' + JSON.stringify(format) + '.'); - } - }); - // CONCATENATED MODULE: ./src/type/unit/function/splitUnit.js - - var splitUnit_name = 'splitUnit'; - var splitUnit_dependencies = ['typed']; - var createSplitUnit = /* #__PURE__ */Object(factory["a" /* factory */])(splitUnit_name, splitUnit_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Split a unit in an array of units whose sum is equal to the original unit. - * - * Syntax: - * - * splitUnit(unit: Unit, parts: Array.) - * - * Example: - * - * math.splitUnit(new Unit(1, 'm'), ['feet', 'inch']) - * // [ 3 feet, 3.3700787401575 inch ] - * - * See also: - * - * unit - * - * @param {Array} [parts] An array of strings or valueless units. - * @return {Array} An array of units. - */ - return typed(splitUnit_name, { - 'Unit, Array': function UnitArray(unit, parts) { - return unit.splitUnit(parts); - } - }); - }); - // CONCATENATED MODULE: ./src/plain/number/arithmetic.js - - var arithmetic_n1 = 'number'; - var arithmetic_n2 = 'number, number'; - function absNumber(a) { - return Math.abs(a); - } - absNumber.signature = arithmetic_n1; - function addNumber(a, b) { - return a + b; - } - addNumber.signature = arithmetic_n2; - function subtractNumber(a, b) { - return a - b; - } - subtractNumber.signature = arithmetic_n2; - function multiplyNumber(a, b) { - return a * b; - } - multiplyNumber.signature = arithmetic_n2; - function divideNumber(a, b) { - return a / b; - } - divideNumber.signature = arithmetic_n2; - function unaryMinusNumber(x) { - return -x; - } - unaryMinusNumber.signature = arithmetic_n1; - function unaryPlusNumber(x) { - return x; - } - unaryPlusNumber.signature = arithmetic_n1; - function cbrtNumber(x) { - return Object(utils_number["d" /* cbrt */])(x); - } - cbrtNumber.signature = arithmetic_n1; - function ceilNumber(x) { - return Math.ceil(x); - } - ceilNumber.signature = arithmetic_n1; - function cubeNumber(x) { - return x * x * x; - } - cubeNumber.signature = arithmetic_n1; - function expNumber(x) { - return Math.exp(x); - } - expNumber.signature = arithmetic_n1; - function expm1Number(x) { - return Object(utils_number["g" /* expm1 */])(x); - } - expm1Number.signature = arithmetic_n1; - function fixNumber(x) { - return x > 0 ? Math.floor(x) : Math.ceil(x); - } - fixNumber.signature = arithmetic_n1; - function floorNumber(x) { - return Math.floor(x); - } - floorNumber.signature = arithmetic_n1; - /** - * Calculate gcd for numbers - * @param {number} a - * @param {number} b - * @returns {number} Returns the greatest common denominator of a and b - */ - - function gcdNumber(a, b) { - if (!Object(utils_number["i" /* isInteger */])(a) || !Object(utils_number["i" /* isInteger */])(b)) { - throw new Error('Parameters in function gcd must be integer numbers'); - } // https://en.wikipedia.org/wiki/Euclidean_algorithm - - var r; - - while (b !== 0) { - r = a % b; - a = b; - b = r; - } - - return a < 0 ? -a : a; - } - gcdNumber.signature = arithmetic_n2; - /** - * Calculate lcm for two numbers - * @param {number} a - * @param {number} b - * @returns {number} Returns the least common multiple of a and b - */ - - function lcmNumber(a, b) { - if (!Object(utils_number["i" /* isInteger */])(a) || !Object(utils_number["i" /* isInteger */])(b)) { - throw new Error('Parameters in function lcm must be integer numbers'); - } - - if (a === 0 || b === 0) { - return 0; - } // https://en.wikipedia.org/wiki/Euclidean_algorithm - // evaluate lcm here inline to reduce overhead - - var t; - var prod = a * b; - - while (b !== 0) { - t = b; - b = a % t; - a = t; - } - - return Math.abs(prod / a); - } - lcmNumber.signature = arithmetic_n2; - /** - * Calculate the logarithm of a value. - * @param {number} x - * @return {number} - */ - - function logNumber(x) { - return Math.log(x); - } - logNumber.signature = arithmetic_n1; - /** - * Calculate the 10-base logarithm of a number - * @param {number} x - * @return {number} - */ - - function log10Number(x) { - return Object(utils_number["j" /* log10 */])(x); - } - log10Number.signature = arithmetic_n1; - /** - * Calculate the 2-base logarithm of a number - * @param {number} x - * @return {number} - */ - - function log2Number(x) { - return Object(utils_number["l" /* log2 */])(x); - } - log2Number.signature = arithmetic_n1; - /** - * Calculate the natural logarithm of a `number+1` - * @param {number} x - * @returns {number} - */ - - function log1pNumber(x) { - return Object(utils_number["k" /* log1p */])(x); - } - log1pNumber.signature = arithmetic_n1; - /** - * Calculate the modulus of two numbers - * @param {number} x - * @param {number} y - * @returns {number} res - * @private - */ - - function modNumber(x, y) { - if (y > 0) { - // We don't use JavaScript's % operator here as this doesn't work - // correctly for x < 0 and x === 0 - // see https://en.wikipedia.org/wiki/Modulo_operation - return x - y * Math.floor(x / y); - } else if (y === 0) { - return x; - } else { - // y < 0 - // TODO: implement mod for a negative divisor - throw new Error('Cannot calculate mod for a negative divisor'); - } - } - modNumber.signature = arithmetic_n2; - /** - * Calculate the nth root of a, solve x^root == a - * http://rosettacode.org/wiki/Nth_root#JavaScript - * @param {number} a - * @param {number} root - * @private - */ - - function nthRootNumber(a, root) { - var inv = root < 0; - - if (inv) { - root = -root; - } - - if (root === 0) { - throw new Error('Root must be non-zero'); - } - - if (a < 0 && Math.abs(root) % 2 !== 1) { - throw new Error('Root must be odd when a is negative.'); - } // edge cases zero and infinity - - if (a === 0) { - return inv ? Infinity : 0; - } - - if (!isFinite(a)) { - return inv ? 0 : a; - } - - var x = Math.pow(Math.abs(a), 1 / root); // If a < 0, we require that root is an odd integer, - // so (-1) ^ (1/root) = -1 - - x = a < 0 ? -x : x; - return inv ? 1 / x : x; // Very nice algorithm, but fails with nthRoot(-2, 3). - // Newton's method has some well-known problems at times: - // https://en.wikipedia.org/wiki/Newton%27s_method#Failure_analysis - - /* - let x = 1 // Initial guess - let xPrev = 1 - let i = 0 - const iMax = 10000 - do { - const delta = (a / Math.pow(x, root - 1) - x) / root - xPrev = x - x = x + delta - i++ - } - while (xPrev !== x && i < iMax) - if (xPrev !== x) { - throw new Error('Function nthRoot failed to converge') - } - return inv ? 1 / x : x - */ - } - nthRootNumber.signature = arithmetic_n2; - function signNumber(x) { - return Object(utils_number["n" /* sign */])(x); - } - signNumber.signature = arithmetic_n1; - function sqrtNumber(x) { - return Math.sqrt(x); - } - sqrtNumber.signature = arithmetic_n1; - function squareNumber(x) { - return x * x; - } - squareNumber.signature = arithmetic_n1; - /** - * Calculate xgcd for two numbers - * @param {number} a - * @param {number} b - * @return {number} result - * @private - */ - - function xgcdNumber(a, b) { - // source: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm - var t; // used to swap two variables - - var q; // quotient - - var r; // remainder - - var x = 0; - var lastx = 1; - var y = 1; - var lasty = 0; - - if (!Object(utils_number["i" /* isInteger */])(a) || !Object(utils_number["i" /* isInteger */])(b)) { - throw new Error('Parameters in function xgcd must be integer numbers'); - } - - while (b) { - q = Math.floor(a / b); - r = a - q * b; - t = x; - x = lastx - q * x; - lastx = t; - t = y; - y = lasty - q * y; - lasty = t; - a = b; - b = r; - } - - var res; - - if (a < 0) { - res = [-a, -lastx, -lasty]; - } else { - res = [a, a ? lastx : 0, lasty]; - } - - return res; - } - xgcdNumber.signature = arithmetic_n2; - /** - * Calculates the power of x to y, x^y, for two numbers. - * @param {number} x - * @param {number} y - * @return {number} res - */ - - function powNumber(x, y) { - // x^Infinity === 0 if -1 < x < 1 - // A real number 0 is returned instead of complex(0) - if (x * x < 1 && y === Infinity || x * x > 1 && y === -Infinity) { - return 0; - } - - return Math.pow(x, y); - } - powNumber.signature = arithmetic_n2; - /** - * round a number to the given number of decimals, or to zero if decimals is - * not provided - * @param {number} value - * @param {number} decimals number of decimals, between 0 and 15 (0 by default) - * @return {number} roundedValue - */ - - function roundNumber(value) { - var decimals = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; - return parseFloat(Object(utils_number["q" /* toFixed */])(value, decimals)); - } - roundNumber.signature = arithmetic_n2; - /** - * Calculate the norm of a number, the absolute value. - * @param {number} x - * @return {number} - */ - - function normNumber(x) { - return Math.abs(x); - } - normNumber.signature = arithmetic_n1; - // CONCATENATED MODULE: ./src/function/arithmetic/unaryMinus.js - - var unaryMinus_name = 'unaryMinus'; - var unaryMinus_dependencies = ['typed']; - var createUnaryMinus = /* #__PURE__ */Object(factory["a" /* factory */])(unaryMinus_name, unaryMinus_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Inverse the sign of a value, apply a unary minus operation. - * - * For matrices, the function is evaluated element wise. Boolean values and - * strings will be converted to a number. For complex numbers, both real and - * complex value are inverted. - * - * Syntax: - * - * math.unaryMinus(x) - * - * Examples: - * - * math.unaryMinus(3.5) // returns -3.5 - * math.unaryMinus(-4.2) // returns 4.2 - * - * See also: - * - * add, subtract, unaryPlus - * - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Number to be inverted. - * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Returns the value with inverted sign. - */ - return typed(unaryMinus_name, { - number: unaryMinusNumber, - Complex: function Complex(x) { - return x.neg(); - }, - BigNumber: function BigNumber(x) { - return x.neg(); - }, - Fraction: function Fraction(x) { - return x.neg(); - }, - Unit: function Unit(x) { - var res = x.clone(); - res.value = this(x.value); - return res; - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since unaryMinus(0) = 0 - return deepMap(x, this, true); - } // TODO: add support for string - - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/unaryPlus.js - - var unaryPlus_name = 'unaryPlus'; - var unaryPlus_dependencies = ['typed', 'config', 'BigNumber']; - var createUnaryPlus = /* #__PURE__ */Object(factory["a" /* factory */])(unaryPlus_name, unaryPlus_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - BigNumber = _ref.BigNumber; - - /** - * Unary plus operation. - * Boolean values and strings will be converted to a number, numeric values will be returned as is. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.unaryPlus(x) - * - * Examples: - * - * math.unaryPlus(3.5) // returns 3.5 - * math.unaryPlus(1) // returns 1 - * - * See also: - * - * unaryMinus, add, subtract - * - * @param {number | BigNumber | Fraction | string | Complex | Unit | Array | Matrix} x - * Input value - * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} - * Returns the input value when numeric, converts to a number when input is non-numeric. - */ - return typed(unaryPlus_name, { - number: unaryPlusNumber, - Complex: function Complex(x) { - return x; // complex numbers are immutable - }, - BigNumber: function BigNumber(x) { - return x; // bignumbers are immutable - }, - Fraction: function Fraction(x) { - return x; // fractions are immutable - }, - Unit: function Unit(x) { - return x.clone(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since unaryPlus(0) = 0 - return deepMap(x, this, true); - }, - 'boolean | string': function booleanString(x) { - // convert to a number or bignumber - return config.number === 'BigNumber' ? new BigNumber(+x) : +x; - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/abs.js - - var abs_name = 'abs'; - var abs_dependencies = ['typed']; - var createAbs = /* #__PURE__ */Object(factory["a" /* factory */])(abs_name, abs_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Calculate the absolute value of a number. For matrices, the function is - * evaluated element wise. - * - * Syntax: - * - * math.abs(x) - * - * Examples: - * - * math.abs(3.5) // returns number 3.5 - * math.abs(-4.2) // returns number 4.2 - * - * math.abs([3, -5, -1, 0, 2]) // returns Array [3, 5, 1, 0, 2] - * - * See also: - * - * sign - * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x - * A number or matrix for which to get the absolute value - * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} - * Absolute value of `x` - */ - return typed(abs_name, { - number: absNumber, - Complex: function Complex(x) { - return x.abs(); - }, - BigNumber: function BigNumber(x) { - return x.abs(); - }, - Fraction: function Fraction(x) { - return x.abs(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since abs(0) = 0 - return deepMap(x, this, true); - }, - Unit: function Unit(x) { - return x.abs(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/matrix/apply.js - - var apply_name = 'apply'; - var apply_dependencies = ['typed', 'isInteger']; - var createApply = /* #__PURE__ */Object(factory["a" /* factory */])(apply_name, apply_dependencies, function (_ref) { - var typed = _ref.typed, - isInteger = _ref.isInteger; - - /** - * Apply a function that maps an array to a scalar - * along a given axis of a matrix or array. - * Returns a new matrix or array with one less dimension than the input. - * - * Syntax: - * - * math.apply(A, dim, callback) - * - * Where: - * - * - `dim: number` is a zero-based dimension over which to concatenate the matrices. - * - * Examples: - * - * const A = [[1, 2], [3, 4]] - * const sum = math.sum - * - * math.apply(A, 0, sum) // returns [4, 6] - * math.apply(A, 1, sum) // returns [3, 7] - * - * See also: - * - * map, filter, forEach - * - * @param {Array | Matrix} array The input Matrix - * @param {number} dim The dimension along which the callback is applied - * @param {Function} callback The callback function that is applied. This Function - * should take an array or 1-d matrix as an input and - * return a number. - * @return {Array | Matrix} res The residual matrix with the function applied over some dimension. - */ - return typed(apply_name, { - 'Array | Matrix, number | BigNumber, function': function ArrayMatrixNumberBigNumberFunction(mat, dim, callback) { - if (!isInteger(dim)) { - throw new TypeError('Integer number expected for dimension'); - } - - var size = Array.isArray(mat) ? Object(utils_array["a" /* arraySize */])(mat) : mat.size(); - - if (dim < 0 || dim >= size.length) { - throw new IndexError["a" /* IndexError */](dim, size.length); - } - - if (Object(is["v" /* isMatrix */])(mat)) { - return mat.create(_apply(mat.valueOf(), dim, callback)); - } else { - return _apply(mat, dim, callback); - } - } - }); - }); - /** - * Recursively reduce a matrix - * @param {Array} mat - * @param {number} dim - * @param {Function} callback - * @returns {Array} ret - * @private - */ - - function _apply(mat, dim, callback) { - var i, ret, tran; - - if (dim <= 0) { - if (!Array.isArray(mat[0])) { - return callback(mat); - } else { - tran = apply_switch(mat); - ret = []; - - for (i = 0; i < tran.length; i++) { - ret[i] = _apply(tran[i], dim - 1, callback); - } - - return ret; - } - } else { - ret = []; - - for (i = 0; i < mat.length; i++) { - ret[i] = _apply(mat[i], dim - 1, callback); - } - - return ret; - } - } - /** - * Transpose a matrix - * @param {Array} mat - * @returns {Array} ret - * @private - */ - - function apply_switch(mat) { - var I = mat.length; - var J = mat[0].length; - var i, j; - var ret = []; - - for (j = 0; j < J; j++) { - var tmp = []; - - for (i = 0; i < I; i++) { - tmp.push(mat[i][j]); - } - - ret.push(tmp); - } - - return ret; - } - // CONCATENATED MODULE: ./src/function/arithmetic/addScalar.js - - var addScalar_name = 'addScalar'; - var addScalar_dependencies = ['typed']; - var createAddScalar = /* #__PURE__ */Object(factory["a" /* factory */])(addScalar_name, addScalar_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Add two scalar values, `x + y`. - * This function is meant for internal use: it is used by the public function - * `add` - * - * This function does not support collections (Array or Matrix). - * - * @param {number | BigNumber | Fraction | Complex | Unit} x First value to add - * @param {number | BigNumber | Fraction | Complex} y Second value to add - * @return {number | BigNumber | Fraction | Complex | Unit} Sum of `x` and `y` - * @private - */ - return typed(addScalar_name, { - 'number, number': addNumber, - 'Complex, Complex': function ComplexComplex(x, y) { - return x.add(y); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return x.plus(y); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.add(y); - }, - 'Unit, Unit': function UnitUnit(x, y) { - if (x.value === null || x.value === undefined) { - throw new Error('Parameter x contains a unit with undefined value'); - } - if (y.value === null || y.value === undefined) { - throw new Error('Parameter y contains a unit with undefined value'); - } - if (!x.equalBase(y)) { - throw new Error('Units do not match'); - } - var res = x.clone(); - res.value = this(res.value, y.value); - res.fixPrefix = false; - return res; - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/cbrt.js - - var cbrt_name = 'cbrt'; - var cbrt_dependencies = ['config', 'typed', 'isNegative', 'unaryMinus', 'matrix', 'Complex', 'BigNumber', 'Fraction']; - var createCbrt = /* #__PURE__ */Object(factory["a" /* factory */])(cbrt_name, cbrt_dependencies, function (_ref) { - var config = _ref.config, - typed = _ref.typed, - isNegative = _ref.isNegative, - unaryMinus = _ref.unaryMinus, - matrix = _ref.matrix, - Complex = _ref.Complex, - BigNumber = _ref.BigNumber, - Fraction = _ref.Fraction; - - /** - * Calculate the cubic root of a value. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.cbrt(x) - * math.cbrt(x, allRoots) - * - * Examples: - * - * math.cbrt(27) // returns 3 - * math.cube(3) // returns 27 - * math.cbrt(-64) // returns -4 - * math.cbrt(math.unit('27 m^3')) // returns Unit 3 m - * math.cbrt([27, 64, 125]) // returns [3, 4, 5] - * - * const x = math.complex('8i') - * math.cbrt(x) // returns Complex 1.7320508075689 + i - * math.cbrt(x, true) // returns Matrix [ - * // 1.7320508075689 + i - * // -1.7320508075689 + i - * // -2i - * // ] - * - * See also: - * - * square, sqrt, cube - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x - * Value for which to calculate the cubic root. - * @param {boolean} [allRoots] Optional, false by default. Only applicable - * when `x` is a number or complex number. If true, all complex - * roots are returned, if false (default) the principal root is - * returned. - * @return {number | BigNumber | Complex | Unit | Array | Matrix} - * Returns the cubic root of `x` - */ - return typed(cbrt_name, { - number: cbrtNumber, - // note: signature 'number, boolean' is also supported, - // created by typed as it knows how to convert number to Complex - Complex: _cbrtComplex, - 'Complex, boolean': _cbrtComplex, - BigNumber: function BigNumber(x) { - return x.cbrt(); - }, - Unit: _cbrtUnit, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since cbrt(0) = 0 - return deepMap(x, this, true); - } - }); - /** - * Calculate the cubic root for a complex number - * @param {Complex} x - * @param {boolean} [allRoots] If true, the function will return an array - * with all three roots. If false or undefined, - * the principal root is returned. - * @returns {Complex | Array. | Matrix.} Returns the cubic root(s) of x - * @private - */ - - function _cbrtComplex(x, allRoots) { - // https://www.wikiwand.com/en/Cube_root#/Complex_numbers - var arg3 = x.arg() / 3; - var abs = x.abs(); // principal root: - - var principal = new Complex(cbrtNumber(abs), 0).mul(new Complex(0, arg3).exp()); - - if (allRoots) { - var all = [principal, new Complex(cbrtNumber(abs), 0).mul(new Complex(0, arg3 + Math.PI * 2 / 3).exp()), new Complex(cbrtNumber(abs), 0).mul(new Complex(0, arg3 - Math.PI * 2 / 3).exp())]; - return config.matrix === 'Array' ? all : matrix(all); - } else { - return principal; - } - } - /** - * Calculate the cubic root for a Unit - * @param {Unit} x - * @return {Unit} Returns the cubic root of x - * @private - */ - - function _cbrtUnit(x) { - if (x.value && Object(is["j" /* isComplex */])(x.value)) { - var result = x.clone(); - result.value = 1.0; - result = result.pow(1.0 / 3); // Compute the units - - result.value = _cbrtComplex(x.value); // Compute the value - - return result; - } else { - var negate = isNegative(x.value); - - if (negate) { - x.value = unaryMinus(x.value); - } // TODO: create a helper function for this - - var third; - - if (Object(is["e" /* isBigNumber */])(x.value)) { - third = new BigNumber(1).div(3); - } else if (Object(is["o" /* isFraction */])(x.value)) { - third = new Fraction(1, 3); - } else { - third = 1 / 3; - } - - var _result = x.pow(third); - - if (negate) { - _result.value = unaryMinus(_result.value); - } - - return _result; - } - } - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm11.js - - var algorithm11_name = 'algorithm11'; - var algorithm11_dependencies = ['typed', 'equalScalar']; - var createAlgorithm11 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm11_name, algorithm11_dependencies, function (_ref) { - var typed = _ref.typed, - equalScalar = _ref.equalScalar; - - /** - * Iterates over SparseMatrix S nonzero items and invokes the callback function f(Sij, b). - * Callback function invoked NZ times (number of nonzero items in S). - * - * - * ┌ f(Sij, b) ; S(i,j) !== 0 - * C(i,j) = ┤ - * └ 0 ; otherwise - * - * - * @param {Matrix} s The SparseMatrix instance (S) - * @param {Scalar} b The Scalar value - * @param {Function} callback The f(Aij,b) operation to invoke - * @param {boolean} inverse A true value indicates callback should be invoked f(b,Sij) - * - * @return {Matrix} SparseMatrix (C) - * - * https://github.com/josdejong/mathjs/pull/346#issuecomment-97626813 - */ - return function algorithm11(s, b, callback, inverse) { - // sparse matrix arrays - var avalues = s._values; - var aindex = s._index; - var aptr = s._ptr; - var asize = s._size; - var adt = s._datatype; // sparse matrix cannot be a Pattern matrix - - if (!avalues) { - throw new Error('Cannot perform operation on Pattern Sparse Matrix and Scalar value'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string') { - // datatype - dt = adt; // find signature that matches (dt, dt) - - eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype - - zero = typed.convert(0, dt); // convert b to the same datatype - - b = typed.convert(b, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // result arrays - - var cvalues = []; - var cindex = []; - var cptr = []; // loop columns - - for (var j = 0; j < columns; j++) { - // initialize ptr - cptr[j] = cindex.length; // values in j - - for (var k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { - // row - var i = aindex[k]; // invoke callback - - var v = inverse ? cf(b, avalues[k]) : cf(avalues[k], b); // check value is zero - - if (!eq(v, zero)) { - // push index & value - cindex.push(i); - cvalues.push(v); - } - } - } // update ptr - - cptr[columns] = cindex.length; // return sparse matrix - - return s.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm14.js - - var algorithm14_name = 'algorithm14'; - var algorithm14_dependencies = ['typed']; - var createAlgorithm14 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm14_name, algorithm14_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Iterates over DenseMatrix items and invokes the callback function f(Aij..z, b). - * Callback function invoked MxN times. - * - * C(i,j,...z) = f(Aij..z, b) - * - * @param {Matrix} a The DenseMatrix instance (A) - * @param {Scalar} b The Scalar value - * @param {Function} callback The f(Aij..z,b) operation to invoke - * @param {boolean} inverse A true value indicates callback should be invoked f(b,Aij..z) - * - * @return {Matrix} DenseMatrix (C) - * - * https://github.com/josdejong/mathjs/pull/346#issuecomment-97659042 - */ - return function algorithm14(a, b, callback, inverse) { - // a arrays - var adata = a._data; - var asize = a._size; - var adt = a._datatype; // datatype - - var dt; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string') { - // datatype - dt = adt; // convert b to the same datatype - - b = typed.convert(b, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // populate cdata, iterate through dimensions - - var cdata = asize.length > 0 ? _iterate(cf, 0, asize, asize[0], adata, b, inverse) : []; // c matrix - - return a.createDenseMatrix({ - data: cdata, - size: Object(utils_object["a" /* clone */])(asize), - datatype: dt - }); - }; // recursive function - - function _iterate(f, level, s, n, av, bv, inverse) { - // initialize array for this level - var cv = []; // check we reach the last level - - if (level === s.length - 1) { - // loop arrays in last level - for (var i = 0; i < n; i++) { - // invoke callback and store value - cv[i] = inverse ? f(bv, av[i]) : f(av[i], bv); - } - } else { - // iterate current level - for (var j = 0; j < n; j++) { - // iterate next level - cv[j] = _iterate(f, level + 1, s, s[level + 1], av[j], bv, inverse); - } - } - - return cv; - } - }); - // CONCATENATED MODULE: ./src/function/arithmetic/ceil.js - function _slicedToArray(arr, i) { - return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); - } - - function _nonIterableRest() { - throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); - } - - function _unsupportedIterableToArray(o, minLen) { - if (!o) { - return; - } if (typeof o === "string") { - return _arrayLikeToArray(o, minLen); - } var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) { - n = o.constructor.name; - } if (n === "Map" || n === "Set") { - return Array.from(o); - } if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) { - return _arrayLikeToArray(o, minLen); - } - } - - function _arrayLikeToArray(arr, len) { - if (len == null || len > arr.length) { - len = arr.length; - } for (var i = 0, arr2 = new Array(len); i < len; i++) { - arr2[i] = arr[i]; - } return arr2; - } - - function _iterableToArrayLimit(arr, i) { - if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) { - return; - } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { - for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { - _arr.push(_s.value); if (i && _arr.length === i) { - break; - } - } - } catch (err) { - _d = true; _e = err; - } finally { - try { - if (!_n && _i["return"] != null) { - _i["return"](); - } - } finally { - if (_d) { - throw _e; - } - } - } return _arr; - } - - function _arrayWithHoles(arr) { - if (Array.isArray(arr)) { - return arr; - } - } - - var ceil_name = 'ceil'; - var ceil_dependencies = ['typed', 'config', 'round', 'matrix', 'equalScalar']; - var createCeil = /* #__PURE__ */Object(factory["a" /* factory */])(ceil_name, ceil_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - round = _ref.round, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar; - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Round a value towards plus infinity - * If `x` is complex, both real and imaginary part are rounded towards plus infinity. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.ceil(x) - * math.ceil(x, n) - * - * Examples: - * - * math.ceil(3.2) // returns number 4 - * math.ceil(3.8) // returns number 4 - * math.ceil(-4.2) // returns number -4 - * math.ceil(-4.7) // returns number -4 - * - * math.ceil(3.212, 2) // returns number 3.22 - * math.ceil(3.288, 2) // returns number 3.29 - * math.ceil(-4.212, 2) // returns number -4.21 - * math.ceil(-4.782, 2) // returns number -4.78 - * - * const c = math.complex(3.24, -2.71) - * math.ceil(c) // returns Complex 4 - 2i - * math.ceil(c, 1) // returns Complex 3.3 - 2.7i - * - * math.ceil([3.2, 3.8, -4.7]) // returns Array [4, 4, -4] - * math.ceil([3.21, 3.82, -4.71], 1) // returns Array [3.3, 3.9, -4.7] - * - * See also: - * - * floor, fix, round - * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded - * @param {number | BigNumber | Array} [n=0] Number of decimals - * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value - */ - - return typed('ceil', { - number: function number(x) { - if (Object(utils_number["m" /* nearlyEqual */])(x, round(x), config.epsilon)) { - return round(x); - } else { - return ceilNumber(x); - } - }, - 'number, number': function numberNumber(x, n) { - if (Object(utils_number["m" /* nearlyEqual */])(x, round(x, n), config.epsilon)) { - return round(x, n); - } else { - var _$split = "".concat(x, "e").split('e'), - _$split2 = _slicedToArray(_$split, 2), - number = _$split2[0], - exponent = _$split2[1]; - - var result = Math.ceil(Number("".concat(number, "e").concat(Number(exponent) + n))); - - var _$split3 = "".concat(result, "e").split('e'); - - var _$split4 = _slicedToArray(_$split3, 2); - - number = _$split4[0]; - exponent = _$split4[1]; - return Number("".concat(number, "e").concat(Number(exponent) - n)); - } - }, - Complex: function Complex(x) { - return x.ceil(); - }, - 'Complex, number': function ComplexNumber(x, n) { - return x.ceil(n); - }, - BigNumber: function BigNumber(x) { - if (nearlyEqual(x, round(x), config.epsilon)) { - return round(x); - } else { - return x.ceil(); - } - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, n) { - if (nearlyEqual(x, round(x, n), config.epsilon)) { - return round(x, n); - } else { - return x.toDecimalPlaces(n.toNumber(), decimal["Decimal"].ROUND_CEIL); - } - }, - Fraction: function Fraction(x) { - return x.ceil(); - }, - 'Fraction, number': function FractionNumber(x, n) { - return x.ceil(n); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since ceil(0) = 0 - return deepMap(x, this, true); - }, - 'Array | Matrix, number': function ArrayMatrixNumber(x, n) { - var _this = this; - - // deep map collection, skip zeros since ceil(0) = 0 - return deepMap(x, function (i) { - return _this(i, n); - }, true); - }, - 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { - return algorithm14(x, y, this, false); - }, - 'number | Complex | BigNumber, Array': function numberComplexBigNumberArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/cube.js - - var cube_name = 'cube'; - var cube_dependencies = ['typed']; - var createCube = /* #__PURE__ */Object(factory["a" /* factory */])(cube_name, cube_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Compute the cube of a value, `x * x * x`. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.cube(x) - * - * Examples: - * - * math.cube(2) // returns number 8 - * math.pow(2, 3) // returns number 8 - * math.cube(4) // returns number 64 - * 4 * 4 * 4 // returns number 64 - * - * math.cube([1, 2, 3, 4]) // returns Array [1, 8, 27, 64] - * - * See also: - * - * multiply, square, pow, cbrt - * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x Number for which to calculate the cube - * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} Cube of x - */ - return typed(cube_name, { - number: cubeNumber, - Complex: function Complex(x) { - return x.mul(x).mul(x); // Is faster than pow(x, 3) - }, - BigNumber: function BigNumber(x) { - return x.times(x).times(x); - }, - Fraction: function Fraction(x) { - return x.pow(3); // Is faster than mul()mul()mul() - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since cube(0) = 0 - return deepMap(x, this, true); - }, - Unit: function Unit(x) { - return x.pow(3); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/exp.js - - var exp_name = 'exp'; - var exp_dependencies = ['typed']; - var createExp = /* #__PURE__ */Object(factory["a" /* factory */])(exp_name, exp_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Calculate the exponent of a value. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.exp(x) - * - * Examples: - * - * math.exp(2) // returns number 7.3890560989306495 - * math.pow(math.e, 2) // returns number 7.3890560989306495 - * math.log(math.exp(2)) // returns number 2 - * - * math.exp([1, 2, 3]) - * // returns Array [ - * // 2.718281828459045, - * // 7.3890560989306495, - * // 20.085536923187668 - * // ] - * - * See also: - * - * expm1, log, pow - * - * @param {number | BigNumber | Complex | Array | Matrix} x A number or matrix to exponentiate - * @return {number | BigNumber | Complex | Array | Matrix} Exponent of `x` - */ - return typed(exp_name, { - number: expNumber, - Complex: function Complex(x) { - return x.exp(); - }, - BigNumber: function BigNumber(x) { - return x.exp(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // TODO: exp(sparse) should return a dense matrix since exp(0)==1 - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/expm1.js - - var expm1_name = 'expm1'; - var expm1_dependencies = ['typed', 'Complex']; - var createExpm1 = /* #__PURE__ */Object(factory["a" /* factory */])(expm1_name, expm1_dependencies, function (_ref) { - var typed = _ref.typed, - _Complex = _ref.Complex; - - /** - * Calculate the value of subtracting 1 from the exponential value. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.expm1(x) - * - * Examples: - * - * math.expm1(2) // returns number 6.38905609893065 - * math.pow(math.e, 2) - 1 // returns number 6.3890560989306495 - * math.log(math.expm1(2) + 1) // returns number 2 - * - * math.expm1([1, 2, 3]) - * // returns Array [ - * // 1.718281828459045, - * // 6.3890560989306495, - * // 19.085536923187668 - * // ] - * - * See also: - * - * exp, log, pow - * - * @param {number | BigNumber | Complex | Array | Matrix} x A number or matrix to apply expm1 - * @return {number | BigNumber | Complex | Array | Matrix} Exponent of `x` - */ - return typed(expm1_name, { - number: expm1Number, - Complex: function Complex(x) { - var r = Math.exp(x.re); - return new _Complex(r * Math.cos(x.im) - 1, r * Math.sin(x.im)); - }, - BigNumber: function BigNumber(x) { - return x.exp().minus(1); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/fix.js - - var fix_name = 'fix'; - var fix_dependencies = ['typed', 'Complex', 'matrix', 'ceil', 'floor']; - var createFix = /* #__PURE__ */Object(factory["a" /* factory */])(fix_name, fix_dependencies, function (_ref) { - var typed = _ref.typed, - _Complex = _ref.Complex, - matrix = _ref.matrix, - ceil = _ref.ceil, - floor = _ref.floor; - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Round a value towards zero. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.fix(x) - * - * Examples: - * - * math.fix(3.2) // returns number 3 - * math.fix(3.8) // returns number 3 - * math.fix(-4.2) // returns number -4 - * math.fix(-4.7) // returns number -4 - * - * math.fix(3.12, 1) // returns number 3.1 - * math.fix(3.18, 1) // returns number 3.1 - * math.fix(-4.12, 1) // returns number -4.1 - * math.fix(-4.17, 1) // returns number -4.1 - * - * const c = math.complex(3.22, -2.78) - * math.fix(c) // returns Complex 3 - 2i - * math.fix(c, 1) // returns Complex 3.2 - 2.7i - * - * math.fix([3.2, 3.8, -4.7]) // returns Array [3, 3, -4] - * math.fix([3.2, 3.8, -4.7], 1) // returns Array [3.2, 3.8, -4.7] - * - * See also: - * - * ceil, floor, round - * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded - * @param {number | BigNumber | Array} [n=0] Number of decimals - * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value - */ - - return typed('fix', { - number: function number(x) { - return x > 0 ? floor(x) : ceil(x); - }, - 'number, number | BigNumber': function numberNumberBigNumber(x, n) { - return x > 0 ? floor(x, n) : ceil(x, n); - }, - Complex: function Complex(x) { - return new _Complex(x.re > 0 ? Math.floor(x.re) : Math.ceil(x.re), x.im > 0 ? Math.floor(x.im) : Math.ceil(x.im)); - }, - 'Complex, number | BigNumber': function ComplexNumberBigNumber(x, n) { - return new _Complex(x.re > 0 ? floor(x.re, n) : ceil(x.re, n), x.im > 0 ? floor(x.im, n) : ceil(x.im, n)); - }, - BigNumber: function BigNumber(x) { - return x.isNegative() ? ceil(x) : floor(x); - }, - 'BigNumber, number | BigNumber': function BigNumberNumberBigNumber(x, n) { - return x.isNegative() ? ceil(x, n) : floor(x, n); - }, - Fraction: function Fraction(x) { - return x.s < 0 ? x.ceil() : x.floor(); - }, - 'Fraction, number | BigNumber': function FractionNumberBigNumber(x, n) { - return x.s < 0 ? x.ceil(n) : x.floor(n); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since fix(0) = 0 - return deepMap(x, this, true); - }, - 'Array | Matrix, number | BigNumber': function ArrayMatrixNumberBigNumber(x, n) { - var _this = this; - - // deep map collection, skip zeros since fix(0) = 0 - return deepMap(x, function (i) { - return _this(i, n); - }, true); - }, - 'number | Complex | BigNumber, Array': function numberComplexBigNumberArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/floor.js - function floor_slicedToArray(arr, i) { - return floor_arrayWithHoles(arr) || floor_iterableToArrayLimit(arr, i) || floor_unsupportedIterableToArray(arr, i) || floor_nonIterableRest(); - } - - function floor_nonIterableRest() { - throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); - } - - function floor_unsupportedIterableToArray(o, minLen) { - if (!o) { - return; - } if (typeof o === "string") { - return floor_arrayLikeToArray(o, minLen); - } var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) { - n = o.constructor.name; - } if (n === "Map" || n === "Set") { - return Array.from(o); - } if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) { - return floor_arrayLikeToArray(o, minLen); - } - } - - function floor_arrayLikeToArray(arr, len) { - if (len == null || len > arr.length) { - len = arr.length; - } for (var i = 0, arr2 = new Array(len); i < len; i++) { - arr2[i] = arr[i]; - } return arr2; - } - - function floor_iterableToArrayLimit(arr, i) { - if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) { - return; - } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { - for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { - _arr.push(_s.value); if (i && _arr.length === i) { - break; - } - } - } catch (err) { - _d = true; _e = err; - } finally { - try { - if (!_n && _i["return"] != null) { - _i["return"](); - } - } finally { - if (_d) { - throw _e; - } - } - } return _arr; - } - - function floor_arrayWithHoles(arr) { - if (Array.isArray(arr)) { - return arr; - } - } - - var floor_name = 'floor'; - var floor_dependencies = ['typed', 'config', 'round', 'matrix', 'equalScalar']; - var createFloor = /* #__PURE__ */Object(factory["a" /* factory */])(floor_name, floor_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - round = _ref.round, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar; - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Round a value towards minus infinity. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.floor(x) - * math.floor(x, n) - * - * Examples: - * - * math.floor(3.2) // returns number 3 - * math.floor(3.8) // returns number 3 - * math.floor(-4.2) // returns number -5 - * math.floor(-4.7) // returns number -5 - * - * math.floor(3.212, 2) // returns number 3.21 - * math.floor(3.288, 2) // returns number 3.28 - * math.floor(-4.212, 2) // returns number -4.22 - * math.floor(-4.782, 2) // returns number -4.79 - * - * const c = math.complex(3.24, -2.71) - * math.floor(c) // returns Complex 3 - 3i - * math.floor(c, 1) // returns Complex 3.2 - 2.8i - * - * math.floor([3.2, 3.8, -4.7]) // returns Array [3, 3, -5] - * math.floor([3.21, 3.82, -4.71], 1) // returns Array [3.2, 3.8, -4.8] - * - * See also: - * - * ceil, fix, round - * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded - * @param {number | BigNumber | Array} [n=0] Number of decimals - * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value - */ - - return typed('floor', { - number: function number(x) { - if (Object(utils_number["m" /* nearlyEqual */])(x, round(x), config.epsilon)) { - return round(x); - } else { - return Math.floor(x); - } - }, - 'number, number': function numberNumber(x, n) { - if (Object(utils_number["m" /* nearlyEqual */])(x, round(x, n), config.epsilon)) { - return round(x, n); - } else { - var _$split = "".concat(x, "e").split('e'), - _$split2 = floor_slicedToArray(_$split, 2), - number = _$split2[0], - exponent = _$split2[1]; - - var result = Math.floor(Number("".concat(number, "e").concat(Number(exponent) + n))); - - var _$split3 = "".concat(result, "e").split('e'); - - var _$split4 = floor_slicedToArray(_$split3, 2); - - number = _$split4[0]; - exponent = _$split4[1]; - return Number("".concat(number, "e").concat(Number(exponent) - n)); - } - }, - Complex: function Complex(x) { - return x.floor(); - }, - 'Complex, number': function ComplexNumber(x, n) { - return x.floor(n); - }, - BigNumber: function BigNumber(x) { - if (nearlyEqual(x, round(x), config.epsilon)) { - return round(x); - } else { - return x.floor(); - } - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, n) { - if (nearlyEqual(x, round(x, n), config.epsilon)) { - return round(x, n); - } else { - return x.toDecimalPlaces(n.toNumber(), decimal["Decimal"].ROUND_FLOOR); - } - }, - Fraction: function Fraction(x) { - return x.floor(); - }, - 'Fraction, number': function FractionNumber(x, n) { - return x.floor(n); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since floor(0) = 0 - return deepMap(x, this, true); - }, - 'Array | Matrix, number': function ArrayMatrixNumber(x, n) { - var _this = this; - - // deep map collection, skip zeros since ceil(0) = 0 - return deepMap(x, function (i) { - return _this(i, n); - }, true); - }, - 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { - return algorithm14(x, y, this, false); - }, - 'number | Complex | BigNumber, Array': function numberComplexBigNumberArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm01.js - - var algorithm01_name = 'algorithm01'; - var algorithm01_dependencies = ['typed']; - var createAlgorithm01 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm01_name, algorithm01_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Iterates over SparseMatrix nonzero items and invokes the callback function f(Dij, Sij). - * Callback function invoked NNZ times (number of nonzero items in SparseMatrix). - * - * - * ┌ f(Dij, Sij) ; S(i,j) !== 0 - * C(i,j) = ┤ - * └ Dij ; otherwise - * - * - * @param {Matrix} denseMatrix The DenseMatrix instance (D) - * @param {Matrix} sparseMatrix The SparseMatrix instance (S) - * @param {Function} callback The f(Dij,Sij) operation to invoke, where Dij = DenseMatrix(i,j) and Sij = SparseMatrix(i,j) - * @param {boolean} inverse A true value indicates callback should be invoked f(Sij,Dij) - * - * @return {Matrix} DenseMatrix (C) - * - * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97477571 - */ - return function algorithm1(denseMatrix, sparseMatrix, callback, inverse) { - // dense matrix arrays - var adata = denseMatrix._data; - var asize = denseMatrix._size; - var adt = denseMatrix._datatype; // sparse matrix arrays - - var bvalues = sparseMatrix._values; - var bindex = sparseMatrix._index; - var bptr = sparseMatrix._ptr; - var bsize = sparseMatrix._size; - var bdt = sparseMatrix._datatype; // validate dimensions - - if (asize.length !== bsize.length) { - throw new DimensionError["a" /* DimensionError */](asize.length, bsize.length); - } // check rows & columns - - if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) { - throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); - } // sparse matrix cannot be a Pattern matrix - - if (!bvalues) { - throw new Error('Cannot perform operation on Dense Matrix and Pattern Sparse Matrix'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // process data types - - var dt = typeof adt === 'string' && adt === bdt ? adt : undefined; // callback function - - var cf = dt ? typed.find(callback, [dt, dt]) : callback; // vars - - var i, j; // result (DenseMatrix) - - var cdata = []; // initialize c - - for (i = 0; i < rows; i++) { - cdata[i] = []; - } // workspace - - var x = []; // marks indicating we have a value in x for a given column - - var w = []; // loop columns in b - - for (j = 0; j < columns; j++) { - // column mark - var mark = j + 1; // values in column j - - for (var k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { - // row - i = bindex[k]; // update workspace - - x[i] = inverse ? cf(bvalues[k], adata[i][j]) : cf(adata[i][j], bvalues[k]); // mark i as updated - - w[i] = mark; - } // loop rows - - for (i = 0; i < rows; i++) { - // check row is in workspace - if (w[i] === mark) { - // c[i][j] was already calculated - cdata[i][j] = x[i]; - } else { - // item does not exist in S - cdata[i][j] = adata[i][j]; - } - } - } // return dense matrix - - return denseMatrix.createDenseMatrix({ - data: cdata, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm04.js - - var algorithm04_name = 'algorithm04'; - var algorithm04_dependencies = ['typed', 'equalScalar']; - var createAlgorithm04 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm04_name, algorithm04_dependencies, function (_ref) { - var typed = _ref.typed, - equalScalar = _ref.equalScalar; - - /** - * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij). - * Callback function invoked MAX(NNZA, NNZB) times - * - * - * ┌ f(Aij, Bij) ; A(i,j) !== 0 && B(i,j) !== 0 - * C(i,j) = ┤ A(i,j) ; A(i,j) !== 0 - * └ B(i,j) ; B(i,j) !== 0 - * - * - * @param {Matrix} a The SparseMatrix instance (A) - * @param {Matrix} b The SparseMatrix instance (B) - * @param {Function} callback The f(Aij,Bij) operation to invoke - * - * @return {Matrix} SparseMatrix (C) - * - * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 - */ - return function algorithm04(a, b, callback) { - // sparse matrix arrays - var avalues = a._values; - var aindex = a._index; - var aptr = a._ptr; - var asize = a._size; - var adt = a._datatype; // sparse matrix arrays - - var bvalues = b._values; - var bindex = b._index; - var bptr = b._ptr; - var bsize = b._size; - var bdt = b._datatype; // validate dimensions - - if (asize.length !== bsize.length) { - throw new DimensionError["a" /* DimensionError */](asize.length, bsize.length); - } // check rows & columns - - if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) { - throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string' && adt === bdt) { - // datatype - dt = adt; // find signature that matches (dt, dt) - - eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype - - zero = typed.convert(0, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // result arrays - - var cvalues = avalues && bvalues ? [] : undefined; - var cindex = []; - var cptr = []; // workspace - - var xa = avalues && bvalues ? [] : undefined; - var xb = avalues && bvalues ? [] : undefined; // marks indicating we have a value in x for a given column - - var wa = []; - var wb = []; // vars - - var i, j, k, k0, k1; // loop columns - - for (j = 0; j < columns; j++) { - // update cptr - cptr[j] = cindex.length; // columns mark - - var mark = j + 1; // loop A(:,j) - - for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { - // row - i = aindex[k]; // update c - - cindex.push(i); // update workspace - - wa[i] = mark; // check we need to process values - - if (xa) { - xa[i] = avalues[k]; - } - } // loop B(:,j) - - for (k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { - // row - i = bindex[k]; // check row exists in A - - if (wa[i] === mark) { - // update record in xa @ i - if (xa) { - // invoke callback - var v = cf(xa[i], bvalues[k]); // check for zero - - if (!eq(v, zero)) { - // update workspace - xa[i] = v; - } else { - // remove mark (index will be removed later) - wa[i] = null; - } - } - } else { - // update c - cindex.push(i); // update workspace - - wb[i] = mark; // check we need to process values - - if (xb) { - xb[i] = bvalues[k]; - } - } - } // check we need to process values (non pattern matrix) - - if (xa && xb) { - // initialize first index in j - k = cptr[j]; // loop index in j - - while (k < cindex.length) { - // row - i = cindex[k]; // check workspace has value @ i - - if (wa[i] === mark) { - // push value (Aij != 0 || (Aij != 0 && Bij != 0)) - cvalues[k] = xa[i]; // increment pointer - - k++; - } else if (wb[i] === mark) { - // push value (bij != 0) - cvalues[k] = xb[i]; // increment pointer - - k++; - } else { - // remove index @ k - cindex.splice(k, 1); - } - } - } - } // update cptr - - cptr[columns] = cindex.length; // return sparse matrix - - return a.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm10.js - - var algorithm10_name = 'algorithm10'; - var algorithm10_dependencies = ['typed', 'DenseMatrix']; - var createAlgorithm10 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm10_name, algorithm10_dependencies, function (_ref) { - var typed = _ref.typed, - DenseMatrix = _ref.DenseMatrix; - - /** - * Iterates over SparseMatrix S nonzero items and invokes the callback function f(Sij, b). - * Callback function invoked NZ times (number of nonzero items in S). - * - * - * ┌ f(Sij, b) ; S(i,j) !== 0 - * C(i,j) = ┤ - * └ b ; otherwise - * - * - * @param {Matrix} s The SparseMatrix instance (S) - * @param {Scalar} b The Scalar value - * @param {Function} callback The f(Aij,b) operation to invoke - * @param {boolean} inverse A true value indicates callback should be invoked f(b,Sij) - * - * @return {Matrix} DenseMatrix (C) - * - * https://github.com/josdejong/mathjs/pull/346#issuecomment-97626813 - */ - return function algorithm10(s, b, callback, inverse) { - // sparse matrix arrays - var avalues = s._values; - var aindex = s._index; - var aptr = s._ptr; - var asize = s._size; - var adt = s._datatype; // sparse matrix cannot be a Pattern matrix - - if (!avalues) { - throw new Error('Cannot perform operation on Pattern Sparse Matrix and Scalar value'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string') { - // datatype - dt = adt; // convert b to the same datatype - - b = typed.convert(b, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // result arrays - - var cdata = []; // workspaces - - var x = []; // marks indicating we have a value in x for a given column - - var w = []; // loop columns - - for (var j = 0; j < columns; j++) { - // columns mark - var mark = j + 1; // values in j - - for (var k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { - // row - var r = aindex[k]; // update workspace - - x[r] = avalues[k]; - w[r] = mark; - } // loop rows - - for (var i = 0; i < rows; i++) { - // initialize C on first column - if (j === 0) { - // create row array - cdata[i] = []; - } // check sparse matrix has a value @ i,j - - if (w[i] === mark) { - // invoke callback, update C - cdata[i][j] = inverse ? cf(b, x[i]) : cf(x[i], b); - } else { - // dense matrix value @ i, j - cdata[i][j] = b; - } - } - } // return dense matrix - - return new DenseMatrix({ - data: cdata, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm13.js - - var algorithm13_name = 'algorithm13'; - var algorithm13_dependencies = ['typed']; - var createAlgorithm13 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm13_name, algorithm13_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Iterates over DenseMatrix items and invokes the callback function f(Aij..z, Bij..z). - * Callback function invoked MxN times. - * - * C(i,j,...z) = f(Aij..z, Bij..z) - * - * @param {Matrix} a The DenseMatrix instance (A) - * @param {Matrix} b The DenseMatrix instance (B) - * @param {Function} callback The f(Aij..z,Bij..z) operation to invoke - * - * @return {Matrix} DenseMatrix (C) - * - * https://github.com/josdejong/mathjs/pull/346#issuecomment-97658658 - */ - return function algorithm13(a, b, callback) { - // a arrays - var adata = a._data; - var asize = a._size; - var adt = a._datatype; // b arrays - - var bdata = b._data; - var bsize = b._size; - var bdt = b._datatype; // c arrays - - var csize = []; // validate dimensions - - if (asize.length !== bsize.length) { - throw new DimensionError["a" /* DimensionError */](asize.length, bsize.length); - } // validate each one of the dimension sizes - - for (var s = 0; s < asize.length; s++) { - // must match - if (asize[s] !== bsize[s]) { - throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); - } // update dimension in c - - csize[s] = asize[s]; - } // datatype - - var dt; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string' && adt === bdt) { - // datatype - dt = adt; // callback - - cf = typed.find(callback, [dt, dt]); - } // populate cdata, iterate through dimensions - - var cdata = csize.length > 0 ? _iterate(cf, 0, csize, csize[0], adata, bdata) : []; // c matrix - - return a.createDenseMatrix({ - data: cdata, - size: csize, - datatype: dt - }); - }; // recursive function - - function _iterate(f, level, s, n, av, bv) { - // initialize array for this level - var cv = []; // check we reach the last level - - if (level === s.length - 1) { - // loop arrays in last level - for (var i = 0; i < n; i++) { - // invoke callback and store value - cv[i] = f(av[i], bv[i]); - } - } else { - // iterate current level - for (var j = 0; j < n; j++) { - // iterate next level - cv[j] = _iterate(f, level + 1, s, s[level + 1], av[j], bv[j]); - } - } - - return cv; - } - }); - // CONCATENATED MODULE: ./src/function/arithmetic/gcd.js - - var gcd_name = 'gcd'; - var gcd_dependencies = ['typed', 'matrix', 'equalScalar', 'BigNumber', 'DenseMatrix']; - var createGcd = /* #__PURE__ */Object(factory["a" /* factory */])(gcd_name, gcd_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - BigNumber = _ref.BigNumber, - DenseMatrix = _ref.DenseMatrix; - var algorithm01 = createAlgorithm01({ - typed: typed - }); - var algorithm04 = createAlgorithm04({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm10 = createAlgorithm10({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Calculate the greatest common divisor for two or more values or arrays. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.gcd(a, b) - * math.gcd(a, b, c, ...) - * - * Examples: - * - * math.gcd(8, 12) // returns 4 - * math.gcd(-4, 6) // returns 2 - * math.gcd(25, 15, -10) // returns 5 - * - * math.gcd([8, -4], [12, 6]) // returns [4, 2] - * - * See also: - * - * lcm, xgcd - * - * @param {... number | BigNumber | Fraction | Array | Matrix} args Two or more integer numbers - * @return {number | BigNumber | Fraction | Array | Matrix} The greatest common divisor - */ - - return typed(gcd_name, { - 'number, number': gcdNumber, - 'BigNumber, BigNumber': _gcdBigNumber, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.gcd(y); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm04(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm01(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm01(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { - return algorithm10(x, y, this, false); - }, - 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { - return algorithm14(x, y, this, false); - }, - 'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) { - return algorithm10(y, x, this, true); - }, - 'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'number | BigNumber, Array': function numberBigNumberArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - }, - // TODO: need a smarter notation here - 'Array | Matrix | number | BigNumber, Array | Matrix | number | BigNumber, ...Array | Matrix | number | BigNumber': function ArrayMatrixNumberBigNumberArrayMatrixNumberBigNumberArrayMatrixNumberBigNumber(a, b, args) { - var res = this(a, b); - - for (var i = 0; i < args.length; i++) { - res = this(res, args[i]); - } - - return res; - } - }); - /** - * Calculate gcd for BigNumbers - * @param {BigNumber} a - * @param {BigNumber} b - * @returns {BigNumber} Returns greatest common denominator of a and b - * @private - */ - - function _gcdBigNumber(a, b) { - if (!a.isInt() || !b.isInt()) { - throw new Error('Parameters in function gcd must be integer numbers'); - } // https://en.wikipedia.org/wiki/Euclidean_algorithm - - var zero = new BigNumber(0); - - while (!b.isZero()) { - var r = a.mod(b); - a = b; - b = r; - } - - return a.lt(zero) ? a.neg() : a; - } - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm02.js - - var algorithm02_name = 'algorithm02'; - var algorithm02_dependencies = ['typed', 'equalScalar']; - var createAlgorithm02 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm02_name, algorithm02_dependencies, function (_ref) { - var typed = _ref.typed, - equalScalar = _ref.equalScalar; - - /** - * Iterates over SparseMatrix nonzero items and invokes the callback function f(Dij, Sij). - * Callback function invoked NNZ times (number of nonzero items in SparseMatrix). - * - * - * ┌ f(Dij, Sij) ; S(i,j) !== 0 - * C(i,j) = ┤ - * └ 0 ; otherwise - * - * - * @param {Matrix} denseMatrix The DenseMatrix instance (D) - * @param {Matrix} sparseMatrix The SparseMatrix instance (S) - * @param {Function} callback The f(Dij,Sij) operation to invoke, where Dij = DenseMatrix(i,j) and Sij = SparseMatrix(i,j) - * @param {boolean} inverse A true value indicates callback should be invoked f(Sij,Dij) - * - * @return {Matrix} SparseMatrix (C) - * - * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97477571 - */ - return function algorithm02(denseMatrix, sparseMatrix, callback, inverse) { - // dense matrix arrays - var adata = denseMatrix._data; - var asize = denseMatrix._size; - var adt = denseMatrix._datatype; // sparse matrix arrays - - var bvalues = sparseMatrix._values; - var bindex = sparseMatrix._index; - var bptr = sparseMatrix._ptr; - var bsize = sparseMatrix._size; - var bdt = sparseMatrix._datatype; // validate dimensions - - if (asize.length !== bsize.length) { - throw new DimensionError["a" /* DimensionError */](asize.length, bsize.length); - } // check rows & columns - - if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) { - throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); - } // sparse matrix cannot be a Pattern matrix - - if (!bvalues) { - throw new Error('Cannot perform operation on Dense Matrix and Pattern Sparse Matrix'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string' && adt === bdt) { - // datatype - dt = adt; // find signature that matches (dt, dt) - - eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype - - zero = typed.convert(0, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // result (SparseMatrix) - - var cvalues = []; - var cindex = []; - var cptr = []; // loop columns in b - - for (var j = 0; j < columns; j++) { - // update cptr - cptr[j] = cindex.length; // values in column j - - for (var k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { - // row - var i = bindex[k]; // update C(i,j) - - var cij = inverse ? cf(bvalues[k], adata[i][j]) : cf(adata[i][j], bvalues[k]); // check for nonzero - - if (!eq(cij, zero)) { - // push i & v - cindex.push(i); - cvalues.push(cij); - } - } - } // update cptr - - cptr[columns] = cindex.length; // return sparse matrix - - return sparseMatrix.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm06.js - - var algorithm06_name = 'algorithm06'; - var algorithm06_dependencies = ['typed', 'equalScalar']; - var createAlgorithm06 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm06_name, algorithm06_dependencies, function (_ref) { - var typed = _ref.typed, - equalScalar = _ref.equalScalar; - - /** - * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij). - * Callback function invoked (Anz U Bnz) times, where Anz and Bnz are the nonzero elements in both matrices. - * - * - * ┌ f(Aij, Bij) ; A(i,j) !== 0 && B(i,j) !== 0 - * C(i,j) = ┤ - * └ 0 ; otherwise - * - * - * @param {Matrix} a The SparseMatrix instance (A) - * @param {Matrix} b The SparseMatrix instance (B) - * @param {Function} callback The f(Aij,Bij) operation to invoke - * - * @return {Matrix} SparseMatrix (C) - * - * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 - */ - return function algorithm06(a, b, callback) { - // sparse matrix arrays - var avalues = a._values; - var asize = a._size; - var adt = a._datatype; // sparse matrix arrays - - var bvalues = b._values; - var bsize = b._size; - var bdt = b._datatype; // validate dimensions - - if (asize.length !== bsize.length) { - throw new DimensionError["a" /* DimensionError */](asize.length, bsize.length); - } // check rows & columns - - if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) { - throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string' && adt === bdt) { - // datatype - dt = adt; // find signature that matches (dt, dt) - - eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype - - zero = typed.convert(0, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // result arrays - - var cvalues = avalues && bvalues ? [] : undefined; - var cindex = []; - var cptr = []; // workspaces - - var x = cvalues ? [] : undefined; // marks indicating we have a value in x for a given column - - var w = []; // marks indicating value in a given row has been updated - - var u = []; // loop columns - - for (var j = 0; j < columns; j++) { - // update cptr - cptr[j] = cindex.length; // columns mark - - var mark = j + 1; // scatter the values of A(:,j) into workspace - - scatter(a, j, w, x, u, mark, cindex, cf); // scatter the values of B(:,j) into workspace - - scatter(b, j, w, x, u, mark, cindex, cf); // check we need to process values (non pattern matrix) - - if (x) { - // initialize first index in j - var k = cptr[j]; // loop index in j - - while (k < cindex.length) { - // row - var i = cindex[k]; // check function was invoked on current row (Aij !=0 && Bij != 0) - - if (u[i] === mark) { - // value @ i - var v = x[i]; // check for zero value - - if (!eq(v, zero)) { - // push value - cvalues.push(v); // increment pointer - - k++; - } else { - // remove value @ i, do not increment pointer - cindex.splice(k, 1); - } - } else { - // remove value @ i, do not increment pointer - cindex.splice(k, 1); - } - } - } else { - // initialize first index in j - var p = cptr[j]; // loop index in j - - while (p < cindex.length) { - // row - var r = cindex[p]; // check function was invoked on current row (Aij !=0 && Bij != 0) - - if (u[r] !== mark) { - // remove value @ i, do not increment pointer - cindex.splice(p, 1); - } else { - // increment pointer - p++; - } - } - } - } // update cptr - - cptr[columns] = cindex.length; // return sparse matrix - - return a.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/function/arithmetic/lcm.js - - var lcm_name = 'lcm'; - var lcm_dependencies = ['typed', 'matrix', 'equalScalar']; - var createLcm = /* #__PURE__ */Object(factory["a" /* factory */])(lcm_name, lcm_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar; - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm06 = createAlgorithm06({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Calculate the least common multiple for two or more values or arrays. - * - * lcm is defined as: - * - * lcm(a, b) = abs(a * b) / gcd(a, b) - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.lcm(a, b) - * math.lcm(a, b, c, ...) - * - * Examples: - * - * math.lcm(4, 6) // returns 12 - * math.lcm(6, 21) // returns 42 - * math.lcm(6, 21, 5) // returns 210 - * - * math.lcm([4, 6], [6, 21]) // returns [12, 42] - * - * See also: - * - * gcd, xgcd - * - * @param {... number | BigNumber | Array | Matrix} args Two or more integer numbers - * @return {number | BigNumber | Array | Matrix} The least common multiple - */ - - return typed(lcm_name, { - 'number, number': lcmNumber, - 'BigNumber, BigNumber': _lcmBigNumber, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.lcm(y); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm06(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm02(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm02(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { - return algorithm14(x, y, this, false); - }, - 'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) { - return algorithm11(y, x, this, true); - }, - 'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'number | BigNumber, Array': function numberBigNumberArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - }, - // TODO: need a smarter notation here - 'Array | Matrix | number | BigNumber, Array | Matrix | number | BigNumber, ...Array | Matrix | number | BigNumber': function ArrayMatrixNumberBigNumberArrayMatrixNumberBigNumberArrayMatrixNumberBigNumber(a, b, args) { - var res = this(a, b); - - for (var i = 0; i < args.length; i++) { - res = this(res, args[i]); - } - - return res; - } - }); - /** - * Calculate lcm for two BigNumbers - * @param {BigNumber} a - * @param {BigNumber} b - * @returns {BigNumber} Returns the least common multiple of a and b - * @private - */ - - function _lcmBigNumber(a, b) { - if (!a.isInt() || !b.isInt()) { - throw new Error('Parameters in function lcm must be integer numbers'); - } - - if (a.isZero()) { - return a; - } - - if (b.isZero()) { - return b; - } // https://en.wikipedia.org/wiki/Euclidean_algorithm - // evaluate lcm here inline to reduce overhead - - var prod = a.times(b); - - while (!b.isZero()) { - var t = b; - b = a.mod(t); - a = t; - } - - return prod.div(a).abs(); - } - }); - // CONCATENATED MODULE: ./src/function/arithmetic/log10.js - - var log10_name = 'log10'; - var log10_dependencies = ['typed', 'config', 'Complex']; - var createLog10 = /* #__PURE__ */Object(factory["a" /* factory */])(log10_name, log10_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - _Complex = _ref.Complex; - - /** - * Calculate the 10-base logarithm of a value. This is the same as calculating `log(x, 10)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.log10(x) - * - * Examples: - * - * math.log10(0.00001) // returns -5 - * math.log10(10000) // returns 4 - * math.log(10000) / math.log(10) // returns 4 - * math.pow(10, 4) // returns 10000 - * - * See also: - * - * exp, log, log1p, log2 - * - * @param {number | BigNumber | Complex | Array | Matrix} x - * Value for which to calculate the logarithm. - * @return {number | BigNumber | Complex | Array | Matrix} - * Returns the 10-base logarithm of `x` - */ - return typed(log10_name, { - number: function number(x) { - if (x >= 0 || config.predictable) { - return log10Number(x); - } else { - // negative value -> complex value computation - return new _Complex(x, 0).log().div(Math.LN10); - } - }, - Complex: function Complex(x) { - return new _Complex(x).log().div(Math.LN10); - }, - BigNumber: function BigNumber(x) { - if (!x.isNegative() || config.predictable) { - return x.log(); - } else { - // downgrade to number, return Complex valued result - return new _Complex(x.toNumber(), 0).log().div(Math.LN10); - } - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/log2.js - - var log2_name = 'log2'; - var log2_dependencies = ['typed', 'config', 'Complex']; - var createLog2 = /* #__PURE__ */Object(factory["a" /* factory */])(log2_name, log2_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - Complex = _ref.Complex; - - /** - * Calculate the 2-base of a value. This is the same as calculating `log(x, 2)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.log2(x) - * - * Examples: - * - * math.log2(0.03125) // returns -5 - * math.log2(16) // returns 4 - * math.log2(16) / math.log2(2) // returns 4 - * math.pow(2, 4) // returns 16 - * - * See also: - * - * exp, log, log1p, log10 - * - * @param {number | BigNumber | Complex | Array | Matrix} x - * Value for which to calculate the logarithm. - * @return {number | BigNumber | Complex | Array | Matrix} - * Returns the 2-base logarithm of `x` - */ - return typed(log2_name, { - number: function number(x) { - if (x >= 0 || config.predictable) { - return log2Number(x); - } else { - // negative value -> complex value computation - return _log2Complex(new Complex(x, 0)); - } - }, - Complex: _log2Complex, - BigNumber: function BigNumber(x) { - if (!x.isNegative() || config.predictable) { - return x.log(2); - } else { - // downgrade to number, return Complex valued result - return _log2Complex(new Complex(x.toNumber(), 0)); - } - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - /** - * Calculate log2 for a complex value - * @param {Complex} x - * @returns {Complex} - * @private - */ - - function _log2Complex(x) { - var newX = Math.sqrt(x.re * x.re + x.im * x.im); - return new Complex(Math.log2 ? Math.log2(newX) : Math.log(newX) / Math.LN2, Math.atan2(x.im, x.re) / Math.LN2); - } - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm03.js - - var algorithm03_name = 'algorithm03'; - var algorithm03_dependencies = ['typed']; - var createAlgorithm03 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm03_name, algorithm03_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Iterates over SparseMatrix items and invokes the callback function f(Dij, Sij). - * Callback function invoked M*N times. - * - * - * ┌ f(Dij, Sij) ; S(i,j) !== 0 - * C(i,j) = ┤ - * └ f(Dij, 0) ; otherwise - * - * - * @param {Matrix} denseMatrix The DenseMatrix instance (D) - * @param {Matrix} sparseMatrix The SparseMatrix instance (C) - * @param {Function} callback The f(Dij,Sij) operation to invoke, where Dij = DenseMatrix(i,j) and Sij = SparseMatrix(i,j) - * @param {boolean} inverse A true value indicates callback should be invoked f(Sij,Dij) - * - * @return {Matrix} DenseMatrix (C) - * - * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97477571 - */ - return function algorithm03(denseMatrix, sparseMatrix, callback, inverse) { - // dense matrix arrays - var adata = denseMatrix._data; - var asize = denseMatrix._size; - var adt = denseMatrix._datatype; // sparse matrix arrays - - var bvalues = sparseMatrix._values; - var bindex = sparseMatrix._index; - var bptr = sparseMatrix._ptr; - var bsize = sparseMatrix._size; - var bdt = sparseMatrix._datatype; // validate dimensions - - if (asize.length !== bsize.length) { - throw new DimensionError["a" /* DimensionError */](asize.length, bsize.length); - } // check rows & columns - - if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) { - throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); - } // sparse matrix cannot be a Pattern matrix - - if (!bvalues) { - throw new Error('Cannot perform operation on Dense Matrix and Pattern Sparse Matrix'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // zero value - - var zero = 0; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string' && adt === bdt) { - // datatype - dt = adt; // convert 0 to the same datatype - - zero = typed.convert(0, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // result (DenseMatrix) - - var cdata = []; // initialize dense matrix - - for (var z = 0; z < rows; z++) { - // initialize row - cdata[z] = []; - } // workspace - - var x = []; // marks indicating we have a value in x for a given column - - var w = []; // loop columns in b - - for (var j = 0; j < columns; j++) { - // column mark - var mark = j + 1; // values in column j - - for (var k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { - // row - var i = bindex[k]; // update workspace - - x[i] = inverse ? cf(bvalues[k], adata[i][j]) : cf(adata[i][j], bvalues[k]); - w[i] = mark; - } // process workspace - - for (var y = 0; y < rows; y++) { - // check we have a calculated value for current row - if (w[y] === mark) { - // use calculated value - cdata[y][j] = x[y]; - } else { - // calculate value - cdata[y][j] = inverse ? cf(zero, adata[y][j]) : cf(adata[y][j], zero); - } - } - } // return dense matrix - - return denseMatrix.createDenseMatrix({ - data: cdata, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm05.js - - var algorithm05_name = 'algorithm05'; - var algorithm05_dependencies = ['typed', 'equalScalar']; - var createAlgorithm05 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm05_name, algorithm05_dependencies, function (_ref) { - var typed = _ref.typed, - equalScalar = _ref.equalScalar; - - /** - * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij). - * Callback function invoked MAX(NNZA, NNZB) times - * - * - * ┌ f(Aij, Bij) ; A(i,j) !== 0 || B(i,j) !== 0 - * C(i,j) = ┤ - * └ 0 ; otherwise - * - * - * @param {Matrix} a The SparseMatrix instance (A) - * @param {Matrix} b The SparseMatrix instance (B) - * @param {Function} callback The f(Aij,Bij) operation to invoke - * - * @return {Matrix} SparseMatrix (C) - * - * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 - */ - return function algorithm05(a, b, callback) { - // sparse matrix arrays - var avalues = a._values; - var aindex = a._index; - var aptr = a._ptr; - var asize = a._size; - var adt = a._datatype; // sparse matrix arrays - - var bvalues = b._values; - var bindex = b._index; - var bptr = b._ptr; - var bsize = b._size; - var bdt = b._datatype; // validate dimensions - - if (asize.length !== bsize.length) { - throw new DimensionError["a" /* DimensionError */](asize.length, bsize.length); - } // check rows & columns - - if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) { - throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string' && adt === bdt) { - // datatype - dt = adt; // find signature that matches (dt, dt) - - eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype - - zero = typed.convert(0, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // result arrays - - var cvalues = avalues && bvalues ? [] : undefined; - var cindex = []; - var cptr = []; // workspaces - - var xa = cvalues ? [] : undefined; - var xb = cvalues ? [] : undefined; // marks indicating we have a value in x for a given column - - var wa = []; - var wb = []; // vars - - var i, j, k, k1; // loop columns - - for (j = 0; j < columns; j++) { - // update cptr - cptr[j] = cindex.length; // columns mark - - var mark = j + 1; // loop values A(:,j) - - for (k = aptr[j], k1 = aptr[j + 1]; k < k1; k++) { - // row - i = aindex[k]; // push index - - cindex.push(i); // update workspace - - wa[i] = mark; // check we need to process values - - if (xa) { - xa[i] = avalues[k]; - } - } // loop values B(:,j) - - for (k = bptr[j], k1 = bptr[j + 1]; k < k1; k++) { - // row - i = bindex[k]; // check row existed in A - - if (wa[i] !== mark) { - // push index - cindex.push(i); - } // update workspace - - wb[i] = mark; // check we need to process values - - if (xb) { - xb[i] = bvalues[k]; - } - } // check we need to process values (non pattern matrix) - - if (cvalues) { - // initialize first index in j - k = cptr[j]; // loop index in j - - while (k < cindex.length) { - // row - i = cindex[k]; // marks - - var wai = wa[i]; - var wbi = wb[i]; // check Aij or Bij are nonzero - - if (wai === mark || wbi === mark) { - // matrix values @ i,j - var va = wai === mark ? xa[i] : zero; - var vb = wbi === mark ? xb[i] : zero; // Cij - - var vc = cf(va, vb); // check for zero - - if (!eq(vc, zero)) { - // push value - cvalues.push(vc); // increment pointer - - k++; - } else { - // remove value @ i, do not increment pointer - cindex.splice(k, 1); - } - } - } - } - } // update cptr - - cptr[columns] = cindex.length; // return sparse matrix - - return a.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm12.js - - var algorithm12_name = 'algorithm12'; - var algorithm12_dependencies = ['typed', 'DenseMatrix']; - var createAlgorithm12 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm12_name, algorithm12_dependencies, function (_ref) { - var typed = _ref.typed, - DenseMatrix = _ref.DenseMatrix; - - /** - * Iterates over SparseMatrix S nonzero items and invokes the callback function f(Sij, b). - * Callback function invoked MxN times. - * - * - * ┌ f(Sij, b) ; S(i,j) !== 0 - * C(i,j) = ┤ - * └ f(0, b) ; otherwise - * - * - * @param {Matrix} s The SparseMatrix instance (S) - * @param {Scalar} b The Scalar value - * @param {Function} callback The f(Aij,b) operation to invoke - * @param {boolean} inverse A true value indicates callback should be invoked f(b,Sij) - * - * @return {Matrix} DenseMatrix (C) - * - * https://github.com/josdejong/mathjs/pull/346#issuecomment-97626813 - */ - return function algorithm12(s, b, callback, inverse) { - // sparse matrix arrays - var avalues = s._values; - var aindex = s._index; - var aptr = s._ptr; - var asize = s._size; - var adt = s._datatype; // sparse matrix cannot be a Pattern matrix - - if (!avalues) { - throw new Error('Cannot perform operation on Pattern Sparse Matrix and Scalar value'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string') { - // datatype - dt = adt; // convert b to the same datatype - - b = typed.convert(b, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // result arrays - - var cdata = []; // workspaces - - var x = []; // marks indicating we have a value in x for a given column - - var w = []; // loop columns - - for (var j = 0; j < columns; j++) { - // columns mark - var mark = j + 1; // values in j - - for (var k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { - // row - var r = aindex[k]; // update workspace - - x[r] = avalues[k]; - w[r] = mark; - } // loop rows - - for (var i = 0; i < rows; i++) { - // initialize C on first column - if (j === 0) { - // create row array - cdata[i] = []; - } // check sparse matrix has a value @ i,j - - if (w[i] === mark) { - // invoke callback, update C - cdata[i][j] = inverse ? cf(b, x[i]) : cf(x[i], b); - } else { - // dense matrix value @ i, j - cdata[i][j] = inverse ? cf(b, 0) : cf(0, b); - } - } - } // return dense matrix - - return new DenseMatrix({ - data: cdata, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/function/arithmetic/mod.js - - var mod_name = 'mod'; - var mod_dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix']; - var createMod = /* #__PURE__ */Object(factory["a" /* factory */])(mod_name, mod_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - DenseMatrix = _ref.DenseMatrix; - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm05 = createAlgorithm05({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Calculates the modulus, the remainder of an integer division. - * - * For matrices, the function is evaluated element wise. - * - * The modulus is defined as: - * - * x - y * floor(x / y) - * - * See https://en.wikipedia.org/wiki/Modulo_operation. - * - * Syntax: - * - * math.mod(x, y) - * - * Examples: - * - * math.mod(8, 3) // returns 2 - * math.mod(11, 2) // returns 1 - * - * function isOdd(x) { - * return math.mod(x, 2) != 0 - * } - * - * isOdd(2) // returns false - * isOdd(3) // returns true - * - * See also: - * - * divide - * - * @param {number | BigNumber | Fraction | Array | Matrix} x Dividend - * @param {number | BigNumber | Fraction | Array | Matrix} y Divisor - * @return {number | BigNumber | Fraction | Array | Matrix} Returns the remainder of `x` divided by `y`. - */ - - return typed(mod_name, { - 'number, number': modNumber, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - if (y.isNeg()) { - throw new Error('Cannot calculate mod for a negative divisor'); - } - - return y.isZero() ? x : x.mod(y); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - if (y.compare(0) < 0) { - throw new Error('Cannot calculate mod for a negative divisor'); - } // Workaround suggested in Fraction.js library to calculate correct modulo for negative dividend - - return x.compare(0) >= 0 ? x.mod(y) : x.mod(y).add(y).mod(y); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm05(x, y, this, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm02(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/multiplyScalar.js - - var multiplyScalar_name = 'multiplyScalar'; - var multiplyScalar_dependencies = ['typed']; - var createMultiplyScalar = /* #__PURE__ */Object(factory["a" /* factory */])(multiplyScalar_name, multiplyScalar_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Multiply two scalar values, `x * y`. - * This function is meant for internal use: it is used by the public function - * `multiply` - * - * This function does not support collections (Array or Matrix). - * - * @param {number | BigNumber | Fraction | Complex | Unit} x First value to multiply - * @param {number | BigNumber | Fraction | Complex} y Second value to multiply - * @return {number | BigNumber | Fraction | Complex | Unit} Multiplication of `x` and `y` - * @private - */ - return typed('multiplyScalar', { - 'number, number': multiplyNumber, - 'Complex, Complex': function ComplexComplex(x, y) { - return x.mul(y); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return x.times(y); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.mul(y); - }, - 'number | Fraction | BigNumber | Complex, Unit': function numberFractionBigNumberComplexUnit(x, y) { - var res = y.clone(); - res.value = res.value === null ? res._normalize(x) : this(res.value, x); - return res; - }, - 'Unit, number | Fraction | BigNumber | Complex': function UnitNumberFractionBigNumberComplex(x, y) { - var res = x.clone(); - res.value = res.value === null ? res._normalize(y) : this(res.value, y); - return res; - }, - 'Unit, Unit': function UnitUnit(x, y) { - return x.multiply(y); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/multiply.js - - var multiply_name = 'multiply'; - var multiply_dependencies = ['typed', 'matrix', 'addScalar', 'multiplyScalar', 'equalScalar', 'dot']; - var createMultiply = /* #__PURE__ */Object(factory["a" /* factory */])(multiply_name, multiply_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - addScalar = _ref.addScalar, - multiplyScalar = _ref.multiplyScalar, - equalScalar = _ref.equalScalar, - dot = _ref.dot; - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - - function _validateMatrixDimensions(size1, size2) { - // check left operand dimensions - switch (size1.length) { - case 1: - // check size2 - switch (size2.length) { - case 1: - // Vector x Vector - if (size1[0] !== size2[0]) { - // throw error - throw new RangeError('Dimension mismatch in multiplication. Vectors must have the same length'); - } - - break; - - case 2: - // Vector x Matrix - if (size1[0] !== size2[0]) { - // throw error - throw new RangeError('Dimension mismatch in multiplication. Vector length (' + size1[0] + ') must match Matrix rows (' + size2[0] + ')'); - } - - break; - - default: - throw new Error('Can only multiply a 1 or 2 dimensional matrix (Matrix B has ' + size2.length + ' dimensions)'); - } - - break; - - case 2: - // check size2 - switch (size2.length) { - case 1: - // Matrix x Vector - if (size1[1] !== size2[0]) { - // throw error - throw new RangeError('Dimension mismatch in multiplication. Matrix columns (' + size1[1] + ') must match Vector length (' + size2[0] + ')'); - } - - break; - - case 2: - // Matrix x Matrix - if (size1[1] !== size2[0]) { - // throw error - throw new RangeError('Dimension mismatch in multiplication. Matrix A columns (' + size1[1] + ') must match Matrix B rows (' + size2[0] + ')'); - } - - break; - - default: - throw new Error('Can only multiply a 1 or 2 dimensional matrix (Matrix B has ' + size2.length + ' dimensions)'); - } - - break; - - default: - throw new Error('Can only multiply a 1 or 2 dimensional matrix (Matrix A has ' + size1.length + ' dimensions)'); - } - } - /** - * C = A * B - * - * @param {Matrix} a Dense Vector (N) - * @param {Matrix} b Dense Vector (N) - * - * @return {number} Scalar value - */ - - function _multiplyVectorVector(a, b, n) { - // check empty vector - if (n === 0) { - throw new Error('Cannot multiply two empty vectors'); - } - - return dot(a, b); - } - /** - * C = A * B - * - * @param {Matrix} a Dense Vector (M) - * @param {Matrix} b Matrix (MxN) - * - * @return {Matrix} Dense Vector (N) - */ - - function _multiplyVectorMatrix(a, b) { - // process storage - if (b.storage() !== 'dense') { - throw new Error('Support for SparseMatrix not implemented'); - } - - return _multiplyVectorDenseMatrix(a, b); - } - /** - * C = A * B - * - * @param {Matrix} a Dense Vector (M) - * @param {Matrix} b Dense Matrix (MxN) - * - * @return {Matrix} Dense Vector (N) - */ - - function _multiplyVectorDenseMatrix(a, b) { - // a dense - var adata = a._data; - var asize = a._size; - var adt = a._datatype; // b dense - - var bdata = b._data; - var bsize = b._size; - var bdt = b._datatype; // rows & columns - - var alength = asize[0]; - var bcolumns = bsize[1]; // datatype - - var dt; // addScalar signature to use - - var af = addScalar; // multiplyScalar signature to use - - var mf = multiplyScalar; // process data types - - if (adt && bdt && adt === bdt && typeof adt === 'string') { - // datatype - dt = adt; // find signatures that matches (dt, dt) - - af = typed.find(addScalar, [dt, dt]); - mf = typed.find(multiplyScalar, [dt, dt]); - } // result - - var c = []; // loop matrix columns - - for (var j = 0; j < bcolumns; j++) { - // sum (do not initialize it with zero) - var sum = mf(adata[0], bdata[0][j]); // loop vector - - for (var i = 1; i < alength; i++) { - // multiply & accumulate - sum = af(sum, mf(adata[i], bdata[i][j])); - } - - c[j] = sum; - } // return matrix - - return a.createDenseMatrix({ - data: c, - size: [bcolumns], - datatype: dt - }); - } - /** - * C = A * B - * - * @param {Matrix} a Matrix (MxN) - * @param {Matrix} b Dense Vector (N) - * - * @return {Matrix} Dense Vector (M) - */ - - var _multiplyMatrixVector = typed('_multiplyMatrixVector', { - 'DenseMatrix, any': _multiplyDenseMatrixVector, - 'SparseMatrix, any': _multiplySparseMatrixVector - }); - /** - * C = A * B - * - * @param {Matrix} a Matrix (MxN) - * @param {Matrix} b Matrix (NxC) - * - * @return {Matrix} Matrix (MxC) - */ - - var _multiplyMatrixMatrix = typed('_multiplyMatrixMatrix', { - 'DenseMatrix, DenseMatrix': _multiplyDenseMatrixDenseMatrix, - 'DenseMatrix, SparseMatrix': _multiplyDenseMatrixSparseMatrix, - 'SparseMatrix, DenseMatrix': _multiplySparseMatrixDenseMatrix, - 'SparseMatrix, SparseMatrix': _multiplySparseMatrixSparseMatrix - }); - /** - * C = A * B - * - * @param {Matrix} a DenseMatrix (MxN) - * @param {Matrix} b Dense Vector (N) - * - * @return {Matrix} Dense Vector (M) - */ - - function _multiplyDenseMatrixVector(a, b) { - // a dense - var adata = a._data; - var asize = a._size; - var adt = a._datatype; // b dense - - var bdata = b._data; - var bdt = b._datatype; // rows & columns - - var arows = asize[0]; - var acolumns = asize[1]; // datatype - - var dt; // addScalar signature to use - - var af = addScalar; // multiplyScalar signature to use - - var mf = multiplyScalar; // process data types - - if (adt && bdt && adt === bdt && typeof adt === 'string') { - // datatype - dt = adt; // find signatures that matches (dt, dt) - - af = typed.find(addScalar, [dt, dt]); - mf = typed.find(multiplyScalar, [dt, dt]); - } // result - - var c = []; // loop matrix a rows - - for (var i = 0; i < arows; i++) { - // current row - var row = adata[i]; // sum (do not initialize it with zero) - - var sum = mf(row[0], bdata[0]); // loop matrix a columns - - for (var j = 1; j < acolumns; j++) { - // multiply & accumulate - sum = af(sum, mf(row[j], bdata[j])); - } - - c[i] = sum; - } // return matrix - - return a.createDenseMatrix({ - data: c, - size: [arows], - datatype: dt - }); - } - /** - * C = A * B - * - * @param {Matrix} a DenseMatrix (MxN) - * @param {Matrix} b DenseMatrix (NxC) - * - * @return {Matrix} DenseMatrix (MxC) - */ - - function _multiplyDenseMatrixDenseMatrix(a, b) { - // a dense - var adata = a._data; - var asize = a._size; - var adt = a._datatype; // b dense - - var bdata = b._data; - var bsize = b._size; - var bdt = b._datatype; // rows & columns - - var arows = asize[0]; - var acolumns = asize[1]; - var bcolumns = bsize[1]; // datatype - - var dt; // addScalar signature to use - - var af = addScalar; // multiplyScalar signature to use - - var mf = multiplyScalar; // process data types - - if (adt && bdt && adt === bdt && typeof adt === 'string') { - // datatype - dt = adt; // find signatures that matches (dt, dt) - - af = typed.find(addScalar, [dt, dt]); - mf = typed.find(multiplyScalar, [dt, dt]); - } // result - - var c = []; // loop matrix a rows - - for (var i = 0; i < arows; i++) { - // current row - var row = adata[i]; // initialize row array - - c[i] = []; // loop matrix b columns - - for (var j = 0; j < bcolumns; j++) { - // sum (avoid initializing sum to zero) - var sum = mf(row[0], bdata[0][j]); // loop matrix a columns - - for (var x = 1; x < acolumns; x++) { - // multiply & accumulate - sum = af(sum, mf(row[x], bdata[x][j])); - } - - c[i][j] = sum; - } - } // return matrix - - return a.createDenseMatrix({ - data: c, - size: [arows, bcolumns], - datatype: dt - }); - } - /** - * C = A * B - * - * @param {Matrix} a DenseMatrix (MxN) - * @param {Matrix} b SparseMatrix (NxC) - * - * @return {Matrix} SparseMatrix (MxC) - */ - - function _multiplyDenseMatrixSparseMatrix(a, b) { - // a dense - var adata = a._data; - var asize = a._size; - var adt = a._datatype; // b sparse - - var bvalues = b._values; - var bindex = b._index; - var bptr = b._ptr; - var bsize = b._size; - var bdt = b._datatype; // validate b matrix - - if (!bvalues) { - throw new Error('Cannot multiply Dense Matrix times Pattern only Matrix'); - } // rows & columns - - var arows = asize[0]; - var bcolumns = bsize[1]; // datatype - - var dt; // addScalar signature to use - - var af = addScalar; // multiplyScalar signature to use - - var mf = multiplyScalar; // equalScalar signature to use - - var eq = equalScalar; // zero value - - var zero = 0; // process data types - - if (adt && bdt && adt === bdt && typeof adt === 'string') { - // datatype - dt = adt; // find signatures that matches (dt, dt) - - af = typed.find(addScalar, [dt, dt]); - mf = typed.find(multiplyScalar, [dt, dt]); - eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype - - zero = typed.convert(0, dt); - } // result - - var cvalues = []; - var cindex = []; - var cptr = []; // c matrix - - var c = b.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [arows, bcolumns], - datatype: dt - }); // loop b columns - - for (var jb = 0; jb < bcolumns; jb++) { - // update ptr - cptr[jb] = cindex.length; // indeces in column jb - - var kb0 = bptr[jb]; - var kb1 = bptr[jb + 1]; // do not process column jb if no data exists - - if (kb1 > kb0) { - // last row mark processed - var last = 0; // loop a rows - - for (var i = 0; i < arows; i++) { - // column mark - var mark = i + 1; // C[i, jb] - - var cij = void 0; // values in b column j - - for (var kb = kb0; kb < kb1; kb++) { - // row - var ib = bindex[kb]; // check value has been initialized - - if (last !== mark) { - // first value in column jb - cij = mf(adata[i][ib], bvalues[kb]); // update mark - - last = mark; - } else { - // accumulate value - cij = af(cij, mf(adata[i][ib], bvalues[kb])); - } - } // check column has been processed and value != 0 - - if (last === mark && !eq(cij, zero)) { - // push row & value - cindex.push(i); - cvalues.push(cij); - } - } - } - } // update ptr - - cptr[bcolumns] = cindex.length; // return sparse matrix - - return c; - } - /** - * C = A * B - * - * @param {Matrix} a SparseMatrix (MxN) - * @param {Matrix} b Dense Vector (N) - * - * @return {Matrix} SparseMatrix (M, 1) - */ - - function _multiplySparseMatrixVector(a, b) { - // a sparse - var avalues = a._values; - var aindex = a._index; - var aptr = a._ptr; - var adt = a._datatype; // validate a matrix - - if (!avalues) { - throw new Error('Cannot multiply Pattern only Matrix times Dense Matrix'); - } // b dense - - var bdata = b._data; - var bdt = b._datatype; // rows & columns - - var arows = a._size[0]; - var brows = b._size[0]; // result - - var cvalues = []; - var cindex = []; - var cptr = []; // datatype - - var dt; // addScalar signature to use - - var af = addScalar; // multiplyScalar signature to use - - var mf = multiplyScalar; // equalScalar signature to use - - var eq = equalScalar; // zero value - - var zero = 0; // process data types - - if (adt && bdt && adt === bdt && typeof adt === 'string') { - // datatype - dt = adt; // find signatures that matches (dt, dt) - - af = typed.find(addScalar, [dt, dt]); - mf = typed.find(multiplyScalar, [dt, dt]); - eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype - - zero = typed.convert(0, dt); - } // workspace - - var x = []; // vector with marks indicating a value x[i] exists in a given column - - var w = []; // update ptr - - cptr[0] = 0; // rows in b - - for (var ib = 0; ib < brows; ib++) { - // b[ib] - var vbi = bdata[ib]; // check b[ib] != 0, avoid loops - - if (!eq(vbi, zero)) { - // A values & index in ib column - for (var ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { - // a row - var ia = aindex[ka]; // check value exists in current j - - if (!w[ia]) { - // ia is new entry in j - w[ia] = true; // add i to pattern of C - - cindex.push(ia); // x(ia) = A - - x[ia] = mf(vbi, avalues[ka]); - } else { - // i exists in C already - x[ia] = af(x[ia], mf(vbi, avalues[ka])); - } - } - } - } // copy values from x to column jb of c - - for (var p1 = cindex.length, p = 0; p < p1; p++) { - // row - var ic = cindex[p]; // copy value - - cvalues[p] = x[ic]; - } // update ptr - - cptr[1] = cindex.length; // return sparse matrix - - return a.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [arows, 1], - datatype: dt - }); - } - /** - * C = A * B - * - * @param {Matrix} a SparseMatrix (MxN) - * @param {Matrix} b DenseMatrix (NxC) - * - * @return {Matrix} SparseMatrix (MxC) - */ - - function _multiplySparseMatrixDenseMatrix(a, b) { - // a sparse - var avalues = a._values; - var aindex = a._index; - var aptr = a._ptr; - var adt = a._datatype; // validate a matrix - - if (!avalues) { - throw new Error('Cannot multiply Pattern only Matrix times Dense Matrix'); - } // b dense - - var bdata = b._data; - var bdt = b._datatype; // rows & columns - - var arows = a._size[0]; - var brows = b._size[0]; - var bcolumns = b._size[1]; // datatype - - var dt; // addScalar signature to use - - var af = addScalar; // multiplyScalar signature to use - - var mf = multiplyScalar; // equalScalar signature to use - - var eq = equalScalar; // zero value - - var zero = 0; // process data types - - if (adt && bdt && adt === bdt && typeof adt === 'string') { - // datatype - dt = adt; // find signatures that matches (dt, dt) - - af = typed.find(addScalar, [dt, dt]); - mf = typed.find(multiplyScalar, [dt, dt]); - eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype - - zero = typed.convert(0, dt); - } // result - - var cvalues = []; - var cindex = []; - var cptr = []; // c matrix - - var c = a.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [arows, bcolumns], - datatype: dt - }); // workspace - - var x = []; // vector with marks indicating a value x[i] exists in a given column - - var w = []; // loop b columns - - for (var jb = 0; jb < bcolumns; jb++) { - // update ptr - cptr[jb] = cindex.length; // mark in workspace for current column - - var mark = jb + 1; // rows in jb - - for (var ib = 0; ib < brows; ib++) { - // b[ib, jb] - var vbij = bdata[ib][jb]; // check b[ib, jb] != 0, avoid loops - - if (!eq(vbij, zero)) { - // A values & index in ib column - for (var ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { - // a row - var ia = aindex[ka]; // check value exists in current j - - if (w[ia] !== mark) { - // ia is new entry in j - w[ia] = mark; // add i to pattern of C - - cindex.push(ia); // x(ia) = A - - x[ia] = mf(vbij, avalues[ka]); - } else { - // i exists in C already - x[ia] = af(x[ia], mf(vbij, avalues[ka])); - } - } - } - } // copy values from x to column jb of c - - for (var p0 = cptr[jb], p1 = cindex.length, p = p0; p < p1; p++) { - // row - var ic = cindex[p]; // copy value - - cvalues[p] = x[ic]; - } - } // update ptr - - cptr[bcolumns] = cindex.length; // return sparse matrix - - return c; - } - /** - * C = A * B - * - * @param {Matrix} a SparseMatrix (MxN) - * @param {Matrix} b SparseMatrix (NxC) - * - * @return {Matrix} SparseMatrix (MxC) - */ - - function _multiplySparseMatrixSparseMatrix(a, b) { - // a sparse - var avalues = a._values; - var aindex = a._index; - var aptr = a._ptr; - var adt = a._datatype; // b sparse - - var bvalues = b._values; - var bindex = b._index; - var bptr = b._ptr; - var bdt = b._datatype; // rows & columns - - var arows = a._size[0]; - var bcolumns = b._size[1]; // flag indicating both matrices (a & b) contain data - - var values = avalues && bvalues; // datatype - - var dt; // addScalar signature to use - - var af = addScalar; // multiplyScalar signature to use - - var mf = multiplyScalar; // process data types - - if (adt && bdt && adt === bdt && typeof adt === 'string') { - // datatype - dt = adt; // find signatures that matches (dt, dt) - - af = typed.find(addScalar, [dt, dt]); - mf = typed.find(multiplyScalar, [dt, dt]); - } // result - - var cvalues = values ? [] : undefined; - var cindex = []; - var cptr = []; // c matrix - - var c = a.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [arows, bcolumns], - datatype: dt - }); // workspace - - var x = values ? [] : undefined; // vector with marks indicating a value x[i] exists in a given column - - var w = []; // variables - - var ka, ka0, ka1, kb, kb0, kb1, ia, ib; // loop b columns - - for (var jb = 0; jb < bcolumns; jb++) { - // update ptr - cptr[jb] = cindex.length; // mark in workspace for current column - - var mark = jb + 1; // B values & index in j - - for (kb0 = bptr[jb], kb1 = bptr[jb + 1], kb = kb0; kb < kb1; kb++) { - // b row - ib = bindex[kb]; // check we need to process values - - if (values) { - // loop values in a[:,ib] - for (ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { - // row - ia = aindex[ka]; // check value exists in current j - - if (w[ia] !== mark) { - // ia is new entry in j - w[ia] = mark; // add i to pattern of C - - cindex.push(ia); // x(ia) = A - - x[ia] = mf(bvalues[kb], avalues[ka]); - } else { - // i exists in C already - x[ia] = af(x[ia], mf(bvalues[kb], avalues[ka])); - } - } - } else { - // loop values in a[:,ib] - for (ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { - // row - ia = aindex[ka]; // check value exists in current j - - if (w[ia] !== mark) { - // ia is new entry in j - w[ia] = mark; // add i to pattern of C - - cindex.push(ia); - } - } - } - } // check we need to process matrix values (pattern matrix) - - if (values) { - // copy values from x to column jb of c - for (var p0 = cptr[jb], p1 = cindex.length, p = p0; p < p1; p++) { - // row - var ic = cindex[p]; // copy value - - cvalues[p] = x[ic]; - } - } - } // update ptr - - cptr[bcolumns] = cindex.length; // return sparse matrix - - return c; - } - /** - * Multiply two or more values, `x * y`. - * For matrices, the matrix product is calculated. - * - * Syntax: - * - * math.multiply(x, y) - * math.multiply(x, y, z, ...) - * - * Examples: - * - * math.multiply(4, 5.2) // returns number 20.8 - * math.multiply(2, 3, 4) // returns number 24 - * - * const a = math.complex(2, 3) - * const b = math.complex(4, 1) - * math.multiply(a, b) // returns Complex 5 + 14i - * - * const c = [[1, 2], [4, 3]] - * const d = [[1, 2, 3], [3, -4, 7]] - * math.multiply(c, d) // returns Array [[7, -6, 17], [13, -4, 33]] - * - * const e = math.unit('2.1 km') - * math.multiply(3, e) // returns Unit 6.3 km - * - * See also: - * - * divide, prod, cross, dot - * - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First value to multiply - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second value to multiply - * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Multiplication of `x` and `y` - */ - - return typed(multiply_name, Object(utils_object["e" /* extend */])({ - // we extend the signatures of multiplyScalar with signatures dealing with matrices - 'Array, Array': function ArrayArray(x, y) { - // check dimensions - _validateMatrixDimensions(Object(utils_array["a" /* arraySize */])(x), Object(utils_array["a" /* arraySize */])(y)); // use dense matrix implementation - - var m = this(matrix(x), matrix(y)); // return array or scalar - - return Object(is["v" /* isMatrix */])(m) ? m.valueOf() : m; - }, - 'Matrix, Matrix': function MatrixMatrix(x, y) { - // dimensions - var xsize = x.size(); - var ysize = y.size(); // check dimensions - - _validateMatrixDimensions(xsize, ysize); // process dimensions - - if (xsize.length === 1) { - // process y dimensions - if (ysize.length === 1) { - // Vector * Vector - return _multiplyVectorVector(x, y, xsize[0]); - } // Vector * Matrix - - return _multiplyVectorMatrix(x, y); - } // process y dimensions - - if (ysize.length === 1) { - // Matrix * Vector - return _multiplyMatrixVector(x, y); - } // Matrix * Matrix - - return _multiplyMatrixMatrix(x, y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use Matrix * Matrix implementation - return this(x, matrix(y)); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use Matrix * Matrix implementation - return this(matrix(x, y.storage()), y); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm11(x, y, multiplyScalar, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, multiplyScalar, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm11(y, x, multiplyScalar, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, multiplyScalar, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, multiplyScalar, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, multiplyScalar, true).valueOf(); - }, - 'any, any': multiplyScalar, - 'any, any, ...any': function anyAnyAny(x, y, rest) { - var result = this(x, y); - - for (var i = 0; i < rest.length; i++) { - result = this(result, rest[i]); - } - - return result; - } - }, multiplyScalar.signatures)); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/nthRoot.js - - var nthRoot_name = 'nthRoot'; - var nthRoot_dependencies = ['typed', 'matrix', 'equalScalar', 'BigNumber']; - var createNthRoot = /* #__PURE__ */Object(factory["a" /* factory */])(nthRoot_name, nthRoot_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - _BigNumber = _ref.BigNumber; - var algorithm01 = createAlgorithm01({ - typed: typed - }); - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm06 = createAlgorithm06({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Calculate the nth root of a value. - * The principal nth root of a positive real number A, is the positive real - * solution of the equation - * - * x^root = A - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.nthRoot(a) - * math.nthRoot(a, root) - * - * Examples: - * - * math.nthRoot(9, 2) // returns 3, as 3^2 == 9 - * math.sqrt(9) // returns 3, as 3^2 == 9 - * math.nthRoot(64, 3) // returns 4, as 4^3 == 64 - * - * See also: - * - * sqrt, pow - * - * @param {number | BigNumber | Array | Matrix | Complex} a - * Value for which to calculate the nth root - * @param {number | BigNumber} [root=2] The root. - * @return {number | Complex | Array | Matrix} Returns the nth root of `a` - */ - - var complexErr = '' + 'Complex number not supported in function nthRoot. ' + 'Use nthRoots instead.'; - return typed(nthRoot_name, { - number: function number(x) { - return nthRootNumber(x, 2); - }, - 'number, number': nthRootNumber, - BigNumber: function BigNumber(x) { - return _bigNthRoot(x, new _BigNumber(2)); - }, - Complex: function Complex(x) { - throw new Error(complexErr); - }, - 'Complex, number': function ComplexNumber(x, y) { - throw new Error(complexErr); - }, - 'BigNumber, BigNumber': _bigNthRoot, - 'Array | Matrix': function ArrayMatrix(x) { - return this(x, 2); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - // density must be one (no zeros in matrix) - if (y.density() === 1) { - // sparse + sparse - return algorithm06(x, y, this); - } else { - // throw exception - throw new Error('Root must be non-zero'); - } - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm02(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - // density must be one (no zeros in matrix) - if (y.density() === 1) { - // dense + sparse - return algorithm01(x, y, this, false); - } else { - // throw exception - throw new Error('Root must be non-zero'); - } - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { - return algorithm14(x, y, this, false); - }, - 'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) { - // density must be one (no zeros in matrix) - if (y.density() === 1) { - // sparse - scalar - return algorithm11(y, x, this, true); - } else { - // throw exception - throw new Error('Root must be non-zero'); - } - }, - 'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) { - // use matrix implementation - return this(matrix(x), y).valueOf(); - }, - 'number | BigNumber, Array': function numberBigNumberArray(x, y) { - // use matrix implementation - return this(x, matrix(y)).valueOf(); - } - }); - /** - * Calculate the nth root of a for BigNumbers, solve x^root == a - * https://rosettacode.org/wiki/Nth_root#JavaScript - * @param {BigNumber} a - * @param {BigNumber} root - * @private - */ - - function _bigNthRoot(a, root) { - var precision = _BigNumber.precision; - - var Big = _BigNumber.clone({ - precision: precision + 2 - }); - - var zero = new _BigNumber(0); - var one = new Big(1); - var inv = root.isNegative(); - - if (inv) { - root = root.neg(); - } - - if (root.isZero()) { - throw new Error('Root must be non-zero'); - } - - if (a.isNegative() && !root.abs().mod(2).equals(1)) { - throw new Error('Root must be odd when a is negative.'); - } // edge cases zero and infinity - - if (a.isZero()) { - return inv ? new Big(Infinity) : 0; - } - - if (!a.isFinite()) { - return inv ? zero : a; - } - - var x = a.abs().pow(one.div(root)); // If a < 0, we require that root is an odd integer, - // so (-1) ^ (1/root) = -1 - - x = a.isNeg() ? x.neg() : x; - return new _BigNumber((inv ? one.div(x) : x).toPrecision(precision)); - } - }); - var createNthRootNumber = /* #__PURE__ */Object(factory["a" /* factory */])(nthRoot_name, ['typed'], function (_ref2) { - var typed = _ref2.typed; - return typed(nthRoot_name, { - number: nthRootNumber, - 'number, number': nthRootNumber - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/sign.js - - var sign_name = 'sign'; - var sign_dependencies = ['typed', 'BigNumber', 'Fraction', 'complex']; - var createSign = /* #__PURE__ */Object(factory["a" /* factory */])(sign_name, sign_dependencies, function (_ref) { - var typed = _ref.typed, - _BigNumber = _ref.BigNumber, - complex = _ref.complex, - _Fraction = _ref.Fraction; - - /** - * Compute the sign of a value. The sign of a value x is: - * - * - 1 when x > 0 - * - -1 when x < 0 - * - 0 when x == 0 - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.sign(x) - * - * Examples: - * - * math.sign(3.5) // returns 1 - * math.sign(-4.2) // returns -1 - * math.sign(0) // returns 0 - * - * math.sign([3, 5, -2, 0, 2]) // returns [1, 1, -1, 0, 1] - * - * See also: - * - * abs - * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x - * The number for which to determine the sign - * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit}e - * The sign of `x` - */ - return typed(sign_name, { - number: signNumber, - Complex: function Complex(x) { - return x.im === 0 ? complex(signNumber(x.re)) : x.sign(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(x.cmp(0)); - }, - Fraction: function Fraction(x) { - return new _Fraction(x.s, 1); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since sign(0) = 0 - return deepMap(x, this, true); - }, - Unit: function Unit(x) { - return this(x.value); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/sqrt.js - - var sqrt_name = 'sqrt'; - var sqrt_dependencies = ['config', 'typed', 'Complex']; - var createSqrt = /* #__PURE__ */Object(factory["a" /* factory */])(sqrt_name, sqrt_dependencies, function (_ref) { - var config = _ref.config, - typed = _ref.typed, - Complex = _ref.Complex; - - /** - * Calculate the square root of a value. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.sqrt(x) - * - * Examples: - * - * math.sqrt(25) // returns 5 - * math.square(5) // returns 25 - * math.sqrt(-4) // returns Complex 2i - * - * See also: - * - * square, multiply, cube, cbrt, sqrtm - * - * @param {number | BigNumber | Complex | Array | Matrix | Unit} x - * Value for which to calculate the square root. - * @return {number | BigNumber | Complex | Array | Matrix | Unit} - * Returns the square root of `x` - */ - return typed('sqrt', { - number: _sqrtNumber, - Complex: function Complex(x) { - return x.sqrt(); - }, - BigNumber: function BigNumber(x) { - if (!x.isNegative() || config.predictable) { - return x.sqrt(); - } else { - // negative value -> downgrade to number to do complex value computation - return _sqrtNumber(x.toNumber()); - } - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since sqrt(0) = 0 - return deepMap(x, this, true); - }, - Unit: function Unit(x) { - // Someday will work for complex units when they are implemented - return x.pow(0.5); - } - }); - /** - * Calculate sqrt for a number - * @param {number} x - * @returns {number | Complex} Returns the square root of x - * @private - */ - - function _sqrtNumber(x) { - if (isNaN(x)) { - return NaN; - } else if (x >= 0 || config.predictable) { - return Math.sqrt(x); - } else { - return new Complex(x, 0).sqrt(); - } - } - }); - // CONCATENATED MODULE: ./src/function/arithmetic/square.js - - var square_name = 'square'; - var square_dependencies = ['typed']; - var createSquare = /* #__PURE__ */Object(factory["a" /* factory */])(square_name, square_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Compute the square of a value, `x * x`. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.square(x) - * - * Examples: - * - * math.square(2) // returns number 4 - * math.square(3) // returns number 9 - * math.pow(3, 2) // returns number 9 - * math.multiply(3, 3) // returns number 9 - * - * math.square([1, 2, 3, 4]) // returns Array [1, 4, 9, 16] - * - * See also: - * - * multiply, cube, sqrt, pow - * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x - * Number for which to calculate the square - * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} - * Squared value - */ - return typed(square_name, { - number: squareNumber, - Complex: function Complex(x) { - return x.mul(x); - }, - BigNumber: function BigNumber(x) { - return x.times(x); - }, - Fraction: function Fraction(x) { - return x.mul(x); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since square(0) = 0 - return deepMap(x, this, true); - }, - Unit: function Unit(x) { - return x.pow(2); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/subtract.js - - var subtract_name = 'subtract'; - var subtract_dependencies = ['typed', 'matrix', 'equalScalar', 'addScalar', 'unaryMinus', 'DenseMatrix']; - var createSubtract = /* #__PURE__ */Object(factory["a" /* factory */])(subtract_name, subtract_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - addScalar = _ref.addScalar, - unaryMinus = _ref.unaryMinus, - DenseMatrix = _ref.DenseMatrix; - // TODO: split function subtract in two: subtract and subtractScalar - var algorithm01 = createAlgorithm01({ - typed: typed - }); - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm05 = createAlgorithm05({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm10 = createAlgorithm10({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Subtract two values, `x - y`. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.subtract(x, y) - * - * Examples: - * - * math.subtract(5.3, 2) // returns number 3.3 - * - * const a = math.complex(2, 3) - * const b = math.complex(4, 1) - * math.subtract(a, b) // returns Complex -2 + 2i - * - * math.subtract([5, 7, 4], 4) // returns Array [1, 3, 0] - * - * const c = math.unit('2.1 km') - * const d = math.unit('500m') - * math.subtract(c, d) // returns Unit 1.6 km - * - * See also: - * - * add - * - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x - * Initial value - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y - * Value to subtract from `x` - * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} - * Subtraction of `x` and `y` - */ - - return typed(subtract_name, { - 'number, number': function numberNumber(x, y) { - return x - y; - }, - 'Complex, Complex': function ComplexComplex(x, y) { - return x.sub(y); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return x.minus(y); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.sub(y); - }, - 'Unit, Unit': function UnitUnit(x, y) { - if (x.value === null) { - throw new Error('Parameter x contains a unit with undefined value'); - } - - if (y.value === null) { - throw new Error('Parameter y contains a unit with undefined value'); - } - - if (!x.equalBase(y)) { - throw new Error('Units do not match'); - } - - var res = x.clone(); - res.value = this(res.value, y.value); - res.fixPrefix = false; - return res; - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - checkEqualDimensions(x, y); - return algorithm05(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - checkEqualDimensions(x, y); - return algorithm03(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - checkEqualDimensions(x, y); - return algorithm01(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - checkEqualDimensions(x, y); - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm10(x, unaryMinus(y), addScalar); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm10(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - /** - * Check whether matrix x and y have the same number of dimensions. - * Throws a DimensionError when dimensions are not equal - * @param {Matrix} x - * @param {Matrix} y - */ - - function checkEqualDimensions(x, y) { - var xsize = x.size(); - var ysize = y.size(); - - if (xsize.length !== ysize.length) { - throw new DimensionError["a" /* DimensionError */](xsize.length, ysize.length); - } - } - // CONCATENATED MODULE: ./src/function/arithmetic/xgcd.js - - var xgcd_name = 'xgcd'; - var xgcd_dependencies = ['typed', 'config', 'matrix', 'BigNumber']; - var createXgcd = /* #__PURE__ */Object(factory["a" /* factory */])(xgcd_name, xgcd_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix, - BigNumber = _ref.BigNumber; - - /** - * Calculate the extended greatest common divisor for two values. - * See https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm. - * - * Syntax: - * - * math.xgcd(a, b) - * - * Examples: - * - * math.xgcd(8, 12) // returns [4, -1, 1] - * math.gcd(8, 12) // returns 4 - * math.xgcd(36163, 21199) // returns [1247, -7, 12] - * - * See also: - * - * gcd, lcm - * - * @param {number | BigNumber} a An integer number - * @param {number | BigNumber} b An integer number - * @return {Array} Returns an array containing 3 integers `[div, m, n]` - * where `div = gcd(a, b)` and `a*m + b*n = div` - */ - return typed(xgcd_name, { - 'number, number': function numberNumber(a, b) { - var res = xgcdNumber(a, b); - return config.matrix === 'Array' ? res : matrix(res); - }, - 'BigNumber, BigNumber': _xgcdBigNumber // TODO: implement support for Fraction - - }); - /** - * Calculate xgcd for two BigNumbers - * @param {BigNumber} a - * @param {BigNumber} b - * @return {BigNumber[]} result - * @private - */ - - function _xgcdBigNumber(a, b) { - // source: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm - var // used to swap two variables - t; - var // quotient - q; - var // remainder - r; - var zero = new BigNumber(0); - var one = new BigNumber(1); - var x = zero; - var lastx = one; - var y = one; - var lasty = zero; - - if (!a.isInt() || !b.isInt()) { - throw new Error('Parameters in function xgcd must be integer numbers'); - } - - while (!b.isZero()) { - q = a.div(b).floor(); - r = a.mod(b); - t = x; - x = lastx.minus(q.times(x)); - lastx = t; - t = y; - y = lasty.minus(q.times(y)); - lasty = t; - a = b; - b = r; - } - - var res; - - if (a.lt(zero)) { - res = [a.neg(), lastx.neg(), lasty.neg()]; - } else { - res = [a, !a.isZero() ? lastx : 0, lasty]; - } - - return config.matrix === 'Array' ? res : matrix(res); - } - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm09.js - - var algorithm09_name = 'algorithm09'; - var algorithm09_dependencies = ['typed', 'equalScalar']; - var createAlgorithm09 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm09_name, algorithm09_dependencies, function (_ref) { - var typed = _ref.typed, - equalScalar = _ref.equalScalar; - - /** - * Iterates over SparseMatrix A and invokes the callback function f(Aij, Bij). - * Callback function invoked NZA times, number of nonzero elements in A. - * - * - * ┌ f(Aij, Bij) ; A(i,j) !== 0 - * C(i,j) = ┤ - * └ 0 ; otherwise - * - * - * @param {Matrix} a The SparseMatrix instance (A) - * @param {Matrix} b The SparseMatrix instance (B) - * @param {Function} callback The f(Aij,Bij) operation to invoke - * - * @return {Matrix} SparseMatrix (C) - * - * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 - */ - return function algorithm09(a, b, callback) { - // sparse matrix arrays - var avalues = a._values; - var aindex = a._index; - var aptr = a._ptr; - var asize = a._size; - var adt = a._datatype; // sparse matrix arrays - - var bvalues = b._values; - var bindex = b._index; - var bptr = b._ptr; - var bsize = b._size; - var bdt = b._datatype; // validate dimensions - - if (asize.length !== bsize.length) { - throw new DimensionError["a" /* DimensionError */](asize.length, bsize.length); - } // check rows & columns - - if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) { - throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string' && adt === bdt) { - // datatype - dt = adt; // find signature that matches (dt, dt) - - eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype - - zero = typed.convert(0, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // result arrays - - var cvalues = avalues && bvalues ? [] : undefined; - var cindex = []; - var cptr = []; // workspaces - - var x = cvalues ? [] : undefined; // marks indicating we have a value in x for a given column - - var w = []; // vars - - var i, j, k, k0, k1; // loop columns - - for (j = 0; j < columns; j++) { - // update cptr - cptr[j] = cindex.length; // column mark - - var mark = j + 1; // check we need to process values - - if (x) { - // loop B(:,j) - for (k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { - // row - i = bindex[k]; // update workspace - - w[i] = mark; - x[i] = bvalues[k]; - } - } // loop A(:,j) - - for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { - // row - i = aindex[k]; // check we need to process values - - if (x) { - // b value @ i,j - var vb = w[i] === mark ? x[i] : zero; // invoke f - - var vc = cf(avalues[k], vb); // check zero value - - if (!eq(vc, zero)) { - // push index - cindex.push(i); // push value - - cvalues.push(vc); - } - } else { - // push index - cindex.push(i); - } - } - } // update cptr - - cptr[columns] = cindex.length; // return sparse matrix - - return a.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/function/arithmetic/dotMultiply.js - - var dotMultiply_name = 'dotMultiply'; - var dotMultiply_dependencies = ['typed', 'matrix', 'equalScalar', 'multiplyScalar']; - var createDotMultiply = /* #__PURE__ */Object(factory["a" /* factory */])(dotMultiply_name, dotMultiply_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - multiplyScalar = _ref.multiplyScalar; - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm09 = createAlgorithm09({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Multiply two matrices element wise. The function accepts both matrices and - * scalar values. - * - * Syntax: - * - * math.dotMultiply(x, y) - * - * Examples: - * - * math.dotMultiply(2, 4) // returns 8 - * - * a = [[9, 5], [6, 1]] - * b = [[3, 2], [5, 2]] - * - * math.dotMultiply(a, b) // returns [[27, 10], [30, 2]] - * math.multiply(a, b) // returns [[52, 28], [23, 14]] - * - * See also: - * - * multiply, divide, dotDivide - * - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Left hand value - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Right hand value - * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Multiplication of `x` and `y` - */ - - return typed(dotMultiply_name, { - 'any, any': multiplyScalar, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm09(x, y, multiplyScalar, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm02(y, x, multiplyScalar, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm02(x, y, multiplyScalar, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, multiplyScalar); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm11(x, y, multiplyScalar, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, multiplyScalar, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm11(y, x, multiplyScalar, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, multiplyScalar, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, multiplyScalar, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, multiplyScalar, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/utils/bignumber/bitwise.js - /** - * Bitwise and for Bignumbers - * - * Special Cases: - * N & n = N - * n & 0 = 0 - * n & -1 = n - * n & n = n - * I & I = I - * -I & -I = -I - * I & -I = 0 - * I & n = n - * I & -n = I - * -I & n = 0 - * -I & -n = -I - * - * @param {BigNumber} x - * @param {BigNumber} y - * @return {BigNumber} Result of `x` & `y`, is fully precise - * @private - */ - function bitAndBigNumber(x, y) { - if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) { - throw new Error('Integers expected in function bitAnd'); - } - - var BigNumber = x.constructor; - - if (x.isNaN() || y.isNaN()) { - return new BigNumber(NaN); - } - - if (x.isZero() || y.eq(-1) || x.eq(y)) { - return x; - } - - if (y.isZero() || x.eq(-1)) { - return y; - } - - if (!x.isFinite() || !y.isFinite()) { - if (!x.isFinite() && !y.isFinite()) { - if (x.isNegative() === y.isNegative()) { - return x; - } - - return new BigNumber(0); - } - - if (!x.isFinite()) { - if (y.isNegative()) { - return x; - } - - if (x.isNegative()) { - return new BigNumber(0); - } - - return y; - } - - if (!y.isFinite()) { - if (x.isNegative()) { - return y; - } - - if (y.isNegative()) { - return new BigNumber(0); - } - - return x; - } - } - - return bitwise(x, y, function (a, b) { - return a & b; - }); - } - /** - * Bitwise not - * @param {BigNumber} x - * @return {BigNumber} Result of ~`x`, fully precise - * - */ - - function bitNotBigNumber(x) { - if (x.isFinite() && !x.isInteger()) { - throw new Error('Integer expected in function bitNot'); - } - - var BigNumber = x.constructor; - var prevPrec = BigNumber.precision; - BigNumber.config({ - precision: 1E9 - }); - var result = x.plus(new BigNumber(1)); - result.s = -result.s || null; - BigNumber.config({ - precision: prevPrec - }); - return result; - } - /** - * Bitwise OR for BigNumbers - * - * Special Cases: - * N | n = N - * n | 0 = n - * n | -1 = -1 - * n | n = n - * I | I = I - * -I | -I = -I - * I | -n = -1 - * I | -I = -1 - * I | n = I - * -I | n = -I - * -I | -n = -n - * - * @param {BigNumber} x - * @param {BigNumber} y - * @return {BigNumber} Result of `x` | `y`, fully precise - */ - - function bitOrBigNumber(x, y) { - if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) { - throw new Error('Integers expected in function bitOr'); - } - - var BigNumber = x.constructor; - - if (x.isNaN() || y.isNaN()) { - return new BigNumber(NaN); - } - - var negOne = new BigNumber(-1); - - if (x.isZero() || y.eq(negOne) || x.eq(y)) { - return y; - } - - if (y.isZero() || x.eq(negOne)) { - return x; - } - - if (!x.isFinite() || !y.isFinite()) { - if (!x.isFinite() && !x.isNegative() && y.isNegative() || x.isNegative() && !y.isNegative() && !y.isFinite()) { - return negOne; - } - - if (x.isNegative() && y.isNegative()) { - return x.isFinite() ? x : y; - } - - return x.isFinite() ? y : x; - } - - return bitwise(x, y, function (a, b) { - return a | b; - }); - } - /** - * Applies bitwise function to numbers - * @param {BigNumber} x - * @param {BigNumber} y - * @param {function (a, b)} func - * @return {BigNumber} - */ - - function bitwise(x, y, func) { - var BigNumber = x.constructor; - var xBits, yBits; - var xSign = +(x.s < 0); - var ySign = +(y.s < 0); - - if (xSign) { - xBits = decCoefficientToBinaryString(bitNotBigNumber(x)); - - for (var i = 0; i < xBits.length; ++i) { - xBits[i] ^= 1; - } - } else { - xBits = decCoefficientToBinaryString(x); - } - - if (ySign) { - yBits = decCoefficientToBinaryString(bitNotBigNumber(y)); - - for (var _i = 0; _i < yBits.length; ++_i) { - yBits[_i] ^= 1; - } - } else { - yBits = decCoefficientToBinaryString(y); - } - - var minBits, maxBits, minSign; - - if (xBits.length <= yBits.length) { - minBits = xBits; - maxBits = yBits; - minSign = xSign; - } else { - minBits = yBits; - maxBits = xBits; - minSign = ySign; - } - - var shortLen = minBits.length; - var longLen = maxBits.length; - var expFuncVal = func(xSign, ySign) ^ 1; - var outVal = new BigNumber(expFuncVal ^ 1); - var twoPower = new BigNumber(1); - var two = new BigNumber(2); - var prevPrec = BigNumber.precision; - BigNumber.config({ - precision: 1E9 - }); - - while (shortLen > 0) { - if (func(minBits[--shortLen], maxBits[--longLen]) === expFuncVal) { - outVal = outVal.plus(twoPower); - } - - twoPower = twoPower.times(two); - } - - while (longLen > 0) { - if (func(minSign, maxBits[--longLen]) === expFuncVal) { - outVal = outVal.plus(twoPower); - } - - twoPower = twoPower.times(two); - } - - BigNumber.config({ - precision: prevPrec - }); - - if (expFuncVal === 0) { - outVal.s = -outVal.s; - } - - return outVal; - } - /* Extracted from decimal.js, and edited to specialize. */ - - function decCoefficientToBinaryString(x) { - // Convert to string - var a = x.d; // array with digits - - var r = a[0] + ''; - - for (var i = 1; i < a.length; ++i) { - var s = a[i] + ''; - - for (var z = 7 - s.length; z--;) { - s = '0' + s; - } - - r += s; - } - - var j = r.length; - - while (r.charAt(j) === '0') { - j--; - } - - var xe = x.e; - var str = r.slice(0, j + 1 || 1); - var strL = str.length; - - if (xe > 0) { - if (++xe > strL) { - // Append zeros. - xe -= strL; - - while (xe--) { - str += '0'; - } - } else if (xe < strL) { - str = str.slice(0, xe) + '.' + str.slice(xe); - } - } // Convert from base 10 (decimal) to base 2 - - var arr = [0]; - - for (var _i2 = 0; _i2 < str.length;) { - var arrL = arr.length; - - while (arrL--) { - arr[arrL] *= 10; - } - - arr[0] += parseInt(str.charAt(_i2++)); // convert to int - - for (var _j = 0; _j < arr.length; ++_j) { - if (arr[_j] > 1) { - if (arr[_j + 1] === null || arr[_j + 1] === undefined) { - arr[_j + 1] = 0; - } - - arr[_j + 1] += arr[_j] >> 1; - arr[_j] &= 1; - } - } - } - - return arr.reverse(); - } - /** - * Bitwise XOR for BigNumbers - * - * Special Cases: - * N ^ n = N - * n ^ 0 = n - * n ^ n = 0 - * n ^ -1 = ~n - * I ^ n = I - * I ^ -n = -I - * I ^ -I = -1 - * -I ^ n = -I - * -I ^ -n = I - * - * @param {BigNumber} x - * @param {BigNumber} y - * @return {BigNumber} Result of `x` ^ `y`, fully precise - * - */ - - function bitXor(x, y) { - if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) { - throw new Error('Integers expected in function bitXor'); - } - - var BigNumber = x.constructor; - - if (x.isNaN() || y.isNaN()) { - return new BigNumber(NaN); - } - - if (x.isZero()) { - return y; - } - - if (y.isZero()) { - return x; - } - - if (x.eq(y)) { - return new BigNumber(0); - } - - var negOne = new BigNumber(-1); - - if (x.eq(negOne)) { - return bitNotBigNumber(y); - } - - if (y.eq(negOne)) { - return bitNotBigNumber(x); - } - - if (!x.isFinite() || !y.isFinite()) { - if (!x.isFinite() && !y.isFinite()) { - return negOne; - } - - return new BigNumber(x.isNegative() === y.isNegative() ? Infinity : -Infinity); - } - - return bitwise(x, y, function (a, b) { - return a ^ b; - }); - } - /** - * Bitwise left shift - * - * Special Cases: - * n << -n = N - * n << N = N - * N << n = N - * n << 0 = n - * 0 << n = 0 - * I << I = N - * I << n = I - * n << I = I - * - * @param {BigNumber} x - * @param {BigNumber} y - * @return {BigNumber} Result of `x` << `y` - * - */ - - function leftShiftBigNumber(x, y) { - if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) { - throw new Error('Integers expected in function leftShift'); - } - - var BigNumber = x.constructor; - - if (x.isNaN() || y.isNaN() || y.isNegative() && !y.isZero()) { - return new BigNumber(NaN); - } - - if (x.isZero() || y.isZero()) { - return x; - } - - if (!x.isFinite() && !y.isFinite()) { - return new BigNumber(NaN); - } // Math.pow(2, y) is fully precise for y < 55, and fast - - if (y.lt(55)) { - return x.times(Math.pow(2, y.toNumber()) + ''); - } - - return x.times(new BigNumber(2).pow(y)); - } - /* - * Special Cases: - * n >> -n = N - * n >> N = N - * N >> n = N - * I >> I = N - * n >> 0 = n - * I >> n = I - * -I >> n = -I - * -I >> I = -I - * n >> I = I - * -n >> I = -1 - * 0 >> n = 0 - * - * @param {BigNumber} value - * @param {BigNumber} value - * @return {BigNumber} Result of `x` >> `y` - * - */ - - function rightArithShiftBigNumber(x, y) { - if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) { - throw new Error('Integers expected in function rightArithShift'); - } - - var BigNumber = x.constructor; - - if (x.isNaN() || y.isNaN() || y.isNegative() && !y.isZero()) { - return new BigNumber(NaN); - } - - if (x.isZero() || y.isZero()) { - return x; - } - - if (!y.isFinite()) { - if (x.isNegative()) { - return new BigNumber(-1); - } - - if (!x.isFinite()) { - return new BigNumber(NaN); - } - - return new BigNumber(0); - } // Math.pow(2, y) is fully precise for y < 55, and fast - - if (y.lt(55)) { - return x.div(Math.pow(2, y.toNumber()) + '').floor(); - } - - return x.div(new BigNumber(2).pow(y)).floor(); - } - // CONCATENATED MODULE: ./src/plain/number/bitwise.js - - var bitwise_n1 = 'number'; - var bitwise_n2 = 'number, number'; - function bitAndNumber(x, y) { - if (!Object(utils_number["i" /* isInteger */])(x) || !Object(utils_number["i" /* isInteger */])(y)) { - throw new Error('Integers expected in function bitAnd'); - } - - return x & y; - } - bitAndNumber.signature = bitwise_n2; - function bitNotNumber(x) { - if (!Object(utils_number["i" /* isInteger */])(x)) { - throw new Error('Integer expected in function bitNot'); - } - - return ~x; - } - bitNotNumber.signature = bitwise_n1; - function bitOrNumber(x, y) { - if (!Object(utils_number["i" /* isInteger */])(x) || !Object(utils_number["i" /* isInteger */])(y)) { - throw new Error('Integers expected in function bitOr'); - } - - return x | y; - } - bitOrNumber.signature = bitwise_n2; - function bitXorNumber(x, y) { - if (!Object(utils_number["i" /* isInteger */])(x) || !Object(utils_number["i" /* isInteger */])(y)) { - throw new Error('Integers expected in function bitXor'); - } - - return x ^ y; - } - bitXorNumber.signature = bitwise_n2; - function leftShiftNumber(x, y) { - if (!Object(utils_number["i" /* isInteger */])(x) || !Object(utils_number["i" /* isInteger */])(y)) { - throw new Error('Integers expected in function leftShift'); - } - - return x << y; - } - leftShiftNumber.signature = bitwise_n2; - function rightArithShiftNumber(x, y) { - if (!Object(utils_number["i" /* isInteger */])(x) || !Object(utils_number["i" /* isInteger */])(y)) { - throw new Error('Integers expected in function rightArithShift'); - } - - return x >> y; - } - rightArithShiftNumber.signature = bitwise_n2; - function rightLogShiftNumber(x, y) { - if (!Object(utils_number["i" /* isInteger */])(x) || !Object(utils_number["i" /* isInteger */])(y)) { - throw new Error('Integers expected in function rightLogShift'); - } - - return x >>> y; - } - rightLogShiftNumber.signature = bitwise_n2; - // CONCATENATED MODULE: ./src/function/bitwise/bitAnd.js - - var bitAnd_name = 'bitAnd'; - var bitAnd_dependencies = ['typed', 'matrix', 'equalScalar']; - var createBitAnd = /* #__PURE__ */Object(factory["a" /* factory */])(bitAnd_name, bitAnd_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar; - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm06 = createAlgorithm06({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Bitwise AND two values, `x & y`. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.bitAnd(x, y) - * - * Examples: - * - * math.bitAnd(53, 131) // returns number 1 - * - * math.bitAnd([1, 12, 31], 42) // returns Array [0, 8, 10] - * - * See also: - * - * bitNot, bitOr, bitXor, leftShift, rightArithShift, rightLogShift - * - * @param {number | BigNumber | Array | Matrix} x First value to and - * @param {number | BigNumber | Array | Matrix} y Second value to and - * @return {number | BigNumber | Array | Matrix} AND of `x` and `y` - */ - - return typed(bitAnd_name, { - 'number, number': bitAndNumber, - 'BigNumber, BigNumber': bitAndBigNumber, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm06(x, y, this, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm02(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm02(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm11(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/bitwise/bitNot.js - - var bitNot_name = 'bitNot'; - var bitNot_dependencies = ['typed']; - var createBitNot = /* #__PURE__ */Object(factory["a" /* factory */])(bitNot_name, bitNot_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Bitwise NOT value, `~x`. - * For matrices, the function is evaluated element wise. - * For units, the function is evaluated on the best prefix base. - * - * Syntax: - * - * math.bitNot(x) - * - * Examples: - * - * math.bitNot(1) // returns number -2 - * - * math.bitNot([2, -3, 4]) // returns Array [-3, 2, 5] - * - * See also: - * - * bitAnd, bitOr, bitXor, leftShift, rightArithShift, rightLogShift - * - * @param {number | BigNumber | Array | Matrix} x Value to not - * @return {number | BigNumber | Array | Matrix} NOT of `x` - */ - return typed(bitNot_name, { - number: bitNotNumber, - BigNumber: bitNotBigNumber, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/bitwise/bitOr.js - - var bitOr_name = 'bitOr'; - var bitOr_dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix']; - var createBitOr = /* #__PURE__ */Object(factory["a" /* factory */])(bitOr_name, bitOr_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - DenseMatrix = _ref.DenseMatrix; - var algorithm01 = createAlgorithm01({ - typed: typed - }); - var algorithm04 = createAlgorithm04({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm10 = createAlgorithm10({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Bitwise OR two values, `x | y`. - * For matrices, the function is evaluated element wise. - * For units, the function is evaluated on the lowest print base. - * - * Syntax: - * - * math.bitOr(x, y) - * - * Examples: - * - * math.bitOr(1, 2) // returns number 3 - * - * math.bitOr([1, 2, 3], 4) // returns Array [5, 6, 7] - * - * See also: - * - * bitAnd, bitNot, bitXor, leftShift, rightArithShift, rightLogShift - * - * @param {number | BigNumber | Array | Matrix} x First value to or - * @param {number | BigNumber | Array | Matrix} y Second value to or - * @return {number | BigNumber | Array | Matrix} OR of `x` and `y` - */ - - return typed(bitOr_name, { - 'number, number': bitOrNumber, - 'BigNumber, BigNumber': bitOrBigNumber, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm04(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm01(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm01(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm10(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm10(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm07.js - - var algorithm07_name = 'algorithm07'; - var algorithm07_dependencies = ['typed', 'DenseMatrix']; - var createAlgorithm07 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm07_name, algorithm07_dependencies, function (_ref) { - var typed = _ref.typed, - DenseMatrix = _ref.DenseMatrix; - - /** - * Iterates over SparseMatrix A and SparseMatrix B items (zero and nonzero) and invokes the callback function f(Aij, Bij). - * Callback function invoked MxN times. - * - * C(i,j) = f(Aij, Bij) - * - * @param {Matrix} a The SparseMatrix instance (A) - * @param {Matrix} b The SparseMatrix instance (B) - * @param {Function} callback The f(Aij,Bij) operation to invoke - * - * @return {Matrix} DenseMatrix (C) - * - * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 - */ - return function algorithm07(a, b, callback) { - // sparse matrix arrays - var asize = a._size; - var adt = a._datatype; // sparse matrix arrays - - var bsize = b._size; - var bdt = b._datatype; // validate dimensions - - if (asize.length !== bsize.length) { - throw new DimensionError["a" /* DimensionError */](asize.length, bsize.length); - } // check rows & columns - - if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) { - throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // zero value - - var zero = 0; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string' && adt === bdt) { - // datatype - dt = adt; // convert 0 to the same datatype - - zero = typed.convert(0, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // vars - - var i, j; // result arrays - - var cdata = []; // initialize c - - for (i = 0; i < rows; i++) { - cdata[i] = []; - } // workspaces - - var xa = []; - var xb = []; // marks indicating we have a value in x for a given column - - var wa = []; - var wb = []; // loop columns - - for (j = 0; j < columns; j++) { - // columns mark - var mark = j + 1; // scatter the values of A(:,j) into workspace - - _scatter(a, j, wa, xa, mark); // scatter the values of B(:,j) into workspace - - _scatter(b, j, wb, xb, mark); // loop rows - - for (i = 0; i < rows; i++) { - // matrix values @ i,j - var va = wa[i] === mark ? xa[i] : zero; - var vb = wb[i] === mark ? xb[i] : zero; // invoke callback - - cdata[i][j] = cf(va, vb); - } - } // return dense matrix - - return new DenseMatrix({ - data: cdata, - size: [rows, columns], - datatype: dt - }); - }; - - function _scatter(m, j, w, x, mark) { - // a arrays - var values = m._values; - var index = m._index; - var ptr = m._ptr; // loop values in column j - - for (var k = ptr[j], k1 = ptr[j + 1]; k < k1; k++) { - // row - var i = index[k]; // update workspace - - w[i] = mark; - x[i] = values[k]; - } - } - }); - // CONCATENATED MODULE: ./src/function/bitwise/bitXor.js - - var bitXor_name = 'bitXor'; - var bitXor_dependencies = ['typed', 'matrix', 'DenseMatrix']; - var createBitXor = /* #__PURE__ */Object(factory["a" /* factory */])(bitXor_name, bitXor_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm07 = createAlgorithm07({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Bitwise XOR two values, `x ^ y`. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.bitXor(x, y) - * - * Examples: - * - * math.bitXor(1, 2) // returns number 3 - * - * math.bitXor([2, 3, 4], 4) // returns Array [6, 7, 0] - * - * See also: - * - * bitAnd, bitNot, bitOr, leftShift, rightArithShift, rightLogShift - * - * @param {number | BigNumber | Array | Matrix} x First value to xor - * @param {number | BigNumber | Array | Matrix} y Second value to xor - * @return {number | BigNumber | Array | Matrix} XOR of `x` and `y` - */ - - return typed(bitXor_name, { - 'number, number': bitXorNumber, - 'BigNumber, BigNumber': bitXor, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm07(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm12(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/complex/arg.js - - var arg_name = 'arg'; - var arg_dependencies = ['typed']; - var createArg = /* #__PURE__ */Object(factory["a" /* factory */])(arg_name, arg_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Compute the argument of a complex value. - * For a complex number `a + bi`, the argument is computed as `atan2(b, a)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.arg(x) - * - * Examples: - * - * const a = math.complex(2, 2) - * math.arg(a) / math.pi // returns number 0.25 - * - * const b = math.complex('2 + 3i') - * math.arg(b) // returns number 0.982793723247329 - * math.atan2(3, 2) // returns number 0.982793723247329 - * - * See also: - * - * re, im, conj, abs - * - * @param {number | BigNumber | Complex | Array | Matrix} x - * A complex number or array with complex numbers - * @return {number | BigNumber | Array | Matrix} The argument of x - */ - return typed(arg_name, { - number: function number(x) { - return Math.atan2(0, x); - }, - BigNumber: function BigNumber(x) { - return x.constructor.atan2(0, x); - }, - Complex: function Complex(x) { - return x.arg(); - }, - // TODO: implement BigNumber support for function arg - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/complex/conj.js - - var conj_name = 'conj'; - var conj_dependencies = ['typed']; - var createConj = /* #__PURE__ */Object(factory["a" /* factory */])(conj_name, conj_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Compute the complex conjugate of a complex value. - * If `x = a+bi`, the complex conjugate of `x` is `a - bi`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.conj(x) - * - * Examples: - * - * math.conj(math.complex('2 + 3i')) // returns Complex 2 - 3i - * math.conj(math.complex('2 - 3i')) // returns Complex 2 + 3i - * math.conj(math.complex('-5.2i')) // returns Complex 5.2i - * - * See also: - * - * re, im, arg, abs - * - * @param {number | BigNumber | Complex | Array | Matrix} x - * A complex number or array with complex numbers - * @return {number | BigNumber | Complex | Array | Matrix} - * The complex conjugate of x - */ - return typed(conj_name, { - number: function number(x) { - return x; - }, - BigNumber: function BigNumber(x) { - return x; - }, - Complex: function Complex(x) { - return x.conjugate(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/complex/im.js - - var im_name = 'im'; - var im_dependencies = ['typed']; - var createIm = /* #__PURE__ */Object(factory["a" /* factory */])(im_name, im_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Get the imaginary part of a complex number. - * For a complex number `a + bi`, the function returns `b`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.im(x) - * - * Examples: - * - * const a = math.complex(2, 3) - * math.re(a) // returns number 2 - * math.im(a) // returns number 3 - * - * math.re(math.complex('-5.2i')) // returns number -5.2 - * math.re(math.complex(2.4)) // returns number 0 - * - * See also: - * - * re, conj, abs, arg - * - * @param {number | BigNumber | Complex | Array | Matrix} x - * A complex number or array with complex numbers - * @return {number | BigNumber | Array | Matrix} The imaginary part of x - */ - return typed(im_name, { - number: function number(x) { - return 0; - }, - BigNumber: function BigNumber(x) { - return x.mul(0); - }, - Complex: function Complex(x) { - return x.im; - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/complex/re.js - - var re_name = 're'; - var re_dependencies = ['typed']; - var createRe = /* #__PURE__ */Object(factory["a" /* factory */])(re_name, re_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Get the real part of a complex number. - * For a complex number `a + bi`, the function returns `a`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.re(x) - * - * Examples: - * - * const a = math.complex(2, 3) - * math.re(a) // returns number 2 - * math.im(a) // returns number 3 - * - * math.re(math.complex('-5.2i')) // returns number 0 - * math.re(math.complex(2.4)) // returns number 2.4 - * - * See also: - * - * im, conj, abs, arg - * - * @param {number | BigNumber | Complex | Array | Matrix} x - * A complex number or array with complex numbers - * @return {number | BigNumber | Array | Matrix} The real part of x - */ - return typed(re_name, { - number: function number(x) { - return x; - }, - BigNumber: function BigNumber(x) { - return x; - }, - Complex: function Complex(x) { - return x.re; - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/plain/number/logical.js - var logical_n1 = 'number'; - var logical_n2 = 'number, number'; - function notNumber(x) { - return !x; - } - notNumber.signature = logical_n1; - function orNumber(x, y) { - return !!(x || y); - } - orNumber.signature = logical_n2; - function xorNumber(x, y) { - return !!x !== !!y; - } - xorNumber.signature = logical_n2; - function andNumber(x, y) { - return !!(x && y); - } - andNumber.signature = logical_n2; - // CONCATENATED MODULE: ./src/function/logical/not.js - - var not_name = 'not'; - var not_dependencies = ['typed']; - var createNot = /* #__PURE__ */Object(factory["a" /* factory */])(not_name, not_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Logical `not`. Flips boolean value of a given parameter. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.not(x) - * - * Examples: - * - * math.not(2) // returns false - * math.not(0) // returns true - * math.not(true) // returns false - * - * a = [2, -7, 0] - * math.not(a) // returns [false, false, true] - * - * See also: - * - * and, or, xor - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check - * @return {boolean | Array | Matrix} - * Returns true when input is a zero or empty value. - */ - return typed(not_name, { - number: notNumber, - Complex: function Complex(x) { - return x.re === 0 && x.im === 0; - }, - BigNumber: function BigNumber(x) { - return x.isZero() || x.isNaN(); - }, - Unit: function Unit(x) { - return x.value !== null ? this(x.value) : true; - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/logical/or.js - - var or_name = 'or'; - var or_dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix']; - var createOr = /* #__PURE__ */Object(factory["a" /* factory */])(or_name, or_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm05 = createAlgorithm05({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Logical `or`. Test if at least one value is defined with a nonzero/nonempty value. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.or(x, y) - * - * Examples: - * - * math.or(2, 4) // returns true - * - * a = [2, 5, 0] - * b = [0, 22, 0] - * c = 0 - * - * math.or(a, b) // returns [true, true, false] - * math.or(b, c) // returns [false, true, false] - * - * See also: - * - * and, not, xor - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check - * @param {number | BigNumber | Complex | Unit | Array | Matrix} y Second value to check - * @return {boolean | Array | Matrix} - * Returns true when one of the inputs is defined with a nonzero/nonempty value. - */ - - return typed(or_name, { - 'number, number': orNumber, - 'Complex, Complex': function ComplexComplex(x, y) { - return x.re !== 0 || x.im !== 0 || y.re !== 0 || y.im !== 0; - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return !x.isZero() && !x.isNaN() || !y.isZero() && !y.isNaN(); - }, - 'Unit, Unit': function UnitUnit(x, y) { - return this(x.value || 0, y.value || 0); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm05(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm12(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/logical/xor.js - - var xor_name = 'xor'; - var xor_dependencies = ['typed', 'matrix', 'DenseMatrix']; - var createXor = /* #__PURE__ */Object(factory["a" /* factory */])(xor_name, xor_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm07 = createAlgorithm07({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Logical `xor`. Test whether one and only one value is defined with a nonzero/nonempty value. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.xor(x, y) - * - * Examples: - * - * math.xor(2, 4) // returns false - * - * a = [2, 0, 0] - * b = [2, 7, 0] - * c = 0 - * - * math.xor(a, b) // returns [false, true, false] - * math.xor(a, c) // returns [true, false, false] - * - * See also: - * - * and, not, or - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check - * @param {number | BigNumber | Complex | Unit | Array | Matrix} y Second value to check - * @return {boolean | Array | Matrix} - * Returns true when one and only one input is defined with a nonzero/nonempty value. - */ - - return typed(xor_name, { - 'number, number': xorNumber, - 'Complex, Complex': function ComplexComplex(x, y) { - return (x.re !== 0 || x.im !== 0) !== (y.re !== 0 || y.im !== 0); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return (!x.isZero() && !x.isNaN()) !== (!y.isZero() && !y.isNaN()); - }, - 'Unit, Unit': function UnitUnit(x, y) { - return this(x.value || 0, y.value || 0); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm07(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm12(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/matrix/concat.js - - var concat_name = 'concat'; - var concat_dependencies = ['typed', 'matrix', 'isInteger']; - var createConcat = /* #__PURE__ */Object(factory["a" /* factory */])(concat_name, concat_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - isInteger = _ref.isInteger; - - /** - * Concatenate two or more matrices. - * - * Syntax: - * - * math.concat(A, B, C, ...) - * math.concat(A, B, C, ..., dim) - * - * Where: - * - * - `dim: number` is a zero-based dimension over which to concatenate the matrices. - * By default the last dimension of the matrices. - * - * Examples: - * - * const A = [[1, 2], [5, 6]] - * const B = [[3, 4], [7, 8]] - * - * math.concat(A, B) // returns [[1, 2, 3, 4], [5, 6, 7, 8]] - * math.concat(A, B, 0) // returns [[1, 2], [5, 6], [3, 4], [7, 8]] - * math.concat('hello', ' ', 'world') // returns 'hello world' - * - * See also: - * - * size, squeeze, subset, transpose - * - * @param {... Array | Matrix} args Two or more matrices - * @return {Array | Matrix} Concatenated matrix - */ - return typed(concat_name, { - // TODO: change signature to '...Array | Matrix, dim?' when supported - '...Array | Matrix | number | BigNumber': function ArrayMatrixNumberBigNumber(args) { - var i; - var len = args.length; - var dim = -1; // zero-based dimension - - var prevDim; - var asMatrix = false; - var matrices = []; // contains multi dimensional arrays - - for (i = 0; i < len; i++) { - var arg = args[i]; // test whether we need to return a Matrix (if not we return an Array) - - if (Object(is["v" /* isMatrix */])(arg)) { - asMatrix = true; - } - - if (Object(is["y" /* isNumber */])(arg) || Object(is["e" /* isBigNumber */])(arg)) { - if (i !== len - 1) { - throw new Error('Dimension must be specified as last argument'); - } // last argument contains the dimension on which to concatenate - - prevDim = dim; - dim = arg.valueOf(); // change BigNumber to number - - if (!isInteger(dim)) { - throw new TypeError('Integer number expected for dimension'); - } - - if (dim < 0 || i > 0 && dim > prevDim) { - // TODO: would be more clear when throwing a DimensionError here - throw new IndexError["a" /* IndexError */](dim, prevDim + 1); - } - } else { - // this is a matrix or array - var m = Object(utils_object["a" /* clone */])(arg).valueOf(); - var size = Object(utils_array["a" /* arraySize */])(m); - matrices[i] = m; - prevDim = dim; - dim = size.length - 1; // verify whether each of the matrices has the same number of dimensions - - if (i > 0 && dim !== prevDim) { - throw new DimensionError["a" /* DimensionError */](prevDim + 1, dim + 1); - } - } - } - - if (matrices.length === 0) { - throw new SyntaxError('At least one matrix expected'); - } - - var res = matrices.shift(); - - while (matrices.length) { - res = _concat(res, matrices.shift(), dim, 0); - } - - return asMatrix ? matrix(res) : res; - }, - '...string': function string(args) { - return args.join(''); - } - }); - }); - /** - * Recursively concatenate two matrices. - * The contents of the matrices is not cloned. - * @param {Array} a Multi dimensional array - * @param {Array} b Multi dimensional array - * @param {number} concatDim The dimension on which to concatenate (zero-based) - * @param {number} dim The current dim (zero-based) - * @return {Array} c The concatenated matrix - * @private - */ - - function _concat(a, b, concatDim, dim) { - if (dim < concatDim) { - // recurse into next dimension - if (a.length !== b.length) { - throw new DimensionError["a" /* DimensionError */](a.length, b.length); - } - - var c = []; - - for (var i = 0; i < a.length; i++) { - c[i] = _concat(a[i], b[i], concatDim, dim + 1); - } - - return c; - } else { - // concatenate this dimension - return a.concat(b); - } - } - // CONCATENATED MODULE: ./src/function/matrix/column.js - - var column_name = 'column'; - var column_dependencies = ['typed', 'Index', 'matrix', 'range']; - var createColumn = /* #__PURE__ */Object(factory["a" /* factory */])(column_name, column_dependencies, function (_ref) { - var typed = _ref.typed, - Index = _ref.Index, - matrix = _ref.matrix, - range = _ref.range; - - /** - * Return a column from a Matrix. - * - * Syntax: - * - * math.column(value, index) - * - * Example: - * - * // get a column - * const d = [[1, 2], [3, 4]] - * math.column(d, 1) // returns [[2], [4]] - * - * See also: - * - * row - * - * @param {Array | Matrix } value An array or matrix - * @param {number} column The index of the column - * @return {Array | Matrix} The retrieved column - */ - return typed(column_name, { - 'Matrix, number': _column, - 'Array, number': function ArrayNumber(value, column) { - return _column(matrix(Object(utils_object["a" /* clone */])(value)), column).valueOf(); - } - }); - /** - * Retrieve a column of a matrix - * @param {Matrix } value A matrix - * @param {number} column The index of the column - * @return {Matrix} The retrieved column - */ - - function _column(value, column) { - // check dimensions - if (value.size().length !== 2) { - throw new Error('Only two dimensional matrix is supported'); - } - - Object(utils_array["q" /* validateIndex */])(column, value.size()[1]); - var rowRange = range(0, value.size()[0]); - var index = new Index(rowRange, column); - return value.subset(index); - } - }); - // CONCATENATED MODULE: ./src/function/matrix/cross.js - - var cross_name = 'cross'; - var cross_dependencies = ['typed', 'matrix', 'subtract', 'multiply']; - var createCross = /* #__PURE__ */Object(factory["a" /* factory */])(cross_name, cross_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - subtract = _ref.subtract, - multiply = _ref.multiply; - - /** - * Calculate the cross product for two vectors in three dimensional space. - * The cross product of `A = [a1, a2, a3]` and `B = [b1, b2, b3]` is defined - * as: - * - * cross(A, B) = [ - * a2 * b3 - a3 * b2, - * a3 * b1 - a1 * b3, - * a1 * b2 - a2 * b1 - * ] - * - * If one of the input vectors has a dimension greater than 1, the output - * vector will be a 1x3 (2-dimensional) matrix. - * - * Syntax: - * - * math.cross(x, y) - * - * Examples: - * - * math.cross([1, 1, 0], [0, 1, 1]) // Returns [1, -1, 1] - * math.cross([3, -3, 1], [4, 9, 2]) // Returns [-15, -2, 39] - * math.cross([2, 3, 4], [5, 6, 7]) // Returns [-3, 6, -3] - * math.cross([[1, 2, 3]], [[4], [5], [6]]) // Returns [[-3, 6, -3]] - * - * See also: - * - * dot, multiply - * - * @param {Array | Matrix} x First vector - * @param {Array | Matrix} y Second vector - * @return {Array | Matrix} Returns the cross product of `x` and `y` - */ - return typed(cross_name, { - 'Matrix, Matrix': function MatrixMatrix(x, y) { - return matrix(_cross(x.toArray(), y.toArray())); - }, - 'Matrix, Array': function MatrixArray(x, y) { - return matrix(_cross(x.toArray(), y)); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - return matrix(_cross(x, y.toArray())); - }, - 'Array, Array': _cross - }); - /** - * Calculate the cross product for two arrays - * @param {Array} x First vector - * @param {Array} y Second vector - * @returns {Array} Returns the cross product of x and y - * @private - */ - - function _cross(x, y) { - var highestDimension = Math.max(Object(utils_array["a" /* arraySize */])(x).length, Object(utils_array["a" /* arraySize */])(y).length); - x = Object(utils_array["n" /* squeeze */])(x); - y = Object(utils_array["n" /* squeeze */])(y); - var xSize = Object(utils_array["a" /* arraySize */])(x); - var ySize = Object(utils_array["a" /* arraySize */])(y); - - if (xSize.length !== 1 || ySize.length !== 1 || xSize[0] !== 3 || ySize[0] !== 3) { - throw new RangeError('Vectors with length 3 expected ' + '(Size A = [' + xSize.join(', ') + '], B = [' + ySize.join(', ') + '])'); - } - - var product = [subtract(multiply(x[1], y[2]), multiply(x[2], y[1])), subtract(multiply(x[2], y[0]), multiply(x[0], y[2])), subtract(multiply(x[0], y[1]), multiply(x[1], y[0]))]; - - if (highestDimension > 1) { - return [product]; - } else { - return product; - } - } - }); - // CONCATENATED MODULE: ./src/function/matrix/diag.js - - var diag_name = 'diag'; - var diag_dependencies = ['typed', 'matrix', 'DenseMatrix', 'SparseMatrix']; - var createDiag = /* #__PURE__ */Object(factory["a" /* factory */])(diag_name, diag_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - DenseMatrix = _ref.DenseMatrix, - SparseMatrix = _ref.SparseMatrix; - - /** - * Create a diagonal matrix or retrieve the diagonal of a matrix - * - * When `x` is a vector, a matrix with vector `x` on the diagonal will be returned. - * When `x` is a two dimensional matrix, the matrixes `k`th diagonal will be returned as vector. - * When k is positive, the values are placed on the super diagonal. - * When k is negative, the values are placed on the sub diagonal. - * - * Syntax: - * - * math.diag(X) - * math.diag(X, format) - * math.diag(X, k) - * math.diag(X, k, format) - * - * Examples: - * - * // create a diagonal matrix - * math.diag([1, 2, 3]) // returns [[1, 0, 0], [0, 2, 0], [0, 0, 3]] - * math.diag([1, 2, 3], 1) // returns [[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3]] - * math.diag([1, 2, 3], -1) // returns [[0, 0, 0], [1, 0, 0], [0, 2, 0], [0, 0, 3]] - * - * // retrieve the diagonal from a matrix - * const a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] - * math.diag(a) // returns [1, 5, 9] - * - * See also: - * - * ones, zeros, identity - * - * @param {Matrix | Array} x A two dimensional matrix or a vector - * @param {number | BigNumber} [k=0] The diagonal where the vector will be filled - * in or retrieved. - * @param {string} [format='dense'] The matrix storage format. - * - * @returns {Matrix | Array} Diagonal matrix from input vector, or diagonal from input matrix. - */ - return typed(diag_name, { - // FIXME: simplify this huge amount of signatures as soon as typed-function supports optional arguments - Array: function Array(x) { - return _diag(x, 0, Object(utils_array["a" /* arraySize */])(x), null); - }, - 'Array, number': function ArrayNumber(x, k) { - return _diag(x, k, Object(utils_array["a" /* arraySize */])(x), null); - }, - 'Array, BigNumber': function ArrayBigNumber(x, k) { - return _diag(x, k.toNumber(), Object(utils_array["a" /* arraySize */])(x), null); - }, - 'Array, string': function ArrayString(x, format) { - return _diag(x, 0, Object(utils_array["a" /* arraySize */])(x), format); - }, - 'Array, number, string': function ArrayNumberString(x, k, format) { - return _diag(x, k, Object(utils_array["a" /* arraySize */])(x), format); - }, - 'Array, BigNumber, string': function ArrayBigNumberString(x, k, format) { - return _diag(x, k.toNumber(), Object(utils_array["a" /* arraySize */])(x), format); - }, - Matrix: function Matrix(x) { - return _diag(x, 0, x.size(), x.storage()); - }, - 'Matrix, number': function MatrixNumber(x, k) { - return _diag(x, k, x.size(), x.storage()); - }, - 'Matrix, BigNumber': function MatrixBigNumber(x, k) { - return _diag(x, k.toNumber(), x.size(), x.storage()); - }, - 'Matrix, string': function MatrixString(x, format) { - return _diag(x, 0, x.size(), format); - }, - 'Matrix, number, string': function MatrixNumberString(x, k, format) { - return _diag(x, k, x.size(), format); - }, - 'Matrix, BigNumber, string': function MatrixBigNumberString(x, k, format) { - return _diag(x, k.toNumber(), x.size(), format); - } - }); - /** - * Creeate diagonal matrix from a vector or vice versa - * @param {Array | Matrix} x - * @param {number} k - * @param {string} format Storage format for matrix. If null, - * an Array is returned - * @returns {Array | Matrix} - * @private - */ - - function _diag(x, k, size, format) { - if (!Object(utils_number["i" /* isInteger */])(k)) { - throw new TypeError('Second parameter in function diag must be an integer'); - } - - var kSuper = k > 0 ? k : 0; - var kSub = k < 0 ? -k : 0; // check dimensions - - switch (size.length) { - case 1: - return _createDiagonalMatrix(x, k, format, size[0], kSub, kSuper); - - case 2: - return _getDiagonal(x, k, format, size, kSub, kSuper); - } - - throw new RangeError('Matrix for function diag must be 2 dimensional'); - } - - function _createDiagonalMatrix(x, k, format, l, kSub, kSuper) { - // matrix size - var ms = [l + kSub, l + kSuper]; - - if (format && format !== 'sparse' && format !== 'dense') { - throw new TypeError("Unknown matrix type ".concat(format, "\"")); - } // create diagonal matrix - - var m = format === 'sparse' ? SparseMatrix.diagonal(ms, x, k) : DenseMatrix.diagonal(ms, x, k); // check we need to return a matrix - - return format !== null ? m : m.valueOf(); - } - - function _getDiagonal(x, k, format, s, kSub, kSuper) { - // check x is a Matrix - if (Object(is["v" /* isMatrix */])(x)) { - // get diagonal matrix - var dm = x.diagonal(k); // check we need to return a matrix - - if (format !== null) { - // check we need to change matrix format - if (format !== dm.storage()) { - return matrix(dm, format); - } - - return dm; - } - - return dm.valueOf(); - } // vector size - - var n = Math.min(s[0] - kSub, s[1] - kSuper); // diagonal values - - var vector = []; // loop diagonal - - for (var i = 0; i < n; i++) { - vector[i] = x[i + kSub][i + kSuper]; - } // check we need to return a matrix - - return format !== null ? matrix(vector) : vector; - } - }); - // CONCATENATED MODULE: ./src/utils/function.js - function function_typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - function_typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - function_typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return function_typeof(obj); - } - - // function utils - - /** - * Memoize a given function by caching the computed result. - * The cache of a memoized function can be cleared by deleting the `cache` - * property of the function. - * - * @param {function} fn The function to be memoized. - * Must be a pure function. - * @param {function(args: Array)} [hasher] A custom hash builder. - * Is JSON.stringify by default. - * @return {function} Returns the memoized function - */ - function memoize(fn, hasher) { - return function memoize() { - if (function_typeof(memoize.cache) !== 'object') { - memoize.cache = {}; - } - - var args = []; - - for (var i = 0; i < arguments.length; i++) { - args[i] = arguments[i]; - } - - var hash = hasher ? hasher(args) : JSON.stringify(args); - - if (!(hash in memoize.cache)) { - memoize.cache[hash] = fn.apply(fn, args); - } - - return memoize.cache[hash]; - }; - } - /** - * Memoize a given function by caching all results and the arguments, - * and comparing against the arguments of previous results before - * executing again. - * This is less performant than `memoize` which calculates a hash, - * which is very fast to compare. Use `memoizeCompare` only when it is - * not possible to create a unique serializable hash from the function - * arguments. - * The isEqual function must compare two sets of arguments - * and return true when equal (can be a deep equality check for example). - * @param {function} fn - * @param {function(a: *, b: *) : boolean} isEqual - * @returns {function} - */ - - function memoizeCompare(fn, isEqual) { - var memoize = function memoize() { - var args = []; - - for (var i = 0; i < arguments.length; i++) { - args[i] = arguments[i]; - } - - for (var c = 0; c < memoize.cache.length; c++) { - var cached = memoize.cache[c]; - - if (isEqual(args, cached.args)) { - // TODO: move this cache entry to the top so recently used entries move up? - return cached.res; - } - } - - var res = fn.apply(fn, args); - memoize.cache.unshift({ - args: args, - res: res - }); - return res; - }; - - memoize.cache = []; - return memoize; - } - /** - * Find the maximum number of arguments expected by a typed function. - * @param {function} fn A typed function - * @return {number} Returns the maximum number of expected arguments. - * Returns -1 when no signatures where found on the function. - */ - - function maxArgumentCount(fn) { - return Object.keys(fn.signatures || {}).reduce(function (args, signature) { - var count = (signature.match(/,/g) || []).length + 1; - return Math.max(args, count); - }, -1); - } - // CONCATENATED MODULE: ./src/function/matrix/filter.js - - var filter_name = 'filter'; - var filter_dependencies = ['typed']; - var createFilter = /* #__PURE__ */Object(factory["a" /* factory */])(filter_name, filter_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Filter the items in an array or one dimensional matrix. - * - * Syntax: - * - * math.filter(x, test) - * - * Examples: - * - * function isPositive (x) { - * return x > 0 - * } - * math.filter([6, -2, -1, 4, 3], isPositive) // returns [6, 4, 3] - * - * math.filter(["23", "foo", "100", "55", "bar"], /[0-9]+/) // returns ["23", "100", "55"] - * - * See also: - * - * forEach, map, sort - * - * @param {Matrix | Array} x A one dimensional matrix or array to filter - * @param {Function | RegExp} test - * A function or regular expression to test items. - * All entries for which `test` returns true are returned. - * When `test` is a function, it is invoked with three parameters: - * the value of the element, the index of the element, and the - * matrix/array being traversed. The function must return a boolean. - * @return {Matrix | Array} Returns the filtered matrix. - */ - return typed('filter', { - 'Array, function': _filterCallback, - 'Matrix, function': function MatrixFunction(x, test) { - return x.create(_filterCallback(x.toArray(), test)); - }, - 'Array, RegExp': utils_array["d" /* filterRegExp */], - 'Matrix, RegExp': function MatrixRegExp(x, test) { - return x.create(Object(utils_array["d" /* filterRegExp */])(x.toArray(), test)); - } - }); - }); - /** - * Filter values in a callback given a callback function - * @param {Array} x - * @param {Function} callback - * @return {Array} Returns the filtered array - * @private - */ - - function _filterCallback(x, callback) { - // figure out what number of arguments the callback function expects - var args = maxArgumentCount(callback); - return Object(utils_array["c" /* filter */])(x, function (value, index, array) { - // invoke the callback function with the right number of arguments - if (args === 1) { - return callback(value); - } else if (args === 2) { - return callback(value, [index]); - } else { - // 3 or -1 - return callback(value, [index], array); - } - }); - } - // CONCATENATED MODULE: ./src/function/matrix/flatten.js - - var flatten_name = 'flatten'; - var flatten_dependencies = ['typed', 'matrix']; - var createFlatten = /* #__PURE__ */Object(factory["a" /* factory */])(flatten_name, flatten_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix; - - /** - * Flatten a multi dimensional matrix into a single dimensional matrix. - * - * Syntax: - * - * math.flatten(x) - * - * Examples: - * - * math.flatten([[1,2], [3,4]]) // returns [1, 2, 3, 4] - * - * See also: - * - * concat, resize, size, squeeze - * - * @param {Matrix | Array} x Matrix to be flattened - * @return {Matrix | Array} Returns the flattened matrix - */ - return typed(flatten_name, { - Array: function Array(x) { - return Object(utils_array["e" /* flatten */])(Object(utils_object["a" /* clone */])(x)); - }, - Matrix: function Matrix(x) { - var flat = Object(utils_array["e" /* flatten */])(Object(utils_object["a" /* clone */])(x.toArray())); // TODO: return the same matrix type as x - - return matrix(flat); - } - }); - }); - // CONCATENATED MODULE: ./src/function/matrix/forEach.js - - var forEach_name = 'forEach'; - var forEach_dependencies = ['typed']; - var createForEach = /* #__PURE__ */Object(factory["a" /* factory */])(forEach_name, forEach_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Iterate over all elements of a matrix/array, and executes the given callback function. - * - * Syntax: - * - * math.forEach(x, callback) - * - * Examples: - * - * math.forEach([1, 2, 3], function(value) { - * console.log(value) - * }) - * // outputs 1, 2, 3 - * - * See also: - * - * filter, map, sort - * - * @param {Matrix | Array} x The matrix to iterate on. - * @param {Function} callback The callback function is invoked with three - * parameters: the value of the element, the index - * of the element, and the Matrix/array being traversed. - */ - return typed(forEach_name, { - 'Array, function': forEach_forEach, - 'Matrix, function': function MatrixFunction(x, callback) { - return x.forEach(callback); - } - }); - }); - /** - * forEach for a multi dimensional array - * @param {Array} array - * @param {Function} callback - * @private - */ - - function forEach_forEach(array, callback) { - // figure out what number of arguments the callback function expects - var args = maxArgumentCount(callback); - - var recurse = function recurse(value, index) { - if (Array.isArray(value)) { - Object(utils_array["f" /* forEach */])(value, function (child, i) { - // we create a copy of the index array and append the new index value - recurse(child, index.concat(i)); - }); - } else { - // invoke the callback function with the right number of arguments - if (args === 1) { - callback(value); - } else if (args === 2) { - callback(value, index); - } else { - // 3 or -1 - callback(value, index, array); - } - } - }; - - recurse(array, []); - } - // CONCATENATED MODULE: ./src/function/matrix/getMatrixDataType.js - - var getMatrixDataType_name = 'getMatrixDataType'; - var getMatrixDataType_dependencies = ['typed']; - var createGetMatrixDataType = /* #__PURE__ */Object(factory["a" /* factory */])(getMatrixDataType_name, getMatrixDataType_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Find the data type of all elements in a matrix or array, - * for example 'number' if all items are a number and 'Complex' if all values - * are complex numbers. - * If a matrix contains more than one data type, it will return 'mixed'. - * - * Syntax: - * - * math.getMatrixDataType(x) - * - * Examples: - * - * const x = [ [1, 2, 3], [4, 5, 6] ] - * const mixedX = [ [1, true], [2, 3] ] - * const fractionX = [ [math.fraction(1, 3)], [math.fraction(1, 3] ] - * const unitX = [ [math.unit('5cm')], [math.unit('5cm')] ] - * const bigNumberX = [ [math.bignumber(1)], [math.bignumber(0)] ] - * const sparse = math.sparse(x) - * const dense = math.matrix(x) - * math.getMatrixDataType(x) // returns 'number' - * math.getMatrixDataType(sparse) // returns 'number' - * math.getMatrixDataType(dense) // returns 'number' - * math.getMatrixDataType(mixedX) // returns 'mixed' - * math.getMatrixDataType(fractionX) // returns 'Fraction' - * math.getMatrixDataType(unitX) // returns 'Unit' - * math.getMatrixDataType(bigNumberX) // return 'BigNumber' - * - * See also: - * SparseMatrix, DenseMatrix - * - * @param {...Matrix | Array} x The Matrix with values. - * - * @return {string} A string representation of the matrix type - */ - return typed(getMatrixDataType_name, { - Array: function Array(x) { - return Object(utils_array["h" /* getArrayDataType */])(x, is["M" /* typeOf */]); - }, - Matrix: function Matrix(x) { - return x.getDataType(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/matrix/identity.js - - var identity_name = 'identity'; - var identity_dependencies = ['typed', 'config', 'matrix', 'BigNumber', 'DenseMatrix', 'SparseMatrix']; - var createIdentity = /* #__PURE__ */Object(factory["a" /* factory */])(identity_name, identity_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix, - BigNumber = _ref.BigNumber, - DenseMatrix = _ref.DenseMatrix, - SparseMatrix = _ref.SparseMatrix; - - /** - * Create a 2-dimensional identity matrix with size m x n or n x n. - * The matrix has ones on the diagonal and zeros elsewhere. - * - * Syntax: - * - * math.identity(n) - * math.identity(n, format) - * math.identity(m, n) - * math.identity(m, n, format) - * math.identity([m, n]) - * math.identity([m, n], format) - * - * Examples: - * - * math.identity(3) // returns [[1, 0, 0], [0, 1, 0], [0, 0, 1]] - * math.identity(3, 2) // returns [[1, 0], [0, 1], [0, 0]] - * - * const A = [[1, 2, 3], [4, 5, 6]] - * math.identity(math.size(A)) // returns [[1, 0, 0], [0, 1, 0]] - * - * See also: - * - * diag, ones, zeros, size, range - * - * @param {...number | Matrix | Array} size The size for the matrix - * @param {string} [format] The Matrix storage format - * - * @return {Matrix | Array | number} A matrix with ones on the diagonal. - */ - return typed(identity_name, { - '': function _() { - return config.matrix === 'Matrix' ? matrix([]) : []; - }, - string: function string(format) { - return matrix(format); - }, - 'number | BigNumber': function numberBigNumber(rows) { - return _identity(rows, rows, config.matrix === 'Matrix' ? 'dense' : undefined); - }, - 'number | BigNumber, string': function numberBigNumberString(rows, format) { - return _identity(rows, rows, format); - }, - 'number | BigNumber, number | BigNumber': function numberBigNumberNumberBigNumber(rows, cols) { - return _identity(rows, cols, config.matrix === 'Matrix' ? 'dense' : undefined); - }, - 'number | BigNumber, number | BigNumber, string': function numberBigNumberNumberBigNumberString(rows, cols, format) { - return _identity(rows, cols, format); - }, - Array: function Array(size) { - return _identityVector(size); - }, - 'Array, string': function ArrayString(size, format) { - return _identityVector(size, format); - }, - Matrix: function Matrix(size) { - return _identityVector(size.valueOf(), size.storage()); - }, - 'Matrix, string': function MatrixString(size, format) { - return _identityVector(size.valueOf(), format); - } - }); - - function _identityVector(size, format) { - switch (size.length) { - case 0: - return format ? matrix(format) : []; - - case 1: - return _identity(size[0], size[0], format); - - case 2: - return _identity(size[0], size[1], format); - - default: - throw new Error('Vector containing two values expected'); - } - } - /** - * Create an identity matrix - * @param {number | BigNumber} rows - * @param {number | BigNumber} cols - * @param {string} [format] - * @returns {Matrix} - * @private - */ - - function _identity(rows, cols, format) { - // BigNumber constructor with the right precision - var Big = Object(is["e" /* isBigNumber */])(rows) || Object(is["e" /* isBigNumber */])(cols) ? BigNumber : null; - if (Object(is["e" /* isBigNumber */])(rows)) { - rows = rows.toNumber(); - } - if (Object(is["e" /* isBigNumber */])(cols)) { - cols = cols.toNumber(); - } - - if (!Object(utils_number["i" /* isInteger */])(rows) || rows < 1) { - throw new Error('Parameters in function identity must be positive integers'); - } - - if (!Object(utils_number["i" /* isInteger */])(cols) || cols < 1) { - throw new Error('Parameters in function identity must be positive integers'); - } - - var one = Big ? new BigNumber(1) : 1; - var defaultValue = Big ? new Big(0) : 0; - var size = [rows, cols]; // check we need to return a matrix - - if (format) { - // create diagonal matrix (use optimized implementation for storage format) - if (format === 'sparse') { - return SparseMatrix.diagonal(size, one, 0, defaultValue); - } - - if (format === 'dense') { - return DenseMatrix.diagonal(size, one, 0, defaultValue); - } - - throw new TypeError("Unknown matrix type \"".concat(format, "\"")); - } // create and resize array - - var res = Object(utils_array["m" /* resize */])([], size, defaultValue); // fill in ones on the diagonal - - var minimum = rows < cols ? rows : cols; // fill diagonal - - for (var d = 0; d < minimum; d++) { - res[d][d] = one; - } - - return res; - } - }); - // CONCATENATED MODULE: ./src/function/matrix/kron.js - - var kron_name = 'kron'; - var kron_dependencies = ['typed', 'matrix', 'multiplyScalar']; - var createKron = /* #__PURE__ */Object(factory["a" /* factory */])(kron_name, kron_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - multiplyScalar = _ref.multiplyScalar; - - /** - * Calculates the kronecker product of 2 matrices or vectors. - * - * NOTE: If a one dimensional vector / matrix is given, it will be - * wrapped so its two dimensions. - * See the examples. - * - * Syntax: - * - * math.kron(x, y) - * - * Examples: - * - * math.kron([[1, 0], [0, 1]], [[1, 2], [3, 4]]) - * // returns [ [ 1, 2, 0, 0 ], [ 3, 4, 0, 0 ], [ 0, 0, 1, 2 ], [ 0, 0, 3, 4 ] ] - * - * math.kron([1,1], [2,3,4]) - * // returns [ [ 2, 3, 4, 2, 3, 4 ] ] - * - * See also: - * - * multiply, dot, cross - * - * @param {Array | Matrix} x First vector - * @param {Array | Matrix} y Second vector - * @return {Array | Matrix} Returns the kronecker product of `x` and `y` - */ - return typed(kron_name, { - 'Matrix, Matrix': function MatrixMatrix(x, y) { - return matrix(_kron(x.toArray(), y.toArray())); - }, - 'Matrix, Array': function MatrixArray(x, y) { - return matrix(_kron(x.toArray(), y)); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - return matrix(_kron(x, y.toArray())); - }, - 'Array, Array': _kron - }); - /** - * Calculate the kronecker product of two matrices / vectors - * @param {Array} a First vector - * @param {Array} b Second vector - * @returns {Array} Returns the kronecker product of x and y - * @private - */ - - function _kron(a, b) { - // Deal with the dimensions of the matricies. - if (Object(utils_array["a" /* arraySize */])(a).length === 1) { - // Wrap it in a 2D Matrix - a = [a]; - } - - if (Object(utils_array["a" /* arraySize */])(b).length === 1) { - // Wrap it in a 2D Matrix - b = [b]; - } - - if (Object(utils_array["a" /* arraySize */])(a).length > 2 || Object(utils_array["a" /* arraySize */])(b).length > 2) { - throw new RangeError('Vectors with dimensions greater then 2 are not supported expected ' + '(Size x = ' + JSON.stringify(a.length) + ', y = ' + JSON.stringify(b.length) + ')'); - } - - var t = []; - var r = []; - return a.map(function (a) { - return b.map(function (b) { - r = []; - t.push(r); - return a.map(function (y) { - return b.map(function (x) { - return r.push(multiplyScalar(y, x)); - }); - }); - }); - }) && t; - } - }); - // CONCATENATED MODULE: ./src/function/matrix/map.js - - var map_name = 'map'; - var map_dependencies = ['typed']; - var createMap = /* #__PURE__ */Object(factory["a" /* factory */])(map_name, map_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Create a new matrix or array with the results of the callback function executed on - * each entry of the matrix/array. - * - * Syntax: - * - * math.map(x, callback) - * - * Examples: - * - * math.map([1, 2, 3], function(value) { - * return value * value - * }) // returns [1, 4, 9] - * - * See also: - * - * filter, forEach, sort - * - * @param {Matrix | Array} x The matrix to iterate on. - * @param {Function} callback The callback method is invoked with three - * parameters: the value of the element, the index - * of the element, and the matrix being traversed. - * @return {Matrix | array} Transformed map of x - */ - return typed(map_name, { - 'Array, function': map_map, - 'Matrix, function': function MatrixFunction(x, callback) { - return x.map(callback); - } - }); - }); - /** - * Map for a multi dimensional array - * @param {Array} array - * @param {Function} callback - * @return {Array} - * @private - */ - - function map_map(array, callback) { - // figure out what number of arguments the callback function expects - var args = maxArgumentCount(callback); - - var recurse = function recurse(value, index) { - if (Array.isArray(value)) { - return value.map(function (child, i) { - // we create a copy of the index array and append the new index value - return recurse(child, index.concat(i)); - }); - } else { - // invoke the callback function with the right number of arguments - if (args === 1) { - return callback(value); - } else if (args === 2) { - return callback(value, index); - } else { - // 3 or -1 - return callback(value, index, array); - } - } - }; - - return recurse(array, []); - } - // CONCATENATED MODULE: ./src/function/matrix/diff.js - - var diff_name = 'diff'; - var diff_dependencies = ['typed', 'matrix', 'subtract', 'number']; - var createDiff = /* #__PURE__ */Object(factory["a" /* factory */])(diff_name, diff_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - subtract = _ref.subtract, - number = _ref.number; - - /** - * Create a new matrix or array of the difference between elements of the given array - * The optional dim parameter lets you specify the dimension to evaluate the difference of - * If no dimension parameter is passed it is assumed as dimension 0 - * - * Dimension is zero-based in javascript and one-based in the parser and can be a number or bignumber - * Arrays must be 'rectangular' meaning arrays like [1, 2] - * If something is passed as a matrix it will be returned as a matrix but other than that all matrices are converted to arrays - * - * Syntax: - * - * math.diff(arr) - * math.diff(arr, dim) - * - * Examples: - * - * const arr = [1, 2, 4, 7, 0] - * math.diff(arr) // returns [1, 2, 3, -7] (no dimension passed so 0 is assumed) - * math.diff(math.matrix(arr)) // returns math.matrix([1, 2, 3, -7]) - * - * const arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [9, 8, 7, 6, 4]] - * math.diff(arr) // returns [[0, 0, 0, 0, 0], [8, 6, 4, 2, -1]] - * math.diff(arr, 0) // returns [[0, 0, 0, 0, 0], [8, 6, 4, 2, -1]] - * math.diff(arr, 1) // returns [[1, 1, 1, 1], [1, 1, 1, 1], [-1, -1, -1, -2]] - * math.diff(arr, math.bignumber(1)) // returns [[1, 1, 1, 1], [1, 1, 1, 1], [-1, -1, -1, -2]] - * - * math.diff(arr, 2) // throws RangeError as arr is 2 dimensional not 3 - * math.diff(arr, -1) // throws RangeError as negative dimensions are not allowed - * - * // These will all produce the same result - * math.diff([[1, 2], [3, 4]]) - * math.diff([math.matrix([1, 2]), math.matrix([3, 4])]) - * math.diff([[1, 2], math.matrix([3, 4])]) - * math.diff([math.matrix([1, 2]), [3, 4]]) - * // They do not produce the same result as math.diff(math.matrix([[1, 2], [3, 4]])) as this returns a matrix - * - * See Also: - * - * sum - * subtract - * partitionSelect - * - * @param {Array | Matrix} arr An array or matrix - * @param {number} dim Dimension - * @return {Array | Matrix} Difference between array elements in given dimension - */ - return typed(diff_name, { - 'Array | Matrix': function ArrayMatrix(arr) { - // No dimension specified => assume dimension 0 - if (Object(is["v" /* isMatrix */])(arr)) { - return matrix(_diff(arr.toArray())); - } else { - return _diff(arr); - } - }, - 'Array | Matrix, number': function ArrayMatrixNumber(arr, dim) { - if (!Object(utils_number["i" /* isInteger */])(dim)) { - throw new RangeError('Dimension must be a whole number'); - } - - if (Object(is["v" /* isMatrix */])(arr)) { - return matrix(_recursive(arr.toArray(), dim)); - } else { - return _recursive(arr, dim); - } - }, - 'Array | Matrix, BigNumber': function ArrayMatrixBigNumber(arr, dim) { - return this(arr, number(dim)); - } - }); - /** - * Recursively find the correct dimension in the array/matrix - * Then Apply _diff to that dimension - * - * @param {Array} arr The array - * @param {number} dim Dimension - * @return {Array} resulting array - */ - - function _recursive(arr, dim) { - if (Object(is["v" /* isMatrix */])(arr)) { - arr = arr.toArray(); // Makes sure arrays like [ matrix([0, 1]), matrix([1, 0]) ] are processed properly - } - - if (!Array.isArray(arr)) { - throw RangeError('Array/Matrix does not have that many dimensions'); - } - - if (dim > 0) { - var result = []; - arr.forEach(function (element) { - result.push(_recursive(element, dim - 1)); - }); - return result; - } else if (dim === 0) { - return _diff(arr); - } else { - throw RangeError('Cannot have negative dimension'); - } - } - /** - * Difference between elements in the array - * - * @param {Array} arr An array - * @return {Array} resulting array - */ - - function _diff(arr) { - var result = []; - var size = arr.length; - - if (size < 2) { - return arr; - } - - for (var i = 1; i < size; i++) { - result.push(_ElementDiff(arr[i - 1], arr[i])); - } - - return result; - } - /** - * Difference between 2 objects - * - * @param {Object} obj1 First object - * @param {Object} obj2 Second object - * @return {Array} resulting array - */ - - function _ElementDiff(obj1, obj2) { - // Convert matrices to arrays - if (Object(is["v" /* isMatrix */])(obj1)) { - obj1 = obj1.toArray(); - } - if (Object(is["v" /* isMatrix */])(obj2)) { - obj2 = obj2.toArray(); - } - var obj1IsArray = Array.isArray(obj1); - var obj2IsArray = Array.isArray(obj2); - - if (obj1IsArray && obj2IsArray) { - return _ArrayDiff(obj1, obj2); - } - - if (!obj1IsArray && !obj2IsArray) { - return subtract(obj2, obj1); // Difference is (second - first) NOT (first - second) - } - - throw TypeError('Cannot calculate difference between 1 array and 1 non-array'); - } - /** - * Difference of elements in 2 arrays - * - * @param {Array} arr1 Array 1 - * @param {Array} arr2 Array 2 - * @return {Array} resulting array - */ - - function _ArrayDiff(arr1, arr2) { - if (arr1.length !== arr2.length) { - throw RangeError('Not all sub-arrays have the same length'); - } - - var result = []; - var size = arr1.length; - - for (var i = 0; i < size; i++) { - result.push(_ElementDiff(arr1[i], arr2[i])); - } - - return result; - } - }); - // CONCATENATED MODULE: ./src/function/matrix/ones.js - - var ones_name = 'ones'; - var ones_dependencies = ['typed', 'config', 'matrix', 'BigNumber']; - var createOnes = /* #__PURE__ */Object(factory["a" /* factory */])(ones_name, ones_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix, - BigNumber = _ref.BigNumber; - - /** - * Create a matrix filled with ones. The created matrix can have one or - * multiple dimensions. - * - * Syntax: - * - * math.ones(m) - * math.ones(m, format) - * math.ones(m, n) - * math.ones(m, n, format) - * math.ones([m, n]) - * math.ones([m, n], format) - * math.ones([m, n, p, ...]) - * math.ones([m, n, p, ...], format) - * - * Examples: - * - * math.ones(3) // returns [1, 1, 1] - * math.ones(3, 2) // returns [[1, 1], [1, 1], [1, 1]] - * math.ones(3, 2, 'dense') // returns Dense Matrix [[1, 1], [1, 1], [1, 1]] - * - * const A = [[1, 2, 3], [4, 5, 6]] - * math.ones(math.size(A)) // returns [[1, 1, 1], [1, 1, 1]] - * - * See also: - * - * zeros, identity, size, range - * - * @param {...number | Array} size The size of each dimension of the matrix - * @param {string} [format] The Matrix storage format - * - * @return {Array | Matrix | number} A matrix filled with ones - */ - return typed('ones', { - '': function _() { - return config.matrix === 'Array' ? _ones([]) : _ones([], 'default'); - }, - // math.ones(m, n, p, ..., format) - // TODO: more accurate signature '...number | BigNumber, string' as soon as typed-function supports this - '...number | BigNumber | string': function numberBigNumberString(size) { - var last = size[size.length - 1]; - - if (typeof last === 'string') { - var format = size.pop(); - return _ones(size, format); - } else if (config.matrix === 'Array') { - return _ones(size); - } else { - return _ones(size, 'default'); - } - }, - Array: _ones, - Matrix: function Matrix(size) { - var format = size.storage(); - return _ones(size.valueOf(), format); - }, - 'Array | Matrix, string': function ArrayMatrixString(size, format) { - return _ones(size.valueOf(), format); - } - }); - /** - * Create an Array or Matrix with ones - * @param {Array} size - * @param {string} [format='default'] - * @return {Array | Matrix} - * @private - */ - - function _ones(size, format) { - var hasBigNumbers = _normalize(size); - - var defaultValue = hasBigNumbers ? new BigNumber(1) : 1; - - _validate(size); - - if (format) { - // return a matrix - var m = matrix(format); - - if (size.length > 0) { - return m.resize(size, defaultValue); - } - - return m; - } else { - // return an Array - var arr = []; - - if (size.length > 0) { - return Object(utils_array["m" /* resize */])(arr, size, defaultValue); - } - - return arr; - } - } // replace BigNumbers with numbers, returns true if size contained BigNumbers - - function _normalize(size) { - var hasBigNumbers = false; - size.forEach(function (value, index, arr) { - if (Object(is["e" /* isBigNumber */])(value)) { - hasBigNumbers = true; - arr[index] = value.toNumber(); - } - }); - return hasBigNumbers; - } // validate arguments - - function _validate(size) { - size.forEach(function (value) { - if (typeof value !== 'number' || !Object(utils_number["i" /* isInteger */])(value) || value < 0) { - throw new Error('Parameters in function ones must be positive integers'); - } - }); - } - }); - // CONCATENATED MODULE: ./src/utils/noop.js - function noBignumber() { - throw new Error('No "bignumber" implementation available'); - } - function noFraction() { - throw new Error('No "fraction" implementation available'); - } - function noMatrix() { - throw new Error('No "matrix" implementation available'); - } - function noIndex() { - throw new Error('No "index" implementation available'); - } - function noSubset() { - throw new Error('No "matrix" implementation available'); - } - // CONCATENATED MODULE: ./src/function/matrix/range.js - - var range_name = 'range'; - var range_dependencies = ['typed', 'config', '?matrix', '?bignumber', 'smaller', 'smallerEq', 'larger', 'largerEq']; - var range_createRange = /* #__PURE__ */Object(factory["a" /* factory */])(range_name, range_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix, - bignumber = _ref.bignumber, - smaller = _ref.smaller, - smallerEq = _ref.smallerEq, - larger = _ref.larger, - largerEq = _ref.largerEq; - - /** - * Create an array from a range. - * By default, the range end is excluded. This can be customized by providing - * an extra parameter `includeEnd`. - * - * Syntax: - * - * math.range(str [, includeEnd]) // Create a range from a string, - * // where the string contains the - * // start, optional step, and end, - * // separated by a colon. - * math.range(start, end [, includeEnd]) // Create a range with start and - * // end and a step size of 1. - * math.range(start, end, step [, includeEnd]) // Create a range with start, step, - * // and end. - * - * Where: - * - * - `str: string` - * A string 'start:end' or 'start:step:end' - * - `start: {number | BigNumber}` - * Start of the range - * - `end: number | BigNumber` - * End of the range, excluded by default, included when parameter includeEnd=true - * - `step: number | BigNumber` - * Step size. Default value is 1. - * - `includeEnd: boolean` - * Option to specify whether to include the end or not. False by default. - * - * Examples: - * - * math.range(2, 6) // [2, 3, 4, 5] - * math.range(2, -3, -1) // [2, 1, 0, -1, -2] - * math.range('2:1:6') // [2, 3, 4, 5] - * math.range(2, 6, true) // [2, 3, 4, 5, 6] - * - * See also: - * - * ones, zeros, size, subset - * - * @param {*} args Parameters describing the ranges `start`, `end`, and optional `step`. - * @return {Array | Matrix} range - */ - return typed(range_name, { - // TODO: simplify signatures when typed-function supports default values and optional arguments - // TODO: a number or boolean should not be converted to string here - string: _strRange, - 'string, boolean': _strRange, - 'number, number': function numberNumber(start, end) { - return _out(_rangeEx(start, end, 1)); - }, - 'number, number, number': function numberNumberNumber(start, end, step) { - return _out(_rangeEx(start, end, step)); - }, - 'number, number, boolean': function numberNumberBoolean(start, end, includeEnd) { - return includeEnd ? _out(_rangeInc(start, end, 1)) : _out(_rangeEx(start, end, 1)); - }, - 'number, number, number, boolean': function numberNumberNumberBoolean(start, end, step, includeEnd) { - return includeEnd ? _out(_rangeInc(start, end, step)) : _out(_rangeEx(start, end, step)); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(start, end) { - var BigNumber = start.constructor; - return _out(_bigRangeEx(start, end, new BigNumber(1))); - }, - 'BigNumber, BigNumber, BigNumber': function BigNumberBigNumberBigNumber(start, end, step) { - return _out(_bigRangeEx(start, end, step)); - }, - 'BigNumber, BigNumber, boolean': function BigNumberBigNumberBoolean(start, end, includeEnd) { - var BigNumber = start.constructor; - return includeEnd ? _out(_bigRangeInc(start, end, new BigNumber(1))) : _out(_bigRangeEx(start, end, new BigNumber(1))); - }, - 'BigNumber, BigNumber, BigNumber, boolean': function BigNumberBigNumberBigNumberBoolean(start, end, step, includeEnd) { - return includeEnd ? _out(_bigRangeInc(start, end, step)) : _out(_bigRangeEx(start, end, step)); - } - }); - - function _out(arr) { - if (config.matrix === 'Matrix') { - return matrix ? matrix(arr) : noMatrix(); - } - - return arr; - } - - function _strRange(str, includeEnd) { - var r = _parse(str); - - if (!r) { - throw new SyntaxError('String "' + str + '" is no valid range'); - } - - var fn; - - if (config.number === 'BigNumber') { - if (bignumber === undefined) { - noBignumber(); - } - - fn = includeEnd ? _bigRangeInc : _bigRangeEx; - return _out(fn(bignumber(r.start), bignumber(r.end), bignumber(r.step))); - } else { - fn = includeEnd ? _rangeInc : _rangeEx; - return _out(fn(r.start, r.end, r.step)); - } - } - /** - * Create a range with numbers. End is excluded - * @param {number} start - * @param {number} end - * @param {number} step - * @returns {Array} range - * @private - */ - - function _rangeEx(start, end, step) { - var array = []; - var x = start; - - if (step > 0) { - while (smaller(x, end)) { - array.push(x); - x += step; - } - } else if (step < 0) { - while (larger(x, end)) { - array.push(x); - x += step; - } - } - - return array; - } - /** - * Create a range with numbers. End is included - * @param {number} start - * @param {number} end - * @param {number} step - * @returns {Array} range - * @private - */ - - function _rangeInc(start, end, step) { - var array = []; - var x = start; - - if (step > 0) { - while (smallerEq(x, end)) { - array.push(x); - x += step; - } - } else if (step < 0) { - while (largerEq(x, end)) { - array.push(x); - x += step; - } - } - - return array; - } - /** - * Create a range with big numbers. End is excluded - * @param {BigNumber} start - * @param {BigNumber} end - * @param {BigNumber} step - * @returns {Array} range - * @private - */ - - function _bigRangeEx(start, end, step) { - var zero = bignumber(0); - var array = []; - var x = start; - - if (step.gt(zero)) { - while (smaller(x, end)) { - array.push(x); - x = x.plus(step); - } - } else if (step.lt(zero)) { - while (larger(x, end)) { - array.push(x); - x = x.plus(step); - } - } - - return array; - } - /** - * Create a range with big numbers. End is included - * @param {BigNumber} start - * @param {BigNumber} end - * @param {BigNumber} step - * @returns {Array} range - * @private - */ - - function _bigRangeInc(start, end, step) { - var zero = bignumber(0); - var array = []; - var x = start; - - if (step.gt(zero)) { - while (smallerEq(x, end)) { - array.push(x); - x = x.plus(step); - } - } else if (step.lt(zero)) { - while (largerEq(x, end)) { - array.push(x); - x = x.plus(step); - } - } - - return array; - } - /** - * Parse a string into a range, - * The string contains the start, optional step, and end, separated by a colon. - * If the string does not contain a valid range, null is returned. - * For example str='0:2:11'. - * @param {string} str - * @return {{start: number, end: number, step: number} | null} range Object containing properties start, end, step - * @private - */ - - function _parse(str) { - var args = str.split(':'); // number - - var nums = args.map(function (arg) { - // use Number and not parseFloat as Number returns NaN on invalid garbage in the string - return Number(arg); - }); - var invalid = nums.some(function (num) { - return isNaN(num); - }); - - if (invalid) { - return null; - } - - switch (nums.length) { - case 2: - return { - start: nums[0], - end: nums[1], - step: 1 - }; - - case 3: - return { - start: nums[0], - end: nums[2], - step: nums[1] - }; - - default: - return null; - } - } - }); - // CONCATENATED MODULE: ./src/function/matrix/reshape.js - - var reshape_name = 'reshape'; - var reshape_dependencies = ['typed', 'isInteger', 'matrix']; - var createReshape = /* #__PURE__ */Object(factory["a" /* factory */])(reshape_name, reshape_dependencies, function (_ref) { - var typed = _ref.typed, - isInteger = _ref.isInteger, - matrix = _ref.matrix; - - /** - * Reshape a multi dimensional array to fit the specified dimensions - * - * Syntax: - * - * math.reshape(x, sizes) - * - * Examples: - * - * math.reshape([1, 2, 3, 4, 5, 6], [2, 3]) - * // returns Array [[1, 2, 3], [4, 5, 6]] - * - * math.reshape([[1, 2], [3, 4]], [1, 4]) - * // returns Array [[1, 2, 3, 4]] - * - * math.reshape([[1, 2], [3, 4]], [4]) - * // returns Array [1, 2, 3, 4] - * - * const x = math.matrix([1, 2, 3, 4, 5, 6, 7, 8]) - * math.reshape(x, [2, 2, 2]) - * // returns Matrix [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] - * - * See also: - * - * size, squeeze, resize - * - * @param {Array | Matrix | *} x Matrix to be reshaped - * @param {number[]} sizes One dimensional array with integral sizes for - * each dimension - * - * @return {* | Array | Matrix} A reshaped clone of matrix `x` - * - * @throws {TypeError} If `sizes` does not contain solely integers - * @throws {DimensionError} If the product of the new dimension sizes does - * not equal that of the old ones - */ - return typed(reshape_name, { - 'Matrix, Array': function MatrixArray(x, sizes) { - if (x.reshape) { - return x.reshape(sizes); - } else { - return matrix(Object(utils_array["l" /* reshape */])(x.valueOf(), sizes)); - } - }, - 'Array, Array': function ArrayArray(x, sizes) { - sizes.forEach(function (size) { - if (!isInteger(size)) { - throw new TypeError('Invalid size for dimension: ' + size); - } - }); - return Object(utils_array["l" /* reshape */])(x, sizes); - } - }); - }); - // EXTERNAL MODULE: ./src/error/ArgumentsError.js - var ArgumentsError = __webpack_require__(13); - - // CONCATENATED MODULE: ./src/function/matrix/resize.js - - var resize_name = 'resize'; - var resize_dependencies = ['config', 'matrix']; - var createResize = /* #__PURE__ */Object(factory["a" /* factory */])(resize_name, resize_dependencies, function (_ref) { - var config = _ref.config, - matrix = _ref.matrix; - - /** - * Resize a matrix - * - * Syntax: - * - * math.resize(x, size) - * math.resize(x, size, defaultValue) - * - * Examples: - * - * math.resize([1, 2, 3, 4, 5], [3]) // returns Array [1, 2, 3] - * math.resize([1, 2, 3], [5], 0) // returns Array [1, 2, 3, 0, 0] - * math.resize(2, [2, 3], 0) // returns Matrix [[2, 0, 0], [0, 0, 0]] - * math.resize("hello", [8], "!") // returns string 'hello!!!' - * - * See also: - * - * size, squeeze, subset, reshape - * - * @param {Array | Matrix | *} x Matrix to be resized - * @param {Array | Matrix} size One dimensional array with numbers - * @param {number | string} [defaultValue=0] Zero by default, except in - * case of a string, in that case - * defaultValue = ' ' - * @return {* | Array | Matrix} A resized clone of matrix `x` - */ - // TODO: rework resize to a typed-function - return function resize(x, size, defaultValue) { - if (arguments.length !== 2 && arguments.length !== 3) { - throw new ArgumentsError["a" /* ArgumentsError */]('resize', arguments.length, 2, 3); - } - - if (Object(is["v" /* isMatrix */])(size)) { - size = size.valueOf(); // get Array - } - - if (Object(is["e" /* isBigNumber */])(size[0])) { - // convert bignumbers to numbers - size = size.map(function (value) { - return !Object(is["e" /* isBigNumber */])(value) ? value : value.toNumber(); - }); - } // check x is a Matrix - - if (Object(is["v" /* isMatrix */])(x)) { - // use optimized matrix implementation, return copy - return x.resize(size, defaultValue, true); - } - - if (typeof x === 'string') { - // resize string - return _resizeString(x, size, defaultValue); - } // check result should be a matrix - - var asMatrix = Array.isArray(x) ? false : config.matrix !== 'Array'; - - if (size.length === 0) { - // output a scalar - while (Array.isArray(x)) { - x = x[0]; - } - - return Object(utils_object["a" /* clone */])(x); - } else { - // output an array/matrix - if (!Array.isArray(x)) { - x = [x]; - } - - x = Object(utils_object["a" /* clone */])(x); - var res = Object(utils_array["m" /* resize */])(x, size, defaultValue); - return asMatrix ? matrix(res) : res; - } - }; - /** - * Resize a string - * @param {string} str - * @param {number[]} size - * @param {string} [defaultChar=' '] - * @private - */ - - function _resizeString(str, size, defaultChar) { - if (defaultChar !== undefined) { - if (typeof defaultChar !== 'string' || defaultChar.length !== 1) { - throw new TypeError('Single character expected as defaultValue'); - } - } else { - defaultChar = ' '; - } - - if (size.length !== 1) { - throw new DimensionError["a" /* DimensionError */](size.length, 1); - } - - var len = size[0]; - - if (typeof len !== 'number' || !Object(utils_number["i" /* isInteger */])(len)) { - throw new TypeError('Invalid size, must contain positive integers ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - - if (str.length > len) { - return str.substring(0, len); - } else if (str.length < len) { - var res = str; - - for (var i = 0, ii = len - str.length; i < ii; i++) { - res += defaultChar; - } - - return res; - } else { - return str; - } - } - }); - // CONCATENATED MODULE: ./src/function/matrix/rotationMatrix.js - - var rotationMatrix_name = 'rotationMatrix'; - var rotationMatrix_dependencies = ['typed', 'config', 'multiplyScalar', 'addScalar', 'unaryMinus', 'norm', 'matrix', 'BigNumber', 'DenseMatrix', 'SparseMatrix', 'cos', 'sin']; - var createRotationMatrix = /* #__PURE__ */Object(factory["a" /* factory */])(rotationMatrix_name, rotationMatrix_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - multiplyScalar = _ref.multiplyScalar, - addScalar = _ref.addScalar, - unaryMinus = _ref.unaryMinus, - norm = _ref.norm, - BigNumber = _ref.BigNumber, - matrix = _ref.matrix, - DenseMatrix = _ref.DenseMatrix, - SparseMatrix = _ref.SparseMatrix, - cos = _ref.cos, - sin = _ref.sin; - - /** - * Create a 2-dimensional counter-clockwise rotation matrix (2x2) for a given angle (expressed in radians). - * Create a 2-dimensional counter-clockwise rotation matrix (3x3) by a given angle (expressed in radians) around a given axis (1x3). - * - * Syntax: - * - * math.rotationMatrix(theta) - * math.rotationMatrix(theta, format) - * math.rotationMatrix(theta, [v]) - * math.rotationMatrix(theta, [v], format) - * - * Examples: - * - * math.rotationMatrix(math.pi / 2) // returns [[0, -1], [1, 0]] - * math.rotationMatrix(math.bignumber(45)) // returns [[ bignumber(1 / sqrt(2)), - bignumber(1 / sqrt(2))], [ bignumber(1 / sqrt(2)), bignumber(1 / sqrt(2))]] - * math.rotationMatrix(math.complex(1 + i)) // returns [[cos(1 + i), -sin(1 + i)], [sin(1 + i), cos(1 + i)]] - * math.rotationMatrix(math.unit('1rad')) // returns [[cos(1), -sin(1)], [sin(1), cos(1)]] - * - * math.rotationMatrix(math.pi / 2, [0, 1, 0]) // returns [[0, 0, 1], [0, 1, 0], [-1, 0, 0]] - * math.rotationMatrix(math.pi / 2, matrix([0, 1, 0])) // returns matrix([[0, 0, 1], [0, 1, 0], [-1, 0, 0]]) - * - * - * See also: - * - * matrix, cos, sin - * - * - * @param {number | BigNumber | Complex | Unit} theta Rotation angle - * @param {Array | Matrix} [v] Rotation axis - * @param {string} [format] Result Matrix storage format - * @return {Array | Matrix} Rotation matrix - */ - return typed(rotationMatrix_name, { - '': function _() { - return config.matrix === 'Matrix' ? matrix([]) : []; - }, - string: function string(format) { - return matrix(format); - }, - 'number | BigNumber | Complex | Unit': function numberBigNumberComplexUnit(theta) { - return _rotationMatrix2x2(theta, config.matrix === 'Matrix' ? 'dense' : undefined); - }, - 'number | BigNumber | Complex | Unit, string': function numberBigNumberComplexUnitString(theta, format) { - return _rotationMatrix2x2(theta, format); - }, - 'number | BigNumber | Complex | Unit, Array': function numberBigNumberComplexUnitArray(theta, v) { - var matrixV = matrix(v); - - _validateVector(matrixV); - - return _rotationMatrix3x3(theta, matrixV, config.matrix === 'Matrix' ? 'dense' : undefined); - }, - 'number | BigNumber | Complex | Unit, Matrix': function numberBigNumberComplexUnitMatrix(theta, v) { - _validateVector(v); - - return _rotationMatrix3x3(theta, v, config.matrix === 'Matrix' ? 'dense' : undefined); - }, - 'number | BigNumber | Complex | Unit, Array, string': function numberBigNumberComplexUnitArrayString(theta, v, format) { - var matrixV = matrix(v); - - _validateVector(matrixV); - - return _rotationMatrix3x3(theta, matrixV, format); - }, - 'number | BigNumber | Complex | Unit, Matrix, string': function numberBigNumberComplexUnitMatrixString(theta, v, format) { - _validateVector(v); - - return _rotationMatrix3x3(theta, v, format); - } - }); - /** - * Returns 2x2 matrix of 2D rotation of angle theta - * - * @param {number | BigNumber | Complex | Unit} theta The rotation angle - * @param {string} format The result Matrix storage format - * @returns {Matrix} - * @private - */ - - function _rotationMatrix2x2(theta, format) { - var Big = Object(is["e" /* isBigNumber */])(theta); - var minusOne = Big ? new BigNumber(-1) : -1; - var cosTheta = cos(theta); - var sinTheta = sin(theta); - var data = [[cosTheta, multiplyScalar(minusOne, sinTheta)], [sinTheta, cosTheta]]; - return _convertToFormat(data, format); - } - - function _validateVector(v) { - var size = v.size(); - - if (size.length < 1 || size[0] !== 3) { - throw new RangeError('Vector must be of dimensions 1x3'); - } - } - - function _mul(array) { - return array.reduce(function (p, curr) { - return multiplyScalar(p, curr); - }); - } - - function _convertToFormat(data, format) { - if (format) { - if (format === 'sparse') { - return new SparseMatrix(data); - } - - if (format === 'dense') { - return new DenseMatrix(data); - } - - throw new TypeError("Unknown matrix type \"".concat(format, "\"")); - } - - return data; - } - /** - * Returns a 3x3 matrix of rotation of angle theta around vector v - * - * @param {number | BigNumber | Complex | Unit} theta The rotation angle - * @param {Matrix} v The rotation axis vector - * @param {string} format The storage format of the resulting matrix - * @returns {Matrix} - * @private - */ - - function _rotationMatrix3x3(theta, v, format) { - var normV = norm(v); - - if (normV === 0) { - return _convertToFormat([], format); - } - - var Big = Object(is["e" /* isBigNumber */])(theta) ? BigNumber : null; - var one = Big ? new Big(1) : 1; - var minusOne = Big ? new Big(-1) : -1; - var vx = Big ? new Big(v.get([0]) / normV) : v.get([0]) / normV; - var vy = Big ? new Big(v.get([1]) / normV) : v.get([1]) / normV; - var vz = Big ? new Big(v.get([2]) / normV) : v.get([2]) / normV; - var c = cos(theta); - var oneMinusC = addScalar(one, unaryMinus(c)); - var s = sin(theta); - var r11 = addScalar(c, _mul([vx, vx, oneMinusC])); - var r12 = addScalar(_mul([vx, vy, oneMinusC]), _mul([minusOne, vz, s])); - var r13 = addScalar(_mul([vx, vz, oneMinusC]), _mul([vy, s])); - var r21 = addScalar(_mul([vx, vy, oneMinusC]), _mul([vz, s])); - var r22 = addScalar(c, _mul([vy, vy, oneMinusC])); - var r23 = addScalar(_mul([vy, vz, oneMinusC]), _mul([minusOne, vx, s])); - var r31 = addScalar(_mul([vx, vz, oneMinusC]), _mul([minusOne, vy, s])); - var r32 = addScalar(_mul([vy, vz, oneMinusC]), _mul([vx, s])); - var r33 = addScalar(c, _mul([vz, vz, oneMinusC])); - var data = [[r11, r12, r13], [r21, r22, r23], [r31, r32, r33]]; - return _convertToFormat(data, format); - } - }); - // CONCATENATED MODULE: ./src/function/matrix/row.js - - var row_name = 'row'; - var row_dependencies = ['typed', 'Index', 'matrix', 'range']; - var createRow = /* #__PURE__ */Object(factory["a" /* factory */])(row_name, row_dependencies, function (_ref) { - var typed = _ref.typed, - Index = _ref.Index, - matrix = _ref.matrix, - range = _ref.range; - - /** - * Return a row from a Matrix. - * - * Syntax: - * - * math.row(value, index) - * - * Example: - * - * // get a row - * const d = [[1, 2], [3, 4]] - * math.row(d, 1) // returns [[3, 4]] - * - * See also: - * - * column - * - * @param {Array | Matrix } value An array or matrix - * @param {number} row The index of the row - * @return {Array | Matrix} The retrieved row - */ - return typed(row_name, { - 'Matrix, number': _row, - 'Array, number': function ArrayNumber(value, row) { - return _row(matrix(Object(utils_object["a" /* clone */])(value)), row).valueOf(); - } - }); - /** - * Retrieve a row of a matrix - * @param {Matrix } value A matrix - * @param {number} row The index of the row - * @return {Matrix} The retrieved row - */ - - function _row(value, row) { - // check dimensions - if (value.size().length !== 2) { - throw new Error('Only two dimensional matrix is supported'); - } - - Object(utils_array["q" /* validateIndex */])(row, value.size()[0]); - var columnRange = range(0, value.size()[1]); - var index = new Index(row, columnRange); - return value.subset(index); - } - }); - // CONCATENATED MODULE: ./src/function/matrix/size.js - - var size_name = 'size'; - var size_dependencies = ['typed', 'config', '?matrix']; - var createSize = /* #__PURE__ */Object(factory["a" /* factory */])(size_name, size_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix; - - /** - * Calculate the size of a matrix or scalar. - * - * Syntax: - * - * math.size(x) - * - * Examples: - * - * math.size(2.3) // returns [] - * math.size('hello world') // returns [11] - * - * const A = [[1, 2, 3], [4, 5, 6]] - * math.size(A) // returns [2, 3] - * math.size(math.range(1,6)) // returns [5] - * - * See also: - * - * resize, squeeze, subset - * - * @param {boolean | number | Complex | Unit | string | Array | Matrix} x A matrix - * @return {Array | Matrix} A vector with size of `x`. - */ - return typed(size_name, { - Matrix: function Matrix(x) { - return x.create(x.size()); - }, - Array: utils_array["a" /* arraySize */], - string: function string(x) { - return config.matrix === 'Array' ? [x.length] : matrix([x.length]); - }, - 'number | Complex | BigNumber | Unit | boolean | null': function numberComplexBigNumberUnitBooleanNull(x) { - // scalar - return config.matrix === 'Array' ? [] : matrix ? matrix([]) : noMatrix(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/matrix/squeeze.js - - var squeeze_name = 'squeeze'; - var squeeze_dependencies = ['typed', 'matrix']; - var createSqueeze = /* #__PURE__ */Object(factory["a" /* factory */])(squeeze_name, squeeze_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix; - - /** - * Squeeze a matrix, remove inner and outer singleton dimensions from a matrix. - * - * Syntax: - * - * math.squeeze(x) - * - * Examples: - * - * math.squeeze([3]) // returns 3 - * math.squeeze([[3]]) // returns 3 - * - * const A = math.zeros(3, 1) // returns [[0], [0], [0]] (size 3x1) - * math.squeeze(A) // returns [0, 0, 0] (size 3) - * - * const B = math.zeros(1, 3) // returns [[0, 0, 0]] (size 1x3) - * math.squeeze(B) // returns [0, 0, 0] (size 3) - * - * // only inner and outer dimensions are removed - * const C = math.zeros(2, 1, 3) // returns [[[0, 0, 0]], [[0, 0, 0]]] (size 2x1x3) - * math.squeeze(C) // returns [[[0, 0, 0]], [[0, 0, 0]]] (size 2x1x3) - * - * See also: - * - * subset - * - * @param {Matrix | Array} x Matrix to be squeezed - * @return {Matrix | Array} Squeezed matrix - */ - return typed(squeeze_name, { - Array: function Array(x) { - return Object(utils_array["n" /* squeeze */])(Object(utils_object["a" /* clone */])(x)); - }, - Matrix: function Matrix(x) { - var res = Object(utils_array["n" /* squeeze */])(x.toArray()); // FIXME: return the same type of matrix as the input - - return Array.isArray(res) ? matrix(res) : res; - }, - any: function any(x) { - // scalar - return Object(utils_object["a" /* clone */])(x); - } - }); - }); - // CONCATENATED MODULE: ./src/utils/customs.js - function customs_typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - customs_typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - customs_typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return customs_typeof(obj); - } - - /** - * Get a property of a plain object - * Throws an error in case the object is not a plain object or the - * property is not defined on the object itself - * @param {Object} object - * @param {string} prop - * @return {*} Returns the property value when safe - */ - - function getSafeProperty(object, prop) { - // only allow getting safe properties of a plain object - if (isPlainObject(object) && isSafeProperty(object, prop)) { - return object[prop]; - } - - if (typeof object[prop] === 'function' && isSafeMethod(object, prop)) { - throw new Error('Cannot access method "' + prop + '" as a property'); - } - - throw new Error('No access to property "' + prop + '"'); - } - /** - * Set a property on a plain object. - * Throws an error in case the object is not a plain object or the - * property would override an inherited property like .constructor or .toString - * @param {Object} object - * @param {string} prop - * @param {*} value - * @return {*} Returns the value - */ - // TODO: merge this function into access.js? - - function setSafeProperty(object, prop, value) { - // only allow setting safe properties of a plain object - if (isPlainObject(object) && isSafeProperty(object, prop)) { - object[prop] = value; - return value; - } - - throw new Error('No access to property "' + prop + '"'); - } - /** - * Test whether a property is safe to use for an object. - * For example .toString and .constructor are not safe - * @param {string} prop - * @return {boolean} Returns true when safe - */ - - function isSafeProperty(object, prop) { - if (!object || customs_typeof(object) !== 'object') { - return false; - } // SAFE: whitelisted - // e.g length - - if (Object(utils_object["f" /* hasOwnProperty */])(safeNativeProperties, prop)) { - return true; - } // UNSAFE: inherited from Object prototype - // e.g constructor - - if (prop in Object.prototype) { - // 'in' is used instead of hasOwnProperty for nodejs v0.10 - // which is inconsistent on root prototypes. It is safe - // here because Object.prototype is a root object - return false; - } // UNSAFE: inherited from Function prototype - // e.g call, apply - - if (prop in Function.prototype) { - // 'in' is used instead of hasOwnProperty for nodejs v0.10 - // which is inconsistent on root prototypes. It is safe - // here because Function.prototype is a root object - return false; - } - - return true; - } - /** - * Validate whether a method is safe. - * Throws an error when that's not the case. - * @param {Object} object - * @param {string} method - */ - // TODO: merge this function into assign.js? - - function validateSafeMethod(object, method) { - if (!isSafeMethod(object, method)) { - throw new Error('No access to method "' + method + '"'); - } - } - /** - * Check whether a method is safe. - * Throws an error when that's not the case (for example for `constructor`). - * @param {Object} object - * @param {string} method - * @return {boolean} Returns true when safe, false otherwise - */ - - function isSafeMethod(object, method) { - if (object === null || object === undefined || typeof object[method] !== 'function') { - return false; - } // UNSAFE: ghosted - // e.g overridden toString - // Note that IE10 doesn't support __proto__ and we can't do this check there. - - if (Object(utils_object["f" /* hasOwnProperty */])(object, method) && Object.getPrototypeOf && method in Object.getPrototypeOf(object)) { - return false; - } // SAFE: whitelisted - // e.g toString - - if (Object(utils_object["f" /* hasOwnProperty */])(safeNativeMethods, method)) { - return true; - } // UNSAFE: inherited from Object prototype - // e.g constructor - - if (method in Object.prototype) { - // 'in' is used instead of hasOwnProperty for nodejs v0.10 - // which is inconsistent on root prototypes. It is safe - // here because Object.prototype is a root object - return false; - } // UNSAFE: inherited from Function prototype - // e.g call, apply - - if (method in Function.prototype) { - // 'in' is used instead of hasOwnProperty for nodejs v0.10 - // which is inconsistent on root prototypes. It is safe - // here because Function.prototype is a root object - return false; - } - - return true; - } - - function isPlainObject(object) { - return customs_typeof(object) === 'object' && object && object.constructor === Object; - } - - var safeNativeProperties = { - length: true, - name: true - }; - var safeNativeMethods = { - toString: true, - valueOf: true, - toLocaleString: true - }; - - // CONCATENATED MODULE: ./src/function/matrix/subset.js - - var subset_name = 'subset'; - var subset_dependencies = ['typed', 'matrix']; - var createSubset = /* #__PURE__ */Object(factory["a" /* factory */])(subset_name, subset_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix; - - /** - * Get or set a subset of a matrix or string. - * - * Syntax: - * math.subset(value, index) // retrieve a subset - * math.subset(value, index, replacement [, defaultValue]) // replace a subset - * - * Examples: - * - * // get a subset - * const d = [[1, 2], [3, 4]] - * math.subset(d, math.index(1, 0)) // returns 3 - * math.subset(d, math.index([0, 1], 1)) // returns [[2], [4]] - * - * // replace a subset - * const e = [] - * const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]] - * const g = math.subset(f, math.index(1, 1), 7, 0) // g = [[5, 6], [0, 7]] - * - * See also: - * - * size, resize, squeeze, index - * - * @param {Array | Matrix | string} matrix An array, matrix, or string - * @param {Index} index An index containing ranges for each - * dimension - * @param {*} [replacement] An array, matrix, or scalar. - * If provided, the subset is replaced with replacement. - * If not provided, the subset is returned - * @param {*} [defaultValue=undefined] Default value, filled in on new entries when - * the matrix is resized. If not provided, - * math.matrix elements will be left undefined. - * @return {Array | Matrix | string} Either the retrieved subset or the updated matrix. - */ - return typed(subset_name, { - // get subset - 'Array, Index': function ArrayIndex(value, index) { - var m = matrix(value); - var subset = m.subset(index); // returns a Matrix - - return index.isScalar() ? subset : subset.valueOf(); // return an Array (like the input) - }, - 'Matrix, Index': function MatrixIndex(value, index) { - return value.subset(index); - }, - 'Object, Index': _getObjectProperty, - 'string, Index': _getSubstring, - // set subset - 'Array, Index, any': function ArrayIndexAny(value, index, replacement) { - return matrix(Object(utils_object["a" /* clone */])(value)).subset(index, replacement, undefined).valueOf(); - }, - 'Array, Index, any, any': function ArrayIndexAnyAny(value, index, replacement, defaultValue) { - return matrix(Object(utils_object["a" /* clone */])(value)).subset(index, replacement, defaultValue).valueOf(); - }, - 'Matrix, Index, any': function MatrixIndexAny(value, index, replacement) { - return value.clone().subset(index, replacement); - }, - 'Matrix, Index, any, any': function MatrixIndexAnyAny(value, index, replacement, defaultValue) { - return value.clone().subset(index, replacement, defaultValue); - }, - 'string, Index, string': _setSubstring, - 'string, Index, string, string': _setSubstring, - 'Object, Index, any': _setObjectProperty - }); - }); - /** - * Retrieve a subset of a string - * @param {string} str string from which to get a substring - * @param {Index} index An index containing ranges for each dimension - * @returns {string} substring - * @private - */ - - function _getSubstring(str, index) { - if (!Object(is["t" /* isIndex */])(index)) { - // TODO: better error message - throw new TypeError('Index expected'); - } - - if (index.size().length !== 1) { - throw new DimensionError["a" /* DimensionError */](index.size().length, 1); - } // validate whether the range is out of range - - var strLen = str.length; - Object(utils_array["q" /* validateIndex */])(index.min()[0], strLen); - Object(utils_array["q" /* validateIndex */])(index.max()[0], strLen); - var range = index.dimension(0); - var substr = ''; - range.forEach(function (v) { - substr += str.charAt(v); - }); - return substr; - } - /** - * Replace a substring in a string - * @param {string} str string to be replaced - * @param {Index} index An index containing ranges for each dimension - * @param {string} replacement Replacement string - * @param {string} [defaultValue] Default value to be uses when resizing - * the string. is ' ' by default - * @returns {string} result - * @private - */ - - function _setSubstring(str, index, replacement, defaultValue) { - if (!index || index.isIndex !== true) { - // TODO: better error message - throw new TypeError('Index expected'); - } - - if (index.size().length !== 1) { - throw new DimensionError["a" /* DimensionError */](index.size().length, 1); - } - - if (defaultValue !== undefined) { - if (typeof defaultValue !== 'string' || defaultValue.length !== 1) { - throw new TypeError('Single character expected as defaultValue'); - } - } else { - defaultValue = ' '; - } - - var range = index.dimension(0); - var len = range.size()[0]; - - if (len !== replacement.length) { - throw new DimensionError["a" /* DimensionError */](range.size()[0], replacement.length); - } // validate whether the range is out of range - - var strLen = str.length; - Object(utils_array["q" /* validateIndex */])(index.min()[0]); - Object(utils_array["q" /* validateIndex */])(index.max()[0]); // copy the string into an array with characters - - var chars = []; - - for (var i = 0; i < strLen; i++) { - chars[i] = str.charAt(i); - } - - range.forEach(function (v, i) { - chars[v] = replacement.charAt(i[0]); - }); // initialize undefined characters with a space - - if (chars.length > strLen) { - for (var _i = strLen - 1, _len = chars.length; _i < _len; _i++) { - if (!chars[_i]) { - chars[_i] = defaultValue; - } - } - } - - return chars.join(''); - } - /** - * Retrieve a property from an object - * @param {Object} object - * @param {Index} index - * @return {*} Returns the value of the property - * @private - */ - - function _getObjectProperty(object, index) { - if (index.size().length !== 1) { - throw new DimensionError["a" /* DimensionError */](index.size(), 1); - } - - var key = index.dimension(0); - - if (typeof key !== 'string') { - throw new TypeError('String expected as index to retrieve an object property'); - } - - return getSafeProperty(object, key); - } - /** - * Set a property on an object - * @param {Object} object - * @param {Index} index - * @param {*} replacement - * @return {*} Returns the updated object - * @private - */ - - function _setObjectProperty(object, index, replacement) { - if (index.size().length !== 1) { - throw new DimensionError["a" /* DimensionError */](index.size(), 1); - } - - var key = index.dimension(0); - - if (typeof key !== 'string') { - throw new TypeError('String expected as index to retrieve an object property'); - } // clone the object, and apply the property to the clone - - var updated = Object(utils_object["a" /* clone */])(object); - setSafeProperty(updated, key, replacement); - return updated; - } - // CONCATENATED MODULE: ./src/function/matrix/transpose.js - - var transpose_name = 'transpose'; - var transpose_dependencies = ['typed', 'matrix']; - var createTranspose = /* #__PURE__ */Object(factory["a" /* factory */])(transpose_name, transpose_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix; - - /** - * Transpose a matrix. All values of the matrix are reflected over its - * main diagonal. Only applicable to two dimensional matrices containing - * a vector (i.e. having size `[1,n]` or `[n,1]`). One dimensional - * vectors and scalars return the input unchanged. - * - * Syntax: - * - * math.transpose(x) - * - * Examples: - * - * const A = [[1, 2, 3], [4, 5, 6]] - * math.transpose(A) // returns [[1, 4], [2, 5], [3, 6]] - * - * See also: - * - * diag, inv, subset, squeeze - * - * @param {Array | Matrix} x Matrix to be transposed - * @return {Array | Matrix} The transposed matrix - */ - return typed('transpose', { - Array: function Array(x) { - // use dense matrix implementation - return this(matrix(x)).valueOf(); - }, - Matrix: function Matrix(x) { - // matrix size - var size = x.size(); // result - - var c; // process dimensions - - switch (size.length) { - case 1: - // vector - c = x.clone(); - break; - - case 2: - { - // rows and columns - var rows = size[0]; - var columns = size[1]; // check columns - - if (columns === 0) { - // throw exception - throw new RangeError('Cannot transpose a 2D matrix with no columns (size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } // process storage format - - switch (x.storage()) { - case 'dense': - c = _denseTranspose(x, rows, columns); - break; - - case 'sparse': - c = _sparseTranspose(x, rows, columns); - break; - } - } - break; - - default: - // multi dimensional - throw new RangeError('Matrix must be a vector or two dimensional (size: ' + Object(utils_string["d" /* format */])(this._size) + ')'); - } - - return c; - }, - // scalars - any: function any(x) { - return Object(utils_object["a" /* clone */])(x); - } - }); - - function _denseTranspose(m, rows, columns) { - // matrix array - var data = m._data; // transposed matrix data - - var transposed = []; - var transposedRow; // loop columns - - for (var j = 0; j < columns; j++) { - // initialize row - transposedRow = transposed[j] = []; // loop rows - - for (var i = 0; i < rows; i++) { - // set data - transposedRow[i] = Object(utils_object["a" /* clone */])(data[i][j]); - } - } // return matrix - - return m.createDenseMatrix({ - data: transposed, - size: [columns, rows], - datatype: m._datatype - }); - } - - function _sparseTranspose(m, rows, columns) { - // matrix arrays - var values = m._values; - var index = m._index; - var ptr = m._ptr; // result matrices - - var cvalues = values ? [] : undefined; - var cindex = []; - var cptr = []; // row counts - - var w = []; - - for (var x = 0; x < rows; x++) { - w[x] = 0; - } // vars - - var p, l, j; // loop values in matrix - - for (p = 0, l = index.length; p < l; p++) { - // number of values in row - w[index[p]]++; - } // cumulative sum - - var sum = 0; // initialize cptr with the cummulative sum of row counts - - for (var i = 0; i < rows; i++) { - // update cptr - cptr.push(sum); // update sum - - sum += w[i]; // update w - - w[i] = cptr[i]; - } // update cptr - - cptr.push(sum); // loop columns - - for (j = 0; j < columns; j++) { - // values & index in column - for (var k0 = ptr[j], k1 = ptr[j + 1], k = k0; k < k1; k++) { - // C values & index - var q = w[index[k]]++; // C[j, i] = A[i, j] - - cindex[q] = j; // check we need to process values (pattern matrix) - - if (values) { - cvalues[q] = Object(utils_object["a" /* clone */])(values[k]); - } - } - } // return matrix - - return m.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [columns, rows], - datatype: m._datatype - }); - } - }); - // CONCATENATED MODULE: ./src/function/matrix/ctranspose.js - - var ctranspose_name = 'ctranspose'; - var ctranspose_dependencies = ['typed', 'transpose', 'conj']; - var createCtranspose = /* #__PURE__ */Object(factory["a" /* factory */])(ctranspose_name, ctranspose_dependencies, function (_ref) { - var typed = _ref.typed, - transpose = _ref.transpose, - conj = _ref.conj; - - /** - * Transpose and complex conjugate a matrix. All values of the matrix are - * reflected over its main diagonal and then the complex conjugate is - * taken. This is equivalent to complex conjugation for scalars and - * vectors. - * - * Syntax: - * - * math.ctranspose(x) - * - * Examples: - * - * const A = [[1, 2, 3], [4, 5, math.complex(6,7)]] - * math.ctranspose(A) // returns [[1, 4], [2, 5], [3, {re:6,im:7}]] - * - * See also: - * - * transpose, diag, inv, subset, squeeze - * - * @param {Array | Matrix} x Matrix to be ctransposed - * @return {Array | Matrix} The ctransposed matrix - */ - return typed(ctranspose_name, { - any: function any(x) { - return conj(transpose(x)); - } - }); - }); - // CONCATENATED MODULE: ./src/function/matrix/zeros.js - - var zeros_name = 'zeros'; - var zeros_dependencies = ['typed', 'config', 'matrix', 'BigNumber']; - var createZeros = /* #__PURE__ */Object(factory["a" /* factory */])(zeros_name, zeros_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix, - BigNumber = _ref.BigNumber; - - /** - * Create a matrix filled with zeros. The created matrix can have one or - * multiple dimensions. - * - * Syntax: - * - * math.zeros(m) - * math.zeros(m, format) - * math.zeros(m, n) - * math.zeros(m, n, format) - * math.zeros([m, n]) - * math.zeros([m, n], format) - * - * Examples: - * - * math.zeros(3) // returns [0, 0, 0] - * math.zeros(3, 2) // returns [[0, 0], [0, 0], [0, 0]] - * math.zeros(3, 'dense') // returns [0, 0, 0] - * - * const A = [[1, 2, 3], [4, 5, 6]] - * math.zeros(math.size(A)) // returns [[0, 0, 0], [0, 0, 0]] - * - * See also: - * - * ones, identity, size, range - * - * @param {...number | Array} size The size of each dimension of the matrix - * @param {string} [format] The Matrix storage format - * - * @return {Array | Matrix} A matrix filled with zeros - */ - return typed(zeros_name, { - '': function _() { - return config.matrix === 'Array' ? _zeros([]) : _zeros([], 'default'); - }, - // math.zeros(m, n, p, ..., format) - // TODO: more accurate signature '...number | BigNumber, string' as soon as typed-function supports this - '...number | BigNumber | string': function numberBigNumberString(size) { - var last = size[size.length - 1]; - - if (typeof last === 'string') { - var format = size.pop(); - return _zeros(size, format); - } else if (config.matrix === 'Array') { - return _zeros(size); - } else { - return _zeros(size, 'default'); - } - }, - Array: _zeros, - Matrix: function Matrix(size) { - var format = size.storage(); - return _zeros(size.valueOf(), format); - }, - 'Array | Matrix, string': function ArrayMatrixString(size, format) { - return _zeros(size.valueOf(), format); - } - }); - /** - * Create an Array or Matrix with zeros - * @param {Array} size - * @param {string} [format='default'] - * @return {Array | Matrix} - * @private - */ - - function _zeros(size, format) { - var hasBigNumbers = _normalize(size); - - var defaultValue = hasBigNumbers ? new BigNumber(0) : 0; - - _validate(size); - - if (format) { - // return a matrix - var m = matrix(format); - - if (size.length > 0) { - return m.resize(size, defaultValue); - } - - return m; - } else { - // return an Array - var arr = []; - - if (size.length > 0) { - return Object(utils_array["m" /* resize */])(arr, size, defaultValue); - } - - return arr; - } - } // replace BigNumbers with numbers, returns true if size contained BigNumbers - - function _normalize(size) { - var hasBigNumbers = false; - size.forEach(function (value, index, arr) { - if (Object(is["e" /* isBigNumber */])(value)) { - hasBigNumbers = true; - arr[index] = value.toNumber(); - } - }); - return hasBigNumbers; - } // validate arguments - - function _validate(size) { - size.forEach(function (value) { - if (typeof value !== 'number' || !Object(utils_number["i" /* isInteger */])(value) || value < 0) { - throw new Error('Parameters in function zeros must be positive integers'); - } - }); - } - }); // TODO: zeros contains almost the same code as ones. Reuse this? - // CONCATENATED MODULE: ./src/function/special/erf.js - - var erf_name = 'erf'; - var erf_dependencies = ['typed']; - var createErf = /* #__PURE__ */Object(factory["a" /* factory */])(erf_name, erf_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Compute the erf function of a value using a rational Chebyshev - * approximations for different intervals of x. - * - * This is a translation of W. J. Cody's Fortran implementation from 1987 - * ( https://www.netlib.org/specfun/erf ). See the AMS publication - * "Rational Chebyshev Approximations for the Error Function" by W. J. Cody - * for an explanation of this process. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.erf(x) - * - * Examples: - * - * math.erf(0.2) // returns 0.22270258921047847 - * math.erf(-0.5) // returns -0.5204998778130465 - * math.erf(4) // returns 0.9999999845827421 - * - * @param {number | Array | Matrix} x A real number - * @return {number | Array | Matrix} The erf of `x` - */ - return typed('name', { - number: function number(x) { - var y = Math.abs(x); - - if (y >= MAX_NUM) { - return Object(utils_number["n" /* sign */])(x); - } - - if (y <= THRESH) { - return Object(utils_number["n" /* sign */])(x) * erf1(y); - } - - if (y <= 4.0) { - return Object(utils_number["n" /* sign */])(x) * (1 - erfc2(y)); - } - - return Object(utils_number["n" /* sign */])(x) * (1 - erfc3(y)); - }, - 'Array | Matrix': function ArrayMatrix(n) { - return deepMap(n, this); - } // TODO: For complex numbers, use the approximation for the Faddeeva function - // from "More Efficient Computation of the Complex Error Function" (AMS) - - }); - /** - * Approximates the error function erf() for x <= 0.46875 using this function: - * n - * erf(x) = x * sum (p_j * x^(2j)) / (q_j * x^(2j)) - * j=0 - */ - - function erf1(y) { - var ysq = y * y; - var xnum = erf_P[0][4] * ysq; - var xden = ysq; - var i; - - for (i = 0; i < 3; i += 1) { - xnum = (xnum + erf_P[0][i]) * ysq; - xden = (xden + Q[0][i]) * ysq; - } - - return y * (xnum + erf_P[0][3]) / (xden + Q[0][3]); - } - /** - * Approximates the complement of the error function erfc() for - * 0.46875 <= x <= 4.0 using this function: - * n - * erfc(x) = e^(-x^2) * sum (p_j * x^j) / (q_j * x^j) - * j=0 - */ - - function erfc2(y) { - var xnum = erf_P[1][8] * y; - var xden = y; - var i; - - for (i = 0; i < 7; i += 1) { - xnum = (xnum + erf_P[1][i]) * y; - xden = (xden + Q[1][i]) * y; - } - - var result = (xnum + erf_P[1][7]) / (xden + Q[1][7]); - var ysq = parseInt(y * 16) / 16; - var del = (y - ysq) * (y + ysq); - return Math.exp(-ysq * ysq) * Math.exp(-del) * result; - } - /** - * Approximates the complement of the error function erfc() for x > 4.0 using - * this function: - * - * erfc(x) = (e^(-x^2) / x) * [ 1/sqrt(pi) + - * n - * 1/(x^2) * sum (p_j * x^(-2j)) / (q_j * x^(-2j)) ] - * j=0 - */ - - function erfc3(y) { - var ysq = 1 / (y * y); - var xnum = erf_P[2][5] * ysq; - var xden = ysq; - var i; - - for (i = 0; i < 4; i += 1) { - xnum = (xnum + erf_P[2][i]) * ysq; - xden = (xden + Q[2][i]) * ysq; - } - - var result = ysq * (xnum + erf_P[2][4]) / (xden + Q[2][4]); - result = (SQRPI - result) / y; - ysq = parseInt(y * 16) / 16; - var del = (y - ysq) * (y + ysq); - return Math.exp(-ysq * ysq) * Math.exp(-del) * result; - } - }); - /** - * Upper bound for the first approximation interval, 0 <= x <= THRESH - * @constant - */ - - var THRESH = 0.46875; - /** - * Constant used by W. J. Cody's Fortran77 implementation to denote sqrt(pi) - * @constant - */ - - var SQRPI = 5.6418958354775628695e-1; - /** - * Coefficients for each term of the numerator sum (p_j) for each approximation - * interval (see W. J. Cody's paper for more details) - * @constant - */ - - var erf_P = [[3.16112374387056560e00, 1.13864154151050156e02, 3.77485237685302021e02, 3.20937758913846947e03, 1.85777706184603153e-1], [5.64188496988670089e-1, 8.88314979438837594e00, 6.61191906371416295e01, 2.98635138197400131e02, 8.81952221241769090e02, 1.71204761263407058e03, 2.05107837782607147e03, 1.23033935479799725e03, 2.15311535474403846e-8], [3.05326634961232344e-1, 3.60344899949804439e-1, 1.25781726111229246e-1, 1.60837851487422766e-2, 6.58749161529837803e-4, 1.63153871373020978e-2]]; - /** - * Coefficients for each term of the denominator sum (q_j) for each approximation - * interval (see W. J. Cody's paper for more details) - * @constant - */ - - var Q = [[2.36012909523441209e01, 2.44024637934444173e02, 1.28261652607737228e03, 2.84423683343917062e03], [1.57449261107098347e01, 1.17693950891312499e02, 5.37181101862009858e02, 1.62138957456669019e03, 3.29079923573345963e03, 4.36261909014324716e03, 3.43936767414372164e03, 1.23033935480374942e03], [2.56852019228982242e00, 1.87295284992346047e00, 5.27905102951428412e-1, 6.05183413124413191e-2, 2.33520497626869185e-3]]; - /** - * Maximum/minimum safe numbers to input to erf() (in ES6+, this number is - * Number.[MAX|MIN]_SAFE_INTEGER). erf() for all numbers beyond this limit will - * return 1 - */ - - var MAX_NUM = Math.pow(2, 53); - // CONCATENATED MODULE: ./src/function/statistics/mode.js - - var mode_name = 'mode'; - var mode_dependencies = ['typed', 'isNaN', 'isNumeric']; - var createMode = /* #__PURE__ */Object(factory["a" /* factory */])(mode_name, mode_dependencies, function (_ref) { - var typed = _ref.typed, - isNaN = _ref.isNaN, - isNumeric = _ref.isNumeric; - - /** - * Computes the mode of a set of numbers or a list with values(numbers or characters). - * If there are more than one modes, it returns a list of those values. - * - * Syntax: - * - * math.mode(a, b, c, ...) - * math.mode(A) - * - * Examples: - * - * math.mode(2, 1, 4, 3, 1) // returns [1] - * math.mode([1, 2.7, 3.2, 4, 2.7]) // returns [2.7] - * math.mode(1, 4, 6, 1, 6) // returns [1, 6] - * math.mode('a','a','b','c') // returns ["a"] - * math.mode(1, 1.5, 'abc') // returns [1, 1.5, "abc"] - * - * See also: - * - * median, - * mean - * - * @param {... *} args A single matrix - * @return {*} The mode of all values - */ - return typed(mode_name, { - 'Array | Matrix': _mode, - '...': function _(args) { - return _mode(args); - } - }); - /** - * Calculates the mode in an 1-dimensional array - * @param {Array} values - * @return {Array} mode - * @private - */ - - function _mode(values) { - values = Object(utils_array["e" /* flatten */])(values.valueOf()); - var num = values.length; - - if (num === 0) { - throw new Error('Cannot calculate mode of an empty array'); - } - - var count = {}; - var mode = []; - var max = 0; - - for (var i = 0; i < values.length; i++) { - var value = values[i]; - - if (isNumeric(value) && isNaN(value)) { - throw new Error('Cannot calculate mode of an array containing NaN values'); - } - - if (!(value in count)) { - count[value] = 0; - } - - count[value]++; - - if (count[value] === max) { - mode.push(value); - } else if (count[value] > max) { - max = count[value]; - mode = [value]; - } - } - - return mode; - } - }); - // CONCATENATED MODULE: ./src/function/statistics/utils/improveErrorMessage.js - - /** - * Improve error messages for statistics functions. Errors are typically - * thrown in an internally used function like larger, causing the error - * not to mention the function (like max) which is actually used by the user. - * - * @param {Error} err - * @param {String} fnName - * @param {*} [value] - * @return {Error} - */ - - function improveErrorMessage(err, fnName, value) { - // TODO: add information with the index (also needs transform in expression parser) - var details; - - if (String(err).indexOf('Unexpected type') !== -1) { - details = arguments.length > 2 ? ' (type: ' + Object(is["M" /* typeOf */])(value) + ', value: ' + JSON.stringify(value) + ')' : ' (type: ' + err.data.actual + ')'; - return new TypeError('Cannot calculate ' + fnName + ', unexpected type of argument' + details); - } - - if (String(err).indexOf('complex numbers') !== -1) { - details = arguments.length > 2 ? ' (type: ' + Object(is["M" /* typeOf */])(value) + ', value: ' + JSON.stringify(value) + ')' : ''; - return new TypeError('Cannot calculate ' + fnName + ', no ordering relation is defined for complex numbers' + details); - } - - return err; - } - // CONCATENATED MODULE: ./src/function/statistics/prod.js - - var prod_name = 'prod'; - var prod_dependencies = ['typed', 'config', 'multiplyScalar', 'numeric']; - var createProd = /* #__PURE__ */Object(factory["a" /* factory */])(prod_name, prod_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - multiplyScalar = _ref.multiplyScalar, - numeric = _ref.numeric; - - /** - * Compute the product of a matrix or a list with values. - * In case of a (multi dimensional) array or matrix, the sum of all - * elements will be calculated. - * - * Syntax: - * - * math.prod(a, b, c, ...) - * math.prod(A) - * - * Examples: - * - * math.multiply(2, 3) // returns 6 - * math.prod(2, 3) // returns 6 - * math.prod(2, 3, 4) // returns 24 - * math.prod([2, 3, 4]) // returns 24 - * math.prod([[2, 5], [4, 3]]) // returns 120 - * - * See also: - * - * mean, median, min, max, sum, std, variance - * - * @param {... *} args A single matrix or or multiple scalar values - * @return {*} The product of all values - */ - return typed(prod_name, { - // prod([a, b, c, d, ...]) - 'Array | Matrix': _prod, - // prod([a, b, c, d, ...], dim) - 'Array | Matrix, number | BigNumber': function ArrayMatrixNumberBigNumber(array, dim) { - // TODO: implement prod(A, dim) - throw new Error('prod(A, dim) is not yet supported'); // return reduce(arguments[0], arguments[1], math.prod) - }, - // prod(a, b, c, d, ...) - '...': function _(args) { - return _prod(args); - } - }); - /** - * Recursively calculate the product of an n-dimensional array - * @param {Array} array - * @return {number} prod - * @private - */ - - function _prod(array) { - var prod; - deepForEach(array, function (value) { - try { - prod = prod === undefined ? value : multiplyScalar(prod, value); - } catch (err) { - throw improveErrorMessage(err, 'prod', value); - } - }); // make sure returning numeric value: parse a string into a numeric value - - if (typeof prod === 'string') { - prod = numeric(prod, config.number); - } - - if (prod === undefined) { - throw new Error('Cannot calculate prod of an empty array'); - } - - return prod; - } - }); - // CONCATENATED MODULE: ./src/function/string/format.js - - var format_name = 'format'; - var format_dependencies = ['typed']; - var createFormat = /* #__PURE__ */Object(factory["a" /* factory */])(format_name, format_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Format a value of any type into a string. - * - * Syntax: - * - * math.format(value) - * math.format(value, options) - * math.format(value, precision) - * math.format(value, callback) - * - * Where: - * - * - `value: *` - * The value to be formatted - * - `options: Object` - * An object with formatting options. Available options: - * - `notation: string` - * Number notation. Choose from: - * - 'fixed' - * Always use regular number notation. - * For example '123.40' and '14000000' - * - 'exponential' - * Always use exponential notation. - * For example '1.234e+2' and '1.4e+7' - * - 'engineering' - * Always use engineering notation: always have exponential notation, - * and select the exponent to be a multiple of 3. - * For example '123.4e+0' and '14.0e+6' - * - 'auto' (default) - * Regular number notation for numbers having an absolute value between - * `lower` and `upper` bounds, and uses exponential notation elsewhere. - * Lower bound is included, upper bound is excluded. - * For example '123.4' and '1.4e7'. - * - `precision: number` - * A number between 0 and 16 to round the digits of the number. In case - * of notations 'exponential', 'engineering', and 'auto', `precision` - * defines the total number of significant digits returned. - * In case of notation 'fixed', `precision` defines the number of - * significant digits after the decimal point. - * `precision` is undefined by default. - * - `lowerExp: number` - * Exponent determining the lower boundary for formatting a value with - * an exponent when `notation='auto`. Default value is `-3`. - * - `upperExp: number` - * Exponent determining the upper boundary for formatting a value with - * an exponent when `notation='auto`. Default value is `5`. - * - `fraction: string`. Available values: 'ratio' (default) or 'decimal'. - * For example `format(fraction(1, 3))` will output '1/3' when 'ratio' is - * configured, and will output `0.(3)` when 'decimal' is configured. - * - `callback: function` - * A custom formatting function, invoked for all numeric elements in `value`, - * for example all elements of a matrix, or the real and imaginary - * parts of a complex number. This callback can be used to override the - * built-in numeric notation with any type of formatting. Function `callback` - * is called with `value` as parameter and must return a string. - * - * When `value` is an Object: - * - * - When the object contains a property `format` being a function, this function - * is invoked as `value.format(options)` and the result is returned. - * - When the object has its own `toString` method, this method is invoked - * and the result is returned. - * - In other cases the function will loop over all object properties and - * return JSON object notation like '{"a": 2, "b": 3}'. - * - * When value is a function: - * - * - When the function has a property `syntax`, it returns this - * syntax description. - * - In other cases, a string `'function'` is returned. - * - * Examples: - * - * math.format(6.4) // returns '6.4' - * math.format(1240000) // returns '1.24e6' - * math.format(1/3) // returns '0.3333333333333333' - * math.format(1/3, 3) // returns '0.333' - * math.format(21385, 2) // returns '21000' - * math.format(12e8, {notation: 'fixed'}) // returns '1200000000' - * math.format(2.3, {notation: 'fixed', precision: 4}) // returns '2.3000' - * math.format(52.8, {notation: 'exponential'}) // returns '5.28e+1' - * math.format(12400,{notation: 'engineering'}) // returns '12.400e+3' - * math.format(2000, {lowerExp: -2, upperExp: 2}) // returns '2e+3' - * - * function formatCurrency(value) { - * // return currency notation with two digits: - * return '$' + value.toFixed(2) - * - * // you could also use math.format inside the callback: - * // return '$' + math.format(value, {notation: 'fixed', precision: 2}) - * } - * math.format([2.1, 3, 0.016], formatCurrency} // returns '[$2.10, $3.00, $0.02]' - * - * See also: - * - * print - * - * @param {*} value Value to be stringified - * @param {Object | Function | number} [options] Formatting options - * @return {string} The formatted value - */ - return typed(format_name, { - any: utils_string["d" /* format */], - 'any, Object | function | number': utils_string["d" /* format */] - }); - }); - // CONCATENATED MODULE: ./src/function/string/baseUtils.js - - function baseFormatter(base) { - var prefixes = { - 2: '0b', - 8: '0o', - 16: '0x' - }; - var prefix = prefixes[base]; - return function (n) { - if (n > Math.pow(2, 31) - 1 || n < -Math.pow(2, 31)) { - throw new Error('Value must be in range [-2^31, 2^31-1]'); - } - - if (!Object(utils_number["i" /* isInteger */])(n)) { - throw new Error('Value must be an integer'); - } - - if (n < 0) { - n = n + Math.pow(2, 32); - } - - return "".concat(prefix).concat(n.toString(base)); - }; - } - - var baseUtils_dependencies = ['typed']; - function createBaseFormatterFactory(name, base) { - return Object(factory["a" /* factory */])(name, baseUtils_dependencies, function (_ref) { - var typed = _ref.typed; - return typed(name, { - number: baseFormatter(base) - }); - }); - } - // CONCATENATED MODULE: ./src/function/string/bin.js - - /** - * Format a number as binary. - * - * Syntax: - * - * math.bin(value) - * - * Examples: - * - * //the following outputs "0b10" - * math.bin(2) - * - * See also: - * - * oct - * hex - * - * @param {number} value Value to be stringified - * @return {string} The formatted value - */ - - var createBin = createBaseFormatterFactory('bin', 2); - // CONCATENATED MODULE: ./src/function/string/oct.js - - /** - * Format a number as octal. - * - * Syntax: - * - * math.oct(value) - * - * Examples: - * - * //the following outputs "0o70" - * math.oct(56) - * - * See also: - * - * bin - * hex - * - * @param {number} value Value to be stringified - * @return {string} The formatted value - */ - - var createOct = createBaseFormatterFactory('oct', 8); - // CONCATENATED MODULE: ./src/function/string/hex.js - - /** - * Format a number as hexadecimal. - * - * Syntax: - * - * math.hex(value) - * - * Examples: - * - * //the following outputs "0xF0" - * math.hex(240) - * - * See also: - * - * oct - * bin - * - * @param {number} value Value to be stringified - * @return {string} The formatted value - */ - - var createHex = createBaseFormatterFactory('hex', 16); - // CONCATENATED MODULE: ./src/function/string/print.js - - var print_name = 'print'; - var print_dependencies = ['typed']; - var createPrint = /* #__PURE__ */Object(factory["a" /* factory */])(print_name, print_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Interpolate values into a string template. - * - * Syntax: - * - * math.print(template, values) - * math.print(template, values, precision) - * math.print(template, values, options) - * - * Example usage: - * - * // the following outputs: 'Lucy is 5 years old' - * math.print('Lucy is $age years old', {age: 5}) - * - * // the following outputs: 'The value of pi is 3.141592654' - * math.print('The value of pi is $pi', {pi: math.pi}, 10) - * - * // the following outputs: 'hello Mary! The date is 2013-03-23' - * math.print('Hello $user.name! The date is $date', { - * user: { - * name: 'Mary', - * }, - * date: new Date(2013, 2, 23).toISOString().substring(0, 10) - * }) - * - * // the following outputs: 'My favorite fruits are apples and bananas !' - * math.print('My favorite fruits are $0 and $1 !', [ - * 'apples', - * 'bananas' - * ]) - * - * See also: - * - * format - * - * @param {string} template A string containing variable placeholders. - * @param {Object | Array | Matrix} values An object or array containing variables - * which will be filled in in the template. - * @param {number | Object} [options] Formatting options, - * or the number of digits to format numbers. - * See function math.format for a description - * of all options. - * @return {string} Interpolated string - */ - return typed(print_name, { - // note: Matrix will be converted automatically to an Array - 'string, Object | Array': _print, - 'string, Object | Array, number | Object': _print - }); - }); - /** - * Interpolate values into a string template. - * @param {string} template - * @param {Object} values - * @param {number | Object} [options] - * @returns {string} Interpolated string - * @private - */ - - function _print(template, values, options) { - return template.replace(/\$([\w.]+)/g, function (original, key) { - var keys = key.split('.'); - var value = values[keys.shift()]; - - while (keys.length && value !== undefined) { - var k = keys.shift(); - value = k ? value[k] : value + '.'; - } - - if (value !== undefined) { - if (!Object(is["I" /* isString */])(value)) { - return Object(utils_string["d" /* format */])(value, options); - } else { - return value; - } - } - - return original; - }); - } - // CONCATENATED MODULE: ./src/function/unit/to.js - - var to_name = 'to'; - var to_dependencies = ['typed', 'matrix']; - var createTo = /* #__PURE__ */Object(factory["a" /* factory */])(to_name, to_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix; - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Change the unit of a value. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.to(x, unit) - * - * Examples: - * - * math.to(math.unit('2 inch'), 'cm') // returns Unit 5.08 cm - * math.to(math.unit('2 inch'), math.unit(null, 'cm')) // returns Unit 5.08 cm - * math.to(math.unit(16, 'bytes'), 'bits') // returns Unit 128 bits - * - * See also: - * - * unit - * - * @param {Unit | Array | Matrix} x The unit to be converted. - * @param {Unit | Array | Matrix} unit New unit. Can be a string like "cm" - * or a unit without value. - * @return {Unit | Array | Matrix} value with changed, fixed unit. - */ - - return typed(to_name, { - 'Unit, Unit | string': function UnitUnitString(x, unit) { - return x.to(unit); - }, - 'Matrix, Matrix': function MatrixMatrix(x, y) { - // SparseMatrix does not support Units - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'Matrix, any': function MatrixAny(x, y) { - // SparseMatrix does not support Units - return algorithm14(x, y, this, false); - }, - 'any, Matrix': function anyMatrix(x, y) { - // SparseMatrix does not support Units - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/utils/isPrime.js - - var isPrime_name = 'isPrime'; - var isPrime_dependencies = ['typed']; - var createIsPrime = /* #__PURE__ */Object(factory["a" /* factory */])(isPrime_name, isPrime_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Test whether a value is prime: has no divisors other than itself and one. - * The function supports type `number`, `bignumber`. - * - * The function is evaluated element-wise in case of Array or Matrix input. - * - * Syntax: - * - * math.isPrime(x) - * - * Examples: - * - * math.isPrime(3) // returns true - * math.isPrime(-2) // returns false - * math.isPrime(0) // returns false - * math.isPrime(-0) // returns false - * math.isPrime(0.5) // returns false - * math.isPrime('2') // returns true - * math.isPrime([2, 17, 100]) // returns [true, true, false] - * - * See also: - * - * isNumeric, isZero, isNegative, isInteger - * - * @param {number | BigNumber | Array | Matrix} x Value to be tested - * @return {boolean} Returns true when `x` is larger than zero. - * Throws an error in case of an unknown data type. - */ - return typed(isPrime_name, { - number: function number(x) { - if (x * 0 !== 0) { - return false; - } - - if (x <= 3) { - return x > 1; - } - - if (x % 2 === 0 || x % 3 === 0) { - return false; - } - - for (var i = 5; i * i <= x; i += 6) { - if (x % i === 0 || x % (i + 2) === 0) { - return false; - } - } - - return true; - }, - BigNumber: function BigNumber(n) { - if (n.toNumber() * 0 !== 0) { - return false; - } - - if (n.lte(3)) { - return n.gt(1); - } - if (n.mod(2).eq(0) || n.mod(3).eq(0)) { - return false; - } - - for (var i = 5; n.gte(i * i); i += 6) { - if (n.mod(i).eq(0) || n.mod(i + 2).eq(0)) { - return false; - } - } - - return true; - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/utils/numeric.js - - var numeric_name = 'numeric'; - var numeric_dependencies = ['number', '?bignumber', '?fraction']; - var createNumeric = /* #__PURE__ */Object(factory["a" /* factory */])(numeric_name, numeric_dependencies, function (_ref) { - var _number = _ref.number, - bignumber = _ref.bignumber, - fraction = _ref.fraction; - var validInputTypes = { - string: true, - number: true, - BigNumber: true, - Fraction: true - }; // Load the conversion functions for each output type - - var validOutputTypes = { - number: function number(x) { - return _number(x); - }, - BigNumber: bignumber ? function (x) { - return bignumber(x); - } : noBignumber, - Fraction: fraction ? function (x) { - return fraction(x); - } : noFraction - }; - /** - * Convert a numeric input to a specific numeric type: number, BigNumber, or Fraction. - * - * Syntax: - * - * math.numeric(x) - * - * Examples: - * - * math.numeric('4') // returns number 4 - * math.numeric('4', 'number') // returns number 4 - * math.numeric('4', 'BigNumber') // returns BigNumber 4 - * math.numeric('4', 'Fraction') // returns Fraction 4 - * math.numeric(4, 'Fraction') // returns Fraction 4 - * math.numeric(math.fraction(2, 5), 'number') // returns number 0.4 - * - * See also: - * - * number, fraction, bignumber, string, format - * - * @param {string | number | BigNumber | Fraction } value - * A numeric value or a string containing a numeric value - * @param {string} outputType - * Desired numeric output type. - * Available values: 'number', 'BigNumber', or 'Fraction' - * @return {number | BigNumber | Fraction} - * Returns an instance of the numeric in the requested type - */ - - return function numeric(value, outputType) { - var inputType = Object(is["M" /* typeOf */])(value); - - if (!(inputType in validInputTypes)) { - throw new TypeError('Cannot convert ' + value + ' of type "' + inputType + '"; valid input types are ' + Object.keys(validInputTypes).join(', ')); - } - - if (!(outputType in validOutputTypes)) { - throw new TypeError('Cannot convert ' + value + ' to type "' + outputType + '"; valid output types are ' + Object.keys(validOutputTypes).join(', ')); - } - - if (outputType === inputType) { - return value; - } else { - return validOutputTypes[outputType](value); - } - }; - }); - // CONCATENATED MODULE: ./src/function/arithmetic/divideScalar.js - - var divideScalar_name = 'divideScalar'; - var divideScalar_dependencies = ['typed', 'numeric']; - var createDivideScalar = /* #__PURE__ */Object(factory["a" /* factory */])(divideScalar_name, divideScalar_dependencies, function (_ref) { - var typed = _ref.typed, - numeric = _ref.numeric; - - /** - * Divide two scalar values, `x / y`. - * This function is meant for internal use: it is used by the public functions - * `divide` and `inv`. - * - * This function does not support collections (Array or Matrix). - * - * @param {number | BigNumber | Fraction | Complex | Unit} x Numerator - * @param {number | BigNumber | Fraction | Complex} y Denominator - * @return {number | BigNumber | Fraction | Complex | Unit} Quotient, `x / y` - * @private - */ - return typed(divideScalar_name, { - 'number, number': function numberNumber(x, y) { - return x / y; - }, - 'Complex, Complex': function ComplexComplex(x, y) { - return x.div(y); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return x.div(y); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.div(y); - }, - 'Unit, number | Fraction | BigNumber': function UnitNumberFractionBigNumber(x, y) { - var res = x.clone(); // TODO: move the divide function to Unit.js, it uses internals of Unit - - var one = numeric(1, Object(is["M" /* typeOf */])(y)); - res.value = this(res.value === null ? res._normalize(one) : res.value, y); - return res; - }, - 'number | Fraction | BigNumber, Unit': function numberFractionBigNumberUnit(x, y) { - var res = y.clone(); - res = res.pow(-1); // TODO: move the divide function to Unit.js, it uses internals of Unit - - var one = numeric(1, Object(is["M" /* typeOf */])(x)); - res.value = this(x, y.value === null ? y._normalize(one) : y.value); - return res; - }, - 'Unit, Unit': function UnitUnit(x, y) { - return x.divide(y); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/pow.js - - var pow_name = 'pow'; - var pow_dependencies = ['typed', 'config', 'identity', 'multiply', 'matrix', 'fraction', 'number', 'Complex']; - var createPow = /* #__PURE__ */Object(factory["a" /* factory */])(pow_name, pow_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - identity = _ref.identity, - multiply = _ref.multiply, - matrix = _ref.matrix, - number = _ref.number, - fraction = _ref.fraction, - Complex = _ref.Complex; - - /** - * Calculates the power of x to y, `x ^ y`. - * Matrix exponentiation is supported for square matrices `x`, and positive - * integer exponents `y`. - * - * For cubic roots of negative numbers, the function returns the principal - * root by default. In order to let the function return the real root, - * math.js can be configured with `math.config({predictable: true})`. - * To retrieve all cubic roots of a value, use `math.cbrt(x, true)`. - * - * Syntax: - * - * math.pow(x, y) - * - * Examples: - * - * math.pow(2, 3) // returns number 8 - * - * const a = math.complex(2, 3) - * math.pow(a, 2) // returns Complex -5 + 12i - * - * const b = [[1, 2], [4, 3]] - * math.pow(b, 2) // returns Array [[9, 8], [16, 17]] - * - * See also: - * - * multiply, sqrt, cbrt, nthRoot - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x The base - * @param {number | BigNumber | Complex} y The exponent - * @return {number | BigNumber | Complex | Array | Matrix} The value of `x` to the power `y` - */ - return typed(pow_name, { - 'number, number': _pow, - 'Complex, Complex': function ComplexComplex(x, y) { - return x.pow(y); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - if (y.isInteger() || x >= 0 || config.predictable) { - return x.pow(y); - } else { - return new Complex(x.toNumber(), 0).pow(y.toNumber(), 0); - } - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - if (y.d !== 1) { - if (config.predictable) { - throw new Error('Function pow does not support non-integer exponents for fractions.'); - } else { - return _pow(x.valueOf(), y.valueOf()); - } - } else { - return x.pow(y); - } - }, - 'Array, number': _powArray, - 'Array, BigNumber': function ArrayBigNumber(x, y) { - return _powArray(x, y.toNumber()); - }, - 'Matrix, number': _powMatrix, - 'Matrix, BigNumber': function MatrixBigNumber(x, y) { - return _powMatrix(x, y.toNumber()); - }, - 'Unit, number | BigNumber': function UnitNumberBigNumber(x, y) { - return x.pow(y); - } - }); - /** - * Calculates the power of x to y, x^y, for two numbers. - * @param {number} x - * @param {number} y - * @return {number | Complex} res - * @private - */ - - function _pow(x, y) { - // Alternatively could define a 'realmode' config option or something, but - // 'predictable' will work for now - if (config.predictable && !Object(utils_number["i" /* isInteger */])(y) && x < 0) { - // Check to see if y can be represented as a fraction - try { - var yFrac = fraction(y); - var yNum = number(yFrac); - - if (y === yNum || Math.abs((y - yNum) / y) < 1e-14) { - if (yFrac.d % 2 === 1) { - return (yFrac.n % 2 === 0 ? 1 : -1) * Math.pow(-x, y); - } - } - } catch (ex) {// fraction() throws an error if y is Infinity, etc. - } // Unable to express y as a fraction, so continue on - - } // **for predictable mode** x^Infinity === NaN if x < -1 - // N.B. this behavour is different from `Math.pow` which gives - // (-2)^Infinity === Infinity - - if (config.predictable && (x < -1 && y === Infinity || x > -1 && x < 0 && y === -Infinity)) { - return NaN; - } - - if (Object(utils_number["i" /* isInteger */])(y) || x >= 0 || config.predictable) { - return powNumber(x, y); - } else { - // TODO: the following infinity checks are duplicated from powNumber. Deduplicate this somehow - // x^Infinity === 0 if -1 < x < 1 - // A real number 0 is returned instead of complex(0) - if (x * x < 1 && y === Infinity || x * x > 1 && y === -Infinity) { - return 0; - } - - return new Complex(x, 0).pow(y, 0); - } - } - /** - * Calculate the power of a 2d array - * @param {Array} x must be a 2 dimensional, square matrix - * @param {number} y a positive, integer value - * @returns {Array} - * @private - */ - - function _powArray(x, y) { - if (!Object(utils_number["i" /* isInteger */])(y) || y < 0) { - throw new TypeError('For A^b, b must be a positive integer (value is ' + y + ')'); - } // verify that A is a 2 dimensional square matrix - - var s = Object(utils_array["a" /* arraySize */])(x); - - if (s.length !== 2) { - throw new Error('For A^b, A must be 2 dimensional (A has ' + s.length + ' dimensions)'); - } - - if (s[0] !== s[1]) { - throw new Error('For A^b, A must be square (size is ' + s[0] + 'x' + s[1] + ')'); - } - - var res = identity(s[0]).valueOf(); - var px = x; - - while (y >= 1) { - if ((y & 1) === 1) { - res = multiply(px, res); - } - - y >>= 1; - px = multiply(px, px); - } - - return res; - } - /** - * Calculate the power of a 2d matrix - * @param {Matrix} x must be a 2 dimensional, square matrix - * @param {number} y a positive, integer value - * @returns {Matrix} - * @private - */ - - function _powMatrix(x, y) { - return matrix(_powArray(x.valueOf(), y)); - } - }); - // CONCATENATED MODULE: ./src/function/arithmetic/round.js - function ownKeys(object, enumerableOnly) { - var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { - var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { - symbols = symbols.filter(function (sym) { - return Object.getOwnPropertyDescriptor(object, sym).enumerable; - }); - } keys.push.apply(keys, symbols); - } return keys; - } - - function _objectSpread(target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { - ownKeys(Object(source), true).forEach(function (key) { - _defineProperty(target, key, source[key]); - }); - } else if (Object.getOwnPropertyDescriptors) { - Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); - } else { - ownKeys(Object(source)).forEach(function (key) { - Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); - }); - } - } return target; - } - - function _defineProperty(obj, key, value) { - if (key in obj) { - Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); - } else { - obj[key] = value; - } return obj; - } - - var NO_INT = 'Number of decimals in function round must be an integer'; - var round_name = 'round'; - var round_dependencies = ['typed', 'matrix', 'equalScalar', 'zeros', 'BigNumber', 'DenseMatrix']; - var createRound = /* #__PURE__ */Object(factory["a" /* factory */])(round_name, round_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - zeros = _ref.zeros, - BigNumber = _ref.BigNumber, - DenseMatrix = _ref.DenseMatrix; - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Round a value towards the nearest integer. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.round(x) - * math.round(x, n) - * - * Examples: - * - * math.round(3.22) // returns number 3 - * math.round(3.82) // returns number 4 - * math.round(-4.2) // returns number -4 - * math.round(-4.7) // returns number -5 - * math.round(3.22, 1) // returns number 3.2 - * math.round(3.88, 1) // returns number 3.8 - * math.round(-4.21, 1) // returns number -4.2 - * math.round(-4.71, 1) // returns number -4.7 - * math.round(math.pi, 3) // returns number 3.142 - * math.round(123.45678, 2) // returns number 123.46 - * - * const c = math.complex(3.2, -2.7) - * math.round(c) // returns Complex 3 - 3i - * - * math.round([3.2, 3.8, -4.7]) // returns Array [3, 4, -5] - * - * See also: - * - * ceil, fix, floor - * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded - * @param {number | BigNumber | Array} [n=0] Number of decimals - * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value - */ - - return typed(round_name, _objectSpread(_objectSpread({}, roundNumberSignatures), {}, { - Complex: function Complex(x) { - return x.round(); - }, - 'Complex, number': function ComplexNumber(x, n) { - if (n % 1) { - throw new TypeError(NO_INT); - } - - return x.round(n); - }, - 'Complex, BigNumber': function ComplexBigNumber(x, n) { - if (!n.isInteger()) { - throw new TypeError(NO_INT); - } - - var _n = n.toNumber(); - - return x.round(_n); - }, - 'number, BigNumber': function numberBigNumber(x, n) { - if (!n.isInteger()) { - throw new TypeError(NO_INT); - } - - return new BigNumber(x).toDecimalPlaces(n.toNumber()); - }, - BigNumber: function BigNumber(x) { - return x.toDecimalPlaces(0); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, n) { - if (!n.isInteger()) { - throw new TypeError(NO_INT); - } - - return x.toDecimalPlaces(n.toNumber()); - }, - Fraction: function Fraction(x) { - return x.round(); - }, - 'Fraction, number': function FractionNumber(x, n) { - if (n % 1) { - throw new TypeError(NO_INT); - } - - return x.round(n); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since round(0) = 0 - return deepMap(x, this, true); - }, - 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { - return algorithm14(x, y, this, false); - }, - 'number | Complex | BigNumber, SparseMatrix': function numberComplexBigNumberSparseMatrix(x, y) { - // check scalar is zero - if (equalScalar(x, 0)) { - // do not execute algorithm, result will be a zero matrix - return zeros(y.size(), y.storage()); - } - - return algorithm12(y, x, this, true); - }, - 'number | Complex | BigNumber, DenseMatrix': function numberComplexBigNumberDenseMatrix(x, y) { - // check scalar is zero - if (equalScalar(x, 0)) { - // do not execute algorithm, result will be a zero matrix - return zeros(y.size(), y.storage()); - } - - return algorithm14(y, x, this, true); - }, - 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'number | Complex | BigNumber, Array': function numberComplexBigNumberArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - })); - }); - var roundNumberSignatures = { - number: roundNumber, - 'number, number': function numberNumber(x, n) { - if (!Object(utils_number["i" /* isInteger */])(n)) { - throw new TypeError(NO_INT); - } - - if (n < 0 || n > 15) { - throw new Error('Number of decimals in function round must be in te range of 0-15'); - } - - return roundNumber(x, n); - } - }; - var createRoundNumber = /* #__PURE__ */Object(factory["a" /* factory */])(round_name, ['typed'], function (_ref2) { - var typed = _ref2.typed; - return typed(round_name, roundNumberSignatures); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/log.js - - var log_name = 'log'; - var log_dependencies = ['config', 'typed', 'divideScalar', 'Complex']; - var createLog = /* #__PURE__ */Object(factory["a" /* factory */])(log_name, log_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - divideScalar = _ref.divideScalar, - Complex = _ref.Complex; - - /** - * Calculate the logarithm of a value. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.log(x) - * math.log(x, base) - * - * Examples: - * - * math.log(3.5) // returns 1.252762968495368 - * math.exp(math.log(2.4)) // returns 2.4 - * - * math.pow(10, 4) // returns 10000 - * math.log(10000, 10) // returns 4 - * math.log(10000) / math.log(10) // returns 4 - * - * math.log(1024, 2) // returns 10 - * math.pow(2, 10) // returns 1024 - * - * See also: - * - * exp, log2, log10, log1p - * - * @param {number | BigNumber | Complex | Array | Matrix} x - * Value for which to calculate the logarithm. - * @param {number | BigNumber | Complex} [base=e] - * Optional base for the logarithm. If not provided, the natural - * logarithm of `x` is calculated. - * @return {number | BigNumber | Complex | Array | Matrix} - * Returns the logarithm of `x` - */ - return typed(log_name, { - number: function number(x) { - if (x >= 0 || config.predictable) { - return logNumber(x); - } else { - // negative value -> complex value computation - return new Complex(x, 0).log(); - } - }, - Complex: function Complex(x) { - return x.log(); - }, - BigNumber: function BigNumber(x) { - if (!x.isNegative() || config.predictable) { - return x.ln(); - } else { - // downgrade to number, return Complex valued result - return new Complex(x.toNumber(), 0).log(); - } - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - }, - 'any, any': function anyAny(x, base) { - // calculate logarithm for a specified base, log(x, base) - return divideScalar(this(x), this(base)); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/log1p.js - - var log1p_name = 'log1p'; - var log1p_dependencies = ['typed', 'config', 'divideScalar', 'log', 'Complex']; - var createLog1p = /* #__PURE__ */Object(factory["a" /* factory */])(log1p_name, log1p_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - divideScalar = _ref.divideScalar, - log = _ref.log, - Complex = _ref.Complex; - - /** - * Calculate the logarithm of a `value+1`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.log1p(x) - * math.log1p(x, base) - * - * Examples: - * - * math.log1p(2.5) // returns 1.252762968495368 - * math.exp(math.log1p(1.4)) // returns 2.4 - * - * math.pow(10, 4) // returns 10000 - * math.log1p(9999, 10) // returns 4 - * math.log1p(9999) / math.log(10) // returns 4 - * - * See also: - * - * exp, log, log2, log10 - * - * @param {number | BigNumber | Complex | Array | Matrix} x - * Value for which to calculate the logarithm of `x+1`. - * @param {number | BigNumber | Complex} [base=e] - * Optional base for the logarithm. If not provided, the natural - * logarithm of `x+1` is calculated. - * @return {number | BigNumber | Complex | Array | Matrix} - * Returns the logarithm of `x+1` - */ - return typed(log1p_name, { - number: function number(x) { - if (x >= -1 || config.predictable) { - return Object(utils_number["k" /* log1p */])(x); - } else { - // negative value -> complex value computation - return _log1pComplex(new Complex(x, 0)); - } - }, - Complex: _log1pComplex, - BigNumber: function BigNumber(x) { - var y = x.plus(1); - - if (!y.isNegative() || config.predictable) { - return y.ln(); - } else { - // downgrade to number, return Complex valued result - return _log1pComplex(new Complex(x.toNumber(), 0)); - } - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - }, - 'any, any': function anyAny(x, base) { - // calculate logarithm for a specified base, log1p(x, base) - return divideScalar(this(x), log(base)); - } - }); - /** - * Calculate the natural logarithm of a complex number + 1 - * @param {Complex} x - * @returns {Complex} - * @private - */ - - function _log1pComplex(x) { - var xRe1p = x.re + 1; - return new Complex(Math.log(Math.sqrt(xRe1p * xRe1p + x.im * x.im)), Math.atan2(x.im, xRe1p)); - } - }); - // CONCATENATED MODULE: ./src/function/arithmetic/nthRoots.js - - var nthRoots_name = 'nthRoots'; - var nthRoots_dependencies = ['config', 'typed', 'divideScalar', 'Complex']; - var createNthRoots = /* #__PURE__ */Object(factory["a" /* factory */])(nthRoots_name, nthRoots_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - divideScalar = _ref.divideScalar, - Complex = _ref.Complex; - - /** - * Each function here returns a real multiple of i as a Complex value. - * @param {number} val - * @return {Complex} val, i*val, -val or -i*val for index 0, 1, 2, 3 - */ - // This is used to fix float artifacts for zero-valued components. - var _calculateExactResult = [function realPos(val) { - return new Complex(val, 0); - }, function imagPos(val) { - return new Complex(0, val); - }, function realNeg(val) { - return new Complex(-val, 0); - }, function imagNeg(val) { - return new Complex(0, -val); - }]; - /** - * Calculate the nth root of a Complex Number a using De Movire's Theorem. - * @param {Complex} a - * @param {number} root - * @return {Array} array of n Complex Roots - */ - - function _nthComplexRoots(a, root) { - if (root < 0) { - throw new Error('Root must be greater than zero'); - } - if (root === 0) { - throw new Error('Root must be non-zero'); - } - if (root % 1 !== 0) { - throw new Error('Root must be an integer'); - } - if (a === 0 || a.abs() === 0) { - return [new Complex(0, 0)]; - } - var aIsNumeric = typeof a === 'number'; - var offset; // determine the offset (argument of a)/(pi/2) - - if (aIsNumeric || a.re === 0 || a.im === 0) { - if (aIsNumeric) { - offset = 2 * +(a < 0); // numeric value on the real axis - } else if (a.im === 0) { - offset = 2 * +(a.re < 0); // complex value on the real axis - } else { - offset = 2 * +(a.im < 0) + 1; // complex value on the imaginary axis - } - } - - var arg = a.arg(); - var abs = a.abs(); - var roots = []; - var r = Math.pow(abs, 1 / root); - - for (var k = 0; k < root; k++) { - var halfPiFactor = (offset + 4 * k) / root; - /** - * If (offset + 4*k)/root is an integral multiple of pi/2 - * then we can produce a more exact result. - */ - - if (halfPiFactor === Math.round(halfPiFactor)) { - roots.push(_calculateExactResult[halfPiFactor % 4](r)); - continue; - } - - roots.push(new Complex({ - r: r, - phi: (arg + 2 * Math.PI * k) / root - })); - } - - return roots; - } - /** - * Calculate the nth roots of a value. - * An nth root of a positive real number A, - * is a positive real solution of the equation "x^root = A". - * This function returns an array of complex values. - * - * Syntax: - * - * math.nthRoots(x) - * math.nthRoots(x, root) - * - * Examples: - * - * math.nthRoots(1) - * // returns [ - * // {re: 1, im: 0}, - * // {re: -1, im: 0} - * // ] - * nthRoots(1, 3) - * // returns [ - * // { re: 1, im: 0 }, - * // { re: -0.4999999999999998, im: 0.8660254037844387 }, - * // { re: -0.5000000000000004, im: -0.8660254037844385 } - * ] - * - * See also: - * - * nthRoot, pow, sqrt - * - * @param {number | BigNumber | Fraction | Complex} x Number to be rounded - * @return {number | BigNumber | Fraction | Complex} Rounded value - */ - - return typed(nthRoots_name, { - Complex: function Complex(x) { - return _nthComplexRoots(x, 2); - }, - 'Complex, number': _nthComplexRoots - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/dotPow.js - - var dotPow_name = 'dotPow'; - var dotPow_dependencies = ['typed', 'equalScalar', 'matrix', 'pow', 'DenseMatrix']; - var createDotPow = /* #__PURE__ */Object(factory["a" /* factory */])(dotPow_name, dotPow_dependencies, function (_ref) { - var typed = _ref.typed, - equalScalar = _ref.equalScalar, - matrix = _ref.matrix, - pow = _ref.pow, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm07 = createAlgorithm07({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Calculates the power of x to y element wise. - * - * Syntax: - * - * math.dotPow(x, y) - * - * Examples: - * - * math.dotPow(2, 3) // returns number 8 - * - * const a = [[1, 2], [4, 3]] - * math.dotPow(a, 2) // returns Array [[1, 4], [16, 9]] - * math.pow(a, 2) // returns Array [[9, 8], [16, 17]] - * - * See also: - * - * pow, sqrt, multiply - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x The base - * @param {number | BigNumber | Complex | Unit | Array | Matrix} y The exponent - * @return {number | BigNumber | Complex | Unit | Array | Matrix} The value of `x` to the power `y` - */ - - return typed(dotPow_name, { - 'any, any': pow, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm07(x, y, pow, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, pow, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, pow, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, pow); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/dotDivide.js - - var dotDivide_name = 'dotDivide'; - var dotDivide_dependencies = ['typed', 'matrix', 'equalScalar', 'divideScalar', 'DenseMatrix']; - var createDotDivide = /* #__PURE__ */Object(factory["a" /* factory */])(dotDivide_name, dotDivide_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - divideScalar = _ref.divideScalar, - DenseMatrix = _ref.DenseMatrix; - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm07 = createAlgorithm07({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Divide two matrices element wise. The function accepts both matrices and - * scalar values. - * - * Syntax: - * - * math.dotDivide(x, y) - * - * Examples: - * - * math.dotDivide(2, 4) // returns 0.5 - * - * a = [[9, 5], [6, 1]] - * b = [[3, 2], [5, 2]] - * - * math.dotDivide(a, b) // returns [[3, 2.5], [1.2, 0.5]] - * math.divide(a, b) // returns [[1.75, 0.75], [-1.75, 2.25]] - * - * See also: - * - * divide, multiply, dotMultiply - * - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Numerator - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Denominator - * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Quotient, `x ./ y` - */ - - return typed(dotDivide_name, { - 'any, any': divideScalar, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm07(x, y, divideScalar, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm02(y, x, divideScalar, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, divideScalar, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, divideScalar); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm11(x, y, divideScalar, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, divideScalar, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, divideScalar, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, divideScalar, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, divideScalar, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, divideScalar, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/algebra/solver/utils/solveValidation.js - - function createSolveValidation(_ref) { - var DenseMatrix = _ref.DenseMatrix; - - /** - * Validates matrix and column vector b for backward/forward substitution algorithms. - * - * @param {Matrix} m An N x N matrix - * @param {Array | Matrix} b A column vector - * @param {Boolean} copy Return a copy of vector b - * - * @return {DenseMatrix} Dense column vector b - */ - return function solveValidation(m, b, copy) { - var mSize = m.size(); - - if (mSize.length !== 2) { - throw new RangeError('Matrix must be two dimensional (size: ' + Object(utils_string["d" /* format */])(mSize) + ')'); - } - - var rows = mSize[0]; - var columns = mSize[1]; - - if (rows !== columns) { - throw new RangeError('Matrix must be square (size: ' + Object(utils_string["d" /* format */])(mSize) + ')'); - } - - var data = []; - - if (Object(is["v" /* isMatrix */])(b)) { - var bSize = b.size(); - var bdata = b._data; // 1-dim vector - - if (bSize.length === 1) { - if (bSize[0] !== rows) { - throw new RangeError('Dimension mismatch. Matrix columns must match vector length.'); - } - - for (var i = 0; i < rows; i++) { - data[i] = [bdata[i]]; - } - - return new DenseMatrix({ - data: data, - size: [rows, 1], - datatype: b._datatype - }); - } // 2-dim column - - if (bSize.length === 2) { - if (bSize[0] !== rows || bSize[1] !== 1) { - throw new RangeError('Dimension mismatch. Matrix columns must match vector length.'); - } - - if (Object(is["n" /* isDenseMatrix */])(b)) { - if (copy) { - data = []; - - for (var _i = 0; _i < rows; _i++) { - data[_i] = [bdata[_i][0]]; - } - - return new DenseMatrix({ - data: data, - size: [rows, 1], - datatype: b._datatype - }); - } - - return b; - } - - if (Object(is["H" /* isSparseMatrix */])(b)) { - for (var _i2 = 0; _i2 < rows; _i2++) { - data[_i2] = [0]; - } - - var values = b._values; - var index = b._index; - var ptr = b._ptr; - - for (var k1 = ptr[1], k = ptr[0]; k < k1; k++) { - var _i3 = index[k]; - data[_i3][0] = values[k]; - } - - return new DenseMatrix({ - data: data, - size: [rows, 1], - datatype: b._datatype - }); - } - } - - throw new RangeError('Dimension mismatch. The right side has to be either 1- or 2-dimensional vector.'); - } - - if (Object(is["b" /* isArray */])(b)) { - var bsize = Object(utils_array["a" /* arraySize */])(b); - - if (bsize.length === 1) { - if (bsize[0] !== rows) { - throw new RangeError('Dimension mismatch. Matrix columns must match vector length.'); - } - - for (var _i4 = 0; _i4 < rows; _i4++) { - data[_i4] = [b[_i4]]; - } - - return new DenseMatrix({ - data: data, - size: [rows, 1] - }); - } - - if (bsize.length === 2) { - if (bsize[0] !== rows || bsize[1] !== 1) { - throw new RangeError('Dimension mismatch. Matrix columns must match vector length.'); - } - - for (var _i5 = 0; _i5 < rows; _i5++) { - data[_i5] = [b[_i5][0]]; - } - - return new DenseMatrix({ - data: data, - size: [rows, 1] - }); - } - - throw new RangeError('Dimension mismatch. The right side has to be either 1- or 2-dimensional vector.'); - } - }; - } - // CONCATENATED MODULE: ./src/function/algebra/solver/lsolve.js - - var lsolve_name = 'lsolve'; - var lsolve_dependencies = ['typed', 'matrix', 'divideScalar', 'multiplyScalar', 'subtract', 'equalScalar', 'DenseMatrix']; - var createLsolve = /* #__PURE__ */Object(factory["a" /* factory */])(lsolve_name, lsolve_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - divideScalar = _ref.divideScalar, - multiplyScalar = _ref.multiplyScalar, - subtract = _ref.subtract, - equalScalar = _ref.equalScalar, - DenseMatrix = _ref.DenseMatrix; - var solveValidation = createSolveValidation({ - DenseMatrix: DenseMatrix - }); - /** - * Finds one solution of a linear equation system by forwards substitution. Matrix must be a lower triangular matrix. Throws an error if there's no solution. - * - * `L * x = b` - * - * Syntax: - * - * math.lsolve(L, b) - * - * Examples: - * - * const a = [[-2, 3], [2, 1]] - * const b = [11, 9] - * const x = lsolve(a, b) // [[-5.5], [20]] - * - * See also: - * - * lsolveAll, lup, slu, usolve, lusolve - * - * @param {Matrix, Array} L A N x N matrix or array (L) - * @param {Matrix, Array} b A column vector with the b values - * - * @return {DenseMatrix | Array} A column vector with the linear system solution (x) - */ - - return typed(lsolve_name, { - 'SparseMatrix, Array | Matrix': function SparseMatrixArrayMatrix(m, b) { - return _sparseForwardSubstitution(m, b); - }, - 'DenseMatrix, Array | Matrix': function DenseMatrixArrayMatrix(m, b) { - return _denseForwardSubstitution(m, b); - }, - 'Array, Array | Matrix': function ArrayArrayMatrix(a, b) { - var m = matrix(a); - - var r = _denseForwardSubstitution(m, b); - - return r.valueOf(); - } - }); - - function _denseForwardSubstitution(m, b) { - // validate matrix and vector, return copy of column vector b - b = solveValidation(m, b, true); - var bdata = b._data; - var rows = m._size[0]; - var columns = m._size[1]; // result - - var x = []; - var mdata = m._data; // loop columns - - for (var j = 0; j < columns; j++) { - var bj = bdata[j][0] || 0; - var xj = void 0; - - if (!equalScalar(bj, 0)) { - // non-degenerate row, find solution - var vjj = mdata[j][j]; - - if (equalScalar(vjj, 0)) { - throw new Error('Linear system cannot be solved since matrix is singular'); - } - - xj = divideScalar(bj, vjj); // loop rows - - for (var i = j + 1; i < rows; i++) { - bdata[i] = [subtract(bdata[i][0] || 0, multiplyScalar(xj, mdata[i][j]))]; - } - } else { - // degenerate row, we can choose any value - xj = 0; - } - - x[j] = [xj]; - } - - return new DenseMatrix({ - data: x, - size: [rows, 1] - }); - } - - function _sparseForwardSubstitution(m, b) { - // validate matrix and vector, return copy of column vector b - b = solveValidation(m, b, true); - var bdata = b._data; - var rows = m._size[0]; - var columns = m._size[1]; - var values = m._values; - var index = m._index; - var ptr = m._ptr; // result - - var x = []; // loop columns - - for (var j = 0; j < columns; j++) { - var bj = bdata[j][0] || 0; - - if (!equalScalar(bj, 0)) { - // non-degenerate row, find solution - var vjj = 0; // matrix values & indices (column j) - - var jValues = []; - var jIndices = []; // first and last index in the column - - var firstIndex = ptr[j]; - var lastIndex = ptr[j + 1]; // values in column, find value at [j, j] - - for (var k = firstIndex; k < lastIndex; k++) { - var i = index[k]; // check row (rows are not sorted!) - - if (i === j) { - vjj = values[k]; - } else if (i > j) { - // store lower triangular - jValues.push(values[k]); - jIndices.push(i); - } - } // at this point we must have a value in vjj - - if (equalScalar(vjj, 0)) { - throw new Error('Linear system cannot be solved since matrix is singular'); - } - - var xj = divideScalar(bj, vjj); - - for (var _k = 0, l = jIndices.length; _k < l; _k++) { - var _i = jIndices[_k]; - bdata[_i] = [subtract(bdata[_i][0] || 0, multiplyScalar(xj, jValues[_k]))]; - } - - x[j] = [xj]; - } else { - // degenerate row, we can choose any value - x[j] = [0]; - } - } - - return new DenseMatrix({ - data: x, - size: [rows, 1] - }); - } - }); - // CONCATENATED MODULE: ./src/function/algebra/solver/usolve.js - - var usolve_name = 'usolve'; - var usolve_dependencies = ['typed', 'matrix', 'divideScalar', 'multiplyScalar', 'subtract', 'equalScalar', 'DenseMatrix']; - var createUsolve = /* #__PURE__ */Object(factory["a" /* factory */])(usolve_name, usolve_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - divideScalar = _ref.divideScalar, - multiplyScalar = _ref.multiplyScalar, - subtract = _ref.subtract, - equalScalar = _ref.equalScalar, - DenseMatrix = _ref.DenseMatrix; - var solveValidation = createSolveValidation({ - DenseMatrix: DenseMatrix - }); - /** - * Finds one solution of a linear equation system by backward substitution. Matrix must be an upper triangular matrix. Throws an error if there's no solution. - * - * `U * x = b` - * - * Syntax: - * - * math.usolve(U, b) - * - * Examples: - * - * const a = [[-2, 3], [2, 1]] - * const b = [11, 9] - * const x = usolve(a, b) // [[8], [9]] - * - * See also: - * - * usolveAll, lup, slu, usolve, lusolve - * - * @param {Matrix, Array} U A N x N matrix or array (U) - * @param {Matrix, Array} b A column vector with the b values - * - * @return {DenseMatrix | Array} A column vector with the linear system solution (x) - */ - - return typed(usolve_name, { - 'SparseMatrix, Array | Matrix': function SparseMatrixArrayMatrix(m, b) { - return _sparseBackwardSubstitution(m, b); - }, - 'DenseMatrix, Array | Matrix': function DenseMatrixArrayMatrix(m, b) { - return _denseBackwardSubstitution(m, b); - }, - 'Array, Array | Matrix': function ArrayArrayMatrix(a, b) { - var m = matrix(a); - - var r = _denseBackwardSubstitution(m, b); - - return r.valueOf(); - } - }); - - function _denseBackwardSubstitution(m, b) { - // make b into a column vector - b = solveValidation(m, b, true); - var bdata = b._data; - var rows = m._size[0]; - var columns = m._size[1]; // result - - var x = []; - var mdata = m._data; // loop columns backwards - - for (var j = columns - 1; j >= 0; j--) { - // b[j] - var bj = bdata[j][0] || 0; // x[j] - - var xj = void 0; - - if (!equalScalar(bj, 0)) { - // value at [j, j] - var vjj = mdata[j][j]; - - if (equalScalar(vjj, 0)) { - // system cannot be solved - throw new Error('Linear system cannot be solved since matrix is singular'); - } - - xj = divideScalar(bj, vjj); // loop rows - - for (var i = j - 1; i >= 0; i--) { - // update copy of b - bdata[i] = [subtract(bdata[i][0] || 0, multiplyScalar(xj, mdata[i][j]))]; - } - } else { - // zero value at j - xj = 0; - } // update x - - x[j] = [xj]; - } - - return new DenseMatrix({ - data: x, - size: [rows, 1] - }); - } - - function _sparseBackwardSubstitution(m, b) { - // make b into a column vector - b = solveValidation(m, b, true); - var bdata = b._data; - var rows = m._size[0]; - var columns = m._size[1]; - var values = m._values; - var index = m._index; - var ptr = m._ptr; // result - - var x = []; // loop columns backwards - - for (var j = columns - 1; j >= 0; j--) { - var bj = bdata[j][0] || 0; - - if (!equalScalar(bj, 0)) { - // non-degenerate row, find solution - var vjj = 0; // upper triangular matrix values & index (column j) - - var jValues = []; - var jIndices = []; // first & last indeces in column - - var firstIndex = ptr[j]; - var lastIndex = ptr[j + 1]; // values in column, find value at [j, j], loop backwards - - for (var k = lastIndex - 1; k >= firstIndex; k--) { - var i = index[k]; // check row (rows are not sorted!) - - if (i === j) { - vjj = values[k]; - } else if (i < j) { - // store upper triangular - jValues.push(values[k]); - jIndices.push(i); - } - } // at this point we must have a value in vjj - - if (equalScalar(vjj, 0)) { - throw new Error('Linear system cannot be solved since matrix is singular'); - } - - var xj = divideScalar(bj, vjj); - - for (var _k = 0, _lastIndex = jIndices.length; _k < _lastIndex; _k++) { - var _i = jIndices[_k]; - bdata[_i] = [subtract(bdata[_i][0], multiplyScalar(xj, jValues[_k]))]; - } - - x[j] = [xj]; - } else { - // degenerate row, we can choose any value - x[j] = [0]; - } - } - - return new DenseMatrix({ - data: x, - size: [rows, 1] - }); - } - }); - // CONCATENATED MODULE: ./src/function/algebra/solver/lsolveAll.js - function _toConsumableArray(arr) { - return _arrayWithoutHoles(arr) || _iterableToArray(arr) || lsolveAll_unsupportedIterableToArray(arr) || _nonIterableSpread(); - } - - function _nonIterableSpread() { - throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); - } - - function lsolveAll_unsupportedIterableToArray(o, minLen) { - if (!o) { - return; - } if (typeof o === "string") { - return lsolveAll_arrayLikeToArray(o, minLen); - } var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) { - n = o.constructor.name; - } if (n === "Map" || n === "Set") { - return Array.from(o); - } if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) { - return lsolveAll_arrayLikeToArray(o, minLen); - } - } - - function _iterableToArray(iter) { - if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) { - return Array.from(iter); - } - } - - function _arrayWithoutHoles(arr) { - if (Array.isArray(arr)) { - return lsolveAll_arrayLikeToArray(arr); - } - } - - function lsolveAll_arrayLikeToArray(arr, len) { - if (len == null || len > arr.length) { - len = arr.length; - } for (var i = 0, arr2 = new Array(len); i < len; i++) { - arr2[i] = arr[i]; - } return arr2; - } - - var lsolveAll_name = 'lsolveAll'; - var lsolveAll_dependencies = ['typed', 'matrix', 'divideScalar', 'multiplyScalar', 'subtract', 'equalScalar', 'DenseMatrix']; - var createLsolveAll = /* #__PURE__ */Object(factory["a" /* factory */])(lsolveAll_name, lsolveAll_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - divideScalar = _ref.divideScalar, - multiplyScalar = _ref.multiplyScalar, - subtract = _ref.subtract, - equalScalar = _ref.equalScalar, - DenseMatrix = _ref.DenseMatrix; - var solveValidation = createSolveValidation({ - DenseMatrix: DenseMatrix - }); - /** - * Finds all solutions of a linear equation system by forwards substitution. Matrix must be a lower triangular matrix. - * - * `L * x = b` - * - * Syntax: - * - * math.lsolve(L, b) - * - * Examples: - * - * const a = [[-2, 3], [2, 1]] - * const b = [11, 9] - * const x = lsolve(a, b) // [ [[-5.5], [20]] ] - * - * See also: - * - * lsolve, lup, slu, usolve, lusolve - * - * @param {Matrix, Array} L A N x N matrix or array (L) - * @param {Matrix, Array} b A column vector with the b values - * - * @return {DenseMatrix[] | Array[]} An array of affine-independent column vectors (x) that solve the linear system - */ - - return typed(lsolveAll_name, { - 'SparseMatrix, Array | Matrix': function SparseMatrixArrayMatrix(m, b) { - return _sparseForwardSubstitution(m, b); - }, - 'DenseMatrix, Array | Matrix': function DenseMatrixArrayMatrix(m, b) { - return _denseForwardSubstitution(m, b); - }, - 'Array, Array | Matrix': function ArrayArrayMatrix(a, b) { - var m = matrix(a); - - var R = _denseForwardSubstitution(m, b); - - return R.map(function (r) { - return r.valueOf(); - }); - } - }); - - function _denseForwardSubstitution(m, b_) { - // the algorithm is derived from - // https://www.overleaf.com/project/5e6c87c554a3190001a3fc93 - // array of right-hand sides - var B = [solveValidation(m, b_, true)._data.map(function (e) { - return e[0]; - })]; - var M = m._data; - var rows = m._size[0]; - var columns = m._size[1]; // loop columns - - for (var i = 0; i < columns; i++) { - var L = B.length; // loop right-hand sides - - for (var k = 0; k < L; k++) { - var b = B[k]; - - if (!equalScalar(M[i][i], 0)) { - // non-singular row - b[i] = divideScalar(b[i], M[i][i]); - - for (var j = i + 1; j < columns; j++) { - // b[j] -= b[i] * M[j,i] - b[j] = subtract(b[j], multiplyScalar(b[i], M[j][i])); - } - } else if (!equalScalar(b[i], 0)) { - // singular row, nonzero RHS - if (k === 0) { - // There is no valid solution - return []; - } else { - // This RHS is invalid but other solutions may still exist - B.splice(k, 1); - k -= 1; - L -= 1; - } - } else if (k === 0) { - // singular row, RHS is zero - var bNew = _toConsumableArray(b); - - bNew[i] = 1; - - for (var _j = i + 1; _j < columns; _j++) { - bNew[_j] = subtract(bNew[_j], M[_j][i]); - } - - B.push(bNew); - } - } - } - - return B.map(function (x) { - return new DenseMatrix({ - data: x.map(function (e) { - return [e]; - }), - size: [rows, 1] - }); - }); - } - - function _sparseForwardSubstitution(m, b_) { - // array of right-hand sides - var B = [solveValidation(m, b_, true)._data.map(function (e) { - return e[0]; - })]; - var rows = m._size[0]; - var columns = m._size[1]; - var values = m._values; - var index = m._index; - var ptr = m._ptr; // loop columns - - for (var i = 0; i < columns; i++) { - var L = B.length; // loop right-hand sides - - for (var k = 0; k < L; k++) { - var b = B[k]; // values & indices (column i) - - var iValues = []; - var iIndices = []; // first & last indeces in column - - var firstIndex = ptr[i]; - var lastIndex = ptr[i + 1]; // find the value at [i, i] - - var Mii = 0; - - for (var j = firstIndex; j < lastIndex; j++) { - var J = index[j]; // check row - - if (J === i) { - Mii = values[j]; - } else if (J > i) { - // store lower triangular - iValues.push(values[j]); - iIndices.push(J); - } - } - - if (!equalScalar(Mii, 0)) { - // non-singular row - b[i] = divideScalar(b[i], Mii); - - for (var _j2 = 0, _lastIndex = iIndices.length; _j2 < _lastIndex; _j2++) { - var _J = iIndices[_j2]; - b[_J] = subtract(b[_J], multiplyScalar(b[i], iValues[_j2])); - } - } else if (!equalScalar(b[i], 0)) { - // singular row, nonzero RHS - if (k === 0) { - // There is no valid solution - return []; - } else { - // This RHS is invalid but other solutions may still exist - B.splice(k, 1); - k -= 1; - L -= 1; - } - } else if (k === 0) { - // singular row, RHS is zero - var bNew = _toConsumableArray(b); - - bNew[i] = 1; - - for (var _j3 = 0, _lastIndex2 = iIndices.length; _j3 < _lastIndex2; _j3++) { - var _J2 = iIndices[_j3]; - bNew[_J2] = subtract(bNew[_J2], iValues[_j3]); - } - - B.push(bNew); - } - } - } - - return B.map(function (x) { - return new DenseMatrix({ - data: x.map(function (e) { - return [e]; - }), - size: [rows, 1] - }); - }); - } - }); - // CONCATENATED MODULE: ./src/function/algebra/solver/usolveAll.js - function usolveAll_toConsumableArray(arr) { - return usolveAll_arrayWithoutHoles(arr) || usolveAll_iterableToArray(arr) || usolveAll_unsupportedIterableToArray(arr) || usolveAll_nonIterableSpread(); - } - - function usolveAll_nonIterableSpread() { - throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); - } - - function usolveAll_unsupportedIterableToArray(o, minLen) { - if (!o) { - return; - } if (typeof o === "string") { - return usolveAll_arrayLikeToArray(o, minLen); - } var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) { - n = o.constructor.name; - } if (n === "Map" || n === "Set") { - return Array.from(o); - } if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) { - return usolveAll_arrayLikeToArray(o, minLen); - } - } - - function usolveAll_iterableToArray(iter) { - if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) { - return Array.from(iter); - } - } - - function usolveAll_arrayWithoutHoles(arr) { - if (Array.isArray(arr)) { - return usolveAll_arrayLikeToArray(arr); - } - } - - function usolveAll_arrayLikeToArray(arr, len) { - if (len == null || len > arr.length) { - len = arr.length; - } for (var i = 0, arr2 = new Array(len); i < len; i++) { - arr2[i] = arr[i]; - } return arr2; - } - - var usolveAll_name = 'usolveAll'; - var usolveAll_dependencies = ['typed', 'matrix', 'divideScalar', 'multiplyScalar', 'subtract', 'equalScalar', 'DenseMatrix']; - var createUsolveAll = /* #__PURE__ */Object(factory["a" /* factory */])(usolveAll_name, usolveAll_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - divideScalar = _ref.divideScalar, - multiplyScalar = _ref.multiplyScalar, - subtract = _ref.subtract, - equalScalar = _ref.equalScalar, - DenseMatrix = _ref.DenseMatrix; - var solveValidation = createSolveValidation({ - DenseMatrix: DenseMatrix - }); - /** - * Finds all solutions of a linear equation system by backward substitution. Matrix must be an upper triangular matrix. - * - * `U * x = b` - * - * Syntax: - * - * math.usolve(U, b) - * - * Examples: - * - * const a = [[-2, 3], [2, 1]] - * const b = [11, 9] - * const x = usolve(a, b) // [ [[8], [9]] ] - * - * See also: - * - * usolve, lup, slu, usolve, lusolve - * - * @param {Matrix, Array} U A N x N matrix or array (U) - * @param {Matrix, Array} b A column vector with the b values - * - * @return {DenseMatrix[] | Array[]} An array of affine-independent column vectors (x) that solve the linear system - */ - - return typed(usolveAll_name, { - 'SparseMatrix, Array | Matrix': function SparseMatrixArrayMatrix(m, b) { - return _sparseBackwardSubstitution(m, b); - }, - 'DenseMatrix, Array | Matrix': function DenseMatrixArrayMatrix(m, b) { - return _denseBackwardSubstitution(m, b); - }, - 'Array, Array | Matrix': function ArrayArrayMatrix(a, b) { - var m = matrix(a); - - var R = _denseBackwardSubstitution(m, b); - - return R.map(function (r) { - return r.valueOf(); - }); - } - }); - - function _denseBackwardSubstitution(m, b_) { - // the algorithm is derived from - // https://www.overleaf.com/project/5e6c87c554a3190001a3fc93 - // array of right-hand sides - var B = [solveValidation(m, b_, true)._data.map(function (e) { - return e[0]; - })]; - var M = m._data; - var rows = m._size[0]; - var columns = m._size[1]; // loop columns backwards - - for (var i = columns - 1; i >= 0; i--) { - var L = B.length; // loop right-hand sides - - for (var k = 0; k < L; k++) { - var b = B[k]; - - if (!equalScalar(M[i][i], 0)) { - // non-singular row - b[i] = divideScalar(b[i], M[i][i]); - - for (var j = i - 1; j >= 0; j--) { - // b[j] -= b[i] * M[j,i] - b[j] = subtract(b[j], multiplyScalar(b[i], M[j][i])); - } - } else if (!equalScalar(b[i], 0)) { - // singular row, nonzero RHS - if (k === 0) { - // There is no valid solution - return []; - } else { - // This RHS is invalid but other solutions may still exist - B.splice(k, 1); - k -= 1; - L -= 1; - } - } else if (k === 0) { - // singular row, RHS is zero - var bNew = usolveAll_toConsumableArray(b); - - bNew[i] = 1; - - for (var _j = i - 1; _j >= 0; _j--) { - bNew[_j] = subtract(bNew[_j], M[_j][i]); - } - - B.push(bNew); - } - } - } - - return B.map(function (x) { - return new DenseMatrix({ - data: x.map(function (e) { - return [e]; - }), - size: [rows, 1] - }); - }); - } - - function _sparseBackwardSubstitution(m, b_) { - // array of right-hand sides - var B = [solveValidation(m, b_, true)._data.map(function (e) { - return e[0]; - })]; - var rows = m._size[0]; - var columns = m._size[1]; - var values = m._values; - var index = m._index; - var ptr = m._ptr; // loop columns backwards - - for (var i = columns - 1; i >= 0; i--) { - var L = B.length; // loop right-hand sides - - for (var k = 0; k < L; k++) { - var b = B[k]; // values & indices (column i) - - var iValues = []; - var iIndices = []; // first & last indeces in column - - var firstIndex = ptr[i]; - var lastIndex = ptr[i + 1]; // find the value at [i, i] - - var Mii = 0; - - for (var j = lastIndex - 1; j >= firstIndex; j--) { - var J = index[j]; // check row - - if (J === i) { - Mii = values[j]; - } else if (J < i) { - // store upper triangular - iValues.push(values[j]); - iIndices.push(J); - } - } - - if (!equalScalar(Mii, 0)) { - // non-singular row - b[i] = divideScalar(b[i], Mii); // loop upper triangular - - for (var _j2 = 0, _lastIndex = iIndices.length; _j2 < _lastIndex; _j2++) { - var _J = iIndices[_j2]; - b[_J] = subtract(b[_J], multiplyScalar(b[i], iValues[_j2])); - } - } else if (!equalScalar(b[i], 0)) { - // singular row, nonzero RHS - if (k === 0) { - // There is no valid solution - return []; - } else { - // This RHS is invalid but other solutions may still exist - B.splice(k, 1); - k -= 1; - L -= 1; - } - } else if (k === 0) { - // singular row, RHS is zero - var bNew = usolveAll_toConsumableArray(b); - - bNew[i] = 1; // loop upper triangular - - for (var _j3 = 0, _lastIndex2 = iIndices.length; _j3 < _lastIndex2; _j3++) { - var _J2 = iIndices[_j3]; - bNew[_J2] = subtract(bNew[_J2], iValues[_j3]); - } - - B.push(bNew); - } - } - } - - return B.map(function (x) { - return new DenseMatrix({ - data: x.map(function (e) { - return [e]; - }), - size: [rows, 1] - }); - }); - } - }); - // CONCATENATED MODULE: ./src/type/matrix/utils/algorithm08.js - - var algorithm08_name = 'algorithm08'; - var algorithm08_dependencies = ['typed', 'equalScalar']; - var createAlgorithm08 = /* #__PURE__ */Object(factory["a" /* factory */])(algorithm08_name, algorithm08_dependencies, function (_ref) { - var typed = _ref.typed, - equalScalar = _ref.equalScalar; - - /** - * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij). - * Callback function invoked MAX(NNZA, NNZB) times - * - * - * ┌ f(Aij, Bij) ; A(i,j) !== 0 && B(i,j) !== 0 - * C(i,j) = ┤ A(i,j) ; A(i,j) !== 0 - * └ 0 ; otherwise - * - * - * @param {Matrix} a The SparseMatrix instance (A) - * @param {Matrix} b The SparseMatrix instance (B) - * @param {Function} callback The f(Aij,Bij) operation to invoke - * - * @return {Matrix} SparseMatrix (C) - * - * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 - */ - return function algorithm08(a, b, callback) { - // sparse matrix arrays - var avalues = a._values; - var aindex = a._index; - var aptr = a._ptr; - var asize = a._size; - var adt = a._datatype; // sparse matrix arrays - - var bvalues = b._values; - var bindex = b._index; - var bptr = b._ptr; - var bsize = b._size; - var bdt = b._datatype; // validate dimensions - - if (asize.length !== bsize.length) { - throw new DimensionError["a" /* DimensionError */](asize.length, bsize.length); - } // check rows & columns - - if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) { - throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); - } // sparse matrix cannot be a Pattern matrix - - if (!avalues || !bvalues) { - throw new Error('Cannot perform operation on Pattern Sparse Matrices'); - } // rows & columns - - var rows = asize[0]; - var columns = asize[1]; // datatype - - var dt; // equal signature to use - - var eq = equalScalar; // zero value - - var zero = 0; // callback signature to use - - var cf = callback; // process data types - - if (typeof adt === 'string' && adt === bdt) { - // datatype - dt = adt; // find signature that matches (dt, dt) - - eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype - - zero = typed.convert(0, dt); // callback - - cf = typed.find(callback, [dt, dt]); - } // result arrays - - var cvalues = []; - var cindex = []; - var cptr = []; // workspace - - var x = []; // marks indicating we have a value in x for a given column - - var w = []; // vars - - var k, k0, k1, i; // loop columns - - for (var j = 0; j < columns; j++) { - // update cptr - cptr[j] = cindex.length; // columns mark - - var mark = j + 1; // loop values in a - - for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { - // row - i = aindex[k]; // mark workspace - - w[i] = mark; // set value - - x[i] = avalues[k]; // add index - - cindex.push(i); - } // loop values in b - - for (k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { - // row - i = bindex[k]; // check value exists in workspace - - if (w[i] === mark) { - // evaluate callback - x[i] = cf(x[i], bvalues[k]); - } - } // initialize first index in j - - k = cptr[j]; // loop index in j - - while (k < cindex.length) { - // row - i = cindex[k]; // value @ i - - var v = x[i]; // check for zero value - - if (!eq(v, zero)) { - // push value - cvalues.push(v); // increment pointer - - k++; - } else { - // remove value @ i, do not increment pointer - cindex.splice(k, 1); - } - } - } // update cptr - - cptr[columns] = cindex.length; // return sparse matrix - - return a.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [rows, columns], - datatype: dt - }); - }; - }); - // CONCATENATED MODULE: ./src/function/bitwise/leftShift.js - - var leftShift_name = 'leftShift'; - var leftShift_dependencies = ['typed', 'matrix', 'equalScalar', 'zeros', 'DenseMatrix']; - var createLeftShift = /* #__PURE__ */Object(factory["a" /* factory */])(leftShift_name, leftShift_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - zeros = _ref.zeros, - DenseMatrix = _ref.DenseMatrix; - var algorithm01 = createAlgorithm01({ - typed: typed - }); - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm08 = createAlgorithm08({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm10 = createAlgorithm10({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Bitwise left logical shift of a value x by y number of bits, `x << y`. - * For matrices, the function is evaluated element wise. - * For units, the function is evaluated on the best prefix base. - * - * Syntax: - * - * math.leftShift(x, y) - * - * Examples: - * - * math.leftShift(1, 2) // returns number 4 - * - * math.leftShift([1, 2, 3], 4) // returns Array [16, 32, 64] - * - * See also: - * - * leftShift, bitNot, bitOr, bitXor, rightArithShift, rightLogShift - * - * @param {number | BigNumber | Array | Matrix} x Value to be shifted - * @param {number | BigNumber} y Amount of shifts - * @return {number | BigNumber | Array | Matrix} `x` shifted left `y` times - */ - - return typed(leftShift_name, { - 'number, number': leftShiftNumber, - 'BigNumber, BigNumber': leftShiftBigNumber, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm08(x, y, this, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm02(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm01(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { - // check scalar - if (equalScalar(y, 0)) { - return x.clone(); - } - - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { - // check scalar - if (equalScalar(y, 0)) { - return x.clone(); - } - - return algorithm14(x, y, this, false); - }, - 'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) { - // check scalar - if (equalScalar(x, 0)) { - return zeros(y.size(), y.storage()); - } - - return algorithm10(y, x, this, true); - }, - 'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) { - // check scalar - if (equalScalar(x, 0)) { - return zeros(y.size(), y.storage()); - } - - return algorithm14(y, x, this, true); - }, - 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) { - // use matrix implementation - return this(matrix(x), y).valueOf(); - }, - 'number | BigNumber, Array': function numberBigNumberArray(x, y) { - // use matrix implementation - return this(x, matrix(y)).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/bitwise/rightArithShift.js - - var rightArithShift_name = 'rightArithShift'; - var rightArithShift_dependencies = ['typed', 'matrix', 'equalScalar', 'zeros', 'DenseMatrix']; - var createRightArithShift = /* #__PURE__ */Object(factory["a" /* factory */])(rightArithShift_name, rightArithShift_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - zeros = _ref.zeros, - DenseMatrix = _ref.DenseMatrix; - var algorithm01 = createAlgorithm01({ - typed: typed - }); - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm08 = createAlgorithm08({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm10 = createAlgorithm10({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Bitwise right arithmetic shift of a value x by y number of bits, `x >> y`. - * For matrices, the function is evaluated element wise. - * For units, the function is evaluated on the best prefix base. - * - * Syntax: - * - * math.rightArithShift(x, y) - * - * Examples: - * - * math.rightArithShift(4, 2) // returns number 1 - * - * math.rightArithShift([16, -32, 64], 4) // returns Array [1, -2, 3] - * - * See also: - * - * bitAnd, bitNot, bitOr, bitXor, rightArithShift, rightLogShift - * - * @param {number | BigNumber | Array | Matrix} x Value to be shifted - * @param {number | BigNumber} y Amount of shifts - * @return {number | BigNumber | Array | Matrix} `x` sign-filled shifted right `y` times - */ - - return typed(rightArithShift_name, { - 'number, number': rightArithShiftNumber, - 'BigNumber, BigNumber': rightArithShiftBigNumber, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm08(x, y, this, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm02(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm01(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { - // check scalar - if (equalScalar(y, 0)) { - return x.clone(); - } - - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { - // check scalar - if (equalScalar(y, 0)) { - return x.clone(); - } - - return algorithm14(x, y, this, false); - }, - 'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) { - // check scalar - if (equalScalar(x, 0)) { - return zeros(y.size(), y.storage()); - } - - return algorithm10(y, x, this, true); - }, - 'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) { - // check scalar - if (equalScalar(x, 0)) { - return zeros(y.size(), y.storage()); - } - - return algorithm14(y, x, this, true); - }, - 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) { - // use matrix implementation - return this(matrix(x), y).valueOf(); - }, - 'number | BigNumber, Array': function numberBigNumberArray(x, y) { - // use matrix implementation - return this(x, matrix(y)).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/bitwise/rightLogShift.js - - var rightLogShift_name = 'rightLogShift'; - var rightLogShift_dependencies = ['typed', 'matrix', 'equalScalar', 'zeros', 'DenseMatrix']; - var createRightLogShift = /* #__PURE__ */Object(factory["a" /* factory */])(rightLogShift_name, rightLogShift_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - zeros = _ref.zeros, - DenseMatrix = _ref.DenseMatrix; - var algorithm01 = createAlgorithm01({ - typed: typed - }); - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm08 = createAlgorithm08({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm10 = createAlgorithm10({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Bitwise right logical shift of value x by y number of bits, `x >>> y`. - * For matrices, the function is evaluated element wise. - * For units, the function is evaluated on the best prefix base. - * - * Syntax: - * - * math.rightLogShift(x, y) - * - * Examples: - * - * math.rightLogShift(4, 2) // returns number 1 - * - * math.rightLogShift([16, -32, 64], 4) // returns Array [1, 2, 3] - * - * See also: - * - * bitAnd, bitNot, bitOr, bitXor, leftShift, rightLogShift - * - * @param {number | Array | Matrix} x Value to be shifted - * @param {number} y Amount of shifts - * @return {number | Array | Matrix} `x` zero-filled shifted right `y` times - */ - - return typed(rightLogShift_name, { - 'number, number': rightLogShiftNumber, - // 'BigNumber, BigNumber': ..., // TODO: implement BigNumber support for rightLogShift - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm08(x, y, this, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm02(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm01(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { - // check scalar - if (equalScalar(y, 0)) { - return x.clone(); - } - - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { - // check scalar - if (equalScalar(y, 0)) { - return x.clone(); - } - - return algorithm14(x, y, this, false); - }, - 'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) { - // check scalar - if (equalScalar(x, 0)) { - return zeros(y.size(), y.storage()); - } - - return algorithm10(y, x, this, true); - }, - 'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) { - // check scalar - if (equalScalar(x, 0)) { - return zeros(y.size(), y.storage()); - } - - return algorithm14(y, x, this, true); - }, - 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) { - // use matrix implementation - return this(matrix(x), y).valueOf(); - }, - 'number | BigNumber, Array': function numberBigNumberArray(x, y) { - // use matrix implementation - return this(x, matrix(y)).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/logical/and.js - - var and_name = 'and'; - var and_dependencies = ['typed', 'matrix', 'equalScalar', 'zeros', 'not']; - var createAnd = /* #__PURE__ */Object(factory["a" /* factory */])(and_name, and_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - zeros = _ref.zeros, - not = _ref.not; - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm06 = createAlgorithm06({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Logical `and`. Test whether two values are both defined with a nonzero/nonempty value. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.and(x, y) - * - * Examples: - * - * math.and(2, 4) // returns true - * - * a = [2, 0, 0] - * b = [3, 7, 0] - * c = 0 - * - * math.and(a, b) // returns [true, false, false] - * math.and(a, c) // returns [false, false, false] - * - * See also: - * - * not, or, xor - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check - * @param {number | BigNumber | Complex | Unit | Array | Matrix} y Second value to check - * @return {boolean | Array | Matrix} - * Returns true when both inputs are defined with a nonzero/nonempty value. - */ - - return typed(and_name, { - 'number, number': andNumber, - 'Complex, Complex': function ComplexComplex(x, y) { - return (x.re !== 0 || x.im !== 0) && (y.re !== 0 || y.im !== 0); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return !x.isZero() && !y.isZero() && !x.isNaN() && !y.isNaN(); - }, - 'Unit, Unit': function UnitUnit(x, y) { - return this(x.value || 0, y.value || 0); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm06(x, y, this, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm02(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm02(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - // check scalar - if (not(y)) { - // return zero matrix - return zeros(x.size(), x.storage()); - } - - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - // check scalar - if (not(y)) { - // return zero matrix - return zeros(x.size(), x.storage()); - } - - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - // check scalar - if (not(x)) { - // return zero matrix - return zeros(x.size(), x.storage()); - } - - return algorithm11(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - // check scalar - if (not(x)) { - // return zero matrix - return zeros(x.size(), x.storage()); - } - - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return this(matrix(x), y).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return this(x, matrix(y)).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/relational/compare.js - - var compare_name = 'compare'; - var compare_dependencies = ['typed', 'config', 'matrix', 'equalScalar', 'BigNumber', 'Fraction', 'DenseMatrix']; - var createCompare = /* #__PURE__ */Object(factory["a" /* factory */])(compare_name, compare_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - equalScalar = _ref.equalScalar, - matrix = _ref.matrix, - BigNumber = _ref.BigNumber, - Fraction = _ref.Fraction, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm05 = createAlgorithm05({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y. - * - * x and y are considered equal when the relative difference between x and y - * is smaller than the configured epsilon. The function cannot be used to - * compare values smaller than approximately 2.22e-16. - * - * For matrices, the function is evaluated element wise. - * Strings are compared by their numerical value. - * - * Syntax: - * - * math.compare(x, y) - * - * Examples: - * - * math.compare(6, 1) // returns 1 - * math.compare(2, 3) // returns -1 - * math.compare(7, 7) // returns 0 - * math.compare('10', '2') // returns 1 - * math.compare('1000', '1e3') // returns 0 - * - * const a = math.unit('5 cm') - * const b = math.unit('40 mm') - * math.compare(a, b) // returns 1 - * - * math.compare(2, [1, 2, 3]) // returns [1, 0, -1] - * - * See also: - * - * equal, unequal, smaller, smallerEq, larger, largerEq, compareNatural, compareText - * - * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} x First value to compare - * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} y Second value to compare - * @return {number | BigNumber | Fraction | Array | Matrix} Returns the result of the comparison: - * 1 when x > y, -1 when x < y, and 0 when x == y. - */ - - return typed(compare_name, { - 'boolean, boolean': function booleanBoolean(x, y) { - return x === y ? 0 : x > y ? 1 : -1; - }, - 'number, number': function numberNumber(x, y) { - return Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon) ? 0 : x > y ? 1 : -1; - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return nearlyEqual(x, y, config.epsilon) ? new BigNumber(0) : new BigNumber(x.cmp(y)); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - return new Fraction(x.compare(y)); - }, - 'Complex, Complex': function ComplexComplex() { - throw new TypeError('No ordering relation is defined for complex numbers'); - }, - 'Unit, Unit': function UnitUnit(x, y) { - if (!x.equalBase(y)) { - throw new Error('Cannot compare units with different base'); - } - - return this(x.value, y.value); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm05(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm12(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - var createCompareNumber = /* #__PURE__ */Object(factory["a" /* factory */])(compare_name, ['typed', 'config'], function (_ref2) { - var typed = _ref2.typed, - config = _ref2.config; - return typed(compare_name, { - 'number, number': function numberNumber(x, y) { - return Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon) ? 0 : x > y ? 1 : -1; - } - }); - }); - // EXTERNAL MODULE: ./node_modules/javascript-natural-sort/naturalSort.js - var naturalSort = __webpack_require__(11); - var naturalSort_default = /*#__PURE__*/__webpack_require__.n(naturalSort); - - // CONCATENATED MODULE: ./src/function/relational/compareNatural.js - - var compareNatural_name = 'compareNatural'; - var compareNatural_dependencies = ['typed', 'compare']; - var createCompareNatural = /* #__PURE__ */Object(factory["a" /* factory */])(compareNatural_name, compareNatural_dependencies, function (_ref) { - var typed = _ref.typed, - compare = _ref.compare; - var compareBooleans = compare.signatures['boolean,boolean']; - /** - * Compare two values of any type in a deterministic, natural way. - * - * For numeric values, the function works the same as `math.compare`. - * For types of values that can't be compared mathematically, - * the function compares in a natural way. - * - * For numeric values, x and y are considered equal when the relative - * difference between x and y is smaller than the configured epsilon. - * The function cannot be used to compare values smaller than - * approximately 2.22e-16. - * - * For Complex numbers, first the real parts are compared. If equal, - * the imaginary parts are compared. - * - * Strings are compared with a natural sorting algorithm, which - * orders strings in a "logic" way following some heuristics. - * This differs from the function `compare`, which converts the string - * into a numeric value and compares that. The function `compareText` - * on the other hand compares text lexically. - * - * Arrays and Matrices are compared value by value until there is an - * unequal pair of values encountered. Objects are compared by sorted - * keys until the keys or their values are unequal. - * - * Syntax: - * - * math.compareNatural(x, y) - * - * Examples: - * - * math.compareNatural(6, 1) // returns 1 - * math.compareNatural(2, 3) // returns -1 - * math.compareNatural(7, 7) // returns 0 - * - * math.compareNatural('10', '2') // returns 1 - * math.compareText('10', '2') // returns -1 - * math.compare('10', '2') // returns 1 - * - * math.compareNatural('Answer: 10', 'Answer: 2') // returns 1 - * math.compareText('Answer: 10', 'Answer: 2') // returns -1 - * math.compare('Answer: 10', 'Answer: 2') - * // Error: Cannot convert "Answer: 10" to a number - * - * const a = math.unit('5 cm') - * const b = math.unit('40 mm') - * math.compareNatural(a, b) // returns 1 - * - * const c = math.complex('2 + 3i') - * const d = math.complex('2 + 4i') - * math.compareNatural(c, d) // returns -1 - * - * math.compareNatural([1, 2, 4], [1, 2, 3]) // returns 1 - * math.compareNatural([1, 2, 3], [1, 2]) // returns 1 - * math.compareNatural([1, 5], [1, 2, 3]) // returns 1 - * math.compareNatural([1, 2], [1, 2]) // returns 0 - * - * math.compareNatural({a: 2}, {a: 4}) // returns -1 - * - * See also: - * - * compare, compareText - * - * @param {*} x First value to compare - * @param {*} y Second value to compare - * @return {number} Returns the result of the comparison: - * 1 when x > y, -1 when x < y, and 0 when x == y. - */ - - return typed(compareNatural_name, { - 'any, any': function anyAny(x, y) { - var typeX = Object(is["M" /* typeOf */])(x); - var typeY = Object(is["M" /* typeOf */])(y); - var c; // numeric types - - if ((typeX === 'number' || typeX === 'BigNumber' || typeX === 'Fraction') && (typeY === 'number' || typeY === 'BigNumber' || typeY === 'Fraction')) { - c = compare(x, y); - - if (c.toString() !== '0') { - // c can be number, BigNumber, or Fraction - return c > 0 ? 1 : -1; // return a number - } else { - return naturalSort_default()(typeX, typeY); - } - } // matrix types - - if (typeX === 'Array' || typeX === 'Matrix' || typeY === 'Array' || typeY === 'Matrix') { - c = compareMatricesAndArrays(this, x, y); - - if (c !== 0) { - return c; - } else { - return naturalSort_default()(typeX, typeY); - } - } // in case of different types, order by name of type, i.e. 'BigNumber' < 'Complex' - - if (typeX !== typeY) { - return naturalSort_default()(typeX, typeY); - } - - if (typeX === 'Complex') { - return compareComplexNumbers(x, y); - } - - if (typeX === 'Unit') { - if (x.equalBase(y)) { - return this(x.value, y.value); - } // compare by units - - return compareArrays(this, x.formatUnits(), y.formatUnits()); - } - - if (typeX === 'boolean') { - return compareBooleans(x, y); - } - - if (typeX === 'string') { - return naturalSort_default()(x, y); - } - - if (typeX === 'Object') { - return compareObjects(this, x, y); - } - - if (typeX === 'null') { - return 0; - } - - if (typeX === 'undefined') { - return 0; - } // this should not occur... - - throw new TypeError('Unsupported type of value "' + typeX + '"'); - } - }); - /** - * Compare mixed matrix/array types, by converting to same-shaped array. - * This comparator is non-deterministic regarding input types. - * @param {Array | SparseMatrix | DenseMatrix | *} x - * @param {Array | SparseMatrix | DenseMatrix | *} y - * @returns {number} Returns the comparison result: -1, 0, or 1 - */ - - function compareMatricesAndArrays(compareNatural, x, y) { - if (Object(is["H" /* isSparseMatrix */])(x) && Object(is["H" /* isSparseMatrix */])(y)) { - return compareArrays(compareNatural, x.toJSON().values, y.toJSON().values); - } - - if (Object(is["H" /* isSparseMatrix */])(x)) { - // note: convert to array is expensive - return compareMatricesAndArrays(compareNatural, x.toArray(), y); - } - - if (Object(is["H" /* isSparseMatrix */])(y)) { - // note: convert to array is expensive - return compareMatricesAndArrays(compareNatural, x, y.toArray()); - } // convert DenseArray into Array - - if (Object(is["n" /* isDenseMatrix */])(x)) { - return compareMatricesAndArrays(compareNatural, x.toJSON().data, y); - } - - if (Object(is["n" /* isDenseMatrix */])(y)) { - return compareMatricesAndArrays(compareNatural, x, y.toJSON().data); - } // convert scalars to array - - if (!Array.isArray(x)) { - return compareMatricesAndArrays(compareNatural, [x], y); - } - - if (!Array.isArray(y)) { - return compareMatricesAndArrays(compareNatural, x, [y]); - } - - return compareArrays(compareNatural, x, y); - } - /** - * Compare two Arrays - * - * - First, compares value by value - * - Next, if all corresponding values are equal, - * look at the length: longest array will be considered largest - * - * @param {Array} x - * @param {Array} y - * @returns {number} Returns the comparison result: -1, 0, or 1 - */ - - function compareArrays(compareNatural, x, y) { - // compare each value - for (var i = 0, ii = Math.min(x.length, y.length); i < ii; i++) { - var v = compareNatural(x[i], y[i]); - - if (v !== 0) { - return v; - } - } // compare the size of the arrays - - if (x.length > y.length) { - return 1; - } - - if (x.length < y.length) { - return -1; - } // both Arrays have equal size and content - - return 0; - } - /** - * Compare two objects - * - * - First, compare sorted property names - * - Next, compare the property values - * - * @param {Object} x - * @param {Object} y - * @returns {number} Returns the comparison result: -1, 0, or 1 - */ - - function compareObjects(compareNatural, x, y) { - var keysX = Object.keys(x); - var keysY = Object.keys(y); // compare keys - - keysX.sort(naturalSort_default.a); - keysY.sort(naturalSort_default.a); - var c = compareArrays(compareNatural, keysX, keysY); - - if (c !== 0) { - return c; - } // compare values - - for (var i = 0; i < keysX.length; i++) { - var v = compareNatural(x[keysX[i]], y[keysY[i]]); - - if (v !== 0) { - return v; - } - } - - return 0; - } - }); - /** - * Compare two complex numbers, `x` and `y`: - * - * - First, compare the real values of `x` and `y` - * - If equal, compare the imaginary values of `x` and `y` - * - * @params {Complex} x - * @params {Complex} y - * @returns {number} Returns the comparison result: -1, 0, or 1 - */ - - function compareComplexNumbers(x, y) { - if (x.re > y.re) { - return 1; - } - - if (x.re < y.re) { - return -1; - } - - if (x.im > y.im) { - return 1; - } - - if (x.im < y.im) { - return -1; - } - - return 0; - } - // CONCATENATED MODULE: ./src/function/relational/compareText.js - - var compareText_name = 'compareText'; - var compareText_dependencies = ['typed', 'matrix']; - var createCompareText = /* #__PURE__ */Object(factory["a" /* factory */])(compareText_name, compareText_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix; - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Compare two strings lexically. Comparison is case sensitive. - * Returns 1 when x > y, -1 when x < y, and 0 when x == y. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.compareText(x, y) - * - * Examples: - * - * math.compareText('B', 'A') // returns 1 - * math.compareText('2', '10') // returns 1 - * math.compare('2', '10') // returns -1 - * math.compareNatural('2', '10') // returns -1 - * - * math.compareText('B', ['A', 'B', 'C']) // returns [1, 0, -1] - * - * See also: - * - * equal, equalText, compare, compareNatural - * - * @param {string | Array | DenseMatrix} x First string to compare - * @param {string | Array | DenseMatrix} y Second string to compare - * @return {number | Array | DenseMatrix} Returns the result of the comparison: - * 1 when x > y, -1 when x < y, and 0 when x == y. - */ - - return typed(compareText_name, { - 'any, any': utils_string["a" /* compareText */], - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, utils_string["a" /* compareText */]); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, utils_string["a" /* compareText */], false); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, utils_string["a" /* compareText */], true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, utils_string["a" /* compareText */], false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, utils_string["a" /* compareText */], true).valueOf(); - } - }); - }); - var createCompareTextNumber = /* #__PURE__ */Object(factory["a" /* factory */])(compareText_name, ['typed'], function (_ref2) { - var typed = _ref2.typed; - return typed(compareText_name, { - 'any, any': utils_string["a" /* compareText */] - }); - }); - // CONCATENATED MODULE: ./src/function/relational/equal.js - - var equal_name = 'equal'; - var equal_dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix']; - var createEqual = /* #__PURE__ */Object(factory["a" /* factory */])(equal_name, equal_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm07 = createAlgorithm07({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Test whether two values are equal. - * - * The function tests whether the relative difference between x and y is - * smaller than the configured epsilon. The function cannot be used to - * compare values smaller than approximately 2.22e-16. - * - * For matrices, the function is evaluated element wise. - * In case of complex numbers, x.re must equal y.re, and x.im must equal y.im. - * - * Values `null` and `undefined` are compared strictly, thus `null` is only - * equal to `null` and nothing else, and `undefined` is only equal to - * `undefined` and nothing else. Strings are compared by their numerical value. - * - * Syntax: - * - * math.equal(x, y) - * - * Examples: - * - * math.equal(2 + 2, 3) // returns false - * math.equal(2 + 2, 4) // returns true - * - * const a = math.unit('50 cm') - * const b = math.unit('5 m') - * math.equal(a, b) // returns true - * - * const c = [2, 5, 1] - * const d = [2, 7, 1] - * - * math.equal(c, d) // returns [true, false, true] - * math.deepEqual(c, d) // returns false - * - * math.equal("1000", "1e3") // returns true - * math.equal(0, null) // returns false - * - * See also: - * - * unequal, smaller, smallerEq, larger, largerEq, compare, deepEqual, equalText - * - * @param {number | BigNumber | boolean | Complex | Unit | string | Array | Matrix} x First value to compare - * @param {number | BigNumber | boolean | Complex | Unit | string | Array | Matrix} y Second value to compare - * @return {boolean | Array | Matrix} Returns true when the compared values are equal, else returns false - */ - - return typed(equal_name, { - 'any, any': function anyAny(x, y) { - // strict equality for null and undefined? - if (x === null) { - return y === null; - } - - if (y === null) { - return x === null; - } - - if (x === undefined) { - return y === undefined; - } - - if (y === undefined) { - return x === undefined; - } - - return equalScalar(x, y); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm07(x, y, equalScalar); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, equalScalar, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, equalScalar, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, equalScalar); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm12(x, y, equalScalar, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, equalScalar, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, equalScalar, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, equalScalar, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, equalScalar, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, equalScalar, true).valueOf(); - } - }); - }); - var createEqualNumber = Object(factory["a" /* factory */])(equal_name, ['typed', 'equalScalar'], function (_ref2) { - var typed = _ref2.typed, - equalScalar = _ref2.equalScalar; - return typed(equal_name, { - 'any, any': function anyAny(x, y) { - // strict equality for null and undefined? - if (x === null) { - return y === null; - } - - if (y === null) { - return x === null; - } - - if (x === undefined) { - return y === undefined; - } - - if (y === undefined) { - return x === undefined; - } - - return equalScalar(x, y); - } - }); - }); - // CONCATENATED MODULE: ./src/function/relational/equalText.js - - var equalText_name = 'equalText'; - var equalText_dependencies = ['typed', 'compareText', 'isZero']; - var createEqualText = /* #__PURE__ */Object(factory["a" /* factory */])(equalText_name, equalText_dependencies, function (_ref) { - var typed = _ref.typed, - compareText = _ref.compareText, - isZero = _ref.isZero; - - /** - * Check equality of two strings. Comparison is case sensitive. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.equalText(x, y) - * - * Examples: - * - * math.equalText('Hello', 'Hello') // returns true - * math.equalText('a', 'A') // returns false - * math.equal('2e3', '2000') // returns true - * math.equalText('2e3', '2000') // returns false - * - * math.equalText('B', ['A', 'B', 'C']) // returns [false, true, false] - * - * See also: - * - * equal, compareText, compare, compareNatural - * - * @param {string | Array | DenseMatrix} x First string to compare - * @param {string | Array | DenseMatrix} y Second string to compare - * @return {number | Array | DenseMatrix} Returns true if the values are equal, and false if not. - */ - return typed(equalText_name, { - 'any, any': function anyAny(x, y) { - return isZero(compareText(x, y)); - } - }); - }); - // CONCATENATED MODULE: ./src/function/relational/smaller.js - - var smaller_name = 'smaller'; - var smaller_dependencies = ['typed', 'config', 'matrix', 'DenseMatrix']; - var createSmaller = /* #__PURE__ */Object(factory["a" /* factory */])(smaller_name, smaller_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm07 = createAlgorithm07({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Test whether value x is smaller than y. - * - * The function returns true when x is smaller than y and the relative - * difference between x and y is smaller than the configured epsilon. The - * function cannot be used to compare values smaller than approximately 2.22e-16. - * - * For matrices, the function is evaluated element wise. - * Strings are compared by their numerical value. - * - * Syntax: - * - * math.smaller(x, y) - * - * Examples: - * - * math.smaller(2, 3) // returns true - * math.smaller(5, 2 * 2) // returns false - * - * const a = math.unit('5 cm') - * const b = math.unit('2 inch') - * math.smaller(a, b) // returns true - * - * See also: - * - * equal, unequal, smallerEq, smaller, smallerEq, compare - * - * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare - * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare - * @return {boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false - */ - - return typed(smaller_name, { - 'boolean, boolean': function booleanBoolean(x, y) { - return x < y; - }, - 'number, number': function numberNumber(x, y) { - return x < y && !Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return x.lt(y) && !nearlyEqual(x, y, config.epsilon); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.compare(y) === -1; - }, - 'Complex, Complex': function ComplexComplex(x, y) { - throw new TypeError('No ordering relation is defined for complex numbers'); - }, - 'Unit, Unit': function UnitUnit(x, y) { - if (!x.equalBase(y)) { - throw new Error('Cannot compare units with different base'); - } - - return this(x.value, y.value); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm07(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm12(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - var createSmallerNumber = /* #__PURE__ */Object(factory["a" /* factory */])(smaller_name, ['typed', 'config'], function (_ref2) { - var typed = _ref2.typed, - config = _ref2.config; - return typed(smaller_name, { - 'number, number': function numberNumber(x, y) { - return x < y && !Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon); - } - }); - }); - // CONCATENATED MODULE: ./src/function/relational/smallerEq.js - - var smallerEq_name = 'smallerEq'; - var smallerEq_dependencies = ['typed', 'config', 'matrix', 'DenseMatrix']; - var createSmallerEq = /* #__PURE__ */Object(factory["a" /* factory */])(smallerEq_name, smallerEq_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm07 = createAlgorithm07({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Test whether value x is smaller or equal to y. - * - * The function returns true when x is smaller than y or the relative - * difference between x and y is smaller than the configured epsilon. The - * function cannot be used to compare values smaller than approximately 2.22e-16. - * - * For matrices, the function is evaluated element wise. - * Strings are compared by their numerical value. - * - * Syntax: - * - * math.smallerEq(x, y) - * - * Examples: - * - * math.smaller(1 + 2, 3) // returns false - * math.smallerEq(1 + 2, 3) // returns true - * - * See also: - * - * equal, unequal, smaller, larger, largerEq, compare - * - * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare - * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare - * @return {boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false - */ - - return typed(smallerEq_name, { - 'boolean, boolean': function booleanBoolean(x, y) { - return x <= y; - }, - 'number, number': function numberNumber(x, y) { - return x <= y || Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return x.lte(y) || nearlyEqual(x, y, config.epsilon); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.compare(y) !== 1; - }, - 'Complex, Complex': function ComplexComplex() { - throw new TypeError('No ordering relation is defined for complex numbers'); - }, - 'Unit, Unit': function UnitUnit(x, y) { - if (!x.equalBase(y)) { - throw new Error('Cannot compare units with different base'); - } - - return this(x.value, y.value); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm07(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm12(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - var createSmallerEqNumber = /* #__PURE__ */Object(factory["a" /* factory */])(smallerEq_name, ['typed', 'config'], function (_ref2) { - var typed = _ref2.typed, - config = _ref2.config; - return typed(smallerEq_name, { - 'number, number': function numberNumber(x, y) { - return x <= y || Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon); - } - }); - }); - // CONCATENATED MODULE: ./src/function/relational/larger.js - - var larger_name = 'larger'; - var larger_dependencies = ['typed', 'config', 'matrix', 'DenseMatrix']; - var createLarger = /* #__PURE__ */Object(factory["a" /* factory */])(larger_name, larger_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm07 = createAlgorithm07({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Test whether value x is larger than y. - * - * The function returns true when x is larger than y and the relative - * difference between x and y is larger than the configured epsilon. The - * function cannot be used to compare values smaller than approximately 2.22e-16. - * - * For matrices, the function is evaluated element wise. - * Strings are compared by their numerical value. - * - * Syntax: - * - * math.larger(x, y) - * - * Examples: - * - * math.larger(2, 3) // returns false - * math.larger(5, 2 + 2) // returns true - * - * const a = math.unit('5 cm') - * const b = math.unit('2 inch') - * math.larger(a, b) // returns false - * - * See also: - * - * equal, unequal, smaller, smallerEq, largerEq, compare - * - * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare - * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare - * @return {boolean | Array | Matrix} Returns true when the x is larger than y, else returns false - */ - - return typed(larger_name, { - 'boolean, boolean': function booleanBoolean(x, y) { - return x > y; - }, - 'number, number': function numberNumber(x, y) { - return x > y && !Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return x.gt(y) && !nearlyEqual(x, y, config.epsilon); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.compare(y) === 1; - }, - 'Complex, Complex': function ComplexComplex() { - throw new TypeError('No ordering relation is defined for complex numbers'); - }, - 'Unit, Unit': function UnitUnit(x, y) { - if (!x.equalBase(y)) { - throw new Error('Cannot compare units with different base'); - } - - return this(x.value, y.value); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm07(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm12(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - var createLargerNumber = /* #__PURE__ */Object(factory["a" /* factory */])(larger_name, ['typed', 'config'], function (_ref2) { - var typed = _ref2.typed, - config = _ref2.config; - return typed(larger_name, { - 'number, number': function numberNumber(x, y) { - return x > y && !Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon); - } - }); - }); - // CONCATENATED MODULE: ./src/function/relational/largerEq.js - - var largerEq_name = 'largerEq'; - var largerEq_dependencies = ['typed', 'config', 'matrix', 'DenseMatrix']; - var createLargerEq = /* #__PURE__ */Object(factory["a" /* factory */])(largerEq_name, largerEq_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm07 = createAlgorithm07({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Test whether value x is larger or equal to y. - * - * The function returns true when x is larger than y or the relative - * difference between x and y is smaller than the configured epsilon. The - * function cannot be used to compare values smaller than approximately 2.22e-16. - * - * For matrices, the function is evaluated element wise. - * Strings are compared by their numerical value. - * - * Syntax: - * - * math.largerEq(x, y) - * - * Examples: - * - * math.larger(2, 1 + 1) // returns false - * math.largerEq(2, 1 + 1) // returns true - * - * See also: - * - * equal, unequal, smaller, smallerEq, larger, compare - * - * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare - * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare - * @return {boolean | Array | Matrix} Returns true when the x is larger or equal to y, else returns false - */ - - return typed(largerEq_name, { - 'boolean, boolean': function booleanBoolean(x, y) { - return x >= y; - }, - 'number, number': function numberNumber(x, y) { - return x >= y || Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { - return x.gte(y) || nearlyEqual(x, y, config.epsilon); - }, - 'Fraction, Fraction': function FractionFraction(x, y) { - return x.compare(y) !== -1; - }, - 'Complex, Complex': function ComplexComplex() { - throw new TypeError('No ordering relation is defined for complex numbers'); - }, - 'Unit, Unit': function UnitUnit(x, y) { - if (!x.equalBase(y)) { - throw new Error('Cannot compare units with different base'); - } - - return this(x.value, y.value); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm07(x, y, this); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm12(x, y, this, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, this, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, this, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, this, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - var createLargerEqNumber = /* #__PURE__ */Object(factory["a" /* factory */])(largerEq_name, ['typed', 'config'], function (_ref2) { - var typed = _ref2.typed, - config = _ref2.config; - return typed(largerEq_name, { - 'number, number': function numberNumber(x, y) { - return x >= y || Object(utils_number["m" /* nearlyEqual */])(x, y, config.epsilon); - } - }); - }); - // CONCATENATED MODULE: ./src/function/relational/deepEqual.js - - var deepEqual_name = 'deepEqual'; - var deepEqual_dependencies = ['typed', 'equal']; - var createDeepEqual = /* #__PURE__ */Object(factory["a" /* factory */])(deepEqual_name, deepEqual_dependencies, function (_ref) { - var typed = _ref.typed, - equal = _ref.equal; - - /** - * Test element wise whether two matrices are equal. - * The function accepts both matrices and scalar values. - * - * Strings are compared by their numerical value. - * - * Syntax: - * - * math.deepEqual(x, y) - * - * Examples: - * - * math.deepEqual(2, 4) // returns false - * - * a = [2, 5, 1] - * b = [2, 7, 1] - * - * math.deepEqual(a, b) // returns false - * math.equal(a, b) // returns [true, false, true] - * - * See also: - * - * equal, unequal - * - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First matrix to compare - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second matrix to compare - * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} - * Returns true when the input matrices have the same size and each of their elements is equal. - */ - return typed(deepEqual_name, { - 'any, any': function anyAny(x, y) { - return _deepEqual(x.valueOf(), y.valueOf()); - } - }); - /** - * Test whether two arrays have the same size and all elements are equal - * @param {Array | *} x - * @param {Array | *} y - * @return {boolean} Returns true if both arrays are deep equal - */ - - function _deepEqual(x, y) { - if (Array.isArray(x)) { - if (Array.isArray(y)) { - var len = x.length; - - if (len !== y.length) { - return false; - } - - for (var i = 0; i < len; i++) { - if (!_deepEqual(x[i], y[i])) { - return false; - } - } - - return true; - } else { - return false; - } - } else { - if (Array.isArray(y)) { - return false; - } else { - return equal(x, y); - } - } - } - }); - // CONCATENATED MODULE: ./src/function/relational/unequal.js - - var unequal_name = 'unequal'; - var unequal_dependencies = ['typed', 'config', 'equalScalar', 'matrix', 'DenseMatrix']; - var createUnequal = /* #__PURE__ */Object(factory["a" /* factory */])(unequal_name, unequal_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - equalScalar = _ref.equalScalar, - matrix = _ref.matrix, - DenseMatrix = _ref.DenseMatrix; - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm07 = createAlgorithm07({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Test whether two values are unequal. - * - * The function tests whether the relative difference between x and y is - * larger than the configured epsilon. The function cannot be used to compare - * values smaller than approximately 2.22e-16. - * - * For matrices, the function is evaluated element wise. - * In case of complex numbers, x.re must unequal y.re, or x.im must unequal y.im. - * Strings are compared by their numerical value. - * - * Values `null` and `undefined` are compared strictly, thus `null` is unequal - * with everything except `null`, and `undefined` is unequal with everything - * except `undefined`. - * - * Syntax: - * - * math.unequal(x, y) - * - * Examples: - * - * math.unequal(2 + 2, 3) // returns true - * math.unequal(2 + 2, 4) // returns false - * - * const a = math.unit('50 cm') - * const b = math.unit('5 m') - * math.unequal(a, b) // returns false - * - * const c = [2, 5, 1] - * const d = [2, 7, 1] - * - * math.unequal(c, d) // returns [false, true, false] - * math.deepEqual(c, d) // returns false - * - * math.unequal(0, null) // returns true - * See also: - * - * equal, deepEqual, smaller, smallerEq, larger, largerEq, compare - * - * @param {number | BigNumber | Fraction | boolean | Complex | Unit | string | Array | Matrix | undefined} x First value to compare - * @param {number | BigNumber | Fraction | boolean | Complex | Unit | string | Array | Matrix | undefined} y Second value to compare - * @return {boolean | Array | Matrix} Returns true when the compared values are unequal, else returns false - */ - - return typed('unequal', { - 'any, any': function anyAny(x, y) { - // strict equality for null and undefined? - if (x === null) { - return y !== null; - } - - if (y === null) { - return x !== null; - } - - if (x === undefined) { - return y !== undefined; - } - - if (y === undefined) { - return x !== undefined; - } - - return _unequal(x, y); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm07(x, y, _unequal); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm03(y, x, _unequal, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, _unequal, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, _unequal); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm12(x, y, _unequal, false); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, _unequal, false); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm12(y, x, _unequal, true); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, _unequal, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, _unequal, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, _unequal, true).valueOf(); - } - }); - - function _unequal(x, y) { - return !equalScalar(x, y); - } - }); - var createUnequalNumber = Object(factory["a" /* factory */])(unequal_name, ['typed', 'equalScalar'], function (_ref2) { - var typed = _ref2.typed, - equalScalar = _ref2.equalScalar; - return typed(unequal_name, { - 'any, any': function anyAny(x, y) { - // strict equality for null and undefined? - if (x === null) { - return y !== null; - } - - if (y === null) { - return x !== null; - } - - if (x === undefined) { - return y !== undefined; - } - - if (y === undefined) { - return x !== undefined; - } - - return !equalScalar(x, y); - } - }); - }); - // CONCATENATED MODULE: ./src/function/matrix/partitionSelect.js - - var partitionSelect_name = 'partitionSelect'; - var partitionSelect_dependencies = ['typed', 'isNumeric', 'isNaN', 'compare']; - var createPartitionSelect = /* #__PURE__ */Object(factory["a" /* factory */])(partitionSelect_name, partitionSelect_dependencies, function (_ref) { - var typed = _ref.typed, - isNumeric = _ref.isNumeric, - isNaN = _ref.isNaN, - compare = _ref.compare; - var asc = compare; - - var desc = function desc(a, b) { - return -compare(a, b); - }; - /** - * Partition-based selection of an array or 1D matrix. - * Will find the kth smallest value, and mutates the input array. - * Uses Quickselect. - * - * Syntax: - * - * math.partitionSelect(x, k) - * math.partitionSelect(x, k, compare) - * - * Examples: - * - * math.partitionSelect([5, 10, 1], 2) // returns 10 - * math.partitionSelect(['C', 'B', 'A', 'D'], 1) // returns 'B' - * - * function sortByLength (a, b) { - * return a.length - b.length - * } - * math.partitionSelect(['Langdon', 'Tom', 'Sara'], 2, sortByLength) // returns 'Langdon' - * - * See also: - * - * sort - * - * @param {Matrix | Array} x A one dimensional matrix or array to sort - * @param {Number} k The kth smallest value to be retrieved zero-based index - * @param {Function | 'asc' | 'desc'} [compare='asc'] - * An optional comparator function. The function is called as - * `compare(a, b)`, and must return 1 when a > b, -1 when a < b, - * and 0 when a == b. - * @return {*} Returns the kth lowest value. - */ - - return typed(partitionSelect_name, { - 'Array | Matrix, number': function ArrayMatrixNumber(x, k) { - return _partitionSelect(x, k, asc); - }, - 'Array | Matrix, number, string': function ArrayMatrixNumberString(x, k, compare) { - if (compare === 'asc') { - return _partitionSelect(x, k, asc); - } else if (compare === 'desc') { - return _partitionSelect(x, k, desc); - } else { - throw new Error('Compare string must be "asc" or "desc"'); - } - }, - 'Array | Matrix, number, function': _partitionSelect - }); - - function _partitionSelect(x, k, compare) { - if (!Object(utils_number["i" /* isInteger */])(k) || k < 0) { - throw new Error('k must be a non-negative integer'); - } - - if (Object(is["v" /* isMatrix */])(x)) { - var size = x.size(); - - if (size.length > 1) { - throw new Error('Only one dimensional matrices supported'); - } - - return quickSelect(x.valueOf(), k, compare); - } - - if (Array.isArray(x)) { - return quickSelect(x, k, compare); - } - } - /** - * Quickselect algorithm. - * Code adapted from: - * https://blog.teamleadnet.com/2012/07/quick-select-algorithm-find-kth-element.html - * - * @param {Array} arr - * @param {Number} k - * @param {Function} compare - * @private - */ - - function quickSelect(arr, k, compare) { - if (k >= arr.length) { - throw new Error('k out of bounds'); - } // check for NaN values since these can cause an infinite while loop - - for (var i = 0; i < arr.length; i++) { - if (isNumeric(arr[i]) && isNaN(arr[i])) { - return arr[i]; // return NaN - } - } - - var from = 0; - var to = arr.length - 1; // if from == to we reached the kth element - - while (from < to) { - var r = from; - var w = to; - var pivot = arr[Math.floor(Math.random() * (to - from + 1)) + from]; // stop if the reader and writer meets - - while (r < w) { - // arr[r] >= pivot - if (compare(arr[r], pivot) >= 0) { - // put the large values at the end - var tmp = arr[w]; - arr[w] = arr[r]; - arr[r] = tmp; - --w; - } else { - // the value is smaller than the pivot, skip - ++r; - } - } // if we stepped up (r++) we need to step one down (arr[r] > pivot) - - if (compare(arr[r], pivot) > 0) { - --r; - } // the r pointer is on the end of the first k elements - - if (k <= r) { - to = r; - } else { - from = r + 1; - } - } - - return arr[k]; - } - }); - // CONCATENATED MODULE: ./src/function/matrix/sort.js - - var sort_name = 'sort'; - var sort_dependencies = ['typed', 'matrix', 'compare', 'compareNatural']; - var createSort = /* #__PURE__ */Object(factory["a" /* factory */])(sort_name, sort_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - compare = _ref.compare, - compareNatural = _ref.compareNatural; - var compareAsc = compare; - - var compareDesc = function compareDesc(a, b) { - return -compare(a, b); - }; - /** - * Sort the items in a matrix. - * - * Syntax: - * - * math.sort(x) - * math.sort(x, compare) - * - * Examples: - * - * math.sort([5, 10, 1]) // returns [1, 5, 10] - * math.sort(['C', 'B', 'A', 'D'], math.compareNatural) - * // returns ['A', 'B', 'C', 'D'] - * - * function sortByLength (a, b) { - * return a.length - b.length - * } - * math.sort(['Langdon', 'Tom', 'Sara'], sortByLength) - * // returns ['Tom', 'Sara', 'Langdon'] - * - * See also: - * - * filter, forEach, map, compare, compareNatural - * - * @param {Matrix | Array} x A one dimensional matrix or array to sort - * @param {Function | 'asc' | 'desc' | 'natural'} [compare='asc'] - * An optional _comparator function or name. The function is called as - * `compare(a, b)`, and must return 1 when a > b, -1 when a < b, - * and 0 when a == b. - * @return {Matrix | Array} Returns the sorted matrix. - */ - - return typed(sort_name, { - Array: function Array(x) { - _arrayIsVector(x); - - return x.sort(compareAsc); - }, - Matrix: function Matrix(x) { - _matrixIsVector(x); - - return matrix(x.toArray().sort(compareAsc), x.storage()); - }, - 'Array, function': function ArrayFunction(x, _comparator) { - _arrayIsVector(x); - - return x.sort(_comparator); - }, - 'Matrix, function': function MatrixFunction(x, _comparator) { - _matrixIsVector(x); - - return matrix(x.toArray().sort(_comparator), x.storage()); - }, - 'Array, string': function ArrayString(x, order) { - _arrayIsVector(x); - - return x.sort(_comparator(order)); - }, - 'Matrix, string': function MatrixString(x, order) { - _matrixIsVector(x); - - return matrix(x.toArray().sort(_comparator(order)), x.storage()); - } - }); - /** - * Get the comparator for given order ('asc', 'desc', 'natural') - * @param {'asc' | 'desc' | 'natural'} order - * @return {Function} Returns a _comparator function - */ - - function _comparator(order) { - if (order === 'asc') { - return compareAsc; - } else if (order === 'desc') { - return compareDesc; - } else if (order === 'natural') { - return compareNatural; - } else { - throw new Error('String "asc", "desc", or "natural" expected'); - } - } - /** - * Validate whether an array is one dimensional - * Throws an error when this is not the case - * @param {Array} array - * @private - */ - - function _arrayIsVector(array) { - if (Object(utils_array["a" /* arraySize */])(array).length !== 1) { - throw new Error('One dimensional array expected'); - } - } - /** - * Validate whether a matrix is one dimensional - * Throws an error when this is not the case - * @param {Matrix} matrix - * @private - */ - - function _matrixIsVector(matrix) { - if (matrix.size().length !== 1) { - throw new Error('One dimensional matrix expected'); - } - } - }); - // CONCATENATED MODULE: ./src/function/statistics/max.js - - var max_name = 'max'; - var max_dependencies = ['typed', 'config', 'numeric', 'larger']; - var createMax = /* #__PURE__ */Object(factory["a" /* factory */])(max_name, max_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - numeric = _ref.numeric, - larger = _ref.larger; - - /** - * Compute the maximum value of a matrix or a list with values. - * In case of a multi dimensional array, the maximum of the flattened array - * will be calculated. When `dim` is provided, the maximum over the selected - * dimension will be calculated. Parameter `dim` is zero-based. - * - * Syntax: - * - * math.max(a, b, c, ...) - * math.max(A) - * math.max(A, dim) - * - * Examples: - * - * math.max(2, 1, 4, 3) // returns 4 - * math.max([2, 1, 4, 3]) // returns 4 - * - * // maximum over a specified dimension (zero-based) - * math.max([[2, 5], [4, 3], [1, 7]], 0) // returns [4, 7] - * math.max([[2, 5], [4, 3]], [1, 7], 1) // returns [5, 4, 7] - * - * math.max(2.7, 7.1, -4.5, 2.0, 4.1) // returns 7.1 - * math.min(2.7, 7.1, -4.5, 2.0, 4.1) // returns -4.5 - * - * See also: - * - * mean, median, min, prod, std, sum, variance - * - * @param {... *} args A single matrix or or multiple scalar values - * @return {*} The maximum value - */ - return typed(max_name, { - // max([a, b, c, d, ...]) - 'Array | Matrix': _max, - // max([a, b, c, d, ...], dim) - 'Array | Matrix, number | BigNumber': function ArrayMatrixNumberBigNumber(array, dim) { - return reduce(array, dim.valueOf(), _largest); - }, - // max(a, b, c, d, ...) - '...': function _(args) { - if (containsCollections(args)) { - throw new TypeError('Scalar values expected in function max'); - } - - return _max(args); - } - }); - /** - * Return the largest of two values - * @param {*} x - * @param {*} y - * @returns {*} Returns x when x is largest, or y when y is largest - * @private - */ - - function _largest(x, y) { - try { - return larger(x, y) ? x : y; - } catch (err) { - throw improveErrorMessage(err, 'max', y); - } - } - /** - * Recursively calculate the maximum value in an n-dimensional array - * @param {Array} array - * @return {number} max - * @private - */ - - function _max(array) { - var res; - deepForEach(array, function (value) { - try { - if (isNaN(value) && typeof value === 'number') { - res = NaN; - } else if (res === undefined || larger(value, res)) { - res = value; - } - } catch (err) { - throw improveErrorMessage(err, 'max', value); - } - }); - - if (res === undefined) { - throw new Error('Cannot calculate max of an empty array'); - } // make sure returning numeric value: parse a string into a numeric value - - if (typeof res === 'string') { - res = numeric(res, config.number); - } - - return res; - } - }); - // CONCATENATED MODULE: ./src/function/statistics/min.js - - var min_name = 'min'; - var min_dependencies = ['typed', 'config', 'numeric', 'smaller']; - var createMin = /* #__PURE__ */Object(factory["a" /* factory */])(min_name, min_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - numeric = _ref.numeric, - smaller = _ref.smaller; - - /** - * Compute the minimum value of a matrix or a list of values. - * In case of a multi dimensional array, the minimum of the flattened array - * will be calculated. When `dim` is provided, the minimum over the selected - * dimension will be calculated. Parameter `dim` is zero-based. - * - * Syntax: - * - * math.min(a, b, c, ...) - * math.min(A) - * math.min(A, dim) - * - * Examples: - * - * math.min(2, 1, 4, 3) // returns 1 - * math.min([2, 1, 4, 3]) // returns 1 - * - * // minimum over a specified dimension (zero-based) - * math.min([[2, 5], [4, 3], [1, 7]], 0) // returns [1, 3] - * math.min([[2, 5], [4, 3], [1, 7]], 1) // returns [2, 3, 1] - * - * math.max(2.7, 7.1, -4.5, 2.0, 4.1) // returns 7.1 - * math.min(2.7, 7.1, -4.5, 2.0, 4.1) // returns -4.5 - * - * See also: - * - * mean, median, max, prod, std, sum, variance - * - * @param {... *} args A single matrix or or multiple scalar values - * @return {*} The minimum value - */ - return typed(min_name, { - // min([a, b, c, d, ...]) - 'Array | Matrix': _min, - // min([a, b, c, d, ...], dim) - 'Array | Matrix, number | BigNumber': function ArrayMatrixNumberBigNumber(array, dim) { - return reduce(array, dim.valueOf(), _smallest); - }, - // min(a, b, c, d, ...) - '...': function _(args) { - if (containsCollections(args)) { - throw new TypeError('Scalar values expected in function min'); - } - - return _min(args); - } - }); - /** - * Return the smallest of two values - * @param {*} x - * @param {*} y - * @returns {*} Returns x when x is smallest, or y when y is smallest - * @private - */ - - function _smallest(x, y) { - try { - return smaller(x, y) ? x : y; - } catch (err) { - throw improveErrorMessage(err, 'min', y); - } - } - /** - * Recursively calculate the minimum value in an n-dimensional array - * @param {Array} array - * @return {number} min - * @private - */ - - function _min(array) { - var min; - deepForEach(array, function (value) { - try { - if (isNaN(value) && typeof value === 'number') { - min = NaN; - } else if (min === undefined || smaller(value, min)) { - min = value; - } - } catch (err) { - throw improveErrorMessage(err, 'min', value); - } - }); - - if (min === undefined) { - throw new Error('Cannot calculate min of an empty array'); - } // make sure returning numeric value: parse a string into a numeric value - - if (typeof min === 'string') { - min = numeric(min, config.number); - } - - return min; - } - }); - // CONCATENATED MODULE: ./src/type/matrix/ImmutableDenseMatrix.js - - var ImmutableDenseMatrix_name = 'ImmutableDenseMatrix'; - var ImmutableDenseMatrix_dependencies = ['smaller', 'DenseMatrix']; - var createImmutableDenseMatrixClass = /* #__PURE__ */Object(factory["a" /* factory */])(ImmutableDenseMatrix_name, ImmutableDenseMatrix_dependencies, function (_ref) { - var smaller = _ref.smaller, - DenseMatrix = _ref.DenseMatrix; - - function ImmutableDenseMatrix(data, datatype) { - if (!(this instanceof ImmutableDenseMatrix)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - if (datatype && !Object(is["I" /* isString */])(datatype)) { - throw new Error('Invalid datatype: ' + datatype); - } - - if (Object(is["v" /* isMatrix */])(data) || Object(is["b" /* isArray */])(data)) { - // use DenseMatrix implementation - var matrix = new DenseMatrix(data, datatype); // internal structures - - this._data = matrix._data; - this._size = matrix._size; - this._datatype = matrix._datatype; - this._min = null; - this._max = null; - } else if (data && Object(is["b" /* isArray */])(data.data) && Object(is["b" /* isArray */])(data.size)) { - // initialize fields from JSON representation - this._data = data.data; - this._size = data.size; - this._datatype = data.datatype; - this._min = typeof data.min !== 'undefined' ? data.min : null; - this._max = typeof data.max !== 'undefined' ? data.max : null; - } else if (data) { - // unsupported type - throw new TypeError('Unsupported type of data (' + Object(is["M" /* typeOf */])(data) + ')'); - } else { - // nothing provided - this._data = []; - this._size = [0]; - this._datatype = datatype; - this._min = null; - this._max = null; - } - } - - ImmutableDenseMatrix.prototype = new DenseMatrix(); - /** - * Attach type information - */ - - ImmutableDenseMatrix.prototype.type = 'ImmutableDenseMatrix'; - ImmutableDenseMatrix.prototype.isImmutableDenseMatrix = true; - /** - * Get a subset of the matrix, or replace a subset of the matrix. - * - * Usage: - * const subset = matrix.subset(index) // retrieve subset - * const value = matrix.subset(index, replacement) // replace subset - * - * @param {Index} index - * @param {Array | ImmutableDenseMatrix | *} [replacement] - * @param {*} [defaultValue=0] Default value, filled in on new entries when - * the matrix is resized. If not provided, - * new matrix elements will be filled with zeros. - */ - - ImmutableDenseMatrix.prototype.subset = function (index) { - switch (arguments.length) { - case 1: - { - // use base implementation - var m = DenseMatrix.prototype.subset.call(this, index); // check result is a matrix - - if (Object(is["v" /* isMatrix */])(m)) { - // return immutable matrix - return new ImmutableDenseMatrix({ - data: m._data, - size: m._size, - datatype: m._datatype - }); - } - - return m; - } - // intentional fall through - - case 2: - case 3: - throw new Error('Cannot invoke set subset on an Immutable Matrix instance'); - - default: - throw new SyntaxError('Wrong number of arguments'); - } - }; - /** - * Replace a single element in the matrix. - * @param {Number[]} index Zero-based index - * @param {*} value - * @param {*} [defaultValue] Default value, filled in on new entries when - * the matrix is resized. If not provided, - * new matrix elements will be left undefined. - * @return {ImmutableDenseMatrix} self - */ - - ImmutableDenseMatrix.prototype.set = function () { - throw new Error('Cannot invoke set on an Immutable Matrix instance'); - }; - /** - * Resize the matrix to the given size. Returns a copy of the matrix when - * `copy=true`, otherwise return the matrix itself (resize in place). - * - * @param {Number[]} size The new size the matrix should have. - * @param {*} [defaultValue=0] Default value, filled in on new entries. - * If not provided, the matrix elements will - * be filled with zeros. - * @param {boolean} [copy] Return a resized copy of the matrix - * - * @return {Matrix} The resized matrix - */ - - ImmutableDenseMatrix.prototype.resize = function () { - throw new Error('Cannot invoke resize on an Immutable Matrix instance'); - }; - /** - * Disallows reshaping in favor of immutability. - * - * @throws {Error} Operation not allowed - */ - - ImmutableDenseMatrix.prototype.reshape = function () { - throw new Error('Cannot invoke reshape on an Immutable Matrix instance'); - }; - /** - * Create a clone of the matrix - * @return {ImmutableDenseMatrix} clone - */ - - ImmutableDenseMatrix.prototype.clone = function () { - return new ImmutableDenseMatrix({ - data: Object(utils_object["a" /* clone */])(this._data), - size: Object(utils_object["a" /* clone */])(this._size), - datatype: this._datatype - }); - }; - /** - * Get a JSON representation of the matrix - * @returns {Object} - */ - - ImmutableDenseMatrix.prototype.toJSON = function () { - return { - mathjs: 'ImmutableDenseMatrix', - data: this._data, - size: this._size, - datatype: this._datatype - }; - }; - /** - * Generate a matrix from a JSON object - * @param {Object} json An object structured like - * `{"mathjs": "ImmutableDenseMatrix", data: [], size: []}`, - * where mathjs is optional - * @returns {ImmutableDenseMatrix} - */ - - ImmutableDenseMatrix.fromJSON = function (json) { - return new ImmutableDenseMatrix(json); - }; - /** - * Swap rows i and j in Matrix. - * - * @param {Number} i Matrix row index 1 - * @param {Number} j Matrix row index 2 - * - * @return {Matrix} The matrix reference - */ - - ImmutableDenseMatrix.prototype.swapRows = function () { - throw new Error('Cannot invoke swapRows on an Immutable Matrix instance'); - }; - /** - * Calculate the minimum value in the set - * @return {Number | undefined} min - */ - - ImmutableDenseMatrix.prototype.min = function () { - // check min has been calculated before - if (this._min === null) { - // minimum - var m = null; // compute min - - this.forEach(function (v) { - if (m === null || smaller(v, m)) { - m = v; - } - }); - this._min = m !== null ? m : undefined; - } - - return this._min; - }; - /** - * Calculate the maximum value in the set - * @return {Number | undefined} max - */ - - ImmutableDenseMatrix.prototype.max = function () { - // check max has been calculated before - if (this._max === null) { - // maximum - var m = null; // compute max - - this.forEach(function (v) { - if (m === null || smaller(m, v)) { - m = v; - } - }); - this._max = m !== null ? m : undefined; - } - - return this._max; - }; - - return ImmutableDenseMatrix; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/type/matrix/MatrixIndex.js - - var MatrixIndex_name = 'Index'; - var MatrixIndex_dependencies = ['ImmutableDenseMatrix']; - var createIndexClass = /* #__PURE__ */Object(factory["a" /* factory */])(MatrixIndex_name, MatrixIndex_dependencies, function (_ref) { - var ImmutableDenseMatrix = _ref.ImmutableDenseMatrix; - - /** - * Create an index. An Index can store ranges and sets for multiple dimensions. - * Matrix.get, Matrix.set, and math.subset accept an Index as input. - * - * Usage: - * const index = new Index(range1, range2, matrix1, array1, ...) - * - * Where each parameter can be any of: - * A number - * A string (containing a name of an object property) - * An instance of Range - * An Array with the Set values - * A Matrix with the Set values - * - * The parameters start, end, and step must be integer numbers. - * - * @class Index - * @Constructor Index - * @param {...*} ranges - */ - function Index(ranges) { - if (!(this instanceof Index)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this._dimensions = []; - this._isScalar = true; - - for (var i = 0, ii = arguments.length; i < ii; i++) { - var arg = arguments[i]; - - if (Object(is["D" /* isRange */])(arg)) { - this._dimensions.push(arg); - - this._isScalar = false; - } else if (Array.isArray(arg) || Object(is["v" /* isMatrix */])(arg)) { - // create matrix - var m = _createImmutableMatrix(arg.valueOf()); - - this._dimensions.push(m); // size - - var size = m.size(); // scalar - - if (size.length !== 1 || size[0] !== 1) { - this._isScalar = false; - } - } else if (typeof arg === 'number') { - this._dimensions.push(_createImmutableMatrix([arg])); - } else if (typeof arg === 'string') { - // object property (arguments.count should be 1) - this._dimensions.push(arg); - } else { - throw new TypeError('Dimension must be an Array, Matrix, number, string, or Range'); - } // TODO: implement support for wildcard '*' - - } - } - /** - * Attach type information - */ - - Index.prototype.type = 'Index'; - Index.prototype.isIndex = true; - - function _createImmutableMatrix(arg) { - // loop array elements - for (var i = 0, l = arg.length; i < l; i++) { - if (typeof arg[i] !== 'number' || !Object(utils_number["i" /* isInteger */])(arg[i])) { - throw new TypeError('Index parameters must be positive integer numbers'); - } - } // create matrix - - return new ImmutableDenseMatrix(arg); - } - /** - * Create a clone of the index - * @memberof Index - * @return {Index} clone - */ - - Index.prototype.clone = function () { - var index = new Index(); - index._dimensions = Object(utils_object["a" /* clone */])(this._dimensions); - index._isScalar = this._isScalar; - return index; - }; - /** - * Create an index from an array with ranges/numbers - * @memberof Index - * @param {Array.} ranges - * @return {Index} index - * @private - */ - - Index.create = function (ranges) { - var index = new Index(); - Index.apply(index, ranges); - return index; - }; - /** - * Retrieve the size of the index, the number of elements for each dimension. - * @memberof Index - * @returns {number[]} size - */ - - Index.prototype.size = function () { - var size = []; - - for (var i = 0, ii = this._dimensions.length; i < ii; i++) { - var d = this._dimensions[i]; - size[i] = typeof d === 'string' ? 1 : d.size()[0]; - } - - return size; - }; - /** - * Get the maximum value for each of the indexes ranges. - * @memberof Index - * @returns {number[]} max - */ - - Index.prototype.max = function () { - var values = []; - - for (var i = 0, ii = this._dimensions.length; i < ii; i++) { - var range = this._dimensions[i]; - values[i] = typeof range === 'string' ? range : range.max(); - } - - return values; - }; - /** - * Get the minimum value for each of the indexes ranges. - * @memberof Index - * @returns {number[]} min - */ - - Index.prototype.min = function () { - var values = []; - - for (var i = 0, ii = this._dimensions.length; i < ii; i++) { - var range = this._dimensions[i]; - values[i] = typeof range === 'string' ? range : range.min(); - } - - return values; - }; - /** - * Loop over each of the ranges of the index - * @memberof Index - * @param {Function} callback Called for each range with a Range as first - * argument, the dimension as second, and the - * index object as third. - */ - - Index.prototype.forEach = function (callback) { - for (var i = 0, ii = this._dimensions.length; i < ii; i++) { - callback(this._dimensions[i], i, this); - } - }; - /** - * Retrieve the dimension for the given index - * @memberof Index - * @param {Number} dim Number of the dimension - * @returns {Range | null} range - */ - - Index.prototype.dimension = function (dim) { - return this._dimensions[dim] || null; - }; - /** - * Test whether this index contains an object property - * @returns {boolean} Returns true if the index is an object property - */ - - Index.prototype.isObjectProperty = function () { - return this._dimensions.length === 1 && typeof this._dimensions[0] === 'string'; - }; - /** - * Returns the object property name when the Index holds a single object property, - * else returns null - * @returns {string | null} - */ - - Index.prototype.getObjectProperty = function () { - return this.isObjectProperty() ? this._dimensions[0] : null; - }; - /** - * Test whether this index contains only a single value. - * - * This is the case when the index is created with only scalar values as ranges, - * not for ranges resolving into a single value. - * @memberof Index - * @return {boolean} isScalar - */ - - Index.prototype.isScalar = function () { - return this._isScalar; - }; - /** - * Expand the Index into an array. - * For example new Index([0,3], [2,7]) returns [[0,1,2], [2,3,4,5,6]] - * @memberof Index - * @returns {Array} array - */ - - Index.prototype.toArray = function () { - var array = []; - - for (var i = 0, ii = this._dimensions.length; i < ii; i++) { - var dimension = this._dimensions[i]; - array.push(typeof dimension === 'string' ? dimension : dimension.toArray()); - } - - return array; - }; - /** - * Get the primitive value of the Index, a two dimensional array. - * Equivalent to Index.toArray(). - * @memberof Index - * @returns {Array} array - */ - - Index.prototype.valueOf = Index.prototype.toArray; - /** - * Get the string representation of the index, for example '[2:6]' or '[0:2:10, 4:7, [1,2,3]]' - * @memberof Index - * @returns {String} str - */ - - Index.prototype.toString = function () { - var strings = []; - - for (var i = 0, ii = this._dimensions.length; i < ii; i++) { - var dimension = this._dimensions[i]; - - if (typeof dimension === 'string') { - strings.push(JSON.stringify(dimension)); - } else { - strings.push(dimension.toString()); - } - } - - return '[' + strings.join(', ') + ']'; - }; - /** - * Get a JSON representation of the Index - * @memberof Index - * @returns {Object} Returns a JSON object structured as: - * `{"mathjs": "Index", "ranges": [{"mathjs": "Range", start: 0, end: 10, step:1}, ...]}` - */ - - Index.prototype.toJSON = function () { - return { - mathjs: 'Index', - dimensions: this._dimensions - }; - }; - /** - * Instantiate an Index from a JSON object - * @memberof Index - * @param {Object} json A JSON object structured as: - * `{"mathjs": "Index", "dimensions": [{"mathjs": "Range", start: 0, end: 10, step:1}, ...]}` - * @return {Index} - */ - - Index.fromJSON = function (json) { - return Index.create(json.dimensions); - }; - - return Index; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/type/matrix/FibonacciHeap.js - - var FibonacciHeap_name = 'FibonacciHeap'; - var FibonacciHeap_dependencies = ['smaller', 'larger']; - var createFibonacciHeapClass = /* #__PURE__ */Object(factory["a" /* factory */])(FibonacciHeap_name, FibonacciHeap_dependencies, function (_ref) { - var smaller = _ref.smaller, - larger = _ref.larger; - var oneOverLogPhi = 1.0 / Math.log((1.0 + Math.sqrt(5.0)) / 2.0); - /** - * Fibonacci Heap implementation, used interally for Matrix math. - * @class FibonacciHeap - * @constructor FibonacciHeap - */ - - function FibonacciHeap() { - if (!(this instanceof FibonacciHeap)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } // initialize fields - - this._minimum = null; - this._size = 0; - } - /** - * Attach type information - */ - - FibonacciHeap.prototype.type = 'FibonacciHeap'; - FibonacciHeap.prototype.isFibonacciHeap = true; - /** - * Inserts a new data element into the heap. No heap consolidation is - * performed at this time, the new node is simply inserted into the root - * list of this heap. Running time: O(1) actual. - * @memberof FibonacciHeap - */ - - FibonacciHeap.prototype.insert = function (key, value) { - // create node - var node = { - key: key, - value: value, - degree: 0 - }; // check we have a node in the minimum - - if (this._minimum) { - // minimum node - var minimum = this._minimum; // update left & right of node - - node.left = minimum; - node.right = minimum.right; - minimum.right = node; - node.right.left = node; // update minimum node in heap if needed - - if (smaller(key, minimum.key)) { - // node has a smaller key, use it as minimum - this._minimum = node; - } - } else { - // set left & right - node.left = node; - node.right = node; // this is the first node - - this._minimum = node; - } // increment number of nodes in heap - - this._size++; // return node - - return node; - }; - /** - * Returns the number of nodes in heap. Running time: O(1) actual. - * @memberof FibonacciHeap - */ - - FibonacciHeap.prototype.size = function () { - return this._size; - }; - /** - * Removes all elements from this heap. - * @memberof FibonacciHeap - */ - - FibonacciHeap.prototype.clear = function () { - this._minimum = null; - this._size = 0; - }; - /** - * Returns true if the heap is empty, otherwise false. - * @memberof FibonacciHeap - */ - - FibonacciHeap.prototype.isEmpty = function () { - return this._size === 0; - }; - /** - * Extracts the node with minimum key from heap. Amortized running - * time: O(log n). - * @memberof FibonacciHeap - */ - - FibonacciHeap.prototype.extractMinimum = function () { - // node to remove - var node = this._minimum; // check we have a minimum - - if (node === null) { - return node; - } // current minimum - - var minimum = this._minimum; // get number of children - - var numberOfChildren = node.degree; // pointer to the first child - - var x = node.child; // for each child of node do... - - while (numberOfChildren > 0) { - // store node in right side - var tempRight = x.right; // remove x from child list - - x.left.right = x.right; - x.right.left = x.left; // add x to root list of heap - - x.left = minimum; - x.right = minimum.right; - minimum.right = x; - x.right.left = x; // set Parent[x] to null - - x.parent = null; - x = tempRight; - numberOfChildren--; - } // remove node from root list of heap - - node.left.right = node.right; - node.right.left = node.left; // update minimum - - if (node === node.right) { - // empty - minimum = null; - } else { - // update minimum - minimum = node.right; // we need to update the pointer to the root with minimum key - - minimum = _findMinimumNode(minimum, this._size); - } // decrement size of heap - - this._size--; // update minimum - - this._minimum = minimum; // return node - - return node; - }; - /** - * Removes a node from the heap given the reference to the node. The trees - * in the heap will be consolidated, if necessary. This operation may fail - * to remove the correct element if there are nodes with key value -Infinity. - * Running time: O(log n) amortized. - * @memberof FibonacciHeap - */ - - FibonacciHeap.prototype.remove = function (node) { - // decrease key value - this._minimum = _decreaseKey(this._minimum, node, -1); // remove the smallest - - this.extractMinimum(); - }; - /** - * Decreases the key value for a heap node, given the new value to take on. - * The structure of the heap may be changed and will not be consolidated. - * Running time: O(1) amortized. - * @memberof FibonacciHeap - */ - - function _decreaseKey(minimum, node, key) { - // set node key - node.key = key; // get parent node - - var parent = node.parent; - - if (parent && smaller(node.key, parent.key)) { - // remove node from parent - _cut(minimum, node, parent); // remove all nodes from parent to the root parent - - _cascadingCut(minimum, parent); - } // update minimum node if needed - - if (smaller(node.key, minimum.key)) { - minimum = node; - } // return minimum - - return minimum; - } - /** - * The reverse of the link operation: removes node from the child list of parent. - * This method assumes that min is non-null. Running time: O(1). - * @memberof FibonacciHeap - */ - - function _cut(minimum, node, parent) { - // remove node from parent children and decrement Degree[parent] - node.left.right = node.right; - node.right.left = node.left; - parent.degree--; // reset y.child if necessary - - if (parent.child === node) { - parent.child = node.right; - } // remove child if degree is 0 - - if (parent.degree === 0) { - parent.child = null; - } // add node to root list of heap - - node.left = minimum; - node.right = minimum.right; - minimum.right = node; - node.right.left = node; // set parent[node] to null - - node.parent = null; // set mark[node] to false - - node.mark = false; - } - /** - * Performs a cascading cut operation. This cuts node from its parent and then - * does the same for its parent, and so on up the tree. - * Running time: O(log n); O(1) excluding the recursion. - * @memberof FibonacciHeap - */ - - function _cascadingCut(minimum, node) { - // store parent node - var parent = node.parent; // if there's a parent... - - if (!parent) { - return; - } // if node is unmarked, set it marked - - if (!node.mark) { - node.mark = true; - } else { - // it's marked, cut it from parent - _cut(minimum, node, parent); // cut its parent as well - - _cascadingCut(parent); - } - } - /** - * Make the first node a child of the second one. Running time: O(1) actual. - * @memberof FibonacciHeap - */ - - var _linkNodes = function _linkNodes(node, parent) { - // remove node from root list of heap - node.left.right = node.right; - node.right.left = node.left; // make node a Child of parent - - node.parent = parent; - - if (!parent.child) { - parent.child = node; - node.right = node; - node.left = node; - } else { - node.left = parent.child; - node.right = parent.child.right; - parent.child.right = node; - node.right.left = node; - } // increase degree[parent] - - parent.degree++; // set mark[node] false - - node.mark = false; - }; - - function _findMinimumNode(minimum, size) { - // to find trees of the same degree efficiently we use an array of length O(log n) in which we keep a pointer to one root of each degree - var arraySize = Math.floor(Math.log(size) * oneOverLogPhi) + 1; // create list with initial capacity - - var array = new Array(arraySize); // find the number of root nodes. - - var numRoots = 0; - var x = minimum; - - if (x) { - numRoots++; - x = x.right; - - while (x !== minimum) { - numRoots++; - x = x.right; - } - } // vars - - var y; // For each node in root list do... - - while (numRoots > 0) { - // access this node's degree.. - var d = x.degree; // get next node - - var next = x.right; // check if there is a node already in array with the same degree - - while (true) { - // get node with the same degree is any - y = array[d]; - - if (!y) { - break; - } // make one node with the same degree a child of the other, do this based on the key value. - - if (larger(x.key, y.key)) { - var temp = y; - y = x; - x = temp; - } // make y a child of x - - _linkNodes(y, x); // we have handled this degree, go to next one. - - array[d] = null; - d++; - } // save this node for later when we might encounter another of the same degree. - - array[d] = x; // move forward through list. - - x = next; - numRoots--; - } // Set min to null (effectively losing the root list) and reconstruct the root list from the array entries in array[]. - - minimum = null; // loop nodes in array - - for (var i = 0; i < arraySize; i++) { - // get current node - y = array[i]; - - if (!y) { - continue; - } // check if we have a linked list - - if (minimum) { - // First remove node from root list. - y.left.right = y.right; - y.right.left = y.left; // now add to root list, again. - - y.left = minimum; - y.right = minimum.right; - minimum.right = y; - y.right.left = y; // check if this is a new min. - - if (smaller(y.key, minimum.key)) { - minimum = y; - } - } else { - minimum = y; - } - } - - return minimum; - } - - return FibonacciHeap; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/type/matrix/Spa.js - - var Spa_name = 'Spa'; - var Spa_dependencies = ['addScalar', 'equalScalar', 'FibonacciHeap']; - var createSpaClass = /* #__PURE__ */Object(factory["a" /* factory */])(Spa_name, Spa_dependencies, function (_ref) { - var addScalar = _ref.addScalar, - equalScalar = _ref.equalScalar, - FibonacciHeap = _ref.FibonacciHeap; - - /** - * An ordered Sparse Accumulator is a representation for a sparse vector that includes a dense array - * of the vector elements and an ordered list of non-zero elements. - */ - function Spa() { - if (!(this instanceof Spa)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } // allocate vector, TODO use typed arrays - - this._values = []; - this._heap = new FibonacciHeap(); - } - /** - * Attach type information - */ - - Spa.prototype.type = 'Spa'; - Spa.prototype.isSpa = true; - /** - * Set the value for index i. - * - * @param {number} i The index - * @param {number | BigNumber | Complex} The value at index i - */ - - Spa.prototype.set = function (i, v) { - // check we have a value @ i - if (!this._values[i]) { - // insert in heap - var node = this._heap.insert(i, v); // set the value @ i - - this._values[i] = node; - } else { - // update the value @ i - this._values[i].value = v; - } - }; - - Spa.prototype.get = function (i) { - var node = this._values[i]; - - if (node) { - return node.value; - } - - return 0; - }; - - Spa.prototype.accumulate = function (i, v) { - // node @ i - var node = this._values[i]; - - if (!node) { - // insert in heap - node = this._heap.insert(i, v); // initialize value - - this._values[i] = node; - } else { - // accumulate value - node.value = addScalar(node.value, v); - } - }; - - Spa.prototype.forEach = function (from, to, callback) { - // references - var heap = this._heap; - var values = this._values; // nodes - - var nodes = []; // node with minimum key, save it - - var node = heap.extractMinimum(); - - if (node) { - nodes.push(node); - } // extract nodes from heap (ordered) - - while (node && node.key <= to) { - // check it is in range - if (node.key >= from) { - // check value is not zero - if (!equalScalar(node.value, 0)) { - // invoke callback - callback(node.key, node.value, this); - } - } // extract next node, save it - - node = heap.extractMinimum(); - - if (node) { - nodes.push(node); - } - } // reinsert all nodes in heap - - for (var i = 0; i < nodes.length; i++) { - // current node - var n = nodes[i]; // insert node in heap - - node = heap.insert(n.key, n.value); // update values - - values[node.key] = node; - } - }; - - Spa.prototype.swap = function (i, j) { - // node @ i and j - var nodei = this._values[i]; - var nodej = this._values[j]; // check we need to insert indeces - - if (!nodei && nodej) { - // insert in heap - nodei = this._heap.insert(i, nodej.value); // remove from heap - - this._heap.remove(nodej); // set values - - this._values[i] = nodei; - this._values[j] = undefined; - } else if (nodei && !nodej) { - // insert in heap - nodej = this._heap.insert(j, nodei.value); // remove from heap - - this._heap.remove(nodei); // set values - - this._values[j] = nodej; - this._values[i] = undefined; - } else if (nodei && nodej) { - // swap values - var v = nodei.value; - nodei.value = nodej.value; - nodej.value = v; - } - }; - - return Spa; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/utils/bignumber/constants.js - - /** - * Calculate BigNumber e - * @param {function} BigNumber BigNumber constructor - * @returns {BigNumber} Returns e - */ - - var createBigNumberE = memoize(function (BigNumber) { - return new BigNumber(1).exp(); - }, hasher); - /** - * Calculate BigNumber golden ratio, phi = (1+sqrt(5))/2 - * @param {function} BigNumber BigNumber constructor - * @returns {BigNumber} Returns phi - */ - - var createBigNumberPhi = memoize(function (BigNumber) { - return new BigNumber(1).plus(new BigNumber(5).sqrt()).div(2); - }, hasher); - /** - * Calculate BigNumber pi. - * @param {function} BigNumber BigNumber constructor - * @returns {BigNumber} Returns pi - */ - - var createBigNumberPi = memoize(function (BigNumber) { - return BigNumber.acos(-1); - }, hasher); - /** - * Calculate BigNumber tau, tau = 2 * pi - * @param {function} BigNumber BigNumber constructor - * @returns {BigNumber} Returns tau - */ - - var createBigNumberTau = memoize(function (BigNumber) { - return createBigNumberPi(BigNumber).times(2); - }, hasher); - /** - * Create a hash for a BigNumber constructor function. The created has is - * the configured precision - * @param {Array} args Supposed to contain a single entry with - * a BigNumber constructor - * @return {number} precision - * @private - */ - - function hasher(args) { - return args[0].precision; - } - // CONCATENATED MODULE: ./src/type/unit/Unit.js - function Unit_typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - Unit_typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - Unit_typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return Unit_typeof(obj); - } - - function _extends() { - _extends = Object.assign || function (target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i]; for (var key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } return target; - }; return _extends.apply(this, arguments); - } - - function Unit_ownKeys(object, enumerableOnly) { - var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { - var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { - symbols = symbols.filter(function (sym) { - return Object.getOwnPropertyDescriptor(object, sym).enumerable; - }); - } keys.push.apply(keys, symbols); - } return keys; - } - - function Unit_objectSpread(target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { - Unit_ownKeys(Object(source), true).forEach(function (key) { - Unit_defineProperty(target, key, source[key]); - }); - } else if (Object.getOwnPropertyDescriptors) { - Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); - } else { - Unit_ownKeys(Object(source)).forEach(function (key) { - Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); - }); - } - } return target; - } - - function Unit_defineProperty(obj, key, value) { - if (key in obj) { - Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); - } else { - obj[key] = value; - } return obj; - } - - var Unit_name = 'Unit'; - var Unit_dependencies = ['?on', 'config', 'addScalar', 'subtract', 'multiplyScalar', 'divideScalar', 'pow', 'abs', 'fix', 'round', 'equal', 'isNumeric', 'format', 'number', 'Complex', 'BigNumber', 'Fraction']; - var createUnitClass = /* #__PURE__ */Object(factory["a" /* factory */])(Unit_name, Unit_dependencies, function (_ref) { - var on = _ref.on, - config = _ref.config, - addScalar = _ref.addScalar, - subtract = _ref.subtract, - multiplyScalar = _ref.multiplyScalar, - divideScalar = _ref.divideScalar, - pow = _ref.pow, - abs = _ref.abs, - fix = _ref.fix, - round = _ref.round, - equal = _ref.equal, - isNumeric = _ref.isNumeric, - format = _ref.format, - number = _ref.number, - Complex = _ref.Complex, - _BigNumber = _ref.BigNumber, - _Fraction = _ref.Fraction; - var toNumber = number; - /** - * A unit can be constructed in the following ways: - * - * const a = new Unit(value, name) - * const b = new Unit(null, name) - * const c = Unit.parse(str) - * - * Example usage: - * - * const a = new Unit(5, 'cm') // 50 mm - * const b = Unit.parse('23 kg') // 23 kg - * const c = math.in(a, new Unit(null, 'm') // 0.05 m - * const d = new Unit(9.81, "m/s^2") // 9.81 m/s^2 - * - * @class Unit - * @constructor Unit - * @param {number | BigNumber | Fraction | Complex | boolean} [value] A value like 5.2 - * @param {string} [name] A unit name like "cm" or "inch", or a derived unit of the form: "u1[^ex1] [u2[^ex2] ...] [/ u3[^ex3] [u4[^ex4]]]", such as "kg m^2/s^2", where each unit appearing after the forward slash is taken to be in the denominator. "kg m^2 s^-2" is a synonym and is also acceptable. Any of the units can include a prefix. - */ - - function Unit(value, name) { - if (!(this instanceof Unit)) { - throw new Error('Constructor must be called with the new operator'); - } - - if (!(value === null || value === undefined || isNumeric(value) || Object(is["j" /* isComplex */])(value))) { - throw new TypeError('First parameter in Unit constructor must be number, BigNumber, Fraction, Complex, or undefined'); - } - - if (name !== undefined && (typeof name !== 'string' || name === '')) { - throw new TypeError('Second parameter in Unit constructor must be a string'); - } - - if (name !== undefined) { - var u = Unit.parse(name); - this.units = u.units; - this.dimensions = u.dimensions; - } else { - this.units = [{ - unit: UNIT_NONE, - prefix: PREFIXES.NONE, - // link to a list with supported prefixes - power: 0 - }]; - this.dimensions = []; - - for (var i = 0; i < BASE_DIMENSIONS.length; i++) { - this.dimensions[i] = 0; - } - } - - this.value = value !== undefined && value !== null ? this._normalize(value) : null; - this.fixPrefix = false; // if true, function format will not search for the - // best prefix but leave it as initially provided. - // fixPrefix is set true by the method Unit.to - // The justification behind this is that if the constructor is explicitly called, - // the caller wishes the units to be returned exactly as he supplied. - - this.skipAutomaticSimplification = true; - } - /** - * Attach type information - */ - - Unit.prototype.type = 'Unit'; - Unit.prototype.isUnit = true; // private variables and functions for the Unit parser - - var text, index, c; - - function skipWhitespace() { - while (c === ' ' || c === '\t') { - next(); - } - } - - function isDigitDot(c) { - return c >= '0' && c <= '9' || c === '.'; - } - - function isDigit(c) { - return c >= '0' && c <= '9'; - } - - function next() { - index++; - c = text.charAt(index); - } - - function revert(oldIndex) { - index = oldIndex; - c = text.charAt(index); - } - - function parseNumber() { - var number = ''; - var oldIndex = index; - - if (c === '+') { - next(); - } else if (c === '-') { - number += c; - next(); - } - - if (!isDigitDot(c)) { - // a + or - must be followed by a digit - revert(oldIndex); - return null; - } // get number, can have a single dot - - if (c === '.') { - number += c; - next(); - - if (!isDigit(c)) { - // this is no legal number, it is just a dot - revert(oldIndex); - return null; - } - } else { - while (isDigit(c)) { - number += c; - next(); - } - - if (c === '.') { - number += c; - next(); - } - } - - while (isDigit(c)) { - number += c; - next(); - } // check for exponential notation like "2.3e-4" or "1.23e50" - - if (c === 'E' || c === 'e') { - // The grammar branches here. This could either be part of an exponent or the start of a unit that begins with the letter e, such as "4exabytes" - var tentativeNumber = ''; - var tentativeIndex = index; - tentativeNumber += c; - next(); - - if (c === '+' || c === '-') { - tentativeNumber += c; - next(); - } // Scientific notation MUST be followed by an exponent (otherwise we assume it is not scientific notation) - - if (!isDigit(c)) { - // The e or E must belong to something else, so return the number without the e or E. - revert(tentativeIndex); - return number; - } // We can now safely say that this is scientific notation. - - number = number + tentativeNumber; - - while (isDigit(c)) { - number += c; - next(); - } - } - - return number; - } - - function parseUnit() { - var unitName = ''; // Alphanumeric characters only; matches [a-zA-Z0-9] - - var code = text.charCodeAt(index); - - while (code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122) { - unitName += c; - next(); - code = text.charCodeAt(index); - } // Must begin with [a-zA-Z] - - code = unitName.charCodeAt(0); - - if (code >= 65 && code <= 90 || code >= 97 && code <= 122) { - return unitName || null; - } else { - return null; - } - } - - function parseCharacter(toFind) { - if (c === toFind) { - next(); - return toFind; - } else { - return null; - } - } - /** - * Parse a string into a unit. The value of the unit is parsed as number, - * BigNumber, or Fraction depending on the math.js config setting `number`. - * - * Throws an exception if the provided string does not contain a valid unit or - * cannot be parsed. - * @memberof Unit - * @param {string} str A string like "5.2 inch", "4e2 cm/s^2" - * @return {Unit} unit - */ - - Unit.parse = function (str, options) { - options = options || {}; - text = str; - index = -1; - c = ''; - - if (typeof text !== 'string') { - throw new TypeError('Invalid argument in Unit.parse, string expected'); - } - - var unit = new Unit(); - unit.units = []; - var powerMultiplierCurrent = 1; - var expectingUnit = false; // A unit should follow this pattern: - // [number] ...[ [*/] unit[^number] ] - // unit[^number] ... [ [*/] unit[^number] ] - // Rules: - // number is any floating point number. - // unit is any alphanumeric string beginning with an alpha. Units with names like e3 should be avoided because they look like the exponent of a floating point number! - // The string may optionally begin with a number. - // Each unit may optionally be followed by ^number. - // Whitespace or a forward slash is recommended between consecutive units, although the following technically is parseable: - // 2m^2kg/s^2 - // it is not good form. If a unit starts with e, then it could be confused as a floating point number: - // 4erg - - next(); - skipWhitespace(); // Optional number at the start of the string - - var valueStr = parseNumber(); - var value = null; - - if (valueStr) { - if (config.number === 'BigNumber') { - value = new _BigNumber(valueStr); - } else if (config.number === 'Fraction') { - try { - // not all numbers can be turned in Fractions, for example very small numbers not - value = new _Fraction(valueStr); - } catch (err) { - value = parseFloat(valueStr); - } - } else { - // number - value = parseFloat(valueStr); - } - - skipWhitespace(); // Whitespace is not required here - // handle multiplication or division right after the value, like '1/s' - - if (parseCharacter('*')) { - powerMultiplierCurrent = 1; - expectingUnit = true; - } else if (parseCharacter('/')) { - powerMultiplierCurrent = -1; - expectingUnit = true; - } - } // Stack to keep track of powerMultipliers applied to each parentheses group - - var powerMultiplierStack = []; // Running product of all elements in powerMultiplierStack - - var powerMultiplierStackProduct = 1; - - while (true) { - skipWhitespace(); // Check for and consume opening parentheses, pushing powerMultiplierCurrent to the stack - // A '(' will always appear directly before a unit. - - while (c === '(') { - powerMultiplierStack.push(powerMultiplierCurrent); - powerMultiplierStackProduct *= powerMultiplierCurrent; - powerMultiplierCurrent = 1; - next(); - skipWhitespace(); - } // Is there something here? - - var uStr = void 0; - - if (c) { - var oldC = c; - uStr = parseUnit(); - - if (uStr === null) { - throw new SyntaxError('Unexpected "' + oldC + '" in "' + text + '" at index ' + index.toString()); - } - } else { - // End of input. - break; - } // Verify the unit exists and get the prefix (if any) - - var res = _findUnit(uStr); - - if (res === null) { - // Unit not found. - throw new SyntaxError('Unit "' + uStr + '" not found.'); - } - - var power = powerMultiplierCurrent * powerMultiplierStackProduct; // Is there a "^ number"? - - skipWhitespace(); - - if (parseCharacter('^')) { - skipWhitespace(); - var p = parseNumber(); - - if (p === null) { - // No valid number found for the power! - throw new SyntaxError('In "' + str + '", "^" must be followed by a floating-point number'); - } - - power *= p; - } // Add the unit to the list - - unit.units.push({ - unit: res.unit, - prefix: res.prefix, - power: power - }); - - for (var i = 0; i < BASE_DIMENSIONS.length; i++) { - unit.dimensions[i] += (res.unit.dimensions[i] || 0) * power; - } // Check for and consume closing parentheses, popping from the stack. - // A ')' will always follow a unit. - - skipWhitespace(); - - while (c === ')') { - if (powerMultiplierStack.length === 0) { - throw new SyntaxError('Unmatched ")" in "' + text + '" at index ' + index.toString()); - } - - powerMultiplierStackProduct /= powerMultiplierStack.pop(); - next(); - skipWhitespace(); - } // "*" and "/" should mean we are expecting something to come next. - // Is there a forward slash? If so, negate powerMultiplierCurrent. The next unit or paren group is in the denominator. - - expectingUnit = false; - - if (parseCharacter('*')) { - // explicit multiplication - powerMultiplierCurrent = 1; - expectingUnit = true; - } else if (parseCharacter('/')) { - // division - powerMultiplierCurrent = -1; - expectingUnit = true; - } else { - // implicit multiplication - powerMultiplierCurrent = 1; - } // Replace the unit into the auto unit system - - if (res.unit.base) { - var baseDim = res.unit.base.key; - UNIT_SYSTEMS.auto[baseDim] = { - unit: res.unit, - prefix: res.prefix - }; - } - } // Has the string been entirely consumed? - - skipWhitespace(); - - if (c) { - throw new SyntaxError('Could not parse: "' + str + '"'); - } // Is there a trailing slash? - - if (expectingUnit) { - throw new SyntaxError('Trailing characters: "' + str + '"'); - } // Is the parentheses stack empty? - - if (powerMultiplierStack.length !== 0) { - throw new SyntaxError('Unmatched "(" in "' + text + '"'); - } // Are there any units at all? - - if (unit.units.length === 0 && !options.allowNoUnits) { - throw new SyntaxError('"' + str + '" contains no units'); - } - - unit.value = value !== undefined ? unit._normalize(value) : null; - return unit; - }; - /** - * create a copy of this unit - * @memberof Unit - * @return {Unit} Returns a cloned version of the unit - */ - - Unit.prototype.clone = function () { - var unit = new Unit(); - unit.fixPrefix = this.fixPrefix; - unit.skipAutomaticSimplification = this.skipAutomaticSimplification; - unit.value = Object(utils_object["a" /* clone */])(this.value); - unit.dimensions = this.dimensions.slice(0); - unit.units = []; - - for (var i = 0; i < this.units.length; i++) { - unit.units[i] = {}; - - for (var p in this.units[i]) { - if (Object(utils_object["f" /* hasOwnProperty */])(this.units[i], p)) { - unit.units[i][p] = this.units[i][p]; - } - } - } - - return unit; - }; - /** - * Return whether the unit is derived (such as m/s, or cm^2, but not N) - * @memberof Unit - * @return {boolean} True if the unit is derived - */ - - Unit.prototype._isDerived = function () { - if (this.units.length === 0) { - return false; - } - - return this.units.length > 1 || Math.abs(this.units[0].power - 1.0) > 1e-15; - }; - /** - * Normalize a value, based on its currently set unit(s) - * @memberof Unit - * @param {number | BigNumber | Fraction | boolean} value - * @return {number | BigNumber | Fraction | boolean} normalized value - * @private - */ - - Unit.prototype._normalize = function (value) { - var unitValue, unitOffset, unitPower, unitPrefixValue; - var convert; - - if (value === null || value === undefined || this.units.length === 0) { - return value; - } else if (this._isDerived()) { - // This is a derived unit, so do not apply offsets. - // For example, with J kg^-1 degC^-1 you would NOT want to apply the offset. - var res = value; - convert = Unit._getNumberConverter(Object(is["M" /* typeOf */])(value)); // convert to Fraction or BigNumber if needed - - for (var i = 0; i < this.units.length; i++) { - unitValue = convert(this.units[i].unit.value); - unitPrefixValue = convert(this.units[i].prefix.value); - unitPower = convert(this.units[i].power); - res = multiplyScalar(res, pow(multiplyScalar(unitValue, unitPrefixValue), unitPower)); - } - - return res; - } else { - // This is a single unit of power 1, like kg or degC - convert = Unit._getNumberConverter(Object(is["M" /* typeOf */])(value)); // convert to Fraction or BigNumber if needed - - unitValue = convert(this.units[0].unit.value); - unitOffset = convert(this.units[0].unit.offset); - unitPrefixValue = convert(this.units[0].prefix.value); - return multiplyScalar(addScalar(value, unitOffset), multiplyScalar(unitValue, unitPrefixValue)); - } - }; - /** - * Denormalize a value, based on its currently set unit(s) - * @memberof Unit - * @param {number} value - * @param {number} [prefixValue] Optional prefix value to be used (ignored if this is a derived unit) - * @return {number} denormalized value - * @private - */ - - Unit.prototype._denormalize = function (value, prefixValue) { - var unitValue, unitOffset, unitPower, unitPrefixValue; - var convert; - - if (value === null || value === undefined || this.units.length === 0) { - return value; - } else if (this._isDerived()) { - // This is a derived unit, so do not apply offsets. - // For example, with J kg^-1 degC^-1 you would NOT want to apply the offset. - // Also, prefixValue is ignored--but we will still use the prefix value stored in each unit, since kg is usually preferable to g unless the user decides otherwise. - var res = value; - convert = Unit._getNumberConverter(Object(is["M" /* typeOf */])(value)); // convert to Fraction or BigNumber if needed - - for (var i = 0; i < this.units.length; i++) { - unitValue = convert(this.units[i].unit.value); - unitPrefixValue = convert(this.units[i].prefix.value); - unitPower = convert(this.units[i].power); - res = divideScalar(res, pow(multiplyScalar(unitValue, unitPrefixValue), unitPower)); - } - - return res; - } else { - // This is a single unit of power 1, like kg or degC - convert = Unit._getNumberConverter(Object(is["M" /* typeOf */])(value)); // convert to Fraction or BigNumber if needed - - unitValue = convert(this.units[0].unit.value); - unitPrefixValue = convert(this.units[0].prefix.value); - unitOffset = convert(this.units[0].unit.offset); - - if (prefixValue === undefined || prefixValue === null) { - return subtract(divideScalar(divideScalar(value, unitValue), unitPrefixValue), unitOffset); - } else { - return subtract(divideScalar(divideScalar(value, unitValue), prefixValue), unitOffset); - } - } - }; - /** - * Find a unit from a string - * @memberof Unit - * @param {string} str A string like 'cm' or 'inch' - * @returns {Object | null} result When found, an object with fields unit and - * prefix is returned. Else, null is returned. - * @private - */ - - function _findUnit(str) { - // First, match units names exactly. For example, a user could define 'mm' as 10^-4 m, which is silly, but then we would want 'mm' to match the user-defined unit. - if (Object(utils_object["f" /* hasOwnProperty */])(UNITS, str)) { - var unit = UNITS[str]; - var prefix = unit.prefixes['']; - return { - unit: unit, - prefix: prefix - }; - } - - for (var _name in UNITS) { - if (Object(utils_object["f" /* hasOwnProperty */])(UNITS, _name)) { - if (Object(utils_string["b" /* endsWith */])(str, _name)) { - var _unit = UNITS[_name]; - var prefixLen = str.length - _name.length; - var prefixName = str.substring(0, prefixLen); - - var _prefix = Object(utils_object["f" /* hasOwnProperty */])(_unit.prefixes, prefixName) ? _unit.prefixes[prefixName] : undefined; - - if (_prefix !== undefined) { - // store unit, prefix, and value - return { - unit: _unit, - prefix: _prefix - }; - } - } - } - } - - return null; - } - /** - * Test if the given expression is a unit. - * The unit can have a prefix but cannot have a value. - * @memberof Unit - * @param {string} name A string to be tested whether it is a value less unit. - * The unit can have prefix, like "cm" - * @return {boolean} true if the given string is a unit - */ - - Unit.isValuelessUnit = function (name) { - return _findUnit(name) !== null; - }; - /** - * check if this unit has given base unit - * If this unit is a derived unit, this will ALWAYS return false, since by definition base units are not derived. - * @memberof Unit - * @param {BASE_UNITS | string | undefined} base - */ - - Unit.prototype.hasBase = function (base) { - if (typeof base === 'string') { - base = BASE_UNITS[base]; - } - - if (!base) { - return false; - } // All dimensions must be the same - - for (var i = 0; i < BASE_DIMENSIONS.length; i++) { - if (Math.abs((this.dimensions[i] || 0) - (base.dimensions[i] || 0)) > 1e-12) { - return false; - } - } - - return true; - }; - /** - * Check if this unit has a base or bases equal to another base or bases - * For derived units, the exponent on each base also must match - * @memberof Unit - * @param {Unit} other - * @return {boolean} true if equal base - */ - - Unit.prototype.equalBase = function (other) { - // All dimensions must be the same - for (var i = 0; i < BASE_DIMENSIONS.length; i++) { - if (Math.abs((this.dimensions[i] || 0) - (other.dimensions[i] || 0)) > 1e-12) { - return false; - } - } - - return true; - }; - /** - * Check if this unit equals another unit - * @memberof Unit - * @param {Unit} other - * @return {boolean} true if both units are equal - */ - - Unit.prototype.equals = function (other) { - return this.equalBase(other) && equal(this.value, other.value); - }; - /** - * Multiply this unit with another one - * @memberof Unit - * @param {Unit} other - * @return {Unit} product of this unit and the other unit - */ - - Unit.prototype.multiply = function (other) { - var res = this.clone(); - - for (var i = 0; i < BASE_DIMENSIONS.length; i++) { - // Dimensions arrays may be of different lengths. Default to 0. - res.dimensions[i] = (this.dimensions[i] || 0) + (other.dimensions[i] || 0); - } // Append other's units list onto res - - for (var _i = 0; _i < other.units.length; _i++) { - // Make a shallow copy of every unit - var inverted = Unit_objectSpread({}, other.units[_i]); - - res.units.push(inverted); - } // If at least one operand has a value, then the result should also have a value - - if (this.value !== null || other.value !== null) { - var valThis = this.value === null ? this._normalize(1) : this.value; - var valOther = other.value === null ? other._normalize(1) : other.value; - res.value = multiplyScalar(valThis, valOther); - } else { - res.value = null; - } - - res.skipAutomaticSimplification = false; - return getNumericIfUnitless(res); - }; - /** - * Divide this unit by another one - * @memberof Unit - * @param {Unit} other - * @return {Unit} result of dividing this unit by the other unit - */ - - Unit.prototype.divide = function (other) { - var res = this.clone(); - - for (var i = 0; i < BASE_DIMENSIONS.length; i++) { - // Dimensions arrays may be of different lengths. Default to 0. - res.dimensions[i] = (this.dimensions[i] || 0) - (other.dimensions[i] || 0); - } // Invert and append other's units list onto res - - for (var _i2 = 0; _i2 < other.units.length; _i2++) { - // Make a shallow copy of every unit - var inverted = Unit_objectSpread(Unit_objectSpread({}, other.units[_i2]), {}, { - power: -other.units[_i2].power - }); - - res.units.push(inverted); - } // If at least one operand has a value, the result should have a value - - if (this.value !== null || other.value !== null) { - var valThis = this.value === null ? this._normalize(1) : this.value; - var valOther = other.value === null ? other._normalize(1) : other.value; - res.value = divideScalar(valThis, valOther); - } else { - res.value = null; - } - - res.skipAutomaticSimplification = false; - return getNumericIfUnitless(res); - }; - /** - * Calculate the power of a unit - * @memberof Unit - * @param {number | Fraction | BigNumber} p - * @returns {Unit} The result: this^p - */ - - Unit.prototype.pow = function (p) { - var res = this.clone(); - - for (var i = 0; i < BASE_DIMENSIONS.length; i++) { - // Dimensions arrays may be of different lengths. Default to 0. - res.dimensions[i] = (this.dimensions[i] || 0) * p; - } // Adjust the power of each unit in the list - - for (var _i3 = 0; _i3 < res.units.length; _i3++) { - res.units[_i3].power *= p; - } - - if (res.value !== null) { - res.value = pow(res.value, p); // only allow numeric output, we don't want to return a Complex number - // if (!isNumeric(res.value)) { - // res.value = NaN - // } - // Update: Complex supported now - } else { - res.value = null; - } - - res.skipAutomaticSimplification = false; - return getNumericIfUnitless(res); - }; - /** - * Return the numeric value of this unit if it is dimensionless, has a value, and config.predictable == false; or the original unit otherwise - * @param {Unit} unit - * @returns {number | Fraction | BigNumber | Unit} The numeric value of the unit if conditions are met, or the original unit otherwise - */ - - function getNumericIfUnitless(unit) { - if (unit.equalBase(BASE_UNITS.NONE) && unit.value !== null && !config.predictable) { - return unit.value; - } else { - return unit; - } - } - /** - * Calculate the absolute value of a unit - * @memberof Unit - * @param {number | Fraction | BigNumber} x - * @returns {Unit} The result: |x|, absolute value of x - */ - - Unit.prototype.abs = function () { - // This gives correct, but unexpected, results for units with an offset. - // For example, abs(-283.15 degC) = -263.15 degC !!! - var ret = this.clone(); - ret.value = ret.value !== null ? abs(ret.value) : null; - - for (var i in ret.units) { - if (ret.units[i].unit.name === 'VA' || ret.units[i].unit.name === 'VAR') { - ret.units[i].unit = UNITS.W; - } - } - - return ret; - }; - /** - * Convert the unit to a specific unit name. - * @memberof Unit - * @param {string | Unit} valuelessUnit A unit without value. Can have prefix, like "cm" - * @returns {Unit} Returns a clone of the unit with a fixed prefix and unit. - */ - - Unit.prototype.to = function (valuelessUnit) { - var other; - var value = this.value === null ? this._normalize(1) : this.value; - - if (typeof valuelessUnit === 'string') { - // other = new Unit(null, valuelessUnit) - other = Unit.parse(valuelessUnit); - - if (!this.equalBase(other)) { - throw new Error("Units do not match ('".concat(other.toString(), "' != '").concat(this.toString(), "')")); - } - - if (other.value !== null) { - throw new Error('Cannot convert to a unit with a value'); - } - - other.value = Object(utils_object["a" /* clone */])(value); - other.fixPrefix = true; - other.skipAutomaticSimplification = true; - return other; - } else if (Object(is["L" /* isUnit */])(valuelessUnit)) { - if (!this.equalBase(valuelessUnit)) { - throw new Error("Units do not match ('".concat(valuelessUnit.toString(), "' != '").concat(this.toString(), "')")); - } - - if (valuelessUnit.value !== null) { - throw new Error('Cannot convert to a unit with a value'); - } - - other = valuelessUnit.clone(); - other.value = Object(utils_object["a" /* clone */])(value); - other.fixPrefix = true; - other.skipAutomaticSimplification = true; - return other; - } else { - throw new Error('String or Unit expected as parameter'); - } - }; - /** - * Return the value of the unit when represented with given valueless unit - * @memberof Unit - * @param {string | Unit} valuelessUnit For example 'cm' or 'inch' - * @return {number} Returns the unit value as number. - */ - // TODO: deprecate Unit.toNumber? It's always better to use toNumeric - - Unit.prototype.toNumber = function (valuelessUnit) { - return toNumber(this.toNumeric(valuelessUnit)); - }; - /** - * Return the value of the unit in the original numeric type - * @memberof Unit - * @param {string | Unit} valuelessUnit For example 'cm' or 'inch' - * @return {number | BigNumber | Fraction} Returns the unit value - */ - - Unit.prototype.toNumeric = function (valuelessUnit) { - var other; - - if (valuelessUnit) { - // Allow getting the numeric value without converting to a different unit - other = this.to(valuelessUnit); - } else { - other = this.clone(); - } - - if (other._isDerived() || other.units.length === 0) { - return other._denormalize(other.value); - } else { - return other._denormalize(other.value, other.units[0].prefix.value); - } - }; - /** - * Get a string representation of the unit. - * @memberof Unit - * @return {string} - */ - - Unit.prototype.toString = function () { - return this.format(); - }; - /** - * Get a JSON representation of the unit - * @memberof Unit - * @returns {Object} Returns a JSON object structured as: - * `{"mathjs": "Unit", "value": 2, "unit": "cm", "fixPrefix": false}` - */ - - Unit.prototype.toJSON = function () { - return { - mathjs: 'Unit', - value: this._denormalize(this.value), - unit: this.formatUnits(), - fixPrefix: this.fixPrefix - }; - }; - /** - * Instantiate a Unit from a JSON object - * @memberof Unit - * @param {Object} json A JSON object structured as: - * `{"mathjs": "Unit", "value": 2, "unit": "cm", "fixPrefix": false}` - * @return {Unit} - */ - - Unit.fromJSON = function (json) { - var unit = new Unit(json.value, json.unit); - unit.fixPrefix = json.fixPrefix || false; - return unit; - }; - /** - * Returns the string representation of the unit. - * @memberof Unit - * @return {string} - */ - - Unit.prototype.valueOf = Unit.prototype.toString; - /** - * Simplify this Unit's unit list and return a new Unit with the simplified list. - * The returned Unit will contain a list of the "best" units for formatting. - */ - - Unit.prototype.simplify = function () { - var ret = this.clone(); - var proposedUnitList = []; // Search for a matching base - - var matchingBase; - - for (var key in currentUnitSystem) { - if (Object(utils_object["f" /* hasOwnProperty */])(currentUnitSystem, key)) { - if (ret.hasBase(BASE_UNITS[key])) { - matchingBase = key; - break; - } - } - } - - if (matchingBase === 'NONE') { - ret.units = []; - } else { - var matchingUnit; - - if (matchingBase) { - // Does the unit system have a matching unit? - if (Object(utils_object["f" /* hasOwnProperty */])(currentUnitSystem, matchingBase)) { - matchingUnit = currentUnitSystem[matchingBase]; - } - } - - if (matchingUnit) { - ret.units = [{ - unit: matchingUnit.unit, - prefix: matchingUnit.prefix, - power: 1.0 - }]; - } else { - // Multiple units or units with powers are formatted like this: - // 5 (kg m^2) / (s^3 mol) - // Build an representation from the base units of the current unit system - var missingBaseDim = false; - - for (var i = 0; i < BASE_DIMENSIONS.length; i++) { - var baseDim = BASE_DIMENSIONS[i]; - - if (Math.abs(ret.dimensions[i] || 0) > 1e-12) { - if (Object(utils_object["f" /* hasOwnProperty */])(currentUnitSystem, baseDim)) { - proposedUnitList.push({ - unit: currentUnitSystem[baseDim].unit, - prefix: currentUnitSystem[baseDim].prefix, - power: ret.dimensions[i] || 0 - }); - } else { - missingBaseDim = true; - } - } - } // Is the proposed unit list "simpler" than the existing one? - - if (proposedUnitList.length < ret.units.length && !missingBaseDim) { - // Replace this unit list with the proposed list - ret.units = proposedUnitList; - } - } - } - - return ret; - }; - /** - * Returns a new Unit in the SI system with the same value as this one - */ - - Unit.prototype.toSI = function () { - var ret = this.clone(); - var proposedUnitList = []; // Multiple units or units with powers are formatted like this: - // 5 (kg m^2) / (s^3 mol) - // Build an representation from the base units of the SI unit system - - for (var i = 0; i < BASE_DIMENSIONS.length; i++) { - var baseDim = BASE_DIMENSIONS[i]; - - if (Math.abs(ret.dimensions[i] || 0) > 1e-12) { - if (Object(utils_object["f" /* hasOwnProperty */])(UNIT_SYSTEMS.si, baseDim)) { - proposedUnitList.push({ - unit: UNIT_SYSTEMS.si[baseDim].unit, - prefix: UNIT_SYSTEMS.si[baseDim].prefix, - power: ret.dimensions[i] || 0 - }); - } else { - throw new Error('Cannot express custom unit ' + baseDim + ' in SI units'); - } - } - } // Replace this unit list with the proposed list - - ret.units = proposedUnitList; - ret.fixPrefix = true; - ret.skipAutomaticSimplification = true; - return ret; - }; - /** - * Get a string representation of the units of this Unit, without the value. The unit list is formatted as-is without first being simplified. - * @memberof Unit - * @return {string} - */ - - Unit.prototype.formatUnits = function () { - var strNum = ''; - var strDen = ''; - var nNum = 0; - var nDen = 0; - - for (var i = 0; i < this.units.length; i++) { - if (this.units[i].power > 0) { - nNum++; - strNum += ' ' + this.units[i].prefix.name + this.units[i].unit.name; - - if (Math.abs(this.units[i].power - 1.0) > 1e-15) { - strNum += '^' + this.units[i].power; - } - } else if (this.units[i].power < 0) { - nDen++; - } - } - - if (nDen > 0) { - for (var _i4 = 0; _i4 < this.units.length; _i4++) { - if (this.units[_i4].power < 0) { - if (nNum > 0) { - strDen += ' ' + this.units[_i4].prefix.name + this.units[_i4].unit.name; - - if (Math.abs(this.units[_i4].power + 1.0) > 1e-15) { - strDen += '^' + -this.units[_i4].power; - } - } else { - strDen += ' ' + this.units[_i4].prefix.name + this.units[_i4].unit.name; - strDen += '^' + this.units[_i4].power; - } - } - } - } // Remove leading " " - - strNum = strNum.substr(1); - strDen = strDen.substr(1); // Add parans for better copy/paste back into evaluate, for example, or for better pretty print formatting - - if (nNum > 1 && nDen > 0) { - strNum = '(' + strNum + ')'; - } - - if (nDen > 1 && nNum > 0) { - strDen = '(' + strDen + ')'; - } - - var str = strNum; - - if (nNum > 0 && nDen > 0) { - str += ' / '; - } - - str += strDen; - return str; - }; - /** - * Get a string representation of the Unit, with optional formatting options. - * @memberof Unit - * @param {Object | number | Function} [options] Formatting options. See - * lib/utils/number:format for a - * description of the available - * options. - * @return {string} - */ - - Unit.prototype.format = function (options) { - // Simplfy the unit list, unless it is valueless or was created directly in the - // constructor or as the result of to or toSI - var simp = this.skipAutomaticSimplification || this.value === null ? this.clone() : this.simplify(); // Apply some custom logic for handling VA and VAR. The goal is to express the value of the unit as a real value, if possible. Otherwise, use a real-valued unit instead of a complex-valued one. - - var isImaginary = false; - - if (typeof simp.value !== 'undefined' && simp.value !== null && Object(is["j" /* isComplex */])(simp.value)) { - // TODO: Make this better, for example, use relative magnitude of re and im rather than absolute - isImaginary = Math.abs(simp.value.re) < 1e-14; - } - - for (var i in simp.units) { - if (Object(utils_object["f" /* hasOwnProperty */])(simp.units, i)) { - if (simp.units[i].unit) { - if (simp.units[i].unit.name === 'VA' && isImaginary) { - simp.units[i].unit = UNITS.VAR; - } else if (simp.units[i].unit.name === 'VAR' && !isImaginary) { - simp.units[i].unit = UNITS.VA; - } - } - } - } // Now apply the best prefix - // Units must have only one unit and not have the fixPrefix flag set - - if (simp.units.length === 1 && !simp.fixPrefix) { - // Units must have integer powers, otherwise the prefix will change the - // outputted value by not-an-integer-power-of-ten - if (Math.abs(simp.units[0].power - Math.round(simp.units[0].power)) < 1e-14) { - // Apply the best prefix - simp.units[0].prefix = simp._bestPrefix(); - } - } - - var value = simp._denormalize(simp.value); - - var str = simp.value !== null ? format(value, options || {}) : ''; - var unitStr = simp.formatUnits(); - - if (simp.value && Object(is["j" /* isComplex */])(simp.value)) { - str = '(' + str + ')'; // Surround complex values with ( ) to enable better parsing - } - - if (unitStr.length > 0 && str.length > 0) { - str += ' '; - } - - str += unitStr; - return str; - }; - /** - * Calculate the best prefix using current value. - * @memberof Unit - * @returns {Object} prefix - * @private - */ - - Unit.prototype._bestPrefix = function () { - if (this.units.length !== 1) { - throw new Error('Can only compute the best prefix for single units with integer powers, like kg, s^2, N^-1, and so forth!'); - } - - if (Math.abs(this.units[0].power - Math.round(this.units[0].power)) >= 1e-14) { - throw new Error('Can only compute the best prefix for single units with integer powers, like kg, s^2, N^-1, and so forth!'); - } // find the best prefix value (resulting in the value of which - // the absolute value of the log10 is closest to zero, - // though with a little offset of 1.2 for nicer values: you get a - // sequence 1mm 100mm 500mm 0.6m 1m 10m 100m 500m 0.6km 1km ... - // Note: the units value can be any numeric type, but to find the best - // prefix it's enough to work with limited precision of a regular number - // Update: using mathjs abs since we also allow complex numbers - - var absValue = this.value !== null ? abs(this.value) : 0; - var absUnitValue = abs(this.units[0].unit.value); - var bestPrefix = this.units[0].prefix; - - if (absValue === 0) { - return bestPrefix; - } - - var power = this.units[0].power; - var bestDiff = Math.log(absValue / Math.pow(bestPrefix.value * absUnitValue, power)) / Math.LN10 - 1.2; - if (bestDiff > -2.200001 && bestDiff < 1.800001) { - return bestPrefix; - } // Allow the original prefix - - bestDiff = Math.abs(bestDiff); - var prefixes = this.units[0].unit.prefixes; - - for (var p in prefixes) { - if (Object(utils_object["f" /* hasOwnProperty */])(prefixes, p)) { - var prefix = prefixes[p]; - - if (prefix.scientific) { - var diff = Math.abs(Math.log(absValue / Math.pow(prefix.value * absUnitValue, power)) / Math.LN10 - 1.2); - - if (diff < bestDiff || diff === bestDiff && prefix.name.length < bestPrefix.name.length) { - // choose the prefix with the smallest diff, or if equal, choose the one - // with the shortest name (can happen with SHORTLONG for example) - bestPrefix = prefix; - bestDiff = diff; - } - } - } - } - - return bestPrefix; - }; - /** - * Returns an array of units whose sum is equal to this unit - * @memberof Unit - * @param {Array} [parts] An array of strings or valueless units. - * - * Example: - * - * const u = new Unit(1, 'm') - * u.splitUnit(['feet', 'inch']) - * [ 3 feet, 3.3700787401575 inch ] - * - * @return {Array} An array of units. - */ - - Unit.prototype.splitUnit = function (parts) { - var x = this.clone(); - var ret = []; - - for (var i = 0; i < parts.length; i++) { - // Convert x to the requested unit - x = x.to(parts[i]); - if (i === parts.length - 1) { - break; - } // Get the numeric value of this unit - - var xNumeric = x.toNumeric(); // Check to see if xNumeric is nearly equal to an integer, - // since fix can incorrectly round down if there is round-off error - - var xRounded = round(xNumeric); - var xFixed = void 0; - var isNearlyEqual = equal(xRounded, xNumeric); - - if (isNearlyEqual) { - xFixed = xRounded; - } else { - xFixed = fix(x.toNumeric()); - } - - var y = new Unit(xFixed, parts[i].toString()); - ret.push(y); - x = subtract(x, y); - } // This little bit fixes a bug where the remainder should be 0 but is a little bit off. - // But instead of comparing x, the remainder, with zero--we will compare the sum of - // all the parts so far with the original value. If they are nearly equal, - // we set the remainder to 0. - - var testSum = 0; - - for (var _i5 = 0; _i5 < ret.length; _i5++) { - testSum = addScalar(testSum, ret[_i5].value); - } - - if (equal(testSum, this.value)) { - x.value = 0; - } - - ret.push(x); - return ret; - }; - - var PREFIXES = { - NONE: { - '': { - name: '', - value: 1, - scientific: true - } - }, - SHORT: { - '': { - name: '', - value: 1, - scientific: true - }, - da: { - name: 'da', - value: 1e1, - scientific: false - }, - h: { - name: 'h', - value: 1e2, - scientific: false - }, - k: { - name: 'k', - value: 1e3, - scientific: true - }, - M: { - name: 'M', - value: 1e6, - scientific: true - }, - G: { - name: 'G', - value: 1e9, - scientific: true - }, - T: { - name: 'T', - value: 1e12, - scientific: true - }, - P: { - name: 'P', - value: 1e15, - scientific: true - }, - E: { - name: 'E', - value: 1e18, - scientific: true - }, - Z: { - name: 'Z', - value: 1e21, - scientific: true - }, - Y: { - name: 'Y', - value: 1e24, - scientific: true - }, - d: { - name: 'd', - value: 1e-1, - scientific: false - }, - c: { - name: 'c', - value: 1e-2, - scientific: false - }, - m: { - name: 'm', - value: 1e-3, - scientific: true - }, - u: { - name: 'u', - value: 1e-6, - scientific: true - }, - n: { - name: 'n', - value: 1e-9, - scientific: true - }, - p: { - name: 'p', - value: 1e-12, - scientific: true - }, - f: { - name: 'f', - value: 1e-15, - scientific: true - }, - a: { - name: 'a', - value: 1e-18, - scientific: true - }, - z: { - name: 'z', - value: 1e-21, - scientific: true - }, - y: { - name: 'y', - value: 1e-24, - scientific: true - } - }, - LONG: { - '': { - name: '', - value: 1, - scientific: true - }, - deca: { - name: 'deca', - value: 1e1, - scientific: false - }, - hecto: { - name: 'hecto', - value: 1e2, - scientific: false - }, - kilo: { - name: 'kilo', - value: 1e3, - scientific: true - }, - mega: { - name: 'mega', - value: 1e6, - scientific: true - }, - giga: { - name: 'giga', - value: 1e9, - scientific: true - }, - tera: { - name: 'tera', - value: 1e12, - scientific: true - }, - peta: { - name: 'peta', - value: 1e15, - scientific: true - }, - exa: { - name: 'exa', - value: 1e18, - scientific: true - }, - zetta: { - name: 'zetta', - value: 1e21, - scientific: true - }, - yotta: { - name: 'yotta', - value: 1e24, - scientific: true - }, - deci: { - name: 'deci', - value: 1e-1, - scientific: false - }, - centi: { - name: 'centi', - value: 1e-2, - scientific: false - }, - milli: { - name: 'milli', - value: 1e-3, - scientific: true - }, - micro: { - name: 'micro', - value: 1e-6, - scientific: true - }, - nano: { - name: 'nano', - value: 1e-9, - scientific: true - }, - pico: { - name: 'pico', - value: 1e-12, - scientific: true - }, - femto: { - name: 'femto', - value: 1e-15, - scientific: true - }, - atto: { - name: 'atto', - value: 1e-18, - scientific: true - }, - zepto: { - name: 'zepto', - value: 1e-21, - scientific: true - }, - yocto: { - name: 'yocto', - value: 1e-24, - scientific: true - } - }, - SQUARED: { - '': { - name: '', - value: 1, - scientific: true - }, - da: { - name: 'da', - value: 1e2, - scientific: false - }, - h: { - name: 'h', - value: 1e4, - scientific: false - }, - k: { - name: 'k', - value: 1e6, - scientific: true - }, - M: { - name: 'M', - value: 1e12, - scientific: true - }, - G: { - name: 'G', - value: 1e18, - scientific: true - }, - T: { - name: 'T', - value: 1e24, - scientific: true - }, - P: { - name: 'P', - value: 1e30, - scientific: true - }, - E: { - name: 'E', - value: 1e36, - scientific: true - }, - Z: { - name: 'Z', - value: 1e42, - scientific: true - }, - Y: { - name: 'Y', - value: 1e48, - scientific: true - }, - d: { - name: 'd', - value: 1e-2, - scientific: false - }, - c: { - name: 'c', - value: 1e-4, - scientific: false - }, - m: { - name: 'm', - value: 1e-6, - scientific: true - }, - u: { - name: 'u', - value: 1e-12, - scientific: true - }, - n: { - name: 'n', - value: 1e-18, - scientific: true - }, - p: { - name: 'p', - value: 1e-24, - scientific: true - }, - f: { - name: 'f', - value: 1e-30, - scientific: true - }, - a: { - name: 'a', - value: 1e-36, - scientific: true - }, - z: { - name: 'z', - value: 1e-42, - scientific: true - }, - y: { - name: 'y', - value: 1e-48, - scientific: true - } - }, - CUBIC: { - '': { - name: '', - value: 1, - scientific: true - }, - da: { - name: 'da', - value: 1e3, - scientific: false - }, - h: { - name: 'h', - value: 1e6, - scientific: false - }, - k: { - name: 'k', - value: 1e9, - scientific: true - }, - M: { - name: 'M', - value: 1e18, - scientific: true - }, - G: { - name: 'G', - value: 1e27, - scientific: true - }, - T: { - name: 'T', - value: 1e36, - scientific: true - }, - P: { - name: 'P', - value: 1e45, - scientific: true - }, - E: { - name: 'E', - value: 1e54, - scientific: true - }, - Z: { - name: 'Z', - value: 1e63, - scientific: true - }, - Y: { - name: 'Y', - value: 1e72, - scientific: true - }, - d: { - name: 'd', - value: 1e-3, - scientific: false - }, - c: { - name: 'c', - value: 1e-6, - scientific: false - }, - m: { - name: 'm', - value: 1e-9, - scientific: true - }, - u: { - name: 'u', - value: 1e-18, - scientific: true - }, - n: { - name: 'n', - value: 1e-27, - scientific: true - }, - p: { - name: 'p', - value: 1e-36, - scientific: true - }, - f: { - name: 'f', - value: 1e-45, - scientific: true - }, - a: { - name: 'a', - value: 1e-54, - scientific: true - }, - z: { - name: 'z', - value: 1e-63, - scientific: true - }, - y: { - name: 'y', - value: 1e-72, - scientific: true - } - }, - BINARY_SHORT_SI: { - '': { - name: '', - value: 1, - scientific: true - }, - k: { - name: 'k', - value: 1e3, - scientific: true - }, - M: { - name: 'M', - value: 1e6, - scientific: true - }, - G: { - name: 'G', - value: 1e9, - scientific: true - }, - T: { - name: 'T', - value: 1e12, - scientific: true - }, - P: { - name: 'P', - value: 1e15, - scientific: true - }, - E: { - name: 'E', - value: 1e18, - scientific: true - }, - Z: { - name: 'Z', - value: 1e21, - scientific: true - }, - Y: { - name: 'Y', - value: 1e24, - scientific: true - } - }, - BINARY_SHORT_IEC: { - '': { - name: '', - value: 1, - scientific: true - }, - Ki: { - name: 'Ki', - value: 1024, - scientific: true - }, - Mi: { - name: 'Mi', - value: Math.pow(1024, 2), - scientific: true - }, - Gi: { - name: 'Gi', - value: Math.pow(1024, 3), - scientific: true - }, - Ti: { - name: 'Ti', - value: Math.pow(1024, 4), - scientific: true - }, - Pi: { - name: 'Pi', - value: Math.pow(1024, 5), - scientific: true - }, - Ei: { - name: 'Ei', - value: Math.pow(1024, 6), - scientific: true - }, - Zi: { - name: 'Zi', - value: Math.pow(1024, 7), - scientific: true - }, - Yi: { - name: 'Yi', - value: Math.pow(1024, 8), - scientific: true - } - }, - BINARY_LONG_SI: { - '': { - name: '', - value: 1, - scientific: true - }, - kilo: { - name: 'kilo', - value: 1e3, - scientific: true - }, - mega: { - name: 'mega', - value: 1e6, - scientific: true - }, - giga: { - name: 'giga', - value: 1e9, - scientific: true - }, - tera: { - name: 'tera', - value: 1e12, - scientific: true - }, - peta: { - name: 'peta', - value: 1e15, - scientific: true - }, - exa: { - name: 'exa', - value: 1e18, - scientific: true - }, - zetta: { - name: 'zetta', - value: 1e21, - scientific: true - }, - yotta: { - name: 'yotta', - value: 1e24, - scientific: true - } - }, - BINARY_LONG_IEC: { - '': { - name: '', - value: 1, - scientific: true - }, - kibi: { - name: 'kibi', - value: 1024, - scientific: true - }, - mebi: { - name: 'mebi', - value: Math.pow(1024, 2), - scientific: true - }, - gibi: { - name: 'gibi', - value: Math.pow(1024, 3), - scientific: true - }, - tebi: { - name: 'tebi', - value: Math.pow(1024, 4), - scientific: true - }, - pebi: { - name: 'pebi', - value: Math.pow(1024, 5), - scientific: true - }, - exi: { - name: 'exi', - value: Math.pow(1024, 6), - scientific: true - }, - zebi: { - name: 'zebi', - value: Math.pow(1024, 7), - scientific: true - }, - yobi: { - name: 'yobi', - value: Math.pow(1024, 8), - scientific: true - } - }, - BTU: { - '': { - name: '', - value: 1, - scientific: true - }, - MM: { - name: 'MM', - value: 1e6, - scientific: true - } - } - }; - PREFIXES.SHORTLONG = _extends({}, PREFIXES.SHORT, PREFIXES.LONG); - PREFIXES.BINARY_SHORT = _extends({}, PREFIXES.BINARY_SHORT_SI, PREFIXES.BINARY_SHORT_IEC); - PREFIXES.BINARY_LONG = _extends({}, PREFIXES.BINARY_LONG_SI, PREFIXES.BINARY_LONG_IEC); - /* Internally, each unit is represented by a value and a dimension array. The elements of the dimensions array have the following meaning: - * Index Dimension - * ----- --------- - * 0 Length - * 1 Mass - * 2 Time - * 3 Current - * 4 Temperature - * 5 Luminous intensity - * 6 Amount of substance - * 7 Angle - * 8 Bit (digital) - * For example, the unit "298.15 K" is a pure temperature and would have a value of 298.15 and a dimension array of [0, 0, 0, 0, 1, 0, 0, 0, 0]. The unit "1 cal / (gm °C)" can be written in terms of the 9 fundamental dimensions as [length^2] / ([time^2] * [temperature]), and would a value of (after conversion to SI) 4184.0 and a dimensions array of [2, 0, -2, 0, -1, 0, 0, 0, 0]. - * - */ - - var BASE_DIMENSIONS = ['MASS', 'LENGTH', 'TIME', 'CURRENT', 'TEMPERATURE', 'LUMINOUS_INTENSITY', 'AMOUNT_OF_SUBSTANCE', 'ANGLE', 'BIT']; - var BASE_UNITS = { - NONE: { - dimensions: [0, 0, 0, 0, 0, 0, 0, 0, 0] - }, - MASS: { - dimensions: [1, 0, 0, 0, 0, 0, 0, 0, 0] - }, - LENGTH: { - dimensions: [0, 1, 0, 0, 0, 0, 0, 0, 0] - }, - TIME: { - dimensions: [0, 0, 1, 0, 0, 0, 0, 0, 0] - }, - CURRENT: { - dimensions: [0, 0, 0, 1, 0, 0, 0, 0, 0] - }, - TEMPERATURE: { - dimensions: [0, 0, 0, 0, 1, 0, 0, 0, 0] - }, - LUMINOUS_INTENSITY: { - dimensions: [0, 0, 0, 0, 0, 1, 0, 0, 0] - }, - AMOUNT_OF_SUBSTANCE: { - dimensions: [0, 0, 0, 0, 0, 0, 1, 0, 0] - }, - FORCE: { - dimensions: [1, 1, -2, 0, 0, 0, 0, 0, 0] - }, - SURFACE: { - dimensions: [0, 2, 0, 0, 0, 0, 0, 0, 0] - }, - VOLUME: { - dimensions: [0, 3, 0, 0, 0, 0, 0, 0, 0] - }, - ENERGY: { - dimensions: [1, 2, -2, 0, 0, 0, 0, 0, 0] - }, - POWER: { - dimensions: [1, 2, -3, 0, 0, 0, 0, 0, 0] - }, - PRESSURE: { - dimensions: [1, -1, -2, 0, 0, 0, 0, 0, 0] - }, - ELECTRIC_CHARGE: { - dimensions: [0, 0, 1, 1, 0, 0, 0, 0, 0] - }, - ELECTRIC_CAPACITANCE: { - dimensions: [-1, -2, 4, 2, 0, 0, 0, 0, 0] - }, - ELECTRIC_POTENTIAL: { - dimensions: [1, 2, -3, -1, 0, 0, 0, 0, 0] - }, - ELECTRIC_RESISTANCE: { - dimensions: [1, 2, -3, -2, 0, 0, 0, 0, 0] - }, - ELECTRIC_INDUCTANCE: { - dimensions: [1, 2, -2, -2, 0, 0, 0, 0, 0] - }, - ELECTRIC_CONDUCTANCE: { - dimensions: [-1, -2, 3, 2, 0, 0, 0, 0, 0] - }, - MAGNETIC_FLUX: { - dimensions: [1, 2, -2, -1, 0, 0, 0, 0, 0] - }, - MAGNETIC_FLUX_DENSITY: { - dimensions: [1, 0, -2, -1, 0, 0, 0, 0, 0] - }, - FREQUENCY: { - dimensions: [0, 0, -1, 0, 0, 0, 0, 0, 0] - }, - ANGLE: { - dimensions: [0, 0, 0, 0, 0, 0, 0, 1, 0] - }, - BIT: { - dimensions: [0, 0, 0, 0, 0, 0, 0, 0, 1] - } - }; - - for (var key in BASE_UNITS) { - if (Object(utils_object["f" /* hasOwnProperty */])(BASE_UNITS, key)) { - BASE_UNITS[key].key = key; - } - } - - var BASE_UNIT_NONE = {}; - var UNIT_NONE = { - name: '', - base: BASE_UNIT_NONE, - value: 1, - offset: 0, - dimensions: BASE_DIMENSIONS.map(function (x) { - return 0; - }) - }; - var UNITS = { - // length - meter: { - name: 'meter', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - inch: { - name: 'inch', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 0.0254, - offset: 0 - }, - foot: { - name: 'foot', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 0.3048, - offset: 0 - }, - yard: { - name: 'yard', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 0.9144, - offset: 0 - }, - mile: { - name: 'mile', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 1609.344, - offset: 0 - }, - link: { - name: 'link', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 0.201168, - offset: 0 - }, - rod: { - name: 'rod', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 5.0292, - offset: 0 - }, - chain: { - name: 'chain', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 20.1168, - offset: 0 - }, - angstrom: { - name: 'angstrom', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 1e-10, - offset: 0 - }, - m: { - name: 'm', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - "in": { - name: 'in', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 0.0254, - offset: 0 - }, - ft: { - name: 'ft', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 0.3048, - offset: 0 - }, - yd: { - name: 'yd', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 0.9144, - offset: 0 - }, - mi: { - name: 'mi', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 1609.344, - offset: 0 - }, - li: { - name: 'li', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 0.201168, - offset: 0 - }, - rd: { - name: 'rd', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 5.029210, - offset: 0 - }, - ch: { - name: 'ch', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 20.1168, - offset: 0 - }, - mil: { - name: 'mil', - base: BASE_UNITS.LENGTH, - prefixes: PREFIXES.NONE, - value: 0.0000254, - offset: 0 - }, - // 1/1000 inch - // Surface - m2: { - name: 'm2', - base: BASE_UNITS.SURFACE, - prefixes: PREFIXES.SQUARED, - value: 1, - offset: 0 - }, - sqin: { - name: 'sqin', - base: BASE_UNITS.SURFACE, - prefixes: PREFIXES.NONE, - value: 0.00064516, - offset: 0 - }, - // 645.16 mm2 - sqft: { - name: 'sqft', - base: BASE_UNITS.SURFACE, - prefixes: PREFIXES.NONE, - value: 0.09290304, - offset: 0 - }, - // 0.09290304 m2 - sqyd: { - name: 'sqyd', - base: BASE_UNITS.SURFACE, - prefixes: PREFIXES.NONE, - value: 0.83612736, - offset: 0 - }, - // 0.83612736 m2 - sqmi: { - name: 'sqmi', - base: BASE_UNITS.SURFACE, - prefixes: PREFIXES.NONE, - value: 2589988.110336, - offset: 0 - }, - // 2.589988110336 km2 - sqrd: { - name: 'sqrd', - base: BASE_UNITS.SURFACE, - prefixes: PREFIXES.NONE, - value: 25.29295, - offset: 0 - }, - // 25.29295 m2 - sqch: { - name: 'sqch', - base: BASE_UNITS.SURFACE, - prefixes: PREFIXES.NONE, - value: 404.6873, - offset: 0 - }, - // 404.6873 m2 - sqmil: { - name: 'sqmil', - base: BASE_UNITS.SURFACE, - prefixes: PREFIXES.NONE, - value: 6.4516e-10, - offset: 0 - }, - // 6.4516 * 10^-10 m2 - acre: { - name: 'acre', - base: BASE_UNITS.SURFACE, - prefixes: PREFIXES.NONE, - value: 4046.86, - offset: 0 - }, - // 4046.86 m2 - hectare: { - name: 'hectare', - base: BASE_UNITS.SURFACE, - prefixes: PREFIXES.NONE, - value: 10000, - offset: 0 - }, - // 10000 m2 - // Volume - m3: { - name: 'm3', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.CUBIC, - value: 1, - offset: 0 - }, - L: { - name: 'L', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.SHORT, - value: 0.001, - offset: 0 - }, - // litre - l: { - name: 'l', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.SHORT, - value: 0.001, - offset: 0 - }, - // litre - litre: { - name: 'litre', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.LONG, - value: 0.001, - offset: 0 - }, - cuin: { - name: 'cuin', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 1.6387064e-5, - offset: 0 - }, - // 1.6387064e-5 m3 - cuft: { - name: 'cuft', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.028316846592, - offset: 0 - }, - // 28.316 846 592 L - cuyd: { - name: 'cuyd', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.764554857984, - offset: 0 - }, - // 764.554 857 984 L - teaspoon: { - name: 'teaspoon', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.000005, - offset: 0 - }, - // 5 mL - tablespoon: { - name: 'tablespoon', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.000015, - offset: 0 - }, - // 15 mL - // {name: 'cup', base: BASE_UNITS.VOLUME, prefixes: PREFIXES.NONE, value: 0.000240, offset: 0}, // 240 mL // not possible, we have already another cup - drop: { - name: 'drop', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 5e-8, - offset: 0 - }, - // 0.05 mL = 5e-8 m3 - gtt: { - name: 'gtt', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 5e-8, - offset: 0 - }, - // 0.05 mL = 5e-8 m3 - // Liquid volume - minim: { - name: 'minim', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.00000006161152, - offset: 0 - }, - // 0.06161152 mL - fluiddram: { - name: 'fluiddram', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.0000036966911, - offset: 0 - }, - // 3.696691 mL - fluidounce: { - name: 'fluidounce', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.00002957353, - offset: 0 - }, - // 29.57353 mL - gill: { - name: 'gill', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.0001182941, - offset: 0 - }, - // 118.2941 mL - cc: { - name: 'cc', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 1e-6, - offset: 0 - }, - // 1e-6 L - cup: { - name: 'cup', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.0002365882, - offset: 0 - }, - // 236.5882 mL - pint: { - name: 'pint', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.0004731765, - offset: 0 - }, - // 473.1765 mL - quart: { - name: 'quart', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.0009463529, - offset: 0 - }, - // 946.3529 mL - gallon: { - name: 'gallon', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.003785412, - offset: 0 - }, - // 3.785412 L - beerbarrel: { - name: 'beerbarrel', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.1173478, - offset: 0 - }, - // 117.3478 L - oilbarrel: { - name: 'oilbarrel', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.1589873, - offset: 0 - }, - // 158.9873 L - hogshead: { - name: 'hogshead', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.2384810, - offset: 0 - }, - // 238.4810 L - // {name: 'min', base: BASE_UNITS.VOLUME, prefixes: PREFIXES.NONE, value: 0.00000006161152, offset: 0}, // 0.06161152 mL // min is already in use as minute - fldr: { - name: 'fldr', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.0000036966911, - offset: 0 - }, - // 3.696691 mL - floz: { - name: 'floz', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.00002957353, - offset: 0 - }, - // 29.57353 mL - gi: { - name: 'gi', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.0001182941, - offset: 0 - }, - // 118.2941 mL - cp: { - name: 'cp', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.0002365882, - offset: 0 - }, - // 236.5882 mL - pt: { - name: 'pt', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.0004731765, - offset: 0 - }, - // 473.1765 mL - qt: { - name: 'qt', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.0009463529, - offset: 0 - }, - // 946.3529 mL - gal: { - name: 'gal', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.003785412, - offset: 0 - }, - // 3.785412 L - bbl: { - name: 'bbl', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.1173478, - offset: 0 - }, - // 117.3478 L - obl: { - name: 'obl', - base: BASE_UNITS.VOLUME, - prefixes: PREFIXES.NONE, - value: 0.1589873, - offset: 0 - }, - // 158.9873 L - // {name: 'hogshead', base: BASE_UNITS.VOLUME, prefixes: PREFIXES.NONE, value: 0.2384810, offset: 0}, // 238.4810 L // TODO: hh? - // Mass - g: { - name: 'g', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.SHORT, - value: 0.001, - offset: 0 - }, - gram: { - name: 'gram', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.LONG, - value: 0.001, - offset: 0 - }, - ton: { - name: 'ton', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.SHORT, - value: 907.18474, - offset: 0 - }, - t: { - name: 't', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.SHORT, - value: 1000, - offset: 0 - }, - tonne: { - name: 'tonne', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.LONG, - value: 1000, - offset: 0 - }, - grain: { - name: 'grain', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 64.79891e-6, - offset: 0 - }, - dram: { - name: 'dram', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 1.7718451953125e-3, - offset: 0 - }, - ounce: { - name: 'ounce', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 28.349523125e-3, - offset: 0 - }, - poundmass: { - name: 'poundmass', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 453.59237e-3, - offset: 0 - }, - hundredweight: { - name: 'hundredweight', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 45.359237, - offset: 0 - }, - stick: { - name: 'stick', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 115e-3, - offset: 0 - }, - stone: { - name: 'stone', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 6.35029318, - offset: 0 - }, - gr: { - name: 'gr', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 64.79891e-6, - offset: 0 - }, - dr: { - name: 'dr', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 1.7718451953125e-3, - offset: 0 - }, - oz: { - name: 'oz', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 28.349523125e-3, - offset: 0 - }, - lbm: { - name: 'lbm', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 453.59237e-3, - offset: 0 - }, - cwt: { - name: 'cwt', - base: BASE_UNITS.MASS, - prefixes: PREFIXES.NONE, - value: 45.359237, - offset: 0 - }, - // Time - s: { - name: 's', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - min: { - name: 'min', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 60, - offset: 0 - }, - h: { - name: 'h', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 3600, - offset: 0 - }, - second: { - name: 'second', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - sec: { - name: 'sec', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - minute: { - name: 'minute', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 60, - offset: 0 - }, - hour: { - name: 'hour', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 3600, - offset: 0 - }, - day: { - name: 'day', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 86400, - offset: 0 - }, - week: { - name: 'week', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 7 * 86400, - offset: 0 - }, - month: { - name: 'month', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 2629800, - // 1/12th of Julian year - offset: 0 - }, - year: { - name: 'year', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 31557600, - // Julian year - offset: 0 - }, - decade: { - name: 'decade', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 315576000, - // Julian decade - offset: 0 - }, - century: { - name: 'century', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 3155760000, - // Julian century - offset: 0 - }, - millennium: { - name: 'millennium', - base: BASE_UNITS.TIME, - prefixes: PREFIXES.NONE, - value: 31557600000, - // Julian millennium - offset: 0 - }, - // Frequency - hertz: { - name: 'Hertz', - base: BASE_UNITS.FREQUENCY, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0, - reciprocal: true - }, - Hz: { - name: 'Hz', - base: BASE_UNITS.FREQUENCY, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0, - reciprocal: true - }, - // Angle - rad: { - name: 'rad', - base: BASE_UNITS.ANGLE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - radian: { - name: 'radian', - base: BASE_UNITS.ANGLE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - // deg = rad / (2*pi) * 360 = rad / 0.017453292519943295769236907684888 - deg: { - name: 'deg', - base: BASE_UNITS.ANGLE, - prefixes: PREFIXES.SHORT, - value: null, - // will be filled in by calculateAngleValues() - offset: 0 - }, - degree: { - name: 'degree', - base: BASE_UNITS.ANGLE, - prefixes: PREFIXES.LONG, - value: null, - // will be filled in by calculateAngleValues() - offset: 0 - }, - // grad = rad / (2*pi) * 400 = rad / 0.015707963267948966192313216916399 - grad: { - name: 'grad', - base: BASE_UNITS.ANGLE, - prefixes: PREFIXES.SHORT, - value: null, - // will be filled in by calculateAngleValues() - offset: 0 - }, - gradian: { - name: 'gradian', - base: BASE_UNITS.ANGLE, - prefixes: PREFIXES.LONG, - value: null, - // will be filled in by calculateAngleValues() - offset: 0 - }, - // cycle = rad / (2*pi) = rad / 6.2831853071795864769252867665793 - cycle: { - name: 'cycle', - base: BASE_UNITS.ANGLE, - prefixes: PREFIXES.NONE, - value: null, - // will be filled in by calculateAngleValues() - offset: 0 - }, - // arcsec = rad / (3600 * (360 / 2 * pi)) = rad / 0.0000048481368110953599358991410235795 - arcsec: { - name: 'arcsec', - base: BASE_UNITS.ANGLE, - prefixes: PREFIXES.NONE, - value: null, - // will be filled in by calculateAngleValues() - offset: 0 - }, - // arcmin = rad / (60 * (360 / 2 * pi)) = rad / 0.00029088820866572159615394846141477 - arcmin: { - name: 'arcmin', - base: BASE_UNITS.ANGLE, - prefixes: PREFIXES.NONE, - value: null, - // will be filled in by calculateAngleValues() - offset: 0 - }, - // Electric current - A: { - name: 'A', - base: BASE_UNITS.CURRENT, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - ampere: { - name: 'ampere', - base: BASE_UNITS.CURRENT, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - // Temperature - // K(C) = °C + 273.15 - // K(F) = (°F + 459.67) / 1.8 - // K(R) = °R / 1.8 - K: { - name: 'K', - base: BASE_UNITS.TEMPERATURE, - prefixes: PREFIXES.NONE, - value: 1, - offset: 0 - }, - degC: { - name: 'degC', - base: BASE_UNITS.TEMPERATURE, - prefixes: PREFIXES.NONE, - value: 1, - offset: 273.15 - }, - degF: { - name: 'degF', - base: BASE_UNITS.TEMPERATURE, - prefixes: PREFIXES.NONE, - value: 1 / 1.8, - offset: 459.67 - }, - degR: { - name: 'degR', - base: BASE_UNITS.TEMPERATURE, - prefixes: PREFIXES.NONE, - value: 1 / 1.8, - offset: 0 - }, - kelvin: { - name: 'kelvin', - base: BASE_UNITS.TEMPERATURE, - prefixes: PREFIXES.NONE, - value: 1, - offset: 0 - }, - celsius: { - name: 'celsius', - base: BASE_UNITS.TEMPERATURE, - prefixes: PREFIXES.NONE, - value: 1, - offset: 273.15 - }, - fahrenheit: { - name: 'fahrenheit', - base: BASE_UNITS.TEMPERATURE, - prefixes: PREFIXES.NONE, - value: 1 / 1.8, - offset: 459.67 - }, - rankine: { - name: 'rankine', - base: BASE_UNITS.TEMPERATURE, - prefixes: PREFIXES.NONE, - value: 1 / 1.8, - offset: 0 - }, - // amount of substance - mol: { - name: 'mol', - base: BASE_UNITS.AMOUNT_OF_SUBSTANCE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - mole: { - name: 'mole', - base: BASE_UNITS.AMOUNT_OF_SUBSTANCE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - // luminous intensity - cd: { - name: 'cd', - base: BASE_UNITS.LUMINOUS_INTENSITY, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - candela: { - name: 'candela', - base: BASE_UNITS.LUMINOUS_INTENSITY, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - // TODO: units STERADIAN - // {name: 'sr', base: BASE_UNITS.STERADIAN, prefixes: PREFIXES.NONE, value: 1, offset: 0}, - // {name: 'steradian', base: BASE_UNITS.STERADIAN, prefixes: PREFIXES.NONE, value: 1, offset: 0}, - // Force - N: { - name: 'N', - base: BASE_UNITS.FORCE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - newton: { - name: 'newton', - base: BASE_UNITS.FORCE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - dyn: { - name: 'dyn', - base: BASE_UNITS.FORCE, - prefixes: PREFIXES.SHORT, - value: 0.00001, - offset: 0 - }, - dyne: { - name: 'dyne', - base: BASE_UNITS.FORCE, - prefixes: PREFIXES.LONG, - value: 0.00001, - offset: 0 - }, - lbf: { - name: 'lbf', - base: BASE_UNITS.FORCE, - prefixes: PREFIXES.NONE, - value: 4.4482216152605, - offset: 0 - }, - poundforce: { - name: 'poundforce', - base: BASE_UNITS.FORCE, - prefixes: PREFIXES.NONE, - value: 4.4482216152605, - offset: 0 - }, - kip: { - name: 'kip', - base: BASE_UNITS.FORCE, - prefixes: PREFIXES.LONG, - value: 4448.2216, - offset: 0 - }, - // Energy - J: { - name: 'J', - base: BASE_UNITS.ENERGY, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - joule: { - name: 'joule', - base: BASE_UNITS.ENERGY, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - erg: { - name: 'erg', - base: BASE_UNITS.ENERGY, - prefixes: PREFIXES.NONE, - value: 1e-7, - offset: 0 - }, - Wh: { - name: 'Wh', - base: BASE_UNITS.ENERGY, - prefixes: PREFIXES.SHORT, - value: 3600, - offset: 0 - }, - BTU: { - name: 'BTU', - base: BASE_UNITS.ENERGY, - prefixes: PREFIXES.BTU, - value: 1055.05585262, - offset: 0 - }, - eV: { - name: 'eV', - base: BASE_UNITS.ENERGY, - prefixes: PREFIXES.SHORT, - value: 1.602176565e-19, - offset: 0 - }, - electronvolt: { - name: 'electronvolt', - base: BASE_UNITS.ENERGY, - prefixes: PREFIXES.LONG, - value: 1.602176565e-19, - offset: 0 - }, - // Power - W: { - name: 'W', - base: BASE_UNITS.POWER, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - watt: { - name: 'watt', - base: BASE_UNITS.POWER, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - hp: { - name: 'hp', - base: BASE_UNITS.POWER, - prefixes: PREFIXES.NONE, - value: 745.6998715386, - offset: 0 - }, - // Electrical power units - VAR: { - name: 'VAR', - base: BASE_UNITS.POWER, - prefixes: PREFIXES.SHORT, - value: Complex.I, - offset: 0 - }, - VA: { - name: 'VA', - base: BASE_UNITS.POWER, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Pressure - Pa: { - name: 'Pa', - base: BASE_UNITS.PRESSURE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - psi: { - name: 'psi', - base: BASE_UNITS.PRESSURE, - prefixes: PREFIXES.NONE, - value: 6894.75729276459, - offset: 0 - }, - atm: { - name: 'atm', - base: BASE_UNITS.PRESSURE, - prefixes: PREFIXES.NONE, - value: 101325, - offset: 0 - }, - bar: { - name: 'bar', - base: BASE_UNITS.PRESSURE, - prefixes: PREFIXES.SHORTLONG, - value: 100000, - offset: 0 - }, - torr: { - name: 'torr', - base: BASE_UNITS.PRESSURE, - prefixes: PREFIXES.NONE, - value: 133.322, - offset: 0 - }, - mmHg: { - name: 'mmHg', - base: BASE_UNITS.PRESSURE, - prefixes: PREFIXES.NONE, - value: 133.322, - offset: 0 - }, - mmH2O: { - name: 'mmH2O', - base: BASE_UNITS.PRESSURE, - prefixes: PREFIXES.NONE, - value: 9.80665, - offset: 0 - }, - cmH2O: { - name: 'cmH2O', - base: BASE_UNITS.PRESSURE, - prefixes: PREFIXES.NONE, - value: 98.0665, - offset: 0 - }, - // Electric charge - coulomb: { - name: 'coulomb', - base: BASE_UNITS.ELECTRIC_CHARGE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - C: { - name: 'C', - base: BASE_UNITS.ELECTRIC_CHARGE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Electric capacitance - farad: { - name: 'farad', - base: BASE_UNITS.ELECTRIC_CAPACITANCE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - F: { - name: 'F', - base: BASE_UNITS.ELECTRIC_CAPACITANCE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Electric potential - volt: { - name: 'volt', - base: BASE_UNITS.ELECTRIC_POTENTIAL, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - V: { - name: 'V', - base: BASE_UNITS.ELECTRIC_POTENTIAL, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Electric resistance - ohm: { - name: 'ohm', - base: BASE_UNITS.ELECTRIC_RESISTANCE, - prefixes: PREFIXES.SHORTLONG, - // Both Mohm and megaohm are acceptable - value: 1, - offset: 0 - }, - - /* - * Unicode breaks in browsers if charset is not specified - Ω: { - name: 'Ω', - base: BASE_UNITS.ELECTRIC_RESISTANCE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - */ - // Electric inductance - henry: { - name: 'henry', - base: BASE_UNITS.ELECTRIC_INDUCTANCE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - H: { - name: 'H', - base: BASE_UNITS.ELECTRIC_INDUCTANCE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Electric conductance - siemens: { - name: 'siemens', - base: BASE_UNITS.ELECTRIC_CONDUCTANCE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - S: { - name: 'S', - base: BASE_UNITS.ELECTRIC_CONDUCTANCE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Magnetic flux - weber: { - name: 'weber', - base: BASE_UNITS.MAGNETIC_FLUX, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - Wb: { - name: 'Wb', - base: BASE_UNITS.MAGNETIC_FLUX, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Magnetic flux density - tesla: { - name: 'tesla', - base: BASE_UNITS.MAGNETIC_FLUX_DENSITY, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - T: { - name: 'T', - base: BASE_UNITS.MAGNETIC_FLUX_DENSITY, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Binary - b: { - name: 'b', - base: BASE_UNITS.BIT, - prefixes: PREFIXES.BINARY_SHORT, - value: 1, - offset: 0 - }, - bits: { - name: 'bits', - base: BASE_UNITS.BIT, - prefixes: PREFIXES.BINARY_LONG, - value: 1, - offset: 0 - }, - B: { - name: 'B', - base: BASE_UNITS.BIT, - prefixes: PREFIXES.BINARY_SHORT, - value: 8, - offset: 0 - }, - bytes: { - name: 'bytes', - base: BASE_UNITS.BIT, - prefixes: PREFIXES.BINARY_LONG, - value: 8, - offset: 0 - } - }; // aliases (formerly plurals) - - var ALIASES = { - meters: 'meter', - inches: 'inch', - feet: 'foot', - yards: 'yard', - miles: 'mile', - links: 'link', - rods: 'rod', - chains: 'chain', - angstroms: 'angstrom', - lt: 'l', - litres: 'litre', - liter: 'litre', - liters: 'litre', - teaspoons: 'teaspoon', - tablespoons: 'tablespoon', - minims: 'minim', - fluiddrams: 'fluiddram', - fluidounces: 'fluidounce', - gills: 'gill', - cups: 'cup', - pints: 'pint', - quarts: 'quart', - gallons: 'gallon', - beerbarrels: 'beerbarrel', - oilbarrels: 'oilbarrel', - hogsheads: 'hogshead', - gtts: 'gtt', - grams: 'gram', - tons: 'ton', - tonnes: 'tonne', - grains: 'grain', - drams: 'dram', - ounces: 'ounce', - poundmasses: 'poundmass', - hundredweights: 'hundredweight', - sticks: 'stick', - lb: 'lbm', - lbs: 'lbm', - kips: 'kip', - acres: 'acre', - hectares: 'hectare', - sqfeet: 'sqft', - sqyard: 'sqyd', - sqmile: 'sqmi', - sqmiles: 'sqmi', - mmhg: 'mmHg', - mmh2o: 'mmH2O', - cmh2o: 'cmH2O', - seconds: 'second', - secs: 'second', - minutes: 'minute', - mins: 'minute', - hours: 'hour', - hr: 'hour', - hrs: 'hour', - days: 'day', - weeks: 'week', - months: 'month', - years: 'year', - decades: 'decade', - centuries: 'century', - millennia: 'millennium', - hertz: 'hertz', - radians: 'radian', - degrees: 'degree', - gradians: 'gradian', - cycles: 'cycle', - arcsecond: 'arcsec', - arcseconds: 'arcsec', - arcminute: 'arcmin', - arcminutes: 'arcmin', - BTUs: 'BTU', - watts: 'watt', - joules: 'joule', - amperes: 'ampere', - coulombs: 'coulomb', - volts: 'volt', - ohms: 'ohm', - farads: 'farad', - webers: 'weber', - teslas: 'tesla', - electronvolts: 'electronvolt', - moles: 'mole', - bit: 'bits', - "byte": 'bytes' - }; - /** - * Calculate the values for the angle units. - * Value is calculated as number or BigNumber depending on the configuration - * @param {{number: 'number' | 'BigNumber'}} config - */ - - function calculateAngleValues(config) { - if (config.number === 'BigNumber') { - var pi = createBigNumberPi(_BigNumber); - UNITS.rad.value = new _BigNumber(1); - UNITS.deg.value = pi.div(180); // 2 * pi / 360 - - UNITS.grad.value = pi.div(200); // 2 * pi / 400 - - UNITS.cycle.value = pi.times(2); // 2 * pi - - UNITS.arcsec.value = pi.div(648000); // 2 * pi / 360 / 3600 - - UNITS.arcmin.value = pi.div(10800); // 2 * pi / 360 / 60 - } else { - // number - UNITS.rad.value = 1; - UNITS.deg.value = Math.PI / 180; // 2 * pi / 360 - - UNITS.grad.value = Math.PI / 200; // 2 * pi / 400 - - UNITS.cycle.value = Math.PI * 2; // 2 * pi - - UNITS.arcsec.value = Math.PI / 648000; // 2 * pi / 360 / 3600 - - UNITS.arcmin.value = Math.PI / 10800; // 2 * pi / 360 / 60 - } // copy to the full names of the angles - - UNITS.radian.value = UNITS.rad.value; - UNITS.degree.value = UNITS.deg.value; - UNITS.gradian.value = UNITS.grad.value; - } // apply the angle values now - - calculateAngleValues(config); - - if (on) { - // recalculate the values on change of configuration - on('config', function (curr, prev) { - if (curr.number !== prev.number) { - calculateAngleValues(curr); - } - }); - } - /** - * A unit system is a set of dimensionally independent base units plus a set of derived units, formed by multiplication and division of the base units, that are by convention used with the unit system. - * A user perhaps could issue a command to select a preferred unit system, or use the default (see below). - * Auto unit system: The default unit system is updated on the fly anytime a unit is parsed. The corresponding unit in the default unit system is updated, so that answers are given in the same units the user supplies. - */ - - var UNIT_SYSTEMS = { - si: { - // Base units - NONE: { - unit: UNIT_NONE, - prefix: PREFIXES.NONE[''] - }, - LENGTH: { - unit: UNITS.m, - prefix: PREFIXES.SHORT[''] - }, - MASS: { - unit: UNITS.g, - prefix: PREFIXES.SHORT.k - }, - TIME: { - unit: UNITS.s, - prefix: PREFIXES.SHORT[''] - }, - CURRENT: { - unit: UNITS.A, - prefix: PREFIXES.SHORT[''] - }, - TEMPERATURE: { - unit: UNITS.K, - prefix: PREFIXES.SHORT[''] - }, - LUMINOUS_INTENSITY: { - unit: UNITS.cd, - prefix: PREFIXES.SHORT[''] - }, - AMOUNT_OF_SUBSTANCE: { - unit: UNITS.mol, - prefix: PREFIXES.SHORT[''] - }, - ANGLE: { - unit: UNITS.rad, - prefix: PREFIXES.SHORT[''] - }, - BIT: { - unit: UNITS.bits, - prefix: PREFIXES.SHORT[''] - }, - // Derived units - FORCE: { - unit: UNITS.N, - prefix: PREFIXES.SHORT[''] - }, - ENERGY: { - unit: UNITS.J, - prefix: PREFIXES.SHORT[''] - }, - POWER: { - unit: UNITS.W, - prefix: PREFIXES.SHORT[''] - }, - PRESSURE: { - unit: UNITS.Pa, - prefix: PREFIXES.SHORT[''] - }, - ELECTRIC_CHARGE: { - unit: UNITS.C, - prefix: PREFIXES.SHORT[''] - }, - ELECTRIC_CAPACITANCE: { - unit: UNITS.F, - prefix: PREFIXES.SHORT[''] - }, - ELECTRIC_POTENTIAL: { - unit: UNITS.V, - prefix: PREFIXES.SHORT[''] - }, - ELECTRIC_RESISTANCE: { - unit: UNITS.ohm, - prefix: PREFIXES.SHORT[''] - }, - ELECTRIC_INDUCTANCE: { - unit: UNITS.H, - prefix: PREFIXES.SHORT[''] - }, - ELECTRIC_CONDUCTANCE: { - unit: UNITS.S, - prefix: PREFIXES.SHORT[''] - }, - MAGNETIC_FLUX: { - unit: UNITS.Wb, - prefix: PREFIXES.SHORT[''] - }, - MAGNETIC_FLUX_DENSITY: { - unit: UNITS.T, - prefix: PREFIXES.SHORT[''] - }, - FREQUENCY: { - unit: UNITS.Hz, - prefix: PREFIXES.SHORT[''] - } - } - }; // Clone to create the other unit systems - - UNIT_SYSTEMS.cgs = JSON.parse(JSON.stringify(UNIT_SYSTEMS.si)); - UNIT_SYSTEMS.cgs.LENGTH = { - unit: UNITS.m, - prefix: PREFIXES.SHORT.c - }; - UNIT_SYSTEMS.cgs.MASS = { - unit: UNITS.g, - prefix: PREFIXES.SHORT[''] - }; - UNIT_SYSTEMS.cgs.FORCE = { - unit: UNITS.dyn, - prefix: PREFIXES.SHORT[''] - }; - UNIT_SYSTEMS.cgs.ENERGY = { - unit: UNITS.erg, - prefix: PREFIXES.NONE[''] - }; // there are wholly 4 unique cgs systems for electricity and magnetism, - // so let's not worry about it unless somebody complains - - UNIT_SYSTEMS.us = JSON.parse(JSON.stringify(UNIT_SYSTEMS.si)); - UNIT_SYSTEMS.us.LENGTH = { - unit: UNITS.ft, - prefix: PREFIXES.NONE[''] - }; - UNIT_SYSTEMS.us.MASS = { - unit: UNITS.lbm, - prefix: PREFIXES.NONE[''] - }; - UNIT_SYSTEMS.us.TEMPERATURE = { - unit: UNITS.degF, - prefix: PREFIXES.NONE[''] - }; - UNIT_SYSTEMS.us.FORCE = { - unit: UNITS.lbf, - prefix: PREFIXES.NONE[''] - }; - UNIT_SYSTEMS.us.ENERGY = { - unit: UNITS.BTU, - prefix: PREFIXES.BTU[''] - }; - UNIT_SYSTEMS.us.POWER = { - unit: UNITS.hp, - prefix: PREFIXES.NONE[''] - }; - UNIT_SYSTEMS.us.PRESSURE = { - unit: UNITS.psi, - prefix: PREFIXES.NONE[''] - }; // Add additional unit systems here. - // Choose a unit system to seed the auto unit system. - - UNIT_SYSTEMS.auto = JSON.parse(JSON.stringify(UNIT_SYSTEMS.si)); // Set the current unit system - - var currentUnitSystem = UNIT_SYSTEMS.auto; - /** - * Set a unit system for formatting derived units. - * @param {string} [name] The name of the unit system. - */ - - Unit.setUnitSystem = function (name) { - if (Object(utils_object["f" /* hasOwnProperty */])(UNIT_SYSTEMS, name)) { - currentUnitSystem = UNIT_SYSTEMS[name]; - } else { - throw new Error('Unit system ' + name + ' does not exist. Choices are: ' + Object.keys(UNIT_SYSTEMS).join(', ')); - } - }; - /** - * Return the current unit system. - * @return {string} The current unit system. - */ - - Unit.getUnitSystem = function () { - for (var _key in UNIT_SYSTEMS) { - if (Object(utils_object["f" /* hasOwnProperty */])(UNIT_SYSTEMS, _key)) { - if (UNIT_SYSTEMS[_key] === currentUnitSystem) { - return _key; - } - } - } - }; - /** - * Converters to convert from number to an other numeric type like BigNumber - * or Fraction - */ - - Unit.typeConverters = { - BigNumber: function BigNumber(x) { - return new _BigNumber(x + ''); // stringify to prevent constructor error - }, - Fraction: function Fraction(x) { - return new _Fraction(x); - }, - Complex: function Complex(x) { - return x; - }, - number: function number(x) { - return x; - } - }; - /** - * Retrieve the right convertor function corresponding with the type - * of provided exampleValue. - * - * @param {string} type A string 'number', 'BigNumber', or 'Fraction' - * In case of an unknown type, - * @return {Function} - */ - - Unit._getNumberConverter = function (type) { - if (!Unit.typeConverters[type]) { - throw new TypeError('Unsupported type "' + type + '"'); - } - - return Unit.typeConverters[type]; - }; // Add dimensions to each built-in unit - - for (var _key2 in UNITS) { - if (Object(utils_object["f" /* hasOwnProperty */])(UNITS, _key2)) { - var unit = UNITS[_key2]; - unit.dimensions = unit.base.dimensions; - } - } // Create aliases - - for (var _name2 in ALIASES) { - if (Object(utils_object["f" /* hasOwnProperty */])(ALIASES, _name2)) { - var _unit2 = UNITS[ALIASES[_name2]]; - var alias = {}; - - for (var _key3 in _unit2) { - if (Object(utils_object["f" /* hasOwnProperty */])(_unit2, _key3)) { - alias[_key3] = _unit2[_key3]; - } - } - - alias.name = _name2; - UNITS[_name2] = alias; - } - } - - function assertUnitNameIsValid(name) { - for (var i = 0; i < name.length; i++) { - var _c = name.charAt(i); - - var isValidAlpha = function isValidAlpha(p) { - return /^[a-zA-Z]$/.test(p); - }; - - var _isDigit = function _isDigit(c) { - return c >= '0' && c <= '9'; - }; - - if (i === 0 && !isValidAlpha(_c)) { - throw new Error('Invalid unit name (must begin with alpha character): "' + name + '"'); - } - - if (i > 0 && !(isValidAlpha(_c) || _isDigit(_c))) { - throw new Error('Invalid unit name (only alphanumeric characters are allowed): "' + name + '"'); - } - } - } - /** - * Wrapper around createUnitSingle. - * Example: - * createUnit({ - * foo: { }, - * bar: { - * definition: 'kg/foo', - * aliases: ['ba', 'barr', 'bars'], - * offset: 200 - * }, - * baz: '4 bar' - * }, - * { - * override: true - * }) - * @param {object} obj Object map. Each key becomes a unit which is defined by its value. - * @param {object} options - */ - - Unit.createUnit = function (obj, options) { - if (Unit_typeof(obj) !== 'object') { - throw new TypeError("createUnit expects first parameter to be of type 'Object'"); - } // Remove all units and aliases we are overriding - - if (options && options.override) { - for (var _key4 in obj) { - if (Object(utils_object["f" /* hasOwnProperty */])(obj, _key4)) { - Unit.deleteUnit(_key4); - } - - if (obj[_key4].aliases) { - for (var i = 0; i < obj[_key4].aliases.length; i++) { - Unit.deleteUnit(obj[_key4].aliases[i]); - } - } - } - } // TODO: traverse multiple times until all units have been added - - var lastUnit; - - for (var _key5 in obj) { - if (Object(utils_object["f" /* hasOwnProperty */])(obj, _key5)) { - lastUnit = Unit.createUnitSingle(_key5, obj[_key5]); - } - } - - return lastUnit; - }; - /** - * Create a user-defined unit and register it with the Unit type. - * Example: - * createUnitSingle('knot', '0.514444444 m/s') - * createUnitSingle('acre', new Unit(43560, 'ft^2')) - * - * @param {string} name The name of the new unit. Must be unique. Example: 'knot' - * @param {string, Unit, Object} definition Definition of the unit in terms - * of existing units. For example, '0.514444444 m / s'. Can be a Unit, a string, - * or an Object. If an Object, may have the following properties: - * - definition {string|Unit} The definition of this unit. - * - prefixes {string} "none", "short", "long", "binary_short", or "binary_long". - * The default is "none". - * - aliases {Array} Array of strings. Example: ['knots', 'kt', 'kts'] - * - offset {Numeric} An offset to apply when converting from the unit. For - * example, the offset for celsius is 273.15 and the offset for farhenheit - * is 459.67. Default is 0. - * - baseName {string} If the unit's dimension does not match that of any other - * base unit, the name of the newly create base unit. Otherwise, this property - * has no effect. - * - * @param {Object} options (optional) An object containing any of the following - * properties: - * - override {boolean} Whether this unit should be allowed to override existing - * units. - * - * @return {Unit} - */ - - Unit.createUnitSingle = function (name, obj, options) { - if (typeof obj === 'undefined' || obj === null) { - obj = {}; - } - - if (typeof name !== 'string') { - throw new TypeError("createUnitSingle expects first parameter to be of type 'string'"); - } // Check collisions with existing units - - if (Object(utils_object["f" /* hasOwnProperty */])(UNITS, name)) { - throw new Error('Cannot create unit "' + name + '": a unit with that name already exists'); - } // TODO: Validate name for collisions with other built-in functions (like abs or cos, for example), and for acceptable variable names. For example, '42' is probably not a valid unit. Nor is '%', since it is also an operator. - - assertUnitNameIsValid(name); - var defUnit = null; // The Unit from which the new unit will be created. - - var aliases = []; - var offset = 0; - var definition; - var prefixes; - var baseName; - - if (obj && obj.type === 'Unit') { - defUnit = obj.clone(); - } else if (typeof obj === 'string') { - if (obj !== '') { - definition = obj; - } - } else if (Unit_typeof(obj) === 'object') { - definition = obj.definition; - prefixes = obj.prefixes; - offset = obj.offset; - baseName = obj.baseName; - - if (obj.aliases) { - aliases = obj.aliases.valueOf(); // aliases could be a Matrix, so convert to Array - } - } else { - throw new TypeError('Cannot create unit "' + name + '" from "' + obj.toString() + '": expecting "string" or "Unit" or "Object"'); - } - - if (aliases) { - for (var i = 0; i < aliases.length; i++) { - if (Object(utils_object["f" /* hasOwnProperty */])(UNITS, aliases[i])) { - throw new Error('Cannot create alias "' + aliases[i] + '": a unit with that name already exists'); - } - } - } - - if (definition && typeof definition === 'string' && !defUnit) { - try { - defUnit = Unit.parse(definition, { - allowNoUnits: true - }); - } catch (ex) { - ex.message = 'Could not create unit "' + name + '" from "' + definition + '": ' + ex.message; - throw ex; - } - } else if (definition && definition.type === 'Unit') { - defUnit = definition.clone(); - } - - aliases = aliases || []; - offset = offset || 0; - - if (prefixes && prefixes.toUpperCase) { - prefixes = PREFIXES[prefixes.toUpperCase()] || PREFIXES.NONE; - } else { - prefixes = PREFIXES.NONE; - } // If defUnit is null, it is because the user did not - // specify a defintion. So create a new base dimension. - - var newUnit = {}; - - if (!defUnit) { - // Add a new base dimension - baseName = baseName || name + '_STUFF'; // foo --> foo_STUFF, or the essence of foo - - if (BASE_DIMENSIONS.indexOf(baseName) >= 0) { - throw new Error('Cannot create new base unit "' + name + '": a base unit with that name already exists (and cannot be overridden)'); - } - - BASE_DIMENSIONS.push(baseName); // Push 0 onto existing base units - - for (var b in BASE_UNITS) { - if (Object(utils_object["f" /* hasOwnProperty */])(BASE_UNITS, b)) { - BASE_UNITS[b].dimensions[BASE_DIMENSIONS.length - 1] = 0; - } - } // Add the new base unit - - var newBaseUnit = { - dimensions: [] - }; - - for (var _i6 = 0; _i6 < BASE_DIMENSIONS.length; _i6++) { - newBaseUnit.dimensions[_i6] = 0; - } - - newBaseUnit.dimensions[BASE_DIMENSIONS.length - 1] = 1; - newBaseUnit.key = baseName; - BASE_UNITS[baseName] = newBaseUnit; - newUnit = { - name: name, - value: 1, - dimensions: BASE_UNITS[baseName].dimensions.slice(0), - prefixes: prefixes, - offset: offset, - base: BASE_UNITS[baseName] - }; - currentUnitSystem[baseName] = { - unit: newUnit, - prefix: PREFIXES.NONE[''] - }; - } else { - newUnit = { - name: name, - value: defUnit.value, - dimensions: defUnit.dimensions.slice(0), - prefixes: prefixes, - offset: offset - }; // Create a new base if no matching base exists - - var anyMatch = false; - - for (var _i7 in BASE_UNITS) { - if (Object(utils_object["f" /* hasOwnProperty */])(BASE_UNITS, _i7)) { - var match = true; - - for (var j = 0; j < BASE_DIMENSIONS.length; j++) { - if (Math.abs((newUnit.dimensions[j] || 0) - (BASE_UNITS[_i7].dimensions[j] || 0)) > 1e-12) { - match = false; - break; - } - } - - if (match) { - anyMatch = true; - newUnit.base = BASE_UNITS[_i7]; - break; - } - } - } - - if (!anyMatch) { - baseName = baseName || name + '_STUFF'; // foo --> foo_STUFF, or the essence of foo - // Add the new base unit - - var _newBaseUnit = { - dimensions: defUnit.dimensions.slice(0) - }; - _newBaseUnit.key = baseName; - BASE_UNITS[baseName] = _newBaseUnit; - currentUnitSystem[baseName] = { - unit: newUnit, - prefix: PREFIXES.NONE[''] - }; - newUnit.base = BASE_UNITS[baseName]; - } - } - - Unit.UNITS[name] = newUnit; - - for (var _i8 = 0; _i8 < aliases.length; _i8++) { - var aliasName = aliases[_i8]; - var _alias = {}; - - for (var _key6 in newUnit) { - if (Object(utils_object["f" /* hasOwnProperty */])(newUnit, _key6)) { - _alias[_key6] = newUnit[_key6]; - } - } - - _alias.name = aliasName; - Unit.UNITS[aliasName] = _alias; - } - - return new Unit(null, name); - }; - - Unit.deleteUnit = function (name) { - delete Unit.UNITS[name]; - }; // expose arrays with prefixes, dimensions, units, systems - - Unit.PREFIXES = PREFIXES; - Unit.BASE_DIMENSIONS = BASE_DIMENSIONS; - Unit.BASE_UNITS = BASE_UNITS; - Unit.UNIT_SYSTEMS = UNIT_SYSTEMS; - Unit.UNITS = UNITS; - return Unit; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/type/unit/function/unit.js - - var unit_name = 'unit'; - var unit_dependencies = ['typed', 'Unit']; // This function is named createUnitFunction to prevent a naming conflict with createUnit - - var createUnitFunction = /* #__PURE__ */Object(factory["a" /* factory */])(unit_name, unit_dependencies, function (_ref) { - var typed = _ref.typed, - Unit = _ref.Unit; - - /** - * Create a unit. Depending on the passed arguments, the function - * will create and return a new math.Unit object. - * When a matrix is provided, all elements will be converted to units. - * - * Syntax: - * - * math.unit(unit : string) - * math.unit(value : number, unit : string) - * - * Examples: - * - * const a = math.unit(5, 'cm') // returns Unit 50 mm - * const b = math.unit('23 kg') // returns Unit 23 kg - * a.to('m') // returns Unit 0.05 m - * - * See also: - * - * bignumber, boolean, complex, index, matrix, number, string, createUnit - * - * @param {* | Array | Matrix} args A number and unit. - * @return {Unit | Array | Matrix} The created unit - */ - return typed(unit_name, { - Unit: function Unit(x) { - return x.clone(); - }, - string: function string(x) { - if (Unit.isValuelessUnit(x)) { - return new Unit(null, x); // a pure unit - } - - return Unit.parse(x, { - allowNoUnits: true - }); // a unit with value, like '5cm' - }, - 'number | BigNumber | Fraction | Complex, string': function numberBigNumberFractionComplexString(value, unit) { - return new Unit(value, unit); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/type/matrix/function/sparse.js - - var sparse_name = 'sparse'; - var sparse_dependencies = ['typed', 'SparseMatrix']; - var createSparse = /* #__PURE__ */Object(factory["a" /* factory */])(sparse_name, sparse_dependencies, function (_ref) { - var typed = _ref.typed, - SparseMatrix = _ref.SparseMatrix; - - /** - * Create a Sparse Matrix. The function creates a new `math.Matrix` object from - * an `Array`. A Matrix has utility functions to manipulate the data in the - * matrix, like getting the size and getting or setting values in the matrix. - * - * Syntax: - * - * math.sparse() // creates an empty sparse matrix. - * math.sparse(data) // creates a sparse matrix with initial data. - * math.sparse(data, 'number') // creates a sparse matrix with initial data, number datatype. - * - * Examples: - * - * let m = math.sparse([[1, 2], [3, 4]]) - * m.size() // Array [2, 2] - * m.resize([3, 2], 5) - * m.valueOf() // Array [[1, 2], [3, 4], [5, 5]] - * m.get([1, 0]) // number 3 - * - * See also: - * - * bignumber, boolean, complex, index, number, string, unit, matrix - * - * @param {Array | Matrix} [data] A two dimensional array - * - * @return {Matrix} The created matrix - */ - return typed(sparse_name, { - '': function _() { - return new SparseMatrix([]); - }, - string: function string(datatype) { - return new SparseMatrix([], datatype); - }, - 'Array | Matrix': function ArrayMatrix(data) { - return new SparseMatrix(data); - }, - 'Array | Matrix, string': function ArrayMatrixString(data, datatype) { - return new SparseMatrix(data, datatype); - } - }); - }); - // CONCATENATED MODULE: ./src/type/unit/function/createUnit.js - - var createUnit_name = 'createUnit'; - var createUnit_dependencies = ['typed', 'Unit']; - var createCreateUnit = /* #__PURE__ */Object(factory["a" /* factory */])(createUnit_name, createUnit_dependencies, function (_ref) { - var typed = _ref.typed, - Unit = _ref.Unit; - - /** - * Create a user-defined unit and register it with the Unit type. - * - * Syntax: - * - * math.createUnit({ - * baseUnit1: { - * aliases: [string, ...] - * prefixes: object - * }, - * unit2: { - * definition: string, - * aliases: [string, ...] - * prefixes: object, - * offset: number - * }, - * unit3: string // Shortcut - * }) - * - * // Another shortcut: - * math.createUnit(string, unit : string, [object]) - * - * Examples: - * - * math.createUnit('foo') - * math.createUnit('knot', {definition: '0.514444444 m/s', aliases: ['knots', 'kt', 'kts']}) - * math.createUnit('mph', '1 mile/hour') - * - * @param {string} name The name of the new unit. Must be unique. Example: 'knot' - * @param {string, Unit} definition Definition of the unit in terms of existing units. For example, '0.514444444 m / s'. - * @param {Object} options (optional) An object containing any of the following properties: - * - `prefixes {string}` "none", "short", "long", "binary_short", or "binary_long". The default is "none". - * - `aliases {Array}` Array of strings. Example: ['knots', 'kt', 'kts'] - * - `offset {Numeric}` An offset to apply when converting from the unit. For example, the offset for celsius is 273.15. Default is 0. - * - * See also: - * - * unit - * - * @return {Unit} The new unit - */ - return typed(createUnit_name, { - // General function signature. First parameter is an object where each property is the definition of a new unit. The object keys are the unit names and the values are the definitions. The values can be objects, strings, or Units. If a property is an empty object or an empty string, a new base unit is created. The second parameter is the options. - 'Object, Object': function ObjectObject(obj, options) { - return Unit.createUnit(obj, options); - }, - // Same as above but without the options. - Object: function Object(obj) { - return Unit.createUnit(obj, {}); - }, - // Shortcut method for creating one unit. - 'string, Unit | string | Object, Object': function stringUnitStringObjectObject(name, def, options) { - var obj = {}; - obj[name] = def; - return Unit.createUnit(obj, options); - }, - // Same as above but without the options. - 'string, Unit | string | Object': function stringUnitStringObject(name, def) { - var obj = {}; - obj[name] = def; - return Unit.createUnit(obj, {}); - }, - // Without a definition, creates a base unit. - string: function string(name) { - var obj = {}; - obj[name] = {}; - return Unit.createUnit(obj, {}); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/acos.js - - var acos_name = 'acos'; - var acos_dependencies = ['typed', 'config', 'Complex']; - var createAcos = /* #__PURE__ */Object(factory["a" /* factory */])(acos_name, acos_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - Complex = _ref.Complex; - - /** - * Calculate the inverse cosine of a value. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.acos(x) - * - * Examples: - * - * math.acos(0.5) // returns number 1.0471975511965979 - * math.acos(math.cos(1.5)) // returns number 1.5 - * - * math.acos(2) // returns Complex 0 + 1.3169578969248166 i - * - * See also: - * - * cos, atan, asin - * - * @param {number | BigNumber | Complex | Array | Matrix} x Function input - * @return {number | BigNumber | Complex | Array | Matrix} The arc cosine of x - */ - return typed(acos_name, { - number: function number(x) { - if (x >= -1 && x <= 1 || config.predictable) { - return Math.acos(x); - } else { - return new Complex(x, 0).acos(); - } - }, - Complex: function Complex(x) { - return x.acos(); - }, - BigNumber: function BigNumber(x) { - return x.acos(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/plain/number/trigonometry.js - - var trigonometry_n1 = 'number'; - var trigonometry_n2 = 'number, number'; - function acosNumber(x) { - return Math.acos(x); - } - acosNumber.signature = trigonometry_n1; - function acoshNumber(x) { - return Object(utils_number["a" /* acosh */])(x); - } - acoshNumber.signature = trigonometry_n1; - function acotNumber(x) { - return Math.atan(1 / x); - } - acotNumber.signature = trigonometry_n1; - function acothNumber(x) { - return isFinite(x) ? (Math.log((x + 1) / x) + Math.log(x / (x - 1))) / 2 : 0; - } - acothNumber.signature = trigonometry_n1; - function acscNumber(x) { - return Math.asin(1 / x); - } - acscNumber.signature = trigonometry_n1; - function acschNumber(x) { - var xInv = 1 / x; - return Math.log(xInv + Math.sqrt(xInv * xInv + 1)); - } - acschNumber.signature = trigonometry_n1; - function asecNumber(x) { - return Math.acos(1 / x); - } - asecNumber.signature = trigonometry_n1; - function asechNumber(x) { - var xInv = 1 / x; - var ret = Math.sqrt(xInv * xInv - 1); - return Math.log(ret + xInv); - } - asechNumber.signature = trigonometry_n1; - function asinNumber(x) { - return Math.asin(x); - } - asinNumber.signature = trigonometry_n1; - function asinhNumber(x) { - return Object(utils_number["b" /* asinh */])(x); - } - asinhNumber.signature = trigonometry_n1; - function atanNumber(x) { - return Math.atan(x); - } - atanNumber.signature = trigonometry_n1; - function atan2Number(y, x) { - return Math.atan2(y, x); - } - atan2Number.signature = trigonometry_n2; - function atanhNumber(x) { - return Object(utils_number["c" /* atanh */])(x); - } - atanhNumber.signature = trigonometry_n1; - function cosNumber(x) { - return Math.cos(x); - } - cosNumber.signature = trigonometry_n1; - function coshNumber(x) { - return Object(utils_number["e" /* cosh */])(x); - } - coshNumber.signature = trigonometry_n1; - function cotNumber(x) { - return 1 / Math.tan(x); - } - cotNumber.signature = trigonometry_n1; - function cothNumber(x) { - var e = Math.exp(2 * x); - return (e + 1) / (e - 1); - } - cothNumber.signature = trigonometry_n1; - function cscNumber(x) { - return 1 / Math.sin(x); - } - cscNumber.signature = trigonometry_n1; - function cschNumber(x) { - // consider values close to zero (+/-) - if (x === 0) { - return Number.POSITIVE_INFINITY; - } else { - return Math.abs(2 / (Math.exp(x) - Math.exp(-x))) * Object(utils_number["n" /* sign */])(x); - } - } - cschNumber.signature = trigonometry_n1; - function secNumber(x) { - return 1 / Math.cos(x); - } - secNumber.signature = trigonometry_n1; - function sechNumber(x) { - return 2 / (Math.exp(x) + Math.exp(-x)); - } - sechNumber.signature = trigonometry_n1; - function sinNumber(x) { - return Math.sin(x); - } - sinNumber.signature = trigonometry_n1; - function sinhNumber(x) { - return Object(utils_number["o" /* sinh */])(x); - } - sinhNumber.signature = trigonometry_n1; - function tanNumber(x) { - return Math.tan(x); - } - tanNumber.signature = trigonometry_n1; - function tanhNumber(x) { - return Object(utils_number["p" /* tanh */])(x); - } - tanhNumber.signature = trigonometry_n1; - // CONCATENATED MODULE: ./src/function/trigonometry/acosh.js - - var acosh_name = 'acosh'; - var acosh_dependencies = ['typed', 'config', 'Complex']; - var createAcosh = /* #__PURE__ */Object(factory["a" /* factory */])(acosh_name, acosh_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - Complex = _ref.Complex; - - /** - * Calculate the hyperbolic arccos of a value, - * defined as `acosh(x) = ln(sqrt(x^2 - 1) + x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.acosh(x) - * - * Examples: - * - * math.acosh(1.5) // returns 0.9624236501192069 - * - * See also: - * - * cosh, asinh, atanh - * - * @param {number | Complex | Unit | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Hyperbolic arccosine of x - */ - return typed(acosh_name, { - number: function number(x) { - if (x >= 1 || config.predictable) { - return acoshNumber(x); - } - - if (x <= -1) { - return new Complex(Math.log(Math.sqrt(x * x - 1) - x), Math.PI); - } - - return new Complex(x, 0).acosh(); - }, - Complex: function Complex(x) { - return x.acosh(); - }, - BigNumber: function BigNumber(x) { - return x.acosh(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/acot.js - - var acot_name = 'acot'; - var acot_dependencies = ['typed', 'BigNumber']; - var createAcot = /* #__PURE__ */Object(factory["a" /* factory */])(acot_name, acot_dependencies, function (_ref) { - var typed = _ref.typed, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the inverse cotangent of a value, defined as `acot(x) = atan(1/x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.acot(x) - * - * Examples: - * - * math.acot(0.5) // returns number 0.4636476090008061 - * math.acot(math.cot(1.5)) // returns number 1.5 - * - * math.acot(2) // returns Complex 1.5707963267948966 -1.3169578969248166 i - * - * See also: - * - * cot, atan - * - * @param {number | Complex | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} The arc cotangent of x - */ - return typed(acot_name, { - number: acotNumber, - Complex: function Complex(x) { - return x.acot(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x).atan(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/acoth.js - - var acoth_name = 'acoth'; - var acoth_dependencies = ['typed', 'config', 'Complex', 'BigNumber']; - var createAcoth = /* #__PURE__ */Object(factory["a" /* factory */])(acoth_name, acoth_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - Complex = _ref.Complex, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the hyperbolic arccotangent of a value, - * defined as `acoth(x) = atanh(1/x) = (ln((x+1)/x) + ln(x/(x-1))) / 2`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.acoth(x) - * - * Examples: - * - * math.acoth(0.5) // returns 0.8047189562170503 - * - * See also: - * - * acsch, asech - * - * @param {number | Complex | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Hyperbolic arccotangent of x - */ - return typed(acoth_name, { - number: function number(x) { - if (x >= 1 || x <= -1 || config.predictable) { - return acothNumber(x); - } - - return new Complex(x, 0).acoth(); - }, - Complex: function Complex(x) { - return x.acoth(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x).atanh(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/acsc.js - - var acsc_name = 'acsc'; - var acsc_dependencies = ['typed', 'config', 'Complex', 'BigNumber']; - var createAcsc = /* #__PURE__ */Object(factory["a" /* factory */])(acsc_name, acsc_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - Complex = _ref.Complex, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the inverse cosecant of a value, defined as `acsc(x) = asin(1/x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.acsc(x) - * - * Examples: - * - * math.acsc(0.5) // returns number 0.5235987755982989 - * math.acsc(math.csc(1.5)) // returns number ~1.5 - * - * math.acsc(2) // returns Complex 1.5707963267948966 -1.3169578969248166 i - * - * See also: - * - * csc, asin, asec - * - * @param {number | Complex | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} The arc cosecant of x - */ - return typed(acsc_name, { - number: function number(x) { - if (x <= -1 || x >= 1 || config.predictable) { - return acscNumber(x); - } - - return new Complex(x, 0).acsc(); - }, - Complex: function Complex(x) { - return x.acsc(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x).asin(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/acsch.js - - var acsch_name = 'acsch'; - var acsch_dependencies = ['typed', 'BigNumber']; - var createAcsch = /* #__PURE__ */Object(factory["a" /* factory */])(acsch_name, acsch_dependencies, function (_ref) { - var typed = _ref.typed, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the hyperbolic arccosecant of a value, - * defined as `acsch(x) = asinh(1/x) = ln(1/x + sqrt(1/x^2 + 1))`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.acsch(x) - * - * Examples: - * - * math.acsch(0.5) // returns 1.4436354751788103 - * - * See also: - * - * asech, acoth - * - * @param {number | Complex | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Hyperbolic arccosecant of x - */ - return typed(acsch_name, { - number: acschNumber, - Complex: function Complex(x) { - return x.acsch(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x).asinh(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/asec.js - - var asec_name = 'asec'; - var asec_dependencies = ['typed', 'config', 'Complex', 'BigNumber']; - var createAsec = /* #__PURE__ */Object(factory["a" /* factory */])(asec_name, asec_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - Complex = _ref.Complex, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the inverse secant of a value. Defined as `asec(x) = acos(1/x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.asec(x) - * - * Examples: - * - * math.asec(0.5) // returns 1.0471975511965979 - * math.asec(math.sec(1.5)) // returns 1.5 - * - * math.asec(2) // returns 0 + 1.3169578969248166 i - * - * See also: - * - * acos, acot, acsc - * - * @param {number | Complex | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} The arc secant of x - */ - return typed(asec_name, { - number: function number(x) { - if (x <= -1 || x >= 1 || config.predictable) { - return asecNumber(x); - } - - return new Complex(x, 0).asec(); - }, - Complex: function Complex(x) { - return x.asec(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x).acos(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/asech.js - - var asech_name = 'asech'; - var asech_dependencies = ['typed', 'config', 'Complex', 'BigNumber']; - var createAsech = /* #__PURE__ */Object(factory["a" /* factory */])(asech_name, asech_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - Complex = _ref.Complex, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the hyperbolic arcsecant of a value, - * defined as `asech(x) = acosh(1/x) = ln(sqrt(1/x^2 - 1) + 1/x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.asech(x) - * - * Examples: - * - * math.asech(0.5) // returns 1.3169578969248166 - * - * See also: - * - * acsch, acoth - * - * @param {number | Complex | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Hyperbolic arcsecant of x - */ - return typed(asech_name, { - number: function number(x) { - if (x <= 1 && x >= -1 || config.predictable) { - var xInv = 1 / x; - - if (xInv > 0 || config.predictable) { - return asechNumber(x); - } - - var ret = Math.sqrt(xInv * xInv - 1); - return new Complex(Math.log(ret - xInv), Math.PI); - } - - return new Complex(x, 0).asech(); - }, - Complex: function Complex(x) { - return x.asech(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x).acosh(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/asin.js - - var asin_name = 'asin'; - var asin_dependencies = ['typed', 'config', 'Complex']; - var createAsin = /* #__PURE__ */Object(factory["a" /* factory */])(asin_name, asin_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - Complex = _ref.Complex; - - /** - * Calculate the inverse sine of a value. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.asin(x) - * - * Examples: - * - * math.asin(0.5) // returns number 0.5235987755982989 - * math.asin(math.sin(1.5)) // returns number ~1.5 - * - * math.asin(2) // returns Complex 1.5707963267948966 -1.3169578969248166 i - * - * See also: - * - * sin, atan, acos - * - * @param {number | BigNumber | Complex | Array | Matrix} x Function input - * @return {number | BigNumber | Complex | Array | Matrix} The arc sine of x - */ - return typed(asin_name, { - number: function number(x) { - if (x >= -1 && x <= 1 || config.predictable) { - return Math.asin(x); - } else { - return new Complex(x, 0).asin(); - } - }, - Complex: function Complex(x) { - return x.asin(); - }, - BigNumber: function BigNumber(x) { - return x.asin(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since asin(0) = 0 - return deepMap(x, this, true); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/asinh.js - - var asinh_name = 'asinh'; - var asinh_dependencies = ['typed']; - var createAsinh = /* #__PURE__ */Object(factory["a" /* factory */])(asinh_name, asinh_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Calculate the hyperbolic arcsine of a value, - * defined as `asinh(x) = ln(x + sqrt(x^2 + 1))`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.asinh(x) - * - * Examples: - * - * math.asinh(0.5) // returns 0.48121182505960347 - * - * See also: - * - * acosh, atanh - * - * @param {number | Complex | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Hyperbolic arcsine of x - */ - return typed('asinh', { - number: asinhNumber, - Complex: function Complex(x) { - return x.asinh(); - }, - BigNumber: function BigNumber(x) { - return x.asinh(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since asinh(0) = 0 - return deepMap(x, this, true); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/atan.js - - var atan_name = 'atan'; - var atan_dependencies = ['typed']; - var createAtan = /* #__PURE__ */Object(factory["a" /* factory */])(atan_name, atan_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Calculate the inverse tangent of a value. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.atan(x) - * - * Examples: - * - * math.atan(0.5) // returns number 0.4636476090008061 - * math.atan(math.tan(1.5)) // returns number 1.5 - * - * math.atan(2) // returns Complex 1.5707963267948966 -1.3169578969248166 i - * - * See also: - * - * tan, asin, acos - * - * @param {number | BigNumber | Complex | Array | Matrix} x Function input - * @return {number | BigNumber | Complex | Array | Matrix} The arc tangent of x - */ - return typed('atan', { - number: function number(x) { - return Math.atan(x); - }, - Complex: function Complex(x) { - return x.atan(); - }, - BigNumber: function BigNumber(x) { - return x.atan(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since atan(0) = 0 - return deepMap(x, this, true); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/atan2.js - - var atan2_name = 'atan2'; - var atan2_dependencies = ['typed', 'matrix', 'equalScalar', 'BigNumber', 'DenseMatrix']; - var createAtan2 = /* #__PURE__ */Object(factory["a" /* factory */])(atan2_name, atan2_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - equalScalar = _ref.equalScalar, - BigNumber = _ref.BigNumber, - DenseMatrix = _ref.DenseMatrix; - var algorithm02 = createAlgorithm02({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm03 = createAlgorithm03({ - typed: typed - }); - var algorithm09 = createAlgorithm09({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm12 = createAlgorithm12({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Calculate the inverse tangent function with two arguments, y/x. - * By providing two arguments, the right quadrant of the computed angle can be - * determined. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.atan2(y, x) - * - * Examples: - * - * math.atan2(2, 2) / math.pi // returns number 0.25 - * - * const angle = math.unit(60, 'deg') // returns Unit 60 deg - * const x = math.cos(angle) - * const y = math.sin(angle) - * - * math.atan(2) // returns Complex 1.5707963267948966 -1.3169578969248166 i - * - * See also: - * - * tan, atan, sin, cos - * - * @param {number | Array | Matrix} y Second dimension - * @param {number | Array | Matrix} x First dimension - * @return {number | Array | Matrix} Four-quadrant inverse tangent - */ - - return typed(atan2_name, { - 'number, number': Math.atan2, - // Complex numbers doesn't seem to have a reasonable implementation of - // atan2(). Even Matlab removed the support, after they only calculated - // the atan only on base of the real part of the numbers and ignored the imaginary. - 'BigNumber, BigNumber': function BigNumberBigNumber(y, x) { - return BigNumber.atan2(y, x); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm09(x, y, this, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - // mind the order of y and x! - return algorithm02(y, x, this, true); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm03(x, y, this, false); - }, - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, this); - }, - 'Array, Array': function ArrayArray(x, y) { - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - return this(x, matrix(y)); - }, - 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { - return algorithm11(x, y, this, false); - }, - 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { - return algorithm14(x, y, this, false); - }, - 'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) { - // mind the order of y and x - return algorithm12(y, x, this, true); - }, - 'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) { - // mind the order of y and x - return algorithm14(y, x, this, true); - }, - 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) { - return algorithm14(matrix(x), y, this, false).valueOf(); - }, - 'number | BigNumber, Array': function numberBigNumberArray(x, y) { - return algorithm14(matrix(y), x, this, true).valueOf(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/atanh.js - - var atanh_name = 'atanh'; - var atanh_dependencies = ['typed', 'config', 'Complex']; - var createAtanh = /* #__PURE__ */Object(factory["a" /* factory */])(atanh_name, atanh_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - Complex = _ref.Complex; - - /** - * Calculate the hyperbolic arctangent of a value, - * defined as `atanh(x) = ln((1 + x)/(1 - x)) / 2`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.atanh(x) - * - * Examples: - * - * math.atanh(0.5) // returns 0.5493061443340549 - * - * See also: - * - * acosh, asinh - * - * @param {number | Complex | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Hyperbolic arctangent of x - */ - return typed(atanh_name, { - number: function number(x) { - if (x <= 1 && x >= -1 || config.predictable) { - return atanhNumber(x); - } - - return new Complex(x, 0).atanh(); - }, - Complex: function Complex(x) { - return x.atanh(); - }, - BigNumber: function BigNumber(x) { - return x.atanh(); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since atanh(0) = 0 - return deepMap(x, this, true); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/cos.js - - var cos_name = 'cos'; - var cos_dependencies = ['typed']; - var createCos = /* #__PURE__ */Object(factory["a" /* factory */])(cos_name, cos_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Calculate the cosine of a value. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.cos(x) - * - * Examples: - * - * math.cos(2) // returns number -0.4161468365471422 - * math.cos(math.pi / 4) // returns number 0.7071067811865475 - * math.cos(math.unit(180, 'deg')) // returns number -1 - * math.cos(math.unit(60, 'deg')) // returns number 0.5 - * - * const angle = 0.2 - * math.pow(math.sin(angle), 2) + math.pow(math.cos(angle), 2) // returns number ~1 - * - * See also: - * - * cos, tan - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input - * @return {number | BigNumber | Complex | Array | Matrix} Cosine of x - */ - return typed(cos_name, { - number: Math.cos, - Complex: function Complex(x) { - return x.cos(); - }, - BigNumber: function BigNumber(x) { - return x.cos(); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function cos is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/cosh.js - - var cosh_name = 'cosh'; - var cosh_dependencies = ['typed']; - var createCosh = /* #__PURE__ */Object(factory["a" /* factory */])(cosh_name, cosh_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Calculate the hyperbolic cosine of a value, - * defined as `cosh(x) = 1/2 * (exp(x) + exp(-x))`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.cosh(x) - * - * Examples: - * - * math.cosh(0.5) // returns number 1.1276259652063807 - * - * See also: - * - * sinh, tanh - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input - * @return {number | BigNumber | Complex | Array | Matrix} Hyperbolic cosine of x - */ - return typed(cosh_name, { - number: utils_number["e" /* cosh */], - Complex: function Complex(x) { - return x.cosh(); - }, - BigNumber: function BigNumber(x) { - return x.cosh(); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function cosh is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/cot.js - - var cot_name = 'cot'; - var cot_dependencies = ['typed', 'BigNumber']; - var createCot = /* #__PURE__ */Object(factory["a" /* factory */])(cot_name, cot_dependencies, function (_ref) { - var typed = _ref.typed, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the cotangent of a value. Defined as `cot(x) = 1 / tan(x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.cot(x) - * - * Examples: - * - * math.cot(2) // returns number -0.45765755436028577 - * 1 / math.tan(2) // returns number -0.45765755436028577 - * - * See also: - * - * tan, sec, csc - * - * @param {number | Complex | Unit | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Cotangent of x - */ - return typed(cot_name, { - number: cotNumber, - Complex: function Complex(x) { - return x.cot(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x.tan()); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function cot is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/coth.js - - var coth_name = 'coth'; - var coth_dependencies = ['typed', 'BigNumber']; - var createCoth = /* #__PURE__ */Object(factory["a" /* factory */])(coth_name, coth_dependencies, function (_ref) { - var typed = _ref.typed, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the hyperbolic cotangent of a value, - * defined as `coth(x) = 1 / tanh(x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.coth(x) - * - * Examples: - * - * // coth(x) = 1 / tanh(x) - * math.coth(2) // returns 1.0373147207275482 - * 1 / math.tanh(2) // returns 1.0373147207275482 - * - * See also: - * - * sinh, tanh, cosh - * - * @param {number | Complex | Unit | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Hyperbolic cotangent of x - */ - return typed(coth_name, { - number: cothNumber, - Complex: function Complex(x) { - return x.coth(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x.tanh()); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function coth is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/csc.js - - var csc_name = 'csc'; - var csc_dependencies = ['typed', 'BigNumber']; - var createCsc = /* #__PURE__ */Object(factory["a" /* factory */])(csc_name, csc_dependencies, function (_ref) { - var typed = _ref.typed, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the cosecant of a value, defined as `csc(x) = 1/sin(x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.csc(x) - * - * Examples: - * - * math.csc(2) // returns number 1.099750170294617 - * 1 / math.sin(2) // returns number 1.099750170294617 - * - * See also: - * - * sin, sec, cot - * - * @param {number | Complex | Unit | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Cosecant of x - */ - return typed(csc_name, { - number: cscNumber, - Complex: function Complex(x) { - return x.csc(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x.sin()); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function csc is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/csch.js - - var csch_name = 'csch'; - var csch_dependencies = ['typed', 'BigNumber']; - var createCsch = /* #__PURE__ */Object(factory["a" /* factory */])(csch_name, csch_dependencies, function (_ref) { - var typed = _ref.typed, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the hyperbolic cosecant of a value, - * defined as `csch(x) = 1 / sinh(x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.csch(x) - * - * Examples: - * - * // csch(x) = 1/ sinh(x) - * math.csch(0.5) // returns 1.9190347513349437 - * 1 / math.sinh(0.5) // returns 1.9190347513349437 - * - * See also: - * - * sinh, sech, coth - * - * @param {number | Complex | Unit | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Hyperbolic cosecant of x - */ - return typed(csch_name, { - number: cschNumber, - Complex: function Complex(x) { - return x.csch(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x.sinh()); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function csch is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/sec.js - - var sec_name = 'sec'; - var sec_dependencies = ['typed', 'BigNumber']; - var createSec = /* #__PURE__ */Object(factory["a" /* factory */])(sec_name, sec_dependencies, function (_ref) { - var typed = _ref.typed, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the secant of a value, defined as `sec(x) = 1/cos(x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.sec(x) - * - * Examples: - * - * math.sec(2) // returns number -2.4029979617223822 - * 1 / math.cos(2) // returns number -2.4029979617223822 - * - * See also: - * - * cos, csc, cot - * - * @param {number | Complex | Unit | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Secant of x - */ - return typed(sec_name, { - number: secNumber, - Complex: function Complex(x) { - return x.sec(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x.cos()); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function sec is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/sech.js - - var sech_name = 'sech'; - var sech_dependencies = ['typed', 'BigNumber']; - var createSech = /* #__PURE__ */Object(factory["a" /* factory */])(sech_name, sech_dependencies, function (_ref) { - var typed = _ref.typed, - _BigNumber = _ref.BigNumber; - - /** - * Calculate the hyperbolic secant of a value, - * defined as `sech(x) = 1 / cosh(x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.sech(x) - * - * Examples: - * - * // sech(x) = 1/ cosh(x) - * math.sech(0.5) // returns 0.886818883970074 - * 1 / math.cosh(0.5) // returns 0.886818883970074 - * - * See also: - * - * cosh, csch, coth - * - * @param {number | Complex | Unit | Array | Matrix} x Function input - * @return {number | Complex | Array | Matrix} Hyperbolic secant of x - */ - return typed(sech_name, { - number: sechNumber, - Complex: function Complex(x) { - return x.sech(); - }, - BigNumber: function BigNumber(x) { - return new _BigNumber(1).div(x.cosh()); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function sech is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - return deepMap(x, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/sin.js - - var sin_name = 'sin'; - var sin_dependencies = ['typed']; - var createSin = /* #__PURE__ */Object(factory["a" /* factory */])(sin_name, sin_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Calculate the sine of a value. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.sin(x) - * - * Examples: - * - * math.sin(2) // returns number 0.9092974268256813 - * math.sin(math.pi / 4) // returns number 0.7071067811865475 - * math.sin(math.unit(90, 'deg')) // returns number 1 - * math.sin(math.unit(30, 'deg')) // returns number 0.5 - * - * const angle = 0.2 - * math.pow(math.sin(angle), 2) + math.pow(math.cos(angle), 2) // returns number ~1 - * - * See also: - * - * cos, tan - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input - * @return {number | BigNumber | Complex | Array | Matrix} Sine of x - */ - return typed(sin_name, { - number: Math.sin, - Complex: function Complex(x) { - return x.sin(); - }, - BigNumber: function BigNumber(x) { - return x.sin(); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function sin is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since sin(0) = 0 - return deepMap(x, this, true); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/sinh.js - - var sinh_name = 'sinh'; - var sinh_dependencies = ['typed']; - var createSinh = /* #__PURE__ */Object(factory["a" /* factory */])(sinh_name, sinh_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Calculate the hyperbolic sine of a value, - * defined as `sinh(x) = 1/2 * (exp(x) - exp(-x))`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.sinh(x) - * - * Examples: - * - * math.sinh(0.5) // returns number 0.5210953054937474 - * - * See also: - * - * cosh, tanh - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input - * @return {number | BigNumber | Complex | Array | Matrix} Hyperbolic sine of x - */ - return typed(sinh_name, { - number: sinhNumber, - Complex: function Complex(x) { - return x.sinh(); - }, - BigNumber: function BigNumber(x) { - return x.sinh(); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function sinh is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since sinh(0) = 0 - return deepMap(x, this, true); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/tan.js - - var tan_name = 'tan'; - var tan_dependencies = ['typed']; - var createTan = /* #__PURE__ */Object(factory["a" /* factory */])(tan_name, tan_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Calculate the tangent of a value. `tan(x)` is equal to `sin(x) / cos(x)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.tan(x) - * - * Examples: - * - * math.tan(0.5) // returns number 0.5463024898437905 - * math.sin(0.5) / math.cos(0.5) // returns number 0.5463024898437905 - * math.tan(math.pi / 4) // returns number 1 - * math.tan(math.unit(45, 'deg')) // returns number 1 - * - * See also: - * - * atan, sin, cos - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input - * @return {number | BigNumber | Complex | Array | Matrix} Tangent of x - */ - return typed(tan_name, { - number: Math.tan, - Complex: function Complex(x) { - return x.tan(); - }, - BigNumber: function BigNumber(x) { - return x.tan(); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function tan is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since tan(0) = 0 - return deepMap(x, this, true); - } - }); - }); - // CONCATENATED MODULE: ./src/function/trigonometry/tanh.js - - var tanh_name = 'tanh'; - var tanh_dependencies = ['typed']; - var createTanh = /* #__PURE__ */Object(factory["a" /* factory */])(tanh_name, tanh_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Calculate the hyperbolic tangent of a value, - * defined as `tanh(x) = (exp(2 * x) - 1) / (exp(2 * x) + 1)`. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.tanh(x) - * - * Examples: - * - * // tanh(x) = sinh(x) / cosh(x) = 1 / coth(x) - * math.tanh(0.5) // returns 0.46211715726000974 - * math.sinh(0.5) / math.cosh(0.5) // returns 0.46211715726000974 - * 1 / math.coth(0.5) // returns 0.46211715726000974 - * - * See also: - * - * sinh, cosh, coth - * - * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input - * @return {number | BigNumber | Complex | Array | Matrix} Hyperbolic tangent of x - */ - return typed('tanh', { - number: utils_number["p" /* tanh */], - Complex: function Complex(x) { - return x.tanh(); - }, - BigNumber: function BigNumber(x) { - return x.tanh(); - }, - Unit: function Unit(x) { - if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) { - throw new TypeError('Unit in function tanh is no angle'); - } - - return this(x.value); - }, - 'Array | Matrix': function ArrayMatrix(x) { - // deep map collection, skip zeros since tanh(0) = 0 - return deepMap(x, this, true); - } - }); - }); - // CONCATENATED MODULE: ./src/function/set/setCartesian.js - - var setCartesian_name = 'setCartesian'; - var setCartesian_dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index', 'DenseMatrix']; - var createSetCartesian = /* #__PURE__ */Object(factory["a" /* factory */])(setCartesian_name, setCartesian_dependencies, function (_ref) { - var typed = _ref.typed, - size = _ref.size, - subset = _ref.subset, - compareNatural = _ref.compareNatural, - Index = _ref.Index, - DenseMatrix = _ref.DenseMatrix; - - /** - * Create the cartesian product of two (multi)sets. - * Multi-dimension arrays will be converted to single-dimension arrays before the operation. - * - * Syntax: - * - * math.setCartesian(set1, set2) - * - * Examples: - * - * math.setCartesian([1, 2], [3, 4]) // returns [[1, 3], [1, 4], [2, 3], [2, 4]] - * - * See also: - * - * setUnion, setIntersect, setDifference, setPowerset - * - * @param {Array | Matrix} a1 A (multi)set - * @param {Array | Matrix} a2 A (multi)set - * @return {Array | Matrix} The cartesian product of two (multi)sets - */ - return typed(setCartesian_name, { - 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) { - var result = []; - - if (subset(size(a1), new Index(0)) !== 0 && subset(size(a2), new Index(0)) !== 0) { - // if any of them is empty, return empty - var b1 = Object(utils_array["e" /* flatten */])(Array.isArray(a1) ? a1 : a1.toArray()).sort(compareNatural); - var b2 = Object(utils_array["e" /* flatten */])(Array.isArray(a2) ? a2 : a2.toArray()).sort(compareNatural); - result = []; - - for (var i = 0; i < b1.length; i++) { - for (var j = 0; j < b2.length; j++) { - result.push([b1[i], b2[j]]); - } - } - } // return an array, if both inputs were arrays - - if (Array.isArray(a1) && Array.isArray(a2)) { - return result; - } // return a matrix otherwise - - return new DenseMatrix(result); - } - }); - }); - // CONCATENATED MODULE: ./src/function/set/setDifference.js - - var setDifference_name = 'setDifference'; - var setDifference_dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index', 'DenseMatrix']; - var createSetDifference = /* #__PURE__ */Object(factory["a" /* factory */])(setDifference_name, setDifference_dependencies, function (_ref) { - var typed = _ref.typed, - size = _ref.size, - subset = _ref.subset, - compareNatural = _ref.compareNatural, - Index = _ref.Index, - DenseMatrix = _ref.DenseMatrix; - - /** - * Create the difference of two (multi)sets: every element of set1, that is not the element of set2. - * Multi-dimension arrays will be converted to single-dimension arrays before the operation. - * - * Syntax: - * - * math.setDifference(set1, set2) - * - * Examples: - * - * math.setDifference([1, 2, 3, 4], [3, 4, 5, 6]) // returns [1, 2] - * math.setDifference([[1, 2], [3, 4]], [[3, 4], [5, 6]]) // returns [1, 2] - * - * See also: - * - * setUnion, setIntersect, setSymDifference - * - * @param {Array | Matrix} a1 A (multi)set - * @param {Array | Matrix} a2 A (multi)set - * @return {Array | Matrix} The difference of two (multi)sets - */ - return typed(setDifference_name, { - 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) { - var result; - - if (subset(size(a1), new Index(0)) === 0) { - // empty-anything=empty - result = []; - } else if (subset(size(a2), new Index(0)) === 0) { - // anything-empty=anything - return Object(utils_array["e" /* flatten */])(a1.toArray()); - } else { - var b1 = Object(utils_array["i" /* identify */])(Object(utils_array["e" /* flatten */])(Array.isArray(a1) ? a1 : a1.toArray()).sort(compareNatural)); - var b2 = Object(utils_array["i" /* identify */])(Object(utils_array["e" /* flatten */])(Array.isArray(a2) ? a2 : a2.toArray()).sort(compareNatural)); - result = []; - var inb2; - - for (var i = 0; i < b1.length; i++) { - inb2 = false; - - for (var j = 0; j < b2.length; j++) { - if (compareNatural(b1[i].value, b2[j].value) === 0 && b1[i].identifier === b2[j].identifier) { - // the identifier is always a decimal int - inb2 = true; - break; - } - } - - if (!inb2) { - result.push(b1[i]); - } - } - } // return an array, if both inputs were arrays - - if (Array.isArray(a1) && Array.isArray(a2)) { - return Object(utils_array["g" /* generalize */])(result); - } // return a matrix otherwise - - return new DenseMatrix(Object(utils_array["g" /* generalize */])(result)); - } - }); - }); - // CONCATENATED MODULE: ./src/function/set/setDistinct.js - - var setDistinct_name = 'setDistinct'; - var setDistinct_dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index', 'DenseMatrix']; - var createSetDistinct = /* #__PURE__ */Object(factory["a" /* factory */])(setDistinct_name, setDistinct_dependencies, function (_ref) { - var typed = _ref.typed, - size = _ref.size, - subset = _ref.subset, - compareNatural = _ref.compareNatural, - Index = _ref.Index, - DenseMatrix = _ref.DenseMatrix; - - /** - * Collect the distinct elements of a multiset. - * A multi-dimension array will be converted to a single-dimension array before the operation. - * - * Syntax: - * - * math.setDistinct(set) - * - * Examples: - * - * math.setDistinct([1, 1, 1, 2, 2, 3]) // returns [1, 2, 3] - * - * See also: - * - * setMultiplicity - * - * @param {Array | Matrix} a A multiset - * @return {Array | Matrix} A set containing the distinc elements of the multiset - */ - return typed(setDistinct_name, { - 'Array | Matrix': function ArrayMatrix(a) { - var result; - - if (subset(size(a), new Index(0)) === 0) { - // if empty, return empty - result = []; - } else { - var b = Object(utils_array["e" /* flatten */])(Array.isArray(a) ? a : a.toArray()).sort(compareNatural); - result = []; - result.push(b[0]); - - for (var i = 1; i < b.length; i++) { - if (compareNatural(b[i], b[i - 1]) !== 0) { - result.push(b[i]); - } - } - } // return an array, if the input was an array - - if (Array.isArray(a)) { - return result; - } // return a matrix otherwise - - return new DenseMatrix(result); - } - }); - }); - // CONCATENATED MODULE: ./src/function/set/setIntersect.js - - var setIntersect_name = 'setIntersect'; - var setIntersect_dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index', 'DenseMatrix']; - var createSetIntersect = /* #__PURE__ */Object(factory["a" /* factory */])(setIntersect_name, setIntersect_dependencies, function (_ref) { - var typed = _ref.typed, - size = _ref.size, - subset = _ref.subset, - compareNatural = _ref.compareNatural, - Index = _ref.Index, - DenseMatrix = _ref.DenseMatrix; - - /** - * Create the intersection of two (multi)sets. - * Multi-dimension arrays will be converted to single-dimension arrays before the operation. - * - * Syntax: - * - * math.setIntersect(set1, set2) - * - * Examples: - * - * math.setIntersect([1, 2, 3, 4], [3, 4, 5, 6]) // returns [3, 4] - * math.setIntersect([[1, 2], [3, 4]], [[3, 4], [5, 6]]) // returns [3, 4] - * - * See also: - * - * setUnion, setDifference - * - * @param {Array | Matrix} a1 A (multi)set - * @param {Array | Matrix} a2 A (multi)set - * @return {Array | Matrix} The intersection of two (multi)sets - */ - return typed(setIntersect_name, { - 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) { - var result; - - if (subset(size(a1), new Index(0)) === 0 || subset(size(a2), new Index(0)) === 0) { - // of any of them is empty, return empty - result = []; - } else { - var b1 = Object(utils_array["i" /* identify */])(Object(utils_array["e" /* flatten */])(Array.isArray(a1) ? a1 : a1.toArray()).sort(compareNatural)); - var b2 = Object(utils_array["i" /* identify */])(Object(utils_array["e" /* flatten */])(Array.isArray(a2) ? a2 : a2.toArray()).sort(compareNatural)); - result = []; - - for (var i = 0; i < b1.length; i++) { - for (var j = 0; j < b2.length; j++) { - if (compareNatural(b1[i].value, b2[j].value) === 0 && b1[i].identifier === b2[j].identifier) { - // the identifier is always a decimal int - result.push(b1[i]); - break; - } - } - } - } // return an array, if both inputs were arrays - - if (Array.isArray(a1) && Array.isArray(a2)) { - return Object(utils_array["g" /* generalize */])(result); - } // return a matrix otherwise - - return new DenseMatrix(Object(utils_array["g" /* generalize */])(result)); - } - }); - }); - // CONCATENATED MODULE: ./src/function/set/setIsSubset.js - - var setIsSubset_name = 'setIsSubset'; - var setIsSubset_dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index']; - var createSetIsSubset = /* #__PURE__ */Object(factory["a" /* factory */])(setIsSubset_name, setIsSubset_dependencies, function (_ref) { - var typed = _ref.typed, - size = _ref.size, - subset = _ref.subset, - compareNatural = _ref.compareNatural, - Index = _ref.Index; - - /** - * Check whether a (multi)set is a subset of another (multi)set. (Every element of set1 is the element of set2.) - * Multi-dimension arrays will be converted to single-dimension arrays before the operation. - * - * Syntax: - * - * math.setIsSubset(set1, set2) - * - * Examples: - * - * math.setIsSubset([1, 2], [3, 4, 5, 6]) // returns false - * math.setIsSubset([3, 4], [3, 4, 5, 6]) // returns true - * - * See also: - * - * setUnion, setIntersect, setDifference - * - * @param {Array | Matrix} a1 A (multi)set - * @param {Array | Matrix} a2 A (multi)set - * @return {boolean} true | false - */ - return typed(setIsSubset_name, { - 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) { - if (subset(size(a1), new Index(0)) === 0) { - // empty is a subset of anything - return true; - } else if (subset(size(a2), new Index(0)) === 0) { - // anything is not a subset of empty - return false; - } - - var b1 = Object(utils_array["i" /* identify */])(Object(utils_array["e" /* flatten */])(Array.isArray(a1) ? a1 : a1.toArray()).sort(compareNatural)); - var b2 = Object(utils_array["i" /* identify */])(Object(utils_array["e" /* flatten */])(Array.isArray(a2) ? a2 : a2.toArray()).sort(compareNatural)); - var inb2; - - for (var i = 0; i < b1.length; i++) { - inb2 = false; - - for (var j = 0; j < b2.length; j++) { - if (compareNatural(b1[i].value, b2[j].value) === 0 && b1[i].identifier === b2[j].identifier) { - // the identifier is always a decimal int - inb2 = true; - break; - } - } - - if (inb2 === false) { - return false; - } - } - - return true; - } - }); - }); - // CONCATENATED MODULE: ./src/function/set/setMultiplicity.js - - var setMultiplicity_name = 'setMultiplicity'; - var setMultiplicity_dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index']; - var createSetMultiplicity = /* #__PURE__ */Object(factory["a" /* factory */])(setMultiplicity_name, setMultiplicity_dependencies, function (_ref) { - var typed = _ref.typed, - size = _ref.size, - subset = _ref.subset, - compareNatural = _ref.compareNatural, - Index = _ref.Index; - - /** - * Count the multiplicity of an element in a multiset. - * A multi-dimension array will be converted to a single-dimension array before the operation. - * - * Syntax: - * - * math.setMultiplicity(element, set) - * - * Examples: - * - * math.setMultiplicity(1, [1, 2, 2, 4]) // returns 1 - * math.setMultiplicity(2, [1, 2, 2, 4]) // returns 2 - * - * See also: - * - * setDistinct, setSize - * - * @param {number | BigNumber | Fraction | Complex} e An element in the multiset - * @param {Array | Matrix} a A multiset - * @return {number} The number of how many times the multiset contains the element - */ - return typed(setMultiplicity_name, { - 'number | BigNumber | Fraction | Complex, Array | Matrix': function numberBigNumberFractionComplexArrayMatrix(e, a) { - if (subset(size(a), new Index(0)) === 0) { - // if empty, return 0 - return 0; - } - - var b = Object(utils_array["e" /* flatten */])(Array.isArray(a) ? a : a.toArray()); - var count = 0; - - for (var i = 0; i < b.length; i++) { - if (compareNatural(b[i], e) === 0) { - count++; - } - } - - return count; - } - }); - }); - // CONCATENATED MODULE: ./src/function/set/setPowerset.js - - var setPowerset_name = 'setPowerset'; - var setPowerset_dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index']; - var createSetPowerset = /* #__PURE__ */Object(factory["a" /* factory */])(setPowerset_name, setPowerset_dependencies, function (_ref) { - var typed = _ref.typed, - size = _ref.size, - subset = _ref.subset, - compareNatural = _ref.compareNatural, - Index = _ref.Index; - - /** - * Create the powerset of a (multi)set. (The powerset contains very possible subsets of a (multi)set.) - * A multi-dimension array will be converted to a single-dimension array before the operation. - * - * Syntax: - * - * math.setPowerset(set) - * - * Examples: - * - * math.setPowerset([1, 2, 3]) // returns [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]] - * - * See also: - * - * setCartesian - * - * @param {Array | Matrix} a A (multi)set - * @return {Array} The powerset of the (multi)set - */ - return typed(setPowerset_name, { - 'Array | Matrix': function ArrayMatrix(a) { - if (subset(size(a), new Index(0)) === 0) { - // if empty, return empty - return []; - } - - var b = Object(utils_array["e" /* flatten */])(Array.isArray(a) ? a : a.toArray()).sort(compareNatural); - var result = []; - var number = 0; - - while (number.toString(2).length <= b.length) { - result.push(_subset(b, number.toString(2).split('').reverse())); - number++; - } // can not return a matrix, because of the different size of the subarrays - - return _sort(result); - } - }); // create subset - - function _subset(array, bitarray) { - var result = []; - - for (var i = 0; i < bitarray.length; i++) { - if (bitarray[i] === '1') { - result.push(array[i]); - } - } - - return result; - } // sort subsests by length - - function _sort(array) { - var temp = []; - - for (var i = array.length - 1; i > 0; i--) { - for (var j = 0; j < i; j++) { - if (array[j].length > array[j + 1].length) { - temp = array[j]; - array[j] = array[j + 1]; - array[j + 1] = temp; - } - } - } - - return array; - } - }); - // CONCATENATED MODULE: ./src/function/set/setSize.js - - var setSize_name = 'setSize'; - var setSize_dependencies = ['typed', 'compareNatural']; - var createSetSize = /* #__PURE__ */Object(factory["a" /* factory */])(setSize_name, setSize_dependencies, function (_ref) { - var typed = _ref.typed, - compareNatural = _ref.compareNatural; - - /** - * Count the number of elements of a (multi)set. When a second parameter is 'true', count only the unique values. - * A multi-dimension array will be converted to a single-dimension array before the operation. - * - * Syntax: - * - * math.setSize(set) - * math.setSize(set, unique) - * - * Examples: - * - * math.setSize([1, 2, 2, 4]) // returns 4 - * math.setSize([1, 2, 2, 4], true) // returns 3 - * - * See also: - * - * setUnion, setIntersect, setDifference - * - * @param {Array | Matrix} a A multiset - * @return {number} The number of elements of the (multi)set - */ - return typed(setSize_name, { - 'Array | Matrix': function ArrayMatrix(a) { - return Array.isArray(a) ? Object(utils_array["e" /* flatten */])(a).length : Object(utils_array["e" /* flatten */])(a.toArray()).length; - }, - 'Array | Matrix, boolean': function ArrayMatrixBoolean(a, unique) { - if (unique === false || a.length === 0) { - return Array.isArray(a) ? Object(utils_array["e" /* flatten */])(a).length : Object(utils_array["e" /* flatten */])(a.toArray()).length; - } else { - var b = Object(utils_array["e" /* flatten */])(Array.isArray(a) ? a : a.toArray()).sort(compareNatural); - var count = 1; - - for (var i = 1; i < b.length; i++) { - if (compareNatural(b[i], b[i - 1]) !== 0) { - count++; - } - } - - return count; - } - } - }); - }); - // CONCATENATED MODULE: ./src/function/set/setSymDifference.js - - var setSymDifference_name = 'setSymDifference'; - var setSymDifference_dependencies = ['typed', 'size', 'concat', 'subset', 'setDifference', 'Index']; - var createSetSymDifference = /* #__PURE__ */Object(factory["a" /* factory */])(setSymDifference_name, setSymDifference_dependencies, function (_ref) { - var typed = _ref.typed, - size = _ref.size, - concat = _ref.concat, - subset = _ref.subset, - setDifference = _ref.setDifference, - Index = _ref.Index; - - /** - * Create the symmetric difference of two (multi)sets. - * Multi-dimension arrays will be converted to single-dimension arrays before the operation. - * - * Syntax: - * - * math.setSymDifference(set1, set2) - * - * Examples: - * - * math.setSymDifference([1, 2, 3, 4], [3, 4, 5, 6]) // returns [1, 2, 5, 6] - * math.setSymDifference([[1, 2], [3, 4]], [[3, 4], [5, 6]]) // returns [1, 2, 5, 6] - * - * See also: - * - * setUnion, setIntersect, setDifference - * - * @param {Array | Matrix} a1 A (multi)set - * @param {Array | Matrix} a2 A (multi)set - * @return {Array | Matrix} The symmetric difference of two (multi)sets - */ - return typed(setSymDifference_name, { - 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) { - if (subset(size(a1), new Index(0)) === 0) { - // if any of them is empty, return the other one - return Object(utils_array["e" /* flatten */])(a2); - } else if (subset(size(a2), new Index(0)) === 0) { - return Object(utils_array["e" /* flatten */])(a1); - } - - var b1 = Object(utils_array["e" /* flatten */])(a1); - var b2 = Object(utils_array["e" /* flatten */])(a2); - return concat(setDifference(b1, b2), setDifference(b2, b1)); - } - }); - }); - // CONCATENATED MODULE: ./src/function/set/setUnion.js - - var setUnion_name = 'setUnion'; - var setUnion_dependencies = ['typed', 'size', 'concat', 'subset', 'setIntersect', 'setSymDifference', 'Index']; - var createSetUnion = /* #__PURE__ */Object(factory["a" /* factory */])(setUnion_name, setUnion_dependencies, function (_ref) { - var typed = _ref.typed, - size = _ref.size, - concat = _ref.concat, - subset = _ref.subset, - setIntersect = _ref.setIntersect, - setSymDifference = _ref.setSymDifference, - Index = _ref.Index; - - /** - * Create the union of two (multi)sets. - * Multi-dimension arrays will be converted to single-dimension arrays before the operation. - * - * Syntax: - * - * math.setUnion(set1, set2) - * - * Examples: - * - * math.setUnion([1, 2, 3, 4], [3, 4, 5, 6]) // returns [1, 2, 3, 4, 5, 6] - * math.setUnion([[1, 2], [3, 4]], [[3, 4], [5, 6]]) // returns [1, 2, 3, 4, 5, 6] - * - * See also: - * - * setIntersect, setDifference - * - * @param {Array | Matrix} a1 A (multi)set - * @param {Array | Matrix} a2 A (multi)set - * @return {Array | Matrix} The union of two (multi)sets - */ - return typed(setUnion_name, { - 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) { - if (subset(size(a1), new Index(0)) === 0) { - // if any of them is empty, return the other one - return Object(utils_array["e" /* flatten */])(a2); - } else if (subset(size(a2), new Index(0)) === 0) { - return Object(utils_array["e" /* flatten */])(a1); - } - - var b1 = Object(utils_array["e" /* flatten */])(a1); - var b2 = Object(utils_array["e" /* flatten */])(a2); - return concat(setSymDifference(b1, b2), setIntersect(b1, b2)); - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/add.js - - var add_name = 'add'; - var add_dependencies = ['typed', 'matrix', 'addScalar', 'equalScalar', 'DenseMatrix', 'SparseMatrix']; - var createAdd = /* #__PURE__ */Object(factory["a" /* factory */])(add_name, add_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - addScalar = _ref.addScalar, - equalScalar = _ref.equalScalar, - DenseMatrix = _ref.DenseMatrix, - SparseMatrix = _ref.SparseMatrix; - var algorithm01 = createAlgorithm01({ - typed: typed - }); - var algorithm04 = createAlgorithm04({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm10 = createAlgorithm10({ - typed: typed, - DenseMatrix: DenseMatrix - }); - var algorithm13 = createAlgorithm13({ - typed: typed - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Add two or more values, `x + y`. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.add(x, y) - * math.add(x, y, z, ...) - * - * Examples: - * - * math.add(2, 3) // returns number 5 - * math.add(2, 3, 4) // returns number 9 - * - * const a = math.complex(2, 3) - * const b = math.complex(-4, 1) - * math.add(a, b) // returns Complex -2 + 4i - * - * math.add([1, 2, 3], 4) // returns Array [5, 6, 7] - * - * const c = math.unit('5 cm') - * const d = math.unit('2.1 mm') - * math.add(c, d) // returns Unit 52.1 mm - * - * math.add("2.3", "4") // returns number 6.3 - * - * See also: - * - * subtract, sum - * - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First value to add - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second value to add - * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Sum of `x` and `y` - */ - - return typed(add_name, Object(utils_object["e" /* extend */])({ - // we extend the signatures of addScalar with signatures dealing with matrices - 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { - return algorithm13(x, y, addScalar); - }, - 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { - return algorithm01(x, y, addScalar, false); - }, - 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { - return algorithm01(y, x, addScalar, true); - }, - 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { - return algorithm04(x, y, addScalar); - }, - 'Array, Array': function ArrayArray(x, y) { - // use matrix implementation - return this(matrix(x), matrix(y)).valueOf(); - }, - 'Array, Matrix': function ArrayMatrix(x, y) { - // use matrix implementation - return this(matrix(x), y); - }, - 'Matrix, Array': function MatrixArray(x, y) { - // use matrix implementation - return this(x, matrix(y)); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, addScalar, false); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm10(x, y, addScalar, false); - }, - 'any, DenseMatrix': function anyDenseMatrix(x, y) { - return algorithm14(y, x, addScalar, true); - }, - 'any, SparseMatrix': function anySparseMatrix(x, y) { - return algorithm10(y, x, addScalar, true); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, addScalar, false).valueOf(); - }, - 'any, Array': function anyArray(x, y) { - // use matrix implementation - return algorithm14(matrix(y), x, addScalar, true).valueOf(); - }, - 'any, any': addScalar, - 'any, any, ...any': function anyAnyAny(x, y, rest) { - var result = this(x, y); - - for (var i = 0; i < rest.length; i++) { - result = this(result, rest[i]); - } - - return result; - } - }, addScalar.signatures)); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/hypot.js - - var hypot_name = 'hypot'; - var hypot_dependencies = ['typed', 'abs', 'addScalar', 'divideScalar', 'multiplyScalar', 'sqrt', 'smaller', 'isPositive']; - var createHypot = /* #__PURE__ */Object(factory["a" /* factory */])(hypot_name, hypot_dependencies, function (_ref) { - var typed = _ref.typed, - abs = _ref.abs, - addScalar = _ref.addScalar, - divideScalar = _ref.divideScalar, - multiplyScalar = _ref.multiplyScalar, - sqrt = _ref.sqrt, - smaller = _ref.smaller, - isPositive = _ref.isPositive; - - /** - * Calculate the hypotenusa of a list with values. The hypotenusa is defined as: - * - * hypot(a, b, c, ...) = sqrt(a^2 + b^2 + c^2 + ...) - * - * For matrix input, the hypotenusa is calculated for all values in the matrix. - * - * Syntax: - * - * math.hypot(a, b, ...) - * math.hypot([a, b, c, ...]) - * - * Examples: - * - * math.hypot(3, 4) // 5 - * math.hypot(3, 4, 5) // 7.0710678118654755 - * math.hypot([3, 4, 5]) // 7.0710678118654755 - * math.hypot(-2) // 2 - * - * See also: - * - * abs, norm - * - * @param {... number | BigNumber | Array | Matrix} args A list with numeric values or an Array or Matrix. - * Matrix and Array input is flattened and returns a - * single number for the whole matrix. - * @return {number | BigNumber} Returns the hypothenusa of the input values. - */ - return typed(hypot_name, { - '... number | BigNumber': _hypot, - Array: function Array(x) { - return this.apply(this, Object(utils_array["e" /* flatten */])(x)); - }, - Matrix: function Matrix(x) { - return this.apply(this, Object(utils_array["e" /* flatten */])(x.toArray())); - } - }); - /** - * Calculate the hypotenusa for an Array with values - * @param {Array.} args - * @return {number | BigNumber} Returns the result - * @private - */ - - function _hypot(args) { - // code based on `hypot` from es6-shim: - // https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1619-L1633 - var result = 0; - var largest = 0; - - for (var i = 0; i < args.length; i++) { - var value = abs(args[i]); - - if (smaller(largest, value)) { - result = multiplyScalar(result, multiplyScalar(divideScalar(largest, value), divideScalar(largest, value))); - result = addScalar(result, 1); - largest = value; - } else { - result = addScalar(result, isPositive(value) ? multiplyScalar(divideScalar(value, largest), divideScalar(value, largest)) : value); - } - } - - return multiplyScalar(largest, sqrt(result)); - } - }); - // CONCATENATED MODULE: ./src/function/arithmetic/norm.js - - var norm_name = 'norm'; - var norm_dependencies = ['typed', 'abs', 'add', 'pow', 'conj', 'sqrt', 'multiply', 'equalScalar', 'larger', 'smaller', 'matrix', 'ctranspose', 'eigs']; - var createNorm = /* #__PURE__ */Object(factory["a" /* factory */])(norm_name, norm_dependencies, function (_ref) { - var typed = _ref.typed, - abs = _ref.abs, - add = _ref.add, - pow = _ref.pow, - conj = _ref.conj, - sqrt = _ref.sqrt, - multiply = _ref.multiply, - equalScalar = _ref.equalScalar, - larger = _ref.larger, - smaller = _ref.smaller, - matrix = _ref.matrix, - ctranspose = _ref.ctranspose, - eigs = _ref.eigs; - - /** - * Calculate the norm of a number, vector or matrix. - * - * The second parameter p is optional. If not provided, it defaults to 2. - * - * Syntax: - * - * math.norm(x) - * math.norm(x, p) - * - * Examples: - * - * math.abs(-3.5) // returns 3.5 - * math.norm(-3.5) // returns 3.5 - * - * math.norm(math.complex(3, -4)) // returns 5 - * - * math.norm([1, 2, -3], Infinity) // returns 3 - * math.norm([1, 2, -3], -Infinity) // returns 1 - * - * math.norm([3, 4], 2) // returns 5 - * - * math.norm([[1, 2], [3, 4]], 1) // returns 6 - * math.norm([[1, 2], [3, 4]], 'inf') // returns 7 - * math.norm([[1, 2], [3, 4]], 'fro') // returns 5.477225575051661 - * - * See also: - * - * abs, hypot - * - * @param {number | BigNumber | Complex | Array | Matrix} x - * Value for which to calculate the norm - * @param {number | BigNumber | string} [p=2] - * Vector space. - * Supported numbers include Infinity and -Infinity. - * Supported strings are: 'inf', '-inf', and 'fro' (The Frobenius norm) - * @return {number | BigNumber} the p-norm - */ - return typed(norm_name, { - number: Math.abs, - Complex: function Complex(x) { - return x.abs(); - }, - BigNumber: function BigNumber(x) { - // norm(x) = abs(x) - return x.abs(); - }, - "boolean": function boolean(x) { - // norm(x) = abs(x) - return Math.abs(x); - }, - Array: function Array(x) { - return _norm(matrix(x), 2); - }, - Matrix: function Matrix(x) { - return _norm(x, 2); - }, - 'number | Complex | BigNumber | boolean, number | BigNumber | string': function numberComplexBigNumberBooleanNumberBigNumberString(x) { - // ignore second parameter, TODO: remove the option of second parameter for these types - return this(x); - }, - 'Array, number | BigNumber | string': function ArrayNumberBigNumberString(x, p) { - return _norm(matrix(x), p); - }, - 'Matrix, number | BigNumber | string': function MatrixNumberBigNumberString(x, p) { - return _norm(x, p); - } - }); - /** - * Calculate the plus infinity norm for a vector - * @param {Matrix} x - * @returns {number} Returns the norm - * @private - */ - - function _vectorNormPlusInfinity(x) { - // norm(x, Infinity) = max(abs(x)) - var pinf = 0; // skip zeros since abs(0) === 0 - - x.forEach(function (value) { - var v = abs(value); - - if (larger(v, pinf)) { - pinf = v; - } - }, true); - return pinf; - } - /** - * Calculate the minus infinity norm for a vector - * @param {Matrix} x - * @returns {number} Returns the norm - * @private - */ - - function _vectorNormMinusInfinity(x) { - // norm(x, -Infinity) = min(abs(x)) - var ninf; // skip zeros since abs(0) === 0 - - x.forEach(function (value) { - var v = abs(value); - - if (!ninf || smaller(v, ninf)) { - ninf = v; - } - }, true); - return ninf || 0; - } - /** - * Calculate the norm for a vector - * @param {Matrix} x - * @param {number | string} p - * @returns {number} Returns the norm - * @private - */ - - function _vectorNorm(x, p) { - // check p - if (p === Number.POSITIVE_INFINITY || p === 'inf') { - return _vectorNormPlusInfinity(x); - } - - if (p === Number.NEGATIVE_INFINITY || p === '-inf') { - return _vectorNormMinusInfinity(x); - } - - if (p === 'fro') { - return _norm(x, 2); - } - - if (typeof p === 'number' && !isNaN(p)) { - // check p != 0 - if (!equalScalar(p, 0)) { - // norm(x, p) = sum(abs(xi) ^ p) ^ 1/p - var n = 0; // skip zeros since abs(0) === 0 - - x.forEach(function (value) { - n = add(pow(abs(value), p), n); - }, true); - return pow(n, 1 / p); - } - - return Number.POSITIVE_INFINITY; - } // invalid parameter value - - throw new Error('Unsupported parameter value'); - } - /** - * Calculate the Frobenius norm for a matrix - * @param {Matrix} x - * @returns {number} Returns the norm - * @private - */ - - function _matrixNormFrobenius(x) { - // norm(x) = sqrt(sum(diag(x'x))) - var fro = 0; - x.forEach(function (value, index) { - fro = add(fro, multiply(value, conj(value))); - }); - return abs(sqrt(fro)); - } - /** - * Calculate the norm L1 for a matrix - * @param {Matrix} x - * @returns {number} Returns the norm - * @private - */ - - function _matrixNormOne(x) { - // norm(x) = the largest column sum - var c = []; // result - - var maxc = 0; // skip zeros since abs(0) == 0 - - x.forEach(function (value, index) { - var j = index[1]; - var cj = add(c[j] || 0, abs(value)); - - if (larger(cj, maxc)) { - maxc = cj; - } - - c[j] = cj; - }, true); - return maxc; - } - /** - * Calculate the norm L2 for a matrix - * @param {Matrix} x - * @returns {number} Returns the norm - * @private - */ - - function _matrixNormTwo(x) { - // norm(x) = sqrt( max eigenvalue of A*.A) - var sizeX = x.size(); - - if (sizeX[0] !== sizeX[1]) { - throw new RangeError('Invalid matrix dimensions'); - } - - var tx = ctranspose(x); - var squaredX = multiply(tx, x); - var eigenVals = eigs(squaredX).values; - var rho = eigenVals.get([eigenVals.size()[0] - 1]); - return abs(sqrt(rho)); - } - /** - * Calculate the infinity norm for a matrix - * @param {Matrix} x - * @returns {number} Returns the norm - * @private - */ - - function _matrixNormInfinity(x) { - // norm(x) = the largest row sum - var r = []; // result - - var maxr = 0; // skip zeros since abs(0) == 0 - - x.forEach(function (value, index) { - var i = index[0]; - var ri = add(r[i] || 0, abs(value)); - - if (larger(ri, maxr)) { - maxr = ri; - } - - r[i] = ri; - }, true); - return maxr; - } - /** - * Calculate the norm for a 2D Matrix (M*N) - * @param {Matrix} x - * @param {number | string} p - * @returns {number} Returns the norm - * @private - */ - - function _matrixNorm(x, p) { - // check p - if (p === 1) { - return _matrixNormOne(x); - } - - if (p === Number.POSITIVE_INFINITY || p === 'inf') { - return _matrixNormInfinity(x); - } - - if (p === 'fro') { - return _matrixNormFrobenius(x); - } - - if (p === 2) { - return _matrixNormTwo(x); - } // invalid parameter value - - throw new Error('Unsupported parameter value ' + p); - } - /** - * Calculate the norm for an array - * @param {Matrix} x - * @param {number | string} p - * @returns {number} Returns the norm - * @private - */ - - function _norm(x, p) { - // size - var sizeX = x.size(); // check if it is a vector - - if (sizeX.length === 1) { - return _vectorNorm(x, p); - } // MxN matrix - - if (sizeX.length === 2) { - if (sizeX[0] && sizeX[1]) { - return _matrixNorm(x, p); - } else { - throw new RangeError('Invalid matrix dimensions'); - } - } - } - }); - // CONCATENATED MODULE: ./src/function/matrix/dot.js - - var dot_name = 'dot'; - var dot_dependencies = ['typed', 'addScalar', 'multiplyScalar', 'conj', 'size']; - var createDot = /* #__PURE__ */Object(factory["a" /* factory */])(dot_name, dot_dependencies, function (_ref) { - var typed = _ref.typed, - addScalar = _ref.addScalar, - multiplyScalar = _ref.multiplyScalar, - conj = _ref.conj, - size = _ref.size; - - /** - * Calculate the dot product of two vectors. The dot product of - * `A = [a1, a2, ..., an]` and `B = [b1, b2, ..., bn]` is defined as: - * - * dot(A, B) = conj(a1) * b1 + conj(a2) * b2 + ... + conj(an) * bn - * - * Syntax: - * - * math.dot(x, y) - * - * Examples: - * - * math.dot([2, 4, 1], [2, 2, 3]) // returns number 15 - * math.multiply([2, 4, 1], [2, 2, 3]) // returns number 15 - * - * See also: - * - * multiply, cross - * - * @param {Array | Matrix} x First vector - * @param {Array | Matrix} y Second vector - * @return {number} Returns the dot product of `x` and `y` - */ - return typed(dot_name, { - 'Array | DenseMatrix, Array | DenseMatrix': _denseDot, - 'SparseMatrix, SparseMatrix': _sparseDot - }); - - function _validateDim(x, y) { - var xSize = _size(x); - - var ySize = _size(y); - - var xLen, yLen; - - if (xSize.length === 1) { - xLen = xSize[0]; - } else if (xSize.length === 2 && xSize[1] === 1) { - xLen = xSize[0]; - } else { - throw new RangeError('Expected a column vector, instead got a matrix of size (' + xSize.join(', ') + ')'); - } - - if (ySize.length === 1) { - yLen = ySize[0]; - } else if (ySize.length === 2 && ySize[1] === 1) { - yLen = ySize[0]; - } else { - throw new RangeError('Expected a column vector, instead got a matrix of size (' + ySize.join(', ') + ')'); - } - - if (xLen !== yLen) { - throw new RangeError('Vectors must have equal length (' + xLen + ' != ' + yLen + ')'); - } - if (xLen === 0) { - throw new RangeError('Cannot calculate the dot product of empty vectors'); - } - return xLen; - } - - function _denseDot(a, b) { - var N = _validateDim(a, b); - - var adata = Object(is["v" /* isMatrix */])(a) ? a._data : a; - var adt = Object(is["v" /* isMatrix */])(a) ? a._datatype : undefined; - var bdata = Object(is["v" /* isMatrix */])(b) ? b._data : b; - var bdt = Object(is["v" /* isMatrix */])(b) ? b._datatype : undefined; // are these 2-dimensional column vectors? (as opposed to 1-dimensional vectors) - - var aIsColumn = _size(a).length === 2; - var bIsColumn = _size(b).length === 2; - var add = addScalar; - var mul = multiplyScalar; // process data types - - if (adt && bdt && adt === bdt && typeof adt === 'string') { - var dt = adt; // find signatures that matches (dt, dt) - - add = typed.find(addScalar, [dt, dt]); - mul = typed.find(multiplyScalar, [dt, dt]); - } // both vectors 1-dimensional - - if (!aIsColumn && !bIsColumn) { - var c = mul(conj(adata[0]), bdata[0]); - - for (var i = 1; i < N; i++) { - c = add(c, mul(conj(adata[i]), bdata[i])); - } - - return c; - } // a is 1-dim, b is column - - if (!aIsColumn && bIsColumn) { - var _c = mul(conj(adata[0]), bdata[0][0]); - - for (var _i = 1; _i < N; _i++) { - _c = add(_c, mul(conj(adata[_i]), bdata[_i][0])); - } - - return _c; - } // a is column, b is 1-dim - - if (aIsColumn && !bIsColumn) { - var _c2 = mul(conj(adata[0][0]), bdata[0]); - - for (var _i2 = 1; _i2 < N; _i2++) { - _c2 = add(_c2, mul(conj(adata[_i2][0]), bdata[_i2])); - } - - return _c2; - } // both vectors are column - - if (aIsColumn && bIsColumn) { - var _c3 = mul(conj(adata[0][0]), bdata[0][0]); - - for (var _i3 = 1; _i3 < N; _i3++) { - _c3 = add(_c3, mul(conj(adata[_i3][0]), bdata[_i3][0])); - } - - return _c3; - } - } - - function _sparseDot(x, y) { - _validateDim(x, y); - - var xindex = x._index; - var xvalues = x._values; - var yindex = y._index; - var yvalues = y._values; // TODO optimize add & mul using datatype - - var c = 0; - var add = addScalar; - var mul = multiplyScalar; - var i = 0; - var j = 0; - - while (i < xindex.length && j < yindex.length) { - var I = xindex[i]; - var J = yindex[j]; - - if (I < J) { - i++; - continue; - } - - if (I > J) { - j++; - continue; - } - - if (I === J) { - c = add(c, mul(xvalues[i], yvalues[j])); - i++; - j++; - } - } - - return c; - } // TODO remove this once #1771 is fixed - - function _size(x) { - return Object(is["v" /* isMatrix */])(x) ? x.size() : size(x); - } - }); - // CONCATENATED MODULE: ./src/function/matrix/trace.js - - var trace_name = 'trace'; - var trace_dependencies = ['typed', 'matrix', 'add']; - var createTrace = /* #__PURE__ */Object(factory["a" /* factory */])(trace_name, trace_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - add = _ref.add; - - /** - * Calculate the trace of a matrix: the sum of the elements on the main - * diagonal of a square matrix. - * - * Syntax: - * - * math.trace(x) - * - * Examples: - * - * math.trace([[1, 2], [3, 4]]) // returns 5 - * - * const A = [ - * [1, 2, 3], - * [-1, 2, 3], - * [2, 0, 3] - * ] - * math.trace(A) // returns 6 - * - * See also: - * - * diag - * - * @param {Array | Matrix} x A matrix - * - * @return {number} The trace of `x` - */ - return typed('trace', { - Array: function _arrayTrace(x) { - // use dense matrix implementation - return _denseTrace(matrix(x)); - }, - SparseMatrix: _sparseTrace, - DenseMatrix: _denseTrace, - any: utils_object["a" /* clone */] - }); - - function _denseTrace(m) { - // matrix size & data - var size = m._size; - var data = m._data; // process dimensions - - switch (size.length) { - case 1: - // vector - if (size[0] === 1) { - // return data[0] - return Object(utils_object["a" /* clone */])(data[0]); - } - - throw new RangeError('Matrix must be square (size: ' + Object(utils_string["d" /* format */])(size) + ')'); - - case 2: - { - // two dimensional - var rows = size[0]; - var cols = size[1]; - - if (rows === cols) { - // calulate sum - var sum = 0; // loop diagonal - - for (var i = 0; i < rows; i++) { - sum = add(sum, data[i][i]); - } // return trace - - return sum; - } else { - throw new RangeError('Matrix must be square (size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - } - - default: - // multi dimensional - throw new RangeError('Matrix must be two dimensional (size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - } - - function _sparseTrace(m) { - // matrix arrays - var values = m._values; - var index = m._index; - var ptr = m._ptr; - var size = m._size; // check dimensions - - var rows = size[0]; - var columns = size[1]; // matrix must be square - - if (rows === columns) { - // calulate sum - var sum = 0; // check we have data (avoid looping columns) - - if (values.length > 0) { - // loop columns - for (var j = 0; j < columns; j++) { - // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] - var k0 = ptr[j]; - var k1 = ptr[j + 1]; // loop k within [k0, k1[ - - for (var k = k0; k < k1; k++) { - // row index - var i = index[k]; // check row - - if (i === j) { - // accumulate value - sum = add(sum, values[k]); // exit loop - - break; - } - - if (i > j) { - // exit loop, no value on the diagonal for column j - break; - } - } - } - } // return trace - - return sum; - } - - throw new RangeError('Matrix must be square (size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - }); - // CONCATENATED MODULE: ./src/type/matrix/function/index.js - - var function_name = 'index'; - var function_dependencies = ['typed', 'Index']; - var createIndex = /* #__PURE__ */Object(factory["a" /* factory */])(function_name, function_dependencies, function (_ref) { - var typed = _ref.typed, - Index = _ref.Index; - - /** - * Create an index. An Index can store ranges having start, step, and end - * for multiple dimensions. - * Matrix.get, Matrix.set, and math.subset accept an Index as input. - * - * Syntax: - * - * math.index(range1, range2, ...) - * - * Where each range can be any of: - * - * - A number - * - A string for getting/setting an object property - * - An instance of `Range` - * - A one-dimensional Array or a Matrix with numbers - * - * Indexes must be zero-based, integer numbers. - * - * Examples: - * - * const b = [1, 2, 3, 4, 5] - * math.subset(b, math.index([1, 2, 3])) // returns [2, 3, 4] - * - * const a = math.matrix([[1, 2], [3, 4]]) - * a.subset(math.index(0, 1)) // returns 2 - * - * See also: - * - * bignumber, boolean, complex, matrix, number, string, unit - * - * @param {...*} ranges Zero or more ranges or numbers. - * @return {Index} Returns the created index - */ - return typed(function_name, { - '...number | string | BigNumber | Range | Array | Matrix': function numberStringBigNumberRangeArrayMatrix(args) { - var ranges = args.map(function (arg) { - if (Object(is["e" /* isBigNumber */])(arg)) { - return arg.toNumber(); // convert BigNumber to Number - } else if (Array.isArray(arg) || Object(is["v" /* isMatrix */])(arg)) { - return arg.map(function (elem) { - // convert BigNumber to Number - return Object(is["e" /* isBigNumber */])(elem) ? elem.toNumber() : elem; - }); - } else { - return arg; - } - }); - var res = new Index(); - Index.apply(res, ranges); - return res; - } - }); - }); - // CONCATENATED MODULE: ./src/expression/keywords.js - // Reserved keywords not allowed to use in the parser - var keywords = { - end: true - }; - // CONCATENATED MODULE: ./src/expression/node/Node.js - function Node_typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - Node_typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - Node_typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return Node_typeof(obj); - } - - var Node_name = 'Node'; - var Node_dependencies = ['mathWithTransform']; - var createNode = /* #__PURE__ */Object(factory["a" /* factory */])(Node_name, Node_dependencies, function (_ref) { - var mathWithTransform = _ref.mathWithTransform; - - /** - * Node - */ - function Node() { - if (!(this instanceof Node)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - } - /** - * Evaluate the node - * @param {Object} [scope] Scope to read/write variables - * @return {*} Returns the result - */ - - Node.prototype.evaluate = function (scope) { - return this.compile().evaluate(scope); - }; - - Node.prototype.type = 'Node'; - Node.prototype.isNode = true; - Node.prototype.comment = ''; - /** - * Compile the node into an optimized, evauatable JavaScript function - * @return {{evaluate: function([Object])}} object - * Returns an object with a function 'evaluate', - * which can be invoked as expr.evaluate([scope: Object]), - * where scope is an optional object with - * variables. - */ - - Node.prototype.compile = function () { - var expr = this._compile(mathWithTransform, {}); - - var args = {}; - var context = null; - - function evaluate(scope) { - var s = scope || {}; - - _validateScope(s); - - return expr(s, args, context); - } - - return { - evaluate: evaluate - }; - }; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - Node.prototype._compile = function (math, argNames) { - throw new Error('Method _compile should be implemented by type ' + this.type); - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - Node.prototype.forEach = function (callback) { - // must be implemented by each of the Node implementations - throw new Error('Cannot run forEach on a Node interface'); - }; - /** - * Create a new Node having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {OperatorNode} Returns a transformed copy of the node - */ - - Node.prototype.map = function (callback) { - // must be implemented by each of the Node implementations - throw new Error('Cannot run map on a Node interface'); - }; - /** - * Validate whether an object is a Node, for use with map - * @param {Node} node - * @returns {Node} Returns the input if it's a node, else throws an Error - * @protected - */ - - Node.prototype._ifNode = function (node) { - if (!Object(is["w" /* isNode */])(node)) { - throw new TypeError('Callback function must return a Node'); - } - - return node; - }; - /** - * Recursively traverse all nodes in a node tree. Executes given callback for - * this node and each of its child nodes. - * @param {function(node: Node, path: string, parent: Node)} callback - * A callback called for every node in the node tree. - */ - - Node.prototype.traverse = function (callback) { - // execute callback for itself - callback(this, null, null); // eslint-disable-line standard/no-callback-literal - // recursively traverse over all childs of a node - - function _traverse(node, callback) { - node.forEach(function (child, path, parent) { - callback(child, path, parent); - - _traverse(child, callback); - }); - } - - _traverse(this, callback); - }; - /** - * Recursively transform a node tree via a transform function. - * - * For example, to replace all nodes of type SymbolNode having name 'x' with a - * ConstantNode with value 2: - * - * const res = Node.transform(function (node, path, parent) { - * if (node && node.isSymbolNode) && (node.name === 'x')) { - * return new ConstantNode(2) - * } - * else { - * return node - * } - * }) - * - * @param {function(node: Node, path: string, parent: Node) : Node} callback - * A mapping function accepting a node, and returning - * a replacement for the node or the original node. - * Signature: callback(node: Node, index: string, parent: Node) : Node - * @return {Node} Returns the original node or its replacement - */ - - Node.prototype.transform = function (callback) { - function _transform(child, path, parent) { - var replacement = callback(child, path, parent); - - if (replacement !== child) { - // stop iterating when the node is replaced - return replacement; - } - - return child.map(_transform); - } - - return _transform(this, null, null); - }; - /** - * Find any node in the node tree matching given filter function. For example, to - * find all nodes of type SymbolNode having name 'x': - * - * const results = Node.filter(function (node) { - * return (node && node.isSymbolNode) && (node.name === 'x') - * }) - * - * @param {function(node: Node, path: string, parent: Node) : Node} callback - * A test function returning true when a node matches, and false - * otherwise. Function signature: - * callback(node: Node, index: string, parent: Node) : boolean - * @return {Node[]} nodes An array with nodes matching given filter criteria - */ - - Node.prototype.filter = function (callback) { - var nodes = []; - this.traverse(function (node, path, parent) { - if (callback(node, path, parent)) { - nodes.push(node); - } - }); - return nodes; - }; - /** - * Create a shallow clone of this node - * @return {Node} - */ - - Node.prototype.clone = function () { - // must be implemented by each of the Node implementations - throw new Error('Cannot clone a Node interface'); - }; - /** - * Create a deep clone of this node - * @return {Node} - */ - - Node.prototype.cloneDeep = function () { - return this.map(function (node) { - return node.cloneDeep(); - }); - }; - /** - * Deep compare this node with another node. - * @param {Node} other - * @return {boolean} Returns true when both nodes are of the same type and - * contain the same values (as do their childs) - */ - - Node.prototype.equals = function (other) { - return other ? Object(utils_object["d" /* deepStrictEqual */])(this, other) : false; - }; - /** - * Get string representation. (wrapper function) - * - * This function can get an object of the following form: - * { - * handler: //This can be a callback function of the form - * // "function callback(node, options)"or - * // a map that maps function names (used in FunctionNodes) - * // to callbacks - * parenthesis: "keep" //the parenthesis option (This is optional) - * } - * - * @param {Object} [options] - * @return {string} - */ - - Node.prototype.toString = function (options) { - var customString; - - if (options && Node_typeof(options) === 'object') { - switch (Node_typeof(options.handler)) { - case 'object': - case 'undefined': - break; - - case 'function': - customString = options.handler(this, options); - break; - - default: - throw new TypeError('Object or function expected as callback'); - } - } - - if (typeof customString !== 'undefined') { - return customString; - } - - return this._toString(options); - }; - /** - * Get a JSON representation of the node - * Both .toJSON() and the static .fromJSON(json) should be implemented by all - * implementations of Node - * @returns {Object} - */ - - Node.prototype.toJSON = function () { - throw new Error('Cannot serialize object: toJSON not implemented by ' + this.type); - }; - /** - * Get HTML representation. (wrapper function) - * - * This function can get an object of the following form: - * { - * handler: //This can be a callback function of the form - * // "function callback(node, options)" or - * // a map that maps function names (used in FunctionNodes) - * // to callbacks - * parenthesis: "keep" //the parenthesis option (This is optional) - * } - * - * @param {Object} [options] - * @return {string} - */ - - Node.prototype.toHTML = function (options) { - var customString; - - if (options && Node_typeof(options) === 'object') { - switch (Node_typeof(options.handler)) { - case 'object': - case 'undefined': - break; - - case 'function': - customString = options.handler(this, options); - break; - - default: - throw new TypeError('Object or function expected as callback'); - } - } - - if (typeof customString !== 'undefined') { - return customString; - } - - return this.toHTML(options); - }; - /** - * Internal function to generate the string output. - * This has to be implemented by every Node - * - * @throws {Error} - */ - - Node.prototype._toString = function () { - // must be implemented by each of the Node implementations - throw new Error('_toString not implemented for ' + this.type); - }; - /** - * Get LaTeX representation. (wrapper function) - * - * This function can get an object of the following form: - * { - * handler: //This can be a callback function of the form - * // "function callback(node, options)"or - * // a map that maps function names (used in FunctionNodes) - * // to callbacks - * parenthesis: "keep" //the parenthesis option (This is optional) - * } - * - * @param {Object} [options] - * @return {string} - */ - - Node.prototype.toTex = function (options) { - var customTex; - - if (options && Node_typeof(options) === 'object') { - switch (Node_typeof(options.handler)) { - case 'object': - case 'undefined': - break; - - case 'function': - customTex = options.handler(this, options); - break; - - default: - throw new TypeError('Object or function expected as callback'); - } - } - - if (typeof customTex !== 'undefined') { - return customTex; - } - - return this._toTex(options); - }; - /** - * Internal function to generate the LaTeX output. - * This has to be implemented by every Node - * - * @param {Object} [options] - * @throws {Error} - */ - - Node.prototype._toTex = function (options) { - // must be implemented by each of the Node implementations - throw new Error('_toTex not implemented for ' + this.type); - }; - /** - * Get identifier. - * @return {string} - */ - - Node.prototype.getIdentifier = function () { - return this.type; - }; - /** - * Get the content of the current Node. - * @return {Node} node - **/ - - Node.prototype.getContent = function () { - return this; - }; - /** - * Validate the symbol names of a scope. - * Throws an error when the scope contains an illegal symbol. - * @param {Object} scope - */ - - function _validateScope(scope) { - for (var symbol in scope) { - if (Object(utils_object["f" /* hasOwnProperty */])(scope, symbol)) { - if (symbol in keywords) { - throw new Error('Scope contains an illegal symbol, "' + symbol + '" is a reserved keyword'); - } - } - } - } - - return Node; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/transform/utils/errorTransform.js - - /** - * Transform zero-based indices to one-based indices in errors - * @param {Error} err - * @returns {Error | IndexError} Returns the transformed error - */ - - function errorTransform(err) { - if (err && err.isIndexError) { - return new IndexError["a" /* IndexError */](err.index + 1, err.min + 1, err.max !== undefined ? err.max + 1 : undefined); - } - - return err; - } - // CONCATENATED MODULE: ./src/expression/node/utils/access.js - function access_typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - access_typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - access_typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return access_typeof(obj); - } - - function accessFactory(_ref) { - var subset = _ref.subset; - - /** - * Retrieve part of an object: - * - * - Retrieve a property from an object - * - Retrieve a part of a string - * - Retrieve a matrix subset - * - * @param {Object | Array | Matrix | string} object - * @param {Index} index - * @return {Object | Array | Matrix | string} Returns the subset - */ - return function access(object, index) { - try { - if (Array.isArray(object)) { - return subset(object, index); - } else if (object && typeof object.subset === 'function') { - // Matrix - return object.subset(index); - } else if (typeof object === 'string') { - // TODO: move getStringSubset into a separate util file, use that - return subset(object, index); - } else if (access_typeof(object) === 'object') { - if (!index.isObjectProperty()) { - throw new TypeError('Cannot apply a numeric index as object property'); - } - - return getSafeProperty(object, index.getObjectProperty()); - } else { - throw new TypeError('Cannot apply index: unsupported type of object'); - } - } catch (err) { - throw errorTransform(err); - } - }; - } - // CONCATENATED MODULE: ./src/expression/node/AccessorNode.js - - var AccessorNode_name = 'AccessorNode'; - var AccessorNode_dependencies = ['subset', 'Node']; - var createAccessorNode = /* #__PURE__ */Object(factory["a" /* factory */])(AccessorNode_name, AccessorNode_dependencies, function (_ref) { - var subset = _ref.subset, - Node = _ref.Node; - var access = accessFactory({ - subset: subset - }); - /** - * @constructor AccessorNode - * @extends {Node} - * Access an object property or get a matrix subset - * - * @param {Node} object The object from which to retrieve - * a property or subset. - * @param {IndexNode} index IndexNode containing ranges - */ - - function AccessorNode(object, index) { - if (!(this instanceof AccessorNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - if (!Object(is["w" /* isNode */])(object)) { - throw new TypeError('Node expected for parameter "object"'); - } - - if (!Object(is["u" /* isIndexNode */])(index)) { - throw new TypeError('IndexNode expected for parameter "index"'); - } - - this.object = object || null; - this.index = index; // readonly property name - - Object.defineProperty(this, 'name', { - get: function () { - if (this.index) { - return this.index.isObjectProperty() ? this.index.getObjectProperty() : ''; - } else { - return this.object.name || ''; - } - }.bind(this), - set: function set() { - throw new Error('Cannot assign a new name, name is read-only'); - } - }); - } - - AccessorNode.prototype = new Node(); - AccessorNode.prototype.type = 'AccessorNode'; - AccessorNode.prototype.isAccessorNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - AccessorNode.prototype._compile = function (math, argNames) { - var evalObject = this.object._compile(math, argNames); - - var evalIndex = this.index._compile(math, argNames); - - if (this.index.isObjectProperty()) { - var prop = this.index.getObjectProperty(); - return function evalAccessorNode(scope, args, context) { - return getSafeProperty(evalObject(scope, args, context), prop); - }; - } else { - return function evalAccessorNode(scope, args, context) { - var object = evalObject(scope, args, context); - var index = evalIndex(scope, args, object); // we pass object here instead of context - - return access(object, index); - }; - } - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - AccessorNode.prototype.forEach = function (callback) { - callback(this.object, 'object', this); - callback(this.index, 'index', this); - }; - /** - * Create a new AccessorNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {AccessorNode} Returns a transformed copy of the node - */ - - AccessorNode.prototype.map = function (callback) { - return new AccessorNode(this._ifNode(callback(this.object, 'object', this)), this._ifNode(callback(this.index, 'index', this))); - }; - /** - * Create a clone of this node, a shallow copy - * @return {AccessorNode} - */ - - AccessorNode.prototype.clone = function () { - return new AccessorNode(this.object, this.index); - }; - /** - * Get string representation - * @param {Object} options - * @return {string} - */ - - AccessorNode.prototype._toString = function (options) { - var object = this.object.toString(options); - - if (needParenthesis(this.object)) { - object = '(' + object + ')'; - } - - return object + this.index.toString(options); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} - */ - - AccessorNode.prototype.toHTML = function (options) { - var object = this.object.toHTML(options); - - if (needParenthesis(this.object)) { - object = '(' + object + ')'; - } - - return object + this.index.toHTML(options); - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} - */ - - AccessorNode.prototype._toTex = function (options) { - var object = this.object.toTex(options); - - if (needParenthesis(this.object)) { - object = '\\left(\' + object + \'\\right)'; - } - - return object + this.index.toTex(options); - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - AccessorNode.prototype.toJSON = function () { - return { - mathjs: 'AccessorNode', - object: this.object, - index: this.index - }; - }; - /** - * Instantiate an AccessorNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "AccessorNode", object: ..., index: ...}`, - * where mathjs is optional - * @returns {AccessorNode} - */ - - AccessorNode.fromJSON = function (json) { - return new AccessorNode(json.object, json.index); - }; - /** - * Are parenthesis needed? - * @private - */ - - function needParenthesis(node) { - // TODO: maybe make a method on the nodes which tells whether they need parenthesis? - return !(Object(is["a" /* isAccessorNode */])(node) || Object(is["c" /* isArrayNode */])(node) || Object(is["l" /* isConstantNode */])(node) || Object(is["r" /* isFunctionNode */])(node) || Object(is["A" /* isObjectNode */])(node) || Object(is["C" /* isParenthesisNode */])(node) || Object(is["J" /* isSymbolNode */])(node)); - } - - return AccessorNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/ArrayNode.js - - var ArrayNode_name = 'ArrayNode'; - var ArrayNode_dependencies = ['Node']; - var createArrayNode = /* #__PURE__ */Object(factory["a" /* factory */])(ArrayNode_name, ArrayNode_dependencies, function (_ref) { - var Node = _ref.Node; - - /** - * @constructor ArrayNode - * @extends {Node} - * Holds an 1-dimensional array with items - * @param {Node[]} [items] 1 dimensional array with items - */ - function ArrayNode(items) { - if (!(this instanceof ArrayNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this.items = items || []; // validate input - - if (!Array.isArray(this.items) || !this.items.every(is["w" /* isNode */])) { - throw new TypeError('Array containing Nodes expected'); - } - } - - ArrayNode.prototype = new Node(); - ArrayNode.prototype.type = 'ArrayNode'; - ArrayNode.prototype.isArrayNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - ArrayNode.prototype._compile = function (math, argNames) { - var evalItems = Object(utils_array["k" /* map */])(this.items, function (item) { - return item._compile(math, argNames); - }); - var asMatrix = math.config.matrix !== 'Array'; - - if (asMatrix) { - var matrix = math.matrix; - return function evalArrayNode(scope, args, context) { - return matrix(Object(utils_array["k" /* map */])(evalItems, function (evalItem) { - return evalItem(scope, args, context); - })); - }; - } else { - return function evalArrayNode(scope, args, context) { - return Object(utils_array["k" /* map */])(evalItems, function (evalItem) { - return evalItem(scope, args, context); - }); - }; - } - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - ArrayNode.prototype.forEach = function (callback) { - for (var i = 0; i < this.items.length; i++) { - var node = this.items[i]; - callback(node, 'items[' + i + ']', this); - } - }; - /** - * Create a new ArrayNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {ArrayNode} Returns a transformed copy of the node - */ - - ArrayNode.prototype.map = function (callback) { - var items = []; - - for (var i = 0; i < this.items.length; i++) { - items[i] = this._ifNode(callback(this.items[i], 'items[' + i + ']', this)); - } - - return new ArrayNode(items); - }; - /** - * Create a clone of this node, a shallow copy - * @return {ArrayNode} - */ - - ArrayNode.prototype.clone = function () { - return new ArrayNode(this.items.slice(0)); - }; - /** - * Get string representation - * @param {Object} options - * @return {string} str - * @override - */ - - ArrayNode.prototype._toString = function (options) { - var items = this.items.map(function (node) { - return node.toString(options); - }); - return '[' + items.join(', ') + ']'; - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - ArrayNode.prototype.toJSON = function () { - return { - mathjs: 'ArrayNode', - items: this.items - }; - }; - /** - * Instantiate an ArrayNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "ArrayNode", items: [...]}`, - * where mathjs is optional - * @returns {ArrayNode} - */ - - ArrayNode.fromJSON = function (json) { - return new ArrayNode(json.items); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - * @override - */ - - ArrayNode.prototype.toHTML = function (options) { - var items = this.items.map(function (node) { - return node.toHTML(options); - }); - return '[' + items.join(',') + ']'; - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - */ - - ArrayNode.prototype._toTex = function (options) { - var s = '\\begin{bmatrix}'; - this.items.forEach(function (node) { - if (node.items) { - s += node.items.map(function (childNode) { - return childNode.toTex(options); - }).join('&'); - } else { - s += node.toTex(options); - } // new line - - s += '\\\\'; - }); - s += '\\end{bmatrix}'; - return s; - }; - - return ArrayNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/utils/assign.js - function assign_typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - assign_typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - assign_typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return assign_typeof(obj); - } - - function assignFactory(_ref) { - var subset = _ref.subset, - matrix = _ref.matrix; - - /** - * Replace part of an object: - * - * - Assign a property to an object - * - Replace a part of a string - * - Replace a matrix subset - * - * @param {Object | Array | Matrix | string} object - * @param {Index} index - * @param {*} value - * @return {Object | Array | Matrix | string} Returns the original object - * except in case of a string - */ - // TODO: change assign to return the value instead of the object - return function assign(object, index, value) { - try { - if (Array.isArray(object)) { - // we use matrix.subset here instead of the function subset because we must not clone the contents - return matrix(object).subset(index, value).valueOf(); - } else if (object && typeof object.subset === 'function') { - // Matrix - return object.subset(index, value); - } else if (typeof object === 'string') { - // TODO: move setStringSubset into a separate util file, use that - return subset(object, index, value); - } else if (assign_typeof(object) === 'object') { - if (!index.isObjectProperty()) { - throw TypeError('Cannot apply a numeric index as object property'); - } - - setSafeProperty(object, index.getObjectProperty(), value); - return object; - } else { - throw new TypeError('Cannot apply index: unsupported type of object'); - } - } catch (err) { - throw errorTransform(err); - } - }; - } - // CONCATENATED MODULE: ./src/expression/operators.js - // list of identifiers of nodes in order of their precedence - // also contains information about left/right associativity - // and which other operator the operator is associative with - // Example: - // addition is associative with addition and subtraction, because: - // (a+b)+c=a+(b+c) - // (a+b)-c=a+(b-c) - // - // postfix operators are left associative, prefix operators - // are right associative - // - // It's also possible to set the following properties: - // latexParens: if set to false, this node doesn't need to be enclosed - // in parentheses when using LaTeX - // latexLeftParens: if set to false, this !OperatorNode's! - // left argument doesn't need to be enclosed - // in parentheses - // latexRightParens: the same for the right argument - - var operators_properties = [{ - // assignment - AssignmentNode: {}, - FunctionAssignmentNode: {} - }, { - // conditional expression - ConditionalNode: { - latexLeftParens: false, - latexRightParens: false, - latexParens: false // conditionals don't need parentheses in LaTeX because - // they are 2 dimensional - - } - }, { - // logical or - 'OperatorNode:or': { - associativity: 'left', - associativeWith: [] - } - }, { - // logical xor - 'OperatorNode:xor': { - associativity: 'left', - associativeWith: [] - } - }, { - // logical and - 'OperatorNode:and': { - associativity: 'left', - associativeWith: [] - } - }, { - // bitwise or - 'OperatorNode:bitOr': { - associativity: 'left', - associativeWith: [] - } - }, { - // bitwise xor - 'OperatorNode:bitXor': { - associativity: 'left', - associativeWith: [] - } - }, { - // bitwise and - 'OperatorNode:bitAnd': { - associativity: 'left', - associativeWith: [] - } - }, { - // relational operators - 'OperatorNode:equal': { - associativity: 'left', - associativeWith: [] - }, - 'OperatorNode:unequal': { - associativity: 'left', - associativeWith: [] - }, - 'OperatorNode:smaller': { - associativity: 'left', - associativeWith: [] - }, - 'OperatorNode:larger': { - associativity: 'left', - associativeWith: [] - }, - 'OperatorNode:smallerEq': { - associativity: 'left', - associativeWith: [] - }, - 'OperatorNode:largerEq': { - associativity: 'left', - associativeWith: [] - }, - RelationalNode: { - associativity: 'left', - associativeWith: [] - } - }, { - // bitshift operators - 'OperatorNode:leftShift': { - associativity: 'left', - associativeWith: [] - }, - 'OperatorNode:rightArithShift': { - associativity: 'left', - associativeWith: [] - }, - 'OperatorNode:rightLogShift': { - associativity: 'left', - associativeWith: [] - } - }, { - // unit conversion - 'OperatorNode:to': { - associativity: 'left', - associativeWith: [] - } - }, { - // range - RangeNode: {} - }, { - // addition, subtraction - 'OperatorNode:add': { - associativity: 'left', - associativeWith: ['OperatorNode:add', 'OperatorNode:subtract'] - }, - 'OperatorNode:subtract': { - associativity: 'left', - associativeWith: [] - } - }, { - // multiply, divide, modulus - 'OperatorNode:multiply': { - associativity: 'left', - associativeWith: ['OperatorNode:multiply', 'OperatorNode:divide', 'Operator:dotMultiply', 'Operator:dotDivide'] - }, - 'OperatorNode:divide': { - associativity: 'left', - associativeWith: [], - latexLeftParens: false, - latexRightParens: false, - latexParens: false // fractions don't require parentheses because - // they're 2 dimensional, so parens aren't needed - // in LaTeX - - }, - 'OperatorNode:dotMultiply': { - associativity: 'left', - associativeWith: ['OperatorNode:multiply', 'OperatorNode:divide', 'OperatorNode:dotMultiply', 'OperatorNode:doDivide'] - }, - 'OperatorNode:dotDivide': { - associativity: 'left', - associativeWith: [] - }, - 'OperatorNode:mod': { - associativity: 'left', - associativeWith: [] - } - }, { - // unary prefix operators - 'OperatorNode:unaryPlus': { - associativity: 'right' - }, - 'OperatorNode:unaryMinus': { - associativity: 'right' - }, - 'OperatorNode:bitNot': { - associativity: 'right' - }, - 'OperatorNode:not': { - associativity: 'right' - } - }, { - // exponentiation - 'OperatorNode:pow': { - associativity: 'right', - associativeWith: [], - latexRightParens: false // the exponent doesn't need parentheses in - // LaTeX because it's 2 dimensional - // (it's on top) - - }, - 'OperatorNode:dotPow': { - associativity: 'right', - associativeWith: [] - } - }, { - // factorial - 'OperatorNode:factorial': { - associativity: 'left' - } - }, { - // matrix transpose - 'OperatorNode:transpose': { - associativity: 'left' - } - }]; - /** - * Get the precedence of a Node. - * Higher number for higher precedence, starting with 0. - * Returns null if the precedence is undefined. - * - * @param {Node} _node - * @param {string} parenthesis - * @return {number | null} - */ - - function getPrecedence(_node, parenthesis) { - var node = _node; - - if (parenthesis !== 'keep') { - // ParenthesisNodes are only ignored when not in 'keep' mode - node = _node.getContent(); - } - - var identifier = node.getIdentifier(); - - for (var i = 0; i < operators_properties.length; i++) { - if (identifier in operators_properties[i]) { - return i; - } - } - - return null; - } - /** - * Get the associativity of an operator (left or right). - * Returns a string containing 'left' or 'right' or null if - * the associativity is not defined. - * - * @param {Node} - * @param {string} parenthesis - * @return {string|null} - * @throws {Error} - */ - - function getAssociativity(_node, parenthesis) { - var node = _node; - - if (parenthesis !== 'keep') { - // ParenthesisNodes are only ignored when not in 'keep' mode - node = _node.getContent(); - } - - var identifier = node.getIdentifier(); - var index = getPrecedence(node, parenthesis); - - if (index === null) { - // node isn't in the list - return null; - } - - var property = operators_properties[index][identifier]; - - if (Object(utils_object["f" /* hasOwnProperty */])(property, 'associativity')) { - if (property.associativity === 'left') { - return 'left'; - } - - if (property.associativity === 'right') { - return 'right'; - } // associativity is invalid - - throw Error('\'' + identifier + '\' has the invalid associativity \'' + property.associativity + '\'.'); - } // associativity is undefined - - return null; - } - /** - * Check if an operator is associative with another operator. - * Returns either true or false or null if not defined. - * - * @param {Node} nodeA - * @param {Node} nodeB - * @param {string} parenthesis - * @return {boolean | null} - */ - - function isAssociativeWith(nodeA, nodeB, parenthesis) { - // ParenthesisNodes are only ignored when not in 'keep' mode - var a = parenthesis !== 'keep' ? nodeA.getContent() : nodeA; - var b = parenthesis !== 'keep' ? nodeA.getContent() : nodeB; - var identifierA = a.getIdentifier(); - var identifierB = b.getIdentifier(); - var index = getPrecedence(a, parenthesis); - - if (index === null) { - // node isn't in the list - return null; - } - - var property = operators_properties[index][identifierA]; - - if (Object(utils_object["f" /* hasOwnProperty */])(property, 'associativeWith') && property.associativeWith instanceof Array) { - for (var i = 0; i < property.associativeWith.length; i++) { - if (property.associativeWith[i] === identifierB) { - return true; - } - } - - return false; - } // associativeWith is not defined - - return null; - } - // CONCATENATED MODULE: ./src/expression/node/AssignmentNode.js - - var AssignmentNode_name = 'AssignmentNode'; - var AssignmentNode_dependencies = ['subset', '?matrix', // FIXME: should not be needed at all, should be handled by subset - 'Node']; - var createAssignmentNode = /* #__PURE__ */Object(factory["a" /* factory */])(AssignmentNode_name, AssignmentNode_dependencies, function (_ref) { - var subset = _ref.subset, - matrix = _ref.matrix, - Node = _ref.Node; - var access = accessFactory({ - subset: subset - }); - var assign = assignFactory({ - subset: subset, - matrix: matrix - }); - /** - * @constructor AssignmentNode - * @extends {Node} - * - * Define a symbol, like `a=3.2`, update a property like `a.b=3.2`, or - * replace a subset of a matrix like `A[2,2]=42`. - * - * Syntax: - * - * new AssignmentNode(symbol, value) - * new AssignmentNode(object, index, value) - * - * Usage: - * - * new AssignmentNode(new SymbolNode('a'), new ConstantNode(2)) // a=2 - * new AssignmentNode(new SymbolNode('a'), new IndexNode('b'), new ConstantNode(2)) // a.b=2 - * new AssignmentNode(new SymbolNode('a'), new IndexNode(1, 2), new ConstantNode(3)) // a[1,2]=3 - * - * @param {SymbolNode | AccessorNode} object Object on which to assign a value - * @param {IndexNode} [index=null] Index, property name or matrix - * index. Optional. If not provided - * and `object` is a SymbolNode, - * the property is assigned to the - * global scope. - * @param {Node} value The value to be assigned - */ - - function AssignmentNode(object, index, value) { - if (!(this instanceof AssignmentNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this.object = object; - this.index = value ? index : null; - this.value = value || index; // validate input - - if (!Object(is["J" /* isSymbolNode */])(object) && !Object(is["a" /* isAccessorNode */])(object)) { - throw new TypeError('SymbolNode or AccessorNode expected as "object"'); - } - - if (Object(is["J" /* isSymbolNode */])(object) && object.name === 'end') { - throw new Error('Cannot assign to symbol "end"'); - } - - if (this.index && !Object(is["u" /* isIndexNode */])(this.index)) { - // index is optional - throw new TypeError('IndexNode expected as "index"'); - } - - if (!Object(is["w" /* isNode */])(this.value)) { - throw new TypeError('Node expected as "value"'); - } // readonly property name - - Object.defineProperty(this, 'name', { - get: function () { - if (this.index) { - return this.index.isObjectProperty() ? this.index.getObjectProperty() : ''; - } else { - return this.object.name || ''; - } - }.bind(this), - set: function set() { - throw new Error('Cannot assign a new name, name is read-only'); - } - }); - } - - AssignmentNode.prototype = new Node(); - AssignmentNode.prototype.type = 'AssignmentNode'; - AssignmentNode.prototype.isAssignmentNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - AssignmentNode.prototype._compile = function (math, argNames) { - var evalObject = this.object._compile(math, argNames); - - var evalIndex = this.index ? this.index._compile(math, argNames) : null; - - var evalValue = this.value._compile(math, argNames); - - var name = this.object.name; - - if (!this.index) { - // apply a variable to the scope, for example `a=2` - if (!Object(is["J" /* isSymbolNode */])(this.object)) { - throw new TypeError('SymbolNode expected as object'); - } - - return function evalAssignmentNode(scope, args, context) { - return setSafeProperty(scope, name, evalValue(scope, args, context)); - }; - } else if (this.index.isObjectProperty()) { - // apply an object property for example `a.b=2` - var prop = this.index.getObjectProperty(); - return function evalAssignmentNode(scope, args, context) { - var object = evalObject(scope, args, context); - var value = evalValue(scope, args, context); - return setSafeProperty(object, prop, value); - }; - } else if (Object(is["J" /* isSymbolNode */])(this.object)) { - // update a matrix subset, for example `a[2]=3` - return function evalAssignmentNode(scope, args, context) { - var childObject = evalObject(scope, args, context); - var value = evalValue(scope, args, context); - var index = evalIndex(scope, args, childObject); // Important: we pass childObject instead of context - - setSafeProperty(scope, name, assign(childObject, index, value)); - return value; - }; - } else { - // isAccessorNode(node.object) === true - // update a matrix subset, for example `a.b[2]=3` - // we will not use the compile function of the AccessorNode, but compile it - // ourselves here as we need the parent object of the AccessorNode: - // wee need to apply the updated object to parent object - var evalParentObject = this.object.object._compile(math, argNames); - - if (this.object.index.isObjectProperty()) { - var parentProp = this.object.index.getObjectProperty(); - return function evalAssignmentNode(scope, args, context) { - var parent = evalParentObject(scope, args, context); - var childObject = getSafeProperty(parent, parentProp); - var index = evalIndex(scope, args, childObject); // Important: we pass childObject instead of context - - var value = evalValue(scope, args, context); - setSafeProperty(parent, parentProp, assign(childObject, index, value)); - return value; - }; - } else { - // if some parameters use the 'end' parameter, we need to calculate the size - var evalParentIndex = this.object.index._compile(math, argNames); - - return function evalAssignmentNode(scope, args, context) { - var parent = evalParentObject(scope, args, context); - var parentIndex = evalParentIndex(scope, args, parent); // Important: we pass parent instead of context - - var childObject = access(parent, parentIndex); - var index = evalIndex(scope, args, childObject); // Important: we pass childObject instead of context - - var value = evalValue(scope, args, context); - assign(parent, parentIndex, assign(childObject, index, value)); - return value; - }; - } - } - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - AssignmentNode.prototype.forEach = function (callback) { - callback(this.object, 'object', this); - - if (this.index) { - callback(this.index, 'index', this); - } - - callback(this.value, 'value', this); - }; - /** - * Create a new AssignmentNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {AssignmentNode} Returns a transformed copy of the node - */ - - AssignmentNode.prototype.map = function (callback) { - var object = this._ifNode(callback(this.object, 'object', this)); - - var index = this.index ? this._ifNode(callback(this.index, 'index', this)) : null; - - var value = this._ifNode(callback(this.value, 'value', this)); - - return new AssignmentNode(object, index, value); - }; - /** - * Create a clone of this node, a shallow copy - * @return {AssignmentNode} - */ - - AssignmentNode.prototype.clone = function () { - return new AssignmentNode(this.object, this.index, this.value); - }; - /* - * Is parenthesis needed? - * @param {node} node - * @param {string} [parenthesis='keep'] - * @private - */ - - function needParenthesis(node, parenthesis) { - if (!parenthesis) { - parenthesis = 'keep'; - } - - var precedence = getPrecedence(node, parenthesis); - var exprPrecedence = getPrecedence(node.value, parenthesis); - return parenthesis === 'all' || exprPrecedence !== null && exprPrecedence <= precedence; - } - /** - * Get string representation - * @param {Object} options - * @return {string} - */ - - AssignmentNode.prototype._toString = function (options) { - var object = this.object.toString(options); - var index = this.index ? this.index.toString(options) : ''; - var value = this.value.toString(options); - - if (needParenthesis(this, options && options.parenthesis)) { - value = '(' + value + ')'; - } - - return object + index + ' = ' + value; - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - AssignmentNode.prototype.toJSON = function () { - return { - mathjs: 'AssignmentNode', - object: this.object, - index: this.index, - value: this.value - }; - }; - /** - * Instantiate an AssignmentNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "AssignmentNode", object: ..., index: ..., value: ...}`, - * where mathjs is optional - * @returns {AssignmentNode} - */ - - AssignmentNode.fromJSON = function (json) { - return new AssignmentNode(json.object, json.index, json.value); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} - */ - - AssignmentNode.prototype.toHTML = function (options) { - var object = this.object.toHTML(options); - var index = this.index ? this.index.toHTML(options) : ''; - var value = this.value.toHTML(options); - - if (needParenthesis(this, options && options.parenthesis)) { - value = '(' + value + ')'; - } - - return object + index + '=' + value; - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} - */ - - AssignmentNode.prototype._toTex = function (options) { - var object = this.object.toTex(options); - var index = this.index ? this.index.toTex(options) : ''; - var value = this.value.toTex(options); - - if (needParenthesis(this, options && options.parenthesis)) { - value = "\\left(".concat(value, "\\right)"); - } - - return object + index + ':=' + value; - }; - - return AssignmentNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/BlockNode.js - - var BlockNode_name = 'BlockNode'; - var BlockNode_dependencies = ['ResultSet', 'Node']; - var createBlockNode = /* #__PURE__ */Object(factory["a" /* factory */])(BlockNode_name, BlockNode_dependencies, function (_ref) { - var ResultSet = _ref.ResultSet, - Node = _ref.Node; - - /** - * @constructor BlockNode - * @extends {Node} - * Holds a set with blocks - * @param {Array.<{node: Node} | {node: Node, visible: boolean}>} blocks - * An array with blocks, where a block is constructed as an Object - * with properties block, which is a Node, and visible, which is - * a boolean. The property visible is optional and is true by default - */ - function BlockNode(blocks) { - if (!(this instanceof BlockNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } // validate input, copy blocks - - if (!Array.isArray(blocks)) { - throw new Error('Array expected'); - } - this.blocks = blocks.map(function (block) { - var node = block && block.node; - var visible = block && block.visible !== undefined ? block.visible : true; - if (!Object(is["w" /* isNode */])(node)) { - throw new TypeError('Property "node" must be a Node'); - } - if (typeof visible !== 'boolean') { - throw new TypeError('Property "visible" must be a boolean'); - } - return { - node: node, - visible: visible - }; - }); - } - - BlockNode.prototype = new Node(); - BlockNode.prototype.type = 'BlockNode'; - BlockNode.prototype.isBlockNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - BlockNode.prototype._compile = function (math, argNames) { - var evalBlocks = Object(utils_array["k" /* map */])(this.blocks, function (block) { - return { - evaluate: block.node._compile(math, argNames), - visible: block.visible - }; - }); - return function evalBlockNodes(scope, args, context) { - var results = []; - Object(utils_array["f" /* forEach */])(evalBlocks, function evalBlockNode(block) { - var result = block.evaluate(scope, args, context); - - if (block.visible) { - results.push(result); - } - }); - return new ResultSet(results); - }; - }; - /** - * Execute a callback for each of the child blocks of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - BlockNode.prototype.forEach = function (callback) { - for (var i = 0; i < this.blocks.length; i++) { - callback(this.blocks[i].node, 'blocks[' + i + '].node', this); - } - }; - /** - * Create a new BlockNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {BlockNode} Returns a transformed copy of the node - */ - - BlockNode.prototype.map = function (callback) { - var blocks = []; - - for (var i = 0; i < this.blocks.length; i++) { - var block = this.blocks[i]; - - var node = this._ifNode(callback(block.node, 'blocks[' + i + '].node', this)); - - blocks[i] = { - node: node, - visible: block.visible - }; - } - - return new BlockNode(blocks); - }; - /** - * Create a clone of this node, a shallow copy - * @return {BlockNode} - */ - - BlockNode.prototype.clone = function () { - var blocks = this.blocks.map(function (block) { - return { - node: block.node, - visible: block.visible - }; - }); - return new BlockNode(blocks); - }; - /** - * Get string representation - * @param {Object} options - * @return {string} str - * @override - */ - - BlockNode.prototype._toString = function (options) { - return this.blocks.map(function (param) { - return param.node.toString(options) + (param.visible ? '' : ';'); - }).join('\n'); - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - BlockNode.prototype.toJSON = function () { - return { - mathjs: 'BlockNode', - blocks: this.blocks - }; - }; - /** - * Instantiate an BlockNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "BlockNode", blocks: [{node: ..., visible: false}, ...]}`, - * where mathjs is optional - * @returns {BlockNode} - */ - - BlockNode.fromJSON = function (json) { - return new BlockNode(json.blocks); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - * @override - */ - - BlockNode.prototype.toHTML = function (options) { - return this.blocks.map(function (param) { - return param.node.toHTML(options) + (param.visible ? '' : ';'); - }).join('
'); - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - */ - - BlockNode.prototype._toTex = function (options) { - return this.blocks.map(function (param) { - return param.node.toTex(options) + (param.visible ? '' : ';'); - }).join('\\;\\;\n'); - }; - - return BlockNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/ConditionalNode.js - - var ConditionalNode_name = 'ConditionalNode'; - var ConditionalNode_dependencies = ['Node']; - var createConditionalNode = /* #__PURE__ */Object(factory["a" /* factory */])(ConditionalNode_name, ConditionalNode_dependencies, function (_ref) { - var Node = _ref.Node; - - /** - * A lazy evaluating conditional operator: 'condition ? trueExpr : falseExpr' - * - * @param {Node} condition Condition, must result in a boolean - * @param {Node} trueExpr Expression evaluated when condition is true - * @param {Node} falseExpr Expression evaluated when condition is true - * - * @constructor ConditionalNode - * @extends {Node} - */ - function ConditionalNode(condition, trueExpr, falseExpr) { - if (!(this instanceof ConditionalNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - if (!Object(is["w" /* isNode */])(condition)) { - throw new TypeError('Parameter condition must be a Node'); - } - if (!Object(is["w" /* isNode */])(trueExpr)) { - throw new TypeError('Parameter trueExpr must be a Node'); - } - if (!Object(is["w" /* isNode */])(falseExpr)) { - throw new TypeError('Parameter falseExpr must be a Node'); - } - this.condition = condition; - this.trueExpr = trueExpr; - this.falseExpr = falseExpr; - } - - ConditionalNode.prototype = new Node(); - ConditionalNode.prototype.type = 'ConditionalNode'; - ConditionalNode.prototype.isConditionalNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - ConditionalNode.prototype._compile = function (math, argNames) { - var evalCondition = this.condition._compile(math, argNames); - - var evalTrueExpr = this.trueExpr._compile(math, argNames); - - var evalFalseExpr = this.falseExpr._compile(math, argNames); - - return function evalConditionalNode(scope, args, context) { - return testCondition(evalCondition(scope, args, context)) ? evalTrueExpr(scope, args, context) : evalFalseExpr(scope, args, context); - }; - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - ConditionalNode.prototype.forEach = function (callback) { - callback(this.condition, 'condition', this); - callback(this.trueExpr, 'trueExpr', this); - callback(this.falseExpr, 'falseExpr', this); - }; - /** - * Create a new ConditionalNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {ConditionalNode} Returns a transformed copy of the node - */ - - ConditionalNode.prototype.map = function (callback) { - return new ConditionalNode(this._ifNode(callback(this.condition, 'condition', this)), this._ifNode(callback(this.trueExpr, 'trueExpr', this)), this._ifNode(callback(this.falseExpr, 'falseExpr', this))); - }; - /** - * Create a clone of this node, a shallow copy - * @return {ConditionalNode} - */ - - ConditionalNode.prototype.clone = function () { - return new ConditionalNode(this.condition, this.trueExpr, this.falseExpr); - }; - /** - * Get string representation - * @param {Object} options - * @return {string} str - */ - - ConditionalNode.prototype._toString = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var precedence = getPrecedence(this, parenthesis); // Enclose Arguments in parentheses if they are an OperatorNode - // or have lower or equal precedence - // NOTE: enclosing all OperatorNodes in parentheses is a decision - // purely based on aesthetics and readability - - var condition = this.condition.toString(options); - var conditionPrecedence = getPrecedence(this.condition, parenthesis); - - if (parenthesis === 'all' || this.condition.type === 'OperatorNode' || conditionPrecedence !== null && conditionPrecedence <= precedence) { - condition = '(' + condition + ')'; - } - - var trueExpr = this.trueExpr.toString(options); - var truePrecedence = getPrecedence(this.trueExpr, parenthesis); - - if (parenthesis === 'all' || this.trueExpr.type === 'OperatorNode' || truePrecedence !== null && truePrecedence <= precedence) { - trueExpr = '(' + trueExpr + ')'; - } - - var falseExpr = this.falseExpr.toString(options); - var falsePrecedence = getPrecedence(this.falseExpr, parenthesis); - - if (parenthesis === 'all' || this.falseExpr.type === 'OperatorNode' || falsePrecedence !== null && falsePrecedence <= precedence) { - falseExpr = '(' + falseExpr + ')'; - } - - return condition + ' ? ' + trueExpr + ' : ' + falseExpr; - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - ConditionalNode.prototype.toJSON = function () { - return { - mathjs: 'ConditionalNode', - condition: this.condition, - trueExpr: this.trueExpr, - falseExpr: this.falseExpr - }; - }; - /** - * Instantiate an ConditionalNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "ConditionalNode", "condition": ..., "trueExpr": ..., "falseExpr": ...}`, - * where mathjs is optional - * @returns {ConditionalNode} - */ - - ConditionalNode.fromJSON = function (json) { - return new ConditionalNode(json.condition, json.trueExpr, json.falseExpr); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - */ - - ConditionalNode.prototype.toHTML = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var precedence = getPrecedence(this, parenthesis); // Enclose Arguments in parentheses if they are an OperatorNode - // or have lower or equal precedence - // NOTE: enclosing all OperatorNodes in parentheses is a decision - // purely based on aesthetics and readability - - var condition = this.condition.toHTML(options); - var conditionPrecedence = getPrecedence(this.condition, parenthesis); - - if (parenthesis === 'all' || this.condition.type === 'OperatorNode' || conditionPrecedence !== null && conditionPrecedence <= precedence) { - condition = '(' + condition + ')'; - } - - var trueExpr = this.trueExpr.toHTML(options); - var truePrecedence = getPrecedence(this.trueExpr, parenthesis); - - if (parenthesis === 'all' || this.trueExpr.type === 'OperatorNode' || truePrecedence !== null && truePrecedence <= precedence) { - trueExpr = '(' + trueExpr + ')'; - } - - var falseExpr = this.falseExpr.toHTML(options); - var falsePrecedence = getPrecedence(this.falseExpr, parenthesis); - - if (parenthesis === 'all' || this.falseExpr.type === 'OperatorNode' || falsePrecedence !== null && falsePrecedence <= precedence) { - falseExpr = '(' + falseExpr + ')'; - } - - return condition + '?' + trueExpr + ':' + falseExpr; - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - */ - - ConditionalNode.prototype._toTex = function (options) { - return '\\begin{cases} {' + this.trueExpr.toTex(options) + '}, &\\quad{\\text{if }\\;' + this.condition.toTex(options) + '}\\\\{' + this.falseExpr.toTex(options) + '}, &\\quad{\\text{otherwise}}\\end{cases}'; - }; - /** - * Test whether a condition is met - * @param {*} condition - * @returns {boolean} true if condition is true or non-zero, else false - */ - - function testCondition(condition) { - if (typeof condition === 'number' || typeof condition === 'boolean' || typeof condition === 'string') { - return !!condition; - } - - if (condition) { - if (Object(is["e" /* isBigNumber */])(condition)) { - return !condition.isZero(); - } - - if (Object(is["j" /* isComplex */])(condition)) { - return !!(condition.re || condition.im); - } - - if (Object(is["L" /* isUnit */])(condition)) { - return !!condition.value; - } - } - - if (condition === null || condition === undefined) { - return false; - } - - throw new TypeError('Unsupported type of condition "' + Object(is["M" /* typeOf */])(condition) + '"'); - } - - return ConditionalNode; - }, { - isClass: true, - isNode: true - }); - // EXTERNAL MODULE: ./node_modules/escape-latex/dist/index.js - var dist = __webpack_require__(16); - var dist_default = /*#__PURE__*/__webpack_require__.n(dist); - - // CONCATENATED MODULE: ./src/utils/latex.js - /* eslint no-template-curly-in-string: "off" */ - - var latexSymbols = { - // GREEK LETTERS - Alpha: 'A', - alpha: '\\alpha', - Beta: 'B', - beta: '\\beta', - Gamma: '\\Gamma', - gamma: '\\gamma', - Delta: '\\Delta', - delta: '\\delta', - Epsilon: 'E', - epsilon: '\\epsilon', - varepsilon: '\\varepsilon', - Zeta: 'Z', - zeta: '\\zeta', - Eta: 'H', - eta: '\\eta', - Theta: '\\Theta', - theta: '\\theta', - vartheta: '\\vartheta', - Iota: 'I', - iota: '\\iota', - Kappa: 'K', - kappa: '\\kappa', - varkappa: '\\varkappa', - Lambda: '\\Lambda', - lambda: '\\lambda', - Mu: 'M', - mu: '\\mu', - Nu: 'N', - nu: '\\nu', - Xi: '\\Xi', - xi: '\\xi', - Omicron: 'O', - omicron: 'o', - Pi: '\\Pi', - pi: '\\pi', - varpi: '\\varpi', - Rho: 'P', - rho: '\\rho', - varrho: '\\varrho', - Sigma: '\\Sigma', - sigma: '\\sigma', - varsigma: '\\varsigma', - Tau: 'T', - tau: '\\tau', - Upsilon: "\\Upsilon", - upsilon: "\\upsilon", - Phi: '\\Phi', - phi: '\\phi', - varphi: '\\varphi', - Chi: 'X', - chi: '\\chi', - Psi: '\\Psi', - psi: '\\psi', - Omega: '\\Omega', - omega: '\\omega', - // logic - "true": '\\mathrm{True}', - "false": '\\mathrm{False}', - // other - i: 'i', - // TODO use \i ?? - inf: '\\infty', - Inf: '\\infty', - infinity: '\\infty', - Infinity: '\\infty', - oo: '\\infty', - lim: '\\lim', - undefined: '\\mathbf{?}' - }; - var latexOperators = { - transpose: '^\\top', - ctranspose: '^H', - factorial: '!', - pow: '^', - dotPow: '.^\\wedge', - // TODO find ideal solution - unaryPlus: '+', - unaryMinus: '-', - bitNot: '\\~', - // TODO find ideal solution - not: '\\neg', - multiply: '\\cdot', - divide: '\\frac', - // TODO how to handle that properly? - dotMultiply: '.\\cdot', - // TODO find ideal solution - dotDivide: '.:', - // TODO find ideal solution - mod: '\\mod', - add: '+', - subtract: '-', - to: '\\rightarrow', - leftShift: '<<', - rightArithShift: '>>', - rightLogShift: '>>>', - equal: '=', - unequal: '\\neq', - smaller: '<', - larger: '>', - smallerEq: '\\leq', - largerEq: '\\geq', - bitAnd: '\\&', - bitXor: "\\underline{|}", - bitOr: '|', - and: '\\wedge', - xor: '\\veebar', - or: '\\vee' - }; - var latexFunctions = { - // arithmetic - abs: { - 1: '\\left|${args[0]}\\right|' - }, - add: { - 2: "\\left(${args[0]}".concat(latexOperators.add, "${args[1]}\\right)") - }, - cbrt: { - 1: '\\sqrt[3]{${args[0]}}' - }, - ceil: { - 1: '\\left\\lceil${args[0]}\\right\\rceil' - }, - cube: { - 1: '\\left(${args[0]}\\right)^3' - }, - divide: { - 2: '\\frac{${args[0]}}{${args[1]}}' - }, - dotDivide: { - 2: "\\left(${args[0]}".concat(latexOperators.dotDivide, "${args[1]}\\right)") - }, - dotMultiply: { - 2: "\\left(${args[0]}".concat(latexOperators.dotMultiply, "${args[1]}\\right)") - }, - dotPow: { - 2: "\\left(${args[0]}".concat(latexOperators.dotPow, "${args[1]}\\right)") - }, - exp: { - 1: '\\exp\\left(${args[0]}\\right)' - }, - expm1: "\\left(e".concat(latexOperators.pow, "{${args[0]}}-1\\right)"), - fix: { - 1: '\\mathrm{${name}}\\left(${args[0]}\\right)' - }, - floor: { - 1: '\\left\\lfloor${args[0]}\\right\\rfloor' - }, - gcd: '\\gcd\\left(${args}\\right)', - hypot: '\\hypot\\left(${args}\\right)', - log: { - 1: '\\ln\\left(${args[0]}\\right)', - 2: '\\log_{${args[1]}}\\left(${args[0]}\\right)' - }, - log10: { - 1: '\\log_{10}\\left(${args[0]}\\right)' - }, - log1p: { - 1: '\\ln\\left(${args[0]}+1\\right)', - 2: '\\log_{${args[1]}}\\left(${args[0]}+1\\right)' - }, - log2: '\\log_{2}\\left(${args[0]}\\right)', - mod: { - 2: "\\left(${args[0]}".concat(latexOperators.mod, "${args[1]}\\right)") - }, - multiply: { - 2: "\\left(${args[0]}".concat(latexOperators.multiply, "${args[1]}\\right)") - }, - norm: { - 1: '\\left\\|${args[0]}\\right\\|', - 2: undefined // use default template - - }, - nthRoot: { - 2: '\\sqrt[${args[1]}]{${args[0]}}' - }, - nthRoots: { - 2: '\\{y : $y^{args[1]} = {${args[0]}}\\}' - }, - pow: { - 2: "\\left(${args[0]}\\right)".concat(latexOperators.pow, "{${args[1]}}") - }, - round: { - 1: '\\left\\lfloor${args[0]}\\right\\rceil', - 2: undefined // use default template - - }, - sign: { - 1: '\\mathrm{${name}}\\left(${args[0]}\\right)' - }, - sqrt: { - 1: '\\sqrt{${args[0]}}' - }, - square: { - 1: '\\left(${args[0]}\\right)^2' - }, - subtract: { - 2: "\\left(${args[0]}".concat(latexOperators.subtract, "${args[1]}\\right)") - }, - unaryMinus: { - 1: "".concat(latexOperators.unaryMinus, "\\left(${args[0]}\\right)") - }, - unaryPlus: { - 1: "".concat(latexOperators.unaryPlus, "\\left(${args[0]}\\right)") - }, - // bitwise - bitAnd: { - 2: "\\left(${args[0]}".concat(latexOperators.bitAnd, "${args[1]}\\right)") - }, - bitNot: { - 1: latexOperators.bitNot + '\\left(${args[0]}\\right)' - }, - bitOr: { - 2: "\\left(${args[0]}".concat(latexOperators.bitOr, "${args[1]}\\right)") - }, - bitXor: { - 2: "\\left(${args[0]}".concat(latexOperators.bitXor, "${args[1]}\\right)") - }, - leftShift: { - 2: "\\left(${args[0]}".concat(latexOperators.leftShift, "${args[1]}\\right)") - }, - rightArithShift: { - 2: "\\left(${args[0]}".concat(latexOperators.rightArithShift, "${args[1]}\\right)") - }, - rightLogShift: { - 2: "\\left(${args[0]}".concat(latexOperators.rightLogShift, "${args[1]}\\right)") - }, - // combinatorics - bellNumbers: { - 1: '\\mathrm{B}_{${args[0]}}' - }, - catalan: { - 1: '\\mathrm{C}_{${args[0]}}' - }, - stirlingS2: { - 2: '\\mathrm{S}\\left(${args}\\right)' - }, - // complex - arg: { - 1: '\\arg\\left(${args[0]}\\right)' - }, - conj: { - 1: '\\left(${args[0]}\\right)^*' - }, - im: { - 1: '\\Im\\left\\lbrace${args[0]}\\right\\rbrace' - }, - re: { - 1: '\\Re\\left\\lbrace${args[0]}\\right\\rbrace' - }, - // logical - and: { - 2: "\\left(${args[0]}".concat(latexOperators.and, "${args[1]}\\right)") - }, - not: { - 1: latexOperators.not + '\\left(${args[0]}\\right)' - }, - or: { - 2: "\\left(${args[0]}".concat(latexOperators.or, "${args[1]}\\right)") - }, - xor: { - 2: "\\left(${args[0]}".concat(latexOperators.xor, "${args[1]}\\right)") - }, - // matrix - cross: { - 2: '\\left(${args[0]}\\right)\\times\\left(${args[1]}\\right)' - }, - ctranspose: { - 1: "\\left(${args[0]}\\right)".concat(latexOperators.ctranspose) - }, - det: { - 1: '\\det\\left(${args[0]}\\right)' - }, - dot: { - 2: '\\left(${args[0]}\\cdot${args[1]}\\right)' - }, - expm: { - 1: '\\exp\\left(${args[0]}\\right)' - }, - inv: { - 1: '\\left(${args[0]}\\right)^{-1}' - }, - sqrtm: { - 1: "{${args[0]}}".concat(latexOperators.pow, "{\\frac{1}{2}}") - }, - trace: { - 1: '\\mathrm{tr}\\left(${args[0]}\\right)' - }, - transpose: { - 1: "\\left(${args[0]}\\right)".concat(latexOperators.transpose) - }, - // probability - combinations: { - 2: '\\binom{${args[0]}}{${args[1]}}' - }, - combinationsWithRep: { - 2: '\\left(\\!\\!{\\binom{${args[0]}}{${args[1]}}}\\!\\!\\right)' - }, - factorial: { - 1: "\\left(${args[0]}\\right)".concat(latexOperators.factorial) - }, - gamma: { - 1: '\\Gamma\\left(${args[0]}\\right)' - }, - // relational - equal: { - 2: "\\left(${args[0]}".concat(latexOperators.equal, "${args[1]}\\right)") - }, - larger: { - 2: "\\left(${args[0]}".concat(latexOperators.larger, "${args[1]}\\right)") - }, - largerEq: { - 2: "\\left(${args[0]}".concat(latexOperators.largerEq, "${args[1]}\\right)") - }, - smaller: { - 2: "\\left(${args[0]}".concat(latexOperators.smaller, "${args[1]}\\right)") - }, - smallerEq: { - 2: "\\left(${args[0]}".concat(latexOperators.smallerEq, "${args[1]}\\right)") - }, - unequal: { - 2: "\\left(${args[0]}".concat(latexOperators.unequal, "${args[1]}\\right)") - }, - // special - erf: { - 1: 'erf\\left(${args[0]}\\right)' - }, - // statistics - max: '\\max\\left(${args}\\right)', - min: '\\min\\left(${args}\\right)', - variance: '\\mathrm{Var}\\left(${args}\\right)', - // trigonometry - acos: { - 1: '\\cos^{-1}\\left(${args[0]}\\right)' - }, - acosh: { - 1: '\\cosh^{-1}\\left(${args[0]}\\right)' - }, - acot: { - 1: '\\cot^{-1}\\left(${args[0]}\\right)' - }, - acoth: { - 1: '\\coth^{-1}\\left(${args[0]}\\right)' - }, - acsc: { - 1: '\\csc^{-1}\\left(${args[0]}\\right)' - }, - acsch: { - 1: '\\mathrm{csch}^{-1}\\left(${args[0]}\\right)' - }, - asec: { - 1: '\\sec^{-1}\\left(${args[0]}\\right)' - }, - asech: { - 1: '\\mathrm{sech}^{-1}\\left(${args[0]}\\right)' - }, - asin: { - 1: '\\sin^{-1}\\left(${args[0]}\\right)' - }, - asinh: { - 1: '\\sinh^{-1}\\left(${args[0]}\\right)' - }, - atan: { - 1: '\\tan^{-1}\\left(${args[0]}\\right)' - }, - atan2: { - 2: '\\mathrm{atan2}\\left(${args}\\right)' - }, - atanh: { - 1: '\\tanh^{-1}\\left(${args[0]}\\right)' - }, - cos: { - 1: '\\cos\\left(${args[0]}\\right)' - }, - cosh: { - 1: '\\cosh\\left(${args[0]}\\right)' - }, - cot: { - 1: '\\cot\\left(${args[0]}\\right)' - }, - coth: { - 1: '\\coth\\left(${args[0]}\\right)' - }, - csc: { - 1: '\\csc\\left(${args[0]}\\right)' - }, - csch: { - 1: '\\mathrm{csch}\\left(${args[0]}\\right)' - }, - sec: { - 1: '\\sec\\left(${args[0]}\\right)' - }, - sech: { - 1: '\\mathrm{sech}\\left(${args[0]}\\right)' - }, - sin: { - 1: '\\sin\\left(${args[0]}\\right)' - }, - sinh: { - 1: '\\sinh\\left(${args[0]}\\right)' - }, - tan: { - 1: '\\tan\\left(${args[0]}\\right)' - }, - tanh: { - 1: '\\tanh\\left(${args[0]}\\right)' - }, - // unit - to: { - 2: "\\left(${args[0]}".concat(latexOperators.to, "${args[1]}\\right)") - }, - // utils - numeric: function numeric(node, options) { - // Not sure if this is strictly right but should work correctly for the vast majority of use cases. - return node.args[0].toTex(); - }, - // type - number: { - 0: '0', - 1: '\\left(${args[0]}\\right)', - 2: '\\left(\\left(${args[0]}\\right)${args[1]}\\right)' - }, - string: { - 0: '\\mathtt{""}', - 1: '\\mathrm{string}\\left(${args[0]}\\right)' - }, - bignumber: { - 0: '0', - 1: '\\left(${args[0]}\\right)' - }, - complex: { - 0: '0', - 1: '\\left(${args[0]}\\right)', - 2: "\\left(\\left(${args[0]}\\right)+".concat(latexSymbols.i, "\\cdot\\left(${args[1]}\\right)\\right)") - }, - matrix: { - 0: '\\begin{bmatrix}\\end{bmatrix}', - 1: '\\left(${args[0]}\\right)', - 2: '\\left(${args[0]}\\right)' - }, - sparse: { - 0: '\\begin{bsparse}\\end{bsparse}', - 1: '\\left(${args[0]}\\right)' - }, - unit: { - 1: '\\left(${args[0]}\\right)', - 2: '\\left(\\left(${args[0]}\\right)${args[1]}\\right)' - } - }; - var defaultTemplate = '\\mathrm{${name}}\\left(${args}\\right)'; - var latexUnits = { - deg: '^\\circ' - }; - function escapeLatex(string) { - return dist_default()(string, { - preserveFormatting: true - }); - } // @param {string} name - // @param {boolean} isUnit - - function toSymbol(name, isUnit) { - isUnit = typeof isUnit === 'undefined' ? false : isUnit; - - if (isUnit) { - if (Object(utils_object["f" /* hasOwnProperty */])(latexUnits, name)) { - return latexUnits[name]; - } - - return '\\mathrm{' + escapeLatex(name) + '}'; - } - - if (Object(utils_object["f" /* hasOwnProperty */])(latexSymbols, name)) { - return latexSymbols[name]; - } - - return escapeLatex(name); - } - // CONCATENATED MODULE: ./src/expression/node/ConstantNode.js - - var ConstantNode_name = 'ConstantNode'; - var ConstantNode_dependencies = ['Node']; - var ConstantNode_createConstantNode = /* #__PURE__ */Object(factory["a" /* factory */])(ConstantNode_name, ConstantNode_dependencies, function (_ref) { - var Node = _ref.Node; - - /** - * A ConstantNode holds a constant value like a number or string. - * - * Usage: - * - * new ConstantNode(2.3) - * new ConstantNode('hello') - * - * @param {*} value Value can be any type (number, BigNumber, string, ...) - * @constructor ConstantNode - * @extends {Node} - */ - function ConstantNode(value) { - if (!(this instanceof ConstantNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this.value = value; - } - - ConstantNode.prototype = new Node(); - ConstantNode.prototype.type = 'ConstantNode'; - ConstantNode.prototype.isConstantNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - ConstantNode.prototype._compile = function (math, argNames) { - var value = this.value; - return function evalConstantNode() { - return value; - }; - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - ConstantNode.prototype.forEach = function (callback) {// nothing to do, we don't have childs - }; - /** - * Create a new ConstantNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node) : Node} callback - * @returns {ConstantNode} Returns a clone of the node - */ - - ConstantNode.prototype.map = function (callback) { - return this.clone(); - }; - /** - * Create a clone of this node, a shallow copy - * @return {ConstantNode} - */ - - ConstantNode.prototype.clone = function () { - return new ConstantNode(this.value); - }; - /** - * Get string representation - * @param {Object} options - * @return {string} str - */ - - ConstantNode.prototype._toString = function (options) { - return Object(utils_string["d" /* format */])(this.value, options); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - */ - - ConstantNode.prototype.toHTML = function (options) { - var value = this._toString(options); - - switch (Object(is["M" /* typeOf */])(this.value)) { - case 'number': - case 'BigNumber': - case 'Fraction': - return '' + value + ''; - - case 'string': - return '' + value + ''; - - case 'boolean': - return '' + value + ''; - - case 'null': - return '' + value + ''; - - case 'undefined': - return '' + value + ''; - - default: - return '' + value + ''; - } - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - ConstantNode.prototype.toJSON = function () { - return { - mathjs: 'ConstantNode', - value: this.value - }; - }; - /** - * Instantiate a ConstantNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "SymbolNode", value: 2.3}`, - * where mathjs is optional - * @returns {ConstantNode} - */ - - ConstantNode.fromJSON = function (json) { - return new ConstantNode(json.value); - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - */ - - ConstantNode.prototype._toTex = function (options) { - var value = this._toString(options); - - switch (Object(is["M" /* typeOf */])(this.value)) { - case 'string': - return '\\mathtt{' + escapeLatex(value) + '}'; - - case 'number': - case 'BigNumber': - { - if (!isFinite(this.value)) { - return this.value.valueOf() < 0 ? '-\\infty' : '\\infty'; - } - - var index = value.toLowerCase().indexOf('e'); - - if (index !== -1) { - return value.substring(0, index) + '\\cdot10^{' + value.substring(index + 1) + '}'; - } - } - return value; - - case 'Fraction': - return this.value.toLatex(); - - default: - return value; - } - }; - - return ConstantNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/FunctionAssignmentNode.js - - var FunctionAssignmentNode_name = 'FunctionAssignmentNode'; - var FunctionAssignmentNode_dependencies = ['typed', 'Node']; - var createFunctionAssignmentNode = /* #__PURE__ */Object(factory["a" /* factory */])(FunctionAssignmentNode_name, FunctionAssignmentNode_dependencies, function (_ref) { - var typed = _ref.typed, - Node = _ref.Node; - - /** - * @constructor FunctionAssignmentNode - * @extends {Node} - * Function assignment - * - * @param {string} name Function name - * @param {string[] | Array.<{name: string, type: string}>} params - * Array with function parameter names, or an - * array with objects containing the name - * and type of the parameter - * @param {Node} expr The function expression - */ - function FunctionAssignmentNode(name, params, expr) { - if (!(this instanceof FunctionAssignmentNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } // validate input - - if (typeof name !== 'string') { - throw new TypeError('String expected for parameter "name"'); - } - if (!Array.isArray(params)) { - throw new TypeError('Array containing strings or objects expected for parameter "params"'); - } - if (!Object(is["w" /* isNode */])(expr)) { - throw new TypeError('Node expected for parameter "expr"'); - } - if (name in keywords) { - throw new Error('Illegal function name, "' + name + '" is a reserved keyword'); - } - this.name = name; - this.params = params.map(function (param) { - return param && param.name || param; - }); - this.types = params.map(function (param) { - return param && param.type || 'any'; - }); - this.expr = expr; - } - - FunctionAssignmentNode.prototype = new Node(); - FunctionAssignmentNode.prototype.type = 'FunctionAssignmentNode'; - FunctionAssignmentNode.prototype.isFunctionAssignmentNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - FunctionAssignmentNode.prototype._compile = function (math, argNames) { - var childArgNames = Object.create(argNames); - Object(utils_array["f" /* forEach */])(this.params, function (param) { - childArgNames[param] = true; - }); // compile the function expression with the child args - - var evalExpr = this.expr._compile(math, childArgNames); - - var name = this.name; - var params = this.params; - var signature = Object(utils_array["j" /* join */])(this.types, ','); - var syntax = name + '(' + Object(utils_array["j" /* join */])(this.params, ', ') + ')'; - return function evalFunctionAssignmentNode(scope, args, context) { - var signatures = {}; - - signatures[signature] = function () { - var childArgs = Object.create(args); - - for (var i = 0; i < params.length; i++) { - childArgs[params[i]] = arguments[i]; - } - - return evalExpr(scope, childArgs, context); - }; - - var fn = typed(name, signatures); - fn.syntax = syntax; - setSafeProperty(scope, name, fn); - return fn; - }; - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - FunctionAssignmentNode.prototype.forEach = function (callback) { - callback(this.expr, 'expr', this); - }; - /** - * Create a new FunctionAssignmentNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {FunctionAssignmentNode} Returns a transformed copy of the node - */ - - FunctionAssignmentNode.prototype.map = function (callback) { - var expr = this._ifNode(callback(this.expr, 'expr', this)); - - return new FunctionAssignmentNode(this.name, this.params.slice(0), expr); - }; - /** - * Create a clone of this node, a shallow copy - * @return {FunctionAssignmentNode} - */ - - FunctionAssignmentNode.prototype.clone = function () { - return new FunctionAssignmentNode(this.name, this.params.slice(0), this.expr); - }; - /** - * Is parenthesis needed? - * @param {Node} node - * @param {Object} parenthesis - * @private - */ - - function needParenthesis(node, parenthesis) { - var precedence = getPrecedence(node, parenthesis); - var exprPrecedence = getPrecedence(node.expr, parenthesis); - return parenthesis === 'all' || exprPrecedence !== null && exprPrecedence <= precedence; - } - /** - * get string representation - * @param {Object} options - * @return {string} str - */ - - FunctionAssignmentNode.prototype._toString = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var expr = this.expr.toString(options); - - if (needParenthesis(this, parenthesis)) { - expr = '(' + expr + ')'; - } - - return this.name + '(' + this.params.join(', ') + ') = ' + expr; - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - FunctionAssignmentNode.prototype.toJSON = function () { - var types = this.types; - return { - mathjs: 'FunctionAssignmentNode', - name: this.name, - params: this.params.map(function (param, index) { - return { - name: param, - type: types[index] - }; - }), - expr: this.expr - }; - }; - /** - * Instantiate an FunctionAssignmentNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "FunctionAssignmentNode", name: ..., params: ..., expr: ...}`, - * where mathjs is optional - * @returns {FunctionAssignmentNode} - */ - - FunctionAssignmentNode.fromJSON = function (json) { - return new FunctionAssignmentNode(json.name, json.params, json.expr); - }; - /** - * get HTML representation - * @param {Object} options - * @return {string} str - */ - - FunctionAssignmentNode.prototype.toHTML = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var params = []; - - for (var i = 0; i < this.params.length; i++) { - params.push('' + Object(utils_string["c" /* escape */])(this.params[i]) + ''); - } - - var expr = this.expr.toHTML(options); - - if (needParenthesis(this, parenthesis)) { - expr = '(' + expr + ')'; - } - - return '' + Object(utils_string["c" /* escape */])(this.name) + '' + '(' + params.join(',') + ')=' + expr; - }; - /** - * get LaTeX representation - * @param {Object} options - * @return {string} str - */ - - FunctionAssignmentNode.prototype._toTex = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var expr = this.expr.toTex(options); - - if (needParenthesis(this, parenthesis)) { - expr = "\\left(".concat(expr, "\\right)"); - } - - return '\\mathrm{' + this.name + '}\\left(' + this.params.map(toSymbol).join(',') + '\\right):=' + expr; - }; - - return FunctionAssignmentNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/IndexNode.js - function IndexNode_toConsumableArray(arr) { - return IndexNode_arrayWithoutHoles(arr) || IndexNode_iterableToArray(arr) || IndexNode_unsupportedIterableToArray(arr) || IndexNode_nonIterableSpread(); - } - - function IndexNode_nonIterableSpread() { - throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); - } - - function IndexNode_unsupportedIterableToArray(o, minLen) { - if (!o) { - return; - } if (typeof o === "string") { - return IndexNode_arrayLikeToArray(o, minLen); - } var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) { - n = o.constructor.name; - } if (n === "Map" || n === "Set") { - return Array.from(o); - } if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) { - return IndexNode_arrayLikeToArray(o, minLen); - } - } - - function IndexNode_iterableToArray(iter) { - if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) { - return Array.from(iter); - } - } - - function IndexNode_arrayWithoutHoles(arr) { - if (Array.isArray(arr)) { - return IndexNode_arrayLikeToArray(arr); - } - } - - function IndexNode_arrayLikeToArray(arr, len) { - if (len == null || len > arr.length) { - len = arr.length; - } for (var i = 0, arr2 = new Array(len); i < len; i++) { - arr2[i] = arr[i]; - } return arr2; - } - - var IndexNode_name = 'IndexNode'; - var IndexNode_dependencies = ['Range', 'Node', 'size']; - var createIndexNode = /* #__PURE__ */Object(factory["a" /* factory */])(IndexNode_name, IndexNode_dependencies, function (_ref) { - var Range = _ref.Range, - Node = _ref.Node, - size = _ref.size; - - /** - * @constructor IndexNode - * @extends Node - * - * Describes a subset of a matrix or an object property. - * Cannot be used on its own, needs to be used within an AccessorNode or - * AssignmentNode. - * - * @param {Node[]} dimensions - * @param {boolean} [dotNotation=false] Optional property describing whether - * this index was written using dot - * notation like `a.b`, or using bracket - * notation like `a["b"]` (default). - * Used to stringify an IndexNode. - */ - function IndexNode(dimensions, dotNotation) { - if (!(this instanceof IndexNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this.dimensions = dimensions; - this.dotNotation = dotNotation || false; // validate input - - if (!Array.isArray(dimensions) || !dimensions.every(is["w" /* isNode */])) { - throw new TypeError('Array containing Nodes expected for parameter "dimensions"'); - } - - if (this.dotNotation && !this.isObjectProperty()) { - throw new Error('dotNotation only applicable for object properties'); - } - } - - IndexNode.prototype = new Node(); - IndexNode.prototype.type = 'IndexNode'; - IndexNode.prototype.isIndexNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - IndexNode.prototype._compile = function (math, argNames) { - // TODO: implement support for bignumber (currently bignumbers are silently - // reduced to numbers when changing the value to zero-based) - // TODO: Optimization: when the range values are ConstantNodes, - // we can beforehand resolve the zero-based value - // optimization for a simple object property - var evalDimensions = Object(utils_array["k" /* map */])(this.dimensions, function (range, i) { - if (Object(is["E" /* isRangeNode */])(range)) { - if (range.needsEnd()) { - // create a range containing end (like '4:end') - var childArgNames = Object.create(argNames); - childArgNames.end = true; - - var evalStart = range.start._compile(math, childArgNames); - - var evalEnd = range.end._compile(math, childArgNames); - - var evalStep = range.step ? range.step._compile(math, childArgNames) : function () { - return 1; - }; - return function evalDimension(scope, args, context) { - var s = size(context).valueOf(); - var childArgs = Object.create(args); - childArgs.end = s[i]; - return createRange(evalStart(scope, childArgs, context), evalEnd(scope, childArgs, context), evalStep(scope, childArgs, context)); - }; - } else { - // create range - var _evalStart = range.start._compile(math, argNames); - - var _evalEnd = range.end._compile(math, argNames); - - var _evalStep = range.step ? range.step._compile(math, argNames) : function () { - return 1; - }; - - return function evalDimension(scope, args, context) { - return createRange(_evalStart(scope, args, context), _evalEnd(scope, args, context), _evalStep(scope, args, context)); - }; - } - } else if (Object(is["J" /* isSymbolNode */])(range) && range.name === 'end') { - // SymbolNode 'end' - var _childArgNames = Object.create(argNames); - - _childArgNames.end = true; - - var evalRange = range._compile(math, _childArgNames); - - return function evalDimension(scope, args, context) { - var s = size(context).valueOf(); - var childArgs = Object.create(args); - childArgs.end = s[i]; - return evalRange(scope, childArgs, context); - }; - } else { - // ConstantNode - var _evalRange = range._compile(math, argNames); - - return function evalDimension(scope, args, context) { - return _evalRange(scope, args, context); - }; - } - }); - var index = getSafeProperty(math, 'index'); - return function evalIndexNode(scope, args, context) { - var dimensions = Object(utils_array["k" /* map */])(evalDimensions, function (evalDimension) { - return evalDimension(scope, args, context); - }); - return index.apply(void 0, IndexNode_toConsumableArray(dimensions)); - }; - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - IndexNode.prototype.forEach = function (callback) { - for (var i = 0; i < this.dimensions.length; i++) { - callback(this.dimensions[i], 'dimensions[' + i + ']', this); - } - }; - /** - * Create a new IndexNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {IndexNode} Returns a transformed copy of the node - */ - - IndexNode.prototype.map = function (callback) { - var dimensions = []; - - for (var i = 0; i < this.dimensions.length; i++) { - dimensions[i] = this._ifNode(callback(this.dimensions[i], 'dimensions[' + i + ']', this)); - } - - return new IndexNode(dimensions, this.dotNotation); - }; - /** - * Create a clone of this node, a shallow copy - * @return {IndexNode} - */ - - IndexNode.prototype.clone = function () { - return new IndexNode(this.dimensions.slice(0), this.dotNotation); - }; - /** - * Test whether this IndexNode contains a single property name - * @return {boolean} - */ - - IndexNode.prototype.isObjectProperty = function () { - return this.dimensions.length === 1 && Object(is["l" /* isConstantNode */])(this.dimensions[0]) && typeof this.dimensions[0].value === 'string'; - }; - /** - * Returns the property name if IndexNode contains a property. - * If not, returns null. - * @return {string | null} - */ - - IndexNode.prototype.getObjectProperty = function () { - return this.isObjectProperty() ? this.dimensions[0].value : null; - }; - /** - * Get string representation - * @param {Object} options - * @return {string} str - */ - - IndexNode.prototype._toString = function (options) { - // format the parameters like "[1, 0:5]" - return this.dotNotation ? '.' + this.getObjectProperty() : '[' + this.dimensions.join(', ') + ']'; - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - IndexNode.prototype.toJSON = function () { - return { - mathjs: 'IndexNode', - dimensions: this.dimensions, - dotNotation: this.dotNotation - }; - }; - /** - * Instantiate an IndexNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "IndexNode", dimensions: [...], dotNotation: false}`, - * where mathjs is optional - * @returns {IndexNode} - */ - - IndexNode.fromJSON = function (json) { - return new IndexNode(json.dimensions, json.dotNotation); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - */ - - IndexNode.prototype.toHTML = function (options) { - // format the parameters like "[1, 0:5]" - var dimensions = []; - - for (var i = 0; i < this.dimensions.length; i++) { - dimensions[i] = this.dimensions[i].toHTML(); - } - - if (this.dotNotation) { - return '.' + '' + Object(utils_string["c" /* escape */])(this.getObjectProperty()) + ''; - } else { - return '[' + dimensions.join(',') + ']'; - } - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - */ - - IndexNode.prototype._toTex = function (options) { - var dimensions = this.dimensions.map(function (range) { - return range.toTex(options); - }); - return this.dotNotation ? '.' + this.getObjectProperty() + '' : '_{' + dimensions.join(',') + '}'; - }; // helper function to create a Range from start, step and end - - function createRange(start, end, step) { - return new Range(Object(is["e" /* isBigNumber */])(start) ? start.toNumber() : start, Object(is["e" /* isBigNumber */])(end) ? end.toNumber() : end, Object(is["e" /* isBigNumber */])(step) ? step.toNumber() : step); - } - - return IndexNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/ObjectNode.js - function ObjectNode_typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - ObjectNode_typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - ObjectNode_typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return ObjectNode_typeof(obj); - } - - var ObjectNode_name = 'ObjectNode'; - var ObjectNode_dependencies = ['Node']; - var createObjectNode = /* #__PURE__ */Object(factory["a" /* factory */])(ObjectNode_name, ObjectNode_dependencies, function (_ref) { - var Node = _ref.Node; - - /** - * @constructor ObjectNode - * @extends {Node} - * Holds an object with keys/values - * @param {Object.} [properties] object with key/value pairs - */ - function ObjectNode(properties) { - if (!(this instanceof ObjectNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this.properties = properties || {}; // validate input - - if (properties) { - if (!(ObjectNode_typeof(properties) === 'object') || !Object.keys(properties).every(function (key) { - return Object(is["w" /* isNode */])(properties[key]); - })) { - throw new TypeError('Object containing Nodes expected'); - } - } - } - - ObjectNode.prototype = new Node(); - ObjectNode.prototype.type = 'ObjectNode'; - ObjectNode.prototype.isObjectNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - ObjectNode.prototype._compile = function (math, argNames) { - var evalEntries = {}; - - for (var key in this.properties) { - if (Object(utils_object["f" /* hasOwnProperty */])(this.properties, key)) { - // we stringify/parse the key here to resolve unicode characters, - // so you cannot create a key like {"co\\u006Estructor": null} - var stringifiedKey = Object(utils_string["e" /* stringify */])(key); - var parsedKey = JSON.parse(stringifiedKey); - - if (!isSafeProperty(this.properties, parsedKey)) { - throw new Error('No access to property "' + parsedKey + '"'); - } - - evalEntries[parsedKey] = this.properties[key]._compile(math, argNames); - } - } - - return function evalObjectNode(scope, args, context) { - var obj = {}; - - for (var _key in evalEntries) { - if (Object(utils_object["f" /* hasOwnProperty */])(evalEntries, _key)) { - obj[_key] = evalEntries[_key](scope, args, context); - } - } - - return obj; - }; - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - ObjectNode.prototype.forEach = function (callback) { - for (var key in this.properties) { - if (Object(utils_object["f" /* hasOwnProperty */])(this.properties, key)) { - callback(this.properties[key], 'properties[' + Object(utils_string["e" /* stringify */])(key) + ']', this); - } - } - }; - /** - * Create a new ObjectNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {ObjectNode} Returns a transformed copy of the node - */ - - ObjectNode.prototype.map = function (callback) { - var properties = {}; - - for (var key in this.properties) { - if (Object(utils_object["f" /* hasOwnProperty */])(this.properties, key)) { - properties[key] = this._ifNode(callback(this.properties[key], 'properties[' + Object(utils_string["e" /* stringify */])(key) + ']', this)); - } - } - - return new ObjectNode(properties); - }; - /** - * Create a clone of this node, a shallow copy - * @return {ObjectNode} - */ - - ObjectNode.prototype.clone = function () { - var properties = {}; - - for (var key in this.properties) { - if (Object(utils_object["f" /* hasOwnProperty */])(this.properties, key)) { - properties[key] = this.properties[key]; - } - } - - return new ObjectNode(properties); - }; - /** - * Get string representation - * @param {Object} options - * @return {string} str - * @override - */ - - ObjectNode.prototype._toString = function (options) { - var entries = []; - - for (var key in this.properties) { - if (Object(utils_object["f" /* hasOwnProperty */])(this.properties, key)) { - entries.push(Object(utils_string["e" /* stringify */])(key) + ': ' + this.properties[key].toString(options)); - } - } - - return '{' + entries.join(', ') + '}'; - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - ObjectNode.prototype.toJSON = function () { - return { - mathjs: 'ObjectNode', - properties: this.properties - }; - }; - /** - * Instantiate an OperatorNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "ObjectNode", "properties": {...}}`, - * where mathjs is optional - * @returns {ObjectNode} - */ - - ObjectNode.fromJSON = function (json) { - return new ObjectNode(json.properties); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - * @override - */ - - ObjectNode.prototype.toHTML = function (options) { - var entries = []; - - for (var key in this.properties) { - if (Object(utils_object["f" /* hasOwnProperty */])(this.properties, key)) { - entries.push('' + Object(utils_string["c" /* escape */])(key) + '' + ':' + this.properties[key].toHTML(options)); - } - } - - return '{' + entries.join(',') + '}'; - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - */ - - ObjectNode.prototype._toTex = function (options) { - var entries = []; - - for (var key in this.properties) { - if (Object(utils_object["f" /* hasOwnProperty */])(this.properties, key)) { - entries.push('\\mathbf{' + key + ':} & ' + this.properties[key].toTex(options) + '\\\\'); - } - } - - return "\\left\\{\\begin{array}{ll}".concat(entries.join('\n'), "\\end{array}\\right\\}"); - }; - - return ObjectNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/OperatorNode.js - - var OperatorNode_name = 'OperatorNode'; - var OperatorNode_dependencies = ['Node']; - var createOperatorNode = /* #__PURE__ */Object(factory["a" /* factory */])(OperatorNode_name, OperatorNode_dependencies, function (_ref) { - var Node = _ref.Node; - - /** - * @constructor OperatorNode - * @extends {Node} - * An operator with two arguments, like 2+3 - * - * @param {string} op Operator name, for example '+' - * @param {string} fn Function name, for example 'add' - * @param {Node[]} args Operator arguments - * @param {boolean} [implicit] Is this an implicit multiplication? - */ - function OperatorNode(op, fn, args, implicit) { - if (!(this instanceof OperatorNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } // validate input - - if (typeof op !== 'string') { - throw new TypeError('string expected for parameter "op"'); - } - - if (typeof fn !== 'string') { - throw new TypeError('string expected for parameter "fn"'); - } - - if (!Array.isArray(args) || !args.every(is["w" /* isNode */])) { - throw new TypeError('Array containing Nodes expected for parameter "args"'); - } - - this.implicit = implicit === true; - this.op = op; - this.fn = fn; - this.args = args || []; - } - - OperatorNode.prototype = new Node(); - OperatorNode.prototype.type = 'OperatorNode'; - OperatorNode.prototype.isOperatorNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - OperatorNode.prototype._compile = function (math, argNames) { - // validate fn - if (typeof this.fn !== 'string' || !isSafeMethod(math, this.fn)) { - if (!math[this.fn]) { - throw new Error('Function ' + this.fn + ' missing in provided namespace "math"'); - } else { - throw new Error('No access to function "' + this.fn + '"'); - } - } - - var fn = getSafeProperty(math, this.fn); - var evalArgs = Object(utils_array["k" /* map */])(this.args, function (arg) { - return arg._compile(math, argNames); - }); - - if (evalArgs.length === 1) { - var evalArg0 = evalArgs[0]; - return function evalOperatorNode(scope, args, context) { - return fn(evalArg0(scope, args, context)); - }; - } else if (evalArgs.length === 2) { - var _evalArg = evalArgs[0]; - var evalArg1 = evalArgs[1]; - return function evalOperatorNode(scope, args, context) { - return fn(_evalArg(scope, args, context), evalArg1(scope, args, context)); - }; - } else { - return function evalOperatorNode(scope, args, context) { - return fn.apply(null, Object(utils_array["k" /* map */])(evalArgs, function (evalArg) { - return evalArg(scope, args, context); - })); - }; - } - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - OperatorNode.prototype.forEach = function (callback) { - for (var i = 0; i < this.args.length; i++) { - callback(this.args[i], 'args[' + i + ']', this); - } - }; - /** - * Create a new OperatorNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {OperatorNode} Returns a transformed copy of the node - */ - - OperatorNode.prototype.map = function (callback) { - var args = []; - - for (var i = 0; i < this.args.length; i++) { - args[i] = this._ifNode(callback(this.args[i], 'args[' + i + ']', this)); - } - - return new OperatorNode(this.op, this.fn, args, this.implicit); - }; - /** - * Create a clone of this node, a shallow copy - * @return {OperatorNode} - */ - - OperatorNode.prototype.clone = function () { - return new OperatorNode(this.op, this.fn, this.args.slice(0), this.implicit); - }; - /** - * Check whether this is an unary OperatorNode: - * has exactly one argument, like `-a`. - * @return {boolean} Returns true when an unary operator node, false otherwise. - */ - - OperatorNode.prototype.isUnary = function () { - return this.args.length === 1; - }; - /** - * Check whether this is a binary OperatorNode: - * has exactly two arguments, like `a + b`. - * @return {boolean} Returns true when a binary operator node, false otherwise. - */ - - OperatorNode.prototype.isBinary = function () { - return this.args.length === 2; - }; - /** - * Calculate which parentheses are necessary. Gets an OperatorNode - * (which is the root of the tree) and an Array of Nodes - * (this.args) and returns an array where 'true' means that an argument - * has to be enclosed in parentheses whereas 'false' means the opposite. - * - * @param {OperatorNode} root - * @param {string} parenthesis - * @param {Node[]} args - * @param {boolean} latex - * @return {boolean[]} - * @private - */ - - function calculateNecessaryParentheses(root, parenthesis, implicit, args, latex) { - // precedence of the root OperatorNode - var precedence = getPrecedence(root, parenthesis); - var associativity = getAssociativity(root, parenthesis); - - if (parenthesis === 'all' || args.length > 2 && root.getIdentifier() !== 'OperatorNode:add' && root.getIdentifier() !== 'OperatorNode:multiply') { - return args.map(function (arg) { - switch (arg.getContent().type) { - // Nodes that don't need extra parentheses - case 'ArrayNode': - case 'ConstantNode': - case 'SymbolNode': - case 'ParenthesisNode': - return false; - - default: - return true; - } - }); - } - - var result; - - switch (args.length) { - case 0: - result = []; - break; - - case 1: - // unary operators - { - // precedence of the operand - var operandPrecedence = getPrecedence(args[0], parenthesis); // handle special cases for LaTeX, where some of the parentheses aren't needed - - if (latex && operandPrecedence !== null) { - var operandIdentifier; - var rootIdentifier; - - if (parenthesis === 'keep') { - operandIdentifier = args[0].getIdentifier(); - rootIdentifier = root.getIdentifier(); - } else { - // Ignore Parenthesis Nodes when not in 'keep' mode - operandIdentifier = args[0].getContent().getIdentifier(); - rootIdentifier = root.getContent().getIdentifier(); - } - - if (operators_properties[precedence][rootIdentifier].latexLeftParens === false) { - result = [false]; - break; - } - - if (operators_properties[operandPrecedence][operandIdentifier].latexParens === false) { - result = [false]; - break; - } - } - - if (operandPrecedence === null) { - // if the operand has no defined precedence, no parens are needed - result = [false]; - break; - } - - if (operandPrecedence <= precedence) { - // if the operands precedence is lower, parens are needed - result = [true]; - break; - } // otherwise, no parens needed - - result = [false]; - } - break; - - case 2: - // binary operators - { - var lhsParens; // left hand side needs parenthesis? - // precedence of the left hand side - - var lhsPrecedence = getPrecedence(args[0], parenthesis); // is the root node associative with the left hand side - - var assocWithLhs = isAssociativeWith(root, args[0], parenthesis); - - if (lhsPrecedence === null) { - // if the left hand side has no defined precedence, no parens are needed - // FunctionNode for example - lhsParens = false; - } else if (lhsPrecedence === precedence && associativity === 'right' && !assocWithLhs) { - // In case of equal precedence, if the root node is left associative - // parens are **never** necessary for the left hand side. - // If it is right associative however, parens are necessary - // if the root node isn't associative with the left hand side - lhsParens = true; - } else if (lhsPrecedence < precedence) { - lhsParens = true; - } else { - lhsParens = false; - } - - var rhsParens; // right hand side needs parenthesis? - // precedence of the right hand side - - var rhsPrecedence = getPrecedence(args[1], parenthesis); // is the root node associative with the right hand side? - - var assocWithRhs = isAssociativeWith(root, args[1], parenthesis); - - if (rhsPrecedence === null) { - // if the right hand side has no defined precedence, no parens are needed - // FunctionNode for example - rhsParens = false; - } else if (rhsPrecedence === precedence && associativity === 'left' && !assocWithRhs) { - // In case of equal precedence, if the root node is right associative - // parens are **never** necessary for the right hand side. - // If it is left associative however, parens are necessary - // if the root node isn't associative with the right hand side - rhsParens = true; - } else if (rhsPrecedence < precedence) { - rhsParens = true; - } else { - rhsParens = false; - } // handle special cases for LaTeX, where some of the parentheses aren't needed - - if (latex) { - var _rootIdentifier; - - var lhsIdentifier; - var rhsIdentifier; - - if (parenthesis === 'keep') { - _rootIdentifier = root.getIdentifier(); - lhsIdentifier = root.args[0].getIdentifier(); - rhsIdentifier = root.args[1].getIdentifier(); - } else { - // Ignore ParenthesisNodes when not in 'keep' mode - _rootIdentifier = root.getContent().getIdentifier(); - lhsIdentifier = root.args[0].getContent().getIdentifier(); - rhsIdentifier = root.args[1].getContent().getIdentifier(); - } - - if (lhsPrecedence !== null) { - if (operators_properties[precedence][_rootIdentifier].latexLeftParens === false) { - lhsParens = false; - } - - if (operators_properties[lhsPrecedence][lhsIdentifier].latexParens === false) { - lhsParens = false; - } - } - - if (rhsPrecedence !== null) { - if (operators_properties[precedence][_rootIdentifier].latexRightParens === false) { - rhsParens = false; - } - - if (operators_properties[rhsPrecedence][rhsIdentifier].latexParens === false) { - rhsParens = false; - } - } - } - - result = [lhsParens, rhsParens]; - } - break; - - default: - if (root.getIdentifier() === 'OperatorNode:add' || root.getIdentifier() === 'OperatorNode:multiply') { - result = args.map(function (arg) { - var argPrecedence = getPrecedence(arg, parenthesis); - var assocWithArg = isAssociativeWith(root, arg, parenthesis); - var argAssociativity = getAssociativity(arg, parenthesis); - - if (argPrecedence === null) { - // if the argument has no defined precedence, no parens are needed - return false; - } else if (precedence === argPrecedence && associativity === argAssociativity && !assocWithArg) { - return true; - } else if (argPrecedence < precedence) { - return true; - } - - return false; - }); - } - - break; - } // handles an edge case of 'auto' parentheses with implicit multiplication of ConstantNode - // In that case print parentheses for ParenthesisNodes even though they normally wouldn't be - // printed. - - if (args.length >= 2 && root.getIdentifier() === 'OperatorNode:multiply' && root.implicit && parenthesis === 'auto' && implicit === 'hide') { - result = args.map(function (arg, index) { - var isParenthesisNode = arg.getIdentifier() === 'ParenthesisNode'; - - if (result[index] || isParenthesisNode) { - // put in parenthesis? - return true; - } - - return false; - }); - } - - return result; - } - /** - * Get string representation. - * @param {Object} options - * @return {string} str - */ - - OperatorNode.prototype._toString = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var implicit = options && options.implicit ? options.implicit : 'hide'; - var args = this.args; - var parens = calculateNecessaryParentheses(this, parenthesis, implicit, args, false); - - if (args.length === 1) { - // unary operators - var assoc = getAssociativity(this, parenthesis); - var operand = args[0].toString(options); - - if (parens[0]) { - operand = '(' + operand + ')'; - } // for example for "not", we want a space between operand and argument - - var opIsNamed = /[a-zA-Z]+/.test(this.op); - - if (assoc === 'right') { - // prefix operator - return this.op + (opIsNamed ? ' ' : '') + operand; - } else if (assoc === 'left') { - // postfix - return operand + (opIsNamed ? ' ' : '') + this.op; - } // fall back to postfix - - return operand + this.op; - } else if (args.length === 2) { - var lhs = args[0].toString(options); // left hand side - - var rhs = args[1].toString(options); // right hand side - - if (parens[0]) { - // left hand side in parenthesis? - lhs = '(' + lhs + ')'; - } - - if (parens[1]) { - // right hand side in parenthesis? - rhs = '(' + rhs + ')'; - } - - if (this.implicit && this.getIdentifier() === 'OperatorNode:multiply' && implicit === 'hide') { - return lhs + ' ' + rhs; - } - - return lhs + ' ' + this.op + ' ' + rhs; - } else if (args.length > 2 && (this.getIdentifier() === 'OperatorNode:add' || this.getIdentifier() === 'OperatorNode:multiply')) { - var stringifiedArgs = args.map(function (arg, index) { - arg = arg.toString(options); - - if (parens[index]) { - // put in parenthesis? - arg = '(' + arg + ')'; - } - - return arg; - }); - - if (this.implicit && this.getIdentifier() === 'OperatorNode:multiply' && implicit === 'hide') { - return stringifiedArgs.join(' '); - } - - return stringifiedArgs.join(' ' + this.op + ' '); - } else { - // fallback to formatting as a function call - return this.fn + '(' + this.args.join(', ') + ')'; - } - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - OperatorNode.prototype.toJSON = function () { - return { - mathjs: 'OperatorNode', - op: this.op, - fn: this.fn, - args: this.args, - implicit: this.implicit - }; - }; - /** - * Instantiate an OperatorNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "OperatorNode", "op": "+", "fn": "add", "args": [...], "implicit": false}`, - * where mathjs is optional - * @returns {OperatorNode} - */ - - OperatorNode.fromJSON = function (json) { - return new OperatorNode(json.op, json.fn, json.args, json.implicit); - }; - /** - * Get HTML representation. - * @param {Object} options - * @return {string} str - */ - - OperatorNode.prototype.toHTML = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var implicit = options && options.implicit ? options.implicit : 'hide'; - var args = this.args; - var parens = calculateNecessaryParentheses(this, parenthesis, implicit, args, false); - - if (args.length === 1) { - // unary operators - var assoc = getAssociativity(this, parenthesis); - var operand = args[0].toHTML(options); - - if (parens[0]) { - operand = '(' + operand + ')'; - } - - if (assoc === 'right') { - // prefix operator - return '' + Object(utils_string["c" /* escape */])(this.op) + '' + operand; - } else { - // postfix when assoc === 'left' or undefined - return operand + '' + Object(utils_string["c" /* escape */])(this.op) + ''; - } - } else if (args.length === 2) { - // binary operatoes - var lhs = args[0].toHTML(options); // left hand side - - var rhs = args[1].toHTML(options); // right hand side - - if (parens[0]) { - // left hand side in parenthesis? - lhs = '(' + lhs + ')'; - } - - if (parens[1]) { - // right hand side in parenthesis? - rhs = '(' + rhs + ')'; - } - - if (this.implicit && this.getIdentifier() === 'OperatorNode:multiply' && implicit === 'hide') { - return lhs + '' + rhs; - } - - return lhs + '' + Object(utils_string["c" /* escape */])(this.op) + '' + rhs; - } else { - var stringifiedArgs = args.map(function (arg, index) { - arg = arg.toHTML(options); - - if (parens[index]) { - // put in parenthesis? - arg = '(' + arg + ')'; - } - - return arg; - }); - - if (args.length > 2 && (this.getIdentifier() === 'OperatorNode:add' || this.getIdentifier() === 'OperatorNode:multiply')) { - if (this.implicit && this.getIdentifier() === 'OperatorNode:multiply' && implicit === 'hide') { - return stringifiedArgs.join(''); - } - - return stringifiedArgs.join('' + Object(utils_string["c" /* escape */])(this.op) + ''); - } else { - // fallback to formatting as a function call - return '' + Object(utils_string["c" /* escape */])(this.fn) + '(' + stringifiedArgs.join(',') + ')'; - } - } - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - */ - - OperatorNode.prototype._toTex = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var implicit = options && options.implicit ? options.implicit : 'hide'; - var args = this.args; - var parens = calculateNecessaryParentheses(this, parenthesis, implicit, args, true); - var op = latexOperators[this.fn]; - op = typeof op === 'undefined' ? this.op : op; // fall back to using this.op - - if (args.length === 1) { - // unary operators - var assoc = getAssociativity(this, parenthesis); - var operand = args[0].toTex(options); - - if (parens[0]) { - operand = "\\left(".concat(operand, "\\right)"); - } - - if (assoc === 'right') { - // prefix operator - return op + operand; - } else if (assoc === 'left') { - // postfix operator - return operand + op; - } // fall back to postfix - - return operand + op; - } else if (args.length === 2) { - // binary operators - var lhs = args[0]; // left hand side - - var lhsTex = lhs.toTex(options); - - if (parens[0]) { - lhsTex = "\\left(".concat(lhsTex, "\\right)"); - } - - var rhs = args[1]; // right hand side - - var rhsTex = rhs.toTex(options); - - if (parens[1]) { - rhsTex = "\\left(".concat(rhsTex, "\\right)"); - } // handle some exceptions (due to the way LaTeX works) - - var lhsIdentifier; - - if (parenthesis === 'keep') { - lhsIdentifier = lhs.getIdentifier(); - } else { - // Ignore ParenthesisNodes if in 'keep' mode - lhsIdentifier = lhs.getContent().getIdentifier(); - } - - switch (this.getIdentifier()) { - case 'OperatorNode:divide': - // op contains '\\frac' at this point - return op + '{' + lhsTex + '}' + '{' + rhsTex + '}'; - - case 'OperatorNode:pow': - lhsTex = '{' + lhsTex + '}'; - rhsTex = '{' + rhsTex + '}'; - - switch (lhsIdentifier) { - case 'ConditionalNode': // - - case 'OperatorNode:divide': - lhsTex = "\\left(".concat(lhsTex, "\\right)"); - } - - break; - - case 'OperatorNode:multiply': - if (this.implicit && implicit === 'hide') { - return lhsTex + '~' + rhsTex; - } - - } - - return lhsTex + op + rhsTex; - } else if (args.length > 2 && (this.getIdentifier() === 'OperatorNode:add' || this.getIdentifier() === 'OperatorNode:multiply')) { - var texifiedArgs = args.map(function (arg, index) { - arg = arg.toTex(options); - - if (parens[index]) { - arg = "\\left(".concat(arg, "\\right)"); - } - - return arg; - }); - - if (this.getIdentifier() === 'OperatorNode:multiply' && this.implicit) { - return texifiedArgs.join('~'); - } - - return texifiedArgs.join(op); - } else { - // fall back to formatting as a function call - // as this is a fallback, it doesn't use - // fancy function names - return '\\mathrm{' + this.fn + '}\\left(' + args.map(function (arg) { - return arg.toTex(options); - }).join(',') + '\\right)'; - } - }; - /** - * Get identifier. - * @return {string} - */ - - OperatorNode.prototype.getIdentifier = function () { - return this.type + ':' + this.fn; - }; - - return OperatorNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/ParenthesisNode.js - - var ParenthesisNode_name = 'ParenthesisNode'; - var ParenthesisNode_dependencies = ['Node']; - var createParenthesisNode = /* #__PURE__ */Object(factory["a" /* factory */])(ParenthesisNode_name, ParenthesisNode_dependencies, function (_ref) { - var Node = _ref.Node; - - /** - * @constructor ParenthesisNode - * @extends {Node} - * A parenthesis node describes manual parenthesis from the user input - * @param {Node} content - * @extends {Node} - */ - function ParenthesisNode(content) { - if (!(this instanceof ParenthesisNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } // validate input - - if (!Object(is["w" /* isNode */])(content)) { - throw new TypeError('Node expected for parameter "content"'); - } - - this.content = content; - } - - ParenthesisNode.prototype = new Node(); - ParenthesisNode.prototype.type = 'ParenthesisNode'; - ParenthesisNode.prototype.isParenthesisNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - ParenthesisNode.prototype._compile = function (math, argNames) { - return this.content._compile(math, argNames); - }; - /** - * Get the content of the current Node. - * @return {Node} content - * @override - **/ - - ParenthesisNode.prototype.getContent = function () { - return this.content.getContent(); - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - ParenthesisNode.prototype.forEach = function (callback) { - callback(this.content, 'content', this); - }; - /** - * Create a new ParenthesisNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node) : Node} callback - * @returns {ParenthesisNode} Returns a clone of the node - */ - - ParenthesisNode.prototype.map = function (callback) { - var content = callback(this.content, 'content', this); - return new ParenthesisNode(content); - }; - /** - * Create a clone of this node, a shallow copy - * @return {ParenthesisNode} - */ - - ParenthesisNode.prototype.clone = function () { - return new ParenthesisNode(this.content); - }; - /** - * Get string representation - * @param {Object} options - * @return {string} str - * @override - */ - - ParenthesisNode.prototype._toString = function (options) { - if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') { - return '(' + this.content.toString(options) + ')'; - } - - return this.content.toString(options); - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - ParenthesisNode.prototype.toJSON = function () { - return { - mathjs: 'ParenthesisNode', - content: this.content - }; - }; - /** - * Instantiate an ParenthesisNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "ParenthesisNode", "content": ...}`, - * where mathjs is optional - * @returns {ParenthesisNode} - */ - - ParenthesisNode.fromJSON = function (json) { - return new ParenthesisNode(json.content); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - * @override - */ - - ParenthesisNode.prototype.toHTML = function (options) { - if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') { - return '(' + this.content.toHTML(options) + ')'; - } - - return this.content.toHTML(options); - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - * @override - */ - - ParenthesisNode.prototype._toTex = function (options) { - if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') { - return "\\left(".concat(this.content.toTex(options), "\\right)"); - } - - return this.content.toTex(options); - }; - - return ParenthesisNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/RangeNode.js - - var RangeNode_name = 'RangeNode'; - var RangeNode_dependencies = ['Node']; - var createRangeNode = /* #__PURE__ */Object(factory["a" /* factory */])(RangeNode_name, RangeNode_dependencies, function (_ref) { - var Node = _ref.Node; - - /** - * @constructor RangeNode - * @extends {Node} - * create a range - * @param {Node} start included lower-bound - * @param {Node} end included upper-bound - * @param {Node} [step] optional step - */ - function RangeNode(start, end, step) { - if (!(this instanceof RangeNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } // validate inputs - - if (!Object(is["w" /* isNode */])(start)) { - throw new TypeError('Node expected'); - } - if (!Object(is["w" /* isNode */])(end)) { - throw new TypeError('Node expected'); - } - if (step && !Object(is["w" /* isNode */])(step)) { - throw new TypeError('Node expected'); - } - if (arguments.length > 3) { - throw new Error('Too many arguments'); - } - this.start = start; // included lower-bound - - this.end = end; // included upper-bound - - this.step = step || null; // optional step - } - - RangeNode.prototype = new Node(); - RangeNode.prototype.type = 'RangeNode'; - RangeNode.prototype.isRangeNode = true; - /** - * Check whether the RangeNode needs the `end` symbol to be defined. - * This end is the size of the Matrix in current dimension. - * @return {boolean} - */ - - RangeNode.prototype.needsEnd = function () { - // find all `end` symbols in this RangeNode - var endSymbols = this.filter(function (node) { - return Object(is["J" /* isSymbolNode */])(node) && node.name === 'end'; - }); - return endSymbols.length > 0; - }; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - RangeNode.prototype._compile = function (math, argNames) { - var range = math.range; - - var evalStart = this.start._compile(math, argNames); - - var evalEnd = this.end._compile(math, argNames); - - if (this.step) { - var evalStep = this.step._compile(math, argNames); - - return function evalRangeNode(scope, args, context) { - return range(evalStart(scope, args, context), evalEnd(scope, args, context), evalStep(scope, args, context)); - }; - } else { - return function evalRangeNode(scope, args, context) { - return range(evalStart(scope, args, context), evalEnd(scope, args, context)); - }; - } - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - RangeNode.prototype.forEach = function (callback) { - callback(this.start, 'start', this); - callback(this.end, 'end', this); - - if (this.step) { - callback(this.step, 'step', this); - } - }; - /** - * Create a new RangeNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {RangeNode} Returns a transformed copy of the node - */ - - RangeNode.prototype.map = function (callback) { - return new RangeNode(this._ifNode(callback(this.start, 'start', this)), this._ifNode(callback(this.end, 'end', this)), this.step && this._ifNode(callback(this.step, 'step', this))); - }; - /** - * Create a clone of this node, a shallow copy - * @return {RangeNode} - */ - - RangeNode.prototype.clone = function () { - return new RangeNode(this.start, this.end, this.step && this.step); - }; - /** - * Calculate the necessary parentheses - * @param {Node} node - * @param {string} parenthesis - * @return {Object} parentheses - * @private - */ - - function calculateNecessaryParentheses(node, parenthesis) { - var precedence = getPrecedence(node, parenthesis); - var parens = {}; - var startPrecedence = getPrecedence(node.start, parenthesis); - parens.start = startPrecedence !== null && startPrecedence <= precedence || parenthesis === 'all'; - - if (node.step) { - var stepPrecedence = getPrecedence(node.step, parenthesis); - parens.step = stepPrecedence !== null && stepPrecedence <= precedence || parenthesis === 'all'; - } - - var endPrecedence = getPrecedence(node.end, parenthesis); - parens.end = endPrecedence !== null && endPrecedence <= precedence || parenthesis === 'all'; - return parens; - } - /** - * Get string representation - * @param {Object} options - * @return {string} str - */ - - RangeNode.prototype._toString = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var parens = calculateNecessaryParentheses(this, parenthesis); // format string as start:step:stop - - var str; - var start = this.start.toString(options); - - if (parens.start) { - start = '(' + start + ')'; - } - - str = start; - - if (this.step) { - var step = this.step.toString(options); - - if (parens.step) { - step = '(' + step + ')'; - } - - str += ':' + step; - } - - var end = this.end.toString(options); - - if (parens.end) { - end = '(' + end + ')'; - } - - str += ':' + end; - return str; - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - RangeNode.prototype.toJSON = function () { - return { - mathjs: 'RangeNode', - start: this.start, - end: this.end, - step: this.step - }; - }; - /** - * Instantiate an RangeNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "RangeNode", "start": ..., "end": ..., "step": ...}`, - * where mathjs is optional - * @returns {RangeNode} - */ - - RangeNode.fromJSON = function (json) { - return new RangeNode(json.start, json.end, json.step); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - */ - - RangeNode.prototype.toHTML = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var parens = calculateNecessaryParentheses(this, parenthesis); // format string as start:step:stop - - var str; - var start = this.start.toHTML(options); - - if (parens.start) { - start = '(' + start + ')'; - } - - str = start; - - if (this.step) { - var step = this.step.toHTML(options); - - if (parens.step) { - step = '(' + step + ')'; - } - - str += ':' + step; - } - - var end = this.end.toHTML(options); - - if (parens.end) { - end = '(' + end + ')'; - } - - str += ':' + end; - return str; - }; - /** - * Get LaTeX representation - * @params {Object} options - * @return {string} str - */ - - RangeNode.prototype._toTex = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var parens = calculateNecessaryParentheses(this, parenthesis); - var str = this.start.toTex(options); - - if (parens.start) { - str = "\\left(".concat(str, "\\right)"); - } - - if (this.step) { - var step = this.step.toTex(options); - - if (parens.step) { - step = "\\left(".concat(step, "\\right)"); - } - - str += ':' + step; - } - - var end = this.end.toTex(options); - - if (parens.end) { - end = "\\left(".concat(end, "\\right)"); - } - - str += ':' + end; - return str; - }; - - return RangeNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/RelationalNode.js - - var RelationalNode_name = 'RelationalNode'; - var RelationalNode_dependencies = ['Node']; - var createRelationalNode = /* #__PURE__ */Object(factory["a" /* factory */])(RelationalNode_name, RelationalNode_dependencies, function (_ref) { - var Node = _ref.Node; - - /** - * A node representing a chained conditional expression, such as 'x > y > z' - * - * @param {String[]} conditionals An array of conditional operators used to compare the parameters - * @param {Node[]} params The parameters that will be compared - * - * @constructor RelationalNode - * @extends {Node} - */ - function RelationalNode(conditionals, params) { - if (!(this instanceof RelationalNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - if (!Array.isArray(conditionals)) { - throw new TypeError('Parameter conditionals must be an array'); - } - if (!Array.isArray(params)) { - throw new TypeError('Parameter params must be an array'); - } - if (conditionals.length !== params.length - 1) { - throw new TypeError('Parameter params must contain exactly one more element than parameter conditionals'); - } - this.conditionals = conditionals; - this.params = params; - } - - RelationalNode.prototype = new Node(); - RelationalNode.prototype.type = 'RelationalNode'; - RelationalNode.prototype.isRelationalNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - RelationalNode.prototype._compile = function (math, argNames) { - var self = this; - var compiled = this.params.map(function (p) { - return p._compile(math, argNames); - }); - return function evalRelationalNode(scope, args, context) { - var evalLhs; - var evalRhs = compiled[0](scope, args, context); - - for (var i = 0; i < self.conditionals.length; i++) { - evalLhs = evalRhs; - evalRhs = compiled[i + 1](scope, args, context); - var condFn = getSafeProperty(math, self.conditionals[i]); - - if (!condFn(evalLhs, evalRhs)) { - return false; - } - } - - return true; - }; - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - RelationalNode.prototype.forEach = function (callback) { - var _this = this; - - this.params.forEach(function (n, i) { - return callback(n, 'params[' + i + ']', _this); - }, this); - }; - /** - * Create a new RelationalNode having its childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {RelationalNode} Returns a transformed copy of the node - */ - - RelationalNode.prototype.map = function (callback) { - var _this2 = this; - - return new RelationalNode(this.conditionals.slice(), this.params.map(function (n, i) { - return _this2._ifNode(callback(n, 'params[' + i + ']', _this2)); - }, this)); - }; - /** - * Create a clone of this node, a shallow copy - * @return {RelationalNode} - */ - - RelationalNode.prototype.clone = function () { - return new RelationalNode(this.conditionals, this.params); - }; - /** - * Get string representation. - * @param {Object} options - * @return {string} str - */ - - RelationalNode.prototype._toString = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var precedence = getPrecedence(this, parenthesis); - var paramStrings = this.params.map(function (p, index) { - var paramPrecedence = getPrecedence(p, parenthesis); - return parenthesis === 'all' || paramPrecedence !== null && paramPrecedence <= precedence ? '(' + p.toString(options) + ')' : p.toString(options); - }); - var operatorMap = { - equal: '==', - unequal: '!=', - smaller: '<', - larger: '>', - smallerEq: '<=', - largerEq: '>=' - }; - var ret = paramStrings[0]; - - for (var i = 0; i < this.conditionals.length; i++) { - ret += ' ' + operatorMap[this.conditionals[i]] + ' ' + paramStrings[i + 1]; - } - - return ret; - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - RelationalNode.prototype.toJSON = function () { - return { - mathjs: 'RelationalNode', - conditionals: this.conditionals, - params: this.params - }; - }; - /** - * Instantiate a RelationalNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "RelationalNode", "condition": ..., "trueExpr": ..., "falseExpr": ...}`, - * where mathjs is optional - * @returns {RelationalNode} - */ - - RelationalNode.fromJSON = function (json) { - return new RelationalNode(json.conditionals, json.params); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - */ - - RelationalNode.prototype.toHTML = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var precedence = getPrecedence(this, parenthesis); - var paramStrings = this.params.map(function (p, index) { - var paramPrecedence = getPrecedence(p, parenthesis); - return parenthesis === 'all' || paramPrecedence !== null && paramPrecedence <= precedence ? '(' + p.toHTML(options) + ')' : p.toHTML(options); - }); - var operatorMap = { - equal: '==', - unequal: '!=', - smaller: '<', - larger: '>', - smallerEq: '<=', - largerEq: '>=' - }; - var ret = paramStrings[0]; - - for (var i = 0; i < this.conditionals.length; i++) { - ret += '' + Object(utils_string["c" /* escape */])(operatorMap[this.conditionals[i]]) + '' + paramStrings[i + 1]; - } - - return ret; - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - */ - - RelationalNode.prototype._toTex = function (options) { - var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep'; - var precedence = getPrecedence(this, parenthesis); - var paramStrings = this.params.map(function (p, index) { - var paramPrecedence = getPrecedence(p, parenthesis); - return parenthesis === 'all' || paramPrecedence !== null && paramPrecedence <= precedence ? '\\left(' + p.toTex(options) + '\right)' : p.toTex(options); - }); - var ret = paramStrings[0]; - - for (var i = 0; i < this.conditionals.length; i++) { - ret += latexOperators[this.conditionals[i]] + paramStrings[i + 1]; - } - - return ret; - }; - - return RelationalNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/SymbolNode.js - - var SymbolNode_name = 'SymbolNode'; - var SymbolNode_dependencies = ['math', '?Unit', 'Node']; - var createSymbolNode = /* #__PURE__ */Object(factory["a" /* factory */])(SymbolNode_name, SymbolNode_dependencies, function (_ref) { - var math = _ref.math, - Unit = _ref.Unit, - Node = _ref.Node; - - /** - * Check whether some name is a valueless unit like "inch". - * @param {string} name - * @return {boolean} - */ - function isValuelessUnit(name) { - return Unit ? Unit.isValuelessUnit(name) : false; - } - /** - * @constructor SymbolNode - * @extends {Node} - * A symbol node can hold and resolve a symbol - * @param {string} name - * @extends {Node} - */ - - function SymbolNode(name) { - if (!(this instanceof SymbolNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } // validate input - - if (typeof name !== 'string') { - throw new TypeError('String expected for parameter "name"'); - } - this.name = name; - } - - SymbolNode.prototype = new Node(); - SymbolNode.prototype.type = 'SymbolNode'; - SymbolNode.prototype.isSymbolNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - SymbolNode.prototype._compile = function (math, argNames) { - var name = this.name; - - if (argNames[name] === true) { - // this is a FunctionAssignment argument - // (like an x when inside the expression of a function assignment `f(x) = ...`) - return function (scope, args, context) { - return args[name]; - }; - } else if (name in math) { - return function (scope, args, context) { - return name in scope ? getSafeProperty(scope, name) : getSafeProperty(math, name); - }; - } else { - var isUnit = isValuelessUnit(name); - return function (scope, args, context) { - return name in scope ? getSafeProperty(scope, name) : isUnit ? new Unit(null, name) : undef(name); - }; - } - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - SymbolNode.prototype.forEach = function (callback) {// nothing to do, we don't have childs - }; - /** - * Create a new SymbolNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node) : Node} callback - * @returns {SymbolNode} Returns a clone of the node - */ - - SymbolNode.prototype.map = function (callback) { - return this.clone(); - }; - /** - * Throws an error 'Undefined symbol {name}' - * @param {string} name - */ - - function undef(name) { - throw new Error('Undefined symbol ' + name); - } - /** - * Create a clone of this node, a shallow copy - * @return {SymbolNode} - */ - - SymbolNode.prototype.clone = function () { - return new SymbolNode(this.name); - }; - /** - * Get string representation - * @param {Object} options - * @return {string} str - * @override - */ - - SymbolNode.prototype._toString = function (options) { - return this.name; - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - * @override - */ - - SymbolNode.prototype.toHTML = function (options) { - var name = Object(utils_string["c" /* escape */])(this.name); - - if (name === 'true' || name === 'false') { - return '' + name + ''; - } else if (name === 'i') { - return '' + name + ''; - } else if (name === 'Infinity') { - return '' + name + ''; - } else if (name === 'NaN') { - return '' + name + ''; - } else if (name === 'null') { - return '' + name + ''; - } else if (name === 'undefined') { - return '' + name + ''; - } - - return '' + name + ''; - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - SymbolNode.prototype.toJSON = function () { - return { - mathjs: 'SymbolNode', - name: this.name - }; - }; - /** - * Instantiate a SymbolNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "SymbolNode", name: "x"}`, - * where mathjs is optional - * @returns {SymbolNode} - */ - - SymbolNode.fromJSON = function (json) { - return new SymbolNode(json.name); - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - * @override - */ - - SymbolNode.prototype._toTex = function (options) { - var isUnit = false; - - if (typeof math[this.name] === 'undefined' && isValuelessUnit(this.name)) { - isUnit = true; - } - - var symbol = toSymbol(this.name, isUnit); - - if (symbol[0] === '\\') { - // no space needed if the symbol starts with '\' - return symbol; - } // the space prevents symbols from breaking stuff like '\cdot' if it's written right before the symbol - - return ' ' + symbol; - }; - - return SymbolNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/node/FunctionNode.js - function FunctionNode_typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - FunctionNode_typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - FunctionNode_typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return FunctionNode_typeof(obj); - } - - function FunctionNode_extends() { - FunctionNode_extends = Object.assign || function (target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i]; for (var key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } return target; - }; return FunctionNode_extends.apply(this, arguments); - } - - var FunctionNode_name = 'FunctionNode'; - var FunctionNode_dependencies = ['math', 'Node', 'SymbolNode']; - var createFunctionNode = /* #__PURE__ */Object(factory["a" /* factory */])(FunctionNode_name, FunctionNode_dependencies, function (_ref) { - var math = _ref.math, - Node = _ref.Node, - SymbolNode = _ref.SymbolNode; - - /** - * @constructor FunctionNode - * @extends {./Node} - * invoke a list with arguments on a node - * @param {./Node | string} fn Node resolving with a function on which to invoke - * the arguments, typically a SymboNode or AccessorNode - * @param {./Node[]} args - */ - function FunctionNode(fn, args) { - if (!(this instanceof FunctionNode)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - if (typeof fn === 'string') { - fn = new SymbolNode(fn); - } // validate input - - if (!Object(is["w" /* isNode */])(fn)) { - throw new TypeError('Node expected as parameter "fn"'); - } - - if (!Array.isArray(args) || !args.every(is["w" /* isNode */])) { - throw new TypeError('Array containing Nodes expected for parameter "args"'); - } - - this.fn = fn; - this.args = args || []; // readonly property name - - Object.defineProperty(this, 'name', { - get: function () { - return this.fn.name || ''; - }.bind(this), - set: function set() { - throw new Error('Cannot assign a new name, name is read-only'); - } - }); - } - - FunctionNode.prototype = new Node(); - FunctionNode.prototype.type = 'FunctionNode'; - FunctionNode.prototype.isFunctionNode = true; - /** - * Compile a node into a JavaScript function. - * This basically pre-calculates as much as possible and only leaves open - * calculations which depend on a dynamic scope with variables. - * @param {Object} math Math.js namespace with functions and constants. - * @param {Object} argNames An object with argument names as key and `true` - * as value. Used in the SymbolNode to optimize - * for arguments from user assigned functions - * (see FunctionAssignmentNode) or special symbols - * like `end` (see IndexNode). - * @return {function} Returns a function which can be called like: - * evalNode(scope: Object, args: Object, context: *) - */ - - FunctionNode.prototype._compile = function (math, argNames) { - if (!(this instanceof FunctionNode)) { - throw new TypeError('No valid FunctionNode'); - } // compile arguments - - var evalArgs = Object(utils_array["k" /* map */])(this.args, function (arg) { - return arg._compile(math, argNames); - }); - - if (Object(is["J" /* isSymbolNode */])(this.fn)) { - // we can statically determine whether the function has an rawArgs property - var _name = this.fn.name; - var fn = _name in math ? getSafeProperty(math, _name) : undefined; - var isRaw = typeof fn === 'function' && fn.rawArgs === true; - - if (isRaw) { - // pass unevaluated parameters (nodes) to the function - // "raw" evaluation - var rawArgs = this.args; - return function evalFunctionNode(scope, args, context) { - return (_name in scope ? getSafeProperty(scope, _name) : fn)(rawArgs, math, FunctionNode_extends({}, scope, args)); - }; - } else { - // "regular" evaluation - if (evalArgs.length === 1) { - var evalArg0 = evalArgs[0]; - return function evalFunctionNode(scope, args, context) { - return (_name in scope ? getSafeProperty(scope, _name) : fn)(evalArg0(scope, args, context)); - }; - } else if (evalArgs.length === 2) { - var _evalArg = evalArgs[0]; - var evalArg1 = evalArgs[1]; - return function evalFunctionNode(scope, args, context) { - return (_name in scope ? getSafeProperty(scope, _name) : fn)(_evalArg(scope, args, context), evalArg1(scope, args, context)); - }; - } else { - return function evalFunctionNode(scope, args, context) { - return (_name in scope ? getSafeProperty(scope, _name) : fn).apply(null, Object(utils_array["k" /* map */])(evalArgs, function (evalArg) { - return evalArg(scope, args, context); - })); - }; - } - } - } else if (Object(is["a" /* isAccessorNode */])(this.fn) && Object(is["u" /* isIndexNode */])(this.fn.index) && this.fn.index.isObjectProperty()) { - // execute the function with the right context: the object of the AccessorNode - var evalObject = this.fn.object._compile(math, argNames); - - var prop = this.fn.index.getObjectProperty(); - var _rawArgs = this.args; - return function evalFunctionNode(scope, args, context) { - var object = evalObject(scope, args, context); - validateSafeMethod(object, prop); - var isRaw = object[prop] && object[prop].rawArgs; - return isRaw ? object[prop](_rawArgs, math, FunctionNode_extends({}, scope, args)) // "raw" evaluation - : object[prop].apply(object, Object(utils_array["k" /* map */])(evalArgs, function (evalArg) { - // "regular" evaluation - return evalArg(scope, args, context); - })); - }; - } else { - // node.fn.isAccessorNode && !node.fn.index.isObjectProperty() - // we have to dynamically determine whether the function has a rawArgs property - var evalFn = this.fn._compile(math, argNames); - - var _rawArgs2 = this.args; - return function evalFunctionNode(scope, args, context) { - var fn = evalFn(scope, args, context); - var isRaw = fn && fn.rawArgs; - return isRaw ? fn(_rawArgs2, math, FunctionNode_extends({}, scope, args)) // "raw" evaluation - : fn.apply(fn, Object(utils_array["k" /* map */])(evalArgs, function (evalArg) { - // "regular" evaluation - return evalArg(scope, args, context); - })); - }; - } - }; - /** - * Execute a callback for each of the child nodes of this node - * @param {function(child: Node, path: string, parent: Node)} callback - */ - - FunctionNode.prototype.forEach = function (callback) { - callback(this.fn, 'fn', this); - - for (var i = 0; i < this.args.length; i++) { - callback(this.args[i], 'args[' + i + ']', this); - } - }; - /** - * Create a new FunctionNode having it's childs be the results of calling - * the provided callback function for each of the childs of the original node. - * @param {function(child: Node, path: string, parent: Node): Node} callback - * @returns {FunctionNode} Returns a transformed copy of the node - */ - - FunctionNode.prototype.map = function (callback) { - var fn = this._ifNode(callback(this.fn, 'fn', this)); - - var args = []; - - for (var i = 0; i < this.args.length; i++) { - args[i] = this._ifNode(callback(this.args[i], 'args[' + i + ']', this)); - } - - return new FunctionNode(fn, args); - }; - /** - * Create a clone of this node, a shallow copy - * @return {FunctionNode} - */ - - FunctionNode.prototype.clone = function () { - return new FunctionNode(this.fn, this.args.slice(0)); - }; // backup Node's toString function - // @private - - var nodeToString = FunctionNode.prototype.toString; - /** - * Get string representation. (wrapper function) - * This overrides parts of Node's toString function. - * If callback is an object containing callbacks, it - * calls the correct callback for the current node, - * otherwise it falls back to calling Node's toString - * function. - * - * @param {Object} options - * @return {string} str - * @override - */ - - FunctionNode.prototype.toString = function (options) { - var customString; - var name = this.fn.toString(options); - - if (options && FunctionNode_typeof(options.handler) === 'object' && Object(utils_object["f" /* hasOwnProperty */])(options.handler, name)) { - // callback is a map of callback functions - customString = options.handler[name](this, options); - } - - if (typeof customString !== 'undefined') { - return customString; - } // fall back to Node's toString - - return nodeToString.call(this, options); - }; - /** - * Get string representation - * @param {Object} options - * @return {string} str - */ - - FunctionNode.prototype._toString = function (options) { - var args = this.args.map(function (arg) { - return arg.toString(options); - }); - var fn = Object(is["q" /* isFunctionAssignmentNode */])(this.fn) ? '(' + this.fn.toString(options) + ')' : this.fn.toString(options); // format the arguments like "add(2, 4.2)" - - return fn + '(' + args.join(', ') + ')'; - }; - /** - * Get a JSON representation of the node - * @returns {Object} - */ - - FunctionNode.prototype.toJSON = function () { - return { - mathjs: 'FunctionNode', - fn: this.fn, - args: this.args - }; - }; - /** - * Instantiate an AssignmentNode from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "FunctionNode", fn: ..., args: ...}`, - * where mathjs is optional - * @returns {FunctionNode} - */ - - FunctionNode.fromJSON = function (json) { - return new FunctionNode(json.fn, json.args); - }; - /** - * Get HTML representation - * @param {Object} options - * @return {string} str - */ - - FunctionNode.prototype.toHTML = function (options) { - var args = this.args.map(function (arg) { - return arg.toHTML(options); - }); // format the arguments like "add(2, 4.2)" - - return '' + Object(utils_string["c" /* escape */])(this.fn) + '(' + args.join(',') + ')'; - }; - /* - * Expand a LaTeX template - * - * @param {string} template - * @param {Node} node - * @param {Object} options - * @private - **/ - - function expandTemplate(template, node, options) { - var latex = ''; // Match everything of the form ${identifier} or ${identifier[2]} or $$ - // while submatching identifier and 2 (in the second case) - - var regex = new RegExp('\\$(?:\\{([a-z_][a-z_0-9]*)(?:\\[([0-9]+)\\])?\\}|\\$)', 'ig'); - var inputPos = 0; // position in the input string - - var match; - - while ((match = regex.exec(template)) !== null) { - // go through all matches - // add everything in front of the match to the LaTeX string - latex += template.substring(inputPos, match.index); - inputPos = match.index; - - if (match[0] === '$$') { - // escaped dollar sign - latex += '$'; - inputPos++; - } else { - // template parameter - inputPos += match[0].length; - var property = node[match[1]]; - - if (!property) { - throw new ReferenceError('Template: Property ' + match[1] + ' does not exist.'); - } - - if (match[2] === undefined) { - // no square brackets - switch (FunctionNode_typeof(property)) { - case 'string': - latex += property; - break; - - case 'object': - if (Object(is["w" /* isNode */])(property)) { - latex += property.toTex(options); - } else if (Array.isArray(property)) { - // make array of Nodes into comma separated list - latex += property.map(function (arg, index) { - if (Object(is["w" /* isNode */])(arg)) { - return arg.toTex(options); - } - - throw new TypeError('Template: ' + match[1] + '[' + index + '] is not a Node.'); - }).join(','); - } else { - throw new TypeError('Template: ' + match[1] + ' has to be a Node, String or array of Nodes'); - } - - break; - - default: - throw new TypeError('Template: ' + match[1] + ' has to be a Node, String or array of Nodes'); - } - } else { - // with square brackets - if (Object(is["w" /* isNode */])(property[match[2]] && property[match[2]])) { - latex += property[match[2]].toTex(options); - } else { - throw new TypeError('Template: ' + match[1] + '[' + match[2] + '] is not a Node.'); - } - } - } - } - - latex += template.slice(inputPos); // append rest of the template - - return latex; - } // backup Node's toTex function - // @private - - var nodeToTex = FunctionNode.prototype.toTex; - /** - * Get LaTeX representation. (wrapper function) - * This overrides parts of Node's toTex function. - * If callback is an object containing callbacks, it - * calls the correct callback for the current node, - * otherwise it falls back to calling Node's toTex - * function. - * - * @param {Object} options - * @return {string} - */ - - FunctionNode.prototype.toTex = function (options) { - var customTex; - - if (options && FunctionNode_typeof(options.handler) === 'object' && Object(utils_object["f" /* hasOwnProperty */])(options.handler, this.name)) { - // callback is a map of callback functions - customTex = options.handler[this.name](this, options); - } - - if (typeof customTex !== 'undefined') { - return customTex; - } // fall back to Node's toTex - - return nodeToTex.call(this, options); - }; - /** - * Get LaTeX representation - * @param {Object} options - * @return {string} str - */ - - FunctionNode.prototype._toTex = function (options) { - var args = this.args.map(function (arg) { - // get LaTeX of the arguments - return arg.toTex(options); - }); - var latexConverter; - - if (latexFunctions[this.name]) { - latexConverter = latexFunctions[this.name]; - } // toTex property on the function itself - - if (math[this.name] && (typeof math[this.name].toTex === 'function' || FunctionNode_typeof(math[this.name].toTex) === 'object' || typeof math[this.name].toTex === 'string')) { - // .toTex is a callback function - latexConverter = math[this.name].toTex; - } - - var customToTex; - - switch (FunctionNode_typeof(latexConverter)) { - case 'function': - // a callback function - customToTex = latexConverter(this, options); - break; - - case 'string': - // a template string - customToTex = expandTemplate(latexConverter, this, options); - break; - - case 'object': - // an object with different "converters" for different numbers of arguments - switch (FunctionNode_typeof(latexConverter[args.length])) { - case 'function': - customToTex = latexConverter[args.length](this, options); - break; - - case 'string': - customToTex = expandTemplate(latexConverter[args.length], this, options); - break; - } - - } - - if (typeof customToTex !== 'undefined') { - return customToTex; - } - - return expandTemplate(defaultTemplate, this, options); - }; - /** - * Get identifier. - * @return {string} - */ - - FunctionNode.prototype.getIdentifier = function () { - return this.type + ':' + this.name; - }; - - return FunctionNode; - }, { - isClass: true, - isNode: true - }); - // CONCATENATED MODULE: ./src/expression/parse.js - function parse_extends() { - parse_extends = Object.assign || function (target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i]; for (var key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } return target; - }; return parse_extends.apply(this, arguments); - } - - var parse_name = 'parse'; - var parse_dependencies = ['typed', 'numeric', 'config', 'AccessorNode', 'ArrayNode', 'AssignmentNode', 'BlockNode', 'ConditionalNode', 'ConstantNode', 'FunctionAssignmentNode', 'FunctionNode', 'IndexNode', 'ObjectNode', 'OperatorNode', 'ParenthesisNode', 'RangeNode', 'RelationalNode', 'SymbolNode']; - var createParse = /* #__PURE__ */Object(factory["a" /* factory */])(parse_name, parse_dependencies, function (_ref) { - var typed = _ref.typed, - numeric = _ref.numeric, - config = _ref.config, - AccessorNode = _ref.AccessorNode, - ArrayNode = _ref.ArrayNode, - AssignmentNode = _ref.AssignmentNode, - BlockNode = _ref.BlockNode, - ConditionalNode = _ref.ConditionalNode, - ConstantNode = _ref.ConstantNode, - FunctionAssignmentNode = _ref.FunctionAssignmentNode, - FunctionNode = _ref.FunctionNode, - IndexNode = _ref.IndexNode, - ObjectNode = _ref.ObjectNode, - OperatorNode = _ref.OperatorNode, - ParenthesisNode = _ref.ParenthesisNode, - RangeNode = _ref.RangeNode, - RelationalNode = _ref.RelationalNode, - SymbolNode = _ref.SymbolNode; - - /** - * Parse an expression. Returns a node tree, which can be evaluated by - * invoking node.evaluate(). - * - * Note the evaluating arbitrary expressions may involve security risks, - * see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information. - * - * Syntax: - * - * math.parse(expr) - * math.parse(expr, options) - * math.parse([expr1, expr2, expr3, ...]) - * math.parse([expr1, expr2, expr3, ...], options) - * - * Example: - * - * const node1 = math.parse('sqrt(3^2 + 4^2)') - * node1.compile().evaluate() // 5 - * - * let scope = {a:3, b:4} - * const node2 = math.parse('a * b') // 12 - * const code2 = node2.compile() - * code2.evaluate(scope) // 12 - * scope.a = 5 - * code2.evaluate(scope) // 20 - * - * const nodes = math.parse(['a = 3', 'b = 4', 'a * b']) - * nodes[2].compile().evaluate() // 12 - * - * See also: - * - * evaluate, compile - * - * @param {string | string[] | Matrix} expr Expression to be parsed - * @param {{nodes: Object}} [options] Available options: - * - `nodes` a set of custom nodes - * @return {Node | Node[]} node - * @throws {Error} - */ - var parse = typed(parse_name, { - string: function string(expression) { - return parseStart(expression, {}); - }, - 'Array | Matrix': function ArrayMatrix(expressions) { - return parseMultiple(expressions, {}); - }, - 'string, Object': function stringObject(expression, options) { - var extraNodes = options.nodes !== undefined ? options.nodes : {}; - return parseStart(expression, extraNodes); - }, - 'Array | Matrix, Object': parseMultiple - }); - - function parseMultiple(expressions) { - var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - var extraNodes = options.nodes !== undefined ? options.nodes : {}; // parse an array or matrix with expressions - - return deepMap(expressions, function (elem) { - if (typeof elem !== 'string') { - throw new TypeError('String expected'); - } - return parseStart(elem, extraNodes); - }); - } // token types enumeration - - var TOKENTYPE = { - NULL: 0, - DELIMITER: 1, - NUMBER: 2, - SYMBOL: 3, - UNKNOWN: 4 - }; // map with all delimiters - - var DELIMITERS = { - ',': true, - '(': true, - ')': true, - '[': true, - ']': true, - '{': true, - '}': true, - '"': true, - '\'': true, - ';': true, - '+': true, - '-': true, - '*': true, - '.*': true, - '/': true, - './': true, - '%': true, - '^': true, - '.^': true, - '~': true, - '!': true, - '&': true, - '|': true, - '^|': true, - '=': true, - ':': true, - '?': true, - '==': true, - '!=': true, - '<': true, - '>': true, - '<=': true, - '>=': true, - '<<': true, - '>>': true, - '>>>': true - }; // map with all named delimiters - - var NAMED_DELIMITERS = { - mod: true, - to: true, - "in": true, - and: true, - xor: true, - or: true, - not: true - }; - var CONSTANTS = { - "true": true, - "false": false, - "null": null, - undefined: undefined - }; - var NUMERIC_CONSTANTS = ['NaN', 'Infinity']; - - function initialState() { - return { - extraNodes: {}, - // current extra nodes, must be careful not to mutate - expression: '', - // current expression - comment: '', - // last parsed comment - index: 0, - // current index in expr - token: '', - // current token - tokenType: TOKENTYPE.NULL, - // type of the token - nestingLevel: 0, - // level of nesting inside parameters, used to ignore newline characters - conditionalLevel: null // when a conditional is being parsed, the level of the conditional is stored here - - }; - } - /** - * View upto `length` characters of the expression starting at the current character. - * - * @param {Object} state - * @param {number} [length=1] Number of characters to view - * @returns {string} - * @private - */ - - function currentString(state, length) { - return state.expression.substr(state.index, length); - } - /** - * View the current character. Returns '' if end of expression is reached. - * - * @param {Object} state - * @returns {string} - * @private - */ - - function currentCharacter(state) { - return currentString(state, 1); - } - /** - * Get the next character from the expression. - * The character is stored into the char c. If the end of the expression is - * reached, the function puts an empty string in c. - * @private - */ - - function next(state) { - state.index++; - } - /** - * Preview the previous character from the expression. - * @return {string} cNext - * @private - */ - - function prevCharacter(state) { - return state.expression.charAt(state.index - 1); - } - /** - * Preview the next character from the expression. - * @return {string} cNext - * @private - */ - - function nextCharacter(state) { - return state.expression.charAt(state.index + 1); - } - /** - * Get next token in the current string expr. - * The token and token type are available as token and tokenType - * @private - */ - - function getToken(state) { - state.tokenType = TOKENTYPE.NULL; - state.token = ''; - state.comment = ''; // skip over whitespaces - // space, tab, and newline when inside parameters - - while (parse.isWhitespace(currentCharacter(state), state.nestingLevel)) { - next(state); - } // skip comment - - if (currentCharacter(state) === '#') { - while (currentCharacter(state) !== '\n' && currentCharacter(state) !== '') { - state.comment += currentCharacter(state); - next(state); - } - } // check for end of expression - - if (currentCharacter(state) === '') { - // token is still empty - state.tokenType = TOKENTYPE.DELIMITER; - return; - } // check for new line character - - if (currentCharacter(state) === '\n' && !state.nestingLevel) { - state.tokenType = TOKENTYPE.DELIMITER; - state.token = currentCharacter(state); - next(state); - return; - } - - var c1 = currentCharacter(state); - var c2 = currentString(state, 2); - var c3 = currentString(state, 3); - - if (c3.length === 3 && DELIMITERS[c3]) { - state.tokenType = TOKENTYPE.DELIMITER; - state.token = c3; - next(state); - next(state); - next(state); - return; - } // check for delimiters consisting of 2 characters - - if (c2.length === 2 && DELIMITERS[c2]) { - state.tokenType = TOKENTYPE.DELIMITER; - state.token = c2; - next(state); - next(state); - return; - } // check for delimiters consisting of 1 character - - if (DELIMITERS[c1]) { - state.tokenType = TOKENTYPE.DELIMITER; - state.token = c1; - next(state); - return; - } // check for a number - - if (parse.isDigitDot(c1)) { - state.tokenType = TOKENTYPE.NUMBER; // check for binary, octal, or hex - - var _c = currentString(state, 2); - - if (_c === '0b' || _c === '0o' || _c === '0x') { - state.token += currentCharacter(state); - next(state); - state.token += currentCharacter(state); - next(state); - - while (parse.isHexDigit(currentCharacter(state))) { - state.token += currentCharacter(state); - next(state); - } - - return; - } // get number, can have a single dot - - if (currentCharacter(state) === '.') { - state.token += currentCharacter(state); - next(state); - - if (!parse.isDigit(currentCharacter(state))) { - // this is no number, it is just a dot (can be dot notation) - state.tokenType = TOKENTYPE.DELIMITER; - } - } else { - while (parse.isDigit(currentCharacter(state))) { - state.token += currentCharacter(state); - next(state); - } - - if (parse.isDecimalMark(currentCharacter(state), nextCharacter(state))) { - state.token += currentCharacter(state); - next(state); - } - } - - while (parse.isDigit(currentCharacter(state))) { - state.token += currentCharacter(state); - next(state); - } // check for exponential notation like "2.3e-4", "1.23e50" or "2e+4" - - if (currentCharacter(state) === 'E' || currentCharacter(state) === 'e') { - if (parse.isDigit(nextCharacter(state)) || nextCharacter(state) === '-' || nextCharacter(state) === '+') { - state.token += currentCharacter(state); - next(state); - - if (currentCharacter(state) === '+' || currentCharacter(state) === '-') { - state.token += currentCharacter(state); - next(state); - } // Scientific notation MUST be followed by an exponent - - if (!parse.isDigit(currentCharacter(state))) { - throw createSyntaxError(state, 'Digit expected, got "' + currentCharacter(state) + '"'); - } - - while (parse.isDigit(currentCharacter(state))) { - state.token += currentCharacter(state); - next(state); - } - - if (parse.isDecimalMark(currentCharacter(state), nextCharacter(state))) { - throw createSyntaxError(state, 'Digit expected, got "' + currentCharacter(state) + '"'); - } - } else if (nextCharacter(state) === '.') { - next(state); - throw createSyntaxError(state, 'Digit expected, got "' + currentCharacter(state) + '"'); - } - } - - return; - } // check for variables, functions, named operators - - if (parse.isAlpha(currentCharacter(state), prevCharacter(state), nextCharacter(state))) { - while (parse.isAlpha(currentCharacter(state), prevCharacter(state), nextCharacter(state)) || parse.isDigit(currentCharacter(state))) { - state.token += currentCharacter(state); - next(state); - } - - if (Object(utils_object["f" /* hasOwnProperty */])(NAMED_DELIMITERS, state.token)) { - state.tokenType = TOKENTYPE.DELIMITER; - } else { - state.tokenType = TOKENTYPE.SYMBOL; - } - - return; - } // something unknown is found, wrong characters -> a syntax error - - state.tokenType = TOKENTYPE.UNKNOWN; - - while (currentCharacter(state) !== '') { - state.token += currentCharacter(state); - next(state); - } - - throw createSyntaxError(state, 'Syntax error in part "' + state.token + '"'); - } - /** - * Get next token and skip newline tokens - */ - - function getTokenSkipNewline(state) { - do { - getToken(state); - } while (state.token === '\n'); // eslint-disable-line no-unmodified-loop-condition - - } - /** - * Open parameters. - * New line characters will be ignored until closeParams(state) is called - */ - - function openParams(state) { - state.nestingLevel++; - } - /** - * Close parameters. - * New line characters will no longer be ignored - */ - - function closeParams(state) { - state.nestingLevel--; - } - /** - * Checks whether the current character `c` is a valid alpha character: - * - * - A latin letter (upper or lower case) Ascii: a-z, A-Z - * - An underscore Ascii: _ - * - A dollar sign Ascii: $ - * - A latin letter with accents Unicode: \u00C0 - \u02AF - * - A greek letter Unicode: \u0370 - \u03FF - * - A mathematical alphanumeric symbol Unicode: \u{1D400} - \u{1D7FF} excluding invalid code points - * - * The previous and next characters are needed to determine whether - * this character is part of a unicode surrogate pair. - * - * @param {string} c Current character in the expression - * @param {string} cPrev Previous character - * @param {string} cNext Next character - * @return {boolean} - */ - - parse.isAlpha = function isAlpha(c, cPrev, cNext) { - return parse.isValidLatinOrGreek(c) || parse.isValidMathSymbol(c, cNext) || parse.isValidMathSymbol(cPrev, c); - }; - /** - * Test whether a character is a valid latin, greek, or letter-like character - * @param {string} c - * @return {boolean} - */ - - parse.isValidLatinOrGreek = function isValidLatinOrGreek(c) { - return /^[a-zA-Z_$\u00C0-\u02AF\u0370-\u03FF\u2100-\u214F]$/.test(c); - }; - /** - * Test whether two given 16 bit characters form a surrogate pair of a - * unicode math symbol. - * - * https://unicode-table.com/en/ - * https://www.wikiwand.com/en/Mathematical_operators_and_symbols_in_Unicode - * - * Note: In ES6 will be unicode aware: - * https://stackoverflow.com/questions/280712/javascript-unicode-regexes - * https://mathiasbynens.be/notes/es6-unicode-regex - * - * @param {string} high - * @param {string} low - * @return {boolean} - */ - - parse.isValidMathSymbol = function isValidMathSymbol(high, low) { - return /^[\uD835]$/.test(high) && /^[\uDC00-\uDFFF]$/.test(low) && /^[^\uDC55\uDC9D\uDCA0\uDCA1\uDCA3\uDCA4\uDCA7\uDCA8\uDCAD\uDCBA\uDCBC\uDCC4\uDD06\uDD0B\uDD0C\uDD15\uDD1D\uDD3A\uDD3F\uDD45\uDD47-\uDD49\uDD51\uDEA6\uDEA7\uDFCC\uDFCD]$/.test(low); - }; - /** - * Check whether given character c is a white space character: space, tab, or enter - * @param {string} c - * @param {number} nestingLevel - * @return {boolean} - */ - - parse.isWhitespace = function isWhitespace(c, nestingLevel) { - // TODO: also take '\r' carriage return as newline? Or does that give problems on mac? - return c === ' ' || c === '\t' || c === '\n' && nestingLevel > 0; - }; - /** - * Test whether the character c is a decimal mark (dot). - * This is the case when it's not the start of a delimiter '.*', './', or '.^' - * @param {string} c - * @param {string} cNext - * @return {boolean} - */ - - parse.isDecimalMark = function isDecimalMark(c, cNext) { - return c === '.' && cNext !== '/' && cNext !== '*' && cNext !== '^'; - }; - /** - * checks if the given char c is a digit or dot - * @param {string} c a string with one character - * @return {boolean} - */ - - parse.isDigitDot = function isDigitDot(c) { - return c >= '0' && c <= '9' || c === '.'; - }; - /** - * checks if the given char c is a digit - * @param {string} c a string with one character - * @return {boolean} - */ - - parse.isDigit = function isDigit(c) { - return c >= '0' && c <= '9'; - }; - /** - * checks if the given char c is a hex digit - * @param {string} c a string with one character - * @return {boolean} - */ - - parse.isHexDigit = function isHexDigit(c) { - return c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'; - }; - /** - * Start of the parse levels below, in order of precedence - * @return {Node} node - * @private - */ - - function parseStart(expression, extraNodes) { - var state = initialState(); - - parse_extends(state, { - expression: expression, - extraNodes: extraNodes - }); - - getToken(state); - var node = parseBlock(state); // check for garbage at the end of the expression - // an expression ends with a empty character '' and tokenType DELIMITER - - if (state.token !== '') { - if (state.tokenType === TOKENTYPE.DELIMITER) { - // user entered a not existing operator like "//" - // TODO: give hints for aliases, for example with "<>" give as hint " did you mean !== ?" - throw createError(state, 'Unexpected operator ' + state.token); - } else { - throw createSyntaxError(state, 'Unexpected part "' + state.token + '"'); - } - } - - return node; - } - /** - * Parse a block with expressions. Expressions can be separated by a newline - * character '\n', or by a semicolon ';'. In case of a semicolon, no output - * of the preceding line is returned. - * @return {Node} node - * @private - */ - - function parseBlock(state) { - var node; - var blocks = []; - var visible; - - if (state.token !== '' && state.token !== '\n' && state.token !== ';') { - node = parseAssignment(state); - node.comment = state.comment; - } // TODO: simplify this loop - - while (state.token === '\n' || state.token === ';') { - // eslint-disable-line no-unmodified-loop-condition - if (blocks.length === 0 && node) { - visible = state.token !== ';'; - blocks.push({ - node: node, - visible: visible - }); - } - - getToken(state); - - if (state.token !== '\n' && state.token !== ';' && state.token !== '') { - node = parseAssignment(state); - node.comment = state.comment; - visible = state.token !== ';'; - blocks.push({ - node: node, - visible: visible - }); - } - } - - if (blocks.length > 0) { - return new BlockNode(blocks); - } else { - if (!node) { - node = new ConstantNode(undefined); - node.comment = state.comment; - } - - return node; - } - } - /** - * Assignment of a function or variable, - * - can be a variable like 'a=2.3' - * - or a updating an existing variable like 'matrix(2,3:5)=[6,7,8]' - * - defining a function like 'f(x) = x^2' - * @return {Node} node - * @private - */ - - function parseAssignment(state) { - var name, args, value, valid; - var node = parseConditional(state); - - if (state.token === '=') { - if (Object(is["J" /* isSymbolNode */])(node)) { - // parse a variable assignment like 'a = 2/3' - name = node.name; - getTokenSkipNewline(state); - value = parseAssignment(state); - return new AssignmentNode(new SymbolNode(name), value); - } else if (Object(is["a" /* isAccessorNode */])(node)) { - // parse a matrix subset assignment like 'A[1,2] = 4' - getTokenSkipNewline(state); - value = parseAssignment(state); - return new AssignmentNode(node.object, node.index, value); - } else if (Object(is["r" /* isFunctionNode */])(node) && Object(is["J" /* isSymbolNode */])(node.fn)) { - // parse function assignment like 'f(x) = x^2' - valid = true; - args = []; - name = node.name; - node.args.forEach(function (arg, index) { - if (Object(is["J" /* isSymbolNode */])(arg)) { - args[index] = arg.name; - } else { - valid = false; - } - }); - - if (valid) { - getTokenSkipNewline(state); - value = parseAssignment(state); - return new FunctionAssignmentNode(name, args, value); - } - } - - throw createSyntaxError(state, 'Invalid left hand side of assignment operator ='); - } - - return node; - } - /** - * conditional operation - * - * condition ? truePart : falsePart - * - * Note: conditional operator is right-associative - * - * @return {Node} node - * @private - */ - - function parseConditional(state) { - var node = parseLogicalOr(state); - - while (state.token === '?') { - // eslint-disable-line no-unmodified-loop-condition - // set a conditional level, the range operator will be ignored as long - // as conditionalLevel === state.nestingLevel. - var prev = state.conditionalLevel; - state.conditionalLevel = state.nestingLevel; - getTokenSkipNewline(state); - var condition = node; - var trueExpr = parseAssignment(state); - if (state.token !== ':') { - throw createSyntaxError(state, 'False part of conditional expression expected'); - } - state.conditionalLevel = null; - getTokenSkipNewline(state); - var falseExpr = parseAssignment(state); // Note: check for conditional operator again, right associativity - - node = new ConditionalNode(condition, trueExpr, falseExpr); // restore the previous conditional level - - state.conditionalLevel = prev; - } - - return node; - } - /** - * logical or, 'x or y' - * @return {Node} node - * @private - */ - - function parseLogicalOr(state) { - var node = parseLogicalXor(state); - - while (state.token === 'or') { - // eslint-disable-line no-unmodified-loop-condition - getTokenSkipNewline(state); - node = new OperatorNode('or', 'or', [node, parseLogicalXor(state)]); - } - - return node; - } - /** - * logical exclusive or, 'x xor y' - * @return {Node} node - * @private - */ - - function parseLogicalXor(state) { - var node = parseLogicalAnd(state); - - while (state.token === 'xor') { - // eslint-disable-line no-unmodified-loop-condition - getTokenSkipNewline(state); - node = new OperatorNode('xor', 'xor', [node, parseLogicalAnd(state)]); - } - - return node; - } - /** - * logical and, 'x and y' - * @return {Node} node - * @private - */ - - function parseLogicalAnd(state) { - var node = parseBitwiseOr(state); - - while (state.token === 'and') { - // eslint-disable-line no-unmodified-loop-condition - getTokenSkipNewline(state); - node = new OperatorNode('and', 'and', [node, parseBitwiseOr(state)]); - } - - return node; - } - /** - * bitwise or, 'x | y' - * @return {Node} node - * @private - */ - - function parseBitwiseOr(state) { - var node = parseBitwiseXor(state); - - while (state.token === '|') { - // eslint-disable-line no-unmodified-loop-condition - getTokenSkipNewline(state); - node = new OperatorNode('|', 'bitOr', [node, parseBitwiseXor(state)]); - } - - return node; - } - /** - * bitwise exclusive or (xor), 'x ^| y' - * @return {Node} node - * @private - */ - - function parseBitwiseXor(state) { - var node = parseBitwiseAnd(state); - - while (state.token === '^|') { - // eslint-disable-line no-unmodified-loop-condition - getTokenSkipNewline(state); - node = new OperatorNode('^|', 'bitXor', [node, parseBitwiseAnd(state)]); - } - - return node; - } - /** - * bitwise and, 'x & y' - * @return {Node} node - * @private - */ - - function parseBitwiseAnd(state) { - var node = parseRelational(state); - - while (state.token === '&') { - // eslint-disable-line no-unmodified-loop-condition - getTokenSkipNewline(state); - node = new OperatorNode('&', 'bitAnd', [node, parseRelational(state)]); - } - - return node; - } - /** - * Parse a chained conditional, like 'a > b >= c' - * @return {Node} node - */ - - function parseRelational(state) { - var params = [parseShift(state)]; - var conditionals = []; - var operators = { - '==': 'equal', - '!=': 'unequal', - '<': 'smaller', - '>': 'larger', - '<=': 'smallerEq', - '>=': 'largerEq' - }; - - while (Object(utils_object["f" /* hasOwnProperty */])(operators, state.token)) { - // eslint-disable-line no-unmodified-loop-condition - var cond = { - name: state.token, - fn: operators[state.token] - }; - conditionals.push(cond); - getTokenSkipNewline(state); - params.push(parseShift(state)); - } - - if (params.length === 1) { - return params[0]; - } else if (params.length === 2) { - return new OperatorNode(conditionals[0].name, conditionals[0].fn, params); - } else { - return new RelationalNode(conditionals.map(function (c) { - return c.fn; - }), params); - } - } - /** - * Bitwise left shift, bitwise right arithmetic shift, bitwise right logical shift - * @return {Node} node - * @private - */ - - function parseShift(state) { - var node, name, fn, params; - node = parseConversion(state); - var operators = { - '<<': 'leftShift', - '>>': 'rightArithShift', - '>>>': 'rightLogShift' - }; - - while (Object(utils_object["f" /* hasOwnProperty */])(operators, state.token)) { - name = state.token; - fn = operators[name]; - getTokenSkipNewline(state); - params = [node, parseConversion(state)]; - node = new OperatorNode(name, fn, params); - } - - return node; - } - /** - * conversion operators 'to' and 'in' - * @return {Node} node - * @private - */ - - function parseConversion(state) { - var node, name, fn, params; - node = parseRange(state); - var operators = { - to: 'to', - "in": 'to' // alias of 'to' - - }; - - while (Object(utils_object["f" /* hasOwnProperty */])(operators, state.token)) { - name = state.token; - fn = operators[name]; - getTokenSkipNewline(state); - - if (name === 'in' && state.token === '') { - // end of expression -> this is the unit 'in' ('inch') - node = new OperatorNode('*', 'multiply', [node, new SymbolNode('in')], true); - } else { - // operator 'a to b' or 'a in b' - params = [node, parseRange(state)]; - node = new OperatorNode(name, fn, params); - } - } - - return node; - } - /** - * parse range, "start:end", "start:step:end", ":", "start:", ":end", etc - * @return {Node} node - * @private - */ - - function parseRange(state) { - var node; - var params = []; - - if (state.token === ':') { - // implicit start=1 (one-based) - node = new ConstantNode(1); - } else { - // explicit start - node = parseAddSubtract(state); - } - - if (state.token === ':' && state.conditionalLevel !== state.nestingLevel) { - // we ignore the range operator when a conditional operator is being processed on the same level - params.push(node); // parse step and end - - while (state.token === ':' && params.length < 3) { - // eslint-disable-line no-unmodified-loop-condition - getTokenSkipNewline(state); - - if (state.token === ')' || state.token === ']' || state.token === ',' || state.token === '') { - // implicit end - params.push(new SymbolNode('end')); - } else { - // explicit end - params.push(parseAddSubtract(state)); - } - } - - if (params.length === 3) { - // params = [start, step, end] - node = new RangeNode(params[0], params[2], params[1]); // start, end, step - } else { - // length === 2 - // params = [start, end] - node = new RangeNode(params[0], params[1]); // start, end - } - } - - return node; - } - /** - * add or subtract - * @return {Node} node - * @private - */ - - function parseAddSubtract(state) { - var node, name, fn, params; - node = parseMultiplyDivide(state); - var operators = { - '+': 'add', - '-': 'subtract' - }; - - while (Object(utils_object["f" /* hasOwnProperty */])(operators, state.token)) { - name = state.token; - fn = operators[name]; - getTokenSkipNewline(state); - params = [node, parseMultiplyDivide(state)]; - node = new OperatorNode(name, fn, params); - } - - return node; - } - /** - * multiply, divide, modulus - * @return {Node} node - * @private - */ - - function parseMultiplyDivide(state) { - var node, last, name, fn; - node = parseImplicitMultiplication(state); - last = node; - var operators = { - '*': 'multiply', - '.*': 'dotMultiply', - '/': 'divide', - './': 'dotDivide', - '%': 'mod', - mod: 'mod' - }; - - while (true) { - if (Object(utils_object["f" /* hasOwnProperty */])(operators, state.token)) { - // explicit operators - name = state.token; - fn = operators[name]; - getTokenSkipNewline(state); - last = parseImplicitMultiplication(state); - node = new OperatorNode(name, fn, [node, last]); - } else { - break; - } - } - - return node; - } - /** - * implicit multiplication - * @return {Node} node - * @private - */ - - function parseImplicitMultiplication(state) { - var node, last; - node = parseRule2(state); - last = node; - - while (true) { - if (state.tokenType === TOKENTYPE.SYMBOL || state.token === 'in' && Object(is["l" /* isConstantNode */])(node) || state.tokenType === TOKENTYPE.NUMBER && !Object(is["l" /* isConstantNode */])(last) && (!Object(is["B" /* isOperatorNode */])(last) || last.op === '!') || state.token === '(') { - // parse implicit multiplication - // - // symbol: implicit multiplication like '2a', '(2+3)a', 'a b' - // number: implicit multiplication like '(2+3)2' - // parenthesis: implicit multiplication like '2(3+4)', '(3+4)(1+2)' - last = parseRule2(state); - node = new OperatorNode('*', 'multiply', [node, last], true - /* implicit */ - ); - } else { - break; - } - } - - return node; - } - /** - * Infamous "rule 2" as described in https://github.com/josdejong/mathjs/issues/792#issuecomment-361065370 - * Explicit division gets higher precedence than implicit multiplication - * when the division matches this pattern: [number] / [number] [symbol] - * @return {Node} node - * @private - */ - - function parseRule2(state) { - var node = parseUnary(state); - var last = node; - var tokenStates = []; - - while (true) { - // Match the "number /" part of the pattern "number / number symbol" - if (state.token === '/' && Object(is["l" /* isConstantNode */])(last)) { - // Look ahead to see if the next token is a number - tokenStates.push(parse_extends({}, state)); - getTokenSkipNewline(state); // Match the "number / number" part of the pattern - - if (state.tokenType === TOKENTYPE.NUMBER) { - // Look ahead again - tokenStates.push(parse_extends({}, state)); - getTokenSkipNewline(state); // Match the "symbol" part of the pattern, or a left parenthesis - - if (state.tokenType === TOKENTYPE.SYMBOL || state.token === '(') { - // We've matched the pattern "number / number symbol". - // Rewind once and build the "number / number" node; the symbol will be consumed later - parse_extends(state, tokenStates.pop()); - - tokenStates.pop(); - last = parseUnary(state); - node = new OperatorNode('/', 'divide', [node, last]); - } else { - // Not a match, so rewind - tokenStates.pop(); - - parse_extends(state, tokenStates.pop()); - - break; - } - } else { - // Not a match, so rewind - parse_extends(state, tokenStates.pop()); - - break; - } - } else { - break; - } - } - - return node; - } - /** - * Unary plus and minus, and logical and bitwise not - * @return {Node} node - * @private - */ - - function parseUnary(state) { - var name, params, fn; - var operators = { - '-': 'unaryMinus', - '+': 'unaryPlus', - '~': 'bitNot', - not: 'not' - }; - - if (Object(utils_object["f" /* hasOwnProperty */])(operators, state.token)) { - fn = operators[state.token]; - name = state.token; - getTokenSkipNewline(state); - params = [parseUnary(state)]; - return new OperatorNode(name, fn, params); - } - - return parsePow(state); - } - /** - * power - * Note: power operator is right associative - * @return {Node} node - * @private - */ - - function parsePow(state) { - var node, name, fn, params; - node = parseLeftHandOperators(state); - - if (state.token === '^' || state.token === '.^') { - name = state.token; - fn = name === '^' ? 'pow' : 'dotPow'; - getTokenSkipNewline(state); - params = [node, parseUnary(state)]; // Go back to unary, we can have '2^-3' - - node = new OperatorNode(name, fn, params); - } - - return node; - } - /** - * Left hand operators: factorial x!, ctranspose x' - * @return {Node} node - * @private - */ - - function parseLeftHandOperators(state) { - var node, name, fn, params; - node = parseCustomNodes(state); - var operators = { - '!': 'factorial', - '\'': 'ctranspose' - }; - - while (Object(utils_object["f" /* hasOwnProperty */])(operators, state.token)) { - name = state.token; - fn = operators[name]; - getToken(state); - params = [node]; - node = new OperatorNode(name, fn, params); - node = parseAccessors(state, node); - } - - return node; - } - /** - * Parse a custom node handler. A node handler can be used to process - * nodes in a custom way, for example for handling a plot. - * - * A handler must be passed as second argument of the parse function. - * - must extend math.Node - * - must contain a function _compile(defs: Object) : string - * - must contain a function find(filter: Object) : Node[] - * - must contain a function toString() : string - * - the constructor is called with a single argument containing all parameters - * - * For example: - * - * nodes = { - * 'plot': PlotHandler - * } - * - * The constructor of the handler is called as: - * - * node = new PlotHandler(params) - * - * The handler will be invoked when evaluating an expression like: - * - * node = math.parse('plot(sin(x), x)', nodes) - * - * @return {Node} node - * @private - */ - - function parseCustomNodes(state) { - var params = []; - - if (state.tokenType === TOKENTYPE.SYMBOL && Object(utils_object["f" /* hasOwnProperty */])(state.extraNodes, state.token)) { - var CustomNode = state.extraNodes[state.token]; - getToken(state); // parse parameters - - if (state.token === '(') { - params = []; - openParams(state); - getToken(state); - - if (state.token !== ')') { - params.push(parseAssignment(state)); // parse a list with parameters - - while (state.token === ',') { - // eslint-disable-line no-unmodified-loop-condition - getToken(state); - params.push(parseAssignment(state)); - } - } - - if (state.token !== ')') { - throw createSyntaxError(state, 'Parenthesis ) expected'); - } - - closeParams(state); - getToken(state); - } // create a new custom node - // noinspection JSValidateTypes - - return new CustomNode(params); - } - - return parseSymbol(state); - } - /** - * parse symbols: functions, variables, constants, units - * @return {Node} node - * @private - */ - - function parseSymbol(state) { - var node, name; - - if (state.tokenType === TOKENTYPE.SYMBOL || state.tokenType === TOKENTYPE.DELIMITER && state.token in NAMED_DELIMITERS) { - name = state.token; - getToken(state); - - if (Object(utils_object["f" /* hasOwnProperty */])(CONSTANTS, name)) { - // true, false, null, ... - node = new ConstantNode(CONSTANTS[name]); - } else if (NUMERIC_CONSTANTS.indexOf(name) !== -1) { - // NaN, Infinity - node = new ConstantNode(numeric(name, 'number')); - } else { - node = new SymbolNode(name); - } // parse function parameters and matrix index - - node = parseAccessors(state, node); - return node; - } - - return parseDoubleQuotesString(state); - } - /** - * parse accessors: - * - function invocation in round brackets (...), for example sqrt(2) - * - index enclosed in square brackets [...], for example A[2,3] - * - dot notation for properties, like foo.bar - * @param {Object} state - * @param {Node} node Node on which to apply the parameters. If there - * are no parameters in the expression, the node - * itself is returned - * @param {string[]} [types] Filter the types of notations - * can be ['(', '[', '.'] - * @return {Node} node - * @private - */ - - function parseAccessors(state, node, types) { - var params; - - while ((state.token === '(' || state.token === '[' || state.token === '.') && (!types || types.indexOf(state.token) !== -1)) { - // eslint-disable-line no-unmodified-loop-condition - params = []; - - if (state.token === '(') { - if (Object(is["J" /* isSymbolNode */])(node) || Object(is["a" /* isAccessorNode */])(node)) { - // function invocation like fn(2, 3) or obj.fn(2, 3) - openParams(state); - getToken(state); - - if (state.token !== ')') { - params.push(parseAssignment(state)); // parse a list with parameters - - while (state.token === ',') { - // eslint-disable-line no-unmodified-loop-condition - getToken(state); - params.push(parseAssignment(state)); - } - } - - if (state.token !== ')') { - throw createSyntaxError(state, 'Parenthesis ) expected'); - } - - closeParams(state); - getToken(state); - node = new FunctionNode(node, params); - } else { - // implicit multiplication like (2+3)(4+5) or sqrt(2)(1+2) - // don't parse it here but let it be handled by parseImplicitMultiplication - // with correct precedence - return node; - } - } else if (state.token === '[') { - // index notation like variable[2, 3] - openParams(state); - getToken(state); - - if (state.token !== ']') { - params.push(parseAssignment(state)); // parse a list with parameters - - while (state.token === ',') { - // eslint-disable-line no-unmodified-loop-condition - getToken(state); - params.push(parseAssignment(state)); - } - } - - if (state.token !== ']') { - throw createSyntaxError(state, 'Parenthesis ] expected'); - } - - closeParams(state); - getToken(state); - node = new AccessorNode(node, new IndexNode(params)); - } else { - // dot notation like variable.prop - getToken(state); - - if (state.tokenType !== TOKENTYPE.SYMBOL) { - throw createSyntaxError(state, 'Property name expected after dot'); - } - - params.push(new ConstantNode(state.token)); - getToken(state); - var dotNotation = true; - node = new AccessorNode(node, new IndexNode(params, dotNotation)); - } - } - - return node; - } - /** - * Parse a double quotes string. - * @return {Node} node - * @private - */ - - function parseDoubleQuotesString(state) { - var node, str; - - if (state.token === '"') { - str = parseDoubleQuotesStringToken(state); // create constant - - node = new ConstantNode(str); // parse index parameters - - node = parseAccessors(state, node); - return node; - } - - return parseSingleQuotesString(state); - } - /** - * Parse a string surrounded by double quotes "..." - * @return {string} - */ - - function parseDoubleQuotesStringToken(state) { - var str = ''; - - while (currentCharacter(state) !== '' && currentCharacter(state) !== '"') { - if (currentCharacter(state) === '\\') { - // escape character, immediately process the next - // character to prevent stopping at a next '\"' - str += currentCharacter(state); - next(state); - } - - str += currentCharacter(state); - next(state); - } - - getToken(state); - - if (state.token !== '"') { - throw createSyntaxError(state, 'End of string " expected'); - } - - getToken(state); - return JSON.parse('"' + str + '"'); // unescape escaped characters - } - /** - * Parse a single quotes string. - * @return {Node} node - * @private - */ - - function parseSingleQuotesString(state) { - var node, str; - - if (state.token === '\'') { - str = parseSingleQuotesStringToken(state); // create constant - - node = new ConstantNode(str); // parse index parameters - - node = parseAccessors(state, node); - return node; - } - - return parseMatrix(state); - } - /** - * Parse a string surrounded by single quotes '...' - * @return {string} - */ - - function parseSingleQuotesStringToken(state) { - var str = ''; - - while (currentCharacter(state) !== '' && currentCharacter(state) !== '\'') { - if (currentCharacter(state) === '\\') { - // escape character, immediately process the next - // character to prevent stopping at a next '\'' - str += currentCharacter(state); - next(state); - } - - str += currentCharacter(state); - next(state); - } - - getToken(state); - - if (state.token !== '\'') { - throw createSyntaxError(state, 'End of string \' expected'); - } - - getToken(state); - return JSON.parse('"' + str + '"'); // unescape escaped characters - } - /** - * parse the matrix - * @return {Node} node - * @private - */ - - function parseMatrix(state) { - var array, params, rows, cols; - - if (state.token === '[') { - // matrix [...] - openParams(state); - getToken(state); - - if (state.token !== ']') { - // this is a non-empty matrix - var row = parseRow(state); - - if (state.token === ';') { - // 2 dimensional array - rows = 1; - params = [row]; // the rows of the matrix are separated by dot-comma's - - while (state.token === ';') { - // eslint-disable-line no-unmodified-loop-condition - getToken(state); - params[rows] = parseRow(state); - rows++; - } - - if (state.token !== ']') { - throw createSyntaxError(state, 'End of matrix ] expected'); - } - - closeParams(state); - getToken(state); // check if the number of columns matches in all rows - - cols = params[0].items.length; - - for (var r = 1; r < rows; r++) { - if (params[r].items.length !== cols) { - throw createError(state, 'Column dimensions mismatch ' + '(' + params[r].items.length + ' !== ' + cols + ')'); - } - } - - array = new ArrayNode(params); - } else { - // 1 dimensional vector - if (state.token !== ']') { - throw createSyntaxError(state, 'End of matrix ] expected'); - } - - closeParams(state); - getToken(state); - array = row; - } - } else { - // this is an empty matrix "[ ]" - closeParams(state); - getToken(state); - array = new ArrayNode([]); - } - - return parseAccessors(state, array); - } - - return parseObject(state); - } - /** - * Parse a single comma-separated row from a matrix, like 'a, b, c' - * @return {ArrayNode} node - */ - - function parseRow(state) { - var params = [parseAssignment(state)]; - var len = 1; - - while (state.token === ',') { - // eslint-disable-line no-unmodified-loop-condition - getToken(state); // parse expression - - params[len] = parseAssignment(state); - len++; - } - - return new ArrayNode(params); - } - /** - * parse an object, enclosed in angle brackets{...}, for example {value: 2} - * @return {Node} node - * @private - */ - - function parseObject(state) { - if (state.token === '{') { - openParams(state); - var key; - var properties = {}; - - do { - getToken(state); - - if (state.token !== '}') { - // parse key - if (state.token === '"') { - key = parseDoubleQuotesStringToken(state); - } else if (state.token === '\'') { - key = parseSingleQuotesStringToken(state); - } else if (state.tokenType === TOKENTYPE.SYMBOL || state.tokenType === TOKENTYPE.DELIMITER && state.token in NAMED_DELIMITERS) { - key = state.token; - getToken(state); - } else { - throw createSyntaxError(state, 'Symbol or string expected as object key'); - } // parse key/value separator - - if (state.token !== ':') { - throw createSyntaxError(state, 'Colon : expected after object key'); - } - - getToken(state); // parse key - - properties[key] = parseAssignment(state); - } - } while (state.token === ','); // eslint-disable-line no-unmodified-loop-condition - - if (state.token !== '}') { - throw createSyntaxError(state, 'Comma , or bracket } expected after object value'); - } - - closeParams(state); - getToken(state); - var node = new ObjectNode(properties); // parse index parameters - - node = parseAccessors(state, node); - return node; - } - - return parseNumber(state); - } - /** - * parse a number - * @return {Node} node - * @private - */ - - function parseNumber(state) { - var numberStr; - - if (state.tokenType === TOKENTYPE.NUMBER) { - // this is a number - numberStr = state.token; - getToken(state); - return new ConstantNode(numeric(numberStr, config.number)); - } - - return parseParentheses(state); - } - /** - * parentheses - * @return {Node} node - * @private - */ - - function parseParentheses(state) { - var node; // check if it is a parenthesized expression - - if (state.token === '(') { - // parentheses (...) - openParams(state); - getToken(state); - node = parseAssignment(state); // start again - - if (state.token !== ')') { - throw createSyntaxError(state, 'Parenthesis ) expected'); - } - - closeParams(state); - getToken(state); - node = new ParenthesisNode(node); - node = parseAccessors(state, node); - return node; - } - - return parseEnd(state); - } - /** - * Evaluated when the expression is not yet ended but expected to end - * @return {Node} res - * @private - */ - - function parseEnd(state) { - if (state.token === '') { - // syntax error or unexpected end of expression - throw createSyntaxError(state, 'Unexpected end of expression'); - } else { - throw createSyntaxError(state, 'Value expected'); - } - } - /** - * Shortcut for getting the current row value (one based) - * Returns the line of the currently handled expression - * @private - */ - - /* TODO: implement keeping track on the row number - function row () { - return null - } - */ - - /** - * Shortcut for getting the current col value (one based) - * Returns the column (position) where the last state.token starts - * @private - */ - - function col(state) { - return state.index - state.token.length + 1; - } - /** - * Create an error - * @param {Object} state - * @param {string} message - * @return {SyntaxError} instantiated error - * @private - */ - - function createSyntaxError(state, message) { - var c = col(state); - var error = new SyntaxError(message + ' (char ' + c + ')'); - error["char"] = c; - return error; - } - /** - * Create an error - * @param {Object} state - * @param {string} message - * @return {Error} instantiated error - * @private - */ - - function createError(state, message) { - var c = col(state); - var error = new SyntaxError(message + ' (char ' + c + ')'); - error["char"] = c; - return error; - } - - return parse; - }); - // CONCATENATED MODULE: ./src/expression/function/compile.js - - var compile_name = 'compile'; - var compile_dependencies = ['typed', 'parse']; - var createCompile = /* #__PURE__ */Object(factory["a" /* factory */])(compile_name, compile_dependencies, function (_ref) { - var typed = _ref.typed, - parse = _ref.parse; - - /** - * Parse and compile an expression. - * Returns a an object with a function `evaluate([scope])` to evaluate the - * compiled expression. - * - * Syntax: - * - * math.compile(expr) // returns one node - * math.compile([expr1, expr2, expr3, ...]) // returns an array with nodes - * - * Examples: - * - * const code1 = math.compile('sqrt(3^2 + 4^2)') - * code1.evaluate() // 5 - * - * let scope = {a: 3, b: 4} - * const code2 = math.compile('a * b') // 12 - * code2.evaluate(scope) // 12 - * scope.a = 5 - * code2.evaluate(scope) // 20 - * - * const nodes = math.compile(['a = 3', 'b = 4', 'a * b']) - * nodes[2].evaluate() // 12 - * - * See also: - * - * parse, evaluate - * - * @param {string | string[] | Array | Matrix} expr - * The expression to be compiled - * @return {{evaluate: Function} | Array.<{evaluate: Function}>} code - * An object with the compiled expression - * @throws {Error} - */ - return typed(compile_name, { - string: function string(expr) { - return parse(expr).compile(); - }, - 'Array | Matrix': function ArrayMatrix(expr) { - return deepMap(expr, function (entry) { - return parse(entry).compile(); - }); - } - }); - }); - // CONCATENATED MODULE: ./src/expression/function/evaluate.js - - var evaluate_name = 'evaluate'; - var evaluate_dependencies = ['typed', 'parse']; - var createEvaluate = /* #__PURE__ */Object(factory["a" /* factory */])(evaluate_name, evaluate_dependencies, function (_ref) { - var typed = _ref.typed, - parse = _ref.parse; - - /** - * Evaluate an expression. - * - * Note the evaluating arbitrary expressions may involve security risks, - * see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information. - * - * Syntax: - * - * math.evaluate(expr) - * math.evaluate(expr, scope) - * math.evaluate([expr1, expr2, expr3, ...]) - * math.evaluate([expr1, expr2, expr3, ...], scope) - * - * Example: - * - * math.evaluate('(2+3)/4') // 1.25 - * math.evaluate('sqrt(3^2 + 4^2)') // 5 - * math.evaluate('sqrt(-4)') // 2i - * math.evaluate(['a=3', 'b=4', 'a*b']) // [3, 4, 12] - * - * let scope = {a:3, b:4} - * math.evaluate('a * b', scope) // 12 - * - * See also: - * - * parse, compile - * - * @param {string | string[] | Matrix} expr The expression to be evaluated - * @param {Object} [scope] Scope to read/write variables - * @return {*} The result of the expression - * @throws {Error} - */ - return typed(evaluate_name, { - string: function string(expr) { - var scope = {}; - return parse(expr).compile().evaluate(scope); - }, - 'string, Object': function stringObject(expr, scope) { - return parse(expr).compile().evaluate(scope); - }, - 'Array | Matrix': function ArrayMatrix(expr) { - var scope = {}; - return deepMap(expr, function (entry) { - return parse(entry).compile().evaluate(scope); - }); - }, - 'Array | Matrix, Object': function ArrayMatrixObject(expr, scope) { - return deepMap(expr, function (entry) { - return parse(entry).compile().evaluate(scope); - }); - } - }); - }); - // CONCATENATED MODULE: ./src/expression/Parser.js - - var Parser_name = 'Parser'; - var Parser_dependencies = ['parse']; - var createParserClass = /* #__PURE__ */Object(factory["a" /* factory */])(Parser_name, Parser_dependencies, function (_ref) { - var parse = _ref.parse; - - /** - * @constructor Parser - * Parser contains methods to evaluate or parse expressions, and has a number - * of convenience methods to get, set, and remove variables from memory. Parser - * keeps a scope containing variables in memory, which is used for all - * evaluations. - * - * Methods: - * const result = parser.evaluate(expr) // evaluate an expression - * const value = parser.get(name) // retrieve a variable from the parser - * const values = parser.getAll() // retrieve all defined variables - * parser.set(name, value) // set a variable in the parser - * parser.remove(name) // clear a variable from the - * // parsers scope - * parser.clear() // clear the parsers scope - * - * Example usage: - * const parser = new Parser() - * // Note: there is a convenience method which can be used instead: - * // const parser = new math.parser() - * - * // evaluate expressions - * parser.evaluate('sqrt(3^2 + 4^2)') // 5 - * parser.evaluate('sqrt(-4)') // 2i - * parser.evaluate('2 inch in cm') // 5.08 cm - * parser.evaluate('cos(45 deg)') // 0.7071067811865476 - * - * // define variables and functions - * parser.evaluate('x = 7 / 2') // 3.5 - * parser.evaluate('x + 3') // 6.5 - * parser.evaluate('function f(x, y) = x^y') // f(x, y) - * parser.evaluate('f(2, 3)') // 8 - * - * // get and set variables and functions - * const x = parser.get('x') // 7 - * const f = parser.get('f') // function - * const g = f(3, 2) // 9 - * parser.set('h', 500) - * const i = parser.evaluate('h / 2') // 250 - * parser.set('hello', function (name) { - * return 'hello, ' + name + '!' - * }) - * parser.evaluate('hello("user")') // "hello, user!" - * - * // clear defined functions and variables - * parser.clear() - * - */ - function Parser() { - if (!(this instanceof Parser)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - this.scope = {}; - } - /** - * Attach type information - */ - - Parser.prototype.type = 'Parser'; - Parser.prototype.isParser = true; - /** - * Parse and evaluate the given expression - * @param {string} expr A string containing an expression, for example "2+3" - * @return {*} result The result, or undefined when the expression was empty - * @throws {Error} - */ - - Parser.prototype.evaluate = function (expr) { - // TODO: validate arguments - return parse(expr).compile().evaluate(this.scope); - }; - /** - * Get a variable (a function or variable) by name from the parsers scope. - * Returns undefined when not found - * @param {string} name - * @return {* | undefined} value - */ - - Parser.prototype.get = function (name) { - // TODO: validate arguments - return name in this.scope ? getSafeProperty(this.scope, name) : undefined; - }; - /** - * Get a map with all defined variables - * @return {Object} values - */ - - Parser.prototype.getAll = function () { - return Object(utils_object["e" /* extend */])({}, this.scope); - }; - /** - * Set a symbol (a function or variable) by name from the parsers scope. - * @param {string} name - * @param {* | undefined} value - */ - - Parser.prototype.set = function (name, value) { - // TODO: validate arguments - return setSafeProperty(this.scope, name, value); - }; - /** - * Remove a variable from the parsers scope - * @param {string} name - */ - - Parser.prototype.remove = function (name) { - // TODO: validate arguments - delete this.scope[name]; - }; - /** - * Clear the scope with variables and functions - */ - - Parser.prototype.clear = function () { - for (var _name in this.scope) { - if (Object(utils_object["f" /* hasOwnProperty */])(this.scope, _name)) { - delete this.scope[_name]; - } - } - }; - - return Parser; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/expression/function/parser.js - - var parser_name = 'parser'; - var parser_dependencies = ['typed', 'Parser']; - var createParser = /* #__PURE__ */Object(factory["a" /* factory */])(parser_name, parser_dependencies, function (_ref) { - var typed = _ref.typed, - Parser = _ref.Parser; - - /** - * Create a parser. The function creates a new `math.Parser` object. - * - * Syntax: - * - * math.parser() - * - * Examples: - * - * const parser = new math.parser() - * - * // evaluate expressions - * const a = parser.evaluate('sqrt(3^2 + 4^2)') // 5 - * const b = parser.evaluate('sqrt(-4)') // 2i - * const c = parser.evaluate('2 inch in cm') // 5.08 cm - * const d = parser.evaluate('cos(45 deg)') // 0.7071067811865476 - * - * // define variables and functions - * parser.evaluate('x = 7 / 2') // 3.5 - * parser.evaluate('x + 3') // 6.5 - * parser.evaluate('function f(x, y) = x^y') // f(x, y) - * parser.evaluate('f(2, 3)') // 8 - * - * // get and set variables and functions - * const x = parser.get('x') // 7 - * const f = parser.get('f') // function - * const g = f(3, 2) // 9 - * parser.set('h', 500) - * const i = parser.evaluate('h / 2') // 250 - * parser.set('hello', function (name) { - * return 'hello, ' + name + '!' - * }) - * parser.evaluate('hello("user")') // "hello, user!" - * - * // clear defined functions and variables - * parser.clear() - * - * See also: - * - * evaluate, compile, parse - * - * @return {Parser} Parser - */ - return typed(parser_name, { - '': function _() { - return new Parser(); - } - }); - }); - // CONCATENATED MODULE: ./src/function/algebra/decomposition/lup.js - - var lup_name = 'lup'; - var lup_dependencies = ['typed', 'matrix', 'abs', 'addScalar', 'divideScalar', 'multiplyScalar', 'subtract', 'larger', 'equalScalar', 'unaryMinus', 'DenseMatrix', 'SparseMatrix', 'Spa']; - var createLup = /* #__PURE__ */Object(factory["a" /* factory */])(lup_name, lup_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - abs = _ref.abs, - addScalar = _ref.addScalar, - divideScalar = _ref.divideScalar, - multiplyScalar = _ref.multiplyScalar, - subtract = _ref.subtract, - larger = _ref.larger, - equalScalar = _ref.equalScalar, - unaryMinus = _ref.unaryMinus, - DenseMatrix = _ref.DenseMatrix, - SparseMatrix = _ref.SparseMatrix, - Spa = _ref.Spa; - - /** - * Calculate the Matrix LU decomposition with partial pivoting. Matrix `A` is decomposed in two matrices (`L`, `U`) and a - * row permutation vector `p` where `A[p,:] = L * U` - * - * Syntax: - * - * math.lup(A) - * - * Example: - * - * const m = [[2, 1], [1, 4]] - * const r = math.lup(m) - * // r = { - * // L: [[1, 0], [0.5, 1]], - * // U: [[2, 1], [0, 3.5]], - * // P: [0, 1] - * // } - * - * See also: - * - * slu, lsolve, lusolve, usolve - * - * @param {Matrix | Array} A A two dimensional matrix or array for which to get the LUP decomposition. - * - * @return {{L: Array | Matrix, U: Array | Matrix, P: Array.}} The lower triangular matrix, the upper triangular matrix and the permutation matrix. - */ - return typed(lup_name, { - DenseMatrix: function DenseMatrix(m) { - return _denseLUP(m); - }, - SparseMatrix: function SparseMatrix(m) { - return _sparseLUP(m); - }, - Array: function Array(a) { - // create dense matrix from array - var m = matrix(a); // lup, use matrix implementation - - var r = _denseLUP(m); // result - - return { - L: r.L.valueOf(), - U: r.U.valueOf(), - p: r.p - }; - } - }); - - function _denseLUP(m) { - // rows & columns - var rows = m._size[0]; - var columns = m._size[1]; // minimum rows and columns - - var n = Math.min(rows, columns); // matrix array, clone original data - - var data = Object(utils_object["a" /* clone */])(m._data); // l matrix arrays - - var ldata = []; - var lsize = [rows, n]; // u matrix arrays - - var udata = []; - var usize = [n, columns]; // vars - - var i, j, k; // permutation vector - - var p = []; - - for (i = 0; i < rows; i++) { - p[i] = i; - } // loop columns - - for (j = 0; j < columns; j++) { - // skip first column in upper triangular matrix - if (j > 0) { - // loop rows - for (i = 0; i < rows; i++) { - // min i,j - var min = Math.min(i, j); // v[i, j] - - var s = 0; // loop up to min - - for (k = 0; k < min; k++) { - // s = l[i, k] - data[k, j] - s = addScalar(s, multiplyScalar(data[i][k], data[k][j])); - } - - data[i][j] = subtract(data[i][j], s); - } - } // row with larger value in cvector, row >= j - - var pi = j; - var pabsv = 0; - var vjj = 0; // loop rows - - for (i = j; i < rows; i++) { - // data @ i, j - var v = data[i][j]; // absolute value - - var absv = abs(v); // value is greater than pivote value - - if (larger(absv, pabsv)) { - // store row - pi = i; // update max value - - pabsv = absv; // value @ [j, j] - - vjj = v; - } - } // swap rows (j <-> pi) - - if (j !== pi) { - // swap values j <-> pi in p - p[j] = [p[pi], p[pi] = p[j]][0]; // swap j <-> pi in data - - DenseMatrix._swapRows(j, pi, data); - } // check column is in lower triangular matrix - - if (j < rows) { - // loop rows (lower triangular matrix) - for (i = j + 1; i < rows; i++) { - // value @ i, j - var vij = data[i][j]; - - if (!equalScalar(vij, 0)) { - // update data - data[i][j] = divideScalar(data[i][j], vjj); - } - } - } - } // loop columns - - for (j = 0; j < columns; j++) { - // loop rows - for (i = 0; i < rows; i++) { - // initialize row in arrays - if (j === 0) { - // check row exists in upper triangular matrix - if (i < columns) { - // U - udata[i] = []; - } // L - - ldata[i] = []; - } // check we are in the upper triangular matrix - - if (i < j) { - // check row exists in upper triangular matrix - if (i < columns) { - // U - udata[i][j] = data[i][j]; - } // check column exists in lower triangular matrix - - if (j < rows) { - // L - ldata[i][j] = 0; - } - - continue; - } // diagonal value - - if (i === j) { - // check row exists in upper triangular matrix - if (i < columns) { - // U - udata[i][j] = data[i][j]; - } // check column exists in lower triangular matrix - - if (j < rows) { - // L - ldata[i][j] = 1; - } - - continue; - } // check row exists in upper triangular matrix - - if (i < columns) { - // U - udata[i][j] = 0; - } // check column exists in lower triangular matrix - - if (j < rows) { - // L - ldata[i][j] = data[i][j]; - } - } - } // l matrix - - var l = new DenseMatrix({ - data: ldata, - size: lsize - }); // u matrix - - var u = new DenseMatrix({ - data: udata, - size: usize - }); // p vector - - var pv = []; - - for (i = 0, n = p.length; i < n; i++) { - pv[p[i]] = i; - } // return matrices - - return { - L: l, - U: u, - p: pv, - toString: function toString() { - return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\nP: ' + this.p; - } - }; - } - - function _sparseLUP(m) { - // rows & columns - var rows = m._size[0]; - var columns = m._size[1]; // minimum rows and columns - - var n = Math.min(rows, columns); // matrix arrays (will not be modified, thanks to permutation vector) - - var values = m._values; - var index = m._index; - var ptr = m._ptr; // l matrix arrays - - var lvalues = []; - var lindex = []; - var lptr = []; - var lsize = [rows, n]; // u matrix arrays - - var uvalues = []; - var uindex = []; - var uptr = []; - var usize = [n, columns]; // vars - - var i, j, k; // permutation vectors, (current index -> original index) and (original index -> current index) - - var pvCo = []; - var pvOc = []; - - for (i = 0; i < rows; i++) { - pvCo[i] = i; - pvOc[i] = i; - } // swap indices in permutation vectors (condition x < y)! - - var swapIndeces = function swapIndeces(x, y) { - // find pv indeces getting data from x and y - var kx = pvOc[x]; - var ky = pvOc[y]; // update permutation vector current -> original - - pvCo[kx] = y; - pvCo[ky] = x; // update permutation vector original -> current - - pvOc[x] = ky; - pvOc[y] = kx; - }; // loop columns - - var _loop = function _loop() { - // sparse accumulator - var spa = new Spa(); // check lower triangular matrix has a value @ column j - - if (j < rows) { - // update ptr - lptr.push(lvalues.length); // first value in j column for lower triangular matrix - - lvalues.push(1); - lindex.push(j); - } // update ptr - - uptr.push(uvalues.length); // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] - - var k0 = ptr[j]; - var k1 = ptr[j + 1]; // copy column j into sparse accumulator - - for (k = k0; k < k1; k++) { - // row - i = index[k]; // copy column values into sparse accumulator (use permutation vector) - - spa.set(pvCo[i], values[k]); - } // skip first column in upper triangular matrix - - if (j > 0) { - // loop rows in column j (above diagonal) - spa.forEach(0, j - 1, function (k, vkj) { - // loop rows in column k (L) - SparseMatrix._forEachRow(k, lvalues, lindex, lptr, function (i, vik) { - // check row is below k - if (i > k) { - // update spa value - spa.accumulate(i, unaryMinus(multiplyScalar(vik, vkj))); - } - }); - }); - } // row with larger value in spa, row >= j - - var pi = j; - var vjj = spa.get(j); - var pabsv = abs(vjj); // loop values in spa (order by row, below diagonal) - - spa.forEach(j + 1, rows - 1, function (x, v) { - // absolute value - var absv = abs(v); // value is greater than pivote value - - if (larger(absv, pabsv)) { - // store row - pi = x; // update max value - - pabsv = absv; // value @ [j, j] - - vjj = v; - } - }); // swap rows (j <-> pi) - - if (j !== pi) { - // swap values j <-> pi in L - SparseMatrix._swapRows(j, pi, lsize[1], lvalues, lindex, lptr); // swap values j <-> pi in U - - SparseMatrix._swapRows(j, pi, usize[1], uvalues, uindex, uptr); // swap values in spa - - spa.swap(j, pi); // update permutation vector (swap values @ j, pi) - - swapIndeces(j, pi); - } // loop values in spa (order by row) - - spa.forEach(0, rows - 1, function (x, v) { - // check we are above diagonal - if (x <= j) { - // update upper triangular matrix - uvalues.push(v); - uindex.push(x); - } else { - // update value - v = divideScalar(v, vjj); // check value is non zero - - if (!equalScalar(v, 0)) { - // update lower triangular matrix - lvalues.push(v); - lindex.push(x); - } - } - }); - }; - - for (j = 0; j < columns; j++) { - _loop(); - } // update ptrs - - uptr.push(uvalues.length); - lptr.push(lvalues.length); // return matrices - - return { - L: new SparseMatrix({ - values: lvalues, - index: lindex, - ptr: lptr, - size: lsize - }), - U: new SparseMatrix({ - values: uvalues, - index: uindex, - ptr: uptr, - size: usize - }), - p: pvCo, - toString: function toString() { - return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\nP: ' + this.p; - } - }; - } - }); - // CONCATENATED MODULE: ./src/function/algebra/decomposition/qr.js - function qr_extends() { - qr_extends = Object.assign || function (target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i]; for (var key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } return target; - }; return qr_extends.apply(this, arguments); - } - - var qr_name = 'qr'; - var qr_dependencies = ['typed', 'matrix', 'zeros', 'identity', 'isZero', 'equal', 'sign', 'sqrt', 'conj', 'unaryMinus', 'addScalar', 'divideScalar', 'multiplyScalar', 'subtract', 'complex']; - var createQr = /* #__PURE__ */Object(factory["a" /* factory */])(qr_name, qr_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - zeros = _ref.zeros, - identity = _ref.identity, - isZero = _ref.isZero, - equal = _ref.equal, - sign = _ref.sign, - sqrt = _ref.sqrt, - conj = _ref.conj, - unaryMinus = _ref.unaryMinus, - addScalar = _ref.addScalar, - divideScalar = _ref.divideScalar, - multiplyScalar = _ref.multiplyScalar, - subtract = _ref.subtract, - complex = _ref.complex; - - /** - * Calculate the Matrix QR decomposition. Matrix `A` is decomposed in - * two matrices (`Q`, `R`) where `Q` is an - * orthogonal matrix and `R` is an upper triangular matrix. - * - * Syntax: - * - * math.qr(A) - * - * Example: - * - * const m = [ - * [1, -1, 4], - * [1, 4, -2], - * [1, 4, 2], - * [1, -1, 0] - * ] - * const result = math.qr(m) - * // r = { - * // Q: [ - * // [0.5, -0.5, 0.5], - * // [0.5, 0.5, -0.5], - * // [0.5, 0.5, 0.5], - * // [0.5, -0.5, -0.5], - * // ], - * // R: [ - * // [2, 3, 2], - * // [0, 5, -2], - * // [0, 0, 4], - * // [0, 0, 0] - * // ] - * // } - * - * See also: - * - * lup, lusolve - * - * @param {Matrix | Array} A A two dimensional matrix or array - * for which to get the QR decomposition. - * - * @return {{Q: Array | Matrix, R: Array | Matrix}} Q: the orthogonal - * matrix and R: the upper triangular matrix - */ - return qr_extends(typed(qr_name, { - DenseMatrix: function DenseMatrix(m) { - return _denseQR(m); - }, - SparseMatrix: function SparseMatrix(m) { - return _sparseQR(m); - }, - Array: function Array(a) { - // create dense matrix from array - var m = matrix(a); // lup, use matrix implementation - - var r = _denseQR(m); // result - - return { - Q: r.Q.valueOf(), - R: r.R.valueOf() - }; - } - }), { - _denseQRimpl: _denseQRimpl - }); - - function _denseQRimpl(m) { - // rows & columns (m x n) - var rows = m._size[0]; // m - - var cols = m._size[1]; // n - - var Q = identity([rows], 'dense'); - var Qdata = Q._data; - var R = m.clone(); - var Rdata = R._data; // vars - - var i, j, k; - var w = zeros([rows], ''); - - for (k = 0; k < Math.min(cols, rows); ++k) { - /* - * **k-th Household matrix** - * - * The matrix I - 2*v*transpose(v) - * x = first column of A - * x1 = first element of x - * alpha = x1 / |x1| * |x| - * e1 = tranpose([1, 0, 0, ...]) - * u = x - alpha * e1 - * v = u / |u| - * - * Household matrix = I - 2 * v * tranpose(v) - * - * * Initially Q = I and R = A. - * * Household matrix is a reflection in a plane normal to v which - * will zero out all but the top right element in R. - * * Appplying reflection to both Q and R will not change product. - * * Repeat this process on the (1,1) minor to get R as an upper - * triangular matrix. - * * Reflections leave the magnitude of the columns of Q unchanged - * so Q remains othoganal. - * - */ - var pivot = Rdata[k][k]; - var sgn = unaryMinus(equal(pivot, 0) ? 1 : sign(pivot)); - var conjSgn = conj(sgn); - var alphaSquared = 0; - - for (i = k; i < rows; i++) { - alphaSquared = addScalar(alphaSquared, multiplyScalar(Rdata[i][k], conj(Rdata[i][k]))); - } - - var alpha = multiplyScalar(sgn, sqrt(alphaSquared)); - - if (!isZero(alpha)) { - // first element in vector u - var u1 = subtract(pivot, alpha); // w = v * u1 / |u| (only elements k to (rows-1) are used) - - w[k] = 1; - - for (i = k + 1; i < rows; i++) { - w[i] = divideScalar(Rdata[i][k], u1); - } // tau = - conj(u1 / alpha) - - var tau = unaryMinus(conj(divideScalar(u1, alpha))); - var s = void 0; - /* - * tau and w have been choosen so that - * - * 2 * v * tranpose(v) = tau * w * tranpose(w) - */ - - /* - * -- calculate R = R - tau * w * tranpose(w) * R -- - * Only do calculation with rows k to (rows-1) - * Additionally columns 0 to (k-1) will not be changed by this - * multiplication so do not bother recalculating them - */ - - for (j = k; j < cols; j++) { - s = 0.0; // calculate jth element of [tranpose(w) * R] - - for (i = k; i < rows; i++) { - s = addScalar(s, multiplyScalar(conj(w[i]), Rdata[i][j])); - } // calculate the jth element of [tau * transpose(w) * R] - - s = multiplyScalar(s, tau); - - for (i = k; i < rows; i++) { - Rdata[i][j] = multiplyScalar(subtract(Rdata[i][j], multiplyScalar(w[i], s)), conjSgn); - } - } - /* - * -- calculate Q = Q - tau * Q * w * transpose(w) -- - * Q is a square matrix (rows x rows) - * Only do calculation with columns k to (rows-1) - * Additionally rows 0 to (k-1) will not be changed by this - * multiplication so do not bother recalculating them - */ - - for (i = 0; i < rows; i++) { - s = 0.0; // calculate ith element of [Q * w] - - for (j = k; j < rows; j++) { - s = addScalar(s, multiplyScalar(Qdata[i][j], w[j])); - } // calculate the ith element of [tau * Q * w] - - s = multiplyScalar(s, tau); - - for (j = k; j < rows; ++j) { - Qdata[i][j] = divideScalar(subtract(Qdata[i][j], multiplyScalar(s, conj(w[j]))), conjSgn); - } - } - } - } // return matrices - - return { - Q: Q, - R: R, - toString: function toString() { - return 'Q: ' + this.Q.toString() + '\nR: ' + this.R.toString(); - } - }; - } - - function _denseQR(m) { - var ret = _denseQRimpl(m); - - var Rdata = ret.R._data; - - if (m._data.length > 0) { - var zero = Rdata[0][0].type === 'Complex' ? complex(0) : 0; - - for (var i = 0; i < Rdata.length; ++i) { - for (var j = 0; j < i && j < (Rdata[0] || []).length; ++j) { - Rdata[i][j] = zero; - } - } - } - - return ret; - } - - function _sparseQR(m) { - throw new Error('qr not implemented for sparse matrices yet'); - } - }); - // CONCATENATED MODULE: ./src/function/algebra/sparse/csPermute.js - /** - * Permutes a sparse matrix C = P * A * Q - * - * @param {SparseMatrix} a The Matrix A - * @param {Array} pinv The row permutation vector - * @param {Array} q The column permutation vector - * @param {boolean} values Create a pattern matrix (false), values and pattern otherwise - * - * @return {Matrix} C = P * A * Q, null on error - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - function csPermute(a, pinv, q, values) { - // a arrays - var avalues = a._values; - var aindex = a._index; - var aptr = a._ptr; - var asize = a._size; - var adt = a._datatype; // rows & columns - - var m = asize[0]; - var n = asize[1]; // c arrays - - var cvalues = values && a._values ? [] : null; - var cindex = []; // (aptr[n]) - - var cptr = []; // (n + 1) - // initialize vars - - var nz = 0; // loop columns - - for (var k = 0; k < n; k++) { - // column k of C is column q[k] of A - cptr[k] = nz; // apply column permutation - - var j = q ? q[k] : k; // loop values in column j of A - - for (var t0 = aptr[j], t1 = aptr[j + 1], t = t0; t < t1; t++) { - // row i of A is row pinv[i] of C - var r = pinv ? pinv[aindex[t]] : aindex[t]; // index - - cindex[nz] = r; // check we need to populate values - - if (cvalues) { - cvalues[nz] = avalues[t]; - } // increment number of nonzero elements - - nz++; - } - } // finalize the last column of C - - cptr[n] = nz; // return C matrix - - return a.createSparseMatrix({ - values: cvalues, - index: cindex, - ptr: cptr, - size: [m, n], - datatype: adt - }); - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csTdfs.js - /** - * Depth-first search and postorder of a tree rooted at node j - * - * @param {Number} j The tree node - * @param {Number} k - * @param {Array} w The workspace array - * @param {Number} head The index offset within the workspace for the head array - * @param {Number} next The index offset within the workspace for the next array - * @param {Array} post The post ordering array - * @param {Number} stack The index offset within the workspace for the stack array - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - function csTdfs(j, k, w, head, next, post, stack) { - // variables - var top = 0; // place j on the stack - - w[stack] = j; // while (stack is not empty) - - while (top >= 0) { - // p = top of stack - var p = w[stack + top]; // i = youngest child of p - - var i = w[head + p]; - - if (i === -1) { - // p has no unordered children left - top--; // node p is the kth postordered node - - post[k++] = p; - } else { - // remove i from children of p - w[head + p] = w[next + i]; // increment top - - ++top; // start dfs on child node i - - w[stack + top] = i; - } - } - - return k; - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csPost.js - - /** - * Post order a tree of forest - * - * @param {Array} parent The tree or forest - * @param {Number} n Number of columns - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - - function csPost(parent, n) { - // check inputs - if (!parent) { - return null; - } // vars - - var k = 0; - var j; // allocate result - - var post = []; // (n) - // workspace, head: first n entries, next: next n entries, stack: last n entries - - var w = []; // (3 * n) - - var head = 0; - var next = n; - var stack = 2 * n; // initialize workspace - - for (j = 0; j < n; j++) { - // empty linked lists - w[head + j] = -1; - } // traverse nodes in reverse order - - for (j = n - 1; j >= 0; j--) { - // check j is a root - if (parent[j] === -1) { - continue; - } // add j to list of its parent - - w[next + j] = w[head + parent[j]]; - w[head + parent[j]] = j; - } // loop nodes - - for (j = 0; j < n; j++) { - // skip j if it is not a root - if (parent[j] !== -1) { - continue; - } // depth-first search - - k = csTdfs(j, k, w, head, next, post, stack); - } - - return post; - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csEtree.js - /** - * Computes the elimination tree of Matrix A (using triu(A)) or the - * elimination tree of A'A without forming A'A. - * - * @param {Matrix} a The A Matrix - * @param {boolean} ata A value of true the function computes the etree of A'A - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - function csEtree(a, ata) { - // check inputs - if (!a) { - return null; - } // a arrays - - var aindex = a._index; - var aptr = a._ptr; - var asize = a._size; // rows & columns - - var m = asize[0]; - var n = asize[1]; // allocate result - - var parent = []; // (n) - // allocate workspace - - var w = []; // (n + (ata ? m : 0)) - - var ancestor = 0; // first n entries in w - - var prev = n; // last m entries (ata = true) - - var i, inext; // check we are calculating A'A - - if (ata) { - // initialize workspace - for (i = 0; i < m; i++) { - w[prev + i] = -1; - } - } // loop columns - - for (var k = 0; k < n; k++) { - // node k has no parent yet - parent[k] = -1; // nor does k have an ancestor - - w[ancestor + k] = -1; // values in column k - - for (var p0 = aptr[k], p1 = aptr[k + 1], p = p0; p < p1; p++) { - // row - var r = aindex[p]; // node - - i = ata ? w[prev + r] : r; // traverse from i to k - - for (; i !== -1 && i < k; i = inext) { - // inext = ancestor of i - inext = w[ancestor + i]; // path compression - - w[ancestor + i] = k; // check no anc., parent is k - - if (inext === -1) { - parent[i] = k; - } - } - - if (ata) { - w[prev + r] = k; - } - } - } - - return parent; - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csFkeep.js - /** - * Keeps entries in the matrix when the callback function returns true, removes the entry otherwise - * - * @param {Matrix} a The sparse matrix - * @param {function} callback The callback function, function will be invoked with the following args: - * - The entry row - * - The entry column - * - The entry value - * - The state parameter - * @param {any} other The state - * - * @return The number of nonzero elements in the matrix - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - function csFkeep(a, callback, other) { - // a arrays - var avalues = a._values; - var aindex = a._index; - var aptr = a._ptr; - var asize = a._size; // columns - - var n = asize[1]; // nonzero items - - var nz = 0; // loop columns - - for (var j = 0; j < n; j++) { - // get current location of col j - var p = aptr[j]; // record new location of col j - - aptr[j] = nz; - - for (; p < aptr[j + 1]; p++) { - // check we need to keep this item - if (callback(aindex[p], j, avalues ? avalues[p] : 1, other)) { - // keep A(i,j) - aindex[nz] = aindex[p]; // check we need to process values (pattern only) - - if (avalues) { - avalues[nz] = avalues[p]; - } // increment nonzero items - - nz++; - } - } - } // finalize A - - aptr[n] = nz; // trim arrays - - aindex.splice(nz, aindex.length - nz); // check we need to process values (pattern only) - - if (avalues) { - avalues.splice(nz, avalues.length - nz); - } // return number of nonzero items - - return nz; - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csFlip.js - /** - * This function "flips" its input about the integer -1. - * - * @param {Number} i The value to flip - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - function csFlip(i) { - // flip the value - return -i - 2; - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csAmd.js - - var csAmd_name = 'csAmd'; - var csAmd_dependencies = ['add', 'multiply', 'transpose']; - var createCsAmd = /* #__PURE__ */Object(factory["a" /* factory */])(csAmd_name, csAmd_dependencies, function (_ref) { - var add = _ref.add, - multiply = _ref.multiply, - transpose = _ref.transpose; - - /** - * Approximate minimum degree ordering. The minimum degree algorithm is a widely used - * heuristic for finding a permutation P so that P*A*P' has fewer nonzeros in its factorization - * than A. It is a gready method that selects the sparsest pivot row and column during the course - * of a right looking sparse Cholesky factorization. - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - * - * @param {Number} order 0: Natural, 1: Cholesky, 2: LU, 3: QR - * @param {Matrix} m Sparse Matrix - */ - return function csAmd(order, a) { - // check input parameters - if (!a || order <= 0 || order > 3) { - return null; - } // a matrix arrays - - var asize = a._size; // rows and columns - - var m = asize[0]; - var n = asize[1]; // initialize vars - - var lemax = 0; // dense threshold - - var dense = Math.max(16, 10 * Math.sqrt(n)); - dense = Math.min(n - 2, dense); // create target matrix C - - var cm = _createTargetMatrix(order, a, m, n, dense); // drop diagonal entries - - csFkeep(cm, _diag, null); // C matrix arrays - - var cindex = cm._index; - var cptr = cm._ptr; // number of nonzero elements in C - - var cnz = cptr[n]; // allocate result (n+1) - - var P = []; // create workspace (8 * (n + 1)) - - var W = []; - var len = 0; // first n + 1 entries - - var nv = n + 1; // next n + 1 entries - - var next = 2 * (n + 1); // next n + 1 entries - - var head = 3 * (n + 1); // next n + 1 entries - - var elen = 4 * (n + 1); // next n + 1 entries - - var degree = 5 * (n + 1); // next n + 1 entries - - var w = 6 * (n + 1); // next n + 1 entries - - var hhead = 7 * (n + 1); // last n + 1 entries - // use P as workspace for last - - var last = P; // initialize quotient graph - - var mark = _initializeQuotientGraph(n, cptr, W, len, head, last, next, hhead, nv, w, elen, degree); // initialize degree lists - - var nel = _initializeDegreeLists(n, cptr, W, degree, elen, w, dense, nv, head, last, next); // minimum degree node - - var mindeg = 0; // vars - - var i, j, k, k1, k2, e, pj, ln, nvi, pk, eln, p1, p2, pn, h, d; // while (selecting pivots) do - - while (nel < n) { - // select node of minimum approximate degree. amd() is now ready to start eliminating the graph. It first - // finds a node k of minimum degree and removes it from its degree list. The variable nel keeps track of thow - // many nodes have been eliminated. - for (k = -1; mindeg < n && (k = W[head + mindeg]) === -1; mindeg++) { - ; - } - - if (W[next + k] !== -1) { - last[W[next + k]] = -1; - } // remove k from degree list - - W[head + mindeg] = W[next + k]; // elenk = |Ek| - - var elenk = W[elen + k]; // # of nodes k represents - - var nvk = W[nv + k]; // W[nv + k] nodes of A eliminated - - nel += nvk; // Construct a new element. The new element Lk is constructed in place if |Ek| = 0. nv[i] is - // negated for all nodes i in Lk to flag them as members of this set. Each node i is removed from the - // degree lists. All elements e in Ek are absorved into element k. - - var dk = 0; // flag k as in Lk - - W[nv + k] = -nvk; - var p = cptr[k]; // do in place if W[elen + k] === 0 - - var pk1 = elenk === 0 ? p : cnz; - var pk2 = pk1; - - for (k1 = 1; k1 <= elenk + 1; k1++) { - if (k1 > elenk) { - // search the nodes in k - e = k; // list of nodes starts at cindex[pj] - - pj = p; // length of list of nodes in k - - ln = W[len + k] - elenk; - } else { - // search the nodes in e - e = cindex[p++]; - pj = cptr[e]; // length of list of nodes in e - - ln = W[len + e]; - } - - for (k2 = 1; k2 <= ln; k2++) { - i = cindex[pj++]; // check node i dead, or seen - - if ((nvi = W[nv + i]) <= 0) { - continue; - } // W[degree + Lk] += size of node i - - dk += nvi; // negate W[nv + i] to denote i in Lk - - W[nv + i] = -nvi; // place i in Lk - - cindex[pk2++] = i; - - if (W[next + i] !== -1) { - last[W[next + i]] = last[i]; - } // check we need to remove i from degree list - - if (last[i] !== -1) { - W[next + last[i]] = W[next + i]; - } else { - W[head + W[degree + i]] = W[next + i]; - } - } - - if (e !== k) { - // absorb e into k - cptr[e] = csFlip(k); // e is now a dead element - - W[w + e] = 0; - } - } // cindex[cnz...nzmax] is free - - if (elenk !== 0) { - cnz = pk2; - } // external degree of k - |Lk\i| - - W[degree + k] = dk; // element k is in cindex[pk1..pk2-1] - - cptr[k] = pk1; - W[len + k] = pk2 - pk1; // k is now an element - - W[elen + k] = -2; // Find set differences. The scan1 function now computes the set differences |Le \ Lk| for all elements e. At the start of the - // scan, no entry in the w array is greater than or equal to mark. - // clear w if necessary - - mark = _wclear(mark, lemax, W, w, n); // scan 1: find |Le\Lk| - - for (pk = pk1; pk < pk2; pk++) { - i = cindex[pk]; // check if W[elen + i] empty, skip it - - if ((eln = W[elen + i]) <= 0) { - continue; - } // W[nv + i] was negated - - nvi = -W[nv + i]; - var wnvi = mark - nvi; // scan Ei - - for (p = cptr[i], p1 = cptr[i] + eln - 1; p <= p1; p++) { - e = cindex[p]; - - if (W[w + e] >= mark) { - // decrement |Le\Lk| - W[w + e] -= nvi; - } else if (W[w + e] !== 0) { - // ensure e is a live element, 1st time e seen in scan 1 - W[w + e] = W[degree + e] + wnvi; - } - } - } // degree update - // The second pass computes the approximate degree di, prunes the sets Ei and Ai, and computes a hash - // function h(i) for all nodes in Lk. - // scan2: degree update - - for (pk = pk1; pk < pk2; pk++) { - // consider node i in Lk - i = cindex[pk]; - p1 = cptr[i]; - p2 = p1 + W[elen + i] - 1; - pn = p1; // scan Ei - - for (h = 0, d = 0, p = p1; p <= p2; p++) { - e = cindex[p]; // check e is an unabsorbed element - - if (W[w + e] !== 0) { - // dext = |Le\Lk| - var dext = W[w + e] - mark; - - if (dext > 0) { - // sum up the set differences - d += dext; // keep e in Ei - - cindex[pn++] = e; // compute the hash of node i - - h += e; - } else { - // aggressive absorb. e->k - cptr[e] = csFlip(k); // e is a dead element - - W[w + e] = 0; - } - } - } // W[elen + i] = |Ei| - - W[elen + i] = pn - p1 + 1; - var p3 = pn; - var p4 = p1 + W[len + i]; // prune edges in Ai - - for (p = p2 + 1; p < p4; p++) { - j = cindex[p]; // check node j dead or in Lk - - var nvj = W[nv + j]; - - if (nvj <= 0) { - continue; - } // degree(i) += |j| - - d += nvj; // place j in node list of i - - cindex[pn++] = j; // compute hash for node i - - h += j; - } // check for mass elimination - - if (d === 0) { - // absorb i into k - cptr[i] = csFlip(k); - nvi = -W[nv + i]; // |Lk| -= |i| - - dk -= nvi; // |k| += W[nv + i] - - nvk += nvi; - nel += nvi; - W[nv + i] = 0; // node i is dead - - W[elen + i] = -1; - } else { - // update degree(i) - W[degree + i] = Math.min(W[degree + i], d); // move first node to end - - cindex[pn] = cindex[p3]; // move 1st el. to end of Ei - - cindex[p3] = cindex[p1]; // add k as 1st element in of Ei - - cindex[p1] = k; // new len of adj. list of node i - - W[len + i] = pn - p1 + 1; // finalize hash of i - - h = (h < 0 ? -h : h) % n; // place i in hash bucket - - W[next + i] = W[hhead + h]; - W[hhead + h] = i; // save hash of i in last[i] - - last[i] = h; - } - } // finalize |Lk| - - W[degree + k] = dk; - lemax = Math.max(lemax, dk); // clear w - - mark = _wclear(mark + lemax, lemax, W, w, n); // Supernode detection. Supernode detection relies on the hash function h(i) computed for each node i. - // If two nodes have identical adjacency lists, their hash functions wil be identical. - - for (pk = pk1; pk < pk2; pk++) { - i = cindex[pk]; // check i is dead, skip it - - if (W[nv + i] >= 0) { - continue; - } // scan hash bucket of node i - - h = last[i]; - i = W[hhead + h]; // hash bucket will be empty - - W[hhead + h] = -1; - - for (; i !== -1 && W[next + i] !== -1; i = W[next + i], mark++) { - ln = W[len + i]; - eln = W[elen + i]; - - for (p = cptr[i] + 1; p <= cptr[i] + ln - 1; p++) { - W[w + cindex[p]] = mark; - } - - var jlast = i; // compare i with all j - - for (j = W[next + i]; j !== -1;) { - var ok = W[len + j] === ln && W[elen + j] === eln; - - for (p = cptr[j] + 1; ok && p <= cptr[j] + ln - 1; p++) { - // compare i and j - if (W[w + cindex[p]] !== mark) { - ok = 0; - } - } // check i and j are identical - - if (ok) { - // absorb j into i - cptr[j] = csFlip(i); - W[nv + i] += W[nv + j]; - W[nv + j] = 0; // node j is dead - - W[elen + j] = -1; // delete j from hash bucket - - j = W[next + j]; - W[next + jlast] = j; - } else { - // j and i are different - jlast = j; - j = W[next + j]; - } - } - } - } // Finalize new element. The elimination of node k is nearly complete. All nodes i in Lk are scanned one last time. - // Node i is removed from Lk if it is dead. The flagged status of nv[i] is cleared. - - for (p = pk1, pk = pk1; pk < pk2; pk++) { - i = cindex[pk]; // check i is dead, skip it - - if ((nvi = -W[nv + i]) <= 0) { - continue; - } // restore W[nv + i] - - W[nv + i] = nvi; // compute external degree(i) - - d = W[degree + i] + dk - nvi; - d = Math.min(d, n - nel - nvi); - - if (W[head + d] !== -1) { - last[W[head + d]] = i; - } // put i back in degree list - - W[next + i] = W[head + d]; - last[i] = -1; - W[head + d] = i; // find new minimum degree - - mindeg = Math.min(mindeg, d); - W[degree + i] = d; // place i in Lk - - cindex[p++] = i; - } // # nodes absorbed into k - - W[nv + k] = nvk; // length of adj list of element k - - if ((W[len + k] = p - pk1) === 0) { - // k is a root of the tree - cptr[k] = -1; // k is now a dead element - - W[w + k] = 0; - } - - if (elenk !== 0) { - // free unused space in Lk - cnz = p; - } - } // Postordering. The elimination is complete, but no permutation has been computed. All that is left - // of the graph is the assembly tree (ptr) and a set of dead nodes and elements (i is a dead node if - // nv[i] is zero and a dead element if nv[i] > 0). It is from this information only that the final permutation - // is computed. The tree is restored by unflipping all of ptr. - // fix assembly tree - - for (i = 0; i < n; i++) { - cptr[i] = csFlip(cptr[i]); - } - - for (j = 0; j <= n; j++) { - W[head + j] = -1; - } // place unordered nodes in lists - - for (j = n; j >= 0; j--) { - // skip if j is an element - if (W[nv + j] > 0) { - continue; - } // place j in list of its parent - - W[next + j] = W[head + cptr[j]]; - W[head + cptr[j]] = j; - } // place elements in lists - - for (e = n; e >= 0; e--) { - // skip unless e is an element - if (W[nv + e] <= 0) { - continue; - } - - if (cptr[e] !== -1) { - // place e in list of its parent - W[next + e] = W[head + cptr[e]]; - W[head + cptr[e]] = e; - } - } // postorder the assembly tree - - for (k = 0, i = 0; i <= n; i++) { - if (cptr[i] === -1) { - k = csTdfs(i, k, W, head, next, P, w); - } - } // remove last item in array - - P.splice(P.length - 1, 1); // return P - - return P; - }; - /** - * Creates the matrix that will be used by the approximate minimum degree ordering algorithm. The function accepts the matrix M as input and returns a permutation - * vector P. The amd algorithm operates on a symmetrix matrix, so one of three symmetric matrices is formed. - * - * Order: 0 - * A natural ordering P=null matrix is returned. - * - * Order: 1 - * Matrix must be square. This is appropriate for a Cholesky or LU factorization. - * P = M + M' - * - * Order: 2 - * Dense columns from M' are dropped, M recreated from M'. This is appropriatefor LU factorization of unsymmetric matrices. - * P = M' * M - * - * Order: 3 - * This is best used for QR factorization or LU factorization is matrix M has no dense rows. A dense row is a row with more than 10*sqr(columns) entries. - * P = M' * M - */ - - function _createTargetMatrix(order, a, m, n, dense) { - // compute A' - var at = transpose(a); // check order = 1, matrix must be square - - if (order === 1 && n === m) { - // C = A + A' - return add(a, at); - } // check order = 2, drop dense columns from M' - - if (order === 2) { - // transpose arrays - var tindex = at._index; - var tptr = at._ptr; // new column index - - var p2 = 0; // loop A' columns (rows) - - for (var j = 0; j < m; j++) { - // column j of AT starts here - var p = tptr[j]; // new column j starts here - - tptr[j] = p2; // skip dense col j - - if (tptr[j + 1] - p > dense) { - continue; - } // map rows in column j of A - - for (var p1 = tptr[j + 1]; p < p1; p++) { - tindex[p2++] = tindex[p]; - } - } // finalize AT - - tptr[m] = p2; // recreate A from new transpose matrix - - a = transpose(at); // use A' * A - - return multiply(at, a); - } // use A' * A, square or rectangular matrix - - return multiply(at, a); - } - /** - * Initialize quotient graph. There are four kind of nodes and elements that must be represented: - * - * - A live node is a node i (or a supernode) that has not been selected as a pivot nad has not been merged into another supernode. - * - A dead node i is one that has been removed from the graph, having been absorved into r = flip(ptr[i]). - * - A live element e is one that is in the graph, having been formed when node e was selected as the pivot. - * - A dead element e is one that has benn absorved into a subsequent element s = flip(ptr[e]). - */ - - function _initializeQuotientGraph(n, cptr, W, len, head, last, next, hhead, nv, w, elen, degree) { - // Initialize quotient graph - for (var k = 0; k < n; k++) { - W[len + k] = cptr[k + 1] - cptr[k]; - } - - W[len + n] = 0; // initialize workspace - - for (var i = 0; i <= n; i++) { - // degree list i is empty - W[head + i] = -1; - last[i] = -1; - W[next + i] = -1; // hash list i is empty - - W[hhead + i] = -1; // node i is just one node - - W[nv + i] = 1; // node i is alive - - W[w + i] = 1; // Ek of node i is empty - - W[elen + i] = 0; // degree of node i - - W[degree + i] = W[len + i]; - } // clear w - - var mark = _wclear(0, 0, W, w, n); // n is a dead element - - W[elen + n] = -2; // n is a root of assembly tree - - cptr[n] = -1; // n is a dead element - - W[w + n] = 0; // return mark - - return mark; - } - /** - * Initialize degree lists. Each node is placed in its degree lists. Nodes of zero degree are eliminated immediately. Nodes with - * degree >= dense are alsol eliminated and merged into a placeholder node n, a dead element. Thes nodes will appera last in the - * output permutation p. - */ - - function _initializeDegreeLists(n, cptr, W, degree, elen, w, dense, nv, head, last, next) { - // result - var nel = 0; // loop columns - - for (var i = 0; i < n; i++) { - // degree @ i - var d = W[degree + i]; // check node i is empty - - if (d === 0) { - // element i is dead - W[elen + i] = -2; - nel++; // i is a root of assembly tree - - cptr[i] = -1; - W[w + i] = 0; - } else if (d > dense) { - // absorb i into element n - W[nv + i] = 0; // node i is dead - - W[elen + i] = -1; - nel++; - cptr[i] = csFlip(n); - W[nv + n]++; - } else { - var h = W[head + d]; - - if (h !== -1) { - last[h] = i; - } // put node i in degree list d - - W[next + i] = W[head + d]; - W[head + d] = i; - } - } - - return nel; - } - - function _wclear(mark, lemax, W, w, n) { - if (mark < 2 || mark + lemax < 0) { - for (var k = 0; k < n; k++) { - if (W[w + k] !== 0) { - W[w + k] = 1; - } - } - - mark = 2; - } // at this point, W [0..n-1] < mark holds - - return mark; - } - - function _diag(i, j) { - return i !== j; - } - }); - // CONCATENATED MODULE: ./src/function/algebra/sparse/csLeaf.js - /** - * This function determines if j is a leaf of the ith row subtree. - * Consider A(i,j), node j in ith row subtree and return lca(jprev,j) - * - * @param {Number} i The ith row subtree - * @param {Number} j The node to test - * @param {Array} w The workspace array - * @param {Number} first The index offset within the workspace for the first array - * @param {Number} maxfirst The index offset within the workspace for the maxfirst array - * @param {Number} prevleaf The index offset within the workspace for the prevleaf array - * @param {Number} ancestor The index offset within the workspace for the ancestor array - * - * @return {Object} - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - function csLeaf(i, j, w, first, maxfirst, prevleaf, ancestor) { - var s, sparent; // our result - - var jleaf = 0; - var q; // check j is a leaf - - if (i <= j || w[first + j] <= w[maxfirst + i]) { - return -1; - } // update max first[j] seen so far - - w[maxfirst + i] = w[first + j]; // jprev = previous leaf of ith subtree - - var jprev = w[prevleaf + i]; - w[prevleaf + i] = j; // check j is first or subsequent leaf - - if (jprev === -1) { - // 1st leaf, q = root of ith subtree - jleaf = 1; - q = i; - } else { - // update jleaf - jleaf = 2; // q = least common ancester (jprev,j) - - for (q = jprev; q !== w[ancestor + q]; q = w[ancestor + q]) { - ; - } - - for (s = jprev; s !== q; s = sparent) { - // path compression - sparent = w[ancestor + s]; - w[ancestor + s] = q; - } - } - - return { - jleaf: jleaf, - q: q - }; - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csCounts.js - - var csCounts_name = 'csCounts'; - var csCounts_dependencies = ['transpose']; - var createCsCounts = /* #__PURE__ */Object(factory["a" /* factory */])(csCounts_name, csCounts_dependencies, function (_ref) { - var transpose = _ref.transpose; - - /** - * Computes the column counts using the upper triangular part of A. - * It transposes A internally, none of the input parameters are modified. - * - * @param {Matrix} a The sparse matrix A - * - * @param {Matrix} ata Count the columns of A'A instead - * - * @return An array of size n of the column counts or null on error - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - return function (a, parent, post, ata) { - // check inputs - if (!a || !parent || !post) { - return null; - } // a matrix arrays - - var asize = a._size; // rows and columns - - var m = asize[0]; - var n = asize[1]; // variables - - var i, j, k, J, p, p0, p1; // workspace size - - var s = 4 * n + (ata ? n + m + 1 : 0); // allocate workspace - - var w = []; // (s) - - var ancestor = 0; // first n entries - - var maxfirst = n; // next n entries - - var prevleaf = 2 * n; // next n entries - - var first = 3 * n; // next n entries - - var head = 4 * n; // next n + 1 entries (used when ata is true) - - var next = 5 * n + 1; // last entries in workspace - // clear workspace w[0..s-1] - - for (k = 0; k < s; k++) { - w[k] = -1; - } // allocate result - - var colcount = []; // (n) - // AT = A' - - var at = transpose(a); // at arrays - - var tindex = at._index; - var tptr = at._ptr; // find w[first + j] - - for (k = 0; k < n; k++) { - j = post[k]; // colcount[j]=1 if j is a leaf - - colcount[j] = w[first + j] === -1 ? 1 : 0; - - for (; j !== -1 && w[first + j] === -1; j = parent[j]) { - w[first + j] = k; - } - } // initialize ata if needed - - if (ata) { - // invert post - for (k = 0; k < n; k++) { - w[post[k]] = k; - } // loop rows (columns in AT) - - for (i = 0; i < m; i++) { - // values in column i of AT - for (k = n, p0 = tptr[i], p1 = tptr[i + 1], p = p0; p < p1; p++) { - k = Math.min(k, w[tindex[p]]); - } // place row i in linked list k - - w[next + i] = w[head + k]; - w[head + k] = i; - } - } // each node in its own set - - for (i = 0; i < n; i++) { - w[ancestor + i] = i; - } - - for (k = 0; k < n; k++) { - // j is the kth node in postordered etree - j = post[k]; // check j is not a root - - if (parent[j] !== -1) { - colcount[parent[j]]--; - } // J=j for LL'=A case - - for (J = ata ? w[head + k] : j; J !== -1; J = ata ? w[next + J] : -1) { - for (p = tptr[J]; p < tptr[J + 1]; p++) { - i = tindex[p]; - var r = csLeaf(i, j, w, first, maxfirst, prevleaf, ancestor); // check A(i,j) is in skeleton - - if (r.jleaf >= 1) { - colcount[j]++; - } // check account for overlap in q - - if (r.jleaf === 2) { - colcount[r.q]--; - } - } - } - - if (parent[j] !== -1) { - w[ancestor + j] = parent[j]; - } - } // sum up colcount's of each child - - for (j = 0; j < n; j++) { - if (parent[j] !== -1) { - colcount[parent[j]] += colcount[j]; - } - } - - return colcount; - }; - }); - // CONCATENATED MODULE: ./src/function/algebra/sparse/csSqr.js - - var csSqr_name = 'csSqr'; - var csSqr_dependencies = ['add', 'multiply', 'transpose']; - var createCsSqr = /* #__PURE__ */Object(factory["a" /* factory */])(csSqr_name, csSqr_dependencies, function (_ref) { - var add = _ref.add, - multiply = _ref.multiply, - transpose = _ref.transpose; - var csAmd = createCsAmd({ - add: add, - multiply: multiply, - transpose: transpose - }); - var csCounts = createCsCounts({ - transpose: transpose - }); - /** - * Symbolic ordering and analysis for QR and LU decompositions. - * - * @param {Number} order The ordering strategy (see csAmd for more details) - * @param {Matrix} a The A matrix - * @param {boolean} qr Symbolic ordering and analysis for QR decomposition (true) or - * symbolic ordering and analysis for LU decomposition (false) - * - * @return {Object} The Symbolic ordering and analysis for matrix A - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - - return function csSqr(order, a, qr) { - // a arrays - var aptr = a._ptr; - var asize = a._size; // columns - - var n = asize[1]; // vars - - var k; // symbolic analysis result - - var s = {}; // fill-reducing ordering - - s.q = csAmd(order, a); // validate results - - if (order && !s.q) { - return null; - } // QR symbolic analysis - - if (qr) { - // apply permutations if needed - var c = order ? csPermute(a, null, s.q, 0) : a; // etree of C'*C, where C=A(:,q) - - s.parent = csEtree(c, 1); // post order elimination tree - - var post = csPost(s.parent, n); // col counts chol(C'*C) - - s.cp = csCounts(c, s.parent, post, 1); // check we have everything needed to calculate number of nonzero elements - - if (c && s.parent && s.cp && _vcount(c, s)) { - // calculate number of nonzero elements - for (s.unz = 0, k = 0; k < n; k++) { - s.unz += s.cp[k]; - } - } - } else { - // for LU factorization only, guess nnz(L) and nnz(U) - s.unz = 4 * aptr[n] + n; - s.lnz = s.unz; - } // return result S - - return s; - }; - /** - * Compute nnz(V) = s.lnz, s.pinv, s.leftmost, s.m2 from A and s.parent - */ - - function _vcount(a, s) { - // a arrays - var aptr = a._ptr; - var aindex = a._index; - var asize = a._size; // rows & columns - - var m = asize[0]; - var n = asize[1]; // initialize s arrays - - s.pinv = []; // (m + n) - - s.leftmost = []; // (m) - // vars - - var parent = s.parent; - var pinv = s.pinv; - var leftmost = s.leftmost; // workspace, next: first m entries, head: next n entries, tail: next n entries, nque: next n entries - - var w = []; // (m + 3 * n) - - var next = 0; - var head = m; - var tail = m + n; - var nque = m + 2 * n; // vars - - var i, k, p, p0, p1; // initialize w - - for (k = 0; k < n; k++) { - // queue k is empty - w[head + k] = -1; - w[tail + k] = -1; - w[nque + k] = 0; - } // initialize row arrays - - for (i = 0; i < m; i++) { - leftmost[i] = -1; - } // loop columns backwards - - for (k = n - 1; k >= 0; k--) { - // values & index for column k - for (p0 = aptr[k], p1 = aptr[k + 1], p = p0; p < p1; p++) { - // leftmost[i] = min(find(A(i,:))) - leftmost[aindex[p]] = k; - } - } // scan rows in reverse order - - for (i = m - 1; i >= 0; i--) { - // row i is not yet ordered - pinv[i] = -1; - k = leftmost[i]; // check row i is empty - - if (k === -1) { - continue; - } // first row in queue k - - if (w[nque + k]++ === 0) { - w[tail + k] = i; - } // put i at head of queue k - - w[next + i] = w[head + k]; - w[head + k] = i; - } - - s.lnz = 0; - s.m2 = m; // find row permutation and nnz(V) - - for (k = 0; k < n; k++) { - // remove row i from queue k - i = w[head + k]; // count V(k,k) as nonzero - - s.lnz++; // add a fictitious row - - if (i < 0) { - i = s.m2++; - } // associate row i with V(:,k) - - pinv[i] = k; // skip if V(k+1:m,k) is empty - - if (--nque[k] <= 0) { - continue; - } // nque[k] is nnz (V(k+1:m,k)) - - s.lnz += w[nque + k]; // move all rows to parent of k - - var pa = parent[k]; - - if (pa !== -1) { - if (w[nque + pa] === 0) { - w[tail + pa] = w[tail + k]; - } - - w[next + w[tail + k]] = w[head + pa]; - w[head + pa] = w[next + i]; - w[nque + pa] += w[nque + k]; - } - } - - for (i = 0; i < m; i++) { - if (pinv[i] < 0) { - pinv[i] = k++; - } - } - - return true; - } - }); - // CONCATENATED MODULE: ./src/function/algebra/sparse/csMarked.js - /** - * Checks if the node at w[j] is marked - * - * @param {Array} w The array - * @param {Number} j The array index - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - function csMarked(w, j) { - // check node is marked - return w[j] < 0; - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csMark.js - - /** - * Marks the node at w[j] - * - * @param {Array} w The array - * @param {Number} j The array index - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - - function csMark(w, j) { - // mark w[j] - w[j] = csFlip(w[j]); - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csUnflip.js - - /** - * Flips the value if it is negative of returns the same value otherwise. - * - * @param {Number} i The value to flip - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - - function csUnflip(i) { - // flip the value if it is negative - return i < 0 ? csFlip(i) : i; - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csDfs.js - - /** - * Depth-first search computes the nonzero pattern xi of the directed graph G (Matrix) starting - * at nodes in B (see csReach()). - * - * @param {Number} j The starting node for the DFS algorithm - * @param {Matrix} g The G matrix to search, ptr array modified, then restored - * @param {Number} top Start index in stack xi[top..n-1] - * @param {Number} k The kth column in B - * @param {Array} xi The nonzero pattern xi[top] .. xi[n - 1], an array of size = 2 * n - * The first n entries is the nonzero pattern, the last n entries is the stack - * @param {Array} pinv The inverse row permutation vector, must be null for L * x = b - * - * @return {Number} New value of top - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - - function csDfs(j, g, top, xi, pinv) { - // g arrays - var index = g._index; - var ptr = g._ptr; - var size = g._size; // columns - - var n = size[1]; // vars - - var i, p, p2; // initialize head - - var head = 0; // initialize the recursion stack - - xi[0] = j; // loop - - while (head >= 0) { - // get j from the top of the recursion stack - j = xi[head]; // apply permutation vector - - var jnew = pinv ? pinv[j] : j; // check node j is marked - - if (!csMarked(ptr, j)) { - // mark node j as visited - csMark(ptr, j); // update stack (last n entries in xi) - - xi[n + head] = jnew < 0 ? 0 : csUnflip(ptr[jnew]); - } // node j done if no unvisited neighbors - - var done = 1; // examine all neighbors of j, stack (last n entries in xi) - - for (p = xi[n + head], p2 = jnew < 0 ? 0 : csUnflip(ptr[jnew + 1]); p < p2; p++) { - // consider neighbor node i - i = index[p]; // check we have visited node i, skip it - - if (csMarked(ptr, i)) { - continue; - } // pause depth-first search of node j, update stack (last n entries in xi) - - xi[n + head] = p; // start dfs at node i - - xi[++head] = i; // node j is not done - - done = 0; // break, to start dfs(i) - - break; - } // check depth-first search at node j is done - - if (done) { - // remove j from the recursion stack - head--; // and place in the output stack - - xi[--top] = j; - } - } - - return top; - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csReach.js - - /** - * The csReach function computes X = Reach(B), where B is the nonzero pattern of the n-by-1 - * sparse column of vector b. The function returns the set of nodes reachable from any node in B. The - * nonzero pattern xi of the solution x to the sparse linear system Lx=b is given by X=Reach(B). - * - * @param {Matrix} g The G matrix - * @param {Matrix} b The B matrix - * @param {Number} k The kth column in B - * @param {Array} xi The nonzero pattern xi[top] .. xi[n - 1], an array of size = 2 * n - * The first n entries is the nonzero pattern, the last n entries is the stack - * @param {Array} pinv The inverse row permutation vector - * - * @return {Number} The index for the nonzero pattern - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - - function csReach(g, b, k, xi, pinv) { - // g arrays - var gptr = g._ptr; - var gsize = g._size; // b arrays - - var bindex = b._index; - var bptr = b._ptr; // columns - - var n = gsize[1]; // vars - - var p, p0, p1; // initialize top - - var top = n; // loop column indeces in B - - for (p0 = bptr[k], p1 = bptr[k + 1], p = p0; p < p1; p++) { - // node i - var i = bindex[p]; // check node i is marked - - if (!csMarked(gptr, i)) { - // start a dfs at unmarked node i - top = csDfs(i, g, top, xi, pinv); - } - } // loop columns from top -> n - 1 - - for (p = top; p < n; p++) { - // restore G - csMark(gptr, xi[p]); - } - - return top; - } - // CONCATENATED MODULE: ./src/function/algebra/sparse/csSpsolve.js - - var csSpsolve_name = 'csSpsolve'; - var csSpsolve_dependencies = ['divideScalar', 'multiply', 'subtract']; - var createCsSpsolve = /* #__PURE__ */Object(factory["a" /* factory */])(csSpsolve_name, csSpsolve_dependencies, function (_ref) { - var divideScalar = _ref.divideScalar, - multiply = _ref.multiply, - subtract = _ref.subtract; - - /** - * The function csSpsolve() computes the solution to G * x = bk, where bk is the - * kth column of B. When lo is true, the function assumes G = L is lower triangular with the - * diagonal entry as the first entry in each column. When lo is true, the function assumes G = U - * is upper triangular with the diagonal entry as the last entry in each column. - * - * @param {Matrix} g The G matrix - * @param {Matrix} b The B matrix - * @param {Number} k The kth column in B - * @param {Array} xi The nonzero pattern xi[top] .. xi[n - 1], an array of size = 2 * n - * The first n entries is the nonzero pattern, the last n entries is the stack - * @param {Array} x The soluton to the linear system G * x = b - * @param {Array} pinv The inverse row permutation vector, must be null for L * x = b - * @param {boolean} lo The lower (true) upper triangular (false) flag - * - * @return {Number} The index for the nonzero pattern - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - return function csSpsolve(g, b, k, xi, x, pinv, lo) { - // g arrays - var gvalues = g._values; - var gindex = g._index; - var gptr = g._ptr; - var gsize = g._size; // columns - - var n = gsize[1]; // b arrays - - var bvalues = b._values; - var bindex = b._index; - var bptr = b._ptr; // vars - - var p, p0, p1, q; // xi[top..n-1] = csReach(B(:,k)) - - var top = csReach(g, b, k, xi, pinv); // clear x - - for (p = top; p < n; p++) { - x[xi[p]] = 0; - } // scatter b - - for (p0 = bptr[k], p1 = bptr[k + 1], p = p0; p < p1; p++) { - x[bindex[p]] = bvalues[p]; - } // loop columns - - for (var px = top; px < n; px++) { - // x array index for px - var j = xi[px]; // apply permutation vector (U x = b), j maps to column J of G - - var J = pinv ? pinv[j] : j; // check column J is empty - - if (J < 0) { - continue; - } // column value indeces in G, p0 <= p < p1 - - p0 = gptr[J]; - p1 = gptr[J + 1]; // x(j) /= G(j,j) - - x[j] = divideScalar(x[j], gvalues[lo ? p0 : p1 - 1]); // first entry L(j,j) - - p = lo ? p0 + 1 : p0; - q = lo ? p1 : p1 - 1; // loop - - for (; p < q; p++) { - // row - var i = gindex[p]; // x(i) -= G(i,j) * x(j) - - x[i] = subtract(x[i], multiply(gvalues[p], x[j])); - } - } // return top of stack - - return top; - }; - }); - // CONCATENATED MODULE: ./src/function/algebra/sparse/csLu.js - - var csLu_name = 'csLu'; - var csLu_dependencies = ['abs', 'divideScalar', 'multiply', 'subtract', 'larger', 'largerEq', 'SparseMatrix']; - var createCsLu = /* #__PURE__ */Object(factory["a" /* factory */])(csLu_name, csLu_dependencies, function (_ref) { - var abs = _ref.abs, - divideScalar = _ref.divideScalar, - multiply = _ref.multiply, - subtract = _ref.subtract, - larger = _ref.larger, - largerEq = _ref.largerEq, - SparseMatrix = _ref.SparseMatrix; - var csSpsolve = createCsSpsolve({ - divideScalar: divideScalar, - multiply: multiply, - subtract: subtract - }); - /** - * Computes the numeric LU factorization of the sparse matrix A. Implements a Left-looking LU factorization - * algorithm that computes L and U one column at a tume. At the kth step, it access columns 1 to k-1 of L - * and column k of A. Given the fill-reducing column ordering q (see parameter s) computes L, U and pinv so - * L * U = A(p, q), where p is the inverse of pinv. - * - * @param {Matrix} m The A Matrix to factorize - * @param {Object} s The symbolic analysis from csSqr(). Provides the fill-reducing - * column ordering q - * @param {Number} tol Partial pivoting threshold (1 for partial pivoting) - * - * @return {Number} The numeric LU factorization of A or null - * - * Reference: http://faculty.cse.tamu.edu/davis/publications.html - */ - - return function csLu(m, s, tol) { - // validate input - if (!m) { - return null; - } // m arrays - - var size = m._size; // columns - - var n = size[1]; // symbolic analysis result - - var q; - var lnz = 100; - var unz = 100; // update symbolic analysis parameters - - if (s) { - q = s.q; - lnz = s.lnz || lnz; - unz = s.unz || unz; - } // L arrays - - var lvalues = []; // (lnz) - - var lindex = []; // (lnz) - - var lptr = []; // (n + 1) - // L - - var L = new SparseMatrix({ - values: lvalues, - index: lindex, - ptr: lptr, - size: [n, n] - }); // U arrays - - var uvalues = []; // (unz) - - var uindex = []; // (unz) - - var uptr = []; // (n + 1) - // U - - var U = new SparseMatrix({ - values: uvalues, - index: uindex, - ptr: uptr, - size: [n, n] - }); // inverse of permutation vector - - var pinv = []; // (n) - // vars - - var i, p; // allocate arrays - - var x = []; // (n) - - var xi = []; // (2 * n) - // initialize variables - - for (i = 0; i < n; i++) { - // clear workspace - x[i] = 0; // no rows pivotal yet - - pinv[i] = -1; // no cols of L yet - - lptr[i + 1] = 0; - } // reset number of nonzero elements in L and U - - lnz = 0; - unz = 0; // compute L(:,k) and U(:,k) - - for (var k = 0; k < n; k++) { - // update ptr - lptr[k] = lnz; - uptr[k] = unz; // apply column permutations if needed - - var col = q ? q[k] : k; // solve triangular system, x = L\A(:,col) - - var top = csSpsolve(L, m, col, xi, x, pinv, 1); // find pivot - - var ipiv = -1; - var a = -1; // loop xi[] from top -> n - - for (p = top; p < n; p++) { - // x[i] is nonzero - i = xi[p]; // check row i is not yet pivotal - - if (pinv[i] < 0) { - // absolute value of x[i] - var xabs = abs(x[i]); // check absoulte value is greater than pivot value - - if (larger(xabs, a)) { - // largest pivot candidate so far - a = xabs; - ipiv = i; - } - } else { - // x(i) is the entry U(pinv[i],k) - uindex[unz] = pinv[i]; - uvalues[unz++] = x[i]; - } - } // validate we found a valid pivot - - if (ipiv === -1 || a <= 0) { - return null; - } // update actual pivot column, give preference to diagonal value - - if (pinv[col] < 0 && largerEq(abs(x[col]), multiply(a, tol))) { - ipiv = col; - } // the chosen pivot - - var pivot = x[ipiv]; // last entry in U(:,k) is U(k,k) - - uindex[unz] = k; - uvalues[unz++] = pivot; // ipiv is the kth pivot row - - pinv[ipiv] = k; // first entry in L(:,k) is L(k,k) = 1 - - lindex[lnz] = ipiv; - lvalues[lnz++] = 1; // L(k+1:n,k) = x / pivot - - for (p = top; p < n; p++) { - // row - i = xi[p]; // check x(i) is an entry in L(:,k) - - if (pinv[i] < 0) { - // save unpermuted row in L - lindex[lnz] = i; // scale pivot column - - lvalues[lnz++] = divideScalar(x[i], pivot); - } // x[0..n-1] = 0 for next k - - x[i] = 0; - } - } // update ptr - - lptr[n] = lnz; - uptr[n] = unz; // fix row indices of L for final pinv - - for (p = 0; p < lnz; p++) { - lindex[p] = pinv[lindex[p]]; - } // trim arrays - - lvalues.splice(lnz, lvalues.length - lnz); - lindex.splice(lnz, lindex.length - lnz); - uvalues.splice(unz, uvalues.length - unz); - uindex.splice(unz, uindex.length - unz); // return LU factor - - return { - L: L, - U: U, - pinv: pinv - }; - }; - }); - // CONCATENATED MODULE: ./src/function/algebra/decomposition/slu.js - - var slu_name = 'slu'; - var slu_dependencies = ['typed', 'abs', 'add', 'multiply', 'transpose', 'divideScalar', 'subtract', 'larger', 'largerEq', 'SparseMatrix']; - var createSlu = /* #__PURE__ */Object(factory["a" /* factory */])(slu_name, slu_dependencies, function (_ref) { - var typed = _ref.typed, - abs = _ref.abs, - add = _ref.add, - multiply = _ref.multiply, - transpose = _ref.transpose, - divideScalar = _ref.divideScalar, - subtract = _ref.subtract, - larger = _ref.larger, - largerEq = _ref.largerEq, - SparseMatrix = _ref.SparseMatrix; - var csSqr = createCsSqr({ - add: add, - multiply: multiply, - transpose: transpose - }); - var csLu = createCsLu({ - abs: abs, - divideScalar: divideScalar, - multiply: multiply, - subtract: subtract, - larger: larger, - largerEq: largerEq, - SparseMatrix: SparseMatrix - }); - /** - * Calculate the Sparse Matrix LU decomposition with full pivoting. Sparse Matrix `A` is decomposed in two matrices (`L`, `U`) and two permutation vectors (`pinv`, `q`) where - * - * `P * A * Q = L * U` - * - * Syntax: - * - * math.slu(A, order, threshold) - * - * Examples: - * - * const A = math.sparse([[4,3], [6, 3]]) - * math.slu(A, 1, 0.001) - * // returns: - * // { - * // L: [[1, 0], [1.5, 1]] - * // U: [[4, 3], [0, -1.5]] - * // p: [0, 1] - * // q: [0, 1] - * // } - * - * See also: - * - * lup, lsolve, usolve, lusolve - * - * @param {SparseMatrix} A A two dimensional sparse matrix for which to get the LU decomposition. - * @param {Number} order The Symbolic Ordering and Analysis order: - * 0 - Natural ordering, no permutation vector q is returned - * 1 - Matrix must be square, symbolic ordering and analisis is performed on M = A + A' - * 2 - Symbolic ordering and analisis is performed on M = A' * A. Dense columns from A' are dropped, A recreated from A'. - * This is appropriatefor LU factorization of unsymmetric matrices. - * 3 - Symbolic ordering and analisis is performed on M = A' * A. This is best used for LU factorization is matrix M has no dense rows. - * A dense row is a row with more than 10*sqr(columns) entries. - * @param {Number} threshold Partial pivoting threshold (1 for partial pivoting) - * - * @return {Object} The lower triangular matrix, the upper triangular matrix and the permutation vectors. - */ - - return typed(slu_name, { - 'SparseMatrix, number, number': function SparseMatrixNumberNumber(a, order, threshold) { - // verify order - if (!Object(utils_number["i" /* isInteger */])(order) || order < 0 || order > 3) { - throw new Error('Symbolic Ordering and Analysis order must be an integer number in the interval [0, 3]'); - } // verify threshold - - if (threshold < 0 || threshold > 1) { - throw new Error('Partial pivoting threshold must be a number from 0 to 1'); - } // perform symbolic ordering and analysis - - var s = csSqr(order, a, false); // perform lu decomposition - - var f = csLu(a, s, threshold); // return decomposition - - return { - L: f.L, - U: f.U, - p: f.pinv, - q: s.q, - toString: function toString() { - return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\np: ' + this.p.toString() + (this.q ? '\nq: ' + this.q.toString() : '') + '\n'; - } - }; - } - }); - }); - // CONCATENATED MODULE: ./src/function/algebra/sparse/csIpvec.js - /** - * Permutes a vector; x = P'b. In MATLAB notation, x(p)=b. - * - * @param {Array} p The permutation vector of length n. null value denotes identity - * @param {Array} b The input vector - * - * @return {Array} The output vector x = P'b - */ - function csIpvec(p, b) { - // vars - var k; - var n = b.length; - var x = []; // check permutation vector was provided, p = null denotes identity - - if (p) { - // loop vector - for (k = 0; k < n; k++) { - // apply permutation - x[p[k]] = b[k]; - } - } else { - // loop vector - for (k = 0; k < n; k++) { - // x[i] = b[i] - x[k] = b[k]; - } - } - - return x; - } - // CONCATENATED MODULE: ./src/function/algebra/solver/lusolve.js - - var lusolve_name = 'lusolve'; - var lusolve_dependencies = ['typed', 'matrix', 'lup', 'slu', 'usolve', 'lsolve', 'DenseMatrix']; - var createLusolve = /* #__PURE__ */Object(factory["a" /* factory */])(lusolve_name, lusolve_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - lup = _ref.lup, - slu = _ref.slu, - usolve = _ref.usolve, - lsolve = _ref.lsolve, - DenseMatrix = _ref.DenseMatrix; - var solveValidation = createSolveValidation({ - DenseMatrix: DenseMatrix - }); - /** - * Solves the linear system `A * x = b` where `A` is an [n x n] matrix and `b` is a [n] column vector. - * - * Syntax: - * - * math.lusolve(A, b) // returns column vector with the solution to the linear system A * x = b - * math.lusolve(lup, b) // returns column vector with the solution to the linear system A * x = b, lup = math.lup(A) - * - * Examples: - * - * const m = [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]] - * - * const x = math.lusolve(m, [-1, -1, -1, -1]) // x = [[-1], [-0.5], [-1/3], [-0.25]] - * - * const f = math.lup(m) - * const x1 = math.lusolve(f, [-1, -1, -1, -1]) // x1 = [[-1], [-0.5], [-1/3], [-0.25]] - * const x2 = math.lusolve(f, [1, 2, 1, -1]) // x2 = [[1], [1], [1/3], [-0.25]] - * - * const a = [[-2, 3], [2, 1]] - * const b = [11, 9] - * const x = math.lusolve(a, b) // [[2], [5]] - * - * See also: - * - * lup, slu, lsolve, usolve - * - * @param {Matrix | Array | Object} A Invertible Matrix or the Matrix LU decomposition - * @param {Matrix | Array} b Column Vector - * @param {number} [order] The Symbolic Ordering and Analysis order, see slu for details. Matrix must be a SparseMatrix - * @param {Number} [threshold] Partial pivoting threshold (1 for partial pivoting), see slu for details. Matrix must be a SparseMatrix. - * - * @return {DenseMatrix | Array} Column vector with the solution to the linear system A * x = b - */ - - return typed(lusolve_name, { - 'Array, Array | Matrix': function ArrayArrayMatrix(a, b) { - a = matrix(a); - var d = lup(a); - - var x = _lusolve(d.L, d.U, d.p, null, b); - - return x.valueOf(); - }, - 'DenseMatrix, Array | Matrix': function DenseMatrixArrayMatrix(a, b) { - var d = lup(a); - return _lusolve(d.L, d.U, d.p, null, b); - }, - 'SparseMatrix, Array | Matrix': function SparseMatrixArrayMatrix(a, b) { - var d = lup(a); - return _lusolve(d.L, d.U, d.p, null, b); - }, - 'SparseMatrix, Array | Matrix, number, number': function SparseMatrixArrayMatrixNumberNumber(a, b, order, threshold) { - var d = slu(a, order, threshold); - return _lusolve(d.L, d.U, d.p, d.q, b); - }, - 'Object, Array | Matrix': function ObjectArrayMatrix(d, b) { - return _lusolve(d.L, d.U, d.p, d.q, b); - } - }); - - function _toMatrix(a) { - if (Object(is["v" /* isMatrix */])(a)) { - return a; - } - - if (Object(is["b" /* isArray */])(a)) { - return matrix(a); - } - - throw new TypeError('Invalid Matrix LU decomposition'); - } - - function _lusolve(l, u, p, q, b) { - // verify decomposition - l = _toMatrix(l); - u = _toMatrix(u); // apply row permutations if needed (b is a DenseMatrix) - - if (p) { - b = solveValidation(l, b, true); - b._data = csIpvec(p, b._data); - } // use forward substitution to resolve L * y = b - - var y = lsolve(l, b); // use backward substitution to resolve U * x = y - - var x = usolve(u, y); // apply column permutations if needed (x is a DenseMatrix) - - if (q) { - x._data = csIpvec(q, x._data); - } - - return x; - } - }); - // CONCATENATED MODULE: ./src/expression/Help.js - - var Help_name = 'Help'; - var Help_dependencies = ['parse']; - var createHelpClass = /* #__PURE__ */Object(factory["a" /* factory */])(Help_name, Help_dependencies, function (_ref) { - var parse = _ref.parse; - - /** - * Documentation object - * @param {Object} doc Object containing properties: - * {string} name - * {string} category - * {string} description - * {string[]} syntax - * {string[]} examples - * {string[]} seealso - * @constructor - */ - function Help(doc) { - if (!(this instanceof Help)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - if (!doc) { - throw new Error('Argument "doc" missing'); - } - this.doc = doc; - } - /** - * Attach type information - */ - - Help.prototype.type = 'Help'; - Help.prototype.isHelp = true; - /** - * Generate a string representation of the Help object - * @return {string} Returns a string - * @private - */ - - Help.prototype.toString = function () { - var doc = this.doc || {}; - var desc = '\n'; - - if (doc.name) { - desc += 'Name: ' + doc.name + '\n\n'; - } - - if (doc.category) { - desc += 'Category: ' + doc.category + '\n\n'; - } - - if (doc.description) { - desc += 'Description:\n ' + doc.description + '\n\n'; - } - - if (doc.syntax) { - desc += 'Syntax:\n ' + doc.syntax.join('\n ') + '\n\n'; - } - - if (doc.examples) { - desc += 'Examples:\n'; - var scope = {}; - - for (var i = 0; i < doc.examples.length; i++) { - var expr = doc.examples[i]; - desc += ' ' + expr + '\n'; - var res = void 0; - - try { - // note: res can be undefined when `expr` is an empty string - res = parse(expr).compile().evaluate(scope); - } catch (e) { - res = e; - } - - if (res !== undefined && !Object(is["s" /* isHelp */])(res)) { - desc += ' ' + Object(utils_string["d" /* format */])(res, { - precision: 14 - }) + '\n'; - } - } - - desc += '\n'; - } - - if (doc.seealso && doc.seealso.length) { - desc += 'See also: ' + doc.seealso.join(', ') + '\n'; - } - - return desc; - }; - /** - * Export the help object to JSON - */ - - Help.prototype.toJSON = function () { - var obj = Object(utils_object["a" /* clone */])(this.doc); - obj.mathjs = 'Help'; - return obj; - }; - /** - * Instantiate a Help object from a JSON object - * @param {Object} json - * @returns {Help} Returns a new Help object - */ - - Help.fromJSON = function (json) { - var doc = {}; - Object.keys(json).filter(function (prop) { - return prop !== 'mathjs'; - }).forEach(function (prop) { - doc[prop] = json[prop]; - }); - return new Help(doc); - }; - /** - * Returns a string representation of the Help object - */ - - Help.prototype.valueOf = Help.prototype.toString; - return Help; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/type/chain/Chain.js - - var Chain_name = 'Chain'; - var Chain_dependencies = ['?on', 'math']; - var createChainClass = /* #__PURE__ */Object(factory["a" /* factory */])(Chain_name, Chain_dependencies, function (_ref) { - var on = _ref.on, - math = _ref.math; - - /** - * @constructor Chain - * Wrap any value in a chain, allowing to perform chained operations on - * the value. - * - * All methods available in the math.js library can be called upon the chain, - * and then will be evaluated with the value itself as first argument. - * The chain can be closed by executing chain.done(), which will return - * the final value. - * - * The Chain has a number of special functions: - * - done() Finalize the chained operation and return the - * chain's value. - * - valueOf() The same as done() - * - toString() Returns a string representation of the chain's value. - * - * @param {*} [value] - */ - function Chain(value) { - if (!(this instanceof Chain)) { - throw new SyntaxError('Constructor must be called with the new operator'); - } - - if (Object(is["h" /* isChain */])(value)) { - this.value = value.value; - } else { - this.value = value; - } - } - /** - * Attach type information - */ - - Chain.prototype.type = 'Chain'; - Chain.prototype.isChain = true; - /** - * Close the chain. Returns the final value. - * Does the same as method valueOf() - * @returns {*} value - */ - - Chain.prototype.done = function () { - return this.value; - }; - /** - * Close the chain. Returns the final value. - * Does the same as method done() - * @returns {*} value - */ - - Chain.prototype.valueOf = function () { - return this.value; - }; - /** - * Get a string representation of the value in the chain - * @returns {string} - */ - - Chain.prototype.toString = function () { - return Object(utils_string["d" /* format */])(this.value); - }; - /** - * Get a JSON representation of the chain - * @returns {Object} - */ - - Chain.prototype.toJSON = function () { - return { - mathjs: 'Chain', - value: this.value - }; - }; - /** - * Instantiate a Chain from its JSON representation - * @param {Object} json An object structured like - * `{"mathjs": "Chain", value: ...}`, - * where mathjs is optional - * @returns {Chain} - */ - - Chain.fromJSON = function (json) { - return new Chain(json.value); - }; - /** - * Create a proxy method for the chain - * @param {string} name - * @param {Function} fn The function to be proxied - * If fn is no function, it is silently ignored. - * @private - */ - - function createProxy(name, fn) { - if (typeof fn === 'function') { - Chain.prototype[name] = chainify(fn); - } - } - /** - * Create a proxy method for the chain - * @param {string} name - * @param {function} resolver The function resolving with the - * function to be proxied - * @private - */ - - function createLazyProxy(name, resolver) { - Object(utils_object["h" /* lazy */])(Chain.prototype, name, function outerResolver() { - var fn = resolver(); - - if (typeof fn === 'function') { - return chainify(fn); - } - - return undefined; // if not a function, ignore - }); - } - /** - * Make a function chainable - * @param {function} fn - * @return {Function} chain function - * @private - */ - - function chainify(fn) { - return function () { - var args = [this.value]; // `this` will be the context of a Chain instance - - for (var i = 0; i < arguments.length; i++) { - args[i + 1] = arguments[i]; - } - - return new Chain(fn.apply(fn, args)); - }; - } - /** - * Create a proxy for a single method, or an object with multiple methods. - * Example usage: - * - * Chain.createProxy('add', function add (x, y) {...}) - * Chain.createProxy({ - * add: function add (x, y) {...}, - * subtract: function subtract (x, y) {...} - * } - * - * @param {string | Object} arg0 A name (string), or an object with - * functions - * @param {*} [arg1] A function, when arg0 is a name - */ - - Chain.createProxy = function (arg0, arg1) { - if (typeof arg0 === 'string') { - // createProxy(name, value) - createProxy(arg0, arg1); - } else { - var _loop = function _loop(_name) { - if (Object(utils_object["f" /* hasOwnProperty */])(arg0, _name) && excludedNames[_name] === undefined) { - createLazyProxy(_name, function () { - return arg0[_name]; - }); - } - }; - - // createProxy(values) - for (var _name in arg0) { - _loop(_name); - } - } - }; - - var excludedNames = { - expression: true, - docs: true, - type: true, - classes: true, - json: true, - error: true, - isChain: true // conflicts with the property isChain of a Chain instance - - }; // create proxy for everything that is in math.js - - Chain.createProxy(math); // register on the import event, automatically add a proxy for every imported function. - - if (on) { - on('import', function (name, resolver, path) { - if (!path) { - // an imported function (not a data type or something special) - createLazyProxy(name, resolver); - } - }); - } - - return Chain; - }, { - isClass: true - }); - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/bignumber.js - var bignumberDocs = { - name: 'bignumber', - category: 'Construction', - syntax: ['bignumber(x)'], - description: 'Create a big number from a number or string.', - examples: ['0.1 + 0.2', 'bignumber(0.1) + bignumber(0.2)', 'bignumber("7.2")', 'bignumber("7.2e500")', 'bignumber([0.1, 0.2, 0.3])'], - seealso: ['boolean', 'complex', 'fraction', 'index', 'matrix', 'string', 'unit'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/typeOf.js - var typeOfDocs = { - name: 'typeOf', - category: 'Utils', - syntax: ['typeOf(x)'], - description: 'Get the type of a variable.', - examples: ['typeOf(3.5)', 'typeOf(2 - 4i)', 'typeOf(45 deg)', 'typeOf("hello world")'], - seealso: ['getMatrixDataType'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/isZero.js - var isZeroDocs = { - name: 'isZero', - category: 'Utils', - syntax: ['isZero(x)'], - description: 'Test whether a value is zero.', - examples: ['isZero(2)', 'isZero(0)', 'isZero(-4)', 'isZero([3, 0, -2, 0])'], - seealso: ['isInteger', 'isNumeric', 'isNegative', 'isPositive'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/isPrime.js - var isPrimeDocs = { - name: 'isPrime', - category: 'Utils', - syntax: ['isPrime(x)'], - description: 'Test whether a value is prime: has no divisors other than itself and one.', - examples: ['isPrime(3)', 'isPrime(-2)', 'isPrime([2, 17, 100])'], - seealso: ['isInteger', 'isNumeric', 'isNegative', 'isZero'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/isPositive.js - var isPositiveDocs = { - name: 'isPositive', - category: 'Utils', - syntax: ['isPositive(x)'], - description: 'Test whether a value is positive: larger than zero.', - examples: ['isPositive(2)', 'isPositive(0)', 'isPositive(-4)', 'isPositive([3, 0.5, -2])'], - seealso: ['isInteger', 'isNumeric', 'isNegative', 'isZero'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/isNumeric.js - var isNumericDocs = { - name: 'isNumeric', - category: 'Utils', - syntax: ['isNumeric(x)'], - description: 'Test whether a value is a numeric value. ' + 'Returns true when the input is a number, BigNumber, Fraction, or boolean.', - examples: ['isNumeric(2)', 'isNumeric("2")', 'hasNumericValue("2")', 'isNumeric(0)', 'isNumeric(bignumber(500))', 'isNumeric(fraction(0.125))', 'isNumeric(2 + 3i)', 'isNumeric([2.3, "foo", false])'], - seealso: ['isInteger', 'isZero', 'isNegative', 'isPositive', 'isNaN', 'hasNumericValue'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/hasNumericValue.js - var hasNumericValueDocs = { - name: 'hasNumericValue', - category: 'Utils', - syntax: ['hasNumericValue(x)'], - description: 'Test whether a value is an numeric value. ' + 'In case of a string, true is returned if the string contains a numeric value.', - examples: ['hasNumericValue(2)', 'hasNumericValue("2")', 'isNumeric("2")', 'hasNumericValue(0)', 'hasNumericValue(bignumber(500))', 'hasNumericValue(fraction(0.125))', 'hasNumericValue(2 + 3i)', 'hasNumericValue([2.3, "foo", false])'], - seealso: ['isInteger', 'isZero', 'isNegative', 'isPositive', 'isNaN', 'isNumeric'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/isNegative.js - var isNegativeDocs = { - name: 'isNegative', - category: 'Utils', - syntax: ['isNegative(x)'], - description: 'Test whether a value is negative: smaller than zero.', - examples: ['isNegative(2)', 'isNegative(0)', 'isNegative(-4)', 'isNegative([3, 0.5, -2])'], - seealso: ['isInteger', 'isNumeric', 'isPositive', 'isZero'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/isInteger.js - var isIntegerDocs = { - name: 'isInteger', - category: 'Utils', - syntax: ['isInteger(x)'], - description: 'Test whether a value is an integer number.', - examples: ['isInteger(2)', 'isInteger(3.5)', 'isInteger([3, 0.5, -2])'], - seealso: ['isNegative', 'isNumeric', 'isPositive', 'isZero'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/isNaN.js - var isNaNDocs = { - name: 'isNaN', - category: 'Utils', - syntax: ['isNaN(x)'], - description: 'Test whether a value is NaN (not a number)', - examples: ['isNaN(2)', 'isNaN(0 / 0)', 'isNaN(NaN)', 'isNaN(Infinity)'], - seealso: ['isNegative', 'isNumeric', 'isPositive', 'isZero'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/format.js - var formatDocs = { - name: 'format', - category: 'Utils', - syntax: ['format(value)', 'format(value, precision)'], - description: 'Format a value of any type as string.', - examples: ['format(2.3)', 'format(3 - 4i)', 'format([])', 'format(pi, 3)'], - seealso: ['print'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/bin.js - var binDocs = { - name: 'bin', - category: 'Utils', - syntax: ['bin(value)'], - description: 'Format a number as binary', - examples: ['bin(2)'], - seealso: ['oct', 'hex'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/oct.js - var octDocs = { - name: 'oct', - category: 'Utils', - syntax: ['oct(value)'], - description: 'Format a number as octal', - examples: ['oct(56)'], - seealso: ['bin', 'hex'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/hex.js - var hexDocs = { - name: 'hex', - category: 'Utils', - syntax: ['hex(value)'], - description: 'Format a number as hexadecimal', - examples: ['hex(240)'], - seealso: ['bin', 'oct'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/clone.js - var cloneDocs = { - name: 'clone', - category: 'Utils', - syntax: ['clone(x)'], - description: 'Clone a variable. Creates a copy of primitive variables,and a deep copy of matrices', - examples: ['clone(3.5)', 'clone(2 - 4i)', 'clone(45 deg)', 'clone([1, 2; 3, 4])', 'clone("hello world")'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/units/to.js - var toDocs = { - name: 'to', - category: 'Units', - syntax: ['x to unit', 'to(x, unit)'], - description: 'Change the unit of a value.', - examples: ['5 inch to cm', '3.2kg to g', '16 bytes in bits'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/tanh.js - var tanhDocs = { - name: 'tanh', - category: 'Trigonometry', - syntax: ['tanh(x)'], - description: 'Compute the hyperbolic tangent of x in radians.', - examples: ['tanh(0.5)', 'sinh(0.5) / cosh(0.5)'], - seealso: ['sinh', 'cosh'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/tan.js - var tanDocs = { - name: 'tan', - category: 'Trigonometry', - syntax: ['tan(x)'], - description: 'Compute the tangent of x in radians.', - examples: ['tan(0.5)', 'sin(0.5) / cos(0.5)', 'tan(pi / 4)', 'tan(45 deg)'], - seealso: ['atan', 'sin', 'cos'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/sinh.js - var sinhDocs = { - name: 'sinh', - category: 'Trigonometry', - syntax: ['sinh(x)'], - description: 'Compute the hyperbolic sine of x in radians.', - examples: ['sinh(0.5)'], - seealso: ['cosh', 'tanh'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/sech.js - var sechDocs = { - name: 'sech', - category: 'Trigonometry', - syntax: ['sech(x)'], - description: 'Compute the hyperbolic secant of x in radians. Defined as 1/cosh(x)', - examples: ['sech(2)', '1 / cosh(2)'], - seealso: ['coth', 'csch', 'cosh'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/sec.js - var secDocs = { - name: 'sec', - category: 'Trigonometry', - syntax: ['sec(x)'], - description: 'Compute the secant of x in radians. Defined as 1/cos(x)', - examples: ['sec(2)', '1 / cos(2)'], - seealso: ['cot', 'csc', 'cos'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/csch.js - var cschDocs = { - name: 'csch', - category: 'Trigonometry', - syntax: ['csch(x)'], - description: 'Compute the hyperbolic cosecant of x in radians. Defined as 1/sinh(x)', - examples: ['csch(2)', '1 / sinh(2)'], - seealso: ['sech', 'coth', 'sinh'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/csc.js - var cscDocs = { - name: 'csc', - category: 'Trigonometry', - syntax: ['csc(x)'], - description: 'Compute the cosecant of x in radians. Defined as 1/sin(x)', - examples: ['csc(2)', '1 / sin(2)'], - seealso: ['sec', 'cot', 'sin'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/coth.js - var cothDocs = { - name: 'coth', - category: 'Trigonometry', - syntax: ['coth(x)'], - description: 'Compute the hyperbolic cotangent of x in radians.', - examples: ['coth(2)', '1 / tanh(2)'], - seealso: ['sech', 'csch', 'tanh'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/cot.js - var cotDocs = { - name: 'cot', - category: 'Trigonometry', - syntax: ['cot(x)'], - description: 'Compute the cotangent of x in radians. Defined as 1/tan(x)', - examples: ['cot(2)', '1 / tan(2)'], - seealso: ['sec', 'csc', 'tan'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/cosh.js - var coshDocs = { - name: 'cosh', - category: 'Trigonometry', - syntax: ['cosh(x)'], - description: 'Compute the hyperbolic cosine of x in radians.', - examples: ['cosh(0.5)'], - seealso: ['sinh', 'tanh', 'coth'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/cos.js - var cosDocs = { - name: 'cos', - category: 'Trigonometry', - syntax: ['cos(x)'], - description: 'Compute the cosine of x in radians.', - examples: ['cos(2)', 'cos(pi / 4) ^ 2', 'cos(180 deg)', 'cos(60 deg)', 'sin(0.2)^2 + cos(0.2)^2'], - seealso: ['acos', 'sin', 'tan'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/atan2.js - var atan2Docs = { - name: 'atan2', - category: 'Trigonometry', - syntax: ['atan2(y, x)'], - description: 'Computes the principal value of the arc tangent of y/x in radians.', - examples: ['atan2(2, 2) / pi', 'angle = 60 deg in rad', 'x = cos(angle)', 'y = sin(angle)', 'atan2(y, x)'], - seealso: ['sin', 'cos', 'tan'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/atanh.js - var atanhDocs = { - name: 'atanh', - category: 'Trigonometry', - syntax: ['atanh(x)'], - description: 'Calculate the hyperbolic arctangent of a value, defined as `atanh(x) = ln((1 + x)/(1 - x)) / 2`.', - examples: ['atanh(0.5)'], - seealso: ['acosh', 'asinh'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/atan.js - var atanDocs = { - name: 'atan', - category: 'Trigonometry', - syntax: ['atan(x)'], - description: 'Compute the inverse tangent of a value in radians.', - examples: ['atan(0.5)', 'atan(tan(0.5))'], - seealso: ['tan', 'acos', 'asin'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/asinh.js - var asinhDocs = { - name: 'asinh', - category: 'Trigonometry', - syntax: ['asinh(x)'], - description: 'Calculate the hyperbolic arcsine of a value, defined as `asinh(x) = ln(x + sqrt(x^2 + 1))`.', - examples: ['asinh(0.5)'], - seealso: ['acosh', 'atanh'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/asin.js - var asinDocs = { - name: 'asin', - category: 'Trigonometry', - syntax: ['asin(x)'], - description: 'Compute the inverse sine of a value in radians.', - examples: ['asin(0.5)', 'asin(sin(0.5))'], - seealso: ['sin', 'acos', 'atan'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/asech.js - var asechDocs = { - name: 'asech', - category: 'Trigonometry', - syntax: ['asech(x)'], - description: 'Calculate the inverse secant of a value.', - examples: ['asech(0.5)'], - seealso: ['acsch', 'acoth'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/asec.js - var asecDocs = { - name: 'asec', - category: 'Trigonometry', - syntax: ['asec(x)'], - description: 'Calculate the inverse secant of a value.', - examples: ['asec(0.5)', 'asec(sec(0.5))', 'asec(2)'], - seealso: ['acos', 'acot', 'acsc'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/acsch.js - var acschDocs = { - name: 'acsch', - category: 'Trigonometry', - syntax: ['acsch(x)'], - description: 'Calculate the hyperbolic arccosecant of a value, defined as `acsch(x) = ln(1/x + sqrt(1/x^2 + 1))`.', - examples: ['acsch(0.5)'], - seealso: ['asech', 'acoth'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/acsc.js - var acscDocs = { - name: 'acsc', - category: 'Trigonometry', - syntax: ['acsc(x)'], - description: 'Calculate the inverse cotangent of a value.', - examples: ['acsc(2)', 'acsc(csc(0.5))', 'acsc(0.5)'], - seealso: ['csc', 'asin', 'asec'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/acoth.js - var acothDocs = { - name: 'acoth', - category: 'Trigonometry', - syntax: ['acoth(x)'], - description: 'Calculate the hyperbolic arccotangent of a value, defined as `acoth(x) = (ln((x+1)/x) + ln(x/(x-1))) / 2`.', - examples: ['acoth(2)', 'acoth(0.5)'], - seealso: ['acsch', 'asech'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/acot.js - var acotDocs = { - name: 'acot', - category: 'Trigonometry', - syntax: ['acot(x)'], - description: 'Calculate the inverse cotangent of a value.', - examples: ['acot(0.5)', 'acot(cot(0.5))', 'acot(2)'], - seealso: ['cot', 'atan'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/acosh.js - var acoshDocs = { - name: 'acosh', - category: 'Trigonometry', - syntax: ['acosh(x)'], - description: 'Calculate the hyperbolic arccos of a value, defined as `acosh(x) = ln(sqrt(x^2 - 1) + x)`.', - examples: ['acosh(1.5)'], - seealso: ['cosh', 'asinh', 'atanh'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/acos.js - var acosDocs = { - name: 'acos', - category: 'Trigonometry', - syntax: ['acos(x)'], - description: 'Compute the inverse cosine of a value in radians.', - examples: ['acos(0.5)', 'acos(cos(2.3))'], - seealso: ['cos', 'atan', 'asin'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/sum.js - var sumDocs = { - name: 'sum', - category: 'Statistics', - syntax: ['sum(a, b, c, ...)', 'sum(A)'], - description: 'Compute the sum of all values.', - examples: ['sum(2, 3, 4, 1)', 'sum([2, 3, 4, 1])', 'sum([2, 5; 4, 3])'], - seealso: ['max', 'mean', 'median', 'min', 'prod', 'std', 'sum', 'variance'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/std.js - var stdDocs = { - name: 'std', - category: 'Statistics', - syntax: ['std(a, b, c, ...)', 'std(A)', 'std(A, normalization)'], - description: 'Compute the standard deviation of all values, defined as std(A) = sqrt(variance(A)). Optional parameter normalization can be "unbiased" (default), "uncorrected", or "biased".', - examples: ['std(2, 4, 6)', 'std([2, 4, 6, 8])', 'std([2, 4, 6, 8], "uncorrected")', 'std([2, 4, 6, 8], "biased")', 'std([1, 2, 3; 4, 5, 6])'], - seealso: ['max', 'mean', 'min', 'median', 'prod', 'sum', 'variance'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/quantileSeq.js - var quantileSeqDocs = { - name: 'quantileSeq', - category: 'Statistics', - syntax: ['quantileSeq(A, prob[, sorted])', 'quantileSeq(A, [prob1, prob2, ...][, sorted])', 'quantileSeq(A, N[, sorted])'], - description: 'Compute the prob order quantile of a matrix or a list with values. The sequence is sorted and the middle value is returned. Supported types of sequence values are: Number, BigNumber, Unit Supported types of probablity are: Number, BigNumber. \n\nIn case of a (multi dimensional) array or matrix, the prob order quantile of all elements will be calculated.', - examples: ['quantileSeq([3, -1, 5, 7], 0.5)', 'quantileSeq([3, -1, 5, 7], [1/3, 2/3])', 'quantileSeq([3, -1, 5, 7], 2)', 'quantileSeq([-1, 3, 5, 7], 0.5, true)'], - seealso: ['mean', 'median', 'min', 'max', 'prod', 'std', 'sum', 'variance'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/prod.js - var prodDocs = { - name: 'prod', - category: 'Statistics', - syntax: ['prod(a, b, c, ...)', 'prod(A)'], - description: 'Compute the product of all values.', - examples: ['prod(2, 3, 4)', 'prod([2, 3, 4])', 'prod([2, 5; 4, 3])'], - seealso: ['max', 'mean', 'min', 'median', 'min', 'std', 'sum', 'variance'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/mode.js - var modeDocs = { - name: 'mode', - category: 'Statistics', - syntax: ['mode(a, b, c, ...)', 'mode(A)', 'mode(A, a, b, B, c, ...)'], - description: 'Computes the mode of all values as an array. In case mode being more than one, multiple values are returned in an array.', - examples: ['mode(2, 1, 4, 3, 1)', 'mode([1, 2.7, 3.2, 4, 2.7])', 'mode(1, 4, 6, 1, 6)'], - seealso: ['max', 'mean', 'min', 'median', 'prod', 'std', 'sum', 'variance'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/min.js - var minDocs = { - name: 'min', - category: 'Statistics', - syntax: ['min(a, b, c, ...)', 'min(A)', 'min(A, dim)'], - description: 'Compute the minimum value of a list of values.', - examples: ['min(2, 3, 4, 1)', 'min([2, 3, 4, 1])', 'min([2, 5; 4, 3])', 'min([2, 5; 4, 3], 1)', 'min([2, 5; 4, 3], 2)', 'min(2.7, 7.1, -4.5, 2.0, 4.1)', 'max(2.7, 7.1, -4.5, 2.0, 4.1)'], - seealso: ['max', 'mean', 'median', 'prod', 'std', 'sum', 'variance'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/median.js - var medianDocs = { - name: 'median', - category: 'Statistics', - syntax: ['median(a, b, c, ...)', 'median(A)'], - description: 'Compute the median of all values. The values are sorted and the middle value is returned. In case of an even number of values, the average of the two middle values is returned.', - examples: ['median(5, 2, 7)', 'median([3, -1, 5, 7])'], - seealso: ['max', 'mean', 'min', 'prod', 'std', 'sum', 'variance', 'quantileSeq'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/mean.js - var meanDocs = { - name: 'mean', - category: 'Statistics', - syntax: ['mean(a, b, c, ...)', 'mean(A)', 'mean(A, dim)'], - description: 'Compute the arithmetic mean of a list of values.', - examples: ['mean(2, 3, 4, 1)', 'mean([2, 3, 4, 1])', 'mean([2, 5; 4, 3])', 'mean([2, 5; 4, 3], 1)', 'mean([2, 5; 4, 3], 2)', 'mean([1.0, 2.7, 3.2, 4.0])'], - seealso: ['max', 'median', 'min', 'prod', 'std', 'sum', 'variance'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/max.js - var maxDocs = { - name: 'max', - category: 'Statistics', - syntax: ['max(a, b, c, ...)', 'max(A)', 'max(A, dim)'], - description: 'Compute the maximum value of a list of values.', - examples: ['max(2, 3, 4, 1)', 'max([2, 3, 4, 1])', 'max([2, 5; 4, 3])', 'max([2, 5; 4, 3], 1)', 'max([2, 5; 4, 3], 2)', 'max(2.7, 7.1, -4.5, 2.0, 4.1)', 'min(2.7, 7.1, -4.5, 2.0, 4.1)'], - seealso: ['mean', 'median', 'min', 'prod', 'std', 'sum', 'variance'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/mad.js - var madDocs = { - name: 'mad', - category: 'Statistics', - syntax: ['mad(a, b, c, ...)', 'mad(A)'], - description: 'Compute the median absolute deviation of a matrix or a list with values. The median absolute deviation is defined as the median of the absolute deviations from the median.', - examples: ['mad(10, 20, 30)', 'mad([1, 2, 3])'], - seealso: ['mean', 'median', 'std', 'abs'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/special/erf.js - var erfDocs = { - name: 'erf', - category: 'Special', - syntax: ['erf(x)'], - description: 'Compute the erf function of a value using a rational Chebyshev approximations for different intervals of x', - examples: ['erf(0.2)', 'erf(-0.5)', 'erf(4)'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/set/setUnion.js - var setUnionDocs = { - name: 'setUnion', - category: 'Set', - syntax: ['setUnion(set1, set2)'], - description: 'Create the union of two (multi)sets. Multi-dimension arrays will be converted to single-dimension arrays before the operation.', - examples: ['setUnion([1, 2, 3, 4], [3, 4, 5, 6])', 'setUnion([[1, 2], [3, 4]], [[3, 4], [5, 6]])'], - seealso: ['setIntersect', 'setDifference'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/set/setSymDifference.js - var setSymDifferenceDocs = { - name: 'setSymDifference', - category: 'Set', - syntax: ['setSymDifference(set1, set2)'], - description: 'Create the symmetric difference of two (multi)sets. Multi-dimension arrays will be converted to single-dimension arrays before the operation.', - examples: ['setSymDifference([1, 2, 3, 4], [3, 4, 5, 6])', 'setSymDifference([[1, 2], [3, 4]], [[3, 4], [5, 6]])'], - seealso: ['setUnion', 'setIntersect', 'setDifference'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/set/setSize.js - var setSizeDocs = { - name: 'setSize', - category: 'Set', - syntax: ['setSize(set)', 'setSize(set, unique)'], - description: 'Count the number of elements of a (multi)set. When the second parameter "unique" is true, count only the unique values. A multi-dimension array will be converted to a single-dimension array before the operation.', - examples: ['setSize([1, 2, 2, 4])', 'setSize([1, 2, 2, 4], true)'], - seealso: ['setUnion', 'setIntersect', 'setDifference'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/set/setPowerset.js - var setPowersetDocs = { - name: 'setPowerset', - category: 'Set', - syntax: ['setPowerset(set)'], - description: 'Create the powerset of a (multi)set: the powerset contains very possible subsets of a (multi)set. A multi-dimension array will be converted to a single-dimension array before the operation.', - examples: ['setPowerset([1, 2, 3])'], - seealso: ['setCartesian'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/set/setMultiplicity.js - var setMultiplicityDocs = { - name: 'setMultiplicity', - category: 'Set', - syntax: ['setMultiplicity(element, set)'], - description: 'Count the multiplicity of an element in a multiset. A multi-dimension array will be converted to a single-dimension array before the operation.', - examples: ['setMultiplicity(1, [1, 2, 2, 4])', 'setMultiplicity(2, [1, 2, 2, 4])'], - seealso: ['setDistinct', 'setSize'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/set/setIsSubset.js - var setIsSubsetDocs = { - name: 'setIsSubset', - category: 'Set', - syntax: ['setIsSubset(set1, set2)'], - description: 'Check whether a (multi)set is a subset of another (multi)set: every element of set1 is the element of set2. Multi-dimension arrays will be converted to single-dimension arrays before the operation.', - examples: ['setIsSubset([1, 2], [3, 4, 5, 6])', 'setIsSubset([3, 4], [3, 4, 5, 6])'], - seealso: ['setUnion', 'setIntersect', 'setDifference'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/set/setIntersect.js - var setIntersectDocs = { - name: 'setIntersect', - category: 'Set', - syntax: ['setIntersect(set1, set2)'], - description: 'Create the intersection of two (multi)sets. Multi-dimension arrays will be converted to single-dimension arrays before the operation.', - examples: ['setIntersect([1, 2, 3, 4], [3, 4, 5, 6])', 'setIntersect([[1, 2], [3, 4]], [[3, 4], [5, 6]])'], - seealso: ['setUnion', 'setDifference'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/set/setDistinct.js - var setDistinctDocs = { - name: 'setDistinct', - category: 'Set', - syntax: ['setDistinct(set)'], - description: 'Collect the distinct elements of a multiset. A multi-dimension array will be converted to a single-dimension array before the operation.', - examples: ['setDistinct([1, 1, 1, 2, 2, 3])'], - seealso: ['setMultiplicity'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/set/setDifference.js - var setDifferenceDocs = { - name: 'setDifference', - category: 'Set', - syntax: ['setDifference(set1, set2)'], - description: 'Create the difference of two (multi)sets: every element of set1, that is not the element of set2. Multi-dimension arrays will be converted to single-dimension arrays before the operation.', - examples: ['setDifference([1, 2, 3, 4], [3, 4, 5, 6])', 'setDifference([[1, 2], [3, 4]], [[3, 4], [5, 6]])'], - seealso: ['setUnion', 'setIntersect', 'setSymDifference'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/set/setCartesian.js - var setCartesianDocs = { - name: 'setCartesian', - category: 'Set', - syntax: ['setCartesian(set1, set2)'], - description: 'Create the cartesian product of two (multi)sets. Multi-dimension arrays will be converted to single-dimension arrays before the operation.', - examples: ['setCartesian([1, 2], [3, 4])'], - seealso: ['setUnion', 'setIntersect', 'setDifference', 'setPowerset'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/unequal.js - var unequalDocs = { - name: 'unequal', - category: 'Relational', - syntax: ['x != y', 'unequal(x, y)'], - description: 'Check unequality of two values. Returns true if the values are unequal, and false if they are equal.', - examples: ['2+2 != 3', '2+2 != 4', 'a = 3.2', 'b = 6-2.8', 'a != b', '50cm != 0.5m', '5 cm != 2 inch'], - seealso: ['equal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare', 'deepEqual'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/smallerEq.js - var smallerEqDocs = { - name: 'smallerEq', - category: 'Relational', - syntax: ['x <= y', 'smallerEq(x, y)'], - description: 'Check if value x is smaller or equal to value y. Returns true if x is smaller than y, and false if not.', - examples: ['2 <= 1+1', '2 < 1+1', 'a = 3.2', 'b = 6-2.8', '(a <= b)'], - seealso: ['equal', 'unequal', 'larger', 'smaller', 'largerEq', 'compare'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/smaller.js - var smallerDocs = { - name: 'smaller', - category: 'Relational', - syntax: ['x < y', 'smaller(x, y)'], - description: 'Check if value x is smaller than value y. Returns true if x is smaller than y, and false if not.', - examples: ['2 < 3', '5 < 2*2', 'a = 3.3', 'b = 6-2.8', '(a < b)', '5 cm < 2 inch'], - seealso: ['equal', 'unequal', 'larger', 'smallerEq', 'largerEq', 'compare'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/largerEq.js - var largerEqDocs = { - name: 'largerEq', - category: 'Relational', - syntax: ['x >= y', 'largerEq(x, y)'], - description: 'Check if value x is larger or equal to y. Returns true if x is larger or equal to y, and false if not.', - examples: ['2 >= 1+1', '2 > 1+1', 'a = 3.2', 'b = 6-2.8', '(a >= b)'], - seealso: ['equal', 'unequal', 'smallerEq', 'smaller', 'compare'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/larger.js - var largerDocs = { - name: 'larger', - category: 'Relational', - syntax: ['x > y', 'larger(x, y)'], - description: 'Check if value x is larger than y. Returns true if x is larger than y, and false if not.', - examples: ['2 > 3', '5 > 2*2', 'a = 3.3', 'b = 6-2.8', '(a > b)', '(b < a)', '5 cm > 2 inch'], - seealso: ['equal', 'unequal', 'smaller', 'smallerEq', 'largerEq', 'compare'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/equalText.js - var equalTextDocs = { - name: 'equalText', - category: 'Relational', - syntax: ['equalText(x, y)'], - description: 'Check equality of two strings. Comparison is case sensitive. Returns true if the values are equal, and false if not.', - examples: ['equalText("Hello", "Hello")', 'equalText("a", "A")', 'equal("2e3", "2000")', 'equalText("2e3", "2000")', 'equalText("B", ["A", "B", "C"])'], - seealso: ['compare', 'compareNatural', 'compareText', 'equal'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/equal.js - var equalDocs = { - name: 'equal', - category: 'Relational', - syntax: ['x == y', 'equal(x, y)'], - description: 'Check equality of two values. Returns true if the values are equal, and false if not.', - examples: ['2+2 == 3', '2+2 == 4', 'a = 3.2', 'b = 6-2.8', 'a == b', '50cm == 0.5m'], - seealso: ['unequal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare', 'deepEqual', 'equalText'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/deepEqual.js - var deepEqualDocs = { - name: 'deepEqual', - category: 'Relational', - syntax: ['deepEqual(x, y)'], - description: 'Check equality of two matrices element wise. Returns true if the size of both matrices is equal and when and each of the elements are equal.', - examples: ['deepEqual([1,3,4], [1,3,4])', 'deepEqual([1,3,4], [1,3])'], - seealso: ['equal', 'unequal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/compareText.js - var compareTextDocs = { - name: 'compareText', - category: 'Relational', - syntax: ['compareText(x, y)'], - description: 'Compare two strings lexically. Comparison is case sensitive. ' + 'Returns 1 when x > y, -1 when x < y, and 0 when x == y.', - examples: ['compareText("B", "A")', 'compareText("A", "B")', 'compareText("A", "A")', 'compareText("2", "10")', 'compare("2", "10")', 'compare(2, 10)', 'compareNatural("2", "10")', 'compareText("B", ["A", "B", "C"])'], - seealso: ['compare', 'compareNatural'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/compareNatural.js - var compareNaturalDocs = { - name: 'compareNatural', - category: 'Relational', - syntax: ['compareNatural(x, y)'], - description: 'Compare two values of any type in a deterministic, natural way. ' + 'Returns 1 when x > y, -1 when x < y, and 0 when x == y.', - examples: ['compareNatural(2, 3)', 'compareNatural(3, 2)', 'compareNatural(2, 2)', 'compareNatural(5cm, 40mm)', 'compareNatural("2", "10")', 'compareNatural(2 + 3i, 2 + 4i)', 'compareNatural([1, 2, 4], [1, 2, 3])', 'compareNatural([1, 5], [1, 2, 3])', 'compareNatural([1, 2], [1, 2])', 'compareNatural({a: 2}, {a: 4})'], - seealso: ['equal', 'unequal', 'smaller', 'smallerEq', 'largerEq', 'compare', 'compareText'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/relational/compare.js - var compareDocs = { - name: 'compare', - category: 'Relational', - syntax: ['compare(x, y)'], - description: 'Compare two values. ' + 'Returns 1 when x > y, -1 when x < y, and 0 when x == y.', - examples: ['compare(2, 3)', 'compare(3, 2)', 'compare(2, 2)', 'compare(5cm, 40mm)', 'compare(2, [1, 2, 3])'], - seealso: ['equal', 'unequal', 'smaller', 'smallerEq', 'largerEq', 'compareNatural', 'compareText'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/probability/randomInt.js - var randomIntDocs = { - name: 'randomInt', - category: 'Probability', - syntax: ['randomInt(max)', 'randomInt(min, max)', 'randomInt(size)', 'randomInt(size, max)', 'randomInt(size, min, max)'], - description: 'Return a random integer number', - examples: ['randomInt(10, 20)', 'randomInt([2, 3], 10)'], - seealso: ['pickRandom', 'random'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/probability/random.js - var randomDocs = { - name: 'random', - category: 'Probability', - syntax: ['random()', 'random(max)', 'random(min, max)', 'random(size)', 'random(size, max)', 'random(size, min, max)'], - description: 'Return a random number.', - examples: ['random()', 'random(10, 20)', 'random([2, 3])'], - seealso: ['pickRandom', 'randomInt'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/probability/pickRandom.js - var pickRandomDocs = { - name: 'pickRandom', - category: 'Probability', - syntax: ['pickRandom(array)', 'pickRandom(array, number)', 'pickRandom(array, weights)', 'pickRandom(array, number, weights)', 'pickRandom(array, weights, number)'], - description: 'Pick a random entry from a given array.', - examples: ['pickRandom(0:10)', 'pickRandom([1, 3, 1, 6])', 'pickRandom([1, 3, 1, 6], 2)', 'pickRandom([1, 3, 1, 6], [2, 3, 2, 1])', 'pickRandom([1, 3, 1, 6], 2, [2, 3, 2, 1])', 'pickRandom([1, 3, 1, 6], [2, 3, 2, 1], 2)'], - seealso: ['random', 'randomInt'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/probability/permutations.js - var permutationsDocs = { - name: 'permutations', - category: 'Probability', - syntax: ['permutations(n)', 'permutations(n, k)'], - description: 'Compute the number of permutations of n items taken k at a time', - examples: ['permutations(5)', 'permutations(5, 3)'], - seealso: ['combinations', 'combinationsWithRep', 'factorial'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/probability/multinomial.js - var multinomialDocs = { - name: 'multinomial', - category: 'Probability', - syntax: ['multinomial(A)'], - description: 'Multinomial Coefficients compute the number of ways of picking a1, a2, ..., ai unordered outcomes from `n` possibilities. multinomial takes one array of integers as an argument. The following condition must be enforced: every ai > 0.', - examples: ['multinomial([1, 2, 1])'], - seealso: ['combinations', 'factorial'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/probability/kldivergence.js - var kldivergenceDocs = { - name: 'kldivergence', - category: 'Probability', - syntax: ['kldivergence(x, y)'], - description: 'Calculate the Kullback-Leibler (KL) divergence between two distributions.', - examples: ['kldivergence([0.7,0.5,0.4], [0.2,0.9,0.5])'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/probability/gamma.js - var gammaDocs = { - name: 'gamma', - category: 'Probability', - syntax: ['gamma(n)'], - description: 'Compute the gamma function. For small values, the Lanczos approximation is used, and for large values the extended Stirling approximation.', - examples: ['gamma(4)', '3!', 'gamma(1/2)', 'sqrt(pi)'], - seealso: ['factorial'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/probability/factorial.js - var factorialDocs = { - name: 'factorial', - category: 'Probability', - syntax: ['n!', 'factorial(n)'], - description: 'Compute the factorial of a value', - examples: ['5!', '5 * 4 * 3 * 2 * 1', '3!'], - seealso: ['combinations', 'combinationsWithRep', 'permutations', 'gamma'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/probability/combinations.js - var combinationsDocs = { - name: 'combinations', - category: 'Probability', - syntax: ['combinations(n, k)'], - description: 'Compute the number of combinations of n items taken k at a time', - examples: ['combinations(7, 5)'], - seealso: ['combinationsWithRep', 'permutations', 'factorial'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/probability/combinationsWithRep.js - var combinationsWithRepDocs = { - name: 'combinationsWithRep', - category: 'Probability', - syntax: ['combinationsWithRep(n, k)'], - description: 'Compute the number of combinations of n items taken k at a time with replacements.', - examples: ['combinationsWithRep(7, 5)'], - seealso: ['combinations', 'permutations', 'factorial'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/zeros.js - var zerosDocs = { - name: 'zeros', - category: 'Matrix', - syntax: ['zeros(m)', 'zeros(m, n)', 'zeros(m, n, p, ...)', 'zeros([m])', 'zeros([m, n])', 'zeros([m, n, p, ...])'], - description: 'Create a matrix containing zeros.', - examples: ['zeros(3)', 'zeros(3, 5)', 'a = [1, 2, 3; 4, 5, 6]', 'zeros(size(a))'], - seealso: ['concat', 'det', 'diag', 'identity', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/transpose.js - var transposeDocs = { - name: 'transpose', - category: 'Matrix', - syntax: ['x\'', 'transpose(x)'], - description: 'Transpose a matrix', - examples: ['a = [1, 2, 3; 4, 5, 6]', 'a\'', 'transpose(a)'], - seealso: ['concat', 'det', 'diag', 'identity', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/trace.js - var traceDocs = { - name: 'trace', - category: 'Matrix', - syntax: ['trace(A)'], - description: 'Calculate the trace of a matrix: the sum of the elements on the main diagonal of a square matrix.', - examples: ['A = [1, 2, 3; -1, 2, 3; 2, 0, 3]', 'trace(A)'], - seealso: ['concat', 'det', 'diag', 'identity', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/subset.js - var subsetDocs = { - name: 'subset', - category: 'Matrix', - syntax: ['value(index)', 'value(index) = replacement', 'subset(value, [index])', 'subset(value, [index], replacement)'], - description: 'Get or set a subset of a matrix or string. ' + 'Indexes are one-based. ' + 'Both the ranges lower-bound and upper-bound are included.', - examples: ['d = [1, 2; 3, 4]', 'e = []', 'e[1, 1:2] = [5, 6]', 'e[2, :] = [7, 8]', 'f = d * e', 'f[2, 1]', 'f[:, 1]'], - seealso: ['concat', 'det', 'diag', 'identity', 'inv', 'ones', 'range', 'size', 'squeeze', 'trace', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/squeeze.js - var squeezeDocs = { - name: 'squeeze', - category: 'Matrix', - syntax: ['squeeze(x)'], - description: 'Remove inner and outer singleton dimensions from a matrix.', - examples: ['a = zeros(3,2,1)', 'size(squeeze(a))', 'b = zeros(1,1,3)', 'size(squeeze(b))'], - seealso: ['concat', 'det', 'diag', 'identity', 'inv', 'ones', 'range', 'size', 'subset', 'trace', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/sort.js - var sortDocs = { - name: 'sort', - category: 'Matrix', - syntax: ['sort(x)', 'sort(x, compare)'], - description: 'Sort the items in a matrix. Compare can be a string "asc", "desc", "natural", or a custom sort function.', - examples: ['sort([5, 10, 1])', 'sort(["C", "B", "A", "D"])', 'sortByLength(a, b) = size(a)[1] - size(b)[1]', 'sort(["Langdon", "Tom", "Sara"], sortByLength)', 'sort(["10", "1", "2"], "natural")'], - seealso: ['map', 'filter', 'forEach'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/size.js - var sizeDocs = { - name: 'size', - category: 'Matrix', - syntax: ['size(x)'], - description: 'Calculate the size of a matrix.', - examples: ['size(2.3)', 'size("hello world")', 'a = [1, 2; 3, 4; 5, 6]', 'size(a)', 'size(1:6)'], - seealso: ['concat', 'det', 'diag', 'identity', 'inv', 'ones', 'range', 'squeeze', 'subset', 'trace', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/diff.js - var diffDocs = { - name: 'diff', - category: 'Matrix', - syntax: ['diff(arr)', 'diff(arr, dim)'], - description: ['Create a new matrix or array with the difference of the passed matrix or array.', 'Dim parameter is optional and used to indicant the dimension of the array/matrix to apply the difference', 'If no dimension parameter is passed it is assumed as dimension 0', 'Dimension is zero-based in javascript and one-based in the parser', 'Arrays must be \'rectangular\' meaning arrays like [1, 2]', 'If something is passed as a matrix it will be returned as a matrix but other than that all matrices are converted to arrays'], - examples: ['diff([1, 2, 4, 7, 0])', 'diff([1, 2, 4, 7, 0], 0)', 'diff(matrix([1, 2, 4, 7, 0]))', 'diff([[1, 2], [3, 4]])', 'diff([[1, 2], [3, 4]], 0)', 'diff([[1, 2], [3, 4]], 1)', 'diff([[1, 2], [3, 4]], bignumber(1))', 'diff(matrix([[1, 2], [3, 4]]), 1)', 'diff([[1, 2], matrix([3, 4])], 1)'], - seealso: ['subtract', 'partitionSelect'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/reshape.js - var reshapeDocs = { - name: 'reshape', - category: 'Matrix', - syntax: ['reshape(x, sizes)'], - description: 'Reshape a multi dimensional array to fit the specified dimensions.', - examples: ['reshape([1, 2, 3, 4, 5, 6], [2, 3])', 'reshape([[1, 2], [3, 4]], [1, 4])', 'reshape([[1, 2], [3, 4]], [4])'], - seealso: ['size', 'squeeze', 'resize'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/resize.js - var resizeDocs = { - name: 'resize', - category: 'Matrix', - syntax: ['resize(x, size)', 'resize(x, size, defaultValue)'], - description: 'Resize a matrix.', - examples: ['resize([1,2,3,4,5], [3])', 'resize([1,2,3], [5])', 'resize([1,2,3], [5], -1)', 'resize(2, [2, 3])', 'resize("hello", [8], "!")'], - seealso: ['size', 'subset', 'squeeze', 'reshape'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/range.js - var rangeDocs = { - name: 'range', - category: 'Type', - syntax: ['start:end', 'start:step:end', 'range(start, end)', 'range(start, end, step)', 'range(string)'], - description: 'Create a range. Lower bound of the range is included, upper bound is excluded.', - examples: ['1:5', '3:-1:-3', 'range(3, 7)', 'range(0, 12, 2)', 'range("4:10")', 'a = [1, 2, 3, 4; 5, 6, 7, 8]', 'a[1:2, 1:2]'], - seealso: ['concat', 'det', 'diag', 'identity', 'inv', 'ones', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/partitionSelect.js - var partitionSelectDocs = { - name: 'partitionSelect', - category: 'Matrix', - syntax: ['partitionSelect(x, k)', 'partitionSelect(x, k, compare)'], - description: 'Partition-based selection of an array or 1D matrix. Will find the kth smallest value, and mutates the input array. Uses Quickselect.', - examples: ['partitionSelect([5, 10, 1], 2)', 'partitionSelect(["C", "B", "A", "D"], 1)'], - seealso: ['sort'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/ones.js - var onesDocs = { - name: 'ones', - category: 'Matrix', - syntax: ['ones(m)', 'ones(m, n)', 'ones(m, n, p, ...)', 'ones([m])', 'ones([m, n])', 'ones([m, n, p, ...])'], - description: 'Create a matrix containing ones.', - examples: ['ones(3)', 'ones(3, 5)', 'ones([2,3]) * 4.5', 'a = [1, 2, 3; 4, 5, 6]', 'ones(size(a))'], - seealso: ['concat', 'det', 'diag', 'identity', 'inv', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/map.js - var mapDocs = { - name: 'map', - category: 'Matrix', - syntax: ['map(x, callback)'], - description: 'Create a new matrix or array with the results of the callback function executed on each entry of the matrix/array.', - examples: ['map([1, 2, 3], square)'], - seealso: ['filter', 'forEach'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/kron.js - var kronDocs = { - name: 'kron', - category: 'Matrix', - syntax: ['kron(x, y)'], - description: 'Calculates the kronecker product of 2 matrices or vectors.', - examples: ['kron([[1, 0], [0, 1]], [[1, 2], [3, 4]])', 'kron([1,1], [2,3,4])'], - seealso: ['multiply', 'dot', 'cross'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/inv.js - var invDocs = { - name: 'inv', - category: 'Matrix', - syntax: ['inv(x)'], - description: 'Calculate the inverse of a matrix', - examples: ['inv([1, 2; 3, 4])', 'inv(4)', '1 / 4'], - seealso: ['concat', 'det', 'diag', 'identity', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/forEach.js - var forEachDocs = { - name: 'forEach', - category: 'Matrix', - syntax: ['forEach(x, callback)'], - description: 'Iterates over all elements of a matrix/array, and executes the given callback function.', - examples: ['forEach([1, 2, 3], function(val) { console.log(val) })'], - seealso: ['map', 'sort', 'filter'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/flatten.js - var flattenDocs = { - name: 'flatten', - category: 'Matrix', - syntax: ['flatten(x)'], - description: 'Flatten a multi dimensional matrix into a single dimensional matrix.', - examples: ['a = [1, 2, 3; 4, 5, 6]', 'size(a)', 'b = flatten(a)', 'size(b)'], - seealso: ['concat', 'resize', 'size', 'squeeze'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/filter.js - var filterDocs = { - name: 'filter', - category: 'Matrix', - syntax: ['filter(x, test)'], - description: 'Filter items in a matrix.', - examples: ['isPositive(x) = x > 0', 'filter([6, -2, -1, 4, 3], isPositive)', 'filter([6, -2, 0, 1, 0], x != 0)'], - seealso: ['sort', 'map', 'forEach'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/identity.js - var identityDocs = { - name: 'identity', - category: 'Matrix', - syntax: ['identity(n)', 'identity(m, n)', 'identity([m, n])'], - description: 'Returns the identity matrix with size m-by-n. The matrix has ones on the diagonal and zeros elsewhere.', - examples: ['identity(3)', 'identity(3, 5)', 'a = [1, 2, 3; 4, 5, 6]', 'identity(size(a))'], - seealso: ['concat', 'det', 'diag', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/getMatrixDataType.js - var getMatrixDataTypeDocs = { - name: 'getMatrixDataType', - category: 'Matrix', - syntax: ['getMatrixDataType(x)'], - description: 'Find the data type of all elements in a matrix or array, ' + 'for example "number" if all items are a number ' + 'and "Complex" if all values are complex numbers. ' + 'If a matrix contains more than one data type, it will return "mixed".', - examples: ['getMatrixDataType([1, 2, 3])', 'getMatrixDataType([[5 cm], [2 inch]])', 'getMatrixDataType([1, "text"])', 'getMatrixDataType([1, bignumber(4)])'], - seealso: ['matrix', 'sparse', 'typeOf'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/dot.js - var dotDocs = { - name: 'dot', - category: 'Matrix', - syntax: ['dot(A, B)', 'A * B'], - description: 'Calculate the dot product of two vectors. ' + 'The dot product of A = [a1, a2, a3, ..., an] and B = [b1, b2, b3, ..., bn] ' + 'is defined as dot(A, B) = a1 * b1 + a2 * b2 + a3 * b3 + ... + an * bn', - examples: ['dot([2, 4, 1], [2, 2, 3])', '[2, 4, 1] * [2, 2, 3]'], - seealso: ['multiply', 'cross'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/diag.js - var diagDocs = { - name: 'diag', - category: 'Matrix', - syntax: ['diag(x)', 'diag(x, k)'], - description: 'Create a diagonal matrix or retrieve the diagonal of a matrix. When x is a vector, a matrix with the vector values on the diagonal will be returned. When x is a matrix, a vector with the diagonal values of the matrix is returned. When k is provided, the k-th diagonal will be filled in or retrieved, if k is positive, the values are placed on the super diagonal. When k is negative, the values are placed on the sub diagonal.', - examples: ['diag(1:3)', 'diag(1:3, 1)', 'a = [1, 2, 3; 4, 5, 6; 7, 8, 9]', 'diag(a)'], - seealso: ['concat', 'det', 'identity', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/det.js - var detDocs = { - name: 'det', - category: 'Matrix', - syntax: ['det(x)'], - description: 'Calculate the determinant of a matrix', - examples: ['det([1, 2; 3, 4])', 'det([-2, 2, 3; -1, 1, 3; 2, 0, -1])'], - seealso: ['concat', 'diag', 'identity', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/ctranspose.js - var ctransposeDocs = { - name: 'ctranspose', - category: 'Matrix', - syntax: ['x\'', 'ctranspose(x)'], - description: 'Complex Conjugate and Transpose a matrix', - examples: ['a = [1, 2, 3; 4, 5, 6]', 'a\'', 'ctranspose(a)'], - seealso: ['concat', 'det', 'diag', 'identity', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/cross.js - var crossDocs = { - name: 'cross', - category: 'Matrix', - syntax: ['cross(A, B)'], - description: 'Calculate the cross product for two vectors in three dimensional space.', - examples: ['cross([1, 1, 0], [0, 1, 1])', 'cross([3, -3, 1], [4, 9, 2])', 'cross([2, 3, 4], [5, 6, 7])'], - seealso: ['multiply', 'dot'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/concat.js - var concatDocs = { - name: 'concat', - category: 'Matrix', - syntax: ['concat(A, B, C, ...)', 'concat(A, B, C, ..., dim)'], - description: 'Concatenate matrices. By default, the matrices are concatenated by the last dimension. The dimension on which to concatenate can be provided as last argument.', - examples: ['A = [1, 2; 5, 6]', 'B = [3, 4; 7, 8]', 'concat(A, B)', 'concat(A, B, 1)', 'concat(A, B, 2)'], - seealso: ['det', 'diag', 'identity', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/logical/xor.js - var xorDocs = { - name: 'xor', - category: 'Logical', - syntax: ['x xor y', 'xor(x, y)'], - description: 'Logical exclusive or, xor. Test whether one and only one value is defined with a nonzero/nonempty value.', - examples: ['true xor false', 'false xor false', 'true xor true', '0 xor 4'], - seealso: ['not', 'and', 'or'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/logical/or.js - var orDocs = { - name: 'or', - category: 'Logical', - syntax: ['x or y', 'or(x, y)'], - description: 'Logical or. Test if at least one value is defined with a nonzero/nonempty value.', - examples: ['true or false', 'false or false', '0 or 4'], - seealso: ['not', 'and', 'xor'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/logical/not.js - var notDocs = { - name: 'not', - category: 'Logical', - syntax: ['not x', 'not(x)'], - description: 'Logical not. Flips the boolean value of given argument.', - examples: ['not true', 'not false', 'not 2', 'not 0'], - seealso: ['and', 'or', 'xor'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/logical/and.js - var andDocs = { - name: 'and', - category: 'Logical', - syntax: ['x and y', 'and(x, y)'], - description: 'Logical and. Test whether two values are both defined with a nonzero/nonempty value.', - examples: ['true and false', 'true and true', '2 and 4'], - seealso: ['not', 'or', 'xor'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/geometry/intersect.js - var intersectDocs = { - name: 'intersect', - category: 'Geometry', - syntax: ['intersect(expr1, expr2, expr3, expr4)', 'intersect(expr1, expr2, expr3)'], - description: 'Computes the intersection point of lines and/or planes.', - examples: ['intersect([0, 0], [10, 10], [10, 0], [0, 10])', 'intersect([1, 0, 1], [4, -2, 2], [1, 1, 1, 6])'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/geometry/distance.js - var distanceDocs = { - name: 'distance', - category: 'Geometry', - syntax: ['distance([x1, y1], [x2, y2])', 'distance([[x1, y1], [x2, y2]])'], - description: 'Calculates the Euclidean distance between two points.', - examples: ['distance([0,0], [4,4])', 'distance([[0,0], [4,4]])'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/expression/help.js - var helpDocs = { - name: 'help', - category: 'Expression', - syntax: ['help(object)', 'help(string)'], - description: 'Display documentation on a function or data type.', - examples: ['help(sqrt)', 'help("complex")'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/expression/evaluate.js - var evaluateDocs = { - name: 'evaluate', - category: 'Expression', - syntax: ['evaluate(expression)', 'evaluate([expr1, expr2, expr3, ...])'], - description: 'Evaluate an expression or an array with expressions.', - examples: ['evaluate("2 + 3")', 'evaluate("sqrt(" + 4 + ")")'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/complex/im.js - var imDocs = { - name: 'im', - category: 'Complex', - syntax: ['im(x)'], - description: 'Get the imaginary part of a complex number.', - examples: ['im(2 + 3i)', 're(2 + 3i)', 'im(-5.2i)', 'im(2.4)'], - seealso: ['re', 'conj', 'abs', 'arg'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/complex/re.js - var reDocs = { - name: 're', - category: 'Complex', - syntax: ['re(x)'], - description: 'Get the real part of a complex number.', - examples: ['re(2 + 3i)', 'im(2 + 3i)', 're(-5.2i)', 're(2.4)'], - seealso: ['im', 'conj', 'abs', 'arg'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/complex/conj.js - var conjDocs = { - name: 'conj', - category: 'Complex', - syntax: ['conj(x)'], - description: 'Compute the complex conjugate of a complex value. If x = a+bi, the complex conjugate is a-bi.', - examples: ['conj(2 + 3i)', 'conj(2 - 3i)', 'conj(-5.2i)'], - seealso: ['re', 'im', 'abs', 'arg'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/complex/arg.js - var argDocs = { - name: 'arg', - category: 'Complex', - syntax: ['arg(x)'], - description: 'Compute the argument of a complex value. If x = a+bi, the argument is computed as atan2(b, a).', - examples: ['arg(2 + 2i)', 'atan2(3, 2)', 'arg(2 + 3i)'], - seealso: ['re', 'im', 'conj', 'abs'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/core/typed.js - var typedDocs = { - name: 'typed', - category: 'Core', - syntax: ['typed(signatures)', 'typed(name, signatures)'], - description: 'Create a typed function.', - examples: ['double = typed({ "number, number": f(x)=x+x })', 'double(2)', 'double("hello")'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/core/import.js - var importDocs = { - name: 'import', - category: 'Core', - syntax: ['import(functions)', 'import(functions, options)'], - description: 'Import functions or constants from an object.', - examples: ['import({myFn: f(x)=x^2, myConstant: 32 })', 'myFn(2)', 'myConstant'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/core/config.js - var configDocs = { - name: 'config', - category: 'Core', - syntax: ['config()', 'config(options)'], - description: 'Get configuration or change configuration.', - examples: ['config()', '1/3 + 1/4', 'config({number: "Fraction"})', '1/3 + 1/4'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/combinatorics/stirlingS2.js - var stirlingS2Docs = { - name: 'stirlingS2', - category: 'Combinatorics', - syntax: ['stirlingS2(n, k)'], - description: 'he Stirling numbers of the second kind, counts the number of ways to partition a set of n labelled objects into k nonempty unlabelled subsets. `stirlingS2` only takes integer arguments. The following condition must be enforced: k <= n. If n = k or k = 1, then s(n,k) = 1.', - examples: ['stirlingS2(5, 3)'], - seealso: ['bellNumbers'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/combinatorics/composition.js - var compositionDocs = { - name: 'composition', - category: 'Combinatorics', - syntax: ['composition(n, k)'], - description: 'The composition counts of n into k parts. composition only takes integer arguments. The following condition must be enforced: k <= n.', - examples: ['composition(5, 3)'], - seealso: ['combinations'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/combinatorics/catalan.js - var catalanDocs = { - name: 'catalan', - category: 'Combinatorics', - syntax: ['catalan(n)'], - description: 'The Catalan Numbers enumerate combinatorial structures of many different types. catalan only takes integer arguments. The following condition must be enforced: n >= 0.', - examples: ['catalan(3)', 'catalan(8)'], - seealso: ['bellNumbers'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/combinatorics/bellNumbers.js - var bellNumbersDocs = { - name: 'bellNumbers', - category: 'Combinatorics', - syntax: ['bellNumbers(n)'], - description: 'The Bell Numbers count the number of partitions of a set. A partition is a pairwise disjoint subset of S whose union is S. `bellNumbers` only takes integer arguments. The following condition must be enforced: n >= 0.', - examples: ['bellNumbers(3)', 'bellNumbers(8)'], - seealso: ['stirlingS2'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/bitwise/rightLogShift.js - var rightLogShiftDocs = { - name: 'rightLogShift', - category: 'Bitwise', - syntax: ['x >>> y', 'rightLogShift(x, y)'], - description: 'Bitwise right logical shift of a value x by y number of bits.', - examples: ['8 >>> 1', '4 << 1', '-12 >>> 2'], - seealso: ['bitAnd', 'bitNot', 'bitOr', 'bitXor', 'leftShift', 'rightArithShift'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/bitwise/rightArithShift.js - var rightArithShiftDocs = { - name: 'rightArithShift', - category: 'Bitwise', - syntax: ['x >> y', 'rightArithShift(x, y)'], - description: 'Bitwise right arithmetic shift of a value x by y number of bits.', - examples: ['8 >> 1', '4 << 1', '-12 >> 2'], - seealso: ['bitAnd', 'bitNot', 'bitOr', 'bitXor', 'leftShift', 'rightLogShift'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/bitwise/leftShift.js - var leftShiftDocs = { - name: 'leftShift', - category: 'Bitwise', - syntax: ['x << y', 'leftShift(x, y)'], - description: 'Bitwise left logical shift of a value x by y number of bits.', - examples: ['4 << 1', '8 >> 1'], - seealso: ['bitAnd', 'bitNot', 'bitOr', 'bitXor', 'rightArithShift', 'rightLogShift'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/bitwise/bitXor.js - var bitXorDocs = { - name: 'bitXor', - category: 'Bitwise', - syntax: ['bitXor(x, y)'], - description: 'Bitwise XOR operation, exclusive OR. Performs the logical exclusive OR operation on each pair of corresponding bits of the two given values. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1.', - examples: ['bitOr(1, 2)', 'bitXor([2, 3, 4], 4)'], - seealso: ['bitAnd', 'bitNot', 'bitOr', 'leftShift', 'rightArithShift', 'rightLogShift'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/bitwise/bitOr.js - var bitOrDocs = { - name: 'bitOr', - category: 'Bitwise', - syntax: ['x | y', 'bitOr(x, y)'], - description: 'Bitwise OR operation. Performs the logical inclusive OR operation on each pair of corresponding bits of the two given values. The result in each position is 1 if the first bit is 1 or the second bit is 1 or both bits are 1, otherwise, the result is 0.', - examples: ['5 | 3', 'bitOr([1, 2, 3], 4)'], - seealso: ['bitAnd', 'bitNot', 'bitXor', 'leftShift', 'rightArithShift', 'rightLogShift'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/bitwise/bitNot.js - var bitNotDocs = { - name: 'bitNot', - category: 'Bitwise', - syntax: ['~x', 'bitNot(x)'], - description: 'Bitwise NOT operation. Performs a logical negation on each bit of the given value. Bits that are 0 become 1, and those that are 1 become 0.', - examples: ['~1', '~2', 'bitNot([2, -3, 4])'], - seealso: ['bitAnd', 'bitOr', 'bitXor', 'leftShift', 'rightArithShift', 'rightLogShift'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/bitwise/bitAnd.js - var bitAndDocs = { - name: 'bitAnd', - category: 'Bitwise', - syntax: ['x & y', 'bitAnd(x, y)'], - description: 'Bitwise AND operation. Performs the logical AND operation on each pair of the corresponding bits of the two given values by multiplying them. If both bits in the compared position are 1, the bit in the resulting binary representation is 1, otherwise, the result is 0', - examples: ['5 & 3', 'bitAnd(53, 131)', '[1, 12, 31] & 42'], - seealso: ['bitNot', 'bitOr', 'bitXor', 'leftShift', 'rightArithShift', 'rightLogShift'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/xgcd.js - var xgcdDocs = { - name: 'xgcd', - category: 'Arithmetic', - syntax: ['xgcd(a, b)'], - description: 'Calculate the extended greatest common divisor for two values. The result is an array [d, x, y] with 3 entries, where d is the greatest common divisor, and d = x * a + y * b.', - examples: ['xgcd(8, 12)', 'gcd(8, 12)', 'xgcd(36163, 21199)'], - seealso: ['gcd', 'lcm'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/unaryPlus.js - var unaryPlusDocs = { - name: 'unaryPlus', - category: 'Operators', - syntax: ['+x', 'unaryPlus(x)'], - description: 'Converts booleans and strings to numbers.', - examples: ['+true', '+"2"'], - seealso: ['add', 'subtract', 'unaryMinus'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/unaryMinus.js - var unaryMinusDocs = { - name: 'unaryMinus', - category: 'Operators', - syntax: ['-x', 'unaryMinus(x)'], - description: 'Inverse the sign of a value. Converts booleans and strings to numbers.', - examples: ['-4.5', '-(-5.6)', '-"22"'], - seealso: ['add', 'subtract', 'unaryPlus'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/square.js - var squareDocs = { - name: 'square', - category: 'Arithmetic', - syntax: ['square(x)'], - description: 'Compute the square of a value. The square of x is x * x.', - examples: ['square(3)', 'sqrt(9)', '3^2', '3 * 3'], - seealso: ['multiply', 'pow', 'sqrt', 'cube'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/sqrtm.js - var sqrtmDocs = { - name: 'sqrtm', - category: 'Arithmetic', - syntax: ['sqrtm(x)'], - description: 'Calculate the principal square root of a square matrix. The principal square root matrix `X` of another matrix `A` is such that `X * X = A`.', - examples: ['sqrtm([[1, 2], [3, 4]])'], - seealso: ['sqrt', 'abs', 'square', 'multiply'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/sqrt.js - var sqrtDocs = { - name: 'sqrt', - category: 'Arithmetic', - syntax: ['sqrt(x)'], - description: 'Compute the square root value. If x = y * y, then y is the square root of x.', - examples: ['sqrt(25)', '5 * 5', 'sqrt(-1)'], - seealso: ['square', 'sqrtm', 'multiply', 'nthRoot', 'nthRoots', 'pow'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/sign.js - var signDocs = { - name: 'sign', - category: 'Arithmetic', - syntax: ['sign(x)'], - description: 'Compute the sign of a value. The sign of a value x is 1 when x>1, -1 when x<0, and 0 when x=0.', - examples: ['sign(3.5)', 'sign(-4.2)', 'sign(0)'], - seealso: ['abs'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/round.js - var roundDocs = { - name: 'round', - category: 'Arithmetic', - syntax: ['round(x)', 'round(x, n)'], - description: 'round a value towards the nearest integer.If x is complex, both real and imaginary part are rounded towards the nearest integer. When n is specified, the value is rounded to n decimals.', - examples: ['round(3.2)', 'round(3.8)', 'round(-4.2)', 'round(-4.8)', 'round(pi, 3)', 'round(123.45678, 2)'], - seealso: ['ceil', 'floor', 'fix'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/pow.js - var powDocs = { - name: 'pow', - category: 'Operators', - syntax: ['x ^ y', 'pow(x, y)'], - description: 'Calculates the power of x to y, x^y.', - examples: ['2^3', '2*2*2', '1 + e ^ (pi * i)'], - seealso: ['multiply', 'nthRoot', 'nthRoots', 'sqrt'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/nthRoots.js - var nthRootsDocs = { - name: 'nthRoots', - category: 'Arithmetic', - syntax: ['nthRoots(A)', 'nthRoots(A, root)'], - description: '' + 'Calculate the nth roots of a value. ' + 'An nth root of a positive real number A, ' + 'is a positive real solution of the equation "x^root = A". ' + 'This function returns an array of complex values.', - examples: ['nthRoots(1)', 'nthRoots(1, 3)'], - seealso: ['sqrt', 'pow', 'nthRoot'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/nthRoot.js - var nthRootDocs = { - name: 'nthRoot', - category: 'Arithmetic', - syntax: ['nthRoot(a)', 'nthRoot(a, root)'], - description: 'Calculate the nth root of a value. ' + 'The principal nth root of a positive real number A, ' + 'is the positive real solution of the equation "x^root = A".', - examples: ['4 ^ 3', 'nthRoot(64, 3)', 'nthRoot(9, 2)', 'sqrt(9)'], - seealso: ['nthRoots', 'pow', 'sqrt'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/norm.js - var normDocs = { - name: 'norm', - category: 'Arithmetic', - syntax: ['norm(x)', 'norm(x, p)'], - description: 'Calculate the norm of a number, vector or matrix.', - examples: ['abs(-3.5)', 'norm(-3.5)', 'norm(3 - 4i)', 'norm([1, 2, -3], Infinity)', 'norm([1, 2, -3], -Infinity)', 'norm([3, 4], 2)', 'norm([[1, 2], [3, 4]], 1)', 'norm([[1, 2], [3, 4]], "inf")', 'norm([[1, 2], [3, 4]], "fro")'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/multiply.js - var multiplyDocs = { - name: 'multiply', - category: 'Operators', - syntax: ['x * y', 'multiply(x, y)'], - description: 'multiply two values.', - examples: ['a = 2.1 * 3.4', 'a / 3.4', '2 * 3 + 4', '2 * (3 + 4)', '3 * 2.1 km'], - seealso: ['divide'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/mod.js - var modDocs = { - name: 'mod', - category: 'Operators', - syntax: ['x % y', 'x mod y', 'mod(x, y)'], - description: 'Calculates the modulus, the remainder of an integer division.', - examples: ['7 % 3', '11 % 2', '10 mod 4', 'isOdd(x) = x % 2', 'isOdd(2)', 'isOdd(3)'], - seealso: ['divide'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/log10.js - var log10Docs = { - name: 'log10', - category: 'Arithmetic', - syntax: ['log10(x)'], - description: 'Compute the 10-base logarithm of a value.', - examples: ['log10(0.00001)', 'log10(10000)', '10 ^ 4', 'log(10000) / log(10)', 'log(10000, 10)'], - seealso: ['exp', 'log'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/log1p.js - var log1pDocs = { - name: 'log1p', - category: 'Arithmetic', - syntax: ['log1p(x)', 'log1p(x, base)'], - description: 'Calculate the logarithm of a `value+1`', - examples: ['log1p(2.5)', 'exp(log1p(1.4))', 'pow(10, 4)', 'log1p(9999, 10)', 'log1p(9999) / log(10)'], - seealso: ['exp', 'log', 'log2', 'log10'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/log2.js - var log2Docs = { - name: 'log2', - category: 'Arithmetic', - syntax: ['log2(x)'], - description: 'Calculate the 2-base of a value. This is the same as calculating `log(x, 2)`.', - examples: ['log2(0.03125)', 'log2(16)', 'log2(16) / log2(2)', 'pow(2, 4)'], - seealso: ['exp', 'log1p', 'log', 'log10'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/log.js - var logDocs = { - name: 'log', - category: 'Arithmetic', - syntax: ['log(x)', 'log(x, base)'], - description: 'Compute the logarithm of a value. If no base is provided, the natural logarithm of x is calculated. If base if provided, the logarithm is calculated for the specified base. log(x, base) is defined as log(x) / log(base).', - examples: ['log(3.5)', 'a = log(2.4)', 'exp(a)', '10 ^ 4', 'log(10000, 10)', 'log(10000) / log(10)', 'b = log(1024, 2)', '2 ^ b'], - seealso: ['exp', 'log1p', 'log2', 'log10'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/lcm.js - var lcmDocs = { - name: 'lcm', - category: 'Arithmetic', - syntax: ['lcm(x, y)'], - description: 'Compute the least common multiple.', - examples: ['lcm(4, 6)', 'lcm(6, 21)', 'lcm(6, 21, 5)'], - seealso: ['gcd'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/hypot.js - var hypotDocs = { - name: 'hypot', - category: 'Arithmetic', - syntax: ['hypot(a, b, c, ...)', 'hypot([a, b, c, ...])'], - description: 'Calculate the hypotenusa of a list with values. ', - examples: ['hypot(3, 4)', 'sqrt(3^2 + 4^2)', 'hypot(-2)', 'hypot([3, 4, 5])'], - seealso: ['abs', 'norm'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/gcd.js - var gcdDocs = { - name: 'gcd', - category: 'Arithmetic', - syntax: ['gcd(a, b)', 'gcd(a, b, c, ...)'], - description: 'Compute the greatest common divisor.', - examples: ['gcd(8, 12)', 'gcd(-4, 6)', 'gcd(25, 15, -10)'], - seealso: ['lcm', 'xgcd'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/floor.js - var floorDocs = { - name: 'floor', - category: 'Arithmetic', - syntax: ['floor(x)'], - description: 'Round a value towards minus infinity.If x is complex, both real and imaginary part are rounded towards minus infinity.', - examples: ['floor(3.2)', 'floor(3.8)', 'floor(-4.2)'], - seealso: ['ceil', 'fix', 'round'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/fix.js - var fixDocs = { - name: 'fix', - category: 'Arithmetic', - syntax: ['fix(x)'], - description: 'Round a value towards zero. If x is complex, both real and imaginary part are rounded towards zero.', - examples: ['fix(3.2)', 'fix(3.8)', 'fix(-4.2)', 'fix(-4.8)'], - seealso: ['ceil', 'floor', 'round'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/expm1.js - var expm1Docs = { - name: 'expm1', - category: 'Arithmetic', - syntax: ['expm1(x)'], - description: 'Calculate the value of subtracting 1 from the exponential value.', - examples: ['expm1(2)', 'pow(e, 2) - 1', 'log(expm1(2) + 1)'], - seealso: ['exp', 'pow', 'log'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/expm.js - var expmDocs = { - name: 'expm', - category: 'Arithmetic', - syntax: ['exp(x)'], - description: 'Compute the matrix exponential, expm(A) = e^A. ' + 'The matrix must be square. ' + 'Not to be confused with exp(a), which performs element-wise exponentiation.', - examples: ['expm([[0,2],[0,0]])'], - seealso: ['exp'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/eigs.js - var eigsDocs = { - name: 'eigs', - category: 'Matrix', - syntax: ['eigs(x)'], - description: 'Calculate the eigenvalues and eigenvectors of a real symmetric matrix', - examples: ['eigs([[5, 2.3], [2.3, 1]])'], - seealso: ['inv'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/exp.js - var expDocs = { - name: 'exp', - category: 'Arithmetic', - syntax: ['exp(x)'], - description: 'Calculate the exponent of a value.', - examples: ['exp(1.3)', 'e ^ 1.3', 'log(exp(1.3))', 'x = 2.4', '(exp(i*x) == cos(x) + i*sin(x)) # Euler\'s formula'], - seealso: ['expm', 'expm1', 'pow', 'log'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/dotMultiply.js - var dotMultiplyDocs = { - name: 'dotMultiply', - category: 'Operators', - syntax: ['x .* y', 'dotMultiply(x, y)'], - description: 'Multiply two values element wise.', - examples: ['a = [1, 2, 3; 4, 5, 6]', 'b = [2, 1, 1; 3, 2, 5]', 'a .* b'], - seealso: ['multiply', 'divide', 'dotDivide'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/dotDivide.js - var dotDivideDocs = { - name: 'dotDivide', - category: 'Operators', - syntax: ['x ./ y', 'dotDivide(x, y)'], - description: 'Divide two values element wise.', - examples: ['a = [1, 2, 3; 4, 5, 6]', 'b = [2, 1, 1; 3, 2, 5]', 'a ./ b'], - seealso: ['multiply', 'dotMultiply', 'divide'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/divide.js - var divideDocs = { - name: 'divide', - category: 'Operators', - syntax: ['x / y', 'divide(x, y)'], - description: 'Divide two values.', - examples: ['a = 2 / 3', 'a * 3', '4.5 / 2', '3 + 4 / 2', '(3 + 4) / 2', '18 km / 4.5'], - seealso: ['multiply'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/cube.js - var cubeDocs = { - name: 'cube', - category: 'Arithmetic', - syntax: ['cube(x)'], - description: 'Compute the cube of a value. The cube of x is x * x * x.', - examples: ['cube(2)', '2^3', '2 * 2 * 2'], - seealso: ['multiply', 'square', 'pow'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/ceil.js - var ceilDocs = { - name: 'ceil', - category: 'Arithmetic', - syntax: ['ceil(x)'], - description: 'Round a value towards plus infinity. If x is complex, both real and imaginary part are rounded towards plus infinity.', - examples: ['ceil(3.2)', 'ceil(3.8)', 'ceil(-4.2)'], - seealso: ['floor', 'fix', 'round'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/cbrt.js - var cbrtDocs = { - name: 'cbrt', - category: 'Arithmetic', - syntax: ['cbrt(x)', 'cbrt(x, allRoots)'], - description: 'Compute the cubic root value. If x = y * y * y, then y is the cubic root of x. When `x` is a number or complex number, an optional second argument `allRoots` can be provided to return all three cubic roots. If not provided, the principal root is returned', - examples: ['cbrt(64)', 'cube(4)', 'cbrt(-8)', 'cbrt(2 + 3i)', 'cbrt(8i)', 'cbrt(8i, true)', 'cbrt(27 m^3)'], - seealso: ['square', 'sqrt', 'cube', 'multiply'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/add.js - var addDocs = { - name: 'add', - category: 'Operators', - syntax: ['x + y', 'add(x, y)'], - description: 'Add two values.', - examples: ['a = 2.1 + 3.6', 'a - 3.6', '3 + 2i', '3 cm + 2 inch', '"2.3" + "4"'], - seealso: ['subtract'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/abs.js - var absDocs = { - name: 'abs', - category: 'Arithmetic', - syntax: ['abs(x)'], - description: 'Compute the absolute value.', - examples: ['abs(3.5)', 'abs(-4.2)'], - seealso: ['sign'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/qr.js - var qrDocs = { - name: 'qr', - category: 'Algebra', - syntax: ['qr(A)'], - description: 'Calculates the Matrix QR decomposition. Matrix `A` is decomposed in two matrices (`Q`, `R`) where `Q` is an orthogonal matrix and `R` is an upper triangular matrix.', - examples: ['qr([[1, -1, 4], [1, 4, -2], [1, 4, 2], [1, -1, 0]])'], - seealso: ['lup', 'slu', 'matrix'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/usolve.js - var usolveDocs = { - name: 'usolve', - category: 'Algebra', - syntax: ['x=usolve(U, b)'], - description: 'Finds one solution of the linear system U * x = b where U is an [n x n] upper triangular matrix and b is a [n] column vector.', - examples: ['x=usolve(sparse([1, 1, 1, 1; 0, 1, 1, 1; 0, 0, 1, 1; 0, 0, 0, 1]), [1; 2; 3; 4])'], - seealso: ['usolveAll', 'lup', 'lusolve', 'lsolve', 'matrix', 'sparse'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/usolveAll.js - var usolveAllDocs = { - name: 'usolveAll', - category: 'Algebra', - syntax: ['x=usolve(U, b)'], - description: 'Finds all solutions of the linear system U * x = b where U is an [n x n] upper triangular matrix and b is a [n] column vector.', - examples: ['x=usolve(sparse([1, 1, 1, 1; 0, 1, 1, 1; 0, 0, 1, 1; 0, 0, 0, 1]), [1; 2; 3; 4])'], - seealso: ['usolve', 'lup', 'lusolve', 'lsolve', 'matrix', 'sparse'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/slu.js - var sluDocs = { - name: 'slu', - category: 'Algebra', - syntax: ['slu(A, order, threshold)'], - description: 'Calculate the Matrix LU decomposition with full pivoting. Matrix A is decomposed in two matrices (L, U) and two permutation vectors (pinv, q) where P * A * Q = L * U', - examples: ['slu(sparse([4.5, 0, 3.2, 0; 3.1, 2.9, 0, 0.9; 0, 1.7, 3, 0; 3.5, 0.4, 0, 1]), 1, 0.001)'], - seealso: ['lusolve', 'lsolve', 'usolve', 'matrix', 'sparse', 'lup', 'qr'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/rationalize.js - var rationalizeDocs = { - name: 'rationalize', - category: 'Algebra', - syntax: ['rationalize(expr)', 'rationalize(expr, scope)', 'rationalize(expr, scope, detailed)'], - description: 'Transform a rationalizable expression in a rational fraction. If rational fraction is one variable polynomial then converts the numerator and denominator in canonical form, with decreasing exponents, returning the coefficients of numerator.', - examples: ['rationalize("2x/y - y/(x+1)")', 'rationalize("2x/y - y/(x+1)", true)'], - seealso: ['simplify'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/simplify.js - var simplifyDocs = { - name: 'simplify', - category: 'Algebra', - syntax: ['simplify(expr)', 'simplify(expr, rules)'], - description: 'Simplify an expression tree.', - examples: ['simplify("3 + 2 / 4")', 'simplify("2x + x")', 'f = parse("x * (x + 2 + x)")', 'simplified = simplify(f)', 'simplified.evaluate({x: 2})'], - seealso: ['derivative', 'parse', 'evaluate'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/lup.js - var lupDocs = { - name: 'lup', - category: 'Algebra', - syntax: ['lup(m)'], - description: 'Calculate the Matrix LU decomposition with partial pivoting. Matrix A is decomposed in three matrices (L, U, P) where P * A = L * U', - examples: ['lup([[2, 1], [1, 4]])', 'lup(matrix([[2, 1], [1, 4]]))', 'lup(sparse([[2, 1], [1, 4]]))'], - seealso: ['lusolve', 'lsolve', 'usolve', 'matrix', 'sparse', 'slu', 'qr'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/lsolve.js - var lsolveDocs = { - name: 'lsolve', - category: 'Algebra', - syntax: ['x=lsolve(L, b)'], - description: 'Finds one solution of the linear system L * x = b where L is an [n x n] lower triangular matrix and b is a [n] column vector.', - examples: ['a = [-2, 3; 2, 1]', 'b = [11, 9]', 'x = lsolve(a, b)'], - seealso: ['lsolveAll', 'lup', 'lusolve', 'usolve', 'matrix', 'sparse'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/lsolveAll.js - var lsolveAllDocs = { - name: 'lsolveAll', - category: 'Algebra', - syntax: ['x=lsolveAll(L, b)'], - description: 'Finds all solutions of the linear system L * x = b where L is an [n x n] lower triangular matrix and b is a [n] column vector.', - examples: ['a = [-2, 3; 2, 1]', 'b = [11, 9]', 'x = lsolve(a, b)'], - seealso: ['lsolve', 'lup', 'lusolve', 'usolve', 'matrix', 'sparse'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/derivative.js - var derivativeDocs = { - name: 'derivative', - category: 'Algebra', - syntax: ['derivative(expr, variable)', 'derivative(expr, variable, {simplify: boolean})'], - description: 'Takes the derivative of an expression expressed in parser Nodes. The derivative will be taken over the supplied variable in the second parameter. If there are multiple variables in the expression, it will return a partial derivative.', - examples: ['derivative("2x^3", "x")', 'derivative("2x^3", "x", {simplify: false})', 'derivative("2x^2 + 3x + 4", "x")', 'derivative("sin(2x)", "x")', 'f = parse("x^2 + x")', 'x = parse("x")', 'df = derivative(f, x)', 'df.evaluate({x: 3})'], - seealso: ['simplify', 'parse', 'evaluate'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/version.js - var versionDocs = { - name: 'version', - category: 'Constants', - syntax: ['version'], - description: 'A string with the version number of math.js', - examples: ['version'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/true.js - var trueDocs = { - name: 'true', - category: 'Constants', - syntax: ['true'], - description: 'Boolean value true', - examples: ['true'], - seealso: ['false'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/tau.js - var tauDocs = { - name: 'tau', - category: 'Constants', - syntax: ['tau'], - description: 'Tau is the ratio constant of a circle\'s circumference to radius, equal to 2 * pi, approximately 6.2832.', - examples: ['tau', '2 * pi'], - seealso: ['pi'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/SQRT2.js - var SQRT2Docs = { - name: 'SQRT2', - category: 'Constants', - syntax: ['SQRT2'], - description: 'Returns the square root of 2, approximately equal to 1.414', - examples: ['SQRT2', 'sqrt(2)'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/SQRT1_2.js - var SQRT12Docs = { - name: 'SQRT1_2', - category: 'Constants', - syntax: ['SQRT1_2'], - description: 'Returns the square root of 1/2, approximately equal to 0.707', - examples: ['SQRT1_2', 'sqrt(1/2)'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/phi.js - var phiDocs = { - name: 'phi', - category: 'Constants', - syntax: ['phi'], - description: 'Phi is the golden ratio. Two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Phi is defined as `(1 + sqrt(5)) / 2` and is approximately 1.618034...', - examples: ['phi'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/pi.js - var piDocs = { - name: 'pi', - category: 'Constants', - syntax: ['pi'], - description: 'The number pi is a mathematical constant that is the ratio of a circle\'s circumference to its diameter, and is approximately equal to 3.14159', - examples: ['pi', 'sin(pi/2)'], - seealso: ['tau'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/null.js - var nullDocs = { - name: 'null', - category: 'Constants', - syntax: ['null'], - description: 'Value null', - examples: ['null'], - seealso: ['true', 'false'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/NaN.js - var NaNDocs = { - name: 'NaN', - category: 'Constants', - syntax: ['NaN'], - description: 'Not a number', - examples: ['NaN', '0 / 0'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/LOG10E.js - var LOG10EDocs = { - name: 'LOG10E', - category: 'Constants', - syntax: ['LOG10E'], - description: 'Returns the base-10 logarithm of E, approximately equal to 0.434', - examples: ['LOG10E', 'log(e, 10)'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/LOG2E.js - var LOG2EDocs = { - name: 'LOG2E', - category: 'Constants', - syntax: ['LOG2E'], - description: 'Returns the base-2 logarithm of E, approximately equal to 1.442', - examples: ['LOG2E', 'log(e, 2)'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/LN10.js - var LN10Docs = { - name: 'LN10', - category: 'Constants', - syntax: ['LN10'], - description: 'Returns the natural logarithm of 10, approximately equal to 2.302', - examples: ['LN10', 'log(10)'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/LN2.js - var LN2Docs = { - name: 'LN2', - category: 'Constants', - syntax: ['LN2'], - description: 'Returns the natural logarithm of 2, approximately equal to 0.693', - examples: ['LN2', 'log(2)'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/Infinity.js - var InfinityDocs = { - name: 'Infinity', - category: 'Constants', - syntax: ['Infinity'], - description: 'Infinity, a number which is larger than the maximum number that can be handled by a floating point number.', - examples: ['Infinity', '1 / 0'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/i.js - var iDocs = { - name: 'i', - category: 'Constants', - syntax: ['i'], - description: 'Imaginary unit, defined as i*i=-1. A complex number is described as a + b*i, where a is the real part, and b is the imaginary part.', - examples: ['i', 'i * i', 'sqrt(-1)'], - seealso: [] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/false.js - var falseDocs = { - name: 'false', - category: 'Constants', - syntax: ['false'], - description: 'Boolean value false', - examples: ['false'], - seealso: ['true'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/constants/e.js - var eDocs = { - name: 'e', - category: 'Constants', - syntax: ['e'], - description: 'Euler\'s number, the base of the natural logarithm. Approximately equal to 2.71828', - examples: ['e', 'e ^ 2', 'exp(2)', 'log(e)'], - seealso: ['exp'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/unit.js - var unitDocs = { - name: 'unit', - category: 'Construction', - syntax: ['value unit', 'unit(value, unit)', 'unit(string)'], - description: 'Create a unit.', - examples: ['5.5 mm', '3 inch', 'unit(7.1, "kilogram")', 'unit("23 deg")'], - seealso: ['bignumber', 'boolean', 'complex', 'index', 'matrix', 'number', 'string'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/string.js - var stringDocs = { - name: 'string', - category: 'Construction', - syntax: ['"text"', 'string(x)'], - description: 'Create a string or convert a value to a string', - examples: ['"Hello World!"', 'string(4.2)', 'string(3 + 2i)'], - seealso: ['bignumber', 'boolean', 'complex', 'index', 'matrix', 'number', 'unit'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/splitUnit.js - var splitUnitDocs = { - name: 'splitUnit', - category: 'Construction', - syntax: ['splitUnit(unit: Unit, parts: Unit[])'], - description: 'Split a unit in an array of units whose sum is equal to the original unit.', - examples: ['splitUnit(1 m, ["feet", "inch"])'], - seealso: ['unit', 'createUnit'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/sparse.js - var sparseDocs = { - name: 'sparse', - category: 'Construction', - syntax: ['sparse()', 'sparse([a1, b1, ...; a1, b2, ...])', 'sparse([a1, b1, ...; a1, b2, ...], "number")'], - description: 'Create a sparse matrix.', - examples: ['sparse()', 'sparse([3, 4; 5, 6])', 'sparse([3, 0; 5, 0], "number")'], - seealso: ['bignumber', 'boolean', 'complex', 'index', 'number', 'string', 'unit', 'matrix'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/number.js - var numberDocs = { - name: 'number', - category: 'Construction', - syntax: ['x', 'number(x)', 'number(unit, valuelessUnit)'], - description: 'Create a number or convert a string or boolean into a number.', - examples: ['2', '2e3', '4.05', 'number(2)', 'number("7.2")', 'number(true)', 'number([true, false, true, true])', 'number(unit("52cm"), "m")'], - seealso: ['bignumber', 'boolean', 'complex', 'fraction', 'index', 'matrix', 'string', 'unit'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/matrix.js - var matrixDocs = { - name: 'matrix', - category: 'Construction', - syntax: ['[]', '[a1, b1, ...; a2, b2, ...]', 'matrix()', 'matrix("dense")', 'matrix([...])'], - description: 'Create a matrix.', - examples: ['[]', '[1, 2, 3]', '[1, 2, 3; 4, 5, 6]', 'matrix()', 'matrix([3, 4])', 'matrix([3, 4; 5, 6], "sparse")', 'matrix([3, 4; 5, 6], "sparse", "number")'], - seealso: ['bignumber', 'boolean', 'complex', 'index', 'number', 'string', 'unit', 'sparse'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/index.js - var indexDocs = { - name: 'index', - category: 'Construction', - syntax: ['[start]', '[start:end]', '[start:step:end]', '[start1, start 2, ...]', '[start1:end1, start2:end2, ...]', '[start1:step1:end1, start2:step2:end2, ...]'], - description: 'Create an index to get or replace a subset of a matrix', - examples: ['[]', '[1, 2, 3]', 'A = [1, 2, 3; 4, 5, 6]', 'A[1, :]', 'A[1, 2] = 50', 'A[0:2, 0:2] = ones(2, 2)'], - seealso: ['bignumber', 'boolean', 'complex', 'matrix,', 'number', 'range', 'string', 'unit'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/fraction.js - var fractionDocs = { - name: 'fraction', - category: 'Construction', - syntax: ['fraction(num)', 'fraction(num,den)'], - description: 'Create a fraction from a number or from a numerator and denominator.', - examples: ['fraction(0.125)', 'fraction(1, 3) + fraction(2, 5)'], - seealso: ['bignumber', 'boolean', 'complex', 'index', 'matrix', 'string', 'unit'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/createUnit.js - var createUnitDocs = { - name: 'createUnit', - category: 'Construction', - syntax: ['createUnit(definitions)', 'createUnit(name, definition)'], - description: 'Create a user-defined unit and register it with the Unit type.', - examples: ['createUnit("foo")', 'createUnit("knot", {definition: "0.514444444 m/s", aliases: ["knots", "kt", "kts"]})', 'createUnit("mph", "1 mile/hour")'], - seealso: ['unit', 'splitUnit'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/complex.js - var complexDocs = { - name: 'complex', - category: 'Construction', - syntax: ['complex()', 'complex(re, im)', 'complex(string)'], - description: 'Create a complex number.', - examples: ['complex()', 'complex(2, 3)', 'complex("7 - 2i")'], - seealso: ['bignumber', 'boolean', 'index', 'matrix', 'number', 'string', 'unit'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/construction/boolean.js - var booleanDocs = { - name: 'boolean', - category: 'Construction', - syntax: ['x', 'boolean(x)'], - description: 'Convert a string or number into a boolean.', - examples: ['boolean(0)', 'boolean(1)', 'boolean(3)', 'boolean("true")', 'boolean("false")', 'boolean([1, 0, 1, 1])'], - seealso: ['bignumber', 'complex', 'index', 'matrix', 'number', 'string', 'unit'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/dotPow.js - var dotPowDocs = { - name: 'dotPow', - category: 'Operators', - syntax: ['x .^ y', 'dotPow(x, y)'], - description: 'Calculates the power of x to y element wise.', - examples: ['a = [1, 2, 3; 4, 5, 6]', 'a .^ 2'], - seealso: ['pow'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/algebra/lusolve.js - var lusolveDocs = { - name: 'lusolve', - category: 'Algebra', - syntax: ['x=lusolve(A, b)', 'x=lusolve(lu, b)'], - description: 'Solves the linear system A * x = b where A is an [n x n] matrix and b is a [n] column vector.', - examples: ['a = [-2, 3; 2, 1]', 'b = [11, 9]', 'x = lusolve(a, b)'], - seealso: ['lup', 'slu', 'lsolve', 'usolve', 'matrix', 'sparse'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/arithmetic/subtract.js - var subtractDocs = { - name: 'subtract', - category: 'Operators', - syntax: ['x - y', 'subtract(x, y)'], - description: 'subtract two values.', - examples: ['a = 5.3 - 2', 'a + 2', '2/3 - 1/6', '2 * 3 - 3', '2.1 km - 500m'], - seealso: ['add'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/statistics/variance.js - var varianceDocs = { - name: 'variance', - category: 'Statistics', - syntax: ['variance(a, b, c, ...)', 'variance(A)', 'variance(A, normalization)'], - description: 'Compute the variance of all values. Optional parameter normalization can be "unbiased" (default), "uncorrected", or "biased".', - examples: ['variance(2, 4, 6)', 'variance([2, 4, 6, 8])', 'variance([2, 4, 6, 8], "uncorrected")', 'variance([2, 4, 6, 8], "biased")', 'variance([1, 2, 3; 4, 5, 6])'], - seealso: ['max', 'mean', 'min', 'median', 'min', 'prod', 'std', 'sum'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/trigonometry/sin.js - var sinDocs = { - name: 'sin', - category: 'Trigonometry', - syntax: ['sin(x)'], - description: 'Compute the sine of x in radians.', - examples: ['sin(2)', 'sin(pi / 4) ^ 2', 'sin(90 deg)', 'sin(30 deg)', 'sin(0.2)^2 + cos(0.2)^2'], - seealso: ['asin', 'cos', 'tan'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/utils/numeric.js - var numericDocs = { - name: 'numeric', - category: 'Utils', - syntax: ['numeric(x)'], - description: 'Convert a numeric input to a specific numeric type: number, BigNumber, or Fraction.', - examples: ['numeric("4")', 'numeric("4", "number")', 'numeric("4", "BigNumber")', 'numeric("4", "Fraction)', 'numeric(4, "Fraction")', 'numeric(fraction(2, 5), "number)'], - seealso: ['number', 'fraction', 'bignumber', 'string', 'format'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/column.js - var columnDocs = { - name: 'column', - category: 'Matrix', - syntax: ['column(x, index)'], - description: 'Return a column from a matrix or array.', - examples: ['A = [[1, 2], [3, 4]]', 'column(A, 1)', 'column(A, 2)'], - seealso: ['row'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/row.js - var rowDocs = { - name: 'row', - category: 'Matrix', - syntax: ['row(x, index)'], - description: 'Return a row from a matrix or array.', - examples: ['A = [[1, 2], [3, 4]]', 'row(A, 1)', 'row(A, 2)'], - seealso: ['column'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/function/matrix/rotationMatrix.js - var rotationMatrixDocs = { - name: 'rotationMatrix', - category: 'Matrix', - syntax: ['rotationMatrix(theta)', 'rotationMatrix(theta, v)', 'rotationMatrix(theta, v, format)'], - description: 'Returns a 2-D rotation matrix (2x2) for a given angle (in radians). ' + 'Returns a 2-D rotation matrix (3x3) of a given angle (in radians) around given axis.', - examples: ['rotationMatrix(pi / 2)', 'rotationMatrix(unit("45deg"), [0, 0, 1])', 'rotationMatrix(1, matrix([0, 0, 1]), "sparse")'], - seealso: ['cos', 'sin'] - }; - // CONCATENATED MODULE: ./src/expression/embeddedDocs/embeddedDocs.js - - var embeddedDocs = { - // construction functions - bignumber: bignumberDocs, - "boolean": booleanDocs, - complex: complexDocs, - createUnit: createUnitDocs, - fraction: fractionDocs, - index: indexDocs, - matrix: matrixDocs, - number: numberDocs, - sparse: sparseDocs, - splitUnit: splitUnitDocs, - string: stringDocs, - unit: unitDocs, - // constants - e: eDocs, - E: eDocs, - "false": falseDocs, - i: iDocs, - Infinity: InfinityDocs, - LN2: LN2Docs, - LN10: LN10Docs, - LOG2E: LOG2EDocs, - LOG10E: LOG10EDocs, - NaN: NaNDocs, - "null": nullDocs, - pi: piDocs, - PI: piDocs, - phi: phiDocs, - SQRT1_2: SQRT12Docs, - SQRT2: SQRT2Docs, - tau: tauDocs, - "true": trueDocs, - version: versionDocs, - // physical constants - // TODO: more detailed docs for physical constants - speedOfLight: { - description: 'Speed of light in vacuum', - examples: ['speedOfLight'] - }, - gravitationConstant: { - description: 'Newtonian constant of gravitation', - examples: ['gravitationConstant'] - }, - planckConstant: { - description: 'Planck constant', - examples: ['planckConstant'] - }, - reducedPlanckConstant: { - description: 'Reduced Planck constant', - examples: ['reducedPlanckConstant'] - }, - magneticConstant: { - description: 'Magnetic constant (vacuum permeability)', - examples: ['magneticConstant'] - }, - electricConstant: { - description: 'Electric constant (vacuum permeability)', - examples: ['electricConstant'] - }, - vacuumImpedance: { - description: 'Characteristic impedance of vacuum', - examples: ['vacuumImpedance'] - }, - coulomb: { - description: 'Coulomb\'s constant', - examples: ['coulomb'] - }, - elementaryCharge: { - description: 'Elementary charge', - examples: ['elementaryCharge'] - }, - bohrMagneton: { - description: 'Borh magneton', - examples: ['bohrMagneton'] - }, - conductanceQuantum: { - description: 'Conductance quantum', - examples: ['conductanceQuantum'] - }, - inverseConductanceQuantum: { - description: 'Inverse conductance quantum', - examples: ['inverseConductanceQuantum'] - }, - // josephson: {description: 'Josephson constant', examples: ['josephson']}, - magneticFluxQuantum: { - description: 'Magnetic flux quantum', - examples: ['magneticFluxQuantum'] - }, - nuclearMagneton: { - description: 'Nuclear magneton', - examples: ['nuclearMagneton'] - }, - klitzing: { - description: 'Von Klitzing constant', - examples: ['klitzing'] - }, - bohrRadius: { - description: 'Borh radius', - examples: ['bohrRadius'] - }, - classicalElectronRadius: { - description: 'Classical electron radius', - examples: ['classicalElectronRadius'] - }, - electronMass: { - description: 'Electron mass', - examples: ['electronMass'] - }, - fermiCoupling: { - description: 'Fermi coupling constant', - examples: ['fermiCoupling'] - }, - fineStructure: { - description: 'Fine-structure constant', - examples: ['fineStructure'] - }, - hartreeEnergy: { - description: 'Hartree energy', - examples: ['hartreeEnergy'] - }, - protonMass: { - description: 'Proton mass', - examples: ['protonMass'] - }, - deuteronMass: { - description: 'Deuteron Mass', - examples: ['deuteronMass'] - }, - neutronMass: { - description: 'Neutron mass', - examples: ['neutronMass'] - }, - quantumOfCirculation: { - description: 'Quantum of circulation', - examples: ['quantumOfCirculation'] - }, - rydberg: { - description: 'Rydberg constant', - examples: ['rydberg'] - }, - thomsonCrossSection: { - description: 'Thomson cross section', - examples: ['thomsonCrossSection'] - }, - weakMixingAngle: { - description: 'Weak mixing angle', - examples: ['weakMixingAngle'] - }, - efimovFactor: { - description: 'Efimov factor', - examples: ['efimovFactor'] - }, - atomicMass: { - description: 'Atomic mass constant', - examples: ['atomicMass'] - }, - avogadro: { - description: 'Avogadro\'s number', - examples: ['avogadro'] - }, - boltzmann: { - description: 'Boltzmann constant', - examples: ['boltzmann'] - }, - faraday: { - description: 'Faraday constant', - examples: ['faraday'] - }, - firstRadiation: { - description: 'First radiation constant', - examples: ['firstRadiation'] - }, - loschmidt: { - description: 'Loschmidt constant at T=273.15 K and p=101.325 kPa', - examples: ['loschmidt'] - }, - gasConstant: { - description: 'Gas constant', - examples: ['gasConstant'] - }, - molarPlanckConstant: { - description: 'Molar Planck constant', - examples: ['molarPlanckConstant'] - }, - molarVolume: { - description: 'Molar volume of an ideal gas at T=273.15 K and p=101.325 kPa', - examples: ['molarVolume'] - }, - sackurTetrode: { - description: 'Sackur-Tetrode constant at T=1 K and p=101.325 kPa', - examples: ['sackurTetrode'] - }, - secondRadiation: { - description: 'Second radiation constant', - examples: ['secondRadiation'] - }, - stefanBoltzmann: { - description: 'Stefan-Boltzmann constant', - examples: ['stefanBoltzmann'] - }, - wienDisplacement: { - description: 'Wien displacement law constant', - examples: ['wienDisplacement'] - }, - // spectralRadiance: {description: 'First radiation constant for spectral radiance', examples: ['spectralRadiance']}, - molarMass: { - description: 'Molar mass constant', - examples: ['molarMass'] - }, - molarMassC12: { - description: 'Molar mass constant of carbon-12', - examples: ['molarMassC12'] - }, - gravity: { - description: 'Standard acceleration of gravity (standard acceleration of free-fall on Earth)', - examples: ['gravity'] - }, - planckLength: { - description: 'Planck length', - examples: ['planckLength'] - }, - planckMass: { - description: 'Planck mass', - examples: ['planckMass'] - }, - planckTime: { - description: 'Planck time', - examples: ['planckTime'] - }, - planckCharge: { - description: 'Planck charge', - examples: ['planckCharge'] - }, - planckTemperature: { - description: 'Planck temperature', - examples: ['planckTemperature'] - }, - // functions - algebra - derivative: derivativeDocs, - lsolve: lsolveDocs, - lsolveAll: lsolveAllDocs, - lup: lupDocs, - lusolve: lusolveDocs, - simplify: simplifyDocs, - rationalize: rationalizeDocs, - slu: sluDocs, - usolve: usolveDocs, - usolveAll: usolveAllDocs, - qr: qrDocs, - // functions - arithmetic - abs: absDocs, - add: addDocs, - cbrt: cbrtDocs, - ceil: ceilDocs, - cube: cubeDocs, - divide: divideDocs, - dotDivide: dotDivideDocs, - dotMultiply: dotMultiplyDocs, - dotPow: dotPowDocs, - exp: expDocs, - expm: expmDocs, - expm1: expm1Docs, - fix: fixDocs, - floor: floorDocs, - gcd: gcdDocs, - hypot: hypotDocs, - lcm: lcmDocs, - log: logDocs, - log2: log2Docs, - log1p: log1pDocs, - log10: log10Docs, - mod: modDocs, - multiply: multiplyDocs, - norm: normDocs, - nthRoot: nthRootDocs, - nthRoots: nthRootsDocs, - pow: powDocs, - round: roundDocs, - sign: signDocs, - sqrt: sqrtDocs, - sqrtm: sqrtmDocs, - square: squareDocs, - subtract: subtractDocs, - unaryMinus: unaryMinusDocs, - unaryPlus: unaryPlusDocs, - xgcd: xgcdDocs, - // functions - bitwise - bitAnd: bitAndDocs, - bitNot: bitNotDocs, - bitOr: bitOrDocs, - bitXor: bitXorDocs, - leftShift: leftShiftDocs, - rightArithShift: rightArithShiftDocs, - rightLogShift: rightLogShiftDocs, - // functions - combinatorics - bellNumbers: bellNumbersDocs, - catalan: catalanDocs, - composition: compositionDocs, - stirlingS2: stirlingS2Docs, - // functions - core - config: configDocs, - "import": importDocs, - typed: typedDocs, - // functions - complex - arg: argDocs, - conj: conjDocs, - re: reDocs, - im: imDocs, - // functions - expression - evaluate: evaluateDocs, - help: helpDocs, - // functions - geometry - distance: distanceDocs, - intersect: intersectDocs, - // functions - logical - and: andDocs, - not: notDocs, - or: orDocs, - xor: xorDocs, - // functions - matrix - concat: concatDocs, - cross: crossDocs, - column: columnDocs, - ctranspose: ctransposeDocs, - det: detDocs, - diag: diagDocs, - diff: diffDocs, - dot: dotDocs, - getMatrixDataType: getMatrixDataTypeDocs, - identity: identityDocs, - filter: filterDocs, - flatten: flattenDocs, - forEach: forEachDocs, - inv: invDocs, - eigs: eigsDocs, - kron: kronDocs, - map: mapDocs, - ones: onesDocs, - partitionSelect: partitionSelectDocs, - range: rangeDocs, - resize: resizeDocs, - reshape: reshapeDocs, - rotationMatrix: rotationMatrixDocs, - row: rowDocs, - size: sizeDocs, - sort: sortDocs, - squeeze: squeezeDocs, - subset: subsetDocs, - trace: traceDocs, - transpose: transposeDocs, - zeros: zerosDocs, - // functions - probability - combinations: combinationsDocs, - combinationsWithRep: combinationsWithRepDocs, - // distribution: distributionDocs, - factorial: factorialDocs, - gamma: gammaDocs, - kldivergence: kldivergenceDocs, - multinomial: multinomialDocs, - permutations: permutationsDocs, - pickRandom: pickRandomDocs, - random: randomDocs, - randomInt: randomIntDocs, - // functions - relational - compare: compareDocs, - compareNatural: compareNaturalDocs, - compareText: compareTextDocs, - deepEqual: deepEqualDocs, - equal: equalDocs, - equalText: equalTextDocs, - larger: largerDocs, - largerEq: largerEqDocs, - smaller: smallerDocs, - smallerEq: smallerEqDocs, - unequal: unequalDocs, - // functions - set - setCartesian: setCartesianDocs, - setDifference: setDifferenceDocs, - setDistinct: setDistinctDocs, - setIntersect: setIntersectDocs, - setIsSubset: setIsSubsetDocs, - setMultiplicity: setMultiplicityDocs, - setPowerset: setPowersetDocs, - setSize: setSizeDocs, - setSymDifference: setSymDifferenceDocs, - setUnion: setUnionDocs, - // functions - special - erf: erfDocs, - // functions - statistics - mad: madDocs, - max: maxDocs, - mean: meanDocs, - median: medianDocs, - min: minDocs, - mode: modeDocs, - prod: prodDocs, - quantileSeq: quantileSeqDocs, - std: stdDocs, - sum: sumDocs, - variance: varianceDocs, - // functions - trigonometry - acos: acosDocs, - acosh: acoshDocs, - acot: acotDocs, - acoth: acothDocs, - acsc: acscDocs, - acsch: acschDocs, - asec: asecDocs, - asech: asechDocs, - asin: asinDocs, - asinh: asinhDocs, - atan: atanDocs, - atanh: atanhDocs, - atan2: atan2Docs, - cos: cosDocs, - cosh: coshDocs, - cot: cotDocs, - coth: cothDocs, - csc: cscDocs, - csch: cschDocs, - sec: secDocs, - sech: sechDocs, - sin: sinDocs, - sinh: sinhDocs, - tan: tanDocs, - tanh: tanhDocs, - // functions - units - to: toDocs, - // functions - utils - clone: cloneDocs, - format: formatDocs, - bin: binDocs, - oct: octDocs, - hex: hexDocs, - isNaN: isNaNDocs, - isInteger: isIntegerDocs, - isNegative: isNegativeDocs, - isNumeric: isNumericDocs, - hasNumericValue: hasNumericValueDocs, - isPositive: isPositiveDocs, - isPrime: isPrimeDocs, - isZero: isZeroDocs, - // print: printDocs // TODO: add documentation for print as soon as the parser supports objects. - typeOf: typeOfDocs, - numeric: numericDocs - }; - // CONCATENATED MODULE: ./src/expression/function/help.js - - var help_name = 'help'; - var help_dependencies = ['typed', 'mathWithTransform', 'Help']; - var createHelp = /* #__PURE__ */Object(factory["a" /* factory */])(help_name, help_dependencies, function (_ref) { - var typed = _ref.typed, - mathWithTransform = _ref.mathWithTransform, - Help = _ref.Help; - - /** - * Retrieve help on a function or data type. - * Help files are retrieved from the embedded documentation in math.docs. - * - * Syntax: - * - * math.help(search) - * - * Examples: - * - * console.log(math.help('sin').toString()) - * console.log(math.help(math.add).toString()) - * console.log(math.help(math.add).toJSON()) - * - * @param {Function | string | Object} search A function or function name - * for which to get help - * @return {Help} A help object - */ - return typed(help_name, { - any: function any(search) { - var prop; - var searchName = search; - - if (typeof search !== 'string') { - for (prop in mathWithTransform) { - // search in functions and constants - if (Object(utils_object["f" /* hasOwnProperty */])(mathWithTransform, prop) && search === mathWithTransform[prop]) { - searchName = prop; - break; - } - } - /* TODO: implement help for data types - if (!text) { - // search data type - for (prop in math.type) { - if (hasOwnProperty(math, prop)) { - if (search === math.type[prop]) { - text = prop - break - } - } - } - } - */ - - } - - var doc = getSafeProperty(embeddedDocs, searchName); - - if (!doc) { - var searchText = typeof searchName === 'function' ? searchName.name : searchName; - throw new Error('No documentation found on "' + searchText + '"'); - } - - return new Help(doc); - } - }); - }); - // CONCATENATED MODULE: ./src/type/chain/function/chain.js - - var chain_name = 'chain'; - var chain_dependencies = ['typed', 'Chain']; - var createChain = /* #__PURE__ */Object(factory["a" /* factory */])(chain_name, chain_dependencies, function (_ref) { - var typed = _ref.typed, - Chain = _ref.Chain; - - /** - * Wrap any value in a chain, allowing to perform chained operations on - * the value. - * - * All methods available in the math.js library can be called upon the chain, - * and then will be evaluated with the value itself as first argument. - * The chain can be closed by executing `chain.done()`, which returns - * the final value. - * - * The chain has a number of special functions: - * - * - `done()` Finalize the chain and return the chain's value. - * - `valueOf()` The same as `done()` - * - `toString()` Executes `math.format()` onto the chain's value, returning - * a string representation of the value. - * - * Syntax: - * - * math.chain(value) - * - * Examples: - * - * math.chain(3) - * .add(4) - * .subtract(2) - * .done() // 5 - * - * math.chain( [[1, 2], [3, 4]] ) - * .subset(math.index(0, 0), 8) - * .multiply(3) - * .done() // [[24, 6], [9, 12]] - * - * @param {*} [value] A value of any type on which to start a chained operation. - * @return {math.Chain} The created chain - */ - return typed(chain_name, { - '': function _() { - return new Chain(); - }, - any: function any(value) { - return new Chain(value); - } - }); - }); - // CONCATENATED MODULE: ./src/function/matrix/det.js - - var det_name = 'det'; - var det_dependencies = ['typed', 'matrix', 'subtract', 'multiply', 'unaryMinus', 'lup']; - var createDet = /* #__PURE__ */Object(factory["a" /* factory */])(det_name, det_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - subtract = _ref.subtract, - multiply = _ref.multiply, - unaryMinus = _ref.unaryMinus, - lup = _ref.lup; - - /** - * Calculate the determinant of a matrix. - * - * Syntax: - * - * math.det(x) - * - * Examples: - * - * math.det([[1, 2], [3, 4]]) // returns -2 - * - * const A = [ - * [-2, 2, 3], - * [-1, 1, 3], - * [2, 0, -1] - * ] - * math.det(A) // returns 6 - * - * See also: - * - * inv - * - * @param {Array | Matrix} x A matrix - * @return {number} The determinant of `x` - */ - return typed(det_name, { - any: function any(x) { - return Object(utils_object["a" /* clone */])(x); - }, - 'Array | Matrix': function det(x) { - var size; - - if (Object(is["v" /* isMatrix */])(x)) { - size = x.size(); - } else if (Array.isArray(x)) { - x = matrix(x); - size = x.size(); - } else { - // a scalar - size = []; - } - - switch (size.length) { - case 0: - // scalar - return Object(utils_object["a" /* clone */])(x); - - case 1: - // vector - if (size[0] === 1) { - return Object(utils_object["a" /* clone */])(x.valueOf()[0]); - } else { - throw new RangeError('Matrix must be square ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - - case 2: - { - // two dimensional array - var rows = size[0]; - var cols = size[1]; - - if (rows === cols) { - return _det(x.clone().valueOf(), rows, cols); - } else { - throw new RangeError('Matrix must be square ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - } - - default: - // multi dimensional array - throw new RangeError('Matrix must be two dimensional ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - } - }); - /** - * Calculate the determinant of a matrix - * @param {Array[]} matrix A square, two dimensional matrix - * @param {number} rows Number of rows of the matrix (zero-based) - * @param {number} cols Number of columns of the matrix (zero-based) - * @returns {number} det - * @private - */ - - function _det(matrix, rows, cols) { - if (rows === 1) { - // this is a 1 x 1 matrix - return Object(utils_object["a" /* clone */])(matrix[0][0]); - } else if (rows === 2) { - // this is a 2 x 2 matrix - // the determinant of [a11,a12;a21,a22] is det = a11*a22-a21*a12 - return subtract(multiply(matrix[0][0], matrix[1][1]), multiply(matrix[1][0], matrix[0][1])); - } else { - // Compute the LU decomposition - var decomp = lup(matrix); // The determinant is the product of the diagonal entries of U (and those of L, but they are all 1) - - var det = decomp.U[0][0]; - - for (var _i = 1; _i < rows; _i++) { - det = multiply(det, decomp.U[_i][_i]); - } // The determinant will be multiplied by 1 or -1 depending on the parity of the permutation matrix. - // This can be determined by counting the cycles. This is roughly a linear time algorithm. - - var evenCycles = 0; - var i = 0; - var visited = []; - - while (true) { - while (visited[i]) { - i++; - } - - if (i >= rows) { - break; - } - var j = i; - var cycleLen = 0; - - while (!visited[decomp.p[j]]) { - visited[decomp.p[j]] = true; - j = decomp.p[j]; - cycleLen++; - } - - if (cycleLen % 2 === 0) { - evenCycles++; - } - } - - return evenCycles % 2 === 0 ? det : unaryMinus(det); - } - } - }); - // CONCATENATED MODULE: ./src/function/matrix/inv.js - - var inv_name = 'inv'; - var inv_dependencies = ['typed', 'matrix', 'divideScalar', 'addScalar', 'multiply', 'unaryMinus', 'det', 'identity', 'abs']; - var createInv = /* #__PURE__ */Object(factory["a" /* factory */])(inv_name, inv_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - divideScalar = _ref.divideScalar, - addScalar = _ref.addScalar, - multiply = _ref.multiply, - unaryMinus = _ref.unaryMinus, - det = _ref.det, - identity = _ref.identity, - abs = _ref.abs; - - /** - * Calculate the inverse of a square matrix. - * - * Syntax: - * - * math.inv(x) - * - * Examples: - * - * math.inv([[1, 2], [3, 4]]) // returns [[-2, 1], [1.5, -0.5]] - * math.inv(4) // returns 0.25 - * 1 / 4 // returns 0.25 - * - * See also: - * - * det, transpose - * - * @param {number | Complex | Array | Matrix} x Matrix to be inversed - * @return {number | Complex | Array | Matrix} The inverse of `x`. - */ - return typed(inv_name, { - 'Array | Matrix': function ArrayMatrix(x) { - var size = Object(is["v" /* isMatrix */])(x) ? x.size() : Object(utils_array["a" /* arraySize */])(x); - - switch (size.length) { - case 1: - // vector - if (size[0] === 1) { - if (Object(is["v" /* isMatrix */])(x)) { - return matrix([divideScalar(1, x.valueOf()[0])]); - } else { - return [divideScalar(1, x[0])]; - } - } else { - throw new RangeError('Matrix must be square ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - - case 2: - // two dimensional array - { - var rows = size[0]; - var cols = size[1]; - - if (rows === cols) { - if (Object(is["v" /* isMatrix */])(x)) { - return matrix(_inv(x.valueOf(), rows, cols), x.storage()); - } else { - // return an Array - return _inv(x, rows, cols); - } - } else { - throw new RangeError('Matrix must be square ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - } - - default: - // multi dimensional array - throw new RangeError('Matrix must be two dimensional ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - }, - any: function any(x) { - // scalar - return divideScalar(1, x); // FIXME: create a BigNumber one when configured for bignumbers - } - }); - /** - * Calculate the inverse of a square matrix - * @param {Array[]} mat A square matrix - * @param {number} rows Number of rows - * @param {number} cols Number of columns, must equal rows - * @return {Array[]} inv Inverse matrix - * @private - */ - - function _inv(mat, rows, cols) { - var r, s, f, value, temp; - - if (rows === 1) { - // this is a 1 x 1 matrix - value = mat[0][0]; - - if (value === 0) { - throw Error('Cannot calculate inverse, determinant is zero'); - } - - return [[divideScalar(1, value)]]; - } else if (rows === 2) { - // this is a 2 x 2 matrix - var d = det(mat); - - if (d === 0) { - throw Error('Cannot calculate inverse, determinant is zero'); - } - - return [[divideScalar(mat[1][1], d), divideScalar(unaryMinus(mat[0][1]), d)], [divideScalar(unaryMinus(mat[1][0]), d), divideScalar(mat[0][0], d)]]; - } else { - // this is a matrix of 3 x 3 or larger - // calculate inverse using gauss-jordan elimination - // https://en.wikipedia.org/wiki/Gaussian_elimination - // http://mathworld.wolfram.com/MatrixInverse.html - // http://math.uww.edu/~mcfarlat/inverse.htm - // make a copy of the matrix (only the arrays, not of the elements) - var A = mat.concat(); - - for (r = 0; r < rows; r++) { - A[r] = A[r].concat(); - } // create an identity matrix which in the end will contain the - // matrix inverse - - var B = identity(rows).valueOf(); // loop over all columns, and perform row reductions - - for (var c = 0; c < cols; c++) { - // Pivoting: Swap row c with row r, where row r contains the largest element A[r][c] - var ABig = abs(A[c][c]); - var rBig = c; - r = c + 1; - - while (r < rows) { - if (abs(A[r][c]) > ABig) { - ABig = abs(A[r][c]); - rBig = r; - } - - r++; - } - - if (ABig === 0) { - throw Error('Cannot calculate inverse, determinant is zero'); - } - - r = rBig; - - if (r !== c) { - temp = A[c]; - A[c] = A[r]; - A[r] = temp; - temp = B[c]; - B[c] = B[r]; - B[r] = temp; - } // eliminate non-zero values on the other rows at column c - - var Ac = A[c]; - var Bc = B[c]; - - for (r = 0; r < rows; r++) { - var Ar = A[r]; - var Br = B[r]; - - if (r !== c) { - // eliminate value at column c and row r - if (Ar[c] !== 0) { - f = divideScalar(unaryMinus(Ar[c]), Ac[c]); // add (f * row c) to row r to eliminate the value - // at column c - - for (s = c; s < cols; s++) { - Ar[s] = addScalar(Ar[s], multiply(f, Ac[s])); - } - - for (s = 0; s < cols; s++) { - Br[s] = addScalar(Br[s], multiply(f, Bc[s])); - } - } - } else { - // normalize value at Acc to 1, - // divide each value on row r with the value at Acc - f = Ac[c]; - - for (s = c; s < cols; s++) { - Ar[s] = divideScalar(Ar[s], f); - } - - for (s = 0; s < cols; s++) { - Br[s] = divideScalar(Br[s], f); - } - } - } - } - - return B; - } - } - }); - // CONCATENATED MODULE: ./src/function/matrix/eigs.js - - var eigs_name = 'eigs'; - var eigs_dependencies = ['config', 'typed', 'matrix', 'addScalar', 'equal', 'subtract', 'abs', 'atan', 'cos', 'sin', 'multiplyScalar', 'inv', 'bignumber', 'multiply', 'add']; - var createEigs = /* #__PURE__ */Object(factory["a" /* factory */])(eigs_name, eigs_dependencies, function (_ref) { - var config = _ref.config, - typed = _ref.typed, - matrix = _ref.matrix, - addScalar = _ref.addScalar, - subtract = _ref.subtract, - equal = _ref.equal, - abs = _ref.abs, - atan = _ref.atan, - cos = _ref.cos, - sin = _ref.sin, - multiplyScalar = _ref.multiplyScalar, - inv = _ref.inv, - bignumber = _ref.bignumber, - multiply = _ref.multiply, - add = _ref.add; - - /** - * Compute eigenvalue and eigenvector of a real symmetric matrix. - * Only applicable to two dimensional symmetric matrices. Uses Jacobi - * Algorithm. Matrix containing mixed type ('number', 'bignumber', 'fraction') - * of elements are not supported. Input matrix or 2D array should contain all elements - * of either 'number', 'bignumber' or 'fraction' type. For 'number' and 'fraction', the - * eigenvalues are of 'number' type. For 'bignumber' the eigenvalues are of ''bignumber' type. - * Eigenvectors are always of 'number' type. - * - * Syntax: - * - * math.eigs(x) - * - * Examples: - * - * const H = [[5, 2.3], [2.3, 1]] - * const ans = math.eigs(H) // returns {values: [E1,E2...sorted], vectors: [v1,v2.... corresponding vectors as columns]} - * const E = ans.values - * const U = ans.vectors - * math.multiply(H, math.column(U, 0)) // returns math.multiply(E[0], math.column(U, 0)) - * const UTxHxU = math.multiply(math.transpose(U), H, U) // rotates H to the eigen-representation - * E[0] == UTxHxU[0][0] // returns true - * See also: - * - * inv - * - * @param {Array | Matrix} x Matrix to be diagonalized - * @return {{values: Array, vectors: Array} | {values: Matrix, vectors: Matrix}} Object containing eigenvalues (Array or Matrix) and eigenvectors (2D Array/Matrix with eigenvectors as columns). - */ - return typed('eigs', { - Array: function Array(x) { - // check array size - var mat = matrix(x); - var size = mat.size(); - - if (size.length !== 2 || size[0] !== size[1]) { - throw new RangeError('Matrix must be square ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } // use dense 2D matrix implementation - - var ans = checkAndSubmit(mat, size[0]); - return { - values: ans[0], - vectors: ans[1] - }; - }, - Matrix: function Matrix(x) { - // use dense 2D array implementation - // dense matrix - var size = x.size(); - - if (size.length !== 2 || size[0] !== size[1]) { - throw new RangeError('Matrix must be square ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - - var ans = checkAndSubmit(x, size[0]); - return { - values: matrix(ans[0]), - vectors: matrix(ans[1]) - }; - } - }); // Is the matrix - // symmetric ? - - function isSymmetric(x, n) { - for (var i = 0; i < n; i++) { - for (var j = i; j < n; j++) { - // not symmtric - if (!equal(x[i][j], x[j][i])) { - throw new TypeError('Input matrix is not symmetric'); - } - } - } - } // check input for possible problems - // and perform diagonalization efficiently for - // specific type of number - - function checkAndSubmit(x, n) { - var type = x.datatype(); // type check - - if (type === undefined) { - type = x.getDataType(); - } - - if (type !== 'number' && type !== 'BigNumber' && type !== 'Fraction') { - if (type === 'mixed') { - throw new TypeError('Mixed matrix element type is not supported'); - } else { - throw new TypeError('Matrix element type not supported (' + type + ')'); - } - } else { - isSymmetric(x.toArray(), n); - } // perform efficient calculation for 'numbers' - - if (type === 'number') { - return diag(x.toArray()); - } else if (type === 'Fraction') { - var xArr = x.toArray(); // convert fraction to numbers - - for (var i = 0; i < n; i++) { - for (var j = i; j < n; j++) { - xArr[i][j] = xArr[i][j].valueOf(); - xArr[j][i] = xArr[i][j]; - } - } - - return diag(x.toArray()); - } else if (type === 'BigNumber') { - return diagBig(x.toArray()); - } - } // diagonalization implementation for number (efficient) - - function diag(x) { - var N = x.length; - var e0 = Math.abs(config.epsilon / N); - var psi; - var Sij = new Array(N); // Sij is Identity Matrix - - for (var i = 0; i < N; i++) { - Sij[i] = createArray(N, 0); - Sij[i][i] = 1.0; - } // initial error - - var Vab = getAij(x); - - while (Math.abs(Vab[1]) >= Math.abs(e0)) { - var _i = Vab[0][0]; - var j = Vab[0][1]; - psi = getTheta(x[_i][_i], x[j][j], x[_i][j]); - x = x1(x, psi, _i, j); - Sij = Sij1(Sij, psi, _i, j); - Vab = getAij(x); - } - - var Ei = createArray(N, 0); // eigenvalues - - for (var _i2 = 0; _i2 < N; _i2++) { - Ei[_i2] = x[_i2][_i2]; - } - - return sorting(Object(utils_object["a" /* clone */])(Ei), Object(utils_object["a" /* clone */])(Sij)); - } // diagonalization implementation for bigNumber - - function diagBig(x) { - var N = x.length; - var e0 = abs(config.epsilon / N); - var psi; - var Sij = new Array(N); // Sij is Identity Matrix - - for (var i = 0; i < N; i++) { - Sij[i] = createArray(N, 0); - Sij[i][i] = 1.0; - } // initial error - - var Vab = getAijBig(x); - - while (abs(Vab[1]) >= abs(e0)) { - var _i3 = Vab[0][0]; - var j = Vab[0][1]; - psi = getThetaBig(x[_i3][_i3], x[j][j], x[_i3][j]); - x = x1Big(x, psi, _i3, j); - Sij = Sij1Big(Sij, psi, _i3, j); - Vab = getAijBig(x); - } - - var Ei = createArray(N, 0); // eigenvalues - - for (var _i4 = 0; _i4 < N; _i4++) { - Ei[_i4] = x[_i4][_i4]; - } // return [clone(Ei), clone(Sij)] - - return sorting(Object(utils_object["a" /* clone */])(Ei), Object(utils_object["a" /* clone */])(Sij)); - } // get angle - - function getTheta(aii, ajj, aij) { - var denom = ajj - aii; - - if (Math.abs(denom) <= config.epsilon) { - return Math.PI / 4; - } else { - return 0.5 * Math.atan(2 * aij / (ajj - aii)); - } - } // get angle - - function getThetaBig(aii, ajj, aij) { - var denom = subtract(ajj, aii); - - if (abs(denom) <= config.epsilon) { - return bignumber(-1).acos().div(4); - } else { - return multiplyScalar(0.5, atan(multiply(2, aij, inv(denom)))); - } - } // update eigvec - - function Sij1(Sij, theta, i, j) { - var N = Sij.length; - var c = Math.cos(theta); - var s = Math.sin(theta); - var Ski = createArray(N, 0); - var Skj = createArray(N, 0); - - for (var k = 0; k < N; k++) { - Ski[k] = c * Sij[k][i] - s * Sij[k][j]; - Skj[k] = s * Sij[k][i] + c * Sij[k][j]; - } - - for (var _k = 0; _k < N; _k++) { - Sij[_k][i] = Ski[_k]; - Sij[_k][j] = Skj[_k]; - } - - return Sij; - } // update eigvec for overlap - - function Sij1Big(Sij, theta, i, j) { - var N = Sij.length; - var c = cos(theta); - var s = sin(theta); - var Ski = createArray(N, bignumber(0)); - var Skj = createArray(N, bignumber(0)); - - for (var k = 0; k < N; k++) { - Ski[k] = subtract(multiplyScalar(c, Sij[k][i]), multiplyScalar(s, Sij[k][j])); - Skj[k] = addScalar(multiplyScalar(s, Sij[k][i]), multiplyScalar(c, Sij[k][j])); - } - - for (var _k2 = 0; _k2 < N; _k2++) { - Sij[_k2][i] = Ski[_k2]; - Sij[_k2][j] = Skj[_k2]; - } - - return Sij; - } // update matrix - - function x1Big(Hij, theta, i, j) { - var N = Hij.length; - var c = bignumber(cos(theta)); - var s = bignumber(sin(theta)); - var c2 = multiplyScalar(c, c); - var s2 = multiplyScalar(s, s); - var Aki = createArray(N, bignumber(0)); - var Akj = createArray(N, bignumber(0)); // 2cs Hij - - var csHij = multiply(bignumber(2), c, s, Hij[i][j]); // Aii - - var Aii = addScalar(subtract(multiplyScalar(c2, Hij[i][i]), csHij), multiplyScalar(s2, Hij[j][j])); - var Ajj = add(multiplyScalar(s2, Hij[i][i]), csHij, multiplyScalar(c2, Hij[j][j])); // 0 to i - - for (var k = 0; k < N; k++) { - Aki[k] = subtract(multiplyScalar(c, Hij[i][k]), multiplyScalar(s, Hij[j][k])); - Akj[k] = addScalar(multiplyScalar(s, Hij[i][k]), multiplyScalar(c, Hij[j][k])); - } // Modify Hij - - Hij[i][i] = Aii; - Hij[j][j] = Ajj; - Hij[i][j] = bignumber(0); - Hij[j][i] = bignumber(0); // 0 to i - - for (var _k3 = 0; _k3 < N; _k3++) { - if (_k3 !== i && _k3 !== j) { - Hij[i][_k3] = Aki[_k3]; - Hij[_k3][i] = Aki[_k3]; - Hij[j][_k3] = Akj[_k3]; - Hij[_k3][j] = Akj[_k3]; - } - } - - return Hij; - } // update matrix - - function x1(Hij, theta, i, j) { - var N = Hij.length; - var c = Math.cos(theta); - var s = Math.sin(theta); - var c2 = c * c; - var s2 = s * s; - var Aki = createArray(N, 0); - var Akj = createArray(N, 0); // Aii - - var Aii = c2 * Hij[i][i] - 2 * c * s * Hij[i][j] + s2 * Hij[j][j]; - var Ajj = s2 * Hij[i][i] + 2 * c * s * Hij[i][j] + c2 * Hij[j][j]; // 0 to i - - for (var k = 0; k < N; k++) { - Aki[k] = c * Hij[i][k] - s * Hij[j][k]; - Akj[k] = s * Hij[i][k] + c * Hij[j][k]; - } // Modify Hij - - Hij[i][i] = Aii; - Hij[j][j] = Ajj; - Hij[i][j] = 0; - Hij[j][i] = 0; // 0 to i - - for (var _k4 = 0; _k4 < N; _k4++) { - if (_k4 !== i && _k4 !== j) { - Hij[i][_k4] = Aki[_k4]; - Hij[_k4][i] = Aki[_k4]; - Hij[j][_k4] = Akj[_k4]; - Hij[_k4][j] = Akj[_k4]; - } - } - - return Hij; - } // get max off-diagonal value from Upper Diagonal - - function getAij(Mij) { - var N = Mij.length; - var maxMij = 0; - var maxIJ = [0, 1]; - - for (var i = 0; i < N; i++) { - for (var j = i + 1; j < N; j++) { - if (Math.abs(maxMij) < Math.abs(Mij[i][j])) { - maxMij = Math.abs(Mij[i][j]); - maxIJ = [i, j]; - } - } - } - - return [maxIJ, maxMij]; - } // get max off-diagonal value from Upper Diagonal - - function getAijBig(Mij) { - var N = Mij.length; - var maxMij = 0; - var maxIJ = [0, 1]; - - for (var i = 0; i < N; i++) { - for (var j = i + 1; j < N; j++) { - if (abs(maxMij) < abs(Mij[i][j])) { - maxMij = abs(Mij[i][j]); - maxIJ = [i, j]; - } - } - } - - return [maxIJ, maxMij]; - } // sort results - - function sorting(E, S) { - var N = E.length; - var Ef = Array(N); - var Sf = Array(N); - - for (var k = 0; k < N; k++) { - Sf[k] = Array(N); - } - - for (var i = 0; i < N; i++) { - var minID = 0; - var minE = E[0]; - - for (var j = 0; j < E.length; j++) { - if (E[j] < minE) { - minID = j; - minE = E[minID]; - } - } - - Ef[i] = E.splice(minID, 1)[0]; - - for (var _k5 = 0; _k5 < N; _k5++) { - Sf[_k5][i] = S[_k5][minID]; - - S[_k5].splice(minID, 1); - } - } - - return [Object(utils_object["a" /* clone */])(Ef), Object(utils_object["a" /* clone */])(Sf)]; - } - /** - * Create an array of a certain size and fill all items with an initial value - * @param {number} size - * @param {number} value - * @return {number[]} - */ - - function createArray(size, value) { - // TODO: as soon as all browsers support Array.fill, use that instead (IE doesn't support it) - var array = new Array(size); - - for (var i = 0; i < size; i++) { - array[i] = value; - } - - return array; - } - }); - // CONCATENATED MODULE: ./src/function/matrix/expm.js - - var expm_name = 'expm'; - var expm_dependencies = ['typed', 'abs', 'add', 'identity', 'inv', 'multiply']; - var createExpm = /* #__PURE__ */Object(factory["a" /* factory */])(expm_name, expm_dependencies, function (_ref) { - var typed = _ref.typed, - abs = _ref.abs, - add = _ref.add, - identity = _ref.identity, - inv = _ref.inv, - multiply = _ref.multiply; - - /** - * Compute the matrix exponential, expm(A) = e^A. The matrix must be square. - * Not to be confused with exp(a), which performs element-wise - * exponentiation. - * - * The exponential is calculated using the Padé approximant with scaling and - * squaring; see "Nineteen Dubious Ways to Compute the Exponential of a - * Matrix," by Moler and Van Loan. - * - * Syntax: - * - * math.expm(x) - * - * Examples: - * - * const A = [[0,2],[0,0]] - * math.expm(A) // returns [[1,2],[0,1]] - * - * See also: - * - * exp - * - * @param {Matrix} x A square Matrix - * @return {Matrix} The exponential of x - */ - return typed(expm_name, { - Matrix: function Matrix(A) { - // Check matrix size - var size = A.size(); - - if (size.length !== 2 || size[0] !== size[1]) { - throw new RangeError('Matrix must be square ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - - var n = size[0]; // Desired accuracy of the approximant (The actual accuracy - // will be affected by round-off error) - - var eps = 1e-15; // The Padé approximant is not so accurate when the values of A - // are "large", so scale A by powers of two. Then compute the - // exponential, and square the result repeatedly according to - // the identity e^A = (e^(A/m))^m - // Compute infinity-norm of A, ||A||, to see how "big" it is - - var infNorm = infinityNorm(A); // Find the optimal scaling factor and number of terms in the - // Padé approximant to reach the desired accuracy - - var params = findParams(infNorm, eps); - var q = params.q; - var j = params.j; // The Pade approximation to e^A is: - // Rqq(A) = Dqq(A) ^ -1 * Nqq(A) - // where - // Nqq(A) = sum(i=0, q, (2q-i)!p! / [ (2q)!i!(q-i)! ] A^i - // Dqq(A) = sum(i=0, q, (2q-i)!q! / [ (2q)!i!(q-i)! ] (-A)^i - // Scale A by 1 / 2^j - - var Apos = multiply(A, Math.pow(2, -j)); // The i=0 term is just the identity matrix - - var N = identity(n); - var D = identity(n); // Initialization (i=0) - - var factor = 1; // Initialization (i=1) - - var AposToI = Apos; // Cloning not necessary - - var alternate = -1; - - for (var i = 1; i <= q; i++) { - if (i > 1) { - AposToI = multiply(AposToI, Apos); - alternate = -alternate; - } - - factor = factor * (q - i + 1) / ((2 * q - i + 1) * i); - N = add(N, multiply(factor, AposToI)); - D = add(D, multiply(factor * alternate, AposToI)); - } - - var R = multiply(inv(D), N); // Square j times - - for (var _i = 0; _i < j; _i++) { - R = multiply(R, R); - } - - return Object(is["H" /* isSparseMatrix */])(A) ? A.createSparseMatrix(R) : R; - } - }); - - function infinityNorm(A) { - var n = A.size()[0]; - var infNorm = 0; - - for (var i = 0; i < n; i++) { - var rowSum = 0; - - for (var j = 0; j < n; j++) { - rowSum += abs(A.get([i, j])); - } - - infNorm = Math.max(rowSum, infNorm); - } - - return infNorm; - } - /** - * Find the best parameters for the Pade approximant given - * the matrix norm and desired accuracy. Returns the first acceptable - * combination in order of increasing computational load. - */ - - function findParams(infNorm, eps) { - var maxSearchSize = 30; - - for (var k = 0; k < maxSearchSize; k++) { - for (var q = 0; q <= k; q++) { - var j = k - q; - - if (errorEstimate(infNorm, q, j) < eps) { - return { - q: q, - j: j - }; - } - } - } - - throw new Error('Could not find acceptable parameters to compute the matrix exponential (try increasing maxSearchSize in expm.js)'); - } - /** - * Returns the estimated error of the Pade approximant for the given - * parameters. - */ - - function errorEstimate(infNorm, q, j) { - var qfac = 1; - - for (var i = 2; i <= q; i++) { - qfac *= i; - } - - var twoqfac = qfac; - - for (var _i2 = q + 1; _i2 <= 2 * q; _i2++) { - twoqfac *= _i2; - } - - var twoqp1fac = twoqfac * (2 * q + 1); - return 8.0 * Math.pow(infNorm / Math.pow(2, j), 2 * q) * qfac * qfac / (twoqfac * twoqp1fac); - } - }); - // CONCATENATED MODULE: ./src/function/matrix/sqrtm.js - - var sqrtm_name = 'sqrtm'; - var sqrtm_dependencies = ['typed', 'abs', 'add', 'multiply', 'sqrt', 'subtract', 'inv', 'size', 'max', 'identity']; - var createSqrtm = /* #__PURE__ */Object(factory["a" /* factory */])(sqrtm_name, sqrtm_dependencies, function (_ref) { - var typed = _ref.typed, - abs = _ref.abs, - add = _ref.add, - multiply = _ref.multiply, - sqrt = _ref.sqrt, - subtract = _ref.subtract, - inv = _ref.inv, - size = _ref.size, - max = _ref.max, - identity = _ref.identity; - var _maxIterations = 1e3; - var _tolerance = 1e-6; - /** - * Calculate the principal square root matrix using the Denman–Beavers iterative method - * - * https://en.wikipedia.org/wiki/Square_root_of_a_matrix#By_Denman–Beavers_iteration - * - * @param {Array | Matrix} A The square matrix `A` - * @return {Array | Matrix} The principal square root of matrix `A` - * @private - */ - - function _denmanBeavers(A) { - var error; - var iterations = 0; - var Y = A; - var Z = identity(size(A)); - - do { - var Yk = Y; - Y = multiply(0.5, add(Yk, inv(Z))); - Z = multiply(0.5, add(Z, inv(Yk))); - error = max(abs(subtract(Y, Yk))); - - if (error > _tolerance && ++iterations > _maxIterations) { - throw new Error('computing square root of matrix: iterative method could not converge'); - } - } while (error > _tolerance); - - return Y; - } - /** - * Calculate the principal square root of a square matrix. - * The principal square root matrix `X` of another matrix `A` is such that `X * X = A`. - * - * https://en.wikipedia.org/wiki/Square_root_of_a_matrix - * - * Syntax: - * - * X = math.sqrtm(A) - * - * Examples: - * - * math.sqrtm([[1, 2], [3, 4]]) // returns [[-2, 1], [1.5, -0.5]] - * - * See also: - * - * sqrt, pow - * - * @param {Array | Matrix} A The square matrix `A` - * @return {Array | Matrix} The principal square root of matrix `A` - */ - - return typed(sqrtm_name, { - 'Array | Matrix': function ArrayMatrix(A) { - var size = Object(is["v" /* isMatrix */])(A) ? A.size() : Object(utils_array["a" /* arraySize */])(A); - - switch (size.length) { - case 1: - // Single element Array | Matrix - if (size[0] === 1) { - return sqrt(A); - } else { - throw new RangeError('Matrix must be square ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - - case 2: - { - // Two-dimensional Array | Matrix - var rows = size[0]; - var cols = size[1]; - - if (rows === cols) { - return _denmanBeavers(A); - } else { - throw new RangeError('Matrix must be square ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - } - - default: - // Multi dimensional array - throw new RangeError('Matrix must be at most two dimensional ' + '(size: ' + Object(utils_string["d" /* format */])(size) + ')'); - } - } - }); - }); - // CONCATENATED MODULE: ./src/function/arithmetic/divide.js - - var divide_name = 'divide'; - var divide_dependencies = ['typed', 'matrix', 'multiply', 'equalScalar', 'divideScalar', 'inv']; - var createDivide = /* #__PURE__ */Object(factory["a" /* factory */])(divide_name, divide_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - multiply = _ref.multiply, - equalScalar = _ref.equalScalar, - divideScalar = _ref.divideScalar, - inv = _ref.inv; - var algorithm11 = createAlgorithm11({ - typed: typed, - equalScalar: equalScalar - }); - var algorithm14 = createAlgorithm14({ - typed: typed - }); - /** - * Divide two values, `x / y`. - * To divide matrices, `x` is multiplied with the inverse of `y`: `x * inv(y)`. - * - * Syntax: - * - * math.divide(x, y) - * - * Examples: - * - * math.divide(2, 3) // returns number 0.6666666666666666 - * - * const a = math.complex(5, 14) - * const b = math.complex(4, 1) - * math.divide(a, b) // returns Complex 2 + 3i - * - * const c = [[7, -6], [13, -4]] - * const d = [[1, 2], [4, 3]] - * math.divide(c, d) // returns Array [[-9, 4], [-11, 6]] - * - * const e = math.unit('18 km') - * math.divide(e, 4.5) // returns Unit 4 km - * - * See also: - * - * multiply - * - * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Numerator - * @param {number | BigNumber | Fraction | Complex | Array | Matrix} y Denominator - * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Quotient, `x / y` - */ - - return typed('divide', Object(utils_object["e" /* extend */])({ - // we extend the signatures of divideScalar with signatures dealing with matrices - 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(x, y) { - // TODO: implement matrix right division using pseudo inverse - // https://www.mathworks.nl/help/matlab/ref/mrdivide.html - // https://www.gnu.org/software/octave/doc/interpreter/Arithmetic-Ops.html - // https://stackoverflow.com/questions/12263932/how-does-gnu-octave-matrix-division-work-getting-unexpected-behaviour - return multiply(x, inv(y)); - }, - 'DenseMatrix, any': function DenseMatrixAny(x, y) { - return algorithm14(x, y, divideScalar, false); - }, - 'SparseMatrix, any': function SparseMatrixAny(x, y) { - return algorithm11(x, y, divideScalar, false); - }, - 'Array, any': function ArrayAny(x, y) { - // use matrix implementation - return algorithm14(matrix(x), y, divideScalar, false).valueOf(); - }, - 'any, Array | Matrix': function anyArrayMatrix(x, y) { - return multiply(x, inv(y)); - } - }, divideScalar.signatures)); - }); - // CONCATENATED MODULE: ./src/function/geometry/distance.js - - var distance_name = 'distance'; - var distance_dependencies = ['typed', 'addScalar', 'subtract', 'divideScalar', 'multiplyScalar', 'unaryMinus', 'sqrt', 'abs']; - var createDistance = /* #__PURE__ */Object(factory["a" /* factory */])(distance_name, distance_dependencies, function (_ref) { - var typed = _ref.typed, - addScalar = _ref.addScalar, - subtract = _ref.subtract, - multiplyScalar = _ref.multiplyScalar, - divideScalar = _ref.divideScalar, - unaryMinus = _ref.unaryMinus, - sqrt = _ref.sqrt, - abs = _ref.abs; - - /** - * Calculates: - * The eucledian distance between two points in N-dimensional spaces. - * Distance between point and a line in 2 and 3 dimensional spaces. - * Pairwise distance between a set of 2D or 3D points - * NOTE: - * When substituting coefficients of a line(a, b and c), use ax + by + c = 0 instead of ax + by = c - * For parametric equation of a 3D line, x0, y0, z0, a, b, c are from: (x−x0, y−y0, z−z0) = t(a, b, c) - * - * Syntax: - * math.distance([x1, y1], [x2, y2]) - *- math.distance({pointOneX: 4, pointOneY: 5}, {pointTwoX: 2, pointTwoY: 7}) - * math.distance([x1, y1, z1], [x2, y2, z2]) - * math.distance({pointOneX: 4, pointOneY: 5, pointOneZ: 8}, {pointTwoX: 2, pointTwoY: 7, pointTwoZ: 9}) - * math.distance([x1, y1, ... , N1], [x2, y2, ... , N2]) - * math.distance([[A], [B], [C]...]) - * math.distance([x1, y1], [LinePtX1, LinePtY1], [LinePtX2, LinePtY2]) - * math.distance({pointX: 1, pointY: 4}, {lineOnePtX: 6, lineOnePtY: 3}, {lineTwoPtX: 2, lineTwoPtY: 8}) - * math.distance([x1, y1, z1], [LinePtX1, LinePtY1, LinePtZ1], [LinePtX2, LinePtY2, LinePtZ2]) - * math.distance({pointX: 1, pointY: 4, pointZ: 7}, {lineOnePtX: 6, lineOnePtY: 3, lineOnePtZ: 4}, {lineTwoPtX: 2, lineTwoPtY: 8, lineTwoPtZ: 5}) - * math.distance([x1, y1], [xCoeffLine, yCoeffLine, constant]) - * math.distance({pointX: 10, pointY: 10}, {xCoeffLine: 8, yCoeffLine: 1, constant: 3}) - * math.distance([x1, y1, z1], [x0, y0, z0, a-tCoeff, b-tCoeff, c-tCoeff]) point and parametric equation of 3D line - * math.distance([x, y, z], [x0, y0, z0, a, b, c]) - * math.distance({pointX: 2, pointY: 5, pointZ: 9}, {x0: 4, y0: 6, z0: 3, a: 4, b: 2, c: 0}) - * - * Examples: - * math.distance([0,0], [4,4]) // Returns 5.6569 - * math.distance( - * {pointOneX: 0, pointOneY: 0}, - * {pointTwoX: 10, pointTwoY: 10}) // Returns 14.142135623730951 - * math.distance([1, 0, 1], [4, -2, 2]) // Returns 3.74166 - * math.distance( - * {pointOneX: 4, pointOneY: 5, pointOneZ: 8}, - * {pointTwoX: 2, pointTwoY: 7, pointTwoZ: 9}) // Returns 3 - * math.distance([1, 0, 1, 0], [0, -1, 0, -1]) // Returns 2 - * math.distance([[1, 2], [1, 2], [1, 3]]) // Returns [0, 1, 1] - * math.distance([[1,2,4], [1,2,6], [8,1,3]]) // Returns [2, 7.14142842854285, 7.681145747868608] - * math.distance([10, 10], [8, 1, 3]) // Returns 11.535230316796387 - * math.distance([10, 10], [2, 3], [-8, 0]) // Returns 8.759953130362847 - * math.distance( - * {pointX: 1, pointY: 4}, - * {lineOnePtX: 6, lineOnePtY: 3}, - * {lineTwoPtX: 2, lineTwoPtY: 8}) // Returns 2.720549372624744 - * math.distance([2, 3, 1], [1, 1, 2, 5, 0, 1]) // Returns 2.3204774044612857 - * math.distance( - * {pointX: 2, pointY: 3, pointZ: 1}, - * {x0: 1, y0: 1, z0: 2, a: 5, b: 0, c: 1} // Returns 2.3204774044612857 - * - * @param {Array | Matrix | Object} x Co-ordinates of first point - * @param {Array | Matrix | Object} y Co-ordinates of second point - * @return {Number | BigNumber} Returns the distance from two/three points - */ - return typed(distance_name, { - 'Array, Array, Array': function ArrayArrayArray(x, y, z) { - // Point to Line 2D (x=Point, y=LinePoint1, z=LinePoint2) - if (x.length === 2 && y.length === 2 && z.length === 2) { - if (!_2d(x)) { - throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument'); - } - - if (!_2d(y)) { - throw new TypeError('Array with 2 numbers or BigNumbers expected for second argument'); - } - - if (!_2d(z)) { - throw new TypeError('Array with 2 numbers or BigNumbers expected for third argument'); - } - - var m = divideScalar(subtract(z[1], z[0]), subtract(y[1], y[0])); - var xCoeff = multiplyScalar(multiplyScalar(m, m), y[0]); - var yCoeff = unaryMinus(multiplyScalar(m, y[0])); - var constant = x[1]; - return _distancePointLine2D(x[0], x[1], xCoeff, yCoeff, constant); - } else { - throw new TypeError('Invalid Arguments: Try again'); - } - }, - 'Object, Object, Object': function ObjectObjectObject(x, y, z) { - if (Object.keys(x).length === 2 && Object.keys(y).length === 2 && Object.keys(z).length === 2) { - if (!_2d(x)) { - throw new TypeError('Values of pointX and pointY should be numbers or BigNumbers'); - } - - if (!_2d(y)) { - throw new TypeError('Values of lineOnePtX and lineOnePtY should be numbers or BigNumbers'); - } - - if (!_2d(z)) { - throw new TypeError('Values of lineTwoPtX and lineTwoPtY should be numbers or BigNumbers'); - } - - if ('pointX' in x && 'pointY' in x && 'lineOnePtX' in y && 'lineOnePtY' in y && 'lineTwoPtX' in z && 'lineTwoPtY' in z) { - var m = divideScalar(subtract(z.lineTwoPtY, z.lineTwoPtX), subtract(y.lineOnePtY, y.lineOnePtX)); - var xCoeff = multiplyScalar(multiplyScalar(m, m), y.lineOnePtX); - var yCoeff = unaryMinus(multiplyScalar(m, y.lineOnePtX)); - var constant = x.pointX; - return _distancePointLine2D(x.pointX, x.pointY, xCoeff, yCoeff, constant); - } else { - throw new TypeError('Key names do not match'); - } - } else { - throw new TypeError('Invalid Arguments: Try again'); - } - }, - 'Array, Array': function ArrayArray(x, y) { - // Point to Line 2D (x=[pointX, pointY], y=[x-coeff, y-coeff, const]) - if (x.length === 2 && y.length === 3) { - if (!_2d(x)) { - throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument'); - } - - if (!_3d(y)) { - throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument'); - } - - return _distancePointLine2D(x[0], x[1], y[0], y[1], y[2]); - } else if (x.length === 3 && y.length === 6) { - // Point to Line 3D - if (!_3d(x)) { - throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument'); - } - - if (!_parametricLine(y)) { - throw new TypeError('Array with 6 numbers or BigNumbers expected for second argument'); - } - - return _distancePointLine3D(x[0], x[1], x[2], y[0], y[1], y[2], y[3], y[4], y[5]); - } else if (x.length === y.length && x.length > 0) { - // Point to Point N-dimensions - if (!_containsOnlyNumbers(x)) { - throw new TypeError('All values of an array should be numbers or BigNumbers'); - } - - if (!_containsOnlyNumbers(y)) { - throw new TypeError('All values of an array should be numbers or BigNumbers'); - } - - return _euclideanDistance(x, y); - } else { - throw new TypeError('Invalid Arguments: Try again'); - } - }, - 'Object, Object': function ObjectObject(x, y) { - if (Object.keys(x).length === 2 && Object.keys(y).length === 3) { - if (!_2d(x)) { - throw new TypeError('Values of pointX and pointY should be numbers or BigNumbers'); - } - - if (!_3d(y)) { - throw new TypeError('Values of xCoeffLine, yCoeffLine and constant should be numbers or BigNumbers'); - } - - if ('pointX' in x && 'pointY' in x && 'xCoeffLine' in y && 'yCoeffLine' in y && 'constant' in y) { - return _distancePointLine2D(x.pointX, x.pointY, y.xCoeffLine, y.yCoeffLine, y.constant); - } else { - throw new TypeError('Key names do not match'); - } - } else if (Object.keys(x).length === 3 && Object.keys(y).length === 6) { - // Point to Line 3D - if (!_3d(x)) { - throw new TypeError('Values of pointX, pointY and pointZ should be numbers or BigNumbers'); - } - - if (!_parametricLine(y)) { - throw new TypeError('Values of x0, y0, z0, a, b and c should be numbers or BigNumbers'); - } - - if ('pointX' in x && 'pointY' in x && 'x0' in y && 'y0' in y && 'z0' in y && 'a' in y && 'b' in y && 'c' in y) { - return _distancePointLine3D(x.pointX, x.pointY, x.pointZ, y.x0, y.y0, y.z0, y.a, y.b, y.c); - } else { - throw new TypeError('Key names do not match'); - } - } else if (Object.keys(x).length === 2 && Object.keys(y).length === 2) { - // Point to Point 2D - if (!_2d(x)) { - throw new TypeError('Values of pointOneX and pointOneY should be numbers or BigNumbers'); - } - - if (!_2d(y)) { - throw new TypeError('Values of pointTwoX and pointTwoY should be numbers or BigNumbers'); - } - - if ('pointOneX' in x && 'pointOneY' in x && 'pointTwoX' in y && 'pointTwoY' in y) { - return _euclideanDistance([x.pointOneX, x.pointOneY], [y.pointTwoX, y.pointTwoY]); - } else { - throw new TypeError('Key names do not match'); - } - } else if (Object.keys(x).length === 3 && Object.keys(y).length === 3) { - // Point to Point 3D - if (!_3d(x)) { - throw new TypeError('Values of pointOneX, pointOneY and pointOneZ should be numbers or BigNumbers'); - } - - if (!_3d(y)) { - throw new TypeError('Values of pointTwoX, pointTwoY and pointTwoZ should be numbers or BigNumbers'); - } - - if ('pointOneX' in x && 'pointOneY' in x && 'pointOneZ' in x && 'pointTwoX' in y && 'pointTwoY' in y && 'pointTwoZ' in y) { - return _euclideanDistance([x.pointOneX, x.pointOneY, x.pointOneZ], [y.pointTwoX, y.pointTwoY, y.pointTwoZ]); - } else { - throw new TypeError('Key names do not match'); - } - } else { - throw new TypeError('Invalid Arguments: Try again'); - } - }, - Array: function Array(arr) { - if (!_pairwise(arr)) { - throw new TypeError('Incorrect array format entered for pairwise distance calculation'); - } - - return _distancePairwise(arr); - } - }); - - function _isNumber(a) { - // distance supports numbers and bignumbers - return typeof a === 'number' || Object(is["e" /* isBigNumber */])(a); - } - - function _2d(a) { - // checks if the number of arguments are correct in count and are valid (should be numbers) - if (a.constructor !== Array) { - a = _objectToArray(a); - } - - return _isNumber(a[0]) && _isNumber(a[1]); - } - - function _3d(a) { - // checks if the number of arguments are correct in count and are valid (should be numbers) - if (a.constructor !== Array) { - a = _objectToArray(a); - } - - return _isNumber(a[0]) && _isNumber(a[1]) && _isNumber(a[2]); - } - - function _containsOnlyNumbers(a) { - // checks if the number of arguments are correct in count and are valid (should be numbers) - if (!Array.isArray(a)) { - a = _objectToArray(a); - } - - return a.every(_isNumber); - } - - function _parametricLine(a) { - if (a.constructor !== Array) { - a = _objectToArray(a); - } - - return _isNumber(a[0]) && _isNumber(a[1]) && _isNumber(a[2]) && _isNumber(a[3]) && _isNumber(a[4]) && _isNumber(a[5]); - } - - function _objectToArray(o) { - var keys = Object.keys(o); - var a = []; - - for (var i = 0; i < keys.length; i++) { - a.push(o[keys[i]]); - } - - return a; - } - - function _pairwise(a) { - // checks for valid arguments passed to _distancePairwise(Array) - if (a[0].length === 2 && _isNumber(a[0][0]) && _isNumber(a[0][1])) { - if (a.some(function (aI) { - return aI.length !== 2 || !_isNumber(aI[0]) || !_isNumber(aI[1]); - })) { - return false; - } - } else if (a[0].length === 3 && _isNumber(a[0][0]) && _isNumber(a[0][1]) && _isNumber(a[0][2])) { - if (a.some(function (aI) { - return aI.length !== 3 || !_isNumber(aI[0]) || !_isNumber(aI[1]) || !_isNumber(aI[2]); - })) { - return false; - } - } else { - return false; - } - - return true; - } - - function _distancePointLine2D(x, y, a, b, c) { - var num = abs(addScalar(addScalar(multiplyScalar(a, x), multiplyScalar(b, y)), c)); - var den = sqrt(addScalar(multiplyScalar(a, a), multiplyScalar(b, b))); - return divideScalar(num, den); - } - - function _distancePointLine3D(x, y, z, x0, y0, z0, a, b, c) { - var num = [subtract(multiplyScalar(subtract(y0, y), c), multiplyScalar(subtract(z0, z), b)), subtract(multiplyScalar(subtract(z0, z), a), multiplyScalar(subtract(x0, x), c)), subtract(multiplyScalar(subtract(x0, x), b), multiplyScalar(subtract(y0, y), a))]; - num = sqrt(addScalar(addScalar(multiplyScalar(num[0], num[0]), multiplyScalar(num[1], num[1])), multiplyScalar(num[2], num[2]))); - var den = sqrt(addScalar(addScalar(multiplyScalar(a, a), multiplyScalar(b, b)), multiplyScalar(c, c))); - return divideScalar(num, den); - } - - function _euclideanDistance(x, y) { - var vectorSize = x.length; - var result = 0; - var diff = 0; - - for (var i = 0; i < vectorSize; i++) { - diff = subtract(x[i], y[i]); - result = addScalar(multiplyScalar(diff, diff), result); - } - - return sqrt(result); - } - - function _distancePairwise(a) { - var result = []; - var pointA = []; - var pointB = []; - - for (var i = 0; i < a.length - 1; i++) { - for (var j = i + 1; j < a.length; j++) { - if (a[0].length === 2) { - pointA = [a[i][0], a[i][1]]; - pointB = [a[j][0], a[j][1]]; - } else if (a[0].length === 3) { - pointA = [a[i][0], a[i][1], a[i][2]]; - pointB = [a[j][0], a[j][1], a[j][2]]; - } - - result.push(_euclideanDistance(pointA, pointB)); - } - } - - return result; - } - }); - // CONCATENATED MODULE: ./src/function/geometry/intersect.js - - var intersect_name = 'intersect'; - var intersect_dependencies = ['typed', 'config', 'abs', 'add', 'addScalar', 'matrix', 'multiply', 'multiplyScalar', 'divideScalar', 'subtract', 'smaller', 'equalScalar']; - var createIntersect = /* #__PURE__ */Object(factory["a" /* factory */])(intersect_name, intersect_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - abs = _ref.abs, - add = _ref.add, - addScalar = _ref.addScalar, - matrix = _ref.matrix, - multiply = _ref.multiply, - multiplyScalar = _ref.multiplyScalar, - divideScalar = _ref.divideScalar, - subtract = _ref.subtract, - smaller = _ref.smaller, - equalScalar = _ref.equalScalar; - - /** - * Calculates the point of intersection of two lines in two or three dimensions - * and of a line and a plane in three dimensions. The inputs are in the form of - * arrays or 1 dimensional matrices. The line intersection functions return null - * if the lines do not meet. - * - * Note: Fill the plane coefficients as `x + y + z = c` and not as `x + y + z + c = 0`. - * - * Syntax: - * - * math.intersect(endPoint1Line1, endPoint2Line1, endPoint1Line2, endPoint2Line2) - * math.intersect(endPoint1, endPoint2, planeCoefficients) - * - * Examples: - * - * math.intersect([0, 0], [10, 10], [10, 0], [0, 10]) // Returns [5, 5] - * math.intersect([0, 0, 0], [10, 10, 0], [10, 0, 0], [0, 10, 0]) // Returns [5, 5, 0] - * math.intersect([1, 0, 1], [4, -2, 2], [1, 1, 1, 6]) // Returns [7, -4, 3] - * - * @param {Array | Matrix} w Co-ordinates of first end-point of first line - * @param {Array | Matrix} x Co-ordinates of second end-point of first line - * @param {Array | Matrix} y Co-ordinates of first end-point of second line - * OR Co-efficients of the plane's equation - * @param {Array | Matrix} z Co-ordinates of second end-point of second line - * OR null if the calculation is for line and plane - * @return {Array} Returns the point of intersection of lines/lines-planes - */ - return typed('intersect', { - 'Array, Array, Array': function ArrayArrayArray(x, y, plane) { - if (!_3d(x)) { - throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument'); - } - - if (!_3d(y)) { - throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument'); - } - - if (!_4d(plane)) { - throw new TypeError('Array with 4 numbers expected as third argument'); - } - - return _intersectLinePlane(x[0], x[1], x[2], y[0], y[1], y[2], plane[0], plane[1], plane[2], plane[3]); - }, - 'Array, Array, Array, Array': function ArrayArrayArrayArray(w, x, y, z) { - if (w.length === 2) { - if (!_2d(w)) { - throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument'); - } - - if (!_2d(x)) { - throw new TypeError('Array with 2 numbers or BigNumbers expected for second argument'); - } - - if (!_2d(y)) { - throw new TypeError('Array with 2 numbers or BigNumbers expected for third argument'); - } - - if (!_2d(z)) { - throw new TypeError('Array with 2 numbers or BigNumbers expected for fourth argument'); - } - - return _intersect2d(w, x, y, z); - } else if (w.length === 3) { - if (!_3d(w)) { - throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument'); - } - - if (!_3d(x)) { - throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument'); - } - - if (!_3d(y)) { - throw new TypeError('Array with 3 numbers or BigNumbers expected for third argument'); - } - - if (!_3d(z)) { - throw new TypeError('Array with 3 numbers or BigNumbers expected for fourth argument'); - } - - return _intersect3d(w[0], w[1], w[2], x[0], x[1], x[2], y[0], y[1], y[2], z[0], z[1], z[2]); - } else { - throw new TypeError('Arrays with two or thee dimensional points expected'); - } - }, - 'Matrix, Matrix, Matrix': function MatrixMatrixMatrix(x, y, plane) { - return matrix(this(x.valueOf(), y.valueOf(), plane.valueOf())); - }, - 'Matrix, Matrix, Matrix, Matrix': function MatrixMatrixMatrixMatrix(w, x, y, z) { - // TODO: output matrix type should match input matrix type - return matrix(this(w.valueOf(), x.valueOf(), y.valueOf(), z.valueOf())); - } - }); - - function _isNumeric(a) { - // intersect supports numbers and bignumbers - return typeof a === 'number' || Object(is["e" /* isBigNumber */])(a); - } - - function _2d(x) { - return x.length === 2 && _isNumeric(x[0]) && _isNumeric(x[1]); - } - - function _3d(x) { - return x.length === 3 && _isNumeric(x[0]) && _isNumeric(x[1]) && _isNumeric(x[2]); - } - - function _4d(x) { - return x.length === 4 && _isNumeric(x[0]) && _isNumeric(x[1]) && _isNumeric(x[2]) && _isNumeric(x[3]); - } - - function _intersect2d(p1a, p1b, p2a, p2b) { - var o1 = p1a; - var o2 = p2a; - var d1 = subtract(o1, p1b); - var d2 = subtract(o2, p2b); - var det = subtract(multiplyScalar(d1[0], d2[1]), multiplyScalar(d2[0], d1[1])); - - if (smaller(abs(det), config.epsilon)) { - return null; - } - - var d20o11 = multiplyScalar(d2[0], o1[1]); - var d21o10 = multiplyScalar(d2[1], o1[0]); - var d20o21 = multiplyScalar(d2[0], o2[1]); - var d21o20 = multiplyScalar(d2[1], o2[0]); - var t = divideScalar(addScalar(subtract(subtract(d20o11, d21o10), d20o21), d21o20), det); - return add(multiply(d1, t), o1); - } - - function _intersect3dHelper(a, b, c, d, e, f, g, h, i, j, k, l) { - // (a - b)*(c - d) + (e - f)*(g - h) + (i - j)*(k - l) - var add1 = multiplyScalar(subtract(a, b), subtract(c, d)); - var add2 = multiplyScalar(subtract(e, f), subtract(g, h)); - var add3 = multiplyScalar(subtract(i, j), subtract(k, l)); - return addScalar(addScalar(add1, add2), add3); - } - - function _intersect3d(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) { - var d1343 = _intersect3dHelper(x1, x3, x4, x3, y1, y3, y4, y3, z1, z3, z4, z3); - - var d4321 = _intersect3dHelper(x4, x3, x2, x1, y4, y3, y2, y1, z4, z3, z2, z1); - - var d1321 = _intersect3dHelper(x1, x3, x2, x1, y1, y3, y2, y1, z1, z3, z2, z1); - - var d4343 = _intersect3dHelper(x4, x3, x4, x3, y4, y3, y4, y3, z4, z3, z4, z3); - - var d2121 = _intersect3dHelper(x2, x1, x2, x1, y2, y1, y2, y1, z2, z1, z2, z1); - - var ta = divideScalar(subtract(multiplyScalar(d1343, d4321), multiplyScalar(d1321, d4343)), subtract(multiplyScalar(d2121, d4343), multiplyScalar(d4321, d4321))); - var tb = divideScalar(addScalar(d1343, multiplyScalar(ta, d4321)), d4343); - var pax = addScalar(x1, multiplyScalar(ta, subtract(x2, x1))); - var pay = addScalar(y1, multiplyScalar(ta, subtract(y2, y1))); - var paz = addScalar(z1, multiplyScalar(ta, subtract(z2, z1))); - var pbx = addScalar(x3, multiplyScalar(tb, subtract(x4, x3))); - var pby = addScalar(y3, multiplyScalar(tb, subtract(y4, y3))); - var pbz = addScalar(z3, multiplyScalar(tb, subtract(z4, z3))); - - if (equalScalar(pax, pbx) && equalScalar(pay, pby) && equalScalar(paz, pbz)) { - return [pax, pay, paz]; - } else { - return null; - } - } - - function _intersectLinePlane(x1, y1, z1, x2, y2, z2, x, y, z, c) { - var x1x = multiplyScalar(x1, x); - var x2x = multiplyScalar(x2, x); - var y1y = multiplyScalar(y1, y); - var y2y = multiplyScalar(y2, y); - var z1z = multiplyScalar(z1, z); - var z2z = multiplyScalar(z2, z); - var t = divideScalar(subtract(subtract(subtract(c, x1x), y1y), z1z), subtract(subtract(subtract(addScalar(addScalar(x2x, y2y), z2z), x1x), y1y), z1z)); - var px = addScalar(x1, multiplyScalar(t, subtract(x2, x1))); - var py = addScalar(y1, multiplyScalar(t, subtract(y2, y1))); - var pz = addScalar(z1, multiplyScalar(t, subtract(z2, z1))); - return [px, py, pz]; // TODO: Add cases when line is parallel to the plane: - // (a) no intersection, - // (b) line contained in plane - } - }); - // CONCATENATED MODULE: ./src/function/statistics/sum.js - - var sum_name = 'sum'; - var sum_dependencies = ['typed', 'config', 'add', 'numeric']; - var createSum = /* #__PURE__ */Object(factory["a" /* factory */])(sum_name, sum_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - add = _ref.add, - numeric = _ref.numeric; - - /** - * Compute the sum of a matrix or a list with values. - * In case of a (multi dimensional) array or matrix, the sum of all - * elements will be calculated. - * - * Syntax: - * - * math.sum(a, b, c, ...) - * math.sum(A) - * - * Examples: - * - * math.sum(2, 1, 4, 3) // returns 10 - * math.sum([2, 1, 4, 3]) // returns 10 - * math.sum([[2, 5], [4, 3], [1, 7]]) // returns 22 - * - * See also: - * - * mean, median, min, max, prod, std, variance - * - * @param {... *} args A single matrix or or multiple scalar values - * @return {*} The sum of all values - */ - return typed(sum_name, { - // sum([a, b, c, d, ...]) - 'Array | Matrix': _sum, - // sum([a, b, c, d, ...], dim) - 'Array | Matrix, number | BigNumber': _nsumDim, - // sum(a, b, c, d, ...) - '...': function _(args) { - if (containsCollections(args)) { - throw new TypeError('Scalar values expected in function sum'); - } - - return _sum(args); - } - }); - /** - * Recursively calculate the sum of an n-dimensional array - * @param {Array | Matrix} array - * @return {number} sum - * @private - */ - - function _sum(array) { - var sum; - deepForEach(array, function (value) { - try { - sum = sum === undefined ? value : add(sum, value); - } catch (err) { - throw improveErrorMessage(err, 'sum', value); - } - }); // make sure returning numeric value: parse a string into a numeric value - - if (sum === undefined) { - sum = numeric(0, config.number); - } - - if (typeof sum === 'string') { - sum = numeric(sum, config.number); - } - - return sum; - } - - function _nsumDim(array, dim) { - try { - var sum = reduce(array, dim, add); - return sum; - } catch (err) { - throw improveErrorMessage(err, 'sum'); - } - } - }); - // CONCATENATED MODULE: ./src/function/statistics/mean.js - - var mean_name = 'mean'; - var mean_dependencies = ['typed', 'add', 'divide']; - var createMean = /* #__PURE__ */Object(factory["a" /* factory */])(mean_name, mean_dependencies, function (_ref) { - var typed = _ref.typed, - add = _ref.add, - divide = _ref.divide; - - /** - * Compute the mean value of matrix or a list with values. - * In case of a multi dimensional array, the mean of the flattened array - * will be calculated. When `dim` is provided, the maximum over the selected - * dimension will be calculated. Parameter `dim` is zero-based. - * - * Syntax: - * - * math.mean(a, b, c, ...) - * math.mean(A) - * math.mean(A, dim) - * - * Examples: - * - * math.mean(2, 1, 4, 3) // returns 2.5 - * math.mean([1, 2.7, 3.2, 4]) // returns 2.725 - * - * math.mean([[2, 5], [6, 3], [1, 7]], 0) // returns [3, 5] - * math.mean([[2, 5], [6, 3], [1, 7]], 1) // returns [3.5, 4.5, 4] - * - * See also: - * - * median, min, max, sum, prod, std, variance - * - * @param {... *} args A single matrix or or multiple scalar values - * @return {*} The mean of all values - */ - return typed(mean_name, { - // mean([a, b, c, d, ...]) - 'Array | Matrix': _mean, - // mean([a, b, c, d, ...], dim) - 'Array | Matrix, number | BigNumber': _nmeanDim, - // mean(a, b, c, d, ...) - '...': function _(args) { - if (containsCollections(args)) { - throw new TypeError('Scalar values expected in function mean'); - } - - return _mean(args); - } - }); - /** - * Calculate the mean value in an n-dimensional array, returning a - * n-1 dimensional array - * @param {Array} array - * @param {number} dim - * @return {number} mean - * @private - */ - - function _nmeanDim(array, dim) { - try { - var sum = reduce(array, dim, add); - var s = Array.isArray(array) ? Object(utils_array["a" /* arraySize */])(array) : array.size(); - return divide(sum, s[dim]); - } catch (err) { - throw improveErrorMessage(err, 'mean'); - } - } - /** - * Recursively calculate the mean value in an n-dimensional array - * @param {Array} array - * @return {number} mean - * @private - */ - - function _mean(array) { - var sum; - var num = 0; - deepForEach(array, function (value) { - try { - sum = sum === undefined ? value : add(sum, value); - num++; - } catch (err) { - throw improveErrorMessage(err, 'mean', value); - } - }); - - if (num === 0) { - throw new Error('Cannot calculate the mean of an empty array'); - } - - return divide(sum, num); - } - }); - // CONCATENATED MODULE: ./src/function/statistics/median.js - - var median_name = 'median'; - var median_dependencies = ['typed', 'add', 'divide', 'compare', 'partitionSelect']; - var createMedian = /* #__PURE__ */Object(factory["a" /* factory */])(median_name, median_dependencies, function (_ref) { - var typed = _ref.typed, - add = _ref.add, - divide = _ref.divide, - compare = _ref.compare, - partitionSelect = _ref.partitionSelect; - - /** - * Recursively calculate the median of an n-dimensional array - * @param {Array} array - * @return {Number} median - * @private - */ - function _median(array) { - try { - array = Object(utils_array["e" /* flatten */])(array.valueOf()); - var num = array.length; - - if (num === 0) { - throw new Error('Cannot calculate median of an empty array'); - } - - if (num % 2 === 0) { - // even: return the average of the two middle values - var mid = num / 2 - 1; - var right = partitionSelect(array, mid + 1); // array now partitioned at mid + 1, take max of left part - - var left = array[mid]; - - for (var i = 0; i < mid; ++i) { - if (compare(array[i], left) > 0) { - left = array[i]; - } - } - - return middle2(left, right); - } else { - // odd: return the middle value - var m = partitionSelect(array, (num - 1) / 2); - return middle(m); - } - } catch (err) { - throw improveErrorMessage(err, 'median'); - } - } // helper function to type check the middle value of the array - - var middle = typed({ - 'number | BigNumber | Complex | Unit': function numberBigNumberComplexUnit(value) { - return value; - } - }); // helper function to type check the two middle value of the array - - var middle2 = typed({ - 'number | BigNumber | Complex | Unit, number | BigNumber | Complex | Unit': function numberBigNumberComplexUnitNumberBigNumberComplexUnit(left, right) { - return divide(add(left, right), 2); - } - }); - /** - * Compute the median of a matrix or a list with values. The values are - * sorted and the middle value is returned. In case of an even number of - * values, the average of the two middle values is returned. - * Supported types of values are: Number, BigNumber, Unit - * - * In case of a (multi dimensional) array or matrix, the median of all - * elements will be calculated. - * - * Syntax: - * - * math.median(a, b, c, ...) - * math.median(A) - * - * Examples: - * - * math.median(5, 2, 7) // returns 5 - * math.median([3, -1, 5, 7]) // returns 4 - * - * See also: - * - * mean, min, max, sum, prod, std, variance, quantileSeq - * - * @param {... *} args A single matrix or or multiple scalar values - * @return {*} The median - */ - - return typed(median_name, { - // median([a, b, c, d, ...]) - 'Array | Matrix': _median, - // median([a, b, c, d, ...], dim) - 'Array | Matrix, number | BigNumber': function ArrayMatrixNumberBigNumber(array, dim) { - // TODO: implement median(A, dim) - throw new Error('median(A, dim) is not yet supported'); // return reduce(arguments[0], arguments[1], ...) - }, - // median(a, b, c, d, ...) - '...': function _(args) { - if (containsCollections(args)) { - throw new TypeError('Scalar values expected in function median'); - } - - return _median(args); - } - }); - }); - // CONCATENATED MODULE: ./src/function/statistics/mad.js - - var mad_name = 'mad'; - var mad_dependencies = ['typed', 'abs', 'map', 'median', 'subtract']; - var createMad = /* #__PURE__ */Object(factory["a" /* factory */])(mad_name, mad_dependencies, function (_ref) { - var typed = _ref.typed, - abs = _ref.abs, - map = _ref.map, - median = _ref.median, - subtract = _ref.subtract; - - /** - * Compute the median absolute deviation of a matrix or a list with values. - * The median absolute deviation is defined as the median of the absolute - * deviations from the median. - * - * Syntax: - * - * math.mad(a, b, c, ...) - * math.mad(A) - * - * Examples: - * - * math.mad(10, 20, 30) // returns 10 - * math.mad([1, 2, 3]) // returns 1 - * math.mad([[1, 2, 3], [4, 5, 6]]) // returns 1.5 - * - * See also: - * - * median, mean, std, abs - * - * @param {Array | Matrix} array - * A single matrix or multiple scalar values. - * @return {*} The median absolute deviation. - */ - return typed(mad_name, { - // mad([a, b, c, d, ...]) - 'Array | Matrix': _mad, - // mad(a, b, c, d, ...) - '...': function _(args) { - return _mad(args); - } - }); - - function _mad(array) { - array = Object(utils_array["e" /* flatten */])(array.valueOf()); - - if (array.length === 0) { - throw new Error('Cannot calculate median absolute deviation (mad) of an empty array'); - } - - try { - var med = median(array); - return median(map(array, function (value) { - return abs(subtract(value, med)); - })); - } catch (err) { - if (err instanceof TypeError && err.message.indexOf('median') !== -1) { - throw new TypeError(err.message.replace('median', 'mad')); - } else { - throw improveErrorMessage(err, 'mad'); - } - } - } - }); - // CONCATENATED MODULE: ./src/function/statistics/variance.js - - var DEFAULT_NORMALIZATION = 'unbiased'; - var variance_name = 'variance'; - var variance_dependencies = ['typed', 'add', 'subtract', 'multiply', 'divide', 'apply', 'isNaN']; - var createVariance = /* #__PURE__ */Object(factory["a" /* factory */])(variance_name, variance_dependencies, function (_ref) { - var typed = _ref.typed, - add = _ref.add, - subtract = _ref.subtract, - multiply = _ref.multiply, - divide = _ref.divide, - apply = _ref.apply, - isNaN = _ref.isNaN; - - /** - * Compute the variance of a matrix or a list with values. - * In case of a (multi dimensional) array or matrix, the variance over all - * elements will be calculated. - * - * Additionally, it is possible to compute the variance along the rows - * or columns of a matrix by specifying the dimension as the second argument. - * - * Optionally, the type of normalization can be specified as the final - * parameter. The parameter `normalization` can be one of the following values: - * - * - 'unbiased' (default) The sum of squared errors is divided by (n - 1) - * - 'uncorrected' The sum of squared errors is divided by n - * - 'biased' The sum of squared errors is divided by (n + 1) - * - * - * Note that older browser may not like the variable name `var`. In that - * case, the function can be called as `math['var'](...)` instead of - * `math.var(...)`. - * - * Syntax: - * - * math.variance(a, b, c, ...) - * math.variance(A) - * math.variance(A, normalization) - * math.variance(A, dimension) - * math.variance(A, dimension, normalization) - * - * Examples: - * - * math.variance(2, 4, 6) // returns 4 - * math.variance([2, 4, 6, 8]) // returns 6.666666666666667 - * math.variance([2, 4, 6, 8], 'uncorrected') // returns 5 - * math.variance([2, 4, 6, 8], 'biased') // returns 4 - * - * math.variance([[1, 2, 3], [4, 5, 6]]) // returns 3.5 - * math.variance([[1, 2, 3], [4, 6, 8]], 0) // returns [4.5, 8, 12.5] - * math.variance([[1, 2, 3], [4, 6, 8]], 1) // returns [1, 4] - * math.variance([[1, 2, 3], [4, 6, 8]], 1, 'biased') // returns [0.5, 2] - * - * See also: - * - * mean, median, max, min, prod, std, sum - * - * @param {Array | Matrix} array - * A single matrix or or multiple scalar values - * @param {string} [normalization='unbiased'] - * Determines how to normalize the variance. - * Choose 'unbiased' (default), 'uncorrected', or 'biased'. - * @param dimension {number | BigNumber} - * Determines the axis to compute the variance for a matrix - * @return {*} The variance - */ - return typed(variance_name, { - // variance([a, b, c, d, ...]) - 'Array | Matrix': function ArrayMatrix(array) { - return _var(array, DEFAULT_NORMALIZATION); - }, - // variance([a, b, c, d, ...], normalization) - 'Array | Matrix, string': _var, - // variance([a, b, c, c, ...], dim) - 'Array | Matrix, number | BigNumber': function ArrayMatrixNumberBigNumber(array, dim) { - return _varDim(array, dim, DEFAULT_NORMALIZATION); - }, - // variance([a, b, c, c, ...], dim, normalization) - 'Array | Matrix, number | BigNumber, string': _varDim, - // variance(a, b, c, d, ...) - '...': function _(args) { - return _var(args, DEFAULT_NORMALIZATION); - } - }); - /** - * Recursively calculate the variance of an n-dimensional array - * @param {Array} array - * @param {string} normalization - * Determines how to normalize the variance: - * - 'unbiased' The sum of squared errors is divided by (n - 1) - * - 'uncorrected' The sum of squared errors is divided by n - * - 'biased' The sum of squared errors is divided by (n + 1) - * @return {number | BigNumber} variance - * @private - */ - - function _var(array, normalization) { - var sum; - var num = 0; - - if (array.length === 0) { - throw new SyntaxError('Function variance requires one or more parameters (0 provided)'); - } // calculate the mean and number of elements - - deepForEach(array, function (value) { - try { - sum = sum === undefined ? value : add(sum, value); - num++; - } catch (err) { - throw improveErrorMessage(err, 'variance', value); - } - }); - if (num === 0) { - throw new Error('Cannot calculate variance of an empty array'); - } - var mean = divide(sum, num); // calculate the variance - - sum = undefined; - deepForEach(array, function (value) { - var diff = subtract(value, mean); - sum = sum === undefined ? multiply(diff, diff) : add(sum, multiply(diff, diff)); - }); - - if (isNaN(sum)) { - return sum; - } - - switch (normalization) { - case 'uncorrected': - return divide(sum, num); - - case 'biased': - return divide(sum, num + 1); - - case 'unbiased': - { - var zero = Object(is["e" /* isBigNumber */])(sum) ? sum.mul(0) : 0; - return num === 1 ? zero : divide(sum, num - 1); - } - - default: - throw new Error('Unknown normalization "' + normalization + '". ' + 'Choose "unbiased" (default), "uncorrected", or "biased".'); - } - } - - function _varDim(array, dim, normalization) { - try { - if (array.length === 0) { - throw new SyntaxError('Function variance requires one or more parameters (0 provided)'); - } - - return apply(array, dim, function (x) { - return _var(x, normalization); - }); - } catch (err) { - throw improveErrorMessage(err, 'variance'); - } - } - }); - // CONCATENATED MODULE: ./src/function/statistics/quantileSeq.js - - var quantileSeq_name = 'quantileSeq'; - var quantileSeq_dependencies = ['typed', 'add', 'multiply', 'partitionSelect', 'compare']; - var createQuantileSeq = /* #__PURE__ */Object(factory["a" /* factory */])(quantileSeq_name, quantileSeq_dependencies, function (_ref) { - var typed = _ref.typed, - add = _ref.add, - multiply = _ref.multiply, - partitionSelect = _ref.partitionSelect, - compare = _ref.compare; - - /** - * Compute the prob order quantile of a matrix or a list with values. - * The sequence is sorted and the middle value is returned. - * Supported types of sequence values are: Number, BigNumber, Unit - * Supported types of probability are: Number, BigNumber - * - * In case of a (multi dimensional) array or matrix, the prob order quantile - * of all elements will be calculated. - * - * Syntax: - * - * math.quantileSeq(A, prob[, sorted]) - * math.quantileSeq(A, [prob1, prob2, ...][, sorted]) - * math.quantileSeq(A, N[, sorted]) - * - * Examples: - * - * math.quantileSeq([3, -1, 5, 7], 0.5) // returns 4 - * math.quantileSeq([3, -1, 5, 7], [1/3, 2/3]) // returns [3, 5] - * math.quantileSeq([3, -1, 5, 7], 2) // returns [3, 5] - * math.quantileSeq([-1, 3, 5, 7], 0.5, true) // returns 4 - * - * See also: - * - * median, mean, min, max, sum, prod, std, variance - * - * @param {Array, Matrix} data A single matrix or Array - * @param {Number, BigNumber, Array} probOrN prob is the order of the quantile, while N is - * the amount of evenly distributed steps of - * probabilities; only one of these options can - * be provided - * @param {Boolean} sorted=false is data sorted in ascending order - * @return {Number, BigNumber, Unit, Array} Quantile(s) - */ - function quantileSeq(data, probOrN, sorted) { - var probArr, dataArr, one; - - if (arguments.length < 2 || arguments.length > 3) { - throw new SyntaxError('Function quantileSeq requires two or three parameters'); - } - - if (Object(is["i" /* isCollection */])(data)) { - sorted = sorted || false; - - if (typeof sorted === 'boolean') { - dataArr = data.valueOf(); - - if (Object(is["y" /* isNumber */])(probOrN)) { - if (probOrN < 0) { - throw new Error('N/prob must be non-negative'); - } - - if (probOrN <= 1) { - // quantileSeq([a, b, c, d, ...], prob[,sorted]) - return _quantileSeq(dataArr, probOrN, sorted); - } - - if (probOrN > 1) { - // quantileSeq([a, b, c, d, ...], N[,sorted]) - if (!Object(utils_number["i" /* isInteger */])(probOrN)) { - throw new Error('N must be a positive integer'); - } - - var nPlusOne = probOrN + 1; - probArr = new Array(probOrN); - - for (var i = 0; i < probOrN;) { - probArr[i] = _quantileSeq(dataArr, ++i / nPlusOne, sorted); - } - - return probArr; - } - } - - if (Object(is["e" /* isBigNumber */])(probOrN)) { - var BigNumber = probOrN.constructor; - - if (probOrN.isNegative()) { - throw new Error('N/prob must be non-negative'); - } - - one = new BigNumber(1); - - if (probOrN.lte(one)) { - // quantileSeq([a, b, c, d, ...], prob[,sorted]) - return new BigNumber(_quantileSeq(dataArr, probOrN, sorted)); - } - - if (probOrN.gt(one)) { - // quantileSeq([a, b, c, d, ...], N[,sorted]) - if (!probOrN.isInteger()) { - throw new Error('N must be a positive integer'); - } // largest possible Array length is 2^32-1 - // 2^32 < 10^15, thus safe conversion guaranteed - - var intN = probOrN.toNumber(); - - if (intN > 4294967295) { - throw new Error('N must be less than or equal to 2^32-1, as that is the maximum length of an Array'); - } - - var _nPlusOne = new BigNumber(intN + 1); - - probArr = new Array(intN); - - for (var _i = 0; _i < intN;) { - probArr[_i] = new BigNumber(_quantileSeq(dataArr, new BigNumber(++_i).div(_nPlusOne), sorted)); - } - - return probArr; - } - } - - if (Array.isArray(probOrN)) { - // quantileSeq([a, b, c, d, ...], [prob1, prob2, ...][,sorted]) - probArr = new Array(probOrN.length); - - for (var _i2 = 0; _i2 < probArr.length; ++_i2) { - var currProb = probOrN[_i2]; - - if (Object(is["y" /* isNumber */])(currProb)) { - if (currProb < 0 || currProb > 1) { - throw new Error('Probability must be between 0 and 1, inclusive'); - } - } else if (Object(is["e" /* isBigNumber */])(currProb)) { - one = new currProb.constructor(1); - - if (currProb.isNegative() || currProb.gt(one)) { - throw new Error('Probability must be between 0 and 1, inclusive'); - } - } else { - throw new TypeError('Unexpected type of argument in function quantileSeq'); // FIXME: becomes redundant when converted to typed-function - } - - probArr[_i2] = _quantileSeq(dataArr, currProb, sorted); - } - - return probArr; - } - - throw new TypeError('Unexpected type of argument in function quantileSeq'); // FIXME: becomes redundant when converted to typed-function - } - - throw new TypeError('Unexpected type of argument in function quantileSeq'); // FIXME: becomes redundant when converted to typed-function - } - - throw new TypeError('Unexpected type of argument in function quantileSeq'); // FIXME: becomes redundant when converted to typed-function - } - /** - * Calculate the prob order quantile of an n-dimensional array. - * - * @param {Array} array - * @param {Number, BigNumber} prob - * @param {Boolean} sorted - * @return {Number, BigNumber, Unit} prob order quantile - * @private - */ - - function _quantileSeq(array, prob, sorted) { - var flat = Object(utils_array["e" /* flatten */])(array); - var len = flat.length; - - if (len === 0) { - throw new Error('Cannot calculate quantile of an empty sequence'); - } - - if (Object(is["y" /* isNumber */])(prob)) { - var _index = prob * (len - 1); - - var _fracPart = _index % 1; - - if (_fracPart === 0) { - var value = sorted ? flat[_index] : partitionSelect(flat, _index); - validate(value); - return value; - } - - var _integerPart = Math.floor(_index); - - var _left; - - var _right; - - if (sorted) { - _left = flat[_integerPart]; - _right = flat[_integerPart + 1]; - } else { - _right = partitionSelect(flat, _integerPart + 1); // max of partition is kth largest - - _left = flat[_integerPart]; - - for (var i = 0; i < _integerPart; ++i) { - if (compare(flat[i], _left) > 0) { - _left = flat[i]; - } - } - } - - validate(_left); - validate(_right); // Q(prob) = (1-f)*A[floor(index)] + f*A[floor(index)+1] - - return add(multiply(_left, 1 - _fracPart), multiply(_right, _fracPart)); - } // If prob is a BigNumber - - var index = prob.times(len - 1); - - if (index.isInteger()) { - index = index.toNumber(); - - var _value = sorted ? flat[index] : partitionSelect(flat, index); - - validate(_value); - return _value; - } - - var integerPart = index.floor(); - var fracPart = index.minus(integerPart); - var integerPartNumber = integerPart.toNumber(); - var left; - var right; - - if (sorted) { - left = flat[integerPartNumber]; - right = flat[integerPartNumber + 1]; - } else { - right = partitionSelect(flat, integerPartNumber + 1); // max of partition is kth largest - - left = flat[integerPartNumber]; - - for (var _i3 = 0; _i3 < integerPartNumber; ++_i3) { - if (compare(flat[_i3], left) > 0) { - left = flat[_i3]; - } - } - } - - validate(left); - validate(right); // Q(prob) = (1-f)*A[floor(index)] + f*A[floor(index)+1] - - var one = new fracPart.constructor(1); - return add(multiply(left, one.minus(fracPart)), multiply(right, fracPart)); - } - /** - * Check if array value types are valid, throw error otherwise. - * @param {number | BigNumber | Unit} x - * @param {number | BigNumber | Unit} x - * @private - */ - - var validate = typed({ - 'number | BigNumber | Unit': function numberBigNumberUnit(x) { - return x; - } - }); - return quantileSeq; - }); - // CONCATENATED MODULE: ./src/function/statistics/std.js - - var std_name = 'std'; - var std_dependencies = ['typed', 'sqrt', 'variance']; - var createStd = /* #__PURE__ */Object(factory["a" /* factory */])(std_name, std_dependencies, function (_ref) { - var typed = _ref.typed, - sqrt = _ref.sqrt, - variance = _ref.variance; - - /** - * Compute the standard deviation of a matrix or a list with values. - * The standard deviations is defined as the square root of the variance: - * `std(A) = sqrt(variance(A))`. - * In case of a (multi dimensional) array or matrix, the standard deviation - * over all elements will be calculated by default, unless an axis is specified - * in which case the standard deviation will be computed along that axis. - * - * Additionally, it is possible to compute the standard deviation along the rows - * or columns of a matrix by specifying the dimension as the second argument. - * - * Optionally, the type of normalization can be specified as the final - * parameter. The parameter `normalization` can be one of the following values: - * - * - 'unbiased' (default) The sum of squared errors is divided by (n - 1) - * - 'uncorrected' The sum of squared errors is divided by n - * - 'biased' The sum of squared errors is divided by (n + 1) - * - * - * Syntax: - * - * math.std(a, b, c, ...) - * math.std(A) - * math.std(A, normalization) - * math.std(A, dimension) - * math.std(A, dimension, normalization) - * - * Examples: - * - * math.std(2, 4, 6) // returns 2 - * math.std([2, 4, 6, 8]) // returns 2.581988897471611 - * math.std([2, 4, 6, 8], 'uncorrected') // returns 2.23606797749979 - * math.std([2, 4, 6, 8], 'biased') // returns 2 - * - * math.std([[1, 2, 3], [4, 5, 6]]) // returns 1.8708286933869707 - * math.std([[1, 2, 3], [4, 6, 8]], 0) // returns [2.1213203435596424, 2.8284271247461903, 3.5355339059327378] - * math.std([[1, 2, 3], [4, 6, 8]], 1) // returns [1, 2] - * math.std([[1, 2, 3], [4, 6, 8]], 1, 'biased') // returns [0.7071067811865476, 1.4142135623730951] - * - * See also: - * - * mean, median, max, min, prod, sum, variance - * - * @param {Array | Matrix} array - * A single matrix or or multiple scalar values - * @param {string} [normalization='unbiased'] - * Determines how to normalize the variance. - * Choose 'unbiased' (default), 'uncorrected', or 'biased'. - * @param dimension {number | BigNumber} - * Determines the axis to compute the standard deviation for a matrix - * @return {*} The standard deviation - */ - return typed(std_name, { - // std([a, b, c, d, ...]) - 'Array | Matrix': _std, - // std([a, b, c, d, ...], normalization) - 'Array | Matrix, string': _std, - // std([a, b, c, c, ...], dim) - 'Array | Matrix, number | BigNumber': _std, - // std([a, b, c, c, ...], dim, normalization) - 'Array | Matrix, number | BigNumber, string': _std, - // std(a, b, c, d, ...) - '...': function _(args) { - return _std(args); - } - }); - - function _std(array, normalization) { - if (array.length === 0) { - throw new SyntaxError('Function std requires one or more parameters (0 provided)'); - } - - try { - return sqrt(variance.apply(null, arguments)); - } catch (err) { - if (err instanceof TypeError && err.message.indexOf(' variance') !== -1) { - throw new TypeError(err.message.replace(' variance', ' std')); - } else { - throw err; - } - } - } - }); - // CONCATENATED MODULE: ./src/utils/product.js - /** @param {number} i - * @param {number} n - * @returns {number} product of i to n - */ - function product_product(i, n) { - if (n < i) { - return 1; - } - - if (n === i) { - return n; - } - - var half = n + i >> 1; // divide (n + i) by 2 and truncate to integer - - return product_product(i, half) * product_product(half + 1, n); - } - // CONCATENATED MODULE: ./src/plain/number/combinations.js - - function combinationsNumber(n, k) { - if (!Object(utils_number["i" /* isInteger */])(n) || n < 0) { - throw new TypeError('Positive integer value expected in function combinations'); - } - - if (!Object(utils_number["i" /* isInteger */])(k) || k < 0) { - throw new TypeError('Positive integer value expected in function combinations'); - } - - if (k > n) { - throw new TypeError('k must be less than or equal to n'); - } - - var nMinusk = n - k; - var prodrange; - - if (k < nMinusk) { - prodrange = product_product(nMinusk + 1, n); - return prodrange / product_product(1, k); - } - - prodrange = product_product(k + 1, n); - return prodrange / product_product(1, nMinusk); - } - combinationsNumber.signature = 'number, number'; - // CONCATENATED MODULE: ./src/function/probability/combinations.js - - var combinations_name = 'combinations'; - var combinations_dependencies = ['typed']; - var createCombinations = /* #__PURE__ */Object(factory["a" /* factory */])(combinations_name, combinations_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Compute the number of ways of picking `k` unordered outcomes from `n` - * possibilities. - * - * Combinations only takes integer arguments. - * The following condition must be enforced: k <= n. - * - * Syntax: - * - * math.combinations(n, k) - * - * Examples: - * - * math.combinations(7, 5) // returns 21 - * - * See also: - * - * combinationsWithRep, permutations, factorial - * - * @param {number | BigNumber} n Total number of objects in the set - * @param {number | BigNumber} k Number of objects in the subset - * @return {number | BigNumber} Number of possible combinations. - */ - return typed(combinations_name, { - 'number, number': combinationsNumber, - 'BigNumber, BigNumber': function BigNumberBigNumber(n, k) { - var BigNumber = n.constructor; - var result, i; - var nMinusk = n.minus(k); - var one = new BigNumber(1); - - if (!isPositiveInteger(n) || !isPositiveInteger(k)) { - throw new TypeError('Positive integer value expected in function combinations'); - } - - if (k.gt(n)) { - throw new TypeError('k must be less than n in function combinations'); - } - - result = one; - - if (k.lt(nMinusk)) { - for (i = one; i.lte(nMinusk); i = i.plus(one)) { - result = result.times(k.plus(i)).dividedBy(i); - } - } else { - for (i = one; i.lte(k); i = i.plus(one)) { - result = result.times(nMinusk.plus(i)).dividedBy(i); - } - } - - return result; - } // TODO: implement support for collection in combinations - - }); - }); - /** - * Test whether BigNumber n is a positive integer - * @param {BigNumber} n - * @returns {boolean} isPositiveInteger - */ - - function isPositiveInteger(n) { - return n.isInteger() && n.gte(0); - } - // CONCATENATED MODULE: ./src/function/probability/combinationsWithRep.js - - var combinationsWithRep_name = 'combinationsWithRep'; - var combinationsWithRep_dependencies = ['typed']; - var createCombinationsWithRep = /* #__PURE__ */Object(factory["a" /* factory */])(combinationsWithRep_name, combinationsWithRep_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Compute the number of ways of picking `k` unordered outcomes from `n` - * possibilities, allowing individual outcomes to be repeated more than once. - * - * CombinationsWithRep only takes integer arguments. - * The following condition must be enforced: k <= n + k -1. - * - * Syntax: - * - * math.combinationsWithRep(n, k) - * - * Examples: - * - * math.combinationsWithRep(7, 5) // returns 462 - * - * See also: - * - * combinations, permutations, factorial - * - * @param {number | BigNumber} n Total number of objects in the set - * @param {number | BigNumber} k Number of objects in the subset - * @return {number | BigNumber} Number of possible combinations with replacement. - */ - return typed(combinationsWithRep_name, { - 'number, number': function numberNumber(n, k) { - if (!Object(utils_number["i" /* isInteger */])(n) || n < 0) { - throw new TypeError('Positive integer value expected in function combinationsWithRep'); - } - - if (!Object(utils_number["i" /* isInteger */])(k) || k < 0) { - throw new TypeError('Positive integer value expected in function combinationsWithRep'); - } - - if (n < 1) { - throw new TypeError('k must be less than or equal to n + k - 1'); - } - - if (k < n - 1) { - var _prodrange = product_product(n, n + k - 1); - - return _prodrange / product_product(1, k); - } - - var prodrange = product_product(k + 1, n + k - 1); - return prodrange / product_product(1, n - 1); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(n, k) { - var BigNumber = n.constructor; - var result, i; - var one = new BigNumber(1); - var nMinusOne = n.minus(one); - - if (!combinationsWithRep_isPositiveInteger(n) || !combinationsWithRep_isPositiveInteger(k)) { - throw new TypeError('Positive integer value expected in function combinationsWithRep'); - } - - if (n.lt(one)) { - throw new TypeError('k must be less than or equal to n + k - 1 in function combinationsWithRep'); - } - - result = one; - - if (k.lt(nMinusOne)) { - for (i = one; i.lte(nMinusOne); i = i.plus(one)) { - result = result.times(k.plus(i)).dividedBy(i); - } - } else { - for (i = one; i.lte(k); i = i.plus(one)) { - result = result.times(nMinusOne.plus(i)).dividedBy(i); - } - } - - return result; - } - }); - }); - /** - * Test whether BigNumber n is a positive integer - * @param {BigNumber} n - * @returns {boolean} isPositiveInteger - */ - - function combinationsWithRep_isPositiveInteger(n) { - return n.isInteger() && n.gte(0); - } - // CONCATENATED MODULE: ./src/plain/number/probability.js - - function gammaNumber(n) { - var x; - - if (Object(utils_number["i" /* isInteger */])(n)) { - if (n <= 0) { - return isFinite(n) ? Infinity : NaN; - } - - if (n > 171) { - return Infinity; // Will overflow - } - - return product_product(1, n - 1); - } - - if (n < 0.5) { - return Math.PI / (Math.sin(Math.PI * n) * gammaNumber(1 - n)); - } - - if (n >= 171.35) { - return Infinity; // will overflow - } - - if (n > 85.0) { - // Extended Stirling Approx - var twoN = n * n; - var threeN = twoN * n; - var fourN = threeN * n; - var fiveN = fourN * n; - return Math.sqrt(2 * Math.PI / n) * Math.pow(n / Math.E, n) * (1 + 1 / (12 * n) + 1 / (288 * twoN) - 139 / (51840 * threeN) - 571 / (2488320 * fourN) + 163879 / (209018880 * fiveN) + 5246819 / (75246796800 * fiveN * n)); - } - - --n; - x = gammaP[0]; - - for (var i = 1; i < gammaP.length; ++i) { - x += gammaP[i] / (n + i); - } - - var t = n + gammaG + 0.5; - return Math.sqrt(2 * Math.PI) * Math.pow(t, n + 0.5) * Math.exp(-t) * x; - } - gammaNumber.signature = 'number'; // TODO: comment on the variables g and p - - var gammaG = 4.7421875; - var gammaP = [0.99999999999999709182, 57.156235665862923517, -59.597960355475491248, 14.136097974741747174, -0.49191381609762019978, 0.33994649984811888699e-4, 0.46523628927048575665e-4, -0.98374475304879564677e-4, 0.15808870322491248884e-3, -0.21026444172410488319e-3, 0.21743961811521264320e-3, -0.16431810653676389022e-3, 0.84418223983852743293e-4, -0.26190838401581408670e-4, 0.36899182659531622704e-5]; - // CONCATENATED MODULE: ./src/function/probability/gamma.js - - var gamma_name = 'gamma'; - var gamma_dependencies = ['typed', 'config', 'multiplyScalar', 'pow', 'BigNumber', 'Complex']; - var createGamma = /* #__PURE__ */Object(factory["a" /* factory */])(gamma_name, gamma_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - multiplyScalar = _ref.multiplyScalar, - pow = _ref.pow, - _BigNumber = _ref.BigNumber, - _Complex = _ref.Complex; - - /** - * Compute the gamma function of a value using Lanczos approximation for - * small values, and an extended Stirling approximation for large values. - * - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.gamma(n) - * - * Examples: - * - * math.gamma(5) // returns 24 - * math.gamma(-0.5) // returns -3.5449077018110335 - * math.gamma(math.i) // returns -0.15494982830180973 - 0.49801566811835596i - * - * See also: - * - * combinations, factorial, permutations - * - * @param {number | Array | Matrix} n A real or complex number - * @return {number | Array | Matrix} The gamma of `n` - */ - return typed(gamma_name, { - number: gammaNumber, - Complex: function Complex(n) { - if (n.im === 0) { - return this(n.re); - } - - n = new _Complex(n.re - 1, n.im); - var x = new _Complex(gammaP[0], 0); - - for (var i = 1; i < gammaP.length; ++i) { - var real = n.re + i; // x += p[i]/(n+i) - - var den = real * real + n.im * n.im; - - if (den !== 0) { - x.re += gammaP[i] * real / den; - x.im += -(gammaP[i] * n.im) / den; - } else { - x.re = gammaP[i] < 0 ? -Infinity : Infinity; - } - } - - var t = new _Complex(n.re + gammaG + 0.5, n.im); - var twoPiSqrt = Math.sqrt(2 * Math.PI); - n.re += 0.5; - var result = pow(t, n); - - if (result.im === 0) { - // sqrt(2*PI)*result - result.re *= twoPiSqrt; - } else if (result.re === 0) { - result.im *= twoPiSqrt; - } else { - result.re *= twoPiSqrt; - result.im *= twoPiSqrt; - } - - var r = Math.exp(-t.re); // exp(-t) - - t.re = r * Math.cos(-t.im); - t.im = r * Math.sin(-t.im); - return multiplyScalar(multiplyScalar(result, t), x); - }, - BigNumber: function BigNumber(n) { - if (n.isInteger()) { - return n.isNegative() || n.isZero() ? new _BigNumber(Infinity) : bigFactorial(n.minus(1)); - } - - if (!n.isFinite()) { - return new _BigNumber(n.isNegative() ? NaN : Infinity); - } - - throw new Error('Integer BigNumber expected'); - }, - 'Array | Matrix': function ArrayMatrix(n) { - return deepMap(n, this); - } - }); - /** - * Calculate factorial for a BigNumber - * @param {BigNumber} n - * @returns {BigNumber} Returns the factorial of n - */ - - function bigFactorial(n) { - if (n < 8) { - return new _BigNumber([1, 1, 2, 6, 24, 120, 720, 5040][n]); - } - - var precision = config.precision + (Math.log(n.toNumber()) | 0); - - var Big = _BigNumber.clone({ - precision: precision - }); - - if (n % 2 === 1) { - return n.times(bigFactorial(new _BigNumber(n - 1))); - } - - var p = n; - var prod = new Big(n); - var sum = n.toNumber(); - - while (p > 2) { - p -= 2; - sum += p; - prod = prod.times(sum); - } - - return new _BigNumber(prod.toPrecision(_BigNumber.precision)); - } - }); - // CONCATENATED MODULE: ./src/function/probability/factorial.js - - var factorial_name = 'factorial'; - var factorial_dependencies = ['typed', 'gamma']; - var createFactorial = /* #__PURE__ */Object(factory["a" /* factory */])(factorial_name, factorial_dependencies, function (_ref) { - var typed = _ref.typed, - gamma = _ref.gamma; - - /** - * Compute the factorial of a value - * - * Factorial only supports an integer value as argument. - * For matrices, the function is evaluated element wise. - * - * Syntax: - * - * math.factorial(n) - * - * Examples: - * - * math.factorial(5) // returns 120 - * math.factorial(3) // returns 6 - * - * See also: - * - * combinations, combinationsWithRep, gamma, permutations - * - * @param {number | BigNumber | Array | Matrix} n An integer number - * @return {number | BigNumber | Array | Matrix} The factorial of `n` - */ - return typed(factorial_name, { - number: function number(n) { - if (n < 0) { - throw new Error('Value must be non-negative'); - } - - return gamma(n + 1); - }, - BigNumber: function BigNumber(n) { - if (n.isNegative()) { - throw new Error('Value must be non-negative'); - } - - return gamma(n.plus(1)); - }, - 'Array | Matrix': function ArrayMatrix(n) { - return deepMap(n, this); - } - }); - }); - // CONCATENATED MODULE: ./src/function/probability/kldivergence.js - - var kldivergence_name = 'kldivergence'; - var kldivergence_dependencies = ['typed', 'matrix', 'divide', 'sum', 'multiply', 'dotDivide', 'log', 'isNumeric']; - var createKldivergence = /* #__PURE__ */Object(factory["a" /* factory */])(kldivergence_name, kldivergence_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - divide = _ref.divide, - sum = _ref.sum, - multiply = _ref.multiply, - dotDivide = _ref.dotDivide, - log = _ref.log, - isNumeric = _ref.isNumeric; - - /** - * Calculate the Kullback-Leibler (KL) divergence between two distributions - * - * Syntax: - * - * math.kldivergence(x, y) - * - * Examples: - * - * math.kldivergence([0.7,0.5,0.4], [0.2,0.9,0.5]) //returns 0.24376698773121153 - * - * - * @param {Array | Matrix} q First vector - * @param {Array | Matrix} p Second vector - * @return {number} Returns distance between q and p - */ - return typed(kldivergence_name, { - 'Array, Array': function ArrayArray(q, p) { - return _kldiv(matrix(q), matrix(p)); - }, - 'Matrix, Array': function MatrixArray(q, p) { - return _kldiv(q, matrix(p)); - }, - 'Array, Matrix': function ArrayMatrix(q, p) { - return _kldiv(matrix(q), p); - }, - 'Matrix, Matrix': function MatrixMatrix(q, p) { - return _kldiv(q, p); - } - }); - - function _kldiv(q, p) { - var plength = p.size().length; - var qlength = q.size().length; - - if (plength > 1) { - throw new Error('first object must be one dimensional'); - } - - if (qlength > 1) { - throw new Error('second object must be one dimensional'); - } - - if (plength !== qlength) { - throw new Error('Length of two vectors must be equal'); - } // Before calculation, apply normalization - - var sumq = sum(q); - - if (sumq === 0) { - throw new Error('Sum of elements in first object must be non zero'); - } - - var sump = sum(p); - - if (sump === 0) { - throw new Error('Sum of elements in second object must be non zero'); - } - - var qnorm = divide(q, sum(q)); - var pnorm = divide(p, sum(p)); - var result = sum(multiply(qnorm, log(dotDivide(qnorm, pnorm)))); - - if (isNumeric(result)) { - return result; - } else { - return Number.NaN; - } - } - }); - // CONCATENATED MODULE: ./src/function/probability/multinomial.js - - var multinomial_name = 'multinomial'; - var multinomial_dependencies = ['typed', 'add', 'divide', 'multiply', 'factorial', 'isInteger', 'isPositive']; - var createMultinomial = /* #__PURE__ */Object(factory["a" /* factory */])(multinomial_name, multinomial_dependencies, function (_ref) { - var typed = _ref.typed, - add = _ref.add, - divide = _ref.divide, - multiply = _ref.multiply, - factorial = _ref.factorial, - isInteger = _ref.isInteger, - isPositive = _ref.isPositive; - - /** - * Multinomial Coefficients compute the number of ways of picking a1, a2, ..., ai unordered outcomes from `n` possibilities. - * - * multinomial takes one array of integers as an argument. - * The following condition must be enforced: every ai <= 0 - * - * Syntax: - * - * math.multinomial(a) // a is an array type - * - * Examples: - * - * math.multinomial([1,2,1]) // returns 12 - * - * See also: - * - * combinations, factorial - * - * @param {number[] | BigNumber[]} a Integer numbers of objects in the subset - * @return {Number | BigNumber} Multinomial coefficient. - */ - return typed(multinomial_name, { - 'Array | Matrix': function ArrayMatrix(a) { - var sum = 0; - var denom = 1; - deepForEach(a, function (ai) { - if (!isInteger(ai) || !isPositive(ai)) { - throw new TypeError('Positive integer value expected in function multinomial'); - } - - sum = add(sum, ai); - denom = multiply(denom, factorial(ai)); - }); - return divide(factorial(sum), denom); - } - }); - }); - // CONCATENATED MODULE: ./src/function/probability/permutations.js - - var permutations_name = 'permutations'; - var permutations_dependencies = ['typed', 'factorial']; - var createPermutations = /* #__PURE__ */Object(factory["a" /* factory */])(permutations_name, permutations_dependencies, function (_ref) { - var typed = _ref.typed, - factorial = _ref.factorial; - - /** - * Compute the number of ways of obtaining an ordered subset of `k` elements - * from a set of `n` elements. - * - * Permutations only takes integer arguments. - * The following condition must be enforced: k <= n. - * - * Syntax: - * - * math.permutations(n) - * math.permutations(n, k) - * - * Examples: - * - * math.permutations(5) // 120 - * math.permutations(5, 3) // 60 - * - * See also: - * - * combinations, combinationsWithRep, factorial - * - * @param {number | BigNumber} n The number of objects in total - * @param {number | BigNumber} [k] The number of objects in the subset - * @return {number | BigNumber} The number of permutations - */ - return typed(permutations_name, { - 'number | BigNumber': factorial, - 'number, number': function numberNumber(n, k) { - if (!Object(utils_number["i" /* isInteger */])(n) || n < 0) { - throw new TypeError('Positive integer value expected in function permutations'); - } - - if (!Object(utils_number["i" /* isInteger */])(k) || k < 0) { - throw new TypeError('Positive integer value expected in function permutations'); - } - - if (k > n) { - throw new TypeError('second argument k must be less than or equal to first argument n'); - } // Permute n objects, k at a time - - return product_product(n - k + 1, n); - }, - 'BigNumber, BigNumber': function BigNumberBigNumber(n, k) { - var result, i; - - if (!permutations_isPositiveInteger(n) || !permutations_isPositiveInteger(k)) { - throw new TypeError('Positive integer value expected in function permutations'); - } - - if (k.gt(n)) { - throw new TypeError('second argument k must be less than or equal to first argument n'); - } - - var one = n.mul(0).add(1); - result = one; - - for (i = n.minus(k).plus(1); i.lte(n); i = i.plus(1)) { - result = result.times(i); - } - - return result; - } // TODO: implement support for collection in permutations - - }); - }); - /** - * Test whether BigNumber n is a positive integer - * @param {BigNumber} n - * @returns {boolean} isPositiveInteger - */ - - function permutations_isPositiveInteger(n) { - return n.isInteger() && n.gte(0); - } - // EXTERNAL MODULE: ./node_modules/seed-random/index.js - var seed_random = __webpack_require__(15); - var seed_random_default = /*#__PURE__*/__webpack_require__.n(seed_random); - - // CONCATENATED MODULE: ./src/function/probability/util/seededRNG.js - // create a random seed here to prevent an infinite loop from seed-random - // inside the factory. Reason is that math.random is defined as a getter/setter - // and seed-random generates a seed from the local entropy by reading every - // defined object including `math` itself. That means that whilst getting - // math.random, it tries to get math.random, etc... an infinite loop. - // See https://github.com/ForbesLindesay/seed-random/issues/6 - - var singletonRandom = /* #__PURE__ */seed_random_default()(); - function createRng(randomSeed) { - var random; // create a new random generator with given seed - - function setSeed(seed) { - random = seed === null ? singletonRandom : seed_random_default()(String(seed)); - } // initialize a seeded pseudo random number generator with config's random seed - - setSeed(randomSeed); // wrapper function so the rng can be updated via generator - - function rng() { - return random(); - } - - return rng; - } - // CONCATENATED MODULE: ./src/function/probability/pickRandom.js - - var pickRandom_name = 'pickRandom'; - var pickRandom_dependencies = ['typed', 'config', '?on']; - var createPickRandom = /* #__PURE__ */Object(factory["a" /* factory */])(pickRandom_name, pickRandom_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - on = _ref.on; - // seeded pseudo random number generator - var rng = createRng(config.randomSeed); - - if (on) { - on('config', function (curr, prev) { - if (curr.randomSeed !== prev.randomSeed) { - rng = createRng(curr.randomSeed); - } - }); - } - /** - * Random pick one or more values from a one dimensional array. - * Array elements are picked using a random function with uniform or weighted distribution. - * - * Syntax: - * - * math.pickRandom(array) - * math.pickRandom(array, number) - * math.pickRandom(array, weights) - * math.pickRandom(array, number, weights) - * math.pickRandom(array, weights, number) - * - * Examples: - * - * math.pickRandom([3, 6, 12, 2]) // returns one of the values in the array - * math.pickRandom([3, 6, 12, 2], 2) // returns an array of two of the values in the array - * math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1]) // returns one of the values in the array with weighted distribution - * math.pickRandom([3, 6, 12, 2], 2, [1, 3, 2, 1]) // returns an array of two of the values in the array with weighted distribution - * math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1], 2) // returns an array of two of the values in the array with weighted distribution - * - * See also: - * - * random, randomInt - * - * @param {Array | Matrix} array A one dimensional array - * @param {Int} number An int or float - * @param {Array | Matrix} weights An array of ints or floats - * @return {number | Array} Returns a single random value from array when number is 1 or undefined. - * Returns an array with the configured number of elements when number is > 1. - */ - - return typed({ - 'Array | Matrix': function ArrayMatrix(possibles) { - return _pickRandom(possibles); - }, - 'Array | Matrix, number': function ArrayMatrixNumber(possibles, number) { - return _pickRandom(possibles, number, undefined); - }, - 'Array | Matrix, Array': function ArrayMatrixArray(possibles, weights) { - return _pickRandom(possibles, undefined, weights); - }, - 'Array | Matrix, Array | Matrix, number': function ArrayMatrixArrayMatrixNumber(possibles, weights, number) { - return _pickRandom(possibles, number, weights); - }, - 'Array | Matrix, number, Array | Matrix': function ArrayMatrixNumberArrayMatrix(possibles, number, weights) { - return _pickRandom(possibles, number, weights); - } - }); - - function _pickRandom(possibles, number, weights) { - var single = typeof number === 'undefined'; - - if (single) { - number = 1; - } - - possibles = Object(utils_array["e" /* flatten */])(possibles.valueOf()).valueOf(); // get Array - - if (weights) { - weights = weights.valueOf(); // get Array - } - - var totalWeights = 0; - - if (typeof weights !== 'undefined') { - if (weights.length !== possibles.length) { - throw new Error('Weights must have the same length as possibles'); - } - - for (var i = 0, len = weights.length; i < len; i++) { - if (!Object(is["y" /* isNumber */])(weights[i]) || weights[i] < 0) { - throw new Error('Weights must be an array of positive numbers'); - } - - totalWeights += weights[i]; - } - } - - var length = possibles.length; - - if (length === 0) { - return []; - } else if (number >= length) { - return number > 1 ? possibles : possibles[0]; - } - - var result = []; - var pick; - - while (result.length < number) { - if (typeof weights === 'undefined') { - pick = possibles[Math.floor(rng() * length)]; - } else { - var randKey = rng() * totalWeights; - - for (var _i = 0, _len = possibles.length; _i < _len; _i++) { - randKey -= weights[_i]; - - if (randKey < 0) { - pick = possibles[_i]; - break; - } - } - } - - if (result.indexOf(pick) === -1) { - result.push(pick); - } - } - - return single ? result[0] : result; // TODO: return matrix when input was a matrix - // TODO: add support for multi dimensional matrices - } - }); - // CONCATENATED MODULE: ./src/function/probability/util/randomMatrix.js - /** - * This is a util function for generating a random matrix recursively. - * @param {number[]} size - * @param {function} random - * @returns {Array} - */ - function randomMatrix(size, random) { - var data = []; - size = size.slice(0); - - if (size.length > 1) { - for (var i = 0, length = size.shift(); i < length; i++) { - data.push(randomMatrix(size, random)); - } - } else { - for (var _i = 0, _length = size.shift(); _i < _length; _i++) { - data.push(random()); - } - } - - return data; - } - // CONCATENATED MODULE: ./src/function/probability/random.js - - var random_name = 'random'; - var random_dependencies = ['typed', 'config', '?on']; - var createRandom = /* #__PURE__ */Object(factory["a" /* factory */])(random_name, random_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - on = _ref.on; - // seeded pseudo random number generator - var rng = createRng(config.randomSeed); - - if (on) { - on('config', function (curr, prev) { - if (curr.randomSeed !== prev.randomSeed) { - rng = createRng(curr.randomSeed); - } - }); - } - /** - * Return a random number larger or equal to `min` and smaller than `max` - * using a uniform distribution. - * - * Syntax: - * - * math.random() // generate a random number between 0 and 1 - * math.random(max) // generate a random number between 0 and max - * math.random(min, max) // generate a random number between min and max - * math.random(size) // generate a matrix with random numbers between 0 and 1 - * math.random(size, max) // generate a matrix with random numbers between 0 and max - * math.random(size, min, max) // generate a matrix with random numbers between min and max - * - * Examples: - * - * math.random() // returns a random number between 0 and 1 - * math.random(100) // returns a random number between 0 and 100 - * math.random(30, 40) // returns a random number between 30 and 40 - * math.random([2, 3]) // returns a 2x3 matrix with random numbers between 0 and 1 - * - * See also: - * - * randomInt, pickRandom - * - * @param {Array | Matrix} [size] If provided, an array or matrix with given - * size and filled with random values is returned - * @param {number} [min] Minimum boundary for the random value, included - * @param {number} [max] Maximum boundary for the random value, excluded - * @return {number | Array | Matrix} A random number - */ - - return typed(random_name, { - '': function _() { - return _random(0, 1); - }, - number: function number(max) { - return _random(0, max); - }, - 'number, number': function numberNumber(min, max) { - return _random(min, max); - }, - 'Array | Matrix': function ArrayMatrix(size) { - return _randomMatrix(size, 0, 1); - }, - 'Array | Matrix, number': function ArrayMatrixNumber(size, max) { - return _randomMatrix(size, 0, max); - }, - 'Array | Matrix, number, number': function ArrayMatrixNumberNumber(size, min, max) { - return _randomMatrix(size, min, max); - } - }); - - function _randomMatrix(size, min, max) { - var res = randomMatrix(size.valueOf(), function () { - return _random(min, max); - }); - return Object(is["v" /* isMatrix */])(size) ? size.create(res) : res; - } - - function _random(min, max) { - return min + rng() * (max - min); - } - }); // number only implementation of random, no matrix support - // TODO: there is quite some duplicate code in both createRandom and createRandomNumber, can we improve that? - - var createRandomNumber = /* #__PURE__ */Object(factory["a" /* factory */])(random_name, ['typed', 'config', '?on'], function (_ref2) { - var typed = _ref2.typed, - config = _ref2.config, - on = _ref2.on, - matrix = _ref2.matrix; - // seeded pseudo random number generator1 - var rng = createRng(config.randomSeed); - - if (on) { - on('config', function (curr, prev) { - if (curr.randomSeed !== prev.randomSeed) { - rng = createRng(curr.randomSeed); - } - }); - } - - return typed(random_name, { - '': function _() { - return _random(0, 1); - }, - number: function number(max) { - return _random(0, max); - }, - 'number, number': function numberNumber(min, max) { - return _random(min, max); - } - }); - - function _random(min, max) { - return min + rng() * (max - min); - } - }); - // CONCATENATED MODULE: ./src/function/probability/randomInt.js - - var randomInt_name = 'randomInt'; - var randomInt_dependencies = ['typed', 'config', '?on']; - var createRandomInt = /* #__PURE__ */Object(factory["a" /* factory */])(randomInt_name, randomInt_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - on = _ref.on; - // seeded pseudo random number generator - var rng = createRng(config.randomSeed); - - if (on) { - on('config', function (curr, prev) { - if (curr.randomSeed !== prev.randomSeed) { - rng = createRng(curr.randomSeed); - } - }); - } - /** - * Return a random integer number larger or equal to `min` and smaller than `max` - * using a uniform distribution. - * - * Syntax: - * - * math.randomInt() // generate a random integer between 0 and 1 - * math.randomInt(max) // generate a random integer between 0 and max - * math.randomInt(min, max) // generate a random integer between min and max - * math.randomInt(size) // generate a matrix with random integer between 0 and 1 - * math.randomInt(size, max) // generate a matrix with random integer between 0 and max - * math.randomInt(size, min, max) // generate a matrix with random integer between min and max - * - * Examples: - * - * math.randomInt(100) // returns a random integer between 0 and 100 - * math.randomInt(30, 40) // returns a random integer between 30 and 40 - * math.randomInt([2, 3]) // returns a 2x3 matrix with random integers between 0 and 1 - * - * See also: - * - * random, pickRandom - * - * @param {Array | Matrix} [size] If provided, an array or matrix with given - * size and filled with random values is returned - * @param {number} [min] Minimum boundary for the random value, included - * @param {number} [max] Maximum boundary for the random value, excluded - * @return {number | Array | Matrix} A random integer value - */ - - return typed(randomInt_name, { - '': function _() { - return _randomInt(0, 1); - }, - number: function number(max) { - return _randomInt(0, max); - }, - 'number, number': function numberNumber(min, max) { - return _randomInt(min, max); - }, - 'Array | Matrix': function ArrayMatrix(size) { - return _randomIntMatrix(size, 0, 1); - }, - 'Array | Matrix, number': function ArrayMatrixNumber(size, max) { - return _randomIntMatrix(size, 0, max); - }, - 'Array | Matrix, number, number': function ArrayMatrixNumberNumber(size, min, max) { - return _randomIntMatrix(size, min, max); - } - }); - - function _randomIntMatrix(size, min, max) { - var res = randomMatrix(size.valueOf(), function () { - return _randomInt(min, max); - }); - return Object(is["v" /* isMatrix */])(size) ? size.create(res) : res; - } - - function _randomInt(min, max) { - return Math.floor(min + rng() * (max - min)); - } - }); - // CONCATENATED MODULE: ./src/function/combinatorics/stirlingS2.js - - var stirlingS2_name = 'stirlingS2'; - var stirlingS2_dependencies = ['typed', 'addScalar', 'subtract', 'multiplyScalar', 'divideScalar', 'pow', 'factorial', 'combinations', 'isNegative', 'isInteger', 'larger']; - var createStirlingS2 = /* #__PURE__ */Object(factory["a" /* factory */])(stirlingS2_name, stirlingS2_dependencies, function (_ref) { - var typed = _ref.typed, - addScalar = _ref.addScalar, - subtract = _ref.subtract, - multiplyScalar = _ref.multiplyScalar, - divideScalar = _ref.divideScalar, - pow = _ref.pow, - factorial = _ref.factorial, - combinations = _ref.combinations, - isNegative = _ref.isNegative, - isInteger = _ref.isInteger, - larger = _ref.larger; - - /** - * The Stirling numbers of the second kind, counts the number of ways to partition - * a set of n labelled objects into k nonempty unlabelled subsets. - * stirlingS2 only takes integer arguments. - * The following condition must be enforced: k <= n. - * - * If n = k or k = 1, then s(n,k) = 1 - * - * Syntax: - * - * math.stirlingS2(n, k) - * - * Examples: - * - * math.stirlingS2(5, 3) //returns 25 - * - * See also: - * - * bellNumbers - * - * @param {Number | BigNumber} n Total number of objects in the set - * @param {Number | BigNumber} k Number of objects in the subset - * @return {Number | BigNumber} S(n,k) - */ - return typed(stirlingS2_name, { - 'number | BigNumber, number | BigNumber': function numberBigNumberNumberBigNumber(n, k) { - if (!isInteger(n) || isNegative(n) || !isInteger(k) || isNegative(k)) { - throw new TypeError('Non-negative integer value expected in function stirlingS2'); - } else if (larger(k, n)) { - throw new TypeError('k must be less than or equal to n in function stirlingS2'); - } // 1/k! Sum(i=0 -> k) [(-1)^(k-i)*C(k,j)* i^n] - - var kFactorial = factorial(k); - var result = 0; - - for (var i = 0; i <= k; i++) { - var negativeOne = pow(-1, subtract(k, i)); - var kChooseI = combinations(k, i); - var iPower = pow(i, n); - result = addScalar(result, multiplyScalar(multiplyScalar(kChooseI, iPower), negativeOne)); - } - - return divideScalar(result, kFactorial); - } - }); - }); - // CONCATENATED MODULE: ./src/function/combinatorics/bellNumbers.js - - var bellNumbers_name = 'bellNumbers'; - var bellNumbers_dependencies = ['typed', 'addScalar', 'isNegative', 'isInteger', 'stirlingS2']; - var createBellNumbers = /* #__PURE__ */Object(factory["a" /* factory */])(bellNumbers_name, bellNumbers_dependencies, function (_ref) { - var typed = _ref.typed, - addScalar = _ref.addScalar, - isNegative = _ref.isNegative, - isInteger = _ref.isInteger, - stirlingS2 = _ref.stirlingS2; - - /** - * The Bell Numbers count the number of partitions of a set. A partition is a pairwise disjoint subset of S whose union is S. - * bellNumbers only takes integer arguments. - * The following condition must be enforced: n >= 0 - * - * Syntax: - * - * math.bellNumbers(n) - * - * Examples: - * - * math.bellNumbers(3) // returns 5 - * math.bellNumbers(8) // returns 4140 - * - * See also: - * - * stirlingS2 - * - * @param {Number | BigNumber} n Total number of objects in the set - * @return {Number | BigNumber} B(n) - */ - return typed(bellNumbers_name, { - 'number | BigNumber': function numberBigNumber(n) { - if (!isInteger(n) || isNegative(n)) { - throw new TypeError('Non-negative integer value expected in function bellNumbers'); - } // Sum (k=0, n) S(n,k). - - var result = 0; - - for (var i = 0; i <= n; i++) { - result = addScalar(result, stirlingS2(n, i)); - } - - return result; - } - }); - }); - // CONCATENATED MODULE: ./src/function/combinatorics/catalan.js - - var catalan_name = 'catalan'; - var catalan_dependencies = ['typed', 'addScalar', 'divideScalar', 'multiplyScalar', 'combinations', 'isNegative', 'isInteger']; - var createCatalan = /* #__PURE__ */Object(factory["a" /* factory */])(catalan_name, catalan_dependencies, function (_ref) { - var typed = _ref.typed, - addScalar = _ref.addScalar, - divideScalar = _ref.divideScalar, - multiplyScalar = _ref.multiplyScalar, - combinations = _ref.combinations, - isNegative = _ref.isNegative, - isInteger = _ref.isInteger; - - /** - * The Catalan Numbers enumerate combinatorial structures of many different types. - * catalan only takes integer arguments. - * The following condition must be enforced: n >= 0 - * - * Syntax: - * - * math.catalan(n) - * - * Examples: - * - * math.catalan(3) // returns 5 - * math.catalan(8) // returns 1430 - * - * See also: - * - * bellNumbers - * - * @param {Number | BigNumber} n nth Catalan number - * @return {Number | BigNumber} Cn(n) - */ - return typed(catalan_name, { - 'number | BigNumber': function numberBigNumber(n) { - if (!isInteger(n) || isNegative(n)) { - throw new TypeError('Non-negative integer value expected in function catalan'); - } - - return divideScalar(combinations(multiplyScalar(n, 2), n), addScalar(n, 1)); - } - }); - }); - // CONCATENATED MODULE: ./src/function/combinatorics/composition.js - - var composition_name = 'composition'; - var composition_dependencies = ['typed', 'addScalar', 'combinations', 'isNegative', 'isPositive', 'isInteger', 'larger']; - var createComposition = /* #__PURE__ */Object(factory["a" /* factory */])(composition_name, composition_dependencies, function (_ref) { - var typed = _ref.typed, - addScalar = _ref.addScalar, - combinations = _ref.combinations, - isPositive = _ref.isPositive, - isNegative = _ref.isNegative, - isInteger = _ref.isInteger, - larger = _ref.larger; - - /** - * The composition counts of n into k parts. - * - * composition only takes integer arguments. - * The following condition must be enforced: k <= n. - * - * Syntax: - * - * math.composition(n, k) - * - * Examples: - * - * math.composition(5, 3) // returns 6 - * - * See also: - * - * combinations - * - * @param {Number | BigNumber} n Total number of objects in the set - * @param {Number | BigNumber} k Number of objects in the subset - * @return {Number | BigNumber} Returns the composition counts of n into k parts. - */ - return typed(composition_name, { - 'number | BigNumber, number | BigNumber': function numberBigNumberNumberBigNumber(n, k) { - if (!isInteger(n) || !isPositive(n) || !isInteger(k) || !isPositive(k)) { - throw new TypeError('Positive integer value expected in function composition'); - } else if (larger(k, n)) { - throw new TypeError('k must be less than or equal to n in function composition'); - } - - return combinations(addScalar(n, -1), addScalar(k, -1)); - } - }); - }); - // CONCATENATED MODULE: ./src/function/algebra/simplify/util.js - - var util_name = 'simplifyUtil'; - var util_dependencies = ['FunctionNode', 'OperatorNode', 'SymbolNode']; - var createUtil = /* #__PURE__ */Object(factory["a" /* factory */])(util_name, util_dependencies, function (_ref) { - var FunctionNode = _ref.FunctionNode, - OperatorNode = _ref.OperatorNode, - SymbolNode = _ref.SymbolNode; - // TODO commutative/associative properties rely on the arguments - // e.g. multiply is not commutative for matrices - // The properties should be calculated from an argument to simplify, or possibly something in math.config - // the other option is for typed() to specify a return type so that we can evaluate the type of arguments - var commutative = { - add: true, - multiply: true - }; - var associative = { - add: true, - multiply: true - }; - - function isCommutative(node, context) { - if (!Object(is["B" /* isOperatorNode */])(node)) { - return true; - } - - var name = node.fn.toString(); - - if (context && Object(utils_object["f" /* hasOwnProperty */])(context, name) && Object(utils_object["f" /* hasOwnProperty */])(context[name], 'commutative')) { - return context[name].commutative; - } - - return commutative[name] || false; - } - - function isAssociative(node, context) { - if (!Object(is["B" /* isOperatorNode */])(node)) { - return false; - } - - var name = node.fn.toString(); - - if (context && Object(utils_object["f" /* hasOwnProperty */])(context, name) && Object(utils_object["f" /* hasOwnProperty */])(context[name], 'associative')) { - return context[name].associative; - } - - return associative[name] || false; - } - /** - * Flatten all associative operators in an expression tree. - * Assumes parentheses have already been removed. - */ - - function flatten(node) { - if (!node.args || node.args.length === 0) { - return node; - } - - node.args = allChildren(node); - - for (var i = 0; i < node.args.length; i++) { - flatten(node.args[i]); - } - } - /** - * Get the children of a node as if it has been flattened. - * TODO implement for FunctionNodes - */ - - function allChildren(node) { - var op; - var children = []; - - var findChildren = function findChildren(node) { - for (var i = 0; i < node.args.length; i++) { - var child = node.args[i]; - - if (Object(is["B" /* isOperatorNode */])(child) && op === child.op) { - findChildren(child); - } else { - children.push(child); - } - } - }; - - if (isAssociative(node)) { - op = node.op; - findChildren(node); - return children; - } else { - return node.args; - } - } - /** - * Unflatten all flattened operators to a right-heavy binary tree. - */ - - function unflattenr(node) { - if (!node.args || node.args.length === 0) { - return; - } - - var makeNode = createMakeNodeFunction(node); - var l = node.args.length; - - for (var i = 0; i < l; i++) { - unflattenr(node.args[i]); - } - - if (l > 2 && isAssociative(node)) { - var curnode = node.args.pop(); - - while (node.args.length > 0) { - curnode = makeNode([node.args.pop(), curnode]); - } - - node.args = curnode.args; - } - } - /** - * Unflatten all flattened operators to a left-heavy binary tree. - */ - - function unflattenl(node) { - if (!node.args || node.args.length === 0) { - return; - } - - var makeNode = createMakeNodeFunction(node); - var l = node.args.length; - - for (var i = 0; i < l; i++) { - unflattenl(node.args[i]); - } - - if (l > 2 && isAssociative(node)) { - var curnode = node.args.shift(); - - while (node.args.length > 0) { - curnode = makeNode([curnode, node.args.shift()]); - } - - node.args = curnode.args; - } - } - - function createMakeNodeFunction(node) { - if (Object(is["B" /* isOperatorNode */])(node)) { - return function (args) { - try { - return new OperatorNode(node.op, node.fn, args, node.implicit); - } catch (err) { - console.error(err); - return []; - } - }; - } else { - return function (args) { - return new FunctionNode(new SymbolNode(node.name), args); - }; - } - } - - return { - createMakeNodeFunction: createMakeNodeFunction, - isCommutative: isCommutative, - isAssociative: isAssociative, - flatten: flatten, - allChildren: allChildren, - unflattenr: unflattenr, - unflattenl: unflattenl - }; - }); - // CONCATENATED MODULE: ./src/function/algebra/simplify/simplifyCore.js - - var simplifyCore_name = 'simplifyCore'; - var simplifyCore_dependencies = ['equal', 'isZero', 'add', 'subtract', 'multiply', 'divide', 'pow', 'ConstantNode', 'OperatorNode', 'FunctionNode', 'ParenthesisNode']; - var createSimplifyCore = /* #__PURE__ */Object(factory["a" /* factory */])(simplifyCore_name, simplifyCore_dependencies, function (_ref) { - var equal = _ref.equal, - isZero = _ref.isZero, - add = _ref.add, - subtract = _ref.subtract, - multiply = _ref.multiply, - divide = _ref.divide, - pow = _ref.pow, - ConstantNode = _ref.ConstantNode, - OperatorNode = _ref.OperatorNode, - FunctionNode = _ref.FunctionNode, - ParenthesisNode = _ref.ParenthesisNode; - var node0 = new ConstantNode(0); - var node1 = new ConstantNode(1); - /** - * simplifyCore() performs single pass simplification suitable for - * applications requiring ultimate performance. In contrast, simplify() - * extends simplifyCore() with additional passes to provide deeper - * simplification. - * - * Syntax: - * - * simplify.simplifyCore(expr) - * - * Examples: - * - * const f = math.parse('2 * 1 * x ^ (2 - 1)') - * math.simplify.simpifyCore(f) // Node {2 * x} - * math.simplify('2 * 1 * x ^ (2 - 1)', [math.simplify.simpifyCore]) // Node {2 * x} - * - * See also: - * - * derivative - * - * @param {Node} node - * The expression to be simplified - */ - - function simplifyCore(node) { - if (Object(is["B" /* isOperatorNode */])(node) && node.isUnary()) { - var a0 = simplifyCore(node.args[0]); - - if (node.op === '+') { - // unary plus - return a0; - } - - if (node.op === '-') { - // unary minus - if (Object(is["B" /* isOperatorNode */])(a0)) { - if (a0.isUnary() && a0.op === '-') { - return a0.args[0]; - } else if (a0.isBinary() && a0.fn === 'subtract') { - return new OperatorNode('-', 'subtract', [a0.args[1], a0.args[0]]); - } - } - - return new OperatorNode(node.op, node.fn, [a0]); - } - } else if (Object(is["B" /* isOperatorNode */])(node) && node.isBinary()) { - var _a = simplifyCore(node.args[0]); - - var a1 = simplifyCore(node.args[1]); - - if (node.op === '+') { - if (Object(is["l" /* isConstantNode */])(_a)) { - if (isZero(_a.value)) { - return a1; - } else if (Object(is["l" /* isConstantNode */])(a1)) { - return new ConstantNode(add(_a.value, a1.value)); - } - } - - if (Object(is["l" /* isConstantNode */])(a1) && isZero(a1.value)) { - return _a; - } - - if (Object(is["B" /* isOperatorNode */])(a1) && a1.isUnary() && a1.op === '-') { - return new OperatorNode('-', 'subtract', [_a, a1.args[0]]); - } - - return new OperatorNode(node.op, node.fn, a1 ? [_a, a1] : [_a]); - } else if (node.op === '-') { - if (Object(is["l" /* isConstantNode */])(_a) && a1) { - if (Object(is["l" /* isConstantNode */])(a1)) { - return new ConstantNode(subtract(_a.value, a1.value)); - } else if (isZero(_a.value)) { - return new OperatorNode('-', 'unaryMinus', [a1]); - } - } // if (node.fn === "subtract" && node.args.length === 2) { - - if (node.fn === 'subtract') { - if (Object(is["l" /* isConstantNode */])(a1) && isZero(a1.value)) { - return _a; - } - - if (Object(is["B" /* isOperatorNode */])(a1) && a1.isUnary() && a1.op === '-') { - return simplifyCore(new OperatorNode('+', 'add', [_a, a1.args[0]])); - } - - return new OperatorNode(node.op, node.fn, [_a, a1]); - } - } else if (node.op === '*') { - if (Object(is["l" /* isConstantNode */])(_a)) { - if (isZero(_a.value)) { - return node0; - } else if (equal(_a.value, 1)) { - return a1; - } else if (Object(is["l" /* isConstantNode */])(a1)) { - return new ConstantNode(multiply(_a.value, a1.value)); - } - } - - if (Object(is["l" /* isConstantNode */])(a1)) { - if (isZero(a1.value)) { - return node0; - } else if (equal(a1.value, 1)) { - return _a; - } else if (Object(is["B" /* isOperatorNode */])(_a) && _a.isBinary() && _a.op === node.op) { - var a00 = _a.args[0]; - - if (Object(is["l" /* isConstantNode */])(a00)) { - var a00a1 = new ConstantNode(multiply(a00.value, a1.value)); - return new OperatorNode(node.op, node.fn, [a00a1, _a.args[1]], node.implicit); // constants on left - } - } - - return new OperatorNode(node.op, node.fn, [a1, _a], node.implicit); // constants on left - } - - return new OperatorNode(node.op, node.fn, [_a, a1], node.implicit); - } else if (node.op === '/') { - if (Object(is["l" /* isConstantNode */])(_a)) { - if (isZero(_a.value)) { - return node0; - } else if (Object(is["l" /* isConstantNode */])(a1) && (equal(a1.value, 1) || equal(a1.value, 2) || equal(a1.value, 4))) { - return new ConstantNode(divide(_a.value, a1.value)); - } - } - - return new OperatorNode(node.op, node.fn, [_a, a1]); - } else if (node.op === '^') { - if (Object(is["l" /* isConstantNode */])(a1)) { - if (isZero(a1.value)) { - return node1; - } else if (equal(a1.value, 1)) { - return _a; - } else { - if (Object(is["l" /* isConstantNode */])(_a)) { - // fold constant - return new ConstantNode(pow(_a.value, a1.value)); - } else if (Object(is["B" /* isOperatorNode */])(_a) && _a.isBinary() && _a.op === '^') { - var a01 = _a.args[1]; - - if (Object(is["l" /* isConstantNode */])(a01)) { - return new OperatorNode(node.op, node.fn, [_a.args[0], new ConstantNode(multiply(a01.value, a1.value))]); - } - } - } - } - - return new OperatorNode(node.op, node.fn, [_a, a1]); - } - } else if (Object(is["C" /* isParenthesisNode */])(node)) { - var c = simplifyCore(node.content); - - if (Object(is["C" /* isParenthesisNode */])(c) || Object(is["J" /* isSymbolNode */])(c) || Object(is["l" /* isConstantNode */])(c)) { - return c; - } - - return new ParenthesisNode(c); - } else if (Object(is["r" /* isFunctionNode */])(node)) { - var args = node.args.map(simplifyCore).map(function (arg) { - return Object(is["C" /* isParenthesisNode */])(arg) ? arg.content : arg; - }); - return new FunctionNode(simplifyCore(node.fn), args); - } else {// cannot simplify - } - - return node; - } - - return simplifyCore; - }); - // CONCATENATED MODULE: ./src/function/algebra/simplify/simplifyConstant.js - // TODO this could be improved by simplifying seperated constants under associative and commutative operators - - var simplifyConstant_name = 'simplifyConstant'; - var simplifyConstant_dependencies = ['typed', 'config', 'mathWithTransform', '?fraction', '?bignumber', 'ConstantNode', 'OperatorNode', 'FunctionNode', 'SymbolNode']; - var createSimplifyConstant = /* #__PURE__ */Object(factory["a" /* factory */])(simplifyConstant_name, simplifyConstant_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - mathWithTransform = _ref.mathWithTransform, - fraction = _ref.fraction, - bignumber = _ref.bignumber, - ConstantNode = _ref.ConstantNode, - OperatorNode = _ref.OperatorNode, - FunctionNode = _ref.FunctionNode, - SymbolNode = _ref.SymbolNode; - - var _createUtil = createUtil({ - FunctionNode: FunctionNode, - OperatorNode: OperatorNode, - SymbolNode: SymbolNode - }), - isCommutative = _createUtil.isCommutative, - isAssociative = _createUtil.isAssociative, - allChildren = _createUtil.allChildren, - createMakeNodeFunction = _createUtil.createMakeNodeFunction; - - function simplifyConstant(expr, options) { - var res = foldFraction(expr, options); - return Object(is["w" /* isNode */])(res) ? res : _toNode(res); - } - - function _eval(fnname, args, options) { - try { - return _toNumber(mathWithTransform[fnname].apply(null, args), options); - } catch (ignore) { - // sometimes the implicit type conversion causes the evaluation to fail, so we'll try again after removing Fractions - args = args.map(function (x) { - if (Object(is["o" /* isFraction */])(x)) { - return x.valueOf(); - } - - return x; - }); - return _toNumber(mathWithTransform[fnname].apply(null, args), options); - } - } - - var _toNode = typed({ - Fraction: _fractionToNode, - number: function number(n) { - if (n < 0) { - return unaryMinusNode(new ConstantNode(-n)); - } - - return new ConstantNode(n); - }, - BigNumber: function BigNumber(n) { - if (n < 0) { - return unaryMinusNode(new ConstantNode(-n)); - } - - return new ConstantNode(n); // old parameters: (n.toString(), 'number') - }, - Complex: function Complex(s) { - throw new Error('Cannot convert Complex number to Node'); - } - }); // convert a number to a fraction only if it can be expressed exactly - - function _exactFraction(n, options) { - var exactFractions = options && options.exactFractions !== false; - - if (exactFractions && isFinite(n) && fraction) { - var f = fraction(n); - - if (f.valueOf() === n) { - return f; - } - } - - return n; - } // Convert numbers to a preferred number type in preference order: Fraction, number, Complex - // BigNumbers are left alone - - var _toNumber = typed({ - 'string, Object': function stringObject(s, options) { - if (config.number === 'BigNumber') { - if (bignumber === undefined) { - noBignumber(); - } - - return bignumber(s); - } else if (config.number === 'Fraction') { - if (fraction === undefined) { - noFraction(); - } - - return fraction(s); - } else { - var n = parseFloat(s); - return _exactFraction(n, options); - } - }, - 'Fraction, Object': function FractionObject(s, options) { - return s; - }, - // we don't need options here - 'BigNumber, Object': function BigNumberObject(s, options) { - return s; - }, - // we don't need options here - 'number, Object': function numberObject(s, options) { - return _exactFraction(s, options); - }, - 'Complex, Object': function ComplexObject(s, options) { - if (s.im !== 0) { - return s; - } - - return _exactFraction(s.re, options); - } - }); - - function unaryMinusNode(n) { - return new OperatorNode('-', 'unaryMinus', [n]); - } - - function _fractionToNode(f) { - var n; - var vn = f.s * f.n; - - if (vn < 0) { - n = new OperatorNode('-', 'unaryMinus', [new ConstantNode(-vn)]); - } else { - n = new ConstantNode(vn); - } - - if (f.d === 1) { - return n; - } - - return new OperatorNode('/', 'divide', [n, new ConstantNode(f.d)]); - } - /* - * Create a binary tree from a list of Fractions and Nodes. - * Tries to fold Fractions by evaluating them until the first Node in the list is hit, so - * `args` should be sorted to have the Fractions at the start (if the operator is commutative). - * @param args - list of Fractions and Nodes - * @param fn - evaluator for the binary operation evaluator that accepts two Fractions - * @param makeNode - creates a binary OperatorNode/FunctionNode from a list of child Nodes - * if args.length is 1, returns args[0] - * @return - Either a Node representing a binary expression or Fraction - */ - - function foldOp(fn, args, makeNode, options) { - return args.reduce(function (a, b) { - if (!Object(is["w" /* isNode */])(a) && !Object(is["w" /* isNode */])(b)) { - try { - return _eval(fn, [a, b], options); - } catch (ignoreandcontinue) {} - - a = _toNode(a); - b = _toNode(b); - } else if (!Object(is["w" /* isNode */])(a)) { - a = _toNode(a); - } else if (!Object(is["w" /* isNode */])(b)) { - b = _toNode(b); - } - - return makeNode([a, b]); - }); - } // destroys the original node and returns a folded one - - function foldFraction(node, options) { - switch (node.type) { - case 'SymbolNode': - return node; - - case 'ConstantNode': - if (typeof node.value === 'number' || !isNaN(node.value)) { - return _toNumber(node.value, options); - } - - return node; - - case 'FunctionNode': - if (mathWithTransform[node.name] && mathWithTransform[node.name].rawArgs) { - return node; - } - - { - // Process operators as OperatorNode - var operatorFunctions = ['add', 'multiply']; - - if (operatorFunctions.indexOf(node.name) === -1) { - var args = node.args.map(function (arg) { - return foldFraction(arg, options); - }); // If all args are numbers - - if (!args.some(is["w" /* isNode */])) { - try { - return _eval(node.name, args, options); - } catch (ignoreandcontine) {} - } // Convert all args to nodes and construct a symbolic function call - - args = args.map(function (arg) { - return Object(is["w" /* isNode */])(arg) ? arg : _toNode(arg); - }); - return new FunctionNode(node.name, args); - } else {// treat as operator - } - } - - /* falls through */ - - case 'OperatorNode': - { - var fn = node.fn.toString(); - - var _args; - - var res; - var makeNode = createMakeNodeFunction(node); - - if (Object(is["B" /* isOperatorNode */])(node) && node.isUnary()) { - _args = [foldFraction(node.args[0], options)]; - - if (!Object(is["w" /* isNode */])(_args[0])) { - res = _eval(fn, _args, options); - } else { - res = makeNode(_args); - } - } else if (isAssociative(node)) { - _args = allChildren(node); - _args = _args.map(function (arg) { - return foldFraction(arg, options); - }); - - if (isCommutative(fn)) { - // commutative binary operator - var consts = []; - var vars = []; - - for (var i = 0; i < _args.length; i++) { - if (!Object(is["w" /* isNode */])(_args[i])) { - consts.push(_args[i]); - } else { - vars.push(_args[i]); - } - } - - if (consts.length > 1) { - res = foldOp(fn, consts, makeNode, options); - vars.unshift(res); - res = foldOp(fn, vars, makeNode, options); - } else { - // we won't change the children order since it's not neccessary - res = foldOp(fn, _args, makeNode, options); - } - } else { - // non-commutative binary operator - res = foldOp(fn, _args, makeNode, options); - } - } else { - // non-associative binary operator - _args = node.args.map(function (arg) { - return foldFraction(arg, options); - }); - res = foldOp(fn, _args, makeNode, options); - } - - return res; - } - - case 'ParenthesisNode': - // remove the uneccessary parenthesis - return foldFraction(node.content, options); - - case 'AccessorNode': - /* falls through */ - - case 'ArrayNode': - /* falls through */ - - case 'AssignmentNode': - /* falls through */ - - case 'BlockNode': - /* falls through */ - - case 'FunctionAssignmentNode': - /* falls through */ - - case 'IndexNode': - /* falls through */ - - case 'ObjectNode': - /* falls through */ - - case 'RangeNode': - /* falls through */ - - case 'ConditionalNode': - /* falls through */ - - default: - throw new Error("Unimplemented node type in simplifyConstant: ".concat(node.type)); - } - } - - return simplifyConstant; - }); - // CONCATENATED MODULE: ./src/function/algebra/simplify/resolve.js - - var resolve_name = 'resolve'; - var resolve_dependencies = ['parse', 'FunctionNode', 'OperatorNode', 'ParenthesisNode']; - var createResolve = /* #__PURE__ */Object(factory["a" /* factory */])(resolve_name, resolve_dependencies, function (_ref) { - var parse = _ref.parse, - FunctionNode = _ref.FunctionNode, - OperatorNode = _ref.OperatorNode, - ParenthesisNode = _ref.ParenthesisNode; - - /** - * resolve(expr, scope) replaces variable nodes with their scoped values - * - * Syntax: - * - * simplify.resolve(expr, scope) - * - * Examples: - * - * math.simplify.resolve('x + y', {x:1, y:2}) // Node {1 + 2} - * math.simplify.resolve(math.parse('x+y'), {x:1, y:2}) // Node {1 + 2} - * math.simplify('x+y', {x:2, y:'x+x'}).toString() // "6" - * - * @param {Node} node - * The expression tree to be simplified - * @param {Object} scope with variables to be resolved - */ - function resolve(node, scope) { - if (!scope) { - return node; - } - - if (Object(is["J" /* isSymbolNode */])(node)) { - var value = scope[node.name]; - - if (Object(is["w" /* isNode */])(value)) { - return resolve(value, scope); - } else if (typeof value === 'number') { - return parse(String(value)); - } - } else if (Object(is["B" /* isOperatorNode */])(node)) { - var args = node.args.map(function (arg) { - return resolve(arg, scope); - }); - return new OperatorNode(node.op, node.fn, args, node.implicit); - } else if (Object(is["C" /* isParenthesisNode */])(node)) { - return new ParenthesisNode(resolve(node.content, scope)); - } else if (Object(is["r" /* isFunctionNode */])(node)) { - var _args = node.args.map(function (arg) { - return resolve(arg, scope); - }); - - return new FunctionNode(node.name, _args); - } - - return node; - } - - return resolve; - }); - // CONCATENATED MODULE: ./src/function/algebra/simplify.js - function simplify_typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - simplify_typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - simplify_typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return simplify_typeof(obj); - } - - var simplify_name = 'simplify'; - var simplify_dependencies = ['config', 'typed', 'parse', 'add', 'subtract', 'multiply', 'divide', 'pow', 'isZero', 'equal', '?fraction', '?bignumber', 'mathWithTransform', 'ConstantNode', 'FunctionNode', 'OperatorNode', 'ParenthesisNode', 'SymbolNode']; - var createSimplify = /* #__PURE__ */Object(factory["a" /* factory */])(simplify_name, simplify_dependencies, function (_ref) { - var config = _ref.config, - typed = _ref.typed, - parse = _ref.parse, - add = _ref.add, - subtract = _ref.subtract, - multiply = _ref.multiply, - divide = _ref.divide, - pow = _ref.pow, - isZero = _ref.isZero, - equal = _ref.equal, - fraction = _ref.fraction, - bignumber = _ref.bignumber, - mathWithTransform = _ref.mathWithTransform, - ConstantNode = _ref.ConstantNode, - FunctionNode = _ref.FunctionNode, - OperatorNode = _ref.OperatorNode, - ParenthesisNode = _ref.ParenthesisNode, - SymbolNode = _ref.SymbolNode; - var simplifyConstant = createSimplifyConstant({ - typed: typed, - config: config, - mathWithTransform: mathWithTransform, - fraction: fraction, - bignumber: bignumber, - ConstantNode: ConstantNode, - OperatorNode: OperatorNode, - FunctionNode: FunctionNode, - SymbolNode: SymbolNode - }); - var simplifyCore = createSimplifyCore({ - equal: equal, - isZero: isZero, - add: add, - subtract: subtract, - multiply: multiply, - divide: divide, - pow: pow, - ConstantNode: ConstantNode, - OperatorNode: OperatorNode, - FunctionNode: FunctionNode, - ParenthesisNode: ParenthesisNode - }); - var resolve = createResolve({ - parse: parse, - FunctionNode: FunctionNode, - OperatorNode: OperatorNode, - ParenthesisNode: ParenthesisNode - }); - - var _createUtil = createUtil({ - FunctionNode: FunctionNode, - OperatorNode: OperatorNode, - SymbolNode: SymbolNode - }), - isCommutative = _createUtil.isCommutative, - isAssociative = _createUtil.isAssociative, - flatten = _createUtil.flatten, - unflattenr = _createUtil.unflattenr, - unflattenl = _createUtil.unflattenl, - createMakeNodeFunction = _createUtil.createMakeNodeFunction; - /** - * Simplify an expression tree. - * - * A list of rules are applied to an expression, repeating over the list until - * no further changes are made. - * It's possible to pass a custom set of rules to the function as second - * argument. A rule can be specified as an object, string, or function: - * - * const rules = [ - * { l: 'n1*n3 + n2*n3', r: '(n1+n2)*n3' }, - * 'n1*n3 + n2*n3 -> (n1+n2)*n3', - * function (node) { - * // ... return a new node or return the node unchanged - * return node - * } - * ] - * - * String and object rules consist of a left and right pattern. The left is - * used to match against the expression and the right determines what matches - * are replaced with. The main difference between a pattern and a normal - * expression is that variables starting with the following characters are - * interpreted as wildcards: - * - * - 'n' - matches any Node - * - 'c' - matches any ConstantNode - * - 'v' - matches any Node that is not a ConstantNode - * - * The default list of rules is exposed on the function as `simplify.rules` - * and can be used as a basis to built a set of custom rules. - * - * For more details on the theory, see: - * - * - [Strategies for simplifying math expressions (Stackoverflow)](https://stackoverflow.com/questions/7540227/strategies-for-simplifying-math-expressions) - * - [Symbolic computation - Simplification (Wikipedia)](https://en.wikipedia.org/wiki/Symbolic_computation#Simplification) - * - * An optional `options` argument can be passed as last argument of `simplify`. - * There is currently one option available: `exactFractions`, a boolean which - * is `true` by default. - * - * Syntax: - * - * simplify(expr) - * simplify(expr, rules) - * simplify(expr, rules) - * simplify(expr, rules, scope) - * simplify(expr, rules, scope, options) - * simplify(expr, scope) - * simplify(expr, scope, options) - * - * Examples: - * - * math.simplify('2 * 1 * x ^ (2 - 1)') // Node "2 * x" - * math.simplify('2 * 3 * x', {x: 4}) // Node "24" - * const f = math.parse('2 * 1 * x ^ (2 - 1)') - * math.simplify(f) // Node "2 * x" - * math.simplify('0.4 * x', {}, {exactFractions: true}) // Node "x * 2 / 5" - * math.simplify('0.4 * x', {}, {exactFractions: false}) // Node "0.4 * x" - * - * See also: - * - * derivative, parse, evaluate, rationalize - * - * @param {Node | string} expr - * The expression to be simplified - * @param {Array<{l:string, r: string} | string | function>} [rules] - * Optional list with custom rules - * @return {Node} Returns the simplified form of `expr` - */ - - var simplify = typed('simplify', { - string: function string(expr) { - return this(parse(expr), this.rules, {}, {}); - }, - 'string, Object': function stringObject(expr, scope) { - return this(parse(expr), this.rules, scope, {}); - }, - 'string, Object, Object': function stringObjectObject(expr, scope, options) { - return this(parse(expr), this.rules, scope, options); - }, - 'string, Array': function stringArray(expr, rules) { - return this(parse(expr), rules, {}, {}); - }, - 'string, Array, Object': function stringArrayObject(expr, rules, scope) { - return this(parse(expr), rules, scope, {}); - }, - 'string, Array, Object, Object': function stringArrayObjectObject(expr, rules, scope, options) { - return this(parse(expr), rules, scope, options); - }, - 'Node, Object': function NodeObject(expr, scope) { - return this(expr, this.rules, scope, {}); - }, - 'Node, Object, Object': function NodeObjectObject(expr, scope, options) { - return this(expr, this.rules, scope, options); - }, - Node: function Node(expr) { - return this(expr, this.rules, {}, {}); - }, - 'Node, Array': function NodeArray(expr, rules) { - return this(expr, rules, {}, {}); - }, - 'Node, Array, Object': function NodeArrayObject(expr, rules, scope) { - return this(expr, rules, scope, {}); - }, - 'Node, Array, Object, Object': function NodeArrayObjectObject(expr, rules, scope, options) { - rules = _buildRules(rules); - var res = resolve(expr, scope); - res = removeParens(res); - var visited = {}; - var str = res.toString({ - parenthesis: 'all' - }); - - while (!visited[str]) { - visited[str] = true; - _lastsym = 0; // counter for placeholder symbols - - for (var i = 0; i < rules.length; i++) { - if (typeof rules[i] === 'function') { - res = rules[i](res, options); - } else { - flatten(res); - res = applyRule(res, rules[i]); - } - - unflattenl(res); // using left-heavy binary tree here since custom rule functions may expect it - } - - str = res.toString({ - parenthesis: 'all' - }); - } - - return res; - } - }); - simplify.simplifyCore = simplifyCore; - simplify.resolve = resolve; - - function removeParens(node) { - return node.transform(function (node, path, parent) { - return Object(is["C" /* isParenthesisNode */])(node) ? removeParens(node.content) : node; - }); - } // All constants that are allowed in rules - - var SUPPORTED_CONSTANTS = { - "true": true, - "false": true, - e: true, - i: true, - Infinity: true, - LN2: true, - LN10: true, - LOG2E: true, - LOG10E: true, - NaN: true, - phi: true, - pi: true, - SQRT1_2: true, - SQRT2: true, - tau: true // null: false, - // undefined: false, - // version: false, - - }; // Array of strings, used to build the ruleSet. - // Each l (left side) and r (right side) are parsed by - // the expression parser into a node tree. - // Left hand sides are matched to subtrees within the - // expression to be parsed and replaced with the right - // hand side. - // TODO: Add support for constraints on constants (either in the form of a '=' expression or a callback [callback allows things like comparing symbols alphabetically]) - // To evaluate lhs constants for rhs constants, use: { l: 'c1+c2', r: 'c3', evaluate: 'c3 = c1 + c2' }. Multiple assignments are separated by ';' in block format. - // It is possible to get into an infinite loop with conflicting rules - - simplify.rules = [simplifyCore, // { l: 'n+0', r: 'n' }, // simplifyCore - // { l: 'n^0', r: '1' }, // simplifyCore - // { l: '0*n', r: '0' }, // simplifyCore - // { l: 'n/n', r: '1'}, // simplifyCore - // { l: 'n^1', r: 'n' }, // simplifyCore - // { l: '+n1', r:'n1' }, // simplifyCore - // { l: 'n--n1', r:'n+n1' }, // simplifyCore - { - l: 'log(e)', - r: '1' - }, // temporary rules - { - l: 'n-n1', - r: 'n+-n1' - }, // temporarily replace 'subtract' so we can further flatten the 'add' operator - { - l: '-(c*v)', - r: '(-c) * v' - }, // make non-constant terms positive - { - l: '-v', - r: '(-1) * v' - }, { - l: 'n/n1^n2', - r: 'n*n1^-n2' - }, // temporarily replace 'divide' so we can further flatten the 'multiply' operator - { - l: 'n/n1', - r: 'n*n1^-1' - }, // expand nested exponentiation - { - l: '(n ^ n1) ^ n2', - r: 'n ^ (n1 * n2)' - }, // collect like factors - { - l: 'n*n', - r: 'n^2' - }, { - l: 'n * n^n1', - r: 'n^(n1+1)' - }, { - l: 'n^n1 * n^n2', - r: 'n^(n1+n2)' - }, // collect like terms - { - l: 'n+n', - r: '2*n' - }, { - l: 'n+-n', - r: '0' - }, { - l: 'n1*n2 + n2', - r: '(n1+1)*n2' - }, { - l: 'n1*n3 + n2*n3', - r: '(n1+n2)*n3' - }, // remove parenthesis in the case of negating a quantitiy - { - l: 'n1 + -1 * (n2 + n3)', - r: 'n1 + -1 * n2 + -1 * n3' - }, simplifyConstant, { - l: '(-n)*n1', - r: '-(n*n1)' - }, // make factors positive (and undo 'make non-constant terms positive') - // ordering of constants - { - l: 'c+v', - r: 'v+c', - context: { - add: { - commutative: false - } - } - }, { - l: 'v*c', - r: 'c*v', - context: { - multiply: { - commutative: false - } - } - }, // undo temporary rules - // { l: '(-1) * n', r: '-n' }, // #811 added test which proved this is redundant - { - l: 'n+-n1', - r: 'n-n1' - }, // undo replace 'subtract' - { - l: 'n*(n1^-1)', - r: 'n/n1' - }, // undo replace 'divide' - { - l: 'n*n1^-n2', - r: 'n/n1^n2' - }, { - l: 'n1^-1', - r: '1/n1' - }, { - l: 'n*(n1/n2)', - r: '(n*n1)/n2' - }, // '*' before '/' - { - l: 'n-(n1+n2)', - r: 'n-n1-n2' - }, // '-' before '+' - // { l: '(n1/n2)/n3', r: 'n1/(n2*n3)' }, - // { l: '(n*n1)/(n*n2)', r: 'n1/n2' }, - { - l: '1*n', - r: 'n' - }, // this pattern can be produced by simplifyConstant - { - l: 'n1/(n2/n3)', - r: '(n1*n3)/n2' - }]; - /** - * Parse the string array of rules into nodes - * - * Example syntax for rules: - * - * Position constants to the left in a product: - * { l: 'n1 * c1', r: 'c1 * n1' } - * n1 is any Node, and c1 is a ConstantNode. - * - * Apply difference of squares formula: - * { l: '(n1 - n2) * (n1 + n2)', r: 'n1^2 - n2^2' } - * n1, n2 mean any Node. - * - * Short hand notation: - * 'n1 * c1 -> c1 * n1' - */ - - function _buildRules(rules) { - // Array of rules to be used to simplify expressions - var ruleSet = []; - - for (var i = 0; i < rules.length; i++) { - var rule = rules[i]; - var newRule = void 0; - - var ruleType = simplify_typeof(rule); - - switch (ruleType) { - case 'string': - { - var lr = rule.split('->'); - - if (lr.length === 2) { - rule = { - l: lr[0], - r: lr[1] - }; - } else { - throw SyntaxError('Could not parse rule: ' + rule); - } - } - - /* falls through */ - - case 'object': - newRule = { - l: removeParens(parse(rule.l)), - r: removeParens(parse(rule.r)) - }; - - if (rule.context) { - newRule.evaluate = rule.context; - } - - if (rule.evaluate) { - newRule.evaluate = parse(rule.evaluate); - } - - if (isAssociative(newRule.l)) { - var makeNode = createMakeNodeFunction(newRule.l); - - var expandsym = _getExpandPlaceholderSymbol(); - - newRule.expanded = {}; - newRule.expanded.l = makeNode([newRule.l.clone(), expandsym]); // Push the expandsym into the deepest possible branch. - // This helps to match the newRule against nodes returned from getSplits() later on. - - flatten(newRule.expanded.l); - unflattenr(newRule.expanded.l); - newRule.expanded.r = makeNode([newRule.r, expandsym]); - } - - break; - - case 'function': - newRule = rule; - break; - - default: - throw TypeError('Unsupported type of rule: ' + ruleType); - } // console.log('Adding rule: ' + rules[i]) - // console.log(newRule) - - ruleSet.push(newRule); - } - - return ruleSet; - } - - var _lastsym = 0; - - function _getExpandPlaceholderSymbol() { - return new SymbolNode('_p' + _lastsym++); - } - /** - * Returns a simplfied form of node, or the original node if no simplification was possible. - * - * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} node - * @return {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} The simplified form of `expr`, or the original node if no simplification was possible. - */ - - var applyRule = typed('applyRule', { - 'Node, Object': function NodeObject(node, rule) { - // console.log('Entering applyRule(' + node.toString() + ')') - // Do not clone node unless we find a match - var res = node; // First replace our child nodes with their simplified versions - // If a child could not be simplified, the assignments will have - // no effect since the node is returned unchanged - - if (res instanceof OperatorNode || res instanceof FunctionNode) { - if (res.args) { - for (var i = 0; i < res.args.length; i++) { - res.args[i] = applyRule(res.args[i], rule); - } - } - } else if (res instanceof ParenthesisNode) { - if (res.content) { - res.content = applyRule(res.content, rule); - } - } // Try to match a rule against this node - - var repl = rule.r; - - var matches = _ruleMatch(rule.l, res)[0]; // If the rule is associative operator, we can try matching it while allowing additional terms. - // This allows us to match rules like 'n+n' to the expression '(1+x)+x' or even 'x+1+x' if the operator is commutative. - - if (!matches && rule.expanded) { - repl = rule.expanded.r; - matches = _ruleMatch(rule.expanded.l, res)[0]; - } - - if (matches) { - // const before = res.toString({parenthesis: 'all'}) - // Create a new node by cloning the rhs of the matched rule - // we keep any implicit multiplication state if relevant - var implicit = res.implicit; - res = repl.clone(); - - if (implicit && 'implicit' in repl) { - res.implicit = true; - } // Replace placeholders with their respective nodes without traversing deeper into the replaced nodes - - res = res.transform(function (node) { - if (node.isSymbolNode && Object(utils_object["f" /* hasOwnProperty */])(matches.placeholders, node.name)) { - return matches.placeholders[node.name].clone(); - } else { - return node; - } - }); // const after = res.toString({parenthesis: 'all'}) - // console.log('Simplified ' + before + ' to ' + after) - } - - return res; - } - }); - /** - * Get (binary) combinations of a flattened binary node - * e.g. +(node1, node2, node3) -> [ - * +(node1, +(node2, node3)), - * +(node2, +(node1, node3)), - * +(node3, +(node1, node2))] - * - */ - - function getSplits(node, context) { - var res = []; - var right, rightArgs; - var makeNode = createMakeNodeFunction(node); - - if (isCommutative(node, context)) { - for (var i = 0; i < node.args.length; i++) { - rightArgs = node.args.slice(0); - rightArgs.splice(i, 1); - right = rightArgs.length === 1 ? rightArgs[0] : makeNode(rightArgs); - res.push(makeNode([node.args[i], right])); - } - } else { - rightArgs = node.args.slice(1); - right = rightArgs.length === 1 ? rightArgs[0] : makeNode(rightArgs); - res.push(makeNode([node.args[0], right])); - } - - return res; - } - /** - * Returns the set union of two match-placeholders or null if there is a conflict. - */ - - function mergeMatch(match1, match2) { - var res = { - placeholders: {} - }; // Some matches may not have placeholders; this is OK - - if (!match1.placeholders && !match2.placeholders) { - return res; - } else if (!match1.placeholders) { - return match2; - } else if (!match2.placeholders) { - return match1; - } // Placeholders with the same key must match exactly - - for (var key in match1.placeholders) { - if (Object(utils_object["f" /* hasOwnProperty */])(match1.placeholders, key)) { - res.placeholders[key] = match1.placeholders[key]; - - if (Object(utils_object["f" /* hasOwnProperty */])(match2.placeholders, key)) { - if (!_exactMatch(match1.placeholders[key], match2.placeholders[key])) { - return null; - } - } - } - } - - for (var _key in match2.placeholders) { - if (Object(utils_object["f" /* hasOwnProperty */])(match2.placeholders, _key)) { - res.placeholders[_key] = match2.placeholders[_key]; - } - } - - return res; - } - /** - * Combine two lists of matches by applying mergeMatch to the cartesian product of two lists of matches. - * Each list represents matches found in one child of a node. - */ - - function combineChildMatches(list1, list2) { - var res = []; - - if (list1.length === 0 || list2.length === 0) { - return res; - } - - var merged; - - for (var i1 = 0; i1 < list1.length; i1++) { - for (var i2 = 0; i2 < list2.length; i2++) { - merged = mergeMatch(list1[i1], list2[i2]); - - if (merged) { - res.push(merged); - } - } - } - - return res; - } - /** - * Combine multiple lists of matches by applying mergeMatch to the cartesian product of two lists of matches. - * Each list represents matches found in one child of a node. - * Returns a list of unique matches. - */ - - function mergeChildMatches(childMatches) { - if (childMatches.length === 0) { - return childMatches; - } - - var sets = childMatches.reduce(combineChildMatches); - var uniqueSets = []; - var unique = {}; - - for (var i = 0; i < sets.length; i++) { - var s = JSON.stringify(sets[i]); - - if (!unique[s]) { - unique[s] = true; - uniqueSets.push(sets[i]); - } - } - - return uniqueSets; - } - /** - * Determines whether node matches rule. - * - * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} rule - * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} node - * @return {Object} Information about the match, if it exists. - */ - - function _ruleMatch(rule, node, isSplit) { - // console.log('Entering _ruleMatch(' + JSON.stringify(rule) + ', ' + JSON.stringify(node) + ')') - // console.log('rule = ' + rule) - // console.log('node = ' + node) - // console.log('Entering _ruleMatch(' + rule.toString() + ', ' + node.toString() + ')') - var res = [{ - placeholders: {} - }]; - - if (rule instanceof OperatorNode && node instanceof OperatorNode || rule instanceof FunctionNode && node instanceof FunctionNode) { - // If the rule is an OperatorNode or a FunctionNode, then node must match exactly - if (rule instanceof OperatorNode) { - if (rule.op !== node.op || rule.fn !== node.fn) { - return []; - } - } else if (rule instanceof FunctionNode) { - if (rule.name !== node.name) { - return []; - } - } // rule and node match. Search the children of rule and node. - - if (node.args.length === 1 && rule.args.length === 1 || !isAssociative(node) || isSplit) { - // Expect non-associative operators to match exactly - var childMatches = []; - - for (var i = 0; i < rule.args.length; i++) { - var childMatch = _ruleMatch(rule.args[i], node.args[i]); - - if (childMatch.length === 0) { - // Child did not match, so stop searching immediately - return []; - } // The child matched, so add the information returned from the child to our result - - childMatches.push(childMatch); - } - - res = mergeChildMatches(childMatches); - } else if (node.args.length >= 2 && rule.args.length === 2) { - // node is flattened, rule is not - // Associative operators/functions can be split in different ways so we check if the rule matches each - // them and return their union. - var splits = getSplits(node, rule.context); - var splitMatches = []; - - for (var _i = 0; _i < splits.length; _i++) { - var matchSet = _ruleMatch(rule, splits[_i], true); // recursing at the same tree depth here - - splitMatches = splitMatches.concat(matchSet); - } - - return splitMatches; - } else if (rule.args.length > 2) { - throw Error('Unexpected non-binary associative function: ' + rule.toString()); - } else { - // Incorrect number of arguments in rule and node, so no match - return []; - } - } else if (rule instanceof SymbolNode) { - // If the rule is a SymbolNode, then it carries a special meaning - // according to the first character of the symbol node name. - // c.* matches a ConstantNode - // n.* matches any node - if (rule.name.length === 0) { - throw new Error('Symbol in rule has 0 length...!?'); - } - - if (SUPPORTED_CONSTANTS[rule.name]) { - // built-in constant must match exactly - if (rule.name !== node.name) { - return []; - } - } else if (rule.name[0] === 'n' || rule.name.substring(0, 2) === '_p') { - // rule matches _anything_, so assign this node to the rule.name placeholder - // Assign node to the rule.name placeholder. - // Our parent will check for matches among placeholders. - res[0].placeholders[rule.name] = node; - } else if (rule.name[0] === 'v') { - // rule matches any variable thing (not a ConstantNode) - if (!Object(is["l" /* isConstantNode */])(node)) { - res[0].placeholders[rule.name] = node; - } else { - // Mis-match: rule was expecting something other than a ConstantNode - return []; - } - } else if (rule.name[0] === 'c') { - // rule matches any ConstantNode - if (node instanceof ConstantNode) { - res[0].placeholders[rule.name] = node; - } else { - // Mis-match: rule was expecting a ConstantNode - return []; - } - } else { - throw new Error('Invalid symbol in rule: ' + rule.name); - } - } else if (rule instanceof ConstantNode) { - // Literal constant must match exactly - if (!equal(rule.value, node.value)) { - return []; - } - } else { - // Some other node was encountered which we aren't prepared for, so no match - return []; - } // It's a match! - // console.log('_ruleMatch(' + rule.toString() + ', ' + node.toString() + ') found a match') - - return res; - } - /** - * Determines whether p and q (and all their children nodes) are identical. - * - * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} p - * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} q - * @return {Object} Information about the match, if it exists. - */ - - function _exactMatch(p, q) { - if (p instanceof ConstantNode && q instanceof ConstantNode) { - if (!equal(p.value, q.value)) { - return false; - } - } else if (p instanceof SymbolNode && q instanceof SymbolNode) { - if (p.name !== q.name) { - return false; - } - } else if (p instanceof OperatorNode && q instanceof OperatorNode || p instanceof FunctionNode && q instanceof FunctionNode) { - if (p instanceof OperatorNode) { - if (p.op !== q.op || p.fn !== q.fn) { - return false; - } - } else if (p instanceof FunctionNode) { - if (p.name !== q.name) { - return false; - } - } - - if (p.args.length !== q.args.length) { - return false; - } - - for (var i = 0; i < p.args.length; i++) { - if (!_exactMatch(p.args[i], q.args[i])) { - return false; - } - } - } else { - return false; - } - - return true; - } - - return simplify; - }); - // CONCATENATED MODULE: ./src/function/algebra/derivative.js - - var derivative_name = 'derivative'; - var derivative_dependencies = ['typed', 'config', 'parse', 'simplify', 'equal', 'isZero', 'numeric', 'ConstantNode', 'FunctionNode', 'OperatorNode', 'ParenthesisNode', 'SymbolNode']; - var createDerivative = /* #__PURE__ */Object(factory["a" /* factory */])(derivative_name, derivative_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - parse = _ref.parse, - simplify = _ref.simplify, - equal = _ref.equal, - isZero = _ref.isZero, - numeric = _ref.numeric, - ConstantNode = _ref.ConstantNode, - FunctionNode = _ref.FunctionNode, - OperatorNode = _ref.OperatorNode, - ParenthesisNode = _ref.ParenthesisNode, - SymbolNode = _ref.SymbolNode; - - /** - * Takes the derivative of an expression expressed in parser Nodes. - * The derivative will be taken over the supplied variable in the - * second parameter. If there are multiple variables in the expression, - * it will return a partial derivative. - * - * This uses rules of differentiation which can be found here: - * - * - [Differentiation rules (Wikipedia)](https://en.wikipedia.org/wiki/Differentiation_rules) - * - * Syntax: - * - * derivative(expr, variable) - * derivative(expr, variable, options) - * - * Examples: - * - * math.derivative('x^2', 'x') // Node {2 * x} - * math.derivative('x^2', 'x', {simplify: false}) // Node {2 * 1 * x ^ (2 - 1) - * math.derivative('sin(2x)', 'x')) // Node {2 * cos(2 * x)} - * math.derivative('2*x', 'x').evaluate() // number 2 - * math.derivative('x^2', 'x').evaluate({x: 4}) // number 8 - * const f = math.parse('x^2') - * const x = math.parse('x') - * math.derivative(f, x) // Node {2 * x} - * - * See also: - * - * simplify, parse, evaluate - * - * @param {Node | string} expr The expression to differentiate - * @param {SymbolNode | string} variable The variable over which to differentiate - * @param {{simplify: boolean}} [options] - * There is one option available, `simplify`, which - * is true by default. When false, output will not - * be simplified. - * @return {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} The derivative of `expr` - */ - var derivative = typed('derivative', { - 'Node, SymbolNode, Object': function NodeSymbolNodeObject(expr, variable, options) { - var constNodes = {}; - constTag(constNodes, expr, variable.name); - - var res = _derivative(expr, constNodes); - - return options.simplify ? simplify(res) : res; - }, - 'Node, SymbolNode': function NodeSymbolNode(expr, variable) { - return this(expr, variable, { - simplify: true - }); - }, - 'string, SymbolNode': function stringSymbolNode(expr, variable) { - return this(parse(expr), variable); - }, - 'string, SymbolNode, Object': function stringSymbolNodeObject(expr, variable, options) { - return this(parse(expr), variable, options); - }, - 'string, string': function stringString(expr, variable) { - return this(parse(expr), parse(variable)); - }, - 'string, string, Object': function stringStringObject(expr, variable, options) { - return this(parse(expr), parse(variable), options); - }, - 'Node, string': function NodeString(expr, variable) { - return this(expr, parse(variable)); - }, - 'Node, string, Object': function NodeStringObject(expr, variable, options) { - return this(expr, parse(variable), options); - } // TODO: replace the 8 signatures above with 4 as soon as typed-function supports optional arguments - - /* TODO: implement and test syntax with order of derivatives -> implement as an option {order: number} - 'Node, SymbolNode, ConstantNode': function (expr, variable, {order}) { - let res = expr - for (let i = 0; i < order; i++) { - let constNodes = {} - constTag(constNodes, expr, variable.name) - res = _derivative(res, constNodes) - } - return res - } - */ - - }); - derivative._simplify = true; - - derivative.toTex = function (deriv) { - return _derivTex.apply(null, deriv.args); - }; // FIXME: move the toTex method of derivative to latex.js. Difficulty is that it relies on parse. - // NOTE: the optional "order" parameter here is currently unused - - var _derivTex = typed('_derivTex', { - 'Node, SymbolNode': function NodeSymbolNode(expr, x) { - if (Object(is["l" /* isConstantNode */])(expr) && Object(is["M" /* typeOf */])(expr.value) === 'string') { - return _derivTex(parse(expr.value).toString(), x.toString(), 1); - } else { - return _derivTex(expr.toString(), x.toString(), 1); - } - }, - 'Node, ConstantNode': function NodeConstantNode(expr, x) { - if (Object(is["M" /* typeOf */])(x.value) === 'string') { - return _derivTex(expr, parse(x.value)); - } else { - throw new Error("The second parameter to 'derivative' is a non-string constant"); - } - }, - 'Node, SymbolNode, ConstantNode': function NodeSymbolNodeConstantNode(expr, x, order) { - return _derivTex(expr.toString(), x.name, order.value); - }, - 'string, string, number': function stringStringNumber(expr, x, order) { - var d; - - if (order === 1) { - d = '{d\\over d' + x + '}'; - } else { - d = '{d^{' + order + '}\\over d' + x + '^{' + order + '}}'; - } - - return d + "\\left[".concat(expr, "\\right]"); - } - }); - /** - * Does a depth-first search on the expression tree to identify what Nodes - * are constants (e.g. 2 + 2), and stores the ones that are constants in - * constNodes. Classification is done as follows: - * - * 1. ConstantNodes are constants. - * 2. If there exists a SymbolNode, of which we are differentiating over, - * in the subtree it is not constant. - * - * @param {Object} constNodes Holds the nodes that are constant - * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} node - * @param {string} varName Variable that we are differentiating - * @return {boolean} if node is constant - */ - // TODO: can we rewrite constTag into a pure function? - - var constTag = typed('constTag', { - 'Object, ConstantNode, string': function ObjectConstantNodeString(constNodes, node) { - constNodes[node] = true; - return true; - }, - 'Object, SymbolNode, string': function ObjectSymbolNodeString(constNodes, node, varName) { - // Treat other variables like constants. For reasoning, see: - // https://en.wikipedia.org/wiki/Partial_derivative - if (node.name !== varName) { - constNodes[node] = true; - return true; - } - - return false; - }, - 'Object, ParenthesisNode, string': function ObjectParenthesisNodeString(constNodes, node, varName) { - return constTag(constNodes, node.content, varName); - }, - 'Object, FunctionAssignmentNode, string': function ObjectFunctionAssignmentNodeString(constNodes, node, varName) { - if (node.params.indexOf(varName) === -1) { - constNodes[node] = true; - return true; - } - - return constTag(constNodes, node.expr, varName); - }, - 'Object, FunctionNode | OperatorNode, string': function ObjectFunctionNodeOperatorNodeString(constNodes, node, varName) { - if (node.args.length > 0) { - var isConst = constTag(constNodes, node.args[0], varName); - - for (var i = 1; i < node.args.length; ++i) { - isConst = constTag(constNodes, node.args[i], varName) && isConst; - } - - if (isConst) { - constNodes[node] = true; - return true; - } - } - - return false; - } - }); - /** - * Applies differentiation rules. - * - * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} node - * @param {Object} constNodes Holds the nodes that are constant - * @return {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} The derivative of `expr` - */ - - var _derivative = typed('_derivative', { - 'ConstantNode, Object': function ConstantNodeObject(node) { - return createConstantNode(0); - }, - 'SymbolNode, Object': function SymbolNodeObject(node, constNodes) { - if (constNodes[node] !== undefined) { - return createConstantNode(0); - } - - return createConstantNode(1); - }, - 'ParenthesisNode, Object': function ParenthesisNodeObject(node, constNodes) { - return new ParenthesisNode(_derivative(node.content, constNodes)); - }, - 'FunctionAssignmentNode, Object': function FunctionAssignmentNodeObject(node, constNodes) { - if (constNodes[node] !== undefined) { - return createConstantNode(0); - } - - return _derivative(node.expr, constNodes); - }, - 'FunctionNode, Object': function FunctionNodeObject(node, constNodes) { - if (node.args.length !== 1) { - funcArgsCheck(node); - } - - if (constNodes[node] !== undefined) { - return createConstantNode(0); - } - - var arg0 = node.args[0]; - var arg1; - var div = false; // is output a fraction? - - var negative = false; // is output negative? - - var funcDerivative; - - switch (node.name) { - case 'cbrt': - // d/dx(cbrt(x)) = 1 / (3x^(2/3)) - div = true; - funcDerivative = new OperatorNode('*', 'multiply', [createConstantNode(3), new OperatorNode('^', 'pow', [arg0, new OperatorNode('/', 'divide', [createConstantNode(2), createConstantNode(3)])])]); - break; - - case 'sqrt': - case 'nthRoot': - // d/dx(sqrt(x)) = 1 / (2*sqrt(x)) - if (node.args.length === 1) { - div = true; - funcDerivative = new OperatorNode('*', 'multiply', [createConstantNode(2), new FunctionNode('sqrt', [arg0])]); - } else if (node.args.length === 2) { - // Rearrange from nthRoot(x, a) -> x^(1/a) - arg1 = new OperatorNode('/', 'divide', [createConstantNode(1), node.args[1]]); // Is a variable? - - constNodes[arg1] = constNodes[node.args[1]]; - return _derivative(new OperatorNode('^', 'pow', [arg0, arg1]), constNodes); - } - - break; - - case 'log10': - arg1 = createConstantNode(10); - - /* fall through! */ - - case 'log': - if (!arg1 && node.args.length === 1) { - // d/dx(log(x)) = 1 / x - funcDerivative = arg0.clone(); - div = true; - } else if (node.args.length === 1 && arg1 || node.args.length === 2 && constNodes[node.args[1]] !== undefined) { - // d/dx(log(x, c)) = 1 / (x*ln(c)) - funcDerivative = new OperatorNode('*', 'multiply', [arg0.clone(), new FunctionNode('log', [arg1 || node.args[1]])]); - div = true; - } else if (node.args.length === 2) { - // d/dx(log(f(x), g(x))) = d/dx(log(f(x)) / log(g(x))) - return _derivative(new OperatorNode('/', 'divide', [new FunctionNode('log', [arg0]), new FunctionNode('log', [node.args[1]])]), constNodes); - } - - break; - - case 'pow': - constNodes[arg1] = constNodes[node.args[1]]; // Pass to pow operator node parser - - return _derivative(new OperatorNode('^', 'pow', [arg0, node.args[1]]), constNodes); - - case 'exp': - // d/dx(e^x) = e^x - funcDerivative = new FunctionNode('exp', [arg0.clone()]); - break; - - case 'sin': - // d/dx(sin(x)) = cos(x) - funcDerivative = new FunctionNode('cos', [arg0.clone()]); - break; - - case 'cos': - // d/dx(cos(x)) = -sin(x) - funcDerivative = new OperatorNode('-', 'unaryMinus', [new FunctionNode('sin', [arg0.clone()])]); - break; - - case 'tan': - // d/dx(tan(x)) = sec(x)^2 - funcDerivative = new OperatorNode('^', 'pow', [new FunctionNode('sec', [arg0.clone()]), createConstantNode(2)]); - break; - - case 'sec': - // d/dx(sec(x)) = sec(x)tan(x) - funcDerivative = new OperatorNode('*', 'multiply', [node, new FunctionNode('tan', [arg0.clone()])]); - break; - - case 'csc': - // d/dx(csc(x)) = -csc(x)cot(x) - negative = true; - funcDerivative = new OperatorNode('*', 'multiply', [node, new FunctionNode('cot', [arg0.clone()])]); - break; - - case 'cot': - // d/dx(cot(x)) = -csc(x)^2 - negative = true; - funcDerivative = new OperatorNode('^', 'pow', [new FunctionNode('csc', [arg0.clone()]), createConstantNode(2)]); - break; - - case 'asin': - // d/dx(asin(x)) = 1 / sqrt(1 - x^2) - div = true; - funcDerivative = new FunctionNode('sqrt', [new OperatorNode('-', 'subtract', [createConstantNode(1), new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)])])]); - break; - - case 'acos': - // d/dx(acos(x)) = -1 / sqrt(1 - x^2) - div = true; - negative = true; - funcDerivative = new FunctionNode('sqrt', [new OperatorNode('-', 'subtract', [createConstantNode(1), new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)])])]); - break; - - case 'atan': - // d/dx(atan(x)) = 1 / (x^2 + 1) - div = true; - funcDerivative = new OperatorNode('+', 'add', [new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)]), createConstantNode(1)]); - break; - - case 'asec': - // d/dx(asec(x)) = 1 / (|x|*sqrt(x^2 - 1)) - div = true; - funcDerivative = new OperatorNode('*', 'multiply', [new FunctionNode('abs', [arg0.clone()]), new FunctionNode('sqrt', [new OperatorNode('-', 'subtract', [new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)]), createConstantNode(1)])])]); - break; - - case 'acsc': - // d/dx(acsc(x)) = -1 / (|x|*sqrt(x^2 - 1)) - div = true; - negative = true; - funcDerivative = new OperatorNode('*', 'multiply', [new FunctionNode('abs', [arg0.clone()]), new FunctionNode('sqrt', [new OperatorNode('-', 'subtract', [new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)]), createConstantNode(1)])])]); - break; - - case 'acot': - // d/dx(acot(x)) = -1 / (x^2 + 1) - div = true; - negative = true; - funcDerivative = new OperatorNode('+', 'add', [new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)]), createConstantNode(1)]); - break; - - case 'sinh': - // d/dx(sinh(x)) = cosh(x) - funcDerivative = new FunctionNode('cosh', [arg0.clone()]); - break; - - case 'cosh': - // d/dx(cosh(x)) = sinh(x) - funcDerivative = new FunctionNode('sinh', [arg0.clone()]); - break; - - case 'tanh': - // d/dx(tanh(x)) = sech(x)^2 - funcDerivative = new OperatorNode('^', 'pow', [new FunctionNode('sech', [arg0.clone()]), createConstantNode(2)]); - break; - - case 'sech': - // d/dx(sech(x)) = -sech(x)tanh(x) - negative = true; - funcDerivative = new OperatorNode('*', 'multiply', [node, new FunctionNode('tanh', [arg0.clone()])]); - break; - - case 'csch': - // d/dx(csch(x)) = -csch(x)coth(x) - negative = true; - funcDerivative = new OperatorNode('*', 'multiply', [node, new FunctionNode('coth', [arg0.clone()])]); - break; - - case 'coth': - // d/dx(coth(x)) = -csch(x)^2 - negative = true; - funcDerivative = new OperatorNode('^', 'pow', [new FunctionNode('csch', [arg0.clone()]), createConstantNode(2)]); - break; - - case 'asinh': - // d/dx(asinh(x)) = 1 / sqrt(x^2 + 1) - div = true; - funcDerivative = new FunctionNode('sqrt', [new OperatorNode('+', 'add', [new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)]), createConstantNode(1)])]); - break; - - case 'acosh': - // d/dx(acosh(x)) = 1 / sqrt(x^2 - 1); XXX potentially only for x >= 1 (the real spectrum) - div = true; - funcDerivative = new FunctionNode('sqrt', [new OperatorNode('-', 'subtract', [new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)]), createConstantNode(1)])]); - break; - - case 'atanh': - // d/dx(atanh(x)) = 1 / (1 - x^2) - div = true; - funcDerivative = new OperatorNode('-', 'subtract', [createConstantNode(1), new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)])]); - break; - - case 'asech': - // d/dx(asech(x)) = -1 / (x*sqrt(1 - x^2)) - div = true; - negative = true; - funcDerivative = new OperatorNode('*', 'multiply', [arg0.clone(), new FunctionNode('sqrt', [new OperatorNode('-', 'subtract', [createConstantNode(1), new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)])])])]); - break; - - case 'acsch': - // d/dx(acsch(x)) = -1 / (|x|*sqrt(x^2 + 1)) - div = true; - negative = true; - funcDerivative = new OperatorNode('*', 'multiply', [new FunctionNode('abs', [arg0.clone()]), new FunctionNode('sqrt', [new OperatorNode('+', 'add', [new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)]), createConstantNode(1)])])]); - break; - - case 'acoth': - // d/dx(acoth(x)) = -1 / (1 - x^2) - div = true; - negative = true; - funcDerivative = new OperatorNode('-', 'subtract', [createConstantNode(1), new OperatorNode('^', 'pow', [arg0.clone(), createConstantNode(2)])]); - break; - - case 'abs': - // d/dx(abs(x)) = abs(x)/x - funcDerivative = new OperatorNode('/', 'divide', [new FunctionNode(new SymbolNode('abs'), [arg0.clone()]), arg0.clone()]); - break; - - case 'gamma': // Needs digamma function, d/dx(gamma(x)) = gamma(x)digamma(x) - - default: - throw new Error('Function "' + node.name + '" is not supported by derivative, or a wrong number of arguments is passed'); - } - - var op, func; - - if (div) { - op = '/'; - func = 'divide'; - } else { - op = '*'; - func = 'multiply'; - } - /* Apply chain rule to all functions: - F(x) = f(g(x)) - F'(x) = g'(x)*f'(g(x)) */ - - var chainDerivative = _derivative(arg0, constNodes); - - if (negative) { - chainDerivative = new OperatorNode('-', 'unaryMinus', [chainDerivative]); - } - - return new OperatorNode(op, func, [chainDerivative, funcDerivative]); - }, - 'OperatorNode, Object': function OperatorNodeObject(node, constNodes) { - if (constNodes[node] !== undefined) { - return createConstantNode(0); - } - - if (node.op === '+') { - // d/dx(sum(f(x)) = sum(f'(x)) - return new OperatorNode(node.op, node.fn, node.args.map(function (arg) { - return _derivative(arg, constNodes); - })); - } - - if (node.op === '-') { - // d/dx(+/-f(x)) = +/-f'(x) - if (node.isUnary()) { - return new OperatorNode(node.op, node.fn, [_derivative(node.args[0], constNodes)]); - } // Linearity of differentiation, d/dx(f(x) +/- g(x)) = f'(x) +/- g'(x) - - if (node.isBinary()) { - return new OperatorNode(node.op, node.fn, [_derivative(node.args[0], constNodes), _derivative(node.args[1], constNodes)]); - } - } - - if (node.op === '*') { - // d/dx(c*f(x)) = c*f'(x) - var constantTerms = node.args.filter(function (arg) { - return constNodes[arg] !== undefined; - }); - - if (constantTerms.length > 0) { - var nonConstantTerms = node.args.filter(function (arg) { - return constNodes[arg] === undefined; - }); - var nonConstantNode = nonConstantTerms.length === 1 ? nonConstantTerms[0] : new OperatorNode('*', 'multiply', nonConstantTerms); - var newArgs = constantTerms.concat(_derivative(nonConstantNode, constNodes)); - return new OperatorNode('*', 'multiply', newArgs); - } // Product Rule, d/dx(f(x)*g(x)) = f'(x)*g(x) + f(x)*g'(x) - - return new OperatorNode('+', 'add', node.args.map(function (argOuter) { - return new OperatorNode('*', 'multiply', node.args.map(function (argInner) { - return argInner === argOuter ? _derivative(argInner, constNodes) : argInner.clone(); - })); - })); - } - - if (node.op === '/' && node.isBinary()) { - var arg0 = node.args[0]; - var arg1 = node.args[1]; // d/dx(f(x) / c) = f'(x) / c - - if (constNodes[arg1] !== undefined) { - return new OperatorNode('/', 'divide', [_derivative(arg0, constNodes), arg1]); - } // Reciprocal Rule, d/dx(c / f(x)) = -c(f'(x)/f(x)^2) - - if (constNodes[arg0] !== undefined) { - return new OperatorNode('*', 'multiply', [new OperatorNode('-', 'unaryMinus', [arg0]), new OperatorNode('/', 'divide', [_derivative(arg1, constNodes), new OperatorNode('^', 'pow', [arg1.clone(), createConstantNode(2)])])]); - } // Quotient rule, d/dx(f(x) / g(x)) = (f'(x)g(x) - f(x)g'(x)) / g(x)^2 - - return new OperatorNode('/', 'divide', [new OperatorNode('-', 'subtract', [new OperatorNode('*', 'multiply', [_derivative(arg0, constNodes), arg1.clone()]), new OperatorNode('*', 'multiply', [arg0.clone(), _derivative(arg1, constNodes)])]), new OperatorNode('^', 'pow', [arg1.clone(), createConstantNode(2)])]); - } - - if (node.op === '^' && node.isBinary()) { - var _arg = node.args[0]; - var _arg2 = node.args[1]; - - if (constNodes[_arg] !== undefined) { - // If is secretly constant; 0^f(x) = 1 (in JS), 1^f(x) = 1 - if (Object(is["l" /* isConstantNode */])(_arg) && (isZero(_arg.value) || equal(_arg.value, 1))) { - return createConstantNode(0); - } // d/dx(c^f(x)) = c^f(x)*ln(c)*f'(x) - - return new OperatorNode('*', 'multiply', [node, new OperatorNode('*', 'multiply', [new FunctionNode('log', [_arg.clone()]), _derivative(_arg2.clone(), constNodes)])]); - } - - if (constNodes[_arg2] !== undefined) { - if (Object(is["l" /* isConstantNode */])(_arg2)) { - // If is secretly constant; f(x)^0 = 1 -> d/dx(1) = 0 - if (isZero(_arg2.value)) { - return createConstantNode(0); - } // Ignore exponent; f(x)^1 = f(x) - - if (equal(_arg2.value, 1)) { - return _derivative(_arg, constNodes); - } - } // Elementary Power Rule, d/dx(f(x)^c) = c*f'(x)*f(x)^(c-1) - - var powMinusOne = new OperatorNode('^', 'pow', [_arg.clone(), new OperatorNode('-', 'subtract', [_arg2, createConstantNode(1)])]); - return new OperatorNode('*', 'multiply', [_arg2.clone(), new OperatorNode('*', 'multiply', [_derivative(_arg, constNodes), powMinusOne])]); - } // Functional Power Rule, d/dx(f^g) = f^g*[f'*(g/f) + g'ln(f)] - - return new OperatorNode('*', 'multiply', [new OperatorNode('^', 'pow', [_arg.clone(), _arg2.clone()]), new OperatorNode('+', 'add', [new OperatorNode('*', 'multiply', [_derivative(_arg, constNodes), new OperatorNode('/', 'divide', [_arg2.clone(), _arg.clone()])]), new OperatorNode('*', 'multiply', [_derivative(_arg2, constNodes), new FunctionNode('log', [_arg.clone()])])])]); - } - - throw new Error('Operator "' + node.op + '" is not supported by derivative, or a wrong number of arguments is passed'); - } - }); - /** - * Ensures the number of arguments for a function are correct, - * and will throw an error otherwise. - * - * @param {FunctionNode} node - */ - - function funcArgsCheck(node) { - // TODO add min, max etc - if ((node.name === 'log' || node.name === 'nthRoot' || node.name === 'pow') && node.args.length === 2) { - return; - } // There should be an incorrect number of arguments if we reach here - // Change all args to constants to avoid unidentified - // symbol error when compiling function - - for (var i = 0; i < node.args.length; ++i) { - node.args[i] = createConstantNode(0); - } - - node.compile().evaluate(); - throw new Error('Expected TypeError, but none found'); - } - /** - * Helper function to create a constant node with a specific type - * (number, BigNumber, Fraction) - * @param {number} value - * @param {string} [valueType] - * @return {ConstantNode} - */ - - function createConstantNode(value, valueType) { - return new ConstantNode(numeric(value, valueType || config.number)); - } - - return derivative; - }); - // CONCATENATED MODULE: ./src/function/algebra/rationalize.js - - var rationalize_name = 'rationalize'; - var rationalize_dependencies = ['config', 'typed', 'equal', 'isZero', 'add', 'subtract', 'multiply', 'divide', 'pow', 'parse', 'simplify', '?bignumber', '?fraction', 'mathWithTransform', 'ConstantNode', 'OperatorNode', 'FunctionNode', 'SymbolNode', 'ParenthesisNode']; - var createRationalize = /* #__PURE__ */Object(factory["a" /* factory */])(rationalize_name, rationalize_dependencies, function (_ref) { - var config = _ref.config, - typed = _ref.typed, - equal = _ref.equal, - isZero = _ref.isZero, - add = _ref.add, - subtract = _ref.subtract, - multiply = _ref.multiply, - divide = _ref.divide, - pow = _ref.pow, - parse = _ref.parse, - simplify = _ref.simplify, - fraction = _ref.fraction, - bignumber = _ref.bignumber, - mathWithTransform = _ref.mathWithTransform, - ConstantNode = _ref.ConstantNode, - OperatorNode = _ref.OperatorNode, - FunctionNode = _ref.FunctionNode, - SymbolNode = _ref.SymbolNode, - ParenthesisNode = _ref.ParenthesisNode; - var simplifyConstant = createSimplifyConstant({ - typed: typed, - config: config, - mathWithTransform: mathWithTransform, - fraction: fraction, - bignumber: bignumber, - ConstantNode: ConstantNode, - OperatorNode: OperatorNode, - FunctionNode: FunctionNode, - SymbolNode: SymbolNode - }); - var simplifyCore = createSimplifyCore({ - equal: equal, - isZero: isZero, - add: add, - subtract: subtract, - multiply: multiply, - divide: divide, - pow: pow, - ConstantNode: ConstantNode, - OperatorNode: OperatorNode, - FunctionNode: FunctionNode, - ParenthesisNode: ParenthesisNode - }); - /** - * Transform a rationalizable expression in a rational fraction. - * If rational fraction is one variable polynomial then converts - * the numerator and denominator in canonical form, with decreasing - * exponents, returning the coefficients of numerator. - * - * Syntax: - * - * rationalize(expr) - * rationalize(expr, detailed) - * rationalize(expr, scope) - * rationalize(expr, scope, detailed) - * - * Examples: - * - * math.rationalize('sin(x)+y') - * // Error: There is an unsolved function call - * math.rationalize('2x/y - y/(x+1)') - * // (2*x^2-y^2+2*x)/(x*y+y) - * math.rationalize('(2x+1)^6') - * // 64*x^6+192*x^5+240*x^4+160*x^3+60*x^2+12*x+1 - * math.rationalize('2x/( (2x-1) / (3x+2) ) - 5x/ ( (3x+4) / (2x^2-5) ) + 3') - * // -20*x^4+28*x^3+104*x^2+6*x-12)/(6*x^2+5*x-4) - * math.rationalize('x/(1-x)/(x-2)/(x-3)/(x-4) + 2x/ ( (1-2x)/(2-3x) )/ ((3-4x)/(4-5x) )') = - * // (-30*x^7+344*x^6-1506*x^5+3200*x^4-3472*x^3+1846*x^2-381*x)/ - * // (-8*x^6+90*x^5-383*x^4+780*x^3-797*x^2+390*x-72) - * - * math.rationalize('x+x+x+y',{y:1}) // 3*x+1 - * math.rationalize('x+x+x+y',{}) // 3*x+y - * - * const ret = math.rationalize('x+x+x+y',{},true) - * // ret.expression=3*x+y, ret.variables = ["x","y"] - * const ret = math.rationalize('-2+5x^2',{},true) - * // ret.expression=5*x^2-2, ret.variables = ["x"], ret.coefficients=[-2,0,5] - * - * See also: - * - * simplify - * - * @param {Node|string} expr The expression to check if is a polynomial expression - * @param {Object|boolean} optional scope of expression or true for already evaluated rational expression at input - * @param {Boolean} detailed optional True if return an object, false if return expression node (default) - * - * @return {Object | Node} The rational polynomial of `expr` or na object - * {Object} - * {Expression Node} expression: node simplified expression - * {Expression Node} numerator: simplified numerator of expression - * {Expression Node | boolean} denominator: simplified denominator or false (if there is no denominator) - * {Array} variables: variable names - * {Array} coefficients: coefficients of numerator sorted by increased exponent - * {Expression Node} node simplified expression - * - */ - - return typed(rationalize_name, { - string: function string(expr) { - return this(parse(expr), {}, false); - }, - 'string, boolean': function stringBoolean(expr, detailed) { - return this(parse(expr), {}, detailed); - }, - 'string, Object': function stringObject(expr, scope) { - return this(parse(expr), scope, false); - }, - 'string, Object, boolean': function stringObjectBoolean(expr, scope, detailed) { - return this(parse(expr), scope, detailed); - }, - Node: function Node(expr) { - return this(expr, {}, false); - }, - 'Node, boolean': function NodeBoolean(expr, detailed) { - return this(expr, {}, detailed); - }, - 'Node, Object': function NodeObject(expr, scope) { - return this(expr, scope, false); - }, - 'Node, Object, boolean': function NodeObjectBoolean(expr, scope, detailed) { - var setRules = rulesRationalize(); // Rules for change polynomial in near canonical form - - var polyRet = polynomial(expr, scope, true, setRules.firstRules); // Check if expression is a rationalizable polynomial - - var nVars = polyRet.variables.length; - expr = polyRet.expression; - - if (nVars >= 1) { - // If expression in not a constant - expr = expandPower(expr); // First expand power of polynomials (cannot be made from rules!) - - var sBefore; // Previous expression - - var rules; - var eDistrDiv = true; - var redoInic = false; - expr = simplify(expr, setRules.firstRules, {}, { - exactFractions: false - }); // Apply the initial rules, including succ div rules - - var s; - - while (true) { - // Apply alternately successive division rules and distr.div.rules - rules = eDistrDiv ? setRules.distrDivRules : setRules.sucDivRules; - expr = simplify(expr, rules); // until no more changes - - eDistrDiv = !eDistrDiv; // Swap between Distr.Div and Succ. Div. Rules - - s = expr.toString(); - - if (s === sBefore) { - break; // No changes : end of the loop - } - - redoInic = true; - sBefore = s; - } - - if (redoInic) { - // Apply first rules again without succ div rules (if there are changes) - expr = simplify(expr, setRules.firstRulesAgain, {}, { - exactFractions: false - }); - } - - expr = simplify(expr, setRules.finalRules, {}, { - exactFractions: false - }); // Apply final rules - } // NVars >= 1 - - var coefficients = []; - var retRationalize = {}; - - if (expr.type === 'OperatorNode' && expr.isBinary() && expr.op === '/') { - // Separate numerator from denominator - if (nVars === 1) { - expr.args[0] = polyToCanonical(expr.args[0], coefficients); - expr.args[1] = polyToCanonical(expr.args[1]); - } - - if (detailed) { - retRationalize.numerator = expr.args[0]; - retRationalize.denominator = expr.args[1]; - } - } else { - if (nVars === 1) { - expr = polyToCanonical(expr, coefficients); - } - - if (detailed) { - retRationalize.numerator = expr; - retRationalize.denominator = null; - } - } // nVars - - if (!detailed) { - return expr; - } - retRationalize.coefficients = coefficients; - retRationalize.variables = polyRet.variables; - retRationalize.expression = expr; - return retRationalize; - } // ^^^^^^^ end of rationalize ^^^^^^^^ - - }); // end of typed rationalize - - /** - * Function to simplify an expression using an optional scope and - * return it if the expression is a polynomial expression, i.e. - * an expression with one or more variables and the operators - * +, -, *, and ^, where the exponent can only be a positive integer. - * - * Syntax: - * - * polynomial(expr,scope,extended, rules) - * - * @param {Node | string} expr The expression to simplify and check if is polynomial expression - * @param {object} scope Optional scope for expression simplification - * @param {boolean} extended Optional. Default is false. When true allows divide operator. - * @param {array} rules Optional. Default is no rule. - * - * - * @return {Object} - * {Object} node: node simplified expression - * {Array} variables: variable names - */ - - function polynomial(expr, scope, extended, rules) { - var variables = []; - var node = simplify(expr, rules, scope, { - exactFractions: false - }); // Resolves any variables and functions with all defined parameters - - extended = !!extended; - var oper = '+-*' + (extended ? '/' : ''); - recPoly(node); - var retFunc = {}; - retFunc.expression = node; - retFunc.variables = variables; - return retFunc; // ------------------------------------------------------------------------------------------------------- - - /** - * Function to simplify an expression using an optional scope and - * return it if the expression is a polynomial expression, i.e. - * an expression with one or more variables and the operators - * +, -, *, and ^, where the exponent can only be a positive integer. - * - * Syntax: - * - * recPoly(node) - * - * - * @param {Node} node The current sub tree expression in recursion - * - * @return nothing, throw an exception if error - */ - - function recPoly(node) { - var tp = node.type; // node type - - if (tp === 'FunctionNode') { - // No function call in polynomial expression - throw new Error('There is an unsolved function call'); - } else if (tp === 'OperatorNode') { - if (node.op === '^') { - // TODO: handle negative exponents like in '1/x^(-2)' - if (node.args[1].type !== 'ConstantNode' || !Object(utils_number["i" /* isInteger */])(parseFloat(node.args[1].value))) { - throw new Error('There is a non-integer exponent'); - } else { - recPoly(node.args[0]); - } - } else { - if (oper.indexOf(node.op) === -1) { - throw new Error('Operator ' + node.op + ' invalid in polynomial expression'); - } - - for (var i = 0; i < node.args.length; i++) { - recPoly(node.args[i]); - } - } // type of operator - - } else if (tp === 'SymbolNode') { - var _name = node.name; // variable name - - var pos = variables.indexOf(_name); - - if (pos === -1) { - // new variable in expression - variables.push(_name); - } - } else if (tp === 'ParenthesisNode') { - recPoly(node.content); - } else if (tp !== 'ConstantNode') { - throw new Error('type ' + tp + ' is not allowed in polynomial expression'); - } - } // end of recPoly - - } // end of polynomial - // --------------------------------------------------------------------------------------- - - /** - * Return a rule set to rationalize an polynomial expression in rationalize - * - * Syntax: - * - * rulesRationalize() - * - * @return {array} rule set to rationalize an polynomial expression - */ - - function rulesRationalize() { - var oldRules = [simplifyCore, // sCore - { - l: 'n+n', - r: '2*n' - }, { - l: 'n+-n', - r: '0' - }, simplifyConstant, // sConstant - { - l: 'n*(n1^-1)', - r: 'n/n1' - }, { - l: 'n*n1^-n2', - r: 'n/n1^n2' - }, { - l: 'n1^-1', - r: '1/n1' - }, { - l: 'n*(n1/n2)', - r: '(n*n1)/n2' - }, { - l: '1*n', - r: 'n' - }]; - var rulesFirst = [{ - l: '(-n1)/(-n2)', - r: 'n1/n2' - }, // Unary division - { - l: '(-n1)*(-n2)', - r: 'n1*n2' - }, // Unary multiplication - { - l: 'n1--n2', - r: 'n1+n2' - }, // '--' elimination - { - l: 'n1-n2', - r: 'n1+(-n2)' - }, // Subtraction turn into add with un�ry minus - { - l: '(n1+n2)*n3', - r: '(n1*n3 + n2*n3)' - }, // Distributive 1 - { - l: 'n1*(n2+n3)', - r: '(n1*n2+n1*n3)' - }, // Distributive 2 - { - l: 'c1*n + c2*n', - r: '(c1+c2)*n' - }, // Joining constants - { - l: 'c1*n + n', - r: '(c1+1)*n' - }, // Joining constants - { - l: 'c1*n - c2*n', - r: '(c1-c2)*n' - }, // Joining constants - { - l: 'c1*n - n', - r: '(c1-1)*n' - }, // Joining constants - { - l: 'v/c', - r: '(1/c)*v' - }, // variable/constant (new!) - { - l: 'v/-c', - r: '-(1/c)*v' - }, // variable/constant (new!) - { - l: '-v*-c', - r: 'c*v' - }, // Inversion constant and variable 1 - { - l: '-v*c', - r: '-c*v' - }, // Inversion constant and variable 2 - { - l: 'v*-c', - r: '-c*v' - }, // Inversion constant and variable 3 - { - l: 'v*c', - r: 'c*v' - }, // Inversion constant and variable 4 - { - l: '-(-n1*n2)', - r: '(n1*n2)' - }, // Unary propagation - { - l: '-(n1*n2)', - r: '(-n1*n2)' - }, // Unary propagation - { - l: '-(-n1+n2)', - r: '(n1-n2)' - }, // Unary propagation - { - l: '-(n1+n2)', - r: '(-n1-n2)' - }, // Unary propagation - { - l: '(n1^n2)^n3', - r: '(n1^(n2*n3))' - }, // Power to Power - { - l: '-(-n1/n2)', - r: '(n1/n2)' - }, // Division and Unary - { - l: '-(n1/n2)', - r: '(-n1/n2)' - }]; // Divisao and Unary - - var rulesDistrDiv = [{ - l: '(n1/n2 + n3/n4)', - r: '((n1*n4 + n3*n2)/(n2*n4))' - }, // Sum of fractions - { - l: '(n1/n2 + n3)', - r: '((n1 + n3*n2)/n2)' - }, // Sum fraction with number 1 - { - l: '(n1 + n2/n3)', - r: '((n1*n3 + n2)/n3)' - }]; // Sum fraction with number 1 - - var rulesSucDiv = [{ - l: '(n1/(n2/n3))', - r: '((n1*n3)/n2)' - }, // Division simplification - { - l: '(n1/n2/n3)', - r: '(n1/(n2*n3))' - }]; - var setRules = {}; // rules set in 4 steps. - // All rules => infinite loop - // setRules.allRules =oldRules.concat(rulesFirst,rulesDistrDiv,rulesSucDiv) - - setRules.firstRules = oldRules.concat(rulesFirst, rulesSucDiv); // First rule set - - setRules.distrDivRules = rulesDistrDiv; // Just distr. div. rules - - setRules.sucDivRules = rulesSucDiv; // Jus succ. div. rules - - setRules.firstRulesAgain = oldRules.concat(rulesFirst); // Last rules set without succ. div. - // Division simplification - // Second rule set. - // There is no aggregate expression with parentesis, but the only variable can be scattered. - - setRules.finalRules = [simplifyCore, // simplify.rules[0] - { - l: 'n*-n', - r: '-n^2' - }, // Joining multiply with power 1 - { - l: 'n*n', - r: 'n^2' - }, // Joining multiply with power 2 - simplifyConstant, // simplify.rules[14] old 3rd index in oldRules - { - l: 'n*-n^n1', - r: '-n^(n1+1)' - }, // Joining multiply with power 3 - { - l: 'n*n^n1', - r: 'n^(n1+1)' - }, // Joining multiply with power 4 - { - l: 'n^n1*-n^n2', - r: '-n^(n1+n2)' - }, // Joining multiply with power 5 - { - l: 'n^n1*n^n2', - r: 'n^(n1+n2)' - }, // Joining multiply with power 6 - { - l: 'n^n1*-n', - r: '-n^(n1+1)' - }, // Joining multiply with power 7 - { - l: 'n^n1*n', - r: 'n^(n1+1)' - }, // Joining multiply with power 8 - { - l: 'n^n1/-n', - r: '-n^(n1-1)' - }, // Joining multiply with power 8 - { - l: 'n^n1/n', - r: 'n^(n1-1)' - }, // Joining division with power 1 - { - l: 'n/-n^n1', - r: '-n^(1-n1)' - }, // Joining division with power 2 - { - l: 'n/n^n1', - r: 'n^(1-n1)' - }, // Joining division with power 3 - { - l: 'n^n1/-n^n2', - r: 'n^(n1-n2)' - }, // Joining division with power 4 - { - l: 'n^n1/n^n2', - r: 'n^(n1-n2)' - }, // Joining division with power 5 - { - l: 'n1+(-n2*n3)', - r: 'n1-n2*n3' - }, // Solving useless parenthesis 1 - { - l: 'v*(-c)', - r: '-c*v' - }, // Solving useless unary 2 - { - l: 'n1+-n2', - r: 'n1-n2' - }, // Solving +- together (new!) - { - l: 'v*c', - r: 'c*v' - }, // inversion constant with variable - { - l: '(n1^n2)^n3', - r: '(n1^(n2*n3))' - } // Power to Power - ]; - return setRules; - } // End rulesRationalize - // --------------------------------------------------------------------------------------- - - /** - * Expand recursively a tree node for handling with expressions with exponents - * (it's not for constants, symbols or functions with exponents) - * PS: The other parameters are internal for recursion - * - * Syntax: - * - * expandPower(node) - * - * @param {Node} node Current expression node - * @param {node} parent Parent current node inside the recursion - * @param (int} Parent number of chid inside the rercursion - * - * @return {node} node expression with all powers expanded. - */ - - function expandPower(node, parent, indParent) { - var tp = node.type; - var internal = arguments.length > 1; // TRUE in internal calls - - if (tp === 'OperatorNode' && node.isBinary()) { - var does = false; - var val; - - if (node.op === '^') { - // First operator: Parenthesis or UnaryMinus - if ((node.args[0].type === 'ParenthesisNode' || node.args[0].type === 'OperatorNode') && node.args[1].type === 'ConstantNode') { - // Second operator: Constant - val = parseFloat(node.args[1].value); - does = val >= 2 && Object(utils_number["i" /* isInteger */])(val); - } - } - - if (does) { - // Exponent >= 2 - // Before: - // operator A --> Subtree - // parent pow - // constant - // - if (val > 2) { - // Exponent > 2, - // AFTER: (exponent > 2) - // operator A --> Subtree - // parent * - // deep clone (operator A --> Subtree - // pow - // constant - 1 - // - var nEsqTopo = node.args[0]; - var nDirTopo = new OperatorNode('^', 'pow', [node.args[0].cloneDeep(), new ConstantNode(val - 1)]); - node = new OperatorNode('*', 'multiply', [nEsqTopo, nDirTopo]); - } else { - // Expo = 2 - no power - // AFTER: (exponent = 2) - // operator A --> Subtree - // parent oper - // deep clone (operator A --> Subtree) - // - node = new OperatorNode('*', 'multiply', [node.args[0], node.args[0].cloneDeep()]); - } - - if (internal) { - // Change parent references in internal recursive calls - if (indParent === 'content') { - parent.content = node; - } else { - parent.args[indParent] = node; - } - } - } // does - - } // binary OperatorNode - - if (tp === 'ParenthesisNode') { - // Recursion - expandPower(node.content, node, 'content'); - } else if (tp !== 'ConstantNode' && tp !== 'SymbolNode') { - for (var i = 0; i < node.args.length; i++) { - expandPower(node.args[i], node, i); - } - } - - if (!internal) { - // return the root node - return node; - } - } // End expandPower - // --------------------------------------------------------------------------------------- - - /** - * Auxilary function for rationalize - * Convert near canonical polynomial in one variable in a canonical polynomial - * with one term for each exponent in decreasing order - * - * Syntax: - * - * polyToCanonical(node [, coefficients]) - * - * @param {Node | string} expr The near canonical polynomial expression to convert in a a canonical polynomial expression - * - * The string or tree expression needs to be at below syntax, with free spaces: - * ( (^(-)? | [+-]? )cte (*)? var (^expo)? | cte )+ - * Where 'var' is one variable with any valid name - * 'cte' are real numeric constants with any value. It can be omitted if equal than 1 - * 'expo' are integers greater than 0. It can be omitted if equal than 1. - * - * @param {array} coefficients Optional returns coefficients sorted by increased exponent - * - * - * @return {node} new node tree with one variable polynomial or string error. - */ - - function polyToCanonical(node, coefficients) { - if (coefficients === undefined) { - coefficients = []; - } // coefficients. - - coefficients[0] = 0; // index is the exponent - - var o = {}; - o.cte = 1; - o.oper = '+'; // fire: mark with * or ^ when finds * or ^ down tree, reset to "" with + and -. - // It is used to deduce the exponent: 1 for *, 0 for "". - - o.fire = ''; - var maxExpo = 0; // maximum exponent - - var varname = ''; // variable name - - recurPol(node, null, o); - maxExpo = coefficients.length - 1; - var first = true; - var no; - - for (var i = maxExpo; i >= 0; i--) { - if (coefficients[i] === 0) { - continue; - } - var n1 = new ConstantNode(first ? coefficients[i] : Math.abs(coefficients[i])); - var op = coefficients[i] < 0 ? '-' : '+'; - - if (i > 0) { - // Is not a constant without variable - var n2 = new SymbolNode(varname); - - if (i > 1) { - var n3 = new ConstantNode(i); - n2 = new OperatorNode('^', 'pow', [n2, n3]); - } - - if (coefficients[i] === -1 && first) { - n1 = new OperatorNode('-', 'unaryMinus', [n2]); - } else if (Math.abs(coefficients[i]) === 1) { - n1 = n2; - } else { - n1 = new OperatorNode('*', 'multiply', [n1, n2]); - } - } - - if (first) { - no = n1; - } else if (op === '+') { - no = new OperatorNode('+', 'add', [no, n1]); - } else { - no = new OperatorNode('-', 'subtract', [no, n1]); - } - - first = false; - } // for - - if (first) { - return new ConstantNode(0); - } else { - return no; - } - /** - * Recursive auxilary function inside polyToCanonical for - * converting expression in canonical form - * - * Syntax: - * - * recurPol(node, noPai, obj) - * - * @param {Node} node The current subpolynomial expression - * @param {Node | Null} noPai The current parent node - * @param {object} obj Object with many internal flags - * - * @return {} No return. If error, throws an exception - */ - - function recurPol(node, noPai, o) { - var tp = node.type; - - if (tp === 'FunctionNode') { - // ***** FunctionName ***** - // No function call in polynomial expression - throw new Error('There is an unsolved function call'); - } else if (tp === 'OperatorNode') { - // ***** OperatorName ***** - if ('+-*^'.indexOf(node.op) === -1) { - throw new Error('Operator ' + node.op + ' invalid'); - } - - if (noPai !== null) { - // -(unary),^ : children of *,+,- - if ((node.fn === 'unaryMinus' || node.fn === 'pow') && noPai.fn !== 'add' && noPai.fn !== 'subtract' && noPai.fn !== 'multiply') { - throw new Error('Invalid ' + node.op + ' placing'); - } // -,+,* : children of +,- - - if ((node.fn === 'subtract' || node.fn === 'add' || node.fn === 'multiply') && noPai.fn !== 'add' && noPai.fn !== 'subtract') { - throw new Error('Invalid ' + node.op + ' placing'); - } // -,+ : first child - - if ((node.fn === 'subtract' || node.fn === 'add' || node.fn === 'unaryMinus') && o.noFil !== 0) { - throw new Error('Invalid ' + node.op + ' placing'); - } - } // Has parent - // Firers: ^,* Old: ^,&,-(unary): firers - - if (node.op === '^' || node.op === '*') { - o.fire = node.op; - } - - for (var _i = 0; _i < node.args.length; _i++) { - // +,-: reset fire - if (node.fn === 'unaryMinus') { - o.oper = '-'; - } - - if (node.op === '+' || node.fn === 'subtract') { - o.fire = ''; - o.cte = 1; // default if there is no constant - - o.oper = _i === 0 ? '+' : node.op; - } - - o.noFil = _i; // number of son - - recurPol(node.args[_i], node, o); - } // for in children - - } else if (tp === 'SymbolNode') { - // ***** SymbolName ***** - if (node.name !== varname && varname !== '') { - throw new Error('There is more than one variable'); - } - - varname = node.name; - - if (noPai === null) { - coefficients[1] = 1; - return; - } // ^: Symbol is First child - - if (noPai.op === '^' && o.noFil !== 0) { - throw new Error('In power the variable should be the first parameter'); - } // *: Symbol is Second child - - if (noPai.op === '*' && o.noFil !== 1) { - throw new Error('In multiply the variable should be the second parameter'); - } // Symbol: firers '',* => it means there is no exponent above, so it's 1 (cte * var) - - if (o.fire === '' || o.fire === '*') { - if (maxExpo < 1) { - coefficients[1] = 0; - } - coefficients[1] += o.cte * (o.oper === '+' ? 1 : -1); - maxExpo = Math.max(1, maxExpo); - } - } else if (tp === 'ConstantNode') { - var valor = parseFloat(node.value); - - if (noPai === null) { - coefficients[0] = valor; - return; - } - - if (noPai.op === '^') { - // cte: second child of power - if (o.noFil !== 1) { - throw new Error('Constant cannot be powered'); - } - - if (!Object(utils_number["i" /* isInteger */])(valor) || valor <= 0) { - throw new Error('Non-integer exponent is not allowed'); - } - - for (var _i2 = maxExpo + 1; _i2 < valor; _i2++) { - coefficients[_i2] = 0; - } - - if (valor > maxExpo) { - coefficients[valor] = 0; - } - coefficients[valor] += o.cte * (o.oper === '+' ? 1 : -1); - maxExpo = Math.max(valor, maxExpo); - return; - } - - o.cte = valor; // Cte: firer '' => There is no exponent and no multiplication, so the exponent is 0. - - if (o.fire === '') { - coefficients[0] += o.cte * (o.oper === '+' ? 1 : -1); - } - } else { - throw new Error('Type ' + tp + ' is not allowed'); - } - } // End of recurPol - - } // End of polyToCanonical - - }); - // CONCATENATED MODULE: ./src/json/reviver.js - - var reviver_name = 'reviver'; - var reviver_dependencies = ['classes']; - var createReviver = /* #__PURE__ */Object(factory["a" /* factory */])(reviver_name, reviver_dependencies, function (_ref) { - var classes = _ref.classes; - - /** - * Instantiate mathjs data types from their JSON representation - * @param {string} key - * @param {*} value - * @returns {*} Returns the revived object - */ - return function reviver(key, value) { - var constructor = classes[value && value.mathjs]; - - if (constructor && typeof constructor.fromJSON === 'function') { - return constructor.fromJSON(value); - } - - return value; - }; - }); - // CONCATENATED MODULE: ./src/json/replacer.js - - var replacer_name = 'replacer'; - var replacer_dependencies = []; - var createReplacer = /* #__PURE__ */Object(factory["a" /* factory */])(replacer_name, replacer_dependencies, function () { - /** - * Stringify data types into their JSON representation. - * Most data types can be serialized using their `.toJSON` method, - * but not all, for example the number `Infinity`. For these cases you have - * to use the replacer. Example usage: - * - * JSON.stringify([2, Infinity], math.replacer) - * - * @param {string} key - * @param {*} value - * @returns {*} Returns the replaced object - */ - return function replacer(key, value) { - // the numeric values Infinitiy, -Infinity, and NaN cannot be serialized to JSON - if (typeof value === 'number' && (!isFinite(value) || isNaN(value))) { - return { - mathjs: 'number', - value: String(value) - }; - } - - return value; - }; - }); - // CONCATENATED MODULE: ./src/version.js - var version = '7.5.0'; // Note: This file is automatically generated when building math.js. - // Changes made in this file will be overwritten. - // CONCATENATED MODULE: ./src/plain/number/constants.js - var constants_pi = Math.PI; - var tau = 2 * Math.PI; - var constants_e = Math.E; - var constants_phi = 1.61803398874989484820458683436563811772030917980576286213545; - // CONCATENATED MODULE: ./src/constants.js - - var createTrue = /* #__PURE__ */Object(factory["a" /* factory */])('true', [], function () { - return true; - }); - var createFalse = /* #__PURE__ */Object(factory["a" /* factory */])('false', [], function () { - return false; - }); - var createNull = /* #__PURE__ */Object(factory["a" /* factory */])('null', [], function () { - return null; - }); - var createInfinity = /* #__PURE__ */recreateFactory('Infinity', ['config', '?BigNumber'], function (_ref) { - var config = _ref.config, - BigNumber = _ref.BigNumber; - return config.number === 'BigNumber' ? new BigNumber(Infinity) : Infinity; - }); - var createNaN = /* #__PURE__ */recreateFactory('NaN', ['config', '?BigNumber'], function (_ref2) { - var config = _ref2.config, - BigNumber = _ref2.BigNumber; - return config.number === 'BigNumber' ? new BigNumber(NaN) : NaN; - }); - var createPi = /* #__PURE__ */recreateFactory('pi', ['config', '?BigNumber'], function (_ref3) { - var config = _ref3.config, - BigNumber = _ref3.BigNumber; - return config.number === 'BigNumber' ? createBigNumberPi(BigNumber) : constants_pi; - }); - var createTau = /* #__PURE__ */recreateFactory('tau', ['config', '?BigNumber'], function (_ref4) { - var config = _ref4.config, - BigNumber = _ref4.BigNumber; - return config.number === 'BigNumber' ? createBigNumberTau(BigNumber) : tau; - }); - var createE = /* #__PURE__ */recreateFactory('e', ['config', '?BigNumber'], function (_ref5) { - var config = _ref5.config, - BigNumber = _ref5.BigNumber; - return config.number === 'BigNumber' ? createBigNumberE(BigNumber) : constants_e; - }); // golden ratio, (1+sqrt(5))/2 - - var createPhi = /* #__PURE__ */recreateFactory('phi', ['config', '?BigNumber'], function (_ref6) { - var config = _ref6.config, - BigNumber = _ref6.BigNumber; - return config.number === 'BigNumber' ? createBigNumberPhi(BigNumber) : constants_phi; - }); - var createLN2 = /* #__PURE__ */recreateFactory('LN2', ['config', '?BigNumber'], function (_ref7) { - var config = _ref7.config, - BigNumber = _ref7.BigNumber; - return config.number === 'BigNumber' ? new BigNumber(2).ln() : Math.LN2; - }); - var createLN10 = /* #__PURE__ */recreateFactory('LN10', ['config', '?BigNumber'], function (_ref8) { - var config = _ref8.config, - BigNumber = _ref8.BigNumber; - return config.number === 'BigNumber' ? new BigNumber(10).ln() : Math.LN10; - }); - var createLOG2E = /* #__PURE__ */recreateFactory('LOG2E', ['config', '?BigNumber'], function (_ref9) { - var config = _ref9.config, - BigNumber = _ref9.BigNumber; - return config.number === 'BigNumber' ? new BigNumber(1).div(new BigNumber(2).ln()) : Math.LOG2E; - }); - var createLOG10E = /* #__PURE__ */recreateFactory('LOG10E', ['config', '?BigNumber'], function (_ref10) { - var config = _ref10.config, - BigNumber = _ref10.BigNumber; - return config.number === 'BigNumber' ? new BigNumber(1).div(new BigNumber(10).ln()) : Math.LOG10E; - }); - var createSQRT1_2 = /* #__PURE__ */recreateFactory( // eslint-disable-line camelcase - 'SQRT1_2', ['config', '?BigNumber'], function (_ref11) { - var config = _ref11.config, - BigNumber = _ref11.BigNumber; - return config.number === 'BigNumber' ? new BigNumber('0.5').sqrt() : Math.SQRT1_2; - }); - var createSQRT2 = /* #__PURE__ */recreateFactory('SQRT2', ['config', '?BigNumber'], function (_ref12) { - var config = _ref12.config, - BigNumber = _ref12.BigNumber; - return config.number === 'BigNumber' ? new BigNumber(2).sqrt() : Math.SQRT2; - }); - var createI = /* #__PURE__ */recreateFactory('i', ['Complex'], function (_ref13) { - var Complex = _ref13.Complex; - return Complex.I; - }); // for backward compatibility with v5 - - var createUppercasePi = /* #__PURE__ */Object(factory["a" /* factory */])('PI', ['pi'], function (_ref14) { - var pi = _ref14.pi; - return pi; - }); - var createUppercaseE = /* #__PURE__ */Object(factory["a" /* factory */])('E', ['e'], function (_ref15) { - var e = _ref15.e; - return e; - }); - var createVersion = /* #__PURE__ */Object(factory["a" /* factory */])('version', [], function () { - return version; - }); // helper function to create a factory with a flag recreateOnConfigChange - // idea: allow passing optional properties to be attached to the factory function as 4th argument? - - function recreateFactory(name, dependencies, create) { - return Object(factory["a" /* factory */])(name, dependencies, create, { - recreateOnConfigChange: true - }); - } - // CONCATENATED MODULE: ./src/type/unit/physicalConstants.js - // Source: https://en.wikipedia.org/wiki/Physical_constant - // Universal constants - - var createSpeedOfLight = /* #__PURE__ */unitFactory('speedOfLight', '299792458', 'm s^-1'); - var createGravitationConstant = /* #__PURE__ */unitFactory('gravitationConstant', '6.67430e-11', 'm^3 kg^-1 s^-2'); - var createPlanckConstant = /* #__PURE__ */unitFactory('planckConstant', '6.62607015e-34', 'J s'); - var createReducedPlanckConstant = /* #__PURE__ */unitFactory('reducedPlanckConstant', '1.0545718176461565e-34', 'J s'); // Electromagnetic constants - - var createMagneticConstant = /* #__PURE__ */unitFactory('magneticConstant', '1.25663706212e-6', 'N A^-2'); - var createElectricConstant = /* #__PURE__ */unitFactory('electricConstant', '8.8541878128e-12', 'F m^-1'); - var createVacuumImpedance = /* #__PURE__ */unitFactory('vacuumImpedance', '376.730313667', 'ohm'); - var createCoulomb = /* #__PURE__ */unitFactory('coulomb', '8.987551792261171e9', 'N m^2 C^-2'); - var createElementaryCharge = /* #__PURE__ */unitFactory('elementaryCharge', '1.602176634e-19', 'C'); - var createBohrMagneton = /* #__PURE__ */unitFactory('bohrMagneton', '9.2740100783e-24', 'J T^-1'); - var createConductanceQuantum = /* #__PURE__ */unitFactory('conductanceQuantum', '7.748091729863649e-5', 'S'); - var createInverseConductanceQuantum = /* #__PURE__ */unitFactory('inverseConductanceQuantum', '12906.403729652257', 'ohm'); - var createMagneticFluxQuantum = /* #__PURE__ */unitFactory('magneticFluxQuantum', '2.0678338484619295e-15', 'Wb'); - var createNuclearMagneton = /* #__PURE__ */unitFactory('nuclearMagneton', '5.0507837461e-27', 'J T^-1'); - var createKlitzing = /* #__PURE__ */unitFactory('klitzing', '25812.807459304513', 'ohm'); - var createJosephson = /* #__PURE__ */unitFactory('josephson', '4.835978484169836e14 Hz V', 'Hz V^-1'); // TODO: support for Hz needed - // Atomic and nuclear constants - - var createBohrRadius = /* #__PURE__ */unitFactory('bohrRadius', '5.29177210903e-11', 'm'); - var createClassicalElectronRadius = /* #__PURE__ */unitFactory('classicalElectronRadius', '2.8179403262e-15', 'm'); - var createElectronMass = /* #__PURE__ */unitFactory('electronMass', '9.1093837015e-31', 'kg'); - var createFermiCoupling = /* #__PURE__ */unitFactory('fermiCoupling', '1.1663787e-5', 'GeV^-2'); - var createFineStructure = numberFactory('fineStructure', 7.2973525693e-3); - var createHartreeEnergy = /* #__PURE__ */unitFactory('hartreeEnergy', '4.3597447222071e-18', 'J'); - var createProtonMass = /* #__PURE__ */unitFactory('protonMass', '1.67262192369e-27', 'kg'); - var createDeuteronMass = /* #__PURE__ */unitFactory('deuteronMass', '3.3435830926e-27', 'kg'); - var createNeutronMass = /* #__PURE__ */unitFactory('neutronMass', '1.6749271613e-27', 'kg'); - var createQuantumOfCirculation = /* #__PURE__ */unitFactory('quantumOfCirculation', '3.6369475516e-4', 'm^2 s^-1'); - var createRydberg = /* #__PURE__ */unitFactory('rydberg', '10973731.568160', 'm^-1'); - var createThomsonCrossSection = /* #__PURE__ */unitFactory('thomsonCrossSection', '6.6524587321e-29', 'm^2'); - var createWeakMixingAngle = numberFactory('weakMixingAngle', 0.22290); - var createEfimovFactor = numberFactory('efimovFactor', 22.7); // Physico-chemical constants - - var createAtomicMass = /* #__PURE__ */unitFactory('atomicMass', '1.66053906660e-27', 'kg'); - var createAvogadro = /* #__PURE__ */unitFactory('avogadro', '6.02214076e23', 'mol^-1'); - var createBoltzmann = /* #__PURE__ */unitFactory('boltzmann', '1.380649e-23', 'J K^-1'); - var createFaraday = /* #__PURE__ */unitFactory('faraday', '96485.33212331001', 'C mol^-1'); - var createFirstRadiation = /* #__PURE__ */unitFactory('firstRadiation', '3.7417718521927573e-16', 'W m^2'); // export const createSpectralRadiance = /* #__PURE__ */ unitFactory('spectralRadiance', '1.1910429723971881e-16', 'W m^2 sr^-1') // TODO spectralRadiance - - var createLoschmidt = /* #__PURE__ */unitFactory('loschmidt', '2.686780111798444e25', 'm^-3'); - var createGasConstant = /* #__PURE__ */unitFactory('gasConstant', '8.31446261815324', 'J K^-1 mol^-1'); - var createMolarPlanckConstant = /* #__PURE__ */unitFactory('molarPlanckConstant', '3.990312712893431e-10', 'J s mol^-1'); - var createMolarVolume = /* #__PURE__ */unitFactory('molarVolume', '0.022413969545014137', 'm^3 mol^-1'); - var createSackurTetrode = numberFactory('sackurTetrode', -1.16487052358); - var createSecondRadiation = /* #__PURE__ */unitFactory('secondRadiation', '0.014387768775039337', 'm K'); - var createStefanBoltzmann = /* #__PURE__ */unitFactory('stefanBoltzmann', '5.67037441918443e-8', 'W m^-2 K^-4'); - var createWienDisplacement = /* #__PURE__ */unitFactory('wienDisplacement', '2.897771955e-3', 'm K'); // Adopted values - - var createMolarMass = /* #__PURE__ */unitFactory('molarMass', '0.99999999965e-3', 'kg mol^-1'); - var createMolarMassC12 = /* #__PURE__ */unitFactory('molarMassC12', '11.9999999958e-3', 'kg mol^-1'); - var createGravity = /* #__PURE__ */unitFactory('gravity', '9.80665', 'm s^-2'); // atm is defined in Unit.js - // Natural units - - var createPlanckLength = /* #__PURE__ */unitFactory('planckLength', '1.616255e-35', 'm'); - var createPlanckMass = /* #__PURE__ */unitFactory('planckMass', '2.176435e-8', 'kg'); - var createPlanckTime = /* #__PURE__ */unitFactory('planckTime', '5.391245e-44', 's'); - var createPlanckCharge = /* #__PURE__ */unitFactory('planckCharge', '1.87554603778e-18', 'C'); - var createPlanckTemperature = /* #__PURE__ */unitFactory('planckTemperature', '1.416785e+32', 'K'); // helper function to create a factory function which creates a physical constant, - // a Unit with either a number value or a BigNumber value depending on the configuration - - function unitFactory(name, valueStr, unitStr) { - var dependencies = ['config', 'Unit', 'BigNumber']; - return Object(factory["a" /* factory */])(name, dependencies, function (_ref) { - var config = _ref.config, - Unit = _ref.Unit, - BigNumber = _ref.BigNumber; - // Note that we can parse into number or BigNumber. - // We do not parse into Fractions as that doesn't make sense: we would lose precision of the values - // Therefore we dont use Unit.parse() - var value = config.number === 'BigNumber' ? new BigNumber(valueStr) : parseFloat(valueStr); - var unit = new Unit(value, unitStr); - unit.fixPrefix = true; - return unit; - }); - } // helper function to create a factory function which creates a numeric constant, - // either a number or BigNumber depending on the configuration - - function numberFactory(name, value) { - var dependencies = ['config', 'BigNumber']; - return Object(factory["a" /* factory */])(name, dependencies, function (_ref2) { - var config = _ref2.config, - BigNumber = _ref2.BigNumber; - return config.number === 'BigNumber' ? new BigNumber(value) : value; - }); - } - // CONCATENATED MODULE: ./src/expression/transform/apply.transform.js - - var apply_transform_name = 'apply'; - var apply_transform_dependencies = ['typed', 'isInteger']; - /** - * Attach a transform function to math.apply - * Adds a property transform containing the transform function. - * - * This transform changed the last `dim` parameter of function apply - * from one-based to zero based - */ - - var createApplyTransform = /* #__PURE__ */Object(factory["a" /* factory */])(apply_transform_name, apply_transform_dependencies, function (_ref) { - var typed = _ref.typed, - isInteger = _ref.isInteger; - var apply = createApply({ - typed: typed, - isInteger: isInteger - }); // @see: comment of concat itself - - return typed('apply', { - '...any': function any(args) { - // change dim from one-based to zero-based - var dim = args[1]; - - if (Object(is["y" /* isNumber */])(dim)) { - args[1] = dim - 1; - } else if (Object(is["e" /* isBigNumber */])(dim)) { - args[1] = dim.minus(1); - } - - try { - return apply.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/column.transform.js - - var column_transform_name = 'column'; - var column_transform_dependencies = ['typed', 'Index', 'matrix', 'range']; - /** - * Attach a transform function to matrix.column - * Adds a property transform containing the transform function. - * - * This transform changed the last `index` parameter of function column - * from zero-based to one-based - */ - - var createColumnTransform = /* #__PURE__ */Object(factory["a" /* factory */])(column_transform_name, column_transform_dependencies, function (_ref) { - var typed = _ref.typed, - Index = _ref.Index, - matrix = _ref.matrix, - range = _ref.range; - var column = createColumn({ - typed: typed, - Index: Index, - matrix: matrix, - range: range - }); // @see: comment of column itself - - return typed('column', { - '...any': function any(args) { - // change last argument from zero-based to one-based - var lastIndex = args.length - 1; - var last = args[lastIndex]; - - if (Object(is["y" /* isNumber */])(last)) { - args[lastIndex] = last - 1; - } - - try { - return column.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/utils/compileInlineExpression.js - - /** - * Compile an inline expression like "x > 0" - * @param {Node} expression - * @param {Object} math - * @param {Object} scope - * @return {function} Returns a function with one argument which fills in the - * undefined variable (like "x") and evaluates the expression - */ - - function compileInlineExpression(expression, math, scope) { - // find an undefined symbol - var symbol = expression.filter(function (node) { - return Object(is["J" /* isSymbolNode */])(node) && !(node.name in math) && !(node.name in scope); - })[0]; - - if (!symbol) { - throw new Error('No undefined variable found in inline expression "' + expression + '"'); - } // create a test function for this equation - - var name = symbol.name; // variable name - - var subScope = Object.create(scope); - var eq = expression.compile(); - return function inlineExpression(x) { - subScope[name] = x; - return eq.evaluate(subScope); - }; - } - // CONCATENATED MODULE: ./src/expression/transform/filter.transform.js - - var filter_transform_name = 'filter'; - var filter_transform_dependencies = ['typed']; - var createFilterTransform = /* #__PURE__ */Object(factory["a" /* factory */])(filter_transform_name, filter_transform_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Attach a transform function to math.filter - * Adds a property transform containing the transform function. - * - * This transform adds support for equations as test function for math.filter, - * so you can do something like 'filter([3, -2, 5], x > 0)'. - */ - function filterTransform(args, math, scope) { - var x, callback; - - if (args[0]) { - x = args[0].compile().evaluate(scope); - } - - if (args[1]) { - if (Object(is["J" /* isSymbolNode */])(args[1]) || Object(is["q" /* isFunctionAssignmentNode */])(args[1])) { - // a function pointer, like filter([3, -2, 5], myTestFunction) - callback = args[1].compile().evaluate(scope); - } else { - // an expression like filter([3, -2, 5], x > 0) - callback = compileInlineExpression(args[1], math, scope); - } - } - - return filter(x, callback); - } - - filterTransform.rawArgs = true; // one based version of function filter - - var filter = typed('filter', { - 'Array, function': _filter, - 'Matrix, function': function MatrixFunction(x, test) { - return x.create(_filter(x.toArray(), test)); - }, - 'Array, RegExp': utils_array["d" /* filterRegExp */], - 'Matrix, RegExp': function MatrixRegExp(x, test) { - return x.create(Object(utils_array["d" /* filterRegExp */])(x.toArray(), test)); - } - }); - return filterTransform; - }, { - isTransformFunction: true - }); - /** - * Filter values in a callback given a callback function - * - * !!! Passes a one-based index !!! - * - * @param {Array} x - * @param {Function} callback - * @return {Array} Returns the filtered array - * @private - */ - - function _filter(x, callback) { - // figure out what number of arguments the callback function expects - var args = maxArgumentCount(callback); - return Object(utils_array["c" /* filter */])(x, function (value, index, array) { - // invoke the callback function with the right number of arguments - if (args === 1) { - return callback(value); - } else if (args === 2) { - return callback(value, [index + 1]); - } else { - // 3 or -1 - return callback(value, [index + 1], array); - } - }); - } - // CONCATENATED MODULE: ./src/expression/transform/forEach.transform.js - - var forEach_transform_name = 'forEach'; - var forEach_transform_dependencies = ['typed']; - var createForEachTransform = /* #__PURE__ */Object(factory["a" /* factory */])(forEach_transform_name, forEach_transform_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Attach a transform function to math.forEach - * Adds a property transform containing the transform function. - * - * This transform creates a one-based index instead of a zero-based index - */ - function forEachTransform(args, math, scope) { - var x, callback; - - if (args[0]) { - x = args[0].compile().evaluate(scope); - } - - if (args[1]) { - if (Object(is["J" /* isSymbolNode */])(args[1]) || Object(is["q" /* isFunctionAssignmentNode */])(args[1])) { - // a function pointer, like forEach([3, -2, 5], myTestFunction) - callback = args[1].compile().evaluate(scope); - } else { - // an expression like forEach([3, -2, 5], x > 0 ? callback1(x) : callback2(x) ) - callback = compileInlineExpression(args[1], math, scope); - } - } - - return _forEach(x, callback); - } - - forEachTransform.rawArgs = true; // one-based version of forEach - - var _forEach = typed('forEach', { - 'Array | Matrix, function': function ArrayMatrixFunction(array, callback) { - // figure out what number of arguments the callback function expects - var args = maxArgumentCount(callback); - - var recurse = function recurse(value, index) { - if (Array.isArray(value)) { - Object(utils_array["f" /* forEach */])(value, function (child, i) { - // we create a copy of the index array and append the new index value - recurse(child, index.concat(i + 1)); // one based index, hence i+1 - }); - } else { - // invoke the callback function with the right number of arguments - if (args === 1) { - callback(value); - } else if (args === 2) { - callback(value, index); - } else { - // 3 or -1 - callback(value, index, array); - } - } - }; - - recurse(array.valueOf(), []); // pass Array - } - }); - - return forEachTransform; - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/index.transform.js - - var index_transform_name = 'index'; - var index_transform_dependencies = ['Index']; - var createIndexTransform = /* #__PURE__ */Object(factory["a" /* factory */])(index_transform_name, index_transform_dependencies, function (_ref) { - var Index = _ref.Index; - - /** - * Attach a transform function to math.index - * Adds a property transform containing the transform function. - * - * This transform creates a one-based index instead of a zero-based index - */ - return function indexTransform() { - var args = []; - - for (var i = 0, ii = arguments.length; i < ii; i++) { - var arg = arguments[i]; // change from one-based to zero based, and convert BigNumber to number - - if (Object(is["D" /* isRange */])(arg)) { - arg.start--; - arg.end -= arg.step > 0 ? 0 : 2; - } else if (arg && arg.isSet === true) { - arg = arg.map(function (v) { - return v - 1; - }); - } else if (Object(is["b" /* isArray */])(arg) || Object(is["v" /* isMatrix */])(arg)) { - arg = arg.map(function (v) { - return v - 1; - }); - } else if (Object(is["y" /* isNumber */])(arg)) { - arg--; - } else if (Object(is["e" /* isBigNumber */])(arg)) { - arg = arg.toNumber() - 1; - } else if (typeof arg === 'string') {// leave as is - } else { - throw new TypeError('Dimension must be an Array, Matrix, number, string, or Range'); - } - - args[i] = arg; - } - - var res = new Index(); - Index.apply(res, args); - return res; - }; - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/map.transform.js - - var map_transform_name = 'map'; - var map_transform_dependencies = ['typed']; - var createMapTransform = /* #__PURE__ */Object(factory["a" /* factory */])(map_transform_name, map_transform_dependencies, function (_ref) { - var typed = _ref.typed; - - /** - * Attach a transform function to math.map - * Adds a property transform containing the transform function. - * - * This transform creates a one-based index instead of a zero-based index - */ - function mapTransform(args, math, scope) { - var x, callback; - - if (args[0]) { - x = args[0].compile().evaluate(scope); - } - - if (args[1]) { - if (Object(is["J" /* isSymbolNode */])(args[1]) || Object(is["q" /* isFunctionAssignmentNode */])(args[1])) { - // a function pointer, like filter([3, -2, 5], myTestFunction) - callback = args[1].compile().evaluate(scope); - } else { - // an expression like filter([3, -2, 5], x > 0) - callback = compileInlineExpression(args[1], math, scope); - } - } - - return map(x, callback); - } - - mapTransform.rawArgs = true; // one-based version of map function - - var map = typed('map', { - 'Array, function': function ArrayFunction(x, callback) { - return map_transform_map(x, callback, x); - }, - 'Matrix, function': function MatrixFunction(x, callback) { - return x.create(map_transform_map(x.valueOf(), callback, x)); - } - }); - return mapTransform; - }, { - isTransformFunction: true - }); - /** - * Map for a multi dimensional array. One-based indexes - * @param {Array} array - * @param {function} callback - * @param {Array} orig - * @return {Array} - * @private - */ - - function map_transform_map(array, callback, orig) { - // figure out what number of arguments the callback function expects - var argsCount = maxArgumentCount(callback); - - function recurse(value, index) { - if (Array.isArray(value)) { - return Object(utils_array["k" /* map */])(value, function (child, i) { - // we create a copy of the index array and append the new index value - return recurse(child, index.concat(i + 1)); // one based index, hence i + 1 - }); - } else { - // invoke the (typed) callback function with the right number of arguments - if (argsCount === 1) { - return callback(value); - } else if (argsCount === 2) { - return callback(value, index); - } else { - // 3 or -1 - return callback(value, index, orig); - } - } - } - - return recurse(array, []); - } - // CONCATENATED MODULE: ./src/expression/transform/max.transform.js - - var max_transform_name = 'max'; - var max_transform_dependencies = ['typed', 'config', 'numeric', 'larger']; - var createMaxTransform = /* #__PURE__ */Object(factory["a" /* factory */])(max_transform_name, max_transform_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - numeric = _ref.numeric, - larger = _ref.larger; - var max = createMax({ - typed: typed, - config: config, - numeric: numeric, - larger: larger - }); - /** - * Attach a transform function to math.max - * Adds a property transform containing the transform function. - * - * This transform changed the last `dim` parameter of function max - * from one-based to zero based - */ - - return typed('max', { - '...any': function any(args) { - // change last argument dim from one-based to zero-based - if (args.length === 2 && Object(is["i" /* isCollection */])(args[0])) { - var dim = args[1]; - - if (Object(is["y" /* isNumber */])(dim)) { - args[1] = dim - 1; - } else if (Object(is["e" /* isBigNumber */])(dim)) { - args[1] = dim.minus(1); - } - } - - try { - return max.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/mean.transform.js - - var mean_transform_name = 'mean'; - var mean_transform_dependencies = ['typed', 'add', 'divide']; - var createMeanTransform = /* #__PURE__ */Object(factory["a" /* factory */])(mean_transform_name, mean_transform_dependencies, function (_ref) { - var typed = _ref.typed, - add = _ref.add, - divide = _ref.divide; - var mean = createMean({ - typed: typed, - add: add, - divide: divide - }); - /** - * Attach a transform function to math.mean - * Adds a property transform containing the transform function. - * - * This transform changed the last `dim` parameter of function mean - * from one-based to zero based - */ - - return typed('mean', { - '...any': function any(args) { - // change last argument dim from one-based to zero-based - if (args.length === 2 && Object(is["i" /* isCollection */])(args[0])) { - var dim = args[1]; - - if (Object(is["y" /* isNumber */])(dim)) { - args[1] = dim - 1; - } else if (Object(is["e" /* isBigNumber */])(dim)) { - args[1] = dim.minus(1); - } - } - - try { - return mean.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/min.transform.js - - var min_transform_name = 'min'; - var min_transform_dependencies = ['typed', 'config', 'numeric', 'smaller']; - var createMinTransform = /* #__PURE__ */Object(factory["a" /* factory */])(min_transform_name, min_transform_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - numeric = _ref.numeric, - smaller = _ref.smaller; - var min = createMin({ - typed: typed, - config: config, - numeric: numeric, - smaller: smaller - }); - /** - * Attach a transform function to math.min - * Adds a property transform containing the transform function. - * - * This transform changed the last `dim` parameter of function min - * from one-based to zero based - */ - - return typed('min', { - '...any': function any(args) { - // change last argument dim from one-based to zero-based - if (args.length === 2 && Object(is["i" /* isCollection */])(args[0])) { - var dim = args[1]; - - if (Object(is["y" /* isNumber */])(dim)) { - args[1] = dim - 1; - } else if (Object(is["e" /* isBigNumber */])(dim)) { - args[1] = dim.minus(1); - } - } - - try { - return min.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/range.transform.js - - var range_transform_name = 'range'; - var range_transform_dependencies = ['typed', 'config', '?matrix', '?bignumber', 'smaller', 'smallerEq', 'larger', 'largerEq']; - var createRangeTransform = /* #__PURE__ */Object(factory["a" /* factory */])(range_transform_name, range_transform_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - matrix = _ref.matrix, - bignumber = _ref.bignumber, - smaller = _ref.smaller, - smallerEq = _ref.smallerEq, - larger = _ref.larger, - largerEq = _ref.largerEq; - var range = range_createRange({ - typed: typed, - config: config, - matrix: matrix, - bignumber: bignumber, - smaller: smaller, - smallerEq: smallerEq, - larger: larger, - largerEq: largerEq - }); - /** - * Attach a transform function to math.range - * Adds a property transform containing the transform function. - * - * This transform creates a range which includes the end value - */ - - return typed('range', { - '...any': function any(args) { - var lastIndex = args.length - 1; - var last = args[lastIndex]; - - if (typeof last !== 'boolean') { - // append a parameter includeEnd=true - args.push(true); - } - - return range.apply(null, args); - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/row.transform.js - - var row_transform_name = 'row'; - var row_transform_dependencies = ['typed', 'Index', 'matrix', 'range']; - /** - * Attach a transform function to matrix.column - * Adds a property transform containing the transform function. - * - * This transform changed the last `index` parameter of function column - * from zero-based to one-based - */ - - var createRowTransform = /* #__PURE__ */Object(factory["a" /* factory */])(row_transform_name, row_transform_dependencies, function (_ref) { - var typed = _ref.typed, - Index = _ref.Index, - matrix = _ref.matrix, - range = _ref.range; - var row = createRow({ - typed: typed, - Index: Index, - matrix: matrix, - range: range - }); // @see: comment of row itself - - return typed('row', { - '...any': function any(args) { - // change last argument from zero-based to one-based - var lastIndex = args.length - 1; - var last = args[lastIndex]; - - if (Object(is["y" /* isNumber */])(last)) { - args[lastIndex] = last - 1; - } - - try { - return row.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/subset.transform.js - - var subset_transform_name = 'subset'; - var subset_transform_dependencies = ['typed', 'matrix']; - var createSubsetTransform = /* #__PURE__ */Object(factory["a" /* factory */])(subset_transform_name, subset_transform_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix; - var subset = createSubset({ - typed: typed, - matrix: matrix - }); - /** - * Attach a transform function to math.subset - * Adds a property transform containing the transform function. - * - * This transform creates a range which includes the end value - */ - - return typed('subset', { - '...any': function any(args) { - try { - return subset.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/concat.transform.js - - var concat_transform_name = 'concat'; - var concat_transform_dependencies = ['typed', 'matrix', 'isInteger']; - var createConcatTransform = /* #__PURE__ */Object(factory["a" /* factory */])(concat_transform_name, concat_transform_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - isInteger = _ref.isInteger; - var concat = createConcat({ - typed: typed, - matrix: matrix, - isInteger: isInteger - }); - /** - * Attach a transform function to math.range - * Adds a property transform containing the transform function. - * - * This transform changed the last `dim` parameter of function concat - * from one-based to zero based - */ - - return typed('concat', { - '...any': function any(args) { - // change last argument from one-based to zero-based - var lastIndex = args.length - 1; - var last = args[lastIndex]; - - if (Object(is["y" /* isNumber */])(last)) { - args[lastIndex] = last - 1; - } else if (Object(is["e" /* isBigNumber */])(last)) { - args[lastIndex] = last.minus(1); - } - - try { - return concat.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/diff.transform.js - - var diff_transform_name = 'diff'; - var diff_transform_dependencies = ['typed', 'matrix', 'subtract', 'number', 'bignumber']; - var createDiffTransform = /* #__PURE__ */Object(factory["a" /* factory */])(diff_transform_name, diff_transform_dependencies, function (_ref) { - var typed = _ref.typed, - matrix = _ref.matrix, - subtract = _ref.subtract, - number = _ref.number, - bignumber = _ref.bignumber; - var diff = createDiff({ - typed: typed, - matrix: matrix, - subtract: subtract, - number: number, - bignumber: bignumber - }); - /** - * Attach a transform function to math.diff - * Adds a property transform containing the transform function. - * - * This transform creates a range which includes the end value - */ - - return typed(diff_transform_name, { - '...any': function any(args) { - // change last argument dim from one-based to zero-based - if (args.length === 2 && Object(is["i" /* isCollection */])(args[0])) { - var dim = args[1]; - - if (Object(is["y" /* isNumber */])(dim)) { - args[1] = dim - 1; - } else if (Object(is["e" /* isBigNumber */])(dim)) { - args[1] = dim.minus(1); - } - } - - try { - return diff.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/std.transform.js - - var std_transform_name = 'std'; - var std_transform_dependencies = ['typed', 'sqrt', 'variance']; - /** - * Attach a transform function to math.std - * Adds a property transform containing the transform function. - * - * This transform changed the `dim` parameter of function std - * from one-based to zero based - */ - - var createStdTransform = /* #__PURE__ */Object(factory["a" /* factory */])(std_transform_name, std_transform_dependencies, function (_ref) { - var typed = _ref.typed, - sqrt = _ref.sqrt, - variance = _ref.variance; - var std = createStd({ - typed: typed, - sqrt: sqrt, - variance: variance - }); - return typed('std', { - '...any': function any(args) { - // change last argument dim from one-based to zero-based - if (args.length >= 2 && Object(is["i" /* isCollection */])(args[0])) { - var dim = args[1]; - - if (Object(is["y" /* isNumber */])(dim)) { - args[1] = dim - 1; - } else if (Object(is["e" /* isBigNumber */])(dim)) { - args[1] = dim.minus(1); - } - } - - try { - return std.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/sum.transform.js - - /** - * Attach a transform function to math.sum - * Adds a property transform containing the transform function. - * - * This transform changed the last `dim` parameter of function mean - * from one-based to zero based - */ - - var sum_transform_name = 'sum'; - var sum_transform_dependencies = ['typed', 'config', 'add', 'numeric']; - var createSumTransform = /* #__PURE__ */Object(factory["a" /* factory */])(sum_transform_name, sum_transform_dependencies, function (_ref) { - var typed = _ref.typed, - config = _ref.config, - add = _ref.add, - numeric = _ref.numeric; - var sum = createSum({ - typed: typed, - config: config, - add: add, - numeric: numeric - }); - return typed(sum_transform_name, { - '...any': function any(args) { - // change last argument dim from one-based to zero-based - if (args.length === 2 && Object(is["i" /* isCollection */])(args[0])) { - var dim = args[1]; - - if (Object(is["y" /* isNumber */])(dim)) { - args[1] = dim - 1; - } else if (Object(is["e" /* isBigNumber */])(dim)) { - args[1] = dim.minus(1); - } - } - - try { - return sum.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/expression/transform/variance.transform.js - - var variance_transform_name = 'variance'; - var variance_transform_dependencies = ['typed', 'add', 'subtract', 'multiply', 'divide', 'apply', 'isNaN']; - /** - * Attach a transform function to math.var - * Adds a property transform containing the transform function. - * - * This transform changed the `dim` parameter of function var - * from one-based to zero based - */ - - var createVarianceTransform = /* #__PURE__ */Object(factory["a" /* factory */])(variance_transform_name, variance_transform_dependencies, function (_ref) { - var typed = _ref.typed, - add = _ref.add, - subtract = _ref.subtract, - multiply = _ref.multiply, - divide = _ref.divide, - apply = _ref.apply, - isNaN = _ref.isNaN; - var variance = createVariance({ - typed: typed, - add: add, - subtract: subtract, - multiply: multiply, - divide: divide, - apply: apply, - isNaN: isNaN - }); - return typed(variance_transform_name, { - '...any': function any(args) { - // change last argument dim from one-based to zero-based - if (args.length >= 2 && Object(is["i" /* isCollection */])(args[0])) { - var dim = args[1]; - - if (Object(is["y" /* isNumber */])(dim)) { - args[1] = dim - 1; - } else if (Object(is["e" /* isBigNumber */])(dim)) { - args[1] = dim.minus(1); - } - } - - try { - return variance.apply(null, args); - } catch (err) { - throw errorTransform(err); - } - } - }); - }, { - isTransformFunction: true - }); - // CONCATENATED MODULE: ./src/factoriesAny.js - - /***/ }), - /* 21 */ - /***/ (function (module, __webpack_exports__, __webpack_require__) { - - "use strict"; - // ESM COMPAT FLAG - __webpack_require__.r(__webpack_exports__); - - // EXPORTS - __webpack_require__.d(__webpack_exports__, "create", function () { - return /* binding */ create; - }); - - // EXTERNAL MODULE: ./src/utils/object.js - var utils_object = __webpack_require__(3); - - // EXTERNAL MODULE: ./node_modules/tiny-emitter/index.js - var tiny_emitter = __webpack_require__(17); - var tiny_emitter_default = /*#__PURE__*/__webpack_require__.n(tiny_emitter); - - // CONCATENATED MODULE: ./src/utils/emitter.js - - /** - * Extend given object with emitter functions `on`, `off`, `once`, `emit` - * @param {Object} obj - * @return {Object} obj - */ - - function mixin(obj) { - // create event emitter - var emitter = new tiny_emitter_default.a(); // bind methods to obj (we don't want to expose the emitter.e Array...) - - obj.on = emitter.on.bind(emitter); - obj.off = emitter.off.bind(emitter); - obj.once = emitter.once.bind(emitter); - obj.emit = emitter.emit.bind(emitter); - return obj; - } - // EXTERNAL MODULE: ./src/utils/is.js - var is = __webpack_require__(1); - - // EXTERNAL MODULE: ./src/utils/factory.js - var utils_factory = __webpack_require__(0); - - // EXTERNAL MODULE: ./src/utils/array.js - var array = __webpack_require__(2); - - // EXTERNAL MODULE: ./src/error/ArgumentsError.js - var ArgumentsError = __webpack_require__(13); - - // CONCATENATED MODULE: ./src/core/function/import.js - function _defineProperty(obj, key, value) { - if (key in obj) { - Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); - } else { - obj[key] = value; - } return obj; - } - - function _typeof(obj) { - "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - _typeof = function _typeof(obj) { - return typeof obj; - }; - } else { - _typeof = function _typeof(obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } return _typeof(obj); - } - - function importFactory(typed, load, math, importedFactories) { - /** - * Import functions from an object or a module. - * - * This function is only available on a mathjs instance created using `create`. - * - * Syntax: - * - * math.import(functions) - * math.import(functions, options) - * - * Where: - * - * - `functions: Object` - * An object with functions or factories to be imported. - * - `options: Object` An object with import options. Available options: - * - `override: boolean` - * If true, existing functions will be overwritten. False by default. - * - `silent: boolean` - * If true, the function will not throw errors on duplicates or invalid - * types. False by default. - * - `wrap: boolean` - * If true, the functions will be wrapped in a wrapper function - * which converts data types like Matrix to primitive data types like Array. - * The wrapper is needed when extending math.js with libraries which do not - * support these data type. False by default. - * - * Examples: - * - * import { create, all } from 'mathjs' - * import * as numbers from 'numbers' - * - * // create a mathjs instance - * const math = create(all) - * - * // define new functions and variables - * math.import({ - * myvalue: 42, - * hello: function (name) { - * return 'hello, ' + name + '!' - * } - * }) - * - * // use the imported function and variable - * math.myvalue * 2 // 84 - * math.hello('user') // 'hello, user!' - * - * // import the npm module 'numbers' - * // (must be installed first with `npm install numbers`) - * math.import(numbers, {wrap: true}) - * - * math.fibonacci(7) // returns 13 - * - * @param {Object | Array} functions Object with functions to be imported. - * @param {Object} [options] Import options. - */ - function mathImport(functions, options) { - var num = arguments.length; - - if (num !== 1 && num !== 2) { - throw new ArgumentsError["a" /* ArgumentsError */]('import', num, 1, 2); - } - - if (!options) { - options = {}; - } - - function flattenImports(flatValues, value, name) { - if (Array.isArray(value)) { - value.forEach(function (item) { - return flattenImports(flatValues, item); - }); - } else if (_typeof(value) === 'object') { - for (var _name in value) { - if (Object(utils_object["f" /* hasOwnProperty */])(value, _name)) { - flattenImports(flatValues, value[_name], _name); - } - } - } else if (Object(utils_factory["b" /* isFactory */])(value) || name !== undefined) { - var flatName = Object(utils_factory["b" /* isFactory */])(value) ? isTransformFunctionFactory(value) ? value.fn + '.transform' : // TODO: this is ugly - value.fn : name; // we allow importing the same function twice if it points to the same implementation - - if (Object(utils_object["f" /* hasOwnProperty */])(flatValues, flatName) && flatValues[flatName] !== value && !options.silent) { - throw new Error('Cannot import "' + flatName + '" twice'); - } - - flatValues[flatName] = value; - } else { - if (!options.silent) { - throw new TypeError('Factory, Object, or Array expected'); - } - } - } - - var flatValues = {}; - flattenImports(flatValues, functions); - - for (var name in flatValues) { - if (Object(utils_object["f" /* hasOwnProperty */])(flatValues, name)) { - // console.log('import', name) - var value = flatValues[name]; - - if (Object(utils_factory["b" /* isFactory */])(value)) { - // we ignore name here and enforce the name of the factory - // maybe at some point we do want to allow overriding it - // in that case we can implement an option overrideFactoryNames: true - _importFactory(value, options); - } else if (isSupportedType(value)) { - _import(name, value, options); - } else { - if (!options.silent) { - throw new TypeError('Factory, Object, or Array expected'); - } - } - } - } - } - /** - * Add a property to the math namespace - * @param {string} name - * @param {*} value - * @param {Object} options See import for a description of the options - * @private - */ - - function _import(name, value, options) { - // TODO: refactor this function, it's to complicated and contains duplicate code - if (options.wrap && typeof value === 'function') { - // create a wrapper around the function - value = _wrap(value); - } // turn a plain function with a typed-function signature into a typed-function - - if (hasTypedFunctionSignature(value)) { - value = typed(name, _defineProperty({}, value.signature, value)); - } - - if (isTypedFunction(math[name]) && isTypedFunction(value)) { - if (options.override) { - // give the typed function the right name - value = typed(name, value.signatures); - } else { - // merge the existing and typed function - value = typed(math[name], value); - } - - math[name] = value; - delete importedFactories[name]; - - _importTransform(name, value); - - math.emit('import', name, function resolver() { - return value; - }); - return; - } - - if (math[name] === undefined || options.override) { - math[name] = value; - delete importedFactories[name]; - - _importTransform(name, value); - - math.emit('import', name, function resolver() { - return value; - }); - return; - } - - if (!options.silent) { - throw new Error('Cannot import "' + name + '": already exists'); - } - } - - function _importTransform(name, value) { - if (value && typeof value.transform === 'function') { - math.expression.transform[name] = value.transform; - - if (allowedInExpressions(name)) { - math.expression.mathWithTransform[name] = value.transform; - } - } else { - // remove existing transform - delete math.expression.transform[name]; - - if (allowedInExpressions(name)) { - math.expression.mathWithTransform[name] = value; - } - } - } - - function _deleteTransform(name) { - delete math.expression.transform[name]; - - if (allowedInExpressions(name)) { - math.expression.mathWithTransform[name] = math[name]; - } else { - delete math.expression.mathWithTransform[name]; - } - } - /** - * Create a wrapper a round an function which converts the arguments - * to their primitive values (like convert a Matrix to Array) - * @param {Function} fn - * @return {Function} Returns the wrapped function - * @private - */ - - function _wrap(fn) { - var wrapper = function wrapper() { - var args = []; - - for (var i = 0, len = arguments.length; i < len; i++) { - var arg = arguments[i]; - args[i] = arg && arg.valueOf(); - } - - return fn.apply(math, args); - }; - - if (fn.transform) { - wrapper.transform = fn.transform; - } - - return wrapper; - } - /** - * Import an instance of a factory into math.js - * @param {function(scope: object)} factory - * @param {Object} options See import for a description of the options - * @param {string} [name=factory.name] Optional custom name - * @private - */ - - function _importFactory(factory, options) { - var name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : factory.fn; - - if (Object(array["b" /* contains */])(name, '.')) { - throw new Error('Factory name should not contain a nested path. ' + 'Name: ' + JSON.stringify(name)); - } - - var namespace = isTransformFunctionFactory(factory) ? math.expression.transform : math; - var existingTransform = (name in math.expression.transform); - var existing = Object(utils_object["f" /* hasOwnProperty */])(namespace, name) ? namespace[name] : undefined; - - var resolver = function resolver() { - // collect all dependencies, handle finding both functions and classes and other special cases - var dependencies = {}; - factory.dependencies.map(utils_factory["c" /* stripOptionalNotation */]).forEach(function (dependency) { - if (Object(array["b" /* contains */])(dependency, '.')) { - throw new Error('Factory dependency should not contain a nested path. ' + 'Name: ' + JSON.stringify(dependency)); - } - - if (dependency === 'math') { - dependencies.math = math; - } else if (dependency === 'mathWithTransform') { - dependencies.mathWithTransform = math.expression.mathWithTransform; - } else if (dependency === 'classes') { - // special case for json reviver - dependencies.classes = math; - } else { - dependencies[dependency] = math[dependency]; - } - }); - var instance = /* #__PURE__ */factory(dependencies); - - if (instance && typeof instance.transform === 'function') { - throw new Error('Transforms cannot be attached to factory functions. ' + 'Please create a separate function for it with exports.path="expression.transform"'); - } - - if (existing === undefined || options.override) { - return instance; - } - - if (isTypedFunction(existing) && isTypedFunction(instance)) { - // merge the existing and new typed function - return typed(existing, instance); - } - - if (options.silent) { - // keep existing, ignore imported function - return existing; - } else { - throw new Error('Cannot import "' + name + '": already exists'); - } - }; // TODO: add unit test with non-lazy factory - - if (!factory.meta || factory.meta.lazy !== false) { - Object(utils_object["h" /* lazy */])(namespace, name, resolver); // FIXME: remove the `if (existing &&` condition again. Can we make sure subset is loaded before subset.transform? (Name collision, and no dependencies between the two) - - if (existing && existingTransform) { - _deleteTransform(name); - } else { - if (isTransformFunctionFactory(factory) || factoryAllowedInExpressions(factory)) { - Object(utils_object["h" /* lazy */])(math.expression.mathWithTransform, name, function () { - return namespace[name]; - }); - } - } - } else { - namespace[name] = resolver(); // FIXME: remove the `if (existing &&` condition again. Can we make sure subset is loaded before subset.transform? (Name collision, and no dependencies between the two) - - if (existing && existingTransform) { - _deleteTransform(name); - } else { - if (isTransformFunctionFactory(factory) || factoryAllowedInExpressions(factory)) { - Object(utils_object["h" /* lazy */])(math.expression.mathWithTransform, name, function () { - return namespace[name]; - }); - } - } - } // TODO: improve factories, store a list with imports instead which can be re-played - - importedFactories[name] = factory; - math.emit('import', name, resolver); - } - /** - * Check whether given object is a type which can be imported - * @param {Function | number | string | boolean | null | Unit | Complex} object - * @return {boolean} - * @private - */ - - function isSupportedType(object) { - return typeof object === 'function' || typeof object === 'number' || typeof object === 'string' || typeof object === 'boolean' || object === null || Object(is["L" /* isUnit */])(object) || Object(is["j" /* isComplex */])(object) || Object(is["e" /* isBigNumber */])(object) || Object(is["o" /* isFraction */])(object) || Object(is["v" /* isMatrix */])(object) || Array.isArray(object); - } - /** - * Test whether a given thing is a typed-function - * @param {*} fn - * @return {boolean} Returns true when `fn` is a typed-function - */ - - function isTypedFunction(fn) { - return typeof fn === 'function' && _typeof(fn.signatures) === 'object'; - } - - function hasTypedFunctionSignature(fn) { - return typeof fn === 'function' && typeof fn.signature === 'string'; - } - - function allowedInExpressions(name) { - return !Object(utils_object["f" /* hasOwnProperty */])(unsafe, name); - } - - function factoryAllowedInExpressions(factory) { - return factory.fn.indexOf('.') === -1 && // FIXME: make checking on path redundant, check on meta data instead - !Object(utils_object["f" /* hasOwnProperty */])(unsafe, factory.fn) && (!factory.meta || !factory.meta.isClass); - } - - function isTransformFunctionFactory(factory) { - return factory !== undefined && factory.meta !== undefined && factory.meta.isTransformFunction === true || false; - } // namespaces and functions not available in the parser for safety reasons - - var unsafe = { - expression: true, - type: true, - docs: true, - error: true, - json: true, - chain: true // chain method not supported. Note that there is a unit chain too. - - }; - return mathImport; - } - // CONCATENATED MODULE: ./src/core/config.js - var DEFAULT_CONFIG = { - // minimum relative difference between two compared values, - // used by all comparison functions - epsilon: 1e-12, - // type of default matrix output. Choose 'matrix' (default) or 'array' - matrix: 'Matrix', - // type of default number output. Choose 'number' (default) 'BigNumber', or 'Fraction - number: 'number', - // number of significant digits in BigNumbers - precision: 64, - // predictable output type of functions. When true, output type depends only - // on the input types. When false (default), output type can vary depending - // on input values. For example `math.sqrt(-4)` returns `complex('2i')` when - // predictable is false, and returns `NaN` when true. - predictable: false, - // random seed for seeded pseudo random number generation - // null = randomly seed - randomSeed: null - }; - // CONCATENATED MODULE: ./src/core/function/config.js - - var MATRIX_OPTIONS = ['Matrix', 'Array']; // valid values for option matrix - - var NUMBER_OPTIONS = ['number', 'BigNumber', 'Fraction']; // valid values for option number - - function configFactory(config, emit) { - /** - * Set configuration options for math.js, and get current options. - * Will emit a 'config' event, with arguments (curr, prev, changes). - * - * This function is only available on a mathjs instance created using `create`. - * - * Syntax: - * - * math.config(config: Object): Object - * - * Examples: - * - * - * import { create, all } from 'mathjs' - * - * // create a mathjs instance - * const math = create(all) - * - * math.config().number // outputs 'number' - * math.evaluate('0.4') // outputs number 0.4 - * math.config({number: 'Fraction'}) - * math.evaluate('0.4') // outputs Fraction 2/5 - * - * @param {Object} [options] Available options: - * {number} epsilon - * Minimum relative difference between two - * compared values, used by all comparison functions. - * {string} matrix - * A string 'Matrix' (default) or 'Array'. - * {string} number - * A string 'number' (default), 'BigNumber', or 'Fraction' - * {number} precision - * The number of significant digits for BigNumbers. - * Not applicable for Numbers. - * {string} parenthesis - * How to display parentheses in LaTeX and string - * output. - * {string} randomSeed - * Random seed for seeded pseudo random number generator. - * Set to null to randomly seed. - * @return {Object} Returns the current configuration - */ - function _config(options) { - if (options) { - var prev = Object(utils_object["i" /* mapObject */])(config, utils_object["a" /* clone */]); // validate some of the options - - validateOption(options, 'matrix', MATRIX_OPTIONS); - validateOption(options, 'number', NUMBER_OPTIONS); // merge options - - Object(utils_object["b" /* deepExtend */])(config, options); - var curr = Object(utils_object["i" /* mapObject */])(config, utils_object["a" /* clone */]); - var changes = Object(utils_object["i" /* mapObject */])(options, utils_object["a" /* clone */]); // emit 'config' event - - emit('config', curr, prev, changes); - return curr; - } else { - return Object(utils_object["i" /* mapObject */])(config, utils_object["a" /* clone */]); - } - } // attach the valid options to the function so they can be extended - - _config.MATRIX_OPTIONS = MATRIX_OPTIONS; - _config.NUMBER_OPTIONS = NUMBER_OPTIONS; // attach the config properties as readonly properties to the config function - - Object.keys(DEFAULT_CONFIG).forEach(function (key) { - Object.defineProperty(_config, key, { - get: function get() { - return config[key]; - }, - enumerable: true, - configurable: true - }); - }); - return _config; - } - /** - * Test whether an Array contains a specific item. - * @param {Array.} array - * @param {string} item - * @return {boolean} - */ - - function contains(array, item) { - return array.indexOf(item) !== -1; - } - /** - * Validate an option - * @param {Object} options Object with options - * @param {string} name Name of the option to validate - * @param {Array.} values Array with valid values for this option - */ - - function validateOption(options, name, values) { - if (options[name] !== undefined && !contains(values, options[name])) { - // unknown value - console.warn('Warning: Unknown value "' + options[name] + '" for configuration option "' + name + '". ' + 'Available options: ' + values.map(function (value) { - return JSON.stringify(value); - }).join(', ') + '.'); - } - } - // EXTERNAL MODULE: ./src/error/DimensionError.js - var DimensionError = __webpack_require__(6); - - // EXTERNAL MODULE: ./src/error/IndexError.js - var IndexError = __webpack_require__(9); - - // CONCATENATED MODULE: ./src/core/create.js - function _extends() { - _extends = Object.assign || function (target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i]; for (var key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } return target; - }; return _extends.apply(this, arguments); - } - - /** - * Create a mathjs instance from given factory functions and optionally config - * - * Usage: - * - * const mathjs1 = create({ createAdd, createMultiply, ...}) - * const config = { number: 'BigNumber' } - * const mathjs2 = create(all, config) - * - * @param {Object} [factories] An object with factory functions - * The object can contain nested objects, - * all nested objects will be flattened. - * @param {Object} [config] Available options: - * {number} epsilon - * Minimum relative difference between two - * compared values, used by all comparison functions. - * {string} matrix - * A string 'Matrix' (default) or 'Array'. - * {string} number - * A string 'number' (default), 'BigNumber', or 'Fraction' - * {number} precision - * The number of significant digits for BigNumbers. - * Not applicable for Numbers. - * {boolean} predictable - * Predictable output type of functions. When true, - * output type depends only on the input types. When - * false (default), output type can vary depending - * on input values. For example `math.sqrt(-4)` - * returns `complex('2i')` when predictable is false, and - * returns `NaN` when true. - * {string} randomSeed - * Random seed for seeded pseudo random number generator. - * Set to null to randomly seed. - * @returns {Object} Returns a bare-bone math.js instance containing - * functions: - * - `import` to add new functions - * - `config` to change configuration - * - `on`, `off`, `once`, `emit` for events - */ - - function create(factories, config) { - var configInternal = _extends({}, DEFAULT_CONFIG, config); // simple test for ES5 support - - if (typeof Object.create !== 'function') { - throw new Error('ES5 not supported by this JavaScript engine. ' + 'Please load the es5-shim and es5-sham library for compatibility.'); - } // create the mathjs instance - - var math = mixin({ - // only here for backward compatibility for legacy factory functions - isNumber: is["y" /* isNumber */], - isComplex: is["j" /* isComplex */], - isBigNumber: is["e" /* isBigNumber */], - isFraction: is["o" /* isFraction */], - isUnit: is["L" /* isUnit */], - isString: is["I" /* isString */], - isArray: is["b" /* isArray */], - isMatrix: is["v" /* isMatrix */], - isCollection: is["i" /* isCollection */], - isDenseMatrix: is["n" /* isDenseMatrix */], - isSparseMatrix: is["H" /* isSparseMatrix */], - isRange: is["D" /* isRange */], - isIndex: is["t" /* isIndex */], - isBoolean: is["g" /* isBoolean */], - isResultSet: is["G" /* isResultSet */], - isHelp: is["s" /* isHelp */], - isFunction: is["p" /* isFunction */], - isDate: is["m" /* isDate */], - isRegExp: is["F" /* isRegExp */], - isObject: is["z" /* isObject */], - isNull: is["x" /* isNull */], - isUndefined: is["K" /* isUndefined */], - isAccessorNode: is["a" /* isAccessorNode */], - isArrayNode: is["c" /* isArrayNode */], - isAssignmentNode: is["d" /* isAssignmentNode */], - isBlockNode: is["f" /* isBlockNode */], - isConditionalNode: is["k" /* isConditionalNode */], - isConstantNode: is["l" /* isConstantNode */], - isFunctionAssignmentNode: is["q" /* isFunctionAssignmentNode */], - isFunctionNode: is["r" /* isFunctionNode */], - isIndexNode: is["u" /* isIndexNode */], - isNode: is["w" /* isNode */], - isObjectNode: is["A" /* isObjectNode */], - isOperatorNode: is["B" /* isOperatorNode */], - isParenthesisNode: is["C" /* isParenthesisNode */], - isRangeNode: is["E" /* isRangeNode */], - isSymbolNode: is["J" /* isSymbolNode */], - isChain: is["h" /* isChain */] - }); // load config function and apply provided config - - math.config = configFactory(configInternal, math.emit); - math.expression = { - transform: {}, - mathWithTransform: { - config: math.config - } - }; // cached factories and instances used by function load - - var legacyFactories = []; - var legacyInstances = []; - /** - * Load a function or data type from a factory. - * If the function or data type already exists, the existing instance is - * returned. - * @param {Function} factory - * @returns {*} - */ - - function load(factory) { - if (Object(utils_factory["b" /* isFactory */])(factory)) { - return factory(math); - } - - var firstProperty = factory[Object.keys(factory)[0]]; - - if (Object(utils_factory["b" /* isFactory */])(firstProperty)) { - return firstProperty(math); - } - - if (!Object(utils_object["g" /* isLegacyFactory */])(factory)) { - console.warn('Factory object with properties `type`, `name`, and `factory` expected', factory); - throw new Error('Factory object with properties `type`, `name`, and `factory` expected'); - } - - var index = legacyFactories.indexOf(factory); - var instance; - - if (index === -1) { - // doesn't yet exist - if (factory.math === true) { - // pass with math namespace - instance = factory.factory(math.type, configInternal, load, math.typed, math); - } else { - instance = factory.factory(math.type, configInternal, load, math.typed); - } // append to the cache - - legacyFactories.push(factory); - legacyInstances.push(instance); - } else { - // already existing function, return the cached instance - instance = legacyInstances[index]; - } - - return instance; - } - - var importedFactories = {}; // load the import function - - function lazyTyped() { - for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { - args[_key] = arguments[_key]; - } - - return math.typed.apply(math.typed, args); - } - - var internalImport = importFactory(lazyTyped, load, math, importedFactories); - math["import"] = internalImport; // listen for changes in config, import all functions again when changed - // TODO: move this listener into the import function? - - math.on('config', function () { - Object(utils_object["k" /* values */])(importedFactories).forEach(function (factory) { - if (factory && factory.meta && factory.meta.recreateOnConfigChange) { - // FIXME: only re-create when the current instance is the same as was initially created - // FIXME: delete the functions/constants before importing them again? - internalImport(factory, { - override: true - }); - } - }); - }); // the create function exposed on the mathjs instance is bound to - // the factory functions passed before - - math.create = create.bind(null, factories); // export factory function - - math.factory = utils_factory["a" /* factory */]; // import the factory functions like createAdd as an array instead of object, - // else they will get a different naming (`createAdd` instead of `add`). - - math["import"](Object(utils_object["k" /* values */])(Object(utils_object["c" /* deepFlatten */])(factories))); - math.ArgumentsError = ArgumentsError["a" /* ArgumentsError */]; - math.DimensionError = DimensionError["a" /* DimensionError */]; - math.IndexError = IndexError["a" /* IndexError */]; - return math; - } - - /***/ }) - /******/ ]); -}); diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXDataStore.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXDataStore.js index 3dc7b61f531..4b018c3ef03 100644 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXDataStore.js +++ b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXDataStore.js @@ -1,6 +1,6 @@ Include.addScript("/JS/dataStorage.js"); -// TODO use the ts version in src/shared +// FIXME delete. Last user is fbw-a32nx\src\base\flybywire-aircraft-a320-neo\html_ui\Pages\VLivery\Liveries\A32NX_Registration\Registration.js class NXDataStore { /* private */ static get listener() { if (NXDataStore._listener === undefined) { diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXFMGCFlightPhases.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXFMGCFlightPhases.js deleted file mode 100644 index 1d61f0cb6ae..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXFMGCFlightPhases.js +++ /dev/null @@ -1,10 +0,0 @@ -const FmgcFlightPhases = { - PREFLIGHT: 0, - TAKEOFF: 1, - CLIMB: 2, - CRUISE: 3, - DESCENT: 4, - APPROACH: 5, - GOAROUND : 6, - DONE: 7 -}; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXLogic.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXLogic.js deleted file mode 100644 index 760824fe5ed..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXLogic.js +++ /dev/null @@ -1,140 +0,0 @@ -/* - * This file contains various nodes that can be used for logical processing. Systems like the FWC may use them to - * accurately implement their functionality. - */ - -/** - * The following class represents a monostable circuit. It is inspired by the MTRIG nodes as described in the ESLD and - * used by the FWC. - * When it detects either a rising or a falling edge (depending on it's type) it will emit a signal for a certain time t - * after the detection. It is not retriggerable, so a rising/falling edge within t will not reset the timer. - */ -class NXLogic_TriggeredMonostableNode { - constructor(t, risingEdge = true) { - this.t = t; - this.risingEdge = risingEdge; - this._timer = 0; - this._previousValue = null; - } - write(value, _deltaTime) { - if (this._previousValue === null && SimVar.GetSimVarValue("L:A32NX_FWC_SKIP_STARTUP", "Bool")) { - this._previousValue = value; - } - if (this.risingEdge) { - if (this._timer > 0) { - this._timer = Math.max(this._timer - _deltaTime / 1000, 0); - this._previousValue = value; - return true; - } else if (!this._previousValue && value) { - this._timer = this.t; - this._previousValue = value; - return true; - } - } else { - if (this._timer > 0) { - this._timer = Math.max(this._timer - _deltaTime / 1000, 0); - this._previousValue = value; - return true; - } else if (this._previousValue && !value) { - this._timer = this.t; - this._previousValue = value; - return true; - } - } - this._previousValue = value; - return false; - } -} - -/** - * The following class represents a "confirmation" circuit, which only passes a signal once it has been stable for a - * certain amount of time. It is inspired by the CONF nodes as described in the ESLD and used by the FWC. - * When it detects either a rising or falling edge (depending on it's type) it will wait for up to time t and emit the - * incoming signal if it was stable throughout t. If at any point the signal reverts during t the state is fully reset, - * and the original signal will be emitted again. - */ -class NXLogic_ConfirmNode { - constructor(t, risingEdge = true) { - this.t = t; - this.risingEdge = risingEdge; - this._timer = 0; - this._previousInput = null; - this._previousOutput = null; - } - write(value, _deltaTime) { - if (this._previousInput === null && SimVar.GetSimVarValue("L:A32NX_FWC_SKIP_STARTUP", "Bool")) { - this._previousInput = value; - this._previousOutput = value; - } - if (this.risingEdge) { - if (!value) { - this._timer = 0; - } else if (this._timer > 0) { - this._timer = Math.max(this._timer - _deltaTime / 1000, 0); - this._previousInput = value; - this._previousOutput = !value; - return !value; - } else if (!this._previousInput && value) { - this._timer = this.t; - this._previousInput = value; - this._previousOutput = !value; - return !value; - } - } else { - if (value) { - this._timer = 0; - } else if (this._timer > 0) { - this._timer = Math.max(this._timer - _deltaTime / 1000, 0); - this._previousInput = value; - this._previousOutput = !value; - return !value; - } else if (this._previousInput && !value) { - this._timer = this.t; - this._previousInput = value; - this._previousOutput = !value; - return !value; - } - } - this._previousInput = value; - this._previousOutput = value; - return value; - } - read() { - return this._previousOutput; - } -} - -/** - * The following class represents a flip-flop or memory circuit that can be used to store a single bit. It is inspired - * by the S+R nodes as described in the ESLD. - * It has two inputs: Set and Reset. At first it will always emit a falsy value, until it receives a signal on the set - * input, at which point it will start emitting a truthy value. This will continue until a signal is received on the - * reset input, at which point it reverts to the original falsy output. It a signal is sent on both set and reset at the - * same time, the input with a star will have precedence. - * The NVM flag is not implemented right now but can be used to indicate non-volatile memory storage, which means the - * value will persist even when power is lost and subsequently restored. - */ -class NXLogic_MemoryNode { - /** - * @param setStar Whether set has precedence over reset if both are applied simultaneously. - * @param nvm Whether the is non-volatile and will be kept even when power is lost. - */ - constructor(setStar = true, nvm = false) { - this.setStar = setStar; - this.nvm = nvm; // TODO in future, reset non-nvm on power cycle - this._value = false; - } - write(set, reset) { - if (set && reset) { - this._value = this.setStar; - } else if (set && !this._value) { - this._value = true; - } else if (reset && this._value) { - this._value = false; - } - return this._value; - } - read() { - return this._value; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXSpeeds.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXSpeeds.js deleted file mode 100644 index 2c2b037f89f..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXSpeeds.js +++ /dev/null @@ -1,575 +0,0 @@ -/** - * Stall speed table - * calls function(gross weight (t), landing gear) which returns CAS. - * Indexes: 0 - Clean config, 1 - Config 1 + F, 2 - Config 2, 3 - Config 3, 4 - Config Full, 5 - Config 1. - * Sub-Indexes: 0 to 9 represent gross weight (t) in 5t steps from 40 to 80. - */ -const vs = [ - [ - () => 124, - (m) => 124 + 1.4 * (m - 40), - (m) => 131 + 1.4 * (m - 45), - (m) => 138 + 1.4 * (m - 50), - (m) => 145 + m - 55, - (m) => 150 + 1.2 * (m - 60), - (m) => 155 + 1.2 * (m - 65), - (m) => 161 + m - 70, - (m) => 166 + 1.2 * (m - 75), - () => 172 - ], // Clean Conf - [ - () => 93, - (m) => 93 + m - 40, - (m) => 98 + m - 45, - (m) => 103 + m - 50, - (m) => 108 + .8 * (m - 55), - (m) => 112 + m - 60, - (m) => 117 + .8 + (m - 65), - (m) => 121 + .8 + (m - 70), - (m) => 125 + m - 75, - () => 130 - ], // Conf 1 + F - [ - () => 91, - (m) => 91 + m - 40, - (m) => 96 + m - 45, - (m) => 101 + .8 * (m - 50), - (m) => 105 + m - 55, - (m) => 110 + .8 * (m - 60), - (m) => 114 + m - 65, - (m) => 119 + .6 * (m - 70), - (m) => 122 + .8 * (m - 75), - () => 126 - ], // Conf 2 - [ - (_, ldg) => 91 - ldg * 2, - (m, ldg) => 91 + m - 40 - ldg * 2, - (m, ldg) => 96 + m - 45 - ldg * 2, - (m, ldg) => 101 + .8 * (m - 50) - ldg * 2, - (m, ldg) => 105 + m - 55 - ldg * 2, - (m, ldg) => 110 + .8 * (m - 60) - ldg * 2, - (m, ldg) => 114 + m - 65 - ldg * 2, - (m, ldg) => 119 + .6 * (m - 70) - ldg * 2, - (m, ldg) => 122 + .8 * (m - 75) - ldg * 2, - (_, ldg) => 126 - ldg * 2 - ], // Conf 3 - [ - () => 84, - (m) => 84 + .8 * (m - 40), - (m) => 88 + m - 45, - (m) => 93 + .8 * (m - 50), - (m) => 97 + .8 * (m - 55), - (m) => 101 + .8 * (m - 60), - (m) => 105 + .8 * (m - 65), - (m) => 109 + .8 * (m - 70), - (m) => 113 + .6 * (m - 75), - () => 116 - ], // Conf Full - [ - () => 102, - (m) => 102 + m - 40, - (m) => 107 + m - 45, - (m) => 112 + m - 50, - (m) => 117 + 1.2 * (m - 55), - (m) => 123 + .8 * (m - 60), - (m) => 127 + m - 65, - (m) => 132 + m - 70, - (m) => 137 + .8 * (m - 75), - () => 141 - ] // Conf 1 -]; - -/** - * Lowest selectable Speed Table - * calls function(gross weigh (t), landing gear) which returns CAS, automatically compensates for cg. - * Indexes: 0 - Clean config, 1 - Config 1 + F, 2 - Config 2, 3 - Config 3, 4 - Config Full, 5 - Config 1. - * Sub-Indexes: 0 to 9 represent gross weight (t) in 5t steps from 40 to 80. - */ -const vls = [ - [ - () => 159, - (m) => 159 + 1.8 * (m - 40), - (m) => 168 + 1.8 * (m - 45), - (m) => 177 + 1.8 * (m - 50), - (m) => 186 + 1.2 * (m - 55), - (m) => 192 + 1.2 * (m - 60), - (m) => 198 + 1.6 * (m - 65), - (m) => 206 + 1.2 * (m - 70), - (m) => 212 + 1.6 * (m - 75), - () => 220 - ], // Clean Config - [ - () => 114, - (m) => 114 + 1.4 * (m - 40), - (m) => 121 + 1.2 * (m - 45), - (m) => 127 + 1.2 * (m - 50), - (m) => 133 + m - 55, - (m) => 138 + 1.2 * (m - 60), - (m) => 144 + m - 65, - (m) => 149 + m - 70, - (m) => 154 + 1.2 * (m - 75), - () => 160 - ], // Config 1 + F - [ - () => 110, - (m) => 110 + 1.8 * (m - 40), - (m) => 119 + 1.2 * (m - 45), - (m) => 125 + 1.2 * (m - 50), - (m) => 131 + 1.2 * (m - 55), - (m) => 137 + m - 60, - (m) => 142 + .6 * (m - 65), - (m) => 145 + .8 * (m - 70), - (m) => 149 + m - 75, - () => 154 - ], // Config 2 - [ - (_, ldg) => 117 - ldg, - (m, ldg) => correctCg(m, (m, cg) => cg < 25 ? 117 + .4 * (m - 40) : 117) - ldg, - (m, ldg) => correctCg(m, (m, cg) => cg < 25 ? 119 + 1.2 * (m - 45) : 117 + 1.4 * (m - 45)) - ldg, - (m, ldg) => correctCg(m, (m, cg) => cg < 25 ? 125 + 1.2 * (m - 50) : 124 + 1.2 * (m - 50)) - ldg, - (m, ldg) => correctCg(m, (m, cg) => cg < 25 ? 131 + 1.2 * (m - 55) : 130 + m - 55) - ldg, - (m, ldg) => correctCg(m, (m, cg) => cg < 25 ? 137 + m - 60 : 135 + 1.2 * (m - 60)) - ldg, - (m, ldg) => correctCg(m, (m, cg) => (cg < 25 ? 142 : 141) + m - 65) - ldg, - (m, ldg) => correctCg(m, (m, cg) => (cg < 25 ? 147 : 146) + m - 70) - ldg, - (m, ldg) => correctCg(m, (m, cg) => cg < 25 ? 152 + .8 * (m - 75) : 151 + m - 65) - ldg, - (_, ldg) => 156 - ldg - ], // Config 3 - [ - () => 116, - () => 116, - () => 116, - (m) => 116 + correctCg(m, (m, cg) => (cg < 25 ? .8 : .6) * (m - 50)), - (m) => correctCg(m, (m, cg) => (cg < 25 ? 120 : 119) + m - 55), - (m) => correctCg(m, (m, cg) => (cg < 25 ? 125 : 124) + m - 60), - (m) => correctCg(m, (m, cg) => (cg < 25 ? 130 : 129) + m - 65), - (m) => correctCg(m, (m, cg) => cg < 25 ? 135 + .8 * (m - 70) : 134 + m - 70), - (m) => 139 + .8 * (m - 75), - () => 143 - ], // Config Full - [ - () => 125, - (m) => 125 + 1.4 * (m - 40), - (m) => 132 + 1.2 * (m - 45), - (m) => 138 + 1.2 * (m - 50), - (m) => 144 + 1.4 * (m - 55), - (m) => 151 + m - 60, - (m) => 156 + 1.2 * (m - 65), - (m) => 162 + 1.4 * (m - 70), - (m) => 169 + .8 * (m - 75), - () => 173 - ] // Config 1 -]; - -/** - * Lowest selectable Speed Table for TakeOff ONLY - * calls function(gross weight (t)) which returns CAS. - * Indexes: 0 - Clean config, 1 - Config 1 + F, 2 - Config 2, 3 - Config 3, 4 - Config Full, 5 - Config 1. - * Sub-Indexes: 0 to 9 represent gross weight (t) in 5t steps from 40 to 80. - */ -const vlsTo = [ - vls[0], // Clean Config - [ - () => 105, - (m) => 105 + 1.2 * (m - 40), - (m) => 111 + m - 45, - (m) => 116 + 1.2 * (m - 50), - (m) => 122 + m - 55, - (m) => 127 + m - 60, - (m) => 132 + m - 65, - (m) => 137 + .8 * (m - 70), - (m) => 141 + 1.2 * (m - 75), - () => 147 - ], // Config 1 + F - [ - (_) => 101, - (m) => 101 + 1.4 * (m - 40), - (m) => 108 + 1.2 * (m - 45), - (m) => 114 + m - 50, - (m) => 119 + 1.2 * (m - 55), - (m) => 125 + m - 60, - (m) => 130 + .4 * (m - 65), - (m) => 132 + .8 * (m - 70), - (m) => 136 + .8 * (m - 75), - () => 140 - ], // Config 2 - [ - () => 101, - (m) => 101 + m - 40, - (m) => 106 + 1.2 * (m - 45), - (m) => 112 + .8 * (m - 50), - (m) => 116 + 1.2 * (m - 55), - (m) => 122 + m - 60, - (m) => 127 + m - 65, - (m) => 132 + .8 * (m - 70), - (m) => 136 + .8 * (m - 75), - () => 140 - ], // Config 3 - vls[4], // Config Full - vls[5] // Config 1 -]; - -/** - * F-Speed Table - * calls function(gross weight (t)) which returns CAS. - * Indexes: 0 to 9 represent gross weight (t) in 5t steps from 40 to 80. - */ -const f = [ - () => 131, - () => 131, - () => 131, - (m) => 131 + 1.2 * (m - 50), - (m) => 137 + 1.4 * (m - 55), - (m) => 144 + m - 60, - (m) => 149 + 1.2 * (m - 65), - (m) => 155 + m - 70, - (m) => 160 + 1.20 * (m - 75), - () => 166 -]; - -/** - * S-Speed Table - * calls function(gross weight (t)) which returns CAS. - * Indexes: 0 to 9 represent gross weight (t) in 5t steps from 40 to 80. - */ -const s = [ - () => 152, - (m) => 152 + 1.8 * (m - 40), - (m) => 161 + 1.6 * (m - 45), - (m) => 169 + 1.8 * (m - 50), - (m) => 178 + 1.6 * (m - 55), - (m) => 186 + 1.4 * (m - 60), - (m) => 193 + 1.4 * (m - 65), - (m) => 200 + 1.4 * (m - 70), - (m) => 207 + 1.4 * (m - 75), - () => 214 -]; - -const vmca = [ - [-2000, 115], - [0, 114], - [2000, 114], - [4000, 113], - [6000, 112], - [8000, 109], - [10000, 106], - [12000, 103], - [14100, 99], - [15100, 97], -]; - -const vmcg = [ // 1+F, 2, 3 all the same - [-2000, 117], - [0, 116], - [2000, 116], - [4000, 115], - [6000, 114], - [8000, 112], - [10000, 109], - [12000, 106], - [14100, 102], - [15100, 101], -]; - -/** - * Vfe for Flaps/Slats - * @type {number[]} - */ -const vfeFS = [ - 215, // Config 1 + F - 200, // Config 2 - 185, // Config 3 - 177, // Config Full - 230 // Config 1 -]; - -const Vmo = 350; -const Mmo = 0.82; - -/** - * Correct input function for cg - * @param m {number} gross weight (t) - * @param f {function} function to be called with cg variable - * @param cg {number} center of gravity - * @returns {number} cg corrected velocity (CAS) - */ -function correctCg(m, f, cg = SimVar.GetSimVarValue("CG PERCENT", "percent")) { - return f(m, isNaN(cg) ? 24 : cg); -} - -/** - * Ensure gross weight (mass) is withing valid range - * @param m {number} mass: gross weight - * @returns {number} mass: gross weight - * @private - */ -function _correctMass(m) { - return Math.ceil(((m > 80 ? 80 : m) - 40) / 5); -} - -/** - * Calculate green dot speed - * Calculation: - * Gross weight (t) * 2 + 85 when below FL200 - * @returns {number} - */ -function _computeGD(m) { - return m * 2 + 85; -} - -/** - * Corrects velocity for mach effect by adding 1kt for every 1000ft above FL200 - * @param v {number} velocity in kt (CAS) - * @param alt {number} altitude in feet (baro) - * @returns {number} Mach corrected velocity in kt (CAS) - */ -function _compensateForMachEffect(v, alt) { - return Math.ceil(alt > 20000 ? v + (alt - 20000) / 1000 : v); -} - -/** - * Calculates wind component for ground speed mini - * @param vw {number} velocity wind (headwind) - * @returns {number} velocity wind [5, 15] - */ -function _addWindComponent(vw) { - return Math.max(Math.min(15, vw), 5); -} - -/** - * Get difference between angles - * @param a {number} angle a - * @param b {number} angle b - * @returns {number} angle diff - * @private - */ -function _getdiffAngle(a, b) { - return 180 - Math.abs(Math.abs(a - b) - 180); -} - -/** - * Get next flaps index for vfeFS table - * @param fi {number} current flaps index - * @returns {number} vfeFS table index - * @private - */ -function _getVfeNIdx(fi) { - switch (fi) { - case 0: return 4; - case 5: return 1; - default: return fi; - } -} - -/** - * Convert degrees Celsius into Kelvin - * @param T {number} degrees Celsius - * @returns {number} degrees Kelvin - */ -function _convertCtoK(T) { - return T + 273.15; -} - -/** - * Convert Mach to True Air Speed - * @param M {number} Mach - * @param T {number} Kelvin - * @returns {number} True Air Speed - */ -function _convertMachToKTas(M, T) { - return M * 661.4786 * Math.sqrt(T / 288.15); -} - -/** - * Convert TAS to Mach - * @param Vt {number} TAS - * @param T {number} Kelvin - * @returns {number} True Air Speed - */ -function _convertKTASToMach(Vt, T) { - return Vt / 661.4786 / Math.sqrt(T / 288.15); -} - -/** - * Convert TAS to Calibrated Air Speed - * @param Vt {number} velocity true air speed - * @param T {number} current temperature Kelvin - * @param p {number} current pressure hpa - * @returns {number} Calibrated Air Speed - */ -function _convertTasToKCas(Vt, T, p) { - return 1479.1 * Math.sqrt((p / 1013 * ((1 + 1 / (T / 288.15) * (Vt / 1479.1) ** 2) ** 3.5 - 1) + 1) ** (1 / 3.5) - 1); -} - -/** - * Convert KCAS to KTAS - * @param Vc {number} velocity true air speed - * @param T {number} current temperature Kelvin - * @param p {number} current pressure hpa - * @returns {number} Calibrated Air Speed - */ -function _convertKCasToKTAS(Vc, T, p) { - return 1479.1 * Math.sqrt(T / 288.15 * ((1 / (p / 1013) * ((1 + 0.2 * (Vc / 661.4786) ** 2) ** 3.5 - 1) + 1) ** (1 / 3.5) - 1)); -} - -/** - * Convert Mach to Calibrated Air Speed - * @param M {number} Mach - * @param T {number} Kelvin - * @param p {number} current pressure hpa - * @returns {number} Calibrated Air Speed - */ -function _convertMachToKCas(M, T, p) { - return _convertTasToKCas(_convertMachToKTas(M, T), T, p); -} - -/** - * Get correct Vmax for Vmo and Mmo in knots - * @returns {number} Min(Vmo, Mmo) - * @private - */ -function _getVmo() { - return Math.min(Vmo, _convertMachToKCas(Mmo, _convertCtoK(Simplane.getAmbientTemperature()), SimVar.GetSimVarValue("AMBIENT PRESSURE", "millibar"))); -} - -class NXSpeeds { - /** - * Computes Vs, Vls, Vapp, F, S and GD - * @param m {number} mass: gross weight in t - * @param fPos {number} flaps position - * @param gPos {number} landing gear position - * @param isTo {boolean} whether in takeoff config or not - * @param wind {number} wind speed - */ - constructor(m, fPos, gPos, isTo, wind = 0) { - const cm = _correctMass(m); - this.vs = vs[fPos][cm](m, gPos); - this.vls = (isTo ? vlsTo : vls)[fPos][cm](m, gPos); - this.vapp = this.vls + _addWindComponent(wind); - this.f = f[cm](m); - this.s = s[cm](m); - this.gd = _computeGD(m); - this.vmax = fPos === 0 ? _getVmo() : vfeFS[fPos - 1]; - this.vfeN = fPos === 4 ? 0 : vfeFS[_getVfeNIdx(fPos)]; - } - - compensateForMachEffect(alt) { - this.vs = _compensateForMachEffect(this.vs, alt); - this.vls = _compensateForMachEffect(this.vls, alt); - this.gd = _compensateForMachEffect(this.gd, alt); - } -} - -class NXSpeedsApp { - /** - * Calculates VLS and Vapp for selected landing configuration - * @param {number} m Projected landing mass in t - * @param {boolean} isConf3 CONF3 if true, else FULL - * @param {number} [wind=0] tower headwind component - */ - constructor(m, isConf3, wind = 0) { - const cm = _correctMass(m); - this.vls = vls[isConf3 ? 3 : 4][cm](m, 1); - this.vapp = this.vls + NXSpeedsUtils.addWindComponent(wind / 3); - this.f = f[cm](m); - this.s = s[cm](m); - this.gd = _computeGD(m); - this.valid = true; - } -} - -class NXSpeedsUtils { - /** - * Calculates wind component for ground speed mini - * @param vw {number} velocity wind (1/3 steady headwind) - * @returns {number} velocity wind [5, 15] - */ - static addWindComponent(vw = (SimVar.GetSimVarValue("AIRCRAFT WIND Z", "knots") * -1) / 3) { - return _addWindComponent(vw); - } - - /** - * Calculates headwind component - * @param v {number} velocity wind - * @param a {number} angle: a - * @param b {number} angle: b - * @returns {number} velocity headwind - */ - static getHeadwind(v, a, b) { - return v * Math.cos(_getdiffAngle(a, b) * (Math.PI / 180)); - } - - /** - * 1/3 * (current headwind - tower headwind) - * @param vTwr {number} velocity tower headwind - * @param vCur {number} velocity current headwind - * @returns {number} head wind diff - */ - static getHeadWindDiff(vTwr, vCur = SimVar.GetSimVarValue("AIRCRAFT WIND Z", "knots") * -1) { - return Math.round(1 / 3 * (vCur - vTwr)); - } - - /** - * Returns Vtarget limited by Vapp and VFE next - * @param vapp {number} Vapp - * @param windDiff {number} ground speed mini - * @returns {number} - */ - static getVtargetGSMini(vapp, windDiff) { - return Math.max(vapp, Math.min(Math.round(vapp + windDiff), Math.round( - SimVar.GetSimVarValue("L:A32NX_FLAPS_HANDLE_INDEX", "Number") === 4 ? SimVar.GetSimVarValue("L:A32NX_SPEEDS_VMAX", "Number") - 5 : SimVar.GetSimVarValue("L:A32NX_SPEEDS_VFEN", "Number") - ))); - } - - static convertKCasToMach( - Vc, - T = _convertCtoK(Simplane.getAmbientTemperature()), - p = SimVar.GetSimVarValue("AMBIENT PRESSURE", "millibar") - ) { - return _convertKTASToMach(_convertKCasToKTAS(Vc, T, p), T); - } - - /** @private */ - static interpolateTable(table, alt) { - if (alt <= table[0][0]) { - return vmca[0][1]; - } - if (alt >= table[table.length - 1][0]) { - table[table.length - 1][1]; - } - for (let i = 0; i < table.length - 1; i++) { - if (alt >= table[i][0] && alt <= table[i + 1][0]) { - const d = (alt - table[i][0]) / (table[i + 1][0] - table[i][0]); - return Avionics.Utils.lerpAngle(table[i][1], table[i + 1][1], d); - } - } - } - - /** - * Get VMCA (minimum airborne control speed) for a given altitude - * @param {number} altitude Altitude in feet - * @returns VMCA in knots - */ - static getVmca(altitude) { - return this.interpolateTable(vmca, altitude); - } - - /** - * Get VMCG (minimum ground control speed) for a given altitude - * @param {number} altitude Altitude in feet - * @returns VMCG in knots - */ - static getVmcg(altitude) { - return this.interpolateTable(vmcg, altitude); - } - - /** - * Get Vs1g for the given config - * - * @param {number} mass mass of the aircraft in tons - * @param {number} conf 0 - Clean config, 1 - Config 1 + F, 2 - Config 2, 3 - Config 3, 4 - Config Full, 5 - Config 1. - * @param {boolean} gearDown true if the gear is down - */ - static getVs1g(mass, conf, gearDown) { - return vs[conf][_correctMass(mass)](mass, gearDown ? 1 : 0); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXSystemMessages.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXSystemMessages.js deleted file mode 100644 index 8461442bf97..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXSystemMessages.js +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class McduMessage { - constructor(text, isAmber = false, replace = "") { - this.text = text; - this.isAmber = isAmber; - this.replace = replace; - this.isTypeTwo = false; - } -} - -class TypeIMessage extends McduMessage { - constructor(text, isAmber = false, replace = "") { - super(text, isAmber, replace); - } - - /** - * Only returning a "copy" of the object to ensure thread safety when trying to edit the original message - * t {string} replaces defined elements, see this.replace - */ - getModifiedMessage(t) { - return new McduMessage( - !!t ? this.text.replace(this.replace, "" + t) : this.text, - this.isAmber, - this.replace, - ); - } -} - -class TypeIIMessage extends McduMessage { - constructor(text, isAmber = false, replace = "", isResolved = () => false, onClear = () => {}) { - super(text, isAmber, replace); - - this.isTypeTwo = true; - this.isResolved = isResolved; - this.onClear = onClear; - } - - /** - * Only returning a "copy" of the object to ensure thread safety when trying to edit the original message - * t {string} replaces defined elements, see this.replace - * isResolved {function} overrides present function - * onClear {function} overrides present function - */ - getModifiedMessage(t, isResolved = undefined, onClear = undefined) { - return new TypeIIMessage( - !!t ? this.text.replace(this.replace, "" + t) : this.text, - this.isAmber, - this.replace, - isResolved || this.isResolved, - onClear || this.onClear - ); - } -} - -/** - NXSystemMessages only holds real messages - */ -const NXSystemMessages = { - aocActFplnUplink: new TypeIIMessage("AOC ACT F-PLN UPLINK"), - arptTypeAlreadyInUse: new TypeIMessage("ARPT/TYPE ALREADY USED"), - awyWptMismatch: new TypeIMessage("AWY/WPT MISMATCH"), - cancelAtisUpdate: new TypeIMessage("CANCEL UPDATE BEFORE"), - checkMinDestFob: new TypeIIMessage("CHECK MIN DEST FOB"), - checkSpeedMode: new TypeIIMessage("CHECK SPEED MODE"), - checkToData: new TypeIIMessage("CHECK TAKE OFF DATA", true), - checkWeight: new TypeIIMessage("CHECK WEIGHT", true), - comUnavailable: new TypeIMessage("COM UNAVAILABLE"), - cstrDelUpToWpt: new TypeIIMessage("CSTR DEL UP TO WWWWW", false, "WWWWW"), - databaseCodingError: new TypeIIMessage("DATABASE CODING ERROR"), - dcduFileFull: new TypeIMessage("DCDU FILE FULL"), - destEfobBelowMin: new TypeIIMessage("DEST EFOB BELOW MIN", true), - enterDestData: new TypeIIMessage("ENTER DEST DATA", true), - entryOutOfRange: new TypeIMessage("ENTRY OUT OF RANGE"), - mandatoryFields: new TypeIMessage("ENTER MANDATORY FIELDS"), - formatError: new TypeIMessage("FORMAT ERROR"), - fplnElementRetained: new TypeIMessage("F-PLN ELEMENT RETAINED"), - initializeWeightOrCg: new TypeIIMessage("INITIALIZE WEIGHT/CG", true), - keyNotActive: new TypeIMessage("KEY NOT ACTIVE"), - latLonAbreviated: new TypeIMessage("LAT/LON DISPL ABREVIATED"), - listOf99InUse: new TypeIMessage("LIST OF 99 IN USE"), - newAccAlt: new TypeIIMessage("NEW ACC ALT-HHHH", false, "HHHH"), - newAtisReceived: new TypeIMessage("NEW ATIS: READ AGAIN"), - newCrzAlt: new TypeIIMessage("NEW CRZ ALT - HHHHH", false, "HHHHH"), - newThrRedAlt: new TypeIIMessage("NEW THR RED ALT-HHHH", false, "HHHH"), - noAtc: new TypeIMessage("NO ACTIVE ATC"), - noAtisReceived: new TypeIMessage("NO ATIS REPORT RECEIVED"), - noIntersectionFound: new TypeIMessage("NO INTERSECTION FOUND"), - noPreviousAtis: new TypeIMessage("NO PREVIOUS ATIS STORED"), - notAllowed: new TypeIMessage("NOT ALLOWED"), - notAllowedInNav: new TypeIMessage("NOT ALLOWED IN NAV"), - notInDatabase: new TypeIMessage("NOT IN DATABASE"), - rwyLsMismatch: new TypeIIMessage("RWY/LS MISMATCH", true), - selectDesiredSystem: new TypeIMessage("SELECT DESIRED SYSTEM"), - setHoldSpeed: new TypeIIMessage("SET HOLD SPEED"), - spdLimExceeded: new TypeIIMessage("SPD LIM EXCEEDED", true), - systemBusy: new TypeIMessage("SYSTEM BUSY-TRY LATER"), - toSpeedTooLow: new TypeIIMessage("TO SPEEDS TOO LOW", true), - uplinkInsertInProg: new TypeIIMessage("UPLINK INSERT IN PROG"), - usingCostIndex: new TypeIMessage("USING COST INDEX: NNN", false, "NNN"), - vToDisagree: new TypeIIMessage("V1/VR/V2 DISAGREE", true), - waitForSystemResponse: new TypeIMessage("WAIT FOR SYSTEM RESPONSE"), - xxxIsDeselected: new TypeIMessage("XXXX IS DESELECTED", false, "XXXX"), - stepAboveMaxFl: new TypeIIMessage("STEP ABOVE MAX FL"), - stepAhead: new TypeIIMessage("STEP AHEAD"), - stepDeleted: new TypeIIMessage("STEP DELETED"), -}; - -const NXFictionalMessages = { - noNavigraphUser: new TypeIMessage("NO NAVIGRAPH USER"), - internalError: new TypeIMessage("INTERNAL ERROR"), - noAirportSpecified: new TypeIMessage("NO AIRPORT SPECIFIED"), - fltNbrInUse: new TypeIMessage("FLT NBR IN USE"), - fltNbrMissing: new TypeIMessage("ENTER ATC FLT NBR"), - notYetImplemented: new TypeIMessage("NOT YET IMPLEMENTED"), - recipientNotFound: new TypeIMessage("RECIPIENT NOT FOUND"), - authErr: new TypeIMessage("AUTH ERR"), - invalidMsg: new TypeIMessage("INVALID MSG"), - unknownDownlinkErr: new TypeIMessage("UNKNOWN DOWNLINK ERR"), - telexNotEnabled: new TypeIMessage("TELEX NOT ENABLED"), - freeTextDisabled: new TypeIMessage("FREE TEXT DISABLED"), - freetextEnabled: new TypeIMessage("FREE TEXT ENABLED"), - enabledFltNbrInUse: new TypeIMessage("ENABLED. FLT NBR IN USE"), - noOriginApt: new TypeIMessage("NO ORIGIN AIRPORT"), - noOriginSet: new TypeIMessage("NO ORIGIN SET"), - secondIndexNotFound: new TypeIMessage("2ND INDEX NOT FOUND"), - firstIndexNotFound: new TypeIMessage("1ST INDEX NOT FOUND"), - noRefWpt: new TypeIMessage("NO REF WAYPOINT"), - noWptInfos: new TypeIMessage("NO WAYPOINT INFOS"), - emptyMessage: new TypeIMessage(""), - reloadPlaneApply: new TypeIIMessage("RELOAD A/C TO APPLY", true), - noHoppieConnection: new TypeIMessage("NO HOPPIE CONNECTION"), - unknownAtsuMessage: new TypeIMessage("UNKNOWN ATSU MESSAGE"), - reverseProxy: new TypeIMessage("REVERSE PROXY ERROR") -}; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXUnits.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXUnits.js deleted file mode 100644 index b9a65f8a910..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXUnits.js +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Unit conversion utilities - */ - -class NXUnits { - /* private static _metricWeight: boolean; */ - - static get metricWeight() { - if (NXUnits._metricWeight === undefined) { - NXDataStore.getAndSubscribe('CONFIG_USING_METRIC_UNIT', (key, value) => { - NXUnits._metricWeight = value === '1'; - }, '1'); - } - return NXUnits._metricWeight; - } - - static userToKg(value) { - return NXUnits.metricWeight ? value : value * 0.4535934; - } - - static kgToUser(value) { - return NXUnits.metricWeight ? value : value / 0.4535934; - } - - static poundsToUser(value) { - return NXUnits.metricWeight ? value / 2.204625 : value; - } - - static userWeightUnit() { - return NXUnits.metricWeight ? 'KG' : 'LBS'; // EIS uses S suffix on LB - } - - /** - * Converts meter into ft if imperial units are selected - * @param value {number} in unit Meters - * @returns {number} in metric or ft - */ - static mToUser(value) { - return NXUnits.metricWeight ? value : value * 3.28084; - } - - /** - * Returns 'M' for meters and 'FT' for feet depending on the unit system selected - * @returns {string} 'M' (meter) OR 'FT' (feet) - */ - static userDistanceUnit() { - return NXUnits.metricWeight ? 'M' : 'FT'; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/SimBriefApi.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/SimBriefApi.js deleted file mode 100644 index 52b0161baa8..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/SimBriefApi.js +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class SimBriefApi { - static getSimBriefOfp(username, overrideUserID) { - if (username || overrideUserID) { - return fetch(overrideUserID ? `${SimBriefApi.url}&userid=${overrideUserID}` : `${SimBriefApi.url}&username=${username}`) - .then((response) => { - if (!response.ok) { - throw new HttpError(response.status); - } - - return response.json(); - }); - } else { - throw new Error("No Navigraph username or override simbrief user id provided"); - } - } -} - -SimBriefApi.url = "https://www.simbrief.com/api/xml.fetcher.php?json=1"; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/arinc429.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/arinc429.js deleted file mode 100644 index afb8725ab53..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/arinc429.js +++ /dev/null @@ -1,75 +0,0 @@ -class Arinc429Word { - constructor(value) { - Arinc429Word.u32View[0] = (value & 0xffffffff) >>> 0; - this.ssm = Math.trunc(value / 2 ** 32); - this.value = Arinc429Word.f32View[0]; - } - - static empty() { - return new Arinc429Word(0); - } - - static fromSimVarValue(name) { - return new Arinc429Word(SimVar.GetSimVarValue(name, "number")); - } - - static async toSimVarValue(name, value, ssm = Arinc429Word.SignStatusMatrix.NormalOperation) { - Arinc429Word.f32View[0] = value; - const simVal = Arinc429Word.u32View[0] + Math.trunc(ssm) * 2 ** 32; - return SimVar.SetSimVarValue(name, 'string', simVal.toString()); - } - - isFailureWarning() { - return this.ssm === Arinc429Word.SignStatusMatrix.FailureWarning; - } - - isNoComputedData() { - return this.ssm === Arinc429Word.SignStatusMatrix.NoComputedData; - } - - isFunctionalTest() { - return this.ssm === Arinc429Word.SignStatusMatrix.FunctionalTest; - } - - isNormalOperation() { - return this.ssm === Arinc429Word.SignStatusMatrix.NormalOperation; - } - - /** - * Returns the value when normal operation, the supplied default value otherwise. - */ - valueOr(defaultValue) { - return this.isNormalOperation() ? this.value : defaultValue; - } - - bitValue(bit) { - return ((this.value >> (bit - 1)) & 1) !== 0; - } - - bitValueOr(bit, defaultValue) { - return this.isNormalOperation() ? ((this.value >> (bit - 1)) & 1) !== 0 : defaultValue; - } - - setBitValue(bit, value) { - if (value) { - this.value |= 1 << (bit - 1); - } else { - this.value &= ~(1 << (bit - 1)); - } - } - - equals(other) { - return this === other - || (typeof other !== "undefined" && other !== null && this.value === other.value && this.ssm === other.ssm); - } -} - -Arinc429Word.u32View = new Uint32Array(1); -Arinc429Word.f32View = new Float32Array(Arinc429Word.u32View.buffer); - -Arinc429Word.SignStatusMatrix = Object.freeze({ - FailureWarning: 0b00, - NoComputedData: 0b01, - FunctionalTest: 0b10, - NormalOperation: 0b11, -}); diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/bitFlags.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/bitFlags.js deleted file mode 100644 index 00cbe70c5e8..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/bitFlags.js +++ /dev/null @@ -1,80 +0,0 @@ -class BitFlags { - constructor(number) { - this.f64View = new Float64Array(1); - this.u32View = new Uint32Array(this.f64View.buffer); - this.setFlags(number); - } - - setFlags(number) { - this.flags = Array.from(this.u32View); - const bigNumberAsBinaryStr = number.toString(2); - - let bigNumberAsBinaryStr2 = ''; - for (let i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { - bigNumberAsBinaryStr2 += '0'; - } - - bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; - - this.flags[1] = parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2); - this.flags[0] = parseInt(bigNumberAsBinaryStr2.substring(32), 2); - } - - getBitIndex(bit) { - if (bit > 63) { - return false; - } - const f = Math.floor(bit / 31); - const b = bit % 31; - - return ((this.flags[f] >> b) & 1) !== 0; - } - - toggleBitIndex(bit) { - if (bit > 63) { - return; - } - const f = Math.floor(bit / 31); - const b = bit % 31; - - this.flags[f] ^= (1 << b); - } - - toDouble() { - return (new Float64Array(Uint32Array.from(this.flags).buffer))[0]; - } - - toDebug() { - const debug = []; - this.flags.forEach((flag, index) => { - debug.push(flag.toString(2)); - const fL = 32 - flag.toString(2).length; - for (let i = 0; i < fL; i++) { - debug[index] = '0'.concat(debug[index]); - } - }); - return (`HIGH [ ${debug[1]} | ${debug[0]} ] LOW`); - } - - toNumber() { - return this.flags[1] * 2 ** 32 + this.flags[0]; - } - - toString() { - return this.toNumber().toString(); - } - - getTotalBits() { - let total = 0; - this.flags.forEach((flag) => { - const n = 32; - let i = 0; - while (i++ < n) { - if ((1 << i & flag) === (1 << i)) { - total++; - } - } - }); - return total; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/A32NX_BaseAirliners.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/A32NX_BaseAirliners.js deleted file mode 100644 index 93734ac5571..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/A32NX_BaseAirliners.js +++ /dev/null @@ -1,1258 +0,0 @@ -class BaseAirliners extends NavSystem { - constructor() { - super(...arguments); - this.isMachActive = false; - this.machTransition = 0; - } - connectedCallback() { - super.connectedCallback(); - this.preserveAspectRatio("Mainframe"); - this.addEventAlias("FMS_Upper_INC", "NavigationSmallInc"); - this.addEventAlias("FMS_Upper_DEC", "NavigationSmallDec"); - this.addEventAlias("FMS_Lower_INC", "NavigationLargeInc"); - this.addEventAlias("FMS_Lower_DEC", "NavigationLargeDec"); - this.addEventAlias("FMS_Upper_PUSH", "NavigationPush"); - } - onUpdate(_deltaTime) { - super.onUpdate(_deltaTime); - BaseAirliners.isMetric = Simplane.getUnitIsMetric(); - } - static unitIsMetric(_plane) { - switch (_plane) { - case Aircraft.A320_NEO: - return true; - case Aircraft.B747_8: - case Aircraft.AS01B: - return false; - } - return BaseAirliners.isMetric; - } -} -BaseAirliners.isMetric = false; -var Airliners; -(function (Airliners) { - class BaseEICAS extends BaseAirliners { - constructor() { - super(); - this.lowerScreenPages = new Array(); - } - connectedCallback() { - super.connectedCallback(); - if (this.urlConfig.index) { - this.setAttribute("index", this.urlConfig.index.toString()); - } - this.createLowerScreenPages(); - this.pageGroups = [new NavSystemPageGroup(BaseEICAS.LOWER_SCREEN_GROUP_NAME, this, this.lowerScreenPages)]; - } - disconnectedCallback() { - } - onEvent(_event) { - super.onEvent(_event); - const prefix = this.getLowerScreenChangeEventNamePrefix(); - if (_event.indexOf(prefix) >= 0) { - const pageName = _event.replace(prefix, ""); - this.changePage(pageName); - } else { - for (let i = 0; i < this.lowerScreenPages.length; i++) { - this.lowerScreenPages[i].onEvent(_event); - } - } - } - - createLowerScreenPage(_name, _htmlElemId, _eicasPageSelector) { - this.lowerScreenPages.push(new NavSystemPage(_name.toUpperCase(), _htmlElemId, new Airliners.EICASPage(_eicasPageSelector))); - } - changePage(_pageName) { - const pageName = _pageName.toUpperCase(); - this.SwitchToPageName(BaseEICAS.LOWER_SCREEN_GROUP_NAME, pageName); - for (let i = 0; i < this.lowerScreenPages.length; i++) { - if (this.lowerScreenPages[i].name == pageName) { - // SimVar.SetSimVarValue("L:A32NX_ECAM_SD_CURRENT_PAGE_INDEX", "number", i); - break; - } - } - } - } - BaseEICAS.LOWER_SCREEN_GROUP_NAME = "LowerScreenGroup"; - BaseEICAS.LOWER_SCREEN_CHANGE_EVENT_NAME = "ChangeLowerScreenPage"; - Airliners.BaseEICAS = BaseEICAS; - let EICAS_INFO_PANEL_ID; - (function (EICAS_INFO_PANEL_ID) { - EICAS_INFO_PANEL_ID[EICAS_INFO_PANEL_ID["PRIMARY"] = 0] = "PRIMARY"; - EICAS_INFO_PANEL_ID[EICAS_INFO_PANEL_ID["SECONDARY"] = 1] = "SECONDARY"; - })(EICAS_INFO_PANEL_ID = Airliners.EICAS_INFO_PANEL_ID || (Airliners.EICAS_INFO_PANEL_ID = {})); - let EICAS_INFO_PANEL_EVENT_TYPE; - (function (EICAS_INFO_PANEL_EVENT_TYPE) { - EICAS_INFO_PANEL_EVENT_TYPE[EICAS_INFO_PANEL_EVENT_TYPE["ADD_MESSAGE"] = 0] = "ADD_MESSAGE"; - EICAS_INFO_PANEL_EVENT_TYPE[EICAS_INFO_PANEL_EVENT_TYPE["REMOVE_MESSAGE"] = 1] = "REMOVE_MESSAGE"; - EICAS_INFO_PANEL_EVENT_TYPE[EICAS_INFO_PANEL_EVENT_TYPE["MODIFY_MESSAGE"] = 2] = "MODIFY_MESSAGE"; - EICAS_INFO_PANEL_EVENT_TYPE[EICAS_INFO_PANEL_EVENT_TYPE["CLEAR_SCREEN"] = 3] = "CLEAR_SCREEN"; - })(EICAS_INFO_PANEL_EVENT_TYPE = Airliners.EICAS_INFO_PANEL_EVENT_TYPE || (Airliners.EICAS_INFO_PANEL_EVENT_TYPE = {})); - let EICAS_INFO_PANEL_MESSAGE_STYLE; - (function (EICAS_INFO_PANEL_MESSAGE_STYLE) { - EICAS_INFO_PANEL_MESSAGE_STYLE[EICAS_INFO_PANEL_MESSAGE_STYLE["INDICATION"] = 0] = "INDICATION"; - EICAS_INFO_PANEL_MESSAGE_STYLE[EICAS_INFO_PANEL_MESSAGE_STYLE["CAUTION"] = 1] = "CAUTION"; - EICAS_INFO_PANEL_MESSAGE_STYLE[EICAS_INFO_PANEL_MESSAGE_STYLE["WARNING"] = 2] = "WARNING"; - EICAS_INFO_PANEL_MESSAGE_STYLE[EICAS_INFO_PANEL_MESSAGE_STYLE["TITLE"] = 3] = "TITLE"; - EICAS_INFO_PANEL_MESSAGE_STYLE[EICAS_INFO_PANEL_MESSAGE_STYLE["OPTION"] = 4] = "OPTION"; - })(EICAS_INFO_PANEL_MESSAGE_STYLE = Airliners.EICAS_INFO_PANEL_MESSAGE_STYLE || (Airliners.EICAS_INFO_PANEL_MESSAGE_STYLE = {})); - class EICASInfoPanelManager { - } - Airliners.EICASInfoPanelManager = EICASInfoPanelManager; - class EICASTemplateElement extends TemplateElement { - onEvent(_event) { } - ; - getInfoPanelManager() { - return null; - } - ; - } - Airliners.EICASTemplateElement = EICASTemplateElement; - class EICASScreen extends NavSystemElementContainer { - constructor(_name, _root, _selector) { - super(_name, _root, null); - this.selector = ""; - this.screen = null; - this.IndependentElements = new NavSystemElementGroup([]); - this.selector = _selector; - this.element = this.IndependentElements; - this.getDeltaTime = A32NX_Util.createDeltaTimeCalculator(this._lastTime); - } - init() { - super.init(); - const root = this.gps.getChildById(this.htmlElemId); - if (root != null) { - this.screen = root.querySelector(this.selector); - } - } - onUpdate() { - const _deltaTime = this.getDeltaTime(); - super.onUpdate(_deltaTime); - if (this.screen != null) { - this.screen.update(_deltaTime); - } - } - addIndependentElement(_element) { - this.IndependentElements.addElement(_element); - } - getInfoPanelManager() { - if (this.screen) { - return this.screen.getInfoPanelManager(); - } - return null; - } - ; - } - Airliners.EICASScreen = EICASScreen; - class EICASPage extends NavSystemElement { - constructor(_selector) { - super(); - this.selector = ""; - this.selector = _selector; - } - init() { - const root = this.gps.getChildById(this.container.htmlElemId); - if (root != null) { - this.page = root.querySelector(this.selector); - } - this.getDeltaTime = A32NX_Util.createDeltaTimeCalculator(this._lastTime); - } - onEnter() { - if (this.page != null) { - this.page.style.display = "block"; - } - } - onUpdate() { - const _deltaTime = this.getDeltaTime(); - if (this.page != null) { - this.page.update(_deltaTime); - } - } - onEvent(_event) { - if (this.page) { - this.page.onEvent(_event); - } - } - onExit() { - if (this.page != null) { - this.page.style.display = "none"; - } - } - } - Airliners.EICASPage = EICASPage; - class DynamicValueComponent { - constructor(_text, _getValueFunction, _roundToDP = 0, _formatFunction = null) { - this.visible = true; - this.text = null; - this.getValue = null; - this.roundToDP = 0; - this.format = null; - this.currentValue = 0; - this.text = _text; - this.getValue = _getValueFunction; - this.roundToDP = _roundToDP; - this.format = _formatFunction; - this.trySetValue(0, true); - } - set isVisible(_visible) { - this.visible = _visible; - if (this.visible) { - if (this.getValue != null) { - this.trySetValue(this.getValue(), true); - } - } else { - if (this.text != null) { - this.text.textContent = ""; - } - } - } - refresh() { - if (this.visible) { - if (this.getValue != null) { - this.trySetValue(this.getValue()); - } - } - } - trySetValue(_value, _force = false) { - if ((_value != this.currentValue) || _force) { - this.currentValue = _value; - if (this.text != null) { - if (this.format != null) { - this.text.textContent = this.format(_value, this.roundToDP); - } else { - this.text.textContent = DynamicValueComponent.formatValueToString(_value, this.roundToDP); - } - } - } - } - static formatValueToString(_value, _dp = 0) { - return _value.toFixed(_dp); - } - static formatValueToPosNegString(_value, _dp = 0) { - return ((_value > 0) ? "+" : "") + _value.toFixed(_dp); - } - static formatValueToPosNegTemperature(_value, _dp = 0) { - return (DynamicValueComponent.formatValueToPosNegString(_value, _dp) + "c"); - } - static formatValueToThrottleDisplay(_value, _dp = 0) { - if (_value < 0) { - return "REV"; - } else { - return _value.toFixed(_dp); - } - } - } - Airliners.DynamicValueComponent = DynamicValueComponent; - class BaseATC extends BaseAirliners { - constructor() { - super(); - this.currentDigits = [0, 0, 0, 0]; - this.currentCode = 0; - this.bLastInputIsCLR = false; - this.emptySlot = "-"; - } - connectedCallback() { - super.connectedCallback(); - this.valueText = this.querySelector("text"); - } - disconnectedCallback() { - super.disconnectedCallback(); - } - Init() { - super.Init(); - console.log("ATC Init"); - } - onUpdate(_deltaTime) { - super.onUpdate(_deltaTime); - - const lightsTest = SimVar.GetSimVarValue("L:A32NX_OVHD_INTLT_ANN", "number") == 0 && SimVar.GetSimVarValue("L:A32NX_ELEC_DC_2_BUS_IS_POWERED", "Bool"); - const lightsTestChanged = lightsTest !== this.lightsTest; - this.lightsTest = lightsTest; - - if (lightsTest) { - if (lightsTestChanged && this.valueText != null) { - this.valueText.textContent = "8888"; - } - return; - } - - const code = SimVar.GetSimVarValue("TRANSPONDER CODE:1", "number"); - if (code != this.currentCode || lightsTestChanged) { - this.currentCode = code; - this.currentDigits = [Math.floor(code / 1000), Math.floor((code % 1000) / 100), Math.floor((code % 100) / 10), code % 10]; - this.refreshValue(); - } - } - refreshValue() { - let code = ""; - for (let i = 0; i <= 3; i++) { - if (this.currentDigits[i] >= 0) { - code += this.currentDigits[i].toString(); - } else { - code += this.emptySlot; - } - } - if (this.valueText != null) { - this.valueText.textContent = code; - } - } - onEvent(_event) { - if (_event.indexOf("BTN_") >= 0) { - const buttonSuffix = _event.replace("BTN_", ""); - if (buttonSuffix.charAt(0) == 'C') { - if (this.currentDigits[0] >= 0) { - if (this.bLastInputIsCLR) { - for (var i = 3; i >= 0; i--) { - this.currentDigits[i] = -1; - } - } else { - for (var i = 3; i >= 0; i--) { - if (this.currentDigits[i] >= 0) { - this.currentDigits[i] = -1; - this.bLastInputIsCLR = true; - break; - } - } - } - this.refreshValue(); - } - } else if (buttonSuffix.charAt(0) == 'I') { - return; - } else { - let slot = -1; - { - for (var i = 0; i <= 3; i++) { - if (this.currentDigits[i] < 0) { - slot = i; - break; - } - } - } - if (slot < 0) { - for (var i = 0; i <= 3; i++) { - this.currentDigits[i] = -1; - } - slot = 0; - } - const buttonNumber = parseInt(buttonSuffix); - this.currentDigits[slot] = buttonNumber; - this.refreshValue(); - if (slot == 3 && this.currentDigits[3] >= 0) { - const code = (this.currentDigits[0] * 4096) + (this.currentDigits[1] * 256) + (this.currentDigits[2] * 16) + this.currentDigits[3]; - SimVar.SetSimVarValue("K:XPNDR_SET", "Bco16", code); - } - this.bLastInputIsCLR = false; - } - } - } - } - Airliners.BaseATC = BaseATC; - let PopupMenu_ItemType; - (function (PopupMenu_ItemType) { - PopupMenu_ItemType[PopupMenu_ItemType["TITLE"] = 0] = "TITLE"; - PopupMenu_ItemType[PopupMenu_ItemType["LIST"] = 1] = "LIST"; - PopupMenu_ItemType[PopupMenu_ItemType["RANGE"] = 2] = "RANGE"; - PopupMenu_ItemType[PopupMenu_ItemType["RADIO"] = 3] = "RADIO"; - PopupMenu_ItemType[PopupMenu_ItemType["RADIO_LIST"] = 4] = "RADIO_LIST"; - PopupMenu_ItemType[PopupMenu_ItemType["RADIO_RANGE"] = 5] = "RADIO_RANGE"; - PopupMenu_ItemType[PopupMenu_ItemType["SUBMENU"] = 6] = "SUBMENU"; - PopupMenu_ItemType[PopupMenu_ItemType["CHECKBOX"] = 7] = "CHECKBOX"; - })(PopupMenu_ItemType || (PopupMenu_ItemType = {})); - class PopupMenu_Item { - constructor(_type, _section, _y, _height) { - this.y = 0; - this.height = 0; - this.listVal = 0; - this.rangeMin = 0; - this.rangeMax = 0; - this.rangeStep = 0; - this.rangeDecimals = 0; - this.rangeVal = 0; - this.radioVal = false; - this.checkboxVal = false; - this.type = _type; - this.section = _section; - this.y = _y; - this.height = _height; - } - get interactive() { - if (this.type != PopupMenu_ItemType.TITLE) { - return true; - } - return false; - } - get enabled() { - if (this.dictKeys != null || this.subMenu) { - return true; - } - return false; - } - } - class PopupMenu_Section { - constructor() { - this.items = new Array(); - this.startY = 0; - this.endY = 0; - this.interactionColor = ""; - this.defaultRadio = true; - } - } - class PopupMenu_Handler { - constructor() { - this.menuLeft = 0; - this.menuTop = 0; - this.menuWidth = 0; - this.columnLeft1 = 3; - this.columnLeft2 = 20; - this.columnLeft3 = 90; - this.lineHeight = 18; - this.sectionBorderSize = 1; - this.textStyle = "Roboto-Regular"; - this.textMarginX = 3; - this.highlightColor = "cyan"; - this.interactionColor = "cyan"; - this.disabledColor = "grey"; - this.shapeFillColor = "none"; - this.shapeFillIfDisabled = true; - this.shape3D = false; - this.shape3DBorderSize = 3; - this.shape3DBorderLeft = "rgb(100, 100, 100)"; - this.shape3DBorderRight = "rgb(30, 30, 30)"; - this.highlightId = 0; - this.speedInc = 1.0; - this.speedInc_UpFactor = 0.25; - this.speedInc_DownFactor = 0.075; - this.speedInc_PowFactor = 0.9; - } - get height() { - let height = 0; - for (let i = 0; i < this.allSections.length; i++) { - height += this.allSections[i].endY - this.allSections[i].startY; - } - return height; - } - highlight(_index) { - if (_index >= 0) { - this.highlightId = _index; - } - } - reset() { - } - onUpdate(_dTime) { - this.updateHighlight(); - this.updateSpeedInc(); - } - onActivate() { - if (this.highlightItem && this.highlightItem.enabled) { - switch (this.highlightItem.type) { - case PopupMenu_ItemType.RADIO: - case PopupMenu_ItemType.RADIO_LIST: - case PopupMenu_ItemType.RADIO_RANGE: - let changed = false; - const section = this.highlightItem.section; - for (let i = 0; i < section.items.length; i++) { - const item = section.items[i]; - if (item.radioElem) { - if (item == this.highlightItem) { - if (!item.radioVal) { - this.activateItem(item, true); - changed = true; - } else if (!section.defaultRadio) { - this.activateItem(item, false); - changed = true; - } - } else if (item.radioVal) { - this.activateItem(item, false); - } - } - } - if (changed) { - this.onChanged(this.highlightItem); - } - break; - case PopupMenu_ItemType.SUBMENU: - this.highlightItem.subMenu(); - break; - case PopupMenu_ItemType.CHECKBOX: - if (!this.highlightItem.checkboxVal) { - this.activateItem(this.highlightItem, true); - } else { - this.activateItem(this.highlightItem, false); - this.highlightItem.checkboxVal = false; - } - this.onChanged(this.highlightItem); - break; - } - } - } - onDataDec() { - if (this.highlightItem && this.highlightItem.enabled) { - switch (this.highlightItem.type) { - case PopupMenu_ItemType.LIST: - case PopupMenu_ItemType.RADIO_LIST: - if (this.highlightItem.listVal > 0) { - this.highlightItem.listVal--; - this.highlightItem.listElem.textContent = this.highlightItem.listValues[this.highlightItem.listVal]; - this.onChanged(this.highlightItem); - } - break; - case PopupMenu_ItemType.RANGE: - case PopupMenu_ItemType.RADIO_RANGE: - if (this.highlightItem.rangeVal > this.highlightItem.rangeMin) { - this.highlightItem.rangeVal -= this.highlightItem.rangeStep * this.getSpeedAccel(); - this.highlightItem.rangeVal = Math.max(this.highlightItem.rangeVal, this.highlightItem.rangeMin); - this.highlightItem.rangeElem.textContent = this.highlightItem.rangeVal.toFixed(this.highlightItem.rangeDecimals); - this.onChanged(this.highlightItem); - this.speedInc += this.speedInc_UpFactor; - } - break; - } - } - } - onDataInc() { - if (this.highlightItem && this.highlightItem.enabled) { - switch (this.highlightItem.type) { - case PopupMenu_ItemType.LIST: - case PopupMenu_ItemType.RADIO_LIST: - if (this.highlightItem.listVal < this.highlightItem.listValues.length - 1) { - this.highlightItem.listVal++; - this.highlightItem.listElem.textContent = this.highlightItem.listValues[this.highlightItem.listVal]; - this.onChanged(this.highlightItem); - } - break; - case PopupMenu_ItemType.RANGE: - case PopupMenu_ItemType.RADIO_RANGE: - if (this.highlightItem.rangeVal < this.highlightItem.rangeMax) { - this.highlightItem.rangeVal += this.highlightItem.rangeStep * this.getSpeedAccel(); - this.highlightItem.rangeVal = Math.min(this.highlightItem.rangeVal, this.highlightItem.rangeMax); - this.highlightItem.rangeElem.textContent = this.highlightItem.rangeVal.toFixed(this.highlightItem.rangeDecimals); - this.onChanged(this.highlightItem); - this.speedInc += this.speedInc_UpFactor; - } - break; - } - } - } - onMenuDec() { - if (this.highlightId > 0) { - this.highlightId--; - } - } - onMenuInc() { - this.highlightId++; - } - onEscape() { - if (this.escapeCbk) { - this.escapeCbk(); - } - } - openMenu() { - this.allSections = []; - this.sectionRoot = null; - this.highlightItem = null; - this.highlightId = 0; - this.escapeCbk = null; - this.sectionRoot = document.createElementNS(Avionics.SVG.NS, "g"); - this.sectionRoot.setAttribute("transform", "translate(" + this.menuLeft + " " + this.menuTop + ")"); - return this.sectionRoot; - } - closeMenu() { - const bg = document.createElementNS(Avionics.SVG.NS, "rect"); - bg.setAttribute("x", "0"); - bg.setAttribute("y", "0"); - bg.setAttribute("width", this.menuWidth.toString()); - bg.setAttribute("height", this.height.toString()); - bg.setAttribute("fill", "black"); - this.sectionRoot.insertBefore(bg, this.sectionRoot.firstChild); - this.highlightElem = document.createElementNS(Avionics.SVG.NS, "rect"); - this.highlightElem.setAttribute("x", "0"); - this.highlightElem.setAttribute("y", "0"); - this.highlightElem.setAttribute("width", this.menuWidth.toString()); - this.highlightElem.setAttribute("height", this.lineHeight.toString()); - this.highlightElem.setAttribute("fill", "none"); - this.highlightElem.setAttribute("stroke", this.highlightColor); - this.highlightElem.setAttribute("stroke-width", (this.sectionBorderSize + 1).toString()); - this.sectionRoot.appendChild(this.highlightElem); - if (this.dictionary) { - this.dictionary.changed = false; - } - } - beginSection(_defaultRadio = true) { - this.section = new PopupMenu_Section(); - this.section.interactionColor = this.interactionColor; - this.section.defaultRadio = _defaultRadio; - if (this.allSections.length > 0) { - this.section.startY = this.allSections[this.allSections.length - 1].endY; - this.section.endY = this.section.startY; - } - } - endSection() { - const stroke = document.createElementNS(Avionics.SVG.NS, "rect"); - stroke.setAttribute("x", "0"); - stroke.setAttribute("y", this.section.startY.toString()); - stroke.setAttribute("width", this.menuWidth.toString()); - stroke.setAttribute("height", (this.section.endY - this.section.startY).toString()); - stroke.setAttribute("fill", "none"); - stroke.setAttribute("stroke", "white"); - stroke.setAttribute("stroke-width", this.sectionBorderSize.toString()); - this.sectionRoot.appendChild(stroke); - let defaultRadio = null; - for (let i = 0; i < this.section.items.length; i++) { - const item = this.section.items[i]; - if (item.radioElem) { - if (this.dictionary && item.dictKeys && this.dictionary.exists(item.dictKeys[0])) { - if (this.dictionary.get(item.dictKeys[0]) == item.radioName) { - defaultRadio = item; - break; - } - } else if (!defaultRadio && this.section.defaultRadio) { - defaultRadio = item; - } - } - } - for (let i = 0; i < this.section.items.length; i++) { - const item = this.section.items[i]; - let dictIndex = 0; - let changed = false; - if (item.radioElem) { - if (item == defaultRadio) { - this.activateItem(item, true); - changed = true; - } - dictIndex++; - } - if (item.listElem) { - item.listVal = 0; - if (this.dictionary && item.dictKeys && this.dictionary.exists(item.dictKeys[dictIndex])) { - const value = this.dictionary.get(item.dictKeys[dictIndex]); - for (let j = 0; j < item.listValues.length; j++) { - if (item.listValues[j] == value) { - item.listVal = j; - break; - } - } - } - item.listElem.textContent = item.listValues[item.listVal]; - changed = true; - } - if (item.rangeElem) { - item.rangeVal = item.rangeMin; - if (this.dictionary && item.dictKeys && this.dictionary.exists(item.dictKeys[dictIndex])) { - item.rangeVal = parseFloat(this.dictionary.get(item.dictKeys[dictIndex])); - item.rangeVal = Math.max(item.rangeMin, Math.min(item.rangeVal, item.rangeMax)); - } - item.rangeElem.textContent = item.rangeVal.toFixed(item.rangeDecimals); - changed = true; - } - if (item.checkboxElem) { - if (this.dictionary && item.dictKeys && this.dictionary.exists(item.dictKeys[dictIndex])) { - if (this.dictionary.get(item.dictKeys[0]) == "ON") { - this.activateItem(item, true); - changed = true; - } - } - } - if (changed) { - this.onChanged(item); - } - } - this.allSections.push(this.section); - this.section = null; - } - addTitle(_text, _textSize, _bgFactor, _bgColor = "blue", _showEscapeIcon = false) { - const bg = document.createElementNS(Avionics.SVG.NS, "rect"); - bg.setAttribute("x", "0"); - bg.setAttribute("y", this.section.endY.toString()); - bg.setAttribute("width", (this.menuWidth * _bgFactor).toString()); - bg.setAttribute("height", this.lineHeight.toString()); - bg.setAttribute("fill", _bgColor); - this.sectionRoot.appendChild(bg); - let posX = this.columnLeft1; - if (_showEscapeIcon) { - const arrow = document.createElementNS(Avionics.SVG.NS, "path"); - arrow.setAttribute("d", "M" + posX + " " + (this.section.endY + 2) + " l0 " + (this.lineHeight * 0.38) + " l13 0 l0 -2 l2 2 l-2 2 l0 -2"); - arrow.setAttribute("fill", "none"); - arrow.setAttribute("stroke", "white"); - arrow.setAttribute("stroke-width", "1.5"); - this.sectionRoot.appendChild(arrow); - posX += 20; - } - const text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = _text; - text.setAttribute("x", posX.toString()); - text.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", _textSize.toString()); - text.setAttribute("font-family", this.textStyle); - text.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(text); - const item = new PopupMenu_Item(PopupMenu_ItemType.TITLE, this.section, this.section.endY, this.lineHeight); - this.section.items.push(item); - this.section.endY += this.lineHeight; - } - addList(_text, _textSize, _values, _dictKeys) { - const enabled = (_dictKeys != null) ? true : false; - const text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = _text; - text.setAttribute("x", (this.columnLeft2 + this.textMarginX).toString()); - text.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - text.setAttribute("fill", (enabled) ? "white" : this.disabledColor); - text.setAttribute("font-size", _textSize.toString()); - text.setAttribute("font-family", this.textStyle); - text.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(text); - const hl = document.createElementNS(Avionics.SVG.NS, "rect"); - hl.setAttribute("x", (this.columnLeft3 - 2).toString()); - hl.setAttribute("y", (this.section.endY + 2).toString()); - hl.setAttribute("width", (this.menuWidth - 2 - (this.columnLeft3 - 2)).toString()); - hl.setAttribute("height", ((this.section.endY + this.lineHeight - 2) - (this.section.endY + 2)).toString()); - hl.setAttribute("fill", (enabled) ? this.interactionColor : this.disabledColor); - hl.setAttribute("visibility", "hidden"); - this.sectionRoot.appendChild(hl); - const choice = document.createElementNS(Avionics.SVG.NS, "text"); - choice.textContent = _values[0]; - choice.setAttribute("x", this.columnLeft3.toString()); - choice.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - choice.setAttribute("fill", (enabled) ? this.interactionColor : this.disabledColor); - choice.setAttribute("font-size", _textSize.toString()); - choice.setAttribute("font-family", this.textStyle); - choice.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(choice); - const item = new PopupMenu_Item(PopupMenu_ItemType.LIST, this.section, this.section.endY, this.lineHeight); - item.dictKeys = _dictKeys; - item.listElem = choice; - item.listValues = _values; - item.listHLElem = hl; - this.section.items.push(item); - this.section.endY += this.lineHeight; - } - addRange(_text, _textSize, _min, _max, _step, _dictKeys) { - const enabled = (_dictKeys != null) ? true : false; - const text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = _text; - text.setAttribute("x", (this.columnLeft2 + this.textMarginX).toString()); - text.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - text.setAttribute("fill", (enabled) ? "white" : this.disabledColor); - text.setAttribute("font-size", _textSize.toString()); - text.setAttribute("font-family", this.textStyle); - text.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(text); - const hl = document.createElementNS(Avionics.SVG.NS, "rect"); - hl.setAttribute("x", (this.columnLeft3 - 2).toString()); - hl.setAttribute("y", (this.section.endY + 2).toString()); - hl.setAttribute("width", (this.menuWidth - 2 - (this.columnLeft3 - 2)).toString()); - hl.setAttribute("height", ((this.section.endY + this.lineHeight - 2) - (this.section.endY + 2)).toString()); - hl.setAttribute("fill", (enabled) ? this.interactionColor : this.disabledColor); - hl.setAttribute("visibility", "hidden"); - this.sectionRoot.appendChild(hl); - const range = document.createElementNS(Avionics.SVG.NS, "text"); - range.setAttribute("x", this.columnLeft3.toString()); - range.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - range.setAttribute("fill", (enabled) ? this.interactionColor : this.disabledColor); - range.setAttribute("font-size", _textSize.toString()); - range.setAttribute("font-family", this.textStyle); - range.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(range); - const item = new PopupMenu_Item(PopupMenu_ItemType.RANGE, this.section, this.section.endY, this.lineHeight); - item.dictKeys = _dictKeys; - item.rangeElem = range; - item.rangeHLElem = hl; - item.rangeMin = _min; - item.rangeMax = _max; - item.rangeVal = _min; - item.rangeStep = _step; - item.rangeDecimals = Utils.countDecimals(_step); - this.section.items.push(item); - this.section.endY += this.lineHeight; - } - addRadio(_text, _textSize, _dictKeys) { - const enabled = (_dictKeys != null) ? true : false; - const cx = this.columnLeft1 + (this.columnLeft2 - this.columnLeft1) * 0.5; - const cy = this.section.endY + this.lineHeight * 0.5; - let shape; - if (this.shape3D) { - const b = this.shape3DBorderSize; - const h = Math.min(this.lineHeight, this.columnLeft2) * 0.8; - const w = h * 0.75; - if (enabled) { - const leftBorder = document.createElementNS(Avionics.SVG.NS, "path"); - leftBorder.setAttribute("d", "M" + (cx) + " " + (cy - h * 0.5) + " l" + (-w * 0.5) + " " + (h * 0.5) + " l" + (w * 0.5) + " " + (h * 0.5) + " l0" + (-b) + " l" + (-w * 0.5 + b) + " " + (-h * 0.5 + b) + " l" + (w * 0.5 - b) + " " + (-h * 0.5 + b) + " Z"); - leftBorder.setAttribute("fill", this.shape3DBorderLeft); - this.sectionRoot.appendChild(leftBorder); - const rightBorder = document.createElementNS(Avionics.SVG.NS, "path"); - rightBorder.setAttribute("d", "M" + (cx) + " " + (cy - h * 0.5) + " l" + (w * 0.5) + " " + (h * 0.5) + " l" + (-w * 0.5) + " " + (h * 0.5) + " l0" + (-b) + " l" + (w * 0.5 - b) + " " + (-h * 0.5 + b) + " l" + (-w * 0.5 + b) + " " + (-h * 0.5 + b) + " Z"); - rightBorder.setAttribute("fill", this.shape3DBorderRight); - this.sectionRoot.appendChild(rightBorder); - } - shape = document.createElementNS(Avionics.SVG.NS, "path"); - shape.setAttribute("d", "M" + (cx) + " " + (cy - h * 0.5 + b) + " L" + (cx - w * 0.5 + b) + " " + (cy) + " L" + (cx) + " " + (cy + h * 0.5 - b) + " L" + (cx + w * 0.5 - b) + " " + (cy) + " Z"); - shape.setAttribute("fill", (enabled) ? this.shapeFillColor : ((this.shapeFillIfDisabled) ? this.disabledColor : "none")); - if (!enabled) { - shape.setAttribute("stroke", this.disabledColor); - shape.setAttribute("stroke-width", "1"); - } - this.sectionRoot.appendChild(shape); - } else { - const size = Math.min(this.lineHeight, this.columnLeft2) * 0.33; - shape = document.createElementNS(Avionics.SVG.NS, "circle"); - shape.setAttribute("cx", cx.toString()); - shape.setAttribute("cy", cy.toString()); - shape.setAttribute("r", size.toString()); - shape.setAttribute("fill", (enabled) ? this.shapeFillColor : ((this.shapeFillIfDisabled) ? this.disabledColor : "none")); - shape.setAttribute("stroke", (enabled) ? "white" : this.disabledColor); - shape.setAttribute("stroke-width", "1"); - this.sectionRoot.appendChild(shape); - } - const text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = _text; - text.setAttribute("x", (this.columnLeft2 + this.textMarginX).toString()); - text.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - text.setAttribute("fill", (enabled) ? "white" : this.disabledColor); - text.setAttribute("font-size", _textSize.toString()); - text.setAttribute("font-family", this.textStyle); - text.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(text); - const item = new PopupMenu_Item(PopupMenu_ItemType.RADIO, this.section, this.section.endY, this.lineHeight); - item.dictKeys = _dictKeys; - item.radioElem = shape; - item.radioName = _text; - this.section.items.push(item); - this.registerWithMouse(item); - this.section.endY += this.lineHeight; - } - addRadioList(_text, _textSize, _values, _dictKeys) { - const enabled = (_dictKeys != null) ? true : false; - const cx = this.columnLeft1 + (this.columnLeft2 - this.columnLeft1) * 0.5; - const cy = this.section.endY + this.lineHeight * 0.5; - let shape; - if (this.shape3D) { - const b = this.shape3DBorderSize; - const h = Math.min(this.lineHeight, this.columnLeft2) * 0.8; - const w = h * 0.75; - if (enabled) { - const leftBorder = document.createElementNS(Avionics.SVG.NS, "path"); - leftBorder.setAttribute("d", "M" + (cx) + " " + (cy - h * 0.5) + " l" + (-w * 0.5) + " " + (h * 0.5) + " l" + (w * 0.5) + " " + (h * 0.5) + " l0" + (-b) + " l" + (-w * 0.5 + b) + " " + (-h * 0.5 + b) + " l" + (w * 0.5 - b) + " " + (-h * 0.5 + b) + " Z"); - leftBorder.setAttribute("fill", this.shape3DBorderLeft); - this.sectionRoot.appendChild(leftBorder); - const rightBorder = document.createElementNS(Avionics.SVG.NS, "path"); - rightBorder.setAttribute("d", "M" + (cx) + " " + (cy - h * 0.5) + " l" + (w * 0.5) + " " + (h * 0.5) + " l" + (-w * 0.5) + " " + (h * 0.5) + " l0" + (-b) + " l" + (w * 0.5 - b) + " " + (-h * 0.5 + b) + " l" + (-w * 0.5 + b) + " " + (-h * 0.5 + b) + " Z"); - rightBorder.setAttribute("fill", this.shape3DBorderRight); - this.sectionRoot.appendChild(rightBorder); - } - shape = document.createElementNS(Avionics.SVG.NS, "path"); - shape.setAttribute("d", "M" + (cx) + " " + (cy - h * 0.5 + b) + " L" + (cx - w * 0.5 + b) + " " + (cy) + " L" + (cx) + " " + (cy + h * 0.5 - b) + " L" + (cx + w * 0.5 - b) + " " + (cy) + " Z"); - shape.setAttribute("fill", (enabled) ? this.shapeFillColor : ((this.shapeFillIfDisabled) ? this.disabledColor : "none")); - if (!enabled) { - shape.setAttribute("stroke", this.disabledColor); - shape.setAttribute("stroke-width", "1"); - } - this.sectionRoot.appendChild(shape); - } else { - const size = Math.min(this.lineHeight, this.columnLeft2) * 0.33; - shape = document.createElementNS(Avionics.SVG.NS, "circle"); - shape.setAttribute("cx", cx.toString()); - shape.setAttribute("cy", cy.toString()); - shape.setAttribute("r", size.toString()); - shape.setAttribute("fill", (enabled) ? this.shapeFillColor : ((this.shapeFillIfDisabled) ? this.disabledColor : "none")); - shape.setAttribute("stroke", (enabled) ? "white" : this.disabledColor); - shape.setAttribute("stroke-width", "1"); - this.sectionRoot.appendChild(shape); - } - const text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = _text; - text.setAttribute("x", (this.columnLeft2 + this.textMarginX).toString()); - text.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - text.setAttribute("fill", (enabled) ? "white" : this.disabledColor); - text.setAttribute("font-size", _textSize.toString()); - text.setAttribute("font-family", this.textStyle); - text.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(text); - const hl = document.createElementNS(Avionics.SVG.NS, "rect"); - hl.setAttribute("x", (this.columnLeft3 - 2).toString()); - hl.setAttribute("y", (this.section.endY + 2).toString()); - hl.setAttribute("width", (this.menuWidth - 2 - (this.columnLeft3 - 2)).toString()); - hl.setAttribute("height", ((this.section.endY + this.lineHeight - 2) - (this.section.endY + 2)).toString()); - hl.setAttribute("fill", this.interactionColor); - hl.setAttribute("visibility", "hidden"); - this.sectionRoot.appendChild(hl); - const choice = document.createElementNS(Avionics.SVG.NS, "text"); - choice.textContent = _values[0]; - choice.setAttribute("x", this.columnLeft3.toString()); - choice.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - choice.setAttribute("fill", (enabled) ? this.interactionColor : this.disabledColor); - choice.setAttribute("font-size", _textSize.toString()); - choice.setAttribute("font-family", this.textStyle); - choice.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(choice); - const item = new PopupMenu_Item(PopupMenu_ItemType.RADIO_LIST, this.section, this.section.endY, this.lineHeight); - item.dictKeys = _dictKeys; - item.radioElem = shape; - item.radioName = _text; - item.listElem = choice; - item.listHLElem = hl; - item.listValues = _values; - this.section.items.push(item); - this.registerWithMouse(item); - this.section.endY += this.lineHeight; - } - addRadioRange(_text, _textSize, _min, _max, _step, _dictKeys) { - const enabled = (_dictKeys != null) ? true : false; - const cx = this.columnLeft1 + (this.columnLeft2 - this.columnLeft1) * 0.5; - const cy = this.section.endY + this.lineHeight * 0.5; - let shape; - if (this.shape3D) { - const b = this.shape3DBorderSize; - const h = Math.min(this.lineHeight, this.columnLeft2) * 0.8; - const w = h * 0.75; - if (enabled) { - const leftBorder = document.createElementNS(Avionics.SVG.NS, "path"); - leftBorder.setAttribute("d", "M" + (cx) + " " + (cy - h * 0.5) + " l" + (-w * 0.5) + " " + (h * 0.5) + " l" + (w * 0.5) + " " + (h * 0.5) + " l0" + (-b) + " l" + (-w * 0.5 + b) + " " + (-h * 0.5 + b) + " l" + (w * 0.5 - b) + " " + (-h * 0.5 + b) + " Z"); - leftBorder.setAttribute("fill", this.shape3DBorderLeft); - this.sectionRoot.appendChild(leftBorder); - const rightBorder = document.createElementNS(Avionics.SVG.NS, "path"); - rightBorder.setAttribute("d", "M" + (cx) + " " + (cy - h * 0.5) + " l" + (w * 0.5) + " " + (h * 0.5) + " l" + (-w * 0.5) + " " + (h * 0.5) + " l0" + (-b) + " l" + (w * 0.5 - b) + " " + (-h * 0.5 + b) + " l" + (-w * 0.5 + b) + " " + (-h * 0.5 + b) + " Z"); - rightBorder.setAttribute("fill", this.shape3DBorderRight); - this.sectionRoot.appendChild(rightBorder); - } - shape = document.createElementNS(Avionics.SVG.NS, "path"); - shape.setAttribute("d", "M" + (cx) + " " + (cy - h * 0.5 + b) + " L" + (cx - w * 0.5 + b) + " " + (cy) + " L" + (cx) + " " + (cy + h * 0.5 - b) + " L" + (cx + w * 0.5 - b) + " " + (cy) + " Z"); - shape.setAttribute("fill", (enabled) ? this.shapeFillColor : ((this.shapeFillIfDisabled) ? this.disabledColor : "none")); - if (!enabled) { - shape.setAttribute("stroke", this.disabledColor); - shape.setAttribute("stroke-width", "1"); - } - this.sectionRoot.appendChild(shape); - } else { - const size = Math.min(this.lineHeight, this.columnLeft2) * 0.33; - shape = document.createElementNS(Avionics.SVG.NS, "circle"); - shape.setAttribute("cx", cx.toString()); - shape.setAttribute("cy", cy.toString()); - shape.setAttribute("r", size.toString()); - shape.setAttribute("fill", (enabled) ? this.shapeFillColor : ((this.shapeFillIfDisabled) ? this.disabledColor : "none")); - shape.setAttribute("stroke", (enabled) ? "white" : this.disabledColor); - shape.setAttribute("stroke-width", "1"); - this.sectionRoot.appendChild(shape); - } - const text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = _text; - text.setAttribute("x", (this.columnLeft2 + this.textMarginX).toString()); - text.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - text.setAttribute("fill", (enabled) ? "white" : this.disabledColor); - text.setAttribute("font-size", _textSize.toString()); - text.setAttribute("font-family", this.textStyle); - text.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(text); - const hl = document.createElementNS(Avionics.SVG.NS, "rect"); - hl.setAttribute("x", (this.columnLeft3 - 2).toString()); - hl.setAttribute("y", (this.section.endY + 2).toString()); - hl.setAttribute("width", (this.menuWidth - 2 - (this.columnLeft3 - 2)).toString()); - hl.setAttribute("height", ((this.section.endY + this.lineHeight - 2) - (this.section.endY + 2)).toString()); - hl.setAttribute("fill", this.interactionColor); - hl.setAttribute("visibility", "hidden"); - this.sectionRoot.appendChild(hl); - const range = document.createElementNS(Avionics.SVG.NS, "text"); - range.setAttribute("x", this.columnLeft3.toString()); - range.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - range.setAttribute("fill", (enabled) ? this.interactionColor : this.disabledColor); - range.setAttribute("font-size", _textSize.toString()); - range.setAttribute("font-family", this.textStyle); - range.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(range); - const item = new PopupMenu_Item(PopupMenu_ItemType.RADIO_RANGE, this.section, this.section.endY, this.lineHeight); - item.dictKeys = _dictKeys; - item.radioElem = shape; - item.radioName = _text; - item.rangeElem = range; - item.rangeHLElem = hl; - item.rangeMin = _min; - item.rangeMax = _max; - item.rangeStep = _step; - item.rangeDecimals = Utils.countDecimals(_step); - this.section.items.push(item); - this.registerWithMouse(item); - this.section.endY += this.lineHeight; - } - addCheckbox(_text, _textSize, _dictKeys) { - const enabled = (_dictKeys != null) ? true : false; - const size = Math.min(this.lineHeight, this.columnLeft2) * 0.66; - const cx = this.columnLeft1 + (this.columnLeft2 - this.columnLeft1) * 0.5; - const cy = this.section.endY + this.lineHeight * 0.5; - let shape; - if (this.shape3D && enabled) { - const b = this.shape3DBorderSize; - const topLeftBorder = document.createElementNS(Avionics.SVG.NS, "path"); - topLeftBorder.setAttribute("d", "M" + (cx - size * 0.5) + " " + (cy - size * 0.5) + " l" + (size) + " 0 l" + (-b) + " " + (b) + " l" + (-(size - b * 2)) + " 0 l0 " + (size - b * 2) + " l" + (-b) + " " + (b) + " Z"); - topLeftBorder.setAttribute("fill", this.shape3DBorderLeft); - this.sectionRoot.appendChild(topLeftBorder); - const bottomRightBorder = document.createElementNS(Avionics.SVG.NS, "path"); - bottomRightBorder.setAttribute("d", "M" + (cx + size * 0.5) + " " + (cy + size * 0.5) + " l" + (-size) + " 0 l" + (b) + " " + (-b) + " l" + (size - b * 2) + " 0 l0 " + (-(size - b * 2)) + " l" + (b) + " " + (-b) + " Z"); - bottomRightBorder.setAttribute("fill", this.shape3DBorderRight); - this.sectionRoot.appendChild(bottomRightBorder); - shape = document.createElementNS(Avionics.SVG.NS, "rect"); - shape.setAttribute("x", (cx - size * 0.5 + b).toString()); - shape.setAttribute("y", (cy - size * 0.5 + b).toString()); - shape.setAttribute("width", (size - b * 2).toString()); - shape.setAttribute("height", (size - b * 2).toString()); - shape.setAttribute("fill", this.shapeFillColor); - this.sectionRoot.appendChild(shape); - } else { - shape = document.createElementNS(Avionics.SVG.NS, "rect"); - shape.setAttribute("x", (cx - size * 0.5).toString()); - shape.setAttribute("y", (cy - size * 0.5).toString()); - shape.setAttribute("width", size.toString()); - shape.setAttribute("height", size.toString()); - shape.setAttribute("fill", (enabled) ? this.shapeFillColor : ((this.shapeFillIfDisabled) ? this.disabledColor : "none")); - shape.setAttribute("stroke", (enabled) ? "white" : this.disabledColor); - shape.setAttribute("stroke-width", "1"); - this.sectionRoot.appendChild(shape); - } - const tick = document.createElementNS(Avionics.SVG.NS, "path"); - tick.setAttribute("d", "M" + (cx - size * 0.5) + " " + (cy) + " l" + (size * 0.4) + " " + (size * 0.5) + " l" + (size * 0.6) + " " + (-size)); - tick.setAttribute("fill", "none"); - tick.setAttribute("stroke", this.interactionColor); - tick.setAttribute("stroke-width", "4"); - tick.setAttribute("visibility", "hidden"); - this.sectionRoot.appendChild(tick); - const text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = _text; - text.setAttribute("x", (this.columnLeft2 + this.textMarginX).toString()); - text.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - text.setAttribute("fill", (enabled) ? "white" : this.disabledColor); - text.setAttribute("font-size", _textSize.toString()); - text.setAttribute("font-family", this.textStyle); - text.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(text); - const item = new PopupMenu_Item(PopupMenu_ItemType.CHECKBOX, this.section, this.section.endY, this.lineHeight); - item.dictKeys = _dictKeys; - item.checkboxElem = shape; - item.checkboxTickElem = tick; - this.section.items.push(item); - this.registerWithMouse(item); - this.section.endY += this.lineHeight; - } - addSubMenu(_text, _textSize, _callback) { - const enabled = (_callback != null) ? true : false; - const size = Math.min(this.lineHeight, this.columnLeft2) * 0.66; - const cx = this.columnLeft1 + (this.columnLeft2 - this.columnLeft1) * 0.5; - const cy = this.section.endY + this.lineHeight * 0.5; - const arrow = document.createElementNS(Avionics.SVG.NS, "path"); - arrow.setAttribute("d", "M" + (cx - size * 0.5) + " " + (cy - size * 0.5) + " l0 " + (size) + " l" + (size * 0.75) + " " + (-size * 0.5) + " Z"); - arrow.setAttribute("fill", (enabled) ? this.interactionColor : ((this.shapeFillIfDisabled) ? this.disabledColor : "none")); - this.sectionRoot.appendChild(arrow); - const text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = _text; - text.setAttribute("x", (this.columnLeft2 + this.textMarginX).toString()); - text.setAttribute("y", (this.section.endY + this.lineHeight * 0.5).toString()); - text.setAttribute("fill", (enabled) ? "white" : this.disabledColor); - text.setAttribute("font-size", _textSize.toString()); - text.setAttribute("font-family", this.textStyle); - text.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(text); - const item = new PopupMenu_Item(PopupMenu_ItemType.SUBMENU, this.section, this.section.endY, this.lineHeight); - item.subMenu = _callback; - this.section.items.push(item); - this.registerWithMouse(item); - this.section.endY += this.lineHeight; - } - addButton(_text, _textSize, _callback) { - const enabled = (_callback != null) ? true : false; - const cx = this.menuLeft + (this.menuWidth - this.menuLeft) * 0.5; - const cy = this.section.endY + this.lineHeight * 0.5; - const w = this.menuWidth * 0.66; - const h = this.lineHeight * 0.66; - let shape; - if (this.shape3D && enabled) { - const b = this.shape3DBorderSize; - const topLeftBorder = document.createElementNS(Avionics.SVG.NS, "path"); - topLeftBorder.setAttribute("d", "M" + (cx - w * 0.5) + " " + (cy - h * 0.5) + " l" + (w) + " 0 l" + (-b) + " " + (b) + " l" + (-(w - b * 2)) + " 0 l0 " + (h - b * 2) + " l" + (-b) + " " + (b) + " Z"); - topLeftBorder.setAttribute("fill", this.shape3DBorderLeft); - this.sectionRoot.appendChild(topLeftBorder); - const bottomRightBorder = document.createElementNS(Avionics.SVG.NS, "path"); - bottomRightBorder.setAttribute("d", "M" + (cx + w * 0.5) + " " + (cy + h * 0.5) + " l" + (-w) + " 0 l" + (b) + " " + (-b) + " l" + (w - b * 2) + " 0 l0 " + (-(h - b * 2)) + " l" + (b) + " " + (-b) + " Z"); - bottomRightBorder.setAttribute("fill", this.shape3DBorderRight); - this.sectionRoot.appendChild(bottomRightBorder); - shape = document.createElementNS(Avionics.SVG.NS, "rect"); - shape.setAttribute("x", (cx - w * 0.5 + b).toString()); - shape.setAttribute("y", (cy - h * 0.5 + b).toString()); - shape.setAttribute("width", (w - b * 2).toString()); - shape.setAttribute("height", (h - b * 2).toString()); - shape.setAttribute("fill", this.shapeFillColor); - this.sectionRoot.appendChild(shape); - } else { - shape = document.createElementNS(Avionics.SVG.NS, "rect"); - shape.setAttribute("x", (cx - w * 0.5).toString()); - shape.setAttribute("y", (cy - h * 0.5).toString()); - shape.setAttribute("width", w.toString()); - shape.setAttribute("height", h.toString()); - shape.setAttribute("fill", (enabled) ? this.shapeFillColor : ((this.shapeFillIfDisabled) ? this.disabledColor : "none")); - shape.setAttribute("stroke", (enabled) ? "white" : this.disabledColor); - shape.setAttribute("stroke-width", "1"); - this.sectionRoot.appendChild(shape); - } - const text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = _text; - text.setAttribute("x", cx.toString()); - text.setAttribute("y", cy.toString()); - text.setAttribute("fill", (enabled) ? "white" : this.disabledColor); - text.setAttribute("font-size", _textSize.toString()); - text.setAttribute("font-family", this.textStyle); - text.setAttribute("text-anchor", "middle"); - text.setAttribute("alignment-baseline", "central"); - this.sectionRoot.appendChild(text); - const item = new PopupMenu_Item(PopupMenu_ItemType.SUBMENU, this.section, this.section.endY, this.lineHeight); - item.subMenu = _callback; - this.section.items.push(item); - this.registerWithMouse(item); - this.section.endY += this.lineHeight; - } - updateHighlight() { - if (this.highlightElem) { - let itemId = 0; - let lastItem; - for (let i = 0; i < this.allSections.length; i++) { - const section = this.allSections[i]; - for (let j = 0; j < section.items.length; j++) { - const item = section.items[j]; - if (item.interactive) { - if (itemId == this.highlightId) { - this.setHighlightedItem(item); - return true; - } - lastItem = item; - itemId++; - } - } - } - if (lastItem) { - this.highlightId = itemId - 1; - this.setHighlightedItem(lastItem); - } - } - } - setHighlightedItem(_item) { - if (_item != this.highlightItem) { - this.highlightItem = _item; - this.highlightElem.setAttribute("y", _item.y.toString()); - this.speedInc = 1.0; - } - } - activateItem(_item, _val) { - if (!_item.enabled) { - return; - } - switch (_item.type) { - case PopupMenu_ItemType.RADIO: - case PopupMenu_ItemType.RADIO_LIST: - case PopupMenu_ItemType.RADIO_RANGE: - if (_val) { - _item.radioVal = true; - _item.radioElem.setAttribute("fill", this.interactionColor); - } else { - _item.radioVal = false; - _item.radioElem.setAttribute("fill", this.shapeFillColor); - } - break; - case PopupMenu_ItemType.CHECKBOX: - if (_val) { - _item.checkboxVal = true; - _item.checkboxTickElem.setAttribute("visibility", "visible"); - } else { - _item.checkboxVal = false; - _item.checkboxTickElem.setAttribute("visibility", "hidden"); - } - break; - } - } - updateSpeedInc() { - if (this.highlightItem) { - if (this.speedInc > 1) { - this.speedInc -= this.speedInc_DownFactor; - if (this.speedInc < 1) { - this.speedInc = 1; - } - } - } else { - this.speedInc = 1.0; - } - } - getSpeedAccel() { - const pow = 1.0 + ((this.speedInc - 1.0) * this.speedInc_PowFactor); - const accel = Math.pow(this.speedInc, pow); - return Math.floor(accel); - } - onChanged(_item) { - if (this.dictionary && _item.enabled) { - switch (_item.type) { - case PopupMenu_ItemType.RADIO: - case PopupMenu_ItemType.RADIO_LIST: - case PopupMenu_ItemType.RADIO_RANGE: - let found = false; - for (let i = 0; i < _item.section.items.length; i++) { - if (_item.section.items[i].radioVal) { - this.dictionary.set(_item.dictKeys[0], _item.radioName); - found = true; - break; - } - } - if (!found) { - this.dictionary.remove(_item.dictKeys[0]); - } - break; - case PopupMenu_ItemType.LIST: - this.dictionary.set(_item.dictKeys[0], _item.listValues[_item.listVal]); - break; - case PopupMenu_ItemType.RANGE: - this.dictionary.set(_item.dictKeys[0], _item.rangeVal.toString()); - break; - case PopupMenu_ItemType.CHECKBOX: - this.dictionary.set(_item.dictKeys[0], (_item.checkboxVal) ? "ON" : "OFF"); - break; - } - switch (_item.type) { - case PopupMenu_ItemType.RADIO_LIST: - this.dictionary.set(_item.dictKeys[1], _item.listValues[_item.listVal]); - break; - case PopupMenu_ItemType.RADIO_RANGE: - this.dictionary.set(_item.dictKeys[1], _item.rangeVal.toString()); - break; - } - } - } - registerWithMouse(_item) { - const mouseFrame = document.createElementNS(Avionics.SVG.NS, "rect"); - mouseFrame.setAttribute("x", this.menuLeft.toString()); - mouseFrame.setAttribute("y", this.section.endY.toString()); - mouseFrame.setAttribute("width", this.menuWidth.toString()); - mouseFrame.setAttribute("height", this.lineHeight.toString()); - mouseFrame.setAttribute("fill", "none"); - mouseFrame.setAttribute("pointer-events", "visible"); - this.sectionRoot.appendChild(mouseFrame); - mouseFrame.addEventListener("mouseover", this.onMouseOver.bind(this, _item)); - mouseFrame.addEventListener("mouseup", this.onMousePress.bind(this, _item)); - } - onMouseOver(_item) { - if (_item.enabled) { - let itemId = 0; - for (let i = 0; i < this.allSections.length; i++) { - const section = this.allSections[i]; - for (let j = 0; j < section.items.length; j++) { - const item = section.items[j]; - if (item.interactive) { - if (item == _item) { - this.highlightId = itemId; - return; - } - itemId++; - } - } - } - } - } - onMousePress(_item) { - if (_item.enabled) { - this.onActivate(); - } - } - } - Airliners.PopupMenu_Handler = PopupMenu_Handler; -})(Airliners || (Airliners = {})); diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU.html b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU.html deleted file mode 100644 index 5c05322c049..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU.html +++ /dev/null @@ -1,176 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AirportsMonitor.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AirportsMonitor.js deleted file mode 100644 index 0f71f0521ff..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AirportsMonitor.js +++ /dev/null @@ -1,287 +0,0 @@ -class CDUAirportsMonitor { - static ShowPage(mcdu, reset = false) { - mcdu.page.Current = mcdu.page.AirportsMonitor; - - // one delta t unit is about 1.5 ms it seems - const update_ival_ms = 1000; - - this.total_delta_t += mcdu._deltaTime; - - if (reset) { - this.user_ap = undefined; - this.frozen = false; - this.page2 = false; - this.total_delta_t = 0; - } - - // show an empty page while loading the airports - if (!this.icao1) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["CLOSEST AIRPORTS"], - ["LOADING..."], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - ]); - } - - // we want the closest 4 APs with unlimited distance - const max_num_ap = 4; - const max_dist_miles = 100000; - - if (this.total_delta_t >= update_ival_ms || !this.icao1) { - - const ap_line_batch = new SimVar.SimVarBatch("C:fs9gps:NearestAirportItemsNumber", "C:fs9gps:NearestAirportCurrentLine"); - ap_line_batch.add("C:fs9gps:NearestAirportSelectedLatitude", "degree latitude"); - ap_line_batch.add("C:fs9gps:NearestAirportSelectedLongitude", "degree longitude"); - ap_line_batch.add("C:fs9gps:NearestAirportCurrentICAO", "string", "string"); - ap_line_batch.add("C:fs9gps:NearestAirportCurrentDistance", "nautical miles", "number"); - ap_line_batch.add("C:fs9gps:NearestAirportCurrentTrueBearing", "degrees", "number"); - - this.lat = SimVar.GetSimVarValue("PLANE LATITUDE", "degree latitude"); - this.lon = SimVar.GetSimVarValue("PLANE LONGITUDE", "degree longitude"); - this.fob_kg = Simplane.getTotalFuel(); - this.fuel_flow_kg_per_s = SimVar.GetSimVarValue("ENG FUEL FLOW PPH SSL", "kilograms per second"); - SimVar.SetSimVarValue("C:fs9gps:NearestAirportCurrentLatitude", "degree latitude", this.lat); - SimVar.SetSimVarValue("C:fs9gps:NearestAirportCurrentLongitude", "degree longitude", this.lon); - SimVar.SetSimVarValue("C:fs9gps:NearestAirportMaximumItems", "number", max_num_ap); - SimVar.SetSimVarValue("C:fs9gps:NearestAirportMaximumDistance", "nautical miles", max_dist_miles); - - SimVar.GetSimVarArrayValues(ap_line_batch, function (_Values) { - // sometimes we get only one value, - // in which case the display will not be cleared and redrawn - if (_Values.length === 4) { - this.icao1 = _Values[0][2].substr(2).trim(); - this.icao2 = _Values[1][2].substr(2).trim(); - this.icao3 = _Values[2][2].substr(2).trim(); - this.icao4 = _Values[3][2].substr(2).trim(); - this.dist1 = Math.round(parseFloat(_Values[0][3])); - this.dist2 = Math.round(parseFloat(_Values[1][3])); - this.dist3 = Math.round(parseFloat(_Values[2][3])); - this.dist4 = Math.round(parseFloat(_Values[3][3])); - // values are jumpy! mitigated by slow update rate, as only about every 30th value is erroneous - this.magvar = SimVar.GetSimVarValue("MAGVAR", "degree"); - this.brng1 = Math.round(parseFloat(_Values[0][4]) - this.magvar); - this.brng2 = Math.round(parseFloat(_Values[1][4]) - this.magvar); - this.brng3 = Math.round(parseFloat(_Values[2][4]) - this.magvar); - this.brng4 = Math.round(parseFloat(_Values[3][4]) - this.magvar); - this.ll1 = new LatLong(_Values[0][0], _Values[0][1]); - this.ll2 = new LatLong(_Values[1][0], _Values[1][1]); - this.ll3 = new LatLong(_Values[2][0], _Values[2][1]); - this.ll4 = new LatLong(_Values[3][0], _Values[3][1]); - } - }.bind(this)); - - // logic from FlightPlanManager.js - this.gs = SimVar.GetSimVarValue("GPS GROUND SPEED", "knots"); - if (this.gs < 50) { - this.gs = 50; - } - - this.total_delta_t = 0; - } - - const s_per_h = 3600; - const s_per_d = s_per_h * 24; - - // ETA offset in seconds - const eta1_s = Math.round(this.dist1 / this.gs * s_per_h); - const eta2_s = Math.round(this.dist2 / this.gs * s_per_h); - const eta3_s = Math.round(this.dist3 / this.gs * s_per_h); - const eta4_s = Math.round(this.dist4 / this.gs * s_per_h); - - // absolute ETA in seconds - const utc_s = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - const utc1_s = (utc_s + eta1_s) % s_per_d; - const utc2_s = (utc_s + eta2_s) % s_per_d; - const utc3_s = (utc_s + eta3_s) % s_per_d; - const utc4_s = (utc_s + eta4_s) % s_per_d; - - // components for ETAs in HHMM format - const h1 = Math.floor(utc1_s / s_per_h) || 0; - const h2 = Math.floor(utc2_s / s_per_h) || 0; - const h3 = Math.floor(utc3_s / s_per_h) || 0; - const h4 = Math.floor(utc4_s / s_per_h) || 0; - const m1 = Math.floor(utc1_s % s_per_h / 60) || 0; - const m2 = Math.floor(utc2_s % s_per_h / 60) || 0; - const m3 = Math.floor(utc3_s % s_per_h / 60) || 0; - const m4 = Math.floor(utc4_s % s_per_h / 60) || 0; - - // ETA HHMM strings - this.eta1 = `${h1.toString().padStart(2, "0") || "00"}${m1.toString().padStart(2, "0") || "00"}`; - this.eta2 = `${h2.toString().padStart(2, "0") || "00"}${m2.toString().padStart(2, "0") || "00"}`; - this.eta3 = `${h3.toString().padStart(2, "0") || "00"}${m3.toString().padStart(2, "0") || "00"}`; - this.eta4 = `${h4.toString().padStart(2, "0") || "00"}${m4.toString().padStart(2, "0") || "00"}`; - - // EFOBs - this.efob1 = Math.max(0, (this.fob_kg - (this.fuel_flow_kg_per_s * eta1_s)) / 1000).toFixed(1); - this.efob2 = Math.max(0, (this.fob_kg - (this.fuel_flow_kg_per_s * eta2_s)) / 1000).toFixed(1); - this.efob3 = Math.max(0, (this.fob_kg - (this.fuel_flow_kg_per_s * eta3_s)) / 1000).toFixed(1); - this.efob4 = Math.max(0, (this.fob_kg - (this.fuel_flow_kg_per_s * eta4_s)) / 1000).toFixed(1); - - if (!this.user_ap) { - if (this.page2) { - this.user_ap_line = [""]; - } else { - this.user_ap_line = ["[\xa0\xa0][color]cyan", ""]; - } - } else if (this.total_delta_t === 0) { - // calculate values for user selected airport - SimVar.SetSimVarValue("C:fs9gps:GeoCalcLatitude1", "degree", this.lat); - SimVar.SetSimVarValue("C:fs9gps:GeoCalcLongitude1", "degree", this.lon); - SimVar.SetSimVarValue("C:fs9gps:GeoCalcLatitude2", "degree", this.user_ap.infos.lat); - SimVar.SetSimVarValue("C:fs9gps:GeoCalcLongitude2", "degree", this.user_ap.infos.long); - this.brng5 = Math.round(SimVar.GetSimVarValue("C:fs9gps:GeoCalcBearing", "degree") - this.magvar); - this.dist5 = Math.round(SimVar.GetSimVarValue("C:fs9gps:GeoCalcDistance", "nautical miles")); - const eta5_s = Math.round(this.dist5 / this.gs * s_per_h); - const utc5_s = (utc_s + eta5_s) % s_per_d; - const h5 = Math.floor(utc5_s / s_per_h) || 0; - const m5 = Math.floor(utc5_s % s_per_h / 60) || 0; - this.eta5 = `${h5.toString().padStart(2, "0") || "00"}${m5.toString().padStart(2, "0") || "00"}`; - this.efob5 = Math.max(0, (this.fob_kg - (this.fuel_flow_kg_per_s * eta5_s)) / 1000).toFixed(1); - if (this.page2) { - this.user_ap_line = [ - `${this.user_ap.infos.ident}[color]green`, - `---[color]green`, - `${this.efob5.toString().padStart(3, "\xa0")}[color]green` - ]; - } else { - this.user_ap_line = [ - `${this.user_ap.infos.ident}[color]green`, - `${this.eta5}[color]green`, - `${this.brng5.toString().padStart(3, "0")}° ${this.dist5.toString().padStart(5, "\xa0")}[color]green` - ]; - } - } - - // display data on MCDU - if (this.icao1) { - mcdu.clearDisplay(); - if (this.page2) { - mcdu.setTemplate([ - ["CLOSEST AIRPORTS"], - ["", "EFF WIND\xa0", "EFOB"], - [`${this.icao1}[color]green`, - `{small}[KTS]{end}{cyan}[\xa0\xa0\xa0]{end}`, - `${this.efob1.toString().padStart(3, "\xa0")}[color]green`], - [""], - [`${this.icao2}[color]green`, - `[\xa0\xa0\xa0][color]cyan`, - `${this.efob2.toString().padStart(3, "\xa0")}[color]green`], - [""], - [`${this.icao3}[color]green`, - `[\xa0\xa0\xa0][color]cyan`, - `${this.efob3.toString().padStart(3, "\xa0")}[color]green`], - [""], - [`${this.icao4}[color]green`, - `[\xa0\xa0\xa0][color]cyan`, - `${this.efob4.toString().padStart(3, "\xa0")}[color]green`], - [""], - this.user_ap_line, - ["", "", this.frozen ? "LIST FROZEN" : ""], - [""] - ]); - - // force spaces to emulate 4 columns - mcdu._labelElements[0][2].innerHTML = "   "; - for (let i = 0; i < (this.user_ap ? 5 : 4); i++) { - mcdu._lineElements[i][2].innerHTML = mcdu._lineElements[i][2].innerHTML.replace(/_/g, " "); - } - } - } - - // page refresh - if (!this.frozen || !this.icao1) { - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - CDUAirportsMonitor.ShowPage(mcdu,false); - }, mcdu.PageTimeout.Default); - } - - // user-selected 5th airport (only possible to set on page 1) - if (!this.page2) { - mcdu.onLeftInput[4] = (value, scratchpadCallback) => { - if (this.user_ap) { - if (value === FMCMainDisplay.clrValue) { - this.user_ap = undefined; - // trigger data update next frame - this.total_delta_t = update_ival_ms; - this.ShowPage(mcdu); - } - } else if (value !== '' && value !== FMCMainDisplay.clrValue) { - // GetAirportByIdent returns a Waypoint in the callback, - // which internally uses FacilityLoader (and further down calls Coherence) - mcdu.navigationDatabaseService.activeDatabase.searchAirport(value).then((ap_data) => { - if (ap_data) { - this.user_ap = ap_data; - // trigger data update next frame - this.total_delta_t = update_ival_ms; - this.frozen = false; - this.ShowPage(mcdu); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - scratchpadCallback(); - } - }).catch(console.error); - } - }; - } - - // toggle freeze - mcdu.onLeftInput[5] = () => { - this.frozen = !this.frozen; - this.page2 = false; - // trigger data update next frame - this.total_delta_t = update_ival_ms; - this.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - // EFOB/WIND - if (!this.page2) { - mcdu.onRightInput[5] = () => { - this.page2 = true; - this.frozen = true; - // trigger data update next frame - this.total_delta_t = update_ival_ms; - this.ShowPage(mcdu); - }; - } - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AirwaysFromWaypointPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AirwaysFromWaypointPage.js deleted file mode 100644 index 0862b1ae73d..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AirwaysFromWaypointPage.js +++ /dev/null @@ -1,235 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class A320_Neo_CDU_AirwaysFromWaypointPage { - static ShowPage(mcdu, reviseIndex, pendingAirway, lastIndex, forPlan, inAlternate) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AirwaysFromWaypointPage; - - /** @type {FlightPlan} */ - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - const waypoint = targetPlan.legElementAt(reviseIndex); - - const fpIsSec = forPlan >= Fmgc.FlightPlanIndex.FirstSecondary; - const fpIsTmpy = forPlan === Fmgc.FlightPlanIndex.Active && mcdu.flightPlanService.hasTemporary; - - let prevIcao = waypoint.definition.waypoint.databaseId; - let prevFpIndex = reviseIndex; - - if (!targetPlan.pendingAirways) { - mcdu.flightPlanService.startAirwayEntry(reviseIndex, forPlan, inAlternate); - } - - const rows = [["----"], [""], [""], [""], [""]]; - const subRows = [["VIA", ""], [""], [""], [""], [""]]; - const allRows = lastIndex ? A320_Neo_CDU_AirwaysFromWaypointPage._GetAllRows(targetPlan) : []; - - let rowBottomLine = [" { - mcdu.eraseTemporaryFlightPlan(() => { - CDULateralRevisionPage.ShowPage(mcdu, targetPlan.elementAt(reviseIndex), reviseIndex, forPlan, inAlternate); - }); - }; - - if (fpIsSec && targetPlan.pendingAirways && targetPlan.pendingAirways.elements.length > 0) { - rowBottomLine = [" { - targetPlan.pendingAirways.finalize(); // TODO replace with fps call (fms-v2) - - mcdu.updateConstraints(); - - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }; - } else if (fpIsTmpy && targetPlan.pendingAirways && targetPlan.pendingAirways.elements.length > 0) { - rowBottomLine = ["{ERASE[color]amber", "INSERT*[color]amber"]; - - mcdu.onLeftInput[5] = async () => { - mcdu.eraseTemporaryFlightPlan(() => { - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }; - - mcdu.onRightInput[5] = async () => { - mcdu.insertTemporaryFlightPlan(() => { - targetPlan.pendingAirways = undefined; - - mcdu.updateConstraints(); - - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }; - } - - let showInput = false; - for (let i = 0; i < rows.length; i++) { - if (allRows[i]) { - const [airwayIdent, termIdent, fromIcao, fromIndex] = allRows[i]; - rows[i] = [airwayIdent, termIdent]; - subRows[i] = ["\xa0VIA", "TO\xa0"]; - prevIcao = fromIcao; - prevFpIndex = fromIndex; - } else if (!showInput) { - showInput = true; - if (!pendingAirway) { - subRows[i] = ["\xa0VIA", ""]; - rows[i] = ["[\xa0\xa0\xa0][color]cyan", ""]; - - mcdu.onLeftInput[i] = async (value, scratchpadCallback) => { - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - - if (value.length > 0) { - const elements = targetPlan.pendingAirways.elements; - const tailElement = elements[elements.length - 1]; - - const lastFix = tailElement ? tailElement.to : targetPlan.legElementAt(prevFpIndex).terminationWaypoint(); - - const airway = await this._getAirway(mcdu, prevFpIndex, tailElement ? tailElement.airway : undefined, lastFix, value).catch(console.error); - - if (airway) { - const result = targetPlan.pendingAirways.thenAirway(airway); - - A320_Neo_CDU_AirwaysFromWaypointPage.ShowPage(mcdu, reviseIndex, airway, result ? 1 : -1, forPlan, inAlternate); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.awyWptMismatch); - scratchpadCallback(); - } - } - }; - } else if (pendingAirway) { - subRows[i] = ["\xa0VIA", "TO\xa0"]; - rows[i] = [`${pendingAirway.ident}[color]cyan`, "[\xa0\xa0\xa0][color]cyan"]; - - mcdu.onRightInput[i] = (value, scratchpadCallback) => { - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - - if (value.length > 0) { - Fmgc.WaypointEntryUtils.getOrCreateWaypoint(mcdu, value, false).then(/** @param wp {import('msfs-navdata').Fix | undefined} */ (wp) => { - if (wp) { - const result = targetPlan.pendingAirways.thenTo(wp); - - A320_Neo_CDU_AirwaysFromWaypointPage.ShowPage(mcdu, reviseIndex, undefined, result ? 1 : -1, forPlan, inAlternate); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.awyWptMismatch); - scratchpadCallback(); - } - }); - } - }; - if (i + 1 < rows.length) { - rows[i + 1] = ["[\xa0\xa0\xa0][color]cyan", ""]; - subRows[i + 1] = ["\xa0VIA", ""]; - - mcdu.onLeftInput[i + 1] = async (value, scratchpadCallback) => { - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - - if (value.length > 0) { - const airway = await this._getFirstIntersection(mcdu, pendingAirway, prevIcao, value).catch(console.error); - if (airway) { - const result = targetPlan.pendingAirways.thenAirway(airway); - - A320_Neo_CDU_AirwaysFromWaypointPage.ShowPage(mcdu, reviseIndex, airway, result ? 1 : -1, forPlan, inAlternate); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.awyWptMismatch); - scratchpadCallback(); - } - } - }; - } - } - } - } - - mcdu.setTemplate([ - ["AIRWAYS {small}FROM {end}{green}" + waypoint.ident + "{end}"], - subRows[0], - rows[0], - subRows[1], - rows[1], - subRows[2], - rows[2], - subRows[3], - rows[3], - subRows[4], - rows[4], - [""], - rowBottomLine - ]); - } - - /** - * @param plan {FlightPlan} - */ - static _GetAllRows(plan) { - const allRows = []; - const elements = plan.pendingAirways.elements; - - for (let i = 0; i < elements.length; i++) { - const element = elements[i]; - - if (element.to) { - allRows.push([`{cyan}${element.airway.ident}{end}`, `{cyan}${element.isAutoConnected ? '{small}' : '{big}'}${element.to.ident}{end}{end}`, element.to.databaseId, i]); - } - } - - return allRows; - } - - /** - * @param {import('msfs-navdata').Airway} lastAirway - * @param {import('msfs-navdata').Fix} lastFix - * - * @returns {Promise} - */ - static async _getAirway(mcdu, fromFpIndex, lastAirway, lastFix, value) { - const airways = await mcdu.navigationDatabase.searchAirway(value, lastFix); - - let matchingAirway = lastFix && airways.find((it) => it.fixes.some((fix) => fix.ident === lastFix.ident)); - if (!matchingAirway && lastAirway) { - matchingAirway = airways.find((it) => it.fixes.some((fix) => lastAirway.fixes.some((endFix) => endFix.databaseId === fix.databaseId))); - } - - return matchingAirway; - } - - /** - * Distance is measured in number of fixes, not a real distance unit. - * Searching around current fixes index. - * - * @param prevAirway {import('msfs-navdata').Airway} - */ - static async _getFirstIntersection(mcdu, prevAirway, prevAirwayFromIcao, nextAirwayIdent) { - const prevAirwayFixes = prevAirway.fixes; - - const prevAirwayStartIndex = prevAirwayFixes.findIndex(fix => fix.databaseId === prevAirwayFromIcao); - - if (prevAirwayStartIndex < 0) { - throw new Error(`Cannot find waypoint ${icao} in airway ${prevAirway.ident}`); - } - - for (let i = 0; i < prevAirwayFixes.length; i++) { - if ((prevAirwayStartIndex + i) < prevAirwayFixes.length) { - const airway = await this._getRoute(mcdu, nextAirwayIdent, prevAirwayFixes[prevAirwayStartIndex + i]).catch(console.error); - - if (airway) { - return airway; - } - } - if ((prevAirwayStartIndex - i) >= 0) { - const airway = await this._getRoute(mcdu, nextAirwayIdent, prevAirwayFixes[prevAirwayStartIndex - i]).catch(console.error); - - if (airway) { - return airway; - } - } - } - } - - static async _getRoute(mcdu, airwayName, fixOnAirway) { - const airways = await mcdu.navigationDatabase.searchAirway(airwayName, fixOnAirway); - const matchingAirway = airways.find((it) => it.fixes.some((fix) => fix.databaseId === fixOnAirway.databaseId)); - - return matchingAirway; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AvailableArrivalsPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AvailableArrivalsPage.js deleted file mode 100644 index 778f1c07259..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AvailableArrivalsPage.js +++ /dev/null @@ -1,674 +0,0 @@ -/* - * A32NX - * Copyright (C) 2020-2021 FlyByWire Simulations and its contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -const ApproachTypeOrder = Object.freeze({ - [Fmgc.ApproachType.Mls]: 0, - [Fmgc.ApproachType.MlsTypeA]: 1, - [Fmgc.ApproachType.MlsTypeBC]: 2, - [Fmgc.ApproachType.Ils]: 3, - [Fmgc.ApproachType.Gls]: 4, - [Fmgc.ApproachType.Igs]: 5, - [Fmgc.ApproachType.Loc]: 6, - [Fmgc.ApproachType.LocBackcourse]: 7, - [Fmgc.ApproachType.Lda]: 8, - [Fmgc.ApproachType.Sdf]: 9, - [Fmgc.ApproachType.Fms]: 10, - [Fmgc.ApproachType.Gps]: 11, - [Fmgc.ApproachType.Rnav]: 12, - [Fmgc.ApproachType.VorDme]: 13, - [Fmgc.ApproachType.Vortac]: 13, // VORTAC and VORDME are intentionally the same - [Fmgc.ApproachType.Vor]: 14, - [Fmgc.ApproachType.NdbDme]: 15, - [Fmgc.ApproachType.Ndb]: 16, - [Fmgc.ApproachType.Unknown]: 17, -}); - -const ArrivalPagination = Object.freeze( - { - ARR_PAGE: 3, - TRNS_PAGE: 2, - VIA_PAGE: 3, - } -); - -class CDUAvailableArrivalsPage { - static async ShowPage(mcdu, airport, pageCurrent = 0, starSelection = false, forPlan = Fmgc.FlightPlanIndex.Active, inAlternate = false) { - /** @type {BaseFlightPlan} */ - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - - const isTemporary = targetPlan.index === Fmgc.FlightPlanIndex.Temporary; - - const selectedApproachId = targetPlan.approach ? targetPlan.approach.databaseId : targetPlan.approach; - const selectedStarId = targetPlan.arrival ? targetPlan.arrival.databaseId : targetPlan.arrival; - const selectedTransitionId = targetPlan.arrivalEnrouteTransition ? targetPlan.arrivalEnrouteTransition.databaseId : targetPlan.arrivalEnrouteTransition; - - const flightPlanAccentColor = isTemporary ? "yellow" : "green"; - - const ilss = await mcdu.navigationDatabase.backendDatabase.getIlsAtAirport(targetPlan.destinationAirport.ident); - - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AvailableArrivalsPage; - let selectedApproachCell = "------"; - let selectedViasCell = "------"; - let selectedTransitionCell = "------"; - let selectedApproachCellColor = "white"; - let selectedViasCellColor = "white"; - let selectedTransitionCellColor = "white"; - - let viasPageLabel = ""; - let viasPageLine = ""; - - const selectedApproach = targetPlan.approach; - - if (selectedApproach && selectedApproach.ident) { - selectedApproachCell = Fmgc.ApproachUtils.shortApproachName(selectedApproach); - selectedApproachCellColor = flightPlanAccentColor; - - const selectedApproachTransition = targetPlan.approachVia; - const availableVias = targetPlan.availableApproachVias; - - if (availableVias.length === 0 || selectedApproachTransition === null) { - selectedViasCell = "NONE"; - selectedViasCellColor = flightPlanAccentColor; - } else if (selectedApproachTransition) { - selectedViasCell = selectedApproachTransition.ident; - selectedViasCellColor = flightPlanAccentColor; - } - } else if (!selectedApproach && targetPlan.destinationRunway) { - selectedApproachCell = Fmgc.RunwayUtils.runwayString(targetPlan.destinationRunway.ident); - selectedApproachCellColor = flightPlanAccentColor; - - // Runway-only approaches have no VIAs - selectedViasCell = "NONE"; - selectedViasCellColor = flightPlanAccentColor; - } - - let selectedStarCell = "------"; - let selectedStarCellColor = "white"; - - const selectedArrival = targetPlan.arrival; - const availableArrivals = targetPlan.availableArrivals; - - if (selectedArrival) { - selectedStarCell = selectedArrival.ident; - selectedStarCellColor = flightPlanAccentColor; - - const selectedTransition = targetPlan.arrivalEnrouteTransition; - const availableTransitions = selectedArrival.enrouteTransitions; - - if (selectedTransition) { - selectedTransitionCell = selectedTransition.ident; - selectedTransitionCellColor = flightPlanAccentColor; - } else if (availableTransitions.length === 0 || selectedTransition === null) { - selectedTransitionCell = "NONE"; - selectedTransitionCellColor = flightPlanAccentColor; - } - } else if (selectedArrival === null || availableArrivals.length === 0) { - selectedStarCell = "NONE"; - selectedStarCellColor = flightPlanAccentColor; - - selectedTransitionCell = "NONE"; - selectedTransitionCellColor = flightPlanAccentColor; - } - - /** - * @type {import('msfs-navdata').Approach[]} - */ - const approaches = targetPlan.availableApproaches; - - /** - * @type {import('msfs-navdata').Runway[]} - */ - const runways = targetPlan.availableDestinationRunways; - - // Sort the approaches in Honeywell's documented order - const sortedApproaches = approaches.slice() - // The A320 cannot fly TACAN approaches - .filter(({ type }) => type !== Fmgc.ApproachType.TACAN) - // Filter out approaches with no matching runway - // Approaches not going to a specific runway (i.e circling approaches are filtered out at DB level) - .filter((a) => !!runways.find((rw) => rw.ident === a.runwayIdent)) - // Sort the approaches in Honeywell's documented order, and alphabetical in between - .sort((a, b) => a.type != b.type ? ApproachTypeOrder[a.type] - ApproachTypeOrder[b.type] : a.ident.localeCompare(b.ident)) - .map((approach) => ({ approach })) - .concat( - // Runway-by-itself approaches - runways.slice().map((runway) => ({ runway })) - ); - const rows = [[""], [""], [""], [""], [""], [""], [""], [""]]; - - /** - * @type {({ arrival: import('msfs-navdata').Arrival, arrivalIndex: number })[]} - */ - const matchingArrivals = []; - - if (!starSelection) { - for (let i = 0; i < ArrivalPagination.ARR_PAGE; i++) { - const index = i + pageCurrent * ArrivalPagination.ARR_PAGE; - - const approachOrRunway = sortedApproaches[index]; - if (!approachOrRunway) { - break; - } - - const { approach, runway } = approachOrRunway; - if (approach) { - let runwayLength = '----'; - let runwayCourse = '---'; - - const isSelected = selectedApproach && selectedApproachId === approach.databaseId; - const color = isSelected && !isTemporary ? "green" : "cyan"; - - const runway = targetPlan.availableDestinationRunways.find((rw) => rw.ident === approach.runwayIdent); - if (runway) { - runwayLength = runway.length.toFixed(0); // TODO imperial length pin program - runwayCourse = Utils.leadingZeros(Math.round(runway.magneticBearing), 3); - - const finalLeg = approach.legs[approach.legs.length - 1]; - const matchingIls = approach.type === Fmgc.ApproachType.Ils ? ilss.find( - (ils) => finalLeg && finalLeg.recommendedNavaid && ils.databaseId === finalLeg.recommendedNavaid.databaseId - ) : undefined; - const hasIls = !!matchingIls; - const ilsText = hasIls ? `${matchingIls.ident.padStart(6)}/${matchingIls.frequency.toFixed(2)}` : ''; - - rows[2 * i] = [`{${color}}${ !isSelected ? "{" : "{sp}"}${Fmgc.ApproachUtils.shortApproachName(approach)}{end}`, "", `{sp}{sp}{sp}${runwayLength}{small}M{end}[color]${color}`]; - rows[2 * i + 1] = [`{${color}}{sp}{sp}{sp}${runwayCourse}${ilsText}{end}`]; - } - - mcdu.onLeftInput[i + 2] = async (_, scratchpadCallback) => { - // Clicking the already selected approach is not allowed - if (!isSelected) { - try { - await mcdu.flightPlanService.setApproach(approach.databaseId, forPlan, inAlternate); - - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, pageCurrent, false, forPlan, inAlternate); - }); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - - scratchpadCallback(); - } - }; - } else if (runway) { - const runwayLength = runway.length.toFixed(0); // TODO imperial length pin program - const runwayCourse = Utils.leadingZeros(Math.round(runway.magneticBearing), 3); - - const isSelected = !selectedApproach && targetPlan.destinationRunway && runway.databaseId === targetPlan.destinationRunway.databaseId; - const color = isSelected && !isTemporary ? "green" : "cyan"; - - rows[2 * i] = [`{${color}}${ !isSelected ? "{" : "{sp}"}${Fmgc.RunwayUtils.runwayString(runway.ident)}{end}`, "", `{sp}{sp}{sp}${runwayLength}{small}M{end}[color]${color}`]; - rows[2 * i + 1] = ["{sp}{sp}{sp}{sp}" + runwayCourse + "[color]cyan"]; - - mcdu.onLeftInput[i + 2] = async (_, scratchpadCallback) => { - // Clicking the already selected runway is not allowed - if (!isSelected) { - try { - await mcdu.flightPlanService.setApproach(undefined, forPlan, inAlternate); - await mcdu.flightPlanService.setDestinationRunway(runway.ident, forPlan, inAlternate); - - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, pageCurrent, false, forPlan, inAlternate); - }); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - - scratchpadCallback(); - } - }; - } - } - } else { - /** - * @type {import('msfs-navdata').Runway | undefined} - */ - const destinationRunway = targetPlan.destinationRunway; - - if (destinationRunway) { - const arrivals = [...targetPlan.availableArrivals].sort((a, b) => a.ident.localeCompare(b.ident)); - - for (let i = 0; i < arrivals.length; i++) { - const arrival = arrivals[i]; - - if (arrival.runwayTransitions.length) { - for (let j = 0; j < arrival.runwayTransitions.length; j++) { - const runwayTransition = arrival.runwayTransitions[j]; - if (runwayTransition) { - // Check if selectedRunway matches a transition on the approach (and also checks for Center runways) - if (runwayTransition.ident === destinationRunway.ident || (runwayTransition.ident.charAt(6) === 'B' && runwayTransition.ident.substring(4, 6) === destinationRunway.ident.substring(4, 6))) { - matchingArrivals.push({ arrival: arrival, arrivalIndex: i }); - } - } - } - } else { - //add the arrival even if it isn't runway specific - matchingArrivals.push({ arrival: arrival, arrivalIndex: i }); - } - } - } else { - for (let i = 0; i < targetPlan.availableArrivals.length; i++) { - const arrival = targetPlan.availableArrivals[i]; - matchingArrivals.push({ arrival: arrival, arrivalIndex: i }); - } - } - for (let i = 0; i < ArrivalPagination.ARR_PAGE; i++) { - let index = i + (pageCurrent * ArrivalPagination.ARR_PAGE); - if (index === 0) { - const isSelected = selectedArrival === null; - const color = isSelected && !isTemporary ? "green" : "cyan"; - - rows[2 * i] = [`{${color}}${!isSelected ? "{" : "{sp}"}${Labels.NO_STAR}{end}`]; - - if (!isSelected) { - mcdu.onLeftInput[i + 2] = async () => { - try { - await mcdu.flightPlanService.setArrival(null, forPlan, inAlternate); - - const availableVias = targetPlan.availableApproachVias; - - if (selectedApproach !== undefined && availableVias.length > 0) { - CDUAvailableArrivalsPage.ShowViasPage(mcdu, airport, 0, forPlan, inAlternate); - } else { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); - } - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); - }); - } - }; - } - } else { - index--; - if (matchingArrivals[index]) { - const star = matchingArrivals[index].arrival; - const starDatabaseId = matchingArrivals[index].arrival.databaseId; - const isSelected = selectedStarId === starDatabaseId; - const color = isSelected && !isTemporary ? "green" : "cyan"; - - rows[2 * i] = [`{${color}}${!isSelected ? "{" : "{sp}"}${star.ident}{end}`]; - - mcdu.onLeftInput[i + 2] = async (_, scratchpadCallback) => { - // Clicking the already selected star is not allowed - if (!isSelected) { - const destinationRunway = targetPlan.destinationRunway; - - const arrivalRunway = destinationRunway ? star.runwayTransitions.find(t => { - return t.ident === destinationRunway.ident; - }) : undefined; - - try { - if (arrivalRunway !== undefined) { - await mcdu.flightPlanService.setDestinationRunway(arrivalRunway.ident, forPlan, inAlternate); - } - - await mcdu.flightPlanService.setArrival(starDatabaseId, forPlan, inAlternate); - - const availableVias = targetPlan.availableApproachVias; - - if (selectedApproach !== undefined && availableVias.length > 0) { - CDUAvailableArrivalsPage.ShowViasPage(mcdu, airport, 0, forPlan, inAlternate); - } else { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); - } - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); - }); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - - scratchpadCallback(); - } - }; - } - } - } - - if (selectedArrival) { - if (selectedArrival.enrouteTransitions.length > 0) { - const isNoTransSelected = selectedTransitionId === null; - const color = isNoTransSelected && !isTemporary ? "green" : "cyan"; - - rows[0][1] = `${Labels.NO_TRANS}${!isNoTransSelected ? "}" : "{sp}"}[color]${color}`; - - mcdu.onRightInput[2] = async () => { - try { - await mcdu.flightPlanService.setArrivalEnrouteTransition(null, forPlan, inAlternate); - - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); - }); - } - }; - - for (let i = 0; i < ArrivalPagination.TRNS_PAGE; i++) { - const index = i + pageCurrent * ArrivalPagination.TRNS_PAGE; - - const transition = selectedArrival.enrouteTransitions[index]; - if (transition) { - const isSelected = selectedTransitionId === transition.databaseId; - const color = isSelected && !isTemporary ? "green" : "cyan"; - - rows[2 * (i + 1)][1] = `{${color}}${transition.ident}${!isSelected ? "}" : "{sp}"}{end}`; - - // Clicking the already selected transition is not allowed - mcdu.onRightInput[i + 3] = async (_, scratchpadCallback) => { - if (!isSelected) { - try { - await mcdu.flightPlanService.setArrivalEnrouteTransition(transition.databaseId, forPlan, inAlternate); - - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, pageCurrent, true, forPlan, inAlternate); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); - }); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - - scratchpadCallback(); - } - }; - } - } - } - } - - if (selectedApproach) { - const availableApproachVias = targetPlan.availableApproachVias; - - if (availableApproachVias.length > 0) { - viasPageLabel = "{sp}APPR"; - viasPageLine = " { - CDUAvailableArrivalsPage.ShowViasPage(mcdu, airport, 0, forPlan, inAlternate); - }; - } - } - } - - let bottomLine = [" { - mcdu.eraseTemporaryFlightPlan(() => { - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }; - mcdu.onRightInput[5] = async () => { - mcdu.insertTemporaryFlightPlan(() => { - mcdu.updateTowerHeadwind(); - mcdu.updateConstraints(); - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }; - } else { - mcdu.onLeftInput[5] = () => { - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }; - } - let up = false; - let down = false; - const maxPage = starSelection ? (selectedArrival ? Math.max(Math.ceil(selectedArrival.enrouteTransitions.length / ArrivalPagination.TRNS_PAGE) - 1, Math.ceil((matchingArrivals.length + 1) / ArrivalPagination.ARR_PAGE) - 1) : Math.ceil((matchingArrivals.length + 1) / ArrivalPagination.ARR_PAGE) - 1) : (pageCurrent, Math.ceil(sortedApproaches.length / ArrivalPagination.ARR_PAGE) - 1); - if (pageCurrent < maxPage) { - mcdu.onUp = () => { - pageCurrent++; - if (pageCurrent < 0) { - pageCurrent = 0; - } - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, pageCurrent, starSelection, forPlan, inAlternate); - }; - up = true; - } - if (pageCurrent > 0) { - mcdu.onDown = () => { - pageCurrent--; - if (pageCurrent < 0) { - pageCurrent = 0; - } - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, pageCurrent, starSelection, forPlan, inAlternate); - }; - down = true; - } - mcdu.setArrows(up, down, true, true); - mcdu.setTemplate([ - ["ARRIVAL {small}TO{end} {green}" + airport.ident + "{sp}{end}"], - ["{sp}APPR", "STAR{sp}", "{sp}VIA"], - [`{${selectedApproachCellColor}}${selectedApproachCell.padEnd(10)}{end}{${selectedViasCellColor}}${selectedViasCell}{end}`, selectedStarCell + "[color]" + selectedStarCellColor], - [viasPageLabel, "TRANS{sp}"], - [viasPageLine, selectedTransitionCell + "[color]" + selectedTransitionCellColor], - ["{big}" + (starSelection ? "STARS" : "APPR").padEnd(5) + "{end}{sp}{sp}AVAILABLE", starSelection ? "{big}TRANS{end}" : "", ""], - rows[0], - rows[1], - rows[2], - rows[3], - rows[4], - rows[5], - bottomLine - ]); - mcdu.onPrevPage = () => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, !starSelection, forPlan, inAlternate); - }; - mcdu.onNextPage = mcdu.onPrevPage; - - } - - static ShowViasPage(mcdu, airport, pageCurrent = 0, forPlan = Fmgc.FlightPlanIndex.Active, inAlternate = false) { - /** @type {BaseFlightPlan} */ - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - - const isTemporary = targetPlan.index === Fmgc.FlightPlanIndex.Temporary; - const planColor = isTemporary ? "yellow" : "green"; - - const availableApproachVias = targetPlan.availableApproachVias; - - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AvailableArrivalsPageVias; - let selectedApproachCell = "------"; - let selectedApproachCellColor = "white"; - let selectedViasCell = "------"; - let selectedViasCellColor = "white"; - - const selectedApproach = targetPlan.approach; - const selectedApproachVia = targetPlan.approachVia; - - if (selectedApproach) { - selectedApproachCell = Fmgc.ApproachUtils.shortApproachName(selectedApproach); - selectedApproachCellColor = planColor; - - if (selectedApproachVia === null) { - selectedViasCell = "NONE"; - selectedViasCellColor = planColor; - } else if (selectedApproachVia) { - selectedViasCell = selectedApproachVia.ident; - selectedViasCellColor = planColor; - } - } - - let selectedStarCell = "------"; - let selectedStarCellColor = "white"; - - const selectedArrival = targetPlan.arrival; - const availableArrivals = targetPlan.availableArrivals; - - if (selectedArrival) { - selectedStarCell = selectedArrival.ident; - selectedStarCellColor = planColor; - } else if (selectedArrival === null || availableArrivals.length === 0) { - selectedStarCell = "NONE"; - selectedStarCellColor = planColor; - } - - const rows = [[""], [""], [""], [""], [""], [""]]; - - for (let i = 0; i < ArrivalPagination.VIA_PAGE; i++) { - const index = i + pageCurrent * ArrivalPagination.VIA_PAGE; - const via = availableApproachVias[index]; - - if (selectedApproach && via) { - const isSelected = selectedApproachVia && via.databaseId === selectedApproachVia.databaseId; - const color = isSelected && !isTemporary ? "green" : "cyan"; - - rows[2 * i + 1][0] = `{${color}}${!isSelected ? "{" : "{sp}"}${via.ident}{end}`; - - mcdu.onLeftInput[i + 2] = async (_, scratchpadCallback) => { - // Clicking the already selected via is not allowed - if (!isSelected) { - try { - await mcdu.flightPlanService.setApproachVia(via.databaseId, forPlan, inAlternate); - - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); - }); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - - scratchpadCallback(); - } - }; - } - } - - let bottomLine = [" { - mcdu.eraseTemporaryFlightPlan(() => { - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }; - - mcdu.onRightInput[5] = async () => { - mcdu.insertTemporaryFlightPlan(() => { - mcdu.updateTowerHeadwind(); - mcdu.updateConstraints(); - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }; - } else { - mcdu.onLeftInput[5] = () => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); - }; - } - - const isNoViaSelected = selectedApproachVia === null; - const color = isNoViaSelected && !isTemporary ? "green" : "cyan"; - - mcdu.setTemplate([ - ["APPROACH VIAS"], - ["{sp}APPR", "STAR{sp}", "{sp}VIA"], - [`{${selectedApproachCellColor}}${selectedApproachCell.padEnd(10)}{end}{${selectedViasCellColor}}${selectedViasCell}{end}`, selectedStarCell + "[color]" + selectedStarCellColor], - ["APPR VIAS"], - [`${!isNoViaSelected ? "{" : "{sp}"}${Labels.NO_VIA}[color]${color}`], - rows[0], - rows[1], - rows[2], - rows[3], - rows[4], - rows[5], - rows[6], - bottomLine - ]); - mcdu.onLeftInput[1] = async () => { - try { - await mcdu.flightPlanService.setApproachVia(null, forPlan, inAlternate); - - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); - }); - } - }; - let up = false; - let down = false; - - if (pageCurrent < Math.ceil(selectedApproach.transitions.length / ArrivalPagination.VIA_PAGE) - 1) { - mcdu.onUp = () => { - pageCurrent++; - if (pageCurrent < 0) { - pageCurrent = 0; - } - CDUAvailableArrivalsPage.ShowViasPage(mcdu, airport, pageCurrent, forPlan, inAlternate); - }; - up = true; - } - if (pageCurrent > 0) { - mcdu.onDown = () => { - pageCurrent--; - if (pageCurrent < 0) { - pageCurrent = 0; - } - CDUAvailableArrivalsPage.ShowViasPage(mcdu, airport, pageCurrent, forPlan, inAlternate); - }; - down = true; - } - mcdu.setArrows(up, down, true, true); - mcdu.onPrevPage = () => { - CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); - }; - mcdu.onNextPage = mcdu.onPrevPage; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AvailableDeparturesPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AvailableDeparturesPage.js deleted file mode 100644 index 8d74ddb9424..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AvailableDeparturesPage.js +++ /dev/null @@ -1,326 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -const DeparturePagination = Object.freeze( - { - DEPT_PAGE: 4, - } -); - -const Labels = Object.freeze( - { - NO_SID: "NO SID", - NO_TRANS: "NO TRANS", - NO_VIA: "NO VIA", - NO_STAR: "NO STAR", - } -); - -class CDUAvailableDeparturesPage { - static ShowPage(mcdu, airport, pageCurrent = -1, sidSelection = false, forPlan = Fmgc.FlightPlanIndex.Active, inAlternate = false) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AvailableDeparturesPage; - - let selectedRunwayCell = "---"; - let selectedRunwayCellColor = "white"; - let selectedSidCell = "------"; - let selectedSidCellColor = "white"; - let selectedTransCell = "------"; - let selectedTransCellColor = "white"; - - // --- figure out which data is available for the page --- - - const editingTmpy = forPlan === Fmgc.FlightPlanIndex.Active && mcdu.flightPlanService.hasTemporary; - - /** @type {BaseFlightPlan} */ - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - - /** @type {import('msfs-navdata').Runway} */ - const selectedRunway = targetPlan.originRunway; - const selectedSid = targetPlan.originDeparture; - const selectedTransition = targetPlan.departureEnrouteTransition; - - /** @type {Departure} */ - /** @type {ProcedureTransition} */ - const showEosid = selectedRunway && sidSelection; - - /** @type {import('msfs-navdata').Runway[]} */ - const availableRunways = [...targetPlan.availableOriginRunways]; - let availableSids = [...targetPlan.availableDepartures].sort((a, b) => a.ident.localeCompare(b.ident)); - let availableTransitions = []; - - if (selectedRunway) { - // filter out any SIDs not compatible with this runway - availableSids = availableSids.filter((sid) => - sid.runwayTransitions.length === 0 || - findRunwayTransitionIndex(selectedRunway, sid.runwayTransitions) !== -1 - ); - } - - // NO SID option is available at the end of the list when non-zero options - if (availableSids.length > 0) { - availableSids.push(Labels.NO_SID); - } - - let selectedSidPage = -1; - if (selectedSid) { - availableTransitions = [...selectedSid.enrouteTransitions]; - - selectedSidPage = Math.floor((availableSids.findIndex((sid) => sid.databaseId === selectedSid.databaseId)) / DeparturePagination.DEPT_PAGE); - } - - const selectedRunwayPage = selectedRunway ? Math.floor((availableRunways.findIndex((runway) => runway.ident === selectedRunway.ident)) / DeparturePagination.DEPT_PAGE) : -1; - - // NO TRANS option is available at the end of the list when non-zero options - if (availableTransitions.length > 0) { - availableTransitions.push(Labels.NO_TRANS); - } - - // --- render the top part of the page --- - - const selectedColour = editingTmpy ? "yellow" : "green"; - - if (selectedRunway) { - selectedRunwayCell = Fmgc.RunwayUtils.runwayString(selectedRunway.ident); - selectedRunwayCellColor = selectedColour; - - // TODO check type of ls... but awful from raw JS - if (selectedRunway.lsIdent) { - selectedRunwayCell = `${selectedRunwayCell.padEnd(3)}{small}-ILS{end}`; - } - } - - if (selectedSid) { - selectedSidCell = selectedSid.ident; - selectedSidCellColor = selectedColour; - } else if (availableSids.length === 0 || selectedSid === null) { - selectedSidCell = "NONE"; - selectedSidCellColor = selectedColour; - } - - if (selectedTransition) { - selectedTransCell = selectedTransition.ident; - selectedTransCellColor = selectedColour; - } else if (selectedSid !== undefined && availableTransitions.length === 0 || selectedTransition === null) { - selectedTransCell = "NONE"; - selectedTransCellColor = selectedColour; - } - - // --- render the rows --- - - const rows = [[""], [""], [""], [""], [""], [""], [""], ["", "", ""]]; - if (!sidSelection) { - // jump to selected runway page if entering page - if (pageCurrent < 0) { - pageCurrent = Math.max(0, selectedRunwayPage); - } - - for (let i = 0; i < DeparturePagination.DEPT_PAGE; i++) { - const index = i + pageCurrent * DeparturePagination.DEPT_PAGE; - const runway = availableRunways[index]; - if (runway) { - const selected = selectedRunway && selectedRunway.ident === runway.ident; - const color = selected && !editingTmpy ? "green" : "cyan"; - - const hasIls = runway.lsFrequencyChannel > 0; // TODO what if not ILS - rows[2 * i] = [`{${color}}${selected ? "{sp}" : "{"}${Fmgc.RunwayUtils.runwayString(runway.ident).padEnd(3)}${hasIls ? '{small}-ILS{end}' : '{sp}{sp}{sp}{sp}'}${NXUnits.mToUser(runway.length).toFixed(0).padStart(6, '\xa0')}{small}${NXUnits.userDistanceUnit().padEnd(2)}{end}{end}`]; - const ilsText = hasIls ? `${runway.lsIdent.padStart(6)}/${runway.lsFrequencyChannel.toFixed(2)}` : ''; - rows[2 * i + 1] = [`{${color}}{sp}{sp}{sp}${Utils.leadingZeros(Math.round(runway.magneticBearing), 3)}${ilsText}{end}`]; - mcdu.onLeftInput[i + 1] = async (_, scratchpadCallback) => { - // Clicking the already selected runway is not allowed - if (!selected) { - try { - await mcdu.flightPlanService.setOriginRunway(runway.ident, forPlan, inAlternate); - - CDUAvailableDeparturesPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, false, forPlan, inAlternate); - }); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - - scratchpadCallback(); - } - }; - } - } - } else { - // jump to selected SID page if entering page - if (pageCurrent < 0) { - pageCurrent = Math.max(0, selectedSidPage); - } - - // show the available SIDs down the left side - for (let i = 0; i < DeparturePagination.DEPT_PAGE; i++) { - const sid = availableSids[pageCurrent * DeparturePagination.DEPT_PAGE + i]; - if (sid) { - const selected = (sid === Labels.NO_SID && selectedSid === null) || (selectedSid && selectedSid.databaseId === sid.databaseId); - const color = selected && !editingTmpy ? "green" : "cyan"; - - rows[2 * i] = [`{${color}}${selected ? "{sp}" : "{"}${typeof sid === 'string' ? sid : sid.ident}{end}`]; - mcdu.onLeftInput[1 + i] = async (_, scratchpadCallback) => { - // Clicking the already selected SID is not allowed - if (!selected) { - try { - if (sid === Labels.NO_SID) { - await mcdu.flightPlanService.setDepartureProcedure(null, forPlan, inAlternate); - } else { - await mcdu.flightPlanService.setDepartureProcedure(sid.databaseId, forPlan, inAlternate); - } - - CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, true, forPlan, inAlternate); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, false, forPlan, inAlternate); - }); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - - scratchpadCallback(); - } - }; - } - } - - // show the enroute transitions for the selected SID down the right side - if (selectedSid) { - const transPage = selectedSidPage > pageCurrent ? 0 : pageCurrent - selectedSidPage; - - for (let i = 0; i < DeparturePagination.DEPT_PAGE; i++) { - const trans = availableTransitions[transPage * DeparturePagination.DEPT_PAGE + i]; - if (trans) { - const selected = (trans === Labels.NO_TRANS && selectedTransition === null) || (selectedTransition && selectedTransition.ident === trans.ident); - const color = selected && !editingTmpy ? "green" : "cyan"; - - rows[2 * i][1] = `{${color}}${typeof trans === 'string' ? trans : trans.ident}${selected ? " " : "}"}{end}`; - mcdu.onRightInput[i + 1] = async (_, scratchpadCallback) => { - // Clicking the already selected transition is not allowed - if (!selected) { - try { - if (trans === Labels.NO_TRANS) { - await mcdu.flightPlanService.setDepartureEnrouteTransition(null, forPlan, inAlternate); - } else { - await mcdu.flightPlanService.setDepartureEnrouteTransition(trans.databaseId, forPlan, inAlternate); - } - - CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, true, forPlan, inAlternate); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - - mcdu.eraseTemporaryFlightPlan(() => { - CDUAvailableDeparturesPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); - }); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - - scratchpadCallback(); - } - }; - } - } - } - } - - // --- render arrows etc. --- - - let up = false; - let down = false; - let numPages = 0; - if (sidSelection) { - const sidPages = Math.ceil(availableSids.length / DeparturePagination.DEPT_PAGE); - const transPages = Math.ceil(availableTransitions.length / DeparturePagination.DEPT_PAGE); - numPages = Math.max(sidPages, transPages, selectedSidPage + transPages); - } else { - numPages = Math.ceil(availableRunways.length / DeparturePagination.DEPT_PAGE); - } - if (pageCurrent < (numPages - 1)) { - mcdu.onUp = () => { - pageCurrent++; - if (pageCurrent < 0) { - pageCurrent = 0; - } - CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, sidSelection, forPlan, inAlternate); - }; - up = true; - } - if (pageCurrent > 0) { - mcdu.onDown = () => { - pageCurrent--; - if (pageCurrent < 0) { - pageCurrent = 0; - } - CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, sidSelection, forPlan, inAlternate); - }; - down = true; - } - mcdu.setArrows(up, down, true, true); - - if (editingTmpy) { - mcdu.onLeftInput[5] = () => { - mcdu.eraseTemporaryFlightPlan(() => { - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }; - mcdu.onRightInput[5] = () => { - mcdu.insertTemporaryFlightPlan(() => { - mcdu.updateConstraints(); - mcdu.onToRwyChanged(); - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }; - } else { - mcdu.onLeftInput[5] = () => { - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }; - } - - if (showEosid) { - rows[7][2] = 'EOSID'; - } - - mcdu.setTemplate([ - ["{sp}DEPARTURES {small}FROM{end} {green}" + airport.ident + "{sp}{sp}{sp}"], - ["{sp}RWY", "TRANS{sp}", "SID"], - [selectedRunwayCell + "[color]" + selectedRunwayCellColor, selectedTransCell + "[color]" + selectedTransCellColor, selectedSidCell + "{sp}[color]" + selectedSidCellColor], - sidSelection ? ["SIDS", "TRANS", "AVAILABLE"] : ["", "", "AVAILABLE RUNWAYS\xa0"], - rows[0], - rows[1], - rows[2], - rows[3], - rows[4], - rows[5], - rows[6], - rows[7], - [editingTmpy ? "{ERASE[color]amber" : "{RETURN", editingTmpy ? "INSERT*[color]amber" : "", showEosid ? `{${selectedColour}}{sp}NONE{end}` : ''] - ]); - mcdu.onPrevPage = () => { - CDUAvailableDeparturesPage.ShowPage(mcdu, airport, -1, !sidSelection, forPlan, inAlternate); - }; - mcdu.onNextPage = mcdu.onPrevPage; - } -} - -/** - * Check if a runway transition matches with a runway - * @param {Runway} runway - * @param {ProcedureTransition} transition - * @returns {number} -1 if not found, else index of the transition - */ -function findRunwayTransitionIndex(runway, transitions) { - return transitions.findIndex((trans) => trans.ident === runway.ident); -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AvailableFlightPlanPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AvailableFlightPlanPage.js deleted file mode 100644 index 24cbd59ba00..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_AvailableFlightPlanPage.js +++ /dev/null @@ -1,201 +0,0 @@ -class CDUAvailableFlightPlanPage { - static ShowPage(mcdu, offset = 0, currentRoute = 1) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AvailableFlightPlanPage; - let fromTo = "NO ORIGIN/DEST"; - - const origin = mcdu.flightPlanService.active.originAirport; - const dest = mcdu.flightPlanService.active.destinationAirport; - - if (origin && dest) { - fromTo = `${origin.ident}/${dest.ident}`; - } - - const hasCoRoutes = mcdu.coRoute.routes.length > 0; - - if (hasCoRoutes) { - const coRoutesListSize = mcdu.coRoute.routes.length; - - // Page Management - if (currentRoute < 1) { - currentRoute = coRoutesListSize; - } - if (currentRoute > coRoutesListSize) { - currentRoute = 1; - } - - const {navlog, routeName} = mcdu.coRoute.routes[currentRoute - 1]; - - let scrollText = []; - var routeArray = []; - const scrollLimit = 9; - let columnPos = 0; - let rowPos = 0; - - // Scroll Text builder - for (let i = 0; i < navlog.length; i++) { - const fix = navlog[i]; - const nextFix = navlog[i + 1]; - - if (fix.is_sid_star === '1') { - continue; - } - - if (["TOP OF CLIMB", "TOP OF DESCENT"].includes(fix.name)) { - continue; - } - - if (!nextFix) { - continue; - } - - if (fix.via_airway === 'DCT' && nextFix.via_airway === 'DCT') { - switch (columnPos) { - case 1: - routeArray[rowPos] = [ - "", - "", - `${routeArray[rowPos][2]}` + " " + `{green}{big}${fix.via_airway.concat("@".repeat(5 - fix.via_airway.length))}{end}{end}` + " " + `{small}${fix.ident.concat("@".repeat(5 - fix.ident.length))}{end}` - ]; - columnPos = 2; - break; - case 2: - routeArray[rowPos] = ["", "",`${routeArray[rowPos][2]}` + " " + `{green}{big}${fix.via_airway.concat("@".repeat(5 - fix.via_airway.length))}{end}`]; - routeArray[rowPos + 1] = ["", "", `{small}${fix.ident.concat("@".repeat(5 - fix.ident.length))}{end}`]; - columnPos = 1; - rowPos++; - break; - } - - continue; - } - - if (nextFix.via_airway !== fix.via_airway) { - switch (columnPos) { - case 0: - routeArray[rowPos] = ["","",`{small}${fix.ident.concat("@".repeat(5 - fix.ident.length))}{end}`]; - columnPos = 1; - break; - case 1: - routeArray[rowPos] = [ - "", - "", - `${routeArray[rowPos][2]}` + " " + `{green}{big}${fix.via_airway.concat("@".repeat(5 - fix.via_airway.length))}{end}{end}` + " " + `{small}${fix.ident.concat("@".repeat(5 - fix.ident.length))}{end}` - ]; - columnPos = 2; - break; - case 2: - routeArray[rowPos] = [ - "", - "", - `${routeArray[rowPos][2]}` + " " + `{green}{big}${fix.via_airway.concat("@".repeat(5 - fix.via_airway.length))}{end}{end}` - ]; - routeArray[rowPos + 1] = ["","",`{small}${fix.ident.concat("@".repeat(5 - fix.ident.length))}{end}`]; - columnPos = 1; - rowPos++; - break; - } - continue; - } - } - - /* row character width management, - uses @ as a delim for adding spaces in short airways/waypoints */ - routeArray.forEach((line, index) => { - const excludedLength = line[2].replace(/{small}|{green}|{end}|{big}|/g,'').length; - if (excludedLength < 23) { - // Add spaces to make up lack of row width - const adjustedLine = line[2] + "{sp}".repeat(23 - excludedLength); - routeArray[index] = ["", "", adjustedLine]; - } - // Add spaces for short airways/waypoints smaller than 5 characters - routeArray[index] = ["", "", routeArray[index][2].replace(/@/g, '{sp}')]; - }); - - // Offset Management - const routeArrayLength = routeArray.length; - if (offset < 0) { - offset = 0; - }; - if (offset > (routeArrayLength - 9)) { - offset = routeArrayLength - 9; - }; - scrollText = routeArrayLength > 9 ? - [...routeArray.slice(0 + offset, 9 + offset)] - : [...routeArray, ...CDUAvailableFlightPlanPage.insertEmptyRows(scrollLimit - routeArray.length)]; - - mcdu.setTemplate([ - [`{sp}{sp}{sp}{sp}{sp}ROUTE{sp}{sp}{small}${currentRoute}/${coRoutesListSize}{end}`], - ["{sp}CO RTE", "FROM/TO{sp}{sp}" ], - [`${routeName}[color]cyan`, `${fromTo}[color]cyan`], - ...scrollText, - [" { - CDUAvailableFlightPlanPage.ShowPage(mcdu, 0, currentRoute - 1); - }; - mcdu.onNextPage = () => { - CDUAvailableFlightPlanPage.ShowPage(mcdu, 0, currentRoute + 1); - }; - mcdu.onDown = () => {//on page down decrement the page offset. - CDUAvailableFlightPlanPage.ShowPage(mcdu, offset - 1, currentRoute); - }; - mcdu.onUp = () => { - CDUAvailableFlightPlanPage.ShowPage(mcdu, offset + 1, currentRoute); - }; - - mcdu.onLeftInput[5] = () => { - mcdu.coRoute.routes = []; - CDUInitPage.ShowPage1(mcdu); - }; - - mcdu.onRightInput[5] = () => { - const selectedRoute = mcdu.coRoute.routes[currentRoute - 1]; - mcdu.coRoute.routeNumber = routeName; - mcdu.coRoute["originIcao"] = selectedRoute.originIcao; - mcdu.coRoute["destinationIcao"] = selectedRoute.destinationIcao; - mcdu.coRoute["route"] = selectedRoute.route; - if (selectedRoute.alternateIcao) { - mcdu.coRoute["alternateIcao"] = selectedRoute.alternateIcao; - } - mcdu.coRoute["navlog"] = selectedRoute.navlog; - setTimeout(async () => { - await Fmgc.CoRouteUplinkAdapter.uplinkFlightPlanFromCoRoute(mcdu, mcdu.flightPlanService, mcdu.coRoute); - await mcdu.flightPlanService.uplinkInsert(); - this.setGroundTempFromOrigin(); - - CDUInitPage.ShowPage1(mcdu); - }, 0 /* No delay because it takes long enough without artificial delay */); - - }; - } else { - mcdu.setTemplate([ - [fromTo], - [""], - ["NONE[color]green"], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [" { - CDUInitPage.ShowPage1(mcdu); - }; - } - - static insertEmptyRows(rowsToInsert) { - const array = []; - for (let i = 0; i < rowsToInsert; i++) { - array.push([""]); - } - return array; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_DataIndexPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_DataIndexPage.js deleted file mode 100644 index 6e2ed9dc897..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_DataIndexPage.js +++ /dev/null @@ -1,120 +0,0 @@ -class CDUDataIndexPage { - static ShowPage1(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.DataIndexPage1; - mcdu.activeSystem = 'FMGC'; - mcdu.setTemplate([ - ["DATA INDEX", "1", "2"], - ["\xa0POSITION"], - [""] - ]); - - mcdu.leftInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[0] = () => { - CDUPositionMonitorPage.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[1] = () => { - CDUIRSMonitor.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[2] = () => { - CDUGPSMonitor.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[3] = () => { - CDUIdentPage.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[4] = () => { - CDUAirportsMonitor.ShowPage(mcdu, true); - }; - - mcdu.onNextPage = () => { - this.ShowPage2(mcdu); - }; - mcdu.onPrevPage = () => { - this.ShowPage2(mcdu); - }; - } - static ShowPage2(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.DataIndexPage2; - mcdu.setTemplate([ - ["DATA INDEX", "2", "2"], - ["", "STORED\xa0"], - [""], - ["", "STORED\xa0[color]inop"], - ["[color]inop"], - ["", "STORED\xa0[color]inop"], - ["[color]inop"], - ["", "STORED\xa0[color]inop"], - ["[color]inop"], - ["\xa0ACTIVE F-PLAN[color]inop", ""], - [" { - CDUWaypointPage.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onRightInput[0] = () => { - CDUPilotsWaypoint.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[1] = () => { - CDUNavaidPage.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onNextPage = () => { - this.ShowPage1(mcdu); - }; - mcdu.onPrevPage = () => { - this.ShowPage1(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_DirectToPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_DirectToPage.js deleted file mode 100644 index 281e83203b7..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_DirectToPage.js +++ /dev/null @@ -1,172 +0,0 @@ -// Copyright (c) 2020, 2022 FlyByWire Simulations -// SPDX-License-Identifier: GPL-3.0 - -// TODO this whole thing is thales layout... - -class CDUDirectToPage { - static ShowPage(mcdu, directWaypoint, wptsListIndex = 0) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.DirectToPage; - mcdu.returnPageCallback = () => { - CDUDirectToPage.ShowPage(mcdu, directWaypoint, wptsListIndex); - }; - - mcdu.activeSystem = 'FMGC'; - - let directWaypointCell = ""; - if (directWaypoint) { - directWaypointCell = directWaypoint.ident; - } else if (mcdu.flightPlanService.hasTemporary) { - mcdu.eraseTemporaryFlightPlan(() => { - CDUDirectToPage.ShowPage(mcdu); - }); - return; - } - - const waypointsCell = ["", "", "", "", ""]; - let iMax = 5; - let eraseLabel = ""; - let eraseLine = ""; - let insertLabel = ""; - let insertLine = ""; - if (mcdu.flightPlanService.hasTemporary) { - iMax--; - eraseLabel = "\xa0DIR TO[color]amber"; - eraseLine = "{ERASE[color]amber"; - insertLabel = "TMPY\xa0[color]amber"; - insertLine = "DIRECT*[color]amber"; - mcdu.onLeftInput[5] = () => { - mcdu.eraseTemporaryFlightPlan(() => { - CDUDirectToPage.ShowPage(mcdu); - }); - }; - mcdu.onRightInput[5] = () => { - mcdu.insertTemporaryFlightPlan(() => { - SimVar.SetSimVarValue("K:A32NX.FMGC_DIR_TO_TRIGGER", "number", 0); - CDUFlightPlanPage.ShowPage(mcdu); - }); - }; - } - - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - mcdu.eraseTemporaryFlightPlan(() => { - CDUDirectToPage.ShowPage(mcdu, undefined, wptsListIndex); - }); - return; - } - - Fmgc.WaypointEntryUtils.getOrCreateWaypoint(mcdu, value, false).then((w) => { - if (w) { - mcdu.eraseTemporaryFlightPlan(() => { - mcdu.directToWaypoint(w).then(() => { - CDUDirectToPage.ShowPage(mcdu, w, wptsListIndex); - }).catch(err => { - mcdu.logTroubleshootingError(err); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - console.error(err); - }); - }); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } - }).catch((err) => { - // Rethrow if error is not an FMS message to display - if (err.type === undefined) { - throw err; - } - - mcdu.showFmsErrorMessage(err.type); - }); - }; - - mcdu.onRightInput[2] = () => { - mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - }; - mcdu.onRightInput[3] = () => { - mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - }; - mcdu.onRightInput[4] = () => { - mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - }; - - const plan = mcdu.flightPlanService.active; - - let i = 0; - let cellIter = 0; - wptsListIndex = Math.max(wptsListIndex, mcdu.flightPlanService.active.activeLegIndex); - - const totalWaypointsCount = plan.firstMissedApproachLegIndex; - - while (i < totalWaypointsCount && i + wptsListIndex < totalWaypointsCount && cellIter < iMax) { - const legIndex = i + wptsListIndex; - if (plan.elementAt(legIndex).isDiscontinuity) { - i++; - continue; - } - - const leg = plan.legElementAt(legIndex); - - if (leg) { - if (!leg.isXF()) { - i++; - continue; - } - - waypointsCell[cellIter] = "{" + leg.ident + "[color]cyan"; - if (waypointsCell[cellIter]) { - mcdu.onLeftInput[cellIter + 1] = () => { - mcdu.eraseTemporaryFlightPlan(() => { - mcdu.directToLeg(legIndex).then(() => { - CDUDirectToPage.ShowPage(mcdu, leg.terminationWaypoint(), wptsListIndex); - }).catch(err => { - mcdu.logTroubleshootingError(err); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - console.error(err); - }); - }); - }; - } - } else { - waypointsCell[cellIter] = "----"; - } - i++; - cellIter++; - } - if (cellIter < iMax) { - waypointsCell[cellIter] = "--END--"; - } - let up = false; - let down = false; - if (wptsListIndex < totalWaypointsCount - 5) { - mcdu.onUp = () => { - wptsListIndex++; - CDUDirectToPage.ShowPage(mcdu, directWaypoint, wptsListIndex); - }; - up = true; - } - if (wptsListIndex > 0) { - mcdu.onDown = () => { - wptsListIndex--; - CDUDirectToPage.ShowPage(mcdu, directWaypoint, wptsListIndex); - }; - down = true; - } - mcdu.setArrows(up, down, false ,false); - mcdu.setTemplate([ - ["DIR TO"], - ["\xa0WAYPOINT", "DIST\xa0", "UTC"], - ["*[" + (directWaypointCell ? directWaypointCell : "\xa0\xa0\xa0\xa0\xa0") + "][color]cyan", "---", "----"], - ["\xa0F-PLN WPTS"], - [waypointsCell[0], "DIRECT TO[color]cyan"], - ["", "WITH\xa0"], - [waypointsCell[1], "ABEAM PTS[color]cyan"], - ["", "RADIAL IN\xa0"], - [waypointsCell[2], "[ ]°[color]cyan"], - ["", "RADIAL OUT\xa0"], - [waypointsCell[3], "[ ]°[color]cyan"], - [eraseLabel, insertLabel], - [eraseLine ? eraseLine : waypointsCell[4], insertLine] - ]); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Field.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Field.js deleted file mode 100644 index 6bd299d15c7..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Field.js +++ /dev/null @@ -1,201 +0,0 @@ -class CDU_Field { - constructor(mcdu, selectedCallback) { - this.mcdu = mcdu; - this.selectedCallback = selectedCallback; - this.currentValue = null; - } - setOptions(options) { - for (const option in options) { - this[option] = options[option]; - } - } - - getValue() { - return ""; - } - - /** - * @param {string|number|null} value - */ - onSelect(value) { - this.selectedCallback(this.currentValue); - } - - getFieldAsColumnParameters() { - const text = this.getValue(); - let color = Column.white; - - if (text.includes("[color]amber")) { - color = Column.amber; - } else if (text.includes("[color]red")) { - color = Column.red; - } else if (text.includes("[color]green")) { - color = Column.green; - } else if (text.includes("[color]cyan")) { - color = Column.cyan; - } else if (text.includes("[color]magenta")) { - color = Column.magenta; - } else if (text.includes("[color]yellow")) { - color = Column.yellow; - } else if (text.includes("[color]inop")) { - color = Column.inop; - } - - return [ - this.onSelect.bind(this), - text - .replace("[color]white", "") - .replace("[color]amber", "") - .replace("[color]red", "") - .replace("[color]green", "") - .replace("[color]cyan", "") - .replace("[color]magenta", "") - .replace("[color]yellow", "") - .replace("[color]inop", "") - , color - ]; - } -} - -/** - * @description Placeholder field that shows "NOT YET IMPLEMENTED" message when selected - */ -class CDU_InopField extends CDU_Field { - /** - * @param {A320_Neo_CDU_MainDisplay} mcdu - * @param {string} value - * @param {boolean} [inopColor=true] whether to append "[color]inop" to the value - */ - constructor(mcdu, value, inopColor = true) { - super(mcdu, () => {}); - this.value = inopColor ? `${value}[color]inop` : value; - } - getValue() { - return this.value; - } - - onSelect() { - this.mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - super.onSelect(); - } - -} - -class CDU_SingleValueField extends CDU_Field { - /** - * @param {A320_Neo_CDU_MainDisplay} mcdu - * @param {"string"|"int"|"number"} type - * @param {string|number|null} value - * @param {object} options - * @param {boolean} [options.clearable=false] - * @param {string} [options.emptyValue=""] - * @param {number} [options.maxLength=Infinity] - * @param {number} [options.minValue=-Infinity] - * @param {number} [options.maxValue=Infinity] - * @param {number} [options.maxDisplayedDecimalPlaces=] - * @param {string} [options.prefix=""] - * @param {string} [options.suffix=""] - * @param {function} [options.isValid=] - * @param {function(*=): void} selectedCallback - */ - constructor(mcdu, type, value, options, selectedCallback) { - super(mcdu, selectedCallback); - this.type = type; - this.currentValue = value; - this.clearable = false; - this.emptyValue = ""; - this.maxLength = Infinity; - this.minValue = -Infinity; - this.maxValue = Infinity; - this.prefix = ""; - this.suffix = ""; - this.setOptions(options); - } - - /** - * @returns {string} - */ - getValue() { - let value = this.currentValue; - if (value === "" || value == null) { - return this.emptyValue; - } - if (this.type === "number" && isFinite(this.maxDisplayedDecimalPlaces)) { - value = value.toFixed(this.maxDisplayedDecimalPlaces); - } - return `${this.prefix}${value}${this.suffix}`; - } - - /** - * @param {*} value - */ - setValue(value) { - // Custom isValid callback - if (value.length === 0 || (this.isValid && !this.isValid(value))) { - this.mcdu.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - switch (this.type) { - case "string": - // Check max length - if (value.length > this.maxLength) { - this.mcdu.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - break; - case "int": - // Make sure value is an integer and is within the min/max - const valueAsInt = Number.parseInt(value, 10); - if (!isFinite(valueAsInt) || value.includes(".")) { - this.mcdu.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - if (valueAsInt > this.maxValue || valueAsInt < this.minValue) { - this.mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - value = valueAsInt; - break; - case "number": - // Make sure value is a valid number and is within the min/max - const valueAsFloat = Number.parseFloat(value); - if (!isFinite(valueAsFloat)) { - this.mcdu.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - if (valueAsFloat > this.maxValue || valueAsFloat < this.minValue) { - this.mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - value = valueAsFloat; - break; - } - // Update the value - this.currentValue = value; - return true; - } - clearValue() { - if (this.clearable) { - if (this.type === "string") { - this.currentValue = ""; - } else { - this.currentValue = null; - } - } else { - this.mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - } - } - onSelect(value) { - if (value === FMCMainDisplay.clrValue) { - this.clearValue(); - } else { - if (!this.setValue(value)) { - this.mcdu.setScratchpadUserData(value); - } - } - super.onSelect(); - } -} - -// TODO: Create classes for multi value fields diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_FixInfoPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_FixInfoPage.js deleted file mode 100644 index 68d3112d8b9..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_FixInfoPage.js +++ /dev/null @@ -1,189 +0,0 @@ -// Copyright (c) 2021 FlyByWire Simulations -// SPDX-License-Identifier: GPL-3.0 - -class CDUFixInfoPage { - static ShowPage(mcdu, page = 0) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.FixInfoPage; - mcdu.returnPageCallback = () => { - CDUFixInfoPage.ShowPage(mcdu, page); - }; - mcdu.activeSystem = 'FMGC'; - - const fixInfo = mcdu.flightPlanService.active.fixInfos[page]; - - mcdu.onLeftInput[0] = (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - if (fixInfo.fix) { - mcdu.flightPlanService.setFixInfoEntry(page, null); - - return CDUFixInfoPage.ShowPage(mcdu, page); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - - return scratchpadCallback(); - } - } - if (Fmgc.WaypointEntryUtils.isPlaceFormat(value)) { - Fmgc.WaypointEntryUtils.parsePlace(mcdu, value).then((runway) => { - mcdu.flightPlanService.setFixInfoEntry(page, { - fix: runway, - radii: [], - radials: [], - }); - - CDUFixInfoPage.ShowPage(mcdu, page); - }).catch((message) => { - if (message.type !== undefined) { - mcdu.showFmsErrorMessage(message.type); - } else if (message instanceof McduMessage) { - mcdu.setScratchpadMessage(message); - - scratchpadCallback(); - } else { - console.error(err); - } - }); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - - scratchpadCallback(); - } - }; - - const template = [ - [`\xa0\xa0\xa0\xa0\xa0FIX INFO\xa0\xa0{small}${page + 1}/4{end}`], - [`REF FIX ${page + 1}`], - ['{amber}_______{end}'], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - ]; - - if (fixInfo && fixInfo.fix) { - template[2] = [`{cyan}${fixInfo.fix.ident}{end}`]; - template[3] = ['\xa0RADIAL\xa0\xa0TIME\xa0\xa0DTG\xa0\xa0ALT']; - - for (let i = 0; i <= 1; i++) { - const radial = fixInfo.radials[i]; - - if (radial !== undefined) { - template[4 + i * 2] = [`\xa0{cyan}${("" + radial.magneticBearing).padStart(3, "0")}°{end}\xa0\xa0\xa0\xa0----\xa0----\xa0----`]; - } else if (i === 0 || fixInfo.radials[0] !== undefined) { - template[4 + i * 2] = [`\xa0{cyan}[\xa0]°{end}\xa0\xa0\xa0\xa0----\xa0----\xa0----`]; - } - - mcdu.onLeftInput[1 + i] = (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - if (radial !== undefined) { - mcdu.flightPlanService.editFixInfoEntry(page, (fixInfo) => { - fixInfo.radials.splice(i); - }); - - CDUFixInfoPage.ShowPage(mcdu, page); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - - scratchpadCallback(); - } - } else if (value.match(/^[0-9]{1,3}$/)) { - const degrees = parseInt(value); - - if (degrees <= 360) { - mcdu.flightPlanService.editFixInfoEntry(page, (fixInfo) => { - fixInfo.radials[i] = { magneticBearing: degrees, trueBearing: A32NX_Util.magneticToTrue(degrees, A32NX_Util.getRadialMagVar(fixInfo.fix)) }; - }); - - CDUFixInfoPage.ShowPage(mcdu, page); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - - scratchpadCallback(); - } - } else if (value === '' && radial !== undefined) { - mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - - scratchpadCallback(); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - - scratchpadCallback(); - } - }; - } - - template[7] = ['\xa0RADIUS']; - if (fixInfo.radii[0] !== undefined) { - template[8] = [`\xa0{cyan}${("" + fixInfo.radii[0].radius).padStart(3, "\xa0")}{small}NM{end}{end}\xa0\xa0\xa0----\xa0----\xa0----`]; - } else { - template[8] = [`\xa0{cyan}[\xa0]{small}NM{end}{end}\xa0\xa0\xa0----\xa0----\xa0----`]; - } - - mcdu.onLeftInput[3] = (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - if (fixInfo.radii[0] !== undefined) { - mcdu.flightPlanService.editFixInfoEntry(page, (fixInfo) => { - fixInfo.radii.length = 0; - }); - - CDUFixInfoPage.ShowPage(mcdu, page); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - } - } else if (value.match(/^[0-9]{1,3}$/)) { - const radius = parseInt(value); - if (radius >= 1 && radius <= 256) { - mcdu.flightPlanService.editFixInfoEntry(page, (fixInfo) => { - if (fixInfo.radii[0]) { - fixInfo.radii[0].radius = radius; - } else { - fixInfo.radii[0] = { radius }; - } - }); - - CDUFixInfoPage.ShowPage(mcdu, page); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - } - } else if (value === '' && fixInfo.radii[0] !== undefined) { - mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - - scratchpadCallback(); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - - scratchpadCallback(); - } - }; - - template[10] = ['{inop} mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - } - - mcdu.setArrows(false, false, true, true); - mcdu.setTemplate(template); - mcdu.onPrevPage = () => { - if (page > 0) { - CDUFixInfoPage.ShowPage(mcdu, page - 1); - } else { - CDUFixInfoPage.ShowPage(mcdu, 3); - } - }; - mcdu.onNextPage = () => { - if (page < 3) { - CDUFixInfoPage.ShowPage(mcdu, page + 1); - } else { - CDUFixInfoPage.ShowPage(mcdu, 0); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_FlightPlanPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_FlightPlanPage.js deleted file mode 100644 index 83ac475d4a0..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_FlightPlanPage.js +++ /dev/null @@ -1,1139 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -const MAX_FIX_ROW = 5; - -const Markers = { - FPLN_DISCONTINUITY: ["---F-PLN DISCONTINUITY--"], - END_OF_FPLN: ["------END OF F-PLN------"], - NO_ALTN_FPLN: ["-----NO ALTN F-PLN------"], - END_OF_ALTN_FPLN: ["---END OF ALTN F-PLN----"], - TOO_STEEP_PATH: ["-----TOO STEEP PATH-----"] -}; - -const Altitude = Object.freeze({ - Empty: "\xa0\xa0\xa0\xa0\xa0", - NoPrediction: "-----", -}); -const Speed = Object.freeze({ - Empty: "\xa0\xa0\xa0", - NoPrediction: "---", -}); -const Time = Object.freeze({ - Empty: "\xa0\xa0\xa0\xa0", - NoPrediction: "----", -}); - -class CDUFlightPlanPage { - - static ShowPage(mcdu, offset = 0, forPlan = 0) { - - // INIT - function addLskAt(index, delay, callback) { - mcdu.leftInputDelay[index] = (typeof delay === 'function') ? delay : () => delay; - mcdu.onLeftInput[index] = callback; - } - - function addRskAt(index, delay, callback) { - mcdu.rightInputDelay[index] = (typeof delay === 'function') ? delay : () => delay; - mcdu.onRightInput[index] = callback; - } - - //mcdu.flightPlanManager.updateWaypointDistances(false /* approach */); - //mcdu.flightPlanManager.updateWaypointDistances(true /* approach */); - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.FlightPlanPage; - mcdu.returnPageCallback = () => { - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - }; - mcdu.activeSystem = 'FMGC'; - - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.FlightPlanPage) { - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - } - }, mcdu.PageTimeout.Medium); - - const flightPhase = mcdu.flightPhaseManager.phase; - const isFlying = flightPhase >= FmgcFlightPhases.TAKEOFF && flightPhase != FmgcFlightPhases.DONE; - - let showFrom = false; - // TODO FIXME: Correct FMS lateral position calculations and move logic from F-PLN A - // 22-70-00:11 - const adirLat = ADIRS.getLatitude(); - const adirLong = ADIRS.getLongitude(); - const ppos = (adirLat.isNormalOperation() && adirLong.isNormalOperation()) ? { - lat: ADIRS.getLatitude().value, - long: ADIRS.getLongitude().value, - } : { - lat: NaN, - long: NaN - }; - - const forActiveOrTemporary = forPlan === 0; - const targetPlan = forActiveOrTemporary ? mcdu.flightPlanService.activeOrTemporary : mcdu.flightPlanService.secondary(1); - const planAccentColor = forActiveOrTemporary ? mcdu.flightPlanService.hasTemporary ? 'yellow' : 'green' : 'white'; - - let headerText; - if (forActiveOrTemporary) { - if (mcdu.flightPlanService.hasTemporary) { - headerText = `{yellow}{sp}TMPY{end}`; - } else { - headerText = `{sp}`; - } - } else { - headerText = `{sp}{sp}{sp}{sp}{sp}{sp}{sp}{sp}{sp}{sp}{sp}SEC`; - } - - let flightNumberText = ''; - if (forActiveOrTemporary) { - flightNumberText = SimVar.GetSimVarValue("ATC FLIGHT NUMBER", "string", "FMC"); - } - - const waypointsAndMarkers = []; - const first = Math.max(0, targetPlan.fromLegIndex); - let destinationAirportOffset = 0; - let alternateAirportOffset = 0; - - // VNAV - const fmsGeometryProfile = mcdu.guidanceController.vnavDriver.mcduProfile; - const fmsPseudoWaypoints = mcdu.guidanceController.currentPseudoWaypoints; - - /** @type {Map} */ - let vnavPredictionsMapByWaypoint = null; - if (fmsGeometryProfile && fmsGeometryProfile.isReadyToDisplay) { - vnavPredictionsMapByWaypoint = fmsGeometryProfile.waypointPredictions; - } - - let cumulativeDistance = 0; - // Primary F-PLAN - - // In this loop, we insert pseudowaypoints between regular waypoints and compute the distances between the previous and next (pseudo-)waypoint. - for (let i = first; i < targetPlan.legCount; i++) { - const inMissedApproach = i >= targetPlan.firstMissedApproachLegIndex; - const isActiveLeg = i === targetPlan.activeLegIndex && forActiveOrTemporary; - const isBeforeActiveLeg = i < targetPlan.activeLegIndex && forActiveOrTemporary; - - const wp = targetPlan.allLegs[i]; - - if (wp.isDiscontinuity) { - waypointsAndMarkers.push({ marker: Markers.FPLN_DISCONTINUITY, fpIndex: i, inAlternate: false, inMissedApproach }); - continue; - } - - const pseudoWaypointsOnLeg = fmsPseudoWaypoints.filter((it) => it.displayedOnMcdu && it.alongLegIndex === i); - pseudoWaypointsOnLeg.sort((a, b) => a.distanceFromStart - b.distanceFromStart); - - for (let j = 0; j < pseudoWaypointsOnLeg.length; j++) { - const pwp = pseudoWaypointsOnLeg[j]; - const distanceFromLastLine = pwp.distanceFromStart - cumulativeDistance; - cumulativeDistance = pwp.distanceFromStart; - - // No PWP on FROM leg - if (!isBeforeActiveLeg) { - waypointsAndMarkers.push({ pwp, fpIndex: i, inMissedApproach, distanceFromLastLine, isActive: isActiveLeg && j === 0 }); - } - } - - if (i >= targetPlan.activeLegIndex && wp.definition.type === 'HM') { - waypointsAndMarkers.push({ holdResumeExit: wp, fpIndex: i, inMissedApproach, isActive: isActiveLeg }); - } - - const distanceFromLastLine = wp.calculated ? (wp.calculated.cumulativeDistanceWithTransitions - cumulativeDistance) : 0; - cumulativeDistance = wp.calculated ? wp.calculated.cumulativeDistanceWithTransitions : cumulativeDistance; - - waypointsAndMarkers.push({ wp, fpIndex: i, inAlternate: false, inMissedApproach, distanceFromLastLine, isActive: isActiveLeg && pseudoWaypointsOnLeg.length === 0 }); - - if (i === targetPlan.destinationLegIndex) { - destinationAirportOffset = Math.max(waypointsAndMarkers.length - 4, 0); - } - - if (i === targetPlan.lastIndex) { - waypointsAndMarkers.push({ marker: Markers.END_OF_FPLN, fpIndex: i + 1, inAlternate: false, inMissedApproach: false }); - } - } - - // Primary ALTN F-PLAN - if (targetPlan.alternateDestinationAirport) { - for (let i = 0; i < targetPlan.alternateFlightPlan.legCount; i++) { - const inMissedApproach = i >= targetPlan.alternateFlightPlan.firstMissedApproachLegIndex; - - const wp = targetPlan.alternateFlightPlan.allLegs[i]; - - if (wp.isDiscontinuity) { - waypointsAndMarkers.push({ marker: Markers.FPLN_DISCONTINUITY, fpIndex: i, inAlternate: true }); - continue; - } - - if (i >= targetPlan.alternateFlightPlan.activeLegIndex && wp.definition.type === 'HM') { - waypointsAndMarkers.push({ holdResumeExit: wp, fpIndex: i, inAlternate: true }); - } - - const distanceFromLastLine = wp.calculated ? (wp.calculated.cumulativeDistanceWithTransitions - cumulativeDistance) : 0; - cumulativeDistance = wp.calculated ? wp.calculated.cumulativeDistanceWithTransitions : cumulativeDistance; - - waypointsAndMarkers.push({ wp, fpIndex: i, inAlternate: true, inMissedApproach, distanceFromLastLine }); - - if (i === targetPlan.alternateFlightPlan.destinationLegIndex) { - alternateAirportOffset = Math.max(waypointsAndMarkers.length - 4, 0); - } - - if (i === targetPlan.alternateFlightPlan.lastIndex) { - waypointsAndMarkers.push({ marker: Markers.END_OF_ALTN_FPLN, fpIndex: i + 1, inAlternate: true, inMissedApproach: false }); - } - } - } else if (targetPlan.legCount > 0) { - waypointsAndMarkers.push({ marker: Markers.NO_ALTN_FPLN, fpIndex: targetPlan.legCount + 1, inAlternate: true }); - } - - const tocIndex = waypointsAndMarkers.findIndex(({ pwp }) => pwp && pwp.ident === '(T/C)'); - - // Render F-PLAN Display - - // fprow: 1 | 2 | 3 4 | 5 6 | 7 8 | 9 10 | 11 12 | - // display: SPD/ALT| R0 | R1 | R2 | R3 | R4 | DEST | SCRATCHPAD - // functions: | F[0] | F[1] | F[2] | F[3] | F[4] | F[5] | - // | FROM | TO | - let rowsCount = 5; - - if (waypointsAndMarkers.length === 0) { - rowsCount = 0; - mcdu.setTemplate([ - [`{left}{small}{sp}FROM{end}${headerText}{end}{right}{small}${flightNumberText}{sp}{sp}{sp}{end}{end}`], - ...emptyFplnPage(forPlan) - ]); - mcdu.onLeftInput[0] = () => CDULateralRevisionPage.ShowPage(mcdu, undefined, undefined, forPlan); - return; - } else if (waypointsAndMarkers.length >= 5) { - rowsCount = 5; - } else { - rowsCount = waypointsAndMarkers.length; - } - - let useTransitionAltitude = false; - - // Only examine first 5 (or less) waypoints/markers - const scrollWindow = []; - for (let rowI = 0, winI = offset; rowI < rowsCount; rowI++, winI++) { - winI = winI % (waypointsAndMarkers.length); - - const { - /** @type {import('fbw-a32nx/src/systems/fmgc/src/flightplanning/legs/FlightPlanLeg').FlightPlanElement} */ - wp, - pwp, - marker, - /** @type {import('fbw-a32nx/src/systems/fmgc/src/flightplanning/legs/FlightPlanLeg').FlightPlanElement} */ - holdResumeExit, - fpIndex, - inAlternate, - inMissedApproach, - distanceFromLastLine, - isActive - } = waypointsAndMarkers[winI]; - - const legAccentColor = (inAlternate || inMissedApproach) ? "cyan" : planAccentColor; - - const wpPrev = inAlternate ? targetPlan.alternateFlightPlan.maybeElementAt(fpIndex - 1) : targetPlan.maybeElementAt(fpIndex - 1); - const wpNext = inAlternate ? targetPlan.alternateFlightPlan.maybeElementAt(fpIndex - 1) : targetPlan.maybeElementAt(fpIndex + 1); - const wpActive = inAlternate || (fpIndex >= targetPlan.activeLegIndex); - - // Bearing/Track - let bearingTrack = ""; - const maybeBearingTrackTo = pwp ? targetPlan.maybeElementAt(fpIndex) : wp; - const bearingTrackTo = maybeBearingTrackTo ? maybeBearingTrackTo : wpNext; - switch (rowI) { - case 1: - const trueBearing = SimVar.GetSimVarValue("L:A32NX_EFIS_L_TO_WPT_BEARING", "Degrees"); - if (isActive && trueBearing !== null && trueBearing >= 0) { - bearingTrack = `BRG${trueBearing.toFixed(0).padStart(3,"0")}\u00b0`; - } - break; - case 2: - bearingTrack = formatTrack(wpPrev, bearingTrackTo); - break; - } - - const constraintType = wp ? CDUVerticalRevisionPage.constraintType(mcdu, fpIndex, targetPlan.index, inAlternate) : WaypointConstraintType.Unknown; - if (constraintType === WaypointConstraintType.CLB) { - useTransitionAltitude = true; - } else if (constraintType === WaypointConstraintType.DES) { - useTransitionAltitude = false; - } else if (tocIndex >= 0) { - // FIXME Guess because VNAV doesn't tell us whether altitudes are climb or not \o/ - useTransitionAltitude = winI <= tocIndex; - } // else we stick with the last time we were sure... - - if (wp && wp.isDiscontinuity === false) { - // Waypoint - if (offset === 0) { - showFrom = true; - } - - let ident = wp.ident; - let isOverfly = wp.definition.overfly; - const isFromLeg = !inAlternate && fpIndex === targetPlan.fromLegIndex; - - let verticalWaypoint = null; - // TODO: Alternate predictions - if (!inAlternate && vnavPredictionsMapByWaypoint) { - verticalWaypoint = vnavPredictionsMapByWaypoint.get(fpIndex); - } - - // Color - let color; - if (isActive) { - color = "white"; - } else { - const inMissedApproach = targetPlan.index === Fmgc.FlightPlanIndex.Active && fpIndex >= targetPlan.firstMissedApproachLegIndex; - - if (inMissedApproach || inAlternate) { - color = 'cyan'; - } else { - color = planAccentColor; - } - } - - // Time - let timeCell = Time.NoPrediction; - let timeColor = "white"; - if (verticalWaypoint && isFinite(verticalWaypoint.secondsFromPresent)) { - const utcTime = SimVar.GetGlobalVarValue("ZULU TIME", "seconds"); - - timeCell = `${isFromLeg ? "{big}" : "{small}"}${isFlying - ? FMCMainDisplay.secondsToUTC(utcTime + verticalWaypoint.secondsFromPresent) - : FMCMainDisplay.secondsTohhmm(verticalWaypoint.secondsFromPresent)}{end}`; - - timeColor = color; - } else if (!inAlternate && fpIndex === targetPlan.originLegIndex) { - timeCell = "{big}0000{end}"; - timeColor = color; - } - - // Fix Header - const fixAnnotation = wp.annotation; - - // Distance - let distance = ""; - // Active waypoint is live distance, others are distances in the flight plan - if (isActive) { - if (Number.isFinite(mcdu.guidanceController.activeLegAlongTrackCompletePathDtg)) { - distance = Math.round(Math.max(0, Math.min(9999, mcdu.guidanceController.activeLegAlongTrackCompletePathDtg))).toFixed(0); - } - } else { - distance = Math.round(Math.max(0, Math.min(9999, distanceFromLastLine))).toFixed(0); - } - - let fpa = ''; - if (wp.definition.verticalAngle !== undefined) { - fpa = (Math.round(wp.definition.verticalAngle * 10) / 10).toFixed(1); - } - - let altColor = "white"; - let spdColor = "white"; - - // Should show empty speed prediction for waypoint after hold - let speedConstraint = wp.type === "HM" ? Speed.Empty : Speed.NoPrediction; - let speedPrefix = ""; - - if (targetPlan.index !== Fmgc.FlightPlanIndex.Temporary && wp.type !== "HM") { - if (!inAlternate && fpIndex === targetPlan.originLegIndex) { - speedConstraint = Number.isFinite(mcdu.v1Speed) ? `{big}${Math.round(mcdu.v1Speed)}{end}` : Speed.NoPrediction; - spdColor = Number.isFinite(mcdu.v1Speed) ? color : "white"; - } else if (isFromLeg) { - speedConstraint = Speed.Empty; - } else if (verticalWaypoint && verticalWaypoint.speed) { - speedConstraint = `{small}${verticalWaypoint.speed < 1 ? formatMachNumber(verticalWaypoint.speed) : Math.round(verticalWaypoint.speed)}{end}`; - - if (verticalWaypoint.speedConstraint) { - speedPrefix = `${verticalWaypoint.isSpeedConstraintMet ? "{magenta}" : "{amber}"}*{end}`; - } - spdColor = color; - } else if (wp.hasPilotEnteredSpeedConstraint()) { - speedConstraint = Math.round(wp.pilotEnteredSpeedConstraint.speed); - spdColor = "magenta"; - } else if (wp.hasDatabaseSpeedConstraint()) { - speedConstraint = `{small}${Math.round(wp.definition.speed)}{end}`; - spdColor = "magenta"; - } - } - - speedConstraint = speedPrefix + speedConstraint; - - // Altitude - const hasAltConstraint = legHasAltConstraint(wp); - let altitudeConstraint = Altitude.NoPrediction; - let altSize = "big"; - if (targetPlan.index !== Fmgc.FlightPlanIndex.Temporary) { - if (verticalWaypoint && verticalWaypoint.altitude) { - // Just show prediction - let altPrefix = ""; - if (hasAltConstraint && !isFromLeg) { - altPrefix = `{big}${verticalWaypoint.isAltitudeConstraintMet ? "{magenta}*{end}" : "{amber}*{end}"}{end}`; - } - - altitudeConstraint = altPrefix + formatAltitudeOrLevel(mcdu, verticalWaypoint.altitude, useTransitionAltitude).padStart(5, "\xa0"); - altColor = color; - altSize = isFromLeg ? "big" : "small"; - } else if (hasAltConstraint) { - altitudeConstraint = formatAltConstraint(mcdu, wp.altitudeConstraint, useTransitionAltitude); - altColor = "magenta"; - altSize = wp.hasPilotEnteredAltitudeConstraint() ? "big" : "small"; - } else if (inAlternate && fpIndex === targetPlan.alternateFlightPlan.destinationLegIndex) { - if (legIsRunway(wp) && targetPlan.alternateFlightPlan.destinationRunway && Number.isFinite(targetPlan.alternateFlightPlan.destinationRunway.thresholdCrossingHeight)) { - altitudeConstraint = formatAlt(targetPlan.alternateFlightPlan.destinationRunway.thresholdCrossingHeight); - altColor = color; - altSize = "small"; - } else if (legIsAirport(wp) && targetPlan.alternateFlightPlan.destinationAirport && Number.isFinite(targetPlan.alternateFlightPlan.destinationAirport.location.alt)) { - altitudeConstraint = formatAlt(targetPlan.alternateFlightPlan.destinationAirport.location.alt); - altColor = color; - altSize = "small"; - } - } else if (inAlternate && fpIndex === targetPlan.alternateFlightPlan.originLegIndex) { - if (legIsRunway(wp) && targetPlan.alternateFlightPlan.originRunway && Number.isFinite(targetPlan.alternateFlightPlan.originRunway.location.alt)) { - altitudeConstraint = formatAlt(targetPlan.alternateFlightPlan.originRunway.location.alt); - altColor = color; - } else if (legIsAirport(wp) && targetPlan.alternateFlightPlan.originAirport && Number.isFinite(targetPlan.alternateFlightPlan.originAirport.location.alt)) { - altitudeConstraint = formatAlt(targetPlan.alternateFlightPlan.originAirport.location.alt); - altColor = color; - } - } else if (!inAlternate && fpIndex === targetPlan.destinationLegIndex) { - if (legIsRunway(wp) && targetPlan.destinationRunway && Number.isFinite(targetPlan.destinationRunway.thresholdCrossingHeight)) { - altitudeConstraint = formatAlt(targetPlan.destinationRunway.thresholdCrossingHeight); - altColor = color; - altSize = "small"; - } else if (legIsAirport(wp) && targetPlan.destinationAirport && Number.isFinite(targetPlan.destinationAirport.location.alt)) { - altitudeConstraint = formatAlt(targetPlan.destinationAirport.location.alt); - altColor = color; - altSize = "small"; - } - } else if (!inAlternate && fpIndex === targetPlan.originLegIndex) { - if (legIsRunway(wp) && targetPlan.originRunway && Number.isFinite(targetPlan.originRunway.location.alt)) { - altitudeConstraint = formatAlt(targetPlan.originRunway.location.alt); - altColor = color; - } else if (legIsAirport(wp) && targetPlan.originAirport && Number.isFinite(targetPlan.originAirport.location.alt)) { - altitudeConstraint = formatAlt(targetPlan.originAirport.location.alt); - altColor = color; - } - } - } - - // forced turn indication if next leg is not a course reversal - if (wpNext && wpNext.isDiscontinuity === false && legTurnIsForced(wpNext) && !legTypeIsCourseReversal(wpNext)) { - if (wpNext.definition.turnDirection === 'L') { - ident += "{"; - } else { - ident += "}"; - } - - // the overfly symbol is not shown in this case - isOverfly = false; - } - - scrollWindow[rowI] = { - fpIndex, - inAlternate: inAlternate, - active: wpActive, - ident: ident, - color, - distance, - fpa, - spdColor, - speedConstraint, - altColor, - altSize, - altitudeConstraint, - timeCell, - timeColor, - fixAnnotation: fixAnnotation ? fixAnnotation : "", - bearingTrack, - isOverfly, - }; - - if (fpIndex !== targetPlan.destinationLegIndex) { - addLskAt(rowI, - (value) => { - if (value === "") { - return mcdu.getDelaySwitchPage(); - } - return mcdu.getDelayBasic(); - }, - (value, scratchpadCallback) => { - switch (value) { - case "": - CDULateralRevisionPage.ShowPage(mcdu, wp, fpIndex, forPlan, inAlternate); - break; - case FMCMainDisplay.clrValue: - CDUFlightPlanPage.clearElement(mcdu, fpIndex, offset, forPlan, inAlternate, scratchpadCallback); - break; - case FMCMainDisplay.ovfyValue: - mcdu.toggleWaypointOverfly(fpIndex, forPlan, inAlternate, () => { - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - }); - break; - default: - if (value.length > 0) { - mcdu.insertWaypoint(value, forPlan, inAlternate, fpIndex, true, (success) => { - if (!success) { - scratchpadCallback(); - } - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - }, !mcdu.flightPlanService.hasTemporary); - } - break; - } - }); - } else { - addLskAt(rowI, () => mcdu.getDelaySwitchPage(), - (value, scratchpadCallback) => { - if (value === "") { - CDULateralRevisionPage.ShowPage(mcdu, wp, fpIndex, forPlan, inAlternate); - } else if (value.length > 0) { - mcdu.insertWaypoint(value, forPlan, inAlternate, fpIndex, true, (success) => { - if (!success) { - scratchpadCallback(); - } - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - }, true); - } - }); - } - - addRskAt(rowI, () => mcdu.getDelaySwitchPage(), - (value, scratchpadCallback) => { - if (value === "") { - CDUVerticalRevisionPage.ShowPage(mcdu, wp, fpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - } else { - CDUVerticalRevisionPage.setConstraints(mcdu, wp, fpIndex, verticalWaypoint, value, scratchpadCallback, offset, forPlan, inAlternate); - } - }); - - } else if (pwp) { - const baseColor = forActiveOrTemporary ? mcdu.flightPlanService.hasTemporary ? "yellow" : "green" : "white"; - const color = isActive ? "white" : baseColor; - - // TODO: PWP should not be shown while predictions are recomputed or in a temporary flight plan, - // but if I don't show them, the flight plan jumps around because the offset is no longer correct if the number of items in the flight plan changes. - // Like this, they are still there, but have dashes for predictions. - const shouldHidePredictions = !fmsGeometryProfile || !fmsGeometryProfile.isReadyToDisplay || !pwp.flightPlanInfo; - - let timeCell = Time.NoPrediction; - let timeColor = "white"; - if (!shouldHidePredictions && Number.isFinite(pwp.flightPlanInfo.secondsFromPresent)) { - const utcTime = SimVar.GetGlobalVarValue("ZULU TIME", "seconds"); - timeColor = color; - - timeCell = isFlying - ? `${FMCMainDisplay.secondsToUTC(utcTime + pwp.flightPlanInfo.secondsFromPresent)}[s-text]` - : `${FMCMainDisplay.secondsTohhmm(pwp.flightPlanInfo.secondsFromPresent)}[s-text]`; - } - - let speed = Speed.NoPrediction; - let spdColor = "white"; - if (!shouldHidePredictions && Number.isFinite(pwp.flightPlanInfo.speed)) { - speed = `{small}${pwp.flightPlanInfo.speed < 1 ? formatMachNumber(pwp.flightPlanInfo.speed) : Math.round(pwp.flightPlanInfo.speed).toFixed(0)}{end}`; - spdColor = color; - } - - let altitudeConstraint = Altitude.NoPrediction; - let altColor = "white"; - if (!shouldHidePredictions && Number.isFinite(pwp.flightPlanInfo.altitude)) { - altitudeConstraint = formatAltitudeOrLevel(mcdu, pwp.flightPlanInfo.altitude, useTransitionAltitude); - altColor = color; - } - - let distance = undefined; - if (!shouldHidePredictions) { - distance = isActive - ? mcdu.guidanceController.activeLegAlongTrackCompletePathDtg - pwp.distanceFromLegTermination - : distanceFromLastLine; - } - - scrollWindow[rowI] = { - fpIndex: fpIndex, - active: isActive, - ident: pwp.mcduIdent || pwp.ident, - color, - distance: distance !== undefined ? Math.round(Math.max(0, Math.min(9999, distance))).toFixed(0) : "", - spdColor, - speedConstraint: speed, - altColor, - altSize: "small", - altitudeConstraint, - timeCell, - timeColor, - fixAnnotation: `{${color}}${pwp.mcduHeader || ''}{end}`, - bearingTrack, - isOverfly: false, - }; - - addLskAt(rowI, 0, (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - // TODO - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - } - }); - } else if (marker) { - // Marker - scrollWindow[rowI] = waypointsAndMarkers[winI]; - addLskAt(rowI, 0, (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - CDUFlightPlanPage.clearElement(mcdu, fpIndex, offset, forPlan, inAlternate, scratchpadCallback); - return; - } else if (value === "") { - return; - } - - // Insert after last leg if we click on a marker after the flight plan - const insertionIndex = fpIndex < targetPlan.legCount ? fpIndex : targetPlan.legCount - 1; - const insertBeforeVsAfterIndex = fpIndex < targetPlan.legCount; - - // Cannot insert after MANUAL leg - const previousElement = targetPlan.maybeElementAt(fpIndex - 1); - if (previousElement && previousElement.isDiscontinuity === false && previousElement.isVectors()) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } - - mcdu.insertWaypoint(value, forPlan, inAlternate, insertionIndex, insertBeforeVsAfterIndex, (success) => { - if (!success) { - scratchpadCallback(); - } - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - }, !mcdu.flightPlanService.hasTemporary); - }); - } else if (holdResumeExit && holdResumeExit.isDiscontinuity === false) { - const isNext = fpIndex === (targetPlan.activeLegIndex + 1); - - let color = legAccentColor; - if (isActive) { - color = "white"; - } - - const decelReached = isActive || isNext && mcdu.holdDecelReached; - const holdSpeed = fpIndex === mcdu.holdIndex && mcdu.holdSpeedTarget > 0 ? mcdu.holdSpeedTarget.toFixed(0) : '\xa0\xa0\xa0'; - const turnDirection = holdResumeExit.definition.turnDirection; - // prompt should only be shown once entering decel for hold (3 - 20 NM before hold) - const immExit = decelReached && !holdResumeExit.holdImmExit; - const resumeHold = decelReached && holdResumeExit.holdImmExit; - - scrollWindow[rowI] = { - fpIndex, - holdResumeExit, - color, - immExit, - resumeHold, - holdSpeed, - turnDirection, - }; - - addLskAt(rowI, 0, (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - CDUFlightPlanPage.clearElement(mcdu, fpIndex, offset, forPlan, inAlternate, scratchpadCallback); - return; - } - - CDUHoldAtPage.ShowPage(mcdu, fpIndex, forPlan, inAlternate); - scratchpadCallback(); - }); - - addRskAt(rowI, 0, (value, scratchpadCallback) => { - // IMM EXIT, only active once reaching decel - if (isActive) { - mcdu.fmgcMesssagesListener.triggerToAllSubscribers('A32NX_IMM_EXIT', fpIndex, immExit); - setTimeout(() => { - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - }, 500); - } else if (decelReached) { - CDUFlightPlanPage.clearElement(mcdu, fpIndex, offset, forPlan, inAlternate, scratchpadCallback); - return; - } - scratchpadCallback(); - }); - } - } - - CDUFlightPlanPage.updatePlanCentre(mcdu, waypointsAndMarkers, offset, forPlan, 'L'); - CDUFlightPlanPage.updatePlanCentre(mcdu, waypointsAndMarkers, offset, forPlan, 'R'); - - const isMissedApproachLegShown = targetPlan && scrollWindow.some( - (row, index) => index > 0 && !row.marker && row.fpIndex >= targetPlan.firstMissedApproachLegIndex - ); - const isAlternateLegShown = scrollWindow.some((row, index) => index > 0 && !row.marker && row.inAlternate); - const isAlternateMissedApproachLegShown = targetPlan && targetPlan.alternateFlightPlan && scrollWindow.some( - (row, index) => index > 0 && !row.marker && row.inAlternate && row.fpIndex >= targetPlan.alternateFlightPlan.firstMissedApproachLegIndex - ); - - mcdu.efisInterfaces['L'].setShownFplnLegs(isMissedApproachLegShown, isAlternateLegShown, isAlternateMissedApproachLegShown); - mcdu.efisInterfaces['R'].setShownFplnLegs(isMissedApproachLegShown, isAlternateLegShown, isAlternateMissedApproachLegShown); - - mcdu.onUnload = () => { - CDUFlightPlanPage.updatePlanCentre(mcdu, waypointsAndMarkers, 0, Fmgc.FlightPlanIndex.Active, 'L'); - CDUFlightPlanPage.updatePlanCentre(mcdu, waypointsAndMarkers, 0, Fmgc.FlightPlanIndex.Active, 'R'); - - mcdu.efisInterfaces['L'].setShownFplnLegs(false, false, false); - mcdu.efisInterfaces['R'].setShownFplnLegs(false, false, false); - }; - - // Render scrolling data to text >> add ditto marks - - let firstWp = scrollWindow.length; - const scrollText = []; - for (let rowI = 0; rowI < scrollWindow.length; rowI++) { - const { marker: cMarker, holdResumeExit: cHold, spdColor: cSpdColor, altColor: cAltColor, speedConstraint: cSpd, altitudeConstraint: cAlt, ident: cIdent } = scrollWindow[rowI]; - let spdRpt = false; - let altRpt = false; - let showFix = true; - let showDist = true; - let showNm = false; - - if (cHold) { - const { color, immExit, resumeHold, holdSpeed, turnDirection } = scrollWindow[rowI]; - scrollText[(rowI * 2)] = ["", `{amber}${immExit ? 'IMM\xa0\xa0' : ''}${resumeHold ? 'RESUME\xa0' : ''}{end}`, 'HOLD\xa0\xa0\xa0\xa0']; - scrollText[(rowI * 2) + 1] = [`{${color}}HOLD ${turnDirection}{end}`, `{amber}${immExit ? 'EXIT*' : ''}${resumeHold ? 'HOLD*' : ''}{end}`, `\xa0{${color}}{small}{white}SPD{end}\xa0${holdSpeed}{end}{end}`]; - } else if (!cMarker) { // Waypoint - if (rowI > 0) { - const { marker: pMarker, pwp: pPwp, holdResumeExit: pHold, speedConstraint: pSpd, altitudeConstraint: pAlt } = scrollWindow[rowI - 1]; - if (!pMarker && !pPwp && !pHold) { - firstWp = Math.min(firstWp, rowI); - if (rowI === firstWp) { - showNm = true; - } - if (cSpd !== Speed.NoPrediction && cSpdColor !== "magenta" && cSpd === pSpd) { - spdRpt = true; - } - - if (cAlt !== Altitude.NoPrediction && cAltColor !== "magenta" && cAlt === pAlt) { - altRpt = true; - } - // If previous row is a marker, clear all headers unless it's a speed limit - } else if (!pHold) { - showDist = false; - showFix = cIdent === "(LIM)"; - } - } - - scrollText[(rowI * 2)] = renderFixHeader(scrollWindow[rowI], showNm, showDist, showFix); - scrollText[(rowI * 2) + 1] = renderFixContent(scrollWindow[rowI], spdRpt, altRpt); - - // Marker - } else { - scrollText[(rowI * 2)] = []; - scrollText[(rowI * 2) + 1] = cMarker; - } - } - - // Destination (R6) - - const destText = []; - if (mcdu.flightPlanService.hasTemporary) { - destText[0] = [" ", " "]; - destText[1] = ["{ERASE[color]amber", "INSERT*[color]amber"]; - - addLskAt(5, 0, async () => { - mcdu.eraseTemporaryFlightPlan(() => { - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }); - addRskAt(5, 0, async () => { - mcdu.insertTemporaryFlightPlan(() => { - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }); - } else { - let destCell = "----"; - if (targetPlan.destinationAirport) { - destCell = targetPlan.destinationAirport.ident; - - if (targetPlan.destinationRunway) { - destCell = targetPlan.destinationRunway.ident; - } - } - let destTimeCell = "----"; - let destDistCell = "----"; - let destEFOBCell = "---.-"; - - if (targetPlan.destinationAirport) { - if (CDUInitPage.fuelPredConditionsMet(mcdu) && mcdu._fuelPredDone) { - mcdu.tryUpdateRouteTrip(isFlying); - } - - const destDist = mcdu.guidanceController.alongTrackDistanceToDestination; - - if (Number.isFinite(destDist)) { - destDistCell = Math.round(destDist).toFixed(0); - } - - if (fmsGeometryProfile && fmsGeometryProfile.isReadyToDisplay) { - const destEfob = fmsGeometryProfile.getRemainingFuelAtDestination(); - - if (Number.isFinite(destEfob)) { - destEFOBCell = (NXUnits.poundsToUser(destEfob) / 1000).toFixed(1); - } - - const timeRemaining = fmsGeometryProfile.getTimeToDestination(); - if (Number.isFinite(timeRemaining)) { - const utcTime = SimVar.GetGlobalVarValue("ZULU TIME", "seconds"); - - destTimeCell = isFlying - ? FMCMainDisplay.secondsToUTC(utcTime + timeRemaining) - : FMCMainDisplay.secondsTohhmm(timeRemaining); - } - } - } - - destText[0] = ["\xa0DEST", "DIST\xa0\xa0EFOB", isFlying ? "\xa0UTC{sp}{sp}{sp}{sp}" : "TIME{sp}{sp}{sp}{sp}"]; - destText[1] = [destCell, `{small}${destDistCell.padStart(4, '\xa0')}\xa0${destEFOBCell.padStart(5, '\xa0')}{end}`, `{small}${destTimeCell}{end}{sp}{sp}{sp}{sp}`]; - - addLskAt(5, () => mcdu.getDelaySwitchPage(), - () => { - CDULateralRevisionPage.ShowPage(mcdu, targetPlan.destinationLeg, targetPlan.destinationLegIndex, forPlan); - }); - - addRskAt(5, () => mcdu.getDelaySwitchPage(), - () => { - CDUVerticalRevisionPage.ShowPage(mcdu, targetPlan.destinationLeg, targetPlan.destinationLegIndex, undefined, undefined, undefined, undefined, forPlan, false); - }); - } - - // scrollText pad to 10 rows - while (scrollText.length < 10) { - scrollText.push([""]); - } - const allowScroll = waypointsAndMarkers.length > 4; - if (allowScroll) { - mcdu.onAirport = () => { // Only called if > 4 waypoints - const isOnFlightPlanPage = mcdu.page.Current === mcdu.page.FlightPlanPage; - const allowCycleToOriginAirport = mcdu.flightPhaseManager.phase === FmgcFlightPhases.PREFLIGHT; - if (offset >= Math.max(destinationAirportOffset, alternateAirportOffset) && allowCycleToOriginAirport && isOnFlightPlanPage) { // only show origin if still on ground - // Go back to top of flight plan page to show origin airport. - offset = 0; - } else if (offset >= destinationAirportOffset && offset < alternateAirportOffset) { - offset = alternateAirportOffset; - } else { - offset = destinationAirportOffset; - } - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - }; - mcdu.onDown = () => { // on page down decrement the page offset. - if (offset > 0) { // if page not on top - offset--; - } else { // else go to the bottom - offset = waypointsAndMarkers.length - 1; - } - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - }; - mcdu.onUp = () => { - if (offset < waypointsAndMarkers.length - 1) { // if page not on bottom - offset++; - } else { // else go on top - offset = 0; - } - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - }; - } - mcdu.setArrows(allowScroll, allowScroll, true, true); - scrollText[0][1] = "SPD/ALT\xa0\xa0\xa0"; - scrollText[0][2] = isFlying ? "\xa0UTC{sp}{sp}{sp}{sp}" : "TIME{sp}{sp}{sp}{sp}"; - mcdu.setTemplate([ - [`{left}{small}{sp}${showFrom ? "FROM" : "{sp}{sp}{sp}{sp}"}{end}${headerText}{end}{right}{small}${flightNumberText}{sp}{sp}{sp}{end}{end}`], - ...scrollText, - ...destText - ]); - } - - static async clearElement(mcdu, fpIndex, offset, forPlan, forAlternate, scratchpadCallback) { - if (!this.ensureCanClearElement(mcdu, fpIndex, forPlan, forAlternate, scratchpadCallback)) { - return; - } - - const targetPlan = mcdu.flightPlan(forPlan, forAlternate); - const element = targetPlan.elementAt(fpIndex); - - const previousElement = targetPlan.maybeElementAt(fpIndex - 1); - - let insertDiscontinuity = true; - if (element.isDiscontinuity === false) { - if (element.isHX() || (!forAlternate && fpIndex <= targetPlan.activeLegIndex)) { - insertDiscontinuity = false; - } else if (previousElement.isDiscontinuity === false && previousElement.type === 'PI' && element.type === 'CF') { - insertDiscontinuity = element.waypoint.databaseId === previousElement.recommendedNavaid.databaseId; - } - } else { - insertDiscontinuity = false; - } - - try { - await mcdu.flightPlanService.deleteElementAt(fpIndex, insertDiscontinuity, forPlan, forAlternate); - console.log("deleting element"); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - scratchpadCallback(); - } - - CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); - } - - static ensureCanClearElement(mcdu, fpIndex, forPlan, forAlternate, scratchpadCallback) { - const targetPlan = mcdu.flightPlan(forPlan, forAlternate); - - if (forPlan === Fmgc.FlightPlanIndex.Active && mcdu.flightPlanService.hasTemporary) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return false; - } else if (fpIndex === targetPlan.originLegIndex || fpIndex === targetPlan.destinationLegIndex) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return false; - } - - // TODO maybe move this to FMS logic ? - if (forPlan === Fmgc.FlightPlanIndex.Active && !forAlternate && fpIndex <= mcdu.flightPlanService.activeLegIndex) { - // 22-72-00:67 - // Stop clearing TO or FROM waypoints when NAV is engaged - if (mcdu.navModeEngaged()) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowedInNav); - scratchpadCallback(); - return false; - } else if (fpIndex === targetPlan.fromLegIndex /* TODO check this is ppos */ && fpIndex + 2 === targetPlan.destinationLegIndex) { - const nextElement = targetPlan.elementAt(fpIndex + 1); - if (nextElement.isDiscontinuity === true) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return false; - } - } - } - - const element = targetPlan.maybeElementAt(fpIndex); - - if (!element) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return false; - } - - const previousElement = targetPlan.maybeElementAt(fpIndex - 1); - - if (element.isDiscontinuity === true) { - if (previousElement && previousElement.isDiscontinuity === false && previousElement.isVectors()) { - // Cannot clear disco after MANUAL - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return false; - } else if (fpIndex - 1 === targetPlan.fromLegIndex && forPlan === Fmgc.FlightPlanIndex.Active && !forAlternate) { - // Cannot clear disco after FROM leg - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return false; - } else if (fpIndex - 1 === targetPlan.originLegIndex && fpIndex + 1 === targetPlan.destinationLegIndex) { - if (targetPlan.originAirport.ident === targetPlan.destinationAirport.ident) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return false; - } - } - } - - return true; - } - - static updatePlanCentre(mcdu, waypointsAndMarkers, offset, forPlan, side) { - const forActiveOrTemporary = forPlan === 0; - const targetPlan = forActiveOrTemporary ? mcdu.flightPlanService.activeOrTemporary : mcdu.flightPlanService.secondary(1); - - for (let i = 0; i < waypointsAndMarkers.length; i++) { - const { wp, inAlternate, fpIndex } = waypointsAndMarkers[(offset + i + 1) % waypointsAndMarkers.length]; - if (wp) { - mcdu.efisInterfaces[side].setPlanCentre(targetPlan.index, fpIndex, inAlternate); - break; - } - } - } -} - -function renderFixTableHeader(isFlying) { - return [ - `{sp}\xa0FROM`, - "SPD/ALT\xa0\xa0\xa0", - isFlying ? "\xa0UTC{sp}{sp}{sp}{sp}" : "TIME{sp}{sp}{sp}{sp}" - ]; -} - -function renderFixHeader(rowObj, showNm = false, showDist = true, showFix = true) { - const { fixAnnotation, color, distance, bearingTrack, fpa } = rowObj; - let right = showDist ? `{${color}}${distance}{end}` : ''; - if (fpa) { - right += `{white}${fpa}°{end}`; - } else if (showNm) { - right += `{${color}}NM{end}\xa0\xa0\xa0`; - } else { - right += '\xa0\xa0\xa0\xa0\xa0'; - } - return [ - `${(showFix) ? fixAnnotation.padEnd(7, "\xa0").padStart(8, "\xa0") : ""}`, - right, - `{${color}}${bearingTrack}{end}\xa0`, - ]; -} - -function renderFixContent(rowObj, spdRepeat = false, altRepeat = false) { - const {ident, isOverfly, color, spdColor, speedConstraint, altColor, altSize, altitudeConstraint, timeCell, timeColor} = rowObj; - - return [ - `${ident}${isOverfly ? FMCMainDisplay.ovfyValue : ""}[color]${color}`, - `{${spdColor}}${spdRepeat ? "\xa0\"\xa0" : speedConstraint}{end}{${altColor}}{${altSize}}/${altRepeat ? "\xa0\xa0\xa0\"\xa0\xa0" : altitudeConstraint.padStart(6, "\xa0")}{end}{end}`, - `${timeCell}{sp}{sp}{sp}{sp}[color]${timeColor}` - ]; -} - -function emptyFplnPage(forPlan) { - return [ - ["", "SPD/ALT{sp}{sp}{sp}", "TIME{sp}{sp}{sp}{sp}"], - [`PPOS[color]${forPlan === 0 ? 'green' : 'white'}`, "{sp}{sp}{sp}/ -----", "----{sp}{sp}{sp}{sp}"], - [""], - ["---F-PLN DISCONTINUITY---"], - [""], - ["------END OF F-PLN-------"], - [""], - ["-----NO ALTN F-PLN-------"], - [""], - [""], - ["\xa0DEST", "DIST\xa0\xa0EFOB", "TIME{sp}{sp}{sp}{sp}"], - ["-------", "----\xa0---.-[s-text]", "----{sp}{sp}{sp}{sp}[s-text]"] - ]; -} - -/** - * Check whether leg is a course reversal leg - * @param {FlightPlanLeg} leg - * @returns true if leg is a course reversal leg - */ -function legTypeIsCourseReversal(leg) { - switch (leg.type) { - case 'HA': - case 'HF': - case 'HM': - case 'PI': - return true; - default: - } - return false; -} - -/** - * Check whether leg has a coded forced turn direction - * @param {FlightPlanLeg} leg - * @returns true if leg has coded forced turn direction - */ -function legTurnIsForced(leg) { - // forced turns are only for straight legs - return (leg.definition.turnDirection === 'L' /* Left */ || leg.definition.turnDirection === 'R' /* Right */) - && leg.type !== 'AF' && leg.type !== 'RF'; -} - -function formatMachNumber(rawNumber) { - return (Math.round(100 * rawNumber) / 100).toFixed(2).slice(1); -} - -/** - * @param {FlightPlanLeg} leg - * @return {boolean} - */ -function legHasAltConstraint(leg) { - return !leg.isXA() && (leg.hasPilotEnteredAltitudeConstraint() || leg.hasDatabaseAltitudeConstraint()); -} - -/** - * @param {FlightPlanLeg} leg - * @return {boolean} - */ -function legIsRunway(leg) { - return leg.definition && leg.definition.waypointDescriptor === 4; -} - -/** - * @param {FlightPlanLeg} leg - * @return {boolean} - */ -function legIsAirport(leg) { - return leg.definition && leg.definition.waypointDescriptor === 1; -} - -/** - * Formats an altitude as an altitude or flight level for display. - * @param {*} mcdu Reference to the MCDU instance - * @param {number} altitudeToFormat The altitude in feet. - * @param {boolean} useTransAlt Whether to use transition altitude, otherwise transition level is used. - * @returns {string} The formatted altitude/level. - */ -function formatAltitudeOrLevel(mcdu, alt, useTransAlt) { - const activePlan = mcdu.flightPlanService.active; - - let isFl = false; - if (useTransAlt) { - const transAlt = activePlan.performanceData.transitionAltitude; - isFl = transAlt !== null && alt > transAlt; - } else { - const transLevel = activePlan.performanceData.transitionLevel; - isFl = transLevel !== null && alt >= (transLevel * 100); - } - - if (isFl) { - return `FL${(alt / 100).toFixed(0).padStart(3,"0")}`; - } - - return formatAlt(alt); -} - -function formatTrack(from, to) { - // TODO: Does this show something for non-waypoint terminated legs? - if (!from || !from.definition || !from.definition.waypoint || !from.definition.waypoint.location || !to || !to.definition || !to.definition.waypoint || to.definition.type === "HM") { - return ""; - } - - const magVar = Facilities.getMagVar(from.definition.waypoint.location.lat, from.definition.waypoint.location.long); - const tr = Avionics.Utils.computeGreatCircleHeading(from.definition.waypoint.location, to.definition.waypoint.location); - const track = A32NX_Util.trueToMagnetic(tr, magVar); - return `TRK${track.toFixed(0).padStart(3,"0")}\u00b0`; -} - -/** - * Formats a numberical altitude to a string to be displayed in the altitude column. Does not format FLs, use {@link formatAltitudeOrLevel} for this purpose - * @param {Number} alt The altitude to format - * @returns {String} The formatted altitude string - */ -function formatAlt(alt) { - return (Math.round(alt / 10) * 10).toFixed(0); -} - -function formatAltConstraint(mcdu, constraint, useTransAlt) { - if (!constraint) { - return ''; - } - - // Altitude constraint types "G" and "H" are not shown in the flight plan - switch (constraint.altitudeDescriptor) { - case '@': // AtAlt1 - case 'I': // AtAlt1GsIntcptAlt2 - case 'X': // AtAlt1AngleAlt2 - return formatAltitudeOrLevel(mcdu, constraint.altitude1, useTransAlt); - case '+': // AtOrAboveAlt1 - case 'J': // AtOrAboveAlt1GsIntcptAlt2 - case 'V': // AtOrAboveAlt1AngleAlt2 - return '+' + formatAltitudeOrLevel(mcdu, constraint.altitude1, useTransAlt); - case '-': // AtOrBelowAlt1 - case 'Y': // AtOrBelowAlt1AngleAlt2 - return '-' + formatAltitudeOrLevel(mcdu, constraint.altitude1, useTransAlt); - case 'B': // BetweenAlt1Alt2 - return 'WINDOW'; - case 'C': // AtOrAboveAlt2: - return '+' + formatAltitudeOrLevel(mcdu, constraint.altitude2, useTransAlt); - default: - return ''; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Format.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Format.js deleted file mode 100644 index dde4be8b212..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Format.js +++ /dev/null @@ -1,116 +0,0 @@ -/** Used to displayed data on a mcdu page when using the formatting helper */ -class Column { - /** - * @param {number} index - valid range from 0 to 23 - * @param {string} text - text to be displayed - * @param {...({ color: string } | { size: string } | { align: bool })} att - attributes of the text, e.g. text size, color and/or alignment - */ - constructor(index, text, ...att) { - this.index = index; - this.raw = text; - this.color = att.find(e => e.color) || Column.white; - this.length = text.length; - this.anchorPos = !!att.find(e => e.align) ? index - this.length + 1 : index; - const size = att.find(e => e.size); - this.size = !!size ? [`{${size["size"]}}`, "{end}"] : ["", ""]; - } - - /** - * Returns a styled/formatted string. - * @returns {string} - */ - get text() { - return `${this.size[0]}{${this.color.color}}${this.raw}{end}${this.size[1]}`; - } - - /** - * @param {string} text - text to be displayed - */ - set text(text) { - this.raw = text; - this.length = text.length; - - // if text is right aligned => update anchor position - if (this.index !== this.anchorPos) { - this.anchorPos = this.index - this.length + 1; - } - } - - /** - * @param {...({ color: string } | { size: string })} att - attributes of the text, e.g. text size and/or color - */ - updateAttributes(...att) { - this.color = att.find(e => e.color) || this.color; - const size = att.find(e => e.size); - this.size = !!size ? [`{${size["size"]}}`, "{end}"] : this.size; - } - - /** - * @param {string} text - text to be displayed - * @param {...({ color: string } | { size: string })} att - attributes of the text, e.g. text size and/or color - */ - update(text, ...att) { - this.text = text; - this.updateAttributes(...att); - } -} - -Column.right = { "align": true }; -Column.small = { "size": "small" }; -Column.big = { "size": "big" }; -Column.amber = { "color": "amber" }; -Column.red = { "color": "red" }; -Column.green = { "color": "green" }; -Column.cyan = { "color": "cyan" }; -Column.white = { "color": "white" }; -Column.magenta = { "color": "magenta" }; -Column.yellow = { "color": "yellow" }; -Column.inop = { "color": "inop" }; - -/** - * Returns a formatted mcdu page template - * @param {Column[][]} lines - mcdu lines - * @returns {string[][]} mcdu template - */ -const FormatTemplate = lines => lines.map(line => FormatLine(...line)); - -/** - * Returns a formatted mcdu line - * @param {...Column} columns - * @returns {string[]} - */ -function FormatLine(...columns) { - columns.sort((a, b) => a.anchorPos - b.anchorPos); - - let line = "".padStart(24); - let pos = -1; // refers to an imaginary index on the 24 char limited mcdu line - let index = 0; // points at the "cursor" of the actual line - - for (const column of columns) { - /* --------> populating text from left to right --------> */ - const newStart = column.anchorPos; - const newEnd = newStart + column.length; - - // prevent adding empty or invalid stuff - if (column.length === 0 || newEnd < 0 || newStart > 23 || newEnd <= pos) { - continue; - } - - if (pos === -1) { - pos = 0; - } - - // removes text overlap - column.raw = column.raw.slice(Math.max(0, pos - newStart), Math.min(column.length, Math.max(1, column.length + 24 - newEnd))); - - const limMin = Math.max(0, newStart - pos); - const limMax = Math.min(24, newEnd - pos); - - line = line.slice(0, index + limMin) + column.text + line.slice(index + limMax); - index += limMin + column.text.length; - pos = newEnd; - } - - // '{small}{end}{big}{end}' fixes the lines "jumping" (line moves up or down a few pixels) when entering small and large content into the same line. - return [line.replace(/\s/g, '{sp}') + "{small}{end}{big}{end}"]; -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_FuelPredPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_FuelPredPage.js deleted file mode 100644 index c5192400015..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_FuelPredPage.js +++ /dev/null @@ -1,301 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUFuelPredPage { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.FuelPredPage; - mcdu.pageRedrawCallback = () => CDUFuelPredPage.ShowPage(mcdu); - mcdu.activeSystem = 'FMGC'; - const isFlying = mcdu.isFlying(); - - const destination = mcdu.flightPlanService.active ? mcdu.flightPlanService.active.destinationAirport : undefined; - const alternate = mcdu.flightPlanService.active ? mcdu.flightPlanService.active.alternateDestinationAirport : undefined; - - let destIdentCell = "NONE"; - let destTimeCell = "----"; - let destTimeCellColor = "[color]white"; - let destEFOBCell = "---.-"; - let destEFOBCellColor = "[color]white"; - - let altIdentCell = "NONE"; - let altTimeCell = "----"; - let altTimeCellColor = "[color]white"; - let altEFOBCell = "---.-"; - let altEFOBCellColor = "[color]white"; - - let rteRsvWeightCell = "---.-"; - let rteRsvPercentCell = "---.-"; - let rteRSvCellColor = "[color]white"; - let rteRsvPctColor = "{white}"; - - let zfwCell = "___._"; - let zfwCgCell = (" __._"); - let zfwColor = "[color]amber"; - mcdu.onRightInput[2] = async (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } else if (value === "") { - mcdu.setScratchpadText( - (isFinite(getZfw()) ? (getZfw() / 1000).toFixed(1) : "") + - "/" + - (isFinite(getZfwcg()) ? getZfwcg().toFixed(1) : "")); - } else { - if (mcdu.trySetZeroFuelWeightZFWCG(value)) { - CDUFuelPredPage.ShowPage(mcdu); - CDUInitPage.trySetFuelPred(mcdu); - mcdu.removeMessageFromQueue(NXSystemMessages.initializeWeightOrCg.text); - mcdu.removeMessageFromQueue(NXSystemMessages.checkWeight.text); - mcdu._checkWeightSettable = true; - } else { - scratchpadCallback(); - } - } - }; - - let altFuelCell = "---.-"; - let altFuelTimeCell = "----"; - let altFuelColor = "[color]white"; - let altTimeColor = "{white}"; - - let fobCell = "---.-"; - let fobOtherCell = "-----"; - let fobCellColor = "[color]white"; - - let finalFuelCell = "---.-"; - let finalTimeCell = "----"; - let finalColor = "[color]white"; - - let gwCell = "---.-"; - let cgCell = " --.-"; - let gwCgCellColor = "[color]white"; - - let minDestFobCell = "---.-"; - let minDestFobCellColor = "[color]white"; - - let extraFuelCell = "---.-"; - let extraTimeCell = "----"; - let extraCellColor = "[color]white"; - let extraTimeColor = "{white}"; - - if (mcdu._zeroFuelWeightZFWCGEntered) { - if (isFinite(mcdu.zeroFuelWeight)) { - zfwCell = (NXUnits.kgToUser(mcdu.zeroFuelWeight)).toFixed(1); - zfwColor = "[color]cyan"; - } - if (isFinite(mcdu.zeroFuelWeightMassCenter)) { - zfwCgCell = getZfwcg().toFixed(1); - } - if (isFinite(mcdu.zeroFuelWeight) && isFinite(getZfwcg())) { - zfwColor = "[color]cyan"; - } - - if (alternate) { - altIdentCell = alternate.ident; - } - - if (destination) { - destIdentCell = destination.ident; - } - - gwCell = "{small}" + (NXUnits.kgToUser(mcdu.getGW()).toFixed(1)); - cgCell = mcdu.getCG().toFixed(1) + "{end}"; - gwCgCellColor = "[color]green"; - - fobCell = "{small}" + (NXUnits.kgToUser(mcdu.getFOB())).toFixed(1) + "{end}"; - fobOtherCell = "{inop}FF{end}"; - fobCellColor = "[color]cyan"; - } - - if (CDUInitPage.fuelPredConditionsMet(mcdu)) { - const utcTime = SimVar.GetGlobalVarValue("ZULU TIME", "seconds"); - - if (mcdu._fuelPredDone) { - if (!mcdu.routeFinalEntered()) { - mcdu.tryUpdateRouteFinalFuel(); - } - if (isFinite(mcdu.getRouteFinalFuelWeight()) && isFinite(mcdu.getRouteFinalFuelTime())) { - if (mcdu._rteFinalWeightEntered) { - finalFuelCell = "{sp}{sp}" + (NXUnits.kgToUser(mcdu.getRouteFinalFuelWeight())).toFixed(1); - } else { - finalFuelCell = "{sp}{sp}{small}" + (NXUnits.kgToUser(mcdu.getRouteFinalFuelWeight())).toFixed(1) + "{end}"; - } - if (mcdu._rteFinalTimeEntered || !mcdu.routeFinalEntered()) { - finalTimeCell = FMCMainDisplay.minutesTohhmm(mcdu.getRouteFinalFuelTime()); - } else { - finalTimeCell = "{small}" + FMCMainDisplay.minutesTohhmm(mcdu.getRouteFinalFuelTime()) + "{end}"; - } - finalColor = "[color]cyan"; - } - mcdu.onLeftInput[4] = async (value, scratchpadCallback) => { - if (await mcdu.trySetRouteFinalFuel(value)) { - CDUFuelPredPage.ShowPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - if (alternate) { - const altnFuelEntered = mcdu._routeAltFuelEntered; - if (!altnFuelEntered) { - mcdu.tryUpdateRouteAlternate(); - } - if (isFinite(mcdu.getRouteAltFuelWeight())) { - altFuelCell = "{sp}{sp}" + (altnFuelEntered ? "" : "{small}") + (NXUnits.kgToUser(mcdu.getRouteAltFuelWeight())).toFixed(1); - altFuelColor = "[color]cyan"; - const time = mcdu.getRouteAltFuelTime(); - if (time) { - altFuelTimeCell = "{small}" + FMCMainDisplay.minutesTohhmm(time) + "{end}"; - altTimeColor = "{green}"; - } else { - altFuelTimeCell = "----"; - altTimeColor = "{white}"; - } - } - } else { - altFuelCell = "{sp}{sp}{small}0.0{end}"; - altFuelTimeCell = "----"; - altFuelColor = "[color]green"; - altTimeColor = "{white}"; - } - mcdu.onLeftInput[3] = async (value, scratchpadCallback) => { - if (await mcdu.trySetRouteAlternateFuel(value)) { - CDUFuelPredPage.ShowPage(mcdu); - } else { - scratchpadCallback(); - } - }; - if (alternate) { - altIdentCell = alternate.ident; - altEFOBCell = (NXUnits.kgToUser(mcdu.getAltEFOB(true))).toFixed(1); - altEFOBCellColor = "[color]green"; - } - - mcdu.tryUpdateRouteTrip(isFlying); - - const dest = mcdu.flightPlanService.active.destinationAirport; - - if (dest) { - destIdentCell = dest.ident; - } - const efob = mcdu.getDestEFOB(true); - destEFOBCell = (NXUnits.kgToUser(efob)).toFixed(1); - // Should we use predicted values or liveETATo and liveUTCto? - destTimeCell = isFlying ? FMCMainDisplay.secondsToUTC(utcTime + FMCMainDisplay.minuteToSeconds(mcdu._routeTripTime)) - : destTimeCell = FMCMainDisplay.minutesTohhmm(mcdu._routeTripTime); - - if (alternate) { - if (mcdu.getRouteAltFuelTime()) { - altTimeCell = isFlying ? FMCMainDisplay.secondsToUTC(utcTime + FMCMainDisplay.minuteToSeconds(mcdu._routeTripTime) + FMCMainDisplay.minuteToSeconds(mcdu.getRouteAltFuelTime())) - : FMCMainDisplay.minutesTohhmm(mcdu.getRouteAltFuelTime()); - altTimeCellColor = "[color]green"; - } else { - altTimeCell = "----"; - altTimeCellColor = "[color]white"; - } - } - - destTimeCellColor = "[color]green"; - - rteRsvWeightCell = "{sp}{sp}" + (NXUnits.kgToUser(mcdu.getRouteReservedWeight())).toFixed(1); - if (!mcdu._rteReservedWeightEntered) { - rteRsvWeightCell = "{small}" + rteRsvWeightCell + "{end}"; - } - - if (mcdu._rteRsvPercentOOR) { - rteRsvPercentCell = "--.-"; - rteRSvCellColor = "[color]cyan"; - rteRsvPctColor = "{white}"; - } else { - rteRsvPercentCell = mcdu.getRouteReservedPercent().toFixed(1); - if (isFlying || (!mcdu._rteReservedPctEntered && mcdu.routeReservedEntered())) { - rteRsvPercentCell = "{small}" + rteRsvPercentCell + "{end}"; - } - rteRsvPctColor = isFlying ? "{green}" : "{cyan}"; - rteRSvCellColor = isFlying ? "[color]green" : "[color]cyan"; - } - - mcdu.onLeftInput[2] = async (value, scratchpadCallback) => { - if (await mcdu.trySetRouteReservedFuel(value)) { - CDUFuelPredPage.ShowPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - if (mcdu._minDestFobEntered) { - minDestFobCell = "{sp}{sp}" + (NXUnits.kgToUser(mcdu._minDestFob)).toFixed(1); - minDestFobCellColor = "[color]cyan"; - } else { - mcdu.tryUpdateMinDestFob(); - minDestFobCell = "{sp}{sp}{small}" + (NXUnits.kgToUser(mcdu._minDestFob)).toFixed(1) + "{end}"; - minDestFobCellColor = "[color]cyan"; - } - mcdu.onLeftInput[5] = async (value, scratchpadCallback) => { - if (await mcdu.trySetMinDestFob(value)) { - CDUFuelPredPage.ShowPage(mcdu); - } else { - scratchpadCallback(); - } - }; - mcdu.checkEFOBBelowMin(); - - extraFuelCell = "{small}" + (NXUnits.kgToUser(mcdu.tryGetExtraFuel(true))).toFixed(1); - if (mcdu.tryGetExtraFuel(true) < 0) { - extraTimeCell = "----{end}"; - extraTimeColor = "{white}"; - } else { - extraTimeCell = FMCMainDisplay.minutesTohhmm(mcdu.tryGetExtraTime(true)) + "{end}"; - extraTimeColor = "{green}"; - } - extraCellColor = "[color]green"; - - // Currently not updating as there's no simvar to retrieve this. - if (isFinite(mcdu.zeroFuelWeight)) { - zfwCell = (NXUnits.kgToUser(mcdu.zeroFuelWeight)).toFixed(1); - zfwColor = "[color]cyan"; - } - if (isFinite(mcdu.zeroFuelWeightMassCenter)) { - zfwCgCell = mcdu.zeroFuelWeightMassCenter.toFixed(1); - } - - destEFOBCellColor = mcdu._isBelowMinDestFob ? "[color]amber" : "[color]green"; - } - } - - mcdu.setTemplate([ - ["FUEL PRED{sp}"], - ["\xa0AT", "EFOB", isFlying ? "{sp}UTC" : "TIME"], - [destIdentCell + "[color]green", destEFOBCell + destEFOBCellColor, destTimeCell + destTimeCellColor], - [""], - [altIdentCell + "[color]green", altEFOBCell + altEFOBCellColor, altTimeCell + altTimeCellColor], - ["RTE RSV/%", "ZFW/ZFWCG"], - [rteRsvWeightCell + rteRsvPctColor + "/" + rteRsvPercentCell + "{end}" + rteRSvCellColor, zfwCell + "/" + zfwCgCell + "{sp}" + zfwColor], - ["ALTN\xa0/TIME", "FOB{sp}{sp}{sp}{sp}{sp}{sp}"], - [altFuelCell + altTimeColor + "/" + altFuelTimeCell + "{end}" + altFuelColor, fobCell + "/" + fobOtherCell + "{sp}{sp}{sp}" + fobCellColor], - ["FINAL/TIME", "GW/{sp}{sp} CG"], - [finalFuelCell + "/" + finalTimeCell + finalColor, gwCell + "/ " + cgCell + gwCgCellColor], - ["MIN DEST FOB", "EXTRA/TIME"], - [minDestFobCell + minDestFobCellColor, extraFuelCell + extraTimeColor + "/" + extraTimeCell + "{end}" + extraCellColor] - ]); - - mcdu.setArrows(false, false, true, true); - - mcdu.onPrevPage = () => { - CDUInitPage.ShowPage1(mcdu); - }; - mcdu.onNextPage = () => { - CDUInitPage.ShowPage1(mcdu); - }; - - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.FuelPredPage) { - CDUFuelPredPage.ShowPage(mcdu); - } - }, mcdu.PageTimeout.Dyn); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_GPSMonitor.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_GPSMonitor.js deleted file mode 100644 index 457aab403e3..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_GPSMonitor.js +++ /dev/null @@ -1,61 +0,0 @@ -class CDUGPSMonitor { - static ShowPage(mcdu, merit1, merit2, sat1, sat2) { - let currPos = new LatLong(SimVar.GetSimVarValue("GPS POSITION LAT", "degree latitude"), - SimVar.GetSimVarValue("GPS POSITION LON", "degree longitude")).toShortDegreeString(); - if (currPos.includes("N")) { - var currPosSplit = currPos.split("N"); - var sep = "N/"; - } else { - var currPosSplit = currPos.split("S"); - var sep = "S/"; - } - const latStr = currPosSplit[0]; - const lonStr = currPosSplit[1]; - currPos = latStr + sep + lonStr; - const TTRK = SimVar.GetSimVarValue("GPS GROUND MAGNETIC TRACK", "radians"); - const GROUNDSPEED = SimVar.GetSimVarValue("GPS GROUND SPEED", "Knots"); - // Should be corrected from WGS84 to EGM96... when MMR GPS receiver is implemented - const ALTITUDE = Math.min(131072, Math.max(-131072, SimVar.GetSimVarValue("GPS POSITION ALT", "Feet"))); - - const UTC_SECONDS = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - const hours = Math.floor(UTC_SECONDS / 3600) || 0; - const minutes = Math.floor(UTC_SECONDS % 3600 / 60) || 0; - const seconds = Math.floor(UTC_SECONDS % 3600 % 60) || 0; - - const UTC = `${hours.toString().padStart(2, "0") || "00"}:${minutes.toString().padStart(2, "0") || "00"}:${seconds.toString().padStart(2, "0") || "00"}`; - - if (typeof merit1 == 'undefined') { - merit1 = Math.floor(Math.random() * 10) + 40; - merit2 = Math.floor(Math.random() * 10) + 40; - sat1 = Math.floor(Math.random() * 5) + 8; - sat2 = Math.floor(Math.random() * 5) + 8; - } - - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.GPSMonitor; - mcdu.setTemplate([ - ["GPS MONITOR"], - ["GPS1 POSITION"], - [`${currPos}[color]green`], - ["TTRK", "GS", "UTC"], - [`${Math.round(TTRK)}[color]green`, `${Math.round(GROUNDSPEED)}[color]green`, `${UTC}[color]green`], - ["MERIT", "MODE/SAT", "GPS ALT"], - [`${merit1}FT[color]green`, `NAV/${sat1}[color]green`, `${Math.round(ALTITUDE)}[color]green`], - ["GPS2 POSITION"], - [`${currPos}[color]green`], - ["TTRK", "GS", "UTC"], - [`${Math.round(TTRK)}[color]green`, `${Math.round(GROUNDSPEED)}[color]green`, `${UTC}[color]green`], - ["MERIT", "MODE/SAT", "GPS ALT"], - [`${merit2}FT[color]green`, `NAV/${sat2}[color]green`, `${Math.round(ALTITUDE)}[color]green`] - ]); - - // ideally, this would update with the same frequency (is it known?) as the A320 GPS - // updates fast as it shows seconds - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.GPSMonitor) { - CDUGPSMonitor.ShowPage(mcdu, merit1, merit2, sat1, sat2); - } - }, mcdu.PageTimeout.Fast); - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_HoldAtPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_HoldAtPage.js deleted file mode 100644 index b77c34e3a0f..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_HoldAtPage.js +++ /dev/null @@ -1,347 +0,0 @@ -// Copyright (c) 2021-2023, 2025 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -const TurnDirection = Object.freeze({ - Unknown: 'U', - Left: 'L', - Right: 'R', - Either: 'E', -}); - -const HoldType = Object.freeze({ - Computed: 0, - Database: 1, - Modified: 2, -}); - -class CDUHoldAtPage { - static ShowPage(mcdu, waypointIndexFP, forPlan, inAlternate) { - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - const waypoint = targetPlan.legElementAt(waypointIndexFP); - - if (!waypoint) { - return CDUFlightPlanPage.ShowPage(mcdu); - } - - const editingHm = waypoint.type === 'HM'; // HM - - if (editingHm) { - CDUHoldAtPage.DrawPage(mcdu, waypointIndexFP, waypointIndexFP, forPlan, inAlternate); - } else { - const editingHx = waypoint.isHX(); - const alt = waypoint.definition.altitude1 ? waypoint.definition.altitude1 : SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); - - let defaultHold; - let modifiedHold; - if (editingHx) { - defaultHold = waypoint.defaultHold; - modifiedHold = waypoint.modifiedHold; - } else { - const previousLeg = targetPlan.maybeElementAt(waypointIndexFP - 1); - - let inboundMagneticCourse = 100; - if (previousLeg && previousLeg.isDiscontinuity === false && previousLeg.isXF()) { - inboundMagneticCourse = Avionics.Utils.computeGreatCircleHeading( - previousLeg.terminationWaypoint().location, - waypoint.terminationWaypoint().location, - ); - } - - defaultHold = { - inboundMagneticCourse, - turnDirection: TurnDirection.Right, - time: alt <= 14000 ? 1 : 1.5, - type: HoldType.Computed, - }; - modifiedHold = {}; - - const fix = waypoint.terminationWaypoint(); - const promises = []; - // Due to the way MSFS stores holds, we have to try all these possibilities. - if (targetPlan.originAirport) { - promises.push(Fmgc.NavigationDatabaseService.activeDatabase.getHolds(fix.ident, targetPlan.originAirport.ident)); - } - if (targetPlan.destinationAirport) { - promises.push(Fmgc.NavigationDatabaseService.activeDatabase.getHolds(fix.ident, targetPlan.destinationAirport.ident)); - } - if (fix && fix.area === 1 /* WaypointArea.Terminal */ && fix.airportIdent && fix.airportIdent.length > 0) { - promises.push(Fmgc.NavigationDatabaseService.activeDatabase.getHolds(fix.ident, fix.airportIdent)); - } - Promise.all(promises).then((resolvedPromises) => { - // Pick a hold based on altitude suitability and inbound course - // Missing in the navdata is the duplicate indicator that would help us pick the right area/airspace type. - const holds = resolvedPromises - .reduce((allHolds, holds) => { - allHolds.push(...holds); return allHolds; - }) - .filter((v) => v.waypoint.databaseId === fix.databaseId) - .sort((a, b) => { - let ret = CDUHoldAtPage.holdVerticalDistanceFromAlt(alt, a) - CDUHoldAtPage.holdVerticalDistanceFromAlt(alt, b); - if (ret === 0) { - ret = Math.abs(a.magneticCourse - inboundMagneticCourse) - Math.abs(b.magneticCourse - inboundMagneticCourse); - } - return ret; - }); - - if (holds[0]) { - defaultHold = { - inboundMagneticCourse: holds[0].magneticCourse, - turnDirection: holds[0].turnDirection, - time: holds[0].lengthTime ? holds[0].lengthTime : undefined, - distance: holds[0].length ? holds[0].length : undefined, - type: HoldType.Database, - }; - } - - CDUHoldAtPage.addOrEditManualHold( - mcdu, - waypointIndexFP, - // eslint-disable-next-line prefer-object-spread - Object.assign({}, defaultHold), - modifiedHold, - defaultHold, - forPlan, - inAlternate, - ); - }).catch(() => { - CDUHoldAtPage.addOrEditManualHold( - mcdu, - waypointIndexFP, - // eslint-disable-next-line prefer-object-spread - Object.assign({}, defaultHold), - modifiedHold, - defaultHold, - forPlan, - inAlternate, - ); - }); - return; - } - - CDUHoldAtPage.addOrEditManualHold( - mcdu, - waypointIndexFP, - // eslint-disable-next-line prefer-object-spread - Object.assign({}, defaultHold), - modifiedHold, - defaultHold, - forPlan, - inAlternate, - ); - } - } - - static holdVerticalDistanceFromAlt(altitude, hold) { - switch (hold.altitudeDescriptor) { - case "B": - if (altitude <= hold.altitude1 && altitude >= hold.altitude2) { - return 0; - } - if (altitude > hold.altitude1) { - return altitude - hold.altitude1; - } - return hold.altitude2 - altitude; - case "+": - return Math.max(0, hold.altitude1 - altitude); - case "-": - return Math.max(0, altitude - hold.altitude1); - default: - // no restriction, so always suitable - return 0; - } - } - - static addOrEditManualHold( - mcdu, - atIndex, - desiredHold, - modifiedHold, - defaultHold, - planIndex, - alternate, - ) { - mcdu.flightPlanService.addOrEditManualHold( - atIndex, - desiredHold, - modifiedHold, - defaultHold, - planIndex, - alternate, - ).then((holdIndex) => { - CDUHoldAtPage.DrawPage(mcdu, holdIndex, atIndex, planIndex, alternate); - }); - } - - static DrawPage(mcdu, waypointIndexFP, originalFpIndex, forPlan, inAlternate) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.HoldAtPage; - - const tmpy = forPlan === Fmgc.FlightPlanIndex.Active && mcdu.flightPlanService.hasTemporary; - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - - const leg = targetPlan.legElementAt(waypointIndexFP); - - const speed = waypointIndexFP === mcdu.holdIndex && mcdu.holdSpeedTarget > 0 ? mcdu.holdSpeedTarget : 180; - - const modifiedHold = leg.modifiedHold; - const defaultHold = leg.defaultHold; - const currentHold = CDUHoldAtPage.computeDesiredHold(leg); - - // TODO this doesn't account for wind... we really need to access the actual hold leg once the ts/js barrier is broken - const displayTime = currentHold.time === undefined ? currentHold.distance * 60 / speed : currentHold.time; - const displayDistance = currentHold.distance === undefined ? speed * currentHold.time / 60 : currentHold.distance; - - const defaultType = defaultHold.type === HoldType.Database ? 'DATABASE' : 'COMPUTED'; - const defaultTitle = `${defaultType}\xa0`; - const defaultRevert = `${defaultType}}`; - - const ident = leg.ident.replace('T-P', 'PPOS').padEnd(7, '\xa0'); - const rows = []; - rows.push([`${currentHold.type !== HoldType.Modified ? defaultTitle : ''}HOLD\xa0{small}AT{end}\xa0{green}${ident}{end}`]); - rows.push(['INB CRS', '', '']); - rows.push([`{${tmpy ? 'yellow' : 'cyan'}}${modifiedHold && modifiedHold.inboundMagneticCourse !== undefined ? '{big}' : '{small}'}${currentHold.inboundMagneticCourse.toFixed(0).padStart(3, '0')}°{end}{end}`]); - rows.push(['TURN', currentHold.type === HoldType.Modified ? 'REVERT TO' : '']); - rows.push([`{${tmpy ? 'yellow' : 'cyan'}}${modifiedHold && modifiedHold.turnDirection !== undefined ? '{big}' : '{small}'}${currentHold.turnDirection === TurnDirection.Left ? 'L' : 'R'}{end}`, `{cyan}${currentHold.type === HoldType.Modified ? defaultRevert : ''}{end}`]); - rows.push(['TIME/DIST']); - rows.push([`{${tmpy ? 'yellow' : 'cyan'}}${modifiedHold && modifiedHold.time !== undefined ? '{big}' : '{small}'}${displayTime.toFixed(1).padStart(4, '\xa0')}{end}/${modifiedHold && modifiedHold.distance !== undefined ? '{big}' : '{small}'}${displayDistance.toFixed(1)}{end}{end}`]); - rows.push(['', '', '\xa0LAST EXIT']); - rows.push(['', '', '{small}UTC\xa0\xa0\xa0FUEL{end}']); - rows.push(['', '', '----\xa0\xa0----']); - rows.push(['']); - rows.push(['']); - rows.push([tmpy ? '{amber}{ERASE{end}' : '', tmpy ? '{amber}INSERT*{end}' : '', '']); - - mcdu.setTemplate([ - ...rows, - ]); - - // TODO what happens if CLR attemped? - // change course - mcdu.onLeftInput[0] = (value, scratchpadCallback) => { - if (value.match(/^[0-9]{1,3}$/) === null) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - const magCourse = parseInt(value); - if (magCourse > 360) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - return; - } - - CDUHoldAtPage.modifyHold(mcdu, waypointIndexFP, leg, 'inboundMagneticCourse', magCourse, forPlan, inAlternate).then(() => { - CDUHoldAtPage.DrawPage(mcdu, waypointIndexFP, originalFpIndex, forPlan, inAlternate); - }); - }; - - // change turn direction - mcdu.onLeftInput[1] = async (value, scratchpadCallback) => { - if (value !== 'L' && value !== 'R') { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - await CDUHoldAtPage.modifyHold( - mcdu, - waypointIndexFP, - leg, - 'turnDirection', - value === 'L' ? TurnDirection.Left : TurnDirection.Right, - forPlan, - inAlternate, - ); - - CDUHoldAtPage.DrawPage(mcdu, waypointIndexFP, originalFpIndex, forPlan, inAlternate); - }; - - // change time or distance - mcdu.onLeftInput[2] = (value, scratchpadCallback) => { - const m = value.match(/^(([0-9]{0,1}(\.[0-9])?)\/?|\/([0-9]{0,2}(\.[0-9])?))$/); - if (m === null || m[0].length === 0 || m[0] === '/') { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - const time = m[2]; - const dist = m[4]; - - const param = dist ? 'distance' : 'time'; - const newValue = dist ? parseFloat(dist) : parseFloat(time); - - CDUHoldAtPage.modifyHold(mcdu, waypointIndexFP, leg, param, newValue, forPlan, inAlternate).then(() => { - CDUHoldAtPage.DrawPage(mcdu, waypointIndexFP, originalFpIndex, forPlan, inAlternate); - }); - }; - - // revert to computed/database - if (currentHold.type === HoldType.Modified) { - mcdu.onRightInput[1] = async () => { - mcdu.flightPlanService.revertHoldToComputed(waypointIndexFP); - - CDUHoldAtPage.DrawPage(mcdu, waypointIndexFP, originalFpIndex, forPlan, inAlternate); - }; - } - - // erase - mcdu.onLeftInput[5] = () => { - if (tmpy) { - mcdu.eraseTemporaryFlightPlan(() => { - CDULateralRevisionPage.ShowPage(mcdu, targetPlan.maybeElementAt(originalFpIndex), originalFpIndex, forPlan, inAlternate); - }); - } - }; - - // insert - mcdu.onRightInput[5] = () => { - if (tmpy) { - mcdu.insertTemporaryFlightPlan(() => { - CDUFlightPlanPage.ShowPage(mcdu, waypointIndexFP, forPlan); - }); - } - }; - } - - static computeDesiredHold(/** @type {FlightPlanLeg} */ leg) { - const modifiedHold = leg.modifiedHold; - const defaultHold = leg.defaultHold; - - const pilotTimeOrDistance = modifiedHold && (modifiedHold.time !== undefined || modifiedHold.distance !== undefined); - - return { - inboundMagneticCourse: modifiedHold && modifiedHold.inboundMagneticCourse !== undefined ? modifiedHold.inboundMagneticCourse : defaultHold.inboundMagneticCourse, - turnDirection: modifiedHold && modifiedHold.turnDirection !== undefined ? modifiedHold.turnDirection : defaultHold.turnDirection, - distance: pilotTimeOrDistance ? modifiedHold.distance : defaultHold.distance, - time: pilotTimeOrDistance ? modifiedHold.time : defaultHold.time, - type: modifiedHold !== undefined ? modifiedHold.type : defaultHold.type, - }; - } - - static async modifyHold(mcdu, waypointIndexFP, /** @type {FlightPlanLeg} */ waypointData, param, value, forPlan, inAlternate) { - if (waypointData.modifiedHold === undefined) { - waypointData.modifiedHold = {}; - } - - waypointData.modifiedHold.type = HoldType.Modified; - - if (param === 'time') { - waypointData.modifiedHold.distance = undefined; - } else if (param === 'distance') { - waypointData.modifiedHold.time = undefined; - } - - waypointData.modifiedHold[param] = value; - - await mcdu.flightPlanService.addOrEditManualHold( - waypointIndexFP, - CDUHoldAtPage.computeDesiredHold(waypointData), - waypointData.modifiedHold, - waypointData.defaultHold, - forPlan, - inAlternate, - ); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSInit.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSInit.js deleted file mode 100644 index 67340049060..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSInit.js +++ /dev/null @@ -1,220 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUIRSInit { - static ShowPage(mcdu, lon, originAirportLat, originAirportLon, referenceName, originAirportCoordinates, alignMsg = "ALIGN ON REF}[color]cyan") { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.IRSInit; - mcdu.setTitle('IRS INIT'); - - const adiru1State = SimVar.GetSimVarValue("L:A32NX_ADIRS_ADIRU_1_STATE", "Enum"); - const adiru2State = SimVar.GetSimVarValue("L:A32NX_ADIRS_ADIRU_2_STATE", "Enum"); - const adiru3State = SimVar.GetSimVarValue("L:A32NX_ADIRS_ADIRU_3_STATE", "Enum"); - const areAllAligned = adiru1State === 2 && adiru2State === 2 && adiru3State === 2; - - if (adiru1State === 0 || adiru2State === 0 || adiru3State === 0) { - SimVar.SetSimVarValue("L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF", "Enum", 0); - alignMsg = "ALIGN ON REF}[color]cyan"; - } - const emptyIRSGpsString = "--°--.--/---°--.--"; - const arrowupdwn = "↑↓"; - let larrowupdwn = arrowupdwn; - let rarrowupdwn = ""; - if (lon) { - rarrowupdwn = arrowupdwn; - larrowupdwn = ""; - } - let statusIRS1; - let statusIRS2; - let statusIRS3; - let alignType = "---"; - // Ref coordinates are taken based on origin airport - const activeOriginAirport = mcdu.flightPlanService.active.originAirport; - - if (!originAirportLat && !originAirportLon) { - const airportCoordinates = activeOriginAirport.location; - originAirportLat = CDUInitPage.ConvertDDToDMS(airportCoordinates['lat'], false); - originAirportLon = CDUInitPage.ConvertDDToDMS(airportCoordinates['long'], true); - originAirportLat['sec'] = Math.ceil(Number(originAirportLat['sec'] / 100)); - originAirportLon['sec'] = Math.ceil(Number(originAirportLon['sec'] / 100)); - // Must be string for consistency since leading 0's are not allowed in Number - originAirportLat['min'] = originAirportLat['min'].toString(); - originAirportLon['min'] = originAirportLon['min'].toString(); - referenceName = activeOriginAirport.ident + " [color]green"; - originAirportCoordinates = JSON.stringify(originAirportLat) + JSON.stringify(originAirportLon); - } - if (originAirportCoordinates === JSON.stringify(originAirportLat) + JSON.stringify(originAirportLon)) { - referenceName = activeOriginAirport.ident + " [color]green"; - } - const currentGPSLat = CDUInitPage.ConvertDDToDMS(SimVar.GetSimVarValue("GPS POSITION LAT", "degree latitude"), false); - const currentGPSLon = CDUInitPage.ConvertDDToDMS(SimVar.GetSimVarValue("GPS POSITION LON", "degree longitude"), true); - let GPSPosAlign; - let originAirportTitle; - let GPSPosTitle; - let originAirportString; - - if (!areAllAligned) { - GPSPosTitle = ["LAT", "LONG", "GPS POSITION"]; - originAirportTitle = ["LAT" + larrowupdwn , rarrowupdwn + "LONG", "REFERENCE"]; - GPSPosAlign = ["--°--.--", "--°--.--", " "]; - originAirportString = [originAirportLat['deg'] + "°{small}" + originAirportLat['min'] + "." + originAirportLat['sec'] + "{end}" + originAirportLat['dir'] + "[color]cyan", originAirportLon['deg'] + "°{small}" + originAirportLon['min'] + "." + originAirportLon['sec'] + "{end}" + originAirportLon['dir'] + "[color]cyan", referenceName]; - if (SimVar.GetSimVarValue("L:A32NX_OVHD_ADIRS_IR_1_MODE_SELECTOR_KNOB", "Enum") || SimVar.GetSimVarValue("L:A32NX_OVHD_ADIRS_IR_2_MODE_SELECTOR_KNOB", "Enum") || SimVar.GetSimVarValue("L:A32NX_OVHD_ADIRS_IR_3_MODE_SELECTOR_KNOB", "Enum")) { - GPSPosAlign = [currentGPSLat['deg'] + "°{small}" + currentGPSLat['min'] + "." + Math.ceil(Number(currentGPSLat['sec'] / 100)) + "{end}" + currentGPSLat['dir'] + '[color]green', currentGPSLon['deg'] + "°{small}" + currentGPSLon['min'] + "." + Math.ceil(Number(currentGPSLon['sec'] / 100)) + "{end}" + currentGPSLon['dir'] + "[color]green", ""]; - alignType = "GPS"; - } - } - - let IRSAlignOnPos = currentGPSLat['deg'] + "°{small}" + currentGPSLat['min'] + "." + Math.ceil(Number(currentGPSLat['sec'] / 100)) + "{end}" + currentGPSLat['dir'] + "/" + currentGPSLon['deg'] + "°{small}" + currentGPSLon['min'] + "." + Math.ceil(Number(currentGPSLon['sec'] / 100)) + "{end}" + currentGPSLon['dir']; - if (SimVar.GetSimVarValue("L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF", "Enum") === 1) { - alignMsg = ""; - alignType = "REF"; - if (!areAllAligned) { - IRSAlignOnPos = originAirportLat['deg'] + "°{small}" + originAirportLat['min'] + "." + originAirportLat['sec'] + "{end}" + originAirportLat['dir'] + "/" + originAirportLon['deg'] + "°{small}" + originAirportLon['min'] + "." + originAirportLon['sec'] + "{end}" + originAirportLon['dir']; - } - } - - if (areAllAligned) { - alignMsg = ""; - if (SimVar.GetSimVarValue("L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF", "Enum") === 0) { - alignType = "GPS"; - } - } - - let IRS1GpsString = emptyIRSGpsString; - if (SimVar.GetSimVarValue("L:A32NX_OVHD_ADIRS_IR_1_MODE_SELECTOR_KNOB", "Enum") === 1) { - if (adiru1State === 2) { - statusIRS1 = "IRS1 ALIGNED ON " + alignType; - } else { - statusIRS1 = "IRS1 ALIGNING ON " + alignType; - } - IRS1GpsString = IRSAlignOnPos + "[color]green"; - } else { - statusIRS1 = "IRS1 OFF"; - } - let IRS2GpsString = emptyIRSGpsString; - if (SimVar.GetSimVarValue("L:A32NX_OVHD_ADIRS_IR_2_MODE_SELECTOR_KNOB", "Enum") === 1) { - if (adiru2State === 2) { - statusIRS2 = "IRS2 ALIGNED ON " + alignType; - } else { - statusIRS2 = "IRS2 ALIGNING ON " + alignType; - } - IRS2GpsString = IRSAlignOnPos + "[color]green"; - } else { - statusIRS2 = "IRS2 OFF"; - } - let IRS3GpsString = emptyIRSGpsString; - if (SimVar.GetSimVarValue("L:A32NX_OVHD_ADIRS_IR_3_MODE_SELECTOR_KNOB", "Enum") === 1) { - if (adiru3State === 2) { - statusIRS3 = "IRS3 ALIGNED ON " + alignType; - } else { - statusIRS3 = "IRS3 ALIGNING ON " + alignType; - } - IRS3GpsString = IRSAlignOnPos + "[color]green"; - } else { - statusIRS3 = "IRS3 OFF"; - } - - mcdu.setTemplate([ - ["IRS INIT"], - originAirportTitle, - originAirportString, - GPSPosTitle, - GPSPosAlign, - ["", "", statusIRS1], - ["", "", IRS1GpsString], - ["", "", statusIRS2], - ["", "", IRS2GpsString], - ["", "", statusIRS3], - ["", "", IRS3GpsString], - [], - [" { - lon = false; - }; - - mcdu.onRightInput[0] = () => { - lon = true; - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[5] = () => { - CDUInitPage.ShowPage1(mcdu); - }; - - mcdu.onRightInput[5] = () => { - if (!areAllAligned) { - if (alignMsg.includes("CONFIRM")) { - SimVar.SetSimVarValue("L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF", "Enum", 1); - } else { - alignMsg = "CONFIRM ALIGN* [color]amber"; - } - } - }; - - mcdu.onUp = () => { - if (!areAllAligned && SimVar.GetSimVarValue("L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF", "Enum") !== 1) { - referenceName = "----"; - let activeReference = originAirportLat; - if (lon) { - activeReference = originAirportLon; - } - if (activeReference['deg'] >= 90 && !lon || activeReference['deg'] >= 180 && lon) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - } else { - activeReference['sec'] = activeReference['sec'] + 1; - if (activeReference['sec'] >= 9) { - activeReference['min'] = (Number(activeReference['min']) + 1).toString().padStart(2, "0"); - activeReference['sec'] = 0; - } - if (activeReference['min'] >= 60) { - activeReference['min'] = (0).toString().padStart(2, "0"); - activeReference['deg'] = (!lon) ? activeReference['deg'] + 1 : (Number(activeReference['deg']) + 1).toString().padStart(3, "0"); - } - } - } - }; - - mcdu.onDown = () => { - if (!areAllAligned && SimVar.GetSimVarValue("L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF", "Enum") !== 1) { - referenceName = "----"; - let activeReference = originAirportLat; - if (lon) { - activeReference = originAirportLon; - } - if (activeReference['deg'] <= -90 && !lon || activeReference['deg'] <= -180 && lon) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - } else { - activeReference['sec'] = activeReference['sec'] - 1; - if (activeReference['sec'] < 0) { - activeReference['min'] = (Number(activeReference['min']) - 1).toString().padStart(2, "0"); - activeReference['sec'] = 9; - } - if (activeReference['min'] < 0) { - activeReference['min'] = 59; - activeReference['deg'] = (!lon) ? activeReference['deg'] - 1 : (Number(activeReference['deg']) - 1).toString().padStart(3, "0"); - } - } - } - }; - - // This page auto-refreshes based on source material. Function will stop auto-refreshing when page has been changed. - autoRefresh(); - function autoRefresh() { - setTimeout(() => { - if (mcdu.page.Current === mcdu.page.IRSInit) { - CDUIRSInit.ShowPage(mcdu, lon = lon, - originAirportLat = originAirportLat, originAirportLon = originAirportLon, - referenceName = referenceName, originAirportCoordinates = originAirportCoordinates, - alignMsg = alignMsg); - } - }, 1000); - } - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSMonitor.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSMonitor.js deleted file mode 100644 index 38013f4d2a5..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSMonitor.js +++ /dev/null @@ -1,46 +0,0 @@ -class CDUIRSMonitor { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.IRSMonitor; - mcdu.setTemplate([ - ["IRS MONITOR"], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - CDUIRSStatus.ShowPage(mcdu, 1); - }; - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = () => { - CDUIRSStatus.ShowPage(mcdu, 2); - }; - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - CDUIRSStatus.ShowPage(mcdu, 3); - }; - } - - static getAdiruStateMessage(number) { - const state = SimVar.GetSimVarValue(`L:A32NX_ADIRS_ADIRU_${number}_STATE`, "Enum"); - switch (state) { - case 1: - return "ALIGN"; - case 2: - return "NAV"; - default: - return ""; - } - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSStatus.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSStatus.js deleted file mode 100644 index e0c5e46fa44..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSStatus.js +++ /dev/null @@ -1,77 +0,0 @@ -class CDUIRSStatus { - static ShowPage(mcdu, index, prev_wind_dir) { - let currPos = new LatLong(SimVar.GetSimVarValue("GPS POSITION LAT", "degree latitude"), - SimVar.GetSimVarValue("GPS POSITION LON", "degree longitude")).toShortDegreeString(); - if (currPos.includes("N")) { - var currPosSplit = currPos.split("N"); - var sep = "N/"; - } else { - var currPosSplit = currPos.split("S"); - var sep = "S/"; - } - const latStr = currPosSplit[0]; - const lonStr = currPosSplit[1]; - currPos = latStr + sep + lonStr; - const GROUNDSPEED = SimVar.GetSimVarValue("GPS GROUND SPEED", "Knots") || "0"; - const THDG = SimVar.GetSimVarValue("GPS GROUND TRUE HEADING", "radians") || "000"; - const TTRK = SimVar.GetSimVarValue("GPS GROUND MAGNETIC TRACK", "radians") || "000"; - const MHDG = SimVar.GetSimVarValue("GPS GROUND TRUE TRACK", "radians") || "000"; - const WIND_VELOCITY = SimVar.GetSimVarValue("AMBIENT WIND VELOCITY", "Knots") || "00"; - // wind direction smoothing like A32NX_NDInfo.js:setWind() - let wind_dir = Math.round(Simplane.getWindDirection()); - if (typeof (prev_wind_dir) == "undefined") { - prev_wind_dir = wind_dir; - } - let startAngle = prev_wind_dir; - let endAngle = wind_dir; - const delta = endAngle - startAngle; - if (delta > 180) { - startAngle += 360; - } else if (delta < -180) { - endAngle += 360; - } - const smoothedAngle = Utils.SmoothSin(startAngle, endAngle, 0.25, mcdu._deltaTime / 1000); - wind_dir = smoothedAngle % 360; - - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.IRSStatus; - mcdu.setTemplate([ - [`IRS${index}`], - ["POSITION"], - [`${currPos}[color]green`], - ["TTRK", "GS"], - [`${Math.round(TTRK)}[color]green`, `${Math.round(GROUNDSPEED)}[color]green`], - [`THDG`, "MHDG"], - [`${Math.round(THDG)}[color]green`, `${Math.round(MHDG)}[color]green`], - ["WIND", "GPIRS ACCUR"], - [`${Math.round(wind_dir)}°/${Math.round(WIND_VELOCITY)}[color]green`, `200FT[color]green`], - ["GPIRS POSITION"], - [`${currPos}[color]green`], - ["", ""], - ["{FREEZE[color]cyan", `${index < 3 ? "NEXT IRS>" : "RETURN>"}`] - ]); - - mcdu.onLeftInput[5] = () => { - CDUIRSStatusFrozen.ShowPage(mcdu, index, wind_dir); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onRightInput[5] = () => { - if (index > 2) { - CDUIRSMonitor.ShowPage(mcdu); - } else { - this.ShowPage(mcdu, index + 1); - } - }; - - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.IRSStatus) { - CDUIRSStatus.ShowPage(mcdu, index, wind_dir); - } - }, mcdu.PageTimeout.Default); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSStatusFrozen.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSStatusFrozen.js deleted file mode 100644 index 7e3ade021b0..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IRSStatusFrozen.js +++ /dev/null @@ -1,55 +0,0 @@ -class CDUIRSStatusFrozen { - static ShowPage(mcdu, index, wind_dir) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.IRSStatusFrozen; - let currPos = new LatLong(SimVar.GetSimVarValue("GPS POSITION LAT", "degree latitude"), - SimVar.GetSimVarValue("GPS POSITION LON", "degree longitude")).toShortDegreeString(); - if (currPos.includes("N")) { - var currPosSplit = currPos.split("N"); - var sep = "N/"; - } else { - var currPosSplit = currPos.split("S"); - var sep = "S/"; - } - const latStr = currPosSplit[0]; - const lonStr = currPosSplit[1]; - currPos = latStr + sep + lonStr; - const GROUNDSPEED = SimVar.GetSimVarValue("GPS GROUND SPEED", "Meters per second") || "0"; - const THDG = SimVar.GetSimVarValue("GPS GROUND TRUE HEADING", "radians") || "000"; - const TTRK = SimVar.GetSimVarValue("GPS GROUND MAGNETIC TRACK", "radians") || "000"; - const MHDG = SimVar.GetSimVarValue("GPS GROUND TRUE TRACK", "radians") || "000"; - const WIND_VELOCITY = SimVar.GetSimVarValue("AMBIENT WIND VELOCITY", "Knots") || "00"; - const UTC_SECONDS = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - const hours = Math.floor(UTC_SECONDS / 3600) || 0; - const minutes = Math.floor(UTC_SECONDS % 3600 / 60) || 0; - const hhmm = `${hours.toString().padStart(2, "0") || "00"}${minutes.toString().padStart(2, "0") || "00"}`; - - mcdu.setTemplate([ - [`IRS${index} FROZEN AT ${hhmm}`], - ["POSITION"], - [`${currPos}[color]green`], - ["TTRK", "GS"], - [`${Math.round(TTRK)}[color]green`, `${Math.round(GROUNDSPEED)}[color]green`], - [`THDG`, "MHDG"], - [`${Math.round(THDG)}[color]green`, `${Math.round(MHDG)}[color]green`], - ["WIND", "GPIRS ACCUR"], - [`${Math.round(wind_dir)}°/${Math.round(WIND_VELOCITY)}[color]green`, `200FT[color]green`], - ["GPIRS POSITION"], - [`${currPos}[color]green`], - ["", ""], - ["{UNFREEZE[color]cyan", `${index < 3 ? "NEXT IRS>" : "RETURN>"}`] - ]); - - mcdu.onLeftInput[5] = () => { - CDUIRSStatus.ShowPage(mcdu, index); - }; - - mcdu.onRightInput[5] = () => { - if (index > 2) { - CDUIRSMonitor.ShowPage(mcdu); - } else { - CDUIRSStatus.ShowPage(mcdu, index + 1); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IdentPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IdentPage.js deleted file mode 100644 index 1f92462e3cb..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_IdentPage.js +++ /dev/null @@ -1,160 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -const DB_MONTHS = Object.freeze({ - '01': "JAN", - '02': "FEB", - '03': "MAR", - '04': "APR", - '05': "MAY", - '06': "JUN", - '07': "JUL", - '08': "AUG", - '09': "SEP", - '10': "OCT", - '11': "NOV", - '12': "DEC" -}); - -function calculateActiveDate(dbIdent) { - const effDay = dbIdent.effectiveFrom.substring(8); - const effMonth = dbIdent.effectiveFrom.substring(5, 7); - const expDay = dbIdent.effectiveTo.substring(8); - const expMonth = dbIdent.effectiveTo.substring(5, 7); - - return `${effDay}${DB_MONTHS[effMonth]}-${expDay}${DB_MONTHS[expMonth]}`; -} - -function calculateSecondDate(dbIdent) { - const [effYear, effMonth, effDay] = dbIdent.effectiveFrom.split('-'); - const [expYear, expMonth, expDay] = dbIdent.effectiveTo.split('-'); - - return `${effDay}${DB_MONTHS[effMonth]}${effYear}-${expDay}${DB_MONTHS[expMonth]}${expYear}`; -} - -async function switchDataBase(mcdu) { - await mcdu.switchNavDatabase(); -} - -const ConfirmType = { - NoConfirm : 0, - DeleteStored : 1, - SwitchDataBase : 2, -}; - -class CDUIdentPage { - static ShowPage(mcdu, confirmType = ConfirmType.NoConfirm) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.IdentPage; - mcdu.activeSystem = "FMGC"; - - const stored = mcdu.dataManager.numberOfStoredElements(); - - let storedTitleCell = ""; - let storedRoutesRunwaysCell = ""; - let storedWaypointsNavaidsCell = ""; - let storedDeleteCell = ""; - let secondaryDBSubLine = ""; - let secondaryDBTopLine = ""; - if ( - stored.routes + stored.runways + stored.waypoints + stored.navaids > - 0 - ) { - storedTitleCell = "STORED\xa0\xa0\xa0\xa0"; - storedRoutesRunwaysCell = `{green}${stored.routes - .toFixed(0) - .padStart( - 2, - "0" - )}{end}{small}RTES{end}\xa0{green}${stored.runways - .toFixed(0) - .padStart(2, "0")}{end}{small}RWYS{end}`; - storedWaypointsNavaidsCell = `{green}{big}${stored.waypoints - .toFixed(0) - .padStart( - 2, - "0" - )}{end}{end}{small}WPTS{end}\xa0{green}{big}${stored.navaids - .toFixed(0) - .padStart(2, "0")}{end}{end}{small}NAVS{end}`; - storedDeleteCell = - confirmType === ConfirmType.DeleteStored - ? "{amber}CONFIRM DEL*{end}" - : "{cyan}DELETE ALL}{end}"; - - // DELETE ALL - mcdu.onRightInput[4] = () => { - if (confirmType == ConfirmType.DeleteStored) { - mcdu.dataManager.deleteAllStoredWaypoints().then((allDeleted) => { - if (!allDeleted) { - mcdu.setScratchpadMessage(NXSystemMessages.fplnElementRetained); - } - - CDUIdentPage.ShowPage(mcdu); - }); - } else { - CDUIdentPage.ShowPage(mcdu, ConfirmType.DeleteStored); - } - }; - } - - const dbCycle = mcdu.getNavDatabaseIdent(); - const activeCycleDates = dbCycle === null ? '' : calculateActiveDate(dbCycle); - const secondCycleDates = dbCycle === null ? '' : calculateSecondDate(dbCycle); - const navSerial = dbCycle === null ? '' : `${dbCycle.provider.substring(0, 2).toUpperCase()}${dbCycle.airacCycle}0001`; - - // H4+ only confirm prompt + year on second dates - secondaryDBTopLine = - confirmType === ConfirmType.SwitchDataBase - ? `{amber}{small}\xa0${secondCycleDates}{end}` - : "\xa0SECOND\xa0NAV\xa0DATA\xa0BASE"; - secondaryDBSubLine = - confirmType === ConfirmType.SwitchDataBase - ? "{amber}{CANCEL\xa0\xa0\xa0SWAP\xa0\xa0CONFIRM*{end}" - : `{small}{cyan}{${secondCycleDates}{end}{end}`; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[2] = () => { - if (confirmType === ConfirmType.SwitchDataBase) { - CDUIdentPage.ShowPage(mcdu); - } else { - CDUIdentPage.ShowPage(mcdu, ConfirmType.SwitchDataBase); - } - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onRightInput[2] = () => { - if (confirmType === ConfirmType.SwitchDataBase) { - switchDataBase(mcdu).then(() => { - CDUIdentPage.ShowPage(mcdu); - }); - } - }; - - mcdu.setTemplate([ - ["A320-200\xa0\xa0\xa0\xa0"], //This aircraft code is correct and does not include the engine type. - ["\xa0ENG"], - ["LEAP-1A26[color]green"], - ["\xa0ACTIVE NAV DATA BASE"], - [ - `{cyan}\xa0${activeCycleDates}{end}`, - `{green}${navSerial}{end}`, - ], - [secondaryDBTopLine], - [secondaryDBSubLine], - ["", storedTitleCell], - ["", storedRoutesRunwaysCell], - ["CHG CODE", storedWaypointsNavaidsCell], - ["[\xa0][color]inop", storedDeleteCell], - ["IDLE/PERF", "SOFTWARE\xa0\xa0"], - ["{small}{green}+0.0/+0.0{end}{end}", "STATUS/XLOAD>[color]inop"], - ]); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_InitPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_InitPage.js deleted file mode 100644 index 730a451edd0..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_InitPage.js +++ /dev/null @@ -1,827 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUInitPage { - static ShowPage1(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.InitPageA; - mcdu.pageRedrawCallback = () => CDUInitPage.ShowPage1(mcdu); - mcdu.activeSystem = 'FMGC'; - mcdu.coRoute.routes = []; - - const haveFlightPlan = mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport; - - const fromTo = new Column(23, "____|____", Column.amber, Column.right); - const [coRouteAction, coRouteText, coRouteColor] = new CDU_SingleValueField( - mcdu, - "string", - mcdu.coRoute.routeNumber, - { - emptyValue: haveFlightPlan ? "" : "__________[color]amber", - suffix: "[color]cyan", - maxLength: 10, - }, - async (value) => { - await mcdu.updateCoRoute(value, (result) => { - if (result) { - CDUInitPage.ShowPage1(mcdu); - } else { - scratchpadCallback(); - } - }); - } - ).getFieldAsColumnParameters(); - - const [flightNoAction, flightNoText, flightNoColor] = new CDU_SingleValueField(mcdu, - "string", - mcdu.flightNumber, - { - emptyValue: "________[color]amber", - suffix: "[color]cyan", - maxLength: 7 - }, - (value) => { - mcdu.updateFlightNo(value, (result) => { - if (result) { - CDUInitPage.ShowPage1(mcdu); - } else { - mcdu.setScratchpadUserData(value); - } - }); - } - ).getFieldAsColumnParameters(); - - const altnAirport = mcdu.flightPlanService.active.alternateDestinationAirport; - const altDest = new Column(0, `${altnAirport ? altnAirport.ident : '----'}|----------`); - let costIndexText = "---"; - let costIndexAction; - let costIndexColor = Column.white; - - const cruiseFl = new Column(0, "-----"); - const cruiseTemp = new Column (10, "---°", Column.right); - const cruiseFlTempSeparator = new Column(6, "/"); - - let alignOption; - const tropo = new Column(23, "36090", Column.small, Column.cyan, Column.right); - let requestButton = "REQUEST*"; - let requestButtonLabel = "INIT"; - let requestEnable = true; - - if (mcdu.simbrief.sendStatus === "REQUESTING") { - requestEnable = false; - requestButton = "REQUEST "; - } - - const origin = mcdu.flightPlanService.active.originAirport; - const dest = mcdu.flightPlanService.active.destinationAirport; - - if (origin) { - if (dest) { - fromTo.update(origin.ident + "/" + dest.ident, Column.cyan); - - // If an active SimBrief OFP matches the FP, hide the request option - // This allows loading a new OFP via INIT/REVIEW loading a different orig/dest to the current one - if (mcdu.simbrief.sendStatus != "DONE" || - (mcdu.simbrief["originIcao"] === origin.ident && mcdu.simbrief["destinationIcao"] === dest.ident)) { - requestEnable = false; - requestButtonLabel = ""; - requestButton = ""; - } - - // Cost index - [costIndexAction, costIndexText, costIndexColor] = new CDU_SingleValueField(mcdu, - "int", - mcdu.isCostIndexSet ? mcdu.costIndex : null, - { - clearable: true, - emptyValue: "___[color]amber", - minValue: 0, - maxValue: 999, - suffix: "[color]cyan" - }, - (value) => { - if (value != null) { - mcdu.costIndex = value; - // mcdu.isCostIndexSet = true; - } else { - // mcdu.isCostIndexSet = false; - mcdu.costIndex = undefined; - } - CDUInitPage.ShowPage1(mcdu); - } - ).getFieldAsColumnParameters(); - - mcdu.onLeftInput[4] = costIndexAction; - - cruiseFl.update("_____", Column.amber); - cruiseTemp.update("|___°", Column.amber); - cruiseFlTempSeparator.updateAttributes(Column.amber); - - //This is done so pilot enters a FL first, rather than using the computed one - if (mcdu.cruiseLevel) { - cruiseFl.update("FL" + mcdu.cruiseLevel.toFixed(0).padStart(3, "0"), Column.cyan); - if (mcdu.cruiseTemperature) { - cruiseTemp.update(mcdu.cruiseTemperature.toFixed(0) + "°", Column.cyan); - cruiseFlTempSeparator.updateAttributes(Column.cyan); - } else { - cruiseTemp.update(mcdu.tempCurve.evaluate(mcdu.cruiseLevel).toFixed(0) + "°", Column.cyan, Column.small); - cruiseFlTempSeparator.updateAttributes(Column.cyan, Column.small); - } - } - - // CRZ FL / FLX TEMP - mcdu.onLeftInput[5] = (value, scratchpadCallback) => { - if (mcdu.setCruiseFlightLevelAndTemperature(value)) { - CDUInitPage.ShowPage1(mcdu); - } else { - scratchpadCallback(); - } - }; - - if (mcdu.flightPlanService.active.originAirport) { - alignOption = "IRS INIT>"; - } - - altDest.update(altnAirport ? altnAirport.ident : "NONE", Column.cyan); - - mcdu.onLeftInput[1] = async (value, scratchpadCallback) => { - try { - if (value === "") { - await mcdu.getCoRouteList(mcdu); - CDUAvailableFlightPlanPage.ShowPage(mcdu); - } else { - if (await mcdu.tryUpdateAltDestination(value)) { - CDUInitPage.ShowPage1(mcdu); - } else { - scratchpadCallback(); - } - } - } catch (error) { - console.error(error); - mcdu.logTroubleshootingError(error); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - } - }; - } - } - - mcdu.onLeftInput[0] = coRouteAction; - - if (mcdu.tropo) { - tropo.update(mcdu.tropo.toString(), mcdu.isTropoPilotEntered ? Column.big : Column.small); - } - mcdu.onRightInput[4] = (value, scratchpadCallback) => { - if (mcdu.tryUpdateTropo(value)) { - CDUInitPage.ShowPage1(mcdu); - } else { - scratchpadCallback(); - } - }; - - /** - * If scratchpad is filled, attempt to update city pair - * else show route selection pair if city pair is displayed - * Ref: FCOM 4.03.20 P6 - */ - mcdu.onRightInput[0] = (value, scratchpadCallback) => { - if (value !== "") { - mcdu.tryUpdateFromTo(value, (result) => { - if (result) { - CDUAvailableFlightPlanPage.ShowPage(mcdu); - } else { - scratchpadCallback(); - } - }); - } else if (mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport) { - mcdu.getCoRouteList(mcdu).then(() => { - CDUAvailableFlightPlanPage.ShowPage(mcdu); - }); - } - }; - mcdu.onRightInput[1] = () => { - if (requestEnable) { - getSimBriefOfp(mcdu, () => { - if (mcdu.page.Current === mcdu.page.InitPageA) { - CDUInitPage.ShowPage1(mcdu); - } - }) - .then((data) => { - Fmgc.SimBriefUplinkAdapter.uplinkFlightPlanFromSimbrief(mcdu, mcdu.flightPlanService, data, { doUplinkProcedures: false }).then(() => { - console.log('SimBrief data uplinked.'); - - mcdu.flightPlanService.uplinkInsert(); - - const plan = mcdu.flightPlanService.active; - mcdu.updateFlightNo(plan.flightNumber); - mcdu.setGroundTempFromOrigin(); - - if (mcdu.page.Current === mcdu.page.InitPageA) { - CDUInitPage.ShowPage1(mcdu); - } - }).catch((error) => { - console.error(error); - mcdu.logTroubleshootingError(error); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - }); - }); - } - }; - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = () => { - if (alignOption) { - CDUIRSInit.ShowPage(mcdu); - } - }; - - const groundTemp = new Column(23, "---°", Column.right); - if (mcdu.groundTemp !== undefined) { - groundTemp.update(mcdu.groundTemp.toFixed(0) + "°", Column.cyan, (mcdu.groundTempPilot !== undefined ? Column.big : Column.small)); - } - - mcdu.onRightInput[5] = (scratchpadValue, scratchpadCallback) => { - try { - mcdu.trySetGroundTemp(scratchpadValue); - CDUInitPage.ShowPage1(mcdu); - } catch (msg) { - if (msg instanceof McduMessage) { - mcdu.setScratchpadMessage(msg); - scratchpadCallback(); - } else { - throw msg; - } - } - }; - - mcdu.onLeftInput[2] = flightNoAction; - - mcdu.setArrows(false, false, true, true); - - mcdu.setTemplate(FormatTemplate([ - [ - new Column(10, "INIT") - ], - [ - new Column(1, "CO RTE"), - new Column(21, "FROM/TO", Column.right) - ], - [ - new Column(0, coRouteText, coRouteColor), - fromTo - ], - [ - new Column(0, "ALTN/CO RTE"), - new Column(22, requestButtonLabel, Column.amber, Column.right) - ], - [ - altDest, - new Column(23, requestButton, Column.amber, Column.right) - ], - [ - new Column(0, "FLT NBR") - ], - [ - new Column(0, flightNoText, flightNoColor), - new Column(23, alignOption || "", Column.right) - ], - [""], - [ - new Column(23, "WIND/TEMP>", Column.right) - ], - [ - new Column(0, "COST INDEX"), - new Column(23, "TROPO", Column.right) - ], - [ - new Column(0, costIndexText, costIndexColor), - tropo - ], - [ - new Column(0, "CRZ FL/TEMP"), - new Column(23, "GND TEMP", Column.right) - ], - [ - cruiseFl, - cruiseFlTempSeparator, - cruiseTemp, - groundTemp - ] - ])); - - mcdu.onPrevPage = () => { - mcdu.goToFuelPredPage(); - }; - mcdu.onNextPage = () => { - mcdu.goToFuelPredPage(); - }; - - mcdu.onRightInput[3] = () => { - CDUWindPage.Return = () => { - CDUInitPage.ShowPage1(mcdu); - }; - CDUWindPage.ShowPage(mcdu); - }; - - mcdu.onUp = () => {}; - try { - Coherent.trigger("AP_ALT_VAL_SET", 4200); - Coherent.trigger("AP_VS_VAL_SET", 300); - Coherent.trigger("AP_HDG_VAL_SET", 180); - } catch (e) { - console.error(e); - } - } - // Does not refresh page so that other things can be performed first as necessary - static updateTowIfNeeded(mcdu) { - if (isFinite(mcdu.taxiFuelWeight) && isFinite(mcdu.zeroFuelWeight) && isFinite(mcdu.blockFuel)) { - mcdu.takeOffWeight = mcdu.zeroFuelWeight + mcdu.blockFuel - mcdu.taxiFuelWeight; - } - } - static fuelPredConditionsMet(mcdu) { - const fob = mcdu.getFOB(); - - return Number.isFinite(fob) && - Number.isFinite(mcdu.zeroFuelWeightMassCenter) && - Number.isFinite(mcdu.zeroFuelWeight) && - mcdu.flightPlanService.active && mcdu.flightPlanService.active.legCount > 0 && - mcdu._zeroFuelWeightZFWCGEntered; - } - static trySetFuelPred(mcdu) { - if (CDUInitPage.fuelPredConditionsMet(mcdu) && !mcdu._fuelPredDone) { - setTimeout(() => { - if (CDUInitPage.fuelPredConditionsMet(mcdu) && !mcdu._fuelPredDone) { //Double check as user can clear block fuel during timeout - mcdu._fuelPredDone = true; - if (mcdu.page.Current === mcdu.page.InitPageB) { - CDUInitPage.ShowPage2(mcdu); - } - } - }, mcdu.getDelayFuelPred()); - } - } - static ShowPage2(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.InitPageB; - mcdu.activeSystem = 'FMGC'; - mcdu.pageRedrawCallback = () => CDUInitPage.ShowPage2(mcdu); - - const alternate = mcdu.flightPlanService.active ? mcdu.flightPlanService.active.alternateDestinationAirport : undefined; - - const zfwCell = new Column(17, "___._", Column.amber, Column.right); - const zfwCgCell = new Column(22, "__._", Column.amber, Column.right); - const zfwCgCellDivider = new Column(18, "|", Column.amber, Column.right); - - if (mcdu._zeroFuelWeightZFWCGEntered) { - if (isFinite(mcdu.zeroFuelWeight)) { - zfwCell.update(NXUnits.kgToUser(mcdu.zeroFuelWeight).toFixed(1), Column.cyan); - } - if (isFinite(mcdu.zeroFuelWeightMassCenter)) { - zfwCgCell.update(mcdu.zeroFuelWeightMassCenter.toFixed(1), Column.cyan); - } - if (isFinite(mcdu.zeroFuelWeight) && isFinite(mcdu.zeroFuelWeightMassCenter)) { - zfwCgCellDivider.updateAttributes(Column.cyan); - } - } - mcdu.onRightInput[0] = async (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } else if (value === "") { - let zfw = undefined; - let zfwCg = undefined; - const a32nxBoarding = SimVar.GetSimVarValue("L:A32NX_BOARDING_STARTED_BY_USR", "bool"); - const gsxBoarding = SimVar.GetSimVarValue("L:FSDT_GSX_BOARDING_STATE", "number"); - if (a32nxBoarding || (gsxBoarding >= 4 && gsxBoarding < 6)) { - zfw = NXUnits.kgToUser(SimVar.GetSimVarValue("L:A32NX_AIRFRAME_ZFW_DESIRED", "number")); - zfwCg = SimVar.GetSimVarValue("L:A32NX_AIRFRAME_ZFW_CG_PERCENT_MAC_DESIRED", "number"); - } else if (isFinite(getZfw()) && isFinite(getZfwcg())) { - zfw = getZfw(); - zfwCg = getZfwcg(); - } - - // ZFW/ZFWCG auto-fill helper - if (zfw && zfwCg) { - mcdu.setScratchpadText(`${(zfw / 1000).toFixed(1)}/${zfwCg.toFixed(1)}`); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } - } else { - if (mcdu.trySetZeroFuelWeightZFWCG(value)) { - CDUInitPage.updateTowIfNeeded(mcdu); - CDUInitPage.ShowPage2(mcdu); - CDUInitPage.trySetFuelPred(mcdu); - } else { - scratchpadCallback(); - } - } - }; - - const blockFuel = new Column(23, "__._", Column.amber, Column.right); - if (mcdu._blockFuelEntered || mcdu._fuelPlanningPhase === mcdu._fuelPlanningPhases.IN_PROGRESS) { - if (isFinite(mcdu.blockFuel)) { - blockFuel.update(NXUnits.kgToUser(mcdu.blockFuel).toFixed(1), Column.cyan); - } - } - mcdu.onRightInput[1] = async (value, scratchpadCallback) => { - if (mcdu._zeroFuelWeightZFWCGEntered && value !== mcdu.clrValue) { //Simulate delay if calculating trip data - if (await mcdu.trySetBlockFuel(value)) { - CDUInitPage.updateTowIfNeeded(mcdu); - CDUInitPage.ShowPage2(mcdu); - CDUInitPage.trySetFuelPred(mcdu); - } else { - scratchpadCallback(); - } - } else { - if (await mcdu.trySetBlockFuel(value)) { - CDUInitPage.updateTowIfNeeded(mcdu); - CDUInitPage.ShowPage2(mcdu); - } else { - scratchpadCallback(); - } - } - - }; - - const fuelPlanTopTitle = new Column(23, "", Column.amber, Column.right); - const fuelPlanBottomTitle = new Column(23, "", Column.amber, Column.right); - if (mcdu._zeroFuelWeightZFWCGEntered && !mcdu._blockFuelEntered) { - fuelPlanTopTitle.text = "FUEL "; - fuelPlanBottomTitle.text = "PLANNING }"; - mcdu.onRightInput[2] = async () => { - if (await mcdu.tryFuelPlanning()) { - CDUInitPage.updateTowIfNeeded(mcdu); - CDUInitPage.ShowPage2(mcdu); - } - }; - } - if (mcdu._fuelPlanningPhase === mcdu._fuelPlanningPhases.IN_PROGRESS) { - fuelPlanTopTitle.update("BLOCK ", Column.green); - fuelPlanBottomTitle.update("CONFIRM", Column.green); - mcdu.onRightInput[2] = async () => { - if (await mcdu.tryFuelPlanning()) { - CDUInitPage.updateTowIfNeeded(mcdu); - CDUInitPage.ShowPage2(mcdu); - CDUInitPage.trySetFuelPred(mcdu); - } - }; - } - - const towCell = new Column(17, "---.-", Column.right); - const lwCell = new Column(23, "---.-", Column.right); - const towLwCellDivider = new Column(18, "/"); - const taxiFuelCell = new Column(0, "0.4", Column.cyan, Column.small); - - if (isFinite(mcdu.taxiFuelWeight)) { - if (mcdu._taxiEntered) { - taxiFuelCell.update(NXUnits.kgToUser(mcdu.taxiFuelWeight).toFixed(1), Column.big); - } else { - taxiFuelCell.text = NXUnits.kgToUser(mcdu.taxiFuelWeight).toFixed(1); - } - } - mcdu.onLeftInput[0] = async (value, scratchpadCallback) => { - if (mcdu._fuelPredDone) { - setTimeout(async () => { - if (mcdu.trySetTaxiFuelWeight(value)) { - CDUInitPage.updateTowIfNeeded(mcdu); - if (mcdu.page.Current === mcdu.page.InitPageB) { - CDUInitPage.ShowPage2(mcdu); - } - } else { - scratchpadCallback(); - } - }, mcdu.getDelayHigh()); - } else { - if (mcdu.trySetTaxiFuelWeight(value)) { - CDUInitPage.updateTowIfNeeded(mcdu); - CDUInitPage.ShowPage2(mcdu); - } else { - scratchpadCallback(); - } - } - }; - - const tripWeightCell = new Column(4, "---.-", Column.right); - const tripTimeCell = new Column(9, "----", Column.right); - const tripCellDivider = new Column(5, "/"); - const rteRsvWeightCell = new Column(4, "---.-", Column.right); - const rteRsvPercentCell = new Column(6, "5.0", Column.cyan); - const rteRsvCellDivider = new Column(5, "/", Column.cyan); - - if (isFinite(mcdu.getRouteReservedPercent())) { - rteRsvPercentCell.text = mcdu.getRouteReservedPercent().toFixed(1); - } - mcdu.onLeftInput[2] = async (value, scratchpadCallback) => { - if (await mcdu.trySetRouteReservedPercent(value)) { - CDUInitPage.ShowPage2(mcdu); - } else { - scratchpadCallback(); - } - }; - - const altnWeightCell = new Column(4, "---.-", Column.right); - const altnTimeCell = new Column(9, "----", Column.right); - const altnCellDivider = new Column(5, "/"); - const finalWeightCell = new Column(4, "---.-", Column.right); - const finalTimeCell = new Column(9, "----", Column.right); - const finalCellDivider = new Column(5, "/"); - - if (mcdu.getRouteFinalFuelTime() > 0) { - finalTimeCell.update(FMCMainDisplay.minutesTohhmm(mcdu.getRouteFinalFuelTime()), Column.cyan); - finalCellDivider.updateAttributes(Column.cyan); - } - mcdu.onLeftInput[4] = async (value, scratchpadCallback) => { - if (await mcdu.trySetRouteFinalTime(value)) { - CDUInitPage.ShowPage2(mcdu); - } else { - scratchpadCallback(); - } - }; - - const extraWeightCell = new Column(18, "---.-", Column.right); - const extraTimeCell = new Column(23, "----", Column.right); - const extraCellDivider = new Column(19, "/"); - const minDestFob = new Column(4, "---.-", Column.right); - const tripWindDirCell = new Column(19, "--"); - const tripWindAvgCell = new Column(21, "---"); - - if (mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport) { - tripWindDirCell.update(CDUInitPage.formatWindDirection(mcdu.averageWind), Column.cyan, Column.small); - tripWindAvgCell.update(CDUInitPage.formatWindComponent(mcdu.averageWind), Column.cyan); - - mcdu.onRightInput[4] = (value, scratchpadCallback) => { - if (mcdu.trySetAverageWind(value)) { - CDUInitPage.ShowPage2(mcdu); - } else { - scratchpadCallback(); - } - }; - } - - if (CDUInitPage.fuelPredConditionsMet(mcdu)) { - fuelPlanTopTitle.text = ""; - fuelPlanBottomTitle.text = ""; - - mcdu.tryUpdateTOW(); - if (isFinite(mcdu.takeOffWeight)) { - towCell.update(NXUnits.kgToUser(mcdu.takeOffWeight).toFixed(1), Column.green, Column.small); - } - - if (mcdu._fuelPredDone) { - if (!mcdu.routeFinalEntered()) { - mcdu.tryUpdateRouteFinalFuel(); - } - if (isFinite(mcdu.getRouteFinalFuelWeight()) && isFinite(mcdu.getRouteFinalFuelTime())) { - if (mcdu._rteFinalWeightEntered) { - finalWeightCell.update(NXUnits.kgToUser(mcdu.getRouteFinalFuelWeight()).toFixed(1), Column.cyan); - } else { - finalWeightCell.update(NXUnits.kgToUser(mcdu.getRouteFinalFuelWeight()).toFixed(1), Column.cyan, Column.small); - } - if (mcdu._rteFinalTimeEntered || !mcdu.routeFinalEntered()) { - finalTimeCell.update(FMCMainDisplay.minutesTohhmm(mcdu.getRouteFinalFuelTime()), Column.cyan); - } else { - finalTimeCell.update(FMCMainDisplay.minutesTohhmm(mcdu.getRouteFinalFuelTime()), Column.cyan, Column.small); - finalCellDivider.updateAttributes(Column.small); - } - finalCellDivider.updateAttributes(Column.cyan); - } - mcdu.onLeftInput[4] = async (value, scratchpadCallback) => { - setTimeout(async () => { - if (await mcdu.trySetRouteFinalFuel(value)) { - if (mcdu.page.Current === mcdu.page.InitPageB) { - CDUInitPage.ShowPage2(mcdu); - } - } else { - scratchpadCallback(); - } - }, mcdu.getDelayHigh()); - }; - - if (alternate) { - const altFuelEntered = mcdu._routeAltFuelEntered; - if (!altFuelEntered) { - mcdu.tryUpdateRouteAlternate(); - } - if (isFinite(mcdu.getRouteAltFuelWeight())) { - altnWeightCell.update(NXUnits.kgToUser(mcdu.getRouteAltFuelWeight()).toFixed(1), Column.cyan, altFuelEntered ? Column.big : Column.small); - const time = mcdu.getRouteAltFuelTime(); - if (time) { - altnTimeCell.update(FMCMainDisplay.minutesTohhmm(mcdu.getRouteAltFuelTime()), Column.green, Column.small); - altnCellDivider.updateAttributes(Column.green, Column.small); - } else { - altnTimeCell.update('----',Column.white); - altnCellDivider.updateAttributes(Column.white, altFuelEntered ? Column.big : Column.small); - } - } - } else { - altnWeightCell.update("0.0", Column.green, Column.small); - } - - mcdu.onLeftInput[3] = async (value, scratchpadCallback) => { - setTimeout(async () => { - if (await mcdu.trySetRouteAlternateFuel(value)) { - if (mcdu.page.Current === mcdu.page.InitPageB) { - CDUInitPage.ShowPage2(mcdu); - } - } else { - scratchpadCallback(); - } - }, mcdu.getDelayHigh()); - }; - - mcdu.tryUpdateRouteTrip(); - if (isFinite(mcdu.getTotalTripFuelCons()) && isFinite(mcdu.getTotalTripTime())) { - tripWeightCell.update(NXUnits.kgToUser(mcdu.getTotalTripFuelCons()).toFixed(1), Column.green, Column.small); - tripTimeCell.update(FMCMainDisplay.minutesTohhmm(mcdu._routeTripTime), Column.green, Column.small); - tripCellDivider.updateAttributes(Column.green, Column.small); - } - - if (isFinite(mcdu.getRouteReservedWeight())) { - if (mcdu._rteReservedWeightEntered) { - rteRsvWeightCell.update(NXUnits.kgToUser(mcdu.getRouteReservedWeight()).toFixed(1), Column.cyan); - } else { - rteRsvWeightCell.update(NXUnits.kgToUser(mcdu.getRouteReservedWeight()).toFixed(1), Column.cyan, Column.small); - } - } - - if (mcdu._rteRsvPercentOOR) { - rteRsvPercentCell.update("--.-", Column.white); - rteRsvCellDivider.updateAttributes(Column.white); - } else if (isFinite(mcdu.getRouteReservedPercent())) { - if (mcdu._rteReservedPctEntered || !mcdu.routeReservedEntered()) { - rteRsvPercentCell.update(mcdu.getRouteReservedPercent().toFixed(1), Column.cyan); - } else { - rteRsvPercentCell.update(mcdu.getRouteReservedPercent().toFixed(1), Column.cyan, Column.small); - rteRsvCellDivider.updateAttributes(Column.small); - } - } - - mcdu.onLeftInput[2] = async (value, scratchpadCallback) => { - setTimeout(async () => { - if (await mcdu.trySetRouteReservedFuel(value)) { - if (mcdu.page.Current === mcdu.page.InitPageB) { - CDUInitPage.ShowPage2(mcdu); - } - } else { - scratchpadCallback(); - } - }, mcdu.getDelayMedium()); - }; - - mcdu.tryUpdateLW(); - lwCell.update(NXUnits.kgToUser(mcdu.landingWeight).toFixed(1), Column.green, Column.small); - towLwCellDivider.updateAttributes(Column.green, Column.small); - - const windComponent = Number.isFinite(mcdu.averageWind) ? mcdu.averageWind : 0; - - tripWindDirCell.update(CDUInitPage.formatWindDirection(windComponent), Column.small); - tripWindAvgCell.update(CDUInitPage.formatWindComponent(windComponent), Column.big); - - mcdu.onRightInput[4] = async (value, scratchpadCallback) => { - setTimeout(() => { - if (mcdu.trySetAverageWind(value)) { - if (mcdu.page.Current === mcdu.page.InitPageB) { - CDUInitPage.ShowPage2(mcdu); - } - } else { - scratchpadCallback(); - } - }, mcdu.getDelayWindLoad()); - }; - - if (mcdu._minDestFobEntered) { - minDestFob.update(NXUnits.kgToUser(mcdu._minDestFob).toFixed(1), Column.cyan); - } else { - mcdu.tryUpdateMinDestFob(); - minDestFob.update(NXUnits.kgToUser(mcdu._minDestFob).toFixed(1), Column.cyan, Column.small); - } - mcdu.onLeftInput[5] = async (value, scratchpadCallback) => { - setTimeout(async () => { - if (await mcdu.trySetMinDestFob(value)) { - if (mcdu.page.Current === mcdu.page.InitPageB) { - CDUInitPage.ShowPage2(mcdu); - } - } else { - scratchpadCallback(); - } - }, mcdu.getDelayHigh()); - }; - mcdu.checkEFOBBelowMin(); - - extraWeightCell.update(NXUnits.kgToUser(mcdu.tryGetExtraFuel()).toFixed(1), Column.green, Column.small); - if (mcdu.tryGetExtraFuel() >= 0) { - extraTimeCell.update(FMCMainDisplay.minutesTohhmm(mcdu.tryGetExtraTime()), Column.green, Column.small); - extraCellDivider.updateAttributes(Column.green, Column.small); - } - } - } - - mcdu.setArrows(false, false, true, true); - - mcdu.setTemplate(FormatTemplate([ - [ - new Column(5, "INIT FUEL PRED") - ], - [ - new Column(0, "TAXI"), - new Column(15, "ZFW/ZFWCG") - ], - [ - taxiFuelCell, - zfwCell, - zfwCgCellDivider, - zfwCgCell - ], - [ - new Column(0, "TRIP"), - new Column(5, "/TIME"), - new Column(19, "BLOCK") - ], - [ - tripWeightCell, - tripCellDivider, - tripTimeCell, - blockFuel - ], - [ - new Column(0, "RTE RSV/%"), - fuelPlanTopTitle - ], - [ - rteRsvWeightCell, - rteRsvCellDivider, - rteRsvPercentCell, - fuelPlanBottomTitle - ], - [ - new Column(0, "ALTN"), - new Column(5, "/TIME"), - new Column(15, "TOW/"), - new Column(22, "LW") - ], - [ - altnWeightCell, - altnCellDivider, - altnTimeCell, - towCell, - towLwCellDivider, - lwCell - ], - [ - new Column(0, "FINAL/TIME"), - new Column(15, "TRIP WIND") - ], - [ - finalWeightCell, - finalCellDivider, - finalTimeCell, - tripWindDirCell, - tripWindAvgCell - ], - [ - new Column(0, "MIN DEST FOB"), - new Column(14, "EXTRA/TIME") - ], - [ - minDestFob, - extraWeightCell, - extraCellDivider, - extraTimeCell - ] - ])); - - mcdu.onPrevPage = () => { - CDUInitPage.ShowPage1(mcdu); - }; - mcdu.onNextPage = () => { - CDUInitPage.ShowPage1(mcdu); - }; - } - - // Defining as static here to avoid duplicate code in CDUIRSInit - static ConvertDDToDMS(deg, lng) { - // converts decimal degrees to degrees minutes seconds - const M = 0 | (deg % 1) * 60e7; - let degree; - if (lng) { - degree = (0 | (deg < 0 ? -deg : deg)).toString().padStart(3, "0"); - } else { - degree = 0 | (deg < 0 ? -deg : deg); - } - return { - dir : deg < 0 ? lng ? 'W' : 'S' : lng ? 'E' : 'N', - deg : degree, - min : Math.abs(0 | M / 1e7), - sec : Math.abs((0 | M / 1e6 % 1 * 6e4) / 100) - }; - } - - static formatWindDirection(tailwindComponent) { - return Math.round(tailwindComponent) > 0 ? "TL" : "HD"; - } - - static formatWindComponent(tailwindComponent) { - return Math.round(Math.abs(tailwindComponent)).toFixed(0).padStart(3, "0"); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Keypad.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Keypad.js deleted file mode 100644 index 1368dd3782f..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Keypad.js +++ /dev/null @@ -1,73 +0,0 @@ -class Keypad { - constructor(mcdu) { - this._mcdu = mcdu; - this._keys = { - "AIRPORT": () => mcdu.onAirport(), - "ATC": () => CDUAtcMenu.ShowPage(mcdu), - "DATA": () => CDUDataIndexPage.ShowPage1(mcdu), - "DIR": () => { - mcdu.eraseTemporaryFlightPlan(); - CDUDirectToPage.ShowPage(mcdu); - }, - "FPLN": () => CDUFlightPlanPage.ShowPage(mcdu), - "FUEL": () => mcdu.goToFuelPredPage(), - "INIT": () => { - if (mcdu.flightPhaseManager.phase === FmgcFlightPhases.DONE) { - mcdu.flightPhaseManager.changePhase(FmgcFlightPhases.PREFLIGHT); - } - CDUInitPage.ShowPage1(mcdu); - }, - "MENU": () => CDUMenuPage.ShowPage(mcdu), - "PERF": () => { - if (mcdu.flightPhaseManager.phase === FmgcFlightPhases.DONE) { - mcdu.flightPhaseManager.changePhase(FmgcFlightPhases.PREFLIGHT); - } - CDUPerformancePage.ShowPage(mcdu); - }, - "PROG": () => CDUProgressPage.ShowPage(mcdu), - "RAD": () => CDUNavRadioPage.ShowPage(mcdu), - "SEC": () => CDUSecFplnMain.ShowPage(mcdu), - - "PREVPAGE": () => mcdu.onPrevPage(), - "NEXTPAGE": () => mcdu.onNextPage(), - "UP": () => mcdu.onUp(), - "DOWN": () => mcdu.onDown(), - - "CLR": () => mcdu.onClr(), - "CLR_Held": () => mcdu.onClrHeld(), - "DIV": () => mcdu.onDiv(), - "DOT": () => mcdu.onDot(), - "OVFY": () => mcdu.onOvfy(), - "PLUSMINUS": () => mcdu.onPlusMinus(), - "SP": () => mcdu.onSp(), - - "BRT": (side) => mcdu.onBrightnessKey(side, 1), - "DIM": (side) => mcdu.onBrightnessKey(side, -1), - }; - - for (const letter of FMCMainDisplay._AvailableKeys) { - this._keys[letter] = () => this._mcdu.onLetterInput(letter); - } - } - - /** - * Handle CDU key presses - * @param {unknown} value - * @param {'L' | 'R'} side - * @returns true if handled - */ - onKeyPress(value, side) { - const action = this._keys[value]; - if (!action) { - return false; - } - - const cur = this._mcdu.page.Current; - setTimeout(() => { - if (this._mcdu.page.Current === cur) { - action(side); - } - }, this._mcdu.getDelaySwitchPage()); - return true; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_LateralRevisionPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_LateralRevisionPage.js deleted file mode 100644 index fea05f597e4..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_LateralRevisionPage.js +++ /dev/null @@ -1,212 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDULateralRevisionPage { - /** - * - * @param mcdu - * @param leg {FlightPlanLeg} - * @param legIndexFP - * @constructor - */ - static ShowPage(mcdu, leg, legIndexFP, forPlan = Fmgc.FlightPlanIndex.Active, inAlternate = false) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.LateralRevisionPage; - - let coordinates = ""; - if (leg && leg.definition && leg.definition.waypoint && leg.definition.waypoint.location) { - const lat = CDUInitPage.ConvertDDToDMS(leg.definition.waypoint.location['lat'], false); - const long = CDUInitPage.ConvertDDToDMS(leg.definition.waypoint.location['long'], true); - coordinates = `${lat.deg}°${lat.min}.${Math.ceil(Number(lat.sec / 100))}${lat.dir}/${long.deg}°${long.min}.${Math.ceil(Number(long.sec / 100))}${long.dir}[color]green`; - } - /** @type {BaseFlightPlan} */ - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - - const isPpos = leg === undefined || legIndexFP === 0 && leg !== targetPlan.originLeg; - const isFrom = legIndexFP === targetPlan.fromLegIndex && forPlan === Fmgc.FlightPlanIndex.Active && !inAlternate; - const isDeparture = legIndexFP === targetPlan.originLegIndex && !isPpos; // TODO this is bogus... compare icaos - const isDestination = legIndexFP === targetPlan.destinationLegIndex && !isPpos; // TODO this is bogus... compare icaos - const isWaypoint = !isDeparture && !isDestination && !isPpos; - const isManual = leg && leg.isVectors(); - - let waypointIdent = isPpos ? "PPOS" : '---'; - - if (leg) { - if (isDestination && targetPlan.destinationRunway) { - waypointIdent = targetPlan.destinationRunway.ident; - } else { - waypointIdent = leg.ident; - } - } - - let departureCell = ""; - if (isDeparture) { - departureCell = " { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - CDUAvailableDeparturesPage.ShowPage(mcdu, targetPlan.originAirport, -1, false, forPlan, inAlternate); - }; - } - - let arrivalFixInfoCell = ""; - if (isDestination) { - arrivalFixInfoCell = "ARRIVAL>"; - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - CDUAvailableArrivalsPage.ShowPage(mcdu, targetPlan.destinationAirport, 0, false, forPlan, inAlternate); - }; - } else if (isDeparture || isPpos || isFrom) { - arrivalFixInfoCell = "FIX INFO>"; - mcdu.onRightInput[0] = () => { - CDUFixInfoPage.ShowPage(mcdu); - }; - } - - let crossingLabel = ""; - let crossingCell = ""; - if (!isDestination) { - crossingLabel = "LL XING/INCR/NO[color]inop"; - crossingCell = "[{sp}{sp}]°/[{sp}]°/[][color]inop"; - } - - let offsetCell = ""; - if (isDeparture || isWaypoint) { - offsetCell = " { - mcdu.insertWaypoint(value, forPlan, inAlternate, legIndexFP, false, (success) => { - if (!success) { - scratchpadCallback(); - } - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }); - }; - } - - let holdCell = ""; - if (isWaypoint) { - holdCell = " { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - const nextLeg = targetPlan.maybeElementAt(legIndexFP + 1); - - if (nextLeg && nextLeg.isDiscontinuity === false && nextLeg.isHX()) { - CDUHoldAtPage.ShowPage(mcdu, legIndexFP + 1, forPlan, inAlternate); - } else { - CDUHoldAtPage.ShowPage(mcdu, legIndexFP, forPlan, inAlternate); - } - }; - } - - let enableAltnLabel = ""; - let enableAltnCell = ""; - if (targetPlan['alternateDestinationAirport'] && !isDeparture && !inAlternate) { - enableAltnLabel = "{sp}ENABLE[color]cyan"; - enableAltnCell = "{ALTN[color]cyan"; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = async () => { - // TODO fm position - const ppos = { - lat: SimVar.GetSimVarValue('PLANE LATITUDE', 'Degrees'), - long: SimVar.GetSimVarValue('PLANE LONGITUDE', 'Degrees'), - }; - - const flightPlan = mcdu.flightPlan(forPlan, false); - const alternateAirport = flightPlan.alternateDestinationAirport; - if (alternateAirport) { - const distance = Avionics.Utils.computeGreatCircleDistance(ppos, alternateAirport.location); - const cruiseLevel = CDULateralRevisionPage.determineAlternateFlightLevel(distance); - - try { - await mcdu.flightPlanService.enableAltn(legIndexFP, cruiseLevel, forPlan); - } catch (e) { - console.error(e); - mcdu.logTroubleshootingError(e); - mcdu.setScratchpadMessage(NXFictionalMessages.internalError); - } - - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - } - }; - } - - let newDestLabel = ""; - let newDestCell = ""; - if (!isDestination && !isPpos && !isManual) { - newDestLabel = "NEW DEST{sp}"; - newDestCell = "[{sp}{sp}][color]cyan"; - - mcdu.onRightInput[3] = async (value) => { - await mcdu.flightPlanService.newDest(legIndexFP, value, forPlan, inAlternate); - - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }; - } - - let airwaysCell = ""; - if (isWaypoint) { - airwaysCell = "AIRWAYS>"; - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - A320_Neo_CDU_AirwaysFromWaypointPage.ShowPage(mcdu, legIndexFP, undefined, undefined, forPlan, inAlternate); - }; - } - - let altnCell = ''; - if (isDestination) { - altnCell = " { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); - }; - } - - static determineAlternateFlightLevel(distance) { - if (distance > 200) { - return 310; - } else if (distance > 100) { - return 220; - } else { - return 100; - } - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_MainDisplay.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_MainDisplay.js deleted file mode 100644 index 84fcdde82d3..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_MainDisplay.js +++ /dev/null @@ -1,1602 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class A320_Neo_CDU_MainDisplay extends FMCMainDisplay { - constructor() { - super(...arguments); - - this.MIN_BRIGHTNESS = 0.5; - this.MAX_BRIGHTNESS = 8; - - this.minPageUpdateThrottler = new UpdateThrottler(100); - this.mcduServerConnectUpdateThrottler = new UpdateThrottler(1000); - this.powerCheckUpdateThrottler = new UpdateThrottler(500); - - this._registered = false; - this._title = undefined; - this._titleLeft = ''; - this._pageCurrent = undefined; - this._pageCount = undefined; - this._labels = []; - this._lines = []; - this._keypad = new Keypad(this); - this.scratchpadDisplay = null; - this._scratchpad = null; - /** @type {Record<'MCDU' | 'FMGC' | 'ATSU' | 'AIDS' | 'CFDS', ScratchpadDataLink>} */ - this.scratchpads = {}; - this._arrows = [false, false, false, false]; - this.annunciators = { - left: { - // note these must match the base names in the model xml - fmgc: false, - fail: false, - mcdu_menu: false, - fm1: false, - ind: false, - rdy: false, - blank: false, - fm2: false, - }, - right: { - fmgc: false, - fail: false, - mcdu_menu: false, - fm1: false, - ind: false, - rdy: false, - blank: false, - fm2: false, - } - }; - /** MCDU request flags from subsystems */ - this.requests = { - AIDS: false, - ATSU: false, - CFDS: false, - FMGC: false, - }; - this._lastAtsuMessageCount = 0; - this.leftBrightness = 0; - this.rightBrightness = 0; - this.onLeftInput = []; - this.onRightInput = []; - this.leftInputDelay = []; - this.rightInputDelay = []; - /** @type {'FMGC' | 'ATSU' | 'AIDS' | 'CFDS'} */ - this._activeSystem = 'FMGC'; - this.inFocus = false; - this.lastInput = 0; - this.clrStop = false; - this.allSelected = false; - this.updateRequest = false; - this.initB = false; - this.lastPowerState = null; - this.PageTimeout = { - Fast: 500, - Medium: 1000, - Dyn: 1500, - Default: 2000, - Slow: 3000, - }; - this.fmgcMesssagesListener = RegisterViewListener('JS_LISTENER_SIMVARS', null, true); - this.setupFmgcTriggers(); - this.page = { - SelfPtr: false, - Current: 0, - Clear: 0, - AirportsMonitor: 1, - AirwaysFromWaypointPage: 2, - // AirwaysFromWaypointPageGetAllRows: 3, - AvailableArrivalsPage: 4, - AvailableArrivalsPageVias: 5, - AvailableDeparturesPage: 6, - AvailableFlightPlanPage: 7, - DataIndexPage1: 8, - DataIndexPage2: 9, - DirectToPage: 10, - FlightPlanPage: 11, - FuelPredPage: 12, - GPSMonitor: 13, - HoldAtPage: 14, - IdentPage: 15, - InitPageA: 16, - InitPageB: 17, - IRSInit: 18, - IRSMonitor: 19, - IRSStatus: 20, - IRSStatusFrozen: 21, - LateralRevisionPage: 22, - MenuPage: 23, - NavaidPage: 24, - NavRadioPage: 25, - NewWaypoint: 26, - PerformancePageTakeoff: 27, - PerformancePageClb: 28, - PerformancePageCrz: 29, - PerformancePageDes: 30, - PerformancePageAppr: 31, - PerformancePageGoAround: 32, - PilotsWaypoint: 33, - PosFrozen: 34, - PositionMonitorPage: 35, - ProgressPage: 36, - ProgressPageReport: 37, - ProgressPagePredictiveGPS: 38, - SelectedNavaids: 39, - SelectWptPage: 40, - VerticalRevisionPage: 41, - WaypointPage: 42, - AOCInit: 43, - AOCInit2: 44, - AOCOfpData: 45, - AOCOfpData2: 46, - AOCMenu: 47, - AOCRequestWeather: 48, - AOCRequestAtis: 49, - AOCDepartRequest: 50, - ATCMenu: 51, - ATCModify: 52, - ATCAtis: 53, - ATCMessageRecord: 54, - ATCMessageMonitoring: 55, - ATCConnection: 56, - ATCNotification: 57, - ATCConnectionStatus: 58, - ATCPositionReport1: 59, - ATCPositionReport2: 60, - ATCPositionReport3: 61, - ATCFlightRequest: 62, - ATCUsualRequest: 63, - ATCGroundRequest: 64, - ATCReports: 65, - ATCEmergency: 66, - ATCComLastId: 67, // This is needed for automatic page changes triggered by DCDU - ATSUMenu: 68, - ATSUDatalinkStatus: 69, - ClimbWind: 70, - CruiseWind: 71, - DescentWind: 72, - FixInfoPage: 73, - AOCRcvdMsgs: 74, - AOCSentMsgs: 75, - AOCFreeText: 76, - StepAltsPage: 77, - ATCDepartReq: 78, - }; - - this.mcduServerClient = undefined; - - this.emptyLines = { - lines: [ - ['', '', ''], - ['', '', ''], - ['', '', ''], - ['', '', ''], - ['', '', ''], - ['', '', ''], - ['', '', ''], - ['', '', ''], - ['', '', ''], - ['', '', ''], - ['', '', ''], - ['', '', ''], - ], - scratchpad: '', - title: '', - titleLeft: '', - page: '', - arrows: [false, false, false, false], - annunciators: { - fmgc: false, - fail: false, - mcdu_menu: false, - fm1: false, - ind: false, - rdy: false, - blank: false, - fm2: false, - }, - displayBrightness: 0, - integralBrightness: 0, - }; - - } - - // TODO this really belongs in the FMCMainDisplay, not the CDU - setupFmgcTriggers() { - Coherent.on('A32NX_FMGC_SEND_MESSAGE_TO_MCDU', (message) => { - this.addMessageToQueue(new TypeIIMessage(message.text, message.color === 'Amber'), () => false , () => { - if (message.clearable) { - Fmgc.recallMessageById(message.id); - } - }); - }); - - Coherent.on('A32NX_FMGC_RECALL_MESSAGE_FROM_MCDU_WITH_ID', (text) => { - this.removeMessageFromQueue(text); - }); - } - - get templateID() { - return "A320_Neo_CDU"; - } - - get isInteractive() { - return true; - } - - connectedCallback() { - super.connectedCallback(); - RegisterViewListener("JS_LISTENER_KEYEVENT", () => { - console.log("JS_LISTENER_KEYEVENT registered."); - RegisterViewListener("JS_LISTENER_FACILITY", () => { - console.log("JS_LISTENER_FACILITY registered."); - this._registered = true; - }); - }); - } - - // The callback is called when an event is received from the McduServerClient's socket. - // See https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#events for possible events. - // This will be used as a parameter when the McduServerClient's connect method is called. - // this.mcduServerClient.connect(this, this.mcduServerClientEventHandler); - mcduServerClientEventHandler(event) { - switch (event.type) { - case 'open': { - console.log(`[MCDU] Websocket connection to SimBridge opened. (${SimBridgeClient.McduServerClient.url()})`); - (new NXNotifManager).showNotification({title: "MCDU CONNECTED", - message: "A32NX MCDU successfully connected to SimBridge MCDU Server.", timeout: 5000}); - this.sendToMcduServerClient("mcduConnected"); - this.sendUpdate(); - break; - } - case 'close': { - console.log(`[MCDU] Websocket connection to SimBridge closed. (${SimBridgeClient.McduServerClient.url()})`); - break; - } - case 'error': { - console.log(`[MCDU] Websocket connection to SimBridge error. (${SimBridgeClient.McduServerClient.url()}): ${event.get()}`); - break; - } - case 'message': { - const [messageType, ...args] = event.data.split(':'); - if (messageType === 'event') { - // backwards compatible with the old MCDU server... - // accepts either event:button_name (old), or event:side:button_name (current) - const mcduIndex = (args.length > 1 && args[0] === 'right') ? 2 : 1; - const button = args.length > 1 ? args[1] : args[0]; - SimVar.SetSimVarValue(`H:A320_Neo_CDU_${mcduIndex}_BTN_${button}`, "number", 0); - SimVar.SetSimVarValue(`L:A32NX_MCDU_PUSH_ANIM_${mcduIndex}_${button}`, "Number", 1); - } - if (messageType === "requestUpdate") { - this.sendUpdate(); - } - break; - } - } - }; - - Init() { - super.Init(); - - this.generateHTMLLayout(this.getChildById("Mainframe") || this); - - this.scratchpadDisplay = new ScratchpadDisplay(this, this.getChildById("in-out")); - this.scratchpads["MCDU"] = new ScratchpadDataLink(this, this.scratchpadDisplay, 'MCDU', false); - this.scratchpads["FMGC"] = new ScratchpadDataLink(this, this.scratchpadDisplay, 'FMGC'); - this.scratchpads["ATSU"] = new ScratchpadDataLink(this, this.scratchpadDisplay, 'ATSU'); - this.scratchpads["AIDS"] = new ScratchpadDataLink(this, this.scratchpadDisplay, 'AIDS'); - this.scratchpads["CFDS"] = new ScratchpadDataLink(this, this.scratchpadDisplay, 'CFDS'); - this.activateMcduScratchpad(); - - try { - // note: without this, resetting mcdu kills camera - if (this.scratchpadDisplay && this.scratchpadDisplay.guid) { - Coherent.trigger('UNFOCUS_INPUT_FIELD', this.scratchpadDisplay.guid); - } - } catch (e) { - console.error(e); - } - - this.initKeyboardScratchpad(); - this._titleLeftElement = this.getChildById("title-left"); - this._titleElement = this.getChildById("title"); - this._pageCurrentElement = this.getChildById("page-current"); - this._pageCountElement = this.getChildById("page-count"); - this._labelElements = []; - this._lineElements = []; - for (let i = 0; i < 6; i++) { - this._labelElements[i] = [ - this.getChildById("label-" + i + "-left"), - this.getChildById("label-" + i + "-right"), - this.getChildById("label-" + i + "-center") - ]; - this._lineElements[i] = [ - this.getChildById("line-" + i + "-left"), - this.getChildById("line-" + i + "-right"), - this.getChildById("line-" + i + "-center") - ]; - } - - this.setTimeout = (func) => { - setTimeout(() => { - func; - }, this.getDelaySwitchPage()); - }; - - /** The following events remain due to shared use by the keypad and keyboard type entry */ - this.onLetterInput = (l) => this.scratchpad.addChar(l); - this.onSp = () => this.scratchpad.addChar(" "); - this.onDiv = () => this.scratchpad.addChar("/"); - this.onDot = () => this.scratchpad.addChar("."); - this.onClr = () => this.scratchpad.clear(); - this.onClrHeld = () => this.scratchpad.clearHeld(); - this.onPlusMinus = (defaultKey = "-") => this.scratchpad.plusMinus(defaultKey); - this.onLeftFunction = (f) => this.onLsk(this.onLeftInput[f], this.leftInputDelay[f]); - this.onRightFunction = (f) => this.onLsk(this.onRightInput[f], this.rightInputDelay[f]); - this.onOvfy = () => this.scratchpad.addChar('Δ'); - this.onUnload = () => {}; - - CDUMenuPage.ShowPage(this); - - // If the consent is not set, show telex page - const onlineFeaturesStatus = NXDataStore.get("CONFIG_ONLINE_FEATURES_STATUS", "UNKNOWN"); - - if (onlineFeaturesStatus === "UNKNOWN") { - new NXPopUp().showPopUp( - 'TELEX CONFIGURATION', - 'You have not yet configured the telex option. Telex enables free text and live map. If enabled, aircraft position data is published for the duration of the flight. Messages are public and not moderated. USE AT YOUR OWN RISK. To learn more about telex and the features it enables, please go to https://docs.flybywiresim.com/telex. Would you like to enable telex?', - 'small', - () => NXDataStore.set('CONFIG_ONLINE_FEATURES_STATUS', 'ENABLED'), - () => NXDataStore.set('CONFIG_ONLINE_FEATURES_STATUS', 'DISABLED'), - ); - } - - SimVar.SetSimVarValue("L:A32NX_GPS_PRIMARY_LOST_MSG", "Bool", 0).then(); - - NXDataStore.subscribe('*', () => { - this.requestUpdate(); - }); - - this.mcduServerClient = new SimBridgeClient.McduServerClient(); - - // sync annunciator simvar state - this.updateAnnunciators(true); - } - - requestUpdate() { - this.updateRequest = true; - } - - onUpdate(_deltaTime) { - super.onUpdate(_deltaTime); - - // every 100ms - if (this.minPageUpdateThrottler.canUpdate(_deltaTime) !== -1 && this.updateRequest) { - this.updateRequest = false; - if (this.pageRedrawCallback) { - this.pageRedrawCallback(); - } - } - - // Create a connection to the SimBridge MCDU Server if it is not already connected - // every 1000ms - if (this.mcduServerConnectUpdateThrottler.canUpdate(_deltaTime) !== -1 - && this.getGameState() === GameState.ingame - && this.mcduServerClient - ) { - if (this.mcduServerClient.isConnected()) { - // Check if connection or SimBridge Setting is still valid. - // validateConnection() will return false if the connection is not established - // any longer (probably not the case here as we test isConnected) or if the - // SimBridge Enabled setting (persistent property CONFIG_SIMBRIDGE_ENABLED) is set - // to off - this is the case where this clears the remote MCDU screen and - // disconnects the client. - if (!this.mcduServerClient.validateConnection()) { - this.sendClearScreen(); - this.mcduServerClient.disconnect(); - } - } else { - // not connected - try to connect - this.mcduServerClient.connect(this.mcduServerClientEventHandler.bind(this)); - } - } - - // There is no (known) event when power is turned on or off (e.g. Ext Pwr) and remote clients - // would not be updated (cleared or updated). Therefore, monitoring power is necessary. - // every 500ms - if (this.powerCheckUpdateThrottler.canUpdate(_deltaTime) !== -1) { - const isPoweredL = SimVar.GetSimVarValue("L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED", "Number"); - if (this.lastPowerState !== isPoweredL) { - this.lastPowerState = isPoweredL; - this.onFmPowerStateChanged(isPoweredL); - - if (this.mcduServerClient && this.mcduServerClient.isConnected()) { - this.sendUpdate(); - } - } - } - - // TODO these other mechanisms are replaced in the MCDU split PR - if (this.pageUpdate) { - this.pageUpdate(); - } - this.checkAocTimes(); - this.updateMCDU(); - } - - /* MCDU UPDATE */ - - /** - * Updates the MCDU state. - */ - updateMCDU() { - this.updateAnnunciators(); - - this.updateBrightness(); - - this.updateInitBFuelPred(); - - this.updateAtsuRequest(); - } - - /** - * Checks whether INIT page B is open and an engine is being started, if so: - * The INIT page B reverts to the FUEL PRED page 15 seconds after the first engine start and cannot be accessed after engine start. - */ - updateInitBFuelPred() { - if (this.isAnEngineOn()) { - if (!this.initB) { - this.initB = true; - setTimeout(() => { - if (this.page.Current === this.page.InitPageB && this.isAnEngineOn()) { - CDUFuelPredPage.ShowPage(this); - } - }, 15000); - } - } else { - this.initB = false; - } - } - - updateAnnunciators(forceWrite = false) { - const lightTestPowered = SimVar.GetSimVarValue("L:A32NX_ELEC_DC_2_BUS_IS_POWERED", "bool"); - const lightTest = lightTestPowered && SimVar.GetSimVarValue("L:A32NX_OVHD_INTLT_ANN", "number") === 0; - - // lights are AC1, MCDU is ACC ESS SHED - const leftAnnuncPower = SimVar.GetSimVarValue("L:A32NX_ELEC_AC_1_BUS_IS_POWERED", "bool") && SimVar.GetSimVarValue("L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED", "bool"); - this.updateAnnunciatorsForSide("left", lightTest, leftAnnuncPower, forceWrite); - - // lights and MCDU are both AC2 - const rightAnnuncPower = SimVar.GetSimVarValue("L:A32NX_ELEC_AC_2_BUS_IS_POWERED", "bool"); - this.updateAnnunciatorsForSide("right", lightTest, rightAnnuncPower, forceWrite); - } - - updateBrightness() { - const left = SimVar.GetSimVarValue("L:A32NX_MCDU_L_BRIGHTNESS", "number"); - const right = SimVar.GetSimVarValue("L:A32NX_MCDU_R_BRIGHTNESS", "number"); - - let updateNeeded = false; - - if (left !== this.leftBrightness) { - this.leftBrightness = left; - updateNeeded = true; - } - - if (right !== this.rightBrightness) { - this.rightBrightness = right; - updateNeeded = true; - } - - if (updateNeeded) { - this.sendUpdate(); - } - } - - updateAtsuRequest() { - // the ATSU currently doesn't have the MCDU request signal, so we just check for messages and set it's flag - const msgs = SimVar.GetSimVarValue('L:A32NX_COMPANY_MSG_COUNT', 'number'); - if (msgs > this._lastAtsuMessageCount) { - this.setRequest('ATSU'); - } - this._lastAtsuMessageCount = msgs; - } - - /** - * Updates the annunciator light states for one MCDU. - * @param {'left' | 'right'} side Which MCDU to update. - * @param {boolean} lightTest Whether ANN LT TEST is active. - * @param {boolean} powerOn Whether annunciator LED power is available. - */ - updateAnnunciatorsForSide(side, lightTest, powerOn, forceWrite = false) { - let updateNeeded = false; - - const simVarSide = side.toUpperCase().charAt(0); - - const states = this.annunciators[side]; - for (const [annunc, state] of Object.entries(states)) { - let newState = !!(lightTest && powerOn); - - if (annunc === 'fmgc') { - newState = newState || this.isSubsystemRequesting('FMGC'); - } else if (annunc === 'mcdu_menu') { - newState = newState || this.isSubsystemRequesting('AIDS') || this.isSubsystemRequesting('ATSU') || this.isSubsystemRequesting('CFDS'); - } - - if (newState !== state || forceWrite) { - states[annunc] = newState; - SimVar.SetSimVarValue(`L:A32NX_MCDU_${simVarSide}_ANNUNC_${annunc.toUpperCase()}`, 'bool', newState); - updateNeeded = true; - } - } - - if (updateNeeded) { - this.sendUpdate(); - } - } - checkAocTimes() { - if (!this.aocTimes.off) { - if (this.flightPhaseManager.phase === FmgcFlightPhases.TAKEOFF && !this.isOnGround()) { - // Wheels off - // Off: remains blank until Take off time - this.aocTimes.off = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - } - } - - if (!this.aocTimes.out) { - const currentPKGBrakeState = SimVar.GetSimVarValue("L:A32NX_PARK_BRAKE_LEVER_POS", "Bool"); - if (this.flightPhaseManager.phase === FmgcFlightPhases.PREFLIGHT && !currentPKGBrakeState) { - // Out: is when you set the brakes to off - this.aocTimes.out = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - } - } - - if (!this.aocTimes.on) { - if (this.aocTimes.off && this.isOnGround()) { - // On: remains blank until Landing time - this.aocTimes.on = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - } - } - - if (!this.aocTimes.in) { - const currentPKGBrakeState = SimVar.GetSimVarValue("L:A32NX_PARK_BRAKE_LEVER_POS", "Bool"); - const cabinDoorPctOpen = SimVar.GetSimVarValue("INTERACTIVE POINT OPEN:0", "percent"); - if (this.aocTimes.on && currentPKGBrakeState && cabinDoorPctOpen > 20) { - // In: remains blank until brakes set to park AND the first door opens - this.aocTimes.in = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - } - } - - if (this.flightPhaseManager.phase === FmgcFlightPhases.PREFLIGHT) { - const cabinDoorPctOpen = SimVar.GetSimVarValue("INTERACTIVE POINT OPEN:0", "percent"); - if (!this.aocTimes.doors && cabinDoorPctOpen < 20) { - this.aocTimes.doors = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - } else { - if (cabinDoorPctOpen > 20) { - this.aocTimes.doors = ""; - } - } - } - } - - /* END OF MCDU UPDATE */ - /* MCDU INTERFACE/LAYOUT */ - - _formatCell(str) { - return str - .replace(/{big}/g, "") - .replace(/{small}/g, "") - .replace(/{big}/g, "") - .replace(/{amber}/g, "") - .replace(/{red}/g, "") - .replace(/{green}/g, "") - .replace(/{cyan}/g, "") - .replace(/{white}/g, "") - .replace(/{magenta}/g, "") - .replace(/{yellow}/g, "") - .replace(/{inop}/g, "") - .replace(/{sp}/g, " ") - .replace(/{left}/g, "") - .replace(/{right}/g, "") - .replace(/{end}/g, ""); - } - - getTitle() { - if (this._title === undefined) { - this._title = this._titleElement.textContent; - } - return this._title; - } - - setTitle(content) { - let color = content.split("[color]")[1]; - if (!color) { - color = "white"; - } - this._title = content.split("[color]")[0]; - this._title = `{${color}}${this._title}{end}`; - this._titleElement.textContent = this._title; - } - - setTitleLeft(content) { - if (!content) { - this._titleLeft = ""; - this._titleLeftElement.textContent = ""; - return; - } - let color = content.split("[color]")[1]; - if (!color) { - color = "white"; - } - this._titleLeft = content.split("[color]")[0]; - this._titleLeft = `{${color}}${this._titleLeft}{end}`; - this._titleLeftElement.textContent = this._titleLeft; - } - - setPageCurrent(value) { - if (typeof (value) === "number") { - this._pageCurrent = value; - } else if (typeof (value) === "string") { - this._pageCurrent = parseInt(value); - } - this._pageCurrentElement.textContent = (this._pageCurrent > 0 ? this._pageCurrent : "") + ""; - } - - setPageCount(value) { - if (typeof (value) === "number") { - this._pageCount = value; - } else if (typeof (value) === "string") { - this._pageCount = parseInt(value); - } - this._pageCountElement.textContent = (this._pageCount > 0 ? this._pageCount : "") + ""; - if (this._pageCount === 0) { - this.getChildById("page-slash").textContent = ""; - } else { - this.getChildById("page-slash").textContent = "/"; - } - } - - setLabel(label, row, col = -1, websocketDraw = true) { - if (col >= this._labelElements[row].length) { - return; - } - if (!this._labels[row]) { - this._labels[row] = []; - } - if (!label) { - label = ""; - } - if (col === -1) { - for (let i = 0; i < this._labelElements[row].length; i++) { - this._labels[row][i] = ""; - this._labelElements[row][i].textContent = ""; - } - col = 0; - } - if (label === "__FMCSEPARATOR") { - label = "------------------------"; - } - if (label !== "") { - if (label.indexOf("[b-text]") !== -1) { - label = label.replace("[b-text]", ""); - this._lineElements[row][col].classList.remove("s-text"); - this._lineElements[row][col].classList.add("msg-text"); - } else { - this._lineElements[row][col].classList.remove("msg-text"); - } - - let color = label.split("[color]")[1]; - if (!color) { - color = "white"; - } - const e = this._labelElements[row][col]; - label = label.split("[color]")[0]; - label = `{${color}}${label}{end}`; - } - this._labels[row][col] = label; - this._labelElements[row][col].textContent = label; - - if (websocketDraw) { - this.sendUpdate(); - } - } - - /** - * @param {string|CDU_Field} content - * @param {number} row - * @param {number} col - * @param {boolean} websocketDraw - */ - setLine(content, row, col = -1, websocketDraw = true) { - - if (content instanceof CDU_Field) { - const field = content; - ((col === 0 || col === -1) ? this.onLeftInput : this.onRightInput)[row] = (value) => { - field.onSelect(value); - }; - content = content.getValue(); - } - - if (col >= this._lineElements[row].length) { - return; - } - if (!content) { - content = ""; - } - if (!this._lines[row]) { - this._lines[row] = []; - } - if (col === -1) { - for (let i = 0; i < this._lineElements[row].length; i++) { - this._lines[row][i] = ""; - this._lineElements[row][i].textContent = ""; - } - col = 0; - } - if (content === "__FMCSEPARATOR") { - content = "------------------------"; - } - if (content !== "") { - let color = content.split("[color]")[1]; - if (!color) { - color = "white"; - } - const e = this._lineElements[row][col]; - content = content.split("[color]")[0]; - content = `{${color}}${content}{end}`; - if (content.indexOf("[s-text]") !== -1) { - content = content.replace("[s-text]", ""); - content = `{small}${content}{end}`; - } - } - this._lines[row][col] = content; - this._lineElements[row][col].textContent = this._lines[row][col]; - - if (websocketDraw) { - this.sendUpdate(); - } - } - - setTemplate(template, large = false) { - if (template[0]) { - this.setTitle(template[0][0]); - this.setPageCurrent(template[0][1]); - this.setPageCount(template[0][2]); - this.setTitleLeft(template[0][3]); - } - for (let i = 0; i < 6; i++) { - let tIndex = 2 * i + 1; - if (template[tIndex]) { - if (large) { - if (template[tIndex][1] !== undefined) { - this.setLine(template[tIndex][0], i, 0, false); - this.setLine(template[tIndex][1], i, 1, false); - this.setLine(template[tIndex][2], i, 2, false); - this.setLine(template[tIndex][3], i, 3, false); - } else { - this.setLine(template[tIndex][0], i, -1, false); - } - } else { - if (template[tIndex][1] !== undefined) { - this.setLabel(template[tIndex][0], i, 0, false); - this.setLabel(template[tIndex][1], i, 1, false); - this.setLabel(template[tIndex][2], i, 2, false); - this.setLabel(template[tIndex][3], i, 3, false); - } else { - this.setLabel(template[tIndex][0], i, -1, false); - } - } - } - tIndex = 2 * i + 2; - if (template[tIndex]) { - if (template[tIndex][1] !== undefined) { - this.setLine(template[tIndex][0], i, 0, false); - this.setLine(template[tIndex][1], i, 1, false); - this.setLine(template[tIndex][2], i, 2, false); - this.setLine(template[tIndex][3], i, 3, false); - } else { - this.setLine(template[tIndex][0], i, -1, false); - } - } - } - if (template[13]) { - this.setScratchpadText(template[13][0]); - } - - // Apply formatting helper to title page, lines and labels - if (this._titleElement !== null) { - this._titleElement.innerHTML = this._formatCell(this._titleElement.innerHTML); - } - if (this._titleLeftElement !== null) { - this._titleLeftElement.innerHTML = this._formatCell(this._titleLeftElement.innerHTML); - } - this._lineElements.forEach((row) => { - row.forEach((column) => { - if (column !== null) { - column.innerHTML = this._formatCell(column.innerHTML); - } - }); - }); - this._labelElements.forEach((row) => { - row.forEach((column) => { - if (column !== null) { - column.innerHTML = this._formatCell(column.innerHTML); - } - }); - }); - this.sendUpdate(); - } - - /** - * Sets what arrows will be displayed in the corner of the screen. Arrows are removed when clearDisplay() is called. - * @param {boolean} up - whether the up arrow will be displayed - * @param {boolean} down - whether the down arrow will be displayed - * @param {boolean} left - whether the left arrow will be displayed - * @param {boolean} right - whether the right arrow will be displayed - */ - setArrows(up, down, left, right) { - this._arrows = [up, down, left, right]; - this.arrowHorizontal.style.opacity = (left || right) ? "1" : "0"; - this.arrowVertical.style.opacity = (up || down) ? "1" : "0"; - if (up && down) { - this.arrowVertical.innerHTML = "↓↑\xa0"; - } else if (up) { - this.arrowVertical.innerHTML = "↑\xa0"; - } else { - this.arrowVertical.innerHTML = "↓\xa0\xa0"; - } - if (left && right) { - this.arrowHorizontal.innerHTML = "←→\xa0"; - } else if (right) { - this.arrowHorizontal.innerHTML = "→\xa0"; - } else { - this.arrowHorizontal.innerHTML = "←\xa0\xa0"; - } - } - - clearDisplay(webSocketDraw = false) { - this.onUnload(); - this.onUnload = () => {}; - this.setTitle(""); - this.setTitleLeft(""); - this.setPageCurrent(0); - this.setPageCount(0); - for (let i = 0; i < 6; i++) { - this.setLabel("", i, -1, webSocketDraw); - } - for (let i = 0; i < 6; i++) { - this.setLine("", i, -1, webSocketDraw); - } - this.onLeftInput = []; - this.onRightInput = []; - this.leftInputDelay = []; - this.rightInputDelay = []; - this.onPrevPage = () => {}; - this.onNextPage = () => {}; - this.pageUpdate = () => {}; - this.pageRedrawCallback = null; - if (this.page.Current === this.page.MenuPage) { - this.setScratchpadText(""); - } - this.page.Current = this.page.Clear; - this.setArrows(false, false, false, false); - this.tryDeleteTimeout(); - this.onUp = () => {}; - this.onDown = () => {}; - this.updateRequest = false; - } - - /** - * Set the active subsystem - * @param {'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'} subsystem - */ - set activeSystem(subsystem) { - this._activeSystem = subsystem; - this.scratchpad = this.scratchpads[subsystem]; - this._clearRequest(subsystem); - } - - get activeSystem() { - return this._activeSystem; - } - - set scratchpad(sp) { - if (sp === this._scratchpad) { - return; - } - - // pause the old scratchpad so it stops writing to the display - if (this._scratchpad) { - this._scratchpad.pause(); - } - - // set the new scratchpad and resume it to update the display - this._scratchpad = sp; - this._scratchpad.resume(); - } - - get scratchpad() { - return this._scratchpad; - } - - get mcduScratchpad() { - return this.scratchpads['MCDU']; - } - - get fmgcScratchpad() { - return this.scratchpads['FMGC']; - } - - get atsuScratchpad() { - return this.scratchpads['ATSU']; - } - - get aidsScratchpad() { - return this.scratchpads['AIDS']; - } - - get cfdsScratchpad() { - return this.scratchpads['CFDS']; - } - - activateMcduScratchpad() { - this.scratchpad = this.scratchpads['MCDU']; - } - - /** - * Check if there is an active request from a subsystem to the MCDU - * @param {'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'} subsystem - * @returns true if an active request exists - */ - isSubsystemRequesting(subsystem) { - return this.requests[subsystem] === true; - } - - /** - * Set a request from a subsystem to the MCDU - * @param {'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'} subsystem - */ - setRequest(subsystem) { - if (!(subsystem in this.requests) || this.activeSystem === subsystem) { - return; - } - if (!this.requests[subsystem]) { - this.requests[subsystem] = true; - - // refresh the menu page if active - if (this.page.Current === this.page.MenuPage) { - CDUMenuPage.ShowPage(this); - } - } - } - - /** - * Clear a request from a subsystem to the MCDU - * @param {'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'} subsystem - */ - _clearRequest(subsystem) { - if (!(subsystem in this.requests)) { - return; - } - - if (this.requests[subsystem]) { - this.requests[subsystem] = false; - - // refresh the menu page if active - if (this.page.Current === this.page.MenuPage) { - CDUMenuPage.ShowPage(this); - } - } - } - - generateHTMLLayout(parent) { - while (parent.children.length > 0) { - parent.removeChild(parent.children[0]); - } - const header = document.createElement("div"); - header.id = "header"; - - const titleLeft = document.createElement("div"); - titleLeft.classList.add("s-text"); - titleLeft.id = "title-left"; - parent.appendChild(titleLeft); - - const title = document.createElement("span"); - title.id = "title"; - header.appendChild(title); - - this.arrowHorizontal = document.createElement("span"); - this.arrowHorizontal.id = "arrow-horizontal"; - this.arrowHorizontal.innerHTML = "←→\xa0"; - header.appendChild(this.arrowHorizontal); - - parent.appendChild(header); - - const page = document.createElement("div"); - page.id = "page-info"; - page.classList.add("s-text"); - - const pageCurrent = document.createElement("span"); - pageCurrent.id = "page-current"; - - const pageSlash = document.createElement("span"); - pageSlash.id = "page-slash"; - pageSlash.textContent = "/"; - - const pageCount = document.createElement("span"); - pageCount.id = "page-count"; - - page.appendChild(pageCurrent); - page.appendChild(pageSlash); - page.appendChild(pageCount); - parent.appendChild(page); - - for (let i = 0; i < 6; i++) { - const label = document.createElement("div"); - label.classList.add("label", "s-text"); - const labelLeft = document.createElement("span"); - labelLeft.id = "label-" + i + "-left"; - labelLeft.classList.add("fmc-block", "label", "label-left"); - const labelRight = document.createElement("span"); - labelRight.id = "label-" + i + "-right"; - labelRight.classList.add("fmc-block", "label", "label-right"); - const labelCenter = document.createElement("span"); - labelCenter.id = "label-" + i + "-center"; - labelCenter.classList.add("fmc-block", "label", "label-center"); - label.appendChild(labelLeft); - label.appendChild(labelRight); - label.appendChild(labelCenter); - parent.appendChild(label); - const line = document.createElement("div"); - line.classList.add("line"); - const lineLeft = document.createElement("span"); - lineLeft.id = "line-" + i + "-left"; - lineLeft.classList.add("fmc-block", "line", "line-left"); - const lineRight = document.createElement("span"); - lineRight.id = "line-" + i + "-right"; - lineRight.classList.add("fmc-block", "line", "line-right"); - const lineCenter = document.createElement("span"); - lineCenter.id = "line-" + i + "-center"; - lineCenter.classList.add("fmc-block", "line", "line-center"); - line.appendChild(lineLeft); - line.appendChild(lineRight); - line.appendChild(lineCenter); - parent.appendChild(line); - } - const footer = document.createElement("div"); - footer.classList.add("line"); - const inout = document.createElement("span"); - inout.id = "in-out"; - this.arrowVertical = document.createElement("span"); - this.arrowVertical.id = "arrow-vertical"; - this.arrowVertical.innerHTML = "↓↑\xa0"; - - footer.appendChild(inout); - footer.appendChild(this.arrowVertical); - parent.appendChild(footer); - } - - /* END OF MCDU INTERFACE/LAYOUT */ - /* MCDU SCRATCHPAD */ - - setScratchpadUserData(value) { - this.scratchpad.setUserData(value); - } - - clearFocus() { - this.inFocus = false; - this.allSelected = false; - try { - Coherent.trigger('UNFOCUS_INPUT_FIELD', this.scratchpadDisplay.guid); - } catch (e) { - console.error(e); - } - this.scratchpadDisplay.setStyle(null); - this.getChildById("header").style = null; - if (this.check_focus) { - clearInterval(this.check_focus); - } - } - - initKeyboardScratchpad() { - window.document.addEventListener('click', () => { - - const mcduInput = NXDataStore.get("MCDU_KB_INPUT", "DISABLED"); - const mcduTimeout = parseInt(NXDataStore.get("CONFIG_MCDU_KB_TIMEOUT", "60")); - const isPoweredL = SimVar.GetSimVarValue("L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED", "Number"); - const isPoweredR = SimVar.GetSimVarValue("L:A32NX_ELEC_AC_2_BUS_IS_POWERED", "Number"); - - // TODO: L/R MCDU - if (mcduInput === "ENABLED") { - this.inFocus = !this.inFocus; - if (this.inFocus && (isPoweredL || isPoweredR)) { - this.getChildById("header").style = "background: linear-gradient(180deg, rgba(2,182,217,1.0) 65%, rgba(255,255,255,0.0) 65%);"; - this.scratchpadDisplay.setStyle("display: inline-block; width:87%; background: rgba(255,255,255,0.2);"); - try { - Coherent.trigger('FOCUS_INPUT_FIELD', this.scratchpadDisplay.guid, '', '', '', false); - } catch (e) { - console.error(e); - } - this.lastInput = new Date(); - if (mcduTimeout) { - this.check_focus = setInterval(() => { - if (Math.abs(new Date() - this.lastInput) / 1000 >= mcduTimeout) { - this.clearFocus(); - } - }, Math.min(mcduTimeout * 1000 / 2, 1000)); - } - } else { - this.clearFocus(); - } - } else { - this.clearFocus(); - } - }); - window.document.addEventListener('keydown', (e) => { - // MCDU should not accept input while unpowered - if (this.inFocus && SimVar.GetSimVarValue("L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED", "Number")) { - let keycode = e.keyCode; - this.lastInput = new Date(); - if (keycode >= KeyCode.KEY_NUMPAD0 && keycode <= KeyCode.KEY_NUMPAD9) { - keycode -= 48; // numpad support - } - // Note: tried using H-events, worse performance. Reverted to direct input. - // Preventing repeated input also similarly felt awful and defeated the point. - // Clr hold functionality pointless as scratchpad will be cleared (repeated input). - - if (e.altKey || (e.ctrlKey && keycode === KeyCode.KEY_Z)) { - this.clearFocus(); - } else if (e.ctrlKey && keycode === KeyCode.KEY_A) { - this.allSelected = !this.allSelected; - this.scratchpadDisplay.setStyle(`display: inline-block; width:87%; background: ${this.allSelected ? 'rgba(235,64,52,1.0)' : 'rgba(255,255,255,0.2)'};`); - } else if (e.shiftKey && e.ctrlKey && keycode === KeyCode.KEY_BACK_SPACE) { - this.setScratchpadText(""); - } else if (e.ctrlKey && keycode === KeyCode.KEY_BACK_SPACE) { - const scratchpadTextContent = this.scratchpad.getText(); - let wordFlag = !scratchpadTextContent.includes(' '); - for (let i = scratchpadTextContent.length; i > 0; i--) { - if (scratchpadTextContent.slice(-1) === ' ') { - if (!wordFlag) { - this.onClr(); - } else { - wordFlag = true; - break; - } - } - if (scratchpadTextContent.slice(-1) !== ' ') { - if (!wordFlag) { - wordFlag = true; - } else { - this.onClr(); - } - } - } - } else if (e.shiftKey && keycode === KeyCode.KEY_BACK_SPACE) { - if (!this.check_clr) { - this.onClr(); - this.check_clr = setTimeout(() => { - this.onClrHeld(); - }, 2000); - } - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_1_CLR", "Number", 1); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_2_CLR", "Number", 1); - } else if (keycode >= KeyCode.KEY_0 && keycode <= KeyCode.KEY_9 || keycode >= KeyCode.KEY_A && keycode <= KeyCode.KEY_Z) { - const letter = String.fromCharCode(keycode); - this.onLetterInput(letter); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_1_" + letter.toUpperCase(), "Number", 1); // TODO: L/R [1/2] side MCDU Split - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_2_" + letter.toUpperCase(), "Number", 1); - } else if (keycode === KeyCode.KEY_PERIOD || keycode === KeyCode.KEY_DECIMAL) { - this.onDot(); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_1_DOT", "Number", 1); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_2_DOT", "Number", 1); - } else if (keycode === KeyCode.KEY_SLASH || keycode === KeyCode.KEY_BACK_SLASH || keycode === KeyCode.KEY_DIVIDE || keycode === 226) { - this.onDiv(); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_1_SLASH", "Number", 1); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_2_SLASH", "Number", 1); - } else if (keycode === KeyCode.KEY_BACK_SPACE || keycode === KeyCode.KEY_DELETE) { - if (this.allSelected) { - this.setScratchpadText(""); - } else if (!this.clrStop) { - this.onClr(); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_1_CLR", "Number", 1); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_2_CLR", "Number", 1); - this.clrStop = this.scratchpad.isClearStop(); - } - } else if (keycode === KeyCode.KEY_SPACE) { - this.onSp(); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_1_SP", "Number", 1); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_2_SP", "Number", 1); - } else if (keycode === 189 || keycode === KeyCode.KEY_SUBTRACT) { - this.onPlusMinus("-"); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_1_PLUSMINUS", "Number", 1); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_2_PLUSMINUS", "Number", 1); - } else if (keycode === 187 || keycode === KeyCode.KEY_ADD) { - this.onPlusMinus("+"); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_1_PLUSMINUS", "Number", 1); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_2_PLUSMINUS", "Number", 1); - } else if (keycode >= KeyCode.KEY_F1 && keycode <= KeyCode.KEY_F6) { - const func_num = keycode - KeyCode.KEY_F1; - this.onLeftFunction(func_num); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_1_L" + (func_num + 1), "Number", 1); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_2_L" + (func_num + 1), "Number", 1); - } else if (keycode >= KeyCode.KEY_F7 && keycode <= KeyCode.KEY_F12) { - const func_num = keycode - KeyCode.KEY_F7; - this.onRightFunction(func_num); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_1_R" + (func_num + 1), "Number", 1); - SimVar.SetSimVarValue("L:A32NX_MCDU_PUSH_ANIM_2_R" + (func_num + 1), "Number", 1); - } - } - }); - window.document.addEventListener('keyup', (e) => { - this.lastInput = new Date(); - const keycode = e.keyCode; - if (keycode === KeyCode.KEY_BACK_SPACE || keycode === KeyCode.KEY_DELETE) { - this.clrStop = false; - } - if (this.check_clr) { - clearTimeout(this.check_clr); - this.check_clr = undefined; - } - }); - } - - /* END OF MCDU SCRATCHPAD */ - /* MCDU MESSAGE SYSTEM */ - - /** - * Display a type I message on the active subsystem's scratch pad - * @param message {TypeIMessage} - */ - setScratchpadMessage(message) { - if (typeof message === 'TypeIIMessage') { - console.error('Type II message passed to setScratchpadMessage!', message); - return; - } - - if (this.scratchpad) { - this.scratchpad.setMessage(message); - } - } - - setScratchpadText(value) { - this.scratchpad.setText(value); - } - - /** - * Tries to show the MODIFY page if the MCDU is in the ATC COM system - */ - tryToShowAtcModifyPage() { - if (this.page.Current >= this.page.ATCMenu && this.page.Current < this.page.ATCComLastId) { - CDUAtcMessageModify.ShowPage(this, this.atsu.modificationMessage); - } - } - - /** - * General ATSU message handler which converts ATSU status codes to new MCDU messages - * @param code ATSU status code - */ - addNewAtsuMessage(code) { - if (!this.atsuScratchpad) { - return; - } - switch (code) { - case AtsuCommon.AtsuStatusCodes.CallsignInUse: - this.atsuScratchpad.setMessage(NXFictionalMessages.fltNbrInUse); - break; - case AtsuCommon.AtsuStatusCodes.NoHoppieConnection: - this.atsuScratchpad.setMessage(NXFictionalMessages.noHoppieConnection); - break; - case AtsuCommon.AtsuStatusCodes.ComFailed: - this.atsuScratchpad.setMessage(NXSystemMessages.comUnavailable); - break; - case AtsuCommon.AtsuStatusCodes.NoAtc: - this.atsuScratchpad.setMessage(NXSystemMessages.noAtc); - break; - case AtsuCommon.AtsuStatusCodes.MailboxFull: - this.atsuScratchpad.setMessage(NXSystemMessages.dcduFileFull); - break; - case AtsuCommon.AtsuStatusCodes.UnknownMessage: - this.atsuScratchpad.setMessage(NXFictionalMessages.unknownAtsuMessage); - break; - case AtsuCommon.AtsuStatusCodes.ProxyError: - this.atsuScratchpad.setMessage(NXFictionalMessages.reverseProxy); - break; - case AtsuCommon.AtsuStatusCodes.NoTelexConnection: - this.atsuScratchpad.setMessage(NXFictionalMessages.telexNotEnabled); - break; - case AtsuCommon.AtsuStatusCodes.OwnCallsign: - this.atsuScratchpad.setMessage(NXSystemMessages.noAtc); - break; - case AtsuCommon.AtsuStatusCodes.SystemBusy: - this.atsuScratchpad.setMessage(NXSystemMessages.systemBusy); - break; - case AtsuCommon.AtsuStatusCodes.NewAtisReceived: - this.atsuScratchpad.setMessage(NXSystemMessages.newAtisReceived); - break; - case AtsuCommon.AtsuStatusCodes.NoAtisReceived: - this.atsuScratchpad.setMessage(NXSystemMessages.noAtisReceived); - break; - case AtsuCommon.AtsuStatusCodes.EntryOutOfRange: - this.atsuScratchpad.setMessage(NXSystemMessages.entryOutOfRange); - break; - case AtsuCommon.AtsuStatusCodes.FormatError: - this.atsuScratchpad.setMessage(NXSystemMessages.formatError); - break; - case AtsuCommon.AtsuStatusCodes.NotInDatabase: - this.atsuScratchpad.setMessage(NXSystemMessages.notInDatabase); - default: - break; - } - } - - /* END OF MCDU MESSAGE SYSTEM */ - /* MCDU EVENTS */ - - onPowerOn() { - super.onPowerOn(); - } - - onEvent(_event) { - super.onEvent(_event); - - // MCDU should not accept input while unpowered - if (!SimVar.GetSimVarValue("L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED", "Number")) { - return; - } - - const isLeftMcduEvent = _event.indexOf("1_BTN_") !== -1; - const isRightMcduEvent = _event.indexOf("2_BTN_") !== -1; - - if (isLeftMcduEvent || isRightMcduEvent || _event.indexOf("BTN_") !== -1) { - const input = _event.replace("1_BTN_", "").replace("2_BTN_", "").replace("BTN_", ""); - if (this._keypad.onKeyPress(input, isRightMcduEvent ? 'R' : 'L')) { - return; - } - - if (input.length === 2 && input[0] === "L") { - const v = parseInt(input[1]) - 1; - if (isFinite(v)) { - this.onLeftFunction(v); - } - } else if (input.length === 2 && input[0] === "R") { - const v = parseInt(input[1]) - 1; - if (isFinite(v)) { - this.onRightFunction(v); - } - } else { - console.log("'" + input + "'"); - } - } - } - - onLsk(fncAction, fncActionDelay = this.getDelayBasic) { - if (!fncAction) { - return; - } - - // First timeout simulates delay for key press - // Second delay simulates delay for input validation - const cur = this.page.Current; - setTimeout(() => { - const value = this.scratchpad.removeUserContentFromScratchpadAndDisplayAndReturnTextContent(); - setTimeout(() => { - if (this.page.Current === cur) { - fncAction(value, () => this.setScratchpadUserData(value)); - } - }, fncActionDelay()); - }, 100); - } - - /** - * Handle brightness key events - * @param {'L' | 'R'} side - * @param {-1 | 1} sign - */ - onBrightnessKey(side, sign) { - const oldBrightness = side === "R" ? this.rightBrightness : this.leftBrightness; - SimVar.SetSimVarValue(`L:A32NX_MCDU_${side}_BRIGHTNESS`, "number", Math.max(this.MIN_BRIGHTNESS, Math.min(this.MAX_BRIGHTNESS, oldBrightness + sign * 0.2 * oldBrightness))); - } - - /* END OF MCDU EVENTS */ - /* MCDU DELAY SIMULATION */ - - /** - * Used for switching pages - * @returns {number} delay in ms between 150 and 200 - */ - getDelaySwitchPage() { - return 150 + 50 * Math.random(); - } - - /** - * Used for basic inputs e.g. alternate airport, ci, fl, temp, constraints, ... - * @returns {number} delay in ms between 300 and 400 - */ - getDelayBasic() { - return 300 + 100 * Math.random(); - } - - /** - * Used for e.g. loading time fore pages - * @returns {number} delay in ms between 600 and 800 - */ - getDelayMedium() { - return 600 + 200 * Math.random(); - } - - /** - * Used for intense calculation - * @returns {number} delay in ms between 900 and 12000 - */ - getDelayHigh() { - return 900 + 300 * Math.random(); - } - - /** - * Used for calculation time for fuel pred page - * @returns {number} dynamic delay in ms between 2000ms and 4000ms - */ - getDelayFuelPred() { - return Math.max(2000, Math.min(4000, 225 * this.getActivePlanLegCount())); - } - - /** - * Used to load wind data into sfms - * @returns {number} dynamic delay in ms dependent on amount of waypoints - */ - getDelayWindLoad() { - return Math.pow(this.getActivePlanLegCount(), 2); - } - - /** - * Tries to delete a pages timeout - */ - tryDeleteTimeout() { - if (this.page.SelfPtr) { - clearTimeout(this.page.SelfPtr); - this.page.SelfPtr = false; - } - } - - /* END OF MCDU DELAY SIMULATION */ - /* MCDU AOC MESSAGE SYSTEM */ - - printPage(lines) { - if (this.printing) { - return; - } - this.printing = true; - - const formattedValues = lines.map((l) => { - return l.replace(/\[color]cyan/g, "
") - .replace(/{white}[-]{3,}{end}/g, "
") - .replace(/{end}/g, "
") - .replace(/(\[color][a-z]*)/g, "") - .replace(/{[a-z]*}/g, ""); - }); - - const websocketLines = formattedValues.map((l) => { - return l.replace(/[ ]*/g, '\n'); - }); - - if (SimVar.GetSimVarValue("L:A32NX_PRINTER_PRINTING", "bool") === 1) { - SimVar.SetSimVarValue("L:A32NX_PAGES_PRINTED", "number", SimVar.GetSimVarValue("L:A32NX_PAGES_PRINTED", "number") + 1); - SimVar.SetSimVarValue("L:A32NX_PRINT_PAGE_OFFSET", "number", 0); - } - SimVar.SetSimVarValue("L:A32NX_PRINT_LINES", "number", lines.length); - SimVar.SetSimVarValue("L:A32NX_PAGE_ID", "number", SimVar.GetSimVarValue("L:A32NX_PAGE_ID", "number") + 1); - SimVar.SetSimVarValue("L:A32NX_PRINTER_PRINTING", "bool", 0).then(v => { - this.fmgcMesssagesListener.triggerToAllSubscribers('A32NX_PRINT', formattedValues); - this.sendToMcduServerClient(`print:${JSON.stringify({lines: websocketLines})}`); - setTimeout(() => { - SimVar.SetSimVarValue("L:A32NX_PRINTER_PRINTING", "bool", 1); - this.printing = false; - }, 2500); - }); - } - - /* END OF MCDU AOC MESSAGE SYSTEM */ - - /* MCDU SERVER CLIENT */ - - /** - * Sends a message to the websocket server (if connected) - * @param {string} message - */ - sendToMcduServerClient(message) { - if (this.mcduServerClient && this.mcduServerClient.isConnected()) { - try { - this.mcduServerClient.send(message); - } catch (e) { - /** ignore **/ - } - } - } - - /** - * Sends an update to the websocket server (if connected) with the current state of the MCDU - */ - sendUpdate() { - // only calculate update when mcduServerClient is established. - if (this.mcduServerClient && !this.mcduServerClient.isConnected()) { - return; - } - let left = this.emptyLines; - let right = this.emptyLines; - - const mcdu1Powered = SimVar.GetSimVarValue("L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED", "bool"); - const mcdu2Powered = SimVar.GetSimVarValue("L:A32NX_ELEC_AC_2_BUS_IS_POWERED", "bool"); - const integralLightsPowered = SimVar.GetSimVarValue("L:A32NX_ELEC_AC_1_BUS_IS_POWERED", "bool"); - - let screenState; - if (mcdu1Powered || mcdu2Powered) { - screenState = { - lines: [ - this._labels[0], - this._lines[0], - this._labels[1], - this._lines[1], - this._labels[2], - this._lines[2], - this._labels[3], - this._lines[3], - this._labels[4], - this._lines[4], - this._labels[5], - this._lines[5], - ], - scratchpad: `{${this.scratchpadDisplay.getColor()}}${this.scratchpadDisplay.getText()}{end}`, - title: this._title, - titleLeft: `{small}${this._titleLeft}{end}`, - page: this._pageCount > 0 ? `{small}${this._pageCurrent}/${this._pageCount}{end}` : '', - arrows: this._arrows, - integralBrightness: integralLightsPowered ? SimVar.GetSimVarValue("A:LIGHT POTENTIOMETER:85", "percent over 100") : 0, - }; - } - - if (mcdu1Powered) { - left = Object.assign({}, screenState); - left.annunciators = this.annunciators.left; - left.displayBrightness = this.leftBrightness / this.MAX_BRIGHTNESS; - } - - if (mcdu2Powered) { - right = Object.assign({}, screenState); - right.annunciators = this.annunciators.right; - right.displayBrightness = this.rightBrightness / this.MAX_BRIGHTNESS; - } - - const content = {right, left}; - this.sendToMcduServerClient(`update:${JSON.stringify(content)}`); - } - - /** - * Clears the remote MCDU clients' screens - */ - sendClearScreen() { - // only calculate update when mcduServerClient is established. - if (this.mcduServerClient && !this.mcduServerClient.isConnected()) { - return; - } - const left = this.emptyLines; - const right = left; - const content = {right, left}; - this.sendToMcduServerClient(`update:${JSON.stringify(content)}`); - } - - /* END OF WEBSOCKET */ - - goToFuelPredPage() { - if (this.isAnEngineOn()) { - CDUFuelPredPage.ShowPage(this); - } else { - CDUInitPage.ShowPage2(this); - } - } - - logTroubleshootingError(msg) { - this.bus.pub('troubleshooting_log_error', String(msg), true, false); - } -} -registerInstrument("a320-neo-cdu-main-display", A320_Neo_CDU_MainDisplay); diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_MenuPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_MenuPage.js deleted file mode 100644 index 14f2c8c3274..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_MenuPage.js +++ /dev/null @@ -1,100 +0,0 @@ -class CDUMenuPage { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.MenuPage; - // The MCDU MENU does not maintain an editable scratchpad... subsystems and the backup nav do that. - mcdu.activateMcduScratchpad(); - - const fmActive = mcdu.activeSystem === "FMGC"; - const atsuActive = mcdu.activeSystem === "ATSU"; - const aidsActive = mcdu.activeSystem === "AIDS"; - const cfdsActive = mcdu.activeSystem === "CFDS"; - - // delay to get text and draw already connected subsystem page - const connectedSubsystemDelay = 200; - // delay to establish initial communication with disconnect systems on low speed ports - const disconnectedSubsystemDelay = Math.floor(Math.random() * 800) + 500; - - /** - * Updates the page text. - * @param {"FMGC" | "ATSU" | "AIDS" | "CFDS" | null} selectedSystem Newly selected system establishing comms, or null if none. - */ - const updateView = (selectedSystem = null) => { - const getText = (name, isRequesting = false, isLeft = true) => { - let flag = null; - if (selectedSystem !== null) { - if (selectedSystem === name) { - flag = "(SEL)"; - } - } else if (isRequesting) { - flag = "(REQ)"; - } - if (isLeft) { - return `${name}\xa0${flag !== null ? flag : ""}`; - } else { - return `${flag !== null ? flag : ""}\xa0${name}`; - } - }; - const getColor = (isActive, isSelected) => isSelected ? Column.cyan : (isActive && selectedSystem === null ? Column.green : Column.white); - - mcdu.setTemplate(FormatTemplate([ - [new Column(7, "MCDU MENU")], - [new Column(22, "SELECT", Column.right, Column.inop)], - [ - new Column(0, getText("", Column.right, Column.inop) - ], - [""], - [new Column(0, getText(" { - mcdu.mcduScratchpad.setMessage(NXSystemMessages.waitForSystemResponse); - updateView("FMGC"); - setTimeout(() => { - mcdu.mcduScratchpad.removeMessage(NXSystemMessages.waitForSystemResponse.text); - CDUIdentPage.ShowPage(mcdu); - }, connectedSubsystemDelay); // FMGCs are on high-speed port... always fast - }; - - mcdu.onLeftInput[1] = () => { - mcdu.mcduScratchpad.setMessage(NXSystemMessages.waitForSystemResponse); - updateView("ATSU"); - setTimeout(() => { - mcdu.mcduScratchpad.removeMessage(NXSystemMessages.waitForSystemResponse.text); - CDUAtsuMenu.ShowPage(mcdu); - }, atsuActive ? connectedSubsystemDelay : disconnectedSubsystemDelay); - }; - - mcdu.onLeftInput[2] = () => { - mcdu.mcduScratchpad.setMessage(NXSystemMessages.waitForSystemResponse); - updateView("AIDS"); - setTimeout(() => { - mcdu.mcduScratchpad.removeMessage(NXSystemMessages.waitForSystemResponse.text); - CDU_AIDS_MainMenu.ShowPage(mcdu); - }, aidsActive ? connectedSubsystemDelay : disconnectedSubsystemDelay); - }; - - mcdu.onLeftInput[3] = () => { - mcdu.mcduScratchpad.setMessage(NXSystemMessages.waitForSystemResponse); - updateView("CFDS"); - setTimeout(() => { - mcdu.mcduScratchpad.removeMessage(NXSystemMessages.waitForSystemResponse.text); - CDUCfdsMainMenu.ShowPage(mcdu); - }, cfdsActive ? connectedSubsystemDelay : disconnectedSubsystemDelay); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_NavRadioPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_NavRadioPage.js deleted file mode 100644 index 5cce5488b8e..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_NavRadioPage.js +++ /dev/null @@ -1,397 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUNavRadioPage { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.NavRadioPage; - mcdu.activeSystem = 'FMGC'; - mcdu.pageRedrawCallback = () => { - CDUNavRadioPage.ShowPage(mcdu); - }; - setTimeout(mcdu.requestUpdate.bind(mcdu), 500); - mcdu.returnPageCallback = () => { - CDUNavRadioPage.ShowPage(mcdu); - }; - - const lsk1Row = []; - const lsk2Row = []; - const lsk3Row = []; - const lsk4Title = ["CRS"]; - const lsk4Row = []; - const lsk5Row = []; - const lsk6Row = []; - - // this is the state when FM radio tuning is not active - const template = [ - ["RADIO NAV"], - ["VOR1/FREQ", "FREQ/VOR2"], - lsk1Row, - ["CRS", "CRS"], - lsk2Row, - ["\xa0LS\xa0/FREQ"], - lsk3Row, - lsk4Title, - lsk4Row, - ["ADF1/FREQ", "FREQ/ADF2"], - lsk5Row, - [""], - lsk6Row, - ]; - - if (!mcdu.isFmTuningActive()) { - mcdu.setTemplate(template); - return; - } - - // VOR 1 - lsk1Row[0] = CDUNavRadioPage.renderVor(mcdu, 1); - mcdu.onLeftInput[0] = CDUNavRadioPage.handleVorLsk.bind(this, mcdu, 1); - lsk2Row[0] = CDUNavRadioPage.renderVorCrs(mcdu, 1); - mcdu.onLeftInput[1] = CDUNavRadioPage.handleVorCrsLsk.bind(this, mcdu, 1); - - // VOR 2 - lsk1Row[1] = CDUNavRadioPage.renderVor(mcdu, 2); - mcdu.onRightInput[0] = CDUNavRadioPage.handleVorLsk.bind(this, mcdu, 2); - lsk2Row[1] = CDUNavRadioPage.renderVorCrs(mcdu, 2); - mcdu.onRightInput[1] = CDUNavRadioPage.handleVorCrsLsk.bind(this, mcdu, 2); - - // LS - lsk3Row[0] = CDUNavRadioPage.renderMmr(mcdu); - mcdu.onLeftInput[2] = CDUNavRadioPage.handleMmrLsk.bind(this, mcdu); - lsk4Row[0] = CDUNavRadioPage.renderMmrCrs(mcdu); - mcdu.onLeftInput[3] = CDUNavRadioPage.handleMmrCrsLsk.bind(this, mcdu); - lsk4Title[0] = CDUNavRadioPage.renderMmrCrsTitle(mcdu); - - // ADF 1 - lsk5Row[0] = CDUNavRadioPage.renderAdf(mcdu, 1); - mcdu.onLeftInput[4] = CDUNavRadioPage.handleAdfLsk.bind(this, mcdu, 1); - // FIXME BFO - - // ADF 2 - lsk5Row[1] = CDUNavRadioPage.renderAdf(mcdu, 2); - mcdu.onRightInput[4] = CDUNavRadioPage.handleAdfLsk.bind(this, mcdu, 2); - // FIXME BFO - - mcdu.setTemplate(template); - } - - static handleVorLsk(mcdu, receiverIndex, input, scratchpadCallback) { - if (input === FMCMainDisplay.clrValue) { - const vor = mcdu.getVorTuningData(receiverIndex); - if (vor.manual) { - mcdu.setManualVor(receiverIndex, null); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } - } else if (input.match(/^\d{3}(\.\d{1,2})?$/) !== null) { - const freq = parseInt(input.replace('.', '').padEnd(5, '0'), 16) << 8; - - if (!Fmgc.RadioUtils.isValidRange(freq, Fmgc.RadioUtils.RadioChannelType.VhfNavaid50)) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - return false; - } else if (!Fmgc.RadioUtils.isValidSpacing(freq, Fmgc.RadioUtils.RadioChannelType.VhfNavaid50)) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return false; - } - - mcdu.setManualVor(receiverIndex, Fmgc.RadioUtils.unpackBcd32(freq) / 1e6); - } else if (input.length >= 1 && input.length <= 4) { - // ident - mcdu.getOrSelectVORsByIdent(input, (navaid) => { - if (navaid) { - if (mcdu.deselectedNavaids.find((databaseId) => databaseId === navaid.databaseId)) { - mcdu.setScratchpadMessage(NXSystemMessages.xxxIsDeselected.getModifiedMessage(navaid.ident)); - scratchpadCallback(); - return; - } - mcdu.setManualVor(receiverIndex, navaid); - mcdu.requestCall(() => { - CDUNavRadioPage.ShowPage(mcdu); - }); - } else { - // FIXME new navaid page when it's built - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - scratchpadCallback(); - } - }); - return; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - mcdu.requestCall(() => { - CDUNavRadioPage.ShowPage(mcdu); - }); - } - - static renderVor(mcdu, receiverIndex) { - const vor = mcdu.getVorTuningData(receiverIndex); - let identText; - let freqText; - if (vor.frequency !== null) { - const ident = vor.ident !== null ? vor.ident : "[\xa0\xa0]"; - identText = `{${vor.manual ? 'big' : 'small'}}${receiverIndex === 2 ? ident.padEnd(4, '\xa0') : ident.padStart(4, '\xa0')}{end}`; - freqText = `{${vor.manual && vor.ident === null ? 'big' : 'small'}}${vor.frequency.toFixed(2)}{end}`; - } else { - identText = "[\xa0\xa0]"; - freqText = "[\xa0\xa0.\xa0]"; - } - - return `{cyan}${receiverIndex === 2 ? freqText : identText}{${vor.manual ? 'big' : 'small'}}/{end}${receiverIndex === 2 ? identText : freqText}{end}`; - } - - static handleVorCrsLsk(mcdu, receiverIndex, input, scratchpadCallback) { - if (input === FMCMainDisplay.clrValue) { - mcdu.setVorCourse(receiverIndex, null); - } else if (input.match(/^\d{1,3}$/) !== null) { - const course = parseInt(input); - if (course < 0 || course > 360) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - return; - } - mcdu.setVorCourse(receiverIndex, course % 360); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - mcdu.requestCall(() => { - CDUNavRadioPage.ShowPage(mcdu); - }); - } - - static renderVorCrs(mcdu, receiverIndex) { - // FIXME T suffix for true-ref VORs - const vor = mcdu.getVorTuningData(receiverIndex); - if (vor.dmeOnly) { - return ""; - } - if (vor.course !== null) { - return `{cyan}${vor.course.toFixed(0).padStart(3, "0")}{end}`; - } - return "{cyan}[\xa0]{end}"; - } - - static handleMmrLsk(mcdu, input, scratchpadCallback) { - if (mcdu.isMmrTuningLocked()) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - return; - } - - const onDone = mcdu.requestCall.bind(mcdu, (() => { - CDUNavRadioPage.ShowPage(mcdu); - })); - - if (input === FMCMainDisplay.clrValue) { - const mmr = mcdu.getMmrTuningData(1); - if (mmr.manual) { - mcdu.setManualIls(null).then(onDone); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } - } else if (input.match(/^\d{3}(\.\d{1,2})?$/) !== null) { - const freq = parseInt(input.replace('.', '').padEnd(5, '0'), 16) << 8; - - if (!Fmgc.RadioUtils.isValidRange(freq, Fmgc.RadioUtils.RadioChannelType.IlsNavaid50)) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - return false; - } else if (!Fmgc.RadioUtils.isValidSpacing(freq, Fmgc.RadioUtils.RadioChannelType.IlsNavaid50)) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return false; - } - - mcdu.setManualIls(Fmgc.RadioUtils.unpackBcd32(freq) / 1e6).then(onDone); - } else if (input.length >= 1 && input.length <= 4) { - // ident - mcdu.getOrSelectILSsByIdent(input, (navaid) => { - if (navaid) { - if (mcdu.deselectedNavaids.find((databaseId) => databaseId === navaid.databaseId)) { - mcdu.setScratchpadMessage(NXSystemMessages.xxxIsDeselected.getModifiedMessage(navaid.ident)); - scratchpadCallback(); - return; - } - - mcdu.setManualIls(navaid).then(onDone); - } else { - // FIXME new navaid page when it's built - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - scratchpadCallback(); - } - }); - return; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - } - - static renderMmr(mcdu) { - const mmr = mcdu.getMmrTuningData(1); - let identText; - let freqText; - if (mmr.frequency !== null) { - const ident = mmr.ident !== null ? mmr.ident : "[\xa0\xa0]"; - identText = `{${mmr.manual ? 'big' : 'small'}}${ident.padStart(4, '\xa0')}{end}`; - freqText = `{${mmr.manual && mmr.ident === null ? 'big' : 'small'}}${mmr.frequency.toFixed(2)}{end}`; - } else { - identText = "[\xa0\xa0]"; - freqText = "[\xa0\xa0\xa0\xa0]"; - } - - return `{cyan}${identText}{${mmr.manual ? 'big' : 'small'}}/{end}${freqText}{end}`; - } - - static handleMmrCrsLsk(mcdu, input, scratchpadCallback) { - if (mcdu.isMmrTuningLocked()) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - return; - } - - if (input === FMCMainDisplay.clrValue) { - const mmr = mcdu.getMmrTuningData(1); - if (mmr.courseManual) { - mcdu.setIlsCourse(null); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } - } else if (input === 'F' || input === 'B') { - // change existing course between front course and back course - const mmr = mcdu.getMmrTuningData(1); - if (mmr.course === null) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - return; - } - const backcourse = input.charAt(0) === 'B'; - mcdu.setIlsCourse(mmr.course, backcourse); - } else if (input.match(/^[BF]?\d{1,3}$/) !== null) { - const backcourse = input.charAt(0) === 'B'; - const course = input.charAt(0) === 'F' || backcourse ? parseInt(input.slice(1)) : parseInt(input); - if (course < 0 || course > 360) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - return; - } - mcdu.setIlsCourse(course % 360, backcourse); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - mcdu.requestCall(() => { - CDUNavRadioPage.ShowPage(mcdu); - }); - } - - static showSlope(mcdu, mmr) { - const takeoff = mcdu.flightPhaseManager.phase <= FmgcFlightPhases.TAKEOFF; - const ilsAppr = mcdu.flightPlanService.active.approach && mcdu.flightPlanService.active.approach.type === 5; // ILS - return mmr.manual || (!takeoff && ilsAppr); - } - - static renderMmrCrs(mcdu) { - const mmr = mcdu.getMmrTuningData(1); - const showSlope = CDUNavRadioPage.showSlope(mcdu, mmr); - const slope = mmr.slope !== null ? `\xa0\xa0{small}{green}${mmr.slope.toFixed(1)}{end}{end}` : '{white}\xa0\xa0\xa0-.-{end}'; - if (mmr.course !== null) { - return `{${mmr.courseManual ? 'big' : 'small'}}{cyan}${mmr.backcourse ? 'B' : 'F'}${mmr.course.toFixed(0).padStart(3, "0")}{end}{end}${showSlope ? slope : ''}`; - } - if (mmr.frequency !== null) { - return `{amber}____{end}${slope}`; - } - return "{cyan}[\xa0\xa0]{end}"; - } - - static renderMmrCrsTitle(mcdu) { - const mmr = mcdu.getMmrTuningData(1); - const showSlope = CDUNavRadioPage.showSlope(mcdu, mmr); - if (showSlope && mmr.frequency !== null) { - return "CRS\xa0\xa0\xa0SLOPE"; - } - return "CRS"; - } - - static handleAdfLsk(mcdu, receiverIndex, input, scratchpadCallback) { - if (input === FMCMainDisplay.clrValue) { - const adf = mcdu.getAdfTuningData(receiverIndex); - if (adf.manual) { - mcdu.setManualAdf(receiverIndex, null); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } - } else if (input.match(/^\d{3,4}(\.\d)?$/) !== null) { - const freq = parseFloat(input); - // 190.0 - 1750.0 with some tolerance for FP precision - if (freq <= 189.95 || freq >= 1750.05) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - return false; - } - - mcdu.setManualAdf(receiverIndex, freq); - } else if (input.length >= 1 && input.length <= 4) { - // ident - mcdu.getOrSelectNDBsByIdent(input, (navaid) => { - if (navaid) { - if (mcdu.deselectedNavaids.find((databaseId) => databaseId === navaid.databaseId)) { - mcdu.setScratchpadMessage(NXSystemMessages.xxxIsDeselected.getModifiedMessage(navaid.ident)); - scratchpadCallback(); - return; - } - mcdu.setManualAdf(receiverIndex, navaid); - mcdu.requestCall(() => { - CDUNavRadioPage.ShowPage(mcdu); - }); - } else { - // FIXME new navaid page when it's built - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - scratchpadCallback(); - } - }); - return; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - mcdu.requestCall(() => { - CDUNavRadioPage.ShowPage(mcdu); - }); - } - - static renderAdf(mcdu, receiverIndex) { - const adf = mcdu.getAdfTuningData(receiverIndex); - let identText; - let freqText; - if (adf.frequency !== null) { - const ident = adf.ident !== null ? adf.ident : "[\xa0\xa0]"; - identText = `{${adf.manual ? 'big' : 'small'}}${receiverIndex === 2 ? ident.padEnd(4, '\xa0') : ident.padStart(4, '\xa0')}{end}`; - freqText = `{${adf.manual && adf.ident === null ? 'big' : 'small'}}${adf.frequency.toFixed(1)}{end}`; - } else { - identText = "[\xa0\xa0]"; - freqText = "[\xa0\xa0\xa0.]"; - } - - return `{cyan}${receiverIndex === 2 ? freqText : identText}{${adf.manual ? 'big' : 'small'}}/{end}${receiverIndex === 2 ? identText : freqText}{end}`; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_NavaidPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_NavaidPage.js deleted file mode 100644 index fadb20a4631..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_NavaidPage.js +++ /dev/null @@ -1,206 +0,0 @@ -const VorClass = Object.freeze({ - Unknown: 1, - Terminal: 2, - LowAlt: 4, - HighAlt: 8 -}); - -const VorType = Object.freeze({ - Unknown: 1, - Vor: 2, - VorDme: 4, - Dme: 8, - Tacan: 16, - Vortac: 32, - Vot: 64, - IlsDme: 128, - IlsTacan: 256 -}); - -const LsCategory = Object.freeze({ - None: 0, - LocOnly: 1, - Category1: 2, - Category2: 3, - Category3: 4, - IgsOnly: 5, - LdaGlideslope: 6, - LdaOnly: 7, - SdfGlideslope: 8, - SdfOnly: 9, -}); - -class CDUNavaidPage { - /** - * @param {A320_Neo_CDU_MainDisplay} mcdu MCDU - * @param {import('msfs-navdata').VhfNavaid | import('msfs-navdata').NdbNavaid | import('msfs-navdata').IlsNavaid | undefined} facility MSFS facility to show - * @param {any} returnPage Callback for the RETURN LSK... only for use by SELECTED NAVAIDS - */ - static ShowPage(mcdu, facility, returnPage) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.NavaidPage; - mcdu.returnPageCallback = () => { - CDUNavaidPage.ShowPage(mcdu); - }; - - const template = [ - ["NAVAID"], - ["\xa0IDENT"], - ["____[color]amber"], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""] - ]; - - if (facility) { - let latLon = facility.location; - if (facility.sectionCode === 4 && facility.subSectionCode === 7) { // airport && ils - CDUNavaidPage.renderIls(facility, template); - latLon = facility.locLocation; - } else if (facility.sectionCode === 1 && facility.subSectionCode === 0) { // navaid && vhf - CDUNavaidPage.renderVorDme(facility, template); - } - - template[2][0] = `{cyan}${facility.ident}{end}`; - - // 3L - template[3][0] = '\xa0\xa0\xa0\xa0LAT/LONG'; - template[4][0] = `{green}${CDUPilotsWaypoint.formatLatLong(latLon)}{end}`; - - // 4L - template[5][0] = '\xa0FREQ'; - template[6][0] = `{green}${CDUNavaidPage.formatFrequency(facility)}{end}`; - } - - if (returnPage !== undefined) { - template[12][1] = 'RETURN>'; - mcdu.onRightInput[5] = () => returnPage(); - } - - mcdu.setTemplate(template); - - mcdu.onLeftInput[0] = (value, scratchpadCallback) => { - mcdu.getOrSelectNavaidsByIdent(value, res => { - if (res) { - CDUNavaidPage.ShowPage(mcdu, res, returnPage); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - } - }); - }; - } - - /** - * - * @param {import('msfs-navdata').IlsNavaid} facility - */ - static renderIls(facility, template) { - let cat = 0; - switch (facility.category) { - case LsCategory.Category1: - cat = 1; - break; - case LsCategory.Category2: - cat = 2; - break; - case LsCategory.Category3: - cat = 3; - break; - } - - // 1R - template[1][1] = 'RWY IDENT'; - template[2][1] = `{green}${facility.runwayIdent}{end}`; - - // 2L - template[3][0] = 'CLASS'; - // FIXME should show "NONCOLLOCATED" for ILS without DME - template[4][0] = '{green}ILSDME{end}'; - - // 2R - if (cat > 0) { - template[3][1] = 'CATEGORY'; - template[4][1] = `{green}${cat}{end}`; - } - - // 3R - template[5][1] = 'COURSE'; - template[6][1] = `{green}${facility.locBearing.toFixed(0).padStart(3, '0')}{end}`; - - // 4R - if (facility.gsSlope) { - template[7][1] = 'SLOPE'; - template[8][1] = `{green}${facility.gsSlope.toFixed(1)}{end}`; - } - } - - /** - * - * @param {import('msfs-navdata').VhfNavaid} facility - */ - static renderVorDme(facility, template) { - const isTrueRef = facility.stationDeclination < 1e-6 && Math.abs(facility.location.lat) > 63; - const suffix = isTrueRef ? 'T' : (facility.stationDeclination < 0 ? 'W' : 'E'); - - // 1R - template[1][1] = 'STATION DEC'; - template[2][1] = `{green}${Math.abs(facility.stationDeclination).toFixed(0).padStart(3, '0')}${suffix}{end}`; - - // 2L - template[3][0] = 'CLASS'; - template[4][0] = facility.type === VorType.VorDme || facility.type === VorType.Vortac ? '{green}VORTAC{end}' : '{green}NONCOLLOCATED{end}'; - - // 5L - if (facility.dmeLocation && facility.dmeLocation.alt !== undefined) { - template[9][0] = 'ELV'; - template[10][0] = `{green}${(10 * Math.round(facility.dmeLocation.alt / 10)).toFixed(0)}{end}`; - } - - // 6L - const fom = CDUNavaidPage.formatFigureOfMerit(facility); - if (fom) { - template[11][0] = '\xa0FIG OF MERIT'; - template[12][0] = `{green}${fom}{end}`; - } - } - - /** - * @param {import('msfs-navdata').NdbNavaid | import('msfs-navdata').VhfNavaid} facility Navaid - * @returns {string} formatted frequency - */ - static formatFrequency(facility) { - if (facility.subSectionCode === 1) { - return facility.frequency.toFixed(0); - } - return facility.frequency.toFixed(2); - } - - /** - * Format the figure of merit if possible - * @param {import('msfs-navdata').NdbNavaid | import('msfs-navdata').VhfNavaid} facility Navaid - * @returns {string} formatted FoM or blank - */ - static formatFigureOfMerit(facility) { - if (facility.subSectionCode === 0 /* VhfNavaid */ && facility.type === 8 /* Dme */ || facility.type === 2 /* Vor */ || facility.type === 4 /* VorDme */ || facility.type === 32 /* Vortac */) { - switch (facility.class) { - case 8 /* HighAlt */: - return '3'; - case 1 /* Unknown */: - return '2'; - case 4 /* LowAlt */: - return '1'; - case 2 /* Terminal */: - return '0'; - } - } - return ''; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_NewWaypoint.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_NewWaypoint.js deleted file mode 100644 index 890725478c9..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_NewWaypoint.js +++ /dev/null @@ -1,236 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUNewWaypoint { - /** - * Callback when a new waypoint has been created, or aborted - * @name NewWaypointDoneCallback - * @function - * @param {PilotWaypoint | undefined | null} waypoint the resultant new waypoint, or undefined if aborted - */ - /** - * New Waypoint Page - * @param {NewWaypointDoneCallback} doneCallback callback when the user is finished with the page - * @param {any} _inProgressData private data used by the page - */ - static ShowPage(mcdu, doneCallback = undefined, _inProgressData = {}) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.NewWaypoint; - mcdu.returnPageCallback = () => { - CDUNewWaypoint.ShowPage(mcdu, doneCallback, _inProgressData); - }; - - const template = [ - ["NEW WAYPOINT"], - ["IDENT"], - [_inProgressData.ident !== undefined ? `{cyan}${_inProgressData.ident}{end}` : "_______[color]amber"], - ["LAT/LONG"], - ["____.__|_____.__[color]amber"], - ["PLACE/BRG /DIST"], - ["_______|___° |___. _[color]amber"], - ["PLACE-BRG /PLACE-BRG"], - ["{amber}_____-___° |_____-___°{end}"], - [""], - ["", "RETURN>"], - [""], - ["", _inProgressData.type !== undefined ? '{amber}STORE}{end}' : ''] - ]; - - switch (_inProgressData.type) { - case StoredWaypointType.LatLon: - template[4][0] = `{cyan}${CDUPilotsWaypoint.formatLatLong(_inProgressData.wp.location)}{end}`; - template[5].length = 0; - template[6].length = 0; - template[7].length = 0; - template[8].length = 0; - break; - case StoredWaypointType.Pbd: - template[4][0] = `{cyan}{small}${CDUPilotsWaypoint.formatLatLong(_inProgressData.wp.location)}{end}{end}`; - template[5][0] = 'PLACE\xa0\xa0/BRG\xa0/DIST'; - template[6][0] = `{cyan}${_inProgressData.place.ident.padEnd(7, '\xa0')}/${CDUPilotsWaypoint.formatBearing(_inProgressData.bearing)}/${_inProgressData.distance.toFixed(1)}{end}`; - template[7].length = 0; - template[8].length = 0; - break; - case StoredWaypointType.Pbx: - template[4][0] = `{cyan}{small}${CDUPilotsWaypoint.formatLatLong(_inProgressData.wp.location)}{end}{end}`; - template[5].length = 0; - template[6].length = 0; - template[7][0] = 'PLACE-BRG\xa0\xa0/PLACE-BRG'; - template[8][0] = `{cyan}${_inProgressData.place1.ident.padEnd(5, '\xa0')}-${CDUPilotsWaypoint.formatBearing(_inProgressData.bearing1)}/${_inProgressData.place2.ident.padEnd(5, '\xa0')}-${CDUPilotsWaypoint.formatBearing(_inProgressData.bearing2)}{end}`; - break; - default: - } - - mcdu.setTemplate(template); - - // ident - mcdu.onLeftInput[0] = (value, scratchpadCallback) => { - if (/^[A-Z0-9]{2,7}$/.test(value)) { - if (_inProgressData === undefined) { - _inProgressData = {}; - } - _inProgressData.ident = value; - mcdu.requestCall(() => CDUNewWaypoint.ShowPage(mcdu, doneCallback, _inProgressData)); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - } - }; - - // lat/lon - mcdu.onLeftInput[1] = (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - if (_inProgressData.type === StoredWaypointType.LatLon) { - mcdu.requestCall(CDUNewWaypoint.ShowPage(mcdu, doneCallback, { ident: _inProgressData.ident })); - } else { - return scratchpadCallback(); - } - } - - if (_inProgressData.type !== undefined) { - return scratchpadCallback(); - } - - if (Fmgc.WaypointEntryUtils.isLatLonFormat(value)) { - try { - const coordinates = Fmgc.WaypointEntryUtils.parseLatLon(value); - mcdu.requestCall(() => CDUNewWaypoint.ShowPage(mcdu, doneCallback, { - ident: _inProgressData.ident, - type: StoredWaypointType.LatLon, - wp: mcdu.dataManager.createLatLonWaypoint(coordinates, false, _inProgressData.ident).waypoint, - coordinates, - })); - } catch (err) { - if (err instanceof McduMessage) { - mcdu.setScratchpadMessage(err); - } else { - console.error(err); - } - scratchpadCallback(); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - } - }; - - // place/bearing/dist - mcdu.onLeftInput[2] = (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - if (_inProgressData.type === StoredWaypointType.Pbd) { - mcdu.requestCall(CDUNewWaypoint.ShowPage(mcdu, doneCallback, { ident: _inProgressData.ident })); - } else { - return scratchpadCallback(); - } - } - - if (_inProgressData.type !== undefined) { - return scratchpadCallback(); - } - - if (Fmgc.WaypointEntryUtils.isPbdFormat(value)) { - try { - Fmgc.WaypointEntryUtils.parsePbd(mcdu, value).then(([place, bearing, distance]) => { - mcdu.requestCall(() => CDUNewWaypoint.ShowPage(mcdu, doneCallback, { - ident: _inProgressData.ident, - type: StoredWaypointType.Pbd, - wp: mcdu.dataManager.createPlaceBearingDistWaypoint(place, bearing, distance, false, _inProgressData.ident).waypoint, - place, - bearing, - distance, - })); - }); - } catch (err) { - if (err instanceof McduMessage) { - mcdu.setScratchpadMessage(err); - } else { - console.error(err); - } - scratchpadCallback(); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - } - }; - - // place-bearing/place-bearing - mcdu.onLeftInput[3] = (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - if (_inProgressData.type === StoredWaypointType.Pbx) { - mcdu.requestCall(CDUNewWaypoint.ShowPage(mcdu, doneCallback, { ident: _inProgressData.ident })); - } else { - return scratchpadCallback(); - } - } - - if (_inProgressData.type !== undefined) { - return scratchpadCallback(); - } - - if (Fmgc.WaypointEntryUtils.isPbxFormat(value)) { - try { - Fmgc.WaypointEntryUtils.parsePbx(mcdu, value).then(([place1, bearing1, place2, bearing2]) => { - mcdu.requestCall(() => CDUNewWaypoint.ShowPage(mcdu, doneCallback, { - ident: _inProgressData.ident, - type: StoredWaypointType.Pbx, - wp: mcdu.dataManager.createPlaceBearingPlaceBearingWaypoint(place1, bearing1, place2, bearing2, false, _inProgressData.ident).waypoint, - place1, - bearing1, - place2, - bearing2, - })); - }); - } catch (err) { - if (err instanceof McduMessage) { - mcdu.setScratchpadMessage(err); - } else { - console.error(err); - } - scratchpadCallback(); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - } - }; - - if (_inProgressData !== undefined) { - mcdu.onRightInput[5] = () => { - let stored; - switch (_inProgressData.type) { - case StoredWaypointType.LatLon: - stored = mcdu.dataManager.createLatLonWaypoint(_inProgressData.coordinates, true, _inProgressData.ident); - break; - case StoredWaypointType.Pbd: - stored = mcdu.dataManager.createPlaceBearingDistWaypoint(_inProgressData.place, _inProgressData.bearing, _inProgressData.distance, true, _inProgressData.ident); - break; - case StoredWaypointType.Pbx: - stored = mcdu.dataManager.createPlaceBearingPlaceBearingWaypoint(_inProgressData.place1, _inProgressData.bearing1, _inProgressData.place2, _inProgressData.bearing2, true, _inProgressData.ident); - break; - default: - mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - return; - } - mcdu.requestCall(() => { - if (doneCallback !== undefined) { - doneCallback(stored.waypoint); - } else { - CDUPilotsWaypoint.ShowPage(mcdu, stored.storedIndex); - } - }); - }; - } - - mcdu.onRightInput[4] = () => { - mcdu.requestCall(() => { - if (doneCallback !== undefined) { - doneCallback(); - } else { - CDUPilotsWaypoint.ShowPage(mcdu); - } - }); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PerformancePage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PerformancePage.js deleted file mode 100644 index 1a56eb23447..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PerformancePage.js +++ /dev/null @@ -1,1271 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUPerformancePage { - static ShowPage(mcdu, _phase = undefined) { - mcdu.activeSystem = 'FMGC'; - - switch (_phase || mcdu.flightPhaseManager.phase) { - case FmgcFlightPhases.PREFLIGHT: CDUPerformancePage.ShowTAKEOFFPage(mcdu); break; - case FmgcFlightPhases.TAKEOFF: CDUPerformancePage.ShowTAKEOFFPage(mcdu); break; - case FmgcFlightPhases.CLIMB: CDUPerformancePage.ShowCLBPage(mcdu); break; - case FmgcFlightPhases.CRUISE: CDUPerformancePage.ShowCRZPage(mcdu); break; - case FmgcFlightPhases.DESCENT: CDUPerformancePage.ShowDESPage(mcdu); break; - case FmgcFlightPhases.APPROACH: CDUPerformancePage.ShowAPPRPage(mcdu); break; - case FmgcFlightPhases.GOAROUND: CDUPerformancePage.ShowGOAROUNDPage(mcdu); break; - } - } - static ShowTAKEOFFPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.PerformancePageTakeoff; - CDUPerformancePage._timer = 0; - CDUPerformancePage._lastPhase = mcdu.flightPhaseManager.phase; - mcdu.pageUpdate = () => { - CDUPerformancePage._timer++; - if (CDUPerformancePage._timer >= 50) { - if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - CDUPerformancePage.ShowPage(mcdu); - } - } - }; - - // TODO SEC F-PLN - const targetPlan = mcdu.flightPlanService.active; - - let titleColor = "white"; - if (mcdu.flightPhaseManager.phase === FmgcFlightPhases.TAKEOFF) { - titleColor = "green"; - } - - // check if we even have an airport - const hasOrigin = !!targetPlan.originAirport; - - // runway - let runway = ""; - let hasRunway = false; - if (hasOrigin) { - const runwayObj = targetPlan.originRunway; - - if (runwayObj) { - runway = Fmgc.RunwayUtils.runwayString(runwayObj.ident); - hasRunway = true; - } - } - - // v speeds - let v1 = "---"; - let vR = "---"; - let v2 = "---"; - let v1Check = "{small}\xa0\xa0\xa0{end}"; - let vRCheck = "{small}\xa0\xa0\xa0{end}"; - let v2Check = "{small}\xa0\xa0\xa0{end}"; - if (mcdu.flightPhaseManager.phase < FmgcFlightPhases.TAKEOFF) { - v1 = "{amber}___{end}"; - if (mcdu.unconfirmedV1Speed) { - v1Check = `{small}{cyan}${("" + mcdu.unconfirmedV1Speed).padEnd(3)}{end}{end}`; - } else if (mcdu.v1Speed) { - v1 = `{cyan}${("" + mcdu.v1Speed).padEnd(3)}{end}`; - } - mcdu.onLeftInput[0] = (value, scratchpadCallback) => { - if (value === "") { - if (mcdu.unconfirmedV1Speed) { - mcdu.v1Speed = mcdu.unconfirmedV1Speed; - mcdu.unconfirmedV1Speed = undefined; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - } - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - if (mcdu.trySetV1Speed(value)) { - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - scratchpadCallback(); - } - } - }; - vR = "{amber}___{end}"; - if (mcdu.unconfirmedVRSpeed) { - vRCheck = `{small}{cyan}${("" + mcdu.unconfirmedVRSpeed).padEnd(3)}{end}{end}`; - } else if (mcdu.vRSpeed) { - vR = `{cyan}${("" + mcdu.vRSpeed).padEnd(3)}{end}`; - } - mcdu.onLeftInput[1] = (value, scratchpadCallback) => { - if (value === "") { - if (mcdu.unconfirmedVRSpeed) { - mcdu.vRSpeed = mcdu.unconfirmedVRSpeed; - mcdu.unconfirmedVRSpeed = undefined; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - } - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - if (mcdu.trySetVRSpeed(value)) { - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - scratchpadCallback(); - } - } - }; - v2 = "{amber}___{end}"; - if (mcdu.unconfirmedV2Speed) { - v2Check = `{small}{cyan}${("" + mcdu.unconfirmedV2Speed).padEnd(3)}{end}{end}`; - } else if (mcdu.v2Speed) { - v2 = `{cyan}${("" + mcdu.v2Speed).padEnd(3)}{end}`; - } - mcdu.onLeftInput[2] = (value, scratchpadCallback) => { - if (value === "") { - if (mcdu.unconfirmedV2Speed) { - mcdu.v2Speed = mcdu.unconfirmedV2Speed; - mcdu.unconfirmedV2Speed = undefined; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - } - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - if (mcdu.trySetV2Speed(value)) { - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - scratchpadCallback(); - } - } - }; - } else { - v1 = "\xa0\xa0\xa0"; - vR = "\xa0\xa0\xa0"; - v2 = "\xa0\xa0\xa0"; - if (mcdu.v1Speed) { - v1 = `{green}${("" + mcdu.v1Speed).padEnd(3)}{end}`; - } - if (mcdu.vRSpeed) { - vR = `{green}${("" + mcdu.vRSpeed).padEnd(3)}{end}`; - } - if (mcdu.v2Speed) { - v2 = `{green}${("" + mcdu.v2Speed).padEnd(3)}{end}`; - } - mcdu.onLeftInput[0] = (value, scratchpadCallback) => { - if (value !== "") { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - } - }; - mcdu.onLeftInput[1] = (value, scratchpadCallback) => { - if (value !== "") { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - } - }; - mcdu.onLeftInput[2] = (value, scratchpadCallback) => { - if (value !== "") { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - } - }; - } - - // transition altitude - remains editable during take off - let transAltCell = ""; - if (hasOrigin) { - transAltCell = "[\xa0".padEnd(4, "\xa0") + "]"; - - const transAlt = targetPlan.performanceData.transitionAltitude; - const transAltitudeIsFromDatabase = targetPlan.performanceData.transitionAltitudeIsFromDatabase; - - if (transAlt !== null) { - transAltCell = `{cyan}${transAlt}{end}`; - if (transAltitudeIsFromDatabase) { - transAltCell += "[s-text]"; - } - } - - mcdu.onLeftInput[3] = (value, scratchpadCallback) => { - if (mcdu.trySetTakeOffTransAltitude(value)) { - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - scratchpadCallback(); - } - }; - } - - // thrust reduction / acceleration altitude - const altitudeColour = hasOrigin ? (mcdu.flightPhaseManager.phase >= FmgcFlightPhases.TAKEOFF ? "green" : "cyan") : "white"; - - const plan = mcdu.flightPlanService.active; - const thrRed = plan.performanceData.thrustReductionAltitude; - const thrRedPilot = plan.performanceData.thrustReductionAltitudeIsPilotEntered; - const acc = plan.performanceData.accelerationAltitude; - const accPilot = plan.performanceData.accelerationAltitudeIsPilotEntered; - const eoAcc = plan.performanceData.engineOutAccelerationAltitude; - const eoAccPilot = plan.performanceData.engineOutAccelerationAltitudeIsPilotEntered; - - const thrRedAcc = `{${thrRedPilot ? 'big' : 'small'}}${thrRed !== null ? thrRed.toFixed(0).padStart(5, '\xa0') : '-----'}{end}/{${accPilot ? 'big' : 'small'}}${acc !== null ? acc.toFixed(0).padEnd(5, '\xa0') : '-----'}{end}`; - - mcdu.onLeftInput[4] = (value, scratchpadCallback) => { - if (mcdu.trySetThrustReductionAccelerationAltitude(value)) { - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - // eng out acceleration altitude - const engOut = `{${eoAccPilot ? 'big' : 'small'}}${eoAcc !== null ? eoAcc.toFixed(0).padStart(5, '\xa0') : '-----'}{end}`; - mcdu.onRightInput[4] = (value, scratchpadCallback) => { - if (mcdu.trySetEngineOutAcceleration(value)) { - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - // center column - let flpRetrCell = "---"; - let sltRetrCell = "---"; - let cleanCell = "---"; - if (isFinite(mcdu.zeroFuelWeight)) { - const flapSpeed = mcdu.computedVfs; - if (flapSpeed !== 0) { - flpRetrCell = `{green}${flapSpeed.toFixed(0)}{end}`; - } - const slatSpeed = mcdu.computedVss; - if (slatSpeed !== 0) { - sltRetrCell = `{green}${slatSpeed.toFixed(0)}{end}`; - } - const cleanSpeed = mcdu.computedVgd; - if (cleanSpeed !== 0) { - cleanCell = `{green}${cleanSpeed.toFixed(0)}{end}`; - } - } - // takeoff shift - let toShiftCell = "{inop}----{end}\xa0"; - if (hasOrigin && hasRunway) { - toShiftCell = "{inop}{small}[M]{end}[\xa0\xa0]*{end}"; - // TODO store and show TO SHIFT - } - - // flaps / trim horizontal stabilizer - let flapsThs = "[]/[\xa0\xa0\xa0][color]cyan"; - // The following line uses a special Javascript concept that is signed - // zeroes. In Javascript -0 is strictly equal to 0, so for most cases we - // don't care about that difference. But here, we use that fact to show - // the pilot the precise value they entered: DN0.0 or UP0.0. The only - // way to figure that difference out is using Object.is, as - // Object.is(+0, -0) returns false. Alternatively we could use a helper - // variable (yuck) or encode it using a very small, but negative value - // such as -0.001. - const formattedThs = mcdu.ths !== null - ? (mcdu.ths >= 0 && !Object.is(mcdu.ths, -0) ? `UP${Math.abs(mcdu.ths).toFixed(1)}` : `DN${Math.abs(mcdu.ths).toFixed(1)}`) - : ''; - if (mcdu.flightPhaseManager.phase < FmgcFlightPhases.TAKEOFF) { - const flaps = mcdu.flaps !== null ? mcdu.flaps : "[]"; - const ths = formattedThs ? formattedThs : "[\xa0\xa0\xa0]"; - flapsThs = `${flaps}/${ths}[color]cyan`; - mcdu.onRightInput[2] = (value, scratchpadCallback) => { - if (mcdu.trySetFlapsTHS(value)) { - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - scratchpadCallback(); - } - }; - } else { - const flaps = mcdu.flaps !== null ? mcdu.flaps : ""; - const ths = formattedThs ? formattedThs : "\xa0\xa0\xa0\xa0\xa0"; - flapsThs = `${flaps}/${ths}[color]green`; - } - - // flex takeoff temperature - let flexTakeOffTempCell = "[\xa0\xa0]°[color]cyan"; - if (mcdu.flightPhaseManager.phase < FmgcFlightPhases.TAKEOFF) { - if (isFinite(mcdu.perfTOTemp)) { - if (mcdu._toFlexChecked) { - flexTakeOffTempCell = `${mcdu.perfTOTemp.toFixed(0)}°[color]cyan`; - } else { - flexTakeOffTempCell = `{small}${mcdu.perfTOTemp.toFixed(0)}{end}${flexTakeOffTempCell}[color]cyan`; - } - } - mcdu.onRightInput[3] = (value, scratchpadCallback) => { - if (mcdu._toFlexChecked) { - if (mcdu.setPerfTOFlexTemp(value)) { - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - scratchpadCallback(); - } - } else { - if (value === "" || mcdu.setPerfTOFlexTemp(value)) { - mcdu._toFlexChecked = true; - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - } else { - scratchpadCallback(); - } - } - }; - } else { - if (isFinite(mcdu.perfTOTemp)) { - flexTakeOffTempCell = `${mcdu.perfTOTemp.toFixed(0)}°[color]green`; - } else { - flexTakeOffTempCell = ""; - } - } - - let next = "NEXT\xa0"; - let nextPhase = "PHASE>"; - if ((mcdu.unconfirmedV1Speed || mcdu.unconfirmedVRSpeed || mcdu.unconfirmedV2Speed || !mcdu._toFlexChecked) && mcdu.flightPhaseManager.phase < FmgcFlightPhases.TAKEOFF) { - next = "CONFIRM\xa0"; - nextPhase = "TO DATA*"; - mcdu.onRightInput[5] = (value) => { - mcdu.v1Speed = mcdu.unconfirmedV1Speed ? mcdu.unconfirmedV1Speed : mcdu.v1Speed; - mcdu.vRSpeed = mcdu.unconfirmedVRSpeed ? mcdu.unconfirmedVRSpeed : mcdu.vRSpeed; - mcdu.v2Speed = mcdu.unconfirmedV2Speed ? mcdu.unconfirmedV2Speed : mcdu.v2Speed; - mcdu.unconfirmedV1Speed = undefined; - mcdu.unconfirmedVRSpeed = undefined; - mcdu.unconfirmedV2Speed = undefined; - mcdu._toFlexChecked = true; - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - }; - } else { - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = (value) => { - CDUPerformancePage.ShowCLBPage(mcdu); - }; - } - - mcdu.setTemplate([ - ["TAKE OFF RWY\xa0{green}" + runway.padStart(3, "\xa0") + "{end}[color]" + titleColor], - ["\xa0V1\xa0\xa0FLP RETR", ""], - [v1 + v1Check + "\xa0F=" + flpRetrCell, ""], - ["\xa0VR\xa0\xa0SLT RETR", "TO SHIFT\xa0"], - [vR + vRCheck + "\xa0S=" + sltRetrCell, toShiftCell], - ["\xa0V2\xa0\xa0\xa0\xa0\xa0CLEAN", "FLAPS/THS"], - [v2 + v2Check + "\xa0O=" + cleanCell, flapsThs], - ["TRANS ALT", "FLEX TO TEMP"], - [`{cyan}${transAltCell}{end}`, flexTakeOffTempCell], - ["THR\xa0RED/ACC", "ENG\xa0OUT\xa0ACC"], - [`{${altitudeColour}}${thrRedAcc}{end}`, `{${altitudeColour}}${engOut}{end}`], - ["\xa0UPLINK[color]inop", next], - [" { - CDUPerformancePage._timer++; - if (CDUPerformancePage._timer >= 100) { - if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { - CDUPerformancePage.ShowCLBPage(mcdu); - } else { - CDUPerformancePage.ShowPage(mcdu); - } - } - }; - - const hasFromToPair = mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport; // TODO use the right flight plan - const showManagedSpeed = hasFromToPair && mcdu.isCostIndexSet && Number.isFinite(mcdu.costIndex); - const isPhaseActive = mcdu.flightPhaseManager.phase === FmgcFlightPhases.CLIMB; - const isTakeoffOrClimbActive = isPhaseActive || (mcdu.flightPhaseManager.phase === FmgcFlightPhases.TAKEOFF); - const titleColor = isPhaseActive ? "green" : "white"; - const isSelected = (isPhaseActive && Simplane.getAutoPilotAirspeedSelected()) || (!isPhaseActive && mcdu.preSelectedClbSpeed !== undefined); - const actModeCell = isSelected ? "SELECTED" : "MANAGED"; - const costIndexCell = CDUPerformancePage.formatCostIndexCell(mcdu, hasFromToPair, true); - const canClickManagedSpeed = showManagedSpeed && mcdu.preSelectedClbSpeed !== undefined && !isPhaseActive; - - // Predictions to altitude - const vnavDriver = mcdu.guidanceController.vnavDriver; - - const cruiseAltitude = mcdu.cruiseLevel * 100; - const fcuAltitude = SimVar.GetSimVarValue("AUTOPILOT ALTITUDE LOCK VAR:3", "feet"); - const altitudeToPredict = mcdu.perfClbPredToAltitudePilot !== undefined ? mcdu.perfClbPredToAltitudePilot : Math.min(cruiseAltitude, fcuAltitude); - - const predToLabel = isTakeoffOrClimbActive ? "\xa0\xa0\xa0\xa0\xa0{small}PRED TO{end}" : ""; - const predToCell = isTakeoffOrClimbActive ? `${CDUPerformancePage.formatAltitudeOrLevel(altitudeToPredict, mcdu.getOriginTransitionAltitude())}[color]cyan` : ""; - - let predToDistanceCell = ""; - let predToTimeCell = ""; - - let expeditePredToDistanceCell = ""; - let expeditePredToTimeCell = ""; - - if (isTakeoffOrClimbActive && vnavDriver) { - [predToDistanceCell, predToTimeCell] = CDUPerformancePage.getTimeAndDistancePredictionsFromGeometryProfile(vnavDriver.ndProfile, altitudeToPredict, true); - [expeditePredToDistanceCell, expeditePredToTimeCell] = CDUPerformancePage.getTimeAndDistancePredictionsFromGeometryProfile(vnavDriver.expediteProfile, altitudeToPredict, true, true); - } - - let managedSpeedCell = ''; - if (isPhaseActive) { - if (mcdu.managedSpeedTarget === mcdu.managedSpeedClimb) { - managedSpeedCell = `\xa0${mcdu.managedSpeedClimb.toFixed(0)}/${mcdu.managedSpeedClimbMach.toFixed(2).replace('0.', '.')}`; - } else if (mcdu.managedSpeedTargetIsMach) { - managedSpeedCell = `\xa0${mcdu.managedSpeedClimbMach.toFixed(2).replace('0.', '.')}`; - } else { - managedSpeedCell = `\xa0${mcdu.managedSpeedTarget.toFixed(0)}`; - } - } else { - let climbSpeed = Math.min(mcdu.managedSpeedClimb, mcdu.getNavModeSpeedConstraint()); - if (mcdu.climbSpeedLimit !== undefined && SimVar.GetSimVarValue("INDICATED ALTITUDE", "feet") < mcdu.climbSpeedLimitAlt) { - climbSpeed = Math.min(climbSpeed, mcdu.climbSpeedLimit); - } - - managedSpeedCell = `${canClickManagedSpeed ? '*' : '\xa0'}${climbSpeed.toFixed(0)}`; - - mcdu.onLeftInput[3] = (value, scratchpadCallback) => { - if (mcdu.trySetPreSelectedClimbSpeed(value)) { - CDUPerformancePage.ShowCLBPage(mcdu); - } else { - scratchpadCallback(); - } - }; - } - const [selectedSpeedTitle, selectedSpeedCell] = CDUPerformancePage.getClbSelectedTitleAndValue(mcdu, isPhaseActive, isSelected, mcdu.preSelectedClbSpeed); - - if (hasFromToPair) { - mcdu.onLeftInput[1] = (value, scratchpadCallback) => { - if (mcdu.tryUpdateCostIndex(value)) { - CDUPerformancePage.ShowCLBPage(mcdu); - } else { - scratchpadCallback(); - } - }; - } - - if (canClickManagedSpeed) { - mcdu.onLeftInput[2] = (_, scratchpadCallback) => { - if (mcdu.trySetPreSelectedClimbSpeed(FMCMainDisplay.clrValue)) { - CDUPerformancePage.ShowCLBPage(mcdu); - } - - scratchpadCallback(); - }; - } - - if (isTakeoffOrClimbActive) { - mcdu.onRightInput[1] = (value, scratchpadCallback) => { - if (mcdu.trySetPerfClbPredToAltitude(value)) { - CDUPerformancePage.ShowCLBPage(mcdu); - } else { - scratchpadCallback(); - } - }; - } - - const [toUtcLabel, toDistLabel] = isTakeoffOrClimbActive ? ["\xa0UTC", "DIST"] : ["", ""]; - - const bottomRowLabels = ['\xa0PREV', 'NEXT\xa0']; - const bottomRowCells = ['']; - mcdu.leftInputDelay[5] = () => mcdu.getDelaySwitchPage(); - if (isPhaseActive) { - if (confirmAppr) { - bottomRowLabels[0] = '\xa0CONFIRM[color]amber'; - bottomRowCells[0] = '*APPR PHASE[color]amber'; - mcdu.onLeftInput[5] = async () => { - if (mcdu.flightPhaseManager.tryGoInApproachPhase()) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } - }; - } else { - bottomRowLabels[0] = '\xa0ACTIVATE[color]cyan'; - bottomRowCells[0] = '{APPR PHASE[color]cyan'; - mcdu.onLeftInput[5] = () => { - CDUPerformancePage.ShowCLBPage(mcdu, true); - }; - } - } else { - mcdu.onLeftInput[5] = () => { - CDUPerformancePage.ShowTAKEOFFPage(mcdu); - }; - } - - mcdu.rightInputDelay[5] = () => mcdu.getDelaySwitchPage(); - mcdu.onRightInput[5] = () => { - CDUPerformancePage.ShowCRZPage(mcdu); - }; - mcdu.setTemplate([ - [`\xa0CLB[color]${titleColor}`], - ["ACT MODE"], - [`${actModeCell}[color]green`], - ["CI"], - [costIndexCell, predToCell, predToLabel], - ["MANAGED", toDistLabel, toUtcLabel], - [`{small}${showManagedSpeed ? managedSpeedCell : "\xa0---/---"}{end}[color]${showManagedSpeed ? "green" : "white"}`, !isSelected ? predToDistanceCell : "", !isSelected ? predToTimeCell : ""], - [selectedSpeedTitle], - [selectedSpeedCell, isSelected ? predToDistanceCell : "", isSelected ? predToTimeCell : ""], - [""], - isPhaseActive ? ["{small}EXPEDITE{end}[color]green", expeditePredToDistanceCell, expeditePredToTimeCell] : [""], - bottomRowLabels, - bottomRowCells, - ]); - } - - static ShowCRZPage(mcdu, confirmAppr = false) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.PerformancePageCrz; - CDUPerformancePage._timer = 0; - CDUPerformancePage._lastPhase = mcdu.flightPhaseManager.phase; - mcdu.pageUpdate = () => { - CDUPerformancePage._timer++; - if (CDUPerformancePage._timer >= 100) { - if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { - CDUPerformancePage.ShowCRZPage(mcdu); - } else { - CDUPerformancePage.ShowPage(mcdu); - } - } - }; - - const hasFromToPair = mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport; // TODO use the right flight plan - const isPhaseActive = mcdu.flightPhaseManager.phase === FmgcFlightPhases.CRUISE; - const titleColor = isPhaseActive ? "green" : "white"; - const isSelected = (isPhaseActive && Simplane.getAutoPilotAirspeedSelected()) || (!isPhaseActive && mcdu.preSelectedCrzSpeed !== undefined); - const isFlying = mcdu.flightPhaseManager.phase >= FmgcFlightPhases.TAKEOFF; - const actModeCell = isSelected ? "SELECTED" : "MANAGED"; - const costIndexCell = CDUPerformancePage.formatCostIndexCell(mcdu, hasFromToPair, true); - - // TODO: Figure out correct condition - const showManagedSpeed = hasFromToPair && mcdu.isCostIndexSet && Number.isFinite(mcdu.costIndex); - const canClickManagedSpeed = showManagedSpeed && mcdu.preSelectedCrzSpeed !== undefined && !isPhaseActive; - let managedSpeedCell = "{small}\xa0---/---{end}[color]white"; - if (showManagedSpeed && mcdu.cruiseLevel && Number.isFinite(mcdu.managedSpeedCruise) && Number.isFinite(mcdu.managedSpeedCruiseMach)) { - const shouldShowCruiseMach = mcdu.cruiseLevel > 250; - managedSpeedCell = `{small}${canClickManagedSpeed ? "*" : "\xa0"}${shouldShowCruiseMach ? mcdu.managedSpeedCruiseMach.toFixed(2).replace("0.", ".") : mcdu.managedSpeedCruise.toFixed(0)}{end}[color]green`; - } - const preselTitle = isPhaseActive ? "" : "PRESEL"; - let preselCell = ""; - if (!isPhaseActive) { - const hasPreselectedSpeedOrMach = mcdu.preSelectedCrzSpeed !== undefined; - if (hasPreselectedSpeedOrMach) { - preselCell = `\xa0${mcdu.preSelectedCrzSpeed < 1 ? mcdu.preSelectedCrzSpeed.toFixed(2).replace("0.", ".") : mcdu.preSelectedCrzSpeed.toFixed(0)}[color]cyan`; - } else { - preselCell = "{small}*{end}[ ][color]cyan"; - } - } - - if (hasFromToPair) { - mcdu.onLeftInput[1] = (value, scratchpadCallback) => { - if (mcdu.tryUpdateCostIndex(value)) { - CDUPerformancePage.ShowCRZPage(mcdu); - } else { - scratchpadCallback(); - } - }; - } - - const timeLabel = isFlying ? '\xa0UTC' : 'TIME'; - - const [destEfobCell, destTimeCell] = CDUPerformancePage.formatDestEfobAndTime(mcdu, isFlying); - const [toUtcLabel, toDistLabel] = isFlying ? ["\xa0UTC", "DIST"] : ["", ""]; - const [toReasonCell, toDistCell, toTimeCell] = isFlying ? CDUPerformancePage.formatToReasonDistanceAndTime(mcdu) : ["", "", ""]; - const desCabinRateCell = "{small}-350{end}"; - const shouldShowStepAltsOption = mcdu.cruiseLevel - && (mcdu.flightPhaseManager.phase < FmgcFlightPhases.DESCENT || mcdu.flightPhaseManager.phase > FmgcFlightPhases.GOAROUND); - - const bottomRowLabels = ["\xa0PREV", "NEXT\xa0"]; - const bottomRowCells = [""]; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - if (isPhaseActive) { - if (confirmAppr) { - bottomRowLabels[0] = "\xa0CONFIRM[color]amber"; - bottomRowCells[0] = "*APPR PHASE[color]amber"; - mcdu.onLeftInput[5] = async () => { - if (mcdu.flightPhaseManager.tryGoInApproachPhase()) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } - }; - } else { - bottomRowLabels[0] = "\xa0ACTIVATE[color]cyan"; - bottomRowCells[0] = "{APPR PHASE[color]cyan"; - mcdu.onLeftInput[5] = () => { - CDUPerformancePage.ShowCRZPage(mcdu, true); - }; - } - } else { - mcdu.onLeftInput[3] = (value, scratchpadCallback) => { - if (mcdu.trySetPreSelectedCruiseSpeed(value)) { - CDUPerformancePage.ShowCRZPage(mcdu); - } else { - scratchpadCallback(); - } - }; - mcdu.onLeftInput[5] = () => { - CDUPerformancePage.ShowCLBPage(mcdu); - }; - } - if (canClickManagedSpeed) { - mcdu.onLeftInput[2] = (_, scratchpadCallback) => { - if (mcdu.trySetPreSelectedCruiseSpeed(FMCMainDisplay.clrValue)) { - CDUPerformancePage.ShowCRZPage(mcdu); - } - - scratchpadCallback(); - }; - } - mcdu.onRightInput[3] = () => { - // DES CABIN RATE - mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - }; - if (shouldShowStepAltsOption) { - CDUStepAltsPage.Return = () => { - CDUPerformancePage.ShowCRZPage(mcdu, false); - }; - mcdu.onRightInput[4] = () => { - CDUStepAltsPage.ShowPage(mcdu); - }; - } - mcdu.rightInputDelay[5] = () => mcdu.getDelaySwitchPage(); - mcdu.onRightInput[5] = () => { - CDUPerformancePage.ShowDESPage(mcdu); - }; - mcdu.setTemplate([ - [`\xa0CRZ[color]${titleColor}`], - ["ACT MODE", "DEST EFOB", timeLabel], - [`${actModeCell}[color]green`, destEfobCell, destTimeCell], - ["CI"], - [costIndexCell, toReasonCell], - ["MANAGED", toDistLabel, toUtcLabel], - [managedSpeedCell, toDistCell, toTimeCell], - [preselTitle, "DES CABIN RATE"], - [preselCell, `\xa0{cyan}${desCabinRateCell}{end}{white}{small}FT/MN{end}{end}`], - [""], - ["", shouldShowStepAltsOption ? "STEP ALTS>" : ""], - bottomRowLabels, - bottomRowCells - ]); - } - - static ShowDESPage(mcdu, confirmAppr = false) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.PerformancePageDes; - CDUPerformancePage._timer = 0; - CDUPerformancePage._lastPhase = mcdu.flightPhaseManager.phase; - mcdu.pageUpdate = () => { - CDUPerformancePage._timer++; - if (CDUPerformancePage._timer >= 100) { - if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { - CDUPerformancePage.ShowDESPage(mcdu); - } else { - CDUPerformancePage.ShowPage(mcdu); - } - } - }; - - const hasFromToPair = mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport; // TODO use the right flight plan - const isPhaseActive = mcdu.flightPhaseManager.phase === FmgcFlightPhases.DESCENT; - const titleColor = isPhaseActive ? "green" : "white"; - const isFlying = mcdu.flightPhaseManager.phase >= FmgcFlightPhases.TAKEOFF; - const isSelected = isPhaseActive && Simplane.getAutoPilotAirspeedSelected(); - const actModeCell = isSelected ? "SELECTED" : "MANAGED"; - - // Predictions to altitude - const vnavDriver = mcdu.guidanceController.vnavDriver; - const fcuAltitude = SimVar.GetSimVarValue("AUTOPILOT ALTITUDE LOCK VAR:3", "feet"); - const altitudeToPredict = mcdu.perfDesPredToAltitudePilot !== undefined ? mcdu.perfDesPredToAltitudePilot : fcuAltitude; - - const predToLabel = isPhaseActive ? "\xa0\xa0\xa0\xa0\xa0{small}PRED TO{end}" : ""; - const predToCell = isPhaseActive ? `${CDUPerformancePage.formatAltitudeOrLevel(altitudeToPredict, mcdu.getDestinationTransitionLevel() * 100)}[color]cyan` : ""; - - let predToDistanceCell = ""; - let predToTimeCell = ""; - - if (isPhaseActive && vnavDriver) { - [predToDistanceCell, predToTimeCell] = CDUPerformancePage.getTimeAndDistancePredictionsFromGeometryProfile(vnavDriver.ndProfile, altitudeToPredict, false); - } - - const costIndexCell = CDUPerformancePage.formatCostIndexCell(mcdu, hasFromToPair, !isPhaseActive); - - const econDesPilotEntered = mcdu.managedSpeedDescendPilot !== undefined; - const econDes = econDesPilotEntered ? mcdu.managedSpeedDescendPilot : mcdu.managedSpeedDescend; - const econDesMachPilotEntered = mcdu.managedSpeedDescendMachPilot !== undefined; - const econDesMach = econDesMachPilotEntered ? mcdu.managedSpeedDescendMachPilot : mcdu.managedSpeedDescendMach; - - // TODO: Figure out correct condition - const showManagedSpeed = hasFromToPair && mcdu.isCostIndexSet && Number.isFinite(mcdu.costIndex) && econDesMach !== undefined && econDes !== undefined; - const managedDescentSpeedCellMach = `{${econDesMachPilotEntered ? "big" : "small"}}${econDesMach.toFixed(2).replace("0.", ".")}{end}`; - const managedDescentSpeedCellSpeed = `{${econDesPilotEntered ? "big" : "small"}}/${econDes.toFixed(0)}{end}`; - - const managedDescentSpeedCell = showManagedSpeed - ? `\xa0${managedDescentSpeedCellMach}${managedDescentSpeedCellSpeed}[color]cyan` - : "\xa0{small}---/---{end}[color]white"; - - const [selectedSpeedTitle, selectedSpeedCell] = CDUPerformancePage.getDesSelectedTitleAndValue(mcdu, isPhaseActive, isSelected); - const timeLabel = isFlying ? "\xa0UTC" : "TIME"; - const [destEfobCell, destTimeCell] = CDUPerformancePage.formatDestEfobAndTime(mcdu, isFlying); - const [toUtcLabel, toDistLabel] = isPhaseActive ? ["\xa0UTC", "DIST"] : ["", ""]; - - const bottomRowLabels = ["\xa0PREV", "NEXT\xa0"]; - const bottomRowCells = [""]; - mcdu.leftInputDelay[5] = () => mcdu.getDelaySwitchPage(); - if (isPhaseActive) { - mcdu.onRightInput[1] = (value, scratchpadCallback) => { - if (mcdu.trySetPerfDesPredToAltitude(value)) { - CDUPerformancePage.ShowDESPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - if (confirmAppr) { - bottomRowLabels[0] = "\xa0CONFIRM[color]amber"; - bottomRowCells[0] = "*APPR PHASE[color]amber"; - mcdu.onLeftInput[5] = async () => { - if (mcdu.flightPhaseManager.tryGoInApproachPhase()) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } - }; - } else { - bottomRowLabels[0] = "\xa0ACTIVATE[color]cyan"; - bottomRowCells[0] = "{APPR PHASE[color]cyan"; - mcdu.onLeftInput[5] = () => { - CDUPerformancePage.ShowDESPage(mcdu, true); - }; - } - } else { - mcdu.onLeftInput[5] = () => { - CDUPerformancePage.ShowCRZPage(mcdu); - }; - } - // Can only modify cost index until the phase is active - if (hasFromToPair && !isPhaseActive) { - mcdu.onLeftInput[1] = (value, scratchpadCallback) => { - if (mcdu.tryUpdateCostIndex(value)) { - CDUPerformancePage.ShowDESPage(mcdu); - } else { - scratchpadCallback(); - } - }; - } - - if (showManagedSpeed) { - mcdu.onLeftInput[2] = (value, scratchpadCallback) => { - if (mcdu.trySetManagedDescentSpeed(value)) { - CDUPerformancePage.ShowDESPage(mcdu); - } else { - scratchpadCallback(); - } - }; - } - - mcdu.rightInputDelay[5] = () => mcdu.getDelaySwitchPage(); - mcdu.onRightInput[5] = () => { - CDUPerformancePage.ShowAPPRPage(mcdu); - }; - mcdu.setTemplate([ - [`\xa0DES[color]${titleColor}`], - ["ACT MODE", "DEST EFOB", timeLabel], - [`${actModeCell}[color]green`, destEfobCell, destTimeCell], - ["CI"], - [costIndexCell, predToCell, predToLabel], - ["MANAGED", toDistLabel, toUtcLabel], - [managedDescentSpeedCell, !isSelected ? predToDistanceCell : "", !isSelected ? predToTimeCell : ""], - [selectedSpeedTitle], - [selectedSpeedCell, isSelected ? predToDistanceCell : "", isSelected ? predToTimeCell : ""], - [""], - [""], - bottomRowLabels, - bottomRowCells, - ]); - } - - static ShowAPPRPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.PerformancePageAppr; - - const plan = mcdu.flightPlanService.active; - - CDUPerformancePage._timer = 0; - CDUPerformancePage._lastPhase = mcdu.flightPhaseManager.phase; - mcdu.pageUpdate = () => { - CDUPerformancePage._timer++; - if (CDUPerformancePage._timer >= 100) { - if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } - } - }; - - const distanceToDest = mcdu.getDistanceToDestination(); - const closeToDest = distanceToDest !== undefined && distanceToDest <= 180; - - let qnhCell = "[\xa0\xa0][color]cyan"; - if (isFinite(mcdu.perfApprQNH)) { - if (mcdu.perfApprQNH < 500) { - qnhCell = mcdu.perfApprQNH.toFixed(2) + "[color]cyan"; - } else { - qnhCell = mcdu.perfApprQNH.toFixed(0) + "[color]cyan"; - } - } else if (closeToDest) { - qnhCell = "____[color]amber"; - } - mcdu.onLeftInput[0] = (value, scratchpadCallback) => { - if (mcdu.setPerfApprQNH(value)) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - let tempCell = "{cyan}[\xa0]°{end}"; - if (isFinite(mcdu.perfApprTemp)) { - tempCell = "{cyan}" + (mcdu.perfApprTemp >= 0 ? "+" : "-") + ("" + Math.abs(mcdu.perfApprTemp).toFixed(0)).padStart(2).replace(/ /g, "\xa0") + "°{end}"; - } else if (closeToDest) { - tempCell = "{amber}___°{end}"; - } - mcdu.onLeftInput[1] = (value, scratchpadCallback) => { - if (mcdu.setPerfApprTemp(value)) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } else { - scratchpadCallback(); - } - }; - let magWindHeadingCell = "[\xa0]"; - if (isFinite(mcdu.perfApprWindHeading)) { - magWindHeadingCell = ("" + mcdu.perfApprWindHeading.toFixed(0)).padStart(3, 0); - } - let magWindSpeedCell = "[\xa0]"; - if (isFinite(mcdu.perfApprWindSpeed)) { - magWindSpeedCell = mcdu.perfApprWindSpeed.toFixed(0).padStart(3, "0"); - } - mcdu.onLeftInput[2] = (value, scratchpadCallback) => { - if (mcdu.setPerfApprWind(value)) { - mcdu.updateTowerHeadwind(); - mcdu.updatePerfSpeeds(); - CDUPerformancePage.ShowAPPRPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - let transAltCell = "\xa0".repeat(5); - const hasDestination = !!plan.destinationAirport; - - if (hasDestination) { - const transitionLevel = plan.performanceData.transitionLevel; - - if (transitionLevel !== null) { - transAltCell = (transitionLevel * 100).toFixed(0).padEnd(5, "\xa0"); - - if (plan.performanceData.transitionLevelIsFromDatabase) { - transAltCell = `{small}${transAltCell}{end}`; - } - } else { - transAltCell = "[\xa0]".padEnd(5, "\xa0"); - } - } - mcdu.onLeftInput[3] = (value, scratchpadCallback) => { - if (mcdu.setPerfApprTransAlt(value)) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - let vappCell = "---"; - let vlsCell = "---"; - let flpRetrCell = "---"; - let sltRetrCell = "---"; - let cleanCell = "---"; - if (isFinite(mcdu.zeroFuelWeight) && mcdu.approachSpeeds && mcdu.approachSpeeds.valid) { - vappCell = `{cyan}{small}${mcdu.approachSpeeds.vapp.toFixed(0)}{end}{end}`; - vlsCell = `{green}${mcdu.approachSpeeds.vls.toFixed(0)}{end}`; - flpRetrCell = `{green}${mcdu.approachSpeeds.f.toFixed(0)}{end}`; - sltRetrCell = `{green}${mcdu.approachSpeeds.s.toFixed(0)}{end}`; - cleanCell = `{green}${mcdu.approachSpeeds.gd.toFixed(0)}{end}`; - } - if (isFinite(mcdu.vApp)) { // pilot override - vappCell = `{cyan}${mcdu.vApp.toFixed(0).padStart(3, "\xa0")}{end}`; - } - mcdu.onLeftInput[4] = (value, scratchpadCallback) => { - if (mcdu.setPerfApprVApp(value)) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } else { - scratchpadCallback(); - } - }; - mcdu.onRightInput[4] = () => { - mcdu.setPerfApprFlaps3(!mcdu.perfApprFlaps3); - mcdu.updatePerfSpeeds(); - CDUPerformancePage.ShowAPPRPage(mcdu); - }; - - let baroCell = "[\xa0\xa0\xa0]"; - if (mcdu.perfApprMDA !== null) { - baroCell = mcdu.perfApprMDA.toFixed(0); - } - mcdu.onRightInput[1] = (value, scratchpadCallback) => { - if (mcdu.setPerfApprMDA(value) && mcdu.setPerfApprDH(FMCMainDisplay.clrValue)) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - const approach = plan.approach; - const isILS = approach && approach.type === 5; - let radioLabel = ""; - let radioCell = ""; - if (isILS) { - radioLabel = "RADIO"; - if (typeof mcdu.perfApprDH === 'number') { - radioCell = mcdu.perfApprDH.toFixed(0); - } else if (mcdu.perfApprDH === "NO DH") { - radioCell = "NO DH"; - } else { - radioCell = "[\xa0]"; - } - mcdu.onRightInput[2] = (value, scratchpadCallback) => { - if (mcdu.setPerfApprDH(value) && mcdu.setPerfApprMDA(FMCMainDisplay.clrValue)) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } else { - scratchpadCallback(); - } - }; - } - - const bottomRowLabels = ["\xa0PREV", "NEXT\xa0"]; - const bottomRowCells = [""]; - let titleColor = "white"; - if (mcdu.flightPhaseManager.phase === FmgcFlightPhases.APPROACH) { - bottomRowLabels[0] = ""; - bottomRowCells[0] = ""; - titleColor = "green"; - } else { - if (mcdu.flightPhaseManager.phase === FmgcFlightPhases.GOAROUND) { - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = (value) => { - CDUPerformancePage.ShowGOAROUNDPage(mcdu); - }; - } else { - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = (value) => { - CDUPerformancePage.ShowDESPage(mcdu); - }; - } - } - if (mcdu.flightPhaseManager.phase === FmgcFlightPhases.GOAROUND) { - bottomRowLabels[1] = ""; - bottomRowCells[1] = ""; - } else { - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = (value) => { - CDUPerformancePage.ShowGOAROUNDPage(mcdu); - }; - } - - let titleCell = `${"\xa0".repeat(5)}{${titleColor}}APPR{end}\xa0`; - if (approach) { - const approachName = Fmgc.ApproachUtils.shortApproachName(approach); - titleCell += `{green}${approachName}{end}` + "\xa0".repeat(24 - 10 - approachName.length); - } else { - titleCell += "\xa0".repeat(24 - 10); - } - - mcdu.setTemplate([ - /* t */[titleCell], - /* 1l */["QNH"], - /* 1L */[qnhCell], - /* 2l */["TEMP", "BARO"], - /* 2L */[`${tempCell}${"\xa0".repeat(6)}O=${cleanCell}`, baroCell + "[color]cyan"], - /* 3l */["MAG WIND", radioLabel], - /* 3L */[`{cyan}${magWindHeadingCell}°/${magWindSpeedCell}{end}\xa0\xa0S=${sltRetrCell}`, radioCell + "[color]cyan"], - /* 4l */["TRANS ALT"], - /* 4L */[`{cyan}${transAltCell}{end}${"\xa0".repeat(5)}F=${flpRetrCell}`], - /* 5l */["VAPP\xa0\xa0\xa0VLS", "LDG CONF\xa0"], - /* 5L */[`${vappCell}${"\xa0".repeat(4)}${vlsCell}`, mcdu.perfApprFlaps3 ? "{cyan}CONF3/{end}{small}FULL{end}*" : "{cyan}FULL/{end}{small}CONF3{end}*"], - /* 6l */bottomRowLabels, - /* 6L */bottomRowCells, - ]); - } - - static ShowGOAROUNDPage(mcdu, confirmAppr = false) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.PerformancePageGoAround; - CDUPerformancePage._timer = 0; - CDUPerformancePage._lastPhase = mcdu.flightPhaseManager.phase; - mcdu.pageUpdate = () => { - CDUPerformancePage._timer++; - if (CDUPerformancePage._timer >= 100) { - if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { - CDUPerformancePage.ShowGOAROUNDPage(mcdu); - } else { - CDUPerformancePage.ShowPage(mcdu); - } - } - }; - - const haveDestination = mcdu.flightPlanService.active.destinationAirport !== undefined; - - const titleColor = mcdu.flightPhaseManager.phase === FmgcFlightPhases.GOAROUND ? "green" : "white"; - const altitudeColour = haveDestination ? (mcdu.flightPhaseManager.phase >= FmgcFlightPhases.GOAROUND ? "green" : "cyan") : "white"; - - const plan = mcdu.flightPlanService.active; - const thrRed = plan.performanceData.missedThrustReductionAltitude; - const thrRedPilot = plan.performanceData.missedThrustReductionAltitudeIsPilotEntered; - const acc = plan.performanceData.missedAccelerationAltitude; - const accPilot = plan.performanceData.missedAccelerationAltitudeIsPilotEntered; - const eoAcc = plan.performanceData.missedEngineOutAccelerationAltitude; - const eoAccPilot = plan.performanceData.missedEngineOutAccelerationAltitudeIsPilotEntered; - - const thrRedAcc = `{${thrRedPilot ? 'big' : 'small'}}${thrRed !== null ? thrRed.toFixed(0).padStart(5, '\xa0') : '-----'}{end}/{${accPilot ? 'big' : 'small'}}${acc !== null ? acc.toFixed(0).padEnd(5, '\xa0') : '-----'}{end}`; - const engOut = `{${eoAccPilot ? 'big' : 'small'}}${eoAcc !== null ? eoAcc.toFixed(0).padStart(5, '\xa0') : '-----'}{end}`; - - mcdu.onLeftInput[4] = (value, scratchpadCallback) => { - if (mcdu.trySetThrustReductionAccelerationAltitudeGoaround(value)) { - CDUPerformancePage.ShowGOAROUNDPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - mcdu.onRightInput[4] = (value, scratchpadCallback) => { - if (mcdu.trySetEngineOutAccelerationAltitudeGoaround(value)) { - CDUPerformancePage.ShowGOAROUNDPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - let flpRetrCell = "---"; - let sltRetrCell = "---"; - let cleanCell = "---"; - if (isFinite(mcdu.zeroFuelWeight)) { - const flapSpeed = mcdu.computedVfs; - if (isFinite(flapSpeed)) { - flpRetrCell = `{green}${flapSpeed.toFixed(0).padEnd(3, '\xa0')}{end}`; - } - const slatSpeed = mcdu.computedVss; - if (isFinite(slatSpeed)) { - sltRetrCell = `{green}${slatSpeed.toFixed(0).padEnd(3, '\xa0')}{end}`; - } - const cleanSpeed = mcdu.computedVgd; - if (isFinite(cleanSpeed)) { - cleanCell = `{green}${cleanSpeed.toFixed(0).padEnd(3, '\xa0')}{end}`; - } - } - - const bottomRowLabels = ["\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0", "\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"]; - const bottomRowCells = ["\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0", "\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"]; - if (mcdu.flightPhaseManager.phase === FmgcFlightPhases.GOAROUND) { - if (confirmAppr) { - bottomRowLabels[0] = "\xa0{amber}CONFIRM{amber}\xa0\xa0\xa0\xa0"; - bottomRowCells[0] = "{amber}*APPR\xa0PHASE{end}\xa0"; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = async () => { - if (mcdu.flightPhaseManager.tryGoInApproachPhase()) { - CDUPerformancePage.ShowAPPRPage(mcdu); - } - }; - } else { - bottomRowLabels[0] = "\xa0{cyan}ACTIVATE{end}\xa0\xa0\xa0"; - bottomRowCells[0] = "{cyan}{APPR\xa0PHASE{end}\xa0"; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUPerformancePage.ShowGOAROUNDPage(mcdu, true); - }; - } - bottomRowLabels[1] = "\xa0\xa0\xa0\xa0\xa0\xa0\xa0{white}NEXT{end}\xa0"; - bottomRowCells[1] = "\xa0\xa0\xa0\xa0\xa0\xa0{white}PHASE>{end}"; - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - CDUPerformancePage.ShowAPPRPage(mcdu); - }; - } else { - bottomRowLabels[0] = "\xa0{white}PREV{end}\xa0\xa0\xa0\xa0\xa0\xa0\xa0"; - bottomRowCells[0] = "{white} { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUPerformancePage.ShowAPPRPage(mcdu); - }; - } - - mcdu.setTemplate([ - [`{${titleColor}}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0GO\xa0AROUND\xa0\xa0\xa0\xa0\xa0\xa0{end}`], - ["", "", "\xa0\xa0\xa0\xa0\xa0FLP\xa0RETR\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"], - ["", "", `\xa0\xa0\xa0\xa0\xa0\xa0\xa0F=${flpRetrCell}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0`], - ["", "", "\xa0\xa0\xa0\xa0\xa0SLT RETR\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"], - ["", "", `\xa0\xa0\xa0\xa0\xa0\xa0\xa0S=${sltRetrCell}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0`], - ["", "", "\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0CLEAN\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"], - ["", "", `\xa0\xa0\xa0\xa0\xa0\xa0\xa0O=${cleanCell}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0`], - [""], - [""], - ["", "", "THR\xa0RED/ACC\xa0\xa0ENG\xa0OUT\xa0ACC"], - ["", "", `{${altitudeColour}}${thrRedAcc}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0${engOut}{end}`], - ["", "", bottomRowLabels.join("")], - ["", "", bottomRowCells.join("")], - ]); - } - - static getClbSelectedTitleAndValue(mcdu, isPhaseActive, isSelected, preSel) { - if (!isPhaseActive) { - return ["PRESEL", (isFinite(preSel) ? "\xa0" + preSel : "*[ ]") + "[color]cyan"]; - } - - if (!isSelected) { - return ["", ""]; - } - - const aircraftAltitude = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); - const selectedSpdMach = SimVar.GetSimVarValue('L:A32NX_AUTOPILOT_SPEED_SELECTED', 'number'); - - if (selectedSpdMach < 1) { - return ["SELECTED", `\xa0${selectedSpdMach.toFixed(2).replace('0.', '.')}[color]green`]; - } else { - const machAtManualCrossoverAlt = mcdu.casToMachManualCrossoverCurve.evaluate(selectedSpdMach); - const manualCrossoverAltitude = mcdu.computeManualCrossoverAltitude(machAtManualCrossoverAlt); - const shouldShowMach = aircraftAltitude < manualCrossoverAltitude && (!mcdu.cruiseLevel || manualCrossoverAltitude < mcdu.cruiseLevel * 100); - - return ["SELECTED", `\xa0${Math.round(selectedSpdMach)}${shouldShowMach ? ("{small}/" + machAtManualCrossoverAlt.toFixed(2).replace('0.', '.') + "{end}") : ""}[color]green`]; - } - } - - static getDesSelectedTitleAndValue(mcdu, isPhaseActive, isSelected) { - if (!isPhaseActive || !isSelected) { - return ["", ""]; - } - - const aircraftAltitude = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); - const selectedSpdMach = SimVar.GetSimVarValue('L:A32NX_AUTOPILOT_SPEED_SELECTED', 'number'); - - if (selectedSpdMach < 1) { - const casAtCrossoverAltitude = mcdu.machToCasManualCrossoverCurve.evaluate(selectedSpdMach); - const manualCrossoverAltitude = mcdu.computeManualCrossoverAltitude(selectedSpdMach); - const shouldShowCas = aircraftAltitude > manualCrossoverAltitude; - - return ["SELECTED", `\xa0${shouldShowCas ? "{small}" + Math.round(casAtCrossoverAltitude) + "/{end}" : ""}${selectedSpdMach.toFixed(2).replace('0.', '.')}[color]green`]; - } else { - return ["SELECTED", `\xa0${Math.round(selectedSpdMach)}[color]green`]; - } - } - - static formatAltitudeOrLevel(altitudeToFormat, transitionAltitude) { - if (transitionAltitude >= 100 && altitudeToFormat > transitionAltitude) { - return `FL${(altitudeToFormat / 100).toFixed(0).toString().padStart(3,"0")}`; - } - - return (10 * Math.round(altitudeToFormat / 10)).toFixed(0).toString().padStart(5,"\xa0"); - } - - static getTimeAndDistancePredictionsFromGeometryProfile(geometryProfile, altitudeToPredict, isClimbVsDescent, printSmall = false) { - let predToDistanceCell = "---"; - let predToTimeCell = "----"; - - if (!geometryProfile || !geometryProfile.isReadyToDisplay) { - return [predToTimeCell, predToDistanceCell]; - } - - const predictions = isClimbVsDescent - ? geometryProfile.computeClimbPredictionToAltitude(altitudeToPredict) - : geometryProfile.computeDescentPredictionToAltitude(altitudeToPredict); - - if (predictions) { - if (Number.isFinite(predictions.distanceFromStart)) { - if (printSmall) { - predToDistanceCell = "{small}" + predictions.distanceFromStart.toFixed(0) + "{end}[color]green"; - } else { - predToDistanceCell = predictions.distanceFromStart.toFixed(0) + "[color]green"; - } - } - - if (Number.isFinite(predictions.secondsFromPresent)) { - const utcTime = SimVar.GetGlobalVarValue("ZULU TIME", "seconds"); - const predToTimeCellText = FMCMainDisplay.secondsToUTC(utcTime + predictions.secondsFromPresent); - - if (printSmall) { - predToTimeCell = "{small}" + predToTimeCellText + "{end}[color]green"; - } else { - predToTimeCell = predToTimeCellText + "[color]green"; - } - } - } - - return [predToDistanceCell, predToTimeCell]; - } - - static formatDestEfobAndTime(mcdu, isFlying) { - const destinationPrediction = mcdu.guidanceController.vnavDriver.getDestinationPrediction(); - - let destEfobCell = "---.-"; - let destTimeCell = "----"; - - if (destinationPrediction) { - if (Number.isFinite(destinationPrediction.estimatedFuelOnBoard)) { - destEfobCell = (NXUnits.poundsToUser(destinationPrediction.estimatedFuelOnBoard) / 1000).toFixed(1) + "[color]green"; - } - - if (Number.isFinite(destinationPrediction.secondsFromPresent)) { - const utcTime = SimVar.GetGlobalVarValue("ZULU TIME", "seconds"); - - const predToTimeCellText = isFlying - ? FMCMainDisplay.secondsToUTC(utcTime + destinationPrediction.secondsFromPresent) - : FMCMainDisplay.secondsTohhmm(destinationPrediction.secondsFromPresent); - - destTimeCell = predToTimeCellText + "[color]green"; - } - } - - return [destEfobCell, destTimeCell]; - } - - static formatToReasonDistanceAndTime(mcdu) { - const toPrediction = mcdu.guidanceController.vnavDriver.getPerfCrzToPrediction(); - - let reasonCell = "(T/D)"; - let distCell = "---"; - let timeCell = "----"; - - if (toPrediction) { - if (Number.isFinite(toPrediction.distanceFromPresentPosition)) { - distCell = Math.round(toPrediction.distanceFromPresentPosition) + "[color]green"; - } - - if (Number.isFinite(toPrediction.secondsFromPresent)) { - const utcTime = SimVar.GetGlobalVarValue("ZULU TIME", "seconds"); - - timeCell = FMCMainDisplay.secondsToUTC(utcTime + toPrediction.secondsFromPresent) + "[color]green"; - } - - if (toPrediction.reason === "StepClimb") { - reasonCell = "(S/C)"; - } else if (toPrediction.reason === "StepDescent") { - reasonCell = "(S/D)"; - } - } - - return ["{small}TO{end}\xa0{green}" + reasonCell + "{end}", distCell, timeCell]; - } - - static formatCostIndexCell(mcdu, hasFromToPair, allowModification) { - let costIndexCell = "---"; - if (hasFromToPair) { - if (mcdu.isCostIndexSet && Number.isFinite(mcdu.costIndex)) { - costIndexCell = `${mcdu.costIndex.toFixed(0)}[color]${allowModification ? "cyan" : "green"}`; - } else { - costIndexCell = "___[color]amber"; - } - } - - return costIndexCell; - } -} -CDUPerformancePage._timer = 0; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PilotsWaypoint.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PilotsWaypoint.js deleted file mode 100644 index 241349e4665..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PilotsWaypoint.js +++ /dev/null @@ -1,120 +0,0 @@ -class CDUPilotsWaypoint { - static ShowPage(mcdu, index = 0, confirmDeleteAll = false) { - if (mcdu.dataManager.numberOfStoredWaypoints() < 1) { - return CDUNewWaypoint.ShowPage(mcdu, () => CDUDataIndexPage.ShowPage2(mcdu)); - } - if (mcdu.dataManager.storedWaypoints[index] === undefined) { - index = mcdu.dataManager.prevStoredWaypointIndex(index); - } - const number = mcdu.dataManager.storedWaypointNumber(index); - - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.PilotsWaypoint; - - const template = [ - [`STORED WAYPOINT\xa0{small}${number}/99{end}\xa0`], - ['\xa0IDENT'], - [''], - ['\xa0\xa0\xa0\xa0LAT/LONG'], - [''], - [''], - [''], - [''], - [''], - ['', 'NEW\xa0'], - ['', 'WAYPOINT>'], - ['', confirmDeleteAll ? '{amber}CONFIRM\xa0{end}' : ''], - ['', `{${confirmDeleteAll ? 'amber' : 'cyan'}}DELETE ALL${confirmDeleteAll ? '*' : '}'}{end}`] - ]; - - const storedWp = mcdu.dataManager.storedWaypoints[index]; - if (storedWp !== undefined) { - template[2][0] = `{green}${storedWp.waypoint.ident}{end}`; - - switch (storedWp.type) { - case StoredWaypointType.LatLon: - template[4][0] = `{green}${CDUPilotsWaypoint.formatLatLong(storedWp.waypoint.location)}{end}`; - break; - case StoredWaypointType.Pbd: - template[4][0] = `{green}{small}${CDUPilotsWaypoint.formatLatLong(storedWp.waypoint.location)}{end}{end}`; - template[5][0] = 'PLACE\xa0\xa0/BRG\xa0/DIST'; - template[6][0] = `{green}${storedWp.pbdPlace.padEnd(7, '\xa0')}/${CDUPilotsWaypoint.formatBearing(storedWp.pbdBearing)}/${storedWp.pbdDistance.toFixed(1)}{end}`; - break; - case StoredWaypointType.Pbx: - template[4][0] = `{green}{small}${CDUPilotsWaypoint.formatLatLong(storedWp.waypoint.location)}{end}{end}`; - template[7][0] = 'PLACE-BRG\xa0\xa0/PLACE-BRG'; - template[8][0] = `{green}${storedWp.pbxPlace1.substr(0, 5).padStart(5, '\xa0')}-${CDUPilotsWaypoint.formatBearing(storedWp.pbxBearing1)}\xa0/${storedWp.pbxPlace2.substr(0, 5).padStart(5, '\xa0')}-${CDUPilotsWaypoint.formatBearing(storedWp.pbxBearing2)}{end}`; - break; - default: - } - } - - mcdu.setTemplate(template); - - // delete the waypoint on ident LSK - mcdu.onLeftInput[0] = (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - mcdu.dataManager.deleteStoredWaypoint(index).then((deleted) => { - if (!deleted) { - mcdu.setScratchpadMessage(NXSystemMessages.fplnElementRetained); - } else if (mcdu.dataManager.numberOfStoredWaypoints() < 1) { - CDUNewWaypoint.ShowPage(mcdu, () => CDUDataIndexPage.ShowPage2(mcdu)); - } else { - CDUPilotsWaypoint.ShowPage(mcdu, mcdu.dataManager.nextStoredWaypointIndex()); - } - }); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - } - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onRightInput[4] = () => { - CDUNewWaypoint.ShowPage(mcdu); - }; - - // DELETE ALL - mcdu.onRightInput[5] = () => { - if (confirmDeleteAll) { - mcdu.dataManager.deleteAllStoredWaypoints().then((allDeleted) => { - if (!allDeleted) { - mcdu.setScratchpadMessage(NXSystemMessages.fplnElementRetained); - } - - CDUPilotsWaypoint.ShowPage(mcdu, index); - }); - } else { - CDUPilotsWaypoint.ShowPage(mcdu, index, true); - } - }; - - const canScroll = mcdu.dataManager.numberOfStoredWaypoints() > 1; - mcdu.setArrows(false, false, canScroll, canScroll); - if (canScroll) { - mcdu.onPrevPage = () => { - CDUPilotsWaypoint.ShowPage(mcdu, mcdu.dataManager.prevStoredWaypointIndex(index)); - }; - mcdu.onNextPage = () => { - CDUPilotsWaypoint.ShowPage(mcdu, mcdu.dataManager.nextStoredWaypointIndex(index)); - }; - } - } - - static formatAngle(angle, digits) { - const mins = (Math.abs(angle) % 1) * 60; - return `${Math.abs(Math.trunc(angle)).toFixed(0).padStart(digits, '0')}${Math.trunc(mins).toFixed(0).padStart(2, '0')}.${((mins % 1) * 10).toFixed(0)}`; - } - - // TODO is this already existing? - static formatLatLong(coordinates) { - return `${CDUPilotsWaypoint.formatAngle(coordinates.lat, 2)}${coordinates.lat < 0 ? 'S' : 'N'}/${CDUPilotsWaypoint.formatAngle(coordinates.long, 3)}${coordinates.long < 0 ? 'W' : 'E'}`; - } - - static formatBearing(bearing) { - return `${bearing.toFixed(0).padStart(3, '0')}°`; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PositionFrozen.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PositionFrozen.js deleted file mode 100644 index d4f0d1c7c67..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PositionFrozen.js +++ /dev/null @@ -1,33 +0,0 @@ -class CDUPosFrozen { - static ShowPage(mcdu, currPos) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.PosFrozen; - const UTC_SECONDS = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - const hours = Math.floor(UTC_SECONDS / 3600) || 0; - const minutes = Math.floor(UTC_SECONDS % 3600 / 60) || 0; - const hhmm = `${hours.toString().padStart(2, "0") || "00"}${minutes.toString().padStart(2, "0") || "00"}`; - mcdu.setTemplate([ - [`POSITION FROZEN AT ${hhmm}`], - [""], - ["{small}FMS1{end}", `${currPos}[color]green`], - ["\xa0\xa0\xa0\xa0\xa0\xa03IRS/GPS"], - ["{small}FMS2{end}", `${currPos}[color]green`], - ["\xa0\xa0\xa0\xa0\xa0\xa03IRS/GPS"], - ["{small}GPIRS{end}", `${currPos}[color]green`], - [""], - ["{small}MIX IRS{end}", `${currPos}[color]green`], - ["\xa0\xa0IRS1", "IRS3\xa0", "\xa0IRS2"], - ["{small}NAV 0.0{end}[color]green", "{small}NAV 0.0{end}[color]green", "{small}NAV 0.0{end}[color]green"], - ["", "SEL\xa0"], - ["{UNFREEZE[color]cyan", "NAVAIDS>"] - ]); - - mcdu.onLeftInput[5] = () => { - CDUPositionMonitorPage.ShowPage(mcdu); - }; - - mcdu.onRightInput[5] = () => { - CDUSelectedNavaids.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PositionMonitorPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PositionMonitorPage.js deleted file mode 100644 index a0c818a0f76..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_PositionMonitorPage.js +++ /dev/null @@ -1,54 +0,0 @@ -class CDUPositionMonitorPage { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.PositionMonitorPage; - - let currPos = new LatLong(SimVar.GetSimVarValue("GPS POSITION LAT", "degree latitude"), - SimVar.GetSimVarValue("GPS POSITION LON", "degree longitude")).toShortDegreeString(); - if (currPos.includes("N")) { - var currPosSplit = currPos.split("N"); - var sep = "N/"; - } else { - var currPosSplit = currPos.split("S"); - var sep = "S/"; - } - const latStr = currPosSplit[0]; - const lonStr = currPosSplit[1]; - currPos = latStr + sep + lonStr; - - mcdu.setTemplate([ - ["POSITION MONITOR"], - [""], - ["{small}FMS1{end}", currPos + "[color]green"], - ["\xa0\xa0\xa0\xa0\xa0\xa03IRS/GPS"], - ["{small}FMS2{end}", currPos + "[color]green"], - ["\xa0\xa0\xa0\xa0\xa0\xa03IRS/GPS"], - ["{small}GPIRS{end}", currPos + "[color]green"], - [""], - ["{small}MIX IRS{end}", currPos + "[color]green"], - ["\xa0\xa0IRS1", "IRS3\xa0", "\xa0IRS2"], - ["{small}NAV 0.0{end}[color]green", "{small}NAV 0.0{end}[color]green", "{small}NAV 0.0{end}[color]green"], - ["", "SEL\xa0"], - ["{FREEZE[color]cyan", "NAVAIDS>"] - ]); - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onRightInput[5] = () => { - CDUSelectedNavaids.ShowPage(mcdu); - }; - - mcdu.onLeftInput[5] = () => { - CDUPosFrozen.ShowPage(mcdu, currPos); - }; - - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.PositionMonitorPage) { - CDUPositionMonitorPage.ShowPage(mcdu); - } - }, mcdu.PageTimeout.Default); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_ProgressPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_ProgressPage.js deleted file mode 100644 index 941b9067ba0..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_ProgressPage.js +++ /dev/null @@ -1,298 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUProgressPage { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ProgressPage; - mcdu.returnPageCallback = () => { - CDUProgressPage.ShowPage(mcdu); - }; - mcdu.activeSystem = 'FMGC'; - const flightNo = SimVar.GetSimVarValue("ATC FLIGHT NUMBER", "string"); - const flMax = mcdu.getMaxFlCorrected(); - const flOpt = (mcdu._zeroFuelWeightZFWCGEntered && mcdu._blockFuelEntered && (mcdu.isAllEngineOn() || mcdu.isOnGround())) ? "{green}FL" + (Math.floor(flMax / 5) * 5).toString() + "{end}" : "-----"; - const adirsUsesGpsAsPrimary = SimVar.GetSimVarValue("L:A32NX_ADIRS_USES_GPS_AS_PRIMARY", "Bool"); - const gpsPrimaryStatus = adirsUsesGpsAsPrimary ? "{green}GPS PRIMARY{end}" : ""; - let flCrz = "-----"; - let vDevCell = ""; - switch (mcdu.flightPhaseManager.phase) { - case FmgcFlightPhases.PREFLIGHT: - case FmgcFlightPhases.TAKEOFF: { - if (mcdu.cruiseLevel) { - flCrz = "FL" + mcdu.cruiseLevel.toFixed(0).padStart(3, "0") + "[color]cyan"; - } - break; - } - case FmgcFlightPhases.CLIMB: { - const alt = Math.round(Simplane.getAutoPilotSelectedAltitudeLockValue("feet") / 100); - const altCtn = Math.round(mcdu.constraintAlt / 100); - if (!mcdu.cruiseLevel && !mcdu._activeCruiseFlightLevelDefaulToFcu) { - flCrz = "FL" + (altCtn && alt > altCtn ? altCtn.toFixed(0).padStart(3, "0") : alt.toFixed(0).padStart(3, "0")) + "[color]cyan"; - } else { - flCrz = "FL" + mcdu.cruiseLevel.toFixed(0).padStart(3, "0") + "[color]cyan"; - } - break; - } - case FmgcFlightPhases.CRUISE: { - // TODO check if this is correct - // We can get here by taking off without FROM/TO entered, and climbing to the FCU altitude (which will then be used as cruise altitude) - // to enter the cruise phase. We then enter a new FROM/TO which resets the cruise altitude, but I don't know if it puts us in the CLB phase - // or keeps us in CRZ. - if (mcdu.cruiseLevel) { - flCrz = "FL" + mcdu.cruiseLevel.toFixed(0).padStart(3, "0") + "[color]cyan"; - } - break; - } - case FmgcFlightPhases.DESCENT: { - const vDev = mcdu.guidanceController.vnavDriver.getLinearDeviation(); - let vDevFormattedNumber = "{small}-----{end}"; - - if (vDev && isFinite(vDev)) { - const paddedVdev = (10 * Math.round(vDev / 10)).toFixed(0).padStart(4, "\xa0"); - const vDevSign = vDev > 0 ? "+" : " "; - const extraSpace = paddedVdev.length > 4 ? "" : "\xa0"; - - vDevFormattedNumber = "{green}" + extraSpace + vDevSign + paddedVdev + "{end}"; - } - - vDevCell = "{small}VDEV={end}" + vDevFormattedNumber + "{small}FT{end}"; - } - } - let flightPhase; - switch (mcdu.flightPhaseManager.phase) { - case FmgcFlightPhases.PREFLIGHT: - case FmgcFlightPhases.TAKEOFF: - flightPhase = "TO"; - break; - case FmgcFlightPhases.CLIMB: - flightPhase = "CLB"; - break; - case FmgcFlightPhases.CRUISE: - flightPhase = "CRZ"; - break; - case FmgcFlightPhases.DESCENT: - flightPhase = "DES"; - break; - case FmgcFlightPhases.APPROACH: - flightPhase = "APPR"; - break; - case FmgcFlightPhases.GOAROUND: - flightPhase = "GA"; - break; - default: - flightPhase = ""; - break; - } - - mcdu.onLeftInput[0] = (value, scratchpadCallback) => { - if (mcdu.trySetCruiseFlCheckInput(value)) { - CDUProgressPage.ShowPage(mcdu); - } else { - scratchpadCallback(); - } - }; - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = () => { - CDUProgressPage.ShowReportPage(mcdu); - }; - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUProgressPage.ShowPredictiveGPSPage(mcdu); - }; - - let progBearingDist = "{small}\xa0---°\xa0/----.-{end}"; - let progWaypoint = "[\xa0\xa0\xa0\xa0\xa0]"; - if (mcdu.progWaypointIdent !== undefined) { - progWaypoint = mcdu.progWaypointIdent.padEnd("7", "\xa0"); - if (mcdu.progBearing > 0 && mcdu.progDistance > 0) { - const distDigits = mcdu.progDistance > 9999 ? 0 : 1; - progBearingDist = `{small}{green}\xa0${mcdu.progBearing.toFixed(0).padStart(3, "0")}°\xa0/${mcdu.progDistance.toFixed(distDigits).padStart(3)}{end}{end}`; - } - } - // the actual query takes long enough... - mcdu.rightInputDelay[3] = () => 0; - mcdu.onRightInput[3] = (input, scratchpadCallback) => { - mcdu.trySetProgWaypoint(input, (success) => { - if (!success) { - scratchpadCallback(input); - } - - CDUProgressPage.ShowPage(mcdu); - }); - }; - - let rnpCell = '-.-'; - const rnpSize = mcdu.navigation.requiredPerformance.manualRnp ? 'big' : 'small'; - const rnp = mcdu.navigation.requiredPerformance.activeRnp; - // TODO check 2 decimal cut-off - if (rnp > 1) { - rnpCell = rnp.toFixed(1).padStart(4); - } else if (rnp !== undefined) { - rnpCell = rnp.toFixed(2); - } - - mcdu.onLeftInput[5] = (input, scratchpadCallback) => { - if (input === FMCMainDisplay.clrValue) { - mcdu.navigation.requiredPerformance.clearPilotRnp(); - return CDUProgressPage.ShowPage(mcdu); - } - - const match = input.match(/^\d{1,2}(\.\d{1,2})?$/); - if (match === null) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(input); - return; - } - - const rnp = parseFloat(input); - if (rnp < 0.01 || rnp > 20) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(input); - return; - } - - mcdu.navigation.requiredPerformance.setPilotRnp(rnp); - CDUProgressPage.ShowPage(mcdu); - }; - - let anpCell = '-.-'; - const anp = mcdu.navigation.currentPerformance; - // TODO check 2 decimal cut-off - if (anp > 1) { - anpCell = anp.toFixed(1).padStart(4); - } else if (anp !== undefined) { - anpCell = anp.toFixed(2); - } - - mcdu.setTemplate([ - ["{green}" + flightPhase.padStart(15, "\xa0") + "{end}\xa0" + flightNo.padEnd(11, "\xa0")], - ["\xa0" + "CRZ\xa0", "OPT\xa0\xa0\xa0\xa0REC MAX"], - [flCrz, flOpt + "\xa0\xa0\xa0\xa0" + "{magenta}FL" + flMax.toString() + "\xa0{end}"], - [""], - [" { - if (mcdu.page.Current === mcdu.page.ProgressPage) { - CDUProgressPage.ShowPage(mcdu); - } - }, mcdu.PageTimeout.Default); - - } - - static ShowReportPage(mcdu) { - const plan = mcdu.flightPlanService.active; - - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ProgressPageReport; - let altCell = "---"; - if (isFinite(mcdu.cruiseLevel)) { - altCell = mcdu.cruiseLevel.toFixed(0); - } - mcdu.onRightInput[0] = (value, scratchpadCallback) => { - if (mcdu.setCruiseFlightLevelAndTemperature(value)) { - CDUProgressPage.ShowReportPage(mcdu); - } else { - scratchpadCallback(); - } - }; - - const toLeg = plan.activeLeg; - let toWaypointCell = ""; - const toWaypointUTCCell = "---"; - const toWaypointAltCell = "----"; - let nextWaypointCell = ""; - const nextWaypointUTCCell = "----"; - const nextWaypointAltCell = "---"; - if (toLeg && toLeg.isDiscontinuity === false) { - toWaypointCell = toLeg.ident; - // toWaypointUTCCell = FMCMainDisplay.secondsTohhmm(toLeg.infos.etaInFP); TODO port over - const nextLeg = plan.maybeElementAt(plan.activeLegIndex + 1); - - if (nextLeg && nextLeg.isDiscontinuity === false) { - nextWaypointCell = nextLeg.ident; - // nextWaypointUTCCell = FMCMainDisplay.secondsTohhmm(nextLeg.infos.etaInFP); TODO port over - } - } - - let destCell = ""; - const destUTCCell = "---"; - const destDistCell = "----"; - if (plan.destinationAirport) { - destCell = plan.destinationRunway - ? plan.destinationRunway.ident - : plan.destinationAirport.ident; - - } - - mcdu.setTemplate([ - ["REPORT"], - ["\xa0OVHD", "ALT\xa0", "UTC"], - ["", altCell + "[color]cyan"], - ["\xa0TO"], - [toWaypointCell + "[color]green", toWaypointAltCell + "[color]green", toWaypointUTCCell + "[color]green"], - ["\xa0NEXT"], - [nextWaypointCell + "[color]green", nextWaypointAltCell + "[color]green", nextWaypointUTCCell + "[color]green"], - ["\xa0SAT", "FOB\xa0", "T. WIND"], - ["[][color]cyan"], - ["\xa0S/C", "", "UTC DIST"], - [""], - ["\xa0DEST", "EFOB", "UTC DIST"], - [destCell, "", destUTCCell + " " + destDistCell] - ]); - } - - static ShowPredictiveGPSPage(mcdu, overrideDestETA = "") { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ProgressPagePredictiveGPS; - - const plan = mcdu.flightPlanService.active; - - let destIdentCell = ""; - let destETACell = ""; - if (plan.destinationAirport) { - destIdentCell = plan.destinationAirport.ident + "[color]green"; - - if (overrideDestETA) { - destETACell = overrideDestETA; - } else { - // destETACell = FMCMainDisplay.secondsTohhmm(mcdu.flightPlanManager.getDestination().infos.etaInFP); TODO port over (fms-v2) - } - - mcdu.onRightInput[0] = (value) => { - CDUProgressPage.ShowPredictiveGPSPage(mcdu, value); - }; - } - - mcdu.setTemplate([ - ["PREDICTIVE GPS"], - ["DEST", "ETA"], - [destIdentCell, destETACell + "[color]cyan", "{small}PRIMARY{end}"], - ["\xa0\xa0-15 -10 -5 ETA+5 +10 +15"], - ["{small}\xa0\xa0\xa0\xa0Y\xa0\xa0Y\xa0\xa0\xa0Y\xa0\xa0Y\xa0\xa0Y\xa0\xa0\xa0Y\xa0\xa0Y{end}[color]green"], - ["WPT", "ETA"], - ["[ ][color]cyan", "", "{small}PRIMARY{end}"], - ["\xa0\xa0-15 -10 -5 ETA+5 +10 +15"], - [""], - ["", "", "DESELECTED SATELLITES"], - ["[ ][color]cyan"], - [""], - [""] - ]); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Scratchpad.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Scratchpad.js deleted file mode 100644 index 7b1ff1d9ae4..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_Scratchpad.js +++ /dev/null @@ -1,188 +0,0 @@ -// private variables and function are marked with _ prefix -/** The MCDU scratchpad display. This belongs to the MCDU itself. */ -class ScratchpadDisplay { - constructor(mcdu, scratchpadElement) { - this.guid = `SP-${Utils.generateGUID()}`; - this._mcdu = mcdu; - this._scratchpadElement = scratchpadElement; - this._scratchpadElement.className = "white"; - } - - write(value = "", color = "white") { - this._scratchpadElement.textContent = value; - this._scratchpadElement.className = color; - this._mcdu.sendUpdate(); - } - - setStyle(style) { - this._scratchpadElement.style = style; - } - - getText() { - return this._scratchpadElement.textContent; - } - - getColor() { - return this._scratchpadElement.className; - } -} - -/** - * The scratchpad for each subsystem. These belong to the subsystems, - * and one will be connected to the MCDU display (not paused) at any given time. - */ -class ScratchpadDataLink { - constructor(mcdu, displayUnit, subsystem, keypadEnabled = true) { - this._mcdu = mcdu; - this._subsystem = subsystem; - - // actual scratchpad text/colour - this._value = ""; - this._colour = ""; - - // internal state - this._text = ""; - this._message = undefined; - this._status = 0; - this._displayUnit = displayUnit; - this._isPaused = true; - this._keypadEnabled = keypadEnabled; - } - - setText(text) { - this._message = undefined; - this._text = text; - this._display(text); - } - - setMessage(message) { - if (this._message && !this._message.isTypeTwo && message.isTypeTwo) { - return; - } - this._message = message; - this._display(message.text, message.isAmber ? "amber" : "white"); - } - - removeMessage(messageText) { - if (this._message && this._message.text === messageText) { - this.setText(""); - } - } - - addChar(char) { - if (!this._keypadEnabled) { - return; - } - if (this._status !== SpDisplayStatus.userContent) { - this.setText(char); - } else if (this._text.length + 1 < 23) { - this.setText(this._text + char); - } - } - - clear() { - if (!this._keypadEnabled) { - return; - } - if (this._status === SpDisplayStatus.empty) { - this.setText(FMCMainDisplay.clrValue); - } else if (this._status === SpDisplayStatus.clrValue) { - this.setText(""); - } else if (this._status === SpDisplayStatus.userContent) { - this.setText(this._text.slice(0, -1)); - } else { - this._mcdu.removeMessageFromQueue(this._message.text); - this.setText(this._text); - } - } - - clearHeld() { - if (!this._keypadEnabled) { - return; - } - if (this._status === SpDisplayStatus.clrValue || this._status === SpDisplayStatus.userContent) { - this.setText(""); - } - } - - isClearStop() { - return this._status !== SpDisplayStatus.userContent; - }; - - plusMinus(char) { - if (!this._keypadEnabled) { - return; - } - if (this._status === SpDisplayStatus.userContent && this._text.slice(-1) === "-") { - this.setText(this._text.slice(0, -1) + "+"); - } else { - this.addChar(char); - } - } - - setUserData(data) { - this._text = data; - } - - removeUserContentFromScratchpadAndDisplayAndReturnTextContent() { - const userContent = this._text; - if (this._status < SpDisplayStatus.typeOneMessage) { - this.setText(""); - } - return userContent; - } - - getText() { - return this._text; - }; - - getColor() { - return this._colour; - } - - pause() { - this._isPaused = true; - } - - resume() { - this._isPaused = false; - this._display(this._value, this._colour); - } - - _display(value, color = "white") { - // store the content whether we're paused or not - this._colour = color; - this._value = value; - this._updateStatus(value); - - // if we're not paused, write to the display - if (!this._isPaused) { - this._displayUnit.write(value, color); - } - // flag the annunciator if needed - this._mcdu.setRequest(this._subsystem); - } - - _updateStatus(scratchpadText) { - if (this._message) { - this._status = this._message.isTypeTwo ? SpDisplayStatus.typeTwoMessage : SpDisplayStatus.typeOneMessage; - } else { - if (this._text === "" || scratchpadText === "") { - this._status = SpDisplayStatus.empty; - setTimeout(() => this._mcdu.updateMessageQueue(), 150); - } else if (this._text === FMCMainDisplay.clrValue) { - this._status = SpDisplayStatus.clrValue; - } else { - this._status = SpDisplayStatus.userContent; - } - } - } -} - -const SpDisplayStatus = { - empty: 0, - clrValue: 1, - userContent: 2, - typeOneMessage: 3, - typeTwoMessage: 4 -}; diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_SecFplnMain.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_SecFplnMain.js deleted file mode 100644 index 3cb3dc73c69..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_SecFplnMain.js +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) 2021-2024 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUSecFplnMain { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.activeSystem = 'FMGC'; - - mcdu.efisInterfaces.L.setSecRelatedPageOpen(true); - mcdu.efisInterfaces.R.setSecRelatedPageOpen(true); - mcdu.onUnload = () => { - mcdu.efisInterfaces.L.setSecRelatedPageOpen(false); - mcdu.efisInterfaces.R.setSecRelatedPageOpen(false); - }; - - mcdu.onLeftInput[0] = () => { - return; - mcdu.flightPlanService.flightPlanManager.copy(Fmgc.FlightPlanIndex.Active, Fmgc.FlightPlanIndex.FirstSecondary); - CDUFlightPlanPage.ShowPage(mcdu, 0, Fmgc.FlightPlanIndex.FirstSecondary); - }; - - mcdu.onLeftInput[1] = () => { - return; - CDUFlightPlanPage.ShowPage(mcdu, 0, Fmgc.FlightPlanIndex.FirstSecondary); - }; - - mcdu.onLeftInput[2] = () => { - return; - mcdu.flightPlanService.secondaryReset(1); - }; - - mcdu.onLeftInput[5] = () => { - return; - mcdu.flightPlanService.flightPlanManager.swap(Fmgc.FlightPlanIndex.FirstSecondary, Fmgc.FlightPlanIndex.Active); - }; - - mcdu.setTemplate([ - ["SEC INDEX"], - [""], - ["{COPY ACTIVE[color]inop", "INIT>[color]inop"], - [""], - ["[color]inop"], - [""], - ["{DELETE SEC[color]inop"], - [""], - ["*ACTIVATE SEC[color]inop"], - [""], - [""], - [""], - ["*SWAP ACTIVE[color]inop"] - ]); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_SelectWptPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_SelectWptPage.js deleted file mode 100644 index f880154a6c9..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_SelectWptPage.js +++ /dev/null @@ -1,88 +0,0 @@ -class A320_Neo_CDU_SelectWptPage { - /** - * @param mcdu - * @param fixes {Array.} - * @param callback - * @param page - * @constructor - */ - static ShowPage(mcdu, fixes, callback, page = 0) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.SelectWptPage; - const rows = [ - ["", 'FREQ', 'LAT/LONG'], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [" calculateDistance(a) - calculateDistance(b)); - - for (let i = 0; i < 5; i++) { - const w = orderedWaypoints[i + 5 * page]; - if (w) { - let freq = ""; - - if (w.databaseId[0] === "V" || w.databaseId[0] === "N") { - freq = w.frequency ? fastToFixed(w.frequency, 2) : " "; - } - - const lat = w.locLocation ? w.locLocation.lat : w.location.lat; - const long = w.locLocation ? w.locLocation.long : w.location.long; - - const latString = `${Math.abs(lat).toFixed(0).padStart(2, "0")}${lat >= 0 ? 'N' : 'S'}`; - const longString = `${Math.abs(long).toFixed(0).padStart(3, "0")}${long >= 0 ? 'E' : 'W'}`; - - const dist = Math.min(calculateDistance(w), 9999); - - rows[2 * i].splice(0, 1, "{green}" + dist.toFixed(0) + "{end}NM"); - rows[2 * i + 1] = ["*" + w.ident + "[color]cyan", freq + "[color]green", `${latString}/${longString}[color]green`]; - mcdu.onLeftInput[i] = () => { - callback(w); - }; - mcdu.onRightInput[i] = () => { - callback(w); - }; - mcdu.onLeftInput[5] = () => { - if (mcdu.returnPageCallback) { - mcdu.returnPageCallback(); - } else { - console.error("A return page callback was expected but not declared. Add a returnPageCallback to page: " + mcdu.page.Current); - } - }; - } - } - mcdu.setTemplate([ - ["DUPLICATE NAMES", (page + 1).toFixed(0), Math.ceil(orderedWaypoints.length / 5).toFixed(0)], - ...rows, - [""] - ]); - mcdu.onPrevPage = () => { - if (page > 0) { - A320_Neo_CDU_SelectWptPage.ShowPage(mcdu, orderedWaypoints, callback, page - 1); - } - }; - mcdu.onNextPage = () => { - if (page < Math.floor(fixes.length / 5)) { - A320_Neo_CDU_SelectWptPage.ShowPage(mcdu, orderedWaypoints, callback, page + 1); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_SelectedNavaids.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_SelectedNavaids.js deleted file mode 100644 index 506ccfda12a..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_SelectedNavaids.js +++ /dev/null @@ -1,145 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -/** - * @typedef {Object} SelectedNavaid - * @property {Fmgc.SelectedNavaidType} type - * @property {Fmgc.SelectedNavaidMode} mode - * @property {string} ident - * @property {number} frequency - * @property {RawVor | null} facility - */ - -NAVAID_TYPE_STRINGS = Object.freeze({ - [Fmgc.SelectedNavaidType.None]: '', - [Fmgc.SelectedNavaidType.Dme]: 'DME', - [Fmgc.SelectedNavaidType.Vor]: 'VOR', - [Fmgc.SelectedNavaidType.VorDme]: 'VORDME', - [Fmgc.SelectedNavaidType.VorTac]: 'VORTAC', - [Fmgc.SelectedNavaidType.Tacan]: 'TACAN', - [Fmgc.SelectedNavaidType.Ils]: 'ILSDME', - [Fmgc.SelectedNavaidType.Gls]: 'GLS', - [Fmgc.SelectedNavaidType.Mls]: 'MLS', -}); - -NAVAID_MODE_STRINGS = Object.freeze({ - [Fmgc.SelectedNavaidMode.Auto]: 'AUTO', - [Fmgc.SelectedNavaidMode.Manual]: 'MAN', - [Fmgc.SelectedNavaidMode.Rmp]: 'RMP', -}); - -class CDUSelectedNavaids { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.SelectedNavaids; - mcdu.returnPageCallback = () => CDUSelectedNavaids.ShowPage(mcdu); - mcdu.pageRedrawCallback = () => CDUSelectedNavaids.ShowPage(mcdu); - setTimeout(mcdu.requestUpdate.bind(mcdu), 500); - - const template = [ - ["\xa0SELECTED NAVAIDS"], - ["", "DESELECT"], - ["", '', ""], - [""], - [""], - [""], - [""], - [""], - [""], - ["\xa0RADIONAV SELECTED[color]cyan"], - ["{DESELECT[color]inop"], - ["\xa0GPS SELECTED[color]cyan"], - ["{DESELECT[color]inop", "RETURN>"] - ]; - - /** @type {SelectedNavaid[]} */ - const selectedNavaids = mcdu.getSelectedNavaids(); - - for (const [i, navaid] of selectedNavaids.entries()) { - if (navaid.frequency < 1) { - continue; - } - - const labelRow = 2 * i + 1; - const lineRow = labelRow + 1; - - template[labelRow][0] = `\xa0${NAVAID_TYPE_STRINGS[navaid.type].padEnd(9, '\xa0')}${NAVAID_MODE_STRINGS[navaid.mode]}`; - template[lineRow][0] = `{cyan}${navaid.facility !== null ? '{' : '\xa0'}${(navaid.ident !== null ? navaid.ident : '').padEnd(6, '\xa0')}{end}{small}{green}${navaid.frequency.toFixed(2)}{end}{end}`; - - if (navaid.facility !== null) { - mcdu.onLeftInput[i] = (text, scratchpadCallback) => { - if (text === '') { - CDUNavaidPage.ShowPage(mcdu, navaid.facility, () => CDUSelectedNavaids.ShowPage(mcdu)); - } else { - scratchpadCallback(); - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - } - }; - } - } - - const deselected = mcdu.deselectedNavaids; - for (let i = 0; i < 4; i++) { - const icao = deselected[i]; - if (!icao) { - break; - } - - const lineRow = 2 * i + 2; - - // FIXME take facilities rather than database idents - template[lineRow][1] = `{cyan}${icao.substring(7).trim()}{end}`; - - mcdu.onRightInput[i] = (text, scratchpadCallback) => { - if (text === FMCMainDisplay.clrValue) { - mcdu.reselectNavaid(icao); - mcdu.requestUpdate(); - } else if (text.match(/^[A-Z0-9]{1,4}$/) !== null) { - mcdu.getOrSelectNavaidsByIdent(text, (navaid) => { - if (navaid) { - mcdu.reselectNavaid(icao); - mcdu.deselectNavaid(navaid.databaseId); - CDUSelectedNavaids.ShowPage(mcdu); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } - }); - } else { - scratchpadCallback(); - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } - }; - } - if (deselected.length < 4) { - const lineRow = 2 * deselected.length + 2; - template[lineRow][1] = '{cyan}[\xa0\xa0]{small}*{end}{end}'; - - mcdu.onRightInput[deselected.length] = (text, scratchpadCallback) => { - if (text.match(/^[A-Z0-9]{1,4}$/) !== null) { - mcdu.getOrSelectNavaidsByIdent(text, (navaid) => { - if (navaid) { - mcdu.deselectNavaid(navaid.databaseId); - CDUSelectedNavaids.ShowPage(mcdu); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } - }); - } else { - scratchpadCallback(); - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } - }; - } - - mcdu.setTemplate(template); - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onRightInput[5] = () => { - CDUPositionMonitorPage.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_StepAltsPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_StepAltsPage.js deleted file mode 100644 index 22d7a971ffa..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_StepAltsPage.js +++ /dev/null @@ -1,356 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUStepAltsPage { - static Return() {} - - static ShowPage(mcdu) { - mcdu.pageUpdate = () => { }; - - mcdu.page.Current = mcdu.page.StepAltsPage; - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.StepAltsPage) { - CDUStepAltsPage.ShowPage(mcdu); - } - }, mcdu.PageTimeout.Medium); - - const activePlan = mcdu.flightPlanService.active; - - /** @type {FlightPlanLeg[]} */ - const legsWithSteps = activePlan.allLegs.filter((it) => it.isDiscontinuity === false && it.cruiseStep); - - const isFlying = mcdu.flightPhaseManager.phase >= FmgcFlightPhases.TAKEOFF && mcdu.flightPhaseManager.phase < FmgcFlightPhases.DONE; - const transitionAltitude = activePlan.performanceData.transitionAltitude; - - const predictions = mcdu.guidanceController.vnavDriver.mcduProfile && mcdu.guidanceController.vnavDriver.mcduProfile.isReadyToDisplay - ? mcdu.guidanceController.vnavDriver.mcduProfile.waypointPredictions - : null; - - mcdu.setTemplate([ - ["STEP ALTS {small}FROM{end} {green}FL" + mcdu.cruiseLevel + "{end}"], - ["\xa0ALT\xa0/\xa0WPT", "DIST\xa0TIME"], - CDUStepAltsPage.formatStepClimbLine(mcdu, legsWithSteps, 0, predictions, isFlying, transitionAltitude), - [""], - CDUStepAltsPage.formatStepClimbLine(mcdu, legsWithSteps, 1, predictions, isFlying, transitionAltitude), - [""], - CDUStepAltsPage.formatStepClimbLine(mcdu, legsWithSteps, 2, predictions, isFlying, transitionAltitude), - [""], - CDUStepAltsPage.formatStepClimbLine(mcdu, legsWithSteps, 3, predictions, isFlying, transitionAltitude), - [""], - CDUStepAltsPage.formatOptStepLine(legsWithSteps), - [""], - [" CDUStepAltsPage.tryAddOrUpdateCruiseStepFromLeftInput(mcdu, scratchpadCallback, legsWithSteps, i, value); - } - - mcdu.onLeftInput[4] = () => { }; - - mcdu.onLeftInput[5] = () => { - CDUStepAltsPage.Return(); - }; - - mcdu.onRightInput[0] = () => { }; - mcdu.onRightInput[1] = () => { }; - mcdu.onRightInput[2] = () => { }; - mcdu.onRightInput[3] = () => { }; - mcdu.onRightInput[4] = () => { }; - mcdu.onRightInput[5] = () => { }; - } - - static formatFl(altitude, transAlt) { - if (transAlt >= 100 && altitude > transAlt) { - return "FL" + Math.round(altitude / 100); - } - return altitude; - } - - static formatOptStepLine(steps) { - if (steps.length > 0) { - return ["", ""]; - } - - return ["{small}OPT STEP:{end}", "{small}ENTER ALT ONLY{end}"]; - } - - // TODO: I think it should not allow entries of step climbs after step descents, but I'm not sure if it rejects it entirely - // or gives you an IGNORED. - /** - * @param legsWithSteps {FlightPlanLeg[]} - */ - static formatStepClimbLine(mcdu, legsWithSteps, index, predictions, isFlying, transitionAltitude) { - if (!legsWithSteps || index > legsWithSteps.length) { - return [""]; - } else if (index === legsWithSteps.length) { - return ["{cyan}[\xa0\xa0\xa0]/[\xa0\xa0\xa0\xa0\xa0]{end}"]; - } else { - const waypoint = legsWithSteps[index]; - const step = legsWithSteps[index].cruiseStep; - - const prediction = predictions ? predictions.get(step.waypointIndex) : null; - - // Cases: - // 1. Step above MAX FL (on PROG page) - // 2. IGNORED (If too close to T/D or before T/C) - // 3. STEP AHEAD - // 4. Distance and time< - - let lastColumn = "----\xa0----"; - if (this.checkIfStepAboveMaxFl(mcdu, step.toAltitude)) { - lastColumn = "ABOVE\xa0MAX[s-text]"; - } else if (step.isIgnored) { - lastColumn = "IGNORED\xa0[s-text]"; - } else if (prediction) { - const { distanceFromAircraft, secondsFromPresent } = prediction; - - if (Number.isFinite(distanceFromAircraft) && Number.isFinite(secondsFromPresent)) { - if (distanceFromAircraft < 20) { - lastColumn = "STEP\xa0AHEAD[s-text]"; - } else { - const distanceCell = "{green}" + Math.round(distanceFromAircraft).toFixed(0) + "{end}"; - - const utcTime = SimVar.GetGlobalVarValue("ZULU TIME", "seconds"); - const timeCell = isFlying - ? `{green}${FMCMainDisplay.secondsToUTC(utcTime + secondsFromPresent)}[s-text]{end}` - : `{green}${FMCMainDisplay.secondsTohhmm(secondsFromPresent)}[s-text]{end}`; - - lastColumn = distanceCell + "\xa0" + timeCell; - } - } - } - - return ["{cyan}" + CDUStepAltsPage.formatFl(step.toAltitude, transitionAltitude) + "/" + waypoint.ident + "{end}", lastColumn]; - } - } - - /** - * @param stepLegs {FlightPlanLeg[]} - */ - static tryAddOrUpdateCruiseStepFromLeftInput(mcdu, scratchpadCallback, stepLegs, index, input) { - if (index < stepLegs.length) { - this.onClickExistingStepClimb(mcdu, scratchpadCallback, stepLegs, index, input); - - return; - } - - // Create new step altitude - if (stepLegs.length >= 4) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } - - const splitInputs = input.split("/"); - const rawAltitudeInput = splitInputs[0]; - const rawIdentInput = splitInputs[1]; - - if (!rawIdentInput) { - // OPT STEP - mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - return false; - } - - const alt = this.tryParseAltitude(rawAltitudeInput); - if (!alt) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - const plan = mcdu.flightPlanService.active; // TODO allow other plans, maybe (fms-v2) - const legIndex = plan.findLegIndexByFixIdent(rawIdentInput); - - if (legIndex < 0) { - // Waypoint ident not found in flightplan - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } else if (legIndex < plan.activeLegIndex) { - // Don't allow step on FROM waypoint - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } else if (!this.checkStepInsertionRules(mcdu, stepLegs, legIndex, alt)) { - // Step too small or step descent after step climb - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } - - const leg = plan.legElementAt(legIndex); - - // It is not allowed to have two steps on the same waypoint (FCOM) - if (leg.cruiseStep !== undefined) { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } - - mcdu.flightPlanService.addOrUpdateCruiseStep(legIndex, alt, Fmgc.FlightPlanIndex.Active); - - if (CDUStepAltsPage.checkIfStepAboveMaxFl(mcdu, alt)) { - mcdu.addMessageToQueue(NXSystemMessages.stepAboveMaxFl); - } - } - - static tryParseAltitude(altitudeInput) { - if (!altitudeInput) { - return false; - } - - const match = altitudeInput.match(/^(FL)?(\d{1,3})$/); - - if (!match) { - return false; - } - - const altValue = parseInt(match[2]) * 100; - if (altValue < 1000 || altValue > 39000) { - return false; - } - - return altValue; - } - - /** - * @param stepLegs {FlightPlanLeg[]} - */ - static onClickExistingStepClimb(mcdu, scratchpadCallback, stepLegs, index, input) { - const plan = mcdu.flightPlanService.active; - - const stepWaypoint = stepLegs[index]; - const clickedStep = stepWaypoint.cruiseStep; - - if (input === FMCMainDisplay.clrValue) { - mcdu.flightPlanService.removeCruiseStep(clickedStep.waypointIndex); - - return true; - } - - // Edit step - const splitInputs = input.split("/"); - if (splitInputs.length === 1 || splitInputs.length === 2 && splitInputs[1] === "") { - // Altitude - const altitude = this.tryParseAltitude(splitInputs[0]); - if (!altitude) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } else if (!this.checkStepInsertionRules(mcdu, stepLegs, clickedStep.waypointIndex, clickedStep.toAltitude)) { - // Step too small or step descent after step climb - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - return; - } - - mcdu.flightPlanService.addOrUpdateCruiseStep(clickedStep.waypointIndex, altitude); - - if (this.checkIfStepAboveMaxFl(mcdu, altitude)) { - mcdu.addMessageToQueue(NXSystemMessages.stepAboveMaxFl); - } - } else if (splitInputs.length === 2) { - const rawAltitudeInput = splitInputs[0]; - const rawIdentInput = splitInputs[1]; - - const legIndex = plan.findLegIndexByFixIdent(rawIdentInput); - - if (legIndex < 0) { - // Waypoint ident not found in flightplan - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - if (rawAltitudeInput === "") { - // /Waypoint - mcdu.flightPlanService.addOrUpdateCruiseStep(legIndex, clickedStep.toAltitude); - mcdu.flightPlanService.removeCruiseStep(clickedStep.waypointIndex); - } else { - // Altitude/waypoint - const altitude = this.tryParseAltitude(rawAltitudeInput); - - if (!altitude) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - mcdu.flightPlanService.addOrUpdateCruiseStep(legIndex, altitude); - mcdu.flightPlanService.removeCruiseStep(clickedStep.waypointIndex); - - if (this.checkIfStepAboveMaxFl(mcdu, altitude)) { - mcdu.addMessageToQueue(NXSystemMessages.stepAboveMaxFl); - } - } - } else if (splitInputs.length === 3) { - // Altitude/place/distance or - // /Place/distance - mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - scratchpadCallback(); - return; - } - } - - static checkIfStepAboveMaxFl(mcdu, altitude) { - const maxFl = mcdu.getMaxFlCorrected(); - return Number.isFinite(maxFl) && altitude > maxFl * 100; - } - - /** - * Check a couple of rules about insertion of step: - * - Minimum step size is 1000ft - * - S/C follows step descent - * TODO: It's possible that the insertion of a step in between already inserted steps causes a step descent after step climb - * I don't know how the plane handles this. - * @param {*} mcdu - * @param {FlightPlanLeg[]} stepLegs Existing steps - * @param {*} insertAtIndex Index of waypoint to insert step at - * @param {*} toAltitude Altitude of step - */ - static checkStepInsertionRules(mcdu, stepLegs, insertAtIndex, toAltitude) { - let altitude = mcdu.cruiseLevel * 100; - let doesHaveStepDescent = false; - - let i = 0; - for (; i < stepLegs.length; i++) { - const step = stepLegs[i].cruiseStep; - if (step.waypointIndex > insertAtIndex) { - break; - } - - const stepAltitude = step.toAltitude; - if (stepAltitude < altitude) { - doesHaveStepDescent = true; - } - - altitude = stepAltitude; - } - - const isStepSizeValid = Math.abs(toAltitude - altitude) >= 1000; - if (!isStepSizeValid) { - return false; - } - - const isClimbVsDescent = toAltitude > altitude; - if (!isClimbVsDescent) { - doesHaveStepDescent = true; - } else if (doesHaveStepDescent) { - return false; - } - - if (i < stepLegs.length) { - const stepAfter = stepLegs[i].cruiseStep; - const isStepSizeValid = Math.abs(stepAfter.toAltitude - toAltitude) >= 1000; - const isClimbVsDescent = stepAfter.toAltitude > toAltitude; - - const isClimbAfterDescent = isClimbVsDescent && doesHaveStepDescent; - - return isStepSizeValid && !isClimbAfterDescent; - } - - return true; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_VerticalRevisionPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_VerticalRevisionPage.js deleted file mode 100644 index 9c0ea557b9a..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_VerticalRevisionPage.js +++ /dev/null @@ -1,508 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -/** This must match WaypointConstraintType from TS */ -const WaypointConstraintType = Object.freeze({ - Unknown: 0, - CLB: 1, - DES: 2, -}); - -class CDUVerticalRevisionPage { - /** - * @param mcdu - * @param {FlightPlanLeg} waypoint - * @param verticalWaypoint - * @param confirmSpeed - * @param confirmAlt - * @param confirmCode - */ - static ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, confirmSpeed = undefined, confirmAlt = undefined, confirmCode = undefined, forPlan = Fmgc.FlightPlanIndex.Active, inAlternate = false) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.VerticalRevisionPage; - - /** @type {BaseFlightPlan} */ - const targetPlan = mcdu.flightPlan(forPlan, inAlternate); - const mainTargetPlan = mcdu.flightPlan(forPlan, false); - // Use performance data of primary for waypoints in alternate - const performanceData = mainTargetPlan.performanceData; - - const confirmConstraint = Number.isFinite(confirmSpeed) || Number.isFinite(confirmAlt); - const constraintType = CDUVerticalRevisionPage.constraintType(mcdu, wpIndex, forPlan, inAlternate); - const isOrigin = wpIndex === 0; - const isDestination = wpIndex === targetPlan.destinationLegIndex; - - let waypointIdent = "---"; - if (waypoint) { - if (isDestination && targetPlan.destinationRunway) { - waypointIdent = targetPlan.destinationRunway.ident; - } else { - waypointIdent = waypoint.ident; - } - } - - const showSpeedLim = mcdu._fuelPredDone || isOrigin || isDestination || constraintType !== WaypointConstraintType.Unknown; - // the conditions other than isDestination are a workaround for no ToC - const showDesSpeedLim = showSpeedLim && (isDestination || - constraintType === WaypointConstraintType.DES || - (mcdu.flightPhaseManager.phase > FmgcFlightPhases.CRUISE && - mcdu.flightPhaseManager.phase < FmgcFlightPhases.GOAROUND)); - - const climbSpeedLimitSpeed = inAlternate ? performanceData.alternateClimbSpeedLimitSpeed : performanceData.climbSpeedLimitSpeed; - const climbSpeedLimitAltitude = inAlternate ? performanceData.alternateClimbSpeedLimitAltitude : performanceData.climbSpeedLimitAltitude; - const isClimbSpeedLimitPilotEntered = inAlternate ? performanceData.isAlternateClimbSpeedLimitPilotEntered : performanceData.isClimbSpeedLimitPilotEntered; - - const descentSpeedLimitSpeed = inAlternate ? performanceData.alternateDescentSpeedLimitSpeed : performanceData.descentSpeedLimitSpeed; - const descentSpeedLimitAltitude = inAlternate ? performanceData.alternateDescentSpeedLimitAltitude : performanceData.descentSpeedLimitAltitude; - const isDescentSpeedLimitPilotEntered = inAlternate ? performanceData.isAlternateDescentSpeedLimitPilotEntered : performanceData.isDescentSpeedLimitPilotEntered; - - let speedLimitTitle = ""; - let speedLimitCell = ""; - if (showDesSpeedLim) { - speedLimitTitle = "\xa0DES SPD LIM"; - if (descentSpeedLimitSpeed !== null) { - speedLimitCell = `{magenta}{${isDescentSpeedLimitPilotEntered ? 'big' : 'small'}}${descentSpeedLimitSpeed.toFixed(0).padStart(3, "0")}/${this.formatFl(descentSpeedLimitAltitude, performanceData.transitionLevel * 100)}{end}{end}`; - } else { - speedLimitCell = "{cyan}*[ ]/[ ]{end}"; - } - } else if (showSpeedLim) { - speedLimitTitle = "\xa0CLB SPD LIM"; - if (climbSpeedLimitSpeed !== null) { - speedLimitCell = `{magenta}{${isClimbSpeedLimitPilotEntered ? 'big' : 'small'}}${climbSpeedLimitSpeed.toFixed(0).padStart(3, "0")}/${this.formatFl(climbSpeedLimitAltitude, performanceData.transitionAltitude)}{end}{end}`; - } else { - speedLimitCell = "{cyan}*[ ]/[ ]{end}"; - } - } - - const speedConstraint = waypoint.speedConstraint ? Math.round(waypoint.speedConstraint.speed).toFixed(0) : undefined; - const transAltLevel = constraintType === WaypointConstraintType.DES ? performanceData.transitionLevel * 100 : performanceData.transitionAltitude; - const altitudeConstraint = this.formatAltConstraint(waypoint.altitudeConstraint, transAltLevel); - const canHaveAltConstraint = !isDestination && !waypoint.isXA(); - - let r3Title = canHaveAltConstraint ? "ALT CSTR\xa0" : ""; - let r3Cell = canHaveAltConstraint ? "{cyan}[\xa0\xa0\xa0\xa0]*{end}" : ""; - let l3Title = "\xa0SPD CSTR"; - let l3Cell = "{cyan}*[\xa0\xa0\xa0]{end}"; - let l4Title = "MACH/START WPT[color]inop"; - let l4Cell = `\xa0{inop}[\xa0]/{small}${waypointIdent}{end}{end}`; - let r4Title = ""; - let r4Cell = ""; - let r5Cell = ""; - - if (isDestination) { - const hasGsIntercept = targetPlan.approach && (targetPlan.approach.type === 5 /* ILS */ || targetPlan.approach.type === 6 /* GLS */); - const gsIntercept = hasGsIntercept ? targetPlan.glideslopeIntercept() : 0; - - if (hasGsIntercept && gsIntercept > 0) { - r3Title = "G/S INTCP\xa0"; - r3Cell = `{green}{small}${gsIntercept.toFixed(0)}{end}{end}`; - } else { - r3Title = ""; - r3Cell = ""; - } - - const distanceToDest = mcdu.getDistanceToDestination(); - const closeToDest = distanceToDest !== undefined && distanceToDest <= 180; - l4Title = "\xa0QNH"; - if (isFinite(mcdu.perfApprQNH)) { - if (mcdu.perfApprQNH < 500) { - l4Cell = `{cyan}${mcdu.perfApprQNH.toFixed(2)}{end}`; - } else { - l4Cell = `{cyan}${mcdu.perfApprQNH.toFixed(0)}{end}`; - } - } else if (closeToDest) { - l4Cell = "{amber}____{end}"; - } else { - l4Cell = "{cyan}[\xa0\xa0]{end}"; - } - mcdu.onLeftInput[3] = (value, scratchpadCallback) => { - if (mcdu.setPerfApprQNH(value)) { - CDUVerticalRevisionPage.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, confirmSpeed, confirmAlt, confirmCode, forPlan, inAlternate); - } else { - scratchpadCallback(); - } - }; - - l3Title = ""; - l3Cell = ""; - r5Cell = ""; - } else { - if (canHaveAltConstraint && altitudeConstraint) { - r3Cell = `{magenta}${altitudeConstraint}{end}`; - } - if (speedConstraint) { - l3Cell = `{magenta}${speedConstraint}{end}`; - } - - [r4Title, r4Cell] = this.formatAltErrorTitleAndValue(waypoint, verticalWaypoint); - - if (mcdu.cruiseLevel && (mcdu.flightPhaseManager.phase < FmgcFlightPhases.DESCENT || mcdu.flightPhaseManager.phase > FmgcFlightPhases.GOAROUND)) { - r5Cell = "STEP ALTS>"; - } - } - - mcdu.setTemplate([ - ["VERT REV {small}AT{end}{green} " + waypointIdent + "{end}"], - [], - [""], - [speedLimitTitle, ""], - [speedLimitCell, "RTA>[color]inop"], - [l3Title, r3Title], - [l3Cell, r3Cell], - [l4Title, r4Title], - [l4Cell, r4Cell], - [""], - [" { - if (!showSpeedLim) { - scratchpadCallback(); - return; - } - - if (value === FMCMainDisplay.clrValue) { - if (showDesSpeedLim) { - mainTargetPlan.setPerformanceData(inAlternate ? 'alternateDescentSpeedLimitSpeed' : 'descentSpeedLimitSpeed', null); - mainTargetPlan.setPerformanceData(inAlternate ? 'alternateDescentSpeedLimitAltitude' : 'descentSpeedLimitAltitude', null); - mainTargetPlan.setPerformanceData(inAlternate ? 'isAlternateDescentSpeedLimitPilotEntered' : 'isDescentSpeedLimitPilotEntered', false); - } else { - mainTargetPlan.setPerformanceData(inAlternate ? 'alternateClimbSpeedLimitSpeed' : 'climbSpeedLimitSpeed', null); - mainTargetPlan.setPerformanceData(inAlternate ? 'alternateClimbSpeedLimitAltitude' : 'climbSpeedLimitAltitude', null); - mainTargetPlan.setPerformanceData(inAlternate ? 'isAlternateClimbSpeedLimitPilotEntered' : 'isClimbSpeedLimitPilotEntered', false); - } - CDUVerticalRevisionPage.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - return; - } - - const matchResult = value.match(/^([0-9]{1,3})\/(((FL)?([0-9]{1,3}))|([0-9]{4,5}))$/); - if (matchResult === null) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - const speed = parseInt(matchResult[1]); - let alt = matchResult[5] !== undefined ? parseInt(matchResult[5]) * 100 : parseInt(matchResult[6]); - - if (speed < 90 || speed > 350 || alt > 45000) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - return; - } - - alt = Math.round(alt / 10) * 10; - - if (showDesSpeedLim) { - mainTargetPlan.setPerformanceData(inAlternate ? 'alternateDescentSpeedLimitSpeed' : 'descentSpeedLimitSpeed', speed); - mainTargetPlan.setPerformanceData(inAlternate ? 'alternateDescentSpeedLimitAltitude' : 'descentSpeedLimitAltitude', alt); - mainTargetPlan.setPerformanceData(inAlternate ? 'isAlternateDescentSpeedLimitPilotEntered' : 'isDescentSpeedLimitPilotEntered', true); - } else { - mainTargetPlan.setPerformanceData(inAlternate ? 'alternateClimbSpeedLimitSpeed' : 'climbSpeedLimitSpeed', speed); - mainTargetPlan.setPerformanceData(inAlternate ? 'alternateClimbSpeedLimitAltitude' : 'climbSpeedLimitAltitude', alt); - mainTargetPlan.setPerformanceData(inAlternate ? 'isAlternateClimbSpeedLimitPilotEntered' : 'isClimbSpeedLimitPilotEntered', true); - } - - CDUVerticalRevisionPage.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - }; // SPD LIM - mcdu.onRightInput[1] = () => {}; // RTA - mcdu.onLeftInput[2] = async (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt(wpIndex, constraintType === WaypointConstraintType.DES, undefined, forPlan, inAlternate); - - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - return; - } - - if (value.match(/^[0-9]{1,3}$/) === null) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - const speed = parseInt(value); - - if (speed < 90 || speed > 350) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - return; - } - - if (constraintType === WaypointConstraintType.Unknown) { - CDUVerticalRevisionPage.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, speed, undefined, undefined, forPlan, inAlternate); - return; - } - - await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt(wpIndex, constraintType === WaypointConstraintType.DES, speed, forPlan, inAlternate); - - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - }; // SPD CSTR - if (canHaveAltConstraint) { - mcdu.onRightInput[2] = async (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt(wpIndex, constraintType === WaypointConstraintType.DES, undefined, forPlan, inAlternate); - - mcdu.updateConstraints(); - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - - return; - } - - const matchResult = value.match(/^([+-])?(((FL)?([0-9]{1,3}))|([0-9]{4,5}))$/); - - if (matchResult === null) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - const altitude = matchResult[5] !== undefined ? parseInt(matchResult[5]) * 100 : parseInt(matchResult[6]); - const code = matchResult[1] === undefined ? '@' : (matchResult[1] === '-' ? '-' : '+'); - - if (altitude > 45000) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - return; - } - - if (constraintType === WaypointConstraintType.Unknown) { - CDUVerticalRevisionPage.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, altitude, code, forPlan, inAlternate,); - return; - } - - await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt(wpIndex, constraintType === WaypointConstraintType.DES, { - altitudeDescriptor: code, - altitude1: altitude, - }, forPlan, inAlternate); - - mcdu.updateConstraints(); - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - }; // ALT CSTR - } - mcdu.onLeftInput[4] = () => { - //TODO: show appropriate wind page based on waypoint - CDUWindPage.Return = () => { - CDUVerticalRevisionPage.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - }; - CDUWindPage.ShowPage(mcdu); - }; // WIND - mcdu.onRightInput[4] = () => { - if (!mcdu.cruiseLevel) { - return; - } - CDUStepAltsPage.Return = () => { - CDUVerticalRevisionPage.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - }; - CDUStepAltsPage.ShowPage(mcdu); - }; // STEP ALTS - if (!confirmConstraint) { - mcdu.onLeftInput[5] = () => { - CDUFlightPlanPage.ShowPage(mcdu); - }; - } else { - mcdu.onLeftInput[5] = async () => { - if (Number.isFinite(confirmSpeed)) { - await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt(wpIndex, false, confirmSpeed, forPlan, inAlternate); - - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - } - - if (Number.isFinite(confirmAlt)) { - await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt(wpIndex, false, { - altitudeDescriptor: confirmCode, - altitude1: confirmAlt, - }, forPlan, inAlternate); - - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - } - }; - - mcdu.onRightInput[5] = async () => { - if (Number.isFinite(confirmSpeed)) { - await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt(wpIndex, true, confirmSpeed, forPlan, inAlternate); - - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - } - if (Number.isFinite(confirmAlt)) { - await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt(wpIndex, true, { - altitudeDescriptor: confirmCode, - altitude1: confirmAlt, - }, forPlan, inAlternate); - - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); - } - }; - } - } - - static formatFl(constraint, transAlt) { - if (transAlt >= 100 && constraint > transAlt) { - return "FL" + Math.round(constraint / 100); - } - return constraint; - } - - static formatAltConstraint(constraint, transAltLvl) { - if (!constraint) { - return ''; - } - - switch (constraint.altitudeDescriptor) { - case '@': // AtAlt1 - case 'I': // AtAlt1GsIntcptAlt2 - case 'X': // AtAlt1AngleAlt2 - return this.formatFl(Math.round(constraint.altitude1), transAltLvl); - case '+': // AtOrAboveAlt1 - case 'J': // AtOrAboveAlt1GsIntcptAlt2 - case 'V': // AtOrAboveAlt1AngleAlt2 - return "+" + this.formatFl(Math.round(constraint.altitude1), transAltLvl); - case '-': // AtOrBelowAlt1 - case 'Y': // AtOrBelowAlt1AngleAlt2 - return "-" + this.formatFl(Math.round(constraint.altitude1), transAltLvl); - case 'B': // range - if (constraint.altitude1 < constraint.altitude2) { - return "+" + this.formatFl(Math.round(constraint.altitude1), transAltLvl) + "/-" + this.formatFl(Math.round(constraint.altitude2), transAltLvl); - } else { - return "+" + this.formatFl(Math.round(constraint.altitude2), transAltLvl) + "/-" + this.formatFl(Math.round(constraint.altitude1), transAltLvl); - } - case 'C': // AtOrAboveAlt2: - return "+" + this.formatFl(Math.round(constraint.altitude2), transAltLvl); - default: - return ''; - } - } - - /** - * @param mcdu - * @param legIndex {number} - * @param forPlan {number} - * @param inAlternate {boolean} - * @returns {number|(function(*, *): (*))|*|WaypointConstraintType|VerticalWaypointType} - */ - static constraintType(mcdu, legIndex, forPlan, inAlternate) { - const planAtIndex = mcdu.flightPlanService.get(forPlan); - const plan = inAlternate ? planAtIndex.alternateFlightPlan : planAtIndex; - - const leg = plan.legElementAt(legIndex); - - if (leg.constraintType !== 3 /* Unknown */) { - return leg.constraintType; - } - - return plan.autoConstraintTypeForLegIndex(legIndex); - } - - // constraints can be set directly by LSK on f-pln page - static async setConstraints(mcdu, leg, legIndex, verticalWaypoint, value, scratchpadCallback, offset = 0, forPlan = 0, inAlternate = false) { - const type = CDUVerticalRevisionPage.constraintType(mcdu, legIndex, forPlan, inAlternate); - const isDescentConstraint = type === WaypointConstraintType.DES; - - if (value === FMCMainDisplay.clrValue) { - await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt(legIndex, isDescentConstraint, undefined, forPlan, inAlternate); - await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt(legIndex, isDescentConstraint, undefined, forPlan, inAlternate); - - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - CDUFlightPlanPage.ShowPage(mcdu, offset); - return; - } - - const matchResult = value.match(/^(([0-9]{1,3})\/?)?(\/([+-])?(((FL)?([0-9]{1,3}))|([0-9]{4,5})))?$/); - if (matchResult === null) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - return; - } - - let speed; - let alt; - - if (matchResult[2] !== undefined) { - speed = parseInt(matchResult[2]); - } - - const code = matchResult[4] === undefined ? '@' : (matchResult[4] === '-' ? '-' : '+'); - - if (matchResult[8] !== undefined) { - alt = parseInt(matchResult[8]) * 100; - } - - if (matchResult[9] !== undefined) { - alt = parseInt(matchResult[9]); - } - - if ((speed !== undefined && (speed < 90 || speed > 350)) || (alt !== undefined && alt > 45000)) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - scratchpadCallback(); - return; - } - - if (type === WaypointConstraintType.Unknown) { - CDUVerticalRevisionPage.ShowPage(mcdu, leg, legIndex, verticalWaypoint, speed, alt, code, forPlan, inAlternate); - return; - } - - if (speed !== undefined) { - await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt(legIndex, isDescentConstraint, speed, forPlan, inAlternate); - - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - CDUFlightPlanPage.ShowPage(mcdu, offset); - } - - if (alt !== undefined) { - await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt(legIndex, isDescentConstraint, { - altitudeDescriptor: code, - altitude1: alt, - }, forPlan, inAlternate); - - mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - - CDUFlightPlanPage.ShowPage(mcdu, offset); - } - } - - static formatAltErrorTitleAndValue(waypoint, verticalWaypoint) { - const empty = ["", ""]; - - if (!waypoint || !verticalWaypoint) { - return empty; - } - - // No constraint - if (!verticalWaypoint.altitudeConstraint || verticalWaypoint.isAltitudeConstraintMet) { - return empty; - } - - // Weird prediction error - if (!isFinite(verticalWaypoint.altError)) { - return empty; - } - - let formattedAltError = (Math.round(verticalWaypoint.altError / 10) * 10).toFixed(0); - if (verticalWaypoint.altError > 0) { - formattedAltError = "+" + formattedAltError; - } - - return ["ALT ERROR\xa0", "{green}{small}" + formattedAltError + "{end}{end}"]; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_WaypointPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_WaypointPage.js deleted file mode 100644 index bc90a797492..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_WaypointPage.js +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2021-2024 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -/* - Displays blank waypoint field, when waypoint inputted, LAT, LONG will show. - Derives from Data Index PG2 -*/ - -class CDUWaypointPage { - static ShowPage(mcdu, waypoint = undefined) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.WaypointPage; - mcdu.returnPageCallback = () => { - CDUWaypointPage.ShowPage(mcdu); - }; - - let identValue = "_______[color]amber"; - let latLongLabel = ""; - let latLongValue = ""; - - if (waypoint) { - identValue = `${waypoint.ident}[color]cyan`; - latLongLabel = '\xa0\xa0\xa0\xa0LAT/LONG'; ; - latLongValue = `${CDUPilotsWaypoint.formatLatLong(waypoint.location)}[color]green`; - } - - mcdu.onLeftInput[0] = (value, scratchpadCallback) => { - if (value === FMCMainDisplay.clrValue) { - CDUWaypointPage.ShowPage(mcdu, undefined); - return; - } - - mcdu.getOrSelectWaypointByIdent(value, res => { - if (res) { - CDUWaypointPage.ShowPage(mcdu, res); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); - scratchpadCallback(); - } - }); - }; - - mcdu.setTemplate([ - ["WAYPOINT"], - ["\xa0IDENT"], - [identValue], - [latLongLabel], - [latLongValue], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""] - ]); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_WindPage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_WindPage.js deleted file mode 100644 index 6afbbac1402..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU_WindPage.js +++ /dev/null @@ -1,449 +0,0 @@ -class CDUWindPage { - - static Return() {} - - static ShowPage(mcdu) { - CDUWindPage.ShowCLBPage(mcdu); - } - - static ShowCLBPage(mcdu, offset = 0) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ClimbWind; - - let requestButton = "REQUEST*[color]amber"; - let requestEnable = true; - if (mcdu.simbrief.sendStatus === "REQUESTING") { - requestEnable = false; - requestButton = "REQUEST [color]amber"; - } - - const template = ([ - ["CLIMB WIND"], - ["TRU WIND/ALT", "HISTORY[color]inop"], - ["", "WIND>[color]inop"], - ["", ""], - ["", ""], - ["", "WIND{sp}[color]amber"], - ["", requestButton], - ["", ""], - ["", ""], - ["", "NEXT{sp}"], - ["", "PHASE>"], - ["", ""], - [" { - CDUWindPage.ShowCRZPage(mcdu); - }; - - mcdu.onLeftInput[5] = () => { - CDUWindPage.Return(); - }; - - mcdu.onRightInput[2] = () => { - if (requestEnable) { - CDUWindPage.WindRequest(mcdu, "CLB", CDUWindPage.ShowCLBPage); - } - }; - } - - static ShowCRZPage(mcdu, offset = 0) { - - //TODO: allow wind to be set for each waypoint - - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.CruiseWind; - - let requestButton = "REQUEST*[color]amber"; - let requestEnable = true; - if (mcdu.simbrief.sendStatus === "REQUESTING") { - requestEnable = false; - requestButton = "REQUEST [color]amber"; - } - - const template = ([ - //["CRZ WIND {small}AT{end} {green}WAYPOINT{end}"], - ["CRZ WIND"], - ["TRU WIND/ALT", ""], - ["", ""], - ["", ""], - ["", ""], - ["", "WIND{sp}[color]amber"], - ["", requestButton], - ["", "PREV{sp}"], - ["", "PHASE>"], - ["{small}SAT / ALT{end}[color]inop", "NEXT{sp}"], - ["[ ]°/[{sp}{sp}{sp}][color]inop", "PHASE>"], - ["", ""], - [" { - CDUWindPage.ShowCLBPage(mcdu); - }; - mcdu.onRightInput[4] = () => { - CDUWindPage.ShowDESPage(mcdu); - }; - - mcdu.onLeftInput[5] = () => { - CDUWindPage.Return(); - }; - - mcdu.onRightInput[2] = () => { - if (requestEnable) { - CDUWindPage.WindRequest(mcdu, "CRZ", CDUWindPage.ShowCRZPage); - } - }; - } - - static ShowDESPage(mcdu, offset = 0) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.DescentWind; - - const alternateAirport = mcdu.flightPlanService.active ? mcdu.flightPlanService.active.alternateDestinationAirport : undefined; - - let requestButton = "REQUEST*[color]amber"; - let requestEnable = true; - if (mcdu.simbrief.sendStatus === "REQUESTING") { - requestEnable = false; - requestButton = "REQUEST [color]amber"; - } - - let alternateHeader = ""; - let alternateCell = ""; - let altFLightlevel = ""; - - if (alternateAirport) { - alternateHeader = "ALTN WIND"; - alternateCell = "[ ]°/[ ][color]cyan"; - altFLightlevel = "{green}{small}FL100{end}{end}"; - if (mcdu.winds.alternate != null) { - alternateCell = `${CDUWindPage.FormatNumber(mcdu.winds.alternate.direction)}°/${CDUWindPage.FormatNumber(mcdu.winds.alternate.speed)}[color]cyan`; - } - } - const template = [ - ["DESCENT WIND"], - ["TRU WIND/ALT", alternateHeader], - ["", alternateCell], - ["", altFLightlevel], - ["", ""], - ["", "WIND{sp}[color]amber"], - ["", requestButton], - ["", "PREV{sp}"], - ["", "PHASE>"], - ["", ""], - ["", ""], - ["", ""], - [" { - if (value == FMCMainDisplay.clrValue) { - mcdu.winds.alternate = null; - CDUWindPage.ShowDESPage(mcdu, offset); - return; - } - const wind = CDUWindPage.ParseWind(value); - if (wind == null) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(); - } else { - mcdu.winds.alternate = wind; - CDUWindPage.ShowDESPage(mcdu, offset); - } - }; - } - - mcdu.onRightInput[3] = () => { - CDUWindPage.ShowCRZPage(mcdu); - }; - - mcdu.onLeftInput[5] = () => { - CDUWindPage.Return(); - }; - - mcdu.onRightInput[2] = () => { - if (requestEnable) { - CDUWindPage.WindRequest(mcdu, "DSC", CDUWindPage.ShowDESPage); - } - }; - } - - static FormatNumber(n, leadingZeroes) { - let output = `${n.toFixed(0)}`; - for (let i = 0; i < leadingZeroes; i++) { - if (n < (10 ** (leadingZeroes - i))) { - output = `0${output}`; - } - } - return output; - } - - static ShowWinds(rows, mcdu, _showPage, _winds, _offset, _max = 3) { - let entries = 0; - for (let i = 0; i < (_winds.length - _offset); i++) { - if (i < _max) { - const wind = _winds[i + _offset]; - rows[(i * 2) + 2][0] = `${CDUWindPage.FormatNumber(wind.direction, 2)}°/${CDUWindPage.FormatNumber(wind.speed, 2)}/FL${CDUWindPage.FormatNumber(wind.altitude, 2)}[color]cyan`; - entries = i + 1; - mcdu.onLeftInput[i] = (value) => { - if (value == FMCMainDisplay.clrValue) { - _winds.splice(i + _offset, 1); - _showPage(mcdu, _offset); - } - }; - } - } - if (entries < _max) { - rows[(entries * 2) + 2][0] = "{cyan}[ ]°/[ ]/[{sp}{sp}{sp}]{end}"; - mcdu.onLeftInput[entries] = (value, scratchpadCallback) => { - CDUWindPage.TryAddWind(mcdu, _winds, value, () => _showPage(mcdu, _offset), scratchpadCallback); - }; - } - - let up = false; - let down = false; - - if (_winds.length > (_max - 1) && _offset > 0) { - mcdu.onDown = () => { - _showPage(mcdu, _offset - 1); - }; - down = true; - } - - if (_offset < (_winds.length - (_max - 1))) { - mcdu.onUp = () => { - _showPage(mcdu, _offset + 1); - }; - up = true; - } - - mcdu.setArrows(up, down, false, false); - - return rows; - } - - static ParseTrueWindAlt(_input) { - const elements = _input.split('/'); - if (elements.length != 3) { - return null; - } - - let direction = parseInt(elements[0]); - if (direction == 360) { - direction = 0; - } - if (!isFinite(direction) || direction < 0 || direction > 359) { - return null; - } - - const speed = parseInt(elements[1]); - if (!isFinite(speed) || speed < 0 || speed > 999) { - return null; - } - - const altitude = parseInt(elements[2]); - if (!isFinite(altitude) || altitude < 0 || altitude > 450) { - return null; - } - - return { - direction: direction, - speed: speed, - altitude: altitude - }; - } - - static TryAddWind(mcdu, _windArray, _input, _showPage, scratchpadCallback) { - const data = CDUWindPage.ParseTrueWindAlt(_input); - if (data == null) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - scratchpadCallback(_input); - } else { - _windArray.push(data); - _showPage(); - } - } - - static ParseWind(_input) { - const elements = _input.split('/'); - if (elements.length != 2) { - return null; - } - - let direction = parseInt(elements[0]); - if (direction == 360) { - direction = 0; - } - if (!isFinite(direction) || direction < 0 || direction > 359) { - return null; - } - - const speed = parseInt(elements[1]); - if (!isFinite(speed) || speed < 0 || speed > 999) { - return null; - } - - return { - direction: direction, - speed: speed - }; - } - - static WindRequest(mcdu, stage, _showPage) { - getSimBriefOfp(mcdu, () => {}, () => { - let windData = []; - let lastAltitude = 0; - switch (stage) { - case "CLB": - const clbWpts = mcdu.simbrief.navlog.filter((val) => val.stage === stage); - - // iterate through each clbWpt grabbing the wind data - clbWpts.forEach((clbWpt, wptIdx) => { - if (wptIdx == 0) { - let altIdx = 0; - // we need to backfill from altitude 0 to below clbWpt.altitude_feet in windData - while (lastAltitude < clbWpt.altitude_feet) { - const altitude = parseInt(clbWpt.wind_data.level[altIdx].altitude); - const speed = parseInt(clbWpt.wind_data.level[altIdx].wind_spd); - const direction = parseInt(clbWpt.wind_data.level[altIdx].wind_dir); - - windData.push({ - direction, - speed, - altitude: altitude / 100, - }); - lastAltitude = altitude; - altIdx++; - } - } - // Now we add the closest wind data to the altitude of the clbWpt - clbWpt.wind_data.level.forEach((wind, levelIdx) => { - const altitude = parseInt(wind.altitude); - - let deltaPrevLevel = 0; - let deltaThisLevel = 0; - // Look backwards for the closest level - if (levelIdx > 0 && levelIdx < clbWpt.wind_data.level.length - 1) { - deltaPrevLevel = Math.abs(clbWpt.altitude_feet - parseInt(clbWpt.wind_data.level[levelIdx - 1].altitude)); - deltaThisLevel = Math.abs(clbWpt.altitude_feet - altitude); - } - - // Check that altitude isn't backtracking - if (altitude > lastAltitude && lastAltitude <= clbWpt.altitude_feet) { - const idx = (deltaPrevLevel > deltaThisLevel) ? levelIdx : levelIdx - 1; - - const idxAltitude = parseInt(clbWpt.wind_data.level[idx].altitude); - const direction = parseInt(clbWpt.wind_data.level[idx].wind_dir); - const speed = parseInt(clbWpt.wind_data.level[idx].wind_spd); - - // Check again that we didn't backtrack - if (idxAltitude > lastAltitude) { - windData.push({ - direction, - speed, - altitude: idxAltitude / 100, - }); - lastAltitude = idxAltitude; - } - } - }); - }); - mcdu.winds.climb = windData; - break; - case "CRZ": - const toc = mcdu.simbrief.navlog.find((val) => val.ident === "TOC"); - mcdu.winds.cruise = []; - toc.wind_data.level.forEach((val) => { - const direction = parseInt(val.wind_dir); - const speed = parseInt(val.wind_spd); - const altitude = parseInt(val.altitude / 100); - mcdu.winds.cruise.push({ - direction, - speed, - altitude, - }); - lastAltitude = altitude; - }); - break; - case "DSC": - // TOD is marked as cruise stage, but we want it's topmost wind data - const tod = mcdu.simbrief.navlog.find((val) => val.ident === "TOD"); - const desWpts = [tod, ...mcdu.simbrief.navlog.filter((val) => val.stage === stage)]; - - if (isFinite(mcdu.simbrief.alternateAvgWindDir) && isFinite(mcdu.simbrief.alternateAvgWindSpd)) { - mcdu.winds.alternate = { - direction: mcdu.simbrief.alternateAvgWindDir, - speed: mcdu.simbrief.alternateAvgWindSpd, - }; - } else { - mcdu.winds.alternate = null; - } - // iterate through each clbWpt grabbing the wind data - windData = []; - lastAltitude = 45000; - desWpts.forEach((desWpt, wptIdx) => { - if (wptIdx == 0) { - let altIdx = desWpt.wind_data.level.length - 1; - // we need to backfill from crz altitude to above next clbWpt.altitude_feet in windData - while (lastAltitude > desWpt.altitude_feet) { - const altitude = parseInt(desWpt.wind_data.level[altIdx].altitude); - const speed = parseInt(desWpt.wind_data.level[altIdx].wind_spd); - const direction = parseInt(desWpt.wind_data.level[altIdx].wind_dir); - - windData.push({ - direction, - speed, - altitude: altitude / 100, - }); - lastAltitude = altitude; - altIdx--; - } - } - // Now we add the closest wind data to the altitude of the desWpt - desWpt.wind_data.level.reverse().forEach((wind, levelIdx) => { - const altitude = parseInt(wind.altitude); - - let deltaNextLevel = 0; - let deltaThisLevel = 0; - // Look forwards for the closest level - if (levelIdx < desWpt.wind_data.level.length - 2) { - deltaNextLevel = Math.abs(desWpt.altitude_feet - parseInt(desWpt.wind_data.level[levelIdx + 1].altitude)); - deltaThisLevel = Math.abs(desWpt.altitude_feet - altitude); - } - - // Check that altitude isn't backtracking - if (altitude >= lastAltitude && lastAltitude > desWpt.altitude_feet) { - const idx = (deltaNextLevel > deltaThisLevel) ? levelIdx : levelIdx + 1; - - const idxAltitude = parseInt(desWpt.wind_data.level[idx].altitude); - const direction = parseInt(desWpt.wind_data.level[idx].wind_dir); - const speed = parseInt(desWpt.wind_data.level[idx].wind_spd); - - // Check again that we didn't backtrack - if (idxAltitude < lastAltitude) { - windData.push({ - direction, - speed, - altitude: idxAltitude / 100, - }); - lastAltitude = idxAltitude; - } - } - }); - }); - mcdu.winds.des = windData; - break; - } - _showPage(mcdu); - }); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/AIDS/A320_Neo_CDU_AIDS_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/AIDS/A320_Neo_CDU_AIDS_Menu.js deleted file mode 100644 index 4e47b9be27a..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/AIDS/A320_Neo_CDU_AIDS_Menu.js +++ /dev/null @@ -1,28 +0,0 @@ -class CDU_AIDS_MainMenu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.activeSystem = 'AIDS'; - mcdu.setTemplate([ - ["AIDS"], - ["CALL-UP[color]inop"], - ["[color]inop"], - [""], - ["[color]inop"], - ["", "STORED[color]inop"], - ["", "REPORTS>[color]inop"], - ["ASSIGNMENT[color]inop", "MAN REQST[color]inop"], - ["[color]inop"], - ["", "POST[color]cyan"], - ["DAR = RUNNING[color]green", "STOP*[color]cyan"] - ]); - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_FreeText.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_FreeText.js deleted file mode 100644 index 38ded311985..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_FreeText.js +++ /dev/null @@ -1,171 +0,0 @@ -class CDUAocFreeText { - static ShowPage(mcdu, store = { "msg_to": "", "reqID": SimVar.GetSimVarValue('L:A32NX_HOPPIE_ACTIVE', 'number') !== 0 ? 0 : 1, "msg_line1": "", "msg_line2": "", "msg_line3": "", "msg_line4": "", "sendStatus": ""}) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AOCFreeText; - const networkTypes = [ - 'HOPPIE', - 'FBW' - ]; - - const updateView = () => { - let oneLineFilled = false; - if (store["msg_line1"] !== "" || store["msg_line2"] !== "" || store["msg_line3"] !== "" || store["msg_line4"] !== "") { - oneLineFilled = true; - } - let sendValid = oneLineFilled === true && store["msg_to"] !== ""; - if (store["sendStatus"] === "SENDING" || store["sendStatus"] === "SENT") { - sendValid = false; - } - - mcdu.setTemplate([ - ["AOC FREE TEXT"], - ["\xa0TO", "NETWORK\xa0"], - [`${store["msg_to"] !== "" ? store["msg_to"] + "[color]cyan" : "________[color]amber"}`, `↓${networkTypes[store["reqID"]]}[color]cyan`], - [""], - [`${store["msg_line1"] !== "" ? store["msg_line1"] : "["}[color]cyan`, `${store["msg_line1"] != "" ? "" : "]"}[color]cyan`], - [""], - [`${store["msg_line2"] !== "" ? store["msg_line2"] : "["}[color]cyan`, `${store["msg_line2"] != "" ? "" : "]"}[color]cyan`], - [""], - [`${store["msg_line3"] !== "" ? store["msg_line3"] : "["}[color]cyan`, `${store["msg_line3"] != "" ? "" : "]"}[color]cyan`], - [""], - [`${store["msg_line4"] !== "" ? store["msg_line4"] : "["}[color]cyan`, `${store["msg_line4"] != "" ? "" : "]"}[color]cyan`], - ["\xa0RETURN TO", `${store["sendStatus"]}\xa0`], - [" { - if (value === FMCMainDisplay.clrValue) { - store["msg_to"] = ""; - } else { - store["msg_to"] = value; - } - CDUAocFreeText.ShowPage(mcdu, store); - }; - - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - store["msg_line1"] = ""; - } else { - store["msg_line1"] = value; - } - CDUAocFreeText.ShowPage(mcdu, store); - }; - - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - store["msg_line2"] = ""; - } else { - store["msg_line2"] = value; - } - CDUAocFreeText.ShowPage(mcdu, store); - }; - - mcdu.onLeftInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - store["msg_line3"] = ""; - } else { - store["msg_line3"] = value; - } - CDUAocFreeText.ShowPage(mcdu, store); - }; - - mcdu.onLeftInput[4] = (value) => { - if (value === FMCMainDisplay.clrValue) { - store["msg_line4"] = ""; - } else { - store["msg_line4"] = value; - } - CDUAocFreeText.ShowPage(mcdu, store); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - store["reqID"] = (store["reqID"] + 1) % 2; - updateView(); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = async () => { - // do not send two times - if (store["sendStatus"] === "SENDING" || store["sendStatus"] === "SENT") { - return; - } - - let oneLineFilled = false; - if (store["msg_line1"] !== "" || store["msg_line2"] !== "" || store["msg_line3"] !== "" || store["msg_line4"] !== "") { - oneLineFilled = true; - } - const sendValid = oneLineFilled === true && store["msg_to"] !== ""; - - if (sendValid === false) { - mcdu.setScratchpadMessage(NXSystemMessages.mandatoryFields); - return; - } - - store["sendStatus"] = "SENDING"; - if (mcdu.page.Current === mcdu.page.AOCFreeText) { - updateView(); - } - - // create the message - const message = new AtsuCommon.FreetextMessage(); - if (store["reqID"] === 0) { - message.Network = AtsuCommon.AtsuMessageNetwork.Hoppie; - } else { - message.Network = AtsuCommon.AtsuMessageNetwork.FBW; - } - message.Station = store["msg_to"]; - if (store["msg_line1"] !== "") { - message.Message += store["msg_line1"] + '\n'; - } - if (store["msg_line2"] !== "") { - message.Message += store["msg_line2"] + '\n'; - } - if (store["msg_line3"] !== "") { - message.Message += store["msg_line3"] + '\n'; - } - if (store["msg_line4"] !== "") { - message.Message += store["msg_line4"] + '\n'; - } - message.Message = message.Message.substring(0, message.Message.length - 1); - - // send the message - mcdu.atsu.sendMessage(message).then((code) => { - if (code === AtsuCommon.AtsuStatusCodes.Ok) { - store["sendStatus"] = "SENT"; - store["msg_line1"] = ""; - store["msg_line2"] = ""; - store["msg_line3"] = ""; - store["msg_line4"] = ""; - - setTimeout(() => { - store["sendStatus"] = ""; - if (mcdu.page.Current === mcdu.page.AOCFreeText) { - CDUAocFreeText.ShowPage(mcdu, store); - } - }, 5000); - } else { - store["sendStatus"] = "FAILED"; - mcdu.addNewAtsuMessage(code); - } - if (mcdu.page.Current === mcdu.page.AOCFreeText) { - updateView(); - } - }); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[5] = () => { - CDUAocMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_Init.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_Init.js deleted file mode 100644 index 636bf2fc86a..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_Init.js +++ /dev/null @@ -1,199 +0,0 @@ -/** - * Value is rounded to 1000 and fixed to 1 decimal - * @param {number | string} value - */ -function formatWeight(value) { - return (+value).toFixed(1); -} - -class CDUAocInit { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AOCInit; - mcdu.pageRedrawCallback = () => CDUAocInit.ShowPage(mcdu); - mcdu.activeSystem = 'ATSU'; - - let fltNbr = '_______[color]amber'; - let originIcao = '____[color]amber'; - let destinationIcao = '____[color]amber'; - let ete = "____[color]amber"; - let fob = `{small}---.-{end}[color]white`; - let requestButton = "INIT DATA REQ*[color]cyan"; - let gmt = "0000[color]green"; - - const seconds = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - gmt = `{small}${FMCMainDisplay.secondsTohhmm(seconds)}{end}[color]green`; - - function updateView() { - if (mcdu.page.Current === mcdu.page.AOCInit) { - CDUAocInit.ShowPage(mcdu); - } - } - - // regular update due to showing time on this page - mcdu.page.SelfPtr = setTimeout(() => { - updateView(); - }, mcdu.PageTimeout.Default); - - if (mcdu.simbrief.sendStatus !== "READY" && mcdu.simbrief.sendStatus !== "DONE") { - requestButton = "INIT DATA REQ [color]cyan"; - } - if (mcdu.simbrief.originIcao) { - originIcao = `${mcdu.simbrief.originIcao}[color]cyan`; - } - if (mcdu.simbrief.destinationIcao) { - destinationIcao = `${mcdu.simbrief.destinationIcao}[color]cyan`; - } - if (mcdu.simbrief.callsign) { - fltNbr = `{small}${mcdu.simbrief.callsign}{end}[color]green`; - } - if (mcdu.simbrief.ete) { - ete = `${FMCMainDisplay.secondsTohhmm(mcdu.simbrief.ete)}[color]cyan`; - } - if (mcdu.isAnEngineOn()) { - // should only get if an engine running - const currentFob = formatWeight(NXUnits.kgToUser(mcdu.getFOB())); - if (currentFob) { - fob = `{small}${currentFob}{end}[color]green`; - } - } - mcdu.setTemplate([ - ["INIT/REVIEW", "1", "2", "AOC"], - ["\xa0FMC FLT NO", "GMT\xa0"], - [fltNbr, gmt], - ["\xa0DEP"], - [originIcao], - ["\xa0DEST"], - [destinationIcao, "CREW DETAILS>[color]inop"], - ["\xa0FOB"], - [" " + fob], - ["\xa0ETE"], - [ete, requestButton], - ["", "ADVISORY\xa0"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = () => { - // Crew Details - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelayBasic(); - }; - mcdu.onRightInput[4] = () => { - getSimBriefOfp(mcdu, updateView); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAocMenu.ShowPage(mcdu); - }; - - mcdu.onNextPage = () => { - CDUAocInit.ShowPage2(mcdu); - }; - } - - static ShowPage2(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AOCInit2; - mcdu.activeSystem = 'ATSU'; - /** - GMT: is the current zulu time - FLT time: is wheels up to wheels down... so basically shows 0000 as soon as you are wheels up, counts up and then stops timing once you are weight on wheels again - Out: is when you set the brakes to off... - Doors: When the last door closes - Off: remains blank until Take off time - On: remains blank until Landing time - In: remains blank until brakes set to park AND the first door opens - */ - let fob = `{small}---.-{end}[color]white`; - let fltTime = `----[color]white`; - let outTime = `----[color]white`; - let doorsTime = `----[color]white`; - let offTime = `----[color]white`; - let onTime = `----[color]white`; - let inTime = `----[color]white`; - let blockTime = `----[color]white`; - let gmt = "0000[color]green"; - - const seconds = Math.floor(SimVar.GetGlobalVarValue("ZULU TIME", "seconds")); - gmt = `{small}${FMCMainDisplay.secondsTohhmm(seconds)}{end}[color]green`; - if (mcdu.isAnEngineOn()) { - currentFob = formatWeight(NXUnits.kgToUser(mcdu.getFOB())); - if (currentFob) { - fob = `{small}${currentFob}{end}[color]green`; - } - } - if (mcdu.aocTimes.out) { - outTime = `${FMCMainDisplay.secondsTohhmm(mcdu.aocTimes.out)}[color]green`; - } - if (mcdu.aocTimes.doors) { - doorsTime = `${FMCMainDisplay.secondsTohhmm(mcdu.aocTimes.doors)}[color]green`; - } - if (mcdu.aocTimes.off) { - offTime = `${FMCMainDisplay.secondsTohhmm(mcdu.aocTimes.off)}[color]green`; - let currentfltTime = 0; - if (mcdu.aocTimes.on) { - currentfltTime = mcdu.aocTimes.on - mcdu.aocTimes.off; - } else { - currentfltTime = seconds - mcdu.aocTimes.off; - } - fltTime = `${FMCMainDisplay.secondsTohhmm(currentfltTime)}[color]green`; - } - if (mcdu.aocTimes.on) { - onTime = `${FMCMainDisplay.secondsTohhmm(mcdu.aocTimes.on)}[color]green`; - } - if (mcdu.aocTimes.in) { - inTime = `${FMCMainDisplay.secondsTohhmm(mcdu.aocTimes.in)}[color]green`; - } - if (mcdu.aocTimes.in && mcdu.aocTimes.out) { - blockTime = `${FMCMainDisplay.secondsTohhmm(mcdu.aocTimes.in - mcdu.aocTimes.out)}[color]green`; - } - - function updateView() { - if (mcdu.page.Current !== mcdu.page.AOCInit2) { - return; - } - const display = [ - ["INIT/REVIEW", "2", "2", "AOC"], - [" OUT", "OFF ", "DOORS"], - [outTime, offTime, doorsTime], - [" ON", "IN ", "GMT"], - [onTime, inTime, gmt], - [" BLK TIME", "FLT TIME "], - [blockTime, fltTime], - [" FUEL REM", "LDG PILOT "], - [" " + fob, "-------"], - ["", ""], - ["*AUTOLAND <{small}n{end}>[color]cyan"], - ["", "ADVISORY "], - [" { - updateView(); - }, mcdu.PageTimeout.Default); - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAocMenu.ShowPage(mcdu); - }; - - mcdu.onPrevPage = () => { - CDUAocInit.ShowPage(mcdu); - }; - - updateView(); - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_Menu.js deleted file mode 100644 index 4bf08f2984e..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_Menu.js +++ /dev/null @@ -1,61 +0,0 @@ -class CDUAocMenu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AOCMenu; - mcdu.setTemplate([ - ["AOC MENU"], - [""], - [""], - ["", ""], - [""], - ["", "SENT\xa0"], - ["", "MESSAGES>"], - [""], - ["", "DIVERSION>[color]inop"], - ["\xa0ATSU DLK"], - ["[color]inop"] - ]); - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = () => { - CDUAocRequestsWeather.ShowPage(mcdu); - }; - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - CDUAocRequestsAtis.ShowPage(mcdu); - }; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtsuMenu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - CDUAocFreeText.ShowPage(mcdu); - }; - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = () => { - CDUAocMessagesReceived.ShowPage(mcdu); - }; - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = () => { - CDUAocMessagesSent.ShowPage(mcdu); - }; - mcdu.onLeftInput[0] = () => { - CDUAocInit.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_MessageSentDetail.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_MessageSentDetail.js deleted file mode 100644 index 62519c3433c..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_MessageSentDetail.js +++ /dev/null @@ -1,79 +0,0 @@ -class CDUAocMessageSentDetail { - static ShowPage(mcdu, messages, messageIndex, offset = 0) { - mcdu.clearDisplay(); - const message = messages[messageIndex]; - const lines = message.serialize(AtsuCommon.AtsuMessageSerializationFormat.FmsDisplay).split("\n"); - - // mark message as read - mcdu.atsu.messageRead(message.UniqueMessageID, true); - - const msgArrows = messages.length > 1 ? " {}" : ""; - - if (lines.length > 8) { - let up = false; - let down = false; - if (lines[offset + 1]) { - mcdu.onUp = () => { - offset += 1; - CDUAocMessageSentDetail.ShowPage(mcdu, messages, messageIndex, offset); - }; - up = true; - } - if (lines[offset - 1]) { - mcdu.onDown = () => { - offset -= 1; - CDUAocMessageSentDetail.ShowPage(mcdu, messages, messageIndex, offset); - }; - down = true; - } - mcdu.setArrows(up, down, false, false); - } - - mcdu.setTemplate([ - ["AOC SENT MSG"], - [`[b-text]${message.Timestamp.fmsTimestamp()} TO ${message.Station}[color]green`, `${messageIndex + 1}/${messages.length}${msgArrows}`], - [`[s-text]${lines[offset] ? lines[offset] : ""}`], - [`[b-text]${lines[offset + 1] ? lines[offset + 1] : ""}`], - [`[s-text]${lines[offset + 2] ? lines[offset + 2] : ""}`], - [`[b-text]${lines[offset + 3] ? lines[offset + 3] : ""}`], - [`[s-text]${lines[offset + 4] ? lines[offset + 4] : ""}`], - [`[b-text]${lines[offset + 5] ? lines[offset + 5] : ""}`], - [`[s-text]${lines[offset + 6] ? lines[offset + 6] : ""}`], - [`[b-text]${lines[offset + 7] ? lines[offset + 7] : ""}`], - [`[s-text]${lines[offset + 8] ? lines[offset + 8] : ""}`], - ["\xa0SENT MSGS"], - [" { - const nextMesssageIndex = messageIndex + 1; - if (nextMesssageIndex < messages.length) { - CDUAocMessageSentDetail.ShowPage(mcdu, messages, nextMesssageIndex); - } - }; - - mcdu.onPrevPage = () => { - const previousMesssageIndex = messageIndex - 1; - if (previousMesssageIndex >= 0) { - CDUAocMessageSentDetail.ShowPage(mcdu, messages, previousMesssageIndex); - } - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[5] = () => { - CDUAocMessagesSent.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onRightInput[5] = () => { - mcdu.atsu.printMessage(message); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_MessagesReceived.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_MessagesReceived.js deleted file mode 100644 index 26a8c4a6ff8..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_MessagesReceived.js +++ /dev/null @@ -1,96 +0,0 @@ -class CDUAocMessagesReceived { - static ShowPage(mcdu, messages = null, page = 0) { - if (!messages) { - messages = mcdu.atsu.aocInputMessages(); - } - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AOCRcvdMsgs; - - page = Math.max(0, Math.min(Math.floor((messages.length - 1) / 5), page)); - - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.AOCRcvdMsgs) { - CDUAocMessagesReceived.ShowPage(mcdu, null, page); - } - }, mcdu.PageTimeout.Slow); - - const offset = 5 + page * 5; - - const msgTimeHeaders = []; - msgTimeHeaders.length = 6; - for (let i = 5; i > 0; i--) { - let headerLeft = ""; - let headerRight = ""; - - if (messages.length > (offset - i) && messages[offset - i]) { - let sender = messages[offset - i].Station; - if (messages[offset - i].Type === AtsuCommon.AtsuMessageType.ATIS) { - sender = messages[offset - i].Reports[0].airport; - } - headerLeft += `${messages[offset - i].Timestamp.fmsTimestamp()} FROM ${sender}[color]green`; - if (!messages[offset - i].Confirmed) { - headerRight = "NEW[color]green"; - } - } - - msgTimeHeaders[i] = [headerLeft, headerRight]; - } - - let left = false, right = false; - if (messages.length > ((page + 1) * 5)) { - mcdu.onNextPage = () => { - CDUAocMessagesReceived.ShowPage(mcdu, messages, page + 1); - }; - right = true; - } - if (page > 0) { - mcdu.onPrevPage = () => { - CDUAocMessagesReceived.ShowPage(mcdu, messages, page - 1); - }; - left = true; - } - mcdu.setArrows(false, false, left, right); - - mcdu.setTemplate([ - ["AOC RCVD MSGS"], - [msgTimeHeaders[5][0], msgTimeHeaders[5][1]], - [`${messages[offset - 5] ? "<" + translateAtsuMessageType(messages[offset - 5].Type) : "NO MESSAGES"}`], - [msgTimeHeaders[4][0], msgTimeHeaders[4][1]], - [`${messages[offset - 4] ? "<" + translateAtsuMessageType(messages[offset - 4].Type) : ""}`], - [msgTimeHeaders[3][0], msgTimeHeaders[3][1]], - [`${messages[offset - 3] ? "<" + translateAtsuMessageType(messages[offset - 3].Type) : ""}`], - [msgTimeHeaders[2][0], msgTimeHeaders[2][1]], - [`${messages[offset - 2] ? "<" + translateAtsuMessageType(messages[offset - 2].Type) : ""}`], - [msgTimeHeaders[1][0], msgTimeHeaders[1][1]], - [`${messages[offset - 1] ? "<" + translateAtsuMessageType(messages[offset - 1].Type) : ""}`], - ["\xa0AOC MENU"], - [" { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[i] = (value) => { - if (messages[offset - 5 + i]) { - if (value === FMCMainDisplay.clrValue) { - mcdu.atsu.removeMessage(messages[offset - 5 + i].UniqueMessageID, true); - CDUAocMessagesReceived.ShowPage(mcdu, null, page); - } else { - CDUAocRequestsMessage.ShowPage(mcdu, messages, offset - 5 + i); - } - } - }; - } - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[5] = () => { - CDUAocMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_MessagesSent.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_MessagesSent.js deleted file mode 100644 index a3fbb920d05..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_MessagesSent.js +++ /dev/null @@ -1,86 +0,0 @@ -class CDUAocMessagesSent { - static ShowPage(mcdu, messages = null, page = 0) { - if (!messages) { - messages = mcdu.atsu.aocOutputMessages(); - } - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AOCSentMsgs; - - page = Math.max(0, Math.min(Math.floor((messages.length - 1) / 5), page)); - - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.AOCSentMsgs) { - CDUAocMessagesSent.ShowPage(mcdu, null, page); - } - }, mcdu.PageTimeout.Slow); - - const offset = 5 + page * 5; - - const msgTimeHeaders = []; - msgTimeHeaders.length = 6; - for (let i = 5; i > 0; i--) { - let header = ""; - if (messages.length > (offset - i) && messages[offset - i]) { - header += `${messages[offset - i].Timestamp.fmsTimestamp()} TO ${messages[offset - i].Station}[color]green`; - } - msgTimeHeaders[i] = header; - } - - let left = false, right = false; - if (messages.length > ((page + 1) * 5)) { - mcdu.onNextPage = () => { - CDUAocMessagesSent.ShowPage(mcdu, messages, page + 1); - }; - right = true; - } - if (page > 0) { - mcdu.onPrevPage = () => { - CDUAocMessagesSent.ShowPage(mcdu, messages, page - 1); - }; - left = true; - } - mcdu.setArrows(false, false, left, right); - - mcdu.setTemplate([ - ["AOC SENT MSGS"], - [msgTimeHeaders[5]], - [`${messages[offset - 5] ? "<" + translateAtsuMessageType(messages[offset - 5].Type) : "NO MESSAGES"}`], - [msgTimeHeaders[4]], - [`${messages[offset - 4] ? "<" + translateAtsuMessageType(messages[offset - 4].Type) : ""}`], - [msgTimeHeaders[3]], - [`${messages[offset - 3] ? "<" + translateAtsuMessageType(messages[offset - 3].Type) : ""}`], - [msgTimeHeaders[2]], - [`${messages[offset - 2] ? "<" + translateAtsuMessageType(messages[offset - 2].Type) : ""}`], - [msgTimeHeaders[1]], - [`${messages[offset - 1] ? "<" + translateAtsuMessageType(messages[offset - 1].Type) : ""}`], - ["\xa0AOC MENU"], - [" { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[i] = (value) => { - if (messages[offset - 5 + i]) { - if (value === FMCMainDisplay.clrValue) { - mcdu.atsu.removeMessage(messages[offset - 5 + i].UniqueMessageID, true); - CDUAocMessagesSent.ShowPage(mcdu); - } else { - CDUAocMessageSentDetail.ShowPage(mcdu, messages, offset - 5 + i); - } - } - }; - } - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[5] = () => { - CDUAocMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_RequestsAtis.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_RequestsAtis.js deleted file mode 100644 index 0c0eb678990..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_RequestsAtis.js +++ /dev/null @@ -1,205 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUAocRequestsAtis { - static CreateDataBlock(mcdu) { - const retval = { - requestId: mcdu.flightPhaseManager.phase === FmgcFlightPhases.PREFLIGHT ? AtsuCommon.AtisType.Departure : AtsuCommon.AtisType.Arrival, - departure: "", - arrival: "", - selected: "", - manual: false, - formatID: 1, - sendStatus: "" - }; - - const activePlan = mcdu.flightPlanService.active; - - if (activePlan.originAirport) { - retval.departure = activePlan.originAirport.ident; - - if (mcdu.flightPhaseManager.phase === FmgcFlightPhases.PREFLIGHT) { - retval.selected = retval.departure; - } - } - - if (activePlan.destinationAirport) { - retval.arrival = activePlan.destinationAirport.ident; - - if (mcdu.flightPhaseManager.phase !== FmgcFlightPhases.PREFLIGHT) { - retval.selected = retval.arrival; - } - } - - return retval; - } - - static ShowPage(mcdu, store = CDUAocRequestsAtis.CreateDataBlock(mcdu)) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AOCRequestAtis; - let labelTimeout; - let formatString; - - if (store.formatID === 0) { - formatString = "PRINTER*[color]cyan"; - } else { - formatString = "MCDU*[color]cyan"; - } - - let arrivalText = "{ARRIVAL[color]cyan"; - let departureText = "{DEPARTURE[color]cyan"; - let enrouteText = "ENROUTE}[color]cyan"; - - if (store.requestId === AtsuCommon.AtisType.Arrival) { - arrivalText = "ARRIVAL[color]cyan"; - } else if (store.requestId === AtsuCommon.AtisType.Departure) { - departureText = "DEPARTURE[color]cyan"; - } else { - enrouteText = "ENROUTE[color]cyan"; - } - - let arrText = "[ ]"; - if (store.selected !== "") { - arrText = store.selected; - if (!store.manual) { - arrText += "[s-text]"; - } - } - - const updateView = () => { - if (mcdu.page.Current === mcdu.page.AOCRequestAtis) { - let sendMessage = "SEND*[color]cyan"; - if (store.selected === "" || store.sendStatus === "SENDING") { - sendMessage = "SEND\xa0[color]cyan"; - } - - mcdu.setTemplate([ - ["AOC ATIS REQUEST"], - ["\xa0AIRPORT", "↓FORMAT FOR\xa0"], - [`${arrText}[color]cyan`, formatString], - ["", "", "-------SELECT ONE-------"], - [arrivalText, enrouteText], - [""], - [departureText], - [""], - [""], - [""], - [""], - ["\xa0RETURN TO", `${store.sendStatus}\xa0`], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - store.selected = ""; - CDUAocRequestsAtis.ShowPage(mcdu, store); - } else if (value) { - mcdu.navigationDatabaseService.activeDatabase.searchAirport(value).then((airport) => { - if (airport) { - store.selected = value; - store.manual = true; - - if (mcdu.page.Current === mcdu.page.AOCRequestAtis) { - CDUAocRequestsAtis.ShowPage(mcdu, store); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } - }); - } - }; - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = () => { - if (store.reqID !== AtsuCommon.AtisType.Arrival) { - if (!store.manual) { - store.selected = store.arrival; - } - store.requestId = AtsuCommon.AtisType.Arrival; - } - CDUAocRequestsAtis.ShowPage(mcdu, store); - }; - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - if (store.reqID !== AtsuCommon.AtisType.Departure) { - if (!store.manual) { - store.selected = store.departure; - } - store.requestId = AtsuCommon.AtisType.Departure; - } - CDUAocRequestsAtis.ShowPage(mcdu, store); - }; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - clearTimeout(labelTimeout); - CDUAocMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - store.formatID = (store.formatID + 1) % 2; - CDUAocRequestsAtis.ShowPage(mcdu, store); - }; - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = () => { - if (store.reqID !== AtsuCommon.AtisType.Enroute) { - store.requestId = AtsuCommon.AtisType.Enroute; - } - CDUAocRequestsAtis.ShowPage(mcdu, store); - }; - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = async () => { - store.sendStatus = "SENDING"; - updateView(); - - const onRequestSent = () => { - store.sendStatus = "SENT"; - if (mcdu.page.Current === mcdu.page.AOCRequestAtis) { - updateView(); - } - }; - - mcdu.atsu.receiveAocAtis(store.selected, store.requestId, onRequestSent).then((retval) => { - if (retval[0] === AtsuCommon.AtsuStatusCodes.Ok) { - retval[1].Confirmed = store.formatID === 0; - mcdu.atsu.registerMessages([retval[1]]); - store.sendStatus = ""; - if (mcdu.page.Current === mcdu.page.AOCRequestAtis) { - CDUAocRequestsAtis.ShowPage(mcdu, store); - } - - // print the message - if (store.formatID === 0) { - mcdu.atsu.printAocAtis(retval[1]); - } - } else { - mcdu.addNewAtsuMessage(retval[0]); - - if (mcdu.page.Current === mcdu.page.AOCRequestAtis) { - store.sendStatus = "FAILED"; - CDUAocRequestsAtis.ShowPage(mcdu, store); - } - } - }); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_RequestsMessage.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_RequestsMessage.js deleted file mode 100644 index bbba9bb7d3e..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_RequestsMessage.js +++ /dev/null @@ -1,79 +0,0 @@ -class CDUAocRequestsMessage { - static ShowPage(mcdu, messages, messageIndex, offset = 0) { - mcdu.clearDisplay(); - const message = messages[messageIndex]; - const lines = message.serialize(AtsuCommon.AtsuMessageSerializationFormat.FmsDisplay).split("\n"); - - // mark message as read - mcdu.atsu.messageRead(message.UniqueMessageID, true); - - const msgArrows = messages.length > 1 ? " {}" : ""; - - if (lines.length > 8) { - let up = false; - let down = false; - if (lines[offset + 1]) { - mcdu.onUp = () => { - offset += 1; - CDUAocRequestsMessage.ShowPage(mcdu, messages, messageIndex, offset); - }; - up = true; - } - if (lines[offset - 1]) { - mcdu.onDown = () => { - offset -= 1; - CDUAocRequestsMessage.ShowPage(mcdu, messages, messageIndex, offset); - }; - down = true; - } - mcdu.setArrows(up, down, false, false); - } - - let from = message.Station; - if (message.Type === AtsuCommon.AtsuMessageType.ATIS) { - from = message.Reports[0].airport; - } - - mcdu.setTemplate([ - ["AOC MSG DISPLAY"], - [`${message.Timestamp.fmsTimestamp()} FROM ${from}[color]green`, `${messageIndex + 1}/${messages.length}${msgArrows}`], - [`{small}${lines[offset] ? lines[offset] : ""}{end}`], - [`${lines[offset + 1] ? lines[offset + 1] : ""}`], - [`{small}${lines[offset + 2] ? lines[offset + 2] : ""}{end}`], - [`${lines[offset + 3] ? lines[offset + 3] : ""}`], - [`{small}${lines[offset + 4] ? lines[offset + 4] : ""}{end}`], - [`${lines[offset + 5] ? lines[offset + 5] : ""}`], - [`{small}${lines[offset + 6] ? lines[offset + 6] : ""}{end}`], - [`${lines[offset + 7] ? lines[offset + 7] : ""}`], - [`{small}${lines[offset + 8] ? lines[offset + 8] : ""}{end}`], - ["\xa0RCVD MSGS"], - [" { - const nextMesssageIndex = messageIndex + 1; - if (nextMesssageIndex < messages.length) { - CDUAocRequestsMessage.ShowPage(mcdu, messages, nextMesssageIndex); - } - }; - - mcdu.onPrevPage = () => { - const previousMesssageIndex = messageIndex - 1; - if (previousMesssageIndex >= 0) { - CDUAocRequestsMessage.ShowPage(mcdu, messages, previousMesssageIndex); - } - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAocMessagesReceived.ShowPage(mcdu); - }; - - mcdu.onRightInput[5] = () => { - mcdu.atsu.printMessage(message); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_RequestsWeather.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_RequestsWeather.js deleted file mode 100644 index 65192c912cd..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_AOC_RequestsWeather.js +++ /dev/null @@ -1,160 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUAocRequestsWeather { - static CreateDataBlock(mcdu) { - const retval = { - airports: ["", "", "", ""], - managed: [true, true, true, true], - sendStatus: "", - requestId: 0 - }; - - const activePlan = mcdu.flightPlanService.active; - - if (activePlan.originAirport) { - retval.airports[0] = activePlan.originAirport.ident; - } - - if (activePlan.destinationAirport) { - retval.airports[1] = activePlan.destinationAirport.ident; - } - - if (activePlan.alternateDestinationAirport) { - retval.airports[2] = activePlan.alternateDestinationAirport.ident; - } - - return retval; - } - - static ShowPage(mcdu, data = CDUAocRequestsWeather.CreateDataBlock(mcdu)) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.AOCRequestWeather; - let labelTimeout; - const reqTypes = [ - 'METAR', - 'TAF' - ]; - - // get the airports - const airports = ["[ ][color]green", "[ ][color]green", "[ ][color]green", "[ ][color]green"]; - for (let i = 0; i < 4; ++i) { - if (data.airports[i] !== "") { - airports[i] = data.airports[i]; - - if (data.managed[i]) { - airports[i] += "[color]cyan"; - } else { - airports[i] += "[color]green"; - } - } - } - - const updateView = () => { - if (mcdu.page.Current === mcdu.page.AOCRequestWeather) { - let sendMessage = "SEND\xa0[color]cyan"; - if (data.airports.filter((n) => n).length !== 0 && data.sendStatus !== "SENDING") { - sendMessage = "SEND*[color]cyan"; - } - - mcdu.setTemplate([ - ["AOC WEATHER REQUEST"], - ["\xa0WX TYPE", "AIRPORTS\xa0"], - [`↓${reqTypes[data.requestId]}[color]cyan`, airports[0]], - [""], - ["", airports[1]], - [""], - ["", airports[2]], - [""], - ["", airports[3]], - [""], - [""], - ["AOC MENU", `${data.sendStatus}\xa0`], - [" { - if (value === FMCMainDisplay.clrValue) { - data.airports[i] = ""; - CDUAocRequestsWeather.ShowPage(mcdu, data); - } else { - if (!/^[A-Z0-9]{4}$/.test(value)) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } else { - mcdu.navigationDatabaseService.activeDatabase.searchAirport(value).then((airport) => { - if (airport) { - data.airports[i] = value; - data.managed[i] = false; - - if (mcdu.page.Current === mcdu.page.AOCRequestWeather) { - CDUAocRequestsWeather.ShowPage(mcdu, data); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } - }); - } - } - }; - } - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onRightInput[5] = async () => { - const icaos = data.airports.filter((n) => n); - if (icaos.length === 0) { - mcdu.setScratchpadMessage(NXFictionalMessages.noAirportSpecified); - return; - } - data.sendStatus = "SENDING"; - updateView(); - - const sentRequest = () => { - data.sendStatus = "SENT"; - if (mcdu.page.Current === mcdu.page.AOCRequestWeather) { - updateView(); - } - }; - - mcdu.atsu.receiveWeather(data.requestId === 0, icaos, sentRequest).then((retval) => { - if (retval[0] === AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.atsu.registerMessages([retval[1]]); - data.sendStatus = ""; - - if (mcdu.page.Current === mcdu.page.AOCRequestWeather) { - updateView(); - } - } else { - mcdu.addNewAtsuMessage(retval[0]); - - if (mcdu.page.Current === mcdu.page.AOCRequestWeather) { - data.sendStatus = "FAILED"; - updateView(); - } - } - }); - }; - - mcdu.leftInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - data.requestId = (data.requestId + 1) % 2; - CDUAocRequestsWeather.ShowPage(mcdu, data); - }; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - clearTimeout(labelTimeout); - CDUAocMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_AtisAutoUpdate.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_AtisAutoUpdate.js deleted file mode 100644 index e7b5fc37217..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_AtisAutoUpdate.js +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUAtcAtisAutoUpdate { - static ToggleAutoUpdate(mcdu, icao, reloadPage) { - if (mcdu.atsu.atisAutoUpdateActive(icao)) { - mcdu.atsu.deactivateAtisAutoUpdate(icao).then((status) => { - if (status !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(status); - } - if (reloadPage) { - CDUAtcAtisAutoUpdate.ShowPage(mcdu); - } - }); - } else { - mcdu.atsu.activateAtisAutoUpdate(icao, AtsuCommon.AtisType.Arrival).then((status) => { - if (status !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(status); - } - if (reloadPage) { - CDUAtcAtisAutoUpdate.ShowPage(mcdu); - } - }); - } - } - - static ShowPage(mcdu, updateInProgress = false) { - mcdu.clearDisplay(); - - const activeDestinationAirport = mcdu.flightPlanService.active.destinationAirport; - const activeAlternateAirport = mcdu.flightPlanService.active.alternateDestinationAirport; - - let arrAtis = "{inop}\xa0[ ]/[ ]{end}"; - let arrAtisState = ""; - let arrAtisButton = "{cyan}ON\xa0{end}"; - let altAtis = "{inop}\xa0[ ]/[ ]{end}"; - let altAtisState = ""; - let altAtisButton = "{cyan}ON\xa0{end}"; - if (activeDestinationAirport) { - arrAtis = `{cyan}\xa0${activeDestinationAirport.ident}/ARR{end}`; - if (mcdu.atsu.atisAutoUpdateActive(activeDestinationAirport.ident)) { - arrAtisState = "\x3a ON"; - arrAtisButton = `{cyan}OFF${updateInProgress ? '\xa0' : '*'}{end}`; - } else { - arrAtisState = "\x3a OFF"; - arrAtisButton = `{cyan}ON${updateInProgress ? '\xa0' : '*'}{end}`; - } - } - if (activeAlternateAirport && activeAlternateAirport.ident) { - altAtis = `{cyan}\xa0${activeAlternateAirport.ident}/ARR{end}`; - if (mcdu.atsu.atisAutoUpdateActive(activeAlternateAirport.ident)) { - altAtisState = "\x3a ON"; - altAtisButton = "{cyan}OFF*{end}"; - } else { - altAtisState = "\x3a OFF"; - altAtisButton = "{cyan}ON*{end}"; - } - } - - mcdu.setTemplate([ - ["ATIS AUTO UPDATE"], - [""], - [""], - ["", "{cyan}SET\xa0{end}"], - [arrAtis, arrAtisButton, arrAtisState], - ["", "{cyan}SET\xa0{end}"], - [altAtis, altAtisButton, altAtisState], - [""], - [""], - [""], - [""], - ["\xa0ATIS MENU"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcAtisMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = () => { - const activeDestinationAirport = mcdu.flightPlanService.active.destinationAirport; - - if (updateInProgress === false && activeDestinationAirport) { - CDUAtcAtisAutoUpdate.ToggleAutoUpdate(mcdu, activeDestinationAirport.ident, true); - CDUAtcAtisAutoUpdate.ShowPage(mcdu, true); - } - }; - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = () => { - const activeAlternateAirport = mcdu.flightPlanService.active.alternateDestinationAirport; - - if (updateInProgress === false && activeAlternateAirport) { - CDUAtcAtisAutoUpdate.ToggleAutoUpdate(mcdu, activeAlternateAirport.ident, true); - CDUAtcAtisAutoUpdate.ShowPage(mcdu, true); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_AtisMenu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_AtisMenu.js deleted file mode 100644 index 4ecc3a686ce..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_AtisMenu.js +++ /dev/null @@ -1,243 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUAtcAtisMenu { - static CreateDataBlock(mcdu) { - const airports = [ - { icao: "", type: AtsuCommon.AtisType.Departure, requested: false, autoupdate: false }, - { icao: "", type: AtsuCommon.AtisType.Arrival, requested: false, autoupdate: false }, - { icao: "", type: AtsuCommon.AtisType.Arrival, requested: false, autoupdate: false }, - { icao: "", type: AtsuCommon.AtisType.Arrival, requested: false, autoupdate: false } - ]; - - const activePlan = mcdu.flightPlanService.active; - - if (activePlan.originAirport) { - airports[0].icao = activePlan.originAirport.ident; - airports[0].autoupdate = mcdu.atsu.atisAutoUpdateActive(airports[0].icao); - } - - if (activePlan.destinationAirport) { - airports[1].icao = activePlan.destinationAirport.ident; - airports[1].autoupdate = mcdu.atsu.atisAutoUpdateActive(airports[1].icao); - } - - if (activePlan.alternateDestinationAirport) { - airports[2].icao = activePlan.alternateDestinationAirport.ident; - airports[2].autoupdate = mcdu.atsu.atisAutoUpdateActive(airports[2].icao); - } - - return airports; - } - - static InterpretLSK(mcdu, value, airports, idx, updateAtisPrintInProgress) { - if (!value) { - const reports = mcdu.atsu.atisReports(airports[idx].icao); - if (reports.length !== 0) { - CDUAtcReportAtis.ShowPage(mcdu, `${airports[idx].icao}/${airports[idx].type === AtsuCommon.AtisType.Departure ? "DEP" : "ARR"}`, reports, 0); - } - } else if (value === FMCMainDisplay.clrValue) { - airports[idx].icao = ""; - airports[idx].requested = false; - airports[idx].autoupdate = false; - CDUAtcAtisMenu.ShowPage(mcdu, airports, updateAtisPrintInProgress); - } else { - // validate the generic format and if the airport is described by four characters - const entries = value.split("/"); - if (entries.length !== 2 || !/^[A-Z0-9]{4}$/.test(entries[0])) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - return; - } - - // validate the ATIS type - let type = null; - if (entries[1] === "ARR" || entries[1] === "AR" || entries[1] === "A") { - type = AtsuCommon.AtisType.Arrival; - } else if (entries[1] === "DEP" || entries[1] === "DDE" || entries[1] === "D") { - type = AtsuCommon.AtisType.Departure; - } - if (type === null) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - return; - } - - // check that this is setup is not already set - const currentIndex = airports.findIndex((elem) => elem.icao === entries[0] && elem.type === type); - if (currentIndex !== -1 && idx !== currentIndex) { - mcdu.setScratchpadMessage(NXSystemMessages.arptTypeAlreadyInUse); - return; - } - - // validate the airport - mcdu.navigationDatabaseService.activeDatabase.searchAirport(entries[0]).then((airport) => { - if (airport) { - airports[idx].autoupdate = mcdu.atsu.atisAutoUpdateActive(entries[0]); - airports[idx].icao = entries[0]; - airports[idx].type = type; - CDUAtcAtisMenu.ShowPage(mcdu, airports, updateAtisPrintInProgress); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } - }); - } - } - - static InterpretRSK(mcdu, airports, idx, updateAtisPrintInProgress) { - if (airports[idx].icao === "") { - return; - } - - if (airports[idx].autoupdate) { - const reports = mcdu.atsu.atisReports(airports[idx].icao); - if (reports.length !== 0) { - CDUAtcAtisAutoUpdate.ToggleAutoUpdate(mcdu, airports[idx].icao, false); - airports[0].autoupdate = false; - CDUAtcAtisMenu.ShowPage(mcdu, airports, updateAtisPrintInProgress); - } else if (!airports[idx].requested) { - CDUAtcAtisMenu.RequestAtis(mcdu, airports, idx); - } - } else if (!airports[idx].requested) { - CDUAtcAtisMenu.RequestAtis(mcdu, airports, idx); - } - } - - static CreateLineData(mcdu, airport) { - if (airport.icao !== "") { - const reports = mcdu.atsu.atisReports(airport.icao); - - let prefix = "\xa0"; - let middle = ""; - if (reports.length !== 0) { - middle = `\xa0\xa0${reports[0].Information} ${reports[0].Timestamp.mailboxTimestamp()}`; - middle = middle.substring(0, middle.length - 1); - prefix = "{white}<{end}"; - } - - let suffix = "\xa0"; - if (!airport.requested) { - suffix = "*"; - } - - let right = "SEND*"; - let rightTitle = "REQ\xa0"; - const left = `{cyan}${prefix}${airport.icao}/${airport.type === AtsuCommon.AtisType.Arrival ? "ARR" : "DEP"}{end}`; - - if (airport.autoupdate) { - rightTitle = "UPDATE\xa0"; - if (reports.length !== 0) { - right = "CANCEL*"; - } else { - right = `SEND${suffix}`; - } - } else { - right = `SEND${suffix}`; - } - - return [left, middle, rightTitle, right]; - } else { - return ["{cyan}\xa0[ ]/[ ]{end}", "", "REQ\xa0", "SEND\xa0"]; - } - } - - static RequestAtis(mcdu, airports, idx, updateAtisPrintInProgress) { - if (airports[idx].icao !== "" && !airports[idx].requested) { - airports[idx].requested = true; - - mcdu.atsu.receiveAtcAtis(airports[idx].icao, airports[idx].type).then((response) => { - if (response !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(response); - } - - airports[idx].requested = false; - if (mcdu.page.Current === mcdu.page.ATCAtis) { - CDUAtcAtisMenu.ShowPage(mcdu, airports, updateAtisPrintInProgress); - } - }); - - CDUAtcAtisMenu.ShowPage(mcdu, airports, updateAtisPrintInProgress); - } - } - - static ShowPage(mcdu, airports = CDUAtcAtisMenu.CreateDataBlock(mcdu), updateAtisPrintInProgress = false) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCAtis; - - const lines = [ - CDUAtcAtisMenu.CreateLineData(mcdu, airports[0]), - CDUAtcAtisMenu.CreateLineData(mcdu, airports[1]), - CDUAtcAtisMenu.CreateLineData(mcdu, airports[2]), - CDUAtcAtisMenu.CreateLineData(mcdu, airports[3]) - ]; - - let printTitle = "PRINT:MANUAL\xa0"; - let printButton = `SET AUTO${updateAtisPrintInProgress ? '\xa0' : '*'}`; - if (mcdu.atsu.printAtisReportsPrint()) { - printTitle = "PRINT:AUTO\xa0"; - printButton = `SET MANUAL${updateAtisPrintInProgress ? '\xa0' : '*'}`; - } - - mcdu.setTemplate([ - ["ATIS MENU"], - ["\xa0ARPT/TYPE", `{cyan}${lines[0][2]}{end}`], - [lines[0][0], `{cyan}${lines[0][3]}{end}`, `{small}${lines[0][1]}{end}`], - ["", `{cyan}${lines[1][2]}{end}`], - [lines[1][0], `{cyan}${lines[1][3]}{end}`, `{small}${lines[1][1]}{end}`], - ["", `{cyan}${lines[2][2]}{end}`], - [lines[2][0], `{cyan}${lines[2][3]}{end}`, `{small}${lines[2][1]}{end}`], - ["", `{cyan}${lines[3][2]}{end}`], - [lines[3][0], `{cyan}${lines[3][3]}{end}`, `{small}${lines[3][1]}{end}`], - ["", "AUTO\xa0"], - ["", "UPDATE>"], - ["\xa0ATC MENU", printTitle], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[i] = (value) => { - CDUAtcAtisMenu.InterpretLSK(mcdu, value, airports, i); - }; - } - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - for (let i = 0; i < 4; ++i) { - mcdu.rightInputDelay[i] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[i] = () => { - CDUAtcAtisMenu.InterpretRSK(mcdu, airports, i); - }; - } - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - CDUAtcAtisAutoUpdate.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (updateAtisPrintInProgress === false) { - mcdu.atsu.togglePrintAtisReports().then((status) => { - if (status !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(status); - } - CDUAtcAtisMenu.ShowPage(mcdu, airports); - }); - CDUAtcAtisMenu.ShowPage(mcdu, airports, true); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ClearanceReq.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ClearanceReq.js deleted file mode 100644 index 0eae34d787a..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ClearanceReq.js +++ /dev/null @@ -1,107 +0,0 @@ -class CDUAtcClearanceReq { - static CreateDataBlock() { - return { - clearance: false - }; - } - - static CanSendData(data) { - return data.clearance; - } - - static CreateRequest(mcdu) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink["DM25"][1].deepCopy()); - retval.Content[0].Content[0].Value = "DEPARTURE"; - return retval; - } - - static ShowPage(mcdu, title, data = CDUAtcClearanceReq.CreateDataBlock()) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCGroundRequest; - - let addText = "ADD TEXT\xa0"; - let clearance = "{cyan}{{end}CLEARANCE"; - let transfer = ["{cyan}XFR TO\xa0{end}", "{cyan}DCDU\xa0{end}"]; - let erase = ["\xa0ALL FIELDS", "\xa0ERASE"]; - if (mcdu.atsu.fansMode() !== AtsuCommon.FansMode.FansA) { - clearance = " { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (mcdu.atsu.fansMode() !== AtsuCommon.FansMode.FansA) { - CDUAtcDepartReq.ShowPage1(mcdu); - return; - } else if (value === FMCMainDisplay.clrValue) { - data.clearance = false; - } else { - data.clearance = true; - } - - CDUAtcClearanceReq.ShowPage(mcdu, title, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcClearanceReq.ShowPage(mcdu, title); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA && CDUAtcClearanceReq.CanSendData(data)) { - const message = CDUAtcClearanceReq.CreateRequest(mcdu); - CDUAtcTextFansA.ShowPage1(mcdu, [message]); - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA && CDUAtcClearanceReq.CanSendData(data)) { - const message = CDUAtcClearanceReq.CreateRequest(mcdu); - mcdu.atsu.registerMessages([message]); - CDUAtcClearanceReq.ShowPage(mcdu, title); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_Connection.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_Connection.js deleted file mode 100644 index bc88bd5513b..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_Connection.js +++ /dev/null @@ -1,54 +0,0 @@ -class CDUAtcConnection { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCConnection; - - mcdu.setTemplate([ - ["\xa0CONNECTION"], - [""], - [""], - ["\xa0ATC MENU"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - CDUAtcConnectionNotification.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - CDUAtcConnectionStatus.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcMaxUplinkDelay.ShowPage(mcdu); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.keyNotActive); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ConnectionNotification.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ConnectionNotification.js deleted file mode 100644 index 8380c8794b3..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ConnectionNotification.js +++ /dev/null @@ -1,176 +0,0 @@ -class CDUAtcConnectionNotification { - static ShowPage(mcdu, store = {"atcCenter": "", "logonAllowed": false, "loginState": 0}) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCNotification; - - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.ATCNotification) { - CDUAtcConnectionNotification.ShowPage(mcdu, store); - } - }, mcdu.PageTimeout.Default); - - let flightNo = "-------[color]white"; - let atcStation = "____[color]amber"; - let atcStationAvail = false; - let flightNoAvail = false; - let fromToAvail = false; - let centerTitleLeft = "\xa0ATC CENTER[color]white"; - let centerTitleRight = ""; - let notificationStatus = ""; - - if (store["loginState"] === 1) { - centerTitleLeft = "\xa0ATC CENTER-NOTIFYING[color]white"; - } else if (store["loginState"] === 2) { - centerTitleLeft = "\xa0ATC-CENTER-[color]white"; - centerTitleRight = "NOTIF FAILED[color]red"; - } - if (store["atcCenter"] !== "" && store["loginState"] === 0) { - atcStation = `${store["atcCenter"]}[color]cyan`; - atcStationAvail = true; - } - if (mcdu.atsu.flightNumber().length !== 0) { - flightNo = mcdu.atsu.flightNumber() + "[color]green"; - flightNoAvail = true; - } - if (mcdu.flightPlanService.active.destinationAirport && mcdu.flightPlanService.active.destinationAirport.ident) { - fromToAvail = true; - } - - let notifyButton; - if (atcStationAvail && flightNoAvail && fromToAvail && store["loginState"] === 0) { - notifyButton = "NOTIFY*[color]cyan"; - store["logonAllowed"] = true; - } else { - notifyButton = "NOTIFY\xa0[color]cyan"; - store["logonAllowed"] = false; - } - if (!flightNoAvail || !fromToAvail) { - notificationStatus = "NOTIFICATION UNAVAILABLE"; - } - - let linesColor; - if (atcStationAvail && flightNoAvail && fromToAvail) { - linesColor = "[color]cyan"; - } else { - linesColor = "[color]white"; - } - - let notificationMessage = ""; - if (mcdu.atsu.logonInProgress()) { - const seconds = Math.floor(mcdu.atsu.nextStationNotificationTime()); - const hours = Math.floor(seconds / 3600); - const minutes = Math.floor((seconds - hours * 3600) / 60); - const zeroPad = (num, places) => String(num).padStart(places, 0); - - // check if the page is loaded again - if (store["atcCenter"] !== mcdu.atsu.nextStation()) { - store["atcCenter"] = mcdu.atsu.nextStation(); - } - - notificationMessage = `${store["atcCenter"]} NOTIFIED ${`${zeroPad(hours, 2)}${zeroPad(minutes, 2)}Z`}[color]green`; - } else if (mcdu.atsu.currentStation() !== '') { - notificationMessage = `${mcdu.atsu.currentStation()}[color]green`; - } - - mcdu.setTemplate([ - ["NOTIFICATION"], - ["\xa0ATC FLT NBR"], - [flightNo], - [centerTitleLeft, centerTitleRight], - [atcStation, notifyButton, `---------${linesColor}`], - [""], - [""], - [""], - [notificationMessage], - [notificationStatus], - [""], - ["\xa0CONNECTION", "CONNECTION\xa0"], - [""] - ]); - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (store["loginState"] === 1 && mcdu.atsu.nextStation() !== store["atcCenter"]) { - mcdu.setScratchpadMessage(NXSystemMessages.systemBusy); - return; - } - - store["loginState"] = 0; - if (/^[A-Z0-9]{4}$/.test(value) === false) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } else if (mcdu.atsu.flightNumber().length === 0) { - mcdu.setScratchpadMessage(NXFictionalMessages.fltNbrMissing); - } else { - store["atcCenter"] = ""; - - mcdu.atsu.isRemoteStationAvailable(value).then((code) => { - if (code !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(code); - store["atcCenter"] = ""; - } else { - store["atcCenter"] = value; - } - - if (mcdu.page.Current === mcdu.page.ATCNotification) { - CDUAtcConnectionNotification.ShowPage(mcdu, store); - } - }); - } - - CDUAtcConnectionNotification.ShowPage(mcdu, store); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcConnection.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = async () => { - if (store["logonAllowed"] === true) { - store["loginState"] = 1; - - mcdu.atsu.logon(store["atcCenter"]).then((code) => { - if (code === AtsuCommon.AtsuStatusCodes.Ok) { - // check if the login was successful - const interval = setInterval(() => { - if (!mcdu.atsu.logonInProgress()) { - if (mcdu.atsu.currentStation() === store["atcCenter"]) { - store["loginState"] = 0; - } else { - store["loginState"] = 2; - } - - store["atcCenter"] = ""; - clearInterval(interval); - - if (mcdu.page.Current === mcdu.page.ATCNotification) { - CDUAtcConnectionNotification.ShowPage(mcdu, store); - } - } - }, 5000); - } else { - mcdu.addNewAtsuMessage(code); - } - }); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.mandatoryFields); - } - - CDUAtcConnectionNotification.ShowPage(mcdu, store); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - CDUAtcConnectionStatus.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ConnectionStatus.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ConnectionStatus.js deleted file mode 100644 index be788967b3d..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ConnectionStatus.js +++ /dev/null @@ -1,91 +0,0 @@ -class CDUAtcConnectionStatus { - static ShowPage(mcdu, store = { "disconnectInProgress": false, "disconnectAvail": false, "disconnectConfirm": false }) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCConnectionStatus; - - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.ATCConnectionStatus) { - CDUAtcConnectionStatus.ShowPage(mcdu, store); - } - }, mcdu.PageTimeout.Default); - - let currentStation = "-----------[color]white"; - let atcDisconnectHeadline = "ALL ATC\xa0[color]cyan"; - let atcDisconnect = "DISCONNECT\xa0[color]cyan"; - if (!store["disconnectInProgress"]) { - if (mcdu.atsu.currentStation() !== "") { - currentStation = `${mcdu.atsu.currentStation()}[color]green`; - store["disconnectAvail"] = true; - - if (!store["disconnectConfirm"]) { - atcDisconnect = "DISCONNECT*[color]cyan"; - } else { - atcDisconnectHeadline = "DISCONNECT\xa0[color]amber"; - atcDisconnect = "CONFIRM*[color]amber"; - } - } else { - store["disconnectAvail"] = false; - } - } - - let nextStation = "-----------"; - if (mcdu.atsu.nextStation() !== "") { - nextStation = `${mcdu.atsu.nextStation()}[color]green`; - } - - mcdu.setTemplate([ - ["CONNECTION STATUS"], - ["\xa0ACTIVE ATC"], - [currentStation], - ["\xa0NEXT ATC", atcDisconnectHeadline], - [nextStation, atcDisconnect], - [""], - [""], - ["-------ADS-C: ARMED-------"], - ["\xa0SET OFF[color]inop"], - [""], - ["", "ADS-C DETAIL>[color]inop"], - ["\xa0CONNECTION", ""], - [""] - ]); - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcConnection.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = () => { - if (!store["disconnectAvail"]) { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else if (!store["disconnectConfirm"]) { - store["disconnectConfirm"] = true; - CDUAtcConnectionStatus.ShowPage(mcdu, store); - } else if (!store["disconnectInProgress"]) { - store["disconnectInProgress"] = true; - store["disconnectAvail"] = false; - CDUAtcConnectionStatus.ShowPage(mcdu, store); - - mcdu.atsu.logoff().then((code) => { - store["disconnectInProgress"] = false; - if (code !== AtsuCommon.AtsuStatusCodes.Ok) { - store["disconnectAvail"] = true; - mcdu.addNewAtsuMessage(code); - } else { - CDUAtcConnectionStatus.ShowPage(mcdu, store); - } - }); - } - }; - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - CDUAtcConnectionNotification.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_DepartReq.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_DepartReq.js deleted file mode 100644 index ee31f533d85..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_DepartReq.js +++ /dev/null @@ -1,282 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUAtcDepartReq { - static CreateDataBlock() { - return { - firstCall: true, - callsign: "", - station: "", - stationManual: false, - from: "", - to: "", - atis: "", - gate: "", - freetext: [ "", "", "", "", "", "" ] - }; - } - - static CanSendData(store) { - return store.callsign !== "" && store.station !== "" && store.from !== "" && store.to !== "" && store.atis !== ""; - } - - static CreateMessage(store) { - const retval = new AtsuCommon.DclMessage(); - - retval.Callsign = store.callsign; - retval.Origin = store.from; - retval.Destination = store.to; - retval.AcType = "A20N"; - retval.Atis = store.atis; - retval.Gate = store.gate; - retval.Freetext = store.freetext.filter((n) => n); - retval.Station = store.station; - - return retval; - } - - static ShowPage1(mcdu, store = CDUAtcDepartReq.CreateDataBlock()) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCDepartReq; - - if (store.firstCall && store.callsign === "") { - if (mcdu.atsu.flightNumber().length !== 0) { - store.callsign = mcdu.atsu.flightNumber(); - } - } - - const activePlan = mcdu.flightPlanService.active; - - if (store.firstCall && store.from === "") { - if (activePlan.originAirport) { - store.from = activePlan.originAirport.ident; - } - } - - if (store.firstCall && store.to === "") { - if (activePlan.destinationAirport) { - store.to = activePlan.destinationAirport.ident; - } - } - - if (store.firstCall && store.station === "") { - if (mcdu.atsu.currentStation() !== "") { - store.station = mcdu.atsu.currentStation(); - } - } - - store.firstCall = false; - - let flightNo = "--------"; - let fromTo = "{amber}____/____{end}"; - const atis = new CDU_SingleValueField(mcdu, - "string", - store.atis, - { - clearable: true, - emptyValue: "{amber}_{end}", - suffix: "[color]cyan", - maxLength: 1, - isValid: ((value) => { - return /^[A-Z()]*$/.test(value) === true; - }) - }, - (value) => { - store.atis = value; - CDUAtcDepartReq.ShowPage1(mcdu, store); - } - ); - const gate = new CDU_SingleValueField(mcdu, - "string", - store.gate, - { - clearable: true, - emptyValue: "{cyan}[\xa0\xa0\xa0\xa0]{end}", - suffix: "[color]cyan", - maxLength: 4 - }, - (value) => { - store.gate = value; - CDUAtcDepartReq.ShowPage1(mcdu, store); - } - ); - const freetext = new CDU_SingleValueField(mcdu, - "string", - store.freetext[0], - { - clearable: store.freetext[0].length !== 0, - emptyValue: "{cyan}[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0]{end}", - suffix: "[color]white", - maxLength: 22 - }, - (value) => { - store.freetext[0] = value; - CDUAtcDepartReq.ShowPage1(mcdu, store); - } - ); - - if (store.callsign) { - flightNo = `{green}${store.callsign}{end}`; - } - if (store.from !== "" && store.to !== "") { - fromTo = `{cyan}${store.from}/${store.to}{end}`; - - const atisReports = mcdu.atsu.atisReports(store.from); - if (atisReports.length !== 0 && atisReports[0].Information !== "") { - store.atis = atisReports[0].Information; - atis.setValue(store.atis); - } - } - - let station = "{amber}____{end}"; - if (store.station !== "") { - station = `{cyan}${store.station}{end}`; - if (!store.stationManual) { - station = `{small}${station}{end}`; - } - } - - // check if all required information are available to prepare the PDC message - let reqDisplButton = "{cyan}DCDU\xa0{end}"; - if (CDUAtcDepartReq.CanSendData(store)) { - reqDisplButton = "{cyan}DCDU*{end}"; - } - - mcdu.setTemplate([ - ["DEPART REQ"], - ["\xa0ATC FLT NBR", "A/C TYPE\xa0"], - [flightNo, "{cyan}A20N{end}"], - ["\xa0FROM/TO", "STATION\xa0"], - [fromTo, station], - ["\xa0GATE", "ATIS CODE\xa0"], - [gate, atis], - ["-------FREE TEXT--------"], - [freetext], - ["", "MORE\xa0"], - ["", "FREE TEXT>"], - ["\xa0GROUND REQ", "{cyan}XFR TO\xa0{end}"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - store.from = ""; - store.to = ""; - CDUAtcDepartReq.ShowPage1(mcdu, store); - } else if (value) { - const airports = value.split("/"); - if (airports.length !== 2 || !/^[A-Z0-9]{4}$/.test(airports[0]) || !/^[A-Z0-9]{4}$/.test(airports[1])) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } else { - mcdu.navigationDatabaseService.activeDatabase.searchAirport(airports[0]).then((from) => { - mcdu.navigationDatabaseService.activeDatabase.searchAirport(airports[1]).then((to) => { - if (from.ident && to.ident) { - store.from = from.ident; - store.to = to.ident; - CDUAtcDepartReq.ShowPage1(mcdu, store); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } - }); - }); - } - } - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - CDUAtcDepartReq.ShowPage2(mcdu, store); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcClearanceReq.ShowPage(mcdu, "GROUND"); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = async (value) => { - if (value === FMCMainDisplay.clrValue) { - store.station = ""; - } else if (/^[A-Z0-9]{4}$/.test(value)) { - mcdu.atsu.isRemoteStationAvailable(value).then((code) => { - if (code !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(code); - } else { - store.station = value; - store.stationManual = true; - } - - if (mcdu.page.Current === mcdu.page.ATCDepartReq) { - CDUAtcDepartReq.ShowPage1(mcdu, store); - } - }); - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcDepartReq.CanSendData(store)) { - mcdu.atsu.registerMessages([CDUAtcDepartReq.CreateMessage(store)]); - CDUAtcDepartReq.ShowPage1(mcdu); - } - }; - } - - static ShowPage2(mcdu, store) { - mcdu.clearDisplay(); - - const freetextLines = []; - for (let i = 0; i < 5; ++i) { - freetextLines.push(new CDU_SingleValueField(mcdu, - "string", - store.freetext[i + 1], - { - clearable: store.freetext[i + 1].length !== 0, - emptyValue: "{cyan}[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0]{end}", - suffix: "[color]white", - maxLength: 22 - }, - (value) => { - store.freetext[i + 1] = value; - CDUAtcDepartReq.ShowPage2(mcdu, store); - } - )); - } - - // define the template - mcdu.setTemplate([ - ["FREE TEXT"], - [""], - [freetextLines[0]], - [""], - [freetextLines[1]], - [""], - [freetextLines[2]], - [""], - [freetextLines[3]], - [""], - [freetextLines[4]], - ["\xa0DEPART REQ"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcDepartReq.ShowPage1(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_FlightReq.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_FlightReq.js deleted file mode 100644 index f176835f51c..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_FlightReq.js +++ /dev/null @@ -1,112 +0,0 @@ -class CDUAtcFlightReq { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCFlightRequest; - - let procedure = ""; - let freeText = ""; - let contact = ""; - let clearance = ""; - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - procedure = ""], - [""], - [""], - [""], - [freeText], - [""], - ["", clearance], - ["\xa0ATC MENU"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcLatRequestFansA.ShowPage1(mcdu); - } else { - CDUAtcLatRequestFansB.ShowPage(mcdu); - } - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = () => { - CDUAtcSpeedRequest.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcProcedureRequest.ShowPage(mcdu); - } - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcTextFansA.ShowPage1(mcdu); - } - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcVertRequestFansA.ShowPage1(mcdu); - } else { - CDUAtcVertRequestFansB.ShowPage(mcdu); - } - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcContactRequest.ShowPage(mcdu); - } - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = () => { - CDUAtcOceanicReq.ShowPage1(mcdu); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcClearanceReq.ShowPage(mcdu, "CLEARANCE"); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MaxUplinkDelay.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MaxUplinkDelay.js deleted file mode 100644 index 65c43099c8a..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MaxUplinkDelay.js +++ /dev/null @@ -1,74 +0,0 @@ -class CDUAtcMaxUplinkDelay { - static ShowPage(mcdu, updateInProgress = false) { - mcdu.clearDisplay(); - - let activeAtc = "----"; - if (mcdu.atsu.currentStation() !== "") { - activeAtc = mcdu.atsu.currentStation(); - } - - let currentDelay = "\xa0NONE[color]cyan"; - if (mcdu.atsu.maxUplinkDelay !== -1) { - currentDelay = `\xa0${mcdu.atsu.maxUplinkDelay}[color]cyan`; - } - - mcdu.setTemplate([ - ["MAX UPLINK DELAY"], - [""], - [""], - ["MODIFY ONLY ON DEMAND OF"], - [`ACTIVE ATC : {green}${activeAtc}{end}`], - [""], - [""], - ["\xa0MAX UPLINK DELAY"], - [currentDelay], - [""], - [""], - ["\xa0ATC MENU"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else if (updateInProgress === false) { - if (value === FMCMainDisplay.clrValue) { - mcdu.atsu.setMaxUplinkDelay(-1).then((status) => { - if (status !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(status); - } - CDUAtcMaxUplinkDelay.ShowPage(mcdu); - }); - CDUAtcMaxUplinkDelay.ShowPage(mcdu, true); - } else if (value) { - if (/^[0-9]{3}(S)*$/.test(value)) { - const delay = parseInt(value.replace("S", "")); - if (delay < 5 || delay > 999) { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - } else { - mcdu.atsu.setMaxUplinkDelay(delay).then((status) => { - if (status !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(status); - } - CDUAtcMaxUplinkDelay.ShowPage(mcdu); - }); - CDUAtcMaxUplinkDelay.ShowPage(mcdu, true); - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } - } - } - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_Menu.js deleted file mode 100644 index 81f26fd537f..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_Menu.js +++ /dev/null @@ -1,126 +0,0 @@ -class CDUAtcMenu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCMenu; - mcdu.activeSystem = 'ATSU'; - - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.ATCMenu) { - CDUAtcMenu.ShowPage(mcdu); - } - }, mcdu.PageTimeout.Slow); - - let modif = ""; - if (mcdu.atsu.modificationMessage) { - modif = "MODIFY>"; - } - - mcdu.setTemplate([ - ["ATC MENU"], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - ["[color]amber"] - ]); - - mcdu.leftInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = () => { - CDUAtcClearanceReq.ShowPage(mcdu, "GROUND"); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - CDUAtcMessagesRecord.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = () => { - CDUAtcMessageMonitoring.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcConnection.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtsuMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcUsualRequestFansA.ShowPage(mcdu); - } else { - CDUAtcUsualRequestFansB.ShowPage(mcdu); - } - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = () => { - CDUAtcAtisMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcReports.ShowPage(mcdu); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.keyNotActive); - } - }; - - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = () => { - if (mcdu.atsu.modificationMessage) { - CDUAtcMessageModify.ShowPage(mcdu, mcdu.atsu.modificationMessage); - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcEmergencyFansA.ShowPage1(mcdu); - } else { - CDUAtcEmergencyFansB.ShowPage(mcdu); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_Message.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_Message.js deleted file mode 100644 index 033b1ebc1b2..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_Message.js +++ /dev/null @@ -1,118 +0,0 @@ -class CDUAtcMessage { - static TranslateCpdlcResponse(message) { - let retval; - - switch (message.Content[0].TypeId) { - case "DM0": - retval = "WILC"; - break; - case "UM0": - case "DM1": - retval = "UNBL"; - break; - case "UM1": - case "DM2": - retval = "STBY"; - break; - case "UM3": - case "DM3": - retval = "ROGR"; - break; - case "UM4": - case "DM4": - retval = "AFRM"; - break; - case "UM5": - case "DM5": - retval = "NEG"; - break; - default: - return ""; - } - - let color = '{cyan}'; - if (message.ComStatus === AtsuCommon.AtsuMessageComStatus.Failed) { - color = '{red}'; - } - retval = `${color}{small}${retval}{end}{end}`; - - return retval; - } - - static ShowPage(mcdu, messages, messageIndex, messageList, offset = 0) { - mcdu.clearDisplay(); - const message = messages[messageIndex]; - const lines = message.serialize(AtsuCommon.AtsuMessageSerializationFormat.FmsDisplay).split("\n"); - - // mark message as read - mcdu.atsu.messageRead(message.UniqueMessageID, false); - - const msgArrows = messages.length > 1 ? " {}" : ""; - - if (lines.length > 8) { - let up = false; - let down = false; - if (lines[offset + 1]) { - mcdu.onUp = () => { - offset += 1; - CDUAtcMessage.ShowPage(mcdu, messages, messageIndex, messageList, offset); - }; - up = true; - } - if (lines[offset - 1]) { - mcdu.onDown = () => { - offset -= 1; - CDUAtcMessage.ShowPage(mcdu, messages, messageIndex, messageList, offset); - }; - down = true; - } - mcdu.setArrows(up, down, false, false); - } - - mcdu.setTemplate([ - ["ATC MSG DISPLAY"], - ["", `${messageIndex + 1}/${messages.length}${msgArrows}`], - [`{small}${lines[offset] ? lines[offset] : ""}{end}`, `${offset === 0 ? CDUAtcMessage.TranslateCpdlcResponse(message) : ""}`], - [`${lines[offset + 1] ? lines[offset + 1] : ""}`], - [`{small}${lines[offset + 2] ? lines[offset + 2] : ""}{end}`], - [`${lines[offset + 3] ? lines[offset + 3] : ""}`], - [`{small}${lines[offset + 4] ? lines[offset + 4] : ""}{end}`], - [`${lines[offset + 5] ? lines[offset + 5] : ""}`], - [`{small}${lines[offset + 6] ? lines[offset + 6] : ""}{end}`], - [`${lines[offset + 7] ? lines[offset + 7] : ""}`], - [`{small}${lines[offset + 8] ? lines[offset + 8] : ""}{end}`], - [`\xa0${messageList ? "MSG RECORD" : "MONITORED MSG"}`], - [" { - const nextMesssageIndex = messageIndex + 1; - if (nextMesssageIndex < messages.length) { - CDUAtcMessage.ShowPage(mcdu, messages, messageList, nextMesssageIndex); - } - }; - - mcdu.onPrevPage = () => { - const previousMesssageIndex = messageIndex - 1; - if (previousMesssageIndex >= 0) { - CDUAtcMessage.ShowPage(mcdu, messages, messageList, previousMesssageIndex); - } - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - if (messageList) { - CDUAtcMessagesRecord.ShowPage(mcdu); - } else { - CDUAtcMessageMonitoring.ShowPage(mcdu); - } - }; - - mcdu.onRightInput[5] = () => { - mcdu.atsu.printMessage(message); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MessageModify.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MessageModify.js deleted file mode 100644 index 04e60a0ed59..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MessageModify.js +++ /dev/null @@ -1,547 +0,0 @@ -const ModifyLookupTable = { - UM132: [{ - response: "DM33", - text: "PRESENT POSITION", - type: AtsuCommon.CpdlcMessageContentType.Position, - textIndex: null, - valueIndex: 0, - emptyLength: 5 - }], - UM133: [{ - response: "DM32", - text: "PRESENT LEVEL", - type: AtsuCommon.CpdlcMessageContentType.Level, - textIndex: null, - valueIndex: 0, - emptyLength: 5 - }], - UM134: [{ - response: "DM34", - text: "PRESENT SPEED", - type: AtsuCommon.CpdlcMessageContentType.Speed, - textIndex: null, - valueIndex: 0, - emptyLength: 4 - }], - UM135: [{ - response: "DM38", - text: "ASSIGNED LEVEL", - type: AtsuCommon.CpdlcMessageContentType.Level, - textIndex: null, - valueIndex: 0, - emptyLength: 5 - }], - UM136: [{ - response: "DM39", - text: "ASSIGNED SPEED", - type: AtsuCommon.CpdlcMessageContentType.Speed, - textIndex: null, - valueIndex: 0, - emptyLength: 4 - }], - UM137: [{ - response: "DM45", - text: "ASSIGNED ROUTE", - type: AtsuCommon.CpdlcMessageContentType.Freetext, - textIndex: null, - valueIndex: 0, - emptyLength: 6 - }], - UM138: [{ - response: "DM46", - text: "REPORTED TIME", - type: AtsuCommon.CpdlcMessageContentType.Time, - textIndex: null, - valueIndex: 0, - emptyLength: 4 - }], - UM139: [{ - response: "DM45", - text: "REPORTED WAYPOINT", - type: AtsuCommon.CpdlcMessageContentType.Position, - textIndex: null, - valueIndex: 0, - emptyLength: 5 - }], - UM140: [{ - response: "DM42", - text: "NEXT WAYPOINT", - type: AtsuCommon.CpdlcMessageContentType.Position, - textIndex: null, - valueIndex: 0, - emptyLength: 5 - }], - UM141: [{ - response: "DM43", - text: "NEXT WAYPOINT ETA", - type: AtsuCommon.CpdlcMessageContentType.Time, - textIndex: null, - valueIndex: 0, - emptyLength: 4 - }], - UM142: [{ - response: "DM44", - text: "ENSUING WAYPOINT", - type: AtsuCommon.CpdlcMessageContentType.Position, - textIndex: null, - valueIndex: 0, - emptyLength: 5 - }], - UM144: [{ - response: "DM47", - text: "PRESENT SQUAWK", - type: AtsuCommon.CpdlcMessageContentType.Squawk, - textIndex: null, - valueIndex: 0, - emptyLength: 4 - }], - UM145: [{ - response: "DM35", - text: "PRESENT HEADING", - type: AtsuCommon.CpdlcMessageContentType.Degree, - textIndex: null, - valueIndex: 0, - emptyLength: 3 - }], - UM146: [{ - response: "DM36", - text: "PRESENT GROUND TRACK", - type: AtsuCommon.CpdlcMessageContentType.Degree, - textIndex: null, - valueIndex: 0, - emptyLength: 3 - }], - UM148: [{ - response: "DM81", - text: "CAN %s AT", - type: AtsuCommon.CpdlcMessageContentType.Time, - textIndex: 0, - valueIndex: 1, - emptyLength: 5 - }, { - response: "DM67", - text: "CAN %s NOW", - type: AtsuCommon.CpdlcMessageContentType.Unknown, - textIndex: 0, - valueIndex: null, - emptyLength: 0, - freetext: "WE CAN ACCEPT %s NOW" - }, { - response: "DM82", - text: "CANNOT %s", - type: AtsuCommon.CpdlcMessageContentType.Unknown, - textIndex: 0, - valueIndex: null, - emptyLength: 0 - } - ], - UM151: [{ - response: "DM83", - text: "CAN %s AT", - type: AtsuCommon.CpdlcMessageContentType.Time, - textIndex: 0, - valueIndex: 1, - emptyLength: 5 - }, { - response: "DM67", - text: "CAN %s NOW", - type: AtsuCommon.CpdlcMessageContentType.Unknown, - textIndex: 0, - valueIndex: null, - emptyLength: 0, - freetext: "WE CAN ACCEPT %s NOW" - }, { - response: "DM94", - text: "CANNOT %s", - type: AtsuCommon.CpdlcMessageContentType.Unknown, - textIndex: 0, - valueIndex: null, - emptyLength: 0 - } - ], - UM152: [{ - response: "DM85", - text: "CAN %s AT", - type: AtsuCommon.CpdlcMessageContentType.Time, - textIndex: 0, - valueIndex: 1, - emptyLength: 5 - }, { - response: "DM67", - text: "CAN %s NOW", - type: AtsuCommon.CpdlcMessageContentType.Unknown, - textIndex: 0, - valueIndex: null, - emptyLength: 0, - freetext: "WE CAN ACCEPT %s NOW" - }, { - response: "DM86", - text: "CANNOT %s", - type: AtsuCommon.CpdlcMessageContentType.Unknown, - textIndex: 0, - valueIndex: null, - emptyLength: 0 - } - ], - UM181: [{ - response: "DM67", - text: "DISTANCE TO %s", - type: AtsuCommon.CpdlcMessageContentType.Distance, - textIndex: 0, - valueIndex: 0, - emptyLength: 3, - freetext: "DISTANCE TO %s %v" - }], - UM182: [{ - response: "DM79", - text: "ATIS", - type: AtsuCommon.CpdlcMessageContentType.Atis, - textIndex: null, - valueIndex: 0, - emptyLength: 1 - }], - UM184: [{ - response: "DM67", - text: "DISTANCE TO %s", - type: AtsuCommon.CpdlcMessageContentType.Distance, - textIndex: 1, - valueIndex: 0, - emptyLength: 3, - freetext: "DISTANCE TO %s IS %v" - }], - UM228: [{ - response: "DM104", - text: "ETA TO %s", - type: AtsuCommon.CpdlcMessageContentType.Time, - textIndex: 0, - valueIndex: 0, - emptyLength: 4 - }], - UM231: [{ - response: "DM106", - text: "PREFERRED LEVEL", - type: AtsuCommon.CpdlcMessageContentType.Level, - textIndex: null, - valueIndex: 0, - emptyLength: 5 - }], - UM232: [{ - response: "DM109", - text: "TIME TO TOP OF DESCENT", - type: AtsuCommon.CpdlcMessageContentType.Time, - textIndex: null, - valueIndex: 0, - emptyLength: 5 - }] -}; - -class CDUAtcMessageModify { - static CreateDataBlock(message) { - const lutEntry = ModifyLookupTable[message.Content[0].TypeId]; - - return { - value: (message.Response.Content[0].Content.length > lutEntry[0].valueIndex && message.Response.Content[0].TypeId !== "DM67") ? message.Response.Content[0].Content[lutEntry[0].valueIndex].Value : '', - modified: false, - multiSelection: lutEntry.length > 1, - selectedToggles: [false, false] - }; - } - - static CreateDescriptionLine(message, entry) { - if (entry.textIndex !== null) { - return entry.text.replace("%s", message.Content[0].Content[entry.textIndex].Value); - } - return entry.text; - } - - static CreateToggleBasedField(message, data, entry, index) { - const text = CDUAtcMessageModify.CreateDescriptionLine(message, entry); - - if (data.selectedToggles[index - 1]) { - return `{cyan}\xa0${text}{end}`; - } - return `{cyan}{{end}{white}${text}{end}`; - } - - static CreateVisualization(message, data) { - const lutEntry = ModifyLookupTable[message.Content[0].TypeId]; - const visualization = [["", ""], "", ""]; - - const color = !data.selectedToggles[0] && !data.selectedToggles[1] ? "{cyan}" : "{white}"; - visualization[0][0] = `${color}\xa0${CDUAtcMessageModify.CreateDescriptionLine(message, lutEntry[0])}{end}`; - if (data.value !== "") { - const prefix = !data.modified ? "{small}" : ""; - const suffix = !data.modified ? "{end}" : ""; - visualization[0][1] = `{cyan}${prefix}${data.value}${suffix}{end}`; - } else { - visualization[0][1] = `{amber}${"_".repeat(lutEntry[0].emptyLength)}{end}`; - } - - if (lutEntry.length > 1) { - visualization[1] = CDUAtcMessageModify.CreateToggleBasedField(message, data, lutEntry[1], 1); - visualization[2] = CDUAtcMessageModify.CreateToggleBasedField(message, data, lutEntry[2], 2); - } - - return visualization; - } - - static ValidateScratchpadValue(message, value) { - const lutEntry = ModifyLookupTable[message.Content[0].TypeId]; - - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Position) { - return AtsuCommon.InputValidation.validateScratchpadPosition(value); - } - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Speed) { - return AtsuCommon.InputValidation.validateScratchpadSpeed(value); - } - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Level) { - return AtsuCommon.InputValidation.validateScratchpadAltitude(value); - } - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Time) { - return AtsuCommon.InputValidation.validateScratchpadTime(value); - } - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Freetext) { - return AtsuCommon.AtsuStatusCodes.Ok; - } - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Squawk) { - return AtsuCommon.InputValidation.validateScratchpadSquawk(value); - } - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Degree) { - return AtsuCommon.InputValidation.validateScratchpadDegree(value); - } - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Atis) { - return AtsuCommon.InputValidation.validateScratchpadAtis(value); - } - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Distance) { - return AtsuCommon.InputValidation.validateScratchpadDistance(value); - } - - return AtsuCommon.AtsuStatusCodes.UnknownMessage; - } - - static FormatScratchpadValue(message, value) { - const lutEntry = ModifyLookupTable[message.Content[0].TypeId]; - - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Speed) { - return AtsuCommon.InputValidation.formatScratchpadSpeed(value); - } - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Level) { - return AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - if (lutEntry[0].type === AtsuCommon.CpdlcMessageContentType.Distance) { - return AtsuCommon.InputValidation.formatScratchpadDistance(value); - } - - return value; - } - - static CanUpdateMessage(data) { - return data.value !== "" || data.selectedToggles[0] || data.selectedToggles[1]; - } - - static UpdateResponseMessage(message, data) { - const lutEntry = ModifyLookupTable[message.Content[0].TypeId]; - - let lutIndex = 0; - if (data.selectedToggles[0]) { - lutIndex = 1; - } else if (data.selectedToggles[1]) { - lutIndex = 2; - } - - const newContent = AtsuCommon.CpdlcMessagesDownlink[lutEntry[lutIndex].response][1].deepCopy(); - if (newContent.TypeId === "DM67") { - let freetext = lutEntry[lutIndex].freetext; - if (lutEntry[lutIndex].textIndex !== null) { - freetext = freetext.replace("%s", message.Content[0].Content[lutEntry[lutIndex].textIndex].Value); - } - if (lutEntry[lutIndex].valueIndex !== null) { - freetext = freetext.replace("%v", data.value); - } - newContent.Content[0].Value = freetext; - } else if (newContent.TypeId === "DM104") { - newContent.Content[0].Value = message.Content[0].Content[lutEntry[lutIndex].textIndex].Value; - newContent.Content[1].Value = data.value; - } else { - newContent.Content[lutEntry[lutIndex].valueIndex].Value = data.value; - for (let i = 0; i < lutEntry[lutIndex].valueIndex; ++i) { - newContent.Content[i].Value = message.Content[0].Content[lutEntry[lutIndex].textIndex + i].Value; - } - } - - message.Response.Content = [newContent]; - } - - static ShowPage(mcdu, message, data = null) { - mcdu.page.Current = mcdu.page.ATCModify; - - if (message.Content[0].TypeId === "UM147") { - // modify the position report - CDUAtcPositionReport.ShowPage1(mcdu, message); - return; - } else if (message.Content[0].TypeId === "UM131") { - // report persons on board and fuel remaining - CDUAtcMessageModifyUM131.ShowPage(mcdu, message); - return; - } - - if (!data) { - data = CDUAtcMessageModify.CreateDataBlock(message); - } - const visualization = CDUAtcMessageModify.CreateVisualization(message, data); - - let cancel = "\xa0CANCEL"; - let addText = "ADD TEXT\xa0"; - let transfer = "DCDU\xa0"; - if (CDUAtcMessageModify.CanUpdateMessage(data)) { - cancel = "*CANCEL"; - addText = "ADD TEXT>"; - transfer = "DCDU*"; - } - - mcdu.setTemplate([ - ["MODIFY"], - [data.multiSelection ? visualization[0][0] : ""], - [data.multiSelection ? visualization[0][1] : ""], - [!data.multiSelection ? visualization[0][0] : ""], - [!data.multiSelection ? visualization[0][1] : ""], - [""], - [visualization[1]], - [""], - [visualization[2]], - ["{cyan}\xa0PAGE{end}"], - [`{cyan}${cancel}{end}`, `{white}${addText}{end}`], - ["\xa0ATC MENU", "{cyan}XFR TO\xa0{end}"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (data.multiSelection) { - if (value === FMCMainDisplay.clrValue) { - data.modified = true; - data.value = ""; - } else if (value) { - const error = CDUAtcMessageModify.ValidateScratchpadValue(message, value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.value = CDUAtcMessageModify.FormatScratchpadValue(message, value); - data.modified = true; - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcMessageModify.ShowPage(mcdu, message, data); - } - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (!data.multiSelection) { - if (value === FMCMainDisplay.clrValue) { - data.modified = true; - data.value = ""; - } else if (value) { - const error = CDUAtcMessageModify.ValidateScratchpadValue(message, value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.value = CDUAtcMessageModify.FormatScratchpadValue(message, value); - data.modified = true; - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcMessageModify.ShowPage(mcdu, message, data); - } - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (data.multiSelection) { - if (value === FMCMainDisplay.clrValue) { - data.selectedToggles[0] = false; - } else { - data.selectedToggles[0] = true; - data.selectedToggles[1] = false; - } - CDUAtcMessageModify.ShowPage(mcdu, message, data); - } - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - if (data.multiSelection) { - if (value === FMCMainDisplay.clrValue) { - data.selectedToggles[1] = false; - } else { - data.selectedToggles[0] = false; - data.selectedToggles[1] = true; - } - CDUAtcMessageModify.ShowPage(mcdu, message, data); - } - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - if (data.multiSelection) { - if (value === FMCMainDisplay.clrValue) { - data.selectedToggles[1] = false; - } else { - data.selectedToggles[0] = false; - data.selectedToggles[1] = true; - } - CDUAtcMessageModify.ShowPage(mcdu, message, data); - } - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - if (CDUAtcMessageModify.CanUpdateMessage(data)) { - mcdu.atsu.updateMessage(message); - CDUAtcMenu.ShowPage(mcdu); - } - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcMessageModify.CanUpdateMessage(data)) { - CDUAtcMessageModify.UpdateResponseMessage(message, data); - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcTextFansA.ShowPage1(mcdu, [message]); - } else { - CDUAtcTextFansB.ShowPage(mcdu, [message]); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcMessageModify.CanUpdateMessage(data)) { - CDUAtcMessageModify.UpdateResponseMessage(message, data); - mcdu.atsu.updateMessage(message); - CDUAtcMenu.ShowPage(mcdu, message); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MessageMonitoring.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MessageMonitoring.js deleted file mode 100644 index e5cdb50d8ca..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MessageMonitoring.js +++ /dev/null @@ -1,162 +0,0 @@ -class CDUAtcMessageMonitoring { - static TranslateCpdlcResponse(response) { - if (response) { - switch (response.Content[0].TypeId) { - case "DM0": - return "WILC"; - case "UM0": - case "DM1": - return "UNBL"; - case "UM1": - case "DM2": - return "STBY"; - case "UM3": - case "DM3": - return "ROGR"; - case "UM4": - case "DM4": - return "AFRM"; - case "UM5": - case "DM5": - return "NEG"; - default: - return ""; - } - } - - return ""; - } - - static ShowPage(mcdu, messages = null, offset = 0, cancelIndex = -1) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCMessageMonitoring; - - if (!messages) { - messages = mcdu.atsu.monitoredMessages(); - } - - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.ATCMessageMonitoring) { - CDUAtcMessageMonitoring.ShowPage(mcdu, messages, offset, cancelIndex); - } - }, mcdu.PageTimeout.Slow); - - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.ATCMessageMonitoring) { - CDUAtcMessageMonitoring.ShowPage(mcdu, messages, offset, cancelIndex); - } - }, mcdu.PageTimeout.Slow); - - let cancelHeader = ""; - let cancelMessage = ""; - if (cancelIndex > -1) { - cancelHeader = "{yellow}CONFIRM CANCEL\xa0{end}"; - cancelMessage = "{yellow}MONITORING*{end}"; - } - - const msgHeadersLeft = [], msgHeadersRight = [], msgStart = []; - msgHeadersLeft.length = msgHeadersRight.length = msgStart.length = 4; - for (let i = 0; i < 5; ++i) { - let headerLeft = "", headerRight = "", contentStart = ""; - - if (messages.length > (offset + i) && messages[offset + i]) { - headerLeft = `${messages[offset + i].Timestamp.mailboxTimestamp()} ${messages[offset + i].Direction === AtsuCommon.AtsuMessageDirection.Input ? "FROM" : "TO"} `; - headerLeft += messages[offset + i].Station; - headerRight = CDUAtcMessageMonitoring.TranslateCpdlcResponse(messages[offset + i].Response); - - // ignore the headline with the station and the timestamp - const lines = messages[offset + i].serialize(AtsuCommon.AtsuMessageSerializationFormat.FmsDisplayMonitored).split("\n"); - let firstLine = "CPDLC"; - if (lines.length >= 2) { - firstLine = messages[offset + i].serialize(AtsuCommon.AtsuMessageSerializationFormat.FmsDisplayMonitored).split("\n")[1]; - } - if (firstLine.length <= 19) { - contentStart = firstLine; - } else { - firstLine.split(" ").forEach((word) => { - if (contentStart.length + word.length + 1 < 19) { - contentStart += `${word}\xa0`; - } - }); - } - } - - msgHeadersLeft[i] = headerLeft; - msgHeadersRight[i] = headerRight; - msgStart[i] = `${contentStart.length !== 0 ? "<" : ""}${contentStart}`; - } - - let left = false, right = false; - if (messages.length > offset + 4) { - mcdu.onNextPage = () => { - CDUAtcMessageMonitoring.ShowPage(mcdu, messages, offset + 4, false); - }; - right = true; - } - if (offset > 0) { - mcdu.onPrevPage = () => { - CDUAtcMessageMonitoring.ShowPage(mcdu, messages, offset - 4, false); - }; - left = true; - } - mcdu.setArrows(false, false, left, right); - - mcdu.setTemplate([ - ["MONITORED MSG"], - [msgHeadersLeft[0], msgHeadersRight[0]], - [`${messages.length !== 0 ? msgStart[0] : "NO MESSAGES"}`, `${msgStart[0] !== "" ? "{cyan}CANCEL*{end}" : ""}`], - [msgHeadersLeft[1], msgHeadersRight[1]], - [msgStart[1], `${msgStart[1] !== "" ? "{cyan}CANCEL*{end}" : ""}`], - [msgHeadersLeft[2], msgHeadersRight[2]], - [msgStart[2], `${msgStart[2] !== "" ? "{cyan}CANCEL*{end}" : ""}`], - [msgHeadersLeft[3], msgHeadersRight[3]], - [msgStart[3], `${msgStart[3] !== "" ? "{cyan}CANCEL*{end}" : ""}`], - [""], - [""], - ["\xa0ATC MENU", cancelHeader], - [" { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[i] = () => { - if (messages[offset + i]) { - CDUAtcMessage.ShowPage(mcdu, messages, offset + i, false); - } - }; - - mcdu.rightInputDelay[i] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onRightInput[i] = () => { - if (messages[offset + i]) { - CDUAtcMessageMonitoring.ShowPage(mcdu, messages, offset, offset + i); - } - }; - } - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (cancelIndex > -1) { - AtsuCommon.UplinkMessageStateMachine.update(mcdu.atsu, messages[cancelIndex], false); - mcdu.atsu.updateMessage(messages[cancelIndex]); - CDUAtcMessageMonitoring.ShowPage(mcdu); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MessagesRecord.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MessagesRecord.js deleted file mode 100644 index 7f2327410e4..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_MessagesRecord.js +++ /dev/null @@ -1,149 +0,0 @@ -class CDUAtcMessagesRecord { - static TranslateCpdlcResponse(response) { - if (response) { - if (response.Content[0].TypeId === "DM0") { - return "WILC"; - } - if (response.Content[0].TypeId === "UM0" || response.Content[0].TypeId === "DM1") { - return "UNBL"; - } - if (response.Content[0].TypeId === "UM1" || response.Content[0].TypeId === "DM2") { - return "STBY"; - } - if (response.Content[0].TypeId === "UM3" || response.Content[0].TypeId === "DM3") { - return "ROGR"; - } - if (response.Content[0].TypeId === "UM4" || response.Content[0].TypeId === "DM4") { - return "AFRM"; - } - if (response.Content[0].TypeId === "UM5" || response.Content[0].TypeId === "DM5") { - return "NEG"; - } - } - - return ""; - } - - static ShowPage(mcdu, messages = null, offset = 0, confirmErase = false) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCMessageRecord; - - if (!messages) { - messages = mcdu.atsu.atcMessages(); - } - - // regular update due to showing dynamic data on this page - mcdu.page.SelfPtr = setTimeout(() => { - if (mcdu.page.Current === mcdu.page.ATCMessageRecord) { - CDUAtcMessagesRecord.ShowPage(mcdu, messages, offset, confirmErase); - } - }, mcdu.PageTimeout.Slow); - - let eraseRecordTitle = "\xa0MSG RECORD"; - let eraseRecordButton = "*ERASE"; - if (confirmErase) { - eraseRecordTitle = "\xa0ERASE MSG RECORD"; - eraseRecordButton = "*CONFIRM"; - } - - const msgHeadersLeft = [], msgHeadersRight = [], msgStart = []; - msgHeadersLeft.length = msgHeadersRight.length = msgStart.length = 4; - for (let i = 0; i < 5; ++i) { - let headerLeft = "", headerRight = "", contentStart = ""; - - if (messages.length > (offset + i) && messages[offset + i]) { - headerLeft = `${messages[offset + i].Timestamp.mailboxTimestamp()} ${messages[offset + i].Direction === AtsuCommon.AtsuMessageDirection.Input ? "FROM" : "TO"} `; - headerLeft += messages[offset + i].Station; - headerRight = CDUAtcMessagesRecord.TranslateCpdlcResponse(messages[offset + i].Response); - - // ignore the headline with the station and the timestamp - const lines = messages[offset + i].serialize(AtsuCommon.AtsuMessageSerializationFormat.Printer).split("\n"); - let firstLine = "CPDLC"; - if (lines.length >= 2) { - firstLine = messages[offset + i].serialize(AtsuCommon.AtsuMessageSerializationFormat.Printer).split("\n")[1]; - } - if (firstLine.length <= 24) { - contentStart = firstLine; - } else { - firstLine.split(" ").forEach((word) => { - if (contentStart.length + word.length + 1 < 24) { - contentStart += `${word}\xa0`; - } - }); - } - } - - msgHeadersLeft[i] = headerLeft; - msgHeadersRight[i] = headerRight; - msgStart[i] = `${contentStart.length !== 0 ? "<" : ""}${contentStart}`; - } - - let left = false, right = false; - if (messages.length > offset + 4) { - mcdu.onNextPage = () => { - CDUAtcMessagesRecord.ShowPage(mcdu, messages, offset + 4, false); - }; - right = true; - } - if (offset > 0) { - mcdu.onPrevPage = () => { - CDUAtcMessagesRecord.ShowPage(mcdu, messages, offset - 4, false); - }; - left = true; - } - mcdu.setArrows(false, false, left, right); - - mcdu.setTemplate([ - ["MSG RECORD"], - [msgHeadersLeft[0], `{big}${msgHeadersRight[0]}{end}`], - [`${messages.length !== 0 ? msgStart[0] : "NO MESSAGES"}`], - [msgHeadersLeft[1], `{big}${msgHeadersRight[1]}{end}`], - [msgStart[1]], - [msgHeadersLeft[2], `{big}${msgHeadersRight[2]}{end}`], - [msgStart[2]], - [msgHeadersLeft[3], `{big}${msgHeadersRight[3]}{end}`], - [msgStart[3]], - [eraseRecordTitle], - [eraseRecordButton], - ["\xa0ATC MENU", "MSG RECORD\xa0[color]inop"], - [" { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[i] = (value) => { - if (messages[offset + i]) { - if (value === FMCMainDisplay.clrValue) { - mcdu.atsu.removeMessage(messages[offset + i].UniqueMessageID, false); - CDUAtcMessagesRecord.ShowPage(mcdu, null, offset, false); - } else { - CDUAtcMessage.ShowPage(mcdu, messages, offset + i, true); - } - } - }; - } - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - if (messages.length !== 0) { - if (!confirmErase) { - CDUAtcMessagesRecord.ShowPage(mcdu, messages, offset, true); - } else { - mcdu.atsu.cleanupAtcMessages(); - CDUAtcMessagesRecord.ShowPage(mcdu, null, 0, false); - } - } - }; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_OceanicReq.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_OceanicReq.js deleted file mode 100644 index d33c6664d4e..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_OceanicReq.js +++ /dev/null @@ -1,353 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class CDUAtcOceanicReq { - static CreateDataBlock() { - return { - firstCall: true, - callsign: null, - entryPoint: null, - entryTime: null, - requestedMach: null, - requestedFlightlevel: null, - freetext: [ '', '', '', '', '', '' ] - }; - } - - static CanSendData(mcdu, data) { - if (!data.callsign) { - return false; - } - if (!mcdu.flightPlanService.active.destinationAirport) { - return false; - } - if (mcdu.atsu.currentStation() === "") { - return false; - } - return data.entryPoint && data.entryTime && data.requestedMach && data.requestedFlightlevel; - } - - static CreateMessage(mcdu, data) { - const retval = new AtsuCommon.OclMessage(); - - retval.Callsign = data.callsign; - retval.Destination = mcdu.flightPlanService.active.destinationAirport.ident; - retval.EntryPoint = data.entryPoint; - retval.EntryTime = data.entryTime; - retval.RequestedMach = data.requestedMach; - retval.RequestedFlightlevel = data.requestedFlightlevel; - retval.Freetext = data.freetext.filter((n) => n); - retval.Station = mcdu.atsu.currentStation(); - - return retval; - } - - static WaypointOnRoute(mcdu, ident) { - const activePlan = mcdu.flightPlanService.active; - - const totalWaypointsCount = activePlan.legCount; - const wptsListIndex = activePlan.activeLegIndex; - - let i = 0; - - while (i < totalWaypointsCount && i + wptsListIndex < totalWaypointsCount) { - const leg = activePlan.elementAt(i + wptsListIndex); - - if (leg && leg.isDiscontinuity === false && leg.ident === ident) { - return true; - } - - i++; - } - - return false; - } - - static CalculateEntryPointETA(mcdu, ident) { - // TODO this currently does not work as computeWaypointStatistics returns dummy values. Needs a refactor of predictions (fms-v2) - - const activePlan = mcdu.flightPlanService.active; - - const adirLat = ADIRS.getLatitude(); - const adirLong = ADIRS.getLongitude(); - const ppos = (adirLat.isNormalOperation() && adirLong.isNormalOperation()) ? { - lat: ADIRS.getLatitude().value, - long: ADIRS.getLongitude().value, - } : { - lat: undefined, - long: undefined - }; - - let retval = ""; - const stats = activePlan.computeWaypointStatistics(); - stats.forEach((value) => { - if (value.ident === ident && retval === "") { - const eta = value.etaFromPpos; - const hours = Math.floor(eta / 3600); - const minutes = Math.floor(eta / 60) % 60; - retval = `${hours.toString().padStart(2, '0')}${minutes.toString().padStart(2, '0')}Z`; - } - }); - - return retval; - } - - static ShowPage1(mcdu, store = CDUAtcOceanicReq.CreateDataBlock()) { - mcdu.clearDisplay(); - - let flightNo = "{white}-------{end}"; - let atcStation = "{white}----{end}"; - - const entryTime = new CDU_SingleValueField(mcdu, - "string", - store.entryTime, - { - clearable: true, - emptyValue: "{amber}_____{end}", - suffix: "[color]cyan", - maxLength: 5, - isValid: ((value) => { - if (value.length !== 4 && value.length !== 5) { - return false; - } - - let check = value; - if (value.length === 5) { - if (value[4] !== "Z") { - return false; - } - check = value.substring(0, 4); - } - if (!/^[0-9()]*$/.test(check)) { - return false; - } - - const asInt = parseInt(check); - return asInt <= 2359 && asInt >= 0; - }) - }, - (value) => { - if (value.length === 4) { - store.entryTime = `${value}Z`; - } else { - store.entryTime = value; - } - CDUAtcOceanicReq.ShowPage1(mcdu, store); - }); - const entryPoint = new CDU_SingleValueField(mcdu, - "string", - store.entryPoint, - { - clearable: true, - emptyValue: "{amber}_______{end}", - suffix: "[color]cyan" - }, - (value) => { - mcdu.waypointType(mcdu, value).then((type) => { - if (type[0] === -1) { - mcdu.setScratchpadMessage(type[1]); - } else if (type[0] === 1) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } else { - store.entryPoint = value; - if (CDUAtcOceanicReq.WaypointOnRoute(mcdu, value)) { - store.entryTime = CDUAtcOceanicReq.CalculateEntryPointETA(mcdu, value); - if (store.entryTime !== "") { - entryTime.setValue(store.entryTime); - } else { - entryTime.clearValue(); - } - } else { - store.entryTime = ""; - entryTime.clearValue(); - } - } - - CDUAtcOceanicReq.ShowPage1(mcdu, store); - }); - } - ); - const requestedMach = new CDU_SingleValueField(mcdu, - "string", - store.requestedMach, - { - clearable: true, - emptyValue: "{amber}___{end}", - suffix: "[color]cyan", - maxLength: 3, - isValid: ((value) => { - if (value && /^M*.[0-9]{1,2}$/.test(value)) { - const split = value.split('.'); - let number = parseInt(split.length > 0 && split[1]); - if (number < 10) { - number *= 10; - } - return number >= 61 && number <= 92; - } - return false; - }) - }, - (value) => { - if (value) { - const split = value.split('.'); - let number = parseInt(split.length > 0 && split[1]); - if (number < 10) { - number *= 10; - } - store.requestedMach = `M.${number}`; - CDUAtcOceanicReq.ShowPage1(mcdu, store); - } - }); - const requestedFlightlevel = new CDU_SingleValueField(mcdu, - "string", - store.requestedFlightlevel, - { - clearable: true, - emptyValue: "{amber}_____{end}", - suffix: "[color]cyan", - maxLength: 5, - isValid: ((value) => { - if (/^(FL)*[0-9]{2,3}$/.test(value)) { - let level = 0; - if (value.startsWith("FL")) { - level = parseInt(value.substring(2, value.length)); - } else { - level = parseInt(value); - } - return level >= 30 && level <= 410; - } - return false; - }) - }, - (value) => { - if (value.startsWith("FL")) { - store.requestedFlightlevel = value; - } else { - const zeroPad = (str, places) => str.padStart(places, '0'); - store.requestedFlightlevel = `FL${zeroPad(value, 3)}`; - } - CDUAtcOceanicReq.ShowPage1(mcdu, store); - }); - const freetext = new CDU_SingleValueField(mcdu, - "string", - store.freetext[0], - { - clearable: store.freetext[0].length !== 0, - emptyValue: "{cyan}[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0]{end}", - suffix: "[color]white", - maxLength: 22 - }, - (value) => { - store.freetext[0] = value; - CDUAtcOceanicReq.ShowPage1(mcdu, store); - } - ); - - if (store.firstCall && !store.callsign) { - if (mcdu.atsu.flightNumber().length !== 0) { - store.callsign = mcdu.atsu.flightNumber(); - } - } - store.firstCall = false; - - if (store.callsign) { - flightNo = `{green}${store.callsign}{end}`; - } - if (mcdu.atsu.currentStation() !== "") { - atcStation = `{cyan}${mcdu.atsu.currentStation()}{end}`; - } - - // check if all required information are available to prepare the PDC message - let reqDisplButton = "{cyan}DCDU\xa0{end}"; - if (CDUAtcOceanicReq.CanSendData(mcdu, store)) { - reqDisplButton = "{cyan}DCDU*{end}"; - } - - mcdu.setTemplate([ - ["OCEANIC REQ"], - ["\xa0ATC FLT NBR", "OCEAN ATC\xa0"], - [flightNo, atcStation], - ["\xa0ENTRY-POINT", "AT TIME\xa0"], - [entryPoint, entryTime], - ["\xa0REQ MACH", "REQ FL\xa0"], - [requestedMach, requestedFlightlevel], - ["---------FREE TEXT---------"], - [freetext], - ["", "MORE\xa0"], - ["", "FREE TEXT>"], - ["\xa0FLIGHT REQ", "{cyan}XFR TO\xa0{end}"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - CDUAtcOceanicReq.ShowPage2(mcdu, store); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcOceanicReq.CanSendData(mcdu, store)) { - mcdu.atsu.registerMessages([CDUAtcOceanicReq.CreateMessage(mcdu, store)]); - CDUAtcOceanicReq.ShowPage1(mcdu); - } - }; - } - - static ShowPage2(mcdu, store) { - mcdu.clearDisplay(); - - const freetextLines = []; - for (let i = 0; i < 5; ++i) { - freetextLines.push(new CDU_SingleValueField(mcdu, - "string", - store.freetext[i + 1], - { - clearable: store.freetext[i + 1].length !== 0, - emptyValue: "{cyan}[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0]{end}", - suffix: "[color]white", - maxLength: 22 - }, - (value) => { - store.freetext[i + 1] = value; - CDUAtcOceanicReq.ShowPage2(mcdu, store); - } - )); - } - - // define the template - mcdu.setTemplate([ - ["FREE TEXT"], - [""], - [freetextLines[0]], - [""], - [freetextLines[1]], - [""], - [freetextLines[2]], - [""], - [freetextLines[3]], - [""], - [freetextLines[4]], - ["\xa0OCEANIC REQ"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcOceanicReq.ShowPage1(mcdu, store); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ReportAtis.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ReportAtis.js deleted file mode 100644 index fce43318225..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_ReportAtis.js +++ /dev/null @@ -1,141 +0,0 @@ -class CDUAtcReportAtis { - static ConvertAtisInformation(info) { - switch (info) { - case "A": - return "ALPHA"; - case "B": - return "BRAVO"; - case "C": - return "CHARLIE"; - case "D": - return "DELTA"; - case "E": - return "ECHO"; - case "F": - return "FOXTROT"; - case "G": - return "GOLF"; - case "H": - return "HOTEL"; - case "I": - return "INDIA"; - case "J": - return "JULIETT"; - case "K": - return "KILO"; - case "L": - return "LIMA"; - case "M": - return "MIKE"; - case "N": - return "NOVEMBER"; - case "O": - return "OSCAR"; - case "P": - return "PAPA"; - case "Q": - return "QUEBEC"; - case "R": - return "ROMEO"; - case "S": - return "SIERRA"; - case "T": - return "TANGO"; - case "U": - return "UNIFORM"; - case "V": - return "VICTOR"; - case "W": - return "WHISKEY"; - case "X": - return "XRAY"; - case "Y": - return "YANKEE"; - case "Z": - return "ZULU"; - default: - return ""; - } - } - - static ShowPage(mcdu, title, messages, messageIndex, offset = 0) { - mcdu.clearDisplay(); - - const message = messages[messageIndex]; - let serialized = message.serialize(AtsuCommon.AtsuMessageSerializationFormat.FmsDisplay); - serialized = serialized.replace(/{green}|{amber}|{white}|{end}/gi, ""); - const lines = serialized.split("\n"); - lines.shift(); - lines.pop(); - - if (lines.length > 8) { - let up = false; - let down = false; - if (lines[offset - 8]) { - mcdu.onUp = () => { - offset -= 8; - CDUAtcReportAtis.ShowPage(mcdu, title, messages, messageIndex, offset); - }; - up = true; - } - if (lines[offset + 8]) { - mcdu.onDown = () => { - offset += 8; - CDUAtcReportAtis.ShowPage(mcdu, title, messages, messageIndex, offset); - }; - down = true; - } - mcdu.setArrows(up, down, false, false); - } - - const pageCount = Math.round(lines.length / 8 + 0.5); - const currentPage = Math.floor(offset / 8) + 1; - - let prevAtis = "\xa0PREV ATIS"; - if (messages.length > messageIndex + 1) { - prevAtis = " { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - if (messages.length > (messageIndex + 1)) { - CDUAtcReportAtis.ShowPage(mcdu, title, messages, messageIndex + 1, offset); - } else { - mcdu.setScratchpadMessage(NXSystemMessages.noPreviousAtis); - } - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcAtisMenu.ShowPage(mcdu); - }; - - mcdu.onRightInput[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - mcdu.atsu.printMessage(message); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_SpeedReq.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_SpeedReq.js deleted file mode 100644 index 647749d8f23..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATC_SpeedReq.js +++ /dev/null @@ -1,213 +0,0 @@ -class CDUAtcSpeedRequest { - static CreateDataBlock() { - return { - speed: null, - whenSpeed: null, - }; - } - - static CanSendData(data) { - return data.speed || data.whenSpeed; - } - - static CanEraseData(data) { - return data.speed || data.whenSpeed; - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - if (data.speed) { - retval.push(CDUAtcSpeedRequest.CreateRequest(mcdu, "DM18", [data.speed])); - } - if (data.whenSpeed) { - retval.push(CDUAtcSpeedRequest.CreateRequest(mcdu, "DM49", [data.whenSpeed])); - } - - return retval; - } - - static ShowPage(mcdu, data = CDUAtcSpeedRequest.CreateDataBlock()) { - mcdu.clearDisplay(); - - let speed = "[ ][color]cyan"; - if (data.speed) { - speed = `${data.speed}[color]cyan`; - } - - let speedWhenSmall = ""; - let speedWhen = ""; - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - speedWhenSmall = "\xa0WHEN CAN WE EXPECT SPD"; - speedWhen = "[ ][color]cyan"; - if (data.whenSpeed) { - speedWhen = `${data.whenSpeed}[color]cyan`; - } - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcSpeedRequest.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - } - if (CDUAtcSpeedRequest.CanEraseData(data)) { - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["ATC SPEED REQ"], - ["\xa0SPEED[color]white"], - [speed], - [speedWhenSmall], - [speedWhen], - [""], - [""], - [""], - [""], - ["\xa0ALL FIELDS"], - [erase, text], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.speed = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadSpeed(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.speed = AtsuCommon.InputValidation.formatScratchpadSpeed(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcSpeedRequest.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - if (value === FMCMainDisplay.clrValue) { - data.whenSpeed = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadSpeed(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.whenSpeed = AtsuCommon.InputValidation.formatScratchpadSpeed(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcSpeedRequest.ShowPage(mcdu, data); - } - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcSpeedRequest.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.offset = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.offset = AtsuCommon.InputValidation.formatScratchpadOffset(value); - data.offsetStart = null; - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcSpeedRequest.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = async (value) => { - if (value === FMCMainDisplay.clrValue) { - data.weatherDeviation = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.weatherDeviation = AtsuCommon.InputValidation.formatScratchpadOffset(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcSpeedRequest.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.backOnTrack = false; - } else { - data.backOnTrack = true; - } - CDUAtcSpeedRequest.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcSpeedRequest.CanSendData(data)) { - const messages = CDUAtcSpeedRequest.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcSpeedRequest.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcSpeedRequest.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcSpeedRequest.ShowPage(mcdu); - } - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATSU_DatalinkStatus.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATSU_DatalinkStatus.js deleted file mode 100644 index 89931f20578..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATSU_DatalinkStatus.js +++ /dev/null @@ -1,88 +0,0 @@ -class CDUAtsuDatalinkStatus { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATSUDatalinkStatus; - mcdu.activeSystem = "ATSU"; - - function updateView() { - if (mcdu.page.Current === mcdu.page.ATSUDatalinkStatus) { - CDUAtsuDatalinkStatus.ShowPage(mcdu); - } - } - - mcdu.pageRedrawCallback = () => { - updateView(); - }; - setTimeout(mcdu.requestUpdate.bind(mcdu), 500); - SimVar.SetSimVarValue("L:FMC_UPDATE_CURRENT_PAGE", "number", 1); - - const vhfStatusCode = mcdu.atsu.getDatalinkStatus('vhf'); - const vhfModeCode = mcdu.atsu.getDatalinkMode('vhf'); - const satcomStatusCode = mcdu.atsu.getDatalinkStatus('satcom'); - const satcomModeCode = mcdu.atsu.getDatalinkMode('satcom'); - const hfStatusCode = mcdu.atsu.getDatalinkStatus('hf'); - const hfModeCode = mcdu.atsu.getDatalinkMode('hf'); - - const statusCodeToString = { - [-1]: '{red}INOP{end}', - [0]: '{small}NOT INSTALLED{end}', - [1]: '{small}DLK NOT AVAIL{end}', - [2]: '{green}DLK AVAIL{end}' - }; - - const modeCodeToString = { - [1]: 'ATC/AOC', - [2]: 'AOC ONLY', - [3]: 'ATC ONLY', - [0]: ' ' - }; - - const vhfStatus = statusCodeToString[vhfStatusCode] || 'ERROR'; - const vhfMode = modeCodeToString[vhfModeCode] || 'ERROR'; - const satcomStatus = statusCodeToString[satcomStatusCode] || 'ERROR'; - const satcomMode = modeCodeToString[satcomModeCode] || 'ERROR'; - const hfStatus = statusCodeToString[hfStatusCode] || 'ERROR'; - const hfMode = modeCodeToString[hfModeCode] || 'ERROR'; - - mcdu.setTemplate([ - ["DATALINK STATUS"], - [""], - [`VHF3 : ${vhfStatus}`], - [`\xa0\xa0\xa0\xa0\xa0\xa0\xa0${vhfMode}`], - [`SATCOM : ${satcomStatus}`], - [`\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0${satcomMode}`], - [`HF : ${hfStatus}`], - [`\xa0\xa0\xa0\xa0\xa0${hfMode}`], - [""], - [""], - [""], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[5] = () => { - CDUAtsuMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onRightInput[5] = () => { - const lines = [ - "DATALINK STATUS", - `VHF3 : ${vhfStatus}`, - `\xa0\xa0\xa0\xa0\xa0\xa0\xa0${vhfMode}`, - `SATCOM : ${satcomStatus}`, - `\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0${satcomMode}`, - `HF : ${hfStatus}`, - `\xa0\xa0\xa0\xa0\xa0${hfMode}`, - ]; - mcdu.printPage(lines); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATSU_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATSU_Menu.js deleted file mode 100644 index 2dd2c53cc1d..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_ATSU_Menu.js +++ /dev/null @@ -1,52 +0,0 @@ -class CDUAtsuMenu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATSUMenu; - mcdu.activeSystem = "ATSU"; - - const display = [ - ["ATSU DATALINK"], - [""], - [""], - [""], - [""], - [""], - [""], - ["", "DATALINK\xa0"], - ["", "STATUS>"], - [""], - ["", "COMM MENU>"] - ]; - mcdu.setTemplate(display); - - mcdu.leftInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = () => { - CDUAocMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - CDUAtsuDatalinkStatus.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - CDUCommMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_Comm_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_Comm_Menu.js deleted file mode 100644 index 23105f61069..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/A320_Neo_CDU_Comm_Menu.js +++ /dev/null @@ -1,27 +0,0 @@ -class CDUCommMenu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["COMM MENU"], - ["\xa0VHF3[color]inop", "COMM\xa0[color]inop"], - ["[color]inop"], - [""], - [""], - [""], - [""], - [""], - ["", "MAINTENANCE>[color]inop"], - [""], - [""], - ["\xa0ATC MENU", "AUTO PRINT\xa0[color]inop"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtsuMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_ContactRequest.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_ContactRequest.js deleted file mode 100644 index db1ad9762a6..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_ContactRequest.js +++ /dev/null @@ -1,144 +0,0 @@ -class CDUAtcContactRequest { - static CreateDataBlock() { - return { - requestContact: false, - }; - } - - static CanSendData(data) { - return data.requestContact; - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - if (data.requestContact) { - retval.push(CDUAtcContactRequest.CreateRequest(mcdu, "DM20")); - } - - return retval; - } - - static ShowPage(mcdu, data = CDUAtcContactRequest.CreateDataBlock()) { - mcdu.clearDisplay(); - - let requestContact = "{cyan}{{end}REQ VOICE CONTACT"; - if (data.altitude) { - requestContact = "{cyan}\xa0REQ VOICE CONTACT{end]"; - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcContactRequest.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["CONTACT REQ"], - [""], - [requestContact], - [""], - [""], - [""], - [""], - [""], - [""], - ["\xa0ALL FIELDS"], - [erase, text], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.climb = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.climb = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcContactRequest.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.altitude = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.altitude = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcContactRequest.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcContactRequest.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcContactRequest.CanSendData(data)) { - const messages = CDUAtcContactRequest.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcContactRequest.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcContactRequest.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcContactRequest.ShowPage(mcdu); - } - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_Emergency.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_Emergency.js deleted file mode 100644 index ede078e5701..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_Emergency.js +++ /dev/null @@ -1,462 +0,0 @@ -class CDUAtcEmergencyFansA { - static CanSendData(data) { - return data.mayday || data.panpan || data.descendingTo || data.personsOnBoard || data.endurance || data.cancelEmergency || data.deviating || data.climbingTo || data.diverting || data.divertingVia || data.voiceContact; - } - - static CanEraseData(data) { - return data.mayday || data.panpan || data.descendingTo || data.personsOnBoard || data.endurance || data.cancelEmergency || data.deviating || data.climbingTo || data.diverting || data.divertingVia || data.voiceContact; - } - - static CreateDataBlock() { - return { - mayday: false, - panpan: false, - descendingTo: null, - personsOnBoard: null, - endurance: null, - cancelEmergency: false, - deviating: null, - climbingTo: null, - diverting: null, - divertingVia: null, - voiceContact: false - }; - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - if (data.mayday === true) { - retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, "DM56")); - } - if (data.panpan === true) { - retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, "DM55")); - } - if (data.descendingTo) { - retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, "DM55", [data.descendingTo])); - } - if (data.personsOnBoard && data.endurance) { - retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, "DM57", [data.endurance, data.personsOnBoard])); - } - if (data.cancelEmergency === true) { - retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, "DM58")); - } - if (data.deviating) { - const elements = AtsuCommon.InputValidation.expandLateralOffset(data.deviating).split(" "); - retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, "DM80", [elements[0], elements[1]])); - } - if (data.climbingTo) { - retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, "DM29", [data.climbingTo])); - } - if (data.diverting && data.divertingVia) { - retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, "DM59", [data.diverting, data.divertingVia])); - } - if (data.voiceContact === true) { - retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, "DM20")); - } - - return retval; - } - - static ShowPage1(mcdu, data = CDUAtcEmergencyFansA.CreateDataBlock()) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCEmergency; - - let mayday = "{cyan}{{end}{red}MAYDAY{end}"; - let panpan = "{cyan}{{end}{amber}PANPAN{end}"; - if (data.panpan === true) { - panpan = "\xa0{amber}PANPAN{end}"; - } - if (data.mayday === true) { - mayday = "\xa0{red}MAYDAY{end}"; - } - - let descendingTo = "{cyan}[ ]{end}"; - if (data.descendingTo) { - descendingTo = `{cyan}${data.descendingTo}{end}`; - } - - let personsOnBoard = "{cyan}[ ]{end}"; - let endurance = "{cyan}[ ]{end}"; - if (data.personsOnBoard) { - personsOnBoard = `{cyan}${data.personsOnBoard}{end}`; - } - if (data.endurance) { - endurance = `{cyan}${data.endurance}{end}`; - } - - let cancelEmergency = "{amber}CANCEL EMERGENCY{end}{cyan}}{end}"; - if (data.cancelEmergency === true) { - cancelEmergency = "{amber}CANCEL EMERGENCY{end}\xa0"; - } - - let addText = "ADD TEXT\xa0"; - let eraseData = "\xa0ERASE"; - let sendData = "DCDU\xa0[color]cyan"; - if (CDUAtcEmergencyFansA.CanEraseData(data)) { - eraseData = "*ERASE"; - addText = "ADD TEXT>"; - } - if (CDUAtcEmergencyFansA.CanSendData(data)) { - sendData = "DCDU*[color]cyan"; - } - - mcdu.setTemplate([ - ["EMERGENCY[color]amber", "1", "2"], - ["", "EMERG ADS-C:OFF\xa0"], - [mayday, "SET ON*[color]inop"], - ["", "DESCENDING TO\xa0"], - [panpan, descendingTo], - ["\xa0POB", "ENDURANCE\xa0"], - [personsOnBoard, endurance], - [""], - ["", cancelEmergency], - ["\xa0ALL FIELDS"], - [eraseData, addText], - ["\xa0ATC MENU", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.mayday = false; - } else { - data.panpan = false; - data.mayday = true; - } - CDUAtcEmergencyFansA.ShowPage1(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.panpan = false; - } else { - data.panpan = true; - data.mayday = false; - } - CDUAtcEmergencyFansA.ShowPage1(mcdu, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.personsOnBoard = null; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadPersonsOnBoard(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.personsOnBoard = parseInt(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcEmergencyFansA.ShowPage1(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcEmergencyFansA.ShowPage1(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.descendingTo = null; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.descendingTo = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcEmergencyFansA.ShowPage1(mcdu, data); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.endurance = null; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadEndurance(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.endurance = AtsuCommon.InputValidation.formatScratchpadEndurance(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcEmergencyFansA.ShowPage1(mcdu, data); - }; - - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.cancelEmergency = false; - } else { - data.cancelEmergency = true; - } - CDUAtcEmergencyFansA.ShowPage1(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcEmergencyFansA.CanSendData(data)) { - const messages = CDUAtcEmergencyFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcEmergencyFansA.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.addNewMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcEmergencyFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcEmergencyFansA.ShowPage1(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcEmergencyFansA.ShowPage2(mcdu, data); - }; - mcdu.onNextPage = () => { - CDUAtcEmergencyFansA.ShowPage2(mcdu, data); - }; - } - - static ShowPage2(mcdu, data = CDUAtcEmergencyFansA.CreateDataBlock()) { - mcdu.clearDisplay(); - - let deviating = "{cyan}[ ]{end}"; - let climbingTo = "{cyan}[ ]{end}"; - if (data.deviating) { - deviating = `{cyan}${data.deviating}{end}`; - } - if (data.climbingTo) { - climbingTo = `{cyan}${data.climbingTo}{end}`; - } - - let diverting = "{cyan}[ ]/[ ]{end}"; - if (data.diverting && data.divertingVia) { - diverting = `{cyan}${data.diverting}/${data.divertingVia}{end}`; - } - - let reqVoice = "{cyan}{{end}REQ VOICE CONTACT"; - if (data.voiceContact) { - reqVoice = "{cyan}\xa0REQ VOICE CONTACT{end}"; - } - - let addText = "ADD TEXT\xa0"; - let eraseData = "\xa0ERASE"; - let sendData = "DCDU\xa0[color]cyan"; - if (CDUAtcEmergencyFansA.CanEraseData(data)) { - eraseData = "*ERASE"; - addText = "ADD TEXT>"; - } - if (CDUAtcEmergencyFansA.CanSendData(data)) { - sendData = "DCDU*[color]cyan"; - } - - mcdu.setTemplate([ - ["EMERGENCY[color]amber", "2", "2"], - ["\xa0DEVIATING", "CLIMBING TO\xa0"], - [deviating, climbingTo], - ["\xa0DIVERTING/VIA"], - [diverting], - [""], - [reqVoice], - [""], - [""], - ["\xa0ALL FIELDS"], - [eraseData, addText], - ["\xa0ATC MENU", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.deviating = null; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.deviating = AtsuCommon.InputValidation.formatScratchpadOffset(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcEmergencyFansA.ShowPage2(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.diverting = null; - data.divertingVia = null; - } else if (value.length !== 0) { - const split = value.split("/"); - if (split.length === 2) { - if (split[0].length !== 0) { - const error = AtsuCommon.InputValidation.validateScratchpadPosition(split[0]); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.diverting = split[0]; - } else { - mcdu.addNewAtsuMessage(error); - return; - } - } - - const error = AtsuCommon.InputValidation.validateScratchpadPosition(split[1]); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.divertingVia = split[1]; - } else { - mcdu.addNewAtsuMessage(error); - } - } else if (split.length === 1) { - const error = AtsuCommon.InputValidation.validateScratchpadPosition(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - if (data.diverting) { - data.divertingVia = value; - } else { - data.diverting = value; - data.divertingVia = value; - } - } else { - mcdu.addNewAtsuMessage(error); - } - } else { - mcdu.addNewAtsuMessage(AtsuCommon.AtsuStatusCodes.FormatError); - } - } - CDUAtcEmergencyFansA.ShowPage2(mcdu, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.voiceContact = false; - } else { - data.voiceContact = true; - } - CDUAtcEmergencyFansA.ShowPage2(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcEmergencyFansA.ShowPage2(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.climbingTo = null; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.climbingTo = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcEmergencyFansA.ShowPage2(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcEmergencyFansA.CanSendData(data)) { - const messages = CDUAtcEmergencyFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcEmergencyFansA.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.addNewMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcEmergencyFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcEmergencyFansA.ShowPage2(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcEmergencyFansA.ShowPage1(mcdu, data); - }; - mcdu.onNextPage = () => { - CDUAtcEmergencyFansA.ShowPage1(mcdu, data); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_LatRequest.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_LatRequest.js deleted file mode 100644 index c7341bc67a7..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_LatRequest.js +++ /dev/null @@ -1,461 +0,0 @@ -class CDUAtcLatRequestFansA { - static CreateDataBlock() { - return { - directTo: null, - weatherDeviation: null, - offset: null, - offsetStart: null, - heading: null, - track: null, - backOnTrack: false - }; - } - - static CanSendData(data) { - return data.directTo || data.weatherDeviation || data.offset || data.heading || data.track || data.backOnTrack; - } - - static CanEraseData(data) { - return data.directTo || data.weatherDeviation || data.offset || data.heading || data.track || data.backOnTrack; - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - if (data.directTo) { - retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, "DM22", [data.directTo])); - } - if (data.weatherDeviation) { - const elements = AtsuCommon.InputValidation.expandLateralOffset(data.weatherDeviation).split(" "); - retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, "DM27", [elements[0], elements[1]])); - } - if (data.offset) { - const elements = AtsuCommon.InputValidation.expandLateralOffset(data.offset).split(" "); - - if (!data.offsetStart || /$[0-9]{4}Z^/.test(data.offsetStart)) { - retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, "DM17", [!data.offsetStart ? (new AtsuCommon.AtsuTimestamp()).mailboxTimestamp() : data.offsetStart, elements[0], elements[1]])); - } else { - retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, "DM16", [data.offsetStart, elements[0], elements[1]])); - } - } - if (data.heading) { - retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, "DM70", [data.heading === 0 ? "360" : data.heading.toString()])); - } - if (data.track) { - retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, "DM71", [data.track === 0 ? "360" : data.track.toString()])); - } - if (data.backOnTrack) { - retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, "DM51")); - } - - return retval; - } - - static ShowPage1(mcdu, data = CDUAtcLatRequestFansA.CreateDataBlock()) { - mcdu.clearDisplay(); - - let weatherDeviation = "{cyan}[ ]{end}"; - if (data.weatherDeviation) { - weatherDeviation = `${data.weatherDeviation}[color]cyan`; - } - let heading = "[ ]°[color]cyan"; - if (data.heading !== null) { - heading = `${data.heading}°[color]cyan`; - } - let grdTrack = "[ ]°[color]cyan"; - if (data.track !== null) { - grdTrack = `${data.track}°[color]cyan`; - } - let directTo = "{cyan}[ ]{end}"; - if (data.directTo) { - directTo = `${data.directTo}[color]cyan`; - } - let offsetDistance = "[ ]"; - if (data.offset && data.offsetStart === null) { - offsetDistance = data.offset; - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcLatRequestFansA.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - } - if (CDUAtcLatRequestFansA.CanEraseData(data)) { - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["ATC LAT REQ", "1", "2"], - ["\xa0DIR TO[color]white"], - [directTo], - ["\xa0HDG", "OFFSET\xa0"], - [heading, `{cyan}${offsetDistance}{end}`], - ["\xa0GND TRK", "WX DEV\xa0"], - [grdTrack, weatherDeviation], - [""], - [""], - ["\xa0ALL FIELDS"], - [erase, text], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.directTo = null; - } else if (value) { - if (Fmgc.WaypointEntryUtils.isLatLonFormat(value)) { - // format: DDMM.MB/EEEMM.MC - try { - Fmgc.WaypointEntryUtils.parseLatLon(value); - data.directTo = value; - } catch (err) { - if (err === NXSystemMessages.formatError) { - mcdu.setScratchpadMessage(err); - } - }; - } else if (/^[A-Z0-9]{2,7}/.test(value)) { - // place format - mcdu.dataManager.GetWaypointsByIdent.bind(mcdu.dataManager)(value).then((waypoints) => { - if (waypoints.length === 0) { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } else { - data.directTo = value; - } - - CDUAtcLatRequestFansA.ShowPage1(mcdu, data); - }); - } - } - - CDUAtcLatRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.heading = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadDegree(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.heading = parseInt(value) % 360; - } - } - - CDUAtcLatRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.track = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadDegree(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.track = parseInt(value) % 360; - } - } - - CDUAtcLatRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcLatRequestFansA.ShowPage1(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.offset = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.offset = AtsuCommon.InputValidation.formatScratchpadOffset(value); - data.offsetStart = null; - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcLatRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = async (value) => { - if (value === FMCMainDisplay.clrValue) { - data.weatherDeviation = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.weatherDeviation = AtsuCommon.InputValidation.formatScratchpadOffset(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcLatRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.backOnTrack = false; - } else { - data.backOnTrack = true; - } - CDUAtcLatRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcLatRequestFansA.CanSendData(data)) { - const messages = CDUAtcLatRequestFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcLatRequestFansA.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcLatRequestFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcLatRequestFansA.ShowPage1(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcLatRequestFansA.ShowPage2(mcdu, data); - }; - mcdu.onNextPage = () => { - CDUAtcLatRequestFansA.ShowPage2(mcdu, data); - }; - } - - static ShowPage2(mcdu, data = CDUAtcLatRequestFansA.CreateDataBlock()) { - mcdu.clearDisplay(); - - let offsetDistance = "[ ]"; - let offsetStartPoint = "[ ]"; - if (data.offset && data.offsetStart) { - offsetDistance = data.offset; - offsetStartPoint = data.offsetStart; - } - let whenCanWe = "{big}\xa0WHEN CAN WE\xa0{end}"; - let backOnRoute = "{cyan}{{end}EXPECT BACK ON ROUTE"; - if (data.backOnTrack) { - backOnRoute = "{cyan}\xa0EXPECT BACK ON ROUTE{end}"; - whenCanWe = "{cyan}{big}\xa0WHEN CAN WE\xa0{end}{end}"; - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcLatRequestFansA.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - } - if (CDUAtcLatRequestFansA.CanEraseData(data)) { - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["ATC LAT REQ", "2", "2"], - ["", "OFFSET/START AT"], - ["", `{cyan}${offsetDistance}/${offsetStartPoint}{end}`], - [""], - ["------------------------"], - [whenCanWe], - [backOnRoute], - ["------------------------"], - [""], - ["\xa0ALL FIELDS"], - [erase, text], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.backOnTrack = false; - } else { - data.backOnTrack = true; - } - - CDUAtcLatRequestFansA.ShowPage2(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcLatRequestFansA.ShowPage2(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = async (value) => { - if (value === FMCMainDisplay.clrValue) { - data.offset = null; - data.offsetStart = null; - } else if (value) { - const entries = value.split('/'); - let updatedOffset = false; - let offsetStart = null; - let offset = null; - - const error = AtsuCommon.InputValidation.validateScratchpadOffset(entries[0]); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - updatedOffset = true; - offset = AtsuCommon.InputValidation.formatScratchpadOffset(entries[0]); - entries.shift(); - } - - if (entries.length !== 0) { - const startingPoint = entries.join("/"); - - const type = AtsuCommon.InputValidation.classifyScratchpadWaypointType(startingPoint, true); - if (offset || data.offset) { - switch (type[0]) { - case AtsuCommon.InputWaypointType.GeoCoordinate: - case AtsuCommon.InputWaypointType.Place: - offsetStart = startingPoint; - break; - case AtsuCommon.InputWaypointType.Timepoint: - if (startingPoint.endsWith("Z")) { - offsetStart = startingPoint; - } else { - offsetStart = `${startingPoint}Z`; - } - break; - default: - mcdu.addNewAtsuMessage(type[1]); - offsetStart = null; - if (updatedOffset) { - offset = null; - } - break; - } - } - - if (offset || offsetStart) { - const oldOffsetStart = data.offsetStart; - const oldOffset = data.offset; - - data.offset = offset ? offset : oldOffset; - data.offsetStart = offsetStart ? offsetStart : oldOffsetStart; - } - - CDUAtcLatRequestFansA.ShowPage2(mcdu, data); - } else if (updatedOffset) { - if (data.offsetStart) { - data.offset = offset; - } else { - mcdu.addNewAtsuMessage(AtsuCommon.AtsuStatusCodes.FormatError); - } - } else if (error) { - mcdu.addNewAtsuMessage(error); - } - } - - CDUAtcLatRequestFansA.ShowPage2(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcLatRequestFansA.CanSendData(data)) { - const requests = CDUAtcLatRequestFansA.CreateRequests(mcdu, data); - if (requests.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, requests); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcLatRequestFansA.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcLatRequestFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcLatRequestFansA.ShowPage2(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcLatRequestFansA.ShowPage1(mcdu, data); - }; - mcdu.onNextPage = () => { - CDUAtcLatRequestFansA.ShowPage1(mcdu, data); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_PositionReport.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_PositionReport.js deleted file mode 100644 index feedb9ecb75..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_PositionReport.js +++ /dev/null @@ -1,1091 +0,0 @@ -class CDUAtcPositionReport { - static SecondsToString(seconds) { - const hours = Math.floor(seconds / 3600); - const minutes = Math.floor(seconds / 60) % 60; - return `${hours.toString().padStart(2, '0')}${minutes.toString().padStart(2, '0')}`; - } - - static AltitudeToString(altitude) { - if (Simplane.getPressureSelectedMode(Aircraft.A320_NEO) === "STD") { - return AtsuCommon.InputValidation.formatScratchpadAltitude(`FL${Math.round(altitude / 100)}`); - } - return AtsuCommon.InputValidation.formatScratchpadAltitude(`${altitude}FT`); - } - - static FillDataBlock(mcdu, data) { - const current = data.atsuFlightStateData; - const target = data.atsuAutopilotData; - const lastWp = data.atsuLastWaypoint; - const activeWp = data.atsuActiveWaypoint; - const nextWp = data.atsuNextWaypoint; - - if (lastWp && !data.passedWaypoint[3]) { - data.passedWaypoint[0] = lastWp.ident; - data.passedWaypoint[1] = CDUAtcPositionReport.SecondsToString(lastWp.utc); - data.passedWaypoint[2] = CDUAtcPositionReport.AltitudeToString(lastWp.altitude); - } - - // will be abreviated during the rendering - if (!data.currentPosition[2]) { - mcdu.setScratchpadMessage(NXSystemMessages.latLonAbreviated); - } - data.currentPosition = !data.currentPosition[2] ? [current.lat, current.lon, false] : data.currentPosition; - data.currentUtc[0] = !data.currentUtc[1] ? CDUAtcPositionReport.SecondsToString(SimVar.GetSimVarValue('E:ZULU TIME', 'seconds')) : data.currentUtc[0]; - data.currentAltitude[0] = !data.currentAltitude[1] ? CDUAtcPositionReport.AltitudeToString(current.altitude) : data.currentAltitude[0]; - - if (activeWp && !data.activeWaypoint[2]) { - data.activeWaypoint[0] = activeWp.ident; - data.activeWaypoint[1] = CDUAtcPositionReport.SecondsToString(activeWp.utc); - } - if (nextWp && !data.nextWaypoint[1]) { - data.nextWaypoint[0] = nextWp.ident; - } - if (data.atsuDestination && !data.eta[1]) { - data.eta[0] = CDUAtcPositionReport.SecondsToString(data.atsuDestination.utc); - } - - if (!data.wind[1]) { - const windDirection = data.atsuEnvironmentData.windDirection; - const windSpeed = data.atsuEnvironmentData.windSpeed; - - const wind = `${Math.round(windDirection.value)}/${Math.round(windSpeed.value)}`; - if (AtsuCommon.InputValidation.validateScratchpadWind(wind) === AtsuCommon.AtsuStatusCodes.Ok) { - data.wind[0] = AtsuCommon.InputValidation.formatScratchpadWind(wind); - } - } - if (!data.sat[1]) { - const sat = data.atsuEnvironmentData.temperature; - if (AtsuCommon.InputValidation.validateScratchpadTemperature(sat.value) === AtsuCommon.AtsuStatusCodes.Ok) { - data.sat[0] = Math.round(AtsuCommon.InputValidation.formatScratchpadTemperature(`${sat.value}`)); - } - } - - data.indicatedAirspeed[0] = !data.indicatedAirspeed[1] ? AtsuCommon.InputValidation.formatScratchpadSpeed(`${current.indicatedAirspeed}`) : data.indicatedAirspeed[0]; - data.groundSpeed[0] = !data.groundSpeed[1] ? AtsuCommon.InputValidation.formatScratchpadSpeed(`${current.groundSpeed}`) : data.groundSpeed[0]; - data.verticalSpeed[0] = !data.verticalSpeed[1] ? AtsuCommon.InputValidation.formatScratchpadVerticalSpeed(`${current.verticalSpeed}`) : data.verticalSpeed[0]; - // TODO add deviating - data.heading[0] = !data.heading[1] ? current.heading : data.heading[0]; - data.track[0] = !data.track[1] ? current.track : data.track[0]; - if (target.apActive && current.altitude > target.altitude) { - data.descending = CDUAtcPositionReport.AltitudeToString(target.altitude); - } else if (target.apActive && current.altitude < target.altitude) { - data.climbing = CDUAtcPositionReport.AltitudeToString(target.altitude); - } - } - - static CreateDataBlock(mcdu, requestMessage, autoFill) { - const retval = { - atsuFlightStateData: null, - atsuAutopilotData: null, - atsuEnvironmentData: null, - atsuLastWaypoint: null, - atsuActiveWaypoint: null, - atsuNextWaypoint: null, - atsuDestination: null, - passedWaypoint: [null, null, null, !autoFill], - activeWaypoint: [null, null, !autoFill], - nextWaypoint: [null, !autoFill], - currentPosition: [null, !autoFill], - currentUtc: [null, !autoFill], - currentAltitude: [null, !autoFill], - wind: [null, !autoFill], - sat: [null, !autoFill], - icing: [null, !autoFill], - turbulence: [null, !autoFill], - eta: [null, !autoFill], - endurance: [null, !autoFill], - indicatedAirspeed: [null, !autoFill], - groundSpeed: [null, !autoFill], - verticalSpeed: [null, !autoFill], - deviating: [null, !autoFill], - heading: [null, !autoFill], - track: [null, !autoFill], - descending: [null, !autoFill], - climbing: [null, !autoFill], - }; - - if (autoFill === true) { - mcdu.atsu.receivePositionReportData().then((data) => { - retval.atsuFlightStateData = data.flightState; - retval.atsuAutopilotData = data.autopilot; - retval.atsuEnvironmentData = data.environment; - retval.atsuLastWaypoint = data.lastWaypoint; - retval.atsuActiveWaypoint = data.activeWaypoint; - retval.atsuNextWaypoint = data.nextWaypoint; - retval.atsuDestination = data.destination; - - CDUAtcPositionReport.FillDataBlock(mcdu, retval); - if (mcdu.page.Current === mcdu.page.ATCPositionReport1) { - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, retval); - } else if (mcdu.page.Current === mcdu.page.ATCPositionReport2) { - CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, retval); - } else if (mcdu.page.Current === mcdu.page.ATCPositionReport3) { - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, retval); - } - }); - } - - return retval; - } - - static CanEraseData(data) { - return data.passedWaypoint[0] || data.passedWaypoint[1] || data.passedWaypoint[2] || data.activeWaypoint[0] || data.activeWaypoint[1] || data.nextWaypoint[0] || - data.currentPosition[0] || data.currentUtc[0] || data.currentAltitude[0] || data.wind[0] || data.sat[0] || data.icing[0] || data.turbulence[0] || - data.eta[0] || data.endurance[0] || data.indicatedAirspeed[0] || data.groundSpeed[0] || data.verticalSpeed[0] || data.deviating[0] || data.heading[0] || data.track[0] || - data.descending[0] || data.climbing[0]; - } - - static CanSendData(data) { - return data.passedWaypoint[0] && data.passedWaypoint[1] && data.passedWaypoint[2] && data.activeWaypoint[0] && data.activeWaypoint[1] && data.nextWaypoint[0] && - data.currentPosition[0] && data.currentUtc[0] && data.currentAltitude[0]; - } - - static CreateReport(mcdu, data) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink['DM48'][1].deepCopy()); - - // define the overhead - let extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `OVHD: ${data.passedWaypoint[0]}`; - retval.Content.push(extension); - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `AT ${data.passedWaypoint[1]}Z/${data.passedWaypoint[2]}`; - retval.Content.push(extension); - // define the present position - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `PPOS: ${AtsuCommon.coordinateToString({ lat: data.currentPosition[0][0], lon: data.currentPosition[0][1] }, false)}`; - retval.Content.push(extension); - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `AT ${data.currentUtc[0]}Z/${data.currentAltitude[0]}`; - retval.Content.push(extension); - // define the active position - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `TO :${data.activeWaypoint[0]} AT ${data.activeWaypoint[1]}Z`; - retval.Content.push(extension); - // define the next position - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `NEXT: ${data.nextWaypoint[0]}`; - retval.Content.push(extension); - - // create wind and temperature data - if (data.wind[0] && data.sat[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `WIND: ${data.wind[0]} SAT: ${data.sat[0]}`; - retval.Content.push(extension); - } else if (data.wind[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `WIND: ${data.wind[0]}`; - retval.Content.push(extension); - } else if (data.sat[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `SAT: ${data.sat[0]}`; - retval.Content.push(extension); - } - - // create the initial data - if (data.eta[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `DEST ETA: ${data.eta[0]}Z`; - retval.Content.push(extension); - } - if (data.descending[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `DESCENDING TO ${data.descending[0]}`; - retval.Content.push(extension); - } else if (data.climbing[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `CLIMBING TO ${data.climbing[0]}`; - retval.Content.push(extension); - } - if (data.endurance[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `ENDURANCE: ${data.endurance[0]}`; - retval.Content.push(extension); - } - if (data.icing[0] && data.turbulence[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `ICING: ${data.icing[0]} TURBULENCE: ${data.turbulence[0]}`; - retval.Content.push(extension); - } else if (data.icing[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `ICING: ${data.icing[0]}`; - retval.Content.push(extension); - } else if (data.turbulence[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `TURBULENCE: ${data.turbulence[0]}`; - retval.Content.push(extension); - } - if (data.indicatedAirspeed[0] && data.groundSpeed[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `SPD: ${data.indicatedAirspeed[0]} GS: ${data.groundSpeed[0]}`; - retval.Content.push(extension); - } else if (data.indicatedAirspeed[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `SPD: ${data.indicatedAirspeed[0]}`; - retval.Content.push(extension); - } else if (data.groundSpeed[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `GS: ${data.groundSpeed[0]}`; - retval.Content.push(extension); - } - if (data.verticalSpeed[0] && data.verticalSpeed[0] !== "0FTM") { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `VS: ${data.verticalSpeed[0]}`; - retval.Content.push(extension); - } - if (data.heading[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `HDG: ${data.heading[0]}°TRUE`; - retval.Content.push(extension); - } - if (data.track[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `TRK: ${data.track[0]}°`; - retval.Content.push(extension); - } - - if (data.deviating[0]) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = `DEVIATING ${AtsuCommon.InputValidation.expandLateralOffset(data.deviating[0])}`; - retval.Content.push(extension); - } - - return retval; - } - - static ShowPage1(mcdu, requestMessage = null, data = CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, true)) { - mcdu.page.Current = mcdu.page.ATCPositionReport1; - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcPositionReport.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - } - if (CDUAtcPositionReport.CanEraseData(data)) { - erase = "*ERASE"; - } - - const overhead = ["[ ]", "[ ]", "[ ]"]; - if (data.passedWaypoint[0]) { - overhead[0] = data.passedWaypoint[0]; - } - if (data.passedWaypoint[1] && data.passedWaypoint[2]) { - overhead[1] = data.passedWaypoint[1]; - overhead[2] = data.passedWaypoint[2]; - } - if (data.passedWaypoint[3] === false) { - if (data.passedWaypoint[0]) { - overhead[0] = `{small}${overhead[0]}{end}`; - } - if (data.passedWaypoint[1] && data.passedWaypoint[2]) { - overhead[1] = `{small}${overhead[1]}{end}`; - overhead[2] = `{small}${overhead[2]}{end}`; - } - } - - const ppos = ["_______[color]amber", "____/______[color]amber"]; - if (data.currentPosition[0]) { - ppos[0] = `{cyan}${AtsuCommon.coordinateToString({ lat: data.currentPosition[0][0], lon: data.currentPosition[0][1] }, true)}{end}`; - if (data.currentPosition[1] === false) { - ppos[0] = `{small}${ppos[0]}{end}`; - } - } - if (data.currentUtc[0] && data.currentAltitude[0]) { - ppos[1] = `{cyan}${data.currentUtc[0]}/${data.currentAltitude[0]}{end}`; - if (data.currentUtc[1] === false) { - ppos[1] = `{small}${ppos[1]}{end}`; - } - } - - const to = ["[ ]", "[ ]"]; - if (data.activeWaypoint[0]) { - to[0] = data.activeWaypoint[0]; - if (data.activeWaypoint[2] === false) { - to[0] = `{small}${to[0]}{end}`; - } - } - if (data.activeWaypoint[1]) { - to[1] = data.activeWaypoint[1]; - if (data.activeWaypoint[2] === false) { - to[1] = `{small}${to[1]}{end}`; - } - } - - let next = "[ ]"; - if (data.nextWaypoint[0]) { - next = data.nextWaypoint[0]; - if (data.nextWaypoint[1] === false) { - next = `{small}${next}{end}`; - } - } - - mcdu.setTemplate([ - ["POSITION REPORT", "1", "3"], - ["\xa0OVHD-----------UTC/ALT"], - [`{cyan}${overhead[0]}{end}`, `{cyan}${overhead[1]}/${overhead[2]}`], - ["\xa0PPOS-----------UTC/ALT"], - [ppos[0], ppos[1]], - ["\xa0TO-----------------UTC"], - [`{cyan}${to[0]}{end}`, `{cyan}${to[1]}{end}`], - ["\xa0NEXT"], - [`{cyan}${next}{end}`], - ["\xa0ALL FIELDS"], - [erase, text], - [`${requestMessage ? "\xa0ATC MENU" : "\xa0ATC REPORTS"}`, "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.passedWaypoint[0] = null; - data.passedWaypoint[3] = true; - } else if (value) { - if (Fmgc.WaypointEntryUtils.isLatLonFormat(value)) { - // format: DDMM.MB/EEEMM.MC - try { - Fmgc.WaypointEntryUtils.parseLatLon(value); - data.passedWaypoint[0] = value; - data.passedWaypoint[3] = true; - } catch (err) { - if (err === NXSystemMessages.formatError) { - mcdu.setScratchpadMessage(err); - } - }; - } else if (/^[A-Z0-9]{2,7}/.test(value)) { - // place format - mcdu.dataManager.GetWaypointsByIdent.bind(mcdu.dataManager)(value).then((waypoints) => { - if (waypoints.length === 0) { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } else { - data.passedWaypoint[0] = value; - data.passedWaypoint[3] = true; - } - - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }); - } - } - - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.currentPosition[0] = null; - data.currentPosition[1] = true; - } else if (value && Fmgc.WaypointEntryUtils.isLatLonFormat(value)) { - // format: DDMM.MB/EEEMM.MC - try { - Fmgc.WaypointEntryUtils.parseLatLon(value); - data.currentPosition[0] = value; - data.currentPosition[1] = true; - } catch (err) { - if (err === NXSystemMessages.formatError) { - mcdu.setScratchpadMessage(err); - } - }; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } - - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.activeWaypoint[0] = null; - data.activeWaypoint[2] = true; - } else if (value) { - if (Fmgc.WaypointEntryUtils.isLatLonFormat(value)) { - // format: DDMM.MB/EEEMM.MC - try { - Fmgc.WaypointEntryUtils.parseLatLon(value); - data.activeWaypoint[0] = value; - data.activeWaypoint[2] = true; - } catch (err) { - if (err === NXSystemMessages.formatError) { - mcdu.setScratchpadMessage(err); - } - }; - } else if (/^[A-Z0-9]{2,7}/.test(value)) { - // place format - mcdu.dataManager.GetWaypointsByIdent.bind(mcdu.dataManager)(value).then((waypoints) => { - if (waypoints.length === 0) { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } else { - data.activeWaypoint[0] = value; - data.activeWaypoint[2] = true; - } - - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }); - } - } - - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.nextWaypoint[0] = null; - data.nextWaypoint[1] = true; - } else if (value) { - if (Fmgc.WaypointEntryUtils.isLatLonFormat(value)) { - // format: DDMM.MB/EEEMM.MC - try { - Fmgc.WaypointEntryUtils.parseLatLon(value); - data.nextWaypoint[0] = value; - data.nextWaypoint[1] = true; - } catch (err) { - if (err === NXSystemMessages.formatError) { - mcdu.setScratchpadMessage(err); - } - }; - } else if (/^[A-Z0-9]{2,7}/.test(value)) { - // place format - mcdu.dataManager.GetWaypointsByIdent.bind(mcdu.dataManager)(value).then((waypoints) => { - if (waypoints.length === 0) { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } else { - data.nextWaypoint[0] = value; - data.nextWaypoint[1] = true; - } - - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }); - } - } - - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, false)); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - if (requestMessage) { - CDUAtcMenu.ShowPage(mcdu); - } else { - CDUAtcReports.ShowPage(mcdu); - } - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.passedWaypoint[1] = null; - data.passedWaypoint[2] = null; - data.passedWaypoint[3] = true; - } else { - const elements = value.split("/"); - if (elements.length === 2) { - const timeError = AtsuCommon.InputValidation.validateScratchpadTime(elements[0], false); - const altError = AtsuCommon.InputValidation.validateScratchpadAltitude(elements[1]); - - if (timeError !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(timeError); - } else if (altError !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(altError); - } else { - data.passedWaypoint[1] = elements[0].length === 5 ? elements[0].substring(0, 4) : elements[0]; - data.passedWaypoint[2] = AtsuCommon.InputValidation.formatScratchpadAltitude(elements[1]); - data.passedWaypoint[3] = true; - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } - } - - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.currentUtc = [null, true]; - data.currentAltitude = [null, true]; - } else { - const elements = value.split("/"); - if (elements.length === 2) { - const timeError = AtsuCommon.InputValidation.validateScratchpadTime(elements[0], false); - const altError = AtsuCommon.InputValidation.validateScratchpadAltitude(elements[1]); - - if (timeError !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(timeError); - } else if (altError !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(altError); - } else { - data.currentUtc = [elements[0].length === 5 ? elements[0].substring(0, 4) : elements[0], true]; - data.currentAltitude = [elements[1], true]; - } - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } - } - - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.activeWaypoint[1] = null; - data.activeWaypoint[2] = true; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadTime(value, false); - - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.activeWaypoint[1] = value.length === 5 ? value.substring(0, 4) : value; - data.activeWaypoint[2] = true; - } - } - - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcPositionReport.CanSendData(data)) { - const report = CDUAtcPositionReport.CreateReport(mcdu, data); - if (requestMessage) { - requestMessage.Response = report; - CDUAtcTextFansA.ShowPage1(mcdu, [requestMessage]); - } else { - CDUAtcTextFansA.ShowPage1(mcdu, [report]); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcPositionReport.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const report = CDUAtcPositionReport.CreateReport(mcdu, data); - if (requestMessage) { - requestMessage.Response = report; - mcdu.atsu.updateMessage(requestMessage); - } else { - mcdu.atsu.registerMessages([report]); - } - CDUAtcPositionReport.ShowPage1(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); - }; - mcdu.onNextPage = () => { - CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); - }; - } - - static ShowPage2(mcdu, requestMessage = null, data = CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, true)) { - mcdu.page.Current = mcdu.page.ATCPositionReport2; - - const wind = data.wind[0] ? data.wind[0].split("/") : ["[ ]", "[ ]"]; - const sat = data.sat[0] ? data.sat[0] : "[ ]"; - const turbulence = data.turbulence[0] ? data.turbulence[0] : "[ ]"; - const icing = data.icing[0] ? data.icing[0] : "[ ]"; - let eta = data.eta[0] ? data.eta[0] : "[ ]"; - if (data.eta[1] === false && data.eta[0]) { - eta = `{small}${eta}{end}`; - } - const endurance = data.endurance[0] ? data.endurance[0] : "[ ]"; - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcPositionReport.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - } - if (CDUAtcPositionReport.CanEraseData(data)) { - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["POSITION REPORT", "2", "3"], - ["\xa0WIND", "SAT\xa0"], - [`{cyan}${data.wind[1] === false && data.wind[0] ? '{small}' : ''}${wind[0]}/${wind[1]}${data.wind[1] === false && data.wind[0] ? '{end}' : ''}{end}`, `{cyan}${data.sat[1] === false && data.sat[0] ? '{small}' : ''}${sat}${data.sat[1] === false && data.sat[0] ? '{end}' : ''}{end}`], - ["\xa0ICING(TLMS)", "TURB(LMS)\xa0"], - [`{cyan}${icing}{end}`, `{cyan}${turbulence}{end}`], - ["\xa0ETA", "ENDURANCE\xa0"], - [`{cyan}${eta}{end}`, `{cyan}${endurance}{end}`], - [""], - [""], - ["\xa0ALL FIELDS"], - [erase, text], - [`${requestMessage ? "\xa0ATC MENU" : "\xa0ATC REPORTS"}`, "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.wind = [null, true]; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadWind(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.wind = [AtsuCommon.InputValidation.formatScratchpadWind(value), true]; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } - } - CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.icing = [null, true]; - } else if (value === "T" || value === "L" || value === "M" || value === "S") { - data.icing = [value, true]; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - } - CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.eta = [null, true]; - } else if (AtsuCommon.InputValidation.validateScratchpadTime(value)) { - data.eta = [value, true]; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } - CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, false)); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - if (requestMessage) { - CDUAtcMenu.ShowPage(mcdu); - } else { - CDUAtcReports.ShowPage(mcdu); - } - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.sat = [null, true]; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadTemperature(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.sat = [AtsuCommon.InputValidation.formatScratchpadTemperature(value), true]; - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.turbulence = [null, true]; - } else if (value === "L" || value === "M" || value === "S") { - data.turbulence = [value, true]; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - } - CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.endurance = [null, true]; - } else if (AtsuCommon.InputValidation.validateScratchpadEndurance(value)) { - data.endurance = [AtsuCommon.InputValidation.formatScratchpadEndurance(value), true]; - } else { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } - CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcPositionReport.CanSendData(data)) { - const report = CDUAtcPositionReport.CreateReport(mcdu, data); - if (requestMessage) { - requestMessage.Response = report; - CDUAtcTextFansA.ShowPage1(mcdu, [requestMessage]); - } else { - CDUAtcTextFansA.ShowPage1(mcdu, [report]); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcPositionReport.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const report = CDUAtcPositionReport.CreateReport(mcdu, data); - if (requestMessage) { - requestMessage.Response = report; - mcdu.atsu.updateMessage(requestMessage); - } else { - mcdu.atsu.registerMessages([report]); - } - CDUAtcPositionReport.ShowPage1(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }; - mcdu.onNextPage = () => { - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); - }; - } - - static ShowPage3(mcdu, requestMessage = null, data = CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, true)) { - mcdu.page.Current = mcdu.page.ATCPositionReport3; - - let indicatedAirspeed = data.indicatedAirspeed[0] ? data.indicatedAirspeed[0] : "[ ]"; - if (data.indicatedAirspeed[0] && data.indicatedAirspeed[1] === false) { - indicatedAirspeed = `{small}${indicatedAirspeed}{end}`; - } - let groundSpeed = data.groundSpeed[0] ? data.groundSpeed[0] : "[ ]"; - if (data.groundSpeed[0] && data.groundSpeed[1] === false) { - groundSpeed = `{small}${groundSpeed}{end}`; - } - let verticalSpeed = data.verticalSpeed[0] ? data.verticalSpeed[0] : "[ ]"; - if (data.verticalSpeed[0] && data.verticalSpeed[1] === false) { - verticalSpeed = `{small}${verticalSpeed}{end}`; - } - const deviating = data.deviating[0] ? data.deviating[0] : "[ ]"; - let heading = data.heading[0] ? `${data.heading[0]}°TRUE` : "[ ]"; - if (data.heading[0] && data.heading[1] === false) { - heading = `{small}${heading}{end}`; - } - let track = data.track[0] ? `${data.track[0]}{white}°{end}` : "[ ]"; - if (data.track[0] && data.track[1] === false) { - track = `{small}${track}{end}`; - } - const descending = ["\xa0DSCENDING TO", "[ ]"]; - const climbing = ["CLBING TO\xa0", "[ ]"]; - - const current = data.atsuFlightStateData; - const target = data.atsuAutopilotData; - if (target.apActive && target.altitude === current.altitude) { - descending[0] = descending[1] = ""; - climbing[0] = climbing[1] = ""; - } else if (data.climbing[0]) { - descending[0] = descending[1] = ""; - climbing[1] = data.climbing[0]; - } else if (data.descending[0]) { - climbing[0] = climbing[1] = ""; - descending = data.descending[0]; - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcPositionReport.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - } - if (CDUAtcPositionReport.CanEraseData(data)) { - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["POSITION REPORT", "3", "3"], - ["\xa0SPEED", "GROUND SPD\xa0"], - [`{cyan}${indicatedAirspeed}{end}`, `{cyan}${groundSpeed}{end}`], - ["\xa0VERT SPEED", "DEVIATING\xa0"], - [`{cyan}${verticalSpeed}{end}`, `{cyan}${deviating}{end}`], - ["\xa0HEADING", "TRACK ANGLE\xa0"], - [`{cyan}${heading}{end}`, `{cyan}${track}{end}`], - [descending[0], climbing[0]], - [`{cyan}${descending[1]}{end}`, `{cyan}${climbing[1]}{end}`], - ["\xa0ALL FIELDS"], - [erase, text], - [`${requestMessage ? "\xa0ATC MENU" : "\xa0ATC REPORTS"}`, "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.indicatedAirspeed = [null, true]; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadSpeed(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.indicatedAirspeed = [AtsuCommon.InputValidation.formatScratchpadSpeed(value), true]; - } else { - mcdu.addNewAtsuMessage(error); - } - } - - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.verticalSpeed = [null, true]; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadVerticalSpeed(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.verticalSpeed = [AtsuCommon.InputValidation.formatScratchpadVerticalSpeed(value), true]; - } else { - mcdu.addNewAtsuMessage(error); - } - } - - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.heading = [null, true]; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadDegree(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.heading = [value, true]; - } else { - mcdu.addNewAtsuMessage(error); - } - } - - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - const current = data.atsuFlightStateData; - const target = data.atsuAutopilotData; - - if (!target.apActive || (target.apActive && target.altitude !== current.altitude)) { - if (value === FMCMainDisplay.clrValue) { - data.descending = [null, true]; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.descending = [AtsuCommon.InputValidation.formatScratchpadAltitude(value), true]; - } else { - mcdu.addNewAtsuMessage(error); - } - } - } - - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, false)); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - if (requestMessage) { - CDUAtcMenu.ShowPage(mcdu); - } else { - CDUAtcReports.ShowPage(mcdu); - } - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.groundSpeed = [null, true]; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadSpeed(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.groundSpeed = [AtsuCommon.InputValidation.formatScratchpadSpeed(value), true]; - } else { - mcdu.addNewAtsuMessage(error); - } - } - - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.deviating = [null, true]; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.deviating = [AtsuCommon.InputValidation.formatScratchpadOffset(value), true]; - } else { - mcdu.addNewAtsuMessage(error); - } - } - - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.track = [null, true]; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadDegree(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.track = [value, true]; - } else { - mcdu.addNewAtsuMessage(error); - } - } - - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); - }; - - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = (value) => { - const current = mcdu.atsu.currentFlightState(); - const target = mcdu.atsu.targetFlightState(); - - if (!target.apActive || (target.apActive && target.altitude !== current.altitude)) { - if (value === FMCMainDisplay.clrValue) { - data.climbing = [null, true]; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.climbing = [AtsuCommon.InputValidation.formatScratchpadAltitude(value), true]; - } else { - mcdu.addNewAtsuMessage(error); - } - } - } - - CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcPositionReport.CanSendData(data)) { - const report = CDUAtcPositionReport.CreateReport(mcdu, data); - if (requestMessage) { - requestMessage.Response = report; - CDUAtcTextFansA.ShowPage1(mcdu, [requestMessage]); - } else { - CDUAtcTextFansA.ShowPage1(mcdu, [report]); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcPositionReport.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const report = CDUAtcPositionReport.CreateReport(mcdu, data); - if (requestMessage) { - requestMessage.Response = report; - mcdu.atsu.updateMessage(requestMessage); - } else { - mcdu.atsu.registerMessages([report]); - } - CDUAtcPositionReport.ShowPage1(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); - }; - mcdu.onNextPage = () => { - CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_ProcedureRequest.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_ProcedureRequest.js deleted file mode 100644 index a4a83c6c84b..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_ProcedureRequest.js +++ /dev/null @@ -1,203 +0,0 @@ -class CDUAtcProcedureRequest { - static CreateDataBlock() { - return { - sid: null, - departureTransition: null, - star: null, - arrivalTransition: null, - }; - } - - static CanSendData(data) { - return data.sid || data.departureTransition || data.star || data.arrivalTransition; - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - if (data.sid) { - retval.push(CDUAtcProcedureRequest.CreateRequest(mcdu, "DM23", [data.sid])); - } - if (data.departureTransition) { - retval.push(CDUAtcProcedureRequest.CreateRequest(mcdu, "DM23", [data.departureTransition])); - } - if (data.star) { - retval.push(CDUAtcProcedureRequest.CreateRequest(mcdu, "DM23", [data.star])); - } - if (data.arrivalTransition) { - retval.push(CDUAtcProcedureRequest.CreateRequest(mcdu, "DM23", [data.arrivalTransition])); - } - - return retval; - } - - static ShowPage(mcdu, data = CDUAtcProcedureRequest.CreateDataBlock()) { - mcdu.clearDisplay(); - - let sid = "[ ][color]cyan"; - let star = "[ ][color]cyan"; - let arrivalTransition = "[ ][color]cyan"; - let departureTransition = "[ ][color]cyan"; - - if (data.sid) { - sid = `${data.sid}[color]cyan`; - } - if (data.star) { - star = `${data.star}[color]cyan`; - } - if (data.arrivalTransition) { - arrivalTransition = `${data.arrivalTransition}[color]cyan`; - } - if (data.departureTransition) { - departureTransition = `${data.departureTransition}[color]cyan`; - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcProcedureRequest.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["PROCEDURE REQ"], - ["\xa0SID--------------TRANS\xa0"], - [sid, departureTransition], - ["\xa0STAR---------------VIA\xa0"], - [star, arrivalTransition], - [""], - [""], - [""], - [""], - ["\xa0ALL FIELDS"], - [erase, text], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.sid = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadPosition(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.sid = value; - } - } - CDUAtcProcedureRequest.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.star = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadPosition(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.star = value; - } - } - CDUAtcProcedureRequest.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcProcedureRequest.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.departureTransition = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadPosition(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.departureTransition = value; - } - } - CDUAtcProcedureRequest.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.arrivalTransition = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadPosition(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.arrivalTransition = value; - } - } - CDUAtcProcedureRequest.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcProcedureRequest.CanSendData(data)) { - const messages = CDUAtcProcedureRequest.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcProcedureRequest.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcProcedureRequest.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcProcedureRequest.ShowPage(mcdu); - } - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_Reports.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_Reports.js deleted file mode 100644 index 09c43bd9190..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_Reports.js +++ /dev/null @@ -1,174 +0,0 @@ -class CDUAtcReports { - static CreateDataBlock() { - return { - backOnRoute: false, - deviating: null, - updateInProgress: false, - }; - } - - static CanSendData(data) { - return data.requestContact || data.deviating; - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - if (data.requestContact) { - retval.push(CDUAtcReports.CreateRequest(mcdu, "DM20")); - } - if (data.deviating) { - const elements = AtsuCommon.InputValidation.expandLateralOffset(data.deviating).split(" "); - retval.push(CDUAtcReports.CreateRequest(mcdu, "DM80", [elements[0], elements[1]])); - } - - return retval; - } - - static ShowPage(mcdu, data = CDUAtcReports.CreateDataBlock()) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCReports; - - let backOnRoute = "{cyan}{{end}BACK ON RTE"; - if (data.backOnRoute) { - backOnRoute = "{cyan}\xa0BACK ON RTE{end}"; - } - let deviating = "{cyan}[ ]{end}"; - if (data.deviating) { - deviating = `{cyan}${data.deviating}{end}`; - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcReports.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["REPORTS"], - [""], - [backOnRoute], - ["\xa0DEVIATING"], - [deviating], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.backOnRoute = false; - } else { - data.backOnRoute = true; - } - CDUAtcReports.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.deviating = null; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.deviating = AtsuCommon.InputValidation.formatScratchpadOffset(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcReports.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - CDUAtcPositionReport.ShowPage1(mcdu); - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = () => { - mcdu.atsu.toggleAutomaticPositionReportActive().then((status) => { - if (status !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(status); - } - - data.updateInProgress = false; - CDUAtcReports.ShowPage(mcdu, data); - }); - - data.updateInProgress = true; - CDUAtcReports.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcReports.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcReports.CanSendData(data)) { - const messages = CDUAtcReports.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcReports.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcReports.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcReports.ShowPage(mcdu); - } - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_Text.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_Text.js deleted file mode 100644 index 71bad2f5480..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_Text.js +++ /dev/null @@ -1,412 +0,0 @@ -class CDUAtcTextFansA { - static CreateDataBlock() { - return { - performance: false, - weather: false, - turbulence: false, - discretion: false, - icing: false, - freetext: [ "", "", "", "", "" ] - }; - } - - static CanSendData(messages, data) { - if (messages.length !== 0) { - return true; - } - - const freetext = data.freetext.filter((n) => n); - return freetext.length !== 0; - } - - static CanEraseData(data) { - if (data.performance || data.weather || data.turbulence || data.discretion || data.icing) { - return true; - } - const freetext = data.freetext.filter((n) => n); - return freetext.length !== 0; - } - - static CreateMessages(mcdu, messages, data) { - const freetextLines = data.freetext.filter((n) => n); - let freetextElement = null; - let updateFreetext = true; - - // create the freetext elements - if (freetextLines.length !== 0) { - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansB) { - freetextElement = AtsuCommon.CpdlcMessagesDownlink["DM98"][1].deepCopy(); - } else { - freetextElement = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - } - freetextElement.Content[0].Value = freetextLines.join("\n"); - } - - // create the extensions - let extension = null; - if (data.performance) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM66"][1].deepCopy(); - } else if (data.weather) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM65"][1].deepCopy(); - } else if (data.turbulence) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = 'DUE TO TURBULENCE'; - } else if (data.discretion) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM75"][1].deepCopy(); - } else if (data.icing) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM67"][1].deepCopy(); - extension.Content[0].Value = 'DUE TO ICING'; - } - - if (messages.length === 0) { - // the freetext is set (guaranteed due to CanSendData) - messages.push(new AtsuCommon.CpdlcMessage()); - messages[0].Content.push(freetextElement); - messages[0].Station = mcdu.atsu.currentStation(); - updateFreetext = false; - } - - // update all messages, if needed - if (extension || (updateFreetext && freetextElement)) { - messages.forEach((message) => { - if (message.Content[0].TypeId.includes("UM")) { - if (updateFreetext && freetextElement) { - message.Response.Content.push(freetextElement); - } - if (extension) { - message.Response.Content.push(extension); - } - } else { - if (updateFreetext && freetextElement) { - message.Content.push(freetextElement); - } - if (extension) { - message.Content.push(extension); - } - } - }); - } - - return messages; - } - - static ShowPage1(mcdu, messages = [], data = CDUAtcTextFansA.CreateDataBlock()) { - mcdu.clearDisplay(); - - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcTextFansA.CanSendData(messages, data)) { - reqDisplay = "DCDU*[color]cyan"; - } - if (CDUAtcTextFansA.CanEraseData(data)) { - erase = "*ERASE"; - } - - const acPerform = ["\xa0DUE TO", "{cyan}{{end}A/C PERFORM"]; - if (data.performance) { - acPerform[0] += "[color]cyan"; - acPerform[1] = "\xa0A/C PERFORM[color]cyan"; - } - const weather = ["DUE TO\xa0", "WEATHER{cyan}}{end}"]; - if (data.weather) { - weather[0] += "[color]cyan"; - weather[1] = "WEATHER\xa0[color]cyan"; - } - const turbulence = ["DUE TO\xa0", "TURBULENCE{cyan}}{end}"]; - if (data.turbulence) { - turbulence[0] += "[color]cyan"; - turbulence[1] = "TURBULENCE\xa0[color]cyan"; - } - const discretion = ["\xa0AT PILOTS", "{cyan}{{end}DISCRETION"]; - if (data.discretion) { - discretion[0] += "[color]cyan"; - discretion[1] = "\xa0DISCRETION[color]cyan"; - } - const icing = ["DUE TO\xa0", "ICING{cyan}}{end}"]; - if (data.icing) { - icing[0] += "[color]cyan"; - icing[1] = "ICING\xa0[color]cyan"; - } - let freetext = "[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0][color]cyan"; - if (data.freetext[0] !== "") { - freetext = data.freetext[0]; - } - - mcdu.setTemplate([ - ["FREE TEXT", "1", "2"], - [acPerform[0], weather[0]], - [acPerform[1], weather[1]], - [discretion[0], turbulence[0]], - [discretion[1], turbulence[1]], - ["", icing[0]], - ["", icing[1]], - ["---------FREE TEXT---------"], - [freetext], - ["\xa0ALL FIELDS"], - [erase], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.performance = false; - } else { - const oldFreetext = data.freetext; - data = CDUAtcTextFansA.CreateDataBlock(); - data.performance = true; - data.freetext = oldFreetext; - } - CDUAtcTextFansA.ShowPage1(mcdu, messages, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.discretion = false; - } else { - const oldFreetext = data.freetext; - data = CDUAtcTextFansA.CreateDataBlock(); - data.discretion = true; - data.freetext = oldFreetext; - } - CDUAtcTextFansA.ShowPage1(mcdu, messages, data); - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.freetext[0] = ""; - } else if (value) { - data.freetext[0] = value; - } - CDUAtcTextFansA.ShowPage1(mcdu, messages, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcTextFansA.ShowPage1(mcdu, messages); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.weather = false; - } else { - const oldFreetext = data.freetext; - data = CDUAtcTextFansA.CreateDataBlock(); - data.weather = true; - data.freetext = oldFreetext; - } - CDUAtcTextFansA.ShowPage1(mcdu, messages, data); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.turbulence = false; - } else { - const oldFreetext = data.freetext; - data = CDUAtcTextFansA.CreateDataBlock(); - data.turbulence = true; - data.freetext = oldFreetext; - } - CDUAtcTextFansA.ShowPage1(mcdu, messages, data); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.icing = false; - } else { - const oldFreetext = data.freetext; - data = CDUAtcTextFansA.CreateDataBlock(); - data.icing = true; - data.freetext = oldFreetext; - } - CDUAtcTextFansA.ShowPage1(mcdu, messages, data); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcTextFansA.CanSendData(messages, data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const prepMessages = CDUAtcTextFansA.CreateMessages(mcdu, messages, data); - if (prepMessages && prepMessages[0].Content[0].TypeId.includes("UM")) { - mcdu.atsu.updateMessage(prepMessages[0]); - } else if (prepMessages) { - mcdu.atsu.registerMessages(prepMessages); - } - CDUAtcTextFansA.ShowPage1(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcTextFansA.ShowPage2(mcdu, messages, data); - }; - mcdu.onNextPage = () => { - CDUAtcTextFansA.ShowPage2(mcdu, messages, data); - }; - } - - static ShowPage2(mcdu, messages = [], data = CDUAtcTextFansA.CreateDataBlock()) { - mcdu.clearDisplay(); - - let freetext1 = "[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0][color]cyan"; - if (data.freetext[1] !== "") { - freetext1 = data.freetext[1]; - } - let freetext2 = "[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0][color]cyan"; - if (data.freetext[2] !== "") { - freetext2 = data.freetext[2]; - } - let freetext3 = "[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0][color]cyan"; - if (data.freetext[3] !== "") { - freetext3 = data.freetext[3]; - } - let freetext4 = "[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0][color]cyan"; - if (data.freetext[4] !== "") { - freetext4 = data.freetext[4]; - } - - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcTextFansA.CanSendData(messages, data)) { - reqDisplay = "DCDU*[color]cyan"; - } - if (CDUAtcTextFansA.CanEraseData(data)) { - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["FREE TEXT", "2", "2"], - [""], - [freetext1], - [""], - [freetext2], - [""], - [freetext3], - [""], - [freetext4], - ["\xa0ALL FIELDS"], - [erase], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.freetext[1] = ""; - } else if (value) { - data.freetext[1] = value; - } - CDUAtcTextFansA.ShowPage2(mcdu, messages, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.freetext[2] = ""; - } else if (value) { - data.freetext[2] = value; - } - CDUAtcTextFansA.ShowPage2(mcdu, messages, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.freetext[3] = ""; - } else if (value) { - data.freetext[3] = value; - } - CDUAtcTextFansA.ShowPage2(mcdu, messages, data); - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.freetext[4] = ""; - } else if (value) { - data.freetext[4] = value; - } - CDUAtcTextFansA.ShowPage2(mcdu, messages, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcTextFansA.ShowPage2(mcdu, messages); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcTextFansA.CanSendData(messages, data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const prepMessages = CDUAtcTextFansA.CreateMessages(mcdu, messages, data); - if (prepMessages && prepMessages[0].Content[0].TypeId.includes("UM")) { - mcdu.atsu.updateMessage(prepMessages[0]); - } else if (prepMessages) { - mcdu.atsu.registerMessages(prepMessages); - } - CDUAtcTextFansA.ShowPage2(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcTextFansA.ShowPage1(mcdu, messages, data); - }; - mcdu.onNextPage = () => { - CDUAtcTextFansA.ShowPage1(mcdu, messages, data); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_UsualRequest.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_UsualRequest.js deleted file mode 100644 index 753a2d81062..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_UsualRequest.js +++ /dev/null @@ -1,328 +0,0 @@ -class CDUAtcUsualRequestFansA { - static CreateDataBlock() { - return { - directTo: null, - speed: null, - heading: null, - weatherDeviation: null, - climbTo: null, - descentTo: null, - dueToWeather: false, - requestDescent: false, - }; - } - - static CanSendData(data) { - return data.directTo || data.speed || data.heading || data.weatherDeviation || data.climbTo || data.descentTo || data.requestDescent; - } - - static CanEraseData(data) { - return data.directTo || data.speed || data.heading || data.weatherDeviation || data.climbTo || data.descentTo || data.dueToWeather || data.requestDescent; - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - let extension = null; - if (data.dueToWeather) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM65"][1].deepCopy(); - } - - if (data.directTo) { - retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, "DM22", [data.directTo])); - } - if (data.weatherDeviation) { - const elements = AtsuCommon.InputValidation.expandLateralOffset(data.weatherDeviation).split(" "); - retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, "DM27", [elements[0], elements[1]])); - } - if (data.heading) { - retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, "DM70", [data.heading === 0 ? "360" : data.heading.toString()])); - } - if (data.climbTo) { - retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, "DM9", [data.climbTo])); - } - if (data.descentTo) { - retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, "DM10", [data.descentTo])); - } - if (data.requestDescent) { - retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, "DM67")); - retval[retval.length - 1].Content[0].Content[0].Value = "REQUEST DESCENT"; - } - if (data.speed) { - retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, "DM18", [data.speed])); - } - - if (extension) { - retval.forEach((message) => { - if (message.Content[0].TypeId !== "DM27") { - message.Content.push(extension); - } - }); - } - - return retval; - } - - static ShowPage(mcdu, data = CDUAtcUsualRequestFansA.CreateDataBlock()) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCUsualRequest; - - let addText = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcUsualRequestFansA.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - addText = "ADD TEXT>"; - } - if (CDUAtcUsualRequestFansA.CanEraseData(data)) { - erase = "*ERASE"; - } - - let directTo = "{cyan}[ ]{end}"; - if (data.directTo) { - directTo = `${data.directTo}[color]cyan`; - } - let heading = "[ ]°[color]cyan"; - if (data.heading !== null) { - heading = `${data.heading}°[color]cyan`; - } - let weatherDeviation = "{cyan}[ ]{end}"; - if (data.weatherDeviation) { - weatherDeviation = `${data.weatherDeviation}[color]cyan`; - } - let speed = "[ ][color]cyan"; - if (data.speed) { - speed = `${data.speed}[color]cyan`; - } - let climbTo = "[ ][color]cyan"; - let descentTo = "[ ][color]cyan"; - if (data.climbTo) { - climbTo = `${data.climbTo}[color]cyan`; - } - if (data.descentTo) { - descentTo = `${data.descentTo}[color]cyan`; - } - - const dueToWeather = ["\xa0DUE TO", "{cyan}{{end}WEATHER"]; - const requestDescent = ["REQUEST\xa0", "DESCENT{cyan}}{end}"]; - if (data.dueToWeather) { - dueToWeather[0] = "{cyan}\xa0DUE TO{end}"; - dueToWeather[1] = "{cyan}\xa0WEATHER{end}"; - } - if (data.requestDescent) { - requestDescent[0] = "{cyan}REQUEST\xa0{end}"; - requestDescent[1] = "{cyan}DESCENT\xa0{end}"; - } - - mcdu.setTemplate([ - ["USUAL REQ"], - ["\xa0DIR TO", "SPEED\xa0"], - [directTo, speed], - ["\xa0HDG", "WX DEV\xa0"], - [heading, weatherDeviation], - ["\xa0CLB TO", "DES TO\xa0"], - [climbTo, descentTo], - [dueToWeather[0], requestDescent[0]], - [dueToWeather[1], requestDescent[1]], - ["\xa0ALL FIELDS"], - [erase, addText], - ["\xa0ATC MENU", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.directTo = null; - } else if (value) { - if (Fmgc.WaypointEntryUtils.isLatLonFormat(value)) { - // format: DDMM.MB/EEEMM.MC - try { - Fmgc.WaypointEntryUtils.parseLatLon(value); - data.directTo = value; - } catch (err) { - if (err === NXSystemMessages.formatError) { - mcdu.setScratchpadMessage(err); - } - }; - } else if (/^[A-Z0-9]{2,7}/.test(value)) { - // place format - mcdu.dataManager.GetWaypointsByIdent.bind(mcdu.dataManager)(value).then((waypoints) => { - if (waypoints.length === 0) { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } else { - data.directTo = value; - } - - CDUAtcUsualRequestFansA.ShowPage(mcdu, data); - }); - } - } - - CDUAtcUsualRequestFansA.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.heading = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadDegree(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.heading = parseInt(value) % 360; - } - } - - CDUAtcUsualRequestFansA.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.climbTo = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.climbTo = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcUsualRequestFansA.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.dueToWeather = false; - } else { - data.dueToWeather = true; - } - CDUAtcUsualRequestFansA.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcUsualRequestFansA.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.speed = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadSpeed(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.speed = AtsuCommon.InputValidation.formatScratchpadSpeed(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcUsualRequestFansA.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = async (value) => { - if (value === FMCMainDisplay.clrValue) { - data.weatherDeviation = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.weatherDeviation = AtsuCommon.InputValidation.formatScratchpadOffset(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcUsualRequestFansA.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.descentTo = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.descentTo = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcUsualRequestFansA.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.requestDescent = false; - } else { - data.requestDescent = true; - } - CDUAtcUsualRequestFansA.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcUsualRequestFansA.CanSendData(data)) { - const requests = CDUAtcUsualRequestFansA.CreateRequests(mcdu, data); - if (requests.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, requests); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcUsualRequestFansA.CanSendData(data)) { - const requests = CDUAtcUsualRequestFansA.CreateRequests(mcdu, data); - if (requests.length !== 0) { - mcdu.atsu.registerMessages(requests); - CDUAtcUsualRequestFansA.ShowPage(mcdu); - } - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_VertRequest.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_VertRequest.js deleted file mode 100644 index 27cd1bcfd56..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansA/A320_Neo_CDU_ATC_VertRequest.js +++ /dev/null @@ -1,548 +0,0 @@ -class CDUAtcVertRequestFansA { - static CreateDataBlock() { - return { - climb: null, - climbStart: null, - descend: null, - descendStart: null, - startAt: null, - whenHigher: false, - whenLower: false, - altitude: null, - blockAltitudeLow: null, - blockAltitudeHigh: null, - requestDescent: false, - cruise: null, - }; - } - - static CanSendData(data) { - return data.climb || data.climbStart || data.descendStart || data.descend || data.startAt || data.altitude || data.whenHigher || data.whenLower || - data.blockAltitudeLow || data.blockAltitudeHigh || data.cruise || data.requestDescent; - } - - static HandleClbDesStart(mcdu, value, data, climbRequest) { - if (value === FMCMainDisplay.clrValue || !value) { - if (climbRequest) { - data.climbStart = null; - } else { - data.descendStart = null; - } - data.startAt = null; - } else { - const entries = value.split('/'); - let updateAlt = false; - let altitude = null; - let start = null; - - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(entries[0]); - if (!error) { - updateAlt = true; - altitude = AtsuCommon.InputValidation.formatScratchpadAltitude(entries[0]); - entries.shift(); - } - - if (entries.length !== 0) { - const startingPoint = entries.join("/"); - - const type = AtsuCommon.InputValidation.classifyScratchpadWaypointType(startingPoint, true); - if (altitude || (data.climb && climbRequest || data.descend && !climbRequest)) { - switch (type[0]) { - case AtsuCommon.InputWaypointType.GeoCoordinate: - case AtsuCommon.InputWaypointType.Place: - start = startingPoint; - break; - case AtsuCommon.InputWaypointType.Timepoint: - if (startingPoint.endsWith("Z")) { - start = startingPoint; - } else { - start = `${startingPoint}Z`; - } - break; - default: - mcdu.addNewAtsuMessage(type[1]); - start = null; - if (updateAlt) { - altitude = null; - } - break; - } - } - - if (altitude || start) { - if (altitude && start) { - data.startAt = start; - if (climbRequest) { - data.climbStart = altitude; - } else { - data.descendStart = altitude; - } - } else if (altitude) { - // update the altitude and keep the start at - const lastStart = data.startAt; - data.startAt = lastStart; - if (climbRequest) { - data.climbStart = altitude; - } else { - data.descendStart = altitude; - } - } else if (start && (data.climbStart || data.descendStart)) { - // update start at if climb or descend are set - data.startAt = start; - } - } - - CDUAtcVertRequestFansA.ShowPage2(mcdu, data); - } else if (updateAlt) { - if (climbRequest) { - data.climbStart = altitude; - } else { - data.descendStart = altitude; - } - } else if (error) { - mcdu.addNewAtsuMessage(error); - } - } - - CDUAtcVertRequestFansA.ShowPage2(mcdu, data); - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - if (data.climb) { - retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, "DM9", [data.climb])); - } - if (data.climbStart) { - retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, /$[0-9]{4}Z^/.test(data.startAt) ? "DM13" : "DM11", [data.startAt, data.climbStart])); - } - if (data.descend) { - retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, "DM10", [data.descend])); - } - if (data.descendStart) { - retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, /$[0-9]{4}Z^/.test(data.startAt) ? "DM14" : "DM12", [data.startAt, data.descendStart])); - } - if (data.altitude) { - retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, "DM6", [data.altitude])); - } - if (data.whenHigher) { - retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, "DM53")); - } - if (data.whenLower) { - retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, "DM52")); - } - if (data.blockAltitudeLow && data.blockAltitudeHigh) { - retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, "DM7", [data.blockAltitudeLow, data.blockAltitudeHigh])); - } - if (data.requestDescent) { - retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, "DM67")); - retval[retval.length - 1].Content[0].Content[0].Value = "REQUEST DESCENT"; - } - if (data.cruise) { - retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, data.whenCruise ? "DM54" : "DM8", [data.cruise])); - } - - return retval; - } - - static ShowPage1(mcdu, data = CDUAtcVertRequestFansA.CreateDataBlock()) { - mcdu.clearDisplay(); - - let climbTo = "[ ][color]cyan"; - let descentTo = "[ ][color]cyan"; - if (data.climb) { - climbTo = `${data.climb}[color]cyan`; - } - if (data.descend) { - descentTo = `${data.descend}[color]cyan`; - } - let altitude = "[ ][color]cyan"; - if (data.altitude) { - altitude = `${data.altitude}[color]cyan`; - } - let requestDescentSmall = "REQUEST\xa0"; - let requestDescent = "DESCENT{cyan}}{end}"; - if (data.requestDescent) { - requestDescentSmall += "[color]cyan"; - requestDescent = "DESCENT\xa0[color]cyan"; - } - let blockAlt = "[ ]/[ ][color]cyan"; - if (data.blockAltitudeLow && data.blockAltitudeHigh) { - blockAlt = `${data.blockAltitudeLow}/${data.blockAltitudeHigh}[color]cyan`; - } - let crzClimb = "[ ][color]cyan"; - if (data.cruise && !data.whenCruise) { - crzClimb = `${data.cruise}[color]cyan`; - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcVertRequestFansA.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["ATC VERT REQ", "1", "2"], - ["\xa0CLB TO", "DES TO\xa0"], - [climbTo, descentTo], - ["\xa0ALT", requestDescentSmall], - [altitude, requestDescent], - ["\xa0BLOCK ALT/ALT"], - [blockAlt], - ["\xa0CRUISE CLB TO"], - [crzClimb], - ["\xa0ALL FIELDS"], - [erase, text], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.climb = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.climb = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcVertRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.altitude = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.altitude = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcVertRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.blockAltitudeLow = null; - data.blockAltitudeHigh = null; - } else if (value) { - const entries = value.split("/"); - if (entries.length !== 2) { - mcdu.setScratchpadMessage(NXSystemMessages.formatError); - } else { - const error = AtsuCommon.InputValidation.validateAltitudeRange(entries[0], entries[1]); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.blockAltitudeLow = AtsuCommon.InputValidation.formatScratchpadAltitude(entries[0]); - data.blockAltitudeHigh = AtsuCommon.InputValidation.formatScratchpadAltitude(entries[1]); - CDUAtcVertRequestFansA.ShowPage1(mcdu, data); - } - } - } - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.cruise = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.cruise = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcVertRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.cruise = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.cruise = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcVertRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcVertRequestFansA.ShowPage1(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.descend = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.descend = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcVertRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.requestDescent = false; - } else { - data.requestDescent = true; - } - CDUAtcVertRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - if (data.whenSpeedRange) { - data.speedLow = null; - data.speedHigh = null; - data.whenSpeedRange = false; - } - } else if (value) { - const range = AtsuCommon.InputValidation.validateScratchpadSpeedRanges(value); - if (range[0] !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(range[0]); - } else { - if (range[1].length === 2) { - data.speedLow = range[1][0]; - data.speedHigh = range[1][1]; - data.whenSpeedRange = true; - } - } - } - CDUAtcVertRequestFansA.ShowPage1(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcVertRequestFansA.CanSendData(data)) { - const messages = CDUAtcVertRequestFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcVertRequestFansA.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcVertRequestFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcVertRequestFansA.ShowPage1(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcVertRequestFansA.ShowPage2(mcdu, data); - }; - mcdu.onNextPage = () => { - CDUAtcVertRequestFansA.ShowPage2(mcdu, data); - }; - } - - static ShowPage2(mcdu, data = CDUAtcVertRequestFansA.CreateDataBlock()) { - mcdu.clearDisplay(); - - let climbStart = "[ ]/[ ][color]cyan"; - if (data.climbStart) { - climbStart = `${data.climbStart}/${data.startAt ? data.startAt : "[ ]"}[color]cyan`; - } - let descendStart = "[ ]/[ ][color]cyan"; - if (data.descendStart) { - descendStart = `${data.descendStart}/${data.startAt ? data.startAt : "[ ]"}[color]cyan`; - } - - let higherAlt = "{cyan}{{end}HIGHER ALT"; - if (data.whenHigher) { - higherAlt = "\xa0HIGHER ALT[color]cyan"; - } - let lowerAlt = "LOWER ALT{cyan}}{end}"; - if (data.whenLower) { - lowerAlt = "LOWER ALT\xa0[color]cyan"; - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcVertRequestFansA.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["ATC VERT REQ", "2", "2"], - ["\xa0CLB TO/START AT"], - [climbStart], - ["\xa0DES TO/START AT"], - [descendStart], - ["---WHEN CAN WE EXPECT---"], - [higherAlt, lowerAlt], - [""], - [""], - ["\xa0ALL FIELDS"], - [erase, text], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - CDUAtcVertRequestFansA.HandleClbDesStart(mcdu, value, data, true); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - CDUAtcVertRequestFansA.HandleClbDesStart(mcdu, value, data, false); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.whenHigher = false; - } else { - data.whenHigher = true; - data.whenLower = false; - } - CDUAtcVertRequestFansA.ShowPage2(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcVertRequestFansA.ShowPage2(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.whenLower = false; - } else { - data.whenHigher = false; - data.whenLower = true; - } - CDUAtcVertRequestFansA.ShowPage2(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcVertRequestFansA.CanSendData(data)) { - const messages = CDUAtcVertRequestFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansA.ShowPage2(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcVertRequestFansA.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcVertRequestFansA.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcVertRequestFansA.ShowPage2(mcdu); - } - } - }; - - mcdu.onPrevPage = () => { - CDUAtcVertRequestFansA.ShowPage1(mcdu, data); - }; - mcdu.onNextPage = () => { - CDUAtcVertRequestFansA.ShowPage1(mcdu, data); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_Emergency.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_Emergency.js deleted file mode 100644 index 3493efc62a8..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_Emergency.js +++ /dev/null @@ -1,29 +0,0 @@ -class CDUAtcEmergencyFansB { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCEmergency; - - mcdu.setTemplate([ - ["{amber}EMERGENCY{end}"], - ["", "EMERG ADS-C:OFF\xa0"], - ["", "{inop}SET ON*{end}"], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - ["\xa0ATC MENU"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_LatRequest.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_LatRequest.js deleted file mode 100644 index 7b3a4ecd0a1..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_LatRequest.js +++ /dev/null @@ -1,242 +0,0 @@ -class CDUAtcLatRequestFansB { - static CreateDataBlock() { - return { - directTo: null, - weatherDeviation: null, - }; - } - - static CanSendData(data) { - return data.directTo || data.weatherDeviation; - } - - static CanEraseData(data) { - return data.directTo || data.weatherDeviation; - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - if (data.directTo) { - retval.push(CDUAtcLatRequestFansB.CreateRequest(mcdu, "DM22", [data.directTo])); - } - if (data.weatherDeviation) { - const elements = AtsuCommon.InputValidation.expandLateralOffset(data.weatherDeviation).split(" "); - retval.push(CDUAtcLatRequestFansB.CreateRequest(mcdu, "DM27", [elements[0], elements[1]])); - } - - return retval; - } - - static ShowPage(mcdu, data = CDUAtcLatRequestFansB.CreateDataBlock()) { - mcdu.clearDisplay(); - - let weatherDeviation = "{cyan}[ ]{end}"; - if (data.weatherDeviation) { - weatherDeviation = `${data.weatherDeviation}[color]cyan`; - } - let directTo = "{cyan}[ ]{end}"; - if (data.directTo) { - directTo = `${data.directTo}[color]cyan`; - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcLatRequestFansB.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - } - if (CDUAtcLatRequestFansB.CanEraseData(data)) { - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["ATC LAT REQ"], - ["\xa0DIR TO[color]white"], - [directTo], - [""], - [""], - ["", "WX DEV"], - ["", weatherDeviation], - [""], - [""], - ["\xa0ALL FIELDS"], - [erase, text], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.directTo = null; - } else if (value) { - if (Fmgc.WaypointEntryUtils.isLatLonFormat(value)) { - // format: DDMM.MB/EEEMM.MC - try { - Fmgc.WaypointEntryUtils.parseLatLon(value); - data.directTo = value; - } catch (err) { - if (err === NXSystemMessages.formatError) { - mcdu.setScratchpadMessage(err); - } - }; - } else if (/^[A-Z0-9]{2,7}/.test(value)) { - // place format - mcdu.dataManager.GetWaypointsByIdent.bind(mcdu.dataManager)(value).then((waypoints) => { - if (waypoints.length === 0) { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } else { - data.directTo = value; - } - - CDUAtcLatRequestFansB.ShowPage(mcdu, data); - }); - } - } - - CDUAtcLatRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.heading = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadDegree(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.heading = parseInt(value) % 360; - } - } - - CDUAtcLatRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.track = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadDegree(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.track = parseInt(value) % 360; - } - } - - CDUAtcLatRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcLatRequestFansB.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.offset = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.offset = AtsuCommon.InputValidation.formatScratchpadOffset(value); - data.offsetStart = null; - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcLatRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = async (value) => { - if (value === FMCMainDisplay.clrValue) { - data.weatherDeviation = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.weatherDeviation = AtsuCommon.InputValidation.formatScratchpadOffset(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcLatRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.backOnTrack = false; - } else { - data.backOnTrack = true; - } - CDUAtcLatRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcLatRequestFansB.CanSendData(data)) { - const messages = CDUAtcLatRequestFansB.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansA.ShowPage1(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcLatRequestFansB.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcLatRequestFansB.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcLatRequestFansB.ShowPage(mcdu); - } - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_Text.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_Text.js deleted file mode 100644 index 24a5f9f56c6..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_Text.js +++ /dev/null @@ -1,129 +0,0 @@ -class CDUAtcTextFansB { - static CreateDataBlock() { - return { - performance: false, - weather: false - }; - } - - static CanSendData(data) { - return data.performance || data.weather; - } - - static CreateMessages(messages, data) { - let extension = null; - if (data.performance) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM66"][1].deepCopy(); - } else if (data.weather) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM65"][1].deepCopy(); - } - - let updated = false; - for (const message of messages) { - if (message.Content[0].TypeId.includes("UM")) { - message.Response.Content.push(extension); - updated = true; - } else { - message.Content.push(extension); - } - } - - return updated; - } - - static ShowPage(mcdu, messages, data = CDUAtcTextFansB.CreateDataBlock()) { - mcdu.clearDisplay(); - - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcTextFansB.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - erase = "*ERASE"; - } - - const acPerform = ["\xa0DUE TO", "{cyan}{{end}A/C PERFORM"]; - if (data.performance) { - acPerform[0] += "[color]cyan"; - acPerform[1] = "\xa0A/C PERFORM[color]cyan"; - } - const weather = ["\xa0DUE TO", "{cyan}{{end}WEATHER"]; - if (data.weather) { - weather[0] += "[color]cyan"; - weather[1] = "\xa0WEATHER[color]cyan"; - } - - mcdu.setTemplate([ - ["FREE TEXT"], - [acPerform[0], weather[0]], - [acPerform[1], weather[1]], - [""], - [""], - [""], - [""], - [""], - [""], - ["ALL FIELDS"], - [erase], - ["\xa0ATC MENU", `XFR TO\xa0[color]cyan`], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.performance = false; - } else { - data.performance = true; - data.weather = false; - } - CDUAtcTextFansB.ShowPage(mcdu, messages, data); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.performance = false; - } else { - data.performance = true; - data.weather = false; - } - CDUAtcTextFansB.ShowPage(mcdu, messages, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcTextFansB.ShowPage(mcdu, messages); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcTextFansB.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - if (CDUAtcTextFansB.CreateMessages(mcdu, messages, data)) { - mcdu.atsu.updateMessage(messages[0]); - } else { - mcdu.atsu.registerMessages(messages); - } - CDUAtcMenu.ShowPage(mcdu); - } - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_UsualRequest.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_UsualRequest.js deleted file mode 100644 index 6128915a253..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_UsualRequest.js +++ /dev/null @@ -1,280 +0,0 @@ -class CDUAtcUsualRequestFansB { - static CreateDataBlock() { - return { - directTo: null, - speed: null, - weatherDeviation: null, - climbTo: null, - descentTo: null, - dueToWeather: false, - }; - } - - static CanSendData(data) { - return data.directTo || data.speed || data.weatherDeviation || data.climbTo || data.descentTo; - } - - static CanEraseData(data) { - return data.directTo || data.speed || data.weatherDeviation || data.climbTo || data.descentTo || data.dueToWeather; - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - let extension = null; - if (data.dueToWeather) { - extension = AtsuCommon.CpdlcMessagesDownlink["DM65"][1].deepCopy(); - } - - if (data.directTo) { - retval.push(CDUAtcUsualRequestFansB.CreateRequest(mcdu, "DM22", [data.directTo])); - } - if (data.weatherDeviation) { - const elements = AtsuCommon.InputValidation.expandLateralOffset(data.weatherDeviation).split(" "); - retval.push(CDUAtcUsualRequestFansB.CreateRequest(mcdu, "DM27", [elements[0], elements[1]])); - } - if (data.climbTo) { - retval.push(CDUAtcUsualRequestFansB.CreateRequest(mcdu, "DM9", [data.climbTo])); - } - if (data.descentTo) { - retval.push(CDUAtcUsualRequestFansB.CreateRequest(mcdu, "DM10", [data.descentTo])); - } - if (data.speed) { - retval.push(CDUAtcUsualRequestFansB.CreateRequest(mcdu, "DM18", [data.speed])); - } - - if (extension) { - retval.forEach((message) => { - if (message.Content[0].TypeId !== "DM27") { - message.Content.push(extension); - } - }); - } - - return retval; - } - - static ShowPage(mcdu, data = CDUAtcUsualRequestFansB.CreateDataBlock()) { - mcdu.clearDisplay(); - mcdu.page.Current = mcdu.page.ATCUsualRequest; - - let addText = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcUsualRequestFansB.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - addText = "ADD TEXT>"; - } - if (CDUAtcUsualRequestFansB.CanEraseData(data)) { - erase = "*ERASE"; - } - - let directTo = "{cyan}[ ]{end}"; - if (data.directTo) { - directTo = `${data.directTo}[color]cyan`; - } - let weatherDeviation = "{cyan}[ ]{end}"; - if (data.weatherDeviation) { - weatherDeviation = `${data.weatherDeviation}[color]cyan`; - } - let speed = "[ ][color]cyan"; - if (data.speed) { - speed = `${data.speed}[color]cyan`; - } - let climbTo = "[ ][color]cyan"; - let descentTo = "[ ][color]cyan"; - if (data.climbTo) { - climbTo = `${data.climbTo}[color]cyan`; - } - if (data.descentTo) { - descentTo = `${data.descentTo}[color]cyan`; - } - - const dueToWeather = ["\xa0DUE TO", "{cyan}{{end}WEATHER"]; - if (data.dueToWeather) { - dueToWeather[0] = "{cyan}\xa0DUE TO{end}"; - dueToWeather[1] = "{cyan}\xa0WEATHER{end}"; - } - - mcdu.setTemplate([ - ["USUAL REQ"], - ["\xa0DIR TO", "SPEED\xa0"], - [directTo, speed], - ["", "WX DEV\xa0"], - ["", weatherDeviation], - ["\xa0CLB TO", "DES TO\xa0"], - [climbTo, descentTo], - [dueToWeather[0]], - [dueToWeather[1]], - ["\xa0ALL FIELDS"], - [erase, addText], - ["\xa0ATC MENU", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.directTo = null; - } else if (value) { - if (Fmgc.WaypointEntryUtils.isLatLonFormat(value)) { - // format: DDMM.MB/EEEMM.MC - try { - Fmgc.WaypointEntryUtils.parseLatLon(value); - data.directTo = value; - } catch (err) { - if (err === NXSystemMessages.formatError) { - mcdu.setScratchpadMessage(err); - } - }; - } else if (/^[A-Z0-9]{2,7}/.test(value)) { - // place format - mcdu.dataManager.GetWaypointsByIdent.bind(mcdu.dataManager)(value).then((waypoints) => { - if (waypoints.length === 0) { - mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); - } else { - data.directTo = value; - } - - CDUAtcUsualRequestFansB.ShowPage(mcdu, data); - }); - } - } - - CDUAtcUsualRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.climbTo = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.climbTo = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcUsualRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.dueToWeather = false; - } else { - data.dueToWeather = true; - } - CDUAtcUsualRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcUsualRequestFansB.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.speed = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadSpeed(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.speed = AtsuCommon.InputValidation.formatScratchpadSpeed(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcUsualRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = async (value) => { - if (value === FMCMainDisplay.clrValue) { - data.weatherDeviation = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadOffset(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.weatherDeviation = AtsuCommon.InputValidation.formatScratchpadOffset(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcUsualRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.descentTo = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.descentTo = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcUsualRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcUsualRequestFansB.CanSendData(data)) { - const requests = CDUAtcUsualRequestFansB.CreateRequests(mcdu, data); - if (requests.length !== 0) { - CDUAtcTextFansB.ShowPage1(mcdu, requests); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcUsualRequestFansB.CDUAtcUsualRequestFansB(data)) { - const requests = CDUAtcUsualRequestFansB.CreateRequests(mcdu, data); - if (requests.length !== 0) { - mcdu.atsu.registerMessages(requests); - CDUAtcUsualRequestFansB.ShowPage(mcdu); - } - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_VertRequest.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_VertRequest.js deleted file mode 100644 index d2ade951514..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/FansB/A320_Neo_CDU_ATC_VertRequest.js +++ /dev/null @@ -1,177 +0,0 @@ -class CDUAtcVertRequestFansB { - static CreateDataBlock() { - return { - climb: null, - descend: null, - altitude: null, - }; - } - - static CanSendData(data) { - return data.climb || data.descend || data.altitude; - } - - static CreateRequest(mcdu, type, values = []) { - const retval = new AtsuCommon.CpdlcMessage(); - retval.Station = mcdu.atsu.currentStation(); - retval.Content.push(AtsuCommon.CpdlcMessagesDownlink[type][1].deepCopy()); - - for (let i = 0; i < values.length; ++i) { - retval.Content[0].Content[i].Value = values[i]; - } - - return retval; - } - - static CreateRequests(mcdu, data) { - const retval = []; - - if (data.climb) { - retval.push(CDUAtcVertRequestFansB.CreateRequest(mcdu, "DM9", [data.climb])); - } - if (data.descend) { - retval.push(CDUAtcVertRequestFansB.CreateRequest(mcdu, "DM10", [data.descend])); - } - if (data.altitude) { - retval.push(CDUAtcVertRequestFansB.CreateRequest(mcdu, "DM6", [data.altitude])); - } - - return retval; - } - - static ShowPage(mcdu, data = CDUAtcVertRequestFansB.CreateDataBlock()) { - mcdu.clearDisplay(); - - let climbTo = "[ ][color]cyan"; - let descentTo = "[ ][color]cyan"; - if (data.climb) { - climbTo = `${data.climb}[color]cyan`; - } - if (data.descend) { - descentTo = `${data.descend}[color]cyan`; - } - let altitude = "[ ][color]cyan"; - if (data.altitude) { - altitude = `${data.altitude}[color]cyan`; - } - - let text = "ADD TEXT\xa0"; - let erase = "\xa0ERASE"; - let reqDisplay = "DCDU\xa0[color]cyan"; - if (CDUAtcVertRequestFansB.CanSendData(data)) { - reqDisplay = "DCDU*[color]cyan"; - text = "ADD TEXT>"; - erase = "*ERASE"; - } - - mcdu.setTemplate([ - ["ATC VERT REQ"], - ["\xa0CLB TO", "DES TO\xa0"], - [climbTo, descentTo], - ["\xa0ALT"], - [altitude], - [""], - [""], - [""], - [""], - ["\xa0ALL FIELDS"], - [erase, text], - ["\xa0FLIGHT REQ", "XFR TO\xa0[color]cyan"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.climb = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.climb = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcVertRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.altitude = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.altitude = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcVertRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUAtcVertRequestFansB.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcFlightReq.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.descend = null; - } else if (value) { - const error = AtsuCommon.InputValidation.validateScratchpadAltitude(value); - if (error !== AtsuCommon.AtsuStatusCodes.Ok) { - mcdu.addNewAtsuMessage(error); - } else { - data.descend = AtsuCommon.InputValidation.formatScratchpadAltitude(value); - } - } - CDUAtcVertRequestFansB.ShowPage(mcdu, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcVertRequestFansB.CanSendData(data)) { - const messages = CDUAtcVertRequestFansB.CreateRequests(mcdu, data); - if (messages.length !== 0) { - CDUAtcTextFansB.ShowPage1(mcdu, messages); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcVertRequestFansB.CanSendData(data)) { - if (mcdu.atsu.currentStation() === "") { - mcdu.setScratchpadMessage(NXSystemMessages.noAtc); - } else { - const messages = CDUAtcVertRequestFansB.CreateRequests(mcdu, data); - if (messages.length !== 0) { - mcdu.atsu.registerMessages(messages); - } - CDUAtcVertRequestFansB.ShowPage(mcdu); - } - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/MessageModify/A320_Neo_CDU_ATC_MessageModifyUM131.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/MessageModify/A320_Neo_CDU_ATC_MessageModifyUM131.js deleted file mode 100644 index 5d0fb171605..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/ATSU/MessageModify/A320_Neo_CDU_ATC_MessageModifyUM131.js +++ /dev/null @@ -1,129 +0,0 @@ -class CDUAtcMessageModifyUM131 { - static CreateDataBlock(message) { - return { - personsOnBoard: message.Response.Content[0].Content[1].Value !== "" ? message.Response.Content[0].Content[1].Value : null, - endurance: message.Response.Content[0].Content[0].Value !== "" ? message.Response.Content[0].Content[0].Value : null - }; - } - - static CanUpdateMessage(data) { - return data.personsOnBoard && data.endurance; - } - - static UpdateResponseMessage(message, data) { - message.Response.Content[0].Content[0].Value = data.endurance; - message.Response.Content[0].Content[1].Value = data.personsOnBoard; - } - - static ShowPage(mcdu, message, data = CDUAtcMessageModifyUM131.CreateDataBlock(message)) { - let cancel = "\xa0CANCEL"; - let addText = "ADD TEXT\xa0"; - let transfer = "DCDU\xa0"; - if (CDUAtcMessageModifyUM131.CanUpdateMessage(data)) { - cancel = "*CANCEL"; - addText = "ADD TEXT>"; - transfer = "DCDU*"; - } - - let personsOnBoard = "{cyan}[ ]{end}"; - let endurance = "{cyan}[ ]{end}"; - if (data.personsOnBoard) { - personsOnBoard = `{cyan}${data.personsOnBoard}{end}`; - } - if (data.endurance) { - endurance = `{cyan}${data.endurance}{end}`; - } - - mcdu.setTemplate([ - ["MODIFY"], - [""], - [""], - ["\xa0POB", "ENDURANCE\xa0"], - [personsOnBoard, endurance], - [""], - [""], - [""], - [""], - ["{cyan}\xa0PAGE{end}"], - [`{cyan}${cancel}{end}`, `{white}${addText}{end}`], - ["\xa0ATC MENU", "{cyan}XFR TO\xa0{end}"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.personsOnBoard = null; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadPersonsOnBoard(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.personsOnBoard = parseInt(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcMessageModifyUM131.ShowPage(mcdu, message, data); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - if (CDUAtcMessageModifyUM131.CanUpdateMessage(data)) { - mcdu.atsu.updateMessage(message); - CDUAtcMenu.ShowPage(mcdu); - } - }; - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUAtcMenu.ShowPage(mcdu); - }; - - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = (value) => { - if (value === FMCMainDisplay.clrValue) { - data.endurance = null; - } else { - const error = AtsuCommon.InputValidation.validateScratchpadEndurance(value); - if (error === AtsuCommon.AtsuStatusCodes.Ok) { - data.endurance = AtsuCommon.InputValidation.formatScratchpadEndurance(value); - } else { - mcdu.addNewAtsuMessage(error); - } - } - CDUAtcMessageModifyUM131.ShowPage(mcdu, message, data); - }; - - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - if (CDUAtcMessageModifyUM131.CanUpdateMessage(data)) { - CDUAtcMessageModifyUM131.UpdateResponseMessage(message, data); - if (mcdu.atsu.fansMode() === AtsuCommon.FansMode.FansA) { - CDUAtcTextFansA.ShowPage1(mcdu, [message]); - } else { - CDUAtcTextFansB.ShowPage(mcdu, [message]); - } - } - }; - - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - if (CDUAtcMessageModifyUM131.CanUpdateMessage(data)) { - CDUAtcMessageModifyUM131.UpdateResponseMessage(message, data); - mcdu.atsu.updateMessage(message); - CDUAtcMenu.ShowPage(mcdu, message); - } - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/BacklightTemp/MCDUGradient.png b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/BacklightTemp/MCDUGradient.png deleted file mode 100644 index ee23b6b67a1..00000000000 Binary files a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/BacklightTemp/MCDUGradient.png and /dev/null differ diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/A320_Neo_CDU_CFDS_Avionics_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/A320_Neo_CDU_CFDS_Avionics_Menu.js deleted file mode 100644 index 40e742c4b33..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/A320_Neo_CDU_CFDS_Avionics_Menu.js +++ /dev/null @@ -1,71 +0,0 @@ -class CDUCfdsAvionicsMenu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["AVIONICS STATUS", "1", "2"], - [""], - ["NO GPCU DATA"], - [""], - ["ADF 1 (CLASS 3)"], - [""], - ["FMGC"], - [""], - ["VHF"], - [""], - ["AIDS"], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[5] = () => { - CDUCfdsMainMenu.ShowPage(mcdu); - }; - - // PAGE SWITCHING - mcdu.onPrevPage = () => { - CDUCfdsAvionicsMenu.ShowPage2(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsAvionicsMenu.ShowPage2(mcdu); - }; - } - - static ShowPage2(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["AVIONICS STATUS", "2", "2"], - [""], - ["NO ILS DATA"], - [""], - ["DMC (CLASS 3)"], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - - mcdu.onLeftInput[5] = () => { - CDUCfdsMainMenu.ShowPage(mcdu); - }; - - // PAGE SWITCHING - mcdu.onPrevPage = () => { - CDUCfdsAvionicsMenu.ShowPage(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsAvionicsMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/A320_Neo_CDU_CFDS_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/A320_Neo_CDU_CFDS_Menu.js deleted file mode 100644 index 9f7d2d05ef1..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/A320_Neo_CDU_CFDS_Menu.js +++ /dev/null @@ -1,71 +0,0 @@ -class CDUCfdsMainMenu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.activeSystem = 'CFDS'; - mcdu.setTemplate([ - ["CFDS", "1", "2"], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = () => { - CDUCfdsAvionicsMenu.ShowPage(mcdu); - }; - - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - - // PAGE SWITCHING - mcdu.onPrevPage = () => { - CDUCfdsMainMenu.ShowPage2(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsMainMenu.ShowPage2(mcdu); - }; - } - - static ShowPage2(mcdu) { - mcdu.clearDisplay(); - - mcdu.setTemplate([ - ["CFDS", "2", "2"], - [""], - [" { - CDUCfdsMainMenu.ShowPage(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsMainMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/A320_Neo_CDU_CFDS_Test_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/A320_Neo_CDU_CFDS_Test_Menu.js deleted file mode 100644 index 98b4524991a..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/A320_Neo_CDU_CFDS_Test_Menu.js +++ /dev/null @@ -1,135 +0,0 @@ -class CDUCfdsTestMenu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST }"], - [""], - [""], - [""], - ["[color]inop"], - [""], - [""], - [""], - [""], - [""], - [""], - [""], - [""] - ]); - - mcdu.leftInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - CDUCfdsTestAircond.ShowPage(mcdu); - }; - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - CDUCfdsTestCom.ShowPage(mcdu); - }; - mcdu.leftInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[3] = () => { - CDUCfdsTestElec.ShowPage(mcdu); - }; - mcdu.leftInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[4] = () => { - CDUCfdsTestFire.ShowPage(mcdu); - }; - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - CDUCfdsTestFctl.ShowPage(mcdu); - }; - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = () => { - CDUCfdsTestIce.ShowPage(mcdu); - }; - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = () => { - CDUCfdsTestInst.ShowPage(mcdu); - }; - mcdu.rightInputDelay[4] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[4] = () => { - CDUCfdsTestLG.ShowPage(mcdu); - }; - mcdu.rightInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[5] = () => { - CDUCfdsTestNav.ShowPage(mcdu); - }; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsMainMenu.ShowPage(mcdu); - }; - - // PAGE SWITCHING - mcdu.onPrevPage = () => { - CDUCfdsTestMenu.ShowPage2(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsTestMenu.ShowPage2(mcdu); - }; - } - - static ShowPage2(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST }"], - [""], - [""], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - [""], - [""], - [""], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - CDUCfdsTestPneu.ShowPage(mcdu); - }; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsMainMenu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - CDUCfdsTestEng.ShowPage(mcdu); - }; - - // PAGE SWITCHING - mcdu.onPrevPage = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/COMMON/A320_Neo_CDU_CFDS_Common_GroundScanning.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/COMMON/A320_Neo_CDU_CFDS_Common_GroundScanning.js deleted file mode 100644 index 38fd8f29023..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/COMMON/A320_Neo_CDU_CFDS_Common_GroundScanning.js +++ /dev/null @@ -1,29 +0,0 @@ -class CDU_CFDS_Test_Common_GroundScanning { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["ECAM-1"], - ["", "", "FWC1/2-SDAC1/2-ECP"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestInst.ShowPage(mcdu); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/COMMON/A320_Neo_CDU_CFDS_Common_PowerUpTestRes.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/COMMON/A320_Neo_CDU_CFDS_Common_PowerUpTestRes.js deleted file mode 100644 index 81b8cab9cb2..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/COMMON/A320_Neo_CDU_CFDS_Common_PowerUpTestRes.js +++ /dev/null @@ -1,29 +0,0 @@ -class CDU_CFDS_Test_Common_PowerUp { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["ECAM-1"], - ["", "", "FWC1/2-SDAC1/2-ECP"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestInst.ShowPage(mcdu); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_AIRCOND/A320_Neo_CDU_CFDS_Test_Aircond.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_AIRCOND/A320_Neo_CDU_CFDS_Test_Aircond.js deleted file mode 100644 index fef4214dedd..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_AIRCOND/A320_Neo_CDU_CFDS_Test_Aircond.js +++ /dev/null @@ -1,27 +0,0 @@ -class CDUCfdsTestAircond { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "AIR COND"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_COM/A320_Neo_CDU_CFDS_Test_Com.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_COM/A320_Neo_CDU_CFDS_Test_Com.js deleted file mode 100644 index b6dd6733422..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_COM/A320_Neo_CDU_CFDS_Test_Com.js +++ /dev/null @@ -1,69 +0,0 @@ -class CDUCfdsTestCom { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST }"], - ["", "", "COM"], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"] - ]); - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - - // PAGE SWITCHING - mcdu.onPrevPage = () => { - CDUCfdsTestCom.ShowPage2(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsTestCom.ShowPage2(mcdu); - }; - } - - static ShowPage2(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST }"], - ["", "", "COM"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - - // PAGE SWITCHING - mcdu.onPrevPage = () => { - CDUCfdsTestCom.ShowPage(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsTestCom.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_ELEC/A320_Neo_CDU_CFDS_Test_Elec.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_ELEC/A320_Neo_CDU_CFDS_Test_Elec.js deleted file mode 100644 index 124537f9020..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_ELEC/A320_Neo_CDU_CFDS_Test_Elec.js +++ /dev/null @@ -1,27 +0,0 @@ -class CDUCfdsTestElec { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "ELEC"], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_ENG/A320_Neo_CDU_CFDS_Test_Eng.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_ENG/A320_Neo_CDU_CFDS_Test_Eng.js deleted file mode 100644 index 71ad00598b5..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_ENG/A320_Neo_CDU_CFDS_Test_Eng.js +++ /dev/null @@ -1,27 +0,0 @@ -class CDUCfdsTestEng { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "ENG"], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage2(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_FCTL/A320_Neo_CDU_CFDS_Test_Fctl.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_FCTL/A320_Neo_CDU_CFDS_Test_Fctl.js deleted file mode 100644 index e060484a5b2..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_FCTL/A320_Neo_CDU_CFDS_Test_Fctl.js +++ /dev/null @@ -1,27 +0,0 @@ -class CDUCfdsTestFctl { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "F/CTL"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_FIRE/A320_Neo_CDU_CFDS_Test_Fire.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_FIRE/A320_Neo_CDU_CFDS_Test_Fire.js deleted file mode 100644 index f6e69275649..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_FIRE/A320_Neo_CDU_CFDS_Test_Fire.js +++ /dev/null @@ -1,27 +0,0 @@ -class CDUCfdsTestFire { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "FIRE PROT"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_FUEL/A320_Neo_CDU_CFDS_Test_Fuel.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_FUEL/A320_Neo_CDU_CFDS_Test_Fuel.js deleted file mode 100644 index 6863d33478e..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_FUEL/A320_Neo_CDU_CFDS_Test_Fuel.js +++ /dev/null @@ -1,71 +0,0 @@ -class CDUCfdsTestFuel { - // NOT USED ATM - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "FUEL"], - [""], - [""], - [""], - [""], - [""], - [""], - ["", "EIS 3>"], - [""], - [""], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - CDU_CFDS_Test_Inst_ECAM1_Menu.ShowPage(mcdu); - }; - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = () => { - CDU_CFDS_Test_Inst_ECAM2_Menu.ShowPage(mcdu); - }; - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - CDU_CFDS_Test_Inst_DFDRS_Menu.ShowPage(mcdu); - }; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - CDU_CFDS_Test_Inst_CFDIU_Menu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = () => { - CDU_CFDS_Test_Inst_EIS1_Menu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = () => { - CDU_CFDS_Test_Inst_EIS2_Menu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = () => { - CDU_CFDS_Test_Inst_EIS3_Menu.ShowPage(mcdu); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_ICE/A320_Neo_CDU_CFDS_Test_Ice.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_ICE/A320_Neo_CDU_CFDS_Test_Ice.js deleted file mode 100644 index 85e96b2d553..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_ICE/A320_Neo_CDU_CFDS_Test_Ice.js +++ /dev/null @@ -1,27 +0,0 @@ -class CDUCfdsTestIce { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "ICE + RAIN"], - ["[color]inop"], - ["", "(THRU ECS)[color]inop"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INFO/A320_Neo_CDU_CFDS_Test_Info.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INFO/A320_Neo_CDU_CFDS_Test_Info.js deleted file mode 100644 index b557dfb9f5e..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INFO/A320_Neo_CDU_CFDS_Test_Info.js +++ /dev/null @@ -1,71 +0,0 @@ -class CDUCfdsTestInfo { - // not used atm - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "INFO"], - [""], - [""], - [""], - [""], - [""], - [""], - ["", "EIS 3>"], - [""], - [""], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - CDU_CFDS_Test_Inst_ECAM1_Menu.ShowPage(mcdu); - }; - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = () => { - CDU_CFDS_Test_Inst_ECAM2_Menu.ShowPage(mcdu); - }; - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - CDU_CFDS_Test_Inst_DFDRS_Menu.ShowPage(mcdu); - }; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - CDU_CFDS_Test_Inst_CFDIU_Menu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = () => { - CDU_CFDS_Test_Inst_EIS1_Menu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = () => { - CDU_CFDS_Test_Inst_EIS2_Menu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = () => { - CDU_CFDS_Test_Inst_EIS3_Menu.ShowPage(mcdu); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/A320_Neo_CDU_CFDS_Test_Inst.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/A320_Neo_CDU_CFDS_Test_Inst.js deleted file mode 100644 index cd228592672..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/A320_Neo_CDU_CFDS_Test_Inst.js +++ /dev/null @@ -1,70 +0,0 @@ -class CDUCfdsTestInst { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "INST"], - [""], - [""], - [""], - [""], - [""], - [""], - ["", "EIS 3>"], - [""], - [""], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[0] = () => { - CDU_CFDS_Test_Inst_ECAM_Menu.ShowPage(mcdu, 1); - }; - mcdu.leftInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[1] = () => { - CDU_CFDS_Test_Inst_ECAM_Menu.ShowPage(mcdu, 2); - }; - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - CDU_CFDS_Test_Inst_DFDRS_Menu.ShowPage(mcdu); - }; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[0] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[0] = () => { - CDU_CFDS_Test_Inst_CFDIU_Menu.ShowPage(mcdu); - }; - mcdu.rightInputDelay[1] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[1] = () => { - CDU_CFDS_Test_Inst_EIS_Menu.ShowPage(mcdu, 1); - }; - mcdu.rightInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[2] = () => { - CDU_CFDS_Test_Inst_EIS_Menu.ShowPage(mcdu, 2); - }; - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = () => { - CDU_CFDS_Test_Inst_EIS_Menu.ShowPage(mcdu, 3); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/CFDIU/A320_Neo_CDU_CFDS_Test_Inst_CFDIU_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/CFDIU/A320_Neo_CDU_CFDS_Test_Inst_CFDIU_Menu.js deleted file mode 100644 index 44342ca7f3a..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/CFDIU/A320_Neo_CDU_CFDS_Test_Inst_CFDIU_Menu.js +++ /dev/null @@ -1,28 +0,0 @@ -class CDU_CFDS_Test_Inst_CFDIU_Menu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["CFDIU"], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestInst.ShowPage(mcdu); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/DFDRS/A320_Neo_CDU_CFDS_Test_Inst_DFDRS_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/DFDRS/A320_Neo_CDU_CFDS_Test_Inst_DFDRS_Menu.js deleted file mode 100644 index d97e766938a..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/DFDRS/A320_Neo_CDU_CFDS_Test_Inst_DFDRS_Menu.js +++ /dev/null @@ -1,28 +0,0 @@ -class CDU_CFDS_Test_Inst_DFDRS_Menu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["DFDRS"], - ["LAST LEG[color]inop", "CLASS 3[color]inop"], - ["[color]inop"], - ["PREVIOUS LEGS[color]inop"], - ["[color]inop"], - [""], - ["[color]inop"], - ["TROUBLE SHOOT[color]inop", "GROUND[color]inop"], - ["[color]inop"], - ["", "SPECIFIC[color]inop"], - ["[color]inop"] - ]); - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestInst.ShowPage(mcdu); - }; - - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/ECAM/A320_Neo_CDU_CFDS_Test_Inst_ECAM_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/ECAM/A320_Neo_CDU_CFDS_Test_Inst_ECAM_Menu.js deleted file mode 100644 index 9ca2907862c..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/ECAM/A320_Neo_CDU_CFDS_Test_Inst_ECAM_Menu.js +++ /dev/null @@ -1,28 +0,0 @@ -class CDU_CFDS_Test_Inst_ECAM_Menu { - static ShowPage(mcdu, ecamIndex) { - mcdu.clearDisplay(); - const title = "ECAM-" + ecamIndex; - mcdu.setTemplate([ - [title], - ["", "", "FWC1/2-SDAC1/2-ECP"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestInst.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/EIS/A320_Neo_CDU_CFDS_Test_Inst_EIS_Menu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/EIS/A320_Neo_CDU_CFDS_Test_Inst_EIS_Menu.js deleted file mode 100644 index 607005f6745..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/EIS/A320_Neo_CDU_CFDS_Test_Inst_EIS_Menu.js +++ /dev/null @@ -1,37 +0,0 @@ -class CDU_CFDS_Test_Inst_EIS_Menu { - static ShowPage(mcdu, eisIndex) { - mcdu.clearDisplay(); - SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, "Enum", 1); - const title = "EIS ( DMC " + eisIndex + " )"; - mcdu.setTemplate([ - [title], - [""], - [""], - [""], - [" SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, "Enum", 0); - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestInst.ShowPage(mcdu); - }; - mcdu.rightInputDelay[3] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onRightInput[3] = () => { - CDU_CFDS_Test_Inst_EIS_Tests.ShowPage(mcdu, eisIndex); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/EIS/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/EIS/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests.js deleted file mode 100644 index 244077107a5..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/EIS/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests.js +++ /dev/null @@ -1,37 +0,0 @@ -class CDU_CFDS_Test_Inst_EIS_Tests { - static ShowPage(mcdu, eisIndex) { - mcdu.clearDisplay(); - SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, "Enum", 1); - const title = "EIS ( DMC " + eisIndex + " )"; - mcdu.setTemplate([ - [title], - ["", "", "TEST"], - [""], - [""], - [" SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, "Enum", 0); - - mcdu.leftInputDelay[2] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[2] = () => { - CDU_CFDS_Test_Inst_EIS_Tests_Display.ShowPage(mcdu, eisIndex); - }; - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDU_CFDS_Test_Inst_EIS_Menu.ShowPage(mcdu, eisIndex); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/EIS/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests_Display.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/EIS/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests_Display.js deleted file mode 100644 index 1ba2f75cba5..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_INST/EIS/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests_Display.js +++ /dev/null @@ -1,31 +0,0 @@ -class CDU_CFDS_Test_Inst_EIS_Tests_Display { - static ShowPage(mcdu, eisIndex) { - mcdu.clearDisplay(); - SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, "Enum", 2); - const title = "EIS ( DMC " + eisIndex + " )"; - mcdu.setTemplate([ - [title], - [""], - [""], - [""], - [""], - [""], - ["","","DISPLAY TEST"], - [""], - ["","","IN"], - [""], - ["","","PROGRESS "], - [""], - [" SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, "Enum", 0); - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDU_CFDS_Test_Inst_EIS_Tests.ShowPage(mcdu, eisIndex); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_LG/A320_Neo_CDU_CFDS_Test_LG.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_LG/A320_Neo_CDU_CFDS_Test_LG.js deleted file mode 100644 index ca3427e76f8..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_LG/A320_Neo_CDU_CFDS_Test_LG.js +++ /dev/null @@ -1,27 +0,0 @@ -class CDUCfdsTestLG { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "L/G"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_NAV/A320_Neo_CDU_CFDS_Test_Nav.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_NAV/A320_Neo_CDU_CFDS_Test_Nav.js deleted file mode 100644 index 0899b4f1e8f..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_NAV/A320_Neo_CDU_CFDS_Test_Nav.js +++ /dev/null @@ -1,103 +0,0 @@ -class CDUCfdsTestNav { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST }"], - ["", "", "NAV"], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"] - ]); - - mcdu.leftInputDelay[5] = () => { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - - // PAGE SWITCHING - mcdu.onPrevPage = () => { - CDUCfdsTestNav.ShowPage3(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsTestNav.ShowPage2(mcdu); - }; - } - - static ShowPage2(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST }"], - ["", "", "NAV"], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - [""], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - - // PAGE SWITCHING - mcdu.onPrevPage = () => { - CDUCfdsTestNav.ShowPage(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsTestNav.ShowPage3(mcdu); - }; - } - - static ShowPage3(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST }"], - ["", "", "NAV"], - ["[color]inop"], - [""], - ["[color]inop"], - [""], - [""], - [""], - [""], - [""], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage(mcdu); - }; - - // PAGE SWITCHING - mcdu.onPrevPage = () => { - CDUCfdsTestNav.ShowPage2(mcdu); - }; - mcdu.onNextPage = () => { - CDUCfdsTestNav.ShowPage(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_PNEU/A320_Neo_CDU_CFDS_Test_Pneu.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_PNEU/A320_Neo_CDU_CFDS_Test_Pneu.js deleted file mode 100644 index f84c9359354..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/CFDS/TEST_PNEU/A320_Neo_CDU_CFDS_Test_Pneu.js +++ /dev/null @@ -1,27 +0,0 @@ -class CDUCfdsTestPneu { - static ShowPage(mcdu) { - mcdu.clearDisplay(); - mcdu.setTemplate([ - ["SYSTEM REPORT / TEST"], - ["", "", "PNEU"], - [" { - return mcdu.getDelaySwitchPage(); - }; - mcdu.onLeftInput[5] = () => { - CDUCfdsTestMenu.ShowPage2(mcdu); - }; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/FMC/A32NX_FMCDataManager.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/FMC/A32NX_FMCDataManager.js deleted file mode 100644 index dc4e6f983cb..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/FMC/A32NX_FMCDataManager.js +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -const StoredWaypointType = Object.freeze({ - Pbd: 1, - Pbx: 2, - LatLon: 3, -}); - -const IcaoSearchFilter = Object.freeze({ - None: 0, - Airports: 1, - Intersections: 2, - Vors: 3, - Ndbs: 4, -}); diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/FMC/A32NX_FMCMainDisplay.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/FMC/A32NX_FMCMainDisplay.js deleted file mode 100644 index 53476dddb0f..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/FMC/A32NX_FMCMainDisplay.js +++ /dev/null @@ -1,5186 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class FMCMainDisplay extends BaseAirliners { - constructor() { - super(...arguments); - FMCMainDisplay.DEBUG_INSTANCE = this; - this.flightPhaseUpdateThrottler = new UpdateThrottler(800); - this.fmsUpdateThrottler = new UpdateThrottler(250); - this._progBrgDistUpdateThrottler = new UpdateThrottler(2000); - this._apCooldown = 500; - this.lastFlightPlanVersion = 0; - this._messageQueue = new A32NX_MessageQueue(this); - - /** Declaration of every variable used (NOT initialization) */ - this.maxCruiseFL = undefined; - this.recMaxCruiseFL = undefined; - this.routeIndex = undefined; - this.coRoute = { routeNumber: undefined, routes: undefined }; - this.perfTOTemp = undefined; - this._overridenFlapApproachSpeed = undefined; - this._overridenSlatApproachSpeed = undefined; - this._routeFinalFuelWeight = undefined; - this._routeFinalFuelTime = undefined; - this._routeFinalFuelTimeDefault = undefined; - this._routeReservedWeight = undefined; - this._routeReservedPercent = undefined; - this.takeOffWeight = undefined; - this.landingWeight = undefined; - // +ve for tailwind, -ve for headwind - this.averageWind = undefined; - this.perfApprQNH = undefined; - this.perfApprTemp = undefined; - this.perfApprWindHeading = undefined; - this.perfApprWindSpeed = undefined; - this.unconfirmedV1Speed = undefined; - this.unconfirmedVRSpeed = undefined; - this.unconfirmedV2Speed = undefined; - this._toFlexChecked = undefined; - this.toRunway = undefined; - this.vApp = undefined; - this.perfApprMDA = null; - this.perfApprDH = null; - this.perfApprFlaps3 = undefined; - this._debug = undefined; - this._checkFlightPlan = undefined; - this._fuelPlanningPhases = undefined; - this._zeroFuelWeightZFWCGEntered = undefined; - this._taxiEntered = undefined; - this._DistanceToAlt = undefined; - this._routeAltFuelWeight = undefined; - this._routeAltFuelTime = undefined; - this._routeTripFuelWeight = undefined; - this._routeTripTime = undefined; - this._defaultTaxiFuelWeight = undefined; - this._rteRsvPercentOOR = undefined; - this._rteReservedWeightEntered = undefined; - this._rteReservedPctEntered = undefined; - this._rteFinalCoeffecient = undefined; - this._rteFinalWeightEntered = undefined; - this._rteFinalTimeEntered = undefined; - this._routeAltFuelEntered = undefined; - this._minDestFob = undefined; - this._minDestFobEntered = undefined; - this._isBelowMinDestFob = undefined; - this._defaultRouteFinalTime = undefined; - this._fuelPredDone = undefined; - this._fuelPlanningPhase = undefined; - this._blockFuelEntered = undefined; - this._initMessageSettable = undefined; - this._checkWeightSettable = undefined; - this._gwInitDisplayed = undefined; - /* CPDLC Fields */ - this._destDataChecked = undefined; - this._towerHeadwind = undefined; - this._EfobBelowMinClr = undefined; - this.simbrief = undefined; - this.aocWeight = undefined; - this.aocTimes = undefined; - this.winds = undefined; - this.computedVgd = undefined; - this.computedVfs = undefined; - this.computedVss = undefined; - this.computedVls = undefined; - this.approachSpeeds = undefined; // based on selected config, not current config - this._cruiseEntered = undefined; - this._blockFuelEntered = undefined; - this.constraintAlt = undefined; - this.fcuSelAlt = undefined; - this._forceNextAltitudeUpdate = undefined; - this._lastUpdateAPTime = undefined; - this.updateAutopilotCooldown = undefined; - this._lastHasReachFlex = undefined; - this._apMasterStatus = undefined; - this._lastRequestedFLCModeWaypointIndex = undefined; - - this._progBrgDist = undefined; - this.preSelectedClbSpeed = undefined; - this.preSelectedCrzSpeed = undefined; - this.managedSpeedTarget = undefined; - this.managedSpeedTargetIsMach = undefined; - this.managedSpeedClimb = undefined; - this.managedSpeedClimbIsPilotEntered = undefined; - this.managedSpeedClimbMach = undefined; - // this.managedSpeedClimbMachIsPilotEntered = undefined; - this.managedSpeedCruise = undefined; - this.managedSpeedCruiseIsPilotEntered = undefined; - this.managedSpeedCruiseMach = undefined; - // this.managedSpeedCruiseMachIsPilotEntered = undefined; - this.managedSpeedDescend = undefined; - this.managedSpeedDescendPilot = undefined; - this.managedSpeedDescendMach = undefined; - this.managedSpeedDescendMachPilot = undefined; - // this.managedSpeedDescendMachIsPilotEntered = undefined; - this.cruiseFlightLevelTimeOut = undefined; - /** @type {0 | 1 | 2 | 3 | null} Takeoff config entered on PERF TO */ - this.flaps = undefined; - this.ths = undefined; - this.cruiseTemperature = undefined; - this.taxiFuelWeight = undefined; - this.blockFuel = undefined; - this.zeroFuelWeight = undefined; - this.zeroFuelWeightMassCenter = undefined; - this.activeWpIdx = undefined; - this.efisSymbols = undefined; - this.groundTempAuto = undefined; - this.groundTempPilot = undefined; - /** - * Landing elevation in feet MSL. - * This is the destination runway threshold elevation, or airport elevation if runway is not selected. - */ - this.landingElevation = undefined; - /* - * Latitude part of the touch down coordinate. - * This is the destination runway coordinate, or airport coordinate if runway is not selected - */ - this.destinationLatitude = undefined; - /* - * Latitude part of the touch down coordinate. - * This is the destination runway coordinate, or airport coordinate if runway is not selected - */ - this.destinationLongitude = undefined; - /** Speed in KCAS when the first engine failed during takeoff */ - this.takeoffEngineOutSpeed = undefined; - this.checkSpeedModeMessageActive = undefined; - this.perfClbPredToAltitudePilot = undefined; - this.perfDesPredToAltitudePilot = undefined; - - // ATSU data - this.atsu = undefined; - this.holdSpeedTarget = undefined; - this.holdIndex = undefined; - this.holdDecelReached = undefined; - this.setHoldSpeedMessageActive = undefined; - this.managedProfile = undefined; - this.speedLimitExceeded = undefined; - this.toSpeedsNotInserted = false; - this.toSpeedsTooLow = false; - this.vSpeedDisagree = false; - - this.onAirport = undefined; - - // arinc bus output words - this.arincDiscreteWord2 = FmArinc429OutputWord.empty("DISCRETE_WORD_2"); - this.arincDiscreteWord3 = FmArinc429OutputWord.empty("DISCRETE_WORD_3"); - this.arincTakeoffPitchTrim = FmArinc429OutputWord.empty("TO_PITCH_TRIM"); - this.arincLandingElevation = FmArinc429OutputWord.empty("LANDING_ELEVATION"); - this.arincDestinationLatitude = FmArinc429OutputWord.empty("DEST_LAT"); - this.arincDestinationLongitude = FmArinc429OutputWord.empty("DEST_LONG"); - this.arincMDA = FmArinc429OutputWord.empty("MINIMUM_DESCENT_ALTITUDE"); - this.arincDH = FmArinc429OutputWord.empty("DECISION_HEIGHT"); - this.arincThrustReductionAltitude = FmArinc429OutputWord.empty("THR_RED_ALT"); - this.arincAccelerationAltitude = FmArinc429OutputWord.empty("ACC_ALT"); - this.arincEoAccelerationAltitude = FmArinc429OutputWord.empty("EO_ACC_ALT"); - this.arincMissedThrustReductionAltitude = FmArinc429OutputWord.empty("MISSED_THR_RED_ALT"); - this.arincMissedAccelerationAltitude = FmArinc429OutputWord.empty("MISSED_ACC_ALT"); - this.arincMissedEoAccelerationAltitude = FmArinc429OutputWord.empty("MISSED_EO_ACC_ALT"); - this.arincTransitionAltitude = FmArinc429OutputWord.empty("TRANS_ALT"); - this.arincTransitionLevel = FmArinc429OutputWord.empty("TRANS_LVL"); - /** contains fm messages (not yet implemented) and nodh bit */ - this.arincEisWord2 = FmArinc429OutputWord.empty("EIS_DISCRETE_WORD_2"); - - /** These arinc words will be automatically written to the bus, and automatically set to 0/NCD when the FMS resets */ - this.arincBusOutputs = [ - this.arincDiscreteWord2, - this.arincDiscreteWord3, - this.arincTakeoffPitchTrim, - this.arincLandingElevation, - this.arincDestinationLatitude, - this.arincDestinationLongitude, - this.arincMDA, - this.arincDH, - this.arincThrustReductionAltitude, - this.arincAccelerationAltitude, - this.arincEoAccelerationAltitude, - this.arincMissedThrustReductionAltitude, - this.arincMissedAccelerationAltitude, - this.arincMissedEoAccelerationAltitude, - this.arincTransitionAltitude, - this.arincTransitionLevel, - this.arincEisWord2, - ]; - - this.navDbIdent = null; - } - - Init() { - super.Init(); - this.initVariables(); - - this.A32NXCore = new A32NX_Core(); - this.A32NXCore.init(this._lastTime); - - this.dataManager = new Fmgc.DataManager(this); - - this.efisInterfaces = { L: new Fmgc.EfisInterface('L', this.currFlightPlanService), R: new Fmgc.EfisInterface('R', this.currFlightPlanService) }; - this.guidanceController = new Fmgc.GuidanceController(this.bus, this, this.currFlightPlanService, this.efisInterfaces, Fmgc.a320EfisRangeSettings, Fmgc.A320AircraftConfig); - this.navigation = new Fmgc.Navigation(this.bus, this.currFlightPlanService); - this.efisSymbols = new Fmgc.EfisSymbols( - this.bus, - this.guidanceController, - this.currFlightPlanService, - this.navigation.getNavaidTuner(), - this.efisInterfaces, - Fmgc.a320EfisRangeSettings, - ); - - Fmgc.initFmgcLoop(this, this.currFlightPlanService); - - this.guidanceController.init(); - this.efisSymbols.init(); - this.navigation.init(); - - this.tempCurve = new Avionics.Curve(); - this.tempCurve.interpolationFunction = Avionics.CurveTool.NumberInterpolation; - this.tempCurve.add(-10 * 3.28084, 21.50); - this.tempCurve.add(0, 15.00); - this.tempCurve.add(10 * 3.28084, 8.50); - this.tempCurve.add(20 * 3.28084, 2.00); - this.tempCurve.add(30 * 3.28084, -4.49); - this.tempCurve.add(40 * 3.28084, -10.98); - this.tempCurve.add(50 * 3.28084, -17.47); - this.tempCurve.add(60 * 3.28084, -23.96); - this.tempCurve.add(70 * 3.28084, -30.45); - this.tempCurve.add(80 * 3.28084, -36.94); - this.tempCurve.add(90 * 3.28084, -43.42); - this.tempCurve.add(100 * 3.28084, -49.90); - this.tempCurve.add(150 * 3.28084, -56.50); - this.tempCurve.add(200 * 3.28084, -56.50); - this.tempCurve.add(250 * 3.28084, -51.60); - this.tempCurve.add(300 * 3.28084, -46.64); - this.tempCurve.add(400 * 3.28084, -22.80); - this.tempCurve.add(500 * 3.28084, -2.5); - this.tempCurve.add(600 * 3.28084, -26.13); - this.tempCurve.add(700 * 3.28084, -53.57); - this.tempCurve.add(800 * 3.28084, -74.51); - - // This is used to determine the Mach number corresponding to a CAS at the manual crossover altitude - // The curve was calculated numerically and approximated using a few interpolated values - this.casToMachManualCrossoverCurve = new Avionics.Curve(); - this.casToMachManualCrossoverCurve.interpolationFunction = Avionics.CurveTool.NumberInterpolation; - this.casToMachManualCrossoverCurve.add(0, 0); - this.casToMachManualCrossoverCurve.add(100, 0.27928); - this.casToMachManualCrossoverCurve.add(150, 0.41551); - this.casToMachManualCrossoverCurve.add(200, 0.54806); - this.casToMachManualCrossoverCurve.add(250, 0.67633); - this.casToMachManualCrossoverCurve.add(300, 0.8); - this.casToMachManualCrossoverCurve.add(350, 0.82); - - // This is used to determine the CAS corresponding to a Mach number at the manual crossover altitude - // Effectively, the manual crossover altitude is FL305 up to M.80, then decreases linearly to the crossover altitude of (VMO, MMO) - this.machToCasManualCrossoverCurve = new Avionics.Curve(); - this.machToCasManualCrossoverCurve.interpolationFunction = Avionics.CurveTool.NumberInterpolation; - this.machToCasManualCrossoverCurve.add(0, 0); - this.machToCasManualCrossoverCurve.add(0.27928, 100); - this.machToCasManualCrossoverCurve.add(0.41551, 150); - this.machToCasManualCrossoverCurve.add(0.54806, 200); - this.machToCasManualCrossoverCurve.add(0.67633, 250); - this.machToCasManualCrossoverCurve.add(0.8, 300); - this.machToCasManualCrossoverCurve.add(0.82, 350); - - this.updatePerfSpeeds(); - - this.flightPhaseManager.init(); - this.flightPhaseManager.addOnPhaseChanged(this.onFlightPhaseChanged.bind(this)); - - // Start the check routine for system health and status - setInterval(() => { - if (this.flightPhaseManager.phase === FmgcFlightPhases.CRUISE && !this._destDataChecked) { - const adirLat = ADIRS.getLatitude(); - const adirLong = ADIRS.getLongitude(); - const ppos = (adirLat.isNormalOperation() && adirLong.isNormalOperation()) ? { - lat: ADIRS.getLatitude().value, - long: ADIRS.getLongitude().value, - } : { - lat: NaN, - long: NaN - }; - const distanceToDestination = this.getDistanceToDestination(); - if (Number.isFinite(distanceToDestination) && distanceToDestination !== -1 && distanceToDestination < 180) { - this._destDataChecked = true; - this.checkDestData(); - } - } - }, 15000); - - SimVar.SetSimVarValue('L:A32NX_FM_LS_COURSE', 'number', -1); - - this.navigationDatabaseService.activeDatabase.getDatabaseIdent().then((dbIdent) => this.navDbIdent = dbIdent); - } - - initVariables(resetTakeoffData = true) { - this.costIndex = undefined; - this.maxCruiseFL = 390; - this.recMaxCruiseFL = 398; - this.routeIndex = 0; - this.resetCoroute(); - this._overridenFlapApproachSpeed = NaN; - this._overridenSlatApproachSpeed = NaN; - this._routeFinalFuelWeight = 0; - this._routeFinalFuelTime = 30; - this._routeFinalFuelTimeDefault = 30; - this._routeReservedWeight = 0; - this._routeReservedPercent = 5; - this.takeOffWeight = NaN; - this.landingWeight = NaN; - // +ve for tailwind, -ve for headwind - this.averageWind = 0; - this.perfApprQNH = NaN; - this.perfApprTemp = NaN; - this.perfApprWindHeading = NaN; - this.perfApprWindSpeed = NaN; - this.unconfirmedV1Speed = undefined; - this.unconfirmedVRSpeed = undefined; - this.unconfirmedV2Speed = undefined; - this._toFlexChecked = true; - this.toRunway = ""; - this.vApp = NaN; - this.perfApprMDA = null; - this.perfApprDH = null; - this.perfApprFlaps3 = false; - this._debug = 0; - this._checkFlightPlan = 0; - this._fuelPlanningPhases = { - PLANNING: 1, - IN_PROGRESS: 2, - COMPLETED: 3, - }; - this._zeroFuelWeightZFWCGEntered = false; - this._taxiEntered = false; - this._DistanceToAlt = 0; - this._routeAltFuelWeight = 0; - this._routeAltFuelTime = 0; - this._routeTripFuelWeight = 0; - this._routeTripTime = 0; - this._defaultTaxiFuelWeight = 0.2; - this._rteRsvPercentOOR = false; - this._rteReservedWeightEntered = false; - this._rteReservedPctEntered = false; - this._rteFinalCoeffecient = 0; - this._rteFinalWeightEntered = false; - this._rteFinalTimeEntered = false; - this._routeAltFuelEntered = false; - this._minDestFob = 0; - this._minDestFobEntered = false; - this._isBelowMinDestFob = false; - this._defaultRouteFinalTime = 45; - this._fuelPredDone = false; - this._fuelPlanningPhase = this._fuelPlanningPhases.PLANNING; - this._blockFuelEntered = false; - this._initMessageSettable = false; - this._checkWeightSettable = true; - this._gwInitDisplayed = 0; - /* CPDLC Fields */ - this.tropo = undefined; - this._destDataChecked = false; - this._towerHeadwind = 0; - this._EfobBelowMinClr = false; - this.simbrief = { - route: "", - cruiseAltitude: "", - originIcao: "", - destinationIcao: "", - blockFuel: "", - paxCount: "", - cargo: undefined, - payload: undefined, - estZfw: "", - sendStatus: "READY", - costIndex: "", - navlog: [], - callsign: "", - alternateIcao: "", - avgTropopause: "", - ete: "", - blockTime: "", - outTime: "", - onTime: "", - inTime: "", - offTime: "", - taxiFuel: "", - tripFuel: "", - }; - this.aocWeight = { - blockFuel: undefined, - estZfw: undefined, - taxiFuel: undefined, - tripFuel: undefined, - payload: undefined, - }; - this.aocTimes = { - doors: 0, - off: 0, - out: 0, - on: 0, - in: 0, - }; - this.winds = { - climb: [], - cruise: [], - des: [], - alternate: null, - }; - this.computedVls = undefined; - this.approachSpeeds = undefined; // based on selected config, not current config - this._cruiseEntered = false; - this._blockFuelEntered = false; - this.constraintAlt = 0; - this.fcuSelAlt = 0; - this._forceNextAltitudeUpdate = false; - this._lastUpdateAPTime = NaN; - this.updateAutopilotCooldown = 0; - this._apMasterStatus = false; - this._lastRequestedFLCModeWaypointIndex = -1; - - this._activeCruiseFlightLevelDefaulToFcu = false; - const payloadConstruct = new A32NX_PayloadConstructor(); - this.paxStations = payloadConstruct.paxStations; - this.payloadStations = payloadConstruct.payloadStations; - this.fmsUpdateThrottler = new UpdateThrottler(250); - this._progBrgDist = undefined; - this.preSelectedClbSpeed = undefined; - this.preSelectedCrzSpeed = undefined; - this.managedSpeedTarget = NaN; - this.managedSpeedTargetIsMach = false; - this.managedSpeedClimb = 290; - this.managedSpeedClimbIsPilotEntered = false; - this.managedSpeedClimbMach = 0.78; - // this.managedSpeedClimbMachIsPilotEntered = false; - this.managedSpeedCruise = 290; - this.managedSpeedCruiseIsPilotEntered = false; - this.managedSpeedCruiseMach = 0.78; - // this.managedSpeedCruiseMachIsPilotEntered = false; - this.managedSpeedDescend = 290; - this.managedSpeedDescendPilot = undefined; - this.managedSpeedDescendMach = 0.78; - this.managedSpeedDescendMachPilot = undefined; - // this.managedSpeedDescendMachIsPilotEntered = false; - this.cruiseFlightLevelTimeOut = undefined; - this.flightNumber = undefined; - // this.flightNumber = undefined; - this.cruiseTemperature = undefined; - this.taxiFuelWeight = 0.2; - this.blockFuel = undefined; - this.zeroFuelWeight = undefined; - this.zeroFuelWeightMassCenter = undefined; - this.holdSpeedTarget = undefined; - this.holdIndex = 0; - this.holdDecelReached = false; - this.setHoldSpeedMessageActive = false; - this.managedProfile = new Map(); - this.speedLimitExceeded = false; - this.groundTempAuto = undefined; - this.groundTempPilot = undefined; - this.landingElevation = undefined; - this.destinationLatitude = undefined; - this.destinationLongitude = undefined; - this.toSpeedsNotInserted = false; - this.toSpeedsTooLow = false; - this.vSpeedDisagree = false; - this.takeoffEngineOutSpeed = undefined; - this.checkSpeedModeMessageActive = false; - this.perfClbPredToAltitudePilot = undefined; - this.perfDesPredToAltitudePilot = undefined; - - this.onAirport = () => {}; - - if (this.navigation) { - this.navigation.requiredPerformance.clearPilotRnp(); - } - - // ATSU data - this.atsu = new AtsuFmsClient.FmsClient(this, this.flightPlanService); - - // Reset SimVars - SimVar.SetSimVarValue("L:A32NX_SPEEDS_MANAGED_PFD", "knots", 0); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_MANAGED_ATHR", "knots", 0); - - SimVar.SetSimVarValue("L:A32NX_MachPreselVal", "mach", -1); - SimVar.SetSimVarValue("L:A32NX_SpeedPreselVal", "knots", -1); - - SimVar.SetSimVarValue("L:AIRLINER_DECISION_HEIGHT", "feet", -1); - SimVar.SetSimVarValue("L:AIRLINER_MINIMUM_DESCENT_ALTITUDE", "feet", 0); - - SimVar.SetSimVarValue( - "L:A32NX_FG_ALTITUDE_CONSTRAINT", - "feet", - this.constraintAlt - ); - SimVar.SetSimVarValue("L:A32NX_TO_CONFIG_NORMAL", "Bool", 0); - SimVar.SetSimVarValue("L:A32NX_CABIN_READY", "Bool", 0); - SimVar.SetSimVarValue("L:A32NX_FM_GROSS_WEIGHT", "Number", 0); - - if ( - SimVar.GetSimVarValue("L:A32NX_AUTOTHRUST_DISABLED", "number") === 1 - ) { - SimVar.SetSimVarValue("K:A32NX.ATHR_RESET_DISABLE", "number", 1); - } - - SimVar.SetSimVarValue("L:A32NX_PFD_MSG_SET_HOLD_SPEED", "bool", false); - - if (resetTakeoffData) { - // FMGC Message Queue - this._messageQueue.resetQueue(); - - this.computedVgd = undefined; - this.computedVfs = undefined; - this.computedVss = undefined; - this.perfTOTemp = NaN; - this.setTakeoffFlaps(null); - this.setTakeoffTrim(null); - this.unconfirmedV1Speed = undefined; - this.unconfirmedVRSpeed = undefined; - this.unconfirmedV2Speed = undefined; - this._toFlexChecked = true; - } - - this.arincBusOutputs.forEach((word) => { - word.value = 0; - word.ssm = Arinc429Word.SignStatusMatrix.NoComputedData; - }); - - this.toSpeedsChecks(true); - - this.setRequest('FMGC'); - } - - onUpdate(_deltaTime) { - super.onUpdate(_deltaTime); - - // this.flightPlanManager.update(_deltaTime); - const flightPlanChanged = this.flightPlanService.activeOrTemporary.version !== this.lastFlightPlanVersion; - if (flightPlanChanged) { - this.lastFlightPlanVersion = this.flightPlanService.activeOrTemporary.version; - this.setRequest("FMGC"); - } - - Fmgc.updateFmgcLoop(_deltaTime); - - if (this._debug++ > 180) { - this._debug = 0; - } - const flightPhaseManagerDelta = this.flightPhaseUpdateThrottler.canUpdate(_deltaTime); - if (flightPhaseManagerDelta !== -1) { - this.flightPhaseManager.shouldActivateNextPhase(flightPhaseManagerDelta); - } - - if (this.fmsUpdateThrottler.canUpdate(_deltaTime) !== -1) { - this.checkSpeedLimit(); - this.navigation.update(_deltaTime); - this.getGW(); - this.checkGWParams(); - this.toSpeedsChecks(); - this.thrustReductionAccelerationChecks(); - this.updateThrustReductionAcceleration(); - this.updateTransitionAltitudeLevel(); - this.updateMinimums(); - this.updateIlsCourse(); - this.updatePerfPageAltPredictions(); - this.checkEFOBBelowMin(); - } - - this.A32NXCore.update(); - - if (flightPlanChanged) { - this.updateManagedProfile(); - this.updateDestinationData(); - } - - this.updateAutopilot(); - - if (this._progBrgDistUpdateThrottler.canUpdate(_deltaTime) !== -1) { - this.updateProgDistance(); - } - - if (this.guidanceController) { - this.guidanceController.update(_deltaTime); - } - - if (this.efisSymbols) { - this.efisSymbols.update(_deltaTime); - } - this.arincBusOutputs.forEach((word) => word.writeToSimVarIfDirty()); - } - - onFmPowerStateChanged(newState) { - SimVar.SetSimVarValue('L:A32NX_FM1_HEALTHY_DISCRETE', 'boolean', newState); - SimVar.SetSimVarValue('L:A32NX_FM2_HEALTHY_DISCRETE', 'boolean', newState); - } - - async switchNavDatabase() { - // Only performing a reset of the MCDU for now, no secondary database - // Speed AP returns to selected - //const isSelected = Simplane.getAutoPilotAirspeedSelected(); - //if (isSelected == false) - // SimVar.SetSimVarValue("H:A320_Neo_FCU_SPEED_PULL", "boolean", 1); - // flight plan - this.resetCoroute(); - this.atsu.resetAtisAutoUpdate(); - await this.flightPlanService.reset(); - // stored data - this.dataManager.deleteAllStoredWaypoints(); - // Reset MCDU apart from TakeOff config - this.initVariables(false); - - this.navigation.resetState(); - } - - /** - * This method is called by the FlightPhaseManager after a flight phase change - * This method initializes AP States, initiates CDUPerformancePage changes and other set other required states - * @param prevPhase {FmgcFlightPhases} Previous FmgcFlightPhase - * @param nextPhase {FmgcFlightPhases} New FmgcFlightPhase - */ - onFlightPhaseChanged(prevPhase, nextPhase) { - this.updateConstraints(); - this.updateManagedSpeed(); - - this.setRequest("FMGC"); - - SimVar.SetSimVarValue("L:A32NX_CABIN_READY", "Bool", 0); - - switch (nextPhase) { - case FmgcFlightPhases.TAKEOFF: { - this._destDataChecked = false; - - const plan = this.flightPlanService.active; - - if (plan.performanceData.accelerationAltitude === null) { - // it's important to set this immediately as we don't want to immediately sequence to the climb phase - plan.setPerformanceData('pilotAccelerationAltitude', SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') + parseInt(NXDataStore.get("CONFIG_ACCEL_ALT", "1500"))); - this.updateThrustReductionAcceleration(); - } - if (plan.performanceData.engineOutAccelerationAltitude === null) { - // it's important to set this immediately as we don't want to immediately sequence to the climb phase - plan.setPerformanceData('pilotEngineOutAccelerationAltitude', SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') + parseInt(NXDataStore.get("CONFIG_ACCEL_ALT", "1500"))); - this.updateThrustReductionAcceleration(); - } - - if (this.page.Current === this.page.PerformancePageTakeoff) { - CDUPerformancePage.ShowTAKEOFFPage(this); - } else if (this.page.Current === this.page.ProgressPage) { - CDUProgressPage.ShowPage(this); - } - - /** Arm preselected speed/mach for next flight phase */ - this.updatePreSelSpeedMach(this.preSelectedClbSpeed); - - this._rteRsvPercentOOR = false; - this._rteReservedWeightEntered = false; - this._rteReservedPctEntered = false; - - break; - } - - case FmgcFlightPhases.CLIMB: { - - this._destDataChecked = false; - - if (this.page.Current === this.page.ProgressPage) { - CDUProgressPage.ShowPage(this); - } else { - this.tryUpdatePerfPage(prevPhase, nextPhase); - } - - /** Activate pre selected speed/mach */ - if (prevPhase === FmgcFlightPhases.TAKEOFF) { - this.activatePreSelSpeedMach(this.preSelectedClbSpeed); - } - - /** Arm preselected speed/mach for next flight phase */ - this.updatePreSelSpeedMach(this.preSelectedCrzSpeed); - - if (!this.cruiseLevel) { - this.cruiseLevel = Simplane.getAutoPilotDisplayedAltitudeLockValue('feet') / 100; - } - - break; - } - - case FmgcFlightPhases.CRUISE: { - if (this.page.Current === this.page.ProgressPage) { - CDUProgressPage.ShowPage(this); - } else { - this.tryUpdatePerfPage(prevPhase, nextPhase); - } - - SimVar.SetSimVarValue("L:A32NX_GOAROUND_PASSED", "bool", 0); - Coherent.call("GENERAL_ENG_THROTTLE_MANAGED_MODE_SET", ThrottleMode.AUTO).catch(console.error).catch(console.error); - - /** Activate pre selected speed/mach */ - if (prevPhase === FmgcFlightPhases.CLIMB) { - this.triggerCheckSpeedModeMessage(this.preSelectedCrzSpeed); - this.activatePreSelSpeedMach(this.preSelectedCrzSpeed); - } - - /** Arm preselected speed/mach for next flight phase */ - this.updatePreSelSpeedMach(this.preSelectedDesSpeed); - - break; - } - - case FmgcFlightPhases.DESCENT: { - if (this.page.Current === this.page.ProgressPage) { - CDUProgressPage.ShowPage(this); - } else { - this.tryUpdatePerfPage(prevPhase, nextPhase); - } - - this.checkDestData(); - - Coherent.call("GENERAL_ENG_THROTTLE_MANAGED_MODE_SET", ThrottleMode.AUTO).catch(console.error).catch(console.error); - - this.triggerCheckSpeedModeMessage(undefined); - - this.cruiseLevel = null; - - break; - } - - case FmgcFlightPhases.APPROACH: { - if (this.page.Current === this.page.ProgressPage) { - CDUProgressPage.ShowPage(this); - } else { - this.tryUpdatePerfPage(prevPhase, nextPhase); - } - - // I think this is not necessary to port, as it only calls fs9gps stuff (fms-v2) - // this.flightPlanManager.activateApproach().catch(console.error); - - Coherent.call("GENERAL_ENG_THROTTLE_MANAGED_MODE_SET", ThrottleMode.AUTO).catch(console.error); - SimVar.SetSimVarValue("L:A32NX_GOAROUND_PASSED", "bool", 0); - - this.checkDestData(); - - break; - } - - case FmgcFlightPhases.GOAROUND: { - SimVar.SetSimVarValue("L:A32NX_GOAROUND_INIT_SPEED", "number", Simplane.getIndicatedSpeed()); - - this.flightPlanService.stringMissedApproach(/** @type {FlightPlanLeg} */ (map) => { - this.addMessageToQueue(NXSystemMessages.cstrDelUpToWpt.getModifiedMessage(map.ident)); - }); - - const activePlan = this.flightPlanService.active; - if (activePlan.performanceData.missedAccelerationAltitude === null) { - // it's important to set this immediately as we don't want to immediately sequence to the climb phase - activePlan.setPerformanceData('pilotMissedAccelerationAltitude', SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') + parseInt(NXDataStore.get("CONFIG_ENG_OUT_ACCEL_ALT", "1500"))); - this.updateThrustReductionAcceleration(); - } - if (activePlan.performanceData.missedEngineOutAccelerationAltitude === null) { - // it's important to set this immediately as we don't want to immediately sequence to the climb phase - activePlan.setPerformanceData('pilotMissedEngineOutAccelerationAltitude', SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') + parseInt(NXDataStore.get("CONFIG_ENG_OUT_ACCEL_ALT", "1500"))); - this.updateThrustReductionAcceleration(); - } - - if (this.page.Current === this.page.ProgressPage) { - CDUProgressPage.ShowPage(this); - } else { - this.tryUpdatePerfPage(prevPhase, nextPhase); - } - - break; - } - - case FmgcFlightPhases.DONE: - CDUIdentPage.ShowPage(this); - - this.flightPlanService.reset().then(() => { - this.initVariables(); - this.dataManager.deleteAllStoredWaypoints(); - this.setScratchpadText(''); - SimVar.SetSimVarValue('L:A32NX_COLD_AND_DARK_SPAWN', 'Bool', true).then(() => { - CDUIdentPage.ShowPage(this); - }); - }).catch(console.error); - break; - } - } - - triggerCheckSpeedModeMessage(preselectedSpeed) { - const isSpeedSelected = !Simplane.getAutoPilotAirspeedManaged(); - const hasPreselectedSpeed = preselectedSpeed !== undefined; - - if (!this.checkSpeedModeMessageActive && isSpeedSelected && !hasPreselectedSpeed) { - this.checkSpeedModeMessageActive = true; - this.addMessageToQueue( - NXSystemMessages.checkSpeedMode, - () => !this.checkSpeedModeMessageActive, - () => { - this.checkSpeedModeMessageActive = false; - SimVar.SetSimVarValue("L:A32NX_PFD_MSG_CHECK_SPEED_MODE", "bool", false); - }, - ); - SimVar.SetSimVarValue("L:A32NX_PFD_MSG_CHECK_SPEED_MODE", "bool", true); - } - } - - clearCheckSpeedModeMessage() { - if (this.checkSpeedModeMessageActive && Simplane.getAutoPilotAirspeedManaged()) { - this.checkSpeedModeMessageActive = false; - this.removeMessageFromQueue(NXSystemMessages.checkSpeedMode.text); - SimVar.SetSimVarValue("L:A32NX_PFD_MSG_CHECK_SPEED_MODE", "bool", false); - } - } - - /** FIXME these functions are in the new VNAV but not in this branch, remove when able */ - /** - * - * @param {Feet} alt geopotential altitude - * @returns °C - */ - getIsaTemp(alt) { - if (alt > (this.tropo ? this.tropo : 36090)) { - return -56.5; - } - return 15 - (0.0019812 * alt); - } - - /** - * - * @param {Feet} alt geopotential altitude - * @param {Degrees} isaDev temperature deviation from ISA conditions - * @returns °C - */ - getTemp(alt, isaDev = 0) { - return this.getIsaTemp(alt) + isaDev; - } - - /** - * - * @param {Feet} alt geopotential altitude - * @param {Degrees} isaDev temperature deviation from ISA conditions - * @returns hPa - */ - getPressure(alt, isaDev = 0) { - if (alt > (this.tropo ? this.tropo : 36090)) { - return ((216.65 + isaDev) / 288.15) ** 5.25588 * 1013.2; - } - return ((288.15 - 0.0019812 * alt + isaDev) / 288.15) ** 5.25588 * 1013.2; - } - - getPressureAltAtElevation(elev, qnh = 1013.2) { - const p0 = qnh < 500 ? 29.92 : 1013.2; - return elev + 145442.15 * (1 - Math.pow((qnh / p0), 0.190263)); - } - - getPressureAlt() { - for (let n = 1; n <= 3; n++) { - const zp = Arinc429Word.fromSimVarValue(`L:A32NX_ADIRS_ADR_${n}_ALTITUDE`); - if (zp.isNormalOperation()) { - return zp.value; - } - } - return null; - } - - getBaroCorrection1() { - // FIXME hook up to ADIRU or FCU - return Simplane.getPressureValue("millibar"); - } - - /** - * @returns {Degrees} temperature deviation from ISA conditions - */ - getIsaDeviation() { - const geoAlt = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); - const temperature = SimVar.GetSimVarValue('AMBIENT TEMPERATURE', 'celsius'); - return temperature - this.getIsaTemp(geoAlt); - } - /** FIXME ^these functions are in the new VNAV but not in this branch, remove when able */ - - // TODO better decel distance calc - calculateDecelDist(fromSpeed, toSpeed) { - return Math.min(20, Math.max(3, (fromSpeed - toSpeed) * 0.15)); - } - - /* - When the aircraft is in the holding, predictions assume that the leg is flown at holding speed - with a vertical speed equal to - 1000 ft/mn until reaching a restrictive altitude constraint, the - FCU altitude or the exit fix. If FCU or constraint altitude is reached first, the rest of the - pattern is assumed to be flown level at that altitude - */ - getHoldingSpeed(speedConstraint = undefined, altitude = undefined) { - const fcuAltitude = SimVar.GetSimVarValue('AUTOPILOT ALTITUDE LOCK VAR:3', 'feet'); - const alt = Math.max(fcuAltitude, altitude ? altitude : 0); - - let kcas = SimVar.GetSimVarValue('L:A32NX_SPEEDS_GD', 'number'); - if (this.flightPhaseManager.phase === FmgcFlightPhases.APPROACH) { - kcas = this.getAppManagedSpeed(); - } - - if (speedConstraint > 100) { - kcas = Math.min(kcas, speedConstraint); - } - - // apply icao limits - if (alt < 14000) { - kcas = Math.min(230, kcas); - } else if (alt < 20000) { - kcas = Math.min(240, kcas); - } else if (alt < 34000) { - kcas = Math.min(265, kcas); - } else { - const isaDeviation = this.getIsaDeviation(); - const temperature = 273.15 + this.getTemp(alt, isaDeviation); - const pressure = this.getPressure(alt, isaDeviation); - kcas = Math.min( - _convertMachToKCas(0.83, temperature, pressure), - kcas, - ); - } - - // apply speed limit/alt - if (this.flightPhaseManager.phase <= FmgcFlightPhases.CRUISE) { - if (this.climbSpeedLimit !== null && alt <= this.climbSpeedLimitAlt) { - kcas = Math.min(this.climbSpeedLimit, kcas); - } - } else if (this.flightPhaseManager.phase < FmgcFlightPhases.GOAROUND) { - if (this.descentSpeedLimit !== null && alt <= this.descentSpeedLimitAlt) { - kcas = Math.min(this.descentSpeedLimit, kcas); - } - } - - kcas = Math.max(kcas, this.computedVls); - - return Math.ceil(kcas); - } - - updateHoldingSpeed() { - /** - * @type {BaseFlightPlan} - */ - const plan = this.flightPlanService.active; - const currentLegIndex = plan.activeLegIndex; - const nextLegIndex = currentLegIndex + 1; - const currentLegConstraints = this.managedProfile.get(currentLegIndex) || {}; - const nextLegConstraints = this.managedProfile.get(nextLegIndex) || {}; - - const currentLeg = plan.maybeElementAt(currentLegIndex); - const nextLeg = plan.maybeElementAt(nextLegIndex); - - const casWord = ADIRS.getCalibratedAirspeed(); - const cas = casWord.isNormalOperation() ? casWord.value : 0; - - let enableHoldSpeedWarning = false; - let holdSpeedTarget = 0; - let holdDecelReached = this.holdDecelReached; - // FIXME big hack until VNAV can do this - if (currentLeg && currentLeg.isDiscontinuity === false && currentLeg.type === 'HM') { - holdSpeedTarget = this.getHoldingSpeed(currentLegConstraints.descentSpeed, currentLegConstraints.descentAltitude); - holdDecelReached = true; - enableHoldSpeedWarning = !Simplane.getAutoPilotAirspeedManaged(); - this.holdIndex = plan.activeLegIndex; - } else if (nextLeg && nextLeg.isDiscontinuity === false && nextLeg.type === 'HM') { - const adirLat = ADIRS.getLatitude(); - const adirLong = ADIRS.getLongitude(); - - if (adirLat.isNormalOperation() && adirLong.isNormalOperation()) { - holdSpeedTarget = this.getHoldingSpeed(nextLegConstraints.descentSpeed, nextLegConstraints.descentAltitude); - - const dtg = this.guidanceController.activeLegDtg; - // decel range limits are [3, 20] NM - const decelDist = this.calculateDecelDist(cas, holdSpeedTarget); - if (dtg < decelDist) { - holdDecelReached = true; - } - - const gsWord = ADIRS.getGroundSpeed(); - const gs = gsWord.isNormalOperation() ? gsWord.value : 0; - const warningDist = decelDist + gs / 120; - if (!Simplane.getAutoPilotAirspeedManaged() && dtg <= warningDist) { - enableHoldSpeedWarning = true; - } - } - this.holdIndex = plan.activeLegIndex + 1; - } else { - this.holdIndex = 0; - holdDecelReached = false; - } - - if (holdDecelReached !== this.holdDecelReached) { - this.holdDecelReached = holdDecelReached; - SimVar.SetSimVarValue('L:A32NX_FM_HOLD_DECEL', 'bool', this.holdDecelReached); - } - - if (holdSpeedTarget !== this.holdSpeedTarget) { - this.holdSpeedTarget = holdSpeedTarget; - SimVar.SetSimVarValue('L:A32NX_FM_HOLD_SPEED', 'number', this.holdSpeedTarget); - } - - if (enableHoldSpeedWarning && (cas - this.holdSpeedTarget) > 5) { - if (!this.setHoldSpeedMessageActive) { - this.setHoldSpeedMessageActive = true; - this.addMessageToQueue( - NXSystemMessages.setHoldSpeed, - () => !this.setHoldSpeedMessageActive, - () => SimVar.SetSimVarValue("L:A32NX_PFD_MSG_SET_HOLD_SPEED", "bool", false), - ); - SimVar.SetSimVarValue("L:A32NX_PFD_MSG_SET_HOLD_SPEED", "bool", true); - } - } else if (this.setHoldSpeedMessageActive) { - SimVar.SetSimVarValue("L:A32NX_PFD_MSG_SET_HOLD_SPEED", "bool", false); - this.setHoldSpeedMessageActive = false; - } - } - - getManagedTargets(v, m) { - //const vM = _convertMachToKCas(m, _convertCtoK(Simplane.getAmbientTemperature()), SimVar.GetSimVarValue("AMBIENT PRESSURE", "millibar")); - const vM = SimVar.GetGameVarValue("FROM MACH TO KIAS", "number", m); - return v > vM ? [vM, true] : [v, false]; - } - - updateManagedSpeeds() { - if (!this.managedSpeedClimbIsPilotEntered) { - this.managedSpeedClimb = this.getClbManagedSpeedFromCostIndex(); - } - if (!this.managedSpeedCruiseIsPilotEntered) { - this.managedSpeedCruise = this.getCrzManagedSpeedFromCostIndex(); - } - - this.managedSpeedDescend = this.getDesManagedSpeedFromCostIndex(); - } - - updateManagedSpeed() { - let vPfd = 0; - let isMach = false; - - this.updateHoldingSpeed(); - this.clearCheckSpeedModeMessage(); - - if (SimVar.GetSimVarValue("L:A32NX_FMA_EXPEDITE_MODE", "number") === 1) { - const verticalMode = SimVar.GetSimVarValue("L:A32NX_FMA_VERTICAL_MODE", "number"); - if (verticalMode === 12) { - switch (SimVar.GetSimVarValue("L:A32NX_FLAPS_HANDLE_INDEX", "Number")) { - case 0: { - this.managedSpeedTarget = SimVar.GetSimVarValue("L:A32NX_SPEEDS_GD", "number"); - break; - } - case 1: { - this.managedSpeedTarget = SimVar.GetSimVarValue("L:A32NX_SPEEDS_S", "number"); - break; - } - default: { - this.managedSpeedTarget = SimVar.GetSimVarValue("L:A32NX_SPEEDS_F", "number"); - } - } - } else if (verticalMode === 13) { - this.managedSpeedTarget = SimVar.GetSimVarValue("L:A32NX_FLAPS_HANDLE_INDEX", "Number") === 0 ? Math.min(340, SimVar.GetGameVarValue("FROM MACH TO KIAS", "number", 0.8)) : SimVar.GetSimVarValue("L:A32NX_SPEEDS_VMAX", "number") - 10; - } - vPfd = this.managedSpeedTarget; - } else if (this.holdDecelReached) { - vPfd = this.holdSpeedTarget; - this.managedSpeedTarget = this.holdSpeedTarget; - } else { - if (this.setHoldSpeedMessageActive) { - this.setHoldSpeedMessageActive = false; - SimVar.SetSimVarValue("L:A32NX_PFD_MSG_SET_HOLD_SPEED", "bool", false); - this.removeMessageFromQueue(NXSystemMessages.setHoldSpeed.text); - } - - const engineOut = !this.isAllEngineOn(); - - switch (this.flightPhaseManager.phase) { - case FmgcFlightPhases.PREFLIGHT: { - if (this.v2Speed) { - vPfd = this.v2Speed; - this.managedSpeedTarget = this.v2Speed + 10; - } - break; - } - case FmgcFlightPhases.TAKEOFF: { - if (this.v2Speed) { - vPfd = this.v2Speed; - this.managedSpeedTarget = engineOut - ? Math.min(this.v2Speed + 15, Math.max(this.v2Speed, this.takeoffEngineOutSpeed ? this.takeoffEngineOutSpeed : 0)) - : this.v2Speed + 10; - } - break; - } - case FmgcFlightPhases.CLIMB: { - let speed = this.managedSpeedClimb; - - if (this.climbSpeedLimit !== undefined && SimVar.GetSimVarValue("INDICATED ALTITUDE", "feet") < this.climbSpeedLimitAlt) { - speed = Math.min(speed, this.climbSpeedLimit); - } - - speed = Math.min(speed, this.getSpeedConstraint()); - - [this.managedSpeedTarget, isMach] = this.getManagedTargets(speed, this.managedSpeedClimbMach); - vPfd = this.managedSpeedTarget; - break; - } - case FmgcFlightPhases.CRUISE: { - let speed = this.managedSpeedCruise; - - if (this.climbSpeedLimit !== undefined && SimVar.GetSimVarValue("INDICATED ALTITUDE", "feet") < this.climbSpeedLimitAlt) { - speed = Math.min(speed, this.climbSpeedLimit); - } - - [this.managedSpeedTarget, isMach] = this.getManagedTargets(speed, this.managedSpeedCruiseMach); - vPfd = this.managedSpeedTarget; - break; - } - case FmgcFlightPhases.DESCENT: { - // We fetch this data from VNAV - vPfd = SimVar.GetSimVarValue("L:A32NX_SPEEDS_MANAGED_PFD", "knots"); - this.managedSpeedTarget = SimVar.GetSimVarValue("L:A32NX_SPEEDS_MANAGED_ATHR", "knots"); - - // Whether to use Mach or not should be based on the original managed speed, not whatever VNAV uses under the hood to vary it. - // Also, VNAV already does the conversion from Mach if necessary - isMach = this.getManagedTargets(this.getManagedDescentSpeed(), this.getManagedDescentSpeedMach())[1]; - break; - } - case FmgcFlightPhases.APPROACH: { - // the displayed target is Vapp (with GSmini) - // the guidance target is lower limited by FAC manouvering speeds (O, S, F) unless in landing config - // constraints are not considered - const speed = this.getAppManagedSpeed(); - vPfd = this.getVAppGsMini(); - - this.managedSpeedTarget = Math.max(speed, vPfd); - break; - } - case FmgcFlightPhases.GOAROUND: { - if (SimVar.GetSimVarValue("L:A32NX_FMA_VERTICAL_MODE", "number") === 41 /* SRS GA */) { - const speed = Math.min( - this.computedVls + (engineOut ? 15 : 25), - Math.max( - SimVar.GetSimVarValue("L:A32NX_GOAROUND_INIT_SPEED", "number"), - this.getVApp(), - ), - SimVar.GetSimVarValue("L:A32NX_SPEEDS_VMAX", "number") - 5, - ); - - vPfd = speed; - this.managedSpeedTarget = speed; - } else { - const speedConstraint = this.getSpeedConstraint(); - const speed = Math.min(this.computedVgd, speedConstraint); - - vPfd = speed; - this.managedSpeedTarget = speed; - } - break; - } - } - } - - // Automatically change fcu mach/speed mode - if (this.managedSpeedTargetIsMach !== isMach) { - if (isMach) { - SimVar.SetSimVarValue("K:AP_MANAGED_SPEED_IN_MACH_ON", "number", 1); - } else { - SimVar.SetSimVarValue("K:AP_MANAGED_SPEED_IN_MACH_OFF", "number", 1); - } - this.managedSpeedTargetIsMach = isMach; - } - - // Overspeed protection - const Vtap = Math.min(this.managedSpeedTarget, SimVar.GetSimVarValue("L:A32NX_SPEEDS_VMAX", "number")); - - SimVar.SetSimVarValue("L:A32NX_SPEEDS_MANAGED_PFD", "knots", vPfd); - SimVar.SetSimVarValue("L:A32NX_SPEEDS_MANAGED_ATHR", "knots", Vtap); - - if (this.isAirspeedManaged()) { - Coherent.call("AP_SPD_VAR_SET", 0, Vtap).catch(console.error); - } - - // Reset V1/R/2 speed after the TAKEOFF phase - if (this.flightPhaseManager.phase > FmgcFlightPhases.TAKEOFF) { - this.v1Speed = null; - this.vrSpeed = null; - this.v2Speed = null; - } - } - - activatePreSelSpeedMach(preSel) { - if (preSel) { - SimVar.SetSimVarValue("K:A32NX.FMS_PRESET_SPD_ACTIVATE", "number", 1); - } - } - - updatePreSelSpeedMach(preSel) { - // The timeout is required to create a delay for the current value to be read and the new one to be set - setTimeout(() => { - if (preSel) { - if (preSel > 1) { - SimVar.SetSimVarValue("L:A32NX_SpeedPreselVal", "knots", preSel); - SimVar.SetSimVarValue("L:A32NX_MachPreselVal", "mach", -1); - } else { - SimVar.SetSimVarValue("L:A32NX_SpeedPreselVal", "knots", -1); - SimVar.SetSimVarValue("L:A32NX_MachPreselVal", "mach", preSel); - } - } else { - SimVar.SetSimVarValue("L:A32NX_SpeedPreselVal", "knots", -1); - SimVar.SetSimVarValue("L:A32NX_MachPreselVal", "mach", -1); - } - }, 200); - } - - checkSpeedLimit() { - let speedLimit; - let speedLimitAlt; - switch (this.flightPhaseManager.phase) { - case FmgcFlightPhases.CLIMB: - case FmgcFlightPhases.CRUISE: - speedLimit = this.climbSpeedLimit; - speedLimitAlt = this.climbSpeedLimitAlt; - break; - case FmgcFlightPhases.DESCENT: - speedLimit = this.descentSpeedLimit; - speedLimitAlt = this.descentSpeedLimitAlt; - break; - default: - // no speed limit in other phases - this.speedLimitExceeded = false; - return; - } - - if (speedLimit === undefined) { - this.speedLimitExceeded = false; - return; - } - - const cas = ADIRS.getCalibratedAirspeed(); - const alt = ADIRS.getBaroCorrectedAltitude(); - - if (this.speedLimitExceeded) { - const resetLimitExceeded = !cas.isNormalOperation() || !alt.isNormalOperation() || alt.value > speedLimitAlt || cas.value <= (speedLimit + 5); - if (resetLimitExceeded) { - this.speedLimitExceeded = false; - this.removeMessageFromQueue(NXSystemMessages.spdLimExceeded.text); - } - } else if (cas.isNormalOperation() && alt.isNormalOperation()) { - const setLimitExceeded = alt.value < (speedLimitAlt - 150) && cas.value > (speedLimit + 10); - if (setLimitExceeded) { - this.speedLimitExceeded = true; - this.addMessageToQueue(NXSystemMessages.spdLimExceeded, () => !this.speedLimitExceeded); - } - } - } - - updateAutopilot() { - const now = performance.now(); - const dt = now - this._lastUpdateAPTime; - let apLogicOn = (this._apMasterStatus || Simplane.getAutoPilotFlightDirectorActive(1)); - this._lastUpdateAPTime = now; - if (isFinite(dt)) { - this.updateAutopilotCooldown -= dt; - } - if (SimVar.GetSimVarValue("L:AIRLINER_FMC_FORCE_NEXT_UPDATE", "number") === 1) { - SimVar.SetSimVarValue("L:AIRLINER_FMC_FORCE_NEXT_UPDATE", "number", 0); - this.updateAutopilotCooldown = -1; - } - - if (this.flightPhaseManager.phase === FmgcFlightPhases.TAKEOFF && !this.isAllEngineOn() && this.takeoffEngineOutSpeed === undefined) { - const casWord = ADIRS.getCalibratedAirspeed(); - this.takeoffEngineOutSpeed = casWord.isNormalOperation() ? casWord.value : undefined; - } - - if (this.updateAutopilotCooldown < 0) { - this.updatePerfSpeeds(); - this.updateConstraints(); - this.updateManagedSpeed(); - const currentApMasterStatus = SimVar.GetSimVarValue("AUTOPILOT MASTER", "boolean"); - if (currentApMasterStatus !== this._apMasterStatus) { - this._apMasterStatus = currentApMasterStatus; - apLogicOn = (this._apMasterStatus || Simplane.getAutoPilotFlightDirectorActive(1)); - this._forceNextAltitudeUpdate = true; - console.log("Enforce AP in Altitude Lock mode. Cause : AP Master Status has changed."); - SimVar.SetSimVarValue("L:A320_NEO_FCU_FORCE_IDLE_VS", "Number", 1); - if (this._apMasterStatus) { - if (this.flightPlanService.hasActive && this.flightPlanService.active.legCount === 0) { - this._onModeSelectedAltitude(); - this._onModeSelectedHeading(); - } - } - } - if (apLogicOn) { - if (!Simplane.getAutoPilotFLCActive() && !SimVar.GetSimVarValue("AUTOPILOT AIRSPEED HOLD", "Boolean")) { - SimVar.SetSimVarValue("K:AP_PANEL_SPEED_HOLD", "Number", 1); - } - if (!SimVar.GetSimVarValue("AUTOPILOT HEADING LOCK", "Boolean")) { - if (!SimVar.GetSimVarValue("AUTOPILOT APPROACH HOLD", "Boolean")) { - SimVar.SetSimVarValue("K:AP_PANEL_HEADING_HOLD", "Number", 1); - } - } - } - - if (this.isAltitudeManaged()) { - const plan = this.flightPlanService.active; - - const prevWaypoint = plan.hasElement(plan.activeLegIndex - 1); - const nextWaypoint = plan.hasElement(plan.activeLegIndex + 1); - - if (prevWaypoint && nextWaypoint) { - const activeWpIdx = plan.activeLegIndex; - - if (activeWpIdx !== this.activeWpIdx) { - this.activeWpIdx = activeWpIdx; - this.updateConstraints(); - } - if (this.constraintAlt) { - Coherent.call("AP_ALT_VAR_SET_ENGLISH", 2, this.constraintAlt, this._forceNextAltitudeUpdate).catch(console.error); - this._forceNextAltitudeUpdate = false; - } else { - const altitude = Simplane.getAutoPilotSelectedAltitudeLockValue("feet"); - if (isFinite(altitude)) { - Coherent.call("AP_ALT_VAR_SET_ENGLISH", 2, altitude, this._forceNextAltitudeUpdate).catch(console.error); - this._forceNextAltitudeUpdate = false; - } - } - } else { - const altitude = Simplane.getAutoPilotSelectedAltitudeLockValue("feet"); - if (isFinite(altitude)) { - SimVar.SetSimVarValue("L:A32NX_FG_ALTITUDE_CONSTRAINT", "feet", 0); - Coherent.call("AP_ALT_VAR_SET_ENGLISH", 2, altitude, this._forceNextAltitudeUpdate).catch(console.error); - this._forceNextAltitudeUpdate = false; - } - } - } - - if (Simplane.getAutoPilotAltitudeManaged() && this.flightPlanService.hasActive && SimVar.GetSimVarValue("L:A320_NEO_FCU_STATE", "number") !== 1) { - const currentWaypointIndex = this.flightPlanService.active.activeLegIndex; - if (currentWaypointIndex !== this._lastRequestedFLCModeWaypointIndex) { - this._lastRequestedFLCModeWaypointIndex = currentWaypointIndex; - setTimeout(() => { - if (Simplane.getAutoPilotAltitudeManaged()) { - this._onModeManagedAltitude(); - } - }, 1000); - } - } - - if (this.flightPhaseManager.phase === FmgcFlightPhases.GOAROUND && apLogicOn) { - //depending if on HDR/TRK or NAV mode, select appropriate Alt Mode (WIP) - //this._onModeManagedAltitude(); - this._onModeSelectedAltitude(); - } - this.updateAutopilotCooldown = this._apCooldown; - } - } - - /** - * Updates performance speeds such as GD, F, S, Vls and approach speeds - */ - updatePerfSpeeds() { - this.computedVgd = SimVar.GetSimVarValue("L:A32NX_SPEEDS_GD", "number"); - this.computedVfs = SimVar.GetSimVarValue("L:A32NX_SPEEDS_F", "number"); - this.computedVss = SimVar.GetSimVarValue("L:A32NX_SPEEDS_S", "number"); - this.computedVls = SimVar.GetSimVarValue("L:A32NX_SPEEDS_VLS", "number"); - - let weight = this.tryEstimateLandingWeight(); - const vnavPrediction = this.guidanceController.vnavDriver.getDestinationPrediction(); - // Actual weight is used during approach phase (FCOM bulletin 46/2), and we also assume during go-around - // Fallback gross weight set to 64.3T (MZFW), which is replaced by FMGW once input in FMS to avoid function returning undefined results. - if (this.flightPhaseManager.phase >= FmgcFlightPhases.APPROACH || !isFinite(weight)) { - weight = (this.getGW() == 0) ? 64.3 : this.getGW(); - } else if (vnavPrediction && Number.isFinite(vnavPrediction.estimatedFuelOnBoard)) { - weight = this.zeroFuelWeight + Math.max(0, vnavPrediction.estimatedFuelOnBoard * 0.4535934 / 1000); - } - // if pilot has set approach wind in MCDU we use it, otherwise fall back to current measured wind - if (isFinite(this.perfApprWindSpeed) && isFinite(this.perfApprWindHeading)) { - this.approachSpeeds = new NXSpeedsApp(weight, this.perfApprFlaps3, this._towerHeadwind); - } else { - this.approachSpeeds = new NXSpeedsApp(weight, this.perfApprFlaps3); - } - this.approachSpeeds.valid = this.flightPhaseManager.phase >= FmgcFlightPhases.APPROACH || isFinite(weight); - } - - updateConstraints() { - const activeFpIndex = this.flightPlanService.activeLegIndex; - const constraints = this.managedProfile.get(activeFpIndex); - const fcuSelAlt = Simplane.getAutoPilotDisplayedAltitudeLockValue("feet"); - - let constraintAlt = 0; - if (constraints) { - // Altitude constraints are not sent in GA phase. While we cannot engage CLB anyways, ALT counts as a managed mode, so we don't want to show - // a magenta altitude in ALT due to a constraint - if ((this.flightPhaseManager.phase < FmgcFlightPhases.CRUISE) && isFinite(constraints.climbAltitude) && constraints.climbAltitude < fcuSelAlt) { - constraintAlt = constraints.climbAltitude; - } - - if ((this.flightPhaseManager.phase > FmgcFlightPhases.CRUISE && this.flightPhaseManager.phase < FmgcFlightPhases.GOAROUND) && isFinite(constraints.descentAltitude) && constraints.descentAltitude > fcuSelAlt) { - constraintAlt = constraints.descentAltitude; - } - } - - if (constraintAlt !== this.constraintAlt) { - this.constraintAlt = constraintAlt; - SimVar.SetSimVarValue("L:A32NX_FG_ALTITUDE_CONSTRAINT", "feet", this.constraintAlt); - } - } - - // TODO/VNAV: Speed constraint - getSpeedConstraint() { - if (!this.navModeEngaged()) { - return Infinity; - } - - return this.getNavModeSpeedConstraint(); - } - - getNavModeSpeedConstraint() { - const activeLegIndex = this.guidanceController.activeTransIndex >= 0 ? this.guidanceController.activeTransIndex : this.guidanceController.activeLegIndex; - const constraints = this.managedProfile.get(activeLegIndex); - if (constraints) { - if (this.flightPhaseManager.phase < FmgcFlightPhases.CRUISE || this.flightPhaseManager.phase === FmgcFlightPhases.GOAROUND) { - return constraints.climbSpeed; - } - - if (this.flightPhaseManager.phase > FmgcFlightPhases.CRUISE && this.flightPhaseManager.phase < FmgcFlightPhases.GOAROUND) { - // FIXME proper decel calc - if (this.guidanceController.activeLegDtg < this.calculateDecelDist(Math.min(constraints.previousDescentSpeed, this.getManagedDescentSpeed()), constraints.descentSpeed)) { - return constraints.descentSpeed; - } else { - return constraints.previousDescentSpeed; - } - } - } - - return Infinity; - } - - updateManagedProfile() { - this.managedProfile.clear(); - - const plan = this.flightPlanService.active; - - const origin = plan.originAirport; - const destination = plan.destinationAirport; - const destinationElevation = destination ? destination.location.alt : 0; - - // TODO should we save a constraint already propagated to the current leg? - - // propagate descent speed constraints forward - let currentSpeedConstraint = Infinity; - let previousSpeedConstraint = Infinity; - for (let index = 0; index < Math.min(plan.firstMissedApproachLegIndex, plan.legCount); index++) { - const leg = plan.elementAt(index); - - if (leg.isDiscontinuity === true) { - continue; - } - - if (leg.constraintType === 2 /** DES */) { - if (leg.speedConstraint) { - currentSpeedConstraint = Math.min(currentSpeedConstraint, Math.round(leg.speedConstraint.speed)); - } - } - - this.managedProfile.set(index, { - descentSpeed: currentSpeedConstraint, - previousDescentSpeed: previousSpeedConstraint, - climbSpeed: Infinity, - previousClimbSpeed: Infinity, - climbAltitude: Infinity, - descentAltitude: -Infinity, - }); - - previousSpeedConstraint = currentSpeedConstraint; - } - - // propagate climb speed constraints backward - // propagate alt constraints backward - currentSpeedConstraint = Infinity; - previousSpeedConstraint = Infinity; - let currentDesConstraint = -Infinity; - let currentClbConstraint = Infinity; - - for (let index = Math.min(plan.firstMissedApproachLegIndex, plan.legCount) - 1; index >= 0; index--) { - const leg = plan.elementAt(index); - - if (leg.isDiscontinuity === true) { - continue; - } - - const altConstraint = leg.altitudeConstraint; - const speedConstraint = leg.speedConstraint; - - if (leg.constraintType === 1 /** CLB */) { - if (speedConstraint) { - currentSpeedConstraint = Math.min(currentSpeedConstraint, Math.round(speedConstraint.speed)); - } - - if (altConstraint) { - switch (altConstraint.altitudeDescriptor) { - case "@": // at alt 1 - case "-": // at or below alt 1 - case "B": // between alt 1 and alt 2 - currentClbConstraint = Math.min(currentClbConstraint, Math.round(altConstraint.altitude1)); - break; - default: - // not constraining - } - } - } else if (leg.constraintType === 2 /** DES */) { - if (altConstraint) { - switch (altConstraint.altitudeDescriptor) { - case "@": // at alt 1 - case "+": // at or above alt 1 - case "I": // alt1 is at for FACF, Alt2 is glidelope intercept - case "J": // alt1 is at or above for FACF, Alt2 is glideslope intercept - case "V": // alt1 is procedure alt for step-down, Alt2 is at alt for vertical path angle - case "X": // alt 1 is at, Alt 2 is on the vertical angle - currentDesConstraint = Math.max(currentDesConstraint, Math.round(altConstraint.altitude1)); - break; - case "B": // between alt 1 and alt 2 - currentDesConstraint = Math.max(currentDesConstraint, Math.round(altConstraint.altitude2)); - break; - default: - // not constraining - } - } - } - - const profilePoint = this.managedProfile.get(index); - profilePoint.climbSpeed = currentSpeedConstraint; - profilePoint.previousClimbSpeed = previousSpeedConstraint; - profilePoint.climbAltitude = currentClbConstraint; - profilePoint.descentAltitude = Math.max(destinationElevation, currentDesConstraint); - previousSpeedConstraint = currentSpeedConstraint; - } - } - - async updateDestinationData() { - let landingElevation; - let latitude; - let longitude; - - /** @type {import('msfs-navdata').Runway} */ - const runway = this.flightPlanService.active.destinationRunway; - - if (runway) { - landingElevation = runway.thresholdLocation.alt; - latitude = runway.thresholdLocation.lat; - longitude = runway.thresholdLocation.long; - } else { - /** @type {import('msfs-navdata').Airport} */ - const airport = this.flightPlanService.active.destinationAirport; - - if (airport) { - const ele = airport.location.alt; - - landingElevation = isFinite(ele) ? ele : undefined; - latitude = airport.location.lat; - longitude = airport.location.long; - } - } - - if (this.landingElevation !== landingElevation) { - this.landingElevation = landingElevation; - - const ssm = landingElevation !== undefined ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData; - - this.arincLandingElevation.setBnrValue(landingElevation ? landingElevation : 0, ssm, 14, 16384, -2048); - } - - if (this.destinationLatitude !== latitude) { - this.destinationLatitude = latitude; - - const ssm = latitude !== undefined ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData; - - this.arincDestinationLatitude.setBnrValue(latitude ? latitude : 0, ssm, 18, 180, -180); - } - - if (this.destinationLongitude !== longitude) { - this.destinationLongitude = longitude; - - const ssm = longitude !== undefined ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData; - - this.arincDestinationLongitude.setBnrValue(longitude ? longitude : 0, ssm, 18, 180, -180); - } - } - - updateMinimums() { - const inRange = this.shouldTransmitMinimums(); - - const mdaValid = inRange && this.perfApprMDA !== null; - const dhValid = !mdaValid && inRange && typeof this.perfApprDH === 'number'; - - const mdaSsm = mdaValid ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData; - const dhSsm = dhValid ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData; - - this.arincMDA.setBnrValue(mdaValid ? this.perfApprMDA : 0, mdaSsm, 17, 131072, 0); - this.arincDH.setBnrValue(dhValid ? this.perfApprDH : 0, dhSsm, 16, 8192, 0); - this.arincEisWord2.setBitValue(29, inRange && this.perfApprDH === "NO DH"); - // FIXME we need to handle these better - this.arincEisWord2.ssm = Arinc429Word.SignStatusMatrix.NormalOperation; - } - - shouldTransmitMinimums() { - const phase = this.flightPhaseManager.phase; - const distanceToDestination = this.getDistanceToDestination(); - const isCloseToDestination = Number.isFinite(distanceToDestination) ? distanceToDestination < 250 : true; - - return (phase > FmgcFlightPhases.CRUISE || (phase === FmgcFlightPhases.CRUISE && isCloseToDestination)); - } - - getClbManagedSpeedFromCostIndex() { - const dCI = ((this.costIndex ? this.costIndex : 0) / 999) ** 2; - return 290 * (1 - dCI) + 330 * dCI; - } - - getCrzManagedSpeedFromCostIndex() { - const dCI = ((this.costIndex ? this.costIndex : 0) / 999) ** 2; - return 290 * (1 - dCI) + 310 * dCI; - } - - getDesManagedSpeedFromCostIndex() { - const dCI = (this.costIndex ? this.costIndex : 0) / 999; - return 288 * (1 - dCI) + 300 * dCI; - } - - getAppManagedSpeed() { - switch (SimVar.GetSimVarValue("L:A32NX_FLAPS_HANDLE_INDEX", "Number")) { - case 0: return this.computedVgd; - case 1: return this.computedVss; - case 3: return this.perfApprFlaps3 ? this.getVApp() : this.computedVfs; - case 4: return this.getVApp(); - default: return this.computedVfs; - } - } - - /* FMS EVENTS */ - - onPowerOn() { - super.onPowerOn(); - const gpsDriven = SimVar.GetSimVarValue("GPS DRIVES NAV1", "Bool"); - if (!gpsDriven) { - SimVar.SetSimVarValue("K:TOGGLE_GPS_DRIVES_NAV1", "Bool", 0); - } - - this._onModeSelectedHeading(); - this._onModeSelectedAltitude(); - - SimVar.SetSimVarValue("K:VS_SLOT_INDEX_SET", "number", 1); - - this.taxiFuelWeight = 0.2; - CDUInitPage.updateTowIfNeeded(this); - } - - onEvent(_event) { - if (_event === "MODE_SELECTED_HEADING") { - if (Simplane.getAutoPilotHeadingManaged()) { - if (SimVar.GetSimVarValue("L:A320_FCU_SHOW_SELECTED_HEADING", "number") === 0) { - const currentHeading = Simplane.getHeadingMagnetic(); - - Coherent.call("HEADING_BUG_SET", 1, currentHeading).catch(console.error); - } - } - this._onModeSelectedHeading(); - } - if (_event === "MODE_MANAGED_HEADING") { - if (this.flightPlanService.active.legCount === 0) { - return; - } - - this._onModeManagedHeading(); - } - if (_event === "MODE_SELECTED_ALTITUDE") { - const dist = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; - this.flightPhaseManager.handleFcuAltKnobPushPull(dist); - this._onModeSelectedAltitude(); - this._onStepClimbDescent(); - } - if (_event === "MODE_MANAGED_ALTITUDE") { - const dist = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; - this.flightPhaseManager.handleFcuAltKnobPushPull(dist); - this._onModeManagedAltitude(); - this._onStepClimbDescent(); - } - if (_event === "AP_DEC_ALT" || _event === "AP_INC_ALT") { - const dist = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; - this.flightPhaseManager.handleFcuAltKnobTurn(dist); - this._onTrySetCruiseFlightLevel(); - } - if (_event === "AP_DEC_HEADING" || _event === "AP_INC_HEADING") { - if (SimVar.GetSimVarValue("L:A320_FCU_SHOW_SELECTED_HEADING", "number") === 0) { - const currentHeading = Simplane.getHeadingMagnetic(); - Coherent.call("HEADING_BUG_SET", 1, currentHeading).catch(console.error); - } - SimVar.SetSimVarValue("L:A320_FCU_SHOW_SELECTED_HEADING", "number", 1); - } - if (_event === "VS") { - const dist = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; - this.flightPhaseManager.handleFcuVSKnob(dist, this._onStepClimbDescent.bind(this)); - } - } - - _onModeSelectedHeading() { - if (SimVar.GetSimVarValue("AUTOPILOT APPROACH HOLD", "boolean")) { - return; - } - if (!SimVar.GetSimVarValue("AUTOPILOT HEADING LOCK", "Boolean")) { - SimVar.SetSimVarValue("K:AP_PANEL_HEADING_HOLD", "Number", 1); - } - SimVar.SetSimVarValue("K:HEADING_SLOT_INDEX_SET", "number", 1); - } - - _onModeManagedHeading() { - if (SimVar.GetSimVarValue("AUTOPILOT APPROACH HOLD", "boolean")) { - return; - } - if (!SimVar.GetSimVarValue("AUTOPILOT HEADING LOCK", "Boolean")) { - SimVar.SetSimVarValue("K:AP_PANEL_HEADING_HOLD", "Number", 1); - } - SimVar.SetSimVarValue("K:HEADING_SLOT_INDEX_SET", "number", 2); - SimVar.SetSimVarValue("L:A320_FCU_SHOW_SELECTED_HEADING", "number", 0); - } - - _onModeSelectedAltitude() { - if (!Simplane.getAutoPilotGlideslopeHold()) { - SimVar.SetSimVarValue("L:A320_NEO_FCU_FORCE_IDLE_VS", "Number", 1); - } - SimVar.SetSimVarValue("K:ALTITUDE_SLOT_INDEX_SET", "number", 1); - Coherent.call("AP_ALT_VAR_SET_ENGLISH", 1, Simplane.getAutoPilotDisplayedAltitudeLockValue(), this._forceNextAltitudeUpdate).catch(console.error); - } - - _onModeManagedAltitude() { - SimVar.SetSimVarValue("K:ALTITUDE_SLOT_INDEX_SET", "number", 2); - Coherent.call("AP_ALT_VAR_SET_ENGLISH", 1, Simplane.getAutoPilotDisplayedAltitudeLockValue(), this._forceNextAltitudeUpdate).catch(console.error); - Coherent.call("AP_ALT_VAR_SET_ENGLISH", 2, Simplane.getAutoPilotDisplayedAltitudeLockValue(), this._forceNextAltitudeUpdate).catch(console.error); - if (!Simplane.getAutoPilotGlideslopeHold()) { - this.requestCall(() => { - SimVar.SetSimVarValue("L:A320_NEO_FCU_FORCE_IDLE_VS", "Number", 1); - }); - } - } - - _onStepClimbDescent() { - if (!(this.flightPhaseManager.phase === FmgcFlightPhases.CLIMB || this.flightPhaseManager.phase === FmgcFlightPhases.CRUISE)) { - return; - } - - const _targetFl = Simplane.getAutoPilotDisplayedAltitudeLockValue() / 100; - - if ( - (this.flightPhaseManager.phase === FmgcFlightPhases.CLIMB && _targetFl > this.cruiseLevel) || - (this.flightPhaseManager.phase === FmgcFlightPhases.CRUISE && _targetFl !== this.cruiseLevel) - ) { - this.deleteOutdatedCruiseSteps(this.cruiseLevel, _targetFl); - this.addMessageToQueue(NXSystemMessages.newCrzAlt.getModifiedMessage(_targetFl * 100)); - - this.cruiseLevel = _targetFl; - } - } - - deleteOutdatedCruiseSteps(oldCruiseLevel, newCruiseLevel) { - const isClimbVsDescent = newCruiseLevel > oldCruiseLevel; - - const activePlan = this.flightPlanService.active; - - for (let i = activePlan.activeLegIndex; i < activePlan.legCount; i++) { - const element = activePlan.elementAt(i); - - if (!element || element.isDiscontinuity === true || !element.cruiseStep) { - continue; - } - - const stepLevel = Math.round(element.cruiseStep.toAltitude / 100); - - if (isClimbVsDescent && stepLevel >= oldCruiseLevel && stepLevel <= newCruiseLevel || - !isClimbVsDescent && stepLevel <= oldCruiseLevel && stepLevel >= newCruiseLevel - ) { - element.cruiseStep = undefined; // TODO call a method on FPS so that we sync this (fms-v2) - this.removeMessageFromQueue(NXSystemMessages.stepAhead.text); - } - } - } - - /*** - * Executed on every alt knob turn, checks whether or not the crz fl can be changed to the newly selected fcu altitude - * It creates a timeout to simulate real life delay which resets every time the fcu knob alt increases or decreases. - * @private - */ - _onTrySetCruiseFlightLevel() { - if (!(this.flightPhaseManager.phase === FmgcFlightPhases.CLIMB || this.flightPhaseManager.phase === FmgcFlightPhases.CRUISE)) { - return; - } - - const activeVerticalMode = SimVar.GetSimVarValue('L:A32NX_FMA_VERTICAL_MODE', 'enum'); - - if ((activeVerticalMode >= 11 && activeVerticalMode <= 15) || (activeVerticalMode >= 21 && activeVerticalMode <= 23)) { - const fcuFl = Simplane.getAutoPilotDisplayedAltitudeLockValue() / 100; - - if (this.flightPhaseManager.phase === FmgcFlightPhases.CLIMB && fcuFl > this.cruiseLevel || - this.flightPhaseManager.phase === FmgcFlightPhases.CRUISE && fcuFl !== this.cruiseLevel - ) { - if (this.cruiseFlightLevelTimeOut) { - clearTimeout(this.cruiseFlightLevelTimeOut); - this.cruiseFlightLevelTimeOut = undefined; - } - - this.cruiseFlightLevelTimeOut = setTimeout(() => { - if (fcuFl === Simplane.getAutoPilotDisplayedAltitudeLockValue() / 100 && - ( - this.flightPhaseManager.phase === FmgcFlightPhases.CLIMB && fcuFl > this.cruiseLevel || - this.flightPhaseManager.phase === FmgcFlightPhases.CRUISE && fcuFl !== this.cruiseLevel - ) - ) { - this.addMessageToQueue(NXSystemMessages.newCrzAlt.getModifiedMessage(fcuFl * 100)); - this.cruiseLevel = fcuFl; - - if (this.page.Current === this.page.ProgressPage) { - CDUProgressPage.ShowPage(this); - } - } - }, 3000); - } - } - } - - /* END OF FMS EVENTS */ - /* FMS CHECK ROUTINE */ - - checkDestData() { - this.addMessageToQueue(NXSystemMessages.enterDestData, () => { - return isFinite(this.perfApprQNH) && isFinite(this.perfApprTemp) && isFinite(this.perfApprWindHeading) && isFinite(this.perfApprWindSpeed); - }); - } - - checkGWParams() { - const fmGW = SimVar.GetSimVarValue("L:A32NX_FM_GROSS_WEIGHT", "Number"); - const eng1state = SimVar.GetSimVarValue("L:A32NX_ENGINE_STATE:1", "Number"); - const eng2state = SimVar.GetSimVarValue("L:A32NX_ENGINE_STATE:2", "Number"); - const gs = SimVar.GetSimVarValue("GPS GROUND SPEED", "knots"); - const actualGrossWeight = SimVar.GetSimVarValue("TOTAL WEIGHT", "Kilograms") / 1000; //TO-DO Source to be replaced with FAC-GW - const gwMismatch = (Math.abs(fmGW - actualGrossWeight) > 7) ? true : false; - - if (eng1state == 2 || eng2state == 2) { - if (this._gwInitDisplayed < 1 && this.flightPhaseManager.phase < FmgcFlightPhases.TAKEOFF) { - this._initMessageSettable = true; - } - } - //INITIALIZE WEIGHT/CG - if (this.isAnEngineOn() && fmGW === 0 && this._initMessageSettable) { - this.addMessageToQueue(NXSystemMessages.initializeWeightOrCg); - this._gwInitDisplayed++; - this._initMessageSettable = false; - } - - //CHECK WEIGHT - //TO-DO Ground Speed used for redundancy and to simulate delay (~10s) for FAC parameters to be calculated, remove once FAC is available. - if (!this.isOnGround() && gwMismatch && this._checkWeightSettable && gs > 180) { - this.addMessageToQueue(NXSystemMessages.checkWeight); - this._checkWeightSettable = false; - } else if (!gwMismatch) { - this.removeMessageFromQueue(NXSystemMessages.checkWeight.text); - this._checkWeightSettable = true; - } - } - - /* END OF FMS CHECK ROUTINE */ - /* MCDU GET/SET METHODS */ - - setCruiseFlightLevelAndTemperature(input) { - if (input === FMCMainDisplay.clrValue) { - this.cruiseLevel = null; - this.cruiseTemperature = undefined; - return true; - } - const flString = input.split("/")[0].replace("FL", ""); - const tempString = input.split("/")[1]; - const onlyTemp = flString.length === 0; - - if (!!flString && !onlyTemp && this.trySetCruiseFl(parseFloat(flString))) { - if (SimVar.GetSimVarValue("L:A32NX_CRZ_ALT_SET_INITIAL", "bool") === 1 && SimVar.GetSimVarValue("L:A32NX_GOAROUND_PASSED", "bool") === 1) { - SimVar.SetSimVarValue("L:A32NX_NEW_CRZ_ALT", "number", this.cruiseLevel); - } else { - SimVar.SetSimVarValue("L:A32NX_CRZ_ALT_SET_INITIAL", "bool", 1); - } - if (!tempString) { - return true; - } - } - if (!!tempString) { - const temp = parseInt(tempString.replace("M", "-")); - console.log("tS: " + tempString); - console.log("ti: " + temp); - if (isFinite(temp) && this.cruiseLevel) { - if (temp > -270 && temp < 100) { - this.cruiseTemperature = temp; - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } else { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - } - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - tryUpdateCostIndex(costIndex) { - const value = parseInt(costIndex); - if (isFinite(value)) { - if (value >= 0) { - if (value < 1000) { - this.costIndex = value; - this.updateManagedSpeeds(); - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } - } - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - /** - * Any tropopause altitude up to 60,000 ft is able to be entered - * @param {string} tropo Format: NNNN or NNNNN Leading 0’s must be included. Entry is rounded to the nearest 10 ft - * @return {boolean} Whether tropopause could be set or not - */ - tryUpdateTropo(tropo) { - if (tropo === FMCMainDisplay.clrValue) { - if (this.tropo) { - this.tropo = undefined; - return true; - } - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - if (!tropo.match(/^(?=(\D*\d){4,5}\D*$)/g)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const value = parseInt(tropo); - if (isFinite(value) && value >= 0 && value <= 60000) { - this.tropo = Math.round(value / 10) * 10; - return true; - } - - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - //----------------------------------------------------------------------------------- - // TODO:FPM REWRITE: Start of functions to refactor - //----------------------------------------------------------------------------------- - - resetCoroute() { - this.coRoute.routeNumber = undefined; - this.coRoute.routes = []; - } - - /** MCDU Init page method for FROM/TO, NOT for programmatic use */ - tryUpdateFromTo(fromTo, callback = EmptyCallback.Boolean) { - if (fromTo === FMCMainDisplay.clrValue) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return callback(false); - } - - const match = fromTo.match(/^([A-Z]{4})\/([A-Z]{4})$/); - if (match === null) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return callback(false); - } - const [, from, to] = match; - - this.resetCoroute(); - - this.setFromTo(from, to).then(() => { - this.getCoRouteList().then(() => callback(true)).catch(console.log); - }).catch((e) => { - if (e instanceof McduMessage) { - this.setScratchpadMessage(e); - } else { - console.warn(e); - } - callback(false); - }); - } - - /** - * Programmatic method to set from/to - * @param {string} from 4-letter icao code for origin airport - * @param {string} to 4-letter icao code for destination airport - * @throws NXSystemMessage on error (you are responsible for pushing to the scratchpad if appropriate) - */ - async setFromTo(from, to) { - let airportFrom, airportTo; - try { - airportFrom = await this.navigationDatabaseService.activeDatabase.searchAirport(from); - airportTo = await this.navigationDatabaseService.activeDatabase.searchAirport(to); - - if (!airportFrom || !airportTo) { - throw NXSystemMessages.notInDatabase; - } - } catch (e) { - console.log(e); - throw NXSystemMessages.notInDatabase; - } - - this.atsu.resetAtisAutoUpdate(); - - return this.flightPlanService.newCityPair(from, to).then(() => { - this.setGroundTempFromOrigin(); - }); - } - - /** - * Computes distance between destination and alternate destination - */ - tryUpdateDistanceToAlt() { - const activePlan = this.flightPlanService.active; - - if (activePlan && activePlan.destinationAirport && activePlan.alternateDestinationAirport) { - this._DistanceToAlt = Avionics.Utils.computeGreatCircleDistance( - activePlan.destinationAirport.location, - activePlan.alternateDestinationAirport.location, - ); - } else { - this._DistanceToAlt = 0; - } - } - - //----------------------------------------------------------------------------------- - // TODO:FPM REWRITE: End of functions to refactor - //----------------------------------------------------------------------------------- - - // only used by trySetRouteAlternateFuel - isAltFuelInRange(fuel) { - if (Number.isFinite(this.blockFuel)) { - return 0 < fuel && fuel < (this.blockFuel - this._routeTripFuelWeight); - } - - return 0 < fuel; - } - - async trySetRouteAlternateFuel(altFuel) { - if (altFuel === FMCMainDisplay.clrValue) { - this._routeAltFuelEntered = false; - return true; - } - if (!this.flightPlanService || !this.flightPlanService.active || !this.flightPlanService.active.alternateDestinationAirport) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - const value = NXUnits.userToKg(parseFloat(altFuel)); - if (isFinite(value)) { - if (this.isAltFuelInRange(value)) { - this._routeAltFuelEntered = true; - this._routeAltFuelWeight = value; - this._routeAltFuelTime = null; - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - async trySetMinDestFob(fuel) { - if (fuel === FMCMainDisplay.clrValue) { - this._minDestFobEntered = false; - return true; - } - if (!this.representsDecimalNumber(fuel)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const value = NXUnits.userToKg(parseFloat(fuel)); - if (isFinite(value)) { - if (this.isMinDestFobInRange(value)) { - this._minDestFobEntered = true; - if (value < this._routeAltFuelWeight + this.getRouteFinalFuelWeight()) { - this.addMessageToQueue(NXSystemMessages.checkMinDestFob); - } - this._minDestFob = value; - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - async tryUpdateAltDestination(altDestIdent) { - if (!altDestIdent || altDestIdent === "NONE" || altDestIdent === FMCMainDisplay.clrValue) { - this.atsu.resetAtisAutoUpdate(); - this.flightPlanService.setAlternate(undefined); - this._DistanceToAlt = 0; - return true; - } - - const airportAltDest = await this.navigationDatabaseService.activeDatabase.searchAirport(altDestIdent); - if (airportAltDest) { - this.atsu.resetAtisAutoUpdate(); - await this.flightPlanService.setAlternate(altDestIdent); - this.tryUpdateDistanceToAlt(); - return true; - } - - this.setScratchpadMessage(NXSystemMessages.notInDatabase); - return false; - } - - /** - * Updates the Fuel weight cell to tons. Uses a place holder FL120 for 30 min - */ - tryUpdateRouteFinalFuel() { - if (this._routeFinalFuelTime <= 0) { - this._routeFinalFuelTime = this._defaultRouteFinalTime; - } - this._routeFinalFuelWeight = A32NX_FuelPred.computeHoldingTrackFF(this.zeroFuelWeight, 120) / 1000; - this._rteFinalCoeffecient = A32NX_FuelPred.computeHoldingTrackFF(this.zeroFuelWeight, 120) / 30; - } - - /** - * Updates the alternate fuel and time values using a place holder FL of 330 until that can be set - */ - tryUpdateRouteAlternate() { - if (this._DistanceToAlt < 20) { - this._routeAltFuelWeight = 0; - this._routeAltFuelTime = 0; - } else { - const placeholderFl = 120; - const airDistance = A32NX_FuelPred.computeAirDistance(Math.round(this._DistanceToAlt), this.averageWind); - - const deviation = (this.zeroFuelWeight + this._routeFinalFuelWeight - A32NX_FuelPred.refWeight) * A32NX_FuelPred.computeNumbers(airDistance, placeholderFl, A32NX_FuelPred.computations.CORRECTIONS, true); - if ((20 < airDistance && airDistance < 200) && (100 < placeholderFl && placeholderFl < 290)) { //This will always be true until we can setup alternate routes - this._routeAltFuelWeight = (A32NX_FuelPred.computeNumbers(airDistance, placeholderFl, A32NX_FuelPred.computations.FUEL, true) + deviation) / 1000; - this._routeAltFuelTime = this._routeAltFuelEntered ? null : A32NX_FuelPred.computeNumbers(airDistance, placeholderFl, A32NX_FuelPred.computations.TIME, true); - } - } - } - - /** - * Attempts to calculate trip information. Is dynamic in that it will use liveDistanceTo the destination rather than a - * static distance. Works down to 20NM airDistance and FL100 Up to 3100NM airDistance and FL390, anything out of those ranges and values - * won't be updated. - */ - tryUpdateRouteTrip(dynamic = false) { - // TODO Use static distance for `dynamic = false` (fms-v2) - const groundDistance = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; - const airDistance = A32NX_FuelPred.computeAirDistance(groundDistance, this.averageWind); - - let altToUse = this.cruiseLevel; - // Use the cruise level for calculations otherwise after cruise use descent altitude down to 10,000 feet. - if (this.flightPhaseManager.phase >= FmgcFlightPhases.DESCENT) { - altToUse = SimVar.GetSimVarValue("PLANE ALTITUDE", 'Feet') / 100; - } - - if ((20 <= airDistance && airDistance <= 3100) && (100 <= altToUse && altToUse <= 390)) { - const deviation = (this.zeroFuelWeight + this._routeFinalFuelWeight + this._routeAltFuelWeight - A32NX_FuelPred.refWeight) * A32NX_FuelPred.computeNumbers(airDistance, altToUse, A32NX_FuelPred.computations.CORRECTIONS, false); - - this._routeTripFuelWeight = (A32NX_FuelPred.computeNumbers(airDistance, altToUse, A32NX_FuelPred.computations.FUEL, false) + deviation) / 1000; - this._routeTripTime = A32NX_FuelPred.computeNumbers(airDistance, altToUse, A32NX_FuelPred.computations.TIME, false); - } - } - - tryUpdateMinDestFob() { - this._minDestFob = this._routeAltFuelWeight + this.getRouteFinalFuelWeight(); - } - - tryUpdateTOW() { - this.takeOffWeight = this.zeroFuelWeight + this.blockFuel - this.taxiFuelWeight; - } - - tryUpdateLW() { - this.landingWeight = this.takeOffWeight - this._routeTripFuelWeight; - } - - /** - * Computes extra fuel - * @param {boolean}useFOB - States whether to use the FOB rather than block fuel when computing extra fuel - * @returns {number} - */ - tryGetExtraFuel(useFOB = false) { - if (useFOB) { - return this.getFOB() - this.getTotalTripFuelCons() - this._minDestFob - this.taxiFuelWeight - (this.getRouteReservedWeight()); - } else { - return this.blockFuel - this.getTotalTripFuelCons() - this._minDestFob - this.taxiFuelWeight - (this.getRouteReservedWeight()); - } - } - - /**getRouteReservedWeight - * EXPERIMENTAL - * Attempts to calculate the extra time - */ - tryGetExtraTime(useFOB = false) { - if (this.tryGetExtraFuel(useFOB) <= 0) { - return 0; - } - const tempWeight = this.getGW() - this._minDestFob; - const tempFFCoefficient = A32NX_FuelPred.computeHoldingTrackFF(tempWeight, 180) / 30; - return (this.tryGetExtraFuel(useFOB) * 1000) / tempFFCoefficient; - } - - getRouteAltFuelWeight() { - return this._routeAltFuelWeight; - } - - getRouteAltFuelTime() { - return this._routeAltFuelTime; - } - - //----------------------------------------------------------------------------------- - // TODO:FPM REWRITE: Start of functions to refactor - //----------------------------------------------------------------------------------- - - // FIXME remove A32NX_FM_LS_COURSE - async updateIlsCourse() { - let course = -1; - const mmr = this.navigation.getNavaidTuner().getMmrRadioTuningStatus(1); - if (mmr.course !== null) { - course = mmr.course; - } else if (mmr.frequency !== null && SimVar.GetSimVarValue('L:A32NX_RADIO_RECEIVER_LOC_IS_VALID', 'number') === 1) { - course = SimVar.GetSimVarValue('NAV LOCALIZER:3', 'degrees'); - } - - return SimVar.SetSimVarValue('L:A32NX_FM_LS_COURSE', 'number', course); - } - - async updateFlightNo(flightNo, callback = EmptyCallback.Boolean) { - if (flightNo.length > 7) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return callback(false); - } - - this.flightNumber = flightNo; - await SimVar.SetSimVarValue("ATC FLIGHT NUMBER", "string", flightNo, "FMC"); - - // FIXME move ATSU code to ATSU - const code = await this.atsu.connectToNetworks(flightNo); - if (code !== AtsuCommon.AtsuStatusCodes.Ok) { - this.addNewAtsuMessage(code); - } - - return callback(true); - } - - async updateCoRoute(coRouteNum, callback = EmptyCallback.Boolean) { - try { - if (coRouteNum.length > 2 && (coRouteNum !== FMCMainDisplay.clrValue)) { - if (coRouteNum.length < 10) { - if (coRouteNum === "NONE") { - this.resetCoroute(); - } else { - const {success, data} = await SimBridgeClient.CompanyRoute.getCoRoute(coRouteNum); - if (success) { - this.coRoute["originIcao"] = data.origin.icao_code; - this.coRoute["destinationIcao"] = data.destination.icao_code; - this.coRoute["route"] = data.general.route; - if (data.alternate) { - this.coRoute["alternateIcao"] = data.alternate.icao_code; - } - this.coRoute["navlog"] = data.navlog.fix; - - await Fmgc.CoRouteUplinkAdapter.uplinkFlightPlanFromCoRoute(this, this.flightPlanService, this.coRoute); - await this.flightPlanService.uplinkInsert(); - this.setGroundTempFromOrigin(); - - this.coRoute["routeNumber"] = coRouteNum; - } else { - this.setScratchpadMessage(NXSystemMessages.notInDatabase); - } - } - return callback(true); - } - } - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return callback(false); - } catch (error) { - console.error(`Error retrieving coroute from SimBridge ${error}`); - this.setScratchpadMessage(NXFictionalMessages.unknownDownlinkErr); - return callback(false); - } - } - - async getCoRouteList() { - try { - const origin = this.flightPlanService.active.originAirport.ident; - const dest = this.flightPlanService.active.destinationAirport.ident; - const { success, data } = await SimBridgeClient.CompanyRoute.getRouteList(origin, dest); - - if (success) { - data.forEach((route => { - this.coRoute.routes.push({ - originIcao: route.origin.icao_code, - destinationIcao: route.destination.icao_code, - alternateIcao: route.alternate ? route.alternate.icao_code : undefined, - route: route.general.route, - navlog: route.navlog.fix, - routeName: route.name - }); - })); - } else { - this.setScratchpadMessage(NXSystemMessages.notInDatabase); - } - } catch (error) { - console.info(`Error retrieving coroute list ${error}`); - } - } - - getTotalTripTime() { - return this._routeTripTime; - } - - getTotalTripFuelCons() { - return this._routeTripFuelWeight; - } - - onUplinkInProgress() { - this.setScratchpadMessage(NXSystemMessages.uplinkInsertInProg); - } - - onUplinkDone() { - this.removeMessageFromQueue(NXSystemMessages.uplinkInsertInProg.text); - this.setScratchpadMessage(NXSystemMessages.aocActFplnUplink); - } - - /** - @param items {Array.} - */ - deduplicateFacilities(items) { - if (items.length === 0) { - return undefined; - } - if (items.length === 1) { - return items[0]; - } - - return new Promise((resolve) => { - A320_Neo_CDU_SelectWptPage.ShowPage(this, items, resolve); - }); - } - - /** - * Shows a scratchpad message based on the FMS error thrown - * @param type - */ - showFmsErrorMessage(type) { - switch (type) { - case 0: // NotInDatabase - this.setScratchpadMessage(NXSystemMessages.notInDatabase); - break; - case 1: // NotYetImplemented - this.setScratchpadMessage(NXFictionalMessages.notYetImplemented); - break; - case 2: // FormatError - this.setScratchpadMessage(NXSystemMessages.formatError); - break; - case 3: // EntryOutOfRange - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - break; - case 4: // ListOf99InUse - this.setScratchpadMessage(NXSystemMessages.listOf99InUse); - break; - case 5: // AwyWptMismatch - this.setScratchpadMessage(NXSystemMessages.awyWptMismatch); - break; - } - } - - createNewWaypoint(ident) { - return new Promise((resolve, reject) => { - CDUNewWaypoint.ShowPage(this, (waypoint) => { - if (waypoint) { - resolve(waypoint); - } else { - reject(); - } - }, { ident }); - }); - - } - - createLatLonWaypoint(coordinates, stored, ident = undefined) { - return this.dataManager.createLatLonWaypoint(coordinates, stored, ident); - } - - createPlaceBearingPlaceBearingWaypoint(place1, bearing1, place2, bearing2, stored, ident = undefined) { - return this.dataManager.createPlaceBearingPlaceBearingWaypoint(place1, bearing1, place2, bearing2, stored, ident); - } - - createPlaceBearingDistWaypoint(place, bearing, distance, stored, ident = undefined) { - return this.dataManager.createPlaceBearingDistWaypoint(place, bearing, distance, stored, ident); - } - - getStoredWaypointsByIdent(ident) { - return this.dataManager.getStoredWaypointsByIdent(ident); - } - - //----------------------------------------------------------------------------------- - // TODO:FPM REWRITE: Start of functions to refactor - //----------------------------------------------------------------------------------- - - _getOrSelectWaypoints(getter, ident, callback) { - getter(ident).then((waypoints) => { - if (waypoints.length === 0) { - return callback(undefined); - } - if (waypoints.length === 1) { - return callback(waypoints[0]); - } - A320_Neo_CDU_SelectWptPage.ShowPage(this, waypoints, callback); - }); - } - - getOrSelectILSsByIdent(ident, callback) { - this._getOrSelectWaypoints(this.navigationDatabase.searchIls.bind(this.navigationDatabase), ident, callback); - } - - getOrSelectVORsByIdent(ident, callback) { - this._getOrSelectWaypoints(this.navigationDatabase.searchVor.bind(this.navigationDatabase), ident, callback); - } - - getOrSelectNDBsByIdent(ident, callback) { - this._getOrSelectWaypoints(this.navigationDatabase.searchNdb.bind(this.navigationDatabase), ident, callback); - } - - getOrSelectNavaidsByIdent(ident, callback) { - this._getOrSelectWaypoints(this.navigationDatabase.searchAllNavaid.bind(this.navigationDatabase), ident, callback); - } - - /** - * This function only finds waypoints, not navaids. Some fixes may exist as a VOR and a waypoint in the database, this will only return the waypoint. - * Use @see Fmgc.WaypointEntryUtils.getOrCreateWaypoint instead if you don't want that - * @param {*} ident - * @param {*} callback - */ - getOrSelectWaypointByIdent(ident, callback) { - this._getOrSelectWaypoints(this.navigationDatabase.searchWaypoint.bind(this.navigationDatabase), ident, callback); - } - - insertWaypoint(newWaypointTo, fpIndex, forAlternate, index, before = false, callback = EmptyCallback.Boolean, bypassTmpy) { - if (newWaypointTo === "" || newWaypointTo === FMCMainDisplay.clrValue) { - return callback(false); - } - try { - Fmgc.WaypointEntryUtils.getOrCreateWaypoint(this, newWaypointTo, true).then( - /** - * @param {Waypoint} waypoint - */ - (waypoint) => { - if (!waypoint) { - return callback(false); - } - if (bypassTmpy) { - if (fpIndex === Fmgc.FlightPlanIndex.Active && this.flightPlanService.hasTemporary) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return callback(false); - } - - if (before) { - this.flightPlanService.insertWaypointBefore(index, waypoint, fpIndex, forAlternate).then(() => callback(true)); - } else { - this.flightPlanService.nextWaypoint(index, waypoint, fpIndex, forAlternate).then(() => callback(true)); - } - } else { - if (before) { - this.flightPlanService.insertWaypointBefore(index, waypoint, fpIndex, forAlternate).then(() => callback(true)); - } else { - this.flightPlanService.nextWaypoint(index, waypoint, fpIndex, forAlternate).then(() => callback(true)); - } - } - }).catch((err) => { - if (err.type !== undefined) { - this.showFmsErrorMessage(err.type); - } else if (err instanceof McduMessage) { - this.setScratchpadMessage(err); - } else if (err) { - console.error(err); - } - return callback(false); - } - ); - } catch (err) { - if (err.type !== undefined) { - this.showFmsErrorMessage(err.type); - } else if (err instanceof McduMessage) { - this.setScratchpadMessage(err); - } else { - console.error(err); - } - return callback(false); - } - } - - toggleWaypointOverfly(index, fpIndex, forAlternate, callback = EmptyCallback.Void) { - if (this.flightPlanService.hasTemporary) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return callback(false); - } - - this.flightPlanService.toggleOverfly(index, fpIndex, forAlternate); - callback(); - } - - eraseTemporaryFlightPlan(callback = EmptyCallback.Void) { - if (this.flightPlanService.hasTemporary) { - this.flightPlanService.temporaryDelete(); - - SimVar.SetSimVarValue("L:FMC_FLIGHT_PLAN_IS_TEMPORARY", "number", 0); - SimVar.SetSimVarValue("L:MAP_SHOW_TEMPORARY_FLIGHT_PLAN", "number", 0); - callback(); - } else { - callback(); - } - } - - insertTemporaryFlightPlan(callback = EmptyCallback.Void) { - if (this.flightPlanService.hasTemporary) { - const oldCostIndex = this.costIndex; - const oldDestination = this.currFlightPlanService.active.destinationAirport - ? this.currFlightPlanService.active.destinationAirport.ident - : undefined; - const oldCruiseLevel = this.cruiseLevel; - this.flightPlanService.temporaryInsert(); - this.checkCostIndex(oldCostIndex); - // FIXME I don't know if it is actually possible to insert TMPY with no FROM/TO, but we should not crash here, so check this for now - if (oldDestination !== undefined) { - this.checkDestination(oldDestination); - } - this.checkCruiseLevel(oldCruiseLevel); - - SimVar.SetSimVarValue("L:FMC_FLIGHT_PLAN_IS_TEMPORARY", "number", 0); - SimVar.SetSimVarValue("L:MAP_SHOW_TEMPORARY_FLIGHT_PLAN", "number", 0); - - this.guidanceController.vnavDriver.invalidateFlightPlanProfile(); - callback(); - } - } - - checkCostIndex(oldCostIndex) { - if (this.costIndex !== oldCostIndex) { - this.setScratchpadMessage(NXSystemMessages.usingCostIndex.getModifiedMessage(this.costIndex.toFixed(0))); - } - } - - checkDestination(oldDestination) { - const newDestination = this.currFlightPlanService.active.destinationAirport.ident; - - // Enabling alternate or new DEST should sequence out of the GO AROUND phase - if (newDestination !== oldDestination) { - this.flightPhaseManager.handleNewDestinationAirportEntered(); - } - } - - checkCruiseLevel(oldCruiseLevel) { - const newLevel = this.cruiseLevel; - // Keep simvar in sync for the flight phase manager - if (newLevel !== oldCruiseLevel) { - SimVar.SetSimVarValue('L:A32NX_AIRLINER_CRUISE_ALTITUDE', 'number', Number.isFinite(newLevel * 100) ? newLevel * 100 : 0); - } - } - - //----------------------------------------------------------------------------------- - // TODO:FPM REWRITE: End of functions to refactor - //----------------------------------------------------------------------------------- - - /* - * validates the waypoint type - * return values: - * 0 = lat-lon coordinate - * 1 = time - * 2 = place definition - * -1 = unknown - */ - async waypointType(mcdu, waypoint) { - if (Fmgc.WaypointEntryUtils.isLatLonFormat(waypoint)) { - return [0, null]; - } - - // time formatted - if (/([0-2][0-4][0-5][0-9]Z?)/.test(waypoint) && waypoint.length <= 5) { - return [1, null]; - } - - // place formatted - if (/^[A-Z0-9]{2,7}/.test(waypoint)) { - return mcdu.dataManager.GetWaypointsByIdent.bind(mcdu.dataManager)(waypoint).then((waypoints) => { - if (waypoints.length !== 0) { - return [2, null]; - } else { - return [-1, NXSystemMessages.notInDatabase]; - } - }); - } - - return [-1, NXSystemMessages.formatError]; - } - - vSpeedsValid() { - return (!!this.v1Speed && !!this.vRSpeed ? this.v1Speed <= this.vRSpeed : true) - && (!!this.vRSpeed && !!this.v2Speed ? this.vRSpeed <= this.v2Speed : true) - && (!!this.v1Speed && !!this.v2Speed ? this.v1Speed <= this.v2Speed : true); - } - - /** - * Gets the departure runway elevation in feet, if available. - * @returns departure runway elevation in feet, or null if not available. - */ - getDepartureElevation() { - const activePlan = this.flightPlanService.active; - - let departureElevation = null; - if (activePlan.originRunway) { - departureElevation = activePlan.originRunway.thresholdLocation.alt; - } else if (activePlan.originAirport) { - departureElevation = activePlan.originAirport.location.alt; - } - - return departureElevation; - } - - /** - * Gets the gross weight, if available. - * Prior to engine start this is based on ZFW + Fuel entries, - * after engine start ZFW entry + FQI FoB. - * @returns {number | null} gross weight in tons or null if not available. - */ - getGrossWeight() { - const fob = this.getFOB(); - - if (this.zeroFuelWeight === undefined || fob === undefined) { - return null; - } - - return this.zeroFuelWeight + fob; - } - - getToSpeedsTooLow() { - const grossWeight = this.getGrossWeight(); - - if (this.flaps === null || grossWeight === null) { - return false; - } - - const departureElevation = this.getDepartureElevation(); - - const zp = departureElevation !== null ? this.getPressureAltAtElevation(departureElevation, this.getBaroCorrection1()) : this.getPressureAlt(); - if (zp === null) { - return false; - } - - const tow = grossWeight - (this.isAnEngineOn() || this.taxiFuelWeight === undefined ? 0 : this.taxiFuelWeight); - - return ((this.v1Speed == null) ? Infinity : this.v1Speed) < Math.trunc(NXSpeedsUtils.getVmcg(zp)) - || ((this.vRSpeed == null) ? Infinity : this.vRSpeed) < Math.trunc(1.05 * NXSpeedsUtils.getVmca(zp)) - || ((this.v2Speed == null) ? Infinity : this.v2Speed) < Math.trunc(1.1 * NXSpeedsUtils.getVmca(zp)) - || (isFinite(tow) && ((this.v2Speed == null) ? Infinity : this.v2Speed) < Math.trunc(1.13 * NXSpeedsUtils.getVs1g(tow, this.flaps, true))); - } - - toSpeedsChecks() { - const toSpeedsNotInserted = !this.v1Speed || !this.vRSpeed || !this.v2Speed; - if (toSpeedsNotInserted !== this.toSpeedsNotInserted) { - this.toSpeedsNotInserted = toSpeedsNotInserted; - } - - const toSpeedsTooLow = this.getToSpeedsTooLow(); - if (toSpeedsTooLow !== this.toSpeedsTooLow) { - this.toSpeedsTooLow = toSpeedsTooLow; - if (toSpeedsTooLow) { - this.addMessageToQueue(NXSystemMessages.toSpeedTooLow, () => !this.getToSpeedsTooLow()); - } - } - - const vSpeedDisagree = !this.vSpeedsValid(); - if (vSpeedDisagree !== this.vSpeedDisagree) { - this.vSpeedDisagree = vSpeedDisagree; - if (vSpeedDisagree) { - this.addMessageToQueue(NXSystemMessages.vToDisagree, this.vSpeedsValid.bind(this)); - } - } - - this.arincDiscreteWord3.setBitValue(16, vSpeedDisagree); - this.arincDiscreteWord3.setBitValue(17, toSpeedsTooLow); - this.arincDiscreteWord3.setBitValue(18, toSpeedsNotInserted); - this.arincDiscreteWord3.ssm = Arinc429Word.SignStatusMatrix.NormalOperation; - } - - get v1Speed() { - return this.flightPlanService.active.performanceData.v1; - } - - set v1Speed(speed) { - this.flightPlanService.setPerformanceData('v1', speed); - SimVar.SetSimVarValue('L:AIRLINER_V1_SPEED', 'knots', speed ? speed : NaN); - } - - get vRSpeed() { - return this.flightPlanService.active.performanceData.vr; - } - - set vRSpeed(speed) { - this.flightPlanService.setPerformanceData('vr', speed); - SimVar.SetSimVarValue('L:AIRLINER_VR_SPEED', 'knots', speed ? speed : NaN); - } - - get v2Speed() { - return this.flightPlanService.active.performanceData.v2; - } - - set v2Speed(speed) { - this.flightPlanService.setPerformanceData('v2', speed); - SimVar.SetSimVarValue('L:AIRLINER_V2_SPEED', 'knots', speed ? speed : NaN); - } - - //Needs PR Merge #3082 - trySetV1Speed(s) { - if (s === FMCMainDisplay.clrValue) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - const v = parseInt(s); - if (!isFinite(v) || !/^\d{2,3}$/.test(s)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - if (v < 90 || v > 350) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - this.removeMessageFromQueue(NXSystemMessages.checkToData.text); - this.unconfirmedV1Speed = undefined; - this.v1Speed = v; - return true; - } - - //Needs PR Merge #3082 - trySetVRSpeed(s) { - if (s === FMCMainDisplay.clrValue) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - const v = parseInt(s); - if (!isFinite(v) || !/^\d{2,3}$/.test(s)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - if (v < 90 || v > 350) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - this.removeMessageFromQueue(NXSystemMessages.checkToData.text); - this.unconfirmedVRSpeed = undefined; - this.vRSpeed = v; - return true; - } - - //Needs PR Merge #3082 - trySetV2Speed(s) { - if (s === FMCMainDisplay.clrValue) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - const v = parseInt(s); - if (!isFinite(v) || !/^\d{2,3}$/.test(s)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - if (v < 90 || v > 350) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - this.removeMessageFromQueue(NXSystemMessages.checkToData.text); - this.unconfirmedV2Speed = undefined; - this.v2Speed = v; - return true; - } - - trySetTakeOffTransAltitude(s) { - if (s === FMCMainDisplay.clrValue) { - this.flightPlanService.setPerformanceData('pilotTransitionAltitude', null); - this.updateTransitionAltitudeLevel(); - return true; - } - - let value = parseInt(s); - if (!isFinite(value) || !/^\d{4,5}$/.test(s)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - value = Math.round(value / 10) * 10; - if (value < 1000 || value > 45000) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.flightPlanService.setPerformanceData('pilotTransitionAltitude', value); - this.updateTransitionAltitudeLevel(); - return true; - } - - /** - * Rounds a number to the nearest multiple - * @param {number | undefined | null} n the number to round - * @param {number} r the multiple - * @returns {number | undefined | null} n rounded to the nereast multiple of r, or null/undefined if n is null/undefined - */ - static round(n, r = 1) { - if (n === undefined || n === null) { - return n; - } - return Math.round(n / r) * r; - } - - async trySetThrustReductionAccelerationAltitude(s) { - const plan = this.flightPlanService.active; - - if (this.flightPhaseManager.phase >= FmgcFlightPhases.TAKEOFF || !plan.originAirport) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - if (s === FMCMainDisplay.clrValue) { - const hasDefaultThrRed = plan.performanceData.defaultThrustReductionAltitude !== null; - const hasDefaultAcc = plan.performanceData.defaultAccelerationAltitude !== null; - - if (hasDefaultThrRed && hasDefaultAcc) { - plan.setPerformanceData('pilotThrustReductionAltitude', null); - plan.setPerformanceData('pilotAccelerationAltitude', null); - return true; - } - - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - const match = s.match(/^(([0-9]{4,5})\/?)?(\/([0-9]{4,5}))?$/); - if (match === null || (match[2] === undefined && match[4] === undefined) || s.split('/').length > 2) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const thrRed = match[2] !== undefined ? FMCMainDisplay.round(parseInt(match[2]), 10) : null; - const accAlt = match[4] !== undefined ? FMCMainDisplay.round(parseInt(match[4]), 10) : null; - - const origin = this.flightPlanService.active.originAirport; - - let elevation = 0; - if (origin) { - elevation = origin.location.alt; - } - - const minimumAltitude = elevation + 400; - - const newThrRed = thrRed !== null ? thrRed : plan.performanceData.thrustReductionAltitude; - const newAccAlt = accAlt !== null ? accAlt : plan.performanceData.accelerationAltitude; - - if ( - (thrRed !== null && (thrRed < minimumAltitude || thrRed > 45000)) - || (accAlt !== null && (accAlt < minimumAltitude || accAlt > 45000)) - || (newThrRed !== null && newAccAlt !== null && thrRed > accAlt) - ) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - if (thrRed !== null) { - plan.setPerformanceData('pilotThrustReductionAltitude', thrRed); - } - - if (accAlt !== null) { - plan.setPerformanceData('pilotAccelerationAltitude', accAlt); - } - - return true; - } - - async trySetEngineOutAcceleration(s) { - const plan = this.flightPlanService.active; - - if (this.flightPhaseManager.phase >= FmgcFlightPhases.TAKEOFF || !plan.originAirport) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - if (s === FMCMainDisplay.clrValue) { - const hasDefaultEngineOutAcc = plan.performanceData.defaultEngineOutAccelerationAltitude !== null; - - if (hasDefaultEngineOutAcc) { - plan.setPerformanceData('pilotEngineOutAccelerationAltitude', null); - return true; - } - - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - const match = s.match(/^([0-9]{4,5})$/); - if (match === null) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const accAlt = parseInt(match[1]); - - const origin = plan.originAirport; - const elevation = origin.location.alt !== undefined ? origin.location.alt : 0; - const minimumAltitude = elevation + 400; - - if (accAlt < minimumAltitude || accAlt > 45000) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - plan.setPerformanceData('pilotEngineOutAccelerationAltitude', accAlt); - - return true; - } - - async trySetThrustReductionAccelerationAltitudeGoaround(s) { - const plan = this.flightPlanService.active; - - if (this.flightPhaseManager.phase >= FmgcFlightPhases.GOAROUND || !plan.destinationAirport) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - if (s === FMCMainDisplay.clrValue) { - const hasDefaultMissedThrRed = plan.performanceData.defaultMissedThrustReductionAltitude !== null; - const hasDefaultMissedAcc = plan.performanceData.defaultMissedAccelerationAltitude !== null; - - if (hasDefaultMissedThrRed && hasDefaultMissedAcc) { - plan.setPerformanceData('pilotMissedThrustReductionAltitude', null); - plan.setPerformanceData('pilotMissedAccelerationAltitude', null); - return true; - } - - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - const match = s.match(/^(([0-9]{4,5})\/?)?(\/([0-9]{4,5}))?$/); - if (match === null || (match[2] === undefined && match[4] === undefined) || s.split('/').length > 2) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const thrRed = match[2] !== undefined ? FMCMainDisplay.round(parseInt(match[2]), 10) : null; - const accAlt = match[4] !== undefined ? FMCMainDisplay.round(parseInt(match[4]), 10) : null; - - const destination = plan.destinationAirport; - const elevation = destination.location.alt !== undefined ? destination.location.alt : 0; - const minimumAltitude = elevation + 400; - - const newThrRed = thrRed !== null ? thrRed : plan.performanceData.missedThrustReductionAltitude; - const newAccAlt = accAlt !== null ? accAlt : plan.performanceData.missedAccelerationAltitude; - - if ( - (thrRed !== null && (thrRed < minimumAltitude || thrRed > 45000)) - || (accAlt !== null && (accAlt < minimumAltitude || accAlt > 45000)) - || (newThrRed !== null && newAccAlt !== null && thrRed > accAlt) - ) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - if (thrRed !== null) { - plan.setPerformanceData('pilotMissedThrustReductionAltitude', thrRed); - } - - if (accAlt !== null) { - plan.setPerformanceData('pilotMissedAccelerationAltitude', accAlt); - } - - return true; - } - - async trySetEngineOutAccelerationAltitudeGoaround(s) { - const plan = this.flightPlanService.active; - - if (this.flightPhaseManager.phase >= FmgcFlightPhases.GOAROUND || !plan.destinationAirport) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - if (s === FMCMainDisplay.clrValue) { - const hasDefaultMissedEOAcc = plan.performanceData.defaultMissedEngineOutAccelerationAltitude !== null; - - if (hasDefaultMissedEOAcc) { - plan.setPerformanceData('pilotMissedEngineOutAccelerationAltitude', null); - return true; - } - - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - const match = s.match(/^([0-9]{4,5})$/); - if (match === null) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const accAlt = parseInt(match[1]); - - const destination = plan.destinationAirport; - const elevation = destination.location.alt !== undefined ? destination.location.alt : 0; - const minimumAltitude = elevation + 400; - - if (accAlt < minimumAltitude || accAlt > 45000) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - plan.setPerformanceData('pilotMissedEngineOutAccelerationAltitude', accAlt); - - return true; - } - - thrustReductionAccelerationChecks() { - const activePlan = this.flightPlanService.active; - - if (activePlan.reconcileAccelerationWithConstraints()) { - this.addMessageToQueue(NXSystemMessages.newAccAlt.getModifiedMessage(activePlan.performanceData.accelerationAltitude.toFixed(0))); - } - - if (activePlan.reconcileThrustReductionWithConstraints()) { - this.addMessageToQueue(NXSystemMessages.newThrRedAlt.getModifiedMessage(activePlan.performanceData.thrustReductionAltitude.toFixed(0))); - } - } - - updateThrustReductionAcceleration() { - const activePerformanceData = this.flightPlanService.active.performanceData; - - this.arincThrustReductionAltitude.setBnrValue( - activePerformanceData.thrustReductionAltitude !== null ? activePerformanceData.thrustReductionAltitude : 0, - activePerformanceData.thrustReductionAltitude !== null ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData, - 17, 131072, 0, - ); - this.arincAccelerationAltitude.setBnrValue( - activePerformanceData.accelerationAltitude !== null ? activePerformanceData.accelerationAltitude : 0, - activePerformanceData.accelerationAltitude !== null ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData, - 17, 131072, 0, - ); - this.arincEoAccelerationAltitude.setBnrValue( - activePerformanceData.engineOutAccelerationAltitude !== null ? activePerformanceData.engineOutAccelerationAltitude : 0, - activePerformanceData.engineOutAccelerationAltitude !== null ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData, - 17, 131072, 0, - ); - - this.arincMissedThrustReductionAltitude.setBnrValue( - activePerformanceData.missedThrustReductionAltitude !== null ? activePerformanceData.missedThrustReductionAltitude : 0, - activePerformanceData.missedThrustReductionAltitude !== null ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData, - 17, 131072, 0, - ); - this.arincMissedAccelerationAltitude.setBnrValue( - activePerformanceData.missedAccelerationAltitude !== null ? activePerformanceData.missedAccelerationAltitude : 0, - activePerformanceData.missedAccelerationAltitude !== null ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData, - 17, 131072, 0, - ); - this.arincMissedEoAccelerationAltitude.setBnrValue( - activePerformanceData.missedEngineOutAccelerationAltitude !== null ? activePerformanceData.missedEngineOutAccelerationAltitude : 0, - activePerformanceData.missedEngineOutAccelerationAltitude !== null ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData, - 17, 131072, 0, - ); - } - - updateTransitionAltitudeLevel() { - const originTransitionAltitude = this.getOriginTransitionAltitude(); - this.arincTransitionAltitude.setBnrValue( - originTransitionAltitude !== null ? originTransitionAltitude : 0, - originTransitionAltitude !== null ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData, - 17, 131072, 0, - ); - - const destinationTansitionLevel = this.getDestinationTransitionLevel(); - this.arincTransitionLevel.setBnrValue( - destinationTansitionLevel !== null ? destinationTansitionLevel : 0, - destinationTansitionLevel !== null ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData, - 9, 512, 0, - ); - } - - //Needs PR Merge #3082 - //TODO: with FADEC no longer needed - setPerfTOFlexTemp(s) { - if (s === FMCMainDisplay.clrValue) { - this.perfTOTemp = NaN; - // In future we probably want a better way of checking this, as 0 is - // in the valid flex temperature range (-99 to 99). - SimVar.SetSimVarValue("L:A32NX_AIRLINER_TO_FLEX_TEMP", "Number", 0); - return true; - } - let value = parseInt(s); - if (!isFinite(value) || !/^[+\-]?\d{1,2}$/.test(s)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - if (value < -99 || value > 99) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - // As the sim uses 0 as a sentinel value to detect that no flex - // temperature is set, we'll just use 0.1 as the actual value for flex 0 - // and make sure we never display it with decimals. - if (value === 0) { - value = 0.1; - } - this.perfTOTemp = value; - SimVar.SetSimVarValue("L:A32NX_AIRLINER_TO_FLEX_TEMP", "Number", value); - return true; - } - - /** - * Attempts to predict required block fuel for trip - * @returns {boolean} - */ - //TODO: maybe make this part of an update routine? - tryFuelPlanning() { - if (this._fuelPlanningPhase === this._fuelPlanningPhases.IN_PROGRESS) { - this._blockFuelEntered = true; - this._fuelPlanningPhase = this._fuelPlanningPhases.COMPLETED; - return true; - } - const tempRouteFinalFuelTime = this._routeFinalFuelTime; - this.tryUpdateRouteFinalFuel(); - this.tryUpdateRouteAlternate(); - this.tryUpdateRouteTrip(); - - this._routeFinalFuelTime = tempRouteFinalFuelTime; - this._routeFinalFuelWeight = (this._routeFinalFuelTime * this._rteFinalCoeffecient) / 1000; - - this.tryUpdateMinDestFob(); - - this.blockFuel = this.getTotalTripFuelCons() + this._minDestFob + this.taxiFuelWeight + this.getRouteReservedWeight(); - this._fuelPlanningPhase = this._fuelPlanningPhases.IN_PROGRESS; - return true; - } - - trySetTaxiFuelWeight(s) { - if (s === FMCMainDisplay.clrValue) { - this.taxiFuelWeight = this._defaultTaxiFuelWeight; - this._taxiEntered = false; - return true; - } - if (!this.representsDecimalNumber(s)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - const value = NXUnits.userToKg(parseFloat(s)); - if (isFinite(value)) { - if (this.isTaxiFuelInRange(value)) { - this._taxiEntered = true; - this.taxiFuelWeight = value; - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - getRouteFinalFuelWeight() { - if (isFinite(this._routeFinalFuelWeight)) { - this._routeFinalFuelWeight = (this._routeFinalFuelTime * this._rteFinalCoeffecient) / 1000; - return this._routeFinalFuelWeight; - } - } - - getRouteFinalFuelTime() { - return this._routeFinalFuelTime; - } - - /** - * This method is used to set initial Final Time for when INIT B is making predictions - * @param {String} s - containing time value - * @returns {boolean} - */ - async trySetRouteFinalTime(s) { - if (s) { - if (s === FMCMainDisplay.clrValue) { - this._routeFinalFuelTime = this._routeFinalFuelTimeDefault; - this._rteFinalWeightEntered = false; - this._rteFinalTimeEntered = false; - return true; - } - // Time entry must start with '/' - if (s.startsWith("/")) { - const rteFinalTime = s.slice(1); - - if (!/^\d{1,4}$/.test(rteFinalTime)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - if (this.isFinalTimeInRange(rteFinalTime)) { - this._rteFinalWeightEntered = false; - this._rteFinalTimeEntered = true; - this._routeFinalFuelTime = FMCMainDisplay.hhmmToMinutes(rteFinalTime.padStart(4,"0")); - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } - } - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - /** - * - * @param {string} s - * @returns {Promise} - */ - async trySetRouteFinalFuel(s) { - if (s === FMCMainDisplay.clrValue) { - this._routeFinalFuelTime = this._routeFinalFuelTimeDefault; - this._rteFinalWeightEntered = false; - this._rteFinalTimeEntered = false; - return true; - } - if (s) { - // Time entry must start with '/' - if (s.startsWith("/")) { - return this.trySetRouteFinalTime(s); - } else { - // If not time, try to parse as weight - // Weight can be entered with optional trailing slash, if so remove it before parsing the value - const enteredValue = s.endsWith("/") ? s.slice(0, -1) : s; - - if (!this.representsDecimalNumber(enteredValue)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const rteFinalWeight = NXUnits.userToKg(parseFloat(enteredValue)); - - if (this.isFinalFuelInRange(rteFinalWeight)) { - this._rteFinalWeightEntered = true; - this._rteFinalTimeEntered = false; - this._routeFinalFuelWeight = rteFinalWeight; - this._routeFinalFuelTime = (rteFinalWeight * 1000) / this._rteFinalCoeffecient; - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } - } - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - getRouteReservedWeight() { - if (this.isFlying()) { - return 0; - } - if (!this.routeReservedEntered() && (this._rteFinalCoeffecient !== 0)) { - const fivePercentWeight = this._routeReservedPercent * this._routeTripFuelWeight / 100; - const fiveMinuteHoldingWeight = (5 * this._rteFinalCoeffecient) / 1000; - - return fivePercentWeight > fiveMinuteHoldingWeight ? fivePercentWeight : fiveMinuteHoldingWeight; - } - if (isFinite(this._routeReservedWeight) && this._routeReservedWeight !== 0) { - return this._routeReservedWeight; - } else { - return this._routeReservedPercent * this._routeTripFuelWeight / 100; - } - } - - getRouteReservedPercent() { - if (this.isFlying()) { - return 0; - } - if (isFinite(this._routeReservedWeight) && isFinite(this.blockFuel) && this._routeReservedWeight !== 0) { - return this._routeReservedWeight / this._routeTripFuelWeight * 100; - } - return this._routeReservedPercent; - } - - trySetRouteReservedPercent(s) { - if (!this.isFlying()) { - if (s) { - if (s === FMCMainDisplay.clrValue) { - this._rteReservedWeightEntered = false; - this._rteReservedPctEntered = false; - this._routeReservedWeight = 0; - this._routeReservedPercent = 5; - this._rteRsvPercentOOR = false; - return true; - } - // Percentage entry must start with '/' - if (s.startsWith("/")) { - const enteredValue = s.slice(1); - - if (!this.representsDecimalNumber(enteredValue)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const rteRsvPercent = parseFloat(enteredValue); - - if (!this.isRteRsvPercentInRange(rteRsvPercent)) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this._rteRsvPercentOOR = false; - this._rteReservedPctEntered = true; - this._rteReservedWeightEntered = false; - - if (isFinite(rteRsvPercent)) { - this._routeReservedWeight = NaN; - this._routeReservedPercent = rteRsvPercent; - return true; - } - } - } - } - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - /** - * Checks input and passes to trySetCruiseFl() - * @param input - * @returns {boolean} input passed checks - */ - trySetCruiseFlCheckInput(input) { - if (input === FMCMainDisplay.clrValue) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - const flString = input.replace("FL", ""); - if (!flString) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - return this.trySetCruiseFl(parseFloat(flString)); - } - - /** - * Sets new Cruise FL if all conditions good - * @param fl {number} Altitude or FL - * @returns {boolean} input passed checks - */ - trySetCruiseFl(fl) { - if (!isFinite(fl)) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - if (fl >= 1000) { - fl = Math.floor(fl / 100); - } - if (fl > this.maxCruiseFL) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - const phase = this.flightPhaseManager.phase; - const selFl = Math.floor(Math.max(0, Simplane.getAutoPilotDisplayedAltitudeLockValue("feet")) / 100); - if (fl < selFl && (phase === FmgcFlightPhases.CLIMB || phase === FmgcFlightPhases.APPROACH || phase === FmgcFlightPhases.GOAROUND)) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - if (fl <= 0 || fl > this.maxCruiseFL) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.cruiseLevel = fl; - this.onUpdateCruiseLevel(fl); - - return true; - } - - onUpdateCruiseLevel(newCruiseLevel) { - this._cruiseEntered = true; - this.cruiseTemperature = undefined; - this.updateConstraints(); - - this.flightPhaseManager.handleNewCruiseAltitudeEntered(newCruiseLevel); - } - - trySetRouteReservedFuel(s) { - if (!this.isFlying()) { - if (s) { - if (s === FMCMainDisplay.clrValue) { - this._rteReservedWeightEntered = false; - this._rteReservedPctEntered = false; - this._routeReservedWeight = 0; - this._routeReservedPercent = 5; - this._rteRsvPercentOOR = false; - return true; - } - // Percentage entry must start with '/' - if (s.startsWith("/")) { - return this.trySetRouteReservedPercent(s); - } else { - // If not percentage, try to parse as weight - // Weight can be entered with optional trailing slash, if so remove it before parsing the value - const enteredValue = s.endsWith("/") ? s.slice(0, -1) : s; - - if (!this.representsDecimalNumber(enteredValue)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const rteRsvWeight = NXUnits.userToKg(parseFloat(enteredValue)); - - if (!this.isRteRsvFuelInRange(rteRsvWeight)) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this._rteReservedWeightEntered = true; - this._rteReservedPctEntered = false; - - if (isFinite(rteRsvWeight)) { - this._routeReservedWeight = rteRsvWeight; - this._routeReservedPercent = 0; - - if (!this.isRteRsvPercentInRange(this.getRouteReservedPercent())) { // Bit of a hacky method due previous tight coupling of weight and percentage calculations - this._rteRsvPercentOOR = true; - } - - return true; - } - } - } - } - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - trySetZeroFuelWeightZFWCG(s) { - if (s) { - if (s.includes("/")) { - const sSplit = s.split("/"); - const zfw = NXUnits.userToKg(parseFloat(sSplit[0])); - const zfwcg = parseFloat(sSplit[1]); - if (isFinite(zfw) && isFinite(zfwcg)) { - if (this.isZFWInRange(zfw) && this.isZFWCGInRange(zfwcg)) { - this._zeroFuelWeightZFWCGEntered = true; - this.zeroFuelWeight = zfw; - this.zeroFuelWeightMassCenter = zfwcg; - return true; - } - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - if (!this._zeroFuelWeightZFWCGEntered) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - if (this.isZFWInRange(zfw)) { - this.zeroFuelWeight = zfw; - return true; - } - if (this.isZFWCGInRange(zfwcg)) { - this.zeroFuelWeightMassCenter = zfwcg; - return true; - } - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - if (!this._zeroFuelWeightZFWCGEntered) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - const zfw = NXUnits.userToKg(parseFloat(s)); - if (this.isZFWInRange(zfw)) { - this.zeroFuelWeight = zfw; - return true; - } - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - /** - * - * @returns {number} Returns estimated fuel on board when arriving at the destination - */ - getDestEFOB(useFOB = false) { - return (useFOB ? this.getFOB() : this.blockFuel) - this._routeTripFuelWeight - this.taxiFuelWeight; - } - - /** - * @returns {number} Returns EFOB when arriving at the alternate dest - */ - getAltEFOB(useFOB = false) { - return this.getDestEFOB(useFOB) - this._routeAltFuelWeight; - } - - trySetBlockFuel(s) { - if (s === FMCMainDisplay.clrValue) { - this.blockFuel = undefined; - this._blockFuelEntered = false; - this._fuelPredDone = false; - this._fuelPlanningPhase = this._fuelPlanningPhases.PLANNING; - return true; - } - const value = NXUnits.userToKg(parseFloat(s)); - if (isFinite(value) && this.isBlockFuelInRange(value)) { - if (this.isBlockFuelInRange(value)) { - this.blockFuel = value; - this._blockFuelEntered = true; - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - trySetAverageWind(s) { - const validDelims = ["TL", "T", "+", "HD", "H", "-"]; - const matchedIndex = validDelims.findIndex(element => s.startsWith(element)); - const digits = matchedIndex >= 0 ? s.replace(validDelims[matchedIndex], "") : s; - const isNum = /^\d+$/.test(digits); - if (!isNum) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - const wind = parseInt(digits); - if (wind > 250) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - this.averageWind = matchedIndex <= 2 ? wind : -wind; - return true; - } - - trySetPreSelectedClimbSpeed(s) { - const isNextPhase = this.flightPhaseManager.phase === FmgcFlightPhases.TAKEOFF; - if (s === FMCMainDisplay.clrValue) { - this.preSelectedClbSpeed = undefined; - if (isNextPhase) { - this.updatePreSelSpeedMach(undefined); - } - return true; - } - - const SPD_REGEX = /\d{1,3}/; - if (s.match(SPD_REGEX) === null) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const spd = parseInt(s); - if (!Number.isFinite(spd)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - if (spd < 100 || spd > 350) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.preSelectedClbSpeed = spd; - if (isNextPhase) { - this.updatePreSelSpeedMach(spd); - } - - return true; - } - - trySetPreSelectedCruiseSpeed(s) { - const isNextPhase = this.flightPhaseManager.phase === FmgcFlightPhases.CLIMB; - if (s === FMCMainDisplay.clrValue) { - this.preSelectedCrzSpeed = undefined; - if (isNextPhase) { - this.updatePreSelSpeedMach(undefined); - } - return true; - } - - const MACH_OR_SPD_REGEX = /^(\.\d{1,2}|\d{1,3})$/; - if (s.match(MACH_OR_SPD_REGEX) === null) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const v = parseFloat(s); - if (!Number.isFinite(v)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - if (v < 1) { - const mach = Math.round(v * 100) / 100; - if (mach < 0.15 || mach > 0.82) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.preSelectedCrzSpeed = mach; - } else { - const spd = Math.round(v); - if (spd < 100 || spd > 350) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.preSelectedCrzSpeed = spd; - } - - if (isNextPhase) { - this.updatePreSelSpeedMach(this.preSelectedCrzSpeed); - } - - return true; - } - - setPerfApprQNH(s) { - if (s === FMCMainDisplay.clrValue) { - const dest = this.flightPlanService.active.destinationAirport; - const distanceToDestination = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; - - if (dest && distanceToDestination < 180) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } else { - this.perfApprQNH = NaN; - return true; - } - } - - const value = parseFloat(s); - const HPA_REGEX = /^[01]?[0-9]{3}$/; - const INHG_REGEX = /^([23][0-9]|[0-9]{2}\.)[0-9]{2}$/; - - if (HPA_REGEX.test(s)) { - if (value >= 745 && value <= 1050) { - this.perfApprQNH = value; - SimVar.SetSimVarValue("L:A32NX_DESTINATION_QNH", "Millibar", this.perfApprQNH); - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } else if (INHG_REGEX.test(s)) { - if (value >= 2200 && value <= 3100) { - this.perfApprQNH = value / 100; - SimVar.SetSimVarValue("L:A32NX_DESTINATION_QNH", "Millibar", this.perfApprQNH * 33.8639); - return true; - } else if (value >= 22.0 && value <= 31.00) { - this.perfApprQNH = value; - SimVar.SetSimVarValue("L:A32NX_DESTINATION_QNH", "Millibar", this.perfApprQNH * 33.8639); - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - setPerfApprTemp(s) { - if (s === FMCMainDisplay.clrValue) { - const dest = this.flightPlanService.active.destinationAirport; - const distanceToDestination = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; - - if (dest && distanceToDestination < 180) { - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } else { - this.perfApprTemp = NaN; - return true; - } - } - - if (!/^[\+\-]?\d{1,2}$/.test(s)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - this.perfApprTemp = parseInt(s); - return true; - } - - setPerfApprWind(s) { - if (s === FMCMainDisplay.clrValue) { - this.perfApprWindHeading = NaN; - this.perfApprWindSpeed = NaN; - return true; - } - - // both must be entered - if (!/^\d{1,3}\/\d{1,3}$/.test(s)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - const [dir, mag] = s.split("/").map((v) => parseInt(v)); - if (dir > 360 || mag > 500) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - this.perfApprWindHeading = dir % 360; // 360 is displayed as 0 - this.perfApprWindSpeed = mag; - return true; - } - - setPerfApprTransAlt(s) { - if (s === FMCMainDisplay.clrValue) { - this.flightPlanService.setPerformanceData('pilotTransitionLevel', null); - this.updateTransitionAltitudeLevel(); - return true; - } - - if (!/^\d{4,5}$/.test(s)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - const value = Math.round(parseInt(s) / 10) * 10; - if (value < 1000 || value > 45000) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.flightPlanService.setPerformanceData('pilotTransitionLevel', Math.round(value / 100)); - this.updateTransitionAltitudeLevel(); - return true; - } - - /** - * VApp for _selected_ landing config - */ - getVApp() { - if (isFinite(this.vApp)) { - return this.vApp; - } - return this.approachSpeeds.vapp; - } - - /** - * VApp for _selected_ landing config with GSMini correction - */ - getVAppGsMini() { - let vAppTarget = this.getVApp(); - if (isFinite(this.perfApprWindSpeed) && isFinite(this.perfApprWindHeading)) { - vAppTarget = NXSpeedsUtils.getVtargetGSMini(vAppTarget, NXSpeedsUtils.getHeadWindDiff(this._towerHeadwind)); - } - return vAppTarget; - } - - //Needs PR Merge #3154 - setPerfApprVApp(s) { - if (s === FMCMainDisplay.clrValue) { - if (isFinite(this.vApp)) { - this.vApp = NaN; - return true; - } - } else { - if (s.includes(".")) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - const value = parseInt(s); - if (isFinite(value) && value >= 90 && value <= 350) { - this.vApp = value; - return true; - } - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - this.setScratchpadMessage(NXSystemMessages.notAllowed); - return false; - } - - /** - * Tries to estimate the landing weight at destination - * NaN on failure - */ - tryEstimateLandingWeight() { - const altActive = false; - const landingWeight = this.zeroFuelWeight + (altActive ? this.getAltEFOB(true) : this.getDestEFOB(true)); - return isFinite(landingWeight) ? landingWeight : NaN; - } - - setPerfApprMDA(s) { - if (s === FMCMainDisplay.clrValue) { - this.perfApprMDA = null; - SimVar.SetSimVarValue("L:AIRLINER_MINIMUM_DESCENT_ALTITUDE", "feet", 0); - return true; - } else if (s.match(/^[0-9]{1,5}$/) !== null) { - const value = parseInt(s); - - const activePlan = this.flightPlanService.active; - - let ldgRwy = activePlan.destinationRunway; - - if (!ldgRwy) { - if (activePlan.availableDestinationRunways.length > 0) { - ldgRwy = activePlan.availableDestinationRunways[0]; - } - } - - const limitLo = ldgRwy ? ldgRwy.thresholdLocation.alt : 0; - const limitHi = ldgRwy ? ldgRwy.thresholdLocation.alt + 5000 : 39000; - - if (value >= limitLo && value <= limitHi) { - this.perfApprMDA = value; - SimVar.SetSimVarValue("L:AIRLINER_MINIMUM_DESCENT_ALTITUDE", "feet", this.perfApprMDA); - return true; - } - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } else { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - } - - setPerfApprDH(s) { - if (s === FMCMainDisplay.clrValue) { - this.perfApprDH = null; - return true; - } - - if (s === "NO" || s === "NO DH" || s === "NODH") { - this.perfApprDH = "NO DH"; - SimVar.SetSimVarValue("L:AIRLINER_DECISION_HEIGHT", "feet", -2); - return true; - } else if (s.match(/^[0-9]{1,5}$/) !== null) { - const value = parseInt(s); - if (value >= 0 && value <= 5000) { - this.perfApprDH = value; - SimVar.SetSimVarValue("L:AIRLINER_DECISION_HEIGHT", "feet", this.perfApprDH); - return true; - } else { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - } else { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - } - - setPerfApprFlaps3(s) { - this.perfApprFlaps3 = s; - SimVar.SetSimVarValue("L:A32NX_SPEEDS_LANDING_CONF3", "boolean", s); - } - - /** @param {string} icao ID of the navaid to de-select */ - deselectNavaid(icao) { - this.navigation.getNavaidTuner().deselectNavaid(icao); - } - - reselectNavaid(icao) { - this.navigation.getNavaidTuner().reselectNavaid(icao); - } - - /** @returns {string[]} icaos of deselected navaids */ - get deselectedNavaids() { - return this.navigation.getNavaidTuner().deselectedNavaids; - } - - getVorTuningData(index) { - return this.navigation.getNavaidTuner().getVorRadioTuningStatus(index); - } - - /** - * Set a manually tuned VOR - * @param {1 | 2} index - * @param {RawVor | number | null} facilityOrFrequency null to clear - */ - setManualVor(index, facilityOrFrequency) { - return this.navigation.getNavaidTuner().setManualVor(index, facilityOrFrequency); - } - - /** - * Set a VOR course - * @param {1 | 2} index - * @param {number | null} course null to clear - */ - setVorCourse(index, course) { - return this.navigation.getNavaidTuner().setVorCourse(index, course); - } - - getMmrTuningData(index) { - return this.navigation.getNavaidTuner().getMmrRadioTuningStatus(index); - } - - /** - * Set a manually tuned ILS - * @param {RawVor | number | null} facilityOrFrequency null to clear - */ - async setManualIls(facilityOrFrequency) { - return await this.navigation.getNavaidTuner().setManualIls(facilityOrFrequency); - } - - /** - * Set an ILS course - * @param {number | null} course null to clear - * @param {boolean} backcourse Whether the course is a backcourse/backbeam. - */ - setIlsCourse(course, backcourse = false) { - return this.navigation.getNavaidTuner().setIlsCourse(course, backcourse); - } - - getAdfTuningData(index) { - return this.navigation.getNavaidTuner().getAdfRadioTuningStatus(index); - } - - /** - * Set a manually tuned NDB - * @param {1 | 2} index - * @param {RawNdb | number | null} facilityOrFrequency null to clear - */ - setManualAdf(index, facilityOrFrequency) { - return this.navigation.getNavaidTuner().setManualAdf(index, facilityOrFrequency); - } - - isMmrTuningLocked() { - return this.navigation.getNavaidTuner().isMmrTuningLocked(); - } - - isFmTuningActive() { - return this.navigation.getNavaidTuner().isFmTuningActive(); - } - - /** - * Get the currently selected navaids - * @returns {SelectedNavaid[]} - */ - getSelectedNavaids() { - // FIXME 2 when serving CDU 2 - return this.navigation.getSelectedNavaids(1); - } - - /** - * Set the takeoff flap config - * @param {0 | 1 | 2 | 3 | null} flaps - */ - /* private */ setTakeoffFlaps(flaps) { - if (flaps !== this.flaps) { - this.flaps = flaps; - SimVar.SetSimVarValue("L:A32NX_TO_CONFIG_FLAPS", "number", this.flaps !== null ? this.flaps : -1); - - this.arincDiscreteWord2.setBitValue(13, this.flaps === 0); - this.arincDiscreteWord2.setBitValue(14, this.flaps === 1); - this.arincDiscreteWord2.setBitValue(15, this.flaps === 2); - this.arincDiscreteWord2.setBitValue(16, this.flaps === 3); - this.arincDiscreteWord2.ssm = Arinc429Word.SignStatusMatrix.NormalOperation; - } - } - - /** - * Set the takeoff trim config - * @param {number | null} ths - */ - /* private */ setTakeoffTrim(ths) { - if (ths !== this.ths) { - this.ths = ths; - // legacy vars - SimVar.SetSimVarValue("L:A32NX_TO_CONFIG_THS", "degree", this.ths ? this.ths : 0); - SimVar.SetSimVarValue("L:A32NX_TO_CONFIG_THS_ENTERED", "bool", this.ths !== null); - - const ssm = this.ths !== null ? Arinc429Word.SignStatusMatrix.NormalOperation : Arinc429Word.SignStatusMatrix.NoComputedData; - - this.arincTakeoffPitchTrim.setBnrValue(this.ths ? -this.ths : 0, ssm, 12, 180, -180); - } - } - - trySetFlapsTHS(s) { - if (s === FMCMainDisplay.clrValue) { - this.setTakeoffFlaps(null); - this.setTakeoffTrim(null); - this.tryCheckToData(); - return true; - } - - let newFlaps = null; - let newThs = null; - - let [flaps, ths] = s.split("/"); - - if (flaps && flaps.length > 0) { - if (!/^\d$/.test(flaps)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - flaps = parseInt(flaps); - if (flaps < 0 || flaps > 3) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - newFlaps = flaps; - } - - if (ths && ths.length > 0) { - // allow AAN.N and N.NAA, where AA is UP or DN - if (!/^(UP|DN)(\d|\d?\.\d|\d\.\d?)|(\d|\d?\.\d|\d\.\d?)(UP|DN)$/.test(ths)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - let direction = null; - ths = ths.replace(/(UP|DN)/g, (substr) => { - direction = substr; - return ""; - }); - - if (direction) { - ths = parseFloat(ths); - if (direction === "DN") { - // Note that 0 *= -1 will result in -0, which is strictly - // the same as 0 (that is +0 === -0) and doesn't make a - // difference for the calculation itself. However, in order - // to differentiate between DN0.0 and UP0.0 we'll do check - // later when displaying this value using Object.is to - // determine whether the pilot entered DN0.0 or UP0.0. - ths *= -1; - } - if (!isFinite(ths) || ths < -5 || ths > 7) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - newThs = ths; - } - } - - if (newFlaps !== null) { - if (this.flaps !== null) { - this.tryCheckToData(); - } - this.setTakeoffFlaps(newFlaps); - } - if (newThs !== null) { - if (this.ths !== null) { - this.tryCheckToData(); - } - this.setTakeoffTrim(newThs); - } - return true; - } - - checkEFOBBelowMin() { - if (this._fuelPredDone) { - if (!this._minDestFobEntered) { - this.tryUpdateMinDestFob(); - } - - if (this._minDestFob) { - // round & only use 100kgs precision since thats how it is displayed in fuel pred - const destEfob = Math.round(this.getDestEFOB(this.isAnEngineOn()) * 10) / 10; - const roundedMinDestFob = Math.round(this._minDestFob * 10) / 10; - if (!this._isBelowMinDestFob) { - if (destEfob < roundedMinDestFob) { - this._isBelowMinDestFob = true; - // TODO should be in flight only and if fuel is below min dest efob for 2 minutes - if (this.isAnEngineOn()) { - setTimeout(() => { - this.addMessageToQueue(NXSystemMessages.destEfobBelowMin, () => { - return this._EfobBelowMinClr === true; - }, () => { - this._EfobBelowMinClr = true; - }); - }, 120000); - } else { - this.addMessageToQueue(NXSystemMessages.destEfobBelowMin, () => { - return this._EfobBelowMinClr === true; - }, () => { - this._EfobBelowMinClr = true; - }); - } - } - } else { - // check if we are at least 300kgs above min dest efob to show green again & the ability to trigger the message - if (roundedMinDestFob) { - if (destEfob - roundedMinDestFob >= 0.3) { - this._isBelowMinDestFob = false; - this.removeMessageFromQueue(NXSystemMessages.destEfobBelowMin); - } - } - } - } - } - } - - updateTowerHeadwind() { - if (isFinite(this.perfApprWindSpeed) && isFinite(this.perfApprWindHeading)) { - const activePlan = this.flightPlanService.active; - - if (activePlan.destinationRunway) { - this._towerHeadwind = NXSpeedsUtils.getHeadwind(this.perfApprWindSpeed, this.perfApprWindHeading, activePlan.destinationRunway.magneticBearing); - } - } - } - - /** - * Called after Flaps or THS change - */ - tryCheckToData() { - if (isFinite(this.v1Speed) || isFinite(this.vRSpeed) || isFinite(this.v2Speed)) { - this.addMessageToQueue(NXSystemMessages.checkToData); - } - } - - /** - * Called after runway change - * - Sets confirmation prompt state for every entry whether it is defined or not - * - Adds message when at least one entry needs to be confirmed - * Additional: - * Only prompt the confirmation of FLEX TEMP when the TO runway was changed, not on initial insertion of the runway - */ - onToRwyChanged() { - const activePlan = this.flightPlanService.active; - const selectedRunway = activePlan.originRunway; - - if (!!selectedRunway) { - const toRunway = Avionics.Utils.formatRunway(selectedRunway.ident); - if (toRunway === this.toRunway) { - return; - } - if (!!this.toRunway) { - this.toRunway = toRunway; - this._toFlexChecked = !isFinite(this.perfTOTemp); - this.unconfirmedV1Speed = this.v1Speed; - this.unconfirmedVRSpeed = this.vRSpeed; - this.unconfirmedV2Speed = this.v2Speed; - this.v1Speed = undefined; - this.vRSpeed = undefined; - this.v2Speed = undefined; - - if (!this.unconfirmedV1Speed && !this.unconfirmedVRSpeed && !this.unconfirmedV2Speed) { - return; - } - this.addMessageToQueue(NXSystemMessages.checkToData, (mcdu) => !this.unconfirmedV1Speed && !this.unconfirmedVRSpeed && !this.unconfirmedV2Speed && mcdu._toFlexChecked); - } - this.toRunway = toRunway; - } - } - - /** - * Switches to the next/new perf page (if new flight phase is in order) or reloads the current page - * @param _old {FmgcFlightPhases} - * @param _new {FmgcFlightPhases} - */ - tryUpdatePerfPage(_old, _new) { - // Ensure we have a performance page selected... - if (this.page.Current < this.page.PerformancePageTakeoff || this.page.Current > this.page.PerformancePageGoAround) { - return; - } - - const curPerfPagePhase = (() => { - switch (this.page.Current) { - case this.page.PerformancePageTakeoff : return FmgcFlightPhases.TAKEOFF; - case this.page.PerformancePageClb : return FmgcFlightPhases.CLIMB; - case this.page.PerformancePageCrz : return FmgcFlightPhases.CRUISE; - case this.page.PerformancePageDes : return FmgcFlightPhases.DESCENT; - case this.page.PerformancePageAppr : return FmgcFlightPhases.APPROACH; - case this.page.PerformancePageGoAround : return FmgcFlightPhases.GOAROUND; - } - })(); - - if (_new > _old) { - if (_new >= curPerfPagePhase) { - CDUPerformancePage.ShowPage(this, _new); - } - } else if (_old === curPerfPagePhase) { - CDUPerformancePage.ShowPage(this, _old); - } - } - - routeReservedEntered() { - return this._rteReservedWeightEntered || this._rteReservedPctEntered; - } - - routeFinalEntered() { - return this._rteFinalWeightEntered || this._rteFinalTimeEntered; - } - - /** - * Set the progress page bearing/dist location - * @param {string} ident ident of the waypoint or runway, will be replaced by "ENTRY" if brg/dist offset are specified - * @param {LatLongAlt} coordinates co-ordinates of the waypoint/navaid/runway, without brg/dist offset - * @param {string?} icao icao database id of the waypoint if applicable - */ - _setProgLocation(ident, coordinates, icao) { - console.log(`progLocation: ${ident} ${coordinates}`); - this._progBrgDist = { - icao, - ident, - coordinates, - bearing: -1, - distance: -1 - }; - - this.updateProgDistance(); - } - - /** - * Try to set the progress page bearing/dist waypoint/location - * @param {String} s scratchpad entry - * @param {Function} callback callback taking boolean arg for success/failure - */ - trySetProgWaypoint(s, callback = EmptyCallback.Boolean) { - if (s === FMCMainDisplay.clrValue) { - this._progBrgDist = undefined; - return callback(true); - } - - Fmgc.WaypointEntryUtils.getOrCreateWaypoint(this, s, false, "ENTRY").then((wp) => { - this._setProgLocation(wp.ident, wp.location, wp.databaseId); - return callback(true); - }).catch((err) => { - // Rethrow if error is not an FMS message to display - if (err.type === undefined) { - throw err; - } - - this.showFmsErrorMessage(err.type); - return callback(false); - }); - } - - /** - * Recalculate the bearing and distance for progress page - */ - updateProgDistance() { - if (!this._progBrgDist) { - return; - } - - const latitude = ADIRS.getLatitude(); - const longitude = ADIRS.getLongitude(); - - if (!latitude.isNormalOperation() || !longitude.isNormalOperation()) { - this._progBrgDist.distance = -1; - this._progBrgDist.bearing = -1; - return; - } - - const planeLl = new LatLong(latitude.value, longitude.value); - this._progBrgDist.distance = Avionics.Utils.computeGreatCircleDistance(planeLl, this._progBrgDist.coordinates); - this._progBrgDist.bearing = A32NX_Util.trueToMagnetic(Avionics.Utils.computeGreatCircleHeading(planeLl, this._progBrgDist.coordinates)); - } - - get progBearing() { - return this._progBrgDist ? this._progBrgDist.bearing : -1; - } - - get progDistance() { - return this._progBrgDist ? this._progBrgDist.distance : -1; - } - - get progWaypointIdent() { - return this._progBrgDist ? this._progBrgDist.ident : undefined; - } - - /** - * @param wpt {import('msfs-navdata').Waypoint} - */ - isWaypointInUse(wpt) { - return this.flightPlanService.isWaypointInUse(wpt).then((inUseByFlightPlan) => - inUseByFlightPlan || (this._progBrgDist && this._progBrgDist.icao === wpt.databaseId) - ); - } - - setGroundTempFromOrigin() { - const origin = this.flightPlanService.active.originAirport; - - if (!origin) { - return; - } - - this.groundTempAuto = A32NX_Util.getIsaTemp(origin.location.alt); - } - - trySetGroundTemp(scratchpadValue) { - if (this.flightPhaseManager.phase !== FmgcFlightPhases.PREFLIGHT) { - throw NXSystemMessages.notAllowed; - } - - if (scratchpadValue === FMCMainDisplay.clrValue) { - this.groundTempPilot = undefined; - return; - } - - if (scratchpadValue.match(/^[+\-]?[0-9]{1,2}$/) === null) { - throw NXSystemMessages.formatError; - } - - this.groundTempPilot = parseInt(scratchpadValue); - } - - get groundTemp() { - return this.groundTempPilot !== undefined ? this.groundTempPilot : this.groundTempAuto; - } - - navModeEngaged() { - const lateralMode = SimVar.GetSimVarValue("L:A32NX_FMA_LATERAL_MODE", "Number"); - switch (lateralMode) { - case 20: // NAV - case 30: // LOC* - case 31: // LOC - case 32: // LAND - case 33: // FLARE - case 34: // ROLL OUT - return true; - } - return false; - } - - /** - * Add type 2 message to fmgc message queue - * @param _message {TypeIIMessage} MessageObject - * @param _isResolvedOverride {function(*)} Function that determines if the error is resolved at this moment (type II only). - * @param _onClearOverride {function(*)} Function that executes when the error is actively cleared by the pilot (type II only). - */ - addMessageToQueue(_message, _isResolvedOverride = undefined, _onClearOverride = undefined) { - if (!_message.isTypeTwo) { - return; - } - const message = _isResolvedOverride === undefined && _onClearOverride === undefined ? _message : _message.getModifiedMessage("", _isResolvedOverride, _onClearOverride); - this._messageQueue.addMessage(message); - } - - /** - * Removes a message from the queue - * @param value {String} - */ - removeMessageFromQueue(value) { - this._messageQueue.removeMessage(value); - } - - updateMessageQueue() { - this._messageQueue.updateDisplayedMessage(); - } - - /* END OF MCDU GET/SET METHODS */ - /* UNSORTED CODE BELOW */ - - //TODO: can this be util? - static secondsToUTC(seconds) { - const h = Math.floor(seconds / 3600); - const m = Math.floor((seconds - h * 3600) / 60); - return (h % 24).toFixed(0).padStart(2, "0") + m.toFixed(0).padStart(2, "0"); - } - //TODO: can this be util? - static secondsTohhmm(seconds) { - const h = Math.floor(seconds / 3600); - const m = Math.floor((seconds - h * 3600) / 60); - return h.toFixed(0).padStart(2, "0") + m.toFixed(0).padStart(2, "0"); - } - - //TODO: can this be util? - static minuteToSeconds(minutes) { - return minutes * 60; - } - - //TODO: can this be util? - static hhmmToSeconds(hhmm) { - if (!hhmm) { - return NaN; - } - const h = parseInt(hhmm.substring(0, 2)); - const m = parseInt(hhmm.substring(2, 4)); - return h * 3600 + m * 60; - } - - /** - * Computes hour and minutes when given minutes - * @param {number} minutes - minutes used to make the conversion - * @returns {string} A string in the format "HHMM" e.g "0235" - */ - //TODO: can this be util? - static minutesTohhmm(minutes) { - const h = Math.floor(minutes / 60); - const m = minutes - h * 60; - return h.toFixed(0).padStart(2,"0") + m.toFixed(0).padStart(2, "0"); - } - - /** - * computes minutes when given hour and minutes - * @param {string} hhmm - string used to make the conversion - * @returns {number} numbers in minutes form - */ - //TODO: can this be util? - static hhmmToMinutes(hhmm) { - if (!hhmm) { - return NaN; - } - const h = parseInt(hhmm.substring(0, 2)); - const m = parseInt(hhmm.substring(2, 4)); - return h * 60 + m; - } - - /** - * Generic function which returns true if engine(index) is ON (N2 > 20) - * @returns {boolean} - */ - isEngineOn(index) { - return SimVar.GetSimVarValue(`L:A32NX_ENGINE_N2:${index}`, 'number') > 20; - } - /** - * Returns true if any one engine is running (N2 > 20) - * @returns {boolean} - */ - //TODO: can this be an util? - isAnEngineOn() { - return this.isEngineOn(1) || this.isEngineOn(2); - } - - /** - * Returns true only if all engines are running (N2 > 20) - * @returns {boolean} - */ - //TODO: can this be an util? - isAllEngineOn() { - return this.isEngineOn(1) && this.isEngineOn(2); - } - - isOnGround() { - return SimVar.GetSimVarValue("L:A32NX_LGCIU_1_NOSE_GEAR_COMPRESSED", "Number") === 1 || SimVar.GetSimVarValue("L:A32NX_LGCIU_2_NOSE_GEAR_COMPRESSED", "Number") === 1; - } - - isFlying() { - return this.flightPhaseManager.phase >= FmgcFlightPhases.TAKEOFF && this.flightPhaseManager.phase < FmgcFlightPhases.DONE; - } - /** - * Returns the maximum cruise FL for ISA temp and GW - * @param temp {number} ISA in C° - * @param gw {number} GW in t - * @returns {number} MAX FL - */ - //TODO: can this be an util? - getMaxFL(temp = A32NX_Util.getIsaTempDeviation(), gw = this.getGW()) { - return Math.round(temp <= 10 ? -2.778 * gw + 578.667 : (temp * (-0.039) - 2.389) * gw + temp * (-0.667) + 585.334); - } - - /** - * Returns the maximum allowed cruise FL considering max service FL - * @param fl {number} FL to check - * @returns {number} maximum allowed cruise FL - */ - //TODO: can this be an util? - getMaxFlCorrected(fl = this.getMaxFL()) { - return fl >= this.recMaxCruiseFL ? this.recMaxCruiseFL : fl; - } - - // only used by trySetMinDestFob - //TODO: Can this be util? - isMinDestFobInRange(fuel) { - return 0 <= fuel && fuel <= 80.0; - } - - //TODO: Can this be util? - isTaxiFuelInRange(taxi) { - return 0 <= taxi && taxi <= 9.9; - } - - //TODO: Can this be util? - isFinalFuelInRange(fuel) { - return 0 <= fuel && fuel <= 100; - } - - //TODO: Can this be util? - isFinalTimeInRange(time) { - const convertedTime = FMCMainDisplay.hhmmToMinutes(time.padStart(4,"0")); - return 0 <= convertedTime && convertedTime <= 90; - } - - //TODO: Can this be util? - isRteRsvFuelInRange(fuel) { - return 0 <= fuel && fuel <= 10.0; - } - - //TODO: Can this be util? - isRteRsvPercentInRange(value) { - return value >= 0 && value <= 15.0; - } - - //TODO: Can this be util? - isZFWInRange(zfw) { - return 35.0 <= zfw && zfw <= 80.0; - } - - //TODO: Can this be util? - isZFWCGInRange(zfwcg) { - return (8.0 <= zfwcg && zfwcg <= 50.0); - } - - //TODO: Can this be util? - isBlockFuelInRange(fuel) { - return 0 <= fuel && fuel <= 80; - } - - /** - * Retrieves current fuel on boad in tons. - * - * @returns {number | undefined} current fuel on board in tons, or undefined if fuel readings are not available. - */ - //TODO: Can this be util? - getFOB() { - const useFqi = this.isAnEngineOn(); - - // If an engine is not running, use pilot entered block fuel to calculate fuel predictions - return useFqi ? (SimVar.GetSimVarValue("FUEL TOTAL QUANTITY WEIGHT", "pound") * 0.4535934) / 1000 : this.blockFuel; - } - - /** - * retrieves gross weight in tons or 0 if not available - * @returns {number} - * @deprecated use getGrossWeight() instead - */ - //TODO: Can this be util? - getGW() { - const fmGwOrNull = this.getGrossWeight(); - const fmGw = fmGwOrNull !== null ? fmGwOrNull : 0; - - SimVar.SetSimVarValue("L:A32NX_FM_GROSS_WEIGHT", "Number", fmGw); - return fmGw; - } - - //TODO: Can this be util? - getCG() { - return SimVar.GetSimVarValue("CG PERCENT", "Percent over 100") * 100; - } - - //TODO: make this util or local var? - isAirspeedManaged() { - return SimVar.GetSimVarValue("AUTOPILOT SPEED SLOT INDEX", "number") === 2; - } - - //TODO: make this util or local var? - isHeadingManaged() { - return SimVar.GetSimVarValue("AUTOPILOT HEADING SLOT INDEX", "number") === 2; - } - - //TODO: make this util or local var? - isAltitudeManaged() { - return SimVar.GetSimVarValue("AUTOPILOT ALTITUDE SLOT INDEX", "number") === 2; - } - - /** - * Check if the given string represents a decimal number. - * This may be a whole number or a number with one or more decimals. - * If the leading digit is 0 and one or more decimals are given, the leading digit may be omitted. - * @param str {string} String to check - * @returns {bool} True if str represents a decimal value, otherwise false - */ - //TODO: Can this be util? - representsDecimalNumber(str) { - return /^[+-]?\d*(?:\.\d+)?$/.test(str); - } - - /** - * Gets the entered zero fuel weight, or undefined if not entered - * @returns {number | undefined} the zero fuel weight in tonnes or undefined - */ - getZeroFuelWeight() { - return this.zeroFuelWeight; - } - - getV2Speed() { - return this.v2Speed; - } - - getTropoPause() { - return this.tropo; - } - - getManagedClimbSpeed() { - return this.managedSpeedClimb; - } - - getManagedClimbSpeedMach() { - return this.managedSpeedClimbMach; - } - - getManagedCruiseSpeed() { - return this.managedSpeedCruise; - } - - getManagedCruiseSpeedMach() { - return this.managedSpeedCruiseMach; - } - - getAccelerationAltitude() { - const plan = this.currFlightPlanService.active; - - if (plan) { - return plan.performanceData.accelerationAltitude; - } - - return undefined; - } - - getThrustReductionAltitude() { - const plan = this.currFlightPlanService.active; - - if (plan) { - return plan.performanceData.thrustReductionAltitude; - } - - return undefined; - } - - getOriginTransitionAltitude() { - const plan = this.currFlightPlanService.active; - - if (plan) { - return plan.performanceData.transitionAltitude; - } - - return undefined; - } - - getDestinationTransitionLevel() { - const plan = this.currFlightPlanService.active; - - if (plan) { - return plan.performanceData.transitionLevel; - } - - return undefined; - } - - get cruiseLevel() { - const plan = this.currFlightPlanService.active; - - if (plan) { - return plan.performanceData.cruiseFlightLevel; - } - - return undefined; - } - - set cruiseLevel(level) { - const plan = this.currFlightPlanService.active; - - if (plan) { - this.currFlightPlanService.setPerformanceData('cruiseFlightLevel', level); - // used by FlightPhaseManager - SimVar.SetSimVarValue('L:A32NX_AIRLINER_CRUISE_ALTITUDE', 'number', Number.isFinite(level * 100) ? level * 100 : 0); - } - } - - get costIndex() { - const plan = this.currFlightPlanService.active; - - if (plan) { - return plan.performanceData.costIndex; - } - - return undefined; - } - - set costIndex(ci) { - const plan = this.currFlightPlanService.active; - - if (plan) { - this.currFlightPlanService.setPerformanceData('costIndex', ci); - } - } - - get isCostIndexSet() { - const plan = this.currFlightPlanService.active; - - if (plan) { - return plan.performanceData.costIndex !== undefined; - } - - return false; - } - - get tropo() { - const plan = this.currFlightPlanService.active; - - if (plan) { - return plan.performanceData.tropopause; - } - - return undefined; - } - - get isTropoPilotEntered() { - const plan = this.currFlightPlanService.active; - - if (plan) { - return plan.performanceData.tropopauseIsPilotEntered; - } - - return false; - } - - set tropo(tropo) { - const plan = this.currFlightPlanService.active; - - if (plan) { - this.currFlightPlanService.setPerformanceData('pilotTropopause', tropo); - } - } - - get flightNumber() { - const plan = this.currFlightPlanService.active; - - if (plan) { - return this.currFlightPlanService.active.flightNumber; - } - - return undefined; - - } - - set flightNumber(flightNumber) { - const plan = this.currFlightPlanService.active; - - if (plan) { - this.currFlightPlanService.setFlightNumber(flightNumber); - } - } - - /** - * The maximum speed imposed by the climb speed limit in the active flight plan or null if it is not set. - * @returns {number | null} - */ - get climbSpeedLimit() { - const plan = this.currFlightPlanService.active; - - // The plane follows 250 below 10'000 even without a flight plan - return plan ? plan.performanceData.climbSpeedLimitSpeed : DefaultPerformanceData.ClimbSpeedLimitSpeed; - } - - /** - * The altitude below which the climb speed limit of the active flight plan applies or null if not set. - * @returns {number | null} - */ - get climbSpeedLimitAlt() { - const plan = this.currFlightPlanService.active; - - // The plane follows 250 below 10'000 even without a flight plan - return plan ? plan.performanceData.climbSpeedLimitAltitude : DefaultPerformanceData.ClimbSpeedLimitAltitude; - } - - get climbSpeedLimitPilot() { - const plan = this.currFlightPlanService.active; - - return plan ? plan.performanceData.isClimbSpeedLimitPilotEntered : false; - } - - /** - * The maximum speed imposed by the descent speed limit in the active flight plan or null if it is not set. - * @returns {number | null} - */ - get descentSpeedLimit() { - const plan = this.currFlightPlanService.active; - - // The plane follows 250 below 10'000 even without a flight plan - return plan ? plan.performanceData.descentSpeedLimitSpeed : DefaultPerformanceData.DescentSpeedLimitSpeed; - } - - /** - * The altitude below which the descent speed limit of the active flight plan applies or null if not set. - * @returns {number | null} - */ - get descentSpeedLimitAlt() { - const plan = this.currFlightPlanService.active; - - // The plane follows 250 below 10'000 even without a flight plan - return plan ? plan.performanceData.descentSpeedLimitAltitude : DefaultPerformanceData.DescentSpeedLimitAltitude; - } - - get descentSpeedLimitPilot() { - const plan = this.currFlightPlanService.active; - - return plan ? plan.performanceData.isDescentSpeedLimitPilotEntered : false; - } - - getFlightPhase() { - return this.flightPhaseManager.phase; - } - - getClimbSpeedLimit() { - return { - speed: this.climbSpeedLimit, - underAltitude: this.climbSpeedLimitAlt, - }; - } - - getDescentSpeedLimit() { - return { - speed: this.descentSpeedLimit, - underAltitude: this.descentSpeedLimitAlt, - }; - } - - getPreSelectedClbSpeed() { - return this.preSelectedClbSpeed; - } - - getPreSelectedCruiseSpeed() { - return this.preSelectedCrzSpeed; - } - - getTakeoffFlapsSetting() { - return this.flaps; - } - - getManagedDescentSpeed() { - return this.managedSpeedDescendPilot !== undefined ? this.managedSpeedDescendPilot : this.managedSpeedDescend; - } - - getManagedDescentSpeedMach() { - return this.managedSpeedDescendMachPilot !== undefined ? this.managedSpeedDescendMachPilot : this.managedSpeedDescendMach; - } - - getApproachSpeed() { - return this.approachSpeeds && this.approachSpeeds.valid ? this.approachSpeeds.vapp : 0; - } - - getFlapRetractionSpeed() { - return this.approachSpeeds && this.approachSpeeds.valid ? this.approachSpeeds.f : 0; - } - - getSlatRetractionSpeed() { - return this.approachSpeeds && this.approachSpeeds.valid ? this.approachSpeeds.s : 0; - } - - getCleanSpeed() { - return this.approachSpeeds && this.approachSpeeds.valid ? this.approachSpeeds.gd : 0; - } - - getTripWind() { - // FIXME convert vnav to use +ve for tailwind, -ve for headwind, it's the other way around at the moment - return -this.averageWind; - } - - getWinds() { - return this.winds; - } - - getApproachWind() { - const activePlan = this.currFlightPlanService.active; - const destination = activePlan.destinationAirport; - - if (!destination || !destination.location || !isFinite(this.perfApprWindHeading)) { - return { direction: 0, speed: 0 }; - } - - const magVar = Facilities.getMagVar(destination.location.lat, destination.location.long); - const trueHeading = A32NX_Util.magneticToTrue(this.perfApprWindHeading, magVar); - - return { direction: trueHeading, speed: this.perfApprWindSpeed }; - } - - getApproachQnh() { - return this.perfApprQNH; - } - - getApproachTemperature() { - return this.perfApprTemp; - } - - getDestinationElevation() { - return Number.isFinite(this.landingElevation) ? this.landingElevation : 0; - } - - trySetManagedDescentSpeed(value) { - if (value === FMCMainDisplay.clrValue) { - this.managedSpeedDescendPilot = undefined; - this.managedSpeedDescendMachPilot = undefined; - return true; - } - - const MACH_SLASH_SPD_REGEX = /^(\.\d{1,2})?\/(\d{3})?$/; - const machSlashSpeedMatch = value.match(MACH_SLASH_SPD_REGEX); - - const MACH_REGEX = /^\.\d{1,2}$/; - const SPD_REGEX = /^\d{1,3}$/; - - if (machSlashSpeedMatch !== null /* ".NN/" or "/NNN" entry */) { - const speed = parseInt(machSlashSpeedMatch[2]); - if (Number.isFinite(speed)) { - if (speed < 100 || speed > 350) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.managedSpeedDescendPilot = speed; - } - - const mach = Math.round(parseFloat(machSlashSpeedMatch[1]) * 1000) / 1000; - if (Number.isFinite(mach)) { - if (mach < 0.15 || mach > 0.82) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.managedSpeedDescendMachPilot = mach; - } - - return true; - } else if (value.match(MACH_REGEX) !== null /* ".NN" */) { - // Entry of a Mach number only without a slash is allowed - const mach = Math.round(parseFloat(value) * 1000) / 1000; - if (Number.isFinite(mach)) { - if (mach < 0.15 || mach > 0.82) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.managedSpeedDescendMachPilot = mach; - } - - return true; - } else if (value.match(SPD_REGEX) !== null /* "NNN" */) { - const speed = parseInt(value); - if (Number.isFinite(speed)) { - if (speed < 100 || speed > 350) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - // This is the maximum managed Mach number you can get, even with CI 100. - // Through direct testing by a pilot, it was also determined that the plane gives Mach 0.80 for all of the tested CAS entries. - const mach = 0.8; - - this.managedSpeedDescendPilot = speed; - this.managedSpeedDescendMachPilot = mach; - - return true; - } - } - - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - trySetPerfClbPredToAltitude(value) { - if (value === FMCMainDisplay.clrValue) { - this.perfClbPredToAltitudePilot = undefined; - return true; - } - - const currentAlt = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); - const match = value.match(/^(FL\d{3}|\d{1,5})$/); - if (match === null || match.length < 1) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const altOrFlString = match[1].replace("FL", ""); - const altitude = altOrFlString.length < 4 ? 100 * parseInt(altOrFlString) : parseInt(altOrFlString); - - if (!Number.isFinite(altitude)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - if (altitude < currentAlt || (this.cruiseLevel && altitude > this.cruiseLevel * 100)) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.perfClbPredToAltitudePilot = altitude; - return true; - } - - trySetPerfDesPredToAltitude(value) { - if (value === FMCMainDisplay.clrValue) { - this.perfDesPredToAltitudePilot = undefined; - return true; - } - - const currentAlt = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); - const match = value.match(/^(FL\d{3}|\d{1,5})$/); - if (match === null || match.length < 1) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - const altOrFlString = match[1].replace("FL", ""); - const altitude = altOrFlString.length < 4 ? 100 * parseInt(altOrFlString) : parseInt(altOrFlString); - - if (!Number.isFinite(altitude)) { - this.setScratchpadMessage(NXSystemMessages.formatError); - return false; - } - - if (altitude > currentAlt) { - this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); - return false; - } - - this.perfDesPredToAltitudePilot = altitude; - return true; - } - - updatePerfPageAltPredictions() { - const currentAlt = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); - if (this.perfClbPredToAltitudePilot !== undefined && currentAlt > this.perfClbPredToAltitudePilot) { - this.perfClbPredToAltitudePilot = undefined; - } - - if (this.perfDesPredToAltitudePilot !== undefined && currentAlt < this.perfDesPredToAltitudePilot) { - this.perfDesPredToAltitudePilot = undefined; - } - } - - computeManualCrossoverAltitude(mach) { - const maximumCrossoverAltitude = 30594; // Crossover altitude of (300, 0.8) - const mmoCrossoverAltitide = 24554; // Crossover altitude of (VMO, MMO) - - if (mach < 0.8) { - return maximumCrossoverAltitude; - } - - return maximumCrossoverAltitude + (mmoCrossoverAltitide - maximumCrossoverAltitude) * (mach - 0.8) / 0.02; - } - - getActivePlanLegCount() { - if (!this.flightPlanService.hasActive) { - return 0; - } - - return this.flightPlanService.active.legCount; - } - - getDistanceToDestination() { - return this.guidanceController.alongTrackDistanceToDestination; - } - - /** - * Modifies the active flight plan to go direct to a specific waypoint, not necessarily in the flight plan - * @param {import('msfs-navdata').Waypoint} waypoint - */ - async directToWaypoint(waypoint) { - // FIXME fm pos - const adirLat = ADIRS.getLatitude(); - const adirLong = ADIRS.getLongitude(); - const trueTrack = ADIRS.getTrueTrack(); - - if (!adirLat.isNormalOperation() || !adirLong.isNormalOperation() || !trueTrack.isNormalOperation()) { - return; - } - - const ppos = { - lat: adirLat.value, - long: adirLong.value, - }; - - await this.flightPlanService.directToWaypoint(ppos, trueTrack.value, waypoint); - } - - /** - * Modifies the active flight plan to go direct to a specific leg - * @param {number} legIndex index of leg to go direct to - */ - async directToLeg(legIndex) { - // FIXME fm pos - const adirLat = ADIRS.getLatitude(); - const adirLong = ADIRS.getLongitude(); - const trueTrack = ADIRS.getTrueTrack(); - - if (!adirLat.isNormalOperation() || !adirLong.isNormalOperation() || !trueTrack.isNormalOperation()) { - return; - } - - const ppos = { - lat: adirLat.value, - long: adirLong.value, - }; - - await this.flightPlanService.directToLeg(ppos, trueTrack.value, legIndex); - } - - /** - * Gets the navigation database ident (including cycle info). - * @returns {import('msfs-navdata').DatabaseIdent | null}. - */ - getNavDatabaseIdent() { - return this.navDbIdent; - } -} - -FMCMainDisplay.clrValue = "\xa0\xa0\xa0\xa0\xa0CLR"; -FMCMainDisplay.ovfyValue = "\u0394"; -FMCMainDisplay._AvailableKeys = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - -const FlightPlans = Object.freeze({ - Active: 0, - Temporary: 1, -}); - -const DefaultPerformanceData = Object.freeze({ - ClimbSpeedLimitSpeed: 250, - ClimbSpeedLimitAltitude: 10000, - DescentSpeedLimitSpeed: 250, - DescentSpeedLimitAltitude: 10000, -}); - -class FmArinc429OutputWord extends Arinc429Word { - constructor(name, value = 0) { - super(0); - - this.name = name; - this.dirty = true; - this._value = value; - this._ssm = 0; - } - - get value() { - return this._value; - } - - set value(value) { - if (this._value !== value) { - this.dirty = true; - } - this._value = value; - } - - get ssm() { - return this._ssm; - } - - set ssm(ssm) { - if (this._ssm !== ssm) { - this.dirty = true; - } - this._ssm = ssm; - } - - static empty(name) { - return new FmArinc429OutputWord(name, 0); - } - - async writeToSimVarIfDirty() { - if (this.dirty) { - this.dirty = false; - return Promise.all([ - Arinc429Word.toSimVarValue(`L:A32NX_FM1_${this.name}`, this.value, this.ssm), - Arinc429Word.toSimVarValue(`L:A32NX_FM2_${this.name}`, this.value, this.ssm), - ]); - } - return Promise.resolve(); - } - - setBnrValue(value, ssm, bits, rangeMax, rangeMin = 0) { - const quantum = Math.max(Math.abs(rangeMin), rangeMax) / 2 ** bits; - const data = Math.max(rangeMin, Math.min(rangeMax, Math.round(value / quantum) * quantum)); - - this.value = data; - this.ssm = ssm; - } -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/NavSystems/A320_Neo/A32NX_NavSystem.css b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/NavSystems/A320_Neo/A32NX_NavSystem.css deleted file mode 100644 index f07ce62d1dd..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/NavSystems/A320_Neo/A32NX_NavSystem.css +++ /dev/null @@ -1,9 +0,0 @@ -@import url("/CSS/A32NX_Display_Common.css"); - -@font-face { - font-family: "ECAMFontRegular"; - src: url("/Fonts/fbw-a32nx/ECAMFontRegular.ttf") format("truetype"); - fill: var(--displayMagenta); - font-weight: normal; - font-style: normal; -} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/NavSystems/A320_Neo/A32NX_NavSystem.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/NavSystems/A320_Neo/A32NX_NavSystem.js deleted file mode 100644 index 66d5a4fb95f..00000000000 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/NavSystems/A320_Neo/A32NX_NavSystem.js +++ /dev/null @@ -1,2957 +0,0 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -class NavSystem extends BaseInstrument { - constructor() { - super(...arguments); - this.soundSourceNode = "AS1000_MFD"; - this.eventAliases = []; - this.IndependentsElements = []; - this.pageGroups = []; - this.eventLinkedPageGroups = []; - this.currentEventLinkedPageGroup = null; - this.overridePage = null; - this.eventLinkedPopUpElements = []; - this.popUpElement = null; - this.popUpCloseCallback = null; - this.currentPageGroupIndex = 0; - this.currentInteractionState = 0; - this.cursorIndex = 0; - this.currentSelectableArray = []; - this.currentContextualMenu = null; - this.currentSearchFieldWaypoint = null; - this.contextualMenuDisplayBeginIndex = 0; - this.menuMaxElems = 6; - this.useUpdateBudget = true; - this.maxUpdateBudget = 6; - this.budgetedItemId = 0; - this.aspectRatioElement = null; - this.forcedAspectRatioSet = false; - this.forcedAspectRatio = 1; - this.forcedScreenRatio = 1; - this.initDuration = 0; - this.hasBeenOff = false; - this.needValidationAfterInit = false; - this.isStarted = false; - this.initAcknowledged = true; - this.reversionaryMode = false; - this.alwaysUpdateList = new Array(); - this.accumulatedDeltaTime = 0; - this.navDatabaseBackend = Fmgc.NavigationDatabaseBackend.Msfs; - } - get instrumentAlias() { - return null; - } - connectedCallback() { - super.connectedCallback(); - this.contextualMenu = this.getChildById("ContextualMenu"); - this.contextualMenuTitle = this.getChildById("ContextualMenuTitle"); - this.contextualMenuElements = this.getChildById("ContextualMenuElements"); - this.menuSlider = this.getChildById("SliderMenu"); - this.menuSliderCursor = this.getChildById("SliderMenuCursor"); - - if (this.nodeName.includes('CDU')) { - this.bus = new Fmgc.EventBus(); - this.currFlightPhaseManager = new Fmgc.FlightPhaseManager(this.bus); - this.currFlightPlanService = new Fmgc.FlightPlanService(this.bus, new Fmgc.A320FlightPlanPerformanceData()); - this.rpcServer = new Fmgc.FlightPlanRpcServer(this.bus, this.currFlightPlanService); - - this.currFlightPlanService.createFlightPlans(); - - this.currNavigationDatabaseService = Fmgc.NavigationDatabaseService; - - this.navigationDatabase = new Fmgc.NavigationDatabase(Fmgc.NavigationDatabaseBackend.Msfs); - this.currNavigationDatabaseService.activeDatabase = this.navigationDatabase; - } - } - get flightPhaseManager() { - return this.currFlightPhaseManager; - } - - get flightPlanService() { - return this.currFlightPlanService; - } - - flightPlan(index, alternate) { - const plan = index === Fmgc.FlightPlanIndex.Active ? this.flightPlanService.activeOrTemporary : this.flightPlanService.get(index); - - if (alternate) { - return plan.alternateFlightPlan; - } - - return plan; - } - - get navigationDatabaseService() { - return this.currNavigationDatabaseService; - } - - disconnectedCallback() { - super.disconnectedCallback(); - } - parseXMLConfig() { - super.parseXMLConfig(); - const soundSourceNodeElem = this.xmlConfig.getElementsByTagName("SoundSourceNode"); - if (soundSourceNodeElem.length > 0) { - this.soundSourceNode = soundSourceNodeElem[0].textContent; - } - if (this.instrumentXmlConfig) { - const skipValidationAfterInitElem = this.instrumentXmlConfig.getElementsByTagName("SkipValidationAfterInit"); - if (skipValidationAfterInitElem.length > 0 && this.needValidationAfterInit) { - this.needValidationAfterInit = skipValidationAfterInitElem[0].textContent != "True"; - } - const styleNode = this.instrumentXmlConfig.getElementsByTagName("Style"); - if (styleNode.length > 0) { - this.electricity.setAttribute("displaystyle", styleNode[0].textContent); - } - } - } - computeEvent(_event) { - if (this.isBootProcedureComplete()) { - for (let i = 0; i < this.eventLinkedPageGroups.length; i++) { - if (_event == this.eventLinkedPageGroups[i].popUpEvent) { - this.lastRelevantICAO = null; - this.lastRelevantICAOType = null; - if (this.overridePage) { - this.closeOverridePage(); - } - if (this.eventLinkedPageGroups[i] == this.currentEventLinkedPageGroup) { - this.exitEventLinkedPageGroup(); - } else { - const currentGroup = this.getCurrentPageGroup(); - if (currentGroup) { - currentGroup.onExit(); - } - this.currentEventLinkedPageGroup = this.eventLinkedPageGroups[i]; - this.currentEventLinkedPageGroup.pageGroup.onEnter(); - } - this.SwitchToInteractionState(0); - } - } - for (let i = 0; i < this.eventLinkedPopUpElements.length; i++) { - if (_event == this.eventLinkedPopUpElements[i].popUpEvent) { - if (this.popUpElement == this.eventLinkedPopUpElements[i]) { - this.popUpElement.onExit(); - this.popUpElement = null; - } else { - this.switchToPopUpPage(this.eventLinkedPopUpElements[i]); - } - } - } - for (let i = 0; i < this.IndependentsElements.length; i++) { - this.IndependentsElements[i].onEvent(_event); - } - if (this.popUpElement) { - this.popUpElement.onEvent(_event); - } - const currentPage = this.getCurrentPage(); - if (currentPage) { - currentPage.onEvent(_event); - } - switch (this.currentInteractionState) { - case 1: - if (this.currentSelectableArray[this.cursorIndex].SendEvent(_event)) { - break; - } - if (_event == "NavigationPush") { - this.SwitchToInteractionState(0); - } - if (_event == "NavigationLargeInc") { - this.cursorIndex = (this.cursorIndex + 1) % this.currentSelectableArray.length; - while (!this.currentSelectableArray[this.cursorIndex].onSelection(_event)) { - this.cursorIndex = (this.cursorIndex + 1) % this.currentSelectableArray.length; - } - } - if (_event == "NavigationLargeDec") { - this.cursorIndex = (this.cursorIndex - 1) < 0 ? (this.currentSelectableArray.length - 1) : (this.cursorIndex - 1); - while (!this.currentSelectableArray[this.cursorIndex].onSelection(_event)) { - this.cursorIndex = (this.cursorIndex - 1) < 0 ? (this.currentSelectableArray.length - 1) : (this.cursorIndex - 1); - } - } - if (_event == "MENU_Push") { - var defaultMenu; - if (this.popUpElement) { - defaultMenu = this.popUpElement.getDefaultMenu(); - } - if (!defaultMenu) { - var defaultMenu = this.getCurrentPage().defaultMenu; - } - if (defaultMenu != null) { - this.ShowContextualMenu(defaultMenu); - } - } - break; - case 2: - if (_event == "NavigationSmallInc") { - let count = 0; - do { - this.cursorIndex = (this.cursorIndex + 1) % this.currentContextualMenu.elements.length; - if (this.cursorIndex > (this.contextualMenuDisplayBeginIndex + 5)) { - this.contextualMenuDisplayBeginIndex++; - } - if (this.cursorIndex < (this.contextualMenuDisplayBeginIndex)) { - this.contextualMenuDisplayBeginIndex = 0; - } - count++; - } while (this.currentContextualMenu.elements[this.cursorIndex].isInactive() == true && count < this.currentContextualMenu.elements.length); - } - if (_event == "NavigationSmallDec") { - const count = 0; - do { - this.cursorIndex = (this.cursorIndex - 1) < 0 ? (this.currentContextualMenu.elements.length - 1) : (this.cursorIndex - 1); - if (this.cursorIndex < (this.contextualMenuDisplayBeginIndex)) { - this.contextualMenuDisplayBeginIndex--; - } - if (this.cursorIndex > (this.contextualMenuDisplayBeginIndex + 5)) { - this.contextualMenuDisplayBeginIndex = this.currentContextualMenu.elements.length - 5; - } - } while (this.currentContextualMenu.elements[this.cursorIndex].isInactive() == true && count < this.currentContextualMenu.elements.length); - } - if (_event == "MENU_Push") { - this.SwitchToInteractionState(0); - } - if (_event == "ENT_Push") { - this.currentContextualMenu.elements[this.cursorIndex].SendEvent(); - } - break; - case 3: - this.currentSearchFieldWaypoint.onInteractionEvent([_event]); - break; - case 0: - if (_event == "MENU_Push") { - var defaultMenu; - if (this.popUpElement) { - defaultMenu = this.popUpElement.getDefaultMenu(); - } - if (!defaultMenu) { - var defaultMenu = this.getCurrentPage().defaultMenu; - } - if (defaultMenu != null) { - this.ShowContextualMenu(defaultMenu); - } - } - if (_event == "NavigationSmallInc") { - this.lastRelevantICAO = null; - this.lastRelevantICAOType = null; - this.getCurrentPageGroup().nextPage(); - } - if (_event == "NavigationSmallDec") { - this.lastRelevantICAO = null; - this.lastRelevantICAOType = null; - this.getCurrentPageGroup().prevPage(); - } - if (_event == "NavigationLargeInc") { - this.lastRelevantICAO = null; - this.lastRelevantICAOType = null; - if (this.pageGroups.length > 1 && !this.currentEventLinkedPageGroup) { - this.pageGroups[this.currentPageGroupIndex].onExit(); - this.currentPageGroupIndex = (this.currentPageGroupIndex + 1) % this.pageGroups.length; - this.pageGroups[this.currentPageGroupIndex].onEnter(); - } - } - if (_event == "NavigationLargeDec") { - this.lastRelevantICAO = null; - this.lastRelevantICAOType = null; - if (this.pageGroups.length > 1 && !this.currentEventLinkedPageGroup) { - this.pageGroups[this.currentPageGroupIndex].onExit(); - this.currentPageGroupIndex = (this.currentPageGroupIndex + this.pageGroups.length - 1) % this.pageGroups.length; - this.pageGroups[this.currentPageGroupIndex].onEnter(); - } - } - if (_event == "NavigationPush") { - let defaultSelectableArray = this.getCurrentPage().element.getDefaultSelectables(); - if (this.popUpElement) { - defaultSelectableArray = this.popUpElement.element.getDefaultSelectables(); - } - if (defaultSelectableArray != null && defaultSelectableArray.length > 0) { - this.ActiveSelection(defaultSelectableArray); - } - } - break; - } - switch (_event) { - case "ActiveFPL_Modified": - this.currFlightPlan.FillWithCurrentFP(); - } - } - this.onEvent(_event); - } - exitEventLinkedPageGroup() { - this.currentEventLinkedPageGroup.pageGroup.onExit(); - this.currentEventLinkedPageGroup = null; - const currentGroup = this.getCurrentPageGroup(); - if (currentGroup) { - currentGroup.onEnter(); - } - } - DecomposeEventFromPrefix(_args) { - let search = this.instrumentIdentifier + "_"; - if (_args[0].startsWith(search)) { - return _args[0].slice(search.length); - } - search = this.instrumentAlias; - if (search != null && search != "") { - if (this.urlConfig.index) { - search += "_" + this.urlConfig.index; - } - search += "_"; - if (_args[0].startsWith(search)) { - return _args[0].slice(search.length); - } - } - search = this.templateID + "_"; - if (_args[0].startsWith(search)) { - const evt = _args[0].slice(search.length); - const separator = evt.search("_"); - if (separator >= 0) { - if (!isFinite(parseInt(evt.substring(0, separator)))) { - return evt; - } - } else if (!isFinite(parseInt(evt))) { - return evt; - } - } - search = "Generic_"; - if (_args[0].startsWith(search)) { - return _args[0].slice(search.length); - } - return null; - } - onInteractionEvent(_args) { - if (this.isElectricityAvailable() || SimVar.GetSimVarValue("L:A32NX_ELEC_DC_ESS_BUS_IS_POWERED", "Bool")) { - let event = this.DecomposeEventFromPrefix(_args); - if (event) { - if (event == "ElementSetAttribute" && _args.length >= 4) { - const element = this.getChildById(_args[1]); - if (element) { - Avionics.Utils.diffAndSetAttribute(element, _args[2], _args[3]); - } - } else { - this.computeEvent(event); - for (let i = 0; i < this.eventAliases.length; i++) { - if (this.eventAliases[i].source == event) { - this.computeEvent(this.eventAliases[i].output); - } - } - } - } else if (_args[0].startsWith("NavSystem_")) { - event = _args[0].slice("NavSystem_".length); - this.computeEvent(event); - for (let i = 0; i < this.eventAliases.length; i++) { - if (this.eventAliases[i].source == event) { - this.computeEvent(this.eventAliases[i].output); - } - } - } - } else { - console.log("Electricity Is NOT Available"); - } - } - reboot() { - super.reboot(); - this.startTime = Date.now(); - this.hasBeenOff = false; - this.isStarted = false; - this.initAcknowledged = true; - this.budgetedItemId = 0; - } - Update() { - super.Update(); - this.accumulatedDeltaTime += this.deltaTime; - - if (NavSystem._iterations < 10000) { - NavSystem._iterations += 1; - } - const t0 = performance.now(); - if (this.isElectricityAvailable()) { - if (!this.isStarted) { - this.onPowerOn(); - } - if (this.isBootProcedureComplete()) { - if (this.reversionaryMode) { - if (this.electricity) { - this.electricity.setAttribute("state", "Backup"); - SimVar.SetSimVarValue("L:" + this.instrumentIdentifier + "_ScreenLuminosity", "number", 1); - SimVar.SetSimVarValue("L:" + this.instrumentIdentifier + "_State", "number", 3); - } - } else { - if (this.electricity) { - this.electricity.setAttribute("state", "on"); - SimVar.SetSimVarValue("L:" + this.instrumentIdentifier + "_ScreenLuminosity", "number", 1); - SimVar.SetSimVarValue("L:" + this.instrumentIdentifier + "_State", "number", 2); - } - } - } else if (Date.now() - this.startTime > this.initDuration) { - if (this.electricity) { - this.electricity.setAttribute("state", "initWaitingValidation"); - SimVar.SetSimVarValue("L:" + this.instrumentIdentifier + "_ScreenLuminosity", "number", 0.2); - SimVar.SetSimVarValue("L:" + this.instrumentIdentifier + "_State", "number", 1); - } - } else { - if (this.electricity) { - this.electricity.setAttribute("state", "init"); - SimVar.SetSimVarValue("L:" + this.instrumentIdentifier + "_ScreenLuminosity", "number", 0.2); - SimVar.SetSimVarValue("L:" + this.instrumentIdentifier + "_State", "number", 1); - } - } - } else { - if (this.isStarted) { - this.onShutDown(); - } - if (this.electricity) { - this.electricity.setAttribute("state", "off"); - SimVar.SetSimVarValue("L:" + this.instrumentIdentifier + "_ScreenLuminosity", "number", 0); - SimVar.SetSimVarValue("L:" + this.instrumentIdentifier + "_State", "number", 0); - } - } - this.updateAspectRatio(); - if (this.popUpElement) { - this.popUpElement.onUpdate(this.accumulatedDeltaTime); - } - if (this.pagesContainer) { - this.pagesContainer.setAttribute("state", this.getCurrentPage().htmlElemId); - } - if (this.useUpdateBudget) { - this.updateGroupsWithBudget(); - } else { - this.updateGroups(); - } - switch (this.currentInteractionState) { - case 0: - for (var i = 0; i < this.currentSelectableArray.length; i++) { - this.currentSelectableArray[i].updateSelection(false); - } - break; - case 1: - for (var i = 0; i < this.currentSelectableArray.length; i++) { - if (i == this.cursorIndex) { - this.currentSelectableArray[i].updateSelection(this.blinkGetState(400, 200)); - } else { - this.currentSelectableArray[i].updateSelection(false); - } - } - break; - case 2: - this.currentContextualMenu.Update(this, this.menuMaxElems); - break; - } - try { - this.onUpdate(this.accumulatedDeltaTime); - } catch (e) { - console.log("Uncaught exception", e); - } - const t = performance.now() - t0; - NavSystem.maxTimeUpdateAllTime = Math.max(t, NavSystem.maxTimeUpdateAllTime); - NavSystem.maxTimeUpdate = Math.max(t, NavSystem.maxTimeUpdate); - const factor = 1 / NavSystem._iterations; - NavSystem.mediumTimeUpdate *= (1 - factor); - NavSystem.mediumTimeUpdate += factor * t; - this.accumulatedDeltaTime = 0; - } - updateGroups() { - for (let i = 0; i < this.IndependentsElements.length; i++) { - this.IndependentsElements[i].onUpdate(this.accumulatedDeltaTime); - } - if (!this.overridePage) { - const currentGroup = this.getCurrentPageGroup(); - if (currentGroup) { - currentGroup.onUpdate(this.accumulatedDeltaTime); - } - } else { - this.overridePage.onUpdate(this.accumulatedDeltaTime); - } - } - updateGroupsWithBudget() { - const target = this.budgetedItemId + this.maxUpdateBudget; - while (this.budgetedItemId < target) { - if (this.budgetedItemId < this.IndependentsElements.length) { - this.IndependentsElements[this.budgetedItemId].onUpdate(this.accumulatedDeltaTime); - this.budgetedItemId++; - continue; - } - if (!this.overridePage) { - const currentGroup = this.getCurrentPageGroup(); - if (currentGroup) { - const itemId = this.budgetedItemId - this.IndependentsElements.length; - if (currentGroup.onUpdateSpecificItem(this.accumulatedDeltaTime, itemId)) { - this.budgetedItemId++; - continue; - } - } - } else { - this.overridePage.onUpdate(this.accumulatedDeltaTime); - } - this.budgetedItemId = 0; - break; - } - } - onEvent(_event) { - } - onUpdate(_deltaTime) { - } - GetComActiveFreq() { - return this.frequencyFormat(SimVar.GetSimVarValue("COM ACTIVE FREQUENCY:1", "MHz"), 3); - } - GetComStandbyFreq() { - return this.frequencyFormat(SimVar.GetSimVarValue("COM STANDBY FREQUENCY:1", "MHz"), 3); - } - GetNavActiveFreq() { - return this.frequencyFormat(SimVar.GetSimVarValue("NAV ACTIVE FREQUENCY:1", "MHz"), 2); - } - GetNavStandbyFreq() { - return this.frequencyFormat(SimVar.GetSimVarValue("NAV STANDBY FREQUENCY:1", "MHz"), 2); - } - UpdateSlider(_slider, _cursor, _index, _nbElem, _maxElems) { - if (_nbElem > _maxElems) { - _slider.setAttribute("state", "Active"); - _cursor.setAttribute("style", "height:" + (_maxElems * 100 / _nbElem) + - "%;top:" + (_index * 100 / _nbElem) + "%"); - } else { - _slider.setAttribute("state", "Inactive"); - } - } - frequencyFormat(_frequency, _nbDigits) { - const IntPart = Math.floor(_frequency); - const Digits = Math.round((_frequency - IntPart) * Math.pow(10, _nbDigits)); - return fastToFixed(IntPart, 0) + '.' + ('000' + (Digits)).slice(-_nbDigits) + ''; - } - frequencyListFormat(_airport, _baseId, _maxElem = -1, _firstElemIndex = 0) { - if (_airport && _airport.frequencies) { - let htmlFreq = ""; - let endIndex = _airport.frequencies.length; - if (_maxElem != -1) { - endIndex = Math.min(_airport.frequencies.length, _firstElemIndex + _maxElem); - } - for (let i = 0; i < Math.min(_airport.frequencies.length, _maxElem); i++) { - htmlFreq += '
' + _airport.frequencies[i + _firstElemIndex].name.replace(" ", " ").slice(0, 15) + '
' + this.frequencyFormat(_airport.frequencies[i + _firstElemIndex].mhValue, 3) + '
'; - } - return htmlFreq; - } else { - return ""; - } - } - airportPrivateTypeStrFromEnum(_enum) { - switch (_enum) { - case 0: - return "Unknown"; - case 1: - return "Public"; - case 2: - return "Military"; - case 3: - return "Private"; - } - } - longitudeFormat(_longitude) { - let format = ""; - if (_longitude < 0) { - format += "W"; - _longitude = Math.abs(_longitude); - } else { - format += "E"; - } - const degrees = Math.floor(_longitude); - const minutes = ((_longitude - degrees) * 60); - format += fastToFixed(degrees, 0); - format += "°"; - format += fastToFixed(minutes, 2); - format += "'"; - return format; - } - latitudeFormat(_latitude) { - let format = ""; - if (_latitude < 0) { - format += "S"; - _latitude = Math.abs(_latitude); - } else { - format += "N"; - } - const degrees = Math.floor(_latitude); - const minutes = ((_latitude - degrees) * 60); - format += fastToFixed(degrees, 0); - format += "°"; - format += fastToFixed(minutes, 2); - format += "'"; - return format; - } - InteractionStateOut() { - switch (this.currentInteractionState) { - case 0: - break; - case 1: - for (let i = 0; i < this.currentSelectableArray.length; i++) { - this.currentSelectableArray[i].updateSelection(false); - } - break; - case 2: - this.contextualMenu.setAttribute("state", "Inactive"); - break; - } - } - InteractionStateIn() { - switch (this.currentInteractionState) { - case 0: - break; - case 1: - this.cursorIndex = 0; - break; - case 2: - this.contextualMenu.setAttribute("state", "Active"); - this.contextualMenuDisplayBeginIndex = 0; - this.cursorIndex = 0; - if (this.currentContextualMenu.elements[0].isInactive()) { - this.computeEvent("NavigationSmallInc"); - } - break; - } - } - SwitchToInteractionState(_newState) { - this.InteractionStateOut(); - this.currentInteractionState = _newState; - this.InteractionStateIn(); - } - ShowContextualMenu(_menu) { - this.currentContextualMenu = _menu; - this.SwitchToInteractionState(2); - this.currentContextualMenu.Update(this, this.menuMaxElems); - } - ActiveSelection(_selectables) { - this.SwitchToInteractionState(1); - this.currentSelectableArray = _selectables; - const begin = this.cursorIndex; - while (!this.currentSelectableArray[this.cursorIndex].isActive) { - this.cursorIndex = (this.cursorIndex + 1) % this.currentSelectableArray.length; - if (this.cursorIndex == begin) { - this.SwitchToInteractionState(0); - return; - } - } - } - setOverridePage(_page) { - if (this.overridePage) { - this.overridePage.onExit(); - } - if (this.currentContextualMenu) { - this.SwitchToInteractionState(0); - } - this.overridePage = _page; - this.overridePage.onEnter(); - } - closeOverridePage() { - if (this.overridePage) { - this.overridePage.onExit(); - } - if (this.currentContextualMenu) { - this.SwitchToInteractionState(0); - } - this.overridePage = null; - } - SwitchToPageName(_menu, _page) { - this.lastRelevantICAO = null; - this.lastRelevantICAOType = null; - if (this.overridePage) { - this.closeOverridePage(); - } - if (this.currentEventLinkedPageGroup) { - this.currentEventLinkedPageGroup.pageGroup.onExit(); - this.currentEventLinkedPageGroup = null; - } - this.pageGroups[this.currentPageGroupIndex].onExit(); - if (this.currentContextualMenu) { - this.SwitchToInteractionState(0); - } - for (let i = 0; i < this.pageGroups.length; i++) { - if (this.pageGroups[i].name == _menu) { - this.currentPageGroupIndex = i; - } - } - this.pageGroups[this.currentPageGroupIndex].goToPage(_page, true); - } - SwitchToMenuName(_name) { - this.lastRelevantICAO = null; - this.lastRelevantICAOType = null; - this.pageGroups[this.currentPageGroupIndex].onExit(); - if (this.currentContextualMenu) { - this.SwitchToInteractionState(0); - } - for (let i = 0; i < this.pageGroups.length; i++) { - if (this.pageGroups[i].name == _name) { - this.currentPageGroupIndex = i; - } - } - this.pageGroups[this.currentPageGroupIndex].onEnter(); - } - GetInteractionState() { - return this.currentInteractionState; - } - blinkGetState(_blinkPeriod, _duration) { - return Math.round((new Date().getTime()) / _duration) % (_blinkPeriod / _duration) == 0; - } - IsEditingSearchField() { - return this.GetInteractionState() == 3; - } - OnSearchFieldEndEditing() { - this.SwitchToInteractionState(0); - } - addEventLinkedPageGroup(_event, _pageGroup) { - this.eventLinkedPageGroups.push(new NavSystemEventLinkedPageGroup(_pageGroup, _event)); - } - addEventLinkedPopupWindow(_popUp) { - this.eventLinkedPopUpElements.push(_popUp); - _popUp.gps = this; - } - addIndependentElementContainer(_container) { - _container.setGPS(this); - this.IndependentsElements.push(_container); - } - getCurrentPageGroup() { - if (this.currentEventLinkedPageGroup) { - return this.currentEventLinkedPageGroup.pageGroup; - } else { - return this.pageGroups[this.currentPageGroupIndex]; - } - } - getCurrentPage() { - if (!this.overridePage) { - const currentGroup = this.getCurrentPageGroup(); - if (currentGroup) { - return currentGroup.getCurrentPage(); - } - return undefined; - } else { - return this.overridePage; - } - } - leaveEventPage() { - this.lastRelevantICAO = null; - this.lastRelevantICAOType = null; - this.currentEventLinkedPageGroup.pageGroup.onExit(); - this.currentEventLinkedPageGroup = null; - this.getCurrentPageGroup().onEnter(); - } - addEventAlias(_source, _output) { - this.eventAliases.push(new NavSystemEventAlias(_source, _output)); - } - closePopUpElement() { - let callback = null; - if (this.popUpElement) { - callback = this.popUpCloseCallback; - this.popUpElement.onExit(); - } - this.popUpElement = null; - this.popUpCloseCallback = null; - if (this.currentContextualMenu) { - this.SwitchToInteractionState(0); - } - if (callback) { - callback(); - } - ; - } - getElementOfType(c) { - for (let i = 0; i < this.IndependentsElements.length; i++) { - const elem = this.IndependentsElements[i].getElementOfType(c); - if (elem) { - return elem; - } - } - const curr = this.getCurrentPage().element.getElementOfType(c); - if (curr) { - return curr; - } else { - for (let i = 0; i < this.pageGroups.length; i++) { - for (let j = 0; j < this.pageGroups[i].pages.length; j++) { - const elem = this.pageGroups[i].pages[j].getElementOfType(c); - if (elem) { - return elem; - } - } - } - } - return null; - } - switchToPopUpPage(_pageContainer, _PopUpCloseCallback = null) { - if (this.popUpElement) { - this.popUpElement.onExit(); - if (this.popUpCloseCallback) { - this.popUpCloseCallback(); - } - ; - } - if (this.currentContextualMenu) { - this.SwitchToInteractionState(0); - } - this.popUpCloseCallback = _PopUpCloseCallback; - this.popUpElement = _pageContainer; - this.popUpElement.onEnter(); - } - preserveAspectRatio(_HtmlElementId) { - this.aspectRatioElement = _HtmlElementId; - this.forcedAspectRatioSet = false; - this.updateAspectRatio(); - } - updateAspectRatio() { - if (this.forcedAspectRatioSet) { - return; - } - if (this.aspectRatioElement == null) { - return; - } - const frame = this.getChildById(this.aspectRatioElement); - if (!frame) { - return; - } - const vpRect = this.getBoundingClientRect(); - const vpWidth = vpRect.width; - const vpHeight = vpRect.height; - if (vpWidth <= 0 || vpHeight <= 0) { - return; - } - const frameStyle = window.getComputedStyle(frame); - if (!frameStyle) { - return; - } - const refWidth = parseInt(frameStyle.getPropertyValue('--refWidth')); - const refHeight = parseInt(frameStyle.getPropertyValue('--refHeight')); - const curWidth = parseInt(frameStyle.width); - const curHeight = parseInt(frameStyle.height); - const curRatio = curHeight / curWidth; - let refRatio = curRatio; - if (refWidth > 0 && refHeight > 0) { - console.log("Forcing aspectratio to " + refWidth + "*" + refHeight); - refRatio = refHeight / refWidth; - let newLeft = parseInt(frameStyle.left); - let newWidth = curWidth; - let newHeight = curWidth * refRatio; - if (newHeight > vpHeight) { - newWidth = vpHeight / refRatio; - newHeight = vpHeight; - newLeft += (curWidth - newWidth) * 0.5; - } - newLeft = Math.round(newLeft); - newWidth = Math.round(newWidth); - newHeight = Math.round(newHeight); - frame.style.left = newLeft + "px"; - frame.style.width = newWidth + "px"; - frame.style.height = newHeight + "px"; - window.document.documentElement.style.setProperty("--bodyHeightScale", (newHeight / vpHeight).toString()); - } - this.forcedScreenRatio = 1.0 / curRatio; - this.forcedAspectRatio = 1.0 / refRatio; - this.forcedAspectRatioSet = true; - } - isAspectRatioForced() { - return this.forcedAspectRatioSet; - } - getForcedScreenRatio() { - return this.forcedScreenRatio; - } - getForcedAspectRatio() { - return this.forcedAspectRatio; - } - isComputingAspectRatio() { - if (this.aspectRatioElement != null && !this.forcedAspectRatioSet) { - return false; - } - return true; - } - onSoundEnd(_eventId) { - for (let i = 0; i < this.pageGroups.length; i++) { - for (let j = 0; j < this.pageGroups[i].pages.length; j++) { - this.pageGroups[i].pages[j].onSoundEnd(_eventId); - } - } - for (let i = 0; i < this.IndependentsElements.length; i++) { - this.IndependentsElements[i].onSoundEnd(_eventId); - } - for (let i = 0; i < this.eventLinkedPopUpElements.length; i++) { - this.eventLinkedPopUpElements[i].onSoundEnd(_eventId); - } - } - onShutDown() { - console.log("System Turned Off"); - this.hasBeenOff = true; - this.isStarted = false; - this.initAcknowledged = false; - for (let i = 0; i < this.pageGroups.length; i++) { - for (let j = 0; j < this.pageGroups[i].pages.length; j++) { - this.pageGroups[i].pages[j].onShutDown(); - } - } - for (let i = 0; i < this.IndependentsElements.length; i++) { - this.IndependentsElements[i].onShutDown(); - } - for (let i = 0; i < this.eventLinkedPopUpElements.length; i++) { - this.eventLinkedPopUpElements[i].onShutDown(); - } - this.alwaysUpdateList.splice(0, this.alwaysUpdateList.length); - } - onPowerOn() { - console.log("System Turned ON"); - this.startTime = Date.now(); - this.isStarted = true; - this.budgetedItemId = 0; - for (let i = 0; i < this.pageGroups.length; i++) { - for (let j = 0; j < this.pageGroups[i].pages.length; j++) { - this.pageGroups[i].pages[j].onPowerOn(); - } - } - for (let i = 0; i < this.IndependentsElements.length; i++) { - this.IndependentsElements[i].onPowerOn(); - } - for (let i = 0; i < this.eventLinkedPopUpElements.length; i++) { - this.eventLinkedPopUpElements[i].onPowerOn(); - } - } - isBootProcedureComplete() { - if (((Date.now() - this.startTime > this.initDuration) || !this.hasBeenOff) && (this.initAcknowledged || !this.needValidationAfterInit)) { - return true; - } - return false; - } - acknowledgeInit() { - this.initAcknowledged = true; - } - isInReversionaryMode() { - return this.reversionaryMode; - } - wasTurnedOff() { - return this.hasBeenOff; - } - hasWeatherRadar() { - if (this.instrumentXmlConfig) { - const elem = this.instrumentXmlConfig.getElementsByTagName("WeatherRadar"); - if (elem.length > 0 && (elem[0].textContent.toLowerCase() == "off" || elem[0].textContent.toLowerCase() == "none")) { - return false; - } - } - return true; - } - alwaysUpdate(_element, _val) { - for (let i = 0; i < this.alwaysUpdateList.length; i++) { - if (this.alwaysUpdateList[i] == _element) { - if (!_val) { - this.alwaysUpdateList.splice(i, 1); - } - return; - } - } - if (_val) { - this.alwaysUpdateList.push(_element); - } - } -} -NavSystem.maxTimeUpdateAllTime = 0; -NavSystem.maxTimeUpdate = 0; -NavSystem.mediumMaxTimeUpdate = 0; -NavSystem.mediumTimeUpdate = 0; -NavSystem.maxTimeMapUpdateAllTime = 0; -NavSystem.maxTimeMapUpdate = 0; -NavSystem.mediumMaxTimeMapUpdate = 0; -NavSystem.mediumTimeMapUpdate = 0; -NavSystem._iterations = 0; -class NavSystemPageGroup { - constructor(_name, _gps, _pages) { - this._updatingWithBudget = false; - this.name = _name; - this.gps = _gps; - this.pages = _pages; - this.pageIndex = 0; - for (let i = 0; i < _pages.length; i++) { - _pages[i].pageGroup = this; - _pages[i].gps = this.gps; - } - } - getCurrentPage() { - return this.pages[this.pageIndex]; - } - onEnter() { - this.pages[this.pageIndex].onEnter(); - } - onUpdate(_deltaTime) { - if (!this._updatingWithBudget) { - this.pages[this.pageIndex].onUpdate(_deltaTime); - } - } - onUpdateSpecificItem(_deltaTime, _itemId) { - if (_itemId == 0) { - this._updatingWithBudget = true; - { - this.onUpdate(_deltaTime); - } - this._updatingWithBudget = false; - } - return this.pages[this.pageIndex].onUpdateSpecificItem(_deltaTime, _itemId); - } - onExit() { - this.pages[this.pageIndex].onExit(); - } - nextPage() { - if (this.pages.length > 1) { - this.pages[this.pageIndex].onExit(); - this.pageIndex = (this.pageIndex + 1) % this.pages.length; - this.pages[this.pageIndex].onEnter(); - } - } - prevPage() { - if (this.pages.length > 1) { - this.pages[this.pageIndex].onExit(); - this.pageIndex = (this.pageIndex + this.pages.length - 1) % this.pages.length; - this.pages[this.pageIndex].onEnter(); - } - } - goToPage(_name, _skipExit = false) { - if (!_skipExit) { - this.pages[this.pageIndex].onExit(); - } - for (let i = 0; i < this.pages.length; i++) { - if (this.pages[i].name == _name) { - this.pageIndex = i; - } - } - this.onEnter(); - } -} -class NavSystemEventLinkedPageGroup { - constructor(_pageGroup, _event) { - this.pageGroup = _pageGroup; - this.popUpEvent = _event; - } -} -class NavSystemElementContainer { - constructor(_name, _htmlElemId, _element) { - this.name = ""; - this.htmlElemId = ""; - this.isInitialized = false; - this._updatingWithBudget = false; - this.name = _name; - this.htmlElemId = _htmlElemId; - this.element = _element; - if (_element) { - _element.container = this; - } - } - getDefaultMenu() { - return this.defaultMenu; - } - init() { - } - checkInit() { - if (this.element) { - if (this.element.isReady()) { - if (!this.element.isInitialized) { - this.element.container = this; - this.element.setGPS(this.gps); - this.element.init(this.gps.getChildById(this.htmlElemId)); - this.element.isInitialized = true; - } - } else { - return false; - } - } - if (!this.isInitialized) { - this.init(); - this.isInitialized = true; - } - return this.isInitialized; - } - onEnter() { - if (!this.checkInit()) { - return; - } - if (this.element) { - this.element.onEnter(); - } - } - onUpdate(_deltaTime) { - if (!this._updatingWithBudget) { - if (!this.checkInit()) { - return; - } - if (this.element) { - this.element.onUpdate(_deltaTime); - } - } - } - onUpdateSpecificItem(_deltaTime, _itemId) { - if (!this.checkInit()) { - return; - } - if (_itemId == 0) { - this._updatingWithBudget = true; - { - this.onUpdate(_deltaTime); - } - this._updatingWithBudget = false; - } - if (this.element) { - return this.element.onUpdateSpecificItem(_deltaTime, _itemId); - } - return false; - } - onExit() { - if (this.element) { - this.element.onExit(); - } - } - onEvent(_event) { - if (this.element) { - this.element.onEvent(_event); - } - } - onSoundEnd(_eventId) { - if (this.element) { - this.element.onSoundEnd(_eventId); - } - } - onShutDown() { - if (this.element) { - this.element.onShutDown(); - } - } - onPowerOn() { - if (this.element) { - this.element.onPowerOn(); - } - } - setGPS(_gps) { - this.gps = _gps; - if (this.element) { - this.element.setGPS(_gps); - } - } - getElementOfType(c) { - if (this.element) { - return this.element.getElementOfType(c); - } - return null; - } -} -class NavSystemEventLinkedPopUpWindow extends NavSystemElementContainer { - constructor(_name, _htmlElemId, _element, _popUpEvent) { - super(_name, _htmlElemId, _element); - this.popUpEvent = _popUpEvent; - } -} -class SoftKeyHandler { -} -class NavSystemPage extends NavSystemElementContainer { - constructor() { - super(...arguments); - this.softKeys = new SoftKeysMenu(); - } - getSoftKeyMenu() { - return this.softKeys; - } -} -// class Updatable { -// } -class NavSystemElement extends Updatable { - constructor() { - super(...arguments); - this.isInitialized = false; - this.defaultSelectables = []; - this._alwaysUpdate = false; - } - set alwaysUpdate(_val) { - this._alwaysUpdate = _val; - if (this.gps) { - this.gps.alwaysUpdate(this, _val); - } - } - isReady() { - return true; - } - onSoundEnd(_eventId) { - } - onShutDown() { - } - onPowerOn() { - } - onUpdateSpecificItem(_deltaTime, _itemId) { - if (_itemId == 0) { - this.onUpdate(_deltaTime); - } - return false; - } - getDefaultSelectables() { - return this.defaultSelectables; - } - setGPS(_gps) { - if (this.gps && !_gps && this._alwaysUpdate) { - this.gps.alwaysUpdate(this, false); - } - this.gps = _gps; - if (this.gps) { - this.gps.alwaysUpdate(this, this._alwaysUpdate); - } - } - getElementOfType(c) { - if (this instanceof c) { - return this; - } else { - return null; - } - } - redraw() { - } -} -class NavSystemIFrameElement extends NavSystemElement { - constructor(_frameName) { - super(); - this.iFrame = this.gps.getChildById(_frameName); - } - isReady() { - if (this.iFrame) { - this.canvas = this.iFrame.contentWindow; - if (this.canvas) { - const readyToSet = this.canvas["readyToSet"]; - if (readyToSet) { - return true; - } - } - } - return false; - } -} -class NavSystemElementGroup extends NavSystemElement { - constructor(_elements) { - super(); - this._updatingWithBudget = false; - this.elements = _elements; - } - isReady() { - for (let i = 0; i < this.elements.length; i++) { - if (!this.elements[i].isReady()) { - return false; - } - } - return true; - } - init(_root) { - this.defaultSelectables = []; - for (let i = 0; i < this.elements.length; i++) { - if (!this.elements[i].isInitialized) { - this.elements[i].container = this.container; - this.elements[i].setGPS(this.gps); - this.elements[i].init(_root); - this.elements[i].isInitialized = true; - this.defaultSelectables.concat(this.elements[i].getDefaultSelectables()); - } - } - } - onEnter() { - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].onEnter(); - } - } - onUpdate(_deltaTime) { - if (!this._updatingWithBudget) { - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].onUpdate(_deltaTime); - } - } - } - onUpdateSpecificItem(_deltaTime, _itemId) { - if (_itemId == 0) { - this._updatingWithBudget = true; - { - this.onUpdate(_deltaTime); - } - this._updatingWithBudget = false; - } - if (_itemId < this.elements.length) { - this.elements[_itemId].onUpdate(_deltaTime); - if (_itemId + 1 < this.elements.length) { - return true; - } - } - return false; - } - onExit() { - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].onExit(); - } - } - onEvent(_event) { - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].onEvent(_event); - } - } - onSoundEnd(_eventId) { - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].onSoundEnd(_eventId); - } - } - onShutDown() { - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].onShutDown(); - } - } - onPowerOn() { - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].onPowerOn(); - } - } - getDefaultSelectables() { - this.defaultSelectables = []; - for (let i = 0; i < this.elements.length; i++) { - this.defaultSelectables.concat(this.elements[i].getDefaultSelectables()); - } - return this.defaultSelectables; - } - setGPS(_gps) { - this.gps = _gps; - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].setGPS(_gps); - } - } - getElementOfType(c) { - for (let i = 0; i < this.elements.length; i++) { - const elem = this.elements[i].getElementOfType(c); - if (elem) { - return elem; - } - } - return null; - } - addElement(elem) { - this.elements.push(elem); - } -} -class NavSystemElementSelector extends NavSystemElement { - constructor() { - super(...arguments); - this.elements = []; - this.index = 0; - } - isReady() { - for (let i = 0; i < this.elements.length; i++) { - if (!this.elements[i].isReady()) { - return false; - } - } - return true; - } - init(_root) { - for (let i = 0; i < this.elements.length; i++) { - if (!this.elements[i].isInitialized) { - this.elements[i].container = this.container; - this.elements[i].setGPS(this.gps); - this.elements[i].init(_root); - this.elements[i].isInitialized = true; - } - } - } - onEnter() { - this.elements[this.index].onEnter(); - } - onUpdate(_deltaTime) { - this.elements[this.index].onUpdate(_deltaTime); - } - onExit() { - this.elements[this.index].onExit(); - } - onEvent(_event) { - this.elements[this.index].onEvent(_event); - } - onSoundEnd(_eventId) { - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].onSoundEnd(_eventId); - } - } - onShutDown() { - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].onShutDown(); - } - } - onPowerOn() { - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].onPowerOn(); - } - } - selectElement(_name) { - for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i].name == _name) { - this.index = i; - } - } - } - addElement(_elem) { - this.elements.push(_elem); - } - getDefaultSelectables() { - return this.elements[this.index].getDefaultSelectables(); - } - setGPS(_gps) { - this.gps = _gps; - for (let i = 0; i < this.elements.length; i++) { - this.elements[i].setGPS(_gps); - } - } - getElementOfType(c) { - for (let i = 0; i < this.elements.length; i++) { - const elem = this.elements[i].getElementOfType(c); - if (elem) { - return elem; - } - } - return null; - } - switchToIndex(_index) { - if (this.index < this.elements.length) { - this.elements[this.index].onExit(); - } - if (_index < this.elements.length) { - this.index = _index; - this.elements[this.index].onEnter(); - } - } -} -class SoftKeyElement { - constructor(_name = "", _callback = null, _stateCB = null) { - this.callback = _callback; - this.name = _name; - this.state = "None"; - this.stateCallback = _stateCB; - if (!_callback) { - this.state = "Greyed"; - } - } -} -class SoftKeysMenu { -} -class NavSystemEventAlias { - constructor(_from, _to) { - this.source = _from; - this.output = _to; - } -} -class CDIElement extends NavSystemElement { - init(_root) { - this.cdiCursor = this.gps.getChildById("CDICursor"); - this.toFrom = this.gps.getChildById("ToFrom"); - } - onEnter() { - } - onUpdate(_deltaTime) { - const CTD = SimVar.GetSimVarValue("GPS WP CROSS TRK", "nautical mile"); - this.cdiCursor.setAttribute("style", "left:" + ((CTD <= -1 ? -1 : CTD >= 1 ? 1 : CTD) * 50 + 50) + "%"); - } - onExit() { - } - onEvent(_event) { - } -} -var EMapDisplayMode; -(function (EMapDisplayMode) { - EMapDisplayMode[EMapDisplayMode["GPS"] = 0] = "GPS"; - EMapDisplayMode[EMapDisplayMode["RADAR"] = 1] = "RADAR"; -})(EMapDisplayMode || (EMapDisplayMode = {})); -var ERadarMode; -(function (ERadarMode) { - ERadarMode[ERadarMode["HORIZON"] = 0] = "HORIZON"; - ERadarMode[ERadarMode["VERTICAL"] = 1] = "VERTICAL"; -})(ERadarMode || (ERadarMode = {})); -class MapInstrumentElement extends NavSystemElement { - constructor() { - super(); - this.displayMode = EMapDisplayMode.GPS; - this.nexradOn = false; - this.radarMode = ERadarMode.HORIZON; - } - init(root) { - this.instrument = root.querySelector("map-instrument"); - if (this.instrument) { - TemplateElement.callNoBinding(this.instrument, () => { - this.onTemplateLoaded(); - }); - } - } - onTemplateLoaded() { - this.instrument.init(this.gps); - this.instrumentLoaded = true; - // This makes the black parts of the bing map blend perfectly with the backlight in weather and terrain modes - this.instrument.bingMap.m_imgElement.style.mixBlendMode = 'lighten'; - } - setGPS(_gps) { - super.setGPS(_gps); - if (this.instrument) { - this.instrument.init(this.gps); - } - } - onEnter() { - } - onUpdate(_deltaTime) { - if (this.instrumentLoaded) { - this.instrument.update(_deltaTime); - if (this.weatherTexts) { - const range = this.instrument.getWeatherRange(); - const ratio = 1.0 / this.weatherTexts.length; - for (let i = 0; i < this.weatherTexts.length; i++) { - this.weatherTexts[i].textContent = fastToFixed(range * ratio * (i + 1), 2) + "NM"; - } - } - } - } - onExit() { - } - onEvent(_event) { - if (this.instrument) { - this.instrument.onEvent(_event); - } - } - toggleDisplayMode() { - if (this.displayMode == EMapDisplayMode.GPS) { - this.gps.getCurrentPage().name = "WEATHER RADAR"; - this.displayMode = EMapDisplayMode.RADAR; - } else { - this.gps.getCurrentPage().name = "NAVIGATION MAP"; - this.displayMode = EMapDisplayMode.GPS; - } - this.updateWeather(); - } - setDisplayMode(_mode) { - this.displayMode = _mode; - if (_mode == EMapDisplayMode.GPS) { - this.gps.getCurrentPage().name = "NAVIGATION MAP"; - } else { - this.gps.getCurrentPage().name = "WEATHER RADAR"; - } - this.updateWeather(); - } - getDisplayMode() { - return this.displayMode; - } - toggleIsolines() { - if (this.instrument) { - if (this.instrument.getIsolines() == true) { - this.instrument.showIsolines(false); - } else { - this.instrument.showIsolines(true); - } - } - } - getIsolines() { - return this.instrument.getIsolines(); - } - toggleNexrad() { - this.nexradOn = !this.nexradOn; - this.updateWeather(); - } - getNexrad() { - return this.nexradOn; - } - setRadar(_mode) { - this.radarMode = _mode; - this.updateWeather(); - } - getRadarMode() { - return this.radarMode; - } - updateWeather() { - if (this.instrument) { - if (this.displayMode == EMapDisplayMode.GPS) { - if (this.nexradOn) { - this.setWeather(EWeatherRadar.TOPVIEW); - } else { - this.setWeather(EWeatherRadar.OFF); - } - } else { - if (this.radarMode == ERadarMode.HORIZON) { - this.setWeather(EWeatherRadar.HORIZONTAL); - } else { - this.setWeather(EWeatherRadar.VERTICAL); - } - } - } - } - setWeather(_mode) { - this.instrument.showWeather(_mode); - const svgRoot = this.instrument.weatherSVG; - if (svgRoot) { - Utils.RemoveAllChildren(svgRoot); - this.weatherTexts = null; - if (_mode == EWeatherRadar.HORIZONTAL || _mode == EWeatherRadar.VERTICAL) { - const circleRadius = 575; - const dashNbRect = 10; - const dashWidth = 8; - const dashHeight = 6; - if (_mode == EWeatherRadar.HORIZONTAL) { - this.instrument.setBingMapStyle("10.3%", "-13.3%", "127%", "157%"); - var coneAngle = 90; - svgRoot.setAttribute("viewBox", "0 0 400 400"); - var trsGroup = document.createElementNS(Avionics.SVG.NS, "g"); - trsGroup.setAttribute("transform", "translate(-125, 29) scale(1.63)"); - svgRoot.appendChild(trsGroup); - const viewBox = document.createElementNS(Avionics.SVG.NS, "svg"); - viewBox.setAttribute("viewBox", "-600 -600 1200 1200"); - trsGroup.appendChild(viewBox); - var circleGroup = document.createElementNS(Avionics.SVG.NS, "g"); - circleGroup.setAttribute("id", "Circles"); - viewBox.appendChild(circleGroup); - { - const rads = [0.25, 0.50, 0.75, 1.0]; - for (let r = 0; r < rads.length; r++) { - const rad = circleRadius * rads[r]; - let startDegrees = -coneAngle * 0.5; - const endDegrees = coneAngle * 0.5; - while (Math.floor(startDegrees) <= endDegrees) { - const line = document.createElementNS(Avionics.SVG.NS, "rect"); - const degree = (180 + startDegrees + 0.5); - line.setAttribute("x", "0"); - line.setAttribute("y", rad.toString()); - line.setAttribute("width", dashWidth.toString()); - line.setAttribute("height", dashHeight.toString()); - line.setAttribute("transform", "rotate(" + degree + " 0 0)"); - line.setAttribute("fill", "white"); - circleGroup.appendChild(line); - startDegrees += coneAngle / dashNbRect; - } - } - } - var lineGroup = document.createElementNS(Avionics.SVG.NS, "g"); - lineGroup.setAttribute("id", "Lines"); - viewBox.appendChild(lineGroup); - { - var coneStart = 180 - coneAngle * 0.5; - var coneStartLine = document.createElementNS(Avionics.SVG.NS, "line"); - coneStartLine.setAttribute("x1", "0"); - coneStartLine.setAttribute("y1", "0"); - coneStartLine.setAttribute("x2", "0"); - coneStartLine.setAttribute("y2", circleRadius.toString()); - coneStartLine.setAttribute("transform", "rotate(" + coneStart + " 0 0)"); - coneStartLine.setAttribute("stroke", "white"); - coneStartLine.setAttribute("stroke-width", "3"); - lineGroup.appendChild(coneStartLine); - var coneEnd = 180 + coneAngle * 0.5; - var coneEndLine = document.createElementNS(Avionics.SVG.NS, "line"); - coneEndLine.setAttribute("x1", "0"); - coneEndLine.setAttribute("y1", "0"); - coneEndLine.setAttribute("x2", "0"); - coneEndLine.setAttribute("y2", circleRadius.toString()); - coneEndLine.setAttribute("transform", "rotate(" + coneEnd + " 0 0)"); - coneEndLine.setAttribute("stroke", "white"); - coneEndLine.setAttribute("stroke-width", "3"); - lineGroup.appendChild(coneEndLine); - } - var textGroup = document.createElementNS(Avionics.SVG.NS, "g"); - textGroup.setAttribute("id", "Texts"); - viewBox.appendChild(textGroup); - { - this.weatherTexts = []; - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.setAttribute("x", "100"); - text.setAttribute("y", "-85"); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "20"); - textGroup.appendChild(text); - this.weatherTexts.push(text); - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.setAttribute("x", "200"); - text.setAttribute("y", "-185"); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "20"); - textGroup.appendChild(text); - this.weatherTexts.push(text); - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.setAttribute("x", "300"); - text.setAttribute("y", "-285"); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "20"); - textGroup.appendChild(text); - this.weatherTexts.push(text); - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.setAttribute("x", "400"); - text.setAttribute("y", "-385"); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "20"); - textGroup.appendChild(text); - this.weatherTexts.push(text); - } - } else if (_mode == EWeatherRadar.VERTICAL) { - this.instrument.setBingMapStyle("-75%", "-88%", "201%", "250%"); - var coneAngle = 51.43; - svgRoot.setAttribute("viewBox", "0 0 400 400"); - var trsGroup = document.createElementNS(Avionics.SVG.NS, "g"); - trsGroup.setAttribute("transform", "translate(402, -190) scale(1.95) rotate(90)"); - svgRoot.appendChild(trsGroup); - const viewBox = document.createElementNS(Avionics.SVG.NS, "svg"); - viewBox.setAttribute("viewBox", "-600 -600 1200 1200"); - trsGroup.appendChild(viewBox); - var circleGroup = document.createElementNS(Avionics.SVG.NS, "g"); - circleGroup.setAttribute("id", "Circles"); - viewBox.appendChild(circleGroup); - { - const rads = [0.25, 0.50, 0.75, 1.0]; - for (let r = 0; r < rads.length; r++) { - const rad = circleRadius * rads[r]; - let startDegrees = -coneAngle * 0.5; - const endDegrees = coneAngle * 0.5; - while (Math.floor(startDegrees) <= endDegrees) { - const line = document.createElementNS(Avionics.SVG.NS, "rect"); - const degree = (180 + startDegrees + 0.5); - line.setAttribute("x", "0"); - line.setAttribute("y", rad.toString()); - line.setAttribute("width", dashWidth.toString()); - line.setAttribute("height", dashHeight.toString()); - line.setAttribute("transform", "rotate(" + degree + " 0 0)"); - line.setAttribute("fill", "white"); - circleGroup.appendChild(line); - startDegrees += coneAngle / dashNbRect; - } - } - } - const limitGroup = document.createElementNS(Avionics.SVG.NS, "g"); - limitGroup.setAttribute("id", "Limits"); - viewBox.appendChild(limitGroup); - { - const endPosY = circleRadius + 50; - let posX = -130; - let posY = 50; - while (posY <= endPosY) { - const line = document.createElementNS(Avionics.SVG.NS, "rect"); - line.setAttribute("x", posX.toString()); - line.setAttribute("y", (-posY).toString()); - line.setAttribute("width", dashHeight.toString()); - line.setAttribute("height", dashWidth.toString()); - line.setAttribute("fill", "white"); - limitGroup.appendChild(line); - posY += dashWidth * 2; - } - posX = 130; - posY = 50; - while (posY <= endPosY) { - const line = document.createElementNS(Avionics.SVG.NS, "rect"); - line.setAttribute("x", posX.toString()); - line.setAttribute("y", (-posY).toString()); - line.setAttribute("width", dashHeight.toString()); - line.setAttribute("height", dashWidth.toString()); - line.setAttribute("fill", "white"); - limitGroup.appendChild(line); - posY += dashWidth * 2; - } - } - var lineGroup = document.createElementNS(Avionics.SVG.NS, "g"); - lineGroup.setAttribute("id", "Lines"); - viewBox.appendChild(lineGroup); - { - var coneStart = 180 - coneAngle * 0.5; - var coneStartLine = document.createElementNS(Avionics.SVG.NS, "line"); - coneStartLine.setAttribute("x1", "0"); - coneStartLine.setAttribute("y1", "0"); - coneStartLine.setAttribute("x2", "0"); - coneStartLine.setAttribute("y2", circleRadius.toString()); - coneStartLine.setAttribute("transform", "rotate(" + coneStart + " 0 0)"); - coneStartLine.setAttribute("stroke", "white"); - coneStartLine.setAttribute("stroke-width", "3"); - lineGroup.appendChild(coneStartLine); - var coneEnd = 180 + coneAngle * 0.5; - var coneEndLine = document.createElementNS(Avionics.SVG.NS, "line"); - coneEndLine.setAttribute("x1", "0"); - coneEndLine.setAttribute("y1", "0"); - coneEndLine.setAttribute("x2", "0"); - coneEndLine.setAttribute("y2", circleRadius.toString()); - coneEndLine.setAttribute("transform", "rotate(" + coneEnd + " 0 0)"); - coneEndLine.setAttribute("stroke", "white"); - coneEndLine.setAttribute("stroke-width", "3"); - lineGroup.appendChild(coneEndLine); - } - var textGroup = document.createElementNS(Avionics.SVG.NS, "g"); - textGroup.setAttribute("id", "Texts"); - viewBox.appendChild(textGroup); - { - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = "+60000FT"; - text.setAttribute("x", "50"); - text.setAttribute("y", "-150"); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "20"); - text.setAttribute("transform", "rotate(-90)"); - textGroup.appendChild(text); - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = "-60000FT"; - text.setAttribute("x", "50"); - text.setAttribute("y", "160"); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "20"); - text.setAttribute("transform", "rotate(-90)"); - textGroup.appendChild(text); - this.weatherTexts = []; - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.setAttribute("x", "85"); - text.setAttribute("y", "85"); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "18"); - text.setAttribute("transform", "rotate(-90)"); - textGroup.appendChild(text); - this.weatherTexts.push(text); - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.setAttribute("x", "215"); - text.setAttribute("y", "160"); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "18"); - text.setAttribute("transform", "rotate(-90)"); - textGroup.appendChild(text); - this.weatherTexts.push(text); - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.setAttribute("x", "345"); - text.setAttribute("y", "220"); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "18"); - text.setAttribute("transform", "rotate(-90)"); - textGroup.appendChild(text); - this.weatherTexts.push(text); - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.setAttribute("x", "475"); - text.setAttribute("y", "280"); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "18"); - text.setAttribute("transform", "rotate(-90)"); - textGroup.appendChild(text); - this.weatherTexts.push(text); - } - } - const legendGroup = document.createElementNS(Avionics.SVG.NS, "g"); - legendGroup.setAttribute("id", "legendGroup"); - svgRoot.appendChild(legendGroup); - { - const x = -5; - const y = 325; - const w = 70; - const h = 125; - const titleHeight = 20; - const scaleOffsetX = 5; - const scaleOffsetY = 5; - const scaleWidth = 13; - const scaleHeight = 24; - const left = x - w * 0.5; - const top = y - h * 0.5; - let rect = document.createElementNS(Avionics.SVG.NS, "rect"); - rect.setAttribute("x", left.toString()); - rect.setAttribute("y", top.toString()); - rect.setAttribute("width", w.toString()); - rect.setAttribute("height", h.toString()); - rect.setAttribute("stroke", "white"); - rect.setAttribute("stroke-width", "2"); - rect.setAttribute("stroke-opacity", "1"); - legendGroup.appendChild(rect); - rect = document.createElementNS(Avionics.SVG.NS, "rect"); - rect.setAttribute("x", left.toString()); - rect.setAttribute("y", top.toString()); - rect.setAttribute("width", w.toString()); - rect.setAttribute("height", titleHeight.toString()); - rect.setAttribute("stroke", "white"); - rect.setAttribute("stroke-width", "2"); - rect.setAttribute("stroke-opacity", "1"); - legendGroup.appendChild(rect); - var text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = "SCALE"; - text.setAttribute("x", x.toString()); - text.setAttribute("y", (top + titleHeight * 0.5).toString()); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "11"); - text.setAttribute("text-anchor", "middle"); - legendGroup.appendChild(text); - let scaleIndex = 0; - rect = document.createElementNS(Avionics.SVG.NS, "rect"); - rect.setAttribute("x", (left + scaleOffsetX).toString()); - rect.setAttribute("y", (top + titleHeight + scaleOffsetY + scaleIndex * scaleHeight).toString()); - rect.setAttribute("width", scaleWidth.toString()); - rect.setAttribute("height", scaleHeight.toString()); - rect.setAttribute("fill", "red"); - rect.setAttribute("stroke", "white"); - rect.setAttribute("stroke-width", "2"); - rect.setAttribute("stroke-opacity", "1"); - legendGroup.appendChild(rect); - text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = "HEAVY"; - text.setAttribute("x", (left + scaleOffsetX + scaleWidth + 5).toString()); - text.setAttribute("y", (top + titleHeight + scaleOffsetY + scaleIndex * scaleHeight + scaleHeight * 0.5).toString()); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "11"); - legendGroup.appendChild(text); - scaleIndex++; - rect = document.createElementNS(Avionics.SVG.NS, "rect"); - rect.setAttribute("x", (left + scaleOffsetX).toString()); - rect.setAttribute("y", (top + titleHeight + scaleOffsetY + scaleIndex * scaleHeight).toString()); - rect.setAttribute("width", scaleWidth.toString()); - rect.setAttribute("height", scaleHeight.toString()); - rect.setAttribute("fill", "yellow"); - rect.setAttribute("stroke", "white"); - rect.setAttribute("stroke-width", "2"); - rect.setAttribute("stroke-opacity", "1"); - legendGroup.appendChild(rect); - scaleIndex++; - rect = document.createElementNS(Avionics.SVG.NS, "rect"); - rect.setAttribute("x", (left + scaleOffsetX).toString()); - rect.setAttribute("y", (top + titleHeight + scaleOffsetY + scaleIndex * scaleHeight).toString()); - rect.setAttribute("width", scaleWidth.toString()); - rect.setAttribute("height", scaleHeight.toString()); - rect.setAttribute("fill", "green"); - rect.setAttribute("stroke", "white"); - rect.setAttribute("stroke-width", "2"); - rect.setAttribute("stroke-opacity", "1"); - legendGroup.appendChild(rect); - text = document.createElementNS(Avionics.SVG.NS, "text"); - text.textContent = "LIGHT"; - text.setAttribute("x", (left + scaleOffsetX + scaleWidth + 5).toString()); - text.setAttribute("y", (top + titleHeight + scaleOffsetY + scaleIndex * scaleHeight + scaleHeight * 0.5).toString()); - text.setAttribute("fill", "white"); - text.setAttribute("font-size", "11"); - legendGroup.appendChild(text); - scaleIndex++; - rect = document.createElementNS(Avionics.SVG.NS, "rect"); - rect.setAttribute("x", (left + scaleOffsetX).toString()); - rect.setAttribute("y", (top + titleHeight + scaleOffsetY + scaleIndex * scaleHeight).toString()); - rect.setAttribute("width", scaleWidth.toString()); - rect.setAttribute("height", scaleHeight.toString()); - rect.setAttribute("fill", "black"); - rect.setAttribute("stroke", "white"); - rect.setAttribute("stroke-width", "2"); - rect.setAttribute("stroke-opacity", "1"); - legendGroup.appendChild(rect); - } - } - } - } -} -class SoftKeyHtmlElement { - constructor(_elem) { - this.Element = _elem; - this.Value = ""; - } - fillFromElement(_elem) { - const val = _elem.name; - if (this.Value != val) { - this.Element.innerHTML = val; - this.Value = val; - } - if (_elem.stateCallback) { - _elem.state = _elem.stateCallback(); - } else if (!_elem.callback) { - _elem.state = "Greyed"; - } - if (_elem.state) { - Avionics.Utils.diffAndSetAttribute(this.Element, "state", _elem.state); - } - } -} -class SoftKeys extends NavSystemElement { - constructor(_softKeyHTMLClass = SoftKeyHtmlElement) { - super(); - this.softKeys = []; - this.softKeyHTMLClass = _softKeyHTMLClass; - } - init(root) { - for (let i = 1; i <= 12; i++) { - const name = "Key" + i.toString(); - const child = this.gps.getChildById(name); - if (child) { - const e = new this.softKeyHTMLClass(child); - this.softKeys.push(e); - } - } - this.isInitialized = true; - } - onEnter() { - } - onUpdate(_deltaTime) { - const currentPage = this.gps.getCurrentPage(); - if (currentPage) { - this.currentMenu = currentPage.getSoftKeyMenu(); - if (this.currentMenu && this.currentMenu.elements && this.currentMenu.elements.length > 0) { - for (let i = 0; i < this.currentMenu.elements.length; i++) { - this.softKeys[i].fillFromElement(this.currentMenu.elements[i]); - } - } - } - } - onExit() { - } - onEvent(_event) { - switch (_event) { - case "SOFTKEYS_1": - this.activeSoftKey(0); - break; - case "SOFTKEYS_2": - this.activeSoftKey(1); - break; - case "SOFTKEYS_3": - this.activeSoftKey(2); - break; - case "SOFTKEYS_4": - this.activeSoftKey(3); - break; - case "SOFTKEYS_5": - this.activeSoftKey(4); - break; - case "SOFTKEYS_6": - this.activeSoftKey(5); - break; - case "SOFTKEYS_7": - this.activeSoftKey(6); - break; - case "SOFTKEYS_8": - this.activeSoftKey(7); - break; - case "SOFTKEYS_9": - this.activeSoftKey(8); - break; - case "SOFTKEYS_10": - this.activeSoftKey(9); - break; - case "SOFTKEYS_11": - this.activeSoftKey(10); - break; - case "SOFTKEYS_12": - this.activeSoftKey(11); - break; - } - } - activeSoftKey(_number) { - if (this.currentMenu.elements[_number].callback) { - this.currentMenu.elements[_number].callback(); - } - } -} -var Annunciation_MessageType; -(function (Annunciation_MessageType) { - Annunciation_MessageType[Annunciation_MessageType["WARNING"] = 0] = "WARNING"; - Annunciation_MessageType[Annunciation_MessageType["CAUTION"] = 1] = "CAUTION"; - Annunciation_MessageType[Annunciation_MessageType["ADVISORY"] = 2] = "ADVISORY"; - Annunciation_MessageType[Annunciation_MessageType["SAFEOP"] = 3] = "SAFEOP"; -})(Annunciation_MessageType || (Annunciation_MessageType = {})); -; -class Annunciation_Message { - constructor() { - this.Visible = false; - this.Acknowledged = false; - } -} -; -class XMLCondition { - constructor() { - this.suffix = ""; - } -} -class Annunciation_Message_XML extends Annunciation_Message { - constructor() { - super(); - this.lastConditionIndex = -1; - this.conditions = []; - this.Handler = this.getHandlersValue.bind(this); - } - getHandlersValue() { - for (let i = 0; i < this.conditions.length; i++) { - if (this.conditions[i].logic.getValue() != 0) { - if (i != this.lastConditionIndex) { - this.lastConditionIndex = i; - this.Text = this.baseText + (this.conditions[i].suffix ? this.conditions[i].suffix : ""); - return false; - } - return true; - } - } - return false; - } -} -class Annunciation_Message_Timed extends Annunciation_Message { -} -; -class Annunciation_Message_Switch extends Annunciation_Message { - get Text() { - const index = this.Handler(); - if (index > 0) { - return this.Texts[index - 1]; - } else { - return ""; - } - } -} -class Condition { - constructor(_handler, _time = 0) { - this.beginTime = 0; - this.Handler = _handler; - this.Time = _time; - } -} -class Annunciator_Message_MultipleConditions extends Annunciation_Message { - constructor() { - super(); - this.Handler = this.getHandlersValue; - } - getHandlersValue() { - let result = false; - for (let i = 0; i < this.conditions.length; i++) { - if (this.conditions[i].Handler()) { - if (this.conditions[i].beginTime == 0) { - this.conditions[i].beginTime = Date.now() + 1000 * this.conditions[i].Time; - } else if (this.conditions[i].beginTime <= Date.now()) { - result = true; - } - } else { - this.conditions[i].beginTime = 0; - } - } - return result; - } -} -class Annunciations extends NavSystemElement { - constructor() { - super(...arguments); - this.allMessages = []; - this.alertLevel = 0; - this.alert = false; - this.needReload = true; - this.rootElementName = "Annunciations"; - } - init(root) { - this.engineType = Simplane.getEngineType(); - if (this.rootElementName != "") { - this.annunciations = this.gps.getChildById(this.rootElementName); - } - if (this.gps.xmlConfig) { - const annunciationsRoot = this.gps.xmlConfig.getElementsByTagName("Annunciations"); - if (annunciationsRoot.length > 0) { - const annunciations = annunciationsRoot[0].getElementsByTagName("Annunciation"); - for (let i = 0; i < annunciations.length; i++) { - this.addXmlMessage(annunciations[i]); - } - } - } - } - onEnter() { - } - onExit() { - } - addMessage(_type, _text, _handler) { - const msg = new Annunciation_Message(); - msg.Type = _type; - msg.Text = _text; - msg.Handler = _handler.bind(msg); - this.allMessages.push(msg); - } - addXmlMessage(_element) { - const msg = new Annunciation_Message_XML(); - switch (_element.getElementsByTagName("Type")[0].textContent) { - case "Warning": - msg.Type = Annunciation_MessageType.WARNING; - break; - case "Caution": - msg.Type = Annunciation_MessageType.CAUTION; - break; - case "Advisory": - msg.Type = Annunciation_MessageType.ADVISORY; - break; - case "SafeOp": - msg.Type = Annunciation_MessageType.SAFEOP; - break; - } - msg.baseText = _element.getElementsByTagName("Text")[0].textContent; - const conditions = _element.getElementsByTagName("Condition"); - for (let i = 0; i < conditions.length; i++) { - const condition = new XMLCondition(); - condition.logic = new CompositeLogicXMLElement(this.gps, conditions[i]); - condition.suffix = conditions[i].getAttribute("Suffix"); - msg.conditions.push(condition); - } - this.allMessages.push(msg); - } - addMessageTimed(_type, _text, _handler, _time) { - const msg = new Annunciation_Message_Timed(); - msg.Type = _type; - msg.Text = _text; - msg.Handler = _handler.bind(msg); - msg.timeNeeded = _time; - this.allMessages.push(msg); - } - addMessageSwitch(_type, _texts, _handler) { - const msg = new Annunciation_Message_Switch(); - msg.Type = _type; - msg.Texts = _texts; - msg.Handler = _handler.bind(msg); - this.allMessages.push(msg); - } - addMessageMultipleConditions(_type, _text, _conditions) { - const msg = new Annunciator_Message_MultipleConditions(); - msg.Type = _type; - msg.Text = _text; - msg.conditions = _conditions; - this.allMessages.push(msg); - } -} -class Cabin_Annunciations extends Annunciations { - constructor() { - super(...arguments); - this.displayWarning = []; - this.displayCaution = []; - this.displayAdvisory = []; - //this.warningToneNameZ = new Name_Z("CRC"); - this.cautionToneNameZ = new Name_Z("improved_tone_caution"); - this.warningTone = false; - this.firstAcknowledge = true; - this.offStart = false; - } - init(root) { - super.init(root); - this.alwaysUpdate = true; - this.isPlayingWarningTone = false; - for (let i = 0; i < this.allMessages.length; i++) { - const message = this.allMessages[i]; - let value = false; - if (message.Handler) { - value = message.Handler() != 0; - } - if (value != message.Visible) { - this.needReload = true; - message.Visible = value; - message.Acknowledged = !this.offStart; - if (value) { - switch (message.Type) { - case Annunciation_MessageType.WARNING: - this.displayWarning.push(message); - break; - case Annunciation_MessageType.CAUTION: - this.displayCaution.push(message); - break; - case Annunciation_MessageType.ADVISORY: - this.displayAdvisory.push(message); - break; - } - } - } - } - } - onEnter() { - } - onUpdate(_deltaTime) { - for (let i = 0; i < this.allMessages.length; i++) { - const message = this.allMessages[i]; - let value = false; - if (message.Handler) { - value = message.Handler() != 0; - } - if (value != message.Visible) { - this.needReload = true; - message.Visible = value; - message.Acknowledged = (this.gps.getTimeSinceStart() < 10000 && !this.offStart); - if (value) { - switch (message.Type) { - case Annunciation_MessageType.WARNING: - this.displayWarning.push(message); - break; - case Annunciation_MessageType.CAUTION: - this.displayCaution.push(message); - if (!message.Acknowledged && !this.isPlayingWarningTone && this.gps.isPrimary) { - const res = this.gps.playInstrumentSound("improved_tone_caution"); - if (res) { - this.isPlayingWarningTone = true; - } - } - break; - case Annunciation_MessageType.ADVISORY: - this.displayAdvisory.push(message); - break; - } - } else { - switch (message.Type) { - case Annunciation_MessageType.WARNING: - for (let i = 0; i < this.displayWarning.length; i++) { - if (this.displayWarning[i].Text == message.Text) { - this.displayWarning.splice(i, 1); - break; - } - } - break; - case Annunciation_MessageType.CAUTION: - for (let i = 0; i < this.displayCaution.length; i++) { - if (this.displayCaution[i].Text == message.Text) { - this.displayCaution.splice(i, 1); - break; - } - } - break; - case Annunciation_MessageType.ADVISORY: - for (let i = 0; i < this.displayAdvisory.length; i++) { - if (this.displayAdvisory[i].Text == message.Text) { - this.displayAdvisory.splice(i, 1); - break; - } - } - break; - } - } - } - } - if (this.annunciations) { - this.annunciations.setAttribute("state", this.gps.blinkGetState(800, 400) ? "Blink" : "None"); - } - if (this.needReload) { - let warningOn = 0; - let cautionOn = 0; - let messages = ""; - for (let i = this.displayWarning.length - 1; i >= 0; i--) { - messages += '
' + this.displayWarning[i].Text + "
"; - } - for (let i = this.displayCaution.length - 1; i >= 0; i--) { - messages += '
' + this.displayCaution[i].Text + "
"; - } - for (let i = this.displayAdvisory.length - 1; i >= 0; i--) { - messages += '
' + this.displayAdvisory[i].Text + "
"; - } - this.warningTone = warningOn > 0; - if (this.annunciations) { - this.annunciations.innerHTML = messages; - } - this.needReload = false; - } - /*if (this.warningTone && !this.isPlayingWarningTone && this.gps.isPrimary) { - this.isPlayingWarningTone = true; - }*/ - } - onEvent(_event) { - switch (_event) { - case "Master_Caution_Push": - for (let i = 0; i < this.allMessages.length; i++) { - if (this.allMessages[i].Type == Annunciation_MessageType.CAUTION && this.allMessages[i].Visible) { - this.allMessages[i].Acknowledged = true; - this.needReload = true; - } - } - break; - case "Master_Warning_Push": - for (let i = 0; i < this.allMessages.length; i++) { - if (this.allMessages[i].Type == Annunciation_MessageType.WARNING && this.allMessages[i].Visible) { - this.allMessages[i].Acknowledged = true; - this.needReload = true; - } - } - if (this.needReload && this.firstAcknowledge && this.gps.isPrimary) { - const res = this.gps.playInstrumentSound("aural_warning_ok"); - if (res) { - this.firstAcknowledge = false; - } - } - break; - } - } - onSoundEnd(_eventId) { - if (Name_Z.compare(_eventId, this.warningToneNameZ) || Name_Z.compare(_eventId, this.cautionToneNameZ)) { - this.isPlayingWarningTone = false; - } - } - onShutDown() { - for (let i = 0; i < this.allMessages.length; i++) { - this.allMessages[i].Acknowledged = false; - this.allMessages[i].Visible = false; - } - this.displayCaution = []; - this.displayWarning = []; - this.displayAdvisory = []; - this.firstAcknowledge = true; - this.needReload = true; - } - onPowerOn() { - this.offStart = true; - } - hasMessages() { - for (let i = 0; i < this.allMessages.length; i++) { - if (this.allMessages[i].Visible) { - return true; - } - } - return false; - } -} -class Engine_Annunciations extends Cabin_Annunciations { - init(root) { - super.init(root); - switch (this.engineType) { - case EngineType.ENGINE_TYPE_PISTON: - this.addMessage(Annunciation_MessageType.WARNING, "OIL PRESSURE", this.OilPressure); - this.addMessage(Annunciation_MessageType.WARNING, "LOW VOLTS", this.LowVoltage); - this.addMessage(Annunciation_MessageType.WARNING, "HIGH VOLTS", this.HighVoltage); - this.addMessage(Annunciation_MessageType.WARNING, "CO LVL HIGH", this.COLevelHigh); - this.addMessage(Annunciation_MessageType.CAUTION, "STBY BATT", this.StandByBattery); - this.addMessage(Annunciation_MessageType.CAUTION, "LOW VACUUM", this.LowVaccum); - this.addMessage(Annunciation_MessageType.CAUTION, "LOW FUEL R", this.LowFuelR); - this.addMessage(Annunciation_MessageType.CAUTION, "LOW FUEL L", this.LowFuelL); - break; - case EngineType.ENGINE_TYPE_TURBOPROP: - case EngineType.ENGINE_TYPE_JET: - this.addMessage(Annunciation_MessageType.WARNING, "FUEL OFF", this.fuelOff); - this.addMessage(Annunciation_MessageType.WARNING, "FUEL PRESS", this.fuelPress); - this.addMessage(Annunciation_MessageType.WARNING, "OIL PRESS", this.oilPressWarning); - this.addMessageMultipleConditions(Annunciation_MessageType.WARNING, "ITT", [ - new Condition(this.itt.bind(this, "1000")), - new Condition(this.itt.bind(this, "870"), 5), - new Condition(this.itt.bind(this, "840"), 20) - ]); - this.addMessage(Annunciation_MessageType.WARNING, "FLAPS ASYM", this.flapsAsym); - this.addMessage(Annunciation_MessageType.WARNING, "ELEC FEATH FAULT", this.elecFeathFault); - this.addMessage(Annunciation_MessageType.WARNING, "BLEED TEMP", this.bleedTemp); - this.addMessage(Annunciation_MessageType.WARNING, "CABIN ALTITUDE", this.cabinAltitude); - this.addMessage(Annunciation_MessageType.WARNING, "EDM", this.edm); - this.addMessage(Annunciation_MessageType.WARNING, "CABIN DIFF PRESS", this.cabinDiffPress); - this.addMessage(Annunciation_MessageType.WARNING, "DOOR", this.door); - this.addMessage(Annunciation_MessageType.WARNING, "USP ACTIVE", this.uspActive); - this.addMessage(Annunciation_MessageType.WARNING, "GEAR UNSAFE", this.gearUnsafe); - this.addMessage(Annunciation_MessageType.WARNING, "PARK BRAKE", this.parkBrake); - this.addMessage(Annunciation_MessageType.WARNING, "OXYGEN", this.oxygen); - this.addMessage(Annunciation_MessageType.CAUTION, "OIL PRESS", this.oilPressCaution); - this.addMessage(Annunciation_MessageType.CAUTION, "CHIP", this.chip); - this.addMessage(Annunciation_MessageType.CAUTION, "OIL TEMP", this.oilTemp); - this.addMessage(Annunciation_MessageType.CAUTION, "AUX BOOST PMP ON", this.auxBoostPmpOn); - this.addMessageSwitch(Annunciation_MessageType.CAUTION, ["FUEL LOW L", "FUEL LOW R", "FUEL LOW L-R"], this.fuelLowSelector); - this.addMessage(Annunciation_MessageType.CAUTION, "AUTO SEL", this.autoSel); - this.addMessageTimed(Annunciation_MessageType.CAUTION, "FUEL IMBALANCE", this.fuelImbalance, 30); - this.addMessageSwitch(Annunciation_MessageType.CAUTION, ["LOW LVL FAIL L", "LOW LVL FAIL R", "LOW LVL FAIL L-R"], this.lowLvlFailSelector); - this.addMessage(Annunciation_MessageType.CAUTION, "BAT OFF", this.batOff); - this.addMessage(Annunciation_MessageType.CAUTION, "BAT AMP", this.batAmp); - this.addMessage(Annunciation_MessageType.CAUTION, "MAIN GEN", this.mainGen); - this.addMessage(Annunciation_MessageType.CAUTION, "LOW VOLTAGE", this.lowVoltage); - this.addMessage(Annunciation_MessageType.CAUTION, "BLEED OFF", this.bleedOff); - this.addMessage(Annunciation_MessageType.CAUTION, "USE OXYGEN MASK", this.useOxygenMask); - this.addMessage(Annunciation_MessageType.CAUTION, "VACUUM LOW", this.vacuumLow); - this.addMessage(Annunciation_MessageType.CAUTION, "PROP DEICE FAIL", this.propDeiceFail); - this.addMessage(Annunciation_MessageType.CAUTION, "INERT SEP FAIL", this.inertSepFail); - this.addMessageSwitch(Annunciation_MessageType.CAUTION, ["PITOT NO HT L", "PITOT NO HT R", "PITOT NO HT L-R"], this.pitotNoHtSelector); - this.addMessageSwitch(Annunciation_MessageType.CAUTION, ["PITOT HT ON L", "PITOT HT ON R", "PITOT HT ON L-R"], this.pitotHtOnSelector); - this.addMessage(Annunciation_MessageType.CAUTION, "STALL NO HEAT", this.stallNoHeat); - this.addMessage(Annunciation_MessageType.CAUTION, "STALL HEAT ON", this.stallHeatOn); - this.addMessage(Annunciation_MessageType.CAUTION, "FRONT CARGO DOOR", this.frontCargoDoor); - this.addMessage(Annunciation_MessageType.CAUTION, "GPU DOOR", this.gpuDoor); - this.addMessage(Annunciation_MessageType.CAUTION, "IGNITION", this.ignition); - this.addMessage(Annunciation_MessageType.CAUTION, "STARTER", this.starter); - this.addMessage(Annunciation_MessageType.CAUTION, "MAX DIFF MODE", this.maxDiffMode); - this.addMessage(Annunciation_MessageType.CAUTION, "CPCS BACK UP MODE", this.cpcsBackUpMode); - break; - } - } - sayTrue() { - return true; - } - SafePropHeat() { - return false; - } - CautionPropHeat() { - return false; - } - StandByBattery() { - return false; - } - LowVaccum() { - return SimVar.GetSimVarValue("WARNING VACUUM", "Boolean"); - } - LowPower() { - return false; - } - LowFuelR() { - return SimVar.GetSimVarValue("FUEL RIGHT QUANTITY", "gallon") < 5; - } - LowFuelL() { - return SimVar.GetSimVarValue("FUEL LEFT QUANTITY", "gallon") < 5; - } - FuelTempFailed() { - return false; - } - ECUMinorFault() { - return false; - } - PitchTrim() { - return false; - } - StartEngage() { - return false; - } - OilPressure() { - return SimVar.GetSimVarValue("WARNING OIL PRESSURE", "Boolean"); - } - LowFuelPressure() { - const pressure = SimVar.GetSimVarValue("ENG FUEL PRESSURE", "psi"); - if (pressure <= 1) { - return true; - } - return false; - } - LowVoltage() { - const voltage = SimVar.GetSimVarValue("ELECTRICAL MAIN BUS VOLTAGE", "volts"); - if (voltage < 24) { - return true; - } - return false; - } - HighVoltage() { - const voltage = SimVar.GetSimVarValue("ELECTRICAL MAIN BUS VOLTAGE", "volts"); - if (voltage > 32) { - return true; - } - return false; - } - FuelTemperature() { - return false; - } - ECUMajorFault() { - return false; - } - COLevelHigh() { - return false; - } - fuelOff() { - return (SimVar.GetSimVarValue("FUEL TANK SELECTOR:1", "number") == 0); - } - fuelPress() { - return (SimVar.GetSimVarValue("GENERAL ENG FUEL PRESSURE:1", "psi") <= 10); - } - oilPressWarning() { - return (SimVar.GetSimVarValue("ENG OIL PRESSURE:1", "psi") <= 60); - } - itt(_limit = 840) { - const itt = SimVar.GetSimVarValue("TURB ENG ITT:1", "celsius"); - return (itt > _limit); - } - flapsAsym() { - return false; - } - elecFeathFault() { - return false; - } - bleedTemp() { - return false; - } - cabinAltitude() { - return SimVar.GetSimVarValue("PRESSURIZATION CABIN ALTITUDE", "feet") > 10000; - } - edm() { - return false; - } - cabinDiffPress() { - return SimVar.GetSimVarValue("PRESSURIZATION PRESSURE DIFFERENTIAL", "psi") > 6.2; - } - door() { - return SimVar.GetSimVarValue("EXIT OPEN:0", "percent") > 0; - } - uspActive() { - return false; - } - gearUnsafe() { - return false; - } - parkBrake() { - return SimVar.GetSimVarValue("L:A32NX_PARK_BRAKE_LEVER_POS", "Bool"); - } - oxygen() { - return false; - } - oilPressCaution() { - const press = SimVar.GetSimVarValue("ENG OIL PRESSURE:1", "psi"); - return (press <= 105 && press >= 60); - } - chip() { - return false; - } - oilTemp() { - const temp = SimVar.GetSimVarValue("GENERAL ENG OIL TEMPERATURE:1", "celsius"); - return (temp <= 0 || temp >= 104); - } - auxBoostPmpOn() { - return SimVar.GetSimVarValue("GENERAL ENG FUEL PUMP ON:1", "Bool"); - } - fuelLowSelector() { - const left = SimVar.GetSimVarValue("FUEL TANK LEFT MAIN QUANTITY", "gallon") < 9; - const right = SimVar.GetSimVarValue("FUEL TANK RIGHT MAIN QUANTITY", "gallon") < 9; - if (left && right) { - return 3; - } else if (left) { - return 1; - } else if (right) { - return 2; - } else { - return 0; - } - } - autoSel() { - return false; - } - fuelImbalance() { - const left = SimVar.GetSimVarValue("FUEL TANK LEFT MAIN QUANTITY", "gallon"); - const right = SimVar.GetSimVarValue("FUEL TANK RIGHT MAIN QUANTITY", "gallon"); - return Math.abs(left - right) > 15; - } - lowLvlFailSelector() { - return false; - } - batOff() { - return !SimVar.GetSimVarValue("ELECTRICAL MASTER BATTERY", "Bool"); - } - batAmp() { - return SimVar.GetSimVarValue("ELECTRICAL BATTERY BUS AMPS", "amperes") > 50; - } - mainGen() { - return !SimVar.GetSimVarValue("GENERAL ENG GENERATOR SWITCH:1", "Bool"); - } - lowVoltage() { - return SimVar.GetSimVarValue("ELECTRICAL MAIN BUS VOLTAGE", "volts") < 24.5; - } - bleedOff() { - return SimVar.GetSimVarValue("BLEED AIR SOURCE CONTROL", "Enum") == 1; - } - useOxygenMask() { - return SimVar.GetSimVarValue("PRESSURIZATION CABIN ALTITUDE", "feet") > 10000; - } - vacuumLow() { - return SimVar.GetSimVarValue("PARTIAL PANEL VACUUM", "Enum") == 1; - } - propDeiceFail() { - return false; - } - inertSepFail() { - return false; - } - pitotNoHtSelector() { - return 0; - } - pitotHtOnSelector() { - return 0; - } - stallNoHeat() { - return false; - } - stallHeatOn() { - return false; - } - frontCargoDoor() { - return false; - } - gpuDoor() { - return false; - } - ignition() { - return SimVar.GetSimVarValue("TURB ENG IS IGNITING:1", "Bool"); - } - starter() { - return SimVar.GetSimVarValue("GENERAL ENG STARTER ACTIVE:1", "Bool"); - } - maxDiffMode() { - return SimVar.GetSimVarValue("BLEED AIR SOURCE CONTROL", "Enum") == 3; - } - cpcsBackUpMode() { - return false; - } -} -class Warning_Data { - constructor(_shortText, _longText, _soundEvent, _Level, _callback, _once = false) { - this.hasPlayed = false; - this.shortText = _shortText; - this.longText = _longText; - this.soundEvent = _soundEvent; - this.soundEventId = new Name_Z(this.soundEvent); - this.level = _Level; - this.callback = _callback; - this.once = _once; - } -} -class Warning_Data_XML extends Warning_Data { - constructor(_gps, _shortText, _longText, _soundEvent, _Level, _logicElement, _once = false) { - super(_shortText, _longText, _soundEvent, _Level, null, _once); - this.xmlLogic = new CompositeLogicXMLElement(_gps, _logicElement); - this.callback = this.getXMLBoolean.bind(this); - } - getXMLBoolean() { - return this.xmlLogic.getValue() != 0; - } -} -class Warnings extends NavSystemElement { - constructor() { - super(...arguments); - this.warnings = []; - this.playingSounds = []; - this.pullUp_sinkRate_Points = [ - [1160, 0, 0], - [2320, 1070, 1460], - [4930, 2380, 2980], - [11600, 4285, 5360] - ]; - } - init(_root) { - let alertsFromXML = false; - if (this.gps.xmlConfig) { - const alertsGroup = this.gps.xmlConfig.getElementsByTagName("VoicesAlerts"); - if (alertsGroup.length > 0) { - alertsFromXML = true; - const alerts = alertsGroup[0].getElementsByTagName("Alert"); - for (let i = 0; i < alerts.length; i++) { - const typeParam = alerts[i].getElementsByTagName("Type"); - let type = 0; - if (typeParam.length > 0) { - switch (typeParam[0].textContent) { - case "Warning": - type = 3; - break; - case "Caution": - type = 2; - break; - case "Test": - type = 1; - break; - case "SoundOnly": - type = 0; - break; - } - } - let shortText = ""; - let longText = ""; - if (type != 0) { - const shortTextElem = alerts[i].getElementsByTagName("ShortText"); - if (shortTextElem.length > 0) { - shortText = shortTextElem[0].textContent; - } - const longTextElem = alerts[i].getElementsByTagName("LongText"); - if (longTextElem.length > 0) { - longText = longTextElem[0].textContent; - } - } - let soundEvent = ""; - const soundEventElem = alerts[i].getElementsByTagName("SoundEvent"); - if (soundEventElem.length > 0) { - soundEvent = soundEventElem[0].textContent; - } - const condition = alerts[i].getElementsByTagName("Condition")[0]; - let once = false; - const onceElement = alerts[i].getElementsByTagName("Once"); - if (onceElement.length > 0 && onceElement[0].textContent == "True") { - once = true; - } - this.warnings.push(new Warning_Data_XML(this.gps, shortText, longText, soundEvent, type, condition, once)); - } - } - } - if (!alertsFromXML) { - this.warnings.push(new Warning_Data("", "", "Garmin_Stall_f", 0, this.stallCallback.bind(this))); - this.warnings.push(new Warning_Data("PULL UP", "PULL UP", "Garmin_Pull_Up_f", 3, this.pullUpCallback.bind(this))); - this.warnings.push(new Warning_Data("TERRAIN", "SINK RATE", "Garmin_Sink_Rate_f", 2, this.sinkRateCallback.bind(this))); - this.warnings.push(new Warning_Data("", "", "Garmin_landing_gear_f", 0, this.landingGearCallback.bind(this))); - this.warnings.push(new Warning_Data("TAWS TEST", "", "", 1, this.tawsTestCallback.bind(this))); - this.warnings.push(new Warning_Data("", "", "Garmin_TAWS_System_Test_OK_f", 0, this.tawsTestFinishedCallback.bind(this), true)); - } - this.UID = parseInt(this.gps.getAttribute("Guid")) + 1; - SimVar.SetSimVarValue("L:AS1000_Warnings_Master_Set", "number", 0); - } - onUpdate(_deltaTime) { - const masterSet = SimVar.GetSimVarValue("L:AS1000_Warnings_Master_Set", "number"); - if (masterSet == 0) { - SimVar.SetSimVarValue("L:AS1000_Warnings_Master_Set", "number", this.UID); - } else if (masterSet == this.UID) { - let found = false; - let foundText = false; - let bestWarning = 0; - for (let i = 0; i < this.warnings.length; i++) { - const warning = this.warnings[i]; - if (!warning.once || !warning.hasPlayed) { - if (warning.callback()) { - if (warning.soundEvent != "") { - if ((this.playingSounds.length <= 0) || (i < this.playingSounds[this.playingSounds.length - 1])) { - const res = this.gps.playInstrumentSound(warning.soundEvent); - if (res) { - this.playingSounds.push(i); - warning.hasPlayed = true; - } - } - } - if (!foundText) { - bestWarning = i; - } - if (warning.shortText || warning.longText) { - foundText = true; - } - found = true; - } - } - } - if (found) { - SimVar.SetSimVarValue("L:AS1000_Warnings_WarningIndex", "number", bestWarning + 1); - } else { - SimVar.SetSimVarValue("L:AS1000_Warnings_WarningIndex", "number", 0); - } - } - } - onSoundEnd(_eventId) { - let i = 0; - while (i < this.playingSounds.length) { - const soundId = this.playingSounds[i]; - if (Name_Z.compare(this.warnings[soundId].soundEventId, _eventId)) { - this.playingSounds.splice(i, 1); - continue; - } - i++; - } - } - onShutDown() { - for (let i = 0; i < this.warnings.length; i++) { - this.warnings[i].hasPlayed = false; - } - this.playingSounds = []; - } - linearMultiPointsEvaluation(_points, _valueX, _valueY) { - let lastLowerIndex = -1; - for (let i = 0; i < _points.length; i++) { - if (_valueX > _points[i][0]) { - lastLowerIndex = i; - } else { - break; - } - } - if (lastLowerIndex == _points.length - 1) { - for (let i = 1; i < _points[lastLowerIndex].length; i++) { - if (_valueY < _points[lastLowerIndex][i]) { - return i; - } - } - return _points[lastLowerIndex].length; - } else if (lastLowerIndex == -1) { - for (let i = 1; i < _points[0].length; i++) { - if (_valueY < _points[0][i]) { - return i; - } - } - return _points[0].length; - } else { - const factorLower = (_valueX - _points[lastLowerIndex][0]) / _points[lastLowerIndex + 1][0]; - for (let i = 1; i < _points[lastLowerIndex].length; i++) { - const limit = _points[lastLowerIndex][i] * factorLower + _points[lastLowerIndex + 1][i] * (1 - factorLower); - if (_valueY < limit) { - return i; - } - } - return _points[lastLowerIndex].length; - } - } - pullUpCallback() { - const height = SimVar.GetSimVarValue("PLANE ALT ABOVE GROUND", "feet"); - const descentRate = -SimVar.GetSimVarValue("VERTICAL SPEED", "feet per minute"); - return this.linearMultiPointsEvaluation(this.pullUp_sinkRate_Points, descentRate, height) == 1; - } - sinkRateCallback() { - const height = SimVar.GetSimVarValue("PLANE ALT ABOVE GROUND", "feet"); - const descentRate = -SimVar.GetSimVarValue("VERTICAL SPEED", "feet per minute"); - return this.linearMultiPointsEvaluation(this.pullUp_sinkRate_Points, descentRate, height) == 2; - } - landingGearCallback() { - const gear = !SimVar.GetSimVarValue("IS GEAR RETRACTABLE", "Boolean") || SimVar.GetSimVarValue("L:A32NX_GEAR_HANDLE_POSITION", "Percent over 100") > 0.5; - const throttle = SimVar.GetSimVarValue("L:A32NX_AUTOTHRUST_TLA:1", "number"); - const flaps = SimVar.GetSimVarValue("L:A32NX_FLAPS_HANDLE_INDEX", "number"); - return !gear && (flaps > 1 || (throttle == 0)); - } - stallCallback() { - return SimVar.GetSimVarValue("STALL WARNING", "Boolean"); - } - tawsTestCallback() { - return this.gps.getTimeSinceStart() < 30000; - } - tawsTestFinishedCallback() { - return this.gps.getTimeSinceStart() >= 30000; - } -} -class Cabin_Warnings extends Warnings { - constructor() { - super(...arguments); - this.currentWarningLevel = 0; - this.currentWarningText = ""; - } - init(root) { - super.init(root); - this.alwaysUpdate = true; - } - onUpdate(_deltaTime) { - super.onUpdate(_deltaTime); - const warningIndex = SimVar.GetSimVarValue("L:AS1000_Warnings_WarningIndex", "number"); - let warningText; - let warningLevel; - if (warningIndex <= 0 || warningIndex >= this.warnings.length) { - warningText = ""; - warningLevel = 0; - } else { - warningText = this.warnings[warningIndex - 1].shortText; - warningLevel = this.warnings[warningIndex - 1].level; - } - if (this.currentWarningLevel != warningLevel || this.currentWarningText != warningText) { - this.currentWarningText = warningText; - this.currentWarningLevel = warningLevel; - if (this.warningBox && this.warningContent) { - this.warningContent.textContent = warningText; - switch (warningLevel) { - case 0: - this.warningBox.setAttribute("state", "Hidden"); - break; - case 1: - this.warningBox.setAttribute("state", "White"); - break; - case 2: - this.warningBox.setAttribute("state", "Yellow"); - break; - case 3: - this.warningBox.setAttribute("state", "Red"); - break; - } - } - } - } - getCurrentWarningLevel() { - return this.currentWarningLevel; - } - getCurrentWarningText() { - return this.currentWarningText; - } - onEnter() { } - onExit() { } - onEvent(_event) { } -} -class GlassCockpit_XMLEngine extends NavSystemElement { - constructor() { - super(...arguments); - this.xmlEngineDisplay = null; - this._t = 0; - } - init(_root) { - if (this.gps.xmlConfig) { - const engineRoot = this.gps.xmlConfig.getElementsByTagName("EngineDisplay"); - if (engineRoot.length > 0) { - this.xmlEngineDisplay = _root.querySelector("glasscockpit-xmlenginedisplay"); - this.xmlEngineDisplay.setConfiguration(this.gps, engineRoot[0]); - } - } - } - onEnter() { - } - onExit() { - } - onUpdate(_deltaTime) { - if (this.xmlEngineDisplay) { - this.xmlEngineDisplay.update(_deltaTime); - } - this._t++; - if (this._t > 30) { - this.gps.currFlightPlanManager.updateFlightPlan(); - this._t = 0; - } - } - onEvent(_event) { - this.xmlEngineDisplay.onEvent(_event); - } - onSoundEnd(_eventId) { - super.onSoundEnd(_eventId); - this.xmlEngineDisplay.onSoundEnd(_eventId); - } -} diff --git a/fbw-a32nx/src/systems/atsu/fmsclient/src/index.ts b/fbw-a32nx/src/systems/atsu/fmsclient/src/index.ts index 43c2d663789..b20df878af5 100644 --- a/fbw-a32nx/src/systems/atsu/fmsclient/src/index.ts +++ b/fbw-a32nx/src/systems/atsu/fmsclient/src/index.ts @@ -375,6 +375,7 @@ export class FmsClient { } public flightNumber(): string { + // FIXME get FMS flight number from ARINC word return SimVar.GetSimVarValue('ATC FLIGHT NUMBER', 'string'); } diff --git a/fbw-a32nx/src/systems/extras-host/modules/flightplan_sync/FlightPlanAsoboSync.ts b/fbw-a32nx/src/systems/extras-host/modules/flightplan_sync/FlightPlanAsoboSync.ts index bb62eb55632..bf1c264e35f 100644 --- a/fbw-a32nx/src/systems/extras-host/modules/flightplan_sync/FlightPlanAsoboSync.ts +++ b/fbw-a32nx/src/systems/extras-host/modules/flightplan_sync/FlightPlanAsoboSync.ts @@ -13,13 +13,6 @@ import { PerformanceDataFlightPlanSyncEvents, SyncFlightPlanEvents, } from '@fmgc/flightplanning/sync/FlightPlanEvents'; -import { - A320FlightPlanPerformanceData, - FlightPlanIndex, - NavigationDatabase, - NavigationDatabaseBackend, - NavigationDatabaseService, -} from '@fmgc/index'; import { EventBus, FacilityType, @@ -33,6 +26,10 @@ import { } from '@microsoft/msfs-sdk'; import { ApproachType as MSApproachType } from '../../../../../../fbw-common/src/systems/navdata/client/backends/Msfs/FsTypes'; import { FacilityCache } from '../../../../../../fbw-common/src/systems/navdata/client/backends/Msfs/FacilityCache'; +import { NavigationDatabaseService } from '@fmgc/flightplanning/NavigationDatabaseService'; +import { NavigationDatabase, NavigationDatabaseBackend } from '@fmgc/NavigationDatabase'; +import { A320FlightPlanPerformanceData } from '@fmgc/flightplanning/plans/performance/FlightPlanPerformanceData'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; export class FlightPlanAsoboSync { private isReady = false; @@ -66,6 +63,7 @@ export class FlightPlanAsoboSync { } init(): void { + // FIXME this should only ever be used within the FMGC NavigationDatabaseService.activeDatabase = new NavigationDatabase(NavigationDatabaseBackend.Msfs); const sub = this.bus.getSubscriber< diff --git a/fbw-a32nx/src/systems/fmgc/src/components/FmgcComponent.ts b/fbw-a32nx/src/systems/fmgc/src/components/FmgcComponent.ts index 4ced2d16565..2c799e59390 100644 --- a/fbw-a32nx/src/systems/fmgc/src/components/FmgcComponent.ts +++ b/fbw-a32nx/src/systems/fmgc/src/components/FmgcComponent.ts @@ -3,8 +3,10 @@ // SPDX-License-Identifier: GPL-3.0 import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; +import { GuidanceController } from '@fmgc/guidance/GuidanceController'; +import { Navigation } from '@fmgc/navigation/Navigation'; export interface FmgcComponent { - init(baseInstrument: BaseInstrument, flightPlanService: FlightPlanService): void; + init(navigation: Navigation, guidanceController: GuidanceController, flightPlanService: FlightPlanService): void; update(deltaTime: number): void; } diff --git a/fbw-a32nx/src/systems/fmgc/src/components/ReadySignal.ts b/fbw-a32nx/src/systems/fmgc/src/components/ReadySignal.ts index 6be99e158c0..3cde4653d4d 100644 --- a/fbw-a32nx/src/systems/fmgc/src/components/ReadySignal.ts +++ b/fbw-a32nx/src/systems/fmgc/src/components/ReadySignal.ts @@ -1,29 +1,19 @@ -// Copyright (c) 2021-2023 FlyByWire Simulations -// +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations // SPDX-License-Identifier: GPL-3.0 -import { UpdateThrottler } from '@flybywiresim/fbw-sdk'; -import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; import { FmgcComponent } from './FmgcComponent'; +import { GameStateProvider, Wait } from '@microsoft/msfs-sdk'; export class ReadySignal implements FmgcComponent { - private baseInstrument: BaseInstrument = null; - - private updateThrottler = new UpdateThrottler(1000); - - init(baseInstrument: BaseInstrument, _flightPlanService: FlightPlanService): void { - this.baseInstrument = baseInstrument; - } - - update(deltaTime: number): void { - if ( - this.updateThrottler.canUpdate(deltaTime) !== -1 && - this.baseInstrument.getGameState() === GameState.ingame && - SimVar.GetSimVarValue('L:A32NX_IS_READY', 'number') !== 1 - ) { + init(): void { + Wait.awaitSubscribable(GameStateProvider.get(), (state) => state === GameState.ingame, true).then(() => { // set ready signal that JS code is initialized and flight is actually started // -> user pressed 'READY TO FLY' button SimVar.SetSimVarValue('L:A32NX_IS_READY', 'number', 1); - } + }); + } + + update(_deltaTime: number): void { + // noop } } diff --git a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/FmsMessages.ts b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/FmsMessages.ts index 4c8941af198..e958f76105f 100644 --- a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/FmsMessages.ts +++ b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/FmsMessages.ts @@ -22,6 +22,8 @@ import { FmgcComponent } from '../FmgcComponent'; import { GpsPrimary } from './GpsPrimary'; import { GpsPrimaryLost } from './GpsPrimaryLost'; import { MapPartlyDisplayedLeft, MapPartlyDisplayedRight } from './MapPartlyDisplayed'; +import { Navigation } from '@fmgc/navigation/Navigation'; +import { GuidanceController } from '@fmgc/guidance/GuidanceController'; /** * This class manages Type II messages sent from the FMGC. @@ -38,8 +40,6 @@ import { MapPartlyDisplayedLeft, MapPartlyDisplayedRight } from './MapPartlyDisp export class FmsMessages implements FmgcComponent { private listener = RegisterViewListener('JS_LISTENER_SIMVARS', null, true); - private baseInstrument: BaseInstrument; - private ndMessageFlags: Record<'L' | 'R', number> = { L: 0, R: 0, @@ -65,12 +65,10 @@ export class FmsMessages implements FmgcComponent { new StepDeleted(), ]; - init(baseInstrument: BaseInstrument, flightPlanService: FlightPlanService): void { - this.baseInstrument = baseInstrument; - + init(navigation: Navigation, guidanceController: GuidanceController, flightPlanService: FlightPlanService): void { for (const selector of this.messageSelectors) { if (selector.init) { - selector.init(this.baseInstrument, flightPlanService); + selector.init(navigation, guidanceController, flightPlanService); } } } @@ -209,7 +207,7 @@ export interface FMMessageSelector { efisSide?: 'L' | 'R'; - init?(baseInstrument: BaseInstrument, flightPlanService: FlightPlanService): void; + init?(navigation: Navigation, guidanceController: GuidanceController, flightPlanService: FlightPlanService): void; /** * Optionally triggers a message when there isn't any other system or Redux update triggering it. diff --git a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/RwyLsMismatch.ts b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/RwyLsMismatch.ts index f449f5ce383..ed3efc6ae5c 100644 --- a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/RwyLsMismatch.ts +++ b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/RwyLsMismatch.ts @@ -6,6 +6,7 @@ import { FMMessage, FMMessageTypes, Trigger } from '@flybywiresim/fbw-sdk'; import { NavaidTuner } from '@fmgc/navigation/NavaidTuner'; import { FMMessageSelector, FMMessageUpdate } from './FmsMessages'; +import { Navigation } from '@fmgc/navigation/Navigation'; abstract class RwyLsMismatch implements FMMessageSelector { message: FMMessage = FMMessageTypes.RwyLsMismatch; @@ -16,10 +17,10 @@ abstract class RwyLsMismatch implements FMMessageSelector { private trigFalling = new Trigger(true); - private navaidTuner: NavaidTuner; + private navaidTuner?: NavaidTuner; - init(baseInstrument: BaseInstrument): void { - this.navaidTuner = baseInstrument.navigation.getNavaidTuner(); + init(navigation: Navigation): void { + this.navaidTuner = navigation.getNavaidTuner(); } process(deltaTime: number): FMMessageUpdate { diff --git a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/SpecifiedNdbUnavailable.ts b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/SpecifiedNdbUnavailable.ts index 7adda363695..867dd5757cf 100644 --- a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/SpecifiedNdbUnavailable.ts +++ b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/SpecifiedNdbUnavailable.ts @@ -5,6 +5,7 @@ import { NavaidTuner } from '@fmgc/navigation/NavaidTuner'; import { Trigger, FMMessage, FMMessageTypes } from '@flybywiresim/fbw-sdk'; import { FMMessageSelector, FMMessageUpdate } from './FmsMessages'; +import { Navigation } from '@fmgc/navigation/Navigation'; abstract class SpecifiedNdbUnavailable implements FMMessageSelector { message: FMMessage = FMMessageTypes.SpecifiedNdbUnavailble; @@ -15,10 +16,10 @@ abstract class SpecifiedNdbUnavailable implements FMMessageSelector { private trigFalling = new Trigger(true); - private navaidTuner: NavaidTuner; + private navaidTuner?: NavaidTuner; - init(baseInstrument: BaseInstrument): void { - this.navaidTuner = baseInstrument.navigation.getNavaidTuner(); + init(navigation: Navigation): void { + this.navaidTuner = navigation.getNavaidTuner(); } process(deltaTime: number): FMMessageUpdate { diff --git a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/SpecifiedVorUnavailable.ts b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/SpecifiedVorUnavailable.ts index f0cc98808a5..2efe73c0ab0 100644 --- a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/SpecifiedVorUnavailable.ts +++ b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/SpecifiedVorUnavailable.ts @@ -6,6 +6,7 @@ import { Trigger, FMMessage, FMMessageTypes } from '@flybywiresim/fbw-sdk'; import { NavaidTuner } from '@fmgc/navigation/NavaidTuner'; import { FMMessageSelector, FMMessageUpdate } from './FmsMessages'; +import { Navigation } from '@fmgc/navigation/Navigation'; abstract class SpecifiedVorUnavailable implements FMMessageSelector { message: FMMessage = FMMessageTypes.SpecifiedVorDmeUnavailble; @@ -18,8 +19,8 @@ abstract class SpecifiedVorUnavailable implements FMMessageSelector { private navaidTuner: NavaidTuner; - init(baseInstrument: BaseInstrument): void { - this.navaidTuner = baseInstrument.navigation.getNavaidTuner(); + init(navigation: Navigation): void { + this.navaidTuner = navigation.getNavaidTuner(); } process(deltaTime: number): FMMessageUpdate { diff --git a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/StepAhead.ts b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/StepAhead.ts index dd16604476e..5ec27398ffc 100644 --- a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/StepAhead.ts +++ b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/StepAhead.ts @@ -7,18 +7,19 @@ import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; import { FMMessageTypes } from '@flybywiresim/fbw-sdk'; import { FMMessageSelector, FMMessageUpdate } from './FmsMessages'; +import { Navigation } from '@fmgc/navigation/Navigation'; export class StepAhead implements FMMessageSelector { message = FMMessageTypes.StepAhead; - private guidanceController: GuidanceController; + private guidanceController?: GuidanceController; - private flightPlanService: FlightPlanService; + private flightPlanService?: FlightPlanService; private lastState = false; - init(baseInstrument: BaseInstrument, flightPlanService: FlightPlanService): void { - this.guidanceController = baseInstrument.guidanceController; + init(_navigation: Navigation, guidanceController: GuidanceController, flightPlanService: FlightPlanService): void { + this.guidanceController = guidanceController; this.flightPlanService = flightPlanService; } diff --git a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/TuneNavaid.ts b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/TuneNavaid.ts index 7a0b3629b37..37414aceebb 100644 --- a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/TuneNavaid.ts +++ b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/TuneNavaid.ts @@ -5,6 +5,7 @@ import { NavaidTuner } from '@fmgc/navigation/NavaidTuner'; import { FMMessage, FMMessageTypes, Trigger } from '@flybywiresim/fbw-sdk'; import { FMMessageSelector, FMMessageUpdate } from './FmsMessages'; +import { Navigation } from '@fmgc/navigation/Navigation'; abstract class TuneNavaid implements FMMessageSelector { message: FMMessage = { ...FMMessageTypes.TuneNavaid }; @@ -15,10 +16,10 @@ abstract class TuneNavaid implements FMMessageSelector { private trigFalling = new Trigger(true); - private navaidTuner: NavaidTuner; + private navaidTuner?: NavaidTuner; - init(baseInstrument: BaseInstrument): void { - this.navaidTuner = baseInstrument.navigation.getNavaidTuner(); + init(navigation: Navigation): void { + this.navaidTuner = navigation.getNavaidTuner(); } process(deltaTime: number): FMMessageUpdate { diff --git a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/TurnAreaExceedance.ts b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/TurnAreaExceedance.ts index aa0bddf3365..4d85d520779 100644 --- a/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/TurnAreaExceedance.ts +++ b/fbw-a32nx/src/systems/fmgc/src/components/fms-messages/TurnAreaExceedance.ts @@ -18,13 +18,13 @@ abstract class TurnAreaExceedance implements FMMessageSelector { private trigFalling = new Trigger(true); - private guidanceController: GuidanceController; + private guidanceController?: GuidanceController; - private navigation: Navigation; + private navigation?: Navigation; - init(baseInstrument: BaseInstrument): void { - this.guidanceController = baseInstrument.guidanceController; - this.navigation = baseInstrument.navigation; + init(navigation: Navigation, guidanceController: GuidanceController): void { + this.guidanceController = guidanceController; + this.navigation = navigation; } process(deltaTime: number): FMMessageUpdate { diff --git a/fbw-a32nx/src/systems/fmgc/src/components/index.ts b/fbw-a32nx/src/systems/fmgc/src/components/index.ts index 72cc0ea7fdd..2e15cc02745 100644 --- a/fbw-a32nx/src/systems/fmgc/src/components/index.ts +++ b/fbw-a32nx/src/systems/fmgc/src/components/index.ts @@ -7,13 +7,19 @@ import { ReadySignal } from '@fmgc/components/ReadySignal'; import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; import { FmgcComponent } from './FmgcComponent'; import { FmsMessages } from './fms-messages'; +import { Navigation } from '../navigation/Navigation'; +import { GuidanceController } from '../guidance/GuidanceController'; const fmsMessages = new FmsMessages(); const components: FmgcComponent[] = [fmsMessages, new ReadySignal(), new FcuSync()]; -export function initComponents(baseInstrument: BaseInstrument, flightPlanService: FlightPlanService): void { - components.forEach((component) => component.init(baseInstrument, flightPlanService)); +export function initComponents( + navigation: Navigation, + guidanceController: GuidanceController, + flightPlanService: FlightPlanService, +): void { + components.forEach((component) => component.init(navigation, guidanceController, flightPlanService)); } export function updateComponents(deltaTime: number): void { diff --git a/fbw-a32nx/src/systems/fmgc/src/efis/EfisSymbols.ts b/fbw-a32nx/src/systems/fmgc/src/efis/EfisSymbols.ts index 6b6b5bc024a..9adf701b787 100644 --- a/fbw-a32nx/src/systems/fmgc/src/efis/EfisSymbols.ts +++ b/fbw-a32nx/src/systems/fmgc/src/efis/EfisSymbols.ts @@ -79,13 +79,13 @@ export class EfisSymbols { private lastEfisInterfaceVersions: Record = { L: -1, R: -1 }; private mapReferenceLatitude: Record = { - L: Arinc429OutputWord.empty('L:A32NX_EFIS_L_MRP_LAT'), - R: Arinc429OutputWord.empty('L:A32NX_EFIS_R_MRP_LAT'), + L: new Arinc429OutputWord('L:A32NX_EFIS_L_MRP_LAT'), + R: new Arinc429OutputWord('L:A32NX_EFIS_R_MRP_LAT'), }; private mapReferenceLongitude: Record = { - L: Arinc429OutputWord.empty('L:A32NX_EFIS_L_MRP_LONG'), - R: Arinc429OutputWord.empty('L:A32NX_EFIS_R_MRP_LONG'), + L: new Arinc429OutputWord('L:A32NX_EFIS_L_MRP_LONG'), + R: new Arinc429OutputWord('L:A32NX_EFIS_R_MRP_LONG'), }; private readonly flightPhase = ConsumerValue.create( @@ -813,8 +813,8 @@ export class EfisSymbols { ident: fixInfo.fix.ident, location: fixInfo.fix.location, type: NdSymbolTypeFlags.FixInfo, - radials: fixInfo.radials.map((it) => it.trueBearing), - radii: fixInfo.radii.map((it) => it.radius), + radials: fixInfo.radials?.map((it) => it.trueBearing), + radii: fixInfo.radii?.map((it) => it.radius), }); } } diff --git a/fbw-a32nx/src/systems/fmgc/src/flightplanning/DataManager.ts b/fbw-a32nx/src/systems/fmgc/src/flightplanning/DataManager.ts index beebd74ee80..7cceb0b62df 100644 --- a/fbw-a32nx/src/systems/fmgc/src/flightplanning/DataManager.ts +++ b/fbw-a32nx/src/systems/fmgc/src/flightplanning/DataManager.ts @@ -7,8 +7,9 @@ import { FmsError, FmsErrorType } from '@fmgc/FmsError'; import { FmsDisplayInterface } from '@fmgc/flightplanning/interface/FmsDisplayInterface'; import { WaypointFactory } from '@fmgc/flightplanning/waypoints/WaypointFactory'; import { Coordinates } from 'msfs-geo'; +import { A32NX_Util } from '../../../shared/src/A32NX_Util'; -enum PilotWaypointType { +export enum PilotWaypointType { Pbd = 1, Pbx = 2, LatLon = 3, @@ -142,6 +143,10 @@ export class DataManager { this.updateLocalStorage(); } + public getStoredWaypoint(index: number): PilotWaypoint | undefined { + return this.storedWaypoints[index]; + } + async deleteStoredWaypoint(index: number, updateStorage = true) { if (!this.storedWaypoints[index]) { return true; diff --git a/fbw-a32nx/src/systems/fmgc/src/flightplanning/FlightPlanService.ts b/fbw-a32nx/src/systems/fmgc/src/flightplanning/FlightPlanService.ts index aa950d59366..d762c674f81 100644 --- a/fbw-a32nx/src/systems/fmgc/src/flightplanning/FlightPlanService.ts +++ b/fbw-a32nx/src/systems/fmgc/src/flightplanning/FlightPlanService.ts @@ -149,7 +149,7 @@ export class FlightPlanService

{ if (WaypointEntryUtils.isRunwayFormat(place)) { @@ -73,7 +75,11 @@ export class WaypointEntryUtils { // In this case, we only want to return the actual VOR facility const items = WaypointEntryUtils.mergeNavaidsWithWaypoints(navaids, waypoints); - return fms.deduplicateFacilities(items); + const ret = fms.deduplicateFacilities(items); + if (ret === undefined) { + throw new FmsError(FmsErrorType.NotInDatabase); + } + return ret; } static mergeNavaidsWithWaypoints(navaids: (VhfNavaid | NdbNavaid)[], waypoints: Waypoint[]): Fix[] { diff --git a/fbw-a32nx/src/systems/fmgc/src/flightplanning/legs/FlightPlanLeg.ts b/fbw-a32nx/src/systems/fmgc/src/flightplanning/legs/FlightPlanLeg.ts index a6e46ed99aa..793ffbae1dc 100644 --- a/fbw-a32nx/src/systems/fmgc/src/flightplanning/legs/FlightPlanLeg.ts +++ b/fbw-a32nx/src/systems/fmgc/src/flightplanning/legs/FlightPlanLeg.ts @@ -502,3 +502,7 @@ export interface Discontinuity { } export type FlightPlanElement = FlightPlanLeg | Discontinuity; + +export function isDiscontinuity(o: any): o is Discontinuity { + return typeof o === 'object' && o.isDiscontinuity === true; +} diff --git a/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/FixInfo.ts b/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/FixInfo.ts index 27485425f00..6b357564b7a 100644 --- a/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/FixInfo.ts +++ b/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/FixInfo.ts @@ -24,26 +24,22 @@ export interface FixInfoRadius { * A FIX INFO entry in a flight plan */ export class FixInfoEntry implements FixInfoData { - /** The fix concerned by the fix info */ - public fix: Fix; - - /** The radii contained in the fix info */ - public radii?: FixInfoRadius[]; - - /** The radials contained in the fix ino */ - public radials?: FixInfoRadial[]; - - constructor(fix: Fix, radii?: FixInfoRadius[], radials?: FixInfoRadial[]) { - this.fix = fix; - this.radii = radii; - this.radials = radials; - } + /** + * @param fix The fix concerned by the fix info. + * @param radii The radii contained in the fix info. + * @param radials The radials contained in the fix info. + */ + constructor( + public fix: Fix, + public radii?: FixInfoRadius[], + public radials?: FixInfoRadial[], + ) {} public clone(): FixInfoEntry { return new FixInfoEntry( this.fix, - this.radii.map((radius) => ({ ...radius })), - this.radials.map((radial) => ({ ...radial })), + this.radii?.map((radius) => ({ ...radius })), + this.radials?.map((radial) => ({ ...radial })), ); } } diff --git a/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/FlightPlan.ts b/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/FlightPlan.ts index 1c91e55c661..566833d031b 100644 --- a/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/FlightPlan.ts +++ b/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/FlightPlan.ts @@ -21,6 +21,7 @@ import { } from '@fmgc/flightplanning/plans/performance/FlightPlanPerformanceData'; import { BaseFlightPlan, FlightPlanQueuedOperation, SerializedFlightPlan } from './BaseFlightPlan'; import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; +import { A32NX_Util } from '../../../../shared/src/A32NX_Util'; export class FlightPlan

extends BaseFlightPlan

{ static empty

( diff --git a/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/ObservableFlightPlan.ts b/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/ObservableFlightPlan.ts index df7b491b920..a65d47885ce 100644 --- a/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/ObservableFlightPlan.ts +++ b/fbw-a32nx/src/systems/fmgc/src/flightplanning/plans/ObservableFlightPlan.ts @@ -72,7 +72,11 @@ export class ObservableFlightPlan { return false; } - for (let i = 0; i < a?.radials.length; i++) { + if (a.radials?.length !== b.radials?.length) { + return false; + } + + for (let i = 0; i < a?.radials?.length ?? 0; i++) { const aRadial = a?.radials[i]; const bRadial = b?.radials[i]; @@ -81,7 +85,11 @@ export class ObservableFlightPlan { } } - for (let i = 0; i < a?.radii.length; i++) { + if (a.radii?.length !== b.radii?.length) { + return false; + } + + for (let i = 0; i < a?.radii?.length ?? 0; i++) { const aRadius = a?.radii[i]; const bRadius = b?.radii[i]; diff --git a/fbw-a32nx/src/systems/fmgc/src/flightplanning/uplink/SimBriefUplinkAdapter.ts b/fbw-a32nx/src/systems/fmgc/src/flightplanning/uplink/SimBriefUplinkAdapter.ts index 8046d5f6be4..f332345c40b 100644 --- a/fbw-a32nx/src/systems/fmgc/src/flightplanning/uplink/SimBriefUplinkAdapter.ts +++ b/fbw-a32nx/src/systems/fmgc/src/flightplanning/uplink/SimBriefUplinkAdapter.ts @@ -13,6 +13,7 @@ import { Coordinates, distanceTo } from 'msfs-geo'; import { FmsDisplayInterface } from '@fmgc/flightplanning/interface/FmsDisplayInterface'; import { FlightPlanPerformanceData } from '@fmgc/flightplanning/plans/performance/FlightPlanPerformanceData'; import { FmsErrorType } from '@fmgc/FmsError'; +// FIXME rogue import from EFB import { ISimbriefData, simbriefDataParser, @@ -481,17 +482,22 @@ export class SimBriefUplinkAdapter { url += `&username=${username}`; } - let ofp: ISimbriefData; try { - ofp = await fetch(url) - .then((result) => result.json()) - .then((json) => simbriefDataParser(json)); + const response = await fetch(url); + if (!response.ok) { + throw new Error(`HTTP Error: ${response.status} - ${response.statusText}`); + } + const body = await response.json(); + // SimBrief can return an error with an ok HTTP status code. + // In that case, the fetch.status starts with "Error:" + if (typeof body.fetch?.status === 'string' && body.fetch.status.startsWith('Error:')) { + throw new Error(`SimBrief: ${body.fetch.status}`); + } + return simbriefDataParser(body); } catch (e) { console.error('SimBrief OFP download failed'); throw e; } - - return ofp; } static getRouteFromOfp(ofp: ISimbriefData): OfpRoute { diff --git a/fbw-a32nx/src/systems/fmgc/src/guidance/GuidanceController.ts b/fbw-a32nx/src/systems/fmgc/src/guidance/GuidanceController.ts index 0fdd561e0e7..628d2187f56 100644 --- a/fbw-a32nx/src/systems/fmgc/src/guidance/GuidanceController.ts +++ b/fbw-a32nx/src/systems/fmgc/src/guidance/GuidanceController.ts @@ -34,6 +34,7 @@ import { XFLeg } from './lnav/legs/XF'; import { VMLeg } from './lnav/legs/VM'; import { ConsumerValue, EventBus } from '@microsoft/msfs-sdk'; import { FlightPhaseManagerEvents } from '@fmgc/flightphase'; +import { A32NX_Util } from '../../../shared/src/A32NX_Util'; // How often the (milliseconds) const GEOMETRY_RECOMPUTATION_TIMER = 5_000; @@ -135,8 +136,11 @@ export class GuidanceController { */ activeLegAlongTrackCompletePathDtg: NauticalMiles; - /** * Used for vertical guidance and other FMS tasks, such as triggering ENTER DEST DATA */ - alongTrackDistanceToDestination: NauticalMiles; + /** + * Along track distance to destination in nautical miles. + * Used for vertical guidance and other FMS tasks, such as triggering ENTER DEST DATA + */ + alongTrackDistanceToDestination?: number; focusedWaypointCoordinates: Coordinates = { lat: 0, long: 0 }; diff --git a/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/CD.ts b/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/CD.ts index 44c96eba046..8e9a0bf3ffa 100644 --- a/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/CD.ts +++ b/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/CD.ts @@ -6,7 +6,7 @@ import { Leg } from '@fmgc/guidance/lnav/legs/Leg'; import { PathVector, PathVectorType } from '@fmgc/guidance/lnav/PathVector'; import { SegmentType } from '@fmgc/flightplanning/FlightPlanSegment'; -import { NdbNavaid, VhfNavaid, Waypoint } from '@flybywiresim/fbw-sdk'; +import { Fix, Waypoint } from '@flybywiresim/fbw-sdk'; import { Coordinates, distanceTo, firstSmallCircleIntersection } from 'msfs-geo'; import { GuidanceParameters } from '@fmgc/guidance/ControlLaws'; import { LnavConfig } from '@fmgc/guidance/LnavConfig'; @@ -25,7 +25,7 @@ export class CDLeg extends Leg { constructor( private readonly course: DegreesTrue, private readonly dmeDistance: NauticalMiles, - private readonly origin: Waypoint | VhfNavaid | NdbNavaid, + private readonly origin: Fix, public readonly metadata: Readonly, segment: SegmentType, ) { diff --git a/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FA.ts b/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FA.ts index a485bb7185c..0ee2728314d 100644 --- a/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FA.ts +++ b/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FA.ts @@ -7,12 +7,10 @@ import { Coordinates } from '@fmgc/flightplanning/data/geo'; import { AirportSubsectionCode, AltitudeDescriptor, + Fix, LinePathVector, - NdbNavaid, PathVectorType, SectionCode, - VhfNavaid, - Waypoint, } from '@flybywiresim/fbw-sdk'; import { distanceTo, placeBearingDistance } from 'msfs-geo'; import { GuidanceParameters } from '@fmgc/guidance/ControlLaws'; @@ -39,7 +37,7 @@ export class FALeg extends Leg { * @param segment The flight plan segment this leg appears in. */ constructor( - public readonly fix: Waypoint | VhfNavaid | NdbNavaid, + public readonly fix: Fix, private readonly course: number, private readonly altitude: number, public readonly metadata: Readonly, diff --git a/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FD.ts b/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FD.ts index ff24c2f3529..198db65d060 100644 --- a/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FD.ts +++ b/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FD.ts @@ -6,7 +6,7 @@ import { Leg } from '@fmgc/guidance/lnav/legs/Leg'; import { PathVector, PathVectorType } from '@fmgc/guidance/lnav/PathVector'; import { SegmentType } from '@fmgc/flightplanning/FlightPlanSegment'; -import { NdbNavaid, VhfNavaid, Waypoint } from '@flybywiresim/fbw-sdk'; +import { Fix, Waypoint } from '@flybywiresim/fbw-sdk'; import { Coordinates, distanceTo, firstSmallCircleIntersection } from 'msfs-geo'; import { GuidanceParameters } from '@fmgc/guidance/ControlLaws'; import { LnavConfig } from '@fmgc/guidance/LnavConfig'; @@ -26,8 +26,8 @@ export class FDLeg extends Leg { constructor( private readonly course: DegreesTrue, private readonly dmeDistance: NauticalMiles, - private readonly fix: Waypoint | VhfNavaid | NdbNavaid, - private readonly navaid: Waypoint | VhfNavaid | NdbNavaid, + private readonly fix: Fix, + private readonly navaid: Fix, public readonly metadata: Readonly, segment: SegmentType, ) { diff --git a/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FM.ts b/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FM.ts index 099ec359522..84579840324 100644 --- a/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FM.ts +++ b/fbw-a32nx/src/systems/fmgc/src/guidance/lnav/legs/FM.ts @@ -9,7 +9,7 @@ import { Coordinates } from '@fmgc/flightplanning/data/geo'; import { Leg } from '@fmgc/guidance/lnav/legs/Leg'; import { PathVector, PathVectorType } from '@fmgc/guidance/lnav/PathVector'; import { LegMetadata } from '@fmgc/guidance/lnav/legs/index'; -import { NdbNavaid, VhfNavaid, Waypoint } from '@flybywiresim/fbw-sdk'; +import { Fix, Waypoint } from '@flybywiresim/fbw-sdk'; import { placeBearingDistance } from 'msfs-geo'; import { fixToFixGuidance } from '@fmgc/guidance/lnav/CommonGeometry'; @@ -29,7 +29,7 @@ export class FMLeg extends Leg { * @param segment The flight plan segment this leg appears in. */ constructor( - public readonly fix: Waypoint | VhfNavaid | NdbNavaid, + public readonly fix: Fix, private readonly course: number, public readonly metadata: Readonly, segment: SegmentType, diff --git a/fbw-a32nx/src/systems/fmgc/src/index.ts b/fbw-a32nx/src/systems/fmgc/src/index.ts deleted file mode 100644 index 47329a1425f..00000000000 --- a/fbw-a32nx/src/systems/fmgc/src/index.ts +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (c) 2021-2024 FlyByWire Simulations -// -// SPDX-License-Identifier: GPL-3.0 - -import { ApproachType, ApproachUtils, RadioUtils, RunwayUtils, a320EfisRangeSettings } from '@flybywiresim/fbw-sdk'; -import { DataManager } from '@fmgc/flightplanning/DataManager'; -import { CoRouteUplinkAdapter } from '@fmgc/flightplanning/uplink/CoRouteUplinkAdapter'; -import { EfisInterface } from '@fmgc/efis/EfisInterface'; -import { EventBus } from '@microsoft/msfs-sdk'; -import { FlightPlanRpcServer } from '@fmgc/flightplanning/rpc/FlightPlanRpcServer'; -import { FlightPlanService } from './flightplanning/FlightPlanService'; -import { NavigationDatabase, NavigationDatabaseBackend } from './NavigationDatabase'; -import { FlightPhaseManager } from './flightphase'; -import { GuidanceController } from './guidance/GuidanceController'; -import { EfisSymbols } from './efis/EfisSymbols'; -import { DescentPathBuilder } from './guidance/vnav/descent/DescentPathBuilder'; -import { initComponents, updateComponents, recallMessageById } from './components'; -import { Navigation, SelectedNavaidMode, SelectedNavaidType } from './navigation/Navigation'; -import { WaypointFactory } from './flightplanning/waypoints/WaypointFactory'; -import { WaypointEntryUtils } from './flightplanning/WaypointEntryUtils'; -import { FlightPlanIndex } from './flightplanning/FlightPlanManager'; -import { NavigationDatabaseService } from './flightplanning/NavigationDatabaseService'; -import { SimBriefUplinkAdapter } from './flightplanning/uplink/SimBriefUplinkAdapter'; -import { A320FlightPlanPerformanceData } from './flightplanning/plans/performance/FlightPlanPerformanceData'; -import { A320AircraftConfig } from '@fmgc/flightplanning/A320AircraftConfig'; -import { LandingSystemUtils } from './flightplanning/data/landingsystem'; - -function initFmgcLoop( - baseInstrument: BaseInstrument, - flightPlanService: FlightPlanService, -): void { - initComponents(baseInstrument, flightPlanService); -} - -function updateFmgcLoop(deltaTime: number): void { - updateComponents(deltaTime); -} - -export { - ApproachUtils, - RunwayUtils, - ApproachType, - FlightPlanService, - FlightPlanRpcServer, - A320FlightPlanPerformanceData, - NavigationDatabase, - NavigationDatabaseBackend, - NavigationDatabaseService, - FlightPlanIndex, - FlightPhaseManager, - GuidanceController, - initFmgcLoop, - updateFmgcLoop, - recallMessageById, - EfisInterface, - EfisSymbols, - DescentPathBuilder, - Navigation, - SelectedNavaidMode, - SelectedNavaidType, - WaypointFactory, - WaypointEntryUtils, - SimBriefUplinkAdapter, - CoRouteUplinkAdapter, - DataManager, - EventBus, - LandingSystemUtils, - RadioUtils, - a320EfisRangeSettings, - A320AircraftConfig, -}; diff --git a/fbw-a32nx/src/systems/fmgc/src/navigation/NavaidSelectionManager.ts b/fbw-a32nx/src/systems/fmgc/src/navigation/NavaidSelectionManager.ts index 0da9797b137..ef23a8675d7 100644 --- a/fbw-a32nx/src/systems/fmgc/src/navigation/NavaidSelectionManager.ts +++ b/fbw-a32nx/src/systems/fmgc/src/navigation/NavaidSelectionManager.ts @@ -9,6 +9,7 @@ import { NdbNavaid, VhfNavaid, VhfNavaidType, + isNdbNavaid, } from '@flybywiresim/fbw-sdk'; import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; import { FlightPlanLeg } from '@fmgc/flightplanning/legs/FlightPlanLeg'; @@ -163,7 +164,7 @@ export class NavaidSelectionManager { const distance = distanceTo(this.ppos, facility.dmeLocation ?? facility.location); if (this.isInLineOfSight(facility, distance)) { - // FIXME BCD frequency type in msfs-navdata... comparing floats is problematic + // FIXME BCD frequency type in nav db... comparing floats is problematic if (frequencies.has(facility.frequency)) { duplicateFrequencies.add(facility.frequency); } @@ -494,7 +495,7 @@ export class NavaidSelectionManager { // eslint-disable-next-line no-underscore-dangle const facility = segment.lastLeg?.definition.recommendedNavaid ?? null; - if (facility !== null && facility.subSectionCode === NavaidSubsectionCode.NdbNavaid) { + if (facility !== null && isNdbNavaid(facility)) { return facility; } } diff --git a/fbw-a32nx/src/systems/fmgc/src/navigation/Navigation.ts b/fbw-a32nx/src/systems/fmgc/src/navigation/Navigation.ts index f8ddde3cd86..9a4f178a4fc 100644 --- a/fbw-a32nx/src/systems/fmgc/src/navigation/Navigation.ts +++ b/fbw-a32nx/src/systems/fmgc/src/navigation/Navigation.ts @@ -4,14 +4,15 @@ import { Arinc429Register, IlsNavaid, NdbNavaid, VhfNavaid, VhfNavaidType, Icao } from '@flybywiresim/fbw-sdk'; -import { EventBus, FlightPlanService } from '@fmgc/index'; import { LandingSystemSelectionManager } from '@fmgc/navigation/LandingSystemSelectionManager'; import { NavaidSelectionManager, VorSelectionReason } from '@fmgc/navigation/NavaidSelectionManager'; import { NavaidTuner } from '@fmgc/navigation/NavaidTuner'; import { NavigationProvider } from '@fmgc/navigation/NavigationProvider'; import { NearbyFacilities } from '@fmgc/navigation/NearbyFacilities'; import { RequiredPerformance } from '@fmgc/navigation/RequiredPerformance'; +import { EventBus } from '@microsoft/msfs-sdk'; import { Coordinates } from 'msfs-geo'; +import { FlightPlanService } from '../flightplanning/FlightPlanService'; export enum SelectedNavaidType { None, @@ -82,6 +83,20 @@ export class Navigation implements NavigationProvider { (_, i) => `L:A32NX_ADIRS_ADR_${i + 1}_COMPUTED_AIRSPEED`, ); + private trueAirspeed: number | null = null; + + private static readonly trueAirspeedVars = Array.from( + { length: 3 }, + (_, i) => `L:A32NX_ADIRS_ADR_${i + 1}_TRUE_AIRSPEED`, + ); + + private staticAirTemperature: number | null = null; + + private static readonly staticAirTemperatureVars = Array.from( + { length: 3 }, + (_, i) => `L:A32NX_ADIRS_ADR_${i + 1}_STATIC_AIR_TEMPERATURE`, + ); + private readonly navaidSelectionManager: NavaidSelectionManager; private readonly landingSystemSelectionManager: LandingSystemSelectionManager; @@ -117,9 +132,7 @@ export class Navigation implements NavigationProvider { this.updatePosition(); this.updateRadioHeight(); - this.updateBaroAltitude(); - this.updatePressureAltitude(); - this.updateComputedAirspeed(); + this.updateAirData(); NearbyFacilities.getInstance().update(deltaTime); @@ -177,16 +190,13 @@ export class Navigation implements NavigationProvider { this.radioHeight = null; } - private updateBaroAltitude(): void { + private updateAirData(): void { this.baroAltitude = this.getAdiruValue(Navigation.baroAltitudeVars); - } - - private updatePressureAltitude(): void { this.pressureAltitude = this.getAdiruValue(Navigation.pressureAltitudeVars); - } - private updateComputedAirspeed(): void { this.computedAirspeed = this.getAdiruValue(Navigation.computedAirspeedVars); + this.trueAirspeed = this.getAdiruValue(Navigation.trueAirspeedVars); + this.staticAirTemperature = this.getAdiruValue(Navigation.staticAirTemperatureVars); } private updatePosition(): void { @@ -219,6 +229,14 @@ export class Navigation implements NavigationProvider { return this.computedAirspeed; } + public getTrueAirspeed(): number | null { + return this.trueAirspeed; + } + + public getStaticAirTemperature(): number | null { + return this.staticAirTemperature; + } + public getRadioHeight(): number | null { return this.radioHeight; } diff --git a/fbw-a32nx/src/systems/fmgc/src/navigation/NearbyFacilities.ts b/fbw-a32nx/src/systems/fmgc/src/navigation/NearbyFacilities.ts index 2b11dea715b..b167ff927d9 100644 --- a/fbw-a32nx/src/systems/fmgc/src/navigation/NearbyFacilities.ts +++ b/fbw-a32nx/src/systems/fmgc/src/navigation/NearbyFacilities.ts @@ -65,7 +65,7 @@ export class NearbyFacilities { const database = NavigationDatabaseService.activeDatabase.backendDatabase; if (this.pposValid && database) { - // FIXME implement a more efficient diff-type interface in msfs-navdata + // FIXME implement a more efficient diff-type interface in the navdb this.nearbyAirports = await database.getNearbyAirports( this.ppos, this.radius, @@ -75,7 +75,7 @@ export class NearbyFacilities { ); this.nearbyNdbNavaids = await database.getNearbyNdbNavaids(this.ppos, this.radius, this.limit); this.nearbyVhfNavaids = await database.getNearbyVhfNavaids(this.ppos, this.radius, this.limit); - // FIXME rename this method in msfs-navdata + // FIXME rename this method in navdb this.nearbyWaypoints = await database.getWaypointsInRange(this.ppos, this.radius, this.limit); } } diff --git a/fbw-a32nx/src/systems/fmgc/src/navigation/RequiredPerformance.ts b/fbw-a32nx/src/systems/fmgc/src/navigation/RequiredPerformance.ts index e03bc10c1f9..9823d701da9 100644 --- a/fbw-a32nx/src/systems/fmgc/src/navigation/RequiredPerformance.ts +++ b/fbw-a32nx/src/systems/fmgc/src/navigation/RequiredPerformance.ts @@ -1,11 +1,11 @@ // Copyright (c) 2022 FlyByWire Simulations // SPDX-License-Identifier: GPL-3.0 -import { EventBus, FlightPlanService } from '@fmgc/index'; import { FmgcFlightPhase } from '@shared/flightphase'; import { FlightArea } from './FlightArea'; -import { ConsumerValue } from '@microsoft/msfs-sdk'; +import { ConsumerValue, EventBus } from '@microsoft/msfs-sdk'; import { FlightPhaseManagerEvents } from '@fmgc/flightphase'; +import { FlightPlanService } from '../flightplanning/FlightPlanService'; const rnpDefaults: Record = { [FlightArea.Takeoff]: 1, diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/McduBaseInstrument.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/McduBaseInstrument.ts new file mode 100644 index 00000000000..3590001f852 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/McduBaseInstrument.ts @@ -0,0 +1,43 @@ +// Copyright (c) 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { FsBaseInstrument } from '@microsoft/msfs-sdk'; +import { McduFsInstrument } from './McduFsInstrument'; + +/** + * The MCDU Instrument. + */ +export class McduBaseInstrument extends FsBaseInstrument { + /** @inheritdoc */ + public get isInteractive(): boolean { + return true; + } + + /** @inheritdoc */ + public constructInstrument(): McduFsInstrument { + return new McduFsInstrument(this); + } + + /** @inheritdoc */ + get templateID(): string { + return 'A32NX_MCDU'; + } + + /** @inheritdoc */ + public onPowerOn(): void { + super.onPowerOn(); + + // TODO why fsInstrument undefined + //this.fsInstrument.onPowerOn(); + } + + /** @inheritdoc */ + public onShutDown(): void { + super.onShutDown(); + + // TODO why fsInstrument undefined + //this.fsInstrument.onPowerOff(); + } +} + +registerInstrument('a32nx-mcdu', McduBaseInstrument); diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/McduFsInstrument.tsx b/fbw-a32nx/src/systems/instruments/src/MCDU/McduFsInstrument.tsx new file mode 100644 index 00000000000..05f5e495535 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/McduFsInstrument.tsx @@ -0,0 +1,125 @@ +// Copyright (c) 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { + Clock, + DebounceTimer, + EventBus, + FSComponent, + FsInstrument, + HEventPublisher, + InstrumentBackplane, + MappedSubject, + Subject, +} from '@microsoft/msfs-sdk'; +import { A320_Neo_CDU_MainDisplay } from './legacy/A320_Neo_CDU_MainDisplay'; + +import './mcdu.scss'; + +export class McduFsInstrument implements FsInstrument { + private static readonly INIT_DURATION = 1000; + + protected readonly bus = new EventBus(); + + private readonly isPowered = Subject.create(false); + + private readonly backplane = new InstrumentBackplane(); + private readonly clock = new Clock(this.bus); + private readonly hEventPublisher = new HEventPublisher(this.bus); + + //private readonly isFailedKey = A320Failure.Mcdu1; + private readonly isFailed = Subject.create(false); + + private readonly isOperating = Subject.create(false); + private readonly initTimer = new DebounceTimer(); + + private readonly legacyFms = new A320_Neo_CDU_MainDisplay(this.bus); + + private lastTime = Date.now(); + + /** + * Creates a new instance of FsInstrument. + * @param instrument This instrument's parent BaseInstrument. + * @param config The general avionics configuration object. + */ + constructor(public readonly instrument: BaseInstrument) { + // force enable animations + document.documentElement.classList.add('animationsEnabled'); + + this.renderComponents(); + + this.backplane.addInstrument('Clock', this.clock); + this.backplane.addPublisher('HEvent', this.hEventPublisher); + + this.doInit(); + } + + /** @inheritdoc */ + private renderComponents(): void { + FSComponent.render( + <> +

+
+ , + document.getElementById('MCDU_CONTENT'), + ); + + // Remove "instrument didn't load" text + document.getElementById('MCDU_CONTENT')?.querySelector(':scope > h1')?.remove(); + } + + /** @inheritdoc */ + public Update(): void { + this.backplane.onUpdate(); + + // TODO deltaTime + const deltaTime = Date.now() - this.lastTime; + this.lastTime = Date.now(); + this.legacyFms.onUpdate(deltaTime); + } + + /** @inheritdoc */ + public onInteractionEvent(args: string[]): void { + this.hEventPublisher.dispatchHEvent(args[0]); + } + + /** @inheritdoc */ + public onFlightStart(): void { + // noop and not useful + } + + /** @inheritdoc */ + public onGameStateChanged(_oldState: GameState, _newState: GameState): void { + // noop + } + + /** @inheritdoc */ + public onSoundEnd(_soundEventId: Name_Z): void { + // noop + } + + public onPowerOn(): void { + this.isPowered.set(true); + this.legacyFms.onPowerOn(); + } + + public onPowerOff(): void { + this.isPowered.set(false); + } + + /** Init instrument. */ + private doInit(): void { + MappedSubject.create(this.isPowered, this.isFailed).sub(([isPowered, isFailed]) => { + if (isPowered && !isFailed) { + this.initTimer.schedule(() => this.isOperating.set(true), McduFsInstrument.INIT_DURATION); + } else { + this.initTimer.clear(); + this.isOperating.set(false); + } + }, true); + + this.backplane.init(); + + this.legacyFms.connectedCallback(); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/config.json b/fbw-a32nx/src/systems/instruments/src/MCDU/config.json new file mode 100644 index 00000000000..2091076951f --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/config.json @@ -0,0 +1,6 @@ +{ + "isInteractive": true, + "extraDeps": [ + "/fbw-common/src/systems/navdata" + ] +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Field.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Field.ts new file mode 100644 index 00000000000..4a841cfe7c6 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Field.ts @@ -0,0 +1,222 @@ +import { NXFictionalMessages, NXSystemMessages } from '../messages/NXSystemMessages'; +import { Column } from './A320_Neo_CDU_Format'; +import { Keypad } from './A320_Neo_CDU_Keypad'; + +export type SelectedCallback = ( + /** The parsed and formatted value. */ + value: string | number | null, +) => void; + +export class CDU_Field { + protected currentValue: string | number | null = null; + + constructor( + protected mcdu, + protected selectedCallback: SelectedCallback, + ) {} + setOptions(options: CDU_SingleValueFieldOptions) { + for (const option in options) { + this[option] = options[option]; + } + } + + getValue() { + return ''; + } + + onSelect(_value?: string | number | null) { + this.selectedCallback(this.currentValue); + } + + getFieldAsColumnParameters() { + const text = this.getValue(); + let color = Column.white; + + if (text.includes('[color]amber')) { + color = Column.amber; + } else if (text.includes('[color]red')) { + color = Column.red; + } else if (text.includes('[color]green')) { + color = Column.green; + } else if (text.includes('[color]cyan')) { + color = Column.cyan; + } else if (text.includes('[color]magenta')) { + color = Column.magenta; + } else if (text.includes('[color]yellow')) { + color = Column.yellow; + } else if (text.includes('[color]inop')) { + color = Column.inop; + } + + return [ + this.onSelect.bind(this), + text + .replace('[color]white', '') + .replace('[color]amber', '') + .replace('[color]red', '') + .replace('[color]green', '') + .replace('[color]cyan', '') + .replace('[color]magenta', '') + .replace('[color]yellow', '') + .replace('[color]inop', ''), + color, + ]; + } +} + +/** + * @description Placeholder field that shows "NOT YET IMPLEMENTED" message when selected + */ +export class CDU_InopField extends CDU_Field { + private value; + + // mcdu is A320_Neo_CDU_MainDisplay, but typing it would be a circular ref + /** + * @param inopColor whether to append "[color]inop" to the value + */ + constructor(mcdu, value: string, inopColor: boolean = true) { + super(mcdu, () => {}); + this.value = inopColor ? `${value}[color]inop` : value; + } + getValue() { + return this.value; + } + + onSelect() { + this.mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + super.onSelect(); + } +} + +interface CDU_SingleValueFieldOptions { + /** Default false. */ + clearable?: boolean; + /** Default "". */ + emptyValue?: string; + /** Default Infinity. */ + maxLength?: number; + /** Default Infinity. */ + minValue?: number; + /** Default Infinity. */ + maxValue?: number; + /** Default undefined. */ + maxDisplayedDecimalPlaces?: number; + /** Default "". */ + prefix?: string; + /** Default "". */ + suffix?: string; + /** Default undefined. */ + isValid?: (value: string | number | null) => boolean; +} + +export class CDU_SingleValueField extends CDU_Field { + protected clearable = false; + protected emptyValue = ''; + protected maxLength = Infinity; + protected minValue = -Infinity; + protected maxValue = Infinity; + protected prefix = ''; + protected suffix = ''; + protected maxDisplayedDecimalPlaces?: number; + protected isValid?: (value: string | number | null) => boolean; + + constructor( + mcdu, // A320_Neo_CDU_MainDisplay, but typing it would be a circular ref + protected type: string | number, + protected currentValue: string | number | null, + options: CDU_SingleValueFieldOptions, + selectedCallback: SelectedCallback, + ) { + super(mcdu, selectedCallback); + this.setOptions(options); + } + + /** + * @returns {string} + */ + getValue() { + let value = this.currentValue; + if (value === '' || value == null) { + return this.emptyValue; + } + if (this.type === 'number' && isFinite(this.maxDisplayedDecimalPlaces)) { + value = Number(value).toFixed(this.maxDisplayedDecimalPlaces); + } + return `${this.prefix}${value}${this.suffix}`; + } + + /** + * @param {*} value + */ + setValue(value) { + // Custom isValid callback + if (value.length === 0 || (this.isValid && !this.isValid(value))) { + this.mcdu.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + switch (this.type) { + case 'string': + // Check max length + if (value.length > this.maxLength) { + this.mcdu.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + break; + case 'int': { + // Make sure value is an integer and is within the min/max + const valueAsInt = Number.parseInt(value, 10); + if (!isFinite(valueAsInt) || value.includes('.')) { + this.mcdu.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + if (valueAsInt > this.maxValue || valueAsInt < this.minValue) { + this.mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + value = valueAsInt; + break; + } + case 'number': { + // Make sure value is a valid number and is within the min/max + const valueAsFloat = Number.parseFloat(value); + if (!isFinite(valueAsFloat)) { + this.mcdu.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + if (valueAsFloat > this.maxValue || valueAsFloat < this.minValue) { + this.mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + value = valueAsFloat; + break; + } + } + // Update the value + this.currentValue = value; + return true; + } + clearValue() { + if (this.clearable) { + if (this.type === 'string') { + this.currentValue = ''; + } else { + this.currentValue = null; + } + } else { + this.mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + } + } + onSelect(value?: string | number | null) { + if (value === Keypad.clrValue) { + this.clearValue(); + } else { + if (!this.setValue(value)) { + this.mcdu.setScratchpadUserData(value); + } + } + super.onSelect(); + } +} + +// TODO: Create classes for multi value fields diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Format.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Format.ts new file mode 100644 index 00000000000..df7afef36c4 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Format.ts @@ -0,0 +1,150 @@ +// Copyright (c) 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +type ColorValue = 'amber' | 'red' | 'green' | 'cyan' | 'white' | 'magenta' | 'yellow' | 'inop'; +interface ColorAttribute { + color: ColorValue; +} + +type SizeValue = 'small' | 'big'; +interface SizeAttribute { + size: SizeValue; +} + +interface AlignAttribute { + alignRight: boolean; +} + +type Attribute = ColorAttribute | SizeAttribute | AlignAttribute; + +/** Used to displayed data on a mcdu page when using the formatting helper */ +export class Column { + public static left: AlignAttribute = { alignRight: false }; + public static right: AlignAttribute = { alignRight: true }; + public static small: SizeAttribute = { size: 'small' }; + public static big: SizeAttribute = { size: 'big' }; + public static amber: ColorAttribute = { color: 'amber' }; + public static red: ColorAttribute = { color: 'red' }; + public static green: ColorAttribute = { color: 'green' }; + public static cyan: ColorAttribute = { color: 'cyan' }; + public static white: ColorAttribute = { color: 'white' }; + public static magenta: ColorAttribute = { color: 'magenta' }; + public static yellow: ColorAttribute = { color: 'yellow' }; + public static inop: ColorAttribute = { color: 'inop' }; + + public raw: string; + protected color: string; + public length: number; + public anchorPos: number; + protected size: string | null; + + /** + * @param index - valid range from 0 to 23 + * @param text - text to be displayed + * @param att - attributes of the text, e.g. text size, color and/or alignment + */ + constructor( + protected index: number, + text: string, + ...att: Attribute[] + ) { + this.index = index; + this.raw = text; + const colorAttr = att.find((e) => 'color' in e); + this.color = colorAttr?.color ?? Column.white.color; + this.length = text.length; + this.anchorPos = att.find((e) => 'alignRight' in e) ? index - this.length + 1 : index; + const sizeAttr = att.find((e) => 'size' in e); + this.size = sizeAttr ? sizeAttr.size : null; + } + + /** + * Returns a styled/formatted string. + */ + get text(): string { + return `${this.size !== null ? '{' + this.size + '}' : ''}{${this.color}}${this.raw}{end}${this.size !== null ? '{end}' : ''}`; + } + + /** + * @param text - text to be displayed + */ + set text(text: string) { + this.raw = text; + this.length = text.length; + + // if text is right aligned => update anchor position + if (this.index !== this.anchorPos) { + this.anchorPos = this.index - this.length + 1; + } + } + + /** + * @param att - attributes of the text, e.g. text size and/or color + */ + updateAttributes(...att: { color?: string; size?: string }[]) { + const colorAttr = att.find((e) => e.color) as { color: string } | undefined; + this.color = colorAttr?.color ?? this.color; + const sizeAttr = att.find((e) => e.size); + this.size = sizeAttr?.size ?? this.size; + } + + /** + * @param text - text to be displayed + * @param att - attributes of the text, e.g. text size and/or color + */ + update(text: string, ...att: { color?: string; size?: string }[]) { + this.text = text; + this.updateAttributes(...att); + } +} + +/** + * Returns a formatted mcdu page template + * @param lines - mcdu lines + * @returns mcdu template + */ +export const FormatTemplate = (lines: Column[][]): string[][] => lines.map((line) => FormatLine(...line)); + +/** + * Returns a formatted mcdu line + * @param {...Column} columns + * @returns {string[]} + */ +function FormatLine(...columns: Column[]): string[] { + columns.sort((a, b) => a.anchorPos - b.anchorPos); + + let line = ''.padStart(24); + let pos = -1; // refers to an imaginary index on the 24 char limited mcdu line + let index = 0; // points at the "cursor" of the actual line + + for (const column of columns) { + /* --------> populating text from left to right --------> */ + const newStart = column.anchorPos; + const newEnd = newStart + column.length; + + // prevent adding empty or invalid stuff + if (column.length === 0 || newEnd < 0 || newStart > 23 || newEnd <= pos) { + continue; + } + + if (pos === -1) { + pos = 0; + } + + // removes text overlap + column.raw = column.raw.slice( + Math.max(0, pos - newStart), + Math.min(column.length, Math.max(1, column.length + 24 - newEnd)), + ); + + const limMin = Math.max(0, newStart - pos); + const limMax = Math.min(24, newEnd - pos); + + line = line.slice(0, index + limMin) + column.text + line.slice(index + limMax); + index += limMin + column.text.length; + pos = newEnd; + } + + // '{small}{end}{big}{end}' fixes the lines "jumping" (line moves up or down a few pixels) when entering small and large content into the same line. + return [line.replace(/\s/g, '{sp}') + '{small}{end}{big}{end}']; +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Keypad.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Keypad.ts new file mode 100644 index 00000000000..6a46621a3ed --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Keypad.ts @@ -0,0 +1,89 @@ +import { FmgcFlightPhase } from '@shared/flightphase'; +import { CDUDataIndexPage } from '../legacy_pages/A320_Neo_CDU_DataIndexPage'; +import { CDUDirectToPage } from '../legacy_pages/A320_Neo_CDU_DirectToPage'; +import { CDUFlightPlanPage } from '../legacy_pages/A320_Neo_CDU_FlightPlanPage'; +import { CDUInitPage } from '../legacy_pages/A320_Neo_CDU_InitPage'; +import { CDUMenuPage } from '../legacy_pages/A320_Neo_CDU_MenuPage'; +import { CDUNavRadioPage } from '../legacy_pages/A320_Neo_CDU_NavRadioPage'; +import { CDUPerformancePage } from '../legacy_pages/A320_Neo_CDU_PerformancePage'; +import { CDUProgressPage } from '../legacy_pages/A320_Neo_CDU_ProgressPage'; +import { CDUSecFplnMain } from '../legacy_pages/A320_Neo_CDU_SecFplnMain'; +import { CDUAtcMenu } from '../legacy_pages/atsu/A320_Neo_CDU_ATC_Menu'; + +export class Keypad { + private static readonly _AvailableKeys = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + + public static readonly clrValue = '\xa0\xa0\xa0\xa0\xa0CLR'; + public static readonly ovfyValue = '\u0394'; + + private readonly _keys = { + AIRPORT: () => this.mcdu.onAirport(), + ATC: () => CDUAtcMenu.ShowPage(this.mcdu), + DATA: () => CDUDataIndexPage.ShowPage1(this.mcdu), + DIR: () => { + this.mcdu.eraseTemporaryFlightPlan(); + CDUDirectToPage.ShowPage(this.mcdu); + }, + FPLN: () => CDUFlightPlanPage.ShowPage(this.mcdu), + FUEL: () => this.mcdu.goToFuelPredPage(), + INIT: () => { + if (this.mcdu.flightPhaseManager.phase === FmgcFlightPhase.Done) { + this.mcdu.flightPhaseManager.changePhase(FmgcFlightPhase.Preflight); + } + CDUInitPage.ShowPage1(this.mcdu); + }, + MENU: () => CDUMenuPage.ShowPage(this.mcdu), + PERF: () => { + if (this.mcdu.flightPhaseManager.phase === FmgcFlightPhase.Done) { + this.mcdu.flightPhaseManager.changePhase(FmgcFlightPhase.Preflight); + } + CDUPerformancePage.ShowPage(this.mcdu); + }, + PROG: () => CDUProgressPage.ShowPage(this.mcdu), + RAD: () => CDUNavRadioPage.ShowPage(this.mcdu), + SEC: () => CDUSecFplnMain.ShowPage(this.mcdu), + + PREVPAGE: () => this.mcdu.onPrevPage(), + NEXTPAGE: () => this.mcdu.onNextPage(), + UP: () => this.mcdu.onUp(), + DOWN: () => this.mcdu.onDown(), + + CLR: () => this.mcdu.onClr(), + CLR_Held: () => this.mcdu.onClrHeld(), + DIV: () => this.mcdu.onDiv(), + DOT: () => this.mcdu.onDot(), + OVFY: () => this.mcdu.onOvfy(), + PLUSMINUS: () => this.mcdu.onPlusMinus(), + SP: () => this.mcdu.onSp(), + + BRT: (side) => this.mcdu.onBrightnessKey(side, 1), + DIM: (side) => this.mcdu.onBrightnessKey(side, -1), + }; + + constructor(private mcdu) { + for (const letter of Keypad._AvailableKeys) { + this._keys[letter] = () => this.mcdu.onLetterInput(letter); + } + } + + /** + * Handle CDU key presses + * @param {unknown} value + * @param {'L' | 'R'} side + * @returns true if handled + */ + onKeyPress(value, side) { + const action = this._keys[value]; + if (!action) { + return false; + } + + const cur = this.mcdu.page.Current; + setTimeout(() => { + if (this.mcdu.page.Current === cur) { + action(side); + } + }, this.mcdu.getDelaySwitchPage()); + return true; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_MainDisplay.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_MainDisplay.ts new file mode 100644 index 00000000000..a62262dc83c --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_MainDisplay.ts @@ -0,0 +1,1676 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { NXDataStore, UpdateThrottler } from '@flybywiresim/fbw-sdk'; +import { FMCMainDisplay } from './A32NX_FMCMainDisplay'; +import { recallMessageById } from '@fmgc/components'; +import { Keypad } from './A320_Neo_CDU_Keypad'; +import { NXNotifManager, NXPopUp } from '@shared/NxNotif'; +import { McduMessage, NXFictionalMessages, NXSystemMessages, TypeIIMessage } from '../messages/NXSystemMessages'; +import { McduServerClient } from '@simbridge/index'; +import { ScratchpadDataLink, ScratchpadDisplay } from './A320_Neo_CDU_Scratchpad'; +import { CDUMenuPage } from '../legacy_pages/A320_Neo_CDU_MenuPage'; +import { CDUFuelPredPage } from '../legacy_pages/A320_Neo_CDU_FuelPredPage'; +import { FmgcFlightPhase } from '@shared/flightphase'; +import { CDU_Field } from './A320_Neo_CDU_Field'; +import { AtsuStatusCodes } from '@datalink/common'; +import { EventBus, GameStateProvider, HEvent } from '@microsoft/msfs-sdk'; +import { CDUInitPage } from '../legacy_pages/A320_Neo_CDU_InitPage'; +import { LegacyFmsPageInterface, LskCallback, LskDelayFunction } from './LegacyFmsPageInterface'; +import { LegacyAtsuPageInterface } from './LegacyAtsuPageInterface'; + +export class A320_Neo_CDU_MainDisplay + extends FMCMainDisplay + implements LegacyFmsPageInterface, LegacyAtsuPageInterface +{ + private static readonly MIN_BRIGHTNESS = 0.5; + private static readonly MAX_BRIGHTNESS = 8; + + private static readonly H_EVENT_PREFIX = 'A320_Neo_CDU_'; + + private readonly minPageUpdateThrottler = new UpdateThrottler(100); + private readonly mcduServerConnectUpdateThrottler = new UpdateThrottler(1000); + private readonly powerCheckUpdateThrottler = new UpdateThrottler(500); + + public readonly fmgcMesssagesListener = RegisterViewListener('JS_LISTENER_SIMVARS', null, true); + + private readonly _keypad = new Keypad(this); + + private _title = undefined; + private _titleLeft = ''; + private _pageCurrent = undefined; + private _pageCount = undefined; + private _labels = []; + private _lines = []; + private scratchpadDisplay = null; + private _scratchpad = null; + private scratchpads?: Record<'MCDU' | 'FMGC' | 'ATSU' | 'AIDS' | 'CFDS', ScratchpadDataLink>; + private _arrows = [false, false, false, false]; + + private annunciators = { + left: { + // note these must match the base names in the model xml + fmgc: false, + fail: false, + mcdu_menu: false, + fm1: false, + ind: false, + rdy: false, + blank: false, + fm2: false, + }, + right: { + fmgc: false, + fail: false, + mcdu_menu: false, + fm1: false, + ind: false, + rdy: false, + blank: false, + fm2: false, + }, + }; + + /** MCDU request flags from subsystems */ + private requests = { + AIDS: false, + ATSU: false, + CFDS: false, + FMGC: false, + }; + private _lastAtsuMessageCount = 0; + private leftBrightness = 0; + private rightBrightness = 0; + public onLeftInput: LskCallback[] = []; + public onRightInput: LskCallback[] = []; + public leftInputDelay: LskDelayFunction[] = []; + public rightInputDelay: LskDelayFunction[] = []; + private _activeSystem: 'FMGC' | 'ATSU' | 'AIDS' | 'CFDS' = 'FMGC'; + private inFocus = false; + private lastInput = new Date(0); + private clrStop = false; + private allSelected = false; + private updateRequest = false; + private initB = false; + private lastPowerState = null; + public readonly PageTimeout = { + Fast: 500, + Medium: 1000, + Dyn: 1500, + Default: 2000, + Slow: 3000, + }; + public returnPageCallback: typeof EmptyCallback.Void | null = null; + + public SelfPtr: ReturnType | false = false; + + public page = { + Current: 0, + Clear: 0, + AirportsMonitor: 1, + AirwaysFromWaypointPage: 2, + // AirwaysFromWaypointPageGetAllRows: 3, + AvailableArrivalsPage: 4, + AvailableArrivalsPageVias: 5, + AvailableDeparturesPage: 6, + AvailableFlightPlanPage: 7, + DataIndexPage1: 8, + DataIndexPage2: 9, + DirectToPage: 10, + FlightPlanPage: 11, + FuelPredPage: 12, + GPSMonitor: 13, + HoldAtPage: 14, + IdentPage: 15, + InitPageA: 16, + InitPageB: 17, + IRSInit: 18, + IRSMonitor: 19, + IRSStatus: 20, + IRSStatusFrozen: 21, + LateralRevisionPage: 22, + MenuPage: 23, + NavaidPage: 24, + NavRadioPage: 25, + NewWaypoint: 26, + PerformancePageTakeoff: 27, + PerformancePageClb: 28, + PerformancePageCrz: 29, + PerformancePageDes: 30, + PerformancePageAppr: 31, + PerformancePageGoAround: 32, + PilotsWaypoint: 33, + PosFrozen: 34, + PositionMonitorPage: 35, + ProgressPage: 36, + ProgressPageReport: 37, + ProgressPagePredictiveGPS: 38, + SelectedNavaids: 39, + SelectWptPage: 40, + VerticalRevisionPage: 41, + WaypointPage: 42, + AOCInit: 43, + AOCInit2: 44, + AOCOfpData: 45, + AOCOfpData2: 46, + AOCMenu: 47, + AOCRequestWeather: 48, + AOCRequestAtis: 49, + AOCDepartRequest: 50, + ATCMenu: 51, + ATCModify: 52, + ATCAtis: 53, + ATCMessageRecord: 54, + ATCMessageMonitoring: 55, + ATCConnection: 56, + ATCNotification: 57, + ATCConnectionStatus: 58, + ATCPositionReport1: 59, + ATCPositionReport2: 60, + ATCPositionReport3: 61, + ATCFlightRequest: 62, + ATCUsualRequest: 63, + ATCGroundRequest: 64, + ATCReports: 65, + ATCEmergency: 66, + ATCComLastId: 67, // This is needed for automatic page changes triggered by DCDU + ATSUMenu: 68, + ATSUDatalinkStatus: 69, + ClimbWind: 70, + CruiseWind: 71, + DescentWind: 72, + FixInfoPage: 73, + AOCRcvdMsgs: 74, + AOCSentMsgs: 75, + AOCFreeText: 76, + StepAltsPage: 77, + ATCDepartReq: 78, + }; + + private mcduServerClient?: McduServerClient; + + private readonly emptyLines = { + lines: [ + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ], + scratchpad: '', + title: '', + titleLeft: '', + page: '', + arrows: [false, false, false, false], + annunciators: { + fmgc: false, + fail: false, + mcdu_menu: false, + fm1: false, + ind: false, + rdy: false, + blank: false, + fm2: false, + }, + displayBrightness: 0, + integralBrightness: 0, + }; + + private _titleLeftElement?: HTMLElement; + private _titleElement?: HTMLElement; + private _pageCurrentElement?: HTMLElement; + private _pageCountElement?: HTMLElement; + private _labelElements?: any[]; + private _lineElements?: any[]; + + public pageRedrawCallback?: () => void; + public pageUpdate?: () => void; + + private arrowHorizontal?: HTMLSpanElement; + private arrowVertical?: HTMLSpanElement; + + private check_focus?: ReturnType; + private check_clr?: ReturnType; + + private printing = false; + + /** The following events remain due to shared use by the keypad and keyboard type entry */ + public onLetterInput = (l: string) => this.scratchpad.addChar(l); + public onSp = () => this.scratchpad.addChar(' '); + public onDiv = () => this.scratchpad.addChar('/'); + public onDot = () => this.scratchpad.addChar('.'); + public onClr = () => this.scratchpad.clear(); + public onClrHeld = () => this.scratchpad.clearHeld(); + public onPlusMinus = (defaultKey = '-') => this.scratchpad.plusMinus(defaultKey); + public onLeftFunction = (f) => this.onLsk(this.onLeftInput[f], this.leftInputDelay[f]); + public onRightFunction = (f) => this.onLsk(this.onRightInput[f], this.rightInputDelay[f]); + public onOvfy = () => this.scratchpad.addChar('Δ'); + public onUnload = () => {}; + + public onPrevPage = () => {}; + public onNextPage = () => {}; + public onUp = () => {}; + public onDown = () => {}; + + constructor(bus: EventBus) { + super(bus); + this.setupFmgcTriggers(); + } + + // TODO this really belongs in the FMCMainDisplay, not the CDU + private setupFmgcTriggers() { + Coherent.on('A32NX_FMGC_SEND_MESSAGE_TO_MCDU', (message) => { + this.addMessageToQueue( + new TypeIIMessage(message.text, message.color === 'Amber'), + () => false, + () => { + if (message.clearable) { + recallMessageById(message.id); + } + }, + ); + }); + + Coherent.on('A32NX_FMGC_RECALL_MESSAGE_FROM_MCDU_WITH_ID', (text) => { + this.removeMessageFromQueue(text); + }); + } + + public get templateID() { + return 'A320_Neo_CDU'; + } + + public get isInteractive() { + return true; + } + + public connectedCallback() { + RegisterViewListener('JS_LISTENER_KEYEVENT', () => { + console.log('JS_LISTENER_KEYEVENT registered.'); + RegisterViewListener('JS_LISTENER_FACILITY', () => { + console.log('JS_LISTENER_FACILITY registered.'); + }); + }); + + this.bus + .getSubscriber() + .on('hEvent') + .handle((ev) => { + if (ev.startsWith(A320_Neo_CDU_MainDisplay.H_EVENT_PREFIX)) { + this.onEvent(ev.slice(A320_Neo_CDU_MainDisplay.H_EVENT_PREFIX.length)); + } + }); + + this.Init(); + } + + // The callback is called when an event is received from the McduServerClient's socket. + // See https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#events for possible events. + // This will be used as a parameter when the McduServerClient's connect method is called. + // this.mcduServerClient.connect(this, this.mcduServerClientEventHandler); + private mcduServerClientEventHandler(event) { + switch (event.type) { + case 'open': { + console.log(`[MCDU] Websocket connection to SimBridge opened. (${McduServerClient.url()})`); + new NXNotifManager().showNotification({ + title: 'MCDU CONNECTED', + message: 'A32NX MCDU successfully connected to SimBridge MCDU Server.', + timeout: 5000, + }); + this.sendToMcduServerClient('mcduConnected'); + this.sendUpdate(); + break; + } + case 'close': { + console.log(`[MCDU] Websocket connection to SimBridge closed. (${McduServerClient.url()})`); + break; + } + case 'error': { + console.log(`[MCDU] Websocket connection to SimBridge error. (${McduServerClient.url()}): ${event.get()}`); + break; + } + case 'message': { + const [messageType, ...args] = event.data.split(':'); + if (messageType === 'event') { + // backwards compatible with the old MCDU server... + // accepts either event:button_name (old), or event:side:button_name (current) + const mcduIndex = args.length > 1 && args[0] === 'right' ? 2 : 1; + const button = args.length > 1 ? args[1] : args[0]; + SimVar.SetSimVarValue(`H:A320_Neo_CDU_${mcduIndex}_BTN_${button}`, 'number', 0); + SimVar.SetSimVarValue(`L:A32NX_MCDU_PUSH_ANIM_${mcduIndex}_${button}`, 'Number', 1); + } + if (messageType === 'requestUpdate') { + this.sendUpdate(); + } + break; + } + } + } + + private getChildById(elementId: string): HTMLElement | null { + return document.getElementById(elementId); + } + + protected Init() { + super.Init(); + + this.generateHTMLLayout(this.getChildById('Mainframe') || this); + + this.scratchpadDisplay = new ScratchpadDisplay(this, this.getChildById('in-out')); + this.scratchpads = { + MCDU: new ScratchpadDataLink(this, this.scratchpadDisplay, 'MCDU', false), + FMGC: new ScratchpadDataLink(this, this.scratchpadDisplay, 'FMGC'), + ATSU: new ScratchpadDataLink(this, this.scratchpadDisplay, 'ATSU'), + AIDS: new ScratchpadDataLink(this, this.scratchpadDisplay, 'AIDS'), + CFDS: new ScratchpadDataLink(this, this.scratchpadDisplay, 'CFDS'), + }; + this.activateMcduScratchpad(); + + try { + // note: without this, resetting mcdu kills camera + if (this.scratchpadDisplay && this.scratchpadDisplay.guid) { + Coherent.trigger('UNFOCUS_INPUT_FIELD', this.scratchpadDisplay.guid); + } + } catch (e) { + console.error(e); + } + + this.initKeyboardScratchpad(); + this._titleLeftElement = this.getChildById('title-left'); + this._titleElement = this.getChildById('title'); + this._pageCurrentElement = this.getChildById('page-current'); + this._pageCountElement = this.getChildById('page-count'); + this._labelElements = []; + this._lineElements = []; + for (let i = 0; i < 6; i++) { + this._labelElements[i] = [ + this.getChildById('label-' + i + '-left'), + this.getChildById('label-' + i + '-right'), + this.getChildById('label-' + i + '-center'), + ]; + this._lineElements[i] = [ + this.getChildById('line-' + i + '-left'), + this.getChildById('line-' + i + '-right'), + this.getChildById('line-' + i + '-center'), + ]; + } + + CDUMenuPage.ShowPage(this); + + // If the consent is not set, show telex page + const onlineFeaturesStatus = NXDataStore.get('CONFIG_ONLINE_FEATURES_STATUS', 'UNKNOWN'); + + if (onlineFeaturesStatus === 'UNKNOWN') { + new NXPopUp().showPopUp( + 'TELEX CONFIGURATION', + 'You have not yet configured the telex option. Telex enables free text and live map. If enabled, aircraft position data is published for the duration of the flight. Messages are public and not moderated. USE AT YOUR OWN RISK. To learn more about telex and the features it enables, please go to https://docs.flybywiresim.com/telex. Would you like to enable telex?', + 'small', + () => NXDataStore.set('CONFIG_ONLINE_FEATURES_STATUS', 'ENABLED'), + () => NXDataStore.set('CONFIG_ONLINE_FEATURES_STATUS', 'DISABLED'), + ); + } + + SimVar.SetSimVarValue('L:A32NX_GPS_PRIMARY_LOST_MSG', 'Bool', 0).then(); + + NXDataStore.subscribe('*', () => { + this.requestUpdate(); + }); + + this.mcduServerClient = new McduServerClient(); + + // sync annunciator simvar state + this.updateAnnunciators(true); + } + + public requestUpdate() { + this.updateRequest = true; + } + + public onUpdate(_deltaTime: number) { + super.onUpdate(_deltaTime); + + // every 100ms + if (this.minPageUpdateThrottler.canUpdate(_deltaTime) !== -1 && this.updateRequest) { + this.updateRequest = false; + if (this.pageRedrawCallback) { + this.pageRedrawCallback(); + } + } + + // Create a connection to the SimBridge MCDU Server if it is not already connected + // every 1000ms + if ( + this.mcduServerConnectUpdateThrottler.canUpdate(_deltaTime) !== -1 && + GameStateProvider.get().get() === GameState.ingame && + this.mcduServerClient + ) { + if (this.mcduServerClient.isConnected()) { + // Check if connection or SimBridge Setting is still valid. + // validateConnection() will return false if the connection is not established + // any longer (probably not the case here as we test isConnected) or if the + // SimBridge Enabled setting (persistent property CONFIG_SIMBRIDGE_ENABLED) is set + // to off - this is the case where this clears the remote MCDU screen and + // disconnects the client. + if (!this.mcduServerClient.validateConnection()) { + this.sendClearScreen(); + this.mcduServerClient.disconnect(); + } + } else { + // not connected - try to connect + this.mcduServerClient.connect(this.mcduServerClientEventHandler.bind(this)); + } + } + + // There is no (known) event when power is turned on or off (e.g. Ext Pwr) and remote clients + // would not be updated (cleared or updated). Therefore, monitoring power is necessary. + // every 500ms + if (this.powerCheckUpdateThrottler.canUpdate(_deltaTime) !== -1) { + const isPoweredL = SimVar.GetSimVarValue('L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED', 'Number'); + if (this.lastPowerState !== isPoweredL) { + this.lastPowerState = isPoweredL; + this.onFmPowerStateChanged(isPoweredL); + + if (this.mcduServerClient && this.mcduServerClient.isConnected()) { + this.sendUpdate(); + } + } + } + + // TODO these other mechanisms are replaced in the MCDU split PR + if (this.pageUpdate) { + this.pageUpdate(); + } + this.checkAocTimes(); + this.updateMCDU(); + } + + /* MCDU UPDATE */ + + /** + * Updates the MCDU state. + */ + private updateMCDU() { + this.updateAnnunciators(); + + this.updateBrightness(); + + this.updateInitBFuelPred(); + + this.updateAtsuRequest(); + } + + /** + * Checks whether INIT page B is open and an engine is being started, if so: + * The INIT page B reverts to the FUEL PRED page 15 seconds after the first engine start and cannot be accessed after engine start. + */ + private updateInitBFuelPred() { + if (this.isAnEngineOn()) { + if (!this.initB) { + this.initB = true; + setTimeout(() => { + if (this.page.Current === this.page.InitPageB && this.isAnEngineOn()) { + CDUFuelPredPage.ShowPage(this); + } + }, 15000); + } + } else { + this.initB = false; + } + } + + private updateAnnunciators(forceWrite = false) { + const lightTestPowered = SimVar.GetSimVarValue('L:A32NX_ELEC_DC_2_BUS_IS_POWERED', 'bool'); + const lightTest = lightTestPowered && SimVar.GetSimVarValue('L:A32NX_OVHD_INTLT_ANN', 'number') === 0; + + // lights are AC1, MCDU is ACC ESS SHED + const leftAnnuncPower = + SimVar.GetSimVarValue('L:A32NX_ELEC_AC_1_BUS_IS_POWERED', 'bool') && + SimVar.GetSimVarValue('L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED', 'bool'); + this.updateAnnunciatorsForSide('left', lightTest, leftAnnuncPower, forceWrite); + + // lights and MCDU are both AC2 + const rightAnnuncPower = SimVar.GetSimVarValue('L:A32NX_ELEC_AC_2_BUS_IS_POWERED', 'bool'); + this.updateAnnunciatorsForSide('right', lightTest, rightAnnuncPower, forceWrite); + } + + private updateBrightness() { + const left = SimVar.GetSimVarValue('L:A32NX_MCDU_L_BRIGHTNESS', 'number'); + const right = SimVar.GetSimVarValue('L:A32NX_MCDU_R_BRIGHTNESS', 'number'); + + let updateNeeded = false; + + if (left !== this.leftBrightness) { + this.leftBrightness = left; + updateNeeded = true; + } + + if (right !== this.rightBrightness) { + this.rightBrightness = right; + updateNeeded = true; + } + + if (updateNeeded) { + this.sendUpdate(); + } + } + + private updateAtsuRequest() { + // the ATSU currently doesn't have the MCDU request signal, so we just check for messages and set it's flag + const msgs = SimVar.GetSimVarValue('L:A32NX_COMPANY_MSG_COUNT', 'number'); + if (msgs > this._lastAtsuMessageCount) { + this.setRequest('ATSU'); + } + this._lastAtsuMessageCount = msgs; + } + + /** + * Updates the annunciator light states for one MCDU. + * @param side Which MCDU to update. + * @param lightTest Whether ANN LT TEST is active. + * @param powerOn Whether annunciator LED power is available. + */ + private updateAnnunciatorsForSide(side: 'left' | 'right', lightTest: boolean, powerOn: boolean, forceWrite = false) { + let updateNeeded = false; + + const simVarSide = side.toUpperCase().charAt(0); + + const states = this.annunciators[side]; + for (const [annunc, state] of Object.entries(states)) { + let newState = !!(lightTest && powerOn); + + if (annunc === 'fmgc') { + newState = newState || this.isSubsystemRequesting('FMGC'); + } else if (annunc === 'mcdu_menu') { + newState = + newState || + this.isSubsystemRequesting('AIDS') || + this.isSubsystemRequesting('ATSU') || + this.isSubsystemRequesting('CFDS'); + } + + if (newState !== state || forceWrite) { + states[annunc] = newState; + SimVar.SetSimVarValue(`L:A32NX_MCDU_${simVarSide}_ANNUNC_${annunc.toUpperCase()}`, 'bool', newState); + updateNeeded = true; + } + } + + if (updateNeeded) { + this.sendUpdate(); + } + } + + // FIXME move ATSU code to ATSU + private checkAocTimes() { + if (!this.aocTimes.off) { + if (this.flightPhaseManager.phase === FmgcFlightPhase.Takeoff && !this.isOnGround()) { + // Wheels off + // Off: remains blank until Take off time + this.aocTimes.off = Math.floor(SimVar.GetGlobalVarValue('ZULU TIME', 'seconds')); + } + } + + if (!this.aocTimes.out) { + const currentPKGBrakeState = SimVar.GetSimVarValue('L:A32NX_PARK_BRAKE_LEVER_POS', 'Bool'); + if (this.flightPhaseManager.phase === FmgcFlightPhase.Preflight && !currentPKGBrakeState) { + // Out: is when you set the brakes to off + this.aocTimes.out = Math.floor(SimVar.GetGlobalVarValue('ZULU TIME', 'seconds')); + } + } + + if (!this.aocTimes.on) { + if (this.aocTimes.off && this.isOnGround()) { + // On: remains blank until Landing time + this.aocTimes.on = Math.floor(SimVar.GetGlobalVarValue('ZULU TIME', 'seconds')); + } + } + + if (!this.aocTimes.in) { + const currentPKGBrakeState = SimVar.GetSimVarValue('L:A32NX_PARK_BRAKE_LEVER_POS', 'Bool'); + const cabinDoorPctOpen = SimVar.GetSimVarValue('INTERACTIVE POINT OPEN:0', 'percent'); + if (this.aocTimes.on && currentPKGBrakeState && cabinDoorPctOpen > 20) { + // In: remains blank until brakes set to park AND the first door opens + this.aocTimes.in = Math.floor(SimVar.GetGlobalVarValue('ZULU TIME', 'seconds')); + } + } + + if (this.flightPhaseManager.phase === FmgcFlightPhase.Preflight) { + const cabinDoorPctOpen = SimVar.GetSimVarValue('INTERACTIVE POINT OPEN:0', 'percent'); + if (!this.aocTimes.doors && cabinDoorPctOpen < 20) { + this.aocTimes.doors = Math.floor(SimVar.GetGlobalVarValue('ZULU TIME', 'seconds')); + } else { + if (cabinDoorPctOpen > 20) { + this.aocTimes.doors = 0; + } + } + } + } + + /* END OF MCDU UPDATE */ + /* MCDU INTERFACE/LAYOUT */ + + private _formatCell(str) { + return str + .replace(/{big}/g, "") + .replace(/{small}/g, "") + .replace(/{big}/g, "") + .replace(/{amber}/g, "") + .replace(/{red}/g, "") + .replace(/{green}/g, "") + .replace(/{cyan}/g, "") + .replace(/{white}/g, "") + .replace(/{magenta}/g, "") + .replace(/{yellow}/g, "") + .replace(/{inop}/g, "") + .replace(/{sp}/g, ' ') + .replace(/{left}/g, "") + .replace(/{right}/g, "") + .replace(/{end}/g, ''); + } + + public setTitle(content: string) { + let color = content.split('[color]')[1]; + if (!color) { + color = 'white'; + } + this._title = content.split('[color]')[0]; + this._title = `{${color}}${this._title}{end}`; + this._titleElement.textContent = this._title; + } + + private setTitleLeft(content: string) { + if (!content) { + this._titleLeft = ''; + this._titleLeftElement.textContent = ''; + return; + } + let color = content.split('[color]')[1]; + if (!color) { + color = 'white'; + } + this._titleLeft = content.split('[color]')[0]; + this._titleLeft = `{${color}}${this._titleLeft}{end}`; + this._titleLeftElement.textContent = this._titleLeft; + } + + private setPageCurrent(value: string | number) { + if (typeof value === 'number') { + this._pageCurrent = value; + } else if (typeof value === 'string') { + this._pageCurrent = parseInt(value); + } + this._pageCurrentElement.textContent = (this._pageCurrent > 0 ? this._pageCurrent : '') + ''; + } + + private setPageCount(value: string | number) { + if (typeof value === 'number') { + this._pageCount = value; + } else if (typeof value === 'string') { + this._pageCount = parseInt(value); + } + this._pageCountElement.textContent = (this._pageCount > 0 ? this._pageCount : '') + ''; + if (this._pageCount === 0) { + this.getChildById('page-slash').textContent = ''; + } else { + this.getChildById('page-slash').textContent = '/'; + } + } + + private setLabel(label: string, row: number, col = -1, websocketDraw = true) { + if (col >= this._labelElements[row].length) { + return; + } + if (!this._labels[row]) { + this._labels[row] = []; + } + if (!label) { + label = ''; + } + if (col === -1) { + for (let i = 0; i < this._labelElements[row].length; i++) { + this._labels[row][i] = ''; + this._labelElements[row][i].textContent = ''; + } + col = 0; + } + if (label === '__FMCSEPARATOR') { + label = '------------------------'; + } + if (label !== '') { + if (label.indexOf('[b-text]') !== -1) { + label = label.replace('[b-text]', ''); + this._lineElements[row][col].classList.remove('s-text'); + this._lineElements[row][col].classList.add('msg-text'); + } else { + this._lineElements[row][col].classList.remove('msg-text'); + } + + let color = label.split('[color]')[1]; + if (!color) { + color = 'white'; + } + label = label.split('[color]')[0]; + label = `{${color}}${label}{end}`; + } + this._labels[row][col] = label; + this._labelElements[row][col].textContent = label; + + if (websocketDraw) { + this.sendUpdate(); + } + } + + private setLine(content: string | CDU_Field, row: number, col = -1, websocketDraw = true) { + if (content instanceof CDU_Field) { + const field = content; + (col === 0 || col === -1 ? this.onLeftInput : this.onRightInput)[row] = (value) => { + field.onSelect(value); + }; + content = content.getValue(); + } + + if (col >= this._lineElements[row].length) { + return; + } + if (!content) { + content = ''; + } + if (!this._lines[row]) { + this._lines[row] = []; + } + if (col === -1) { + for (let i = 0; i < this._lineElements[row].length; i++) { + this._lines[row][i] = ''; + this._lineElements[row][i].textContent = ''; + } + col = 0; + } + if (content === '__FMCSEPARATOR') { + content = '------------------------'; + } + if (content !== '') { + let color = content.split('[color]')[1]; + if (!color) { + color = 'white'; + } + content = content.split('[color]')[0]; + content = `{${color}}${content}{end}`; + if (content.indexOf('[s-text]') !== -1) { + content = content.replace('[s-text]', ''); + content = `{small}${content}{end}`; + } + } + this._lines[row][col] = content; + this._lineElements[row][col].textContent = this._lines[row][col]; + + if (websocketDraw) { + this.sendUpdate(); + } + } + + public setTemplate(template: any[][], large = false) { + if (template[0]) { + this.setTitle(template[0][0]); + this.setPageCurrent(template[0][1]); + this.setPageCount(template[0][2]); + this.setTitleLeft(template[0][3]); + } + for (let i = 0; i < 6; i++) { + let tIndex = 2 * i + 1; + if (template[tIndex]) { + if (large) { + if (template[tIndex][1] !== undefined) { + this.setLine(template[tIndex][0], i, 0, false); + this.setLine(template[tIndex][1], i, 1, false); + this.setLine(template[tIndex][2], i, 2, false); + this.setLine(template[tIndex][3], i, 3, false); + } else { + this.setLine(template[tIndex][0], i, -1, false); + } + } else { + if (template[tIndex][1] !== undefined) { + this.setLabel(template[tIndex][0], i, 0, false); + this.setLabel(template[tIndex][1], i, 1, false); + this.setLabel(template[tIndex][2], i, 2, false); + this.setLabel(template[tIndex][3], i, 3, false); + } else { + this.setLabel(template[tIndex][0], i, -1, false); + } + } + } + tIndex = 2 * i + 2; + if (template[tIndex]) { + if (template[tIndex][1] !== undefined) { + this.setLine(template[tIndex][0], i, 0, false); + this.setLine(template[tIndex][1], i, 1, false); + this.setLine(template[tIndex][2], i, 2, false); + this.setLine(template[tIndex][3], i, 3, false); + } else { + this.setLine(template[tIndex][0], i, -1, false); + } + } + } + if (template[13]) { + this.setScratchpadText(template[13][0]); + } + + // Apply formatting helper to title page, lines and labels + if (this._titleElement !== null) { + this._titleElement.innerHTML = this._formatCell(this._titleElement.innerHTML); + } + if (this._titleLeftElement !== null) { + this._titleLeftElement.innerHTML = this._formatCell(this._titleLeftElement.innerHTML); + } + this._lineElements.forEach((row) => { + row.forEach((column) => { + if (column !== null) { + column.innerHTML = this._formatCell(column.innerHTML); + } + }); + }); + this._labelElements.forEach((row) => { + row.forEach((column) => { + if (column !== null) { + column.innerHTML = this._formatCell(column.innerHTML); + } + }); + }); + this.sendUpdate(); + } + + /** + * Sets what arrows will be displayed in the corner of the screen. Arrows are removed when clearDisplay() is called. + * @param up whether the up arrow will be displayed + * @param down whether the down arrow will be displayed + * @param left whether the left arrow will be displayed + * @param right whether the right arrow will be displayed + */ + public setArrows(up: boolean, down: boolean, left: boolean, right: boolean) { + this._arrows = [up, down, left, right]; + this.arrowHorizontal.style.opacity = left || right ? '1' : '0'; + this.arrowVertical.style.opacity = up || down ? '1' : '0'; + if (up && down) { + this.arrowVertical.innerHTML = '↓↑\xa0'; + } else if (up) { + this.arrowVertical.innerHTML = '↑\xa0'; + } else { + this.arrowVertical.innerHTML = '↓\xa0\xa0'; + } + if (left && right) { + this.arrowHorizontal.innerHTML = '←→\xa0'; + } else if (right) { + this.arrowHorizontal.innerHTML = '→\xa0'; + } else { + this.arrowHorizontal.innerHTML = '←\xa0\xa0'; + } + } + + public clearDisplay(webSocketDraw = false) { + this.onUnload(); + this.onUnload = () => {}; + this.setTitle(''); + this.setTitleLeft(''); + this.setPageCurrent(0); + this.setPageCount(0); + for (let i = 0; i < 6; i++) { + this.setLabel('', i, -1, webSocketDraw); + } + for (let i = 0; i < 6; i++) { + this.setLine('', i, -1, webSocketDraw); + } + this.onLeftInput = []; + this.onRightInput = []; + this.leftInputDelay = []; + this.rightInputDelay = []; + this.onPrevPage = () => {}; + this.onNextPage = () => {}; + this.pageUpdate = () => {}; + this.pageRedrawCallback = null; + if (this.page.Current === this.page.MenuPage) { + this.setScratchpadText(''); + } + this.page.Current = this.page.Clear; + this.setArrows(false, false, false, false); + this.tryDeleteTimeout(); + this.onUp = () => {}; + this.onDown = () => {}; + this.updateRequest = false; + } + + /** + * Set the active subsystem + * @param {'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'} subsystem + */ + public set activeSystem(subsystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC') { + this._activeSystem = subsystem; + this.scratchpad = this.scratchpads[subsystem]; + this._clearRequest(subsystem); + } + + public get activeSystem() { + return this._activeSystem; + } + + private set scratchpad(sp) { + if (sp === this._scratchpad) { + return; + } + + // pause the old scratchpad so it stops writing to the display + if (this._scratchpad) { + this._scratchpad.pause(); + } + + // set the new scratchpad and resume it to update the display + this._scratchpad = sp; + this._scratchpad.resume(); + } + + private get scratchpad(): ScratchpadDataLink { + return this._scratchpad; + } + + public get mcduScratchpad() { + return this.scratchpads['MCDU']; + } + + public get fmgcScratchpad() { + return this.scratchpads['FMGC']; + } + + public get atsuScratchpad() { + return this.scratchpads['ATSU']; + } + + public get aidsScratchpad() { + return this.scratchpads['AIDS']; + } + + public get cfdsScratchpad() { + return this.scratchpads['CFDS']; + } + + public activateMcduScratchpad() { + this.scratchpad = this.scratchpads['MCDU']; + } + + /** + * Check if there is an active request from a subsystem to the MCDU + * @returns true if an active request exists + */ + public isSubsystemRequesting(subsystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC') { + return this.requests[subsystem] === true; + } + + /** + * Set a request from a subsystem to the MCDU + */ + public setRequest(subsystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC') { + if (!(subsystem in this.requests) || this.activeSystem === subsystem) { + return; + } + if (!this.requests[subsystem]) { + this.requests[subsystem] = true; + + // refresh the menu page if active + if (this.page.Current === this.page.MenuPage) { + CDUMenuPage.ShowPage(this); + } + } + } + + /** + * Clear a request from a subsystem to the MCDU + */ + private _clearRequest(subsystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC') { + if (!(subsystem in this.requests)) { + return; + } + + if (this.requests[subsystem]) { + this.requests[subsystem] = false; + + // refresh the menu page if active + if (this.page.Current === this.page.MenuPage) { + CDUMenuPage.ShowPage(this); + } + } + } + + private generateHTMLLayout(parent) { + while (parent.children.length > 0) { + parent.removeChild(parent.children[0]); + } + const header = document.createElement('div'); + header.id = 'header'; + + const titleLeft = document.createElement('div'); + titleLeft.classList.add('s-text'); + titleLeft.id = 'title-left'; + parent.appendChild(titleLeft); + + const title = document.createElement('span'); + title.id = 'title'; + header.appendChild(title); + + this.arrowHorizontal = document.createElement('span'); + this.arrowHorizontal.id = 'arrow-horizontal'; + this.arrowHorizontal.innerHTML = '←→\xa0'; + header.appendChild(this.arrowHorizontal); + + parent.appendChild(header); + + const page = document.createElement('div'); + page.id = 'page-info'; + page.classList.add('s-text'); + + const pageCurrent = document.createElement('span'); + pageCurrent.id = 'page-current'; + + const pageSlash = document.createElement('span'); + pageSlash.id = 'page-slash'; + pageSlash.textContent = '/'; + + const pageCount = document.createElement('span'); + pageCount.id = 'page-count'; + + page.appendChild(pageCurrent); + page.appendChild(pageSlash); + page.appendChild(pageCount); + parent.appendChild(page); + + for (let i = 0; i < 6; i++) { + const label = document.createElement('div'); + label.classList.add('label', 's-text'); + const labelLeft = document.createElement('span'); + labelLeft.id = 'label-' + i + '-left'; + labelLeft.classList.add('fmc-block', 'label', 'label-left'); + const labelRight = document.createElement('span'); + labelRight.id = 'label-' + i + '-right'; + labelRight.classList.add('fmc-block', 'label', 'label-right'); + const labelCenter = document.createElement('span'); + labelCenter.id = 'label-' + i + '-center'; + labelCenter.classList.add('fmc-block', 'label', 'label-center'); + label.appendChild(labelLeft); + label.appendChild(labelRight); + label.appendChild(labelCenter); + parent.appendChild(label); + const line = document.createElement('div'); + line.classList.add('line'); + const lineLeft = document.createElement('span'); + lineLeft.id = 'line-' + i + '-left'; + lineLeft.classList.add('fmc-block', 'line', 'line-left'); + const lineRight = document.createElement('span'); + lineRight.id = 'line-' + i + '-right'; + lineRight.classList.add('fmc-block', 'line', 'line-right'); + const lineCenter = document.createElement('span'); + lineCenter.id = 'line-' + i + '-center'; + lineCenter.classList.add('fmc-block', 'line', 'line-center'); + line.appendChild(lineLeft); + line.appendChild(lineRight); + line.appendChild(lineCenter); + parent.appendChild(line); + } + const footer = document.createElement('div'); + footer.classList.add('line'); + const inout = document.createElement('span'); + inout.id = 'in-out'; + this.arrowVertical = document.createElement('span'); + this.arrowVertical.id = 'arrow-vertical'; + this.arrowVertical.innerHTML = '↓↑\xa0'; + + footer.appendChild(inout); + footer.appendChild(this.arrowVertical); + parent.appendChild(footer); + } + + /* END OF MCDU INTERFACE/LAYOUT */ + /* MCDU SCRATCHPAD */ + + public setScratchpadUserData(value: string) { + this.scratchpad.setUserData(value); + } + + private clearFocus() { + this.inFocus = false; + this.allSelected = false; + try { + Coherent.trigger('UNFOCUS_INPUT_FIELD', this.scratchpadDisplay.guid); + } catch (e) { + console.error(e); + } + this.scratchpadDisplay.setStyle(null); + // This is legal but the TS DOM types mark it readonly. + (this.getChildById('header').style as unknown as any) = null; + if (this.check_focus) { + clearInterval(this.check_focus); + } + } + + private initKeyboardScratchpad() { + window.document.addEventListener('click', () => { + const mcduInput = NXDataStore.get('MCDU_KB_INPUT', 'DISABLED'); + const mcduTimeout = parseInt(NXDataStore.get('CONFIG_MCDU_KB_TIMEOUT', '60')); + const isPoweredL = SimVar.GetSimVarValue('L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED', 'Number'); + const isPoweredR = SimVar.GetSimVarValue('L:A32NX_ELEC_AC_2_BUS_IS_POWERED', 'Number'); + + // TODO: L/R MCDU + if (mcduInput === 'ENABLED') { + this.inFocus = !this.inFocus; + if (this.inFocus && (isPoweredL || isPoweredR)) { + // This is legal but the TS DOM types mark it readonly. + (this.getChildById('header').style as unknown as any) = + 'background: linear-gradient(180deg, rgba(2,182,217,1.0) 65%, rgba(255,255,255,0.0) 65%);'; + this.scratchpadDisplay.setStyle('display: inline-block; width:87%; background: rgba(255,255,255,0.2);'); + try { + Coherent.trigger('FOCUS_INPUT_FIELD', this.scratchpadDisplay.guid, '', '', '', false); + } catch (e) { + console.error(e); + } + this.lastInput = new Date(); + if (mcduTimeout) { + this.check_focus = setInterval( + () => { + if (Math.abs(Date.now() - this.lastInput.getTime()) / 1000 >= mcduTimeout) { + this.clearFocus(); + } + }, + Math.min((mcduTimeout * 1000) / 2, 1000), + ); + } + } else { + this.clearFocus(); + } + } else { + this.clearFocus(); + } + }); + window.document.addEventListener('keydown', (e) => { + // MCDU should not accept input while unpowered + if (this.inFocus && SimVar.GetSimVarValue('L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED', 'Number')) { + let keycode = e.keyCode; + this.lastInput = new Date(); + if (keycode >= KeyCode.KEY_NUMPAD0 && keycode <= KeyCode.KEY_NUMPAD9) { + keycode -= 48; // numpad support + } + // Note: tried using H-events, worse performance. Reverted to direct input. + // Preventing repeated input also similarly felt awful and defeated the point. + // Clr hold functionality pointless as scratchpad will be cleared (repeated input). + + if (e.altKey || (e.ctrlKey && keycode === KeyCode.KEY_Z)) { + this.clearFocus(); + } else if (e.ctrlKey && keycode === KeyCode.KEY_A) { + this.allSelected = !this.allSelected; + this.scratchpadDisplay.setStyle( + `display: inline-block; width:87%; background: ${this.allSelected ? 'rgba(235,64,52,1.0)' : 'rgba(255,255,255,0.2)'};`, + ); + } else if (e.shiftKey && e.ctrlKey && keycode === KeyCode.KEY_BACK_SPACE) { + this.setScratchpadText(''); + } else if (e.ctrlKey && keycode === KeyCode.KEY_BACK_SPACE) { + const scratchpadTextContent = this.scratchpad.getText(); + let wordFlag = !scratchpadTextContent.includes(' '); + for (let i = scratchpadTextContent.length; i > 0; i--) { + if (scratchpadTextContent.slice(-1) === ' ') { + if (!wordFlag) { + this.onClr(); + } else { + wordFlag = true; + break; + } + } + if (scratchpadTextContent.slice(-1) !== ' ') { + if (!wordFlag) { + wordFlag = true; + } else { + this.onClr(); + } + } + } + } else if (e.shiftKey && keycode === KeyCode.KEY_BACK_SPACE) { + if (!this.check_clr) { + this.onClr(); + this.check_clr = setTimeout(() => { + this.onClrHeld(); + }, 2000); + } + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_1_CLR', 'Number', 1); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_2_CLR', 'Number', 1); + } else if ( + (keycode >= KeyCode.KEY_0 && keycode <= KeyCode.KEY_9) || + (keycode >= KeyCode.KEY_A && keycode <= KeyCode.KEY_Z) + ) { + const letter = String.fromCharCode(keycode); + this.onLetterInput(letter); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_1_' + letter.toUpperCase(), 'Number', 1); // TODO: L/R [1/2] side MCDU Split + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_2_' + letter.toUpperCase(), 'Number', 1); + } else if (keycode === KeyCode.KEY_PERIOD || keycode === KeyCode.KEY_DECIMAL) { + this.onDot(); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_1_DOT', 'Number', 1); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_2_DOT', 'Number', 1); + } else if ( + keycode === KeyCode.KEY_SLASH || + keycode === KeyCode.KEY_BACK_SLASH || + keycode === KeyCode.KEY_DIVIDE || + keycode === 226 + ) { + this.onDiv(); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_1_SLASH', 'Number', 1); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_2_SLASH', 'Number', 1); + } else if (keycode === KeyCode.KEY_BACK_SPACE || keycode === KeyCode.KEY_DELETE) { + if (this.allSelected) { + this.setScratchpadText(''); + } else if (!this.clrStop) { + this.onClr(); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_1_CLR', 'Number', 1); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_2_CLR', 'Number', 1); + this.clrStop = this.scratchpad.isClearStop(); + } + } else if (keycode === KeyCode.KEY_SPACE) { + this.onSp(); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_1_SP', 'Number', 1); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_2_SP', 'Number', 1); + } else if (keycode === 189 || keycode === KeyCode.KEY_SUBTRACT) { + this.onPlusMinus('-'); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_1_PLUSMINUS', 'Number', 1); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_2_PLUSMINUS', 'Number', 1); + } else if (keycode === 187 || keycode === KeyCode.KEY_ADD) { + this.onPlusMinus('+'); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_1_PLUSMINUS', 'Number', 1); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_2_PLUSMINUS', 'Number', 1); + } else if (keycode >= KeyCode.KEY_F1 && keycode <= KeyCode.KEY_F6) { + const func_num = keycode - KeyCode.KEY_F1; + this.onLeftFunction(func_num); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_1_L' + (func_num + 1), 'Number', 1); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_2_L' + (func_num + 1), 'Number', 1); + } else if (keycode >= KeyCode.KEY_F7 && keycode <= KeyCode.KEY_F12) { + const func_num = keycode - KeyCode.KEY_F7; + this.onRightFunction(func_num); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_1_R' + (func_num + 1), 'Number', 1); + SimVar.SetSimVarValue('L:A32NX_MCDU_PUSH_ANIM_2_R' + (func_num + 1), 'Number', 1); + } + } + }); + window.document.addEventListener('keyup', (e) => { + this.lastInput = new Date(); + const keycode = e.keyCode; + if (keycode === KeyCode.KEY_BACK_SPACE || keycode === KeyCode.KEY_DELETE) { + this.clrStop = false; + } + if (this.check_clr) { + clearTimeout(this.check_clr); + this.check_clr = undefined; + } + }); + } + + /* END OF MCDU SCRATCHPAD */ + /* MCDU MESSAGE SYSTEM */ + + /** + * Display a type I message on the active subsystem's scratch pad + */ + public setScratchpadMessage(message: McduMessage) { + if (message instanceof TypeIIMessage) { + console.error('Type II message passed to setScratchpadMessage! Redirecting to the queue.', message); + this.addMessageToQueue(message); + return; + } + + if (this.scratchpad) { + this.scratchpad.setMessage(message); + } + } + + public setScratchpadText(value: string) { + this.scratchpad.setText(value); + } + + // FIXME move non-FMS code out of FMS + /** + * General ATSU message handler which converts ATSU status codes to new MCDU messages + * @param code ATSU status code + */ + public addNewAtsuMessage(code: AtsuStatusCodes) { + if (!this.atsuScratchpad) { + return; + } + switch (code) { + case AtsuStatusCodes.CallsignInUse: + this.atsuScratchpad.setMessage(NXFictionalMessages.fltNbrInUse); + break; + case AtsuStatusCodes.NoHoppieConnection: + this.atsuScratchpad.setMessage(NXFictionalMessages.noHoppieConnection); + break; + case AtsuStatusCodes.ComFailed: + this.atsuScratchpad.setMessage(NXSystemMessages.comUnavailable); + break; + case AtsuStatusCodes.NoAtc: + this.atsuScratchpad.setMessage(NXSystemMessages.noAtc); + break; + case AtsuStatusCodes.MailboxFull: + this.atsuScratchpad.setMessage(NXSystemMessages.dcduFileFull); + break; + case AtsuStatusCodes.UnknownMessage: + this.atsuScratchpad.setMessage(NXFictionalMessages.unknownAtsuMessage); + break; + case AtsuStatusCodes.ProxyError: + this.atsuScratchpad.setMessage(NXFictionalMessages.reverseProxy); + break; + case AtsuStatusCodes.NoTelexConnection: + this.atsuScratchpad.setMessage(NXFictionalMessages.telexNotEnabled); + break; + case AtsuStatusCodes.OwnCallsign: + this.atsuScratchpad.setMessage(NXSystemMessages.noAtc); + break; + case AtsuStatusCodes.SystemBusy: + this.atsuScratchpad.setMessage(NXSystemMessages.systemBusy); + break; + case AtsuStatusCodes.NewAtisReceived: + this.atsuScratchpad.setMessage(NXSystemMessages.newAtisReceived); + break; + case AtsuStatusCodes.NoAtisReceived: + this.atsuScratchpad.setMessage(NXSystemMessages.noAtisReceived); + break; + case AtsuStatusCodes.EntryOutOfRange: + this.atsuScratchpad.setMessage(NXSystemMessages.entryOutOfRange); + break; + case AtsuStatusCodes.FormatError: + this.atsuScratchpad.setMessage(NXSystemMessages.formatError); + break; + case AtsuStatusCodes.NotInDatabase: + this.atsuScratchpad.setMessage(NXSystemMessages.notInDatabase); + break; + default: + break; + } + } + + /* END OF MCDU MESSAGE SYSTEM */ + /* MCDU EVENTS */ + + public onPowerOn() { + super.onPowerOn(); + } + + protected onEvent(_event) { + super.onEvent(_event); + + // MCDU should not accept input while unpowered + if (!SimVar.GetSimVarValue('L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED', 'Number')) { + return; + } + + const isLeftMcduEvent = _event.indexOf('1_BTN_') !== -1; + const isRightMcduEvent = _event.indexOf('2_BTN_') !== -1; + + if (isLeftMcduEvent || isRightMcduEvent || _event.indexOf('BTN_') !== -1) { + const input = _event.replace('1_BTN_', '').replace('2_BTN_', '').replace('BTN_', ''); + if (this._keypad.onKeyPress(input, isRightMcduEvent ? 'R' : 'L')) { + return; + } + + if (input.length === 2 && input[0] === 'L') { + const v = parseInt(input[1]) - 1; + if (isFinite(v)) { + this.onLeftFunction(v); + } + } else if (input.length === 2 && input[0] === 'R') { + const v = parseInt(input[1]) - 1; + if (isFinite(v)) { + this.onRightFunction(v); + } + } else { + console.log("'" + input + "'"); + } + } + } + + private onLsk(fncAction, fncActionDelay = this.getDelayBasic) { + if (!fncAction) { + return; + } + + // First timeout simulates delay for key press + // Second delay simulates delay for input validation + const cur = this.page.Current; + setTimeout(() => { + const value = this.scratchpad.removeUserContentFromScratchpadAndDisplayAndReturnTextContent(); + setTimeout(() => { + if (this.page.Current === cur) { + fncAction(value, () => this.setScratchpadUserData(value)); + } + }, fncActionDelay()); + }, 100); + } + + /** + * Handle brightness key events + */ + public onBrightnessKey(side: 'L' | 'R', sign: -1 | 1) { + const oldBrightness = side === 'R' ? this.rightBrightness : this.leftBrightness; + SimVar.SetSimVarValue( + `L:A32NX_MCDU_${side}_BRIGHTNESS`, + 'number', + Math.max( + A320_Neo_CDU_MainDisplay.MIN_BRIGHTNESS, + Math.min(A320_Neo_CDU_MainDisplay.MAX_BRIGHTNESS, oldBrightness + sign * 0.2 * oldBrightness), + ), + ); + } + + /* END OF MCDU EVENTS */ + /* MCDU DELAY SIMULATION */ + + /** + * Used for switching pages + * @returns delay in ms between 150 and 200 + */ + public getDelaySwitchPage(): number { + return 150 + 50 * Math.random(); + } + + /** + * Used for basic inputs e.g. alternate airport, ci, fl, temp, constraints, ... + * @returns delay in ms between 300 and 400 + */ + public getDelayBasic(): number { + return 300 + 100 * Math.random(); + } + + /** + * Used for e.g. loading time fore pages + * @returns delay in ms between 600 and 800 + */ + public getDelayMedium(): number { + return 600 + 200 * Math.random(); + } + + /** + * Used for intense calculation + * @returns delay in ms between 900 and 12000 + */ + public getDelayHigh(): number { + return 900 + 300 * Math.random(); + } + + /** + * Used for calculation time for fuel pred page + * @returns dynamic delay in ms between 2000ms and 4000ms + */ + public getDelayFuelPred(): number { + return Math.max(2000, Math.min(4000, 225 * this.getActivePlanLegCount())); + } + + /** + * Used to load wind data into sfms + * @returns dynamic delay in ms dependent on amount of waypoints + */ + public getDelayWindLoad(): number { + return Math.pow(this.getActivePlanLegCount(), 2); + } + + /** + * Tries to delete a pages timeout + */ + private tryDeleteTimeout() { + if (this.SelfPtr) { + clearTimeout(this.SelfPtr); + this.SelfPtr = false; + } + } + + /* END OF MCDU DELAY SIMULATION */ + /* MCDU AOC MESSAGE SYSTEM */ + + public printPage(lines: any[]) { + if (this.printing) { + return; + } + this.printing = true; + + const formattedValues = lines.map((l) => { + return l + .replace(/\[color]cyan/g, '
') + .replace(/{white}[-]{3,}{end}/g, '
') + .replace(/{end}/g, '
') + .replace(/(\[color][a-z]*)/g, '') + .replace(/{[a-z]*}/g, ''); + }); + + const websocketLines = formattedValues.map((l) => { + return l.replace(/[ ]*/g, '\n'); + }); + + if (SimVar.GetSimVarValue('L:A32NX_PRINTER_PRINTING', 'bool') === 1) { + SimVar.SetSimVarValue( + 'L:A32NX_PAGES_PRINTED', + 'number', + SimVar.GetSimVarValue('L:A32NX_PAGES_PRINTED', 'number') + 1, + ); + SimVar.SetSimVarValue('L:A32NX_PRINT_PAGE_OFFSET', 'number', 0); + } + SimVar.SetSimVarValue('L:A32NX_PRINT_LINES', 'number', lines.length); + SimVar.SetSimVarValue('L:A32NX_PAGE_ID', 'number', SimVar.GetSimVarValue('L:A32NX_PAGE_ID', 'number') + 1); + SimVar.SetSimVarValue('L:A32NX_PRINTER_PRINTING', 'bool', 0).then(() => { + this.fmgcMesssagesListener.triggerToAllSubscribers('A32NX_PRINT', formattedValues); + this.sendToMcduServerClient(`print:${JSON.stringify({ lines: websocketLines })}`); + setTimeout(() => { + SimVar.SetSimVarValue('L:A32NX_PRINTER_PRINTING', 'bool', 1); + this.printing = false; + }, 2500); + }); + } + + /* END OF MCDU AOC MESSAGE SYSTEM */ + + /* MCDU SERVER CLIENT */ + + /** + * Sends a message to the websocket server (if connected) + * @param {string} message + */ + private sendToMcduServerClient(message: string) { + if (this.mcduServerClient && this.mcduServerClient.isConnected()) { + try { + this.mcduServerClient.send(message); + } catch (e) { + /** ignore **/ + } + } + } + + /** + * Sends an update to the websocket server (if connected) with the current state of the MCDU + */ + public sendUpdate() { + // only calculate update when mcduServerClient is established. + if (this.mcduServerClient && !this.mcduServerClient.isConnected()) { + return; + } + let left = this.emptyLines; + let right = this.emptyLines; + + const mcdu1Powered = SimVar.GetSimVarValue('L:A32NX_ELEC_AC_ESS_SHED_BUS_IS_POWERED', 'bool'); + const mcdu2Powered = SimVar.GetSimVarValue('L:A32NX_ELEC_AC_2_BUS_IS_POWERED', 'bool'); + const integralLightsPowered = SimVar.GetSimVarValue('L:A32NX_ELEC_AC_1_BUS_IS_POWERED', 'bool'); + + let screenState; + if (mcdu1Powered || mcdu2Powered) { + screenState = { + lines: [ + this._labels[0], + this._lines[0], + this._labels[1], + this._lines[1], + this._labels[2], + this._lines[2], + this._labels[3], + this._lines[3], + this._labels[4], + this._lines[4], + this._labels[5], + this._lines[5], + ], + scratchpad: `{${this.scratchpadDisplay.getColor()}}${this.scratchpadDisplay.getText()}{end}`, + title: this._title, + titleLeft: `{small}${this._titleLeft}{end}`, + page: this._pageCount > 0 ? `{small}${this._pageCurrent}/${this._pageCount}{end}` : '', + arrows: this._arrows, + integralBrightness: integralLightsPowered + ? SimVar.GetSimVarValue('A:LIGHT POTENTIOMETER:85', 'percent over 100') + : 0, + }; + } + + if (mcdu1Powered) { + left = Object.assign({}, screenState); + left.annunciators = this.annunciators.left; + left.displayBrightness = this.leftBrightness / A320_Neo_CDU_MainDisplay.MAX_BRIGHTNESS; + } + + if (mcdu2Powered) { + right = Object.assign({}, screenState); + right.annunciators = this.annunciators.right; + right.displayBrightness = this.rightBrightness / A320_Neo_CDU_MainDisplay.MAX_BRIGHTNESS; + } + + const content = { right, left }; + this.sendToMcduServerClient(`update:${JSON.stringify(content)}`); + } + + /** + * Clears the remote MCDU clients' screens + */ + private sendClearScreen() { + // only calculate update when mcduServerClient is established. + if (this.mcduServerClient && !this.mcduServerClient.isConnected()) { + return; + } + const left = this.emptyLines; + const right = left; + const content = { right, left }; + this.sendToMcduServerClient(`update:${JSON.stringify(content)}`); + } + + /* END OF WEBSOCKET */ + + public goToFuelPredPage() { + if (this.isAnEngineOn()) { + CDUFuelPredPage.ShowPage(this); + } else { + CDUInitPage.ShowPage2(this); + } + } + + public logTroubleshootingError(msg: any) { + this.bus.pub('troubleshooting_log_error', String(msg), true, false); + } +} +// registerInstrument('a320-neo-cdu-main-display', A320_Neo_CDU_MainDisplay); diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Scratchpad.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Scratchpad.ts new file mode 100644 index 00000000000..ef242f9d972 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A320_Neo_CDU_Scratchpad.ts @@ -0,0 +1,202 @@ +// private variables and function are marked with _ prefix + +import { McduMessage } from '../messages/NXSystemMessages'; +import { Keypad } from './A320_Neo_CDU_Keypad'; + +/** The MCDU scratchpad display. This belongs to the MCDU itself. */ +export class ScratchpadDisplay { + public readonly guid = `SP-${Utils.generateGUID()}`; + constructor( + private mcdu: { sendUpdate: () => void }, + private scratchpadElement: HTMLElement, + ) { + this.mcdu = mcdu; + this.scratchpadElement = scratchpadElement; + this.scratchpadElement.className = 'white'; + } + + write(value = '', color = 'white') { + this.scratchpadElement.textContent = value; + this.scratchpadElement.className = color; + this.mcdu.sendUpdate(); + } + + setStyle(style: string) { + // This is actually legal as long as you assign a string, but the typescript DOM types mark it readonly so we need a cast. + (this.scratchpadElement.style as unknown as string) = style; + } + + getText() { + return this.scratchpadElement.textContent; + } + + getColor() { + return this.scratchpadElement.className; + } +} + +/** + * The scratchpad for each subsystem. These belong to the subsystems, + * and one will be connected to the MCDU display (not paused) at any given time. + */ +export class ScratchpadDataLink { + // actual scratchpad text/colour + private _value = ''; + private _colour = ''; + + // internal state + private _text = ''; + private _message = undefined; + private _status = 0; + private _isPaused = true; + + constructor( + private mcdu: { + removeMessageFromQueue: (m: string) => void; + setRequest: (subsystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC') => void; + updateMessageQueue: () => void; + }, + private displayUnit: ScratchpadDisplay, + private subsystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC' | 'MCDU', + private keypadEnabled = true, + ) {} + + setText(text: string) { + this._message = undefined; + this._text = text; + this._display(text); + } + + setMessage(message: McduMessage) { + if (this._message && !this._message.isTypeTwo && message.isTypeTwo) { + return; + } + this._message = message; + this._display(message.text, message.isAmber ? 'amber' : 'white'); + } + + removeMessage(messageText: string) { + if (this._message && this._message.text === messageText) { + this.setText(''); + } + } + + addChar(char: string) { + if (!this.keypadEnabled) { + return; + } + if (this._status !== SpDisplayStatus.userContent) { + this.setText(char); + } else if (this._text.length + 1 < 23) { + this.setText(this._text + char); + } + } + + clear() { + if (!this.keypadEnabled) { + return; + } + if (this._status === SpDisplayStatus.empty) { + this.setText(Keypad.clrValue); + } else if (this._status === SpDisplayStatus.clrValue) { + this.setText(''); + } else if (this._status === SpDisplayStatus.userContent) { + this.setText(this._text.slice(0, -1)); + } else { + this.mcdu.removeMessageFromQueue(this._message.text); + this.setText(this._text); + } + } + + clearHeld() { + if (!this.keypadEnabled) { + return; + } + if (this._status === SpDisplayStatus.clrValue || this._status === SpDisplayStatus.userContent) { + this.setText(''); + } + } + + isClearStop() { + return this._status !== SpDisplayStatus.userContent; + } + + plusMinus(char: string) { + if (!this.keypadEnabled) { + return; + } + if (this._status === SpDisplayStatus.userContent && this._text.slice(-1) === '-') { + this.setText(this._text.slice(0, -1) + '+'); + } else { + this.addChar(char); + } + } + + setUserData(data: string) { + this._text = data; + } + + removeUserContentFromScratchpadAndDisplayAndReturnTextContent() { + const userContent = this._text; + if (this._status < SpDisplayStatus.typeOneMessage) { + this.setText(''); + } + return userContent; + } + + getText() { + return this._text; + } + + getColor() { + return this._colour; + } + + pause() { + this._isPaused = true; + } + + resume() { + this._isPaused = false; + this._display(this._value, this._colour); + } + + private _display(value: string, color = 'white') { + // store the content whether we're paused or not + this._colour = color; + this._value = value; + this._updateStatus(value); + + // if we're not paused, write to the display + if (!this._isPaused) { + this.displayUnit.write(value, color); + } + // flag the annunciator if needed + if (this.subsystem !== 'MCDU') { + this.mcdu.setRequest(this.subsystem); + } + } + + private _updateStatus(scratchpadText: string) { + if (this._message) { + this._status = this._message.isTypeTwo ? SpDisplayStatus.typeTwoMessage : SpDisplayStatus.typeOneMessage; + } else { + if (this._text === '' || scratchpadText === '') { + this._status = SpDisplayStatus.empty; + setTimeout(() => this.mcdu.updateMessageQueue(), 150); + } else if (this._text === Keypad.clrValue) { + this._status = SpDisplayStatus.clrValue; + } else { + this._status = SpDisplayStatus.userContent; + } + } + } +} + +const SpDisplayStatus = { + empty: 0, + clrValue: 1, + userContent: 2, + typeOneMessage: 3, + typeTwoMessage: 4, +}; diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_ATSU.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_ATSU.ts new file mode 100644 index 00000000000..2537d90cae9 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_ATSU.ts @@ -0,0 +1,127 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { NXDataStore } from '@flybywiresim/fbw-sdk'; +import { McduMessage, NXFictionalMessages } from '../../messages/NXSystemMessages'; +import { AtsuMessageType } from '@datalink/common'; +// FIXME rogue import from EFB +import { ISimbriefData } from '../../../../../../../../fbw-common/src/systems/instruments/src/EFB/Apis/Simbrief'; +import { SimBriefUplinkAdapter } from '@fmgc/flightplanning/uplink/SimBriefUplinkAdapter'; + +// FIXME move all to ATSU (systems host) + +export function translateAtsuMessageType(type: AtsuMessageType) { + switch (type) { + case AtsuMessageType.Freetext: + return 'FREETEXT'; + case AtsuMessageType.METAR: + return 'METAR'; + case AtsuMessageType.TAF: + return 'TAF'; + case AtsuMessageType.ATIS: + return 'ATIS'; + default: + return 'UNKNOWN'; + } +} + +export function fetchTimeValue() { + let timeValue = SimVar.GetGlobalVarValue('ZULU TIME', 'seconds'); + if (timeValue) { + const seconds = Number.parseInt(timeValue); + const displayTime = Utils.SecondsToDisplayTime(seconds, true, true, false); + timeValue = displayTime.toString(); + return timeValue.substring(0, 5); + } + return null; +} + +/** + * Converts lbs to kg + * @param {string | number} value + */ +const lbsToKg = (value) => { + return (+value * 0.4535934).toString(); +}; + +/** + * Fetch SimBrief OFP data and store on FMCMainDisplay object + */ +export const getSimBriefOfp = ( + mcdu: { simbrief: any; setScratchpadMessage(m: McduMessage) }, + updateView: () => void, + callback = () => {}, +): Promise => { + const navigraphUsername = NXDataStore.get('NAVIGRAPH_USERNAME', ''); + const overrideSimBriefUserID = NXDataStore.get('CONFIG_OVERRIDE_SIMBRIEF_USERID', ''); + + if (!navigraphUsername && !overrideSimBriefUserID) { + mcdu.setScratchpadMessage(NXFictionalMessages.noNavigraphUser); + throw new Error('No Navigraph username provided'); + } + + mcdu.simbrief['sendStatus'] = 'REQUESTING'; + + updateView(); + + return SimBriefUplinkAdapter.downloadOfpForUserID(navigraphUsername, overrideSimBriefUserID) + .then((data) => { + mcdu.simbrief['units'] = data.units; + mcdu.simbrief['route'] = data.route; + mcdu.simbrief['cruiseAltitude'] = data.cruiseAltitude; + mcdu.simbrief['originIcao'] = data.origin.icao; + mcdu.simbrief['originTransAlt'] = data.origin.transAlt; + mcdu.simbrief['originTransLevel'] = data.origin.transLevel; + mcdu.simbrief['destinationIcao'] = data.destination.icao; + mcdu.simbrief['destinationTransAlt'] = data.destination.transAlt; + mcdu.simbrief['destinationTransLevel'] = data.destination.transLevel; + mcdu.simbrief['blockFuel'] = mcdu.simbrief['units'] === 'kgs' ? data.fuel.planRamp : lbsToKg(data.fuel.planRamp); + mcdu.simbrief['payload'] = + mcdu.simbrief['units'] === 'kgs' ? data.weights.payload : lbsToKg(data.weights.payload); + mcdu.simbrief['estZfw'] = + mcdu.simbrief['units'] === 'kgs' ? data.weights.estZeroFuelWeight : lbsToKg(data.weights.estZeroFuelWeight); + mcdu.simbrief['paxCount'] = data.weights.passengerCount; + mcdu.simbrief['bagCount'] = data.weights.bagCount; + mcdu.simbrief['paxWeight'] = data.weights.passengerWeight; + mcdu.simbrief['bagWeight'] = data.weights.bagWeight; + mcdu.simbrief['freight'] = data.weights.freight; + mcdu.simbrief['cargo'] = data.weights.cargo; + mcdu.simbrief['costIndex'] = data.costIndex; + mcdu.simbrief['navlog'] = data.navlog; + mcdu.simbrief['callsign'] = data.flightNumber; + let alternate = data.alternate; + if (Array.isArray(data.alternate)) { + alternate = data.alternate[0]; + } + mcdu.simbrief['alternateIcao'] = alternate.icao; + mcdu.simbrief['alternateTransAlt'] = alternate.transAlt; + mcdu.simbrief['alternateTransLevel'] = alternate.transLevel; + mcdu.simbrief['alternateAvgWindDir'] = alternate.averageWindDirection; + mcdu.simbrief['alternateAvgWindSpd'] = alternate.averageWindSpeed; + mcdu.simbrief['avgTropopause'] = data.averageTropopause; + mcdu.simbrief['ete'] = data.times.estTimeEnroute; + mcdu.simbrief['blockTime'] = data.times.estBlock; + mcdu.simbrief['outTime'] = data.times.estOut; + mcdu.simbrief['onTime'] = data.times.estOn; + mcdu.simbrief['inTime'] = data.times.estIn; + mcdu.simbrief['offTime'] = data.times.estOff; + mcdu.simbrief['taxiFuel'] = mcdu.simbrief['units'] === 'kgs' ? data.fuel.taxi : lbsToKg(data.fuel.taxi); + mcdu.simbrief['tripFuel'] = + mcdu.simbrief['units'] === 'kgs' ? data.fuel.enrouteBurn : lbsToKg(data.fuel.enrouteBurn); + mcdu.simbrief['sendStatus'] = 'DONE'; + + callback(); + + updateView(); + + return data; + }) + .catch((err) => { + mcdu.simbrief['sendStatus'] = 'READY'; + updateView(); + + // we need to rethrow so the upstream thing can handle it, otherwise its promise will resolve with bad data. + throw err; + }); +}; diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_Core.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_Core.ts new file mode 100644 index 00000000000..84ae9e446e8 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_Core.ts @@ -0,0 +1,103 @@ +import { UpdateThrottler } from '@flybywiresim/fbw-sdk'; +import { A32NX_Util } from '../../../../../shared/src/A32NX_Util'; +import { A32NX_Refuel } from './A32NX_Refuel'; +import { A32NX_Speeds } from './A32NX_Speeds'; +import { A32NX_FADEC } from './A32NX_FADEC'; +import { A32NX_FWC } from './A32NX_FWC'; +import { A32NX_GPWS } from './A32NX_GPWS'; +import { A32NX_SoundManager } from './A32NX_SoundManager'; +import { A32NX_LocalVarUpdater } from './A32NX_LocalVarUpdater'; +import { A32NX_TipsManager } from './A32NX_TipsManager'; + +const ENABLE_TOTAL_UPDATE_TIME_TRACING = false; + +// FIXME move Speeds to somewhere nicer in the FMS, and the rest out to system or extras host +export class A32NX_Core { + private readonly modules = [ + { + name: 'Refuel', + module: new A32NX_Refuel(), + updateInterval: 150, + }, + { + name: 'LocalVars', + module: new A32NX_LocalVarUpdater(), + updateInterval: 50, + }, + { + name: 'FADEC #1', + module: new A32NX_FADEC(1), + updateInterval: 100, + }, + { + name: 'FADEC #2', + module: new A32NX_FADEC(2), + updateInterval: 100, + }, + { + name: 'FWC', + module: new A32NX_FWC(), + updateInterval: 50, + }, + { + name: 'GPWS', + module: new A32NX_GPWS(this), + updateInterval: 75, + }, + { + name: 'Speeds', + module: new A32NX_Speeds(), + updateInterval: 500, + }, + ]; + private readonly moduleThrottlers = new Map(this.modules.map((m) => [m.name, new UpdateThrottler(m.updateInterval)])); + + private readonly soundManager = new A32NX_SoundManager(); + private readonly tipsManager = A32NX_TipsManager.instance; + + private getDeltaTime: ReturnType; + + private isInit = false; + + init() { + this.getDeltaTime = A32NX_Util.createDeltaTimeCalculator(); + this.modules.forEach((moduleDefinition) => { + if ('init' in moduleDefinition.module) { + moduleDefinition.module.init(); + } + }); + + this.isInit = true; + } + + update() { + if (!this.isInit) { + return; + } + + const startTime = ENABLE_TOTAL_UPDATE_TIME_TRACING ? Date.now() : 0; + + const deltaTime = this.getDeltaTime(); + + this.soundManager.update(deltaTime); + this.tipsManager.update(deltaTime); + + let updatedModules = 0; + this.modules.forEach((moduleDefinition) => { + const moduleDeltaTime = this.moduleThrottlers.get(moduleDefinition.name).canUpdate(deltaTime); + + if (moduleDeltaTime !== -1) { + moduleDefinition.module.update(moduleDeltaTime, this); + updatedModules++; + } + }); + + if (ENABLE_TOTAL_UPDATE_TIME_TRACING) { + const endTime = Date.now(); + + const updateTime = endTime - startTime; + + console.warn(`NXCore update took: ${updateTime.toFixed(2)}ms (${updatedModules} modules updated)`); + } + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_FADEC.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_FADEC.ts new file mode 100644 index 00000000000..6ecd0ddd15a --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_FADEC.ts @@ -0,0 +1,86 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +// FIXME move to systems host +export class A32NX_FADEC { + private fadecTimer = -1; + private dcEssPoweredInPreviousUpdate = false; + private lastActiveIgniterAutostart = 0; // 0 = A, 1 = B + private lastEngineState; + private igniting; + private lastIgnitionState; + + constructor(private readonly engine: number) {} + + init() { + this.updateSimVars(); + } + + update(deltaTime) { + const dcEssIsPowered = this.isDcEssPowered(); + const ignitionState = SimVar.GetSimVarValue('L:XMLVAR_ENG_MODE_SEL', 'Enum') === 2; + const engineState = SimVar.GetSimVarValue(`L:A32NX_ENGINE_STATE:${this.engine}`, 'Number'); + const n2Percent = SimVar.GetSimVarValue(`L:A32NX_ENGINE_N2:${this.engine}`, 'Number'); + + if ( + (this.dcEssPoweredInPreviousUpdate !== dcEssIsPowered && dcEssIsPowered === 1) || + (this.lastEngineState !== engineState && engineState === 4) + ) { + this.fadecTimer = 5 * 60; + } + if ((this.lastEngineState === 2 || this.lastEngineState === 3) && engineState !== 2 && engineState !== 3) { + this.lastActiveIgniterAutostart ^= 1; // toggles Igniter + } + + this.igniting = ignitionState && (engineState === 2 || engineState === 3) && n2Percent > 25 && n2Percent < 55; + + if (this.lastIgnitionState !== ignitionState && !ignitionState) { + this.fadecTimer = Math.max(30, this.fadecTimer); + } + this.fadecTimer -= deltaTime / 1000; + this.updateSimVars(); + this.dcEssPoweredInPreviousUpdate = dcEssIsPowered; + } + + updateSimVars() { + this.lastIgnitionState = SimVar.GetSimVarValue('L:XMLVAR_ENG_MODE_SEL', 'Enum') === 2; + this.lastEngineState = SimVar.GetSimVarValue(`L:A32NX_ENGINE_STATE:${this.engine}`, 'Number'); + SimVar.SetSimVarValue(`L:A32NX_FADEC_POWERED_ENG${this.engine}`, 'Bool', this.isPowered() ? 1 : 0); + SimVar.SetSimVarValue( + `L:A32NX_FADEC_IGNITER_A_ACTIVE_ENG${this.engine}`, + 'Bool', + this.igniting && this.lastActiveIgniterAutostart === 0 ? 1 : 0, + ); + SimVar.SetSimVarValue( + `L:A32NX_FADEC_IGNITER_B_ACTIVE_ENG${this.engine}`, + 'Bool', + this.igniting && this.lastActiveIgniterAutostart === 1 ? 1 : 0, + ); + } + + isPowered() { + if (SimVar.GetSimVarValue(`L:A32NX_FIRE_BUTTON_ENG${this.engine}`, 'Bool') === 1) { + return false; + } + if (SimVar.GetSimVarValue(`TURB ENG N2:${this.engine}`, 'Percent') > 15) { + return true; + } + if (SimVar.GetSimVarValue('L:XMLVAR_ENG_MODE_SEL', 'Enum') !== 1) { + return true; + } + if (SimVar.GetSimVarValue(`L:A32NX_OVHD_FADEC_${this.engine}`, 'Bool')) { + return true; + } + if (this.fadecTimer > 0) { + return true; + } + return false; + } + + isDcEssPowered() { + // This will have to be revisited when implementing the FADEC. One shouldn't consider this reference + // to DC ESS valuable: it might be powered by multiple buses or related to other things altogether. + return SimVar.GetSimVarValue('L:A32NX_ELEC_DC_ESS_BUS_IS_POWERED', 'Bool'); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_FWC.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_FWC.ts new file mode 100644 index 00000000000..1474e5db2d8 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_FWC.ts @@ -0,0 +1,390 @@ +import { + Arinc429Word, + NXLogicConfirmNode, + NXLogicMemoryNode, + NXLogicTriggeredMonostableNode, +} from '@flybywiresim/fbw-sdk'; + +// FIXME move to PseudoFWC +export class A32NX_FWC { + // momentary + private toConfigTest = null; // WTOCT + + // persistent + private flightPhase = null; + private ldgMemo = null; + private toMemo = null; + + // ESDL 1. 0. 60 + private gndMemo = new NXLogicConfirmNode(1); // outptuts ZGND + + // ESDL 1. 0. 60 + private eng1OrTwoRunningConf = new NXLogicConfirmNode(30); + + // ESDL 1. 0. 73 + private speedAbove80KtsMemo = new NXLogicMemoryNode(true); + + // ESDL 1. 0. 79 / ESDL 1. 0. 80 + private mctMemo = new NXLogicConfirmNode(60, false); + + // ESDL 1. 0.100 + private firePBOutConf = new NXLogicConfirmNode(0.2); // CONF01 + private firePBOutMemo = new NXLogicTriggeredMonostableNode(2); // MTRIG 05 + private firePBClear10 = new NXLogicMemoryNode(false); + private phase110Memo = new NXLogicTriggeredMonostableNode(300); // MTRIG 03 + private phase8GroundMemo = new NXLogicTriggeredMonostableNode(2); // MTRIG 06 + private ac80KtsMemo = new NXLogicTriggeredMonostableNode(2); // MTRIG 04 + private prevPhase9InvertMemo = new NXLogicTriggeredMonostableNode(3, false); // MTRIG 02 + private eng1Or2TOPowerInvertMemo = new NXLogicTriggeredMonostableNode(1, false); // MTRIG 01 + private phase9Nvm = new NXLogicMemoryNode(true, true); + private prevPhase9 = false; + + // ESDL 1. 0.110 + private groundImmediateMemo = new NXLogicTriggeredMonostableNode(2); // MTRIG 03 + private phase5Memo = new NXLogicTriggeredMonostableNode(120); // MTRIG 01 + private phase67Memo = new NXLogicTriggeredMonostableNode(180); // MTRIG 02 + + // ESDL 1. 0.180 + private memoTo_conf01 = new NXLogicConfirmNode(120, true); // CONF 01 + private memoTo_memo = new NXLogicMemoryNode(false); + + // ESDL 1. 0.190 + private memoLdgMemo_conf01 = new NXLogicConfirmNode(1, true); // CONF 01 + private memoLdgMemo_inhibit = new NXLogicMemoryNode(false); + private memoLdgMemo_conf02 = new NXLogicConfirmNode(10, true); // CONF 01 + private memoLdgMemo_below2000ft = new NXLogicMemoryNode(true); + + // ESDL 1. 0.310 + private memoToInhibit_conf01 = new NXLogicConfirmNode(3, true); // CONF 01 + + // ESDL 1. 0.320 + private memoLdgInhibit_conf01 = new NXLogicConfirmNode(3, true); // CONF 01 + + // altitude warning + private previousTargetAltitude = NaN; + private _wasBellowThreshold = false; + private _wasAboveThreshold = false; + private _wasInRange = false; + private _wasReach200ft = false; + + update(_deltaTime, _core) { + this._updateFlightPhase(_deltaTime); + this._updateButtons(_deltaTime); + this._updateTakeoffMemo(_deltaTime); + this._updateLandingMemo(_deltaTime); + this._updateAltitudeWarning(); + } + + _updateButtons(_deltaTime) { + this.toConfigTest = SimVar.GetSimVarValue('L:A32NX_FWS_TO_CONFIG_TEST', 'boolean'); + } + + _updateFlightPhase(_deltaTime) { + const radioHeight1 = Arinc429Word.fromSimVarValue('L:A32NX_RA_1_RADIO_ALTITUDE'); + const radioHeight2 = Arinc429Word.fromSimVarValue('L:A32NX_RA_2_RADIO_ALTITUDE'); + const radioHeight = + radioHeight1.isFailureWarning() || radioHeight1.isNoComputedData() ? radioHeight2 : radioHeight1; + const eng1N1 = SimVar.GetSimVarValue('ENG N1 RPM:1', 'Percent'); + const eng2N1 = SimVar.GetSimVarValue('ENG N1 RPM:2', 'Percent'); + // TODO find a better source for the following value ("core speed at or above idle") + // Note that N1 starts below idle on spawn on the runway, so this should be below 16 to not jump back to phase 1 + const oneEngRunning = eng1N1 > 15 || eng2N1 > 15; + const eng1Or2Running = this.eng1OrTwoRunningConf.write(oneEngRunning, _deltaTime); + const engOneAndTwoNotRunning = !eng1Or2Running; + const hFail = radioHeight1.isFailureWarning() && radioHeight2.isFailureWarning(); + const adcTestInhib = false; + + // ESLD 1.0.60 + const groundImmediate = Simplane.getIsGrounded(); + const ground = this.gndMemo.write(groundImmediate, _deltaTime); + + // ESLD 1.0.73 + const ias = SimVar.GetSimVarValue('AIRSPEED INDICATED', 'knots'); + const acSpeedAbove80kts = this.speedAbove80KtsMemo.write(ias > 83, ias < 77); + + // ESLD 1.0.90 + const hAbv1500 = radioHeight.isNoComputedData() || radioHeight.value > 1500; + const hAbv800 = radioHeight.isNoComputedData() || radioHeight.value > 800; + + // ESLD 1.0.79 + 1.0.80 + const eng1TLA = SimVar.GetSimVarValue('L:A32NX_AUTOTHRUST_TLA:1', 'number'); + const eng1TLAFTO = SimVar.GetSimVarValue('L:A32NX_AIRLINER_TO_FLEX_TEMP', 'number') !== 0; // is a flex temp is set? + const eng1MCT = eng1TLA > 33.3 && eng1TLA < 36.7; + const eng1TLAFullPwr = eng1TLA > 43.3; + const eng2TLA = SimVar.GetSimVarValue('L:A32NX_AUTOTHRUST_TLA:2', 'number'); + const eng2TLAFTO = eng1TLAFTO; // until we have proper FADECs + const eng2MCT = eng2TLA > 33.3 && eng2TLA < 36.7; + const eng2TLAFullPwr = eng2TLA > 43.3; + const eng1OrEng2SupMCT = !(eng1TLA < 36.7) || !(eng2TLA < 36.7); + const eng1AndEng2MCL = eng1TLA > 22.9 && eng2TLA > 22.9; + const eng1Or2TOPowerSignal = + (eng1TLAFTO && eng1MCT) || + (eng2TLAFTO && eng2MCT) || + eng1OrEng2SupMCT || + eng1OrEng2SupMCT || + eng1TLAFullPwr || + eng2TLAFullPwr; + const eng1Or2TOPower = + eng1Or2TOPowerSignal || (this.mctMemo.write(eng1Or2TOPowerSignal, _deltaTime) && !hAbv1500 && eng1AndEng2MCL); + + // ESLD 1.0.100 + const eng1FirePbOut = SimVar.GetSimVarValue('L:A32NX_FIRE_BUTTON_ENG1', 'Bool'); + const eng1FirePbMemo = this.firePBOutMemo.write(this.firePBOutConf.write(eng1FirePbOut, _deltaTime), _deltaTime); + const resetFirePbClear10 = eng1FirePbMemo && ground; + + const phase8 = + (this.phase8GroundMemo.write(groundImmediate, _deltaTime) || groundImmediate) && + !eng1Or2TOPower && + acSpeedAbove80kts; + + const phase34Cond = ground && eng1Or2TOPower; + const phase3 = !acSpeedAbove80kts && eng1Or2Running && phase34Cond; + const phase4 = acSpeedAbove80kts && phase34Cond; + + const setPhase9Nvm = phase3 || phase8; + const resetPhase9Nvm = + (!this.ac80KtsMemo.write(!acSpeedAbove80kts, _deltaTime) && + ((ground && this.prevPhase9InvertMemo.write(this.prevPhase9, _deltaTime)) || + resetFirePbClear10 || + (ground && this.eng1Or2TOPowerInvertMemo.write(eng1Or2TOPower, _deltaTime))) && + !this.prevPhase9) || + adcTestInhib; + const phase9Nvm = this.phase9Nvm.write(setPhase9Nvm, resetPhase9Nvm); // S* / R (NVM) + const phase29Cond = ground && !eng1Or2TOPower && !acSpeedAbove80kts; + const phase9 = oneEngRunning && phase9Nvm && phase29Cond; + const phase2 = phase29Cond && !phase9Nvm && eng1Or2Running; + + const phase110MemoA = this.firePBClear10.write(phase9, resetFirePbClear10); // S / R* + const phase110Cond = !phase9 && engOneAndTwoNotRunning && groundImmediate; + const phase110Memo = this.phase110Memo.write(phase110MemoA && phase110Cond, _deltaTime); // MTRIG 03 + const phase1 = phase110Cond && !phase110Memo; + const phase10 = phase110Cond && phase110Memo; + + this.prevPhase9 = phase9; + + // ESLD 1.0.110 + const ground2sMemorized = this.groundImmediateMemo.write(groundImmediate, _deltaTime) || groundImmediate; + const phase5Cond = !hAbv1500 && eng1Or2TOPower && !hFail && !ground2sMemorized; + const phase5 = this.phase5Memo.write(phase5Cond, _deltaTime) && phase5Cond; + + const phase67Cond = !ground2sMemorized && !hFail && !eng1Or2TOPower && !hAbv1500 && !hAbv800; + const phase67Memo = this.phase67Memo.write(phase67Cond, _deltaTime) && phase67Cond; + + const phase6 = !phase5 && !ground2sMemorized && !phase67Memo; + const phase7 = phase67Memo && !phase8; + + /*** End of ESLD logic ***/ + + // consolidate into single variable (just to be safe) + const phases = [phase1, phase2, phase3, phase4, phase5, phase6, phase7, phase8, phase9, phase10]; + + if (this.flightPhase === null && phases.indexOf(true) !== -1) { + // if we aren't initialized, just grab the first one that is valid + this._setFlightPhase(phases.indexOf(true) + 1); + console.log(`FWC flight phase: ${this.flightPhase}`); + return; + } + + const activePhases = phases + .map((x, i) => [x, i + 1]) + .filter((y) => !!y[0]) + .map((z) => z[1]); + + // the usual and easy case: only one flight phase is valid + if (activePhases.length === 1) { + if (activePhases[0] !== this.flightPhase) { + console.log(`FWC flight phase: ${this.flightPhase} => ${activePhases[0]}`); + this._setFlightPhase(activePhases[0]); + } + return; + } + + // the mixed case => warn + if (activePhases.length > 1) { + console.warn(`Multiple FWC flight phases are valid: ${activePhases.join(', ')}`); + if (activePhases.indexOf(this.flightPhase) !== -1) { + // if the currently active one is present, keep it + console.warn(`Remaining in FWC flight phase ${this.flightPhase}`); + return; + } + // pick the earliest one + this._setFlightPhase(activePhases[0]); + console.log(`Resolving by switching FWC flight phase: ${this.flightPhase} => ${activePhases[0]}`); + return; + } + + // otherwise, no flight phase is valid => warn + console.warn('No valid FWC flight phase'); + if (this.flightPhase === null) { + this._setFlightPhase(null); + } + } + + _setFlightPhase(flightPhase) { + if (flightPhase === this.flightPhase) { + return; + } + + // update flight phase + this.flightPhase = flightPhase; + SimVar.SetSimVarValue('L:A32NX_FWC_FLIGHT_PHASE', 'Enum', this.flightPhase || 0); + } + + _updateTakeoffMemo(_deltaTime) { + /// FWC ESLD 1.0.180 + const setFlightPhaseMemo = this.flightPhase === 2 && this.toConfigTest; + const resetFlightPhaseMemo = + this.flightPhase === 10 || this.flightPhase === 3 || this.flightPhase === 1 || this.flightPhase === 6; + const flightPhaseMemo = this.memoTo_memo.write(setFlightPhaseMemo, resetFlightPhaseMemo); + + const eng1NotRunning = SimVar.GetSimVarValue('ENG N1 RPM:1', 'Percent') < 15; + const eng2NotRunning = SimVar.GetSimVarValue('ENG N1 RPM:2', 'Percent') < 15; + const toTimerElapsed = this.memoTo_conf01.write(!eng1NotRunning && !eng2NotRunning, _deltaTime); + + this.toMemo = flightPhaseMemo || (this.flightPhase === 2 && toTimerElapsed); + SimVar.SetSimVarValue('L:A32NX_FWC_TOMEMO', 'Bool', this.toMemo); + } + + _updateLandingMemo(_deltaTime) { + const radioHeight1 = Arinc429Word.fromSimVarValue('L:A32NX_RA_1_RADIO_ALTITUDE'); + const radioHeight2 = Arinc429Word.fromSimVarValue('L:A32NX_RA_2_RADIO_ALTITUDE'); + const radioHeight1Invalid = radioHeight1.isFailureWarning() || radioHeight1.isNoComputedData(); + const radioHeight2Invalid = radioHeight2.isFailureWarning() || radioHeight2.isNoComputedData(); + const gearDownlocked = SimVar.GetSimVarValue('GEAR TOTAL PCT EXTENDED', 'percent') > 0.95; + + // FWC ESLD 1.0.190 + const setBelow2000ft = + (radioHeight1.value < 2000 && !radioHeight1Invalid) || (radioHeight2.value < 2000 && !radioHeight2Invalid); + const resetBelow2000ft = + (radioHeight1.value > 2200 || radioHeight1Invalid) && (radioHeight2.value > 2200 || radioHeight2Invalid); + const memo2 = this.memoLdgMemo_below2000ft.write(setBelow2000ft, resetBelow2000ft); + + const setInhibitMemo = this.memoLdgMemo_conf01.write( + resetBelow2000ft && !radioHeight1Invalid && !radioHeight2Invalid, + _deltaTime, + ); + const resetInhibitMemo = !(this.flightPhase === 7 || this.flightPhase === 8 || this.flightPhase === 6); + const memo1 = this.memoLdgMemo_inhibit.write(setInhibitMemo, resetInhibitMemo); + + const showInApproach = memo1 && memo2 && this.flightPhase === 6; + + const invalidRadioMemo = this.memoLdgMemo_conf02.write( + radioHeight1Invalid && radioHeight2Invalid && gearDownlocked && this.flightPhase === 6, + _deltaTime, + ); + + this.ldgMemo = showInApproach || invalidRadioMemo || this.flightPhase === 8 || this.flightPhase === 7; + SimVar.SetSimVarValue('L:A32NX_FWC_LDGMEMO', 'Bool', this.ldgMemo); + } + + _updateAltitudeWarning() { + const indicatedAltitude = Simplane.getAltitude(); + const shortAlert = SimVar.GetSimVarValue('L:A32NX_ALT_DEVIATION_SHORT', 'Bool'); + if (shortAlert === 1) { + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION_SHORT', 'Bool', false); + } + + const warningPressed = + SimVar.GetSimVarValue('L:PUSH_AUTOPILOT_MASTERAWARN_L', 'Bool') || + SimVar.GetSimVarValue('L:PUSH_AUTOPILOT_MASTERAWARN_R', 'Bool'); + if (warningPressed) { + this._wasBellowThreshold = false; + this._wasAboveThreshold = false; + this._wasInRange = false; + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION', 'Bool', false); + return; + } + + if (Simplane.getIsGrounded()) { + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION', 'Bool', false); + } + + // Use FCU displayed value + const currentAltitudeConstraint = SimVar.GetSimVarValue('L:A32NX_FG_ALTITUDE_CONSTRAINT', 'feet'); + const currentFCUAltitude = SimVar.GetSimVarValue('AUTOPILOT ALTITUDE LOCK VAR:3', 'feet'); + const targetAltitude = + currentAltitudeConstraint && !this.hasAltitudeConstraint() ? currentAltitudeConstraint : currentFCUAltitude; + + // Exit when selected altitude is being changed + if (this.previousTargetAltitude !== targetAltitude) { + this.previousTargetAltitude = targetAltitude; + this._wasBellowThreshold = false; + this._wasAboveThreshold = false; + this._wasInRange = false; + this._wasReach200ft = false; + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION_SHORT', 'Bool', false); + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION', 'Bool', false); + return; + } + + // Exit when: + // - Landing gear down & slats extended + // - Glide slope captured + // - Landing locked down + + const landingGearIsDown = + SimVar.GetSimVarValue('L:A32NX_FLAPS_HANDLE_INDEX', 'Enum') >= 1 && + SimVar.GetSimVarValue('L:A32NX_GEAR_HANDLE_POSITION', 'Percent over 100') > 0.5; + const verticalMode = SimVar.GetSimVarValue('L:A32NX_FMA_VERTICAL_MODE', 'Number'); + const glideSlopeCaptured = verticalMode >= 30 && verticalMode <= 34; + const landingGearIsLockedDown = SimVar.GetSimVarValue('GEAR POSITION:0', 'Enum') > 0.9; + const isTcasResolutionAdvisoryActive = SimVar.GetSimVarValue('L:A32NX_TCAS_STATE', 'Enum') > 1; + if (landingGearIsDown || glideSlopeCaptured || landingGearIsLockedDown || isTcasResolutionAdvisoryActive) { + this._wasBellowThreshold = false; + this._wasAboveThreshold = false; + this._wasInRange = false; + this._wasReach200ft = false; + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION_SHORT', 'Bool', false); + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION', 'Bool', false); + return; + } + + const delta = Math.abs(indicatedAltitude - targetAltitude); + + if (delta < 200) { + this._wasBellowThreshold = true; + this._wasAboveThreshold = false; + this._wasReach200ft = true; + } + if (750 < delta) { + this._wasAboveThreshold = true; + this._wasBellowThreshold = false; + } + if (200 <= delta && delta <= 750) { + this._wasInRange = true; + } + + if (this._wasBellowThreshold && this._wasReach200ft) { + if (delta >= 200) { + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION', 'Bool', true); + } else if (delta < 200) { + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION', 'Bool', false); + } + } else if (this._wasAboveThreshold && delta <= 750 && !this._wasReach200ft) { + if ( + !SimVar.GetSimVarValue('L:A32NX_AUTOPILOT_1_ACTIVE', 'Bool') && + !SimVar.GetSimVarValue('L:A32NX_AUTOPILOT_2_ACTIVE', 'Bool') + ) { + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION', 'Bool', false); + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION_SHORT', 'Bool', true); + } + } else if (750 < delta && this._wasInRange && !this._wasReach200ft) { + if (750 < delta) { + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION', 'Bool', true); + } else if (delta >= 750) { + SimVar.SetSimVarValue('L:A32NX_ALT_DEVIATION', 'Bool', false); + } + } + } + + hasAltitudeConstraint() { + // FIXME SUSSY code reading an LVar that's never written + if ( + Simplane.getAutoPilotAltitudeManaged() && + SimVar.GetSimVarValue('L:AP_CURRENT_TARGET_ALTITUDE_IS_CONSTRAINT', 'number') != 0 + ) { + return false; + } + return true; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_FuelPred.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_FuelPred.ts new file mode 100644 index 00000000000..32a910f9b1d --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_FuelPred.ts @@ -0,0 +1,292 @@ +import * as math from 'mathjs'; + +export enum FuelPlanningPhases { + PLANNING = 1, + IN_PROGRESS = 2, + COMPLETED = 3, +} + +// DO NOT TOUCH THESE VALUES +const airDistanceCoeff = math.bignumber( + math.matrix([ + [-5.64e-2, 1.0, -3.3e-7, 1.38e-10, -8.55e-15, -3.73e-18, 4.77e-22], + [-1.06e-3, -2.22e-3, -8.92e-10, 1.31e-12, -4.3e-16, 4.02e-20, 0], + [9.93e-5, 4.89e-6, 1.75e-11, -6.27e-15, 6.91e-19, 0, 0], + [1.12e-7, -1.11e-8, -1.14e-13, 1.73e-17, 0, 0, 0], + [-1.3e-8, 3.04e-11, -2.14e-16, 0, 0, 0, 0], + [-4.07e-12, -5.5e-14, 0, 0, 0, 0, 0], + [3.74e-13, 0, 0, 0, 0, 0, 0], + ]), +); + +// DO NOT TOUCH THESE VALUES +const fuelConsumedCoeff = math.bignumber( + math.matrix([ + [4.069435e2, 1.080068e1, 1.868617e-3, 4.469823e-6, -1.075694e-9, 1.699993e-13], + [-1.429171e1, -8.078463e-2, -7.557951e-5, -6.795487e-9, -1.238178e-12, 0], + [1.984013e-1, 6.804436e-4, 2.789351e-7, 2.353046e-11, 0, 0], + [-1.330668e-3, -2.25176e-6, -3.946554e-10, 0, 0, 0], + [3.930031e-6, 2.634909e-9, 0, 0, 0, 0], + [-4.209321e-9, 0, 0, 0, 0, 0], + ]), +); + +// DO NOT TOUCH THESE VALUES +const timeCoeff = math.bignumber( + math.matrix([ + [-2.307264e2, 1.161741, -1.208222e-3, 1.002013e-7, -2.440974e-11, 4.213891e-15], + [4.151808, -1.124149e-2, 9.688891e-6, -1.392537e-10, -3.745942e-14, 0], + [-2.846925e-2, 5.22107e-5, -2.83909e-8, 5.961502e-13, 0, 0], + [8.892639e-5, -1.087864e-7, 2.572222e-11, 0, 0, 0], + [-1.236801e-7, 8.777364e-11, 0, 0, 0, 0], + [5.521856e-11, 0, 0, 0, 0, 0], + ]), +); + +// DO NOT TOUCH THESE VALUES +const correctionsCoef = math.bignumber( + math.matrix([ + [-4.502431, -2.21216e-3, 1.379723e-5, 9.07125e-8, 3.29184e-12, 3.007572e-18], + [-1.410121e-1, 7.319389e-4, -1.299149e-6, -5.614996e-10, -1.37133e-14, 0], + [3.467151e-3, -1.438481e-6, 7.152032e-9, 9.475944e-13, 0, 0], + [-2.559041e-5, -4.887061e-9, -1.067236e-11, 0, 0, 0], + [7.616725e-8, 1.34523e-11, 0, 0, 0, 0], + [-7.977101e-11, 0, 0, 0, 0, 0], + ]), +); + +// DO NOT TOUCH THESE VALUES +const altTimeCoef = math.bignumber( + math.matrix([ + [-2.491288e1, 4.715493e-1, -8.365416e-4, -2.578474e-6, 2.125971e-8, -3.165746e-11], + [2.537249e-1, -1.867867e-3, 1.154863e-5, -6.299859e-8, 1.098651e-10, 0], + [1.299638e-4, 5.2211e-6, 3.780907e-8, -1.382036e-10, 0, 0], + [-1.363711e-5, -3.246849e-8, 1.174097e-10, 0, 0, 0], + [5.570762e-8, -3.60517e-11, 0, 0, 0, 0], + [-5.290598e-11, 0, 0, 0, 0, 0], + ]), +); + +// DO NOT TOUCH THESE VALUES +const altFuelConsumedCoef = math.bignumber( + math.matrix([ + [-1.150449e3, 2.32835e1, -2.914237e-2, -6.834285e-5, 6.611919e-7, -1.041647e-9], + [1.12289e1, -2.179675e-1, 3.322086e-4, -1.966203e-6, 3.776331e-9, 0], + [3.39762e-2, 1.309511e-3, 1.089408e-6, -5.094745e-9, 0, 0], + [-8.409842e-4, -4.082921e-6, 4.695926e-9, 0, 0, 0], + [3.119312e-6, 1.397091e-9, 0, 0, 0, 0], + [-3.065377e-9, 0, 0, 0, 0, 0], + ]), +); + +// DO NOT TOUCH THESE VALUES +const altCorrectionsCoeff = math.bignumber( + math.matrix([ + [5.7353e1, -1.087438e-1, 2.945632e-4, -1.440854e-6, 4.636839e-9, -5.967608e-12], + [-1.495235, 1.909434e-3, -1.015931e-7, -7.0372e-9, 1.818587e-11, 0], + [1.484228e-2, -8.755315e-6, 7.469694e-9, -2.930156e-11, 0, 0], + [-7.065761e-5, 1.559773e-8, 2.51184e-11, 0, 0, 0], + [1.596208e-7, -2.479681e-11, 0, 0, 0, 0], + [-1.354973e-10, 0, 0, 0, 0, 0], + ]), +); + +// DO NOT TOUCH THESE VALUES +const holdingFFCoeff = math.bignumber( + math.matrix([ + [-7.241814e1, 1.547344e2, -9.771374, 2.825355e-1, -4.163005e-3, 3.112997e-5, -9.425687e-8], + [-8.776689e1, 4.591613, -9.195936e-2, 9.173242e-4, -4.938582e-6, 1.249467e-8, 0], + [8.290402e-1, -3.535182e-2, 5.086226e-4, -2.918997e-6, 5.495734e-9, 0, 0], + [-3.263924e-3, 1.145225e-4, -1.177681e-6, 3.652267e-9, 0, 0, 0], + [5.285811e-6, -1.484045e-7, 8.385686e-10, 0, 0, 0, 0], + [-2.100748e-9, 5.800337e-11, 0, 0, 0, 0, 0], + [-1.558294e-12, 0, 0, 0, 0, 0, 0], + ]), +); + +const userAltTimeCoeff = math.bignumber( + math.matrix([ + [1.934198e1, -3.211068e-3, 7.848773e-6, -9.051067e-9, 3.631462e-12, -4.55553e-16], + [-3.851766e-1, 6.104416e-4, 7.078771e-8, -2.693042e-11, 1.890995e-15, 0], + [2.633289e-3, -4.659318e-6, 5.933422e-11, 1.599828e-14, 0, 0], + [-7.320044e-6, 1.295341e-8, -1.857516e-13, 0, 0, 0], + [6.762639e-9, -1.259232e-11, 0, 0, 0, 0], + [9.144145e-13, 0, 0, 0, 0, 0], + ]), +); + +/** + * @param {number}value - the value to build the matrix from + * @returns {math.matrix} return a 7x7 matrix for A predictors + */ +const _buildAMatrix7 = (value) => { + return math.bignumber( + math.matrix([ + [1, value ** 1, value ** 2, value ** 3, value ** 4, value ** 5, value ** 6], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + ]), + ); +}; + +/** + * @param {number}value - the value to build the matrix from + * @returns {math.matrix} return a 6x6 matrix for A predictors + */ +const _buildAMatrix6 = (value) => { + return math.bignumber( + math.matrix([ + [1, value ** 1, value ** 2, value ** 3, value ** 4, value ** 5], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + ]), + ); +}; + +/** + * @param {number}value - the value to build the matrix from + * @returns {math.matrix} return a 7x7 matrix for B predictors + */ +const _buildBMatrix7 = (value) => { + return math.bignumber( + math.matrix([ + [1, 0, 0, 0, 0, 0, 0], + [value ** 1, 0, 0, 0, 0, 0, 0], + [value ** 2, 0, 0, 0, 0, 0, 0], + [value ** 3, 0, 0, 0, 0, 0, 0], + [value ** 4, 0, 0, 0, 0, 0, 0], + [value ** 5, 0, 0, 0, 0, 0, 0], + [value ** 6, 0, 0, 0, 0, 0, 0], + ]), + ); +}; + +/** + * @param {number}value - the value to build the matrix from + * @returns {math.matrix} return a 6x6 matrix for B predictors + */ +const _buildBMatrix6 = (value) => { + return math.bignumber( + math.matrix([ + [1, 0, 0, 0, 0, 0], + [value ** 1, 0, 0, 0, 0, 0], + [value ** 2, 0, 0, 0, 0, 0], + [value ** 3, 0, 0, 0, 0, 0], + [value ** 4, 0, 0, 0, 0, 0], + [value ** 5, 0, 0, 0, 0, 0], + ]), + ); +}; + +// math.js typings seem to not be nice... it says it may return a complex number when it shouldn't +function isComplex(v: math.MathScalarType): v is math.Complex { + return typeof v === 'object' && 'im' in v; +} + +function getRealNumber(v: math.MathScalarType): number { + // should throw maybe?? + return isComplex(v) ? v.re : math.number(v); +} + +//TODO Refactor this when you have time +export class A32NX_FuelPred { + public static refWeight = 55; + + public static computations = { + TIME: 'time', + FUEL: 'fuel', + CORRECTIONS: 'corrections', + }; + + public static correction = { + LOW_AIR_CONDITIONING: -0.005, + ENGINE_ANTI_ICE_ON: 0.02, + TOTAL_ANTI_ICE_ON: 0.05, + }; + + public static altCorrection = { + LOW_AIR_CONDITIONING: -0.05, + ENGINE_ANTI_ICE_ON: 0.02, + TOTAL_ANTI_ICE_ON: 0.09, + LOW_AIR_CONDITIONING_HIGH_FL: -0.005, + ENGINE_ANTI_ICE_ON_HIGH_FL: 0.015, + TOTAL_ANTI_ICE_ON_HIGH_FL: 0.07, + }; + + /** + * Computes a flight time when a user inputs they're own weight for alternate fuel + * @param {number} fuel - fuel in kg e.g 1200KG + * @param {number} flightLevel - Flight Level in raw form e.g FL120 = 120 + * @return {number} predicted flight time + */ + static computeUserAltTime(fuel, flightLevel) { + const fuelMatrix = _buildAMatrix6(fuel); + const flightLevelMatrix = _buildBMatrix6(flightLevel); + const mmOfFuelFL = math.multiply(flightLevelMatrix, fuelMatrix); + return Math.round(getRealNumber(math.sum(math.dotMultiply(userAltTimeCoeff, mmOfFuelFL)))); + } + + /** + * Computes Air Distance in NM using computed polynomial coefficients + * @param {number} groundDistance - ground distance in NM e.g 200 + * @param {number} windComponent - wind in KTs, HD should be identified with a negative number + * e.g HD150 == -150 vice versa for tailwind + * @returns {number} computedAirDistance in NM + */ + static computeAirDistance(groundDistance, windComponent) { + const groundMatrix = _buildAMatrix7(groundDistance); + const windMatrix = _buildBMatrix7(windComponent); + + const mmOfGroundWind = math.multiply(windMatrix, groundMatrix); + return Math.round(getRealNumber(math.sum(math.dotMultiply(airDistanceCoeff, mmOfGroundWind)))); + } + + /** + * + * @param {number} weight - ZFW weight of the aircraft in padded form e.g 53,000KG = 53 + * @param {number} flightLevel - Flight level in padded form without any alpha chracters e.g FL250 = 250 + * @return {number} predicted fuel flow for one engine per hour e.g result = 600, then 600kg for 30 minutes of holding + */ + static computeHoldingTrackFF(weight, flightLevel) { + const weightMatrix = _buildAMatrix7(weight); + const flightLevelMatrix = _buildBMatrix7(flightLevel); + const mmOfWeightFL = math.multiply(flightLevelMatrix, weightMatrix); + return Math.round(getRealNumber(math.sum(math.dotMultiply(holdingFFCoeff, mmOfWeightFL)))); + } + + /** + * Computes time, fuel and corrections needed for a trip or alternate //TODO work on a new method name + * @param {number} airDistance - air distance in NM e.g 200 + * @param {number} flightLevel - cruising flight level e.g FL290 == 290 + * @param {computations} computation - ENUM of either TIME, FUEL or CORRECTIONS + * @param {boolean} alternate - States whether this computations is for an alternate destination or not + * @returns {number} fuel consumed in KG + */ + static computeNumbers(airDistance, flightLevel, computation, alternate) { + const airDistanceMatrix = _buildAMatrix6(airDistance); + const flightLevelMatrix = _buildBMatrix6(flightLevel); + const mmOfDistFL = math.multiply(flightLevelMatrix, airDistanceMatrix); + //TODO Create logic for handling 200NM and FL390 = 0 + switch (computation) { + case this.computations.FUEL: + return Math.round( + getRealNumber(math.sum(math.dotMultiply(alternate ? altFuelConsumedCoef : fuelConsumedCoeff, mmOfDistFL))), + ); + case this.computations.TIME: + return Math.round(getRealNumber(math.sum(math.dotMultiply(alternate ? altTimeCoef : timeCoeff, mmOfDistFL)))); + case this.computations.CORRECTIONS: + return Math.round( + getRealNumber(math.sum(math.dotMultiply(alternate ? altCorrectionsCoeff : correctionsCoef, mmOfDistFL))), + ); + } + } + + constructor() {} +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_GPWS.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_GPWS.ts new file mode 100644 index 00000000000..9a15344a390 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_GPWS.ts @@ -0,0 +1,1118 @@ +import { + Arinc429SignStatusMatrix, + Arinc429Word, + NXDataStore, + NXLogicConfirmNode, + NXLogicTriggeredMonostableNode, +} from '@flybywiresim/fbw-sdk'; +import { A32NX_Util } from '../../../../../shared/src/A32NX_Util'; +import { A32NX_DEFAULT_RADIO_AUTO_CALL_OUTS, A32NXRadioAutoCallOutFlags } from '@shared/AutoCallOuts'; +import { SoundDefinition, soundList } from './A32NX_SoundManager'; +import { FmgcFlightPhase } from '@shared/flightphase'; + +// FIXME move GPWS logic to systems host, and ACOs to PseudoFWC +export class A32NX_GPWS { + private autoCallOutPins = A32NX_DEFAULT_RADIO_AUTO_CALL_OUTS; + + private minimumsState = 0; + + private Mode3MaxBaroAlt = NaN; + + private Mode4MaxRAAlt = 0; + + private Mode2BoundaryLeaveAlt = NaN; + private Mode2NumTerrain = 0; + private Mode2NumFramesInBoundary = 0; + + private RadioAltRate = NaN; + private prevRadioAlt = NaN; + private prevRadioAlt2 = NaN; + + private modes: { + type: { sound?: SoundDefinition; soundPeriod?: number; gpwsLight?: boolean; pullUp?: boolean }[]; + current: number; + previous: number; + onChange?: (current: number, _: any) => void; + }[] = [ + // Mode 1 + { + // 0: no warning, 1: "sink rate", 2 "pull up" + current: 0, + previous: 0, + type: [{}, { sound: soundList.sink_rate, soundPeriod: 1.1, gpwsLight: true }, { gpwsLight: true, pullUp: true }], + }, + // Mode 2 is currently inactive. + { + // 0: no warning, 1: "terrain", 2: "pull up" + current: 0, + previous: 0, + type: [{}, { gpwsLight: true }, { gpwsLight: true, pullUp: true }], + }, + // Mode 3 + { + // 0: no warning, 1: "don't sink" + current: 0, + previous: 0, + type: [{}, { sound: soundList.dont_sink, soundPeriod: 1.1, gpwsLight: true }], + }, + // Mode 4 + { + // 0: no warning, 1: "too low gear", 2: "too low flaps", 3: "too low terrain" + current: 0, + previous: 0, + type: [ + {}, + { sound: soundList.too_low_gear, soundPeriod: 1.1, gpwsLight: true }, + { sound: soundList.too_low_flaps, soundPeriod: 1.1, gpwsLight: true }, + { sound: soundList.too_low_terrain, soundPeriod: 1.1, gpwsLight: true }, + ], + }, + // Mode 5, not all warnings are fully implemented + { + // 0: no warning, 1: "glideslope", 2: "hard glideslope" (louder) + current: 0, + previous: 0, + type: [{}, {}, {}], + onChange: (current: number, _: any) => { + this.setGlideSlopeWarning(current >= 1); + }, + }, + ]; + + private PrevShouldPullUpPlay = false; + + private AltCallState = A32NX_Util.createMachine(AltCallStateMachine); + + private RetardState = A32NX_Util.createMachine(RetardStateMachine); + + private isAirVsGroundMode = SimVar.GetSimVarValue('L:A32NX_GPWS_GROUND_STATE', 'Bool') !== 1; + private airborneFor5s = new NXLogicConfirmNode(5); + private airborneFor10s = new NXLogicConfirmNode(10); + + private isApproachVsTakeoffState = SimVar.GetSimVarValue('L:A32NX_GPWS_APPROACH_STATE', 'Bool') === 1; + + private isOverflightDetected = new NXLogicTriggeredMonostableNode(60, false); + // Only relevant if alternate mode 4b is enabled + private isMode4aInhibited = false; + + // PIN PROGs + private isAudioDeclutterEnabled = false; + private isAlternateMode4bEnabled = false; + private isTerrainClearanceFloorEnabled = false; + private isTerrainAwarenessEnabled = false; + + private egpwsAlertDiscreteWord1 = Arinc429Word.empty(); + private egpwsAlertDiscreteWord2 = Arinc429Word.empty(); + + constructor(private core) { + console.log('A32NX_GPWS constructed'); + + this.AltCallState.setState('ground'); + this.RetardState.setState('landed'); + } + + gpwsUpdateDiscreteWords() { + this.egpwsAlertDiscreteWord1.ssm = Arinc429SignStatusMatrix.NormalOperation; + this.egpwsAlertDiscreteWord1.setBitValue(11, this.modes[0].current === 1); + this.egpwsAlertDiscreteWord1.setBitValue(12, this.modes[0].current === 2); + this.egpwsAlertDiscreteWord1.setBitValue(13, this.modes[1].current === 1); + this.egpwsAlertDiscreteWord1.setBitValue(12, this.modes[1].current === 2); + this.egpwsAlertDiscreteWord1.setBitValue(14, this.modes[2].current === 1); + this.egpwsAlertDiscreteWord1.setBitValue(15, this.modes[3].current === 1); + this.egpwsAlertDiscreteWord1.setBitValue(16, this.modes[3].current === 2); + this.egpwsAlertDiscreteWord1.setBitValue(17, this.modes[3].current === 3); + this.egpwsAlertDiscreteWord1.setBitValue(18, this.modes[4].current === 1); + Arinc429Word.toSimVarValue( + 'L:A32NX_EGPWS_ALERT_1_DISCRETE_WORD_1', + this.egpwsAlertDiscreteWord1.value, + this.egpwsAlertDiscreteWord1.ssm, + ); + Arinc429Word.toSimVarValue( + 'L:A32NX_EGPWS_ALERT_2_DISCRETE_WORD_1', + this.egpwsAlertDiscreteWord1.value, + this.egpwsAlertDiscreteWord1.ssm, + ); + + this.egpwsAlertDiscreteWord2.ssm = Arinc429SignStatusMatrix.NormalOperation; + this.egpwsAlertDiscreteWord2.setBitValue(14, false); + Arinc429Word.toSimVarValue( + 'L:A32NX_EGPWS_ALERT_1_DISCRETE_WORD_2', + this.egpwsAlertDiscreteWord2.value, + this.egpwsAlertDiscreteWord2.ssm, + ); + Arinc429Word.toSimVarValue( + 'L:A32NX_EGPWS_ALERT_2_DISCRETE_WORD_2', + this.egpwsAlertDiscreteWord2.value, + this.egpwsAlertDiscreteWord2.ssm, + ); + } + + setGlideSlopeWarning(state) { + SimVar.SetSimVarValue('L:A32NX_GPWS_GS_Warning_Active', 'Bool', state ? 1 : 0); // Still need this for XML + this.egpwsAlertDiscreteWord2.setBitValue(11, state); + Arinc429Word.toSimVarValue( + 'L:A32NX_EGPWS_ALERT_1_DISCRETE_WORD_2', + this.egpwsAlertDiscreteWord2.value, + this.egpwsAlertDiscreteWord2.ssm, + ); + Arinc429Word.toSimVarValue( + 'L:A32NX_EGPWS_ALERT_2_DISCRETE_WORD_2', + this.egpwsAlertDiscreteWord2.value, + this.egpwsAlertDiscreteWord2.ssm, + ); + } + + setGpwsWarning(state) { + SimVar.SetSimVarValue('L:A32NX_GPWS_Warning_Active', 'Bool', state ? 1 : 0); // Still need this for XML + this.egpwsAlertDiscreteWord2.setBitValue(12, state); + this.egpwsAlertDiscreteWord2.setBitValue(13, state); + Arinc429Word.toSimVarValue( + 'L:A32NX_EGPWS_ALERT_1_DISCRETE_WORD_2', + this.egpwsAlertDiscreteWord2.value, + this.egpwsAlertDiscreteWord2.ssm, + ); + Arinc429Word.toSimVarValue( + 'L:A32NX_EGPWS_ALERT_2_DISCRETE_WORD_2', + this.egpwsAlertDiscreteWord2.value, + this.egpwsAlertDiscreteWord2.ssm, + ); + } + + init() { + console.log('A32NX_GPWS init'); + + this.setGlideSlopeWarning(false); + this.setGpwsWarning(false); + + NXDataStore.getAndSubscribe( + 'CONFIG_A32NX_FWC_RADIO_AUTO_CALL_OUT_PINS', + (k, v) => k === 'CONFIG_A32NX_FWC_RADIO_AUTO_CALL_OUT_PINS' && (this.autoCallOutPins = Number(v)), + A32NX_DEFAULT_RADIO_AUTO_CALL_OUTS.toString(), + ); + } + + update(deltaTime, _core) { + this.gpws(deltaTime); + } + + gpws(deltaTime) { + // EGPWS receives ADR1 only + const baroAlt = Arinc429Word.fromSimVarValue('L:A32NX_ADIRS_ADR_1_BARO_CORRECTED_ALTITUDE_1'); + const computedAirspeed = Arinc429Word.fromSimVarValue('L:A32NX_ADIRS_ADR_1_COMPUTED_AIRSPEED'); + const pitch = Arinc429Word.fromSimVarValue('L:A32NX_ADIRS_IR_1_PITCH'); + const inertialVs = Arinc429Word.fromSimVarValue('L:A32NX_ADIRS_IR_1_VERTICAL_SPEED'); + const barometricVs = Arinc429Word.fromSimVarValue('L:A32NX_ADIRS_ADR_1_BAROMETRIC_VERTICAL_SPEED'); + const radioAlt1 = Arinc429Word.fromSimVarValue('L:A32NX_RA_1_RADIO_ALTITUDE'); + const radioAlt2 = Arinc429Word.fromSimVarValue('L:A32NX_RA_2_RADIO_ALTITUDE'); + const radioAlt = radioAlt1.isFailureWarning() || radioAlt1.isNoComputedData() ? radioAlt2 : radioAlt1; + const radioAltValid = radioAlt.isNormalOperation(); + const isOnGround = !this.isAirVsGroundMode; + + const isGpwsSysOff = SimVar.GetSimVarValue('L:A32NX_GPWS_SYS_OFF', 'Bool') === 1; + const isTerrModeOff = SimVar.GetSimVarValue('L:A32NX_GPWS_TERR_OFF', 'Bool') === 1; + const isFlapModeOff = SimVar.GetSimVarValue('L:A32NX_GPWS_FLAP_OFF', 'Bool') === 1; + const isLdgFlap3On = SimVar.GetSimVarValue('L:A32NX_GPWS_FLAPS3', 'Bool') === 1; + + const sfccPositionWord = Arinc429Word.fromSimVarValue('L:A32NX_SFCC_SLAT_FLAP_ACTUAL_POSITION_WORD'); + const isFlapsFull = sfccPositionWord.bitValueOr(22, false); + const isFlaps3 = sfccPositionWord.bitValueOr(21, false) && !isFlapsFull; + + const areFlapsInLandingConfig = + !sfccPositionWord.isNormalOperation() || isFlapModeOff || (isLdgFlap3On ? isFlaps3 : isFlapsFull); + const isGearDownLocked = SimVar.GetSimVarValue('L:A32NX_LGCIU_1_LEFT_GEAR_DOWNLOCKED', 'Bool') === 1; + + // TODO only use this in the air? + const isNavAccuracyHigh = SimVar.GetSimVarValue('L:A32NX_FMGC_L_NAV_ACCURACY_HIGH', 'Bool') === 1; + const isTcfOperational = this.isTerrainClearanceFloorOperational(isTerrModeOff, radioAlt, isNavAccuracyHigh); + const isTafOperational = this.isTerrainAwarenessOperational(isTerrModeOff); + + this.UpdateAltState(radioAltValid ? radioAlt.value : NaN); + this.differentiate_radioalt(radioAltValid ? radioAlt.value : NaN, deltaTime); + + const mda = SimVar.GetSimVarValue('L:AIRLINER_MINIMUM_DESCENT_ALTITUDE', 'feet'); + const dh = SimVar.GetSimVarValue('L:AIRLINER_DECISION_HEIGHT', 'feet'); + + this.update_maxRA(radioAlt, isOnGround); + + const isOverflightDetected = this.updateOverflightState(deltaTime); + this.updateMode4aInhibited(isGearDownLocked, areFlapsInLandingConfig); + + this.updateAirGroundState(deltaTime, computedAirspeed, radioAlt, pitch); + this.updateApproachTakeoffState( + computedAirspeed, + radioAlt, + isGearDownLocked, + areFlapsInLandingConfig, + isTcfOperational, + isTafOperational, + isOverflightDetected, + ); + + if (!isGpwsSysOff && radioAltValid && radioAlt.value >= 10 && radioAlt.value <= 2450) { + //Activate between 10 - 2450 radio alt unless SYS is off + const altRate = this.selectAltitudeRate(inertialVs, barometricVs); + + this.GPWSMode1(this.modes[0], radioAlt.value, altRate); + //Mode 2 is disabled because of an issue with the terrain height simvar which causes false warnings very frequently. See PR#1742 for more info + //this.GPWSMode2(this.modes[1], radioAlt, computedAirspeed, areFlapsInLandingConfig, isGearDownLocked); + this.GPWSMode3(this.modes[2], baroAlt, radioAlt.value, altRate, areFlapsInLandingConfig, isGearDownLocked); + this.GPWSMode4( + this.modes[3], + radioAlt.value, + computedAirspeed, + areFlapsInLandingConfig, + isGearDownLocked, + isTcfOperational, + isTafOperational, + isOverflightDetected, + ); + this.GPWSMode5(this.modes[4], radioAlt.value); + } else { + this.modes.forEach((mode) => { + mode.current = 0; + }); + + this.Mode3MaxBaroAlt = NaN; + + this.setGlideSlopeWarning(false); + this.setGpwsWarning(false); + } + + this.GPWSComputeLightsAndCallouts(); + this.gpwsUpdateDiscreteWords(); + + if (mda !== 0 || (dh !== -1 && dh !== -2 && this.isApproachVsTakeoffState)) { + let minimumsDA; //MDA or DH + let minimumsIA; //radio or baro altitude + if (dh >= 0) { + minimumsDA = dh; + minimumsIA = radioAlt.isNormalOperation() || radioAlt.isFunctionalTest() ? radioAlt.value : NaN; + } else { + minimumsDA = mda; + minimumsIA = baroAlt.isNormalOperation() || baroAlt.isFunctionalTest() ? baroAlt.value : NaN; + } + if (isFinite(minimumsDA) && isFinite(minimumsIA)) { + this.gpws_minimums(minimumsDA, minimumsIA); + } + } + } + + /** + * Takes the derivative of the radio altimeter. Using central difference, to prevent high frequency noise + * @param radioAlt - in feet + * @param deltaTime - in milliseconds + */ + differentiate_radioalt(radioAlt, deltaTime) { + if (!isNaN(this.prevRadioAlt2) && !isNaN(radioAlt)) { + this.RadioAltRate = (radioAlt - this.prevRadioAlt2) / (deltaTime / 1000 / 60) / 2; + this.prevRadioAlt2 = this.prevRadioAlt; + this.prevRadioAlt = radioAlt; + } else if (!isNaN(this.prevRadioAlt) && !isNaN(radioAlt)) { + this.prevRadioAlt2 = this.prevRadioAlt; + this.prevRadioAlt = radioAlt; + } else { + this.prevRadioAlt2 = radioAlt; + } + } + + update_maxRA(radioAlt, isOnGround) { + // on ground check is to get around the fact that radio alt is set to around 300 while loading + if (isOnGround || this.isApproachVsTakeoffState) { + this.Mode4MaxRAAlt = 0; + } else if (radioAlt.isNormalOperation()) { + this.Mode4MaxRAAlt = Math.max(this.Mode4MaxRAAlt, 0.75 * radioAlt.value); + } + } + + gpws_minimums(minimumsDA, minimumsIA) { + let over100Above = false; + let overMinimums = false; + + if (minimumsDA <= 90) { + overMinimums = minimumsIA >= minimumsDA + 15; + over100Above = minimumsIA >= minimumsDA + 115; + } else { + overMinimums = minimumsIA >= minimumsDA + 5; + over100Above = minimumsIA >= minimumsDA + 105; + } + if (this.minimumsState === 0 && overMinimums) { + this.minimumsState = 1; + } else if (this.minimumsState === 1 && over100Above) { + this.minimumsState = 2; + } else if (this.minimumsState === 2 && !over100Above) { + this.core.soundManager.tryPlaySound(soundList.hundred_above); + this.minimumsState = 1; + } else if (this.minimumsState === 1 && !overMinimums) { + this.core.soundManager.tryPlaySound(soundList.minimums); + this.minimumsState = 0; + } + } + + GPWSComputeLightsAndCallouts() { + this.modes.forEach((mode) => { + if (mode.current === mode.previous) { + return; + } + + const previousType = mode.type[mode.previous]; + this.core.soundManager.removePeriodicSound(previousType.sound); + + const currentType = mode.type[mode.current]; + this.core.soundManager.addPeriodicSound(currentType.sound, currentType.soundPeriod); + + if (mode.onChange) { + mode.onChange(mode.current, mode.previous); + } + + mode.previous = mode.current; + }); + + const activeTypes = this.modes.map((mode) => mode.type[mode.current]); + + const shouldPullUpPlay = activeTypes.some((type) => type.pullUp); + if (shouldPullUpPlay !== this.PrevShouldPullUpPlay) { + if (shouldPullUpPlay) { + this.core.soundManager.addPeriodicSound(soundList.pull_up, 1.1); + } else { + this.core.soundManager.removePeriodicSound(soundList.pull_up); + } + this.PrevShouldPullUpPlay = shouldPullUpPlay; + } + + const illuminateGpwsLight = activeTypes.some((type) => type.gpwsLight); + this.setGpwsWarning(illuminateGpwsLight); + } + + /** + * Compute the GPWS Mode 1 state. + * @param mode - The mode object which stores the state. + * @param radioAlt - Radio altitude in feet + * @param vSpeed - Vertical speed, in feet/min, NaN if not available + */ + GPWSMode1(mode, radioAlt, vSpeed) { + const sinkrate = -vSpeed; + + if (!Number.isFinite(sinkrate) || sinkrate <= 1000) { + mode.current = 0; + return; + } + + const maxSinkrateAlt = 0.61 * sinkrate - 600; + const maxPullUpAlt = sinkrate < 1700 ? 1.3 * sinkrate - 1940 : 0.4 * sinkrate - 410; + + if (radioAlt <= maxPullUpAlt) { + mode.current = 2; + } else if (radioAlt <= maxSinkrateAlt) { + mode.current = 1; + } else { + mode.current = 0; + } + } + + /** + * Compute the GPWS Mode 2 state. + * @param mode - The mode object which stores the state. + * @param radioAlt - Radio altitude in feet + * @param computedAirspeed - Arinc429 value of the computed airspeed in knots. + * @param areFlapsInLandingConfig - If flaps is in landing config + * @param gearExtended - If the gear is deployed + */ + GPWSMode2(mode, radioAlt, computedAirspeed, areFlapsInLandingConfig, gearExtended) { + if (!computedAirspeed.isNormalOperation()) { + return false; + } + + let IsInBoundary = false; + const UpperBoundaryRate = + -this.RadioAltRate < 3500 ? 0.7937 * -this.RadioAltRate - 1557.5 : 0.19166 * -this.RadioAltRate + 610; + const UpperBoundarySpeed = Math.max(1650, Math.min(2450, 8.8888 * computedAirspeed.value - 305.555)); + + if (!areFlapsInLandingConfig && -this.RadioAltRate > 2000) { + if (radioAlt < UpperBoundarySpeed && radioAlt < UpperBoundaryRate) { + this.Mode2NumFramesInBoundary += 1; + } else { + this.Mode2NumFramesInBoundary = 0; + } + } else if (areFlapsInLandingConfig && -this.RadioAltRate > 2000) { + if (radioAlt < 775 && radioAlt < UpperBoundaryRate && -this.RadioAltRate < 10000) { + this.Mode2NumFramesInBoundary += 1; + } else { + this.Mode2NumFramesInBoundary = 0; + } + } + // This is to prevent very quick changes in radio alt rate triggering the alarm. The derivative is sadly pretty jittery. + if (this.Mode2NumFramesInBoundary > 5) { + IsInBoundary = true; + } + + if (IsInBoundary) { + this.Mode2BoundaryLeaveAlt = -1; + if (this.Mode2NumTerrain < 2 || gearExtended) { + if (this.core.soundManager.tryPlaySound(soundList.too_low_terrain)) { + // too low terrain is not correct, but no "terrain" call yet + this.Mode2NumTerrain += 1; + } + mode.current = 1; + } else if (!gearExtended) { + mode.current = 2; + } + } else if (this.Mode2BoundaryLeaveAlt === -1) { + this.Mode2BoundaryLeaveAlt = radioAlt; + } else if (this.Mode2BoundaryLeaveAlt + 300 > radioAlt) { + mode.current = 1; + this.core.soundManager.tryPlaySound(soundList.too_low_terrain); + } else if (this.Mode2BoundaryLeaveAlt + 300 <= radioAlt) { + mode.current = 0; + this.Mode2NumTerrain = 0; + this.Mode2BoundaryLeaveAlt = NaN; + } + } + + /** + * Compute the GPWS Mode 3 state. + * @param {*} mode - The mode object which stores the state. + * @param {*} baroAlt - Arinc429 value of the barometric altitude in feet. + * @param {*} radioAlt - Radio altitude in feet + * @param {*} altRate - Altitude rate in feet per minute + * @param {*} areFlapsInLandingConfig - True if flaps is in landing config + * @param {*} isGearDownLocked - True if the gear is down and locked + */ + GPWSMode3(mode, baroAlt, radioAlt, altRate, areFlapsInLandingConfig, isGearDownLocked) { + if ( + (isGearDownLocked && areFlapsInLandingConfig) || + this.isApproachVsTakeoffState || + radioAlt > 1500 || + radioAlt < 10 || + !baroAlt.isNormalOperation() + ) { + this.Mode3MaxBaroAlt = NaN; + mode.current = 0; + return; + } + + const maxAltLoss = 0.09 * radioAlt + 7.1; + const hasPositiveAltRate = Number.isFinite(altRate) && altRate > 0; + + if (baroAlt.value > this.Mode3MaxBaroAlt || isNaN(this.Mode3MaxBaroAlt)) { + this.Mode3MaxBaroAlt = baroAlt.value; + mode.current = 0; + } else if (!hasPositiveAltRate && this.Mode3MaxBaroAlt - baroAlt.value > maxAltLoss) { + mode.current = 1; + } else { + mode.current = 0; + } + } + + /** + * Compute the GPWS Mode 4 state. + * @param mode - The mode object which stores the state. + * @param radioAlt - Radio altitude in feet + * @param computedAirspeed - ARINC value of the computed airspeed in knots. + * @param areFlapsInLandingConfig - If flaps is in landing config + * @param gearExtended - If the gear is extended + * @param tcfOperational - If the terrain clearance floor is operational + * @param tafOperational - If the terrain awareness floor is operational + * @param isOverflightDetected - If an overflight is detected + * @constructor + */ + GPWSMode4( + mode, + radioAlt, + computedAirspeed, + areFlapsInLandingConfig, + gearExtended, + tcfOperational, + tafOperational, + isOverflightDetected, + ) { + mode.current = 0; + + if (!computedAirspeed.isNormalOperation() || radioAlt < 30 || radioAlt > 1000 || !this.isAirVsGroundMode) { + return; + } + + const isMode4cEnabled = + !this.isApproachVsTakeoffState && (!areFlapsInLandingConfig || !gearExtended) && this.isAirVsGroundMode; + + // Mode 4 A + if (this.isApproachVsTakeoffState && !gearExtended && !this.isMode4aInhibited) { + const boundary = this.mode4aUpperBoundary( + computedAirspeed.value, + areFlapsInLandingConfig, + tcfOperational, + tafOperational, + isOverflightDetected, + ); + + if (computedAirspeed.value < 190 && radioAlt < 500) { + mode.current = 1; // TOO LOW GEAR + } else if (computedAirspeed.value >= 190 && radioAlt < boundary) { + mode.current = areFlapsInLandingConfig ? 1 : 3; // TOO LOW GEAR or TOO LOW TERRAIN + } + // Mode 4 B + } else if (this.isApproachVsTakeoffState && (!areFlapsInLandingConfig || !gearExtended)) { + // Normal 4b mode, flaps down, what is the boundary? + const boundary = this.mode4bUpperBoundary( + computedAirspeed.value, + areFlapsInLandingConfig, + tcfOperational, + tafOperational, + isOverflightDetected, + ); + + if (computedAirspeed.value < 159 && radioAlt < 245) { + mode.current = !gearExtended ? 1 : 2; // TOO LOW GEAR or TOO LOW FLAPS + } else if (computedAirspeed.value >= 159 && radioAlt < boundary) { + mode.current = this.isMode4aInhibited ? 1 : 3; // TOO LOW GEAR or TOO LOW TERRAIN + } + // Mode 4 C + } else if (isMode4cEnabled) { + const maximumFloor = this.mode4cUpperBoundary(computedAirspeed.value); + const maximumFilterValue = Math.min(maximumFloor, this.Mode4MaxRAAlt); + + if (radioAlt < maximumFilterValue) { + mode.current = 3; + } + } + } + + /** + * Compute the GPWS Mode 5 state. + * @param mode - The mode object which stores the state. + * @param - radioAlt Radio altitude in feet + * @constructor + */ + GPWSMode5(mode, radioAlt) { + if (radioAlt > 1000 || radioAlt < 30 || SimVar.GetSimVarValue('L:A32NX_GPWS_GS_OFF', 'Bool')) { + mode.current = 0; + return; + } + + // FIXME add backcourse inhibit + if (!SimVar.GetSimVarValue('L:A32NX_RADIO_RECEIVER_GS_IS_VALID', 'number')) { + mode.current = 0; + return; + } + const error = SimVar.GetSimVarValue('L:A32NX_RADIO_RECEIVER_GS_DEVIATION', 'number'); + const dots = -error * 2.5; //According to the FCOM, one dot is approx. 0.4 degrees. 1/0.4 = 2.5 + + const minAltForWarning = dots < 2.9 ? -75 * dots + 247.5 : 30; + const minAltForHardWarning = dots < 3.8 ? -66.66 * dots + 283.33 : 30; + + if (dots > 2 && radioAlt > minAltForHardWarning && radioAlt < 350) { + mode.current = 2; + } else if (dots > 1.3 && radioAlt > minAltForWarning) { + mode.current = 1; + } else { + mode.current = 0; + } + } + + UpdateAltState(radioAlt) { + if (isNaN(radioAlt)) { + return; + } + switch (this.AltCallState.value) { + case 'ground': + if (radioAlt > 6) { + this.AltCallState.action('up'); + } + break; + case 'over5': + if (radioAlt > 12) { + this.AltCallState.action('up'); + } else if (radioAlt <= 6) { + if (this.RetardState.value !== 'retardPlaying' && this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Five) { + this.core.soundManager.tryPlaySound(soundList.alt_5); + } + this.AltCallState.action('down'); + } + break; + case 'over10': + if (radioAlt > 22) { + this.AltCallState.action('up'); + } else if (radioAlt <= 12) { + if (this.RetardState.value !== 'retardPlaying' && this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Ten) { + this.core.soundManager.tryPlaySound(soundList.alt_10); + } + this.AltCallState.action('down'); + } + break; + case 'over20': + if (radioAlt > 32) { + this.AltCallState.action('up'); + } else if (radioAlt <= 22) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Twenty) { + this.core.soundManager.tryPlaySound(soundList.alt_20); + } + this.AltCallState.action('down'); + } + break; + case 'over30': + if (radioAlt > 42) { + this.AltCallState.action('up'); + } else if (radioAlt <= 32) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Thirty) { + this.core.soundManager.tryPlaySound(soundList.alt_30); + } + this.AltCallState.action('down'); + } + break; + case 'over40': + if (radioAlt > 53) { + this.AltCallState.action('up'); + } else if (radioAlt <= 42) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Forty) { + this.core.soundManager.tryPlaySound(soundList.alt_40); + } + this.AltCallState.action('down'); + } + break; + case 'over50': + if (radioAlt > 110) { + this.AltCallState.action('up'); + } else if (radioAlt <= 53) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.Fifty) { + this.core.soundManager.tryPlaySound(soundList.alt_50); + } + this.AltCallState.action('down'); + } + break; + case 'over100': + if (radioAlt > 210) { + this.AltCallState.action('up'); + } else if (radioAlt <= 110) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.OneHundred) { + this.core.soundManager.tryPlaySound(soundList.alt_100); + } + this.AltCallState.action('down'); + } + break; + case 'over200': + if (radioAlt > 310) { + this.AltCallState.action('up'); + } else if (radioAlt <= 210) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.TwoHundred) { + this.core.soundManager.tryPlaySound(soundList.alt_200); + } + this.AltCallState.action('down'); + } + break; + case 'over300': + if (radioAlt > 410) { + this.AltCallState.action('up'); + } else if (radioAlt <= 310) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.ThreeHundred) { + this.core.soundManager.tryPlaySound(soundList.alt_300); + } + this.AltCallState.action('down'); + } + break; + case 'over400': + if (radioAlt > 513) { + this.AltCallState.action('up'); + } else if (radioAlt <= 410) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.FourHundred) { + this.core.soundManager.tryPlaySound(soundList.alt_400); + } + this.AltCallState.action('down'); + } + break; + case 'over500': + if (radioAlt > 1020) { + this.AltCallState.action('up'); + } else if (radioAlt <= 513) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.FiveHundred) { + this.core.soundManager.tryPlaySound(soundList.alt_500); + } + this.AltCallState.action('down'); + } + break; + case 'over1000': + if (radioAlt > 2020) { + this.AltCallState.action('up'); + } else if (radioAlt <= 1020) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.OneThousand) { + this.core.soundManager.tryPlaySound(soundList.alt_1000); + } + this.AltCallState.action('down'); + } + break; + case 'over2000': + if (radioAlt > 2530) { + this.AltCallState.action('up'); + } else if (radioAlt <= 2020) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.TwoThousand) { + this.core.soundManager.tryPlaySound(soundList.alt_2000); + } + this.AltCallState.action('down'); + } + break; + case 'over2500': + if (radioAlt <= 2530) { + if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.TwoThousandFiveHundred) { + this.core.soundManager.tryPlaySound(soundList.alt_2500); + } else if (this.autoCallOutPins & A32NXRadioAutoCallOutFlags.TwentyFiveHundred) { + this.core.soundManager.tryPlaySound(soundList.alt_2500b); + } + this.AltCallState.action('down'); + } + break; + } + + switch (this.RetardState.value) { + case 'overRetard': + if (radioAlt < 20) { + if (!SimVar.GetSimVarValue('L:A32NX_AUTOPILOT_ACTIVE', 'Bool')) { + this.RetardState.action('play'); + this.core.soundManager.addPeriodicSound(soundList.retard, 1.1); + } else if (radioAlt < 10) { + this.RetardState.action('play'); + this.core.soundManager.addPeriodicSound(soundList.retard, 1.1); + } + } + break; + case 'retardPlaying': + if ( + SimVar.GetSimVarValue('L:A32NX_AUTOTHRUST_TLA:1', 'number') < 2.6 || + SimVar.GetSimVarValue('L:A32NX_AUTOTHRUST_TLA:2', 'number') < 2.6 + ) { + this.RetardState.action('land'); + this.core.soundManager.removePeriodicSound(soundList.retard); + } else if ( + SimVar.GetSimVarValue('L:A32NX_FMGC_FLIGHT_PHASE', 'Enum') === FmgcFlightPhase.GoAround || + radioAlt > 20 + ) { + this.RetardState.action('go_around'); + this.core.soundManager.removePeriodicSound(soundList.retard); + } + break; + case 'landed': + if (radioAlt > 20) { + this.RetardState.action('up'); + } + break; + } + } + + updateAirGroundState(deltaTime, computedAirspeed, radioAlt, pitchAngle) { + if (!computedAirspeed.isNormalOperation() || !radioAlt.isNormalOperation()) { + // Stay in current state + return; + } + + this.airborneFor5s.write( + computedAirspeed.value > 90 && radioAlt.value > 25 && pitchAngle.isNormalOperation() && pitchAngle.value > 5, + deltaTime, + ); + this.airborneFor10s.write(computedAirspeed.value > 90 && radioAlt.value > 25, deltaTime); + + if (this.isAirVsGroundMode) { + if (radioAlt.value < 25) { + this.isAirVsGroundMode = false; + SimVar.SetSimVarValue('L:A32NX_GPWS_GROUND_STATE', 'Bool', 1); + } + } else { + if (this.airborneFor5s.read() || this.airborneFor5s.read()) { + this.isAirVsGroundMode = true; + SimVar.SetSimVarValue('L:A32NX_GPWS_GROUND_STATE', 'Bool', 0); + } + } + } + + updateApproachTakeoffState( + computedAirspeed, + radioAlt, + isGearDown, + areFlapsInLandingConfig, + tcfOperational, + tafOperational, + isOverflightDetected, + ) { + // TODO: what do we do if computedAirspeed is not NO? + if (!computedAirspeed.isNormalOperation()) { + return; + } + + if (this.isApproachVsTakeoffState) { + // Switch to TO if we pass below 245 ft mode 4b floor without an alert (i.e gear down and flaps in landing config) + if (radioAlt.isNormalOperation() && radioAlt.value < 245 && isGearDown && areFlapsInLandingConfig) { + this.isApproachVsTakeoffState = false; + SimVar.SetSimVarValue('L:A32NX_GPWS_APPROACH_STATE', 'Bool', 0); + } + } else { + const isFirstAlgorithmSatisfied = false; + // - 4C filter value exceeds 4A alert boundary + const isSecondAlgorithmSatisfied = + this.Mode4MaxRAAlt > + this.mode4aUpperBoundary( + computedAirspeed.value, + areFlapsInLandingConfig, + tcfOperational, + tafOperational, + isOverflightDetected, + ); + + if ((isFirstAlgorithmSatisfied || !this.isAudioDeclutterEnabled) && isSecondAlgorithmSatisfied) { + this.isApproachVsTakeoffState = true; + SimVar.SetSimVarValue('L:A32NX_GPWS_APPROACH_STATE', 'Bool', 1); + } + } + } + + mode4aUpperBoundary(computedAirspeed, areFlapsInLandingConfig, tcfOperational, tafOperational, isOverflightDetected) { + let expandedBoundary = 1000; + if (areFlapsInLandingConfig || tcfOperational || tafOperational) { + expandedBoundary = 500; + } else if (isOverflightDetected) { + expandedBoundary = 800; + } + + return Math.max(500, Math.min(expandedBoundary, 8.333 * computedAirspeed - 1083.33)); + } + + mode4bUpperBoundary(computedAirspeed, areFlapsInLandingConfig, tcfOperational, tafOperational, isOverflightDetected) { + let expandedBoundary = 1000; + if (areFlapsInLandingConfig || tcfOperational || tafOperational) { + expandedBoundary = 245; + } else if (isOverflightDetected) { + expandedBoundary = 800; + } + + return Math.max(245, Math.min(expandedBoundary, 8.333 * computedAirspeed - 1083.33)); + } + + mode4cUpperBoundary(computedAirspeed) { + return Math.max(500, Math.min(1000, 8.3333 * computedAirspeed.value - 1083.33)); + } + + updateOverflightState(deltaTime) { + // Need -2200 ft/s to trigger an overflight state + return this.isOverflightDetected.write(this.RadioAltRate < -2200 * 60, deltaTime); + } + + isTerrainClearanceFloorOperational(terrPbOff, radioAlt, fmcNavAccuracyHigh) { + // TODO when ground speed is below 60 kts, always consider fms nav accuracy high + // where does GS come from? + return this.isTerrainAwarenessEnabled && !terrPbOff && radioAlt.isNormalOperation() && fmcNavAccuracyHigh; + } + + isTerrainAwarenessOperational(terrPbOff) { + // TODO replace placeholders + const doesTerrainAwarenessUseGeometricAltitude = true; + const isGeometricAltitudeVfomValid = true; + const isGeometricAltitudeHilValid = true; + const geometricAltitudeVfom = 0; + const isRaimFailureDetected = false; + + return ( + this.isTerrainAwarenessEnabled && + !terrPbOff && + !doesTerrainAwarenessUseGeometricAltitude && + isGeometricAltitudeVfomValid && + isGeometricAltitudeHilValid && + !isRaimFailureDetected && + geometricAltitudeVfom < 200 + ); + } + + updateMode4aInhibited(isGearDownLocked, isFlapsInLandingConfig) { + if (this.isMode4aInhibited) { + if (!this.isAirVsGroundMode || !this.isApproachVsTakeoffState) { + // Reset + this.isMode4aInhibited = false; + } + } else if (this.isAlternateMode4bEnabled) { + if (isGearDownLocked || isFlapsInLandingConfig) { + this.isMode4aInhibited = true; + } + } + } + + selectAltitudeRate(inertialVs, baroVs) { + if (inertialVs.isNormalOperation()) { + return inertialVs.value; + } else if (Number.isFinite(this.RadioAltRate)) { + return this.RadioAltRate; + } else if (baroVs.isNormalOperation()) { + return baroVs.value; + } + + return NaN; + } +} + +const RetardStateMachine = { + overRetard: { + transitions: { + play: { + target: 'retardPlaying', + }, + }, + }, + retardPlaying: { + transitions: { + land: { + target: 'landed', + }, + go_around: { + target: 'overRetard', + }, + }, + }, + landed: { + transitions: { + up: { + target: 'overRetard', + }, + }, + }, +}; + +const AltCallStateMachine = { + init: 'ground', + over2500: { + transitions: { + down: { + target: 'over2000', + }, + }, + }, + over2000: { + transitions: { + down: { + target: 'over1000', + }, + up: { + target: 'over2500', + }, + }, + }, + over1000: { + transitions: { + down: { + target: 'over500', + }, + up: { + target: 'over2000', + }, + }, + }, + over500: { + transitions: { + down: { + target: 'over400', + }, + up: { + target: 'over1000', + }, + }, + }, + over400: { + transitions: { + down: { + target: 'over300', + }, + up: { + target: 'over500', + }, + }, + }, + over300: { + transitions: { + down: { + target: 'over200', + }, + up: { + target: 'over400', + }, + }, + }, + over200: { + transitions: { + down: { + target: 'over100', + }, + up: { + target: 'over300', + }, + }, + }, + over100: { + transitions: { + down: { + target: 'over50', + }, + up: { + target: 'over200', + }, + }, + }, + over50: { + transitions: { + down: { + target: 'over40', + }, + up: { + target: 'over100', + }, + }, + }, + over40: { + transitions: { + down: { + target: 'over30', + }, + up: { + target: 'over50', + }, + }, + }, + over30: { + transitions: { + down: { + target: 'over20', + }, + up: { + target: 'over40', + }, + }, + }, + over20: { + transitions: { + down: { + target: 'over10', + }, + up: { + target: 'over30', + }, + }, + }, + over10: { + transitions: { + down: { + target: 'over5', + }, + up: { + target: 'over20', + }, + }, + }, + over5: { + transitions: { + down: { + target: 'ground', + }, + up: { + target: 'over10', + }, + }, + }, + ground: { + transitions: { + up: { + target: 'over5', + }, + }, + }, +}; diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_LocalVarUpdater.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_LocalVarUpdater.ts new file mode 100644 index 00000000000..3e764edd476 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_LocalVarUpdater.ts @@ -0,0 +1,107 @@ +// Use this to create and sync local simvars that are derived from other simvars. +// To create and sync a new local simvar, you need to add a selector and an updater. +// The selector calculates the new value based on other simvars and some logic. +// The updater compares the new value from the selector with the current value from the local simvar, +// and then updates the local simvar if it changed. + +import { UpdateThrottler } from '@flybywiresim/fbw-sdk'; + +const FLAPS_IN_MOTION_MIN_DELTA = 0.1; + +// FIXME move to systems host +export class A32NX_LocalVarUpdater { + // Initial data for deltas + private lastFlapsPosition = SimVar.GetSimVarValue('L:A32NX_LEFT_FLAPS_POSITION_PERCENT', 'Percent'); + // track which compartment has gotten temperature initialization + private initializedCabinTemp = { + CKPT: false, + FWD: false, + AFT: false, + }; + + private updaters = [ + { + varName: 'L:A32NX_NO_SMOKING_MEMO', + type: 'Bool', + selector: this._noSmokingMemoSelector, + refreshInterval: 1000, + }, + { + varName: 'L:A32NX_FLAPS_IN_MOTION', + type: 'Bool', + selector: this._flapsInMotionSelector.bind(this), + refreshInterval: 50, + }, + { + varName: 'L:A32NX_SLIDES_ARMED', + type: 'Bool', + selector: this._areSlidesArmed.bind(this), + refreshInterval: 100, + }, + // New updaters go here... + ]; + + private readonly updaterThrottlers = new Map( + this.updaters.map((u) => [u.varName, new UpdateThrottler(u.refreshInterval)]), + ); + + constructor() {} + + update(deltaTime) { + this.updaters.forEach((updater) => this._runUpdater(deltaTime, updater)); + } + + _runUpdater(deltaTime, { varName, type, selector, identifier = null }) { + const selectorDeltaTime = this.updaterThrottlers.get(varName).canUpdate(deltaTime); + + if (selectorDeltaTime === -1) { + return; + } + + const newValue = selector(selectorDeltaTime, identifier); + const currentValue = SimVar.GetSimVarValue(varName, type); + + if (newValue !== currentValue) { + SimVar.SetSimVarValue(varName, type, newValue); + } + } + + _noSmokingMemoSelector() { + const gearPercent = SimVar.GetSimVarValue('L:A32NX_GEAR_CENTER_POSITION', 'Percent'); + const noSmokingSwitch = SimVar.GetSimVarValue('L:XMLVAR_SWITCH_OVHD_INTLT_NOSMOKING_Position', 'Position'); + + // Switch is ON + if (noSmokingSwitch === 0) { + return true; + } + + // Switch is AUTO and gear more than 50% down + if (noSmokingSwitch === 1 && gearPercent > 50) { + return true; + } + + return false; + } + + _flapsInMotionSelector() { + const currentFlapsPosition = SimVar.GetSimVarValue('L:A32NX_LEFT_FLAPS_POSITION_PERCENT', 'Percent'); + const lastFlapsPosition = this.lastFlapsPosition; + + this.lastFlapsPosition = currentFlapsPosition; + + return Math.abs(lastFlapsPosition - currentFlapsPosition) > FLAPS_IN_MOTION_MIN_DELTA; + } + + _areSlidesArmed() { + return ( + !SimVar.GetSimVarValue('SIM ON GROUND', 'bool') || + SimVar.GetSimVarValue('ON ANY RUNWAY', 'bool') || + (SimVar.GetSimVarValue('LIGHT BEACON ON', 'bool') && + SimVar.GetSimVarValue('INTERACTIVE POINT OPEN:0', 'percent') < 5 && // Pilot side front door for ramp/stairs + SimVar.GetSimVarValue('INTERACTIVE POINT OPEN:3', 'percent') < 5 && // Rear door, FO side for catering + SimVar.GetSimVarValue('L:A32NX_FWD_DOOR_CARGO_LOCKED', 'bool')) // Cargo door FO side + ); + } + + // New selectors go here... +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_PayloadManager.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_PayloadManager.ts new file mode 100644 index 00000000000..b81d5d5b25e --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_PayloadManager.ts @@ -0,0 +1,156 @@ +import { BitFlags, NXUnits } from '@flybywiresim/fbw-sdk'; + +// FIXME move to extras host +/* eslint-disable no-undef */ +export class A32NX_PayloadConstructor { + public readonly paxStations = { + rows1_6: { + name: 'ROWS [1-6]', + seats: 36, + weight: Math.round(NXUnits.kgToUser(3024)), + stationIndex: 0 + 1, + position: 20.5, + simVar: 'A32NX_PAX_A', + }, + rows7_13: { + name: 'ROWS [7-13]', + seats: 42, + weight: Math.round(NXUnits.kgToUser(3530)), + stationIndex: 1 + 1, + position: 1.5, + simVar: 'A32NX_PAX_B', + }, + rows14_21: { + name: 'ROWS [14-21]', + seats: 48, + weight: Math.round(NXUnits.kgToUser(4032)), + stationIndex: 2 + 1, + position: -16.6, + simVar: 'A32NX_PAX_C', + }, + rows22_29: { + name: 'ROWS [22-29]', + seats: 48, + weight: Math.round(NXUnits.kgToUser(4032)), + stationIndex: 3 + 1, + position: -35.6, + simVar: 'A32NX_PAX_D', + }, + }; + + public readonly cargoStations = { + fwdBag: { + name: 'FWD BAGGAGE/CONTAINER', + weight: Math.round(NXUnits.kgToUser(3402)), + load: 0, + stationIndex: 4 + 1, + position: 17.3, + visible: true, + simVar: 'A32NX_CARGO_FWD_BAGGAGE_CONTAINER', + }, + aftCont: { + name: 'AFT CONTAINER', + weight: Math.round(NXUnits.kgToUser(2426)), + load: 0, + stationIndex: 5 + 1, + position: -24.1, + visible: true, + simVar: 'A32NX_CARGO_AFT_CONTAINER', + }, + aftBag: { + name: 'AFT BAGGAGE', + weight: Math.round(NXUnits.kgToUser(2110)), + load: 0, + stationIndex: 6 + 1, + position: -34.1, + visible: true, + simVar: 'A32NX_CARGO_AFT_BAGGAGE', + }, + aftBulk: { + name: 'AFT BULK/LOOSE', + weight: Math.round(NXUnits.kgToUser(1497)), + load: 0, + stationIndex: 7 + 1, + position: -42.4, + visible: true, + simVar: 'A32NX_CARGO_AFT_BULK_LOOSE', + }, + }; + + constructor() {} +} + +const payloadConstruct = new A32NX_PayloadConstructor(); +const paxStations = payloadConstruct.paxStations; +const cargoStations = payloadConstruct.cargoStations; +const MAX_SEAT_AVAILABLE = 174; + +/** + * Calculate %MAC ZWFCG of all stations + */ +export function getZfwcg() { + const leMacZ = -5.383; // Accurate to 3 decimals, replaces debug weight values + const macSize = 13.464; // Accurate to 3 decimals, replaces debug weight values + + const emptyWeight = SimVar.GetSimVarValue('EMPTY WEIGHT', getUserUnit()); + const emptyPosition = -9.42; // Value from flight_model.cfg + const emptyMoment = emptyPosition * emptyWeight; + const PAX_WEIGHT = SimVar.GetSimVarValue('L:A32NX_WB_PER_PAX_WEIGHT', 'Number'); + + const paxTotalMass = Object.values(paxStations) + .map((station) => new BitFlags(SimVar.GetSimVarValue(`L:${station.simVar}`, 'Number')).getTotalBits() * PAX_WEIGHT) + .reduce((acc, cur) => acc + cur, 0); + const paxTotalMoment = Object.values(paxStations) + .map( + (station) => + new BitFlags(SimVar.GetSimVarValue(`L:${station.simVar}`, 'Number')).getTotalBits() * + PAX_WEIGHT * + station.position, + ) + .reduce((acc, cur) => acc + cur, 0); + + const cargoTotalMass = Object.values(cargoStations) + .map((station) => SimVar.GetSimVarValue(`PAYLOAD STATION WEIGHT:${station.stationIndex}`, getUserUnit())) + .reduce((acc, cur) => acc + cur, 0); + const cargoTotalMoment = Object.values(cargoStations) + .map( + (station) => + SimVar.GetSimVarValue(`PAYLOAD STATION WEIGHT:${station.stationIndex}`, getUserUnit()) * station.position, + ) + .reduce((acc, cur) => acc + cur, 0); + + const totalMass = emptyWeight + paxTotalMass + cargoTotalMass; + const totalMoment = emptyMoment + paxTotalMoment + cargoTotalMoment; + + const cgPosition = totalMoment / totalMass; + const cgPositionToLemac = cgPosition - leMacZ; + const cgPercentMac = -100 * (cgPositionToLemac / macSize); + + return cgPercentMac; +} + +export function getTotalCargo() { + const cargoTotalMass = Object.values(cargoStations) + .filter((station) => station.visible) + .map((station) => SimVar.GetSimVarValue(`PAYLOAD STATION WEIGHT:${station.stationIndex}`, getUserUnit())) + .reduce((acc, cur) => acc + cur, 0); + return cargoTotalMass; +} + +export function getTotalPayload() { + const paxTotalMass = Object.values(paxStations) + .map((station) => SimVar.GetSimVarValue(`PAYLOAD STATION WEIGHT:${station.stationIndex}`, getUserUnit())) + .reduce((acc, cur) => acc + cur, 0); + const cargoTotalMass = getTotalCargo(); + return paxTotalMass + cargoTotalMass; +} + +export function getZfw() { + const emptyWeight = SimVar.GetSimVarValue('EMPTY WEIGHT', getUserUnit()); + return emptyWeight + getTotalPayload(); +} + +export function getUserUnit() { + const defaultUnit = NXUnits.userWeightUnit() == 'KG' ? 'Kilograms' : 'Pounds'; + return defaultUnit; +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_Refuel.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_Refuel.ts new file mode 100644 index 00000000000..2508136e2ba --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_Refuel.ts @@ -0,0 +1,190 @@ +// Copyright (c) 2021-2024 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { NXDataStore, NXUnits } from '@flybywiresim/fbw-sdk'; + +const WING_FUELRATE_GAL_SEC = 4.01; +const CENTER_MODIFIER = 0.4528; + +// FIXME move to systems host +export class A32NX_Refuel { + constructor() {} + + init() { + const totalFuelGallons = 6267; + const fuelWeight = SimVar.GetSimVarValue('FUEL WEIGHT PER GALLON', 'kilograms'); + const centerCurrentSimVar = SimVar.GetSimVarValue('FUEL TANK CENTER QUANTITY', 'Gallons'); + const LInnCurrentSimVar = SimVar.GetSimVarValue('FUEL TANK LEFT MAIN QUANTITY', 'Gallons'); + const LOutCurrentSimVar = SimVar.GetSimVarValue('FUEL TANK LEFT AUX QUANTITY', 'Gallons'); + const RInnCurrentSimVar = SimVar.GetSimVarValue('FUEL TANK RIGHT MAIN QUANTITY', 'Gallons'); + const ROutCurrentSimVar = SimVar.GetSimVarValue('FUEL TANK RIGHT AUX QUANTITY', 'Gallons'); + const total = Math.round( + Math.max(LInnCurrentSimVar + LOutCurrentSimVar + RInnCurrentSimVar + ROutCurrentSimVar + centerCurrentSimVar, 0), + ); + const totalConverted = Math.round(NXUnits.kgToUser(total * fuelWeight)); + SimVar.SetSimVarValue('L:A32NX_REFUEL_STARTED_BY_USR', 'Bool', false); + SimVar.SetSimVarValue('L:A32NX_FUEL_TOTAL_DESIRED', 'Number', total); + SimVar.SetSimVarValue('L:A32NX_FUEL_DESIRED', 'Number', totalConverted); // TODO this looks sus... should not be user units in simvars + SimVar.SetSimVarValue('L:A32NX_FUEL_DESIRED_PERCENT', 'Number', Math.round((total / totalFuelGallons) * 100)); + SimVar.SetSimVarValue('L:A32NX_FUEL_CENTER_DESIRED', 'Number', centerCurrentSimVar); + SimVar.SetSimVarValue('L:A32NX_FUEL_LEFT_MAIN_DESIRED', 'Number', LInnCurrentSimVar); + SimVar.SetSimVarValue('L:A32NX_FUEL_LEFT_AUX_DESIRED', 'Number', LOutCurrentSimVar); + SimVar.SetSimVarValue('L:A32NX_FUEL_RIGHT_MAIN_DESIRED', 'Number', RInnCurrentSimVar); + SimVar.SetSimVarValue('L:A32NX_FUEL_RIGHT_AUX_DESIRED', 'Number', ROutCurrentSimVar); + } + + defuelTank(multiplier) { + return -WING_FUELRATE_GAL_SEC * multiplier; + } + refuelTank(multiplier) { + return WING_FUELRATE_GAL_SEC * multiplier; + } + + update(_deltaTime) { + const refuelStartedByUser = SimVar.GetSimVarValue('L:A32NX_REFUEL_STARTED_BY_USR', 'Bool'); + const gsxFuelHose = SimVar.GetSimVarValue('L:FSDT_GSX_FUELHOSE_CONNECTED', 'Number'); + if (!refuelStartedByUser && gsxFuelHose == 0) { + return; + } + if (!refuelStartedByUser && gsxFuelHose == 1) { + SimVar.SetSimVarValue('L:A32NX_REFUEL_STARTED_BY_USR', 'Bool', true); + return; + } + const busDC2 = SimVar.GetSimVarValue('L:A32NX_ELEC_DC_2_BUS_IS_POWERED', 'Bool'); + const busDCHot1 = SimVar.GetSimVarValue('L:A32NX_ELEC_DC_HOT_1_BUS_IS_POWERED', 'Bool'); + const gs = SimVar.GetSimVarValue('GPS GROUND SPEED', 'knots'); + const onGround = SimVar.GetSimVarValue('SIM ON GROUND', 'Bool'); + const eng1Running = SimVar.GetSimVarValue('ENG COMBUSTION:1', 'Bool'); + const eng2Running = SimVar.GetSimVarValue('ENG COMBUSTION:2', 'Bool'); + const refuelRate = NXDataStore.get('REFUEL_RATE_SETTING', '0'); // default = real + if (refuelRate !== '2') { + if (!onGround || eng1Running || eng2Running || gs > 0.1 || (!busDC2 && !busDCHot1)) { + return; + } + } + const centerTargetSimVar = SimVar.GetSimVarValue('L:A32NX_FUEL_CENTER_DESIRED', 'Number'); + const LInnTargetSimVar = SimVar.GetSimVarValue('L:A32NX_FUEL_LEFT_MAIN_DESIRED', 'Number'); + const LOutTargetSimVar = SimVar.GetSimVarValue('L:A32NX_FUEL_LEFT_AUX_DESIRED', 'Number'); + const RInnTargetSimVar = SimVar.GetSimVarValue('L:A32NX_FUEL_RIGHT_MAIN_DESIRED', 'Number'); + const ROutTargetSimVar = SimVar.GetSimVarValue('L:A32NX_FUEL_RIGHT_AUX_DESIRED', 'Number'); + const centerCurrentSimVar = SimVar.GetSimVarValue('FUEL TANK CENTER QUANTITY', 'Gallons'); + const LInnCurrentSimVar = SimVar.GetSimVarValue('FUEL TANK LEFT MAIN QUANTITY', 'Gallons'); + const LOutCurrentSimVar = SimVar.GetSimVarValue('FUEL TANK LEFT AUX QUANTITY', 'Gallons'); + const RInnCurrentSimVar = SimVar.GetSimVarValue('FUEL TANK RIGHT MAIN QUANTITY', 'Gallons'); + const ROutCurrentSimVar = SimVar.GetSimVarValue('FUEL TANK RIGHT AUX QUANTITY', 'Gallons'); + let centerCurrent = centerCurrentSimVar; + let LInnCurrent = LInnCurrentSimVar; + let LOutCurrent = LOutCurrentSimVar; + let RInnCurrent = RInnCurrentSimVar; + let ROutCurrent = ROutCurrentSimVar; + const centerTarget = centerTargetSimVar; + const LInnTarget = LInnTargetSimVar; + const LOutTarget = LOutTargetSimVar; + const RInnTarget = RInnTargetSimVar; + const ROutTarget = ROutTargetSimVar; + if (refuelRate == '2') { + // instant + SimVar.SetSimVarValue('FUEL TANK CENTER QUANTITY', 'Gallons', centerTarget); + SimVar.SetSimVarValue('FUEL TANK LEFT MAIN QUANTITY', 'Gallons', LInnTarget); + SimVar.SetSimVarValue('FUEL TANK LEFT AUX QUANTITY', 'Gallons', LOutTarget); + SimVar.SetSimVarValue('FUEL TANK RIGHT MAIN QUANTITY', 'Gallons', RInnTarget); + SimVar.SetSimVarValue('FUEL TANK RIGHT AUX QUANTITY', 'Gallons', ROutTarget); + } else { + let multiplier = 1; + if (refuelRate == '1') { + // fast + multiplier = 5; + } + multiplier *= _deltaTime / 1000; + //DEFUELING (center tank first, then main, then aux) + if (centerCurrent > centerTarget) { + centerCurrent += this.defuelTank(multiplier) * CENTER_MODIFIER; + if (centerCurrent < centerTarget) { + centerCurrent = centerTarget; + } + SimVar.SetSimVarValue('FUEL TANK CENTER QUANTITY', 'Gallons', centerCurrent); + } + if (LInnCurrent > LInnTarget || RInnCurrent > RInnTarget) { + LInnCurrent += this.defuelTank(multiplier) / 2; + RInnCurrent += this.defuelTank(multiplier) / 2; + if (LInnCurrent < LInnTarget) { + LInnCurrent = LInnTarget; + } + if (RInnCurrent < RInnTarget) { + RInnCurrent = RInnTarget; + } + SimVar.SetSimVarValue('FUEL TANK RIGHT MAIN QUANTITY', 'Gallons', RInnCurrent); + SimVar.SetSimVarValue('FUEL TANK LEFT MAIN QUANTITY', 'Gallons', LInnCurrent); + if (LInnCurrent != LInnTarget || RInnCurrent != RInnTarget) { + return; + } + } + if (LOutCurrent > LOutTarget || ROutCurrent > ROutTarget) { + LOutCurrent += this.defuelTank(multiplier) / 2; + ROutCurrent += this.defuelTank(multiplier) / 2; + if (LOutCurrent < LOutTarget) { + LOutCurrent = LOutTarget; + } + if (ROutCurrent < ROutTarget) { + ROutCurrent = ROutTarget; + } + SimVar.SetSimVarValue('FUEL TANK RIGHT AUX QUANTITY', 'Gallons', ROutCurrent); + SimVar.SetSimVarValue('FUEL TANK LEFT AUX QUANTITY', 'Gallons', LOutCurrent); + if (LOutCurrent != LOutTarget || ROutCurrent != ROutTarget) { + return; + } + } + // REFUELING (aux first, then main, then center tank) + if (centerCurrent < centerTarget) { + centerCurrent += this.refuelTank(multiplier) * CENTER_MODIFIER; + if (centerCurrent > centerTarget) { + centerCurrent = centerTarget; + } + SimVar.SetSimVarValue('FUEL TANK CENTER QUANTITY', 'Gallons', centerCurrent); + } + if (LOutCurrent < LOutTarget || ROutCurrent < ROutTarget) { + LOutCurrent += this.refuelTank(multiplier) / 2; + ROutCurrent += this.refuelTank(multiplier) / 2; + if (LOutCurrent > LOutTarget) { + LOutCurrent = LOutTarget; + } + if (ROutCurrent > ROutTarget) { + ROutCurrent = ROutTarget; + } + SimVar.SetSimVarValue('FUEL TANK RIGHT AUX QUANTITY', 'Gallons', ROutCurrent); + SimVar.SetSimVarValue('FUEL TANK LEFT AUX QUANTITY', 'Gallons', LOutCurrent); + if (LOutCurrent != LOutTarget || ROutCurrent != ROutTarget) { + return; + } + } + if (LInnCurrent < LInnTarget || RInnCurrent < RInnTarget) { + LInnCurrent += this.refuelTank(multiplier) / 2; + RInnCurrent += this.refuelTank(multiplier) / 2; + if (LInnCurrent > LInnTarget) { + LInnCurrent = LInnTarget; + } + if (RInnCurrent > RInnTarget) { + RInnCurrent = RInnTarget; + } + SimVar.SetSimVarValue('FUEL TANK RIGHT MAIN QUANTITY', 'Gallons', RInnCurrent); + SimVar.SetSimVarValue('FUEL TANK LEFT MAIN QUANTITY', 'Gallons', LInnCurrent); + if (LInnCurrent != LInnTarget || RInnCurrent != RInnTarget) { + return; + } + } + } + + if ( + centerCurrent == centerTarget && + LInnCurrent == LInnTarget && + LOutCurrent == LOutTarget && + RInnCurrent == RInnTarget && + ROutCurrent == ROutTarget + ) { + // DONE FUELING + SimVar.SetSimVarValue('L:A32NX_REFUEL_STARTED_BY_USR', 'Bool', false); + SimVar.SetSimVarValue('L:FSDT_GSX_FUELHOSE_CONNECTED', 'Number', 0); + } + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_SoundManager.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_SoundManager.ts new file mode 100644 index 00000000000..29dfbd603c8 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_SoundManager.ts @@ -0,0 +1,197 @@ +export interface SoundDefinition { + name: string; + length: number; +} + +class PeriodicSound { + public timeSinceLastPlayed = NaN; + + constructor( + public sound: SoundDefinition, + public period: number, + ) {} +} + +export class A32NX_SoundManager { + private periodicList: PeriodicSound[] = []; + private soundQueue: SoundDefinition[] = []; + + private playingSound = null; + private playingSoundRemaining = NaN; + + addPeriodicSound(sound, period = NaN) { + if (!sound) { + return; + } + + let useLengthForPeriod = false; + if (period < sound.length) { + console.error( + "A32NXSoundManager ERROR: Sound period can't be smaller than sound length. Using sound length instead.", + ); + useLengthForPeriod = true; + } + + let found = false; + this.periodicList.forEach((element) => { + if (element.sound.name === sound.name) { + found = true; + } + }); + + if (!found) { + this.periodicList.push(new PeriodicSound(sound, useLengthForPeriod ? sound.length : period)); + } + } + + removePeriodicSound(sound) { + if (!sound) { + return; + } + + for (let i = 0; i < this.periodicList.length; i++) { + if (this.periodicList[i].sound.name === sound.name) { + this.periodicList.splice(i, 1); + } + } + } + + tryPlaySound(sound, retry = false, repeatOnce = false) { + if (this.playingSound === null) { + this.playingSound = sound; + this.playingSoundRemaining = sound.length; + + Coherent.call('PLAY_INSTRUMENT_SOUND', sound.name).catch(console.error); + if (repeatOnce) { + this.soundQueue.push(sound); + } + return true; + } else if (retry) { + this.soundQueue.push(sound); + if (repeatOnce) { + this.soundQueue.push(sound); + } + return false; + } + return false; + } + + update(deltaTime: number) { + if (this.playingSoundRemaining <= 0) { + this.playingSound = null; + this.playingSoundRemaining = NaN; + } else if (this.playingSoundRemaining > 0) { + this.playingSoundRemaining -= deltaTime / 1000; + } + + this.periodicList.forEach((element) => { + if (isNaN(element.timeSinceLastPlayed) || element.timeSinceLastPlayed >= element.period) { + if (this.tryPlaySound(element.sound)) { + element.timeSinceLastPlayed = 0; + } + } else { + element.timeSinceLastPlayed += deltaTime / 1000; + } + }); + } +} + +// many lengths are approximate until we can get them accuratly (when boris re-makes them and we have the sources) +export const soundList: Record = { + pull_up: { + name: 'aural_pullup_new', + length: 0.9, + }, + sink_rate: { + name: 'aural_sink_rate_new', + length: 0.9, + }, + dont_sink: { + name: 'aural_dontsink_new', + length: 0.9, + }, + too_low_gear: { + name: 'aural_too_low_gear', + length: 0.8, + }, + too_low_flaps: { + name: 'aural_too_low_flaps', + length: 0.8, + }, + too_low_terrain: { + name: 'aural_too_low_terrain', + length: 0.9, + }, + minimums: { + name: 'aural_minimumnew', + length: 0.67, + }, + hundred_above: { + name: 'aural_100above', + length: 0.72, + }, + retard: { + name: 'new_retard', + length: 0.9, + }, + alt_2500: { + name: 'new_2500', + length: 1.1, + }, + alt_2500b: { + name: 'new_2_500', + length: 1.047, + }, + alt_2000: { + name: 'new_2000', + length: 0.72, + }, + alt_1000: { + name: 'new_1000', + length: 0.9, + }, + alt_500: { + name: 'new_500', + length: 0.6, + }, + alt_400: { + name: 'new_400', + length: 0.6, + }, + alt_300: { + name: 'new_300', + length: 0.6, + }, + alt_200: { + name: 'new_200', + length: 0.6, + }, + alt_100: { + name: 'new_100', + length: 0.6, + }, + alt_50: { + name: 'new_50', + length: 0.4, + }, + alt_40: { + name: 'new_40', + length: 0.4, + }, + alt_30: { + name: 'new_30', + length: 0.4, + }, + alt_20: { + name: 'new_20', + length: 0.4, + }, + alt_10: { + name: 'new_10', + length: 0.3, + }, + alt_5: { + name: 'new_5', + length: 0.3, + }, +}; diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_Speeds.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_Speeds.ts new file mode 100644 index 00000000000..fff166d9ad6 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_Speeds.ts @@ -0,0 +1,89 @@ +import { NXSpeeds } from '../NXSpeeds'; +import { FmgcFlightPhase } from '@shared/flightphase'; + +/** + * Calculates and shares Vs, Vls, F, S and GD. + */ +export class A32NX_Speeds { + private lastGw = 50; + private lastFhi = -1; + private curFhi = -1; + private ldgPos = -1; + private alt = -1; + private cgw = 0; + private isTo = false; + + constructor() { + console.log('A32NX_VSPEEDS constructed'); + } + + init() { + console.log('A32NX_VSPEEDS init'); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_VS', 'number', 0); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_VLS', 'number', 0); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_F', 'number', 0); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_S', 'number', 0); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_GD', 'number', 0); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_LANDING_CONF3', 'boolean', 0); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_VMAX', 'number', 0); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_VFEN', 'number', 0); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_ALPHA_PROTECTION_CALC', 'number', 0); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_ALPHA_MAX_CALC', 'number', 0); + } + + update() { + const fp = SimVar.GetSimVarValue('L:A32NX_FMGC_FLIGHT_PHASE', 'Enum'); + let fhi = SimVar.GetSimVarValue('L:A32NX_FLAPS_HANDLE_INDEX', 'Enum'); + /** Using true fhi for comparison */ + const isTo = fhi === SimVar.GetSimVarValue('L:A32NX_TO_CONFIG_FLAPS', 'number'); + /** Change fhi to differentiate between 1 and 1 + F */ + if (fhi === 1 && SimVar.GetSimVarValue('L:A32NX_FLAPS_CONF_INDEX', 'Enum') === 1) { + fhi = 5; + } + const fmGW = parseFloat(SimVar.GetSimVarValue('L:A32NX_FM_GROSS_WEIGHT', 'Number').toFixed(1)); + const gw = fmGW === 0 ? 64.3 : fmGW; // MZFW = 64300KG + const ldg = Math.round(SimVar.GetSimVarValue('GEAR POSITION:0', 'Enum')); + const alt = this.round(Simplane.getAltitude()); + + if (fhi === this.lastFhi && gw === this.lastGw && ldg === this.ldgPos && alt === this.alt && isTo === this.isTo) { + return; + } + + /** During Take Off allow to change this.isTo + * Otherwise if we are in take off config and change the fhi, we no longer are in take off config */ + if (fp === FmgcFlightPhase.Takeoff && Simplane.getAltitudeAboveGround() < 1.5) { + this.isTo = isTo; + } else if (this.isTo && this.lastFhi !== fhi) { + this.isTo = false; + } + + this.lastFhi = fhi; + this.lastGw = gw; + this.cgw = Math.ceil(((gw > 80 ? 80 : gw) - 40) / 5); + this.ldgPos = ldg; + this.alt = alt; + + const speeds = new NXSpeeds(gw, this.lastFhi, ldg, this.isTo); + speeds.compensateForMachEffect(alt); + + SimVar.SetSimVarValue('L:A32NX_SPEEDS_VS', 'number', speeds.vs); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_VLS', 'number', speeds.vls); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_F', 'number', speeds.f); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_S', 'number', speeds.s); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_GD', 'number', speeds.gd); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_VMAX', 'number', speeds.vmax); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_VFEN', 'number', speeds.vfeN); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_ALPHA_PROTECTION_CALC', 'number', speeds.vs * 1.1); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_ALPHA_MAX_CALC', 'number', speeds.vs * 1.03); + } + + /** + * Math.round(x / r) * r + * @param x number to be rounded + * @param r precision + * @returns rounded number + */ + round(x: number, r = 100): number { + return Math.round(x / r) * r; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_TipsManager.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_TipsManager.ts new file mode 100644 index 00000000000..1bcb496d2b1 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/A32NX_TipsManager.ts @@ -0,0 +1,103 @@ +import { UpdateThrottler } from '@flybywiresim/fbw-sdk'; +import { NXNotifManager } from '@shared/NxNotif'; + +// FIXME move to extras host +export class A32NX_TipsManager { + private readonly notif = new NXNotifManager(); + private readonly updateThrottler = new UpdateThrottler(15000); + private wasAnyAssistanceActive = false; + + private constructor() { + this.checkThrottleCalibration(); + } + + static get instance() { + if (!__tipsManager) { + __tipsManager = new A32NX_TipsManager(); + } + return __tipsManager; + } + + update(deltaTime) { + if (this.updateThrottler.canUpdate(deltaTime) !== -1) { + this.checkAssistenceConfiguration(); + } + } + + checkThrottleCalibration() { + let once = false; + let input = Math.round(SimVar.GetSimVarValue('L:A32NX_THROTTLE_MAPPING_INPUT:1', 'Number') * 100) / 100; + + const throttleConfig = SimVar.GetSimVarValue('L:A32NX_THROTTLE_MAPPING_LOADED_CONFIG:1', 'Boolean'); + + if (!throttleConfig) { + const checkThrottle = setInterval(() => { + if (SimVar.GetSimVarValue('L:A32NX_THROTTLE_MAPPING_LOADED_CONFIG:1', 'Boolean')) { + clearInterval(checkThrottle); + return; + } + if (input === Math.round(SimVar.GetSimVarValue('L:A32NX_THROTTLE_MAPPING_INPUT:1', 'Number') * 100) / 100) { + const fwcFlightPhase = SimVar.GetSimVarValue('L:A32NX_FWC_FLIGHT_PHASE', 'Enum'); + const atPhase = SimVar.GetSimVarValue('L:A32NX_AUTOTHRUST_MODE_MESSAGE', 'Enum'); + const clbLow = + Math.round(SimVar.GetSimVarValue('L:A32NX_THROTTLE_MAPPING_CLIMB_LOW:1', 'Number') * 100) / 100; + const clbHigh = + Math.round(SimVar.GetSimVarValue('L:A32NX_THROTTLE_MAPPING_CLIMB_HIGH:1', 'Number') * 100) / 100; + + // If thrust lever is within 0.03 range of the default CLB detent limits in CRZ and A/THR MODE is LVR CLB + if (input >= clbLow - 0.03 && input <= clbHigh + 0.03 && fwcFlightPhase === 6 && atPhase === 3) { + !once + ? (once = true) + : this.notif.showNotification({ + message: + 'Please calibrate your throttles in the flyPad tablet (EFB). Potentially inaccurate throttle calibration has been detected.', + timeout: 60000, + }); + } else { + once = false; + } + } else { + input = Math.round(SimVar.GetSimVarValue('L:A32NX_THROTTLE_MAPPING_INPUT:1', 'Number') * 100) / 100; + } + }, 300000); + } + } + + checkAssistenceConfiguration() { + // only check when actually flying, otherwise return + if (SimVar.GetSimVarValue('L:A32NX_IS_READY', 'Number') !== 1) { + this.wasAnyAssistanceActive = false; + return; + } + + // determine if any assistance is active + const assistanceAiControls = SimVar.GetSimVarValue('AI CONTROLS', 'Bool'); + const assistanceTakeOffEnabled = SimVar.GetSimVarValue('ASSISTANCE TAKEOFF ENABLED', 'Bool'); + const assistanceLandingEnabled = SimVar.GetSimVarValue('ASSISTANCE LANDING ENABLED', 'Bool'); + const assistanceAutotrimActive = SimVar.GetSimVarValue('AI AUTOTRIM ACTIVE', 'Bool'); + const isAnyAssistanceActive = + assistanceAiControls || assistanceTakeOffEnabled || assistanceLandingEnabled || assistanceAutotrimActive; + + // show popup when an enabled assistance is detected and it was not active before + if (!this.wasAnyAssistanceActive && isAnyAssistanceActive) { + this.notif.showNotification({ + message: + 'Ensure you have turned off all assistance functions:\n\n• AUTO-RUDDER\n• ASSISTED YOKE\n• ASSISTED LANDING\n• ASSISTED TAKEOFF\n• AI ANTI-STALL PROTECTION\n• AI AUTO-TRIM\n• ASSISTED CONTROLLER SENSITIVITY\n\nThey cause serious incompatibility!', + timeout: 15000, + }); + } + + // remember if any assistance was active + this.wasAnyAssistanceActive = isAnyAssistanceActive; + } + + showNavRadioTuningTip() { + this.notif.showNotification({ + message: + 'Navigation radio tuning is not possible while the FMGC controls the radios:\n\n• tune via the MCDU RADIO NAV page, or\n• press the NAV button on the RMP to enable manual tuning.', + timeout: 15000, + }); + } +} + +let __tipsManager; diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/Adirs.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/Adirs.ts new file mode 100644 index 00000000000..7f761f3f5f1 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_Core/Adirs.ts @@ -0,0 +1,81 @@ +import { AirDataSwitchingKnob, Arinc429Word, AttHdgSwitchingKnob } from '@flybywiresim/fbw-sdk'; + +export class ADIRS { + static getNdSupplier(displayIndex: 1 | 2, knobValue: AirDataSwitchingKnob | AttHdgSwitchingKnob) { + const adirs3ToCaptain = 0; + const adirs3ToFO = 2; + + if (this.isCaptainSide(displayIndex)) { + return knobValue === adirs3ToCaptain ? 3 : 1; + } + return knobValue === adirs3ToFO ? 3 : 2; + } + + static getNdInertialReferenceSource(displayIndex: 1 | 2) { + return ADIRS.getNdSupplier(displayIndex, SimVar.GetSimVarValue('L:A32NX_ATT_HDG_SWITCHING_KNOB', 'Enum')); + } + + static getNdAirDataReferenceSource(displayIndex: 1 | 2) { + return ADIRS.getNdSupplier(displayIndex, SimVar.GetSimVarValue('L:A32NX_AIR_DATA_SWITCHING_KNOB', 'Enum')); + } + + static isCaptainSide(displayIndex: 1 | 2) { + return displayIndex === 1; + } + + static mapNotAvailable(displayIndex: 1 | 2) { + const inertialReferenceSource = ADIRS.getNdInertialReferenceSource(displayIndex); + return ( + !Arinc429Word.fromSimVarValue(`L:A32NX_ADIRS_IR_${inertialReferenceSource}_LATITUDE`).isNormalOperation() || + !Arinc429Word.fromSimVarValue(`L:A32NX_ADIRS_IR_${inertialReferenceSource}_LONGITUDE`).isNormalOperation() + ); + } + + static getLatitude() { + return ADIRS.getFromAnyAdiru('IR', 'LATITUDE'); + } + + static getLongitude() { + return ADIRS.getFromAnyAdiru('IR', 'LONGITUDE'); + } + + static getTrueTrack() { + return ADIRS.getFromAnyAdiru('IR', 'TRUE_TRACK'); + } + + static getTrueAirspeed() { + return ADIRS.getFromAnyAdiru('ADR', 'TRUE_AIRSPEED'); + } + + static getCalibratedAirspeed() { + return ADIRS.getFromAnyAdiru('ADR', 'COMPUTED_AIRSPEED'); + } + + static getGroundSpeed() { + return ADIRS.getFromAnyAdiru('IR', 'GROUND_SPEED'); + } + + // FIXME there should be baro corrected altitude 1 (capt) and 2 (f/o) + static getBaroCorrectedAltitude() { + return ADIRS.getFromAnyAdiru('ADR', 'ALTITUDE'); + } + + /** + * + * @param type IR or ADR + * @param param the name of the param + */ + private static getFromAnyAdiru(type: 'IR' | 'ADR', param: string): Arinc429Word { + // In the real aircraft, FMGC 1 is supplied by ADIRU 1, and FMGC 2 by ADIRU 2. When any is unavailable + // the FMGC switches to ADIRU 3. If only one ADIRU is available, both FMGCs use the same ADIRU. + // As we don't have a split FMGC, we'll just use the following code for now. + const fromAdiru = 1; + const toAdiru = 3; + for (let adiruNumber = fromAdiru; adiruNumber <= toAdiru; adiruNumber++) { + const arincWord = Arinc429Word.fromSimVarValue(`L:A32NX_ADIRS_${type}_${adiruNumber}_${param}`); + if (arincWord.isNormalOperation() || adiruNumber === toAdiru) { + return arincWord; + } + } + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_FMCMainDisplay.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_FMCMainDisplay.ts new file mode 100644 index 00000000000..6e9077334fd --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_FMCMainDisplay.ts @@ -0,0 +1,5384 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { + a320EfisRangeSettings, + Arinc429OutputWord, + Arinc429SignStatusMatrix, + Arinc429Word, + DatabaseIdent, + DatabaseItem, + EfisSide, + EnrouteNdbNavaid, + Fix, + IlsNavaid, + MathUtils, + NdbNavaid, + NXDataStore, + NXUnits, + TerminalNdbNavaid, + UpdateThrottler, + VhfNavaid, + Waypoint, +} from '@flybywiresim/fbw-sdk'; +import { A32NX_Util } from '../../../../shared/src/A32NX_Util'; +import { EfisInterface } from '@fmgc/efis/EfisInterface'; +import { EfisSymbols } from '@fmgc/efis/EfisSymbols'; +import { A320AircraftConfig } from '@fmgc/flightplanning/A320AircraftConfig'; +import { DataManager } from '@fmgc/flightplanning/DataManager'; +import { FlightPlanRpcServer } from '@fmgc/flightplanning/rpc/FlightPlanRpcServer'; +import { Fmgc, GuidanceController } from '@fmgc/guidance/GuidanceController'; +import { A32NX_Core } from './A32NX_Core/A32NX_Core'; +import { A32NX_FuelPred, FuelPlanningPhases } from './A32NX_Core/A32NX_FuelPred'; +import { ADIRS } from './A32NX_Core/Adirs'; +import { A32NX_MessageQueue } from './A32NX_MessageQueue'; +import { NXSpeedsApp, NXSpeedsUtils } from './NXSpeeds'; +import { CDUIdentPage } from '../legacy_pages/A320_Neo_CDU_IdentPage'; +import { CDUInitPage } from '../legacy_pages/A320_Neo_CDU_InitPage'; +import { CDUNewWaypoint } from '../legacy_pages/A320_Neo_CDU_NewWaypoint'; +import { CDUPerformancePage } from '../legacy_pages/A320_Neo_CDU_PerformancePage'; +import { CDUProgressPage } from '../legacy_pages/A320_Neo_CDU_ProgressPage'; +import { A320_Neo_CDU_SelectWptPage } from '../legacy_pages/A320_Neo_CDU_SelectWptPage'; +import { McduMessage, NXFictionalMessages, NXSystemMessages, TypeIIMessage } from '../messages/NXSystemMessages'; +import { Navigation, SelectedNavaid } from '@fmgc/navigation/Navigation'; +import { FmgcFlightPhase } from '@shared/flightphase'; +import { CompanyRoute } from '@simbridge/index'; +import { Keypad } from './A320_Neo_CDU_Keypad'; +import { FmsClient } from '@atsu/fmsclient'; +import { AtsuStatusCodes } from '@datalink/common'; +import { A320_Neo_CDU_MainDisplay } from './A320_Neo_CDU_MainDisplay'; +import { FmsDisplayInterface } from '@fmgc/flightplanning/interface/FmsDisplayInterface'; +import { FmsErrorType } from '@fmgc/FmsError'; +import { FmsDataInterface } from '@fmgc/flightplanning/interface/FmsDataInterface'; +import { EventBus } from '@microsoft/msfs-sdk'; +import { AdfRadioTuningStatus, MmrRadioTuningStatus, VorRadioTuningStatus } from '@fmgc/navigation/NavaidTuner'; +import { Coordinates } from '@fmgc/flightplanning/data/geo'; +import { FmsFormatters } from './FmsFormatters'; +import { NavigationDatabase, NavigationDatabaseBackend } from '@fmgc/NavigationDatabase'; +import { FlightPhaseManager } from '@fmgc/flightphase'; +import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; +import { A320FlightPlanPerformanceData } from '@fmgc/flightplanning/plans/performance/FlightPlanPerformanceData'; +import { NavigationDatabaseService } from '@fmgc/flightplanning/NavigationDatabaseService'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; +import { initComponents, updateComponents } from '@fmgc/components'; +import { CoRouteUplinkAdapter } from '@fmgc/flightplanning/uplink/CoRouteUplinkAdapter'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +export abstract class FMCMainDisplay implements FmsDataInterface, FmsDisplayInterface, Fmgc { + private static DEBUG_INSTANCE: FMCMainDisplay; + + /** Naughty hack. We assume that we're always subclassed by A320_Neo_CDU_MainDisplay. */ + private readonly mcdu = this as unknown as A320_Neo_CDU_MainDisplay; + + public readonly navDatabaseBackend = NavigationDatabaseBackend.Msfs; + public readonly currFlightPhaseManager = new FlightPhaseManager(this.bus); + public readonly currFlightPlanService = new FlightPlanService(this.bus, new A320FlightPlanPerformanceData()); + public readonly rpcServer = new FlightPlanRpcServer(this.bus, this.currFlightPlanService); + public readonly currNavigationDatabaseService = NavigationDatabaseService; + public readonly navigationDatabase = new NavigationDatabase(NavigationDatabaseBackend.Msfs); + + private readonly flightPhaseUpdateThrottler = new UpdateThrottler(800); + private readonly fmsUpdateThrottler = new UpdateThrottler(250); + private readonly _progBrgDistUpdateThrottler = new UpdateThrottler(2000); + private readonly _apCooldown = 500; + private lastFlightPlanVersion = 0; + private readonly _messageQueue = new A32NX_MessageQueue(this.mcdu); + + public _deltaTime = 0; + + /** Declaration of every variable used (NOT initialization) */ + private maxCruiseFL = 390; + private recMaxCruiseFL = 398; + public coRoute = { routeNumber: undefined, routes: undefined }; + public perfTOTemp = NaN; + private _routeFinalFuelWeight = 0; + private _routeFinalFuelTime = 30; + private _routeFinalFuelTimeDefault = 30; + private _routeReservedWeight = 0; + private _routeReservedPercent = 5; + public takeOffWeight = NaN; + public landingWeight = NaN; + /** +ve for tailwind, -ve for headwind */ + public averageWind = 0; + public perfApprQNH = NaN; + public perfApprTemp = NaN; + public perfApprWindHeading = NaN; + public perfApprWindSpeed = NaN; + public unconfirmedV1Speed = undefined; + public unconfirmedVRSpeed = undefined; + public unconfirmedV2Speed = undefined; + public _toFlexChecked = true; + private toRunway = undefined; + public vApp = NaN; + public perfApprMDA: number | null = null; + public perfApprDH: 'NO DH' | number | null = null; + public perfApprFlaps3 = false; + private _debug = undefined; + public _zeroFuelWeightZFWCGEntered = false; + public _taxiEntered = false; + private _DistanceToAlt = undefined; + private _routeAltFuelWeight: number | null = 0; + private _routeAltFuelTime: number | null = 0; + private _routeTripFuelWeight = 0; + public _routeTripTime = 0; + private _defaultTaxiFuelWeight = 0.2; + public _rteRsvPercentOOR = false; + public _rteReservedWeightEntered = false; + public _rteReservedPctEntered = false; + private _rteFinalCoeffecient = undefined; + public _rteFinalWeightEntered = false; + public _rteFinalTimeEntered = false; + public _routeAltFuelEntered = false; + public _minDestFob = 0; + public _minDestFobEntered = false; + public _isBelowMinDestFob = false; + private _defaultRouteFinalTime = undefined; + public _fuelPredDone = false; + public _fuelPlanningPhase = undefined; + public _blockFuelEntered = false; + private _initMessageSettable = undefined; + public _checkWeightSettable = true; + private _gwInitDisplayed = undefined; + /* CPDLC Fields */ + private _destDataChecked = undefined; + private _towerHeadwind = undefined; + private _EfobBelowMinClr = undefined; + public simbrief = undefined; + public aocTimes = { + doors: 0, + off: 0, + out: 0, + on: 0, + in: 0, + }; + public winds = { + climb: [], + cruise: [], + des: [], + alternate: null, + }; + public computedVgd?: number; + public computedVfs?: number; + public computedVss?: number; + public computedVls?: number; + public approachSpeeds?: NXSpeedsApp; // based on selected config, not current config + public constraintAlt = 0; + private _forceNextAltitudeUpdate = undefined; + private _lastUpdateAPTime = undefined; + private updateAutopilotCooldown = undefined; + private _apMasterStatus = undefined; + private _lastRequestedFLCModeWaypointIndex = undefined; + + private _progBrgDist?: { + icao: string; + ident: string; + coordinates: Coordinates; + bearing: number; + distance: number; + }; + public preSelectedClbSpeed?: number; + public preSelectedCrzSpeed?: number; + public managedSpeedTarget = NaN; + public managedSpeedTargetIsMach = false; + public managedSpeedClimb = 290; + private managedSpeedClimbIsPilotEntered = false; + public managedSpeedClimbMach = 0.78; + // private managedSpeedClimbMachIsPilotEntered = undefined; + public managedSpeedCruise = 290; + public managedSpeedCruiseIsPilotEntered = false; + public managedSpeedCruiseMach = 0.78; + // private managedSpeedCruiseMachIsPilotEntered = undefined; + public managedSpeedDescend = 290; + public managedSpeedDescendPilot?: number; + public managedSpeedDescendMach = 0.78; + public managedSpeedDescendMachPilot?: number; + // private managedSpeedDescendMachIsPilotEntered = undefined; + private cruiseFlightLevelTimeOut?: ReturnType; + /** Takeoff config entered on PERF TO */ + public flaps?: 0 | 1 | 2 | 3 | null = undefined; + public ths?: number | null; + public cruiseTemperature?: number; + public taxiFuelWeight = 0.2; + public blockFuel?: number; + public zeroFuelWeight?: number; + public zeroFuelWeightMassCenter?: number; + private activeWpIdx = undefined; + private efisSymbols = undefined; + public groundTempAuto?: number = undefined; + public groundTempPilot?: number = undefined; + /** + * Landing elevation in feet MSL. + * This is the destination runway threshold elevation, or airport elevation if runway is not selected. + */ + private landingElevation = undefined; + /* + * Latitude part of the touch down coordinate. + * This is the destination runway coordinate, or airport coordinate if runway is not selected + */ + private destinationLatitude = undefined; + /* + * Latitude part of the touch down coordinate. + * This is the destination runway coordinate, or airport coordinate if runway is not selected + */ + private destinationLongitude = undefined; + /** Speed in KCAS when the first engine failed during takeoff */ + private takeoffEngineOutSpeed = undefined; + private checkSpeedModeMessageActive = undefined; + public perfClbPredToAltitudePilot = undefined; + public perfDesPredToAltitudePilot = undefined; + + // ATSU data + public atsu?: FmsClient; + public holdSpeedTarget = undefined; + public holdIndex = 0; + public holdDecelReached = false; + private setHoldSpeedMessageActive = undefined; + private managedProfile = new Map< + number, + { + descentSpeed: number; + previousDescentSpeed: number; + climbSpeed: number; + previousClimbSpeed: number; + climbAltitude: number; + descentAltitude: number; + } + >(); + private speedLimitExceeded = undefined; + private toSpeedsNotInserted = false; + private toSpeedsTooLow = false; + private vSpeedDisagree = false; + public isTrueRefMode = false; + + public onAirport = () => {}; + // FIXME always false! + public _activeCruiseFlightLevelDefaulToFcu = false; + + // arinc bus output words + private readonly arincDiscreteWord2 = new FmArinc429OutputWord('DISCRETE_WORD_2'); + private readonly arincDiscreteWord3 = new FmArinc429OutputWord('DISCRETE_WORD_3'); + private readonly arincTakeoffPitchTrim = new FmArinc429OutputWord('TO_PITCH_TRIM'); + private readonly arincLandingElevation = new FmArinc429OutputWord('LANDING_ELEVATION'); + private readonly arincDestinationLatitude = new FmArinc429OutputWord('DEST_LAT'); + private readonly arincDestinationLongitude = new FmArinc429OutputWord('DEST_LONG'); + private readonly arincMDA = new FmArinc429OutputWord('MINIMUM_DESCENT_ALTITUDE'); + private readonly arincDH = new FmArinc429OutputWord('DECISION_HEIGHT'); + private readonly arincThrustReductionAltitude = new FmArinc429OutputWord('THR_RED_ALT'); + private readonly arincAccelerationAltitude = new FmArinc429OutputWord('ACC_ALT'); + private readonly arincEoAccelerationAltitude = new FmArinc429OutputWord('EO_ACC_ALT'); + private readonly arincMissedThrustReductionAltitude = new FmArinc429OutputWord('MISSED_THR_RED_ALT'); + private readonly arincMissedAccelerationAltitude = new FmArinc429OutputWord('MISSED_ACC_ALT'); + private readonly arincMissedEoAccelerationAltitude = new FmArinc429OutputWord('MISSED_EO_ACC_ALT'); + private readonly arincTransitionAltitude = new FmArinc429OutputWord('TRANS_ALT'); + private readonly arincTransitionLevel = new FmArinc429OutputWord('TRANS_LVL'); + /** contains fm messages (not yet implemented) and nodh bit */ + private readonly arincEisWord2 = new FmArinc429OutputWord('EIS_DISCRETE_WORD_2'); + + /** These arinc words will be automatically written to the bus, and automatically set to 0/NCD when the FMS resets */ + private readonly arincBusOutputs = [ + this.arincDiscreteWord2, + this.arincDiscreteWord3, + this.arincTakeoffPitchTrim, + this.arincLandingElevation, + this.arincDestinationLatitude, + this.arincDestinationLongitude, + this.arincMDA, + this.arincDH, + this.arincThrustReductionAltitude, + this.arincAccelerationAltitude, + this.arincEoAccelerationAltitude, + this.arincMissedThrustReductionAltitude, + this.arincMissedAccelerationAltitude, + this.arincMissedEoAccelerationAltitude, + this.arincTransitionAltitude, + this.arincTransitionLevel, + this.arincEisWord2, + ]; + + private navDbIdent: DatabaseIdent | null = null; + + private A32NXCore?: A32NX_Core; + public dataManager?: DataManager; + public efisInterfaces?: Record; + public guidanceController?: GuidanceController; + public navigation?: Navigation; + + public tempCurve; + public casToMachManualCrossoverCurve; + public machToCasManualCrossoverCurve; + + constructor(public readonly bus: EventBus) { + FMCMainDisplay.DEBUG_INSTANCE = this; + + this.currFlightPlanService.createFlightPlans(); + this.currNavigationDatabaseService.activeDatabase = this.navigationDatabase; + } + + public get flightPhaseManager() { + return this.currFlightPhaseManager; + } + + get flightPlanService() { + return this.currFlightPlanService; + } + + public getFlightPlan(index: FlightPlanIndex) { + return index === FlightPlanIndex.Active + ? this.flightPlanService.activeOrTemporary + : this.flightPlanService.get(index); + } + + public getAlternateFlightPlan(index: FlightPlanIndex) { + return this.getFlightPlan(index).alternateFlightPlan; + } + + public get navigationDatabaseService() { + return this.currNavigationDatabaseService; + } + + protected Init() { + this.initVariables(); + + this.A32NXCore = new A32NX_Core(); + this.A32NXCore.init(); + + this.dataManager = new DataManager(this); + + this.efisInterfaces = { + L: new EfisInterface('L', this.currFlightPlanService), + R: new EfisInterface('R', this.currFlightPlanService), + }; + this.guidanceController = new GuidanceController( + this.bus, + this, + this.currFlightPlanService, + this.efisInterfaces, + a320EfisRangeSettings, + A320AircraftConfig, + ); + this.navigation = new Navigation(this.bus, this.currFlightPlanService); + this.efisSymbols = new EfisSymbols( + this.bus, + this.guidanceController, + this.currFlightPlanService, + this.navigation.getNavaidTuner(), + this.efisInterfaces, + a320EfisRangeSettings, + ); + + initComponents(this.navigation, this.guidanceController, this.flightPlanService); + + this.guidanceController.init(); + this.efisSymbols.init(); + this.navigation.init(); + + this.tempCurve = new Avionics.Curve(); + this.tempCurve.interpolationFunction = Avionics.CurveTool.NumberInterpolation; + this.tempCurve.add(-10 * 3.28084, 21.5); + this.tempCurve.add(0, 15.0); + this.tempCurve.add(10 * 3.28084, 8.5); + this.tempCurve.add(20 * 3.28084, 2.0); + this.tempCurve.add(30 * 3.28084, -4.49); + this.tempCurve.add(40 * 3.28084, -10.98); + this.tempCurve.add(50 * 3.28084, -17.47); + this.tempCurve.add(60 * 3.28084, -23.96); + this.tempCurve.add(70 * 3.28084, -30.45); + this.tempCurve.add(80 * 3.28084, -36.94); + this.tempCurve.add(90 * 3.28084, -43.42); + this.tempCurve.add(100 * 3.28084, -49.9); + this.tempCurve.add(150 * 3.28084, -56.5); + this.tempCurve.add(200 * 3.28084, -56.5); + this.tempCurve.add(250 * 3.28084, -51.6); + this.tempCurve.add(300 * 3.28084, -46.64); + this.tempCurve.add(400 * 3.28084, -22.8); + this.tempCurve.add(500 * 3.28084, -2.5); + this.tempCurve.add(600 * 3.28084, -26.13); + this.tempCurve.add(700 * 3.28084, -53.57); + this.tempCurve.add(800 * 3.28084, -74.51); + + // This is used to determine the Mach number corresponding to a CAS at the manual crossover altitude + // The curve was calculated numerically and approximated using a few interpolated values + this.casToMachManualCrossoverCurve = new Avionics.Curve(); + this.casToMachManualCrossoverCurve.interpolationFunction = Avionics.CurveTool.NumberInterpolation; + this.casToMachManualCrossoverCurve.add(0, 0); + this.casToMachManualCrossoverCurve.add(100, 0.27928); + this.casToMachManualCrossoverCurve.add(150, 0.41551); + this.casToMachManualCrossoverCurve.add(200, 0.54806); + this.casToMachManualCrossoverCurve.add(250, 0.67633); + this.casToMachManualCrossoverCurve.add(300, 0.8); + this.casToMachManualCrossoverCurve.add(350, 0.82); + + // This is used to determine the CAS corresponding to a Mach number at the manual crossover altitude + // Effectively, the manual crossover altitude is FL305 up to M.80, then decreases linearly to the crossover altitude of (VMO, MMO) + this.machToCasManualCrossoverCurve = new Avionics.Curve(); + this.machToCasManualCrossoverCurve.interpolationFunction = Avionics.CurveTool.NumberInterpolation; + this.machToCasManualCrossoverCurve.add(0, 0); + this.machToCasManualCrossoverCurve.add(0.27928, 100); + this.machToCasManualCrossoverCurve.add(0.41551, 150); + this.machToCasManualCrossoverCurve.add(0.54806, 200); + this.machToCasManualCrossoverCurve.add(0.67633, 250); + this.machToCasManualCrossoverCurve.add(0.8, 300); + this.machToCasManualCrossoverCurve.add(0.82, 350); + + this.updatePerfSpeeds(); + + this.flightPhaseManager.init(); + this.flightPhaseManager.addOnPhaseChanged(this.onFlightPhaseChanged.bind(this)); + + // Start the check routine for system health and status + setInterval(() => { + if (this.flightPhaseManager.phase === FmgcFlightPhase.Cruise && !this._destDataChecked) { + const distanceToDestination = this.getDistanceToDestination(); + if (Number.isFinite(distanceToDestination) && distanceToDestination !== -1 && distanceToDestination < 180) { + this._destDataChecked = true; + this.checkDestData(); + } + } + }, 15000); + + SimVar.SetSimVarValue('L:A32NX_FM_LS_COURSE', 'number', -1); + + this.navigationDatabaseService.activeDatabase.getDatabaseIdent().then((dbIdent) => (this.navDbIdent = dbIdent)); + } + + protected initVariables(resetTakeoffData = true) { + this.costIndex = undefined; + this.maxCruiseFL = 390; + this.recMaxCruiseFL = 398; + this.resetCoroute(); + this._routeFinalFuelWeight = 0; + this._routeFinalFuelTime = 30; + this._routeFinalFuelTimeDefault = 30; + this._routeReservedWeight = 0; + this._routeReservedPercent = 5; + this.takeOffWeight = NaN; + this.landingWeight = NaN; + // +ve for tailwind, -ve for headwind + this.averageWind = 0; + this.perfApprQNH = NaN; + this.perfApprTemp = NaN; + this.perfApprWindHeading = NaN; + this.perfApprWindSpeed = NaN; + this.unconfirmedV1Speed = undefined; + this.unconfirmedVRSpeed = undefined; + this.unconfirmedV2Speed = undefined; + this._toFlexChecked = true; + this.toRunway = ''; + this.vApp = NaN; + this.perfApprMDA = null; + this.perfApprDH = null; + this.perfApprFlaps3 = false; + this._debug = 0; + this._zeroFuelWeightZFWCGEntered = false; + this._taxiEntered = false; + this._DistanceToAlt = 0; + this._routeAltFuelWeight = 0; + this._routeAltFuelTime = 0; + this._routeTripFuelWeight = 0; + this._routeTripTime = 0; + this._defaultTaxiFuelWeight = 0.2; + this._rteRsvPercentOOR = false; + this._rteReservedWeightEntered = false; + this._rteReservedPctEntered = false; + this._rteFinalCoeffecient = 0; + this._rteFinalWeightEntered = false; + this._rteFinalTimeEntered = false; + this._routeAltFuelEntered = false; + this._minDestFob = 0; + this._minDestFobEntered = false; + this._isBelowMinDestFob = false; + this._defaultRouteFinalTime = 45; + this._fuelPredDone = false; + this._fuelPlanningPhase = FuelPlanningPhases.PLANNING; + this._blockFuelEntered = false; + this._initMessageSettable = false; + this._checkWeightSettable = true; + this._gwInitDisplayed = 0; + /* CPDLC Fields */ + this.tropo = undefined; + this._destDataChecked = false; + this._towerHeadwind = 0; + this._EfobBelowMinClr = false; + this.simbrief = { + route: '', + cruiseAltitude: '', + originIcao: '', + destinationIcao: '', + blockFuel: '', + paxCount: '', + cargo: undefined, + payload: undefined, + estZfw: '', + sendStatus: 'READY', + costIndex: '', + navlog: [], + callsign: '', + alternateIcao: '', + avgTropopause: '', + ete: '', + blockTime: '', + outTime: '', + onTime: '', + inTime: '', + offTime: '', + taxiFuel: '', + tripFuel: '', + }; + this.aocTimes.doors = 0; + this.aocTimes.off = 0; + this.aocTimes.out = 0; + this.aocTimes.on = 0; + this.aocTimes.in = 0; + this.winds.climb.length = 0; + this.winds.cruise.length = 0; + this.winds.des.length = 0; + this.winds.alternate = null; + this.computedVls = undefined; + this.approachSpeeds = undefined; // based on selected config, not current config + this._blockFuelEntered = false; + this.constraintAlt = 0; + this._forceNextAltitudeUpdate = false; + this._lastUpdateAPTime = NaN; + this.updateAutopilotCooldown = 0; + this._apMasterStatus = false; + this._lastRequestedFLCModeWaypointIndex = -1; + + this._activeCruiseFlightLevelDefaulToFcu = false; + this._progBrgDist = undefined; + this.preSelectedClbSpeed = undefined; + this.preSelectedCrzSpeed = undefined; + this.managedSpeedTarget = NaN; + this.managedSpeedTargetIsMach = false; + this.managedSpeedClimb = 290; + this.managedSpeedClimbIsPilotEntered = false; + this.managedSpeedClimbMach = 0.78; + // this.managedSpeedClimbMachIsPilotEntered = false; + this.managedSpeedCruise = 290; + this.managedSpeedCruiseIsPilotEntered = false; + this.managedSpeedCruiseMach = 0.78; + // this.managedSpeedCruiseMachIsPilotEntered = false; + this.managedSpeedDescend = 290; + this.managedSpeedDescendPilot = undefined; + this.managedSpeedDescendMach = 0.78; + this.managedSpeedDescendMachPilot = undefined; + // this.managedSpeedDescendMachIsPilotEntered = false; + this.cruiseFlightLevelTimeOut = undefined; + this.flightNumber = undefined; + // this.flightNumber = undefined; + this.cruiseTemperature = undefined; + this.taxiFuelWeight = 0.2; + this.blockFuel = undefined; + this.zeroFuelWeight = undefined; + this.zeroFuelWeightMassCenter = undefined; + this.holdSpeedTarget = undefined; + this.holdIndex = 0; + this.holdDecelReached = false; + this.setHoldSpeedMessageActive = false; + this.managedProfile.clear(); + this.speedLimitExceeded = false; + this.groundTempAuto = undefined; + this.groundTempPilot = undefined; + this.landingElevation = undefined; + this.destinationLatitude = undefined; + this.destinationLongitude = undefined; + this.toSpeedsNotInserted = false; + this.toSpeedsTooLow = false; + this.vSpeedDisagree = false; + this.takeoffEngineOutSpeed = undefined; + this.checkSpeedModeMessageActive = false; + this.perfClbPredToAltitudePilot = undefined; + this.perfDesPredToAltitudePilot = undefined; + + this.onAirport = () => {}; + + if (this.navigation) { + this.navigation.requiredPerformance.clearPilotRnp(); + } + + // FIXME WTF! Why create a whole new instance each time the FMS is cleared! + // ATSU data + this.atsu = new FmsClient(this, this.flightPlanService); + + // Reset SimVars + SimVar.SetSimVarValue('L:A32NX_SPEEDS_MANAGED_PFD', 'knots', 0); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_MANAGED_ATHR', 'knots', 0); + + SimVar.SetSimVarValue('L:A32NX_MachPreselVal', 'mach', -1); + SimVar.SetSimVarValue('L:A32NX_SpeedPreselVal', 'knots', -1); + + SimVar.SetSimVarValue('L:AIRLINER_DECISION_HEIGHT', 'feet', -1); + SimVar.SetSimVarValue('L:AIRLINER_MINIMUM_DESCENT_ALTITUDE', 'feet', 0); + + SimVar.SetSimVarValue('L:A32NX_FG_ALTITUDE_CONSTRAINT', 'feet', this.constraintAlt); + SimVar.SetSimVarValue('L:A32NX_TO_CONFIG_NORMAL', 'Bool', 0); + SimVar.SetSimVarValue('L:A32NX_CABIN_READY', 'Bool', 0); + SimVar.SetSimVarValue('L:A32NX_FM_GROSS_WEIGHT', 'Number', 0); + + if (SimVar.GetSimVarValue('L:A32NX_AUTOTHRUST_DISABLED', 'number') === 1) { + SimVar.SetSimVarValue('K:A32NX.ATHR_RESET_DISABLE', 'number', 1); + } + + SimVar.SetSimVarValue('L:A32NX_PFD_MSG_SET_HOLD_SPEED', 'bool', false); + + if (resetTakeoffData) { + // FMGC Message Queue + this._messageQueue.resetQueue(); + + this.computedVgd = undefined; + this.computedVfs = undefined; + this.computedVss = undefined; + this.perfTOTemp = NaN; + this.setTakeoffFlaps(null); + this.setTakeoffTrim(null); + this.unconfirmedV1Speed = undefined; + this.unconfirmedVRSpeed = undefined; + this.unconfirmedV2Speed = undefined; + this._toFlexChecked = true; + } + + this.arincBusOutputs.forEach((word) => { + word.setRawValue(0); + word.setSsm(Arinc429SignStatusMatrix.NoComputedData); + }); + + this.toSpeedsChecks(); + + this.setRequest('FMGC'); + } + + public onUpdate(_deltaTime) { + this._deltaTime = _deltaTime; + // this.flightPlanManager.update(_deltaTime); + const flightPlanChanged = this.flightPlanService.activeOrTemporary.version !== this.lastFlightPlanVersion; + if (flightPlanChanged) { + this.lastFlightPlanVersion = this.flightPlanService.activeOrTemporary.version; + this.setRequest('FMGC'); + } + + updateComponents(_deltaTime); + + this.isTrueRefMode = SimVar.GetSimVarValue('L:A32NX_FMGC_TRUE_REF', 'boolean'); + + if (this._debug++ > 180) { + this._debug = 0; + } + const flightPhaseManagerDelta = this.flightPhaseUpdateThrottler.canUpdate(_deltaTime); + if (flightPhaseManagerDelta !== -1) { + this.flightPhaseManager.shouldActivateNextPhase(flightPhaseManagerDelta); + } + + if (this.fmsUpdateThrottler.canUpdate(_deltaTime) !== -1) { + this.checkSpeedLimit(); + this.navigation.update(_deltaTime); + this.getGW(); + this.checkGWParams(); + this.toSpeedsChecks(); + this.thrustReductionAccelerationChecks(); + this.updateThrustReductionAcceleration(); + this.updateTransitionAltitudeLevel(); + this.updateMinimums(); + this.updateIlsCourse(); + this.updatePerfPageAltPredictions(); + this.checkEFOBBelowMin(); + } + + this.A32NXCore.update(); + + if (flightPlanChanged) { + this.updateManagedProfile(); + this.updateDestinationData(); + } + + this.updateAutopilot(); + + if (this._progBrgDistUpdateThrottler.canUpdate(_deltaTime) !== -1) { + this.updateProgDistance(); + } + + if (this.guidanceController) { + this.guidanceController.update(_deltaTime); + } + + if (this.efisSymbols) { + this.efisSymbols.update(_deltaTime); + } + this.arincBusOutputs.forEach((word) => word.writeToSimVarIfDirty()); + } + + protected onFmPowerStateChanged(newState) { + SimVar.SetSimVarValue('L:A32NX_FM1_HEALTHY_DISCRETE', 'boolean', newState); + SimVar.SetSimVarValue('L:A32NX_FM2_HEALTHY_DISCRETE', 'boolean', newState); + } + + public async switchNavDatabase() { + // Only performing a reset of the MCDU for now, no secondary database + // Speed AP returns to selected + //const isSelected = Simplane.getAutoPilotAirspeedSelected(); + //if (isSelected == false) + // SimVar.SetSimVarValue("H:A320_Neo_FCU_SPEED_PULL", "boolean", 1); + // flight plan + this.resetCoroute(); + this.atsu.resetAtisAutoUpdate(); + await this.flightPlanService.reset(); + // stored data + this.dataManager.deleteAllStoredWaypoints(); + // Reset MCDU apart from TakeOff config + this.initVariables(false); + + this.navigation.resetState(); + } + + /** + * This method is called by the FlightPhaseManager after a flight phase change + * This method initializes AP States, initiates CDUPerformancePage changes and other set other required states + * @param prevPhase Previous FmgcFlightPhase + * @param nextPhase New FmgcFlightPhase + */ + private onFlightPhaseChanged(prevPhase: FmgcFlightPhase, nextPhase: FmgcFlightPhase) { + this.updateConstraints(); + this.updateManagedSpeed(); + + this.setRequest('FMGC'); + + SimVar.SetSimVarValue('L:A32NX_CABIN_READY', 'Bool', 0); + + switch (nextPhase) { + case FmgcFlightPhase.Takeoff: { + this._destDataChecked = false; + + const plan = this.flightPlanService.active; + + if (plan.performanceData.accelerationAltitude === null) { + // it's important to set this immediately as we don't want to immediately sequence to the climb phase + plan.setPerformanceData( + 'pilotAccelerationAltitude', + SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') + parseInt(NXDataStore.get('CONFIG_ACCEL_ALT', '1500')), + ); + this.updateThrustReductionAcceleration(); + } + if (plan.performanceData.engineOutAccelerationAltitude === null) { + // it's important to set this immediately as we don't want to immediately sequence to the climb phase + plan.setPerformanceData( + 'pilotEngineOutAccelerationAltitude', + SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') + parseInt(NXDataStore.get('CONFIG_ACCEL_ALT', '1500')), + ); + this.updateThrustReductionAcceleration(); + } + + if (this.page.Current === this.page.PerformancePageTakeoff) { + CDUPerformancePage.ShowTAKEOFFPage(this.mcdu); + } else if (this.page.Current === this.page.ProgressPage) { + CDUProgressPage.ShowPage(this.mcdu); + } + + /** Arm preselected speed/mach for next flight phase */ + this.updatePreSelSpeedMach(this.preSelectedClbSpeed); + + this._rteRsvPercentOOR = false; + this._rteReservedWeightEntered = false; + this._rteReservedPctEntered = false; + + break; + } + + case FmgcFlightPhase.Climb: { + this._destDataChecked = false; + + if (this.page.Current === this.page.ProgressPage) { + CDUProgressPage.ShowPage(this.mcdu); + } else { + this.tryUpdatePerfPage(prevPhase, nextPhase); + } + + /** Activate pre selected speed/mach */ + if (prevPhase === FmgcFlightPhase.Takeoff) { + this.activatePreSelSpeedMach(this.preSelectedClbSpeed); + } + + /** Arm preselected speed/mach for next flight phase */ + this.updatePreSelSpeedMach(this.preSelectedCrzSpeed); + + if (!this.cruiseLevel) { + this.cruiseLevel = Simplane.getAutoPilotDisplayedAltitudeLockValue('feet') / 100; + } + + break; + } + + case FmgcFlightPhase.Cruise: { + if (this.page.Current === this.page.ProgressPage) { + CDUProgressPage.ShowPage(this.mcdu); + } else { + this.tryUpdatePerfPage(prevPhase, nextPhase); + } + + SimVar.SetSimVarValue('L:A32NX_GOAROUND_PASSED', 'bool', 0); + Coherent.call('GENERAL_ENG_THROTTLE_MANAGED_MODE_SET', ThrottleMode.AUTO) + .catch(console.error) + .catch(console.error); + + /** Activate pre selected speed/mach */ + if (prevPhase === FmgcFlightPhase.Climb) { + this.triggerCheckSpeedModeMessage(this.preSelectedCrzSpeed); + this.activatePreSelSpeedMach(this.preSelectedCrzSpeed); + } + + /** Arm preselected speed/mach for next flight phase */ + // FIXME implement pre-selected descent speed! + //this.updatePreSelSpeedMach(this.preSelectedDesSpeed); + + break; + } + + case FmgcFlightPhase.Descent: { + if (this.page.Current === this.page.ProgressPage) { + CDUProgressPage.ShowPage(this.mcdu); + } else { + this.tryUpdatePerfPage(prevPhase, nextPhase); + } + + this.checkDestData(); + + Coherent.call('GENERAL_ENG_THROTTLE_MANAGED_MODE_SET', ThrottleMode.AUTO) + .catch(console.error) + .catch(console.error); + + this.triggerCheckSpeedModeMessage(undefined); + + this.cruiseLevel = null; + + break; + } + + case FmgcFlightPhase.Approach: { + if (this.page.Current === this.page.ProgressPage) { + CDUProgressPage.ShowPage(this.mcdu); + } else { + this.tryUpdatePerfPage(prevPhase, nextPhase); + } + + // I think this is not necessary to port, as it only calls fs9gps stuff (fms-v2) + // this.flightPlanManager.activateApproach().catch(console.error); + + Coherent.call('GENERAL_ENG_THROTTLE_MANAGED_MODE_SET', ThrottleMode.AUTO).catch(console.error); + SimVar.SetSimVarValue('L:A32NX_GOAROUND_PASSED', 'bool', 0); + + this.checkDestData(); + + break; + } + + case FmgcFlightPhase.GoAround: { + SimVar.SetSimVarValue('L:A32NX_GOAROUND_INIT_SPEED', 'number', Simplane.getIndicatedSpeed()); + + this.flightPlanService.stringMissedApproach( + /** @type {FlightPlanLeg} */ (map) => { + this.addMessageToQueue(NXSystemMessages.cstrDelUpToWpt.getModifiedMessage(map.ident)); + }, + ); + + const activePlan = this.flightPlanService.active; + if (activePlan.performanceData.missedAccelerationAltitude === null) { + // it's important to set this immediately as we don't want to immediately sequence to the climb phase + activePlan.setPerformanceData( + 'pilotMissedAccelerationAltitude', + SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') + + parseInt(NXDataStore.get('CONFIG_ENG_OUT_ACCEL_ALT', '1500')), + ); + this.updateThrustReductionAcceleration(); + } + if (activePlan.performanceData.missedEngineOutAccelerationAltitude === null) { + // it's important to set this immediately as we don't want to immediately sequence to the climb phase + activePlan.setPerformanceData( + 'pilotMissedEngineOutAccelerationAltitude', + SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') + + parseInt(NXDataStore.get('CONFIG_ENG_OUT_ACCEL_ALT', '1500')), + ); + this.updateThrustReductionAcceleration(); + } + + if (this.page.Current === this.page.ProgressPage) { + CDUProgressPage.ShowPage(this.mcdu); + } else { + this.tryUpdatePerfPage(prevPhase, nextPhase); + } + + break; + } + + case FmgcFlightPhase.Done: + CDUIdentPage.ShowPage(this.mcdu); + + this.flightPlanService + .reset() + .then(() => { + this.initVariables(); + this.dataManager.deleteAllStoredWaypoints(); + this.setScratchpadText(''); + SimVar.SetSimVarValue('L:A32NX_COLD_AND_DARK_SPAWN', 'Bool', true).then(() => { + CDUIdentPage.ShowPage(this.mcdu); + }); + }) + .catch(console.error); + break; + } + } + + private triggerCheckSpeedModeMessage(preselectedSpeed) { + const isSpeedSelected = !Simplane.getAutoPilotAirspeedManaged(); + const hasPreselectedSpeed = preselectedSpeed !== undefined; + + if (!this.checkSpeedModeMessageActive && isSpeedSelected && !hasPreselectedSpeed) { + this.checkSpeedModeMessageActive = true; + this.addMessageToQueue( + NXSystemMessages.checkSpeedMode, + () => !this.checkSpeedModeMessageActive, + () => { + this.checkSpeedModeMessageActive = false; + SimVar.SetSimVarValue('L:A32NX_PFD_MSG_CHECK_SPEED_MODE', 'bool', false); + }, + ); + SimVar.SetSimVarValue('L:A32NX_PFD_MSG_CHECK_SPEED_MODE', 'bool', true); + } + } + + private clearCheckSpeedModeMessage() { + if (this.checkSpeedModeMessageActive && Simplane.getAutoPilotAirspeedManaged()) { + this.checkSpeedModeMessageActive = false; + this.removeMessageFromQueue(NXSystemMessages.checkSpeedMode.text); + SimVar.SetSimVarValue('L:A32NX_PFD_MSG_CHECK_SPEED_MODE', 'bool', false); + } + } + + /** FIXME these functions are in the new VNAV but not in this branch, remove when able */ + /** + * + * @param alt geopotential altitude in feet + * @returns °C + */ + public getIsaTemp(alt: number): number { + if (alt > (this.tropo ? this.tropo : 36090)) { + return -56.5; + } + return 15 - 0.0019812 * alt; + } + + /** + * @param alt geopotential altitude in feet + * @param isaDev temperature deviation from ISA conditions in degrees celcius + * @returns hPa + */ + private getPressure(alt: number, isaDev: number = 0) { + if (alt > (this.tropo ? this.tropo : 36090)) { + return ((216.65 + isaDev) / 288.15) ** 5.25588 * 1013.2; + } + return ((288.15 - 0.0019812 * alt + isaDev) / 288.15) ** 5.25588 * 1013.2; + } + + private getPressureAltAtElevation(elev: number, qnh = 1013.2) { + const p0 = qnh < 500 ? 29.92 : 1013.2; + return elev + 145442.15 * (1 - Math.pow(qnh / p0, 0.190263)); + } + + private getPressureAlt() { + for (let n = 1; n <= 3; n++) { + const zp = Arinc429Word.fromSimVarValue(`L:A32NX_ADIRS_ADR_${n}_ALTITUDE`); + if (zp.isNormalOperation()) { + return zp.value; + } + } + return null; + } + + private getBaroCorrection1() { + // FIXME hook up to ADIRU or FCU + return Simplane.getPressureValue('millibar'); + } + + /** + * @returns temperature deviation from ISA conditions in degrees celsius + */ + private getIsaDeviation(): number { + const geoAlt = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); + const temperature = SimVar.GetSimVarValue('AMBIENT TEMPERATURE', 'celsius'); + return temperature - this.getIsaTemp(geoAlt); + } + /** FIXME ^these functions are in the new VNAV but not in this branch, remove when able */ + + // TODO better decel distance calc + private calculateDecelDist(fromSpeed: number, toSpeed: number): number { + return Math.min(20, Math.max(3, (fromSpeed - toSpeed) * 0.15)); + } + + /* + When the aircraft is in the holding, predictions assume that the leg is flown at holding speed + with a vertical speed equal to - 1000 ft/mn until reaching a restrictive altitude constraint, the + FCU altitude or the exit fix. If FCU or constraint altitude is reached first, the rest of the + pattern is assumed to be flown level at that altitude + */ + private getHoldingSpeed(speedConstraint = undefined, altitude = undefined) { + const fcuAltitude = SimVar.GetSimVarValue('AUTOPILOT ALTITUDE LOCK VAR:3', 'feet'); + const alt = Math.max(fcuAltitude, altitude ? altitude : 0); + + let kcas = SimVar.GetSimVarValue('L:A32NX_SPEEDS_GD', 'number'); + if (this.flightPhaseManager.phase === FmgcFlightPhase.Approach) { + kcas = this.getAppManagedSpeed(); + } + + if (speedConstraint > 100) { + kcas = Math.min(kcas, speedConstraint); + } + + // apply icao limits + if (alt < 14000) { + kcas = Math.min(230, kcas); + } else if (alt < 20000) { + kcas = Math.min(240, kcas); + } else if (alt < 34000) { + kcas = Math.min(265, kcas); + } else { + const isaDeviation = this.getIsaDeviation(); + const pressure = this.getPressure(alt, isaDeviation); + kcas = Math.min(MathUtils.convertMachToKCas(0.83, pressure), kcas); + } + + // apply speed limit/alt + if (this.flightPhaseManager.phase <= FmgcFlightPhase.Cruise) { + if (this.climbSpeedLimit !== null && alt <= this.climbSpeedLimitAlt) { + kcas = Math.min(this.climbSpeedLimit, kcas); + } + } else if (this.flightPhaseManager.phase < FmgcFlightPhase.GoAround) { + if (this.descentSpeedLimit !== null && alt <= this.descentSpeedLimitAlt) { + kcas = Math.min(this.descentSpeedLimit, kcas); + } + } + + kcas = Math.max(kcas, this.computedVls); + + return Math.ceil(kcas); + } + + private updateHoldingSpeed() { + const plan = this.flightPlanService.active; + const currentLegIndex = plan.activeLegIndex; + const nextLegIndex = currentLegIndex + 1; + const currentLegConstraints = this.managedProfile.get(currentLegIndex); + const nextLegConstraints = this.managedProfile.get(nextLegIndex); + + const currentLeg = plan.maybeElementAt(currentLegIndex); + const nextLeg = plan.maybeElementAt(nextLegIndex); + + const casWord = ADIRS.getCalibratedAirspeed(); + const cas = casWord.isNormalOperation() ? casWord.value : 0; + + let enableHoldSpeedWarning = false; + let holdSpeedTarget = 0; + let holdDecelReached = this.holdDecelReached; + // FIXME big hack until VNAV can do this + if (currentLeg && currentLeg.isDiscontinuity === false && currentLeg.type === 'HM') { + holdSpeedTarget = this.getHoldingSpeed( + currentLegConstraints?.descentSpeed, + currentLegConstraints?.descentAltitude, + ); + holdDecelReached = true; + enableHoldSpeedWarning = !Simplane.getAutoPilotAirspeedManaged(); + this.holdIndex = plan.activeLegIndex; + } else if (nextLeg && nextLeg.isDiscontinuity === false && nextLeg.type === 'HM') { + const adirLat = ADIRS.getLatitude(); + const adirLong = ADIRS.getLongitude(); + + if (adirLat.isNormalOperation() && adirLong.isNormalOperation()) { + holdSpeedTarget = this.getHoldingSpeed(nextLegConstraints?.descentSpeed, nextLegConstraints?.descentAltitude); + + const dtg = this.guidanceController.activeLegDtg; + // decel range limits are [3, 20] NM + const decelDist = this.calculateDecelDist(cas, holdSpeedTarget); + if (dtg < decelDist) { + holdDecelReached = true; + } + + const gsWord = ADIRS.getGroundSpeed(); + const gs = gsWord.isNormalOperation() ? gsWord.value : 0; + const warningDist = decelDist + gs / 120; + if (!Simplane.getAutoPilotAirspeedManaged() && dtg <= warningDist) { + enableHoldSpeedWarning = true; + } + } + this.holdIndex = plan.activeLegIndex + 1; + } else { + this.holdIndex = 0; + holdDecelReached = false; + } + + if (holdDecelReached !== this.holdDecelReached) { + this.holdDecelReached = holdDecelReached; + SimVar.SetSimVarValue('L:A32NX_FM_HOLD_DECEL', 'bool', this.holdDecelReached); + } + + if (holdSpeedTarget !== this.holdSpeedTarget) { + this.holdSpeedTarget = holdSpeedTarget; + SimVar.SetSimVarValue('L:A32NX_FM_HOLD_SPEED', 'number', this.holdSpeedTarget); + } + + if (enableHoldSpeedWarning && cas - this.holdSpeedTarget > 5) { + if (!this.setHoldSpeedMessageActive) { + this.setHoldSpeedMessageActive = true; + this.addMessageToQueue( + NXSystemMessages.setHoldSpeed, + () => !this.setHoldSpeedMessageActive, + () => SimVar.SetSimVarValue('L:A32NX_PFD_MSG_SET_HOLD_SPEED', 'bool', false), + ); + SimVar.SetSimVarValue('L:A32NX_PFD_MSG_SET_HOLD_SPEED', 'bool', true); + } + } else if (this.setHoldSpeedMessageActive) { + SimVar.SetSimVarValue('L:A32NX_PFD_MSG_SET_HOLD_SPEED', 'bool', false); + this.setHoldSpeedMessageActive = false; + } + } + + private getManagedTargets(v, m) { + //const vM = _convertMachToKCas(m, _convertCtoK(Simplane.getAmbientTemperature()), SimVar.GetSimVarValue("AMBIENT PRESSURE", "millibar")); + const vM = SimVar.GetGameVarValue('FROM MACH TO KIAS', 'number', m); + return v > vM ? [vM, true] : [v, false]; + } + + private updateManagedSpeeds() { + if (!this.managedSpeedClimbIsPilotEntered) { + this.managedSpeedClimb = this.getClbManagedSpeedFromCostIndex(); + } + if (!this.managedSpeedCruiseIsPilotEntered) { + this.managedSpeedCruise = this.getCrzManagedSpeedFromCostIndex(); + } + + this.managedSpeedDescend = this.getDesManagedSpeedFromCostIndex(); + } + + private updateManagedSpeed() { + let vPfd = 0; + let isMach = false; + + this.updateHoldingSpeed(); + this.clearCheckSpeedModeMessage(); + + if (SimVar.GetSimVarValue('L:A32NX_FMA_EXPEDITE_MODE', 'number') === 1) { + const verticalMode = SimVar.GetSimVarValue('L:A32NX_FMA_VERTICAL_MODE', 'number'); + if (verticalMode === 12) { + switch (SimVar.GetSimVarValue('L:A32NX_FLAPS_HANDLE_INDEX', 'Number')) { + case 0: { + this.managedSpeedTarget = SimVar.GetSimVarValue('L:A32NX_SPEEDS_GD', 'number'); + break; + } + case 1: { + this.managedSpeedTarget = SimVar.GetSimVarValue('L:A32NX_SPEEDS_S', 'number'); + break; + } + default: { + this.managedSpeedTarget = SimVar.GetSimVarValue('L:A32NX_SPEEDS_F', 'number'); + } + } + } else if (verticalMode === 13) { + this.managedSpeedTarget = + SimVar.GetSimVarValue('L:A32NX_FLAPS_HANDLE_INDEX', 'Number') === 0 + ? Math.min(340, SimVar.GetGameVarValue('FROM MACH TO KIAS', 'number', 0.8)) + : SimVar.GetSimVarValue('L:A32NX_SPEEDS_VMAX', 'number') - 10; + } + vPfd = this.managedSpeedTarget; + } else if (this.holdDecelReached) { + vPfd = this.holdSpeedTarget; + this.managedSpeedTarget = this.holdSpeedTarget; + } else { + if (this.setHoldSpeedMessageActive) { + this.setHoldSpeedMessageActive = false; + SimVar.SetSimVarValue('L:A32NX_PFD_MSG_SET_HOLD_SPEED', 'bool', false); + this.removeMessageFromQueue(NXSystemMessages.setHoldSpeed.text); + } + + const engineOut = !this.isAllEngineOn(); + + switch (this.flightPhaseManager.phase) { + case FmgcFlightPhase.Preflight: { + if (this.v2Speed) { + vPfd = this.v2Speed; + this.managedSpeedTarget = this.v2Speed + 10; + } + break; + } + case FmgcFlightPhase.Takeoff: { + if (this.v2Speed) { + vPfd = this.v2Speed; + this.managedSpeedTarget = engineOut + ? Math.min( + this.v2Speed + 15, + Math.max(this.v2Speed, this.takeoffEngineOutSpeed ? this.takeoffEngineOutSpeed : 0), + ) + : this.v2Speed + 10; + } + break; + } + case FmgcFlightPhase.Climb: { + let speed = this.managedSpeedClimb; + + if ( + this.climbSpeedLimit !== undefined && + SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') < this.climbSpeedLimitAlt + ) { + speed = Math.min(speed, this.climbSpeedLimit); + } + + speed = Math.min(speed, this.getSpeedConstraint()); + + [this.managedSpeedTarget, isMach] = this.getManagedTargets(speed, this.managedSpeedClimbMach); + vPfd = this.managedSpeedTarget; + break; + } + case FmgcFlightPhase.Cruise: { + let speed = this.managedSpeedCruise; + + if ( + this.climbSpeedLimit !== undefined && + SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') < this.climbSpeedLimitAlt + ) { + speed = Math.min(speed, this.climbSpeedLimit); + } + + [this.managedSpeedTarget, isMach] = this.getManagedTargets(speed, this.managedSpeedCruiseMach); + vPfd = this.managedSpeedTarget; + break; + } + case FmgcFlightPhase.Descent: { + // We fetch this data from VNAV + vPfd = SimVar.GetSimVarValue('L:A32NX_SPEEDS_MANAGED_PFD', 'knots'); + this.managedSpeedTarget = SimVar.GetSimVarValue('L:A32NX_SPEEDS_MANAGED_ATHR', 'knots'); + + // Whether to use Mach or not should be based on the original managed speed, not whatever VNAV uses under the hood to vary it. + // Also, VNAV already does the conversion from Mach if necessary + isMach = this.getManagedTargets(this.getManagedDescentSpeed(), this.getManagedDescentSpeedMach())[1]; + break; + } + case FmgcFlightPhase.Approach: { + // the displayed target is Vapp (with GSmini) + // the guidance target is lower limited by FAC manouvering speeds (O, S, F) unless in landing config + // constraints are not considered + const speed = this.getAppManagedSpeed(); + vPfd = this.getVAppGsMini(); + + this.managedSpeedTarget = Math.max(speed, vPfd); + break; + } + case FmgcFlightPhase.GoAround: { + if (SimVar.GetSimVarValue('L:A32NX_FMA_VERTICAL_MODE', 'number') === 41 /* SRS GA */) { + const speed = Math.min( + this.computedVls + (engineOut ? 15 : 25), + Math.max(SimVar.GetSimVarValue('L:A32NX_GOAROUND_INIT_SPEED', 'number'), this.getVApp()), + SimVar.GetSimVarValue('L:A32NX_SPEEDS_VMAX', 'number') - 5, + ); + + vPfd = speed; + this.managedSpeedTarget = speed; + } else { + const speedConstraint = this.getSpeedConstraint(); + const speed = Math.min(this.computedVgd, speedConstraint); + + vPfd = speed; + this.managedSpeedTarget = speed; + } + break; + } + } + } + + // Automatically change fcu mach/speed mode + if (this.managedSpeedTargetIsMach !== isMach) { + if (isMach) { + SimVar.SetSimVarValue('K:AP_MANAGED_SPEED_IN_MACH_ON', 'number', 1); + } else { + SimVar.SetSimVarValue('K:AP_MANAGED_SPEED_IN_MACH_OFF', 'number', 1); + } + this.managedSpeedTargetIsMach = isMach; + } + + // Overspeed protection + const Vtap = Math.min(this.managedSpeedTarget, SimVar.GetSimVarValue('L:A32NX_SPEEDS_VMAX', 'number')); + + SimVar.SetSimVarValue('L:A32NX_SPEEDS_MANAGED_PFD', 'knots', vPfd); + SimVar.SetSimVarValue('L:A32NX_SPEEDS_MANAGED_ATHR', 'knots', Vtap); + + if (this.isAirspeedManaged()) { + Coherent.call('AP_SPD_VAR_SET', 0, Vtap).catch(console.error); + } + + // Reset V1/R/2 speed after the TAKEOFF phase + if (this.flightPhaseManager.phase > FmgcFlightPhase.Takeoff) { + this.v1Speed = null; + this.vRSpeed = null; + this.v2Speed = null; + } + } + + private activatePreSelSpeedMach(preSel) { + if (preSel) { + SimVar.SetSimVarValue('K:A32NX.FMS_PRESET_SPD_ACTIVATE', 'number', 1); + } + } + + private updatePreSelSpeedMach(preSel) { + // The timeout is required to create a delay for the current value to be read and the new one to be set + setTimeout(() => { + if (preSel) { + if (preSel > 1) { + SimVar.SetSimVarValue('L:A32NX_SpeedPreselVal', 'knots', preSel); + SimVar.SetSimVarValue('L:A32NX_MachPreselVal', 'mach', -1); + } else { + SimVar.SetSimVarValue('L:A32NX_SpeedPreselVal', 'knots', -1); + SimVar.SetSimVarValue('L:A32NX_MachPreselVal', 'mach', preSel); + } + } else { + SimVar.SetSimVarValue('L:A32NX_SpeedPreselVal', 'knots', -1); + SimVar.SetSimVarValue('L:A32NX_MachPreselVal', 'mach', -1); + } + }, 200); + } + + private checkSpeedLimit() { + let speedLimit; + let speedLimitAlt; + switch (this.flightPhaseManager.phase) { + case FmgcFlightPhase.Climb: + case FmgcFlightPhase.Cruise: + speedLimit = this.climbSpeedLimit; + speedLimitAlt = this.climbSpeedLimitAlt; + break; + case FmgcFlightPhase.Descent: + speedLimit = this.descentSpeedLimit; + speedLimitAlt = this.descentSpeedLimitAlt; + break; + default: + // no speed limit in other phases + this.speedLimitExceeded = false; + return; + } + + if (speedLimit === undefined) { + this.speedLimitExceeded = false; + return; + } + + const cas = ADIRS.getCalibratedAirspeed(); + const alt = ADIRS.getBaroCorrectedAltitude(); + + if (this.speedLimitExceeded) { + const resetLimitExceeded = + !cas.isNormalOperation() || + !alt.isNormalOperation() || + alt.value > speedLimitAlt || + cas.value <= speedLimit + 5; + if (resetLimitExceeded) { + this.speedLimitExceeded = false; + this.removeMessageFromQueue(NXSystemMessages.spdLimExceeded.text); + } + } else if (cas.isNormalOperation() && alt.isNormalOperation()) { + const setLimitExceeded = alt.value < speedLimitAlt - 150 && cas.value > speedLimit + 10; + if (setLimitExceeded) { + this.speedLimitExceeded = true; + this.addMessageToQueue(NXSystemMessages.spdLimExceeded, () => !this.speedLimitExceeded); + } + } + } + + private updateAutopilot() { + const now = performance.now(); + const dt = now - this._lastUpdateAPTime; + let apLogicOn = this._apMasterStatus || Simplane.getAutoPilotFlightDirectorActive(1); + this._lastUpdateAPTime = now; + if (isFinite(dt)) { + this.updateAutopilotCooldown -= dt; + } + if (SimVar.GetSimVarValue('L:AIRLINER_FMC_FORCE_NEXT_UPDATE', 'number') === 1) { + SimVar.SetSimVarValue('L:AIRLINER_FMC_FORCE_NEXT_UPDATE', 'number', 0); + this.updateAutopilotCooldown = -1; + } + + if ( + this.flightPhaseManager.phase === FmgcFlightPhase.Takeoff && + !this.isAllEngineOn() && + this.takeoffEngineOutSpeed === undefined + ) { + const casWord = ADIRS.getCalibratedAirspeed(); + this.takeoffEngineOutSpeed = casWord.isNormalOperation() ? casWord.value : undefined; + } + + if (this.updateAutopilotCooldown < 0) { + this.updatePerfSpeeds(); + this.updateConstraints(); + this.updateManagedSpeed(); + const currentApMasterStatus = SimVar.GetSimVarValue('AUTOPILOT MASTER', 'boolean'); + if (currentApMasterStatus !== this._apMasterStatus) { + this._apMasterStatus = currentApMasterStatus; + apLogicOn = this._apMasterStatus || Simplane.getAutoPilotFlightDirectorActive(1); + this._forceNextAltitudeUpdate = true; + console.log('Enforce AP in Altitude Lock mode. Cause : AP Master Status has changed.'); + SimVar.SetSimVarValue('L:A320_NEO_FCU_FORCE_IDLE_VS', 'Number', 1); + if (this._apMasterStatus) { + if (this.flightPlanService.hasActive && this.flightPlanService.active.legCount === 0) { + this._onModeSelectedAltitude(); + this._onModeSelectedHeading(); + } + } + } + if (apLogicOn) { + if (!Simplane.getAutoPilotFLCActive() && !SimVar.GetSimVarValue('AUTOPILOT AIRSPEED HOLD', 'Boolean')) { + SimVar.SetSimVarValue('K:AP_PANEL_SPEED_HOLD', 'Number', 1); + } + if (!SimVar.GetSimVarValue('AUTOPILOT HEADING LOCK', 'Boolean')) { + if (!SimVar.GetSimVarValue('AUTOPILOT APPROACH HOLD', 'Boolean')) { + SimVar.SetSimVarValue('K:AP_PANEL_HEADING_HOLD', 'Number', 1); + } + } + } + + if (this.isAltitudeManaged()) { + const plan = this.flightPlanService.active; + + const prevWaypoint = plan.hasElement(plan.activeLegIndex - 1); + const nextWaypoint = plan.hasElement(plan.activeLegIndex + 1); + + if (prevWaypoint && nextWaypoint) { + const activeWpIdx = plan.activeLegIndex; + + if (activeWpIdx !== this.activeWpIdx) { + this.activeWpIdx = activeWpIdx; + this.updateConstraints(); + } + if (this.constraintAlt) { + Coherent.call('AP_ALT_VAR_SET_ENGLISH', 2, this.constraintAlt, this._forceNextAltitudeUpdate).catch( + console.error, + ); + this._forceNextAltitudeUpdate = false; + } else { + const altitude = Simplane.getAutoPilotSelectedAltitudeLockValue('feet'); + if (isFinite(altitude)) { + Coherent.call('AP_ALT_VAR_SET_ENGLISH', 2, altitude, this._forceNextAltitudeUpdate).catch(console.error); + this._forceNextAltitudeUpdate = false; + } + } + } else { + const altitude = Simplane.getAutoPilotSelectedAltitudeLockValue('feet'); + if (isFinite(altitude)) { + SimVar.SetSimVarValue('L:A32NX_FG_ALTITUDE_CONSTRAINT', 'feet', 0); + Coherent.call('AP_ALT_VAR_SET_ENGLISH', 2, altitude, this._forceNextAltitudeUpdate).catch(console.error); + this._forceNextAltitudeUpdate = false; + } + } + } + + if ( + Simplane.getAutoPilotAltitudeManaged() && + this.flightPlanService.hasActive && + SimVar.GetSimVarValue('L:A320_NEO_FCU_STATE', 'number') !== 1 + ) { + const currentWaypointIndex = this.flightPlanService.active.activeLegIndex; + if (currentWaypointIndex !== this._lastRequestedFLCModeWaypointIndex) { + this._lastRequestedFLCModeWaypointIndex = currentWaypointIndex; + setTimeout(() => { + if (Simplane.getAutoPilotAltitudeManaged()) { + this._onModeManagedAltitude(); + } + }, 1000); + } + } + + if (this.flightPhaseManager.phase === FmgcFlightPhase.GoAround && apLogicOn) { + //depending if on HDR/TRK or NAV mode, select appropriate Alt Mode (WIP) + //this._onModeManagedAltitude(); + this._onModeSelectedAltitude(); + } + this.updateAutopilotCooldown = this._apCooldown; + } + } + + /** + * Updates performance speeds such as GD, F, S, Vls and approach speeds + */ + public updatePerfSpeeds() { + this.computedVgd = SimVar.GetSimVarValue('L:A32NX_SPEEDS_GD', 'number'); + this.computedVfs = SimVar.GetSimVarValue('L:A32NX_SPEEDS_F', 'number'); + this.computedVss = SimVar.GetSimVarValue('L:A32NX_SPEEDS_S', 'number'); + this.computedVls = SimVar.GetSimVarValue('L:A32NX_SPEEDS_VLS', 'number'); + + let weight = this.tryEstimateLandingWeight(); + const vnavPrediction = this.guidanceController.vnavDriver.getDestinationPrediction(); + // Actual weight is used during approach phase (FCOM bulletin 46/2), and we also assume during go-around + // Fallback gross weight set to 64.3T (MZFW), which is replaced by FMGW once input in FMS to avoid function returning undefined results. + if (this.flightPhaseManager.phase >= FmgcFlightPhase.Approach || !isFinite(weight)) { + weight = this.getGW() == 0 ? 64.3 : this.getGW(); + } else if (vnavPrediction && Number.isFinite(vnavPrediction.estimatedFuelOnBoard)) { + weight = this.zeroFuelWeight + Math.max(0, (vnavPrediction.estimatedFuelOnBoard * 0.4535934) / 1000); + } + // if pilot has set approach wind in MCDU we use it, otherwise fall back to current measured wind + if (isFinite(this.perfApprWindSpeed) && isFinite(this.perfApprWindHeading)) { + this.approachSpeeds = new NXSpeedsApp(weight, this.perfApprFlaps3, this._towerHeadwind); + } else { + this.approachSpeeds = new NXSpeedsApp(weight, this.perfApprFlaps3); + } + this.approachSpeeds.valid = this.flightPhaseManager.phase >= FmgcFlightPhase.Approach || isFinite(weight); + } + + public updateConstraints() { + const activeFpIndex = this.flightPlanService.activeLegIndex; + const constraints = this.managedProfile.get(activeFpIndex); + const fcuSelAlt = Simplane.getAutoPilotDisplayedAltitudeLockValue('feet'); + + let constraintAlt = 0; + if (constraints) { + // Altitude constraints are not sent in GA phase. While we cannot engage CLB anyways, ALT counts as a managed mode, so we don't want to show + // a magenta altitude in ALT due to a constraint + if ( + this.flightPhaseManager.phase < FmgcFlightPhase.Cruise && + isFinite(constraints.climbAltitude) && + constraints.climbAltitude < fcuSelAlt + ) { + constraintAlt = constraints.climbAltitude; + } + + if ( + this.flightPhaseManager.phase > FmgcFlightPhase.Cruise && + this.flightPhaseManager.phase < FmgcFlightPhase.GoAround && + isFinite(constraints.descentAltitude) && + constraints.descentAltitude > fcuSelAlt + ) { + constraintAlt = constraints.descentAltitude; + } + } + + if (constraintAlt !== this.constraintAlt) { + this.constraintAlt = constraintAlt; + SimVar.SetSimVarValue('L:A32NX_FG_ALTITUDE_CONSTRAINT', 'feet', this.constraintAlt); + } + } + + // TODO/VNAV: Speed constraint + private getSpeedConstraint() { + if (!this.navModeEngaged()) { + return Infinity; + } + + return this.getNavModeSpeedConstraint(); + } + + public getNavModeSpeedConstraint(): number { + const activeLegIndex = + this.guidanceController.activeTransIndex >= 0 + ? this.guidanceController.activeTransIndex + : this.guidanceController.activeLegIndex; + const constraints = this.managedProfile.get(activeLegIndex); + if (constraints) { + if ( + this.flightPhaseManager.phase < FmgcFlightPhase.Cruise || + this.flightPhaseManager.phase === FmgcFlightPhase.GoAround + ) { + return constraints.climbSpeed; + } + + if ( + this.flightPhaseManager.phase > FmgcFlightPhase.Cruise && + this.flightPhaseManager.phase < FmgcFlightPhase.GoAround + ) { + // FIXME proper decel calc + if ( + this.guidanceController.activeLegDtg < + this.calculateDecelDist( + Math.min(constraints.previousDescentSpeed, this.getManagedDescentSpeed()), + constraints.descentSpeed, + ) + ) { + return constraints.descentSpeed; + } else { + return constraints.previousDescentSpeed; + } + } + } + + return Infinity; + } + + private updateManagedProfile() { + this.managedProfile.clear(); + + const plan = this.flightPlanService.active; + + const destination = plan.destinationAirport; + const destinationElevation = destination ? destination.location.alt : 0; + + // TODO should we save a constraint already propagated to the current leg? + + // propagate descent speed constraints forward + let currentSpeedConstraint = Infinity; + let previousSpeedConstraint = Infinity; + for (let index = 0; index < Math.min(plan.firstMissedApproachLegIndex, plan.legCount); index++) { + const leg = plan.elementAt(index); + + if (leg.isDiscontinuity === true) { + continue; + } + + if (leg.constraintType === 2 /** DES */) { + if (leg.speedConstraint) { + currentSpeedConstraint = Math.min(currentSpeedConstraint, Math.round(leg.speedConstraint.speed)); + } + } + + this.managedProfile.set(index, { + descentSpeed: currentSpeedConstraint, + previousDescentSpeed: previousSpeedConstraint, + climbSpeed: Infinity, + previousClimbSpeed: Infinity, + climbAltitude: Infinity, + descentAltitude: -Infinity, + }); + + previousSpeedConstraint = currentSpeedConstraint; + } + + // propagate climb speed constraints backward + // propagate alt constraints backward + currentSpeedConstraint = Infinity; + previousSpeedConstraint = Infinity; + let currentDesConstraint = -Infinity; + let currentClbConstraint = Infinity; + + for (let index = Math.min(plan.firstMissedApproachLegIndex, plan.legCount) - 1; index >= 0; index--) { + const leg = plan.elementAt(index); + + if (leg.isDiscontinuity === true) { + continue; + } + + const altConstraint = leg.altitudeConstraint; + const speedConstraint = leg.speedConstraint; + + if (leg.constraintType === 1 /** CLB */) { + if (speedConstraint) { + currentSpeedConstraint = Math.min(currentSpeedConstraint, Math.round(speedConstraint.speed)); + } + + if (altConstraint) { + switch (altConstraint.altitudeDescriptor) { + case '@': // at alt 1 + case '-': // at or below alt 1 + case 'B': // between alt 1 and alt 2 + currentClbConstraint = Math.min(currentClbConstraint, Math.round(altConstraint.altitude1)); + break; + default: + // not constraining + } + } + } else if (leg.constraintType === 2 /** DES */) { + if (altConstraint) { + switch (altConstraint.altitudeDescriptor) { + case '@': // at alt 1 + case '+': // at or above alt 1 + case 'I': // alt1 is at for FACF, Alt2 is glidelope intercept + case 'J': // alt1 is at or above for FACF, Alt2 is glideslope intercept + case 'V': // alt1 is procedure alt for step-down, Alt2 is at alt for vertical path angle + case 'X': // alt 1 is at, Alt 2 is on the vertical angle + currentDesConstraint = Math.max(currentDesConstraint, Math.round(altConstraint.altitude1)); + break; + case 'B': // between alt 1 and alt 2 + currentDesConstraint = Math.max(currentDesConstraint, Math.round(altConstraint.altitude2)); + break; + default: + // not constraining + } + } + } + + const profilePoint = this.managedProfile.get(index); + profilePoint.climbSpeed = currentSpeedConstraint; + profilePoint.previousClimbSpeed = previousSpeedConstraint; + profilePoint.climbAltitude = currentClbConstraint; + profilePoint.descentAltitude = Math.max(destinationElevation, currentDesConstraint); + previousSpeedConstraint = currentSpeedConstraint; + } + } + + private async updateDestinationData() { + let landingElevation; + let latitude; + let longitude; + + const runway = this.flightPlanService.active.destinationRunway; + + if (runway) { + landingElevation = runway.thresholdLocation.alt; + latitude = runway.thresholdLocation.lat; + longitude = runway.thresholdLocation.long; + } else { + const airport = this.flightPlanService.active.destinationAirport; + + if (airport) { + const ele = airport.location.alt; + + landingElevation = isFinite(ele) ? ele : undefined; + latitude = airport.location.lat; + longitude = airport.location.long; + } + } + + if (this.landingElevation !== landingElevation) { + this.landingElevation = landingElevation; + + const ssm = + landingElevation !== undefined + ? Arinc429SignStatusMatrix.NormalOperation + : Arinc429SignStatusMatrix.NoComputedData; + + this.arincLandingElevation.setBnrValue(landingElevation ? landingElevation : 0, ssm, 14, 16384, -2048); + } + + if (this.destinationLatitude !== latitude) { + this.destinationLatitude = latitude; + + const ssm = + latitude !== undefined ? Arinc429SignStatusMatrix.NormalOperation : Arinc429SignStatusMatrix.NoComputedData; + + this.arincDestinationLatitude.setBnrValue(latitude ? latitude : 0, ssm, 18, 180, -180); + } + + if (this.destinationLongitude !== longitude) { + this.destinationLongitude = longitude; + + const ssm = + longitude !== undefined ? Arinc429SignStatusMatrix.NormalOperation : Arinc429SignStatusMatrix.NoComputedData; + + this.arincDestinationLongitude.setBnrValue(longitude ? longitude : 0, ssm, 18, 180, -180); + } + } + + private updateMinimums() { + const inRange = this.shouldTransmitMinimums(); + + const mdaValid = inRange && this.perfApprMDA !== null; + const dhValid = !mdaValid && inRange && typeof this.perfApprDH === 'number'; + + const mdaSsm = mdaValid ? Arinc429SignStatusMatrix.NormalOperation : Arinc429SignStatusMatrix.NoComputedData; + const dhSsm = dhValid ? Arinc429SignStatusMatrix.NormalOperation : Arinc429SignStatusMatrix.NoComputedData; + + this.arincMDA.setBnrValue(mdaValid ? this.perfApprMDA : 0, mdaSsm, 17, 131072, 0); + this.arincDH.setBnrValue(dhValid && typeof this.perfApprDH === 'number' ? this.perfApprDH : 0, dhSsm, 16, 8192, 0); + this.arincEisWord2.setBitValue(29, inRange && this.perfApprDH === 'NO DH'); + // FIXME we need to handle these better + this.arincEisWord2.setSsm(Arinc429SignStatusMatrix.NormalOperation); + } + + private shouldTransmitMinimums() { + const phase = this.flightPhaseManager.phase; + const distanceToDestination = this.getDistanceToDestination(); + const isCloseToDestination = Number.isFinite(distanceToDestination) ? distanceToDestination < 250 : true; + + return phase > FmgcFlightPhase.Cruise || (phase === FmgcFlightPhase.Cruise && isCloseToDestination); + } + + private getClbManagedSpeedFromCostIndex() { + const dCI = ((this.costIndex ? this.costIndex : 0) / 999) ** 2; + return 290 * (1 - dCI) + 330 * dCI; + } + + private getCrzManagedSpeedFromCostIndex() { + const dCI = ((this.costIndex ? this.costIndex : 0) / 999) ** 2; + return 290 * (1 - dCI) + 310 * dCI; + } + + private getDesManagedSpeedFromCostIndex() { + const dCI = (this.costIndex ? this.costIndex : 0) / 999; + return 288 * (1 - dCI) + 300 * dCI; + } + + private getAppManagedSpeed() { + switch (SimVar.GetSimVarValue('L:A32NX_FLAPS_HANDLE_INDEX', 'Number')) { + case 0: + return this.computedVgd; + case 1: + return this.computedVss; + case 3: + return this.perfApprFlaps3 ? this.getVApp() : this.computedVfs; + case 4: + return this.getVApp(); + default: + return this.computedVfs; + } + } + + /* FMS EVENTS */ + + public onPowerOn() { + const gpsDriven = SimVar.GetSimVarValue('GPS DRIVES NAV1', 'Bool'); + if (!gpsDriven) { + SimVar.SetSimVarValue('K:TOGGLE_GPS_DRIVES_NAV1', 'Bool', 0); + } + + this._onModeSelectedHeading(); + this._onModeSelectedAltitude(); + + SimVar.SetSimVarValue('K:VS_SLOT_INDEX_SET', 'number', 1); + + this.taxiFuelWeight = 0.2; + CDUInitPage.updateTowIfNeeded(this.mcdu); + } + + protected onEvent(_event) { + if (_event === 'MODE_SELECTED_HEADING') { + if (Simplane.getAutoPilotHeadingManaged()) { + if (SimVar.GetSimVarValue('L:A320_FCU_SHOW_SELECTED_HEADING', 'number') === 0) { + const currentHeading = Simplane.getHeadingMagnetic(); + + Coherent.call('HEADING_BUG_SET', 1, currentHeading).catch(console.error); + } + } + this._onModeSelectedHeading(); + } + if (_event === 'MODE_MANAGED_HEADING') { + if (this.flightPlanService.active.legCount === 0) { + return; + } + + this._onModeManagedHeading(); + } + if (_event === 'MODE_SELECTED_ALTITUDE') { + const dist = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; + this.flightPhaseManager.handleFcuAltKnobPushPull(dist); + this._onModeSelectedAltitude(); + this._onStepClimbDescent(); + } + if (_event === 'MODE_MANAGED_ALTITUDE') { + const dist = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; + this.flightPhaseManager.handleFcuAltKnobPushPull(dist); + this._onModeManagedAltitude(); + this._onStepClimbDescent(); + } + if (_event === 'AP_DEC_ALT' || _event === 'AP_INC_ALT') { + const dist = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; + this.flightPhaseManager.handleFcuAltKnobTurn(dist); + this._onTrySetCruiseFlightLevel(); + } + if (_event === 'AP_DEC_HEADING' || _event === 'AP_INC_HEADING') { + if (SimVar.GetSimVarValue('L:A320_FCU_SHOW_SELECTED_HEADING', 'number') === 0) { + const currentHeading = Simplane.getHeadingMagnetic(); + Coherent.call('HEADING_BUG_SET', 1, currentHeading).catch(console.error); + } + SimVar.SetSimVarValue('L:A320_FCU_SHOW_SELECTED_HEADING', 'number', 1); + } + if (_event === 'VS') { + const dist = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; + this.flightPhaseManager.handleFcuVSKnob(dist, this._onStepClimbDescent.bind(this)); + } + } + + private _onModeSelectedHeading() { + if (SimVar.GetSimVarValue('AUTOPILOT APPROACH HOLD', 'boolean')) { + return; + } + if (!SimVar.GetSimVarValue('AUTOPILOT HEADING LOCK', 'Boolean')) { + SimVar.SetSimVarValue('K:AP_PANEL_HEADING_HOLD', 'Number', 1); + } + SimVar.SetSimVarValue('K:HEADING_SLOT_INDEX_SET', 'number', 1); + } + + private _onModeManagedHeading() { + if (SimVar.GetSimVarValue('AUTOPILOT APPROACH HOLD', 'boolean')) { + return; + } + if (!SimVar.GetSimVarValue('AUTOPILOT HEADING LOCK', 'Boolean')) { + SimVar.SetSimVarValue('K:AP_PANEL_HEADING_HOLD', 'Number', 1); + } + SimVar.SetSimVarValue('K:HEADING_SLOT_INDEX_SET', 'number', 2); + SimVar.SetSimVarValue('L:A320_FCU_SHOW_SELECTED_HEADING', 'number', 0); + } + + private _onModeSelectedAltitude() { + if (!Simplane.getAutoPilotGlideslopeHold()) { + SimVar.SetSimVarValue('L:A320_NEO_FCU_FORCE_IDLE_VS', 'Number', 1); + } + SimVar.SetSimVarValue('K:ALTITUDE_SLOT_INDEX_SET', 'number', 1); + Coherent.call( + 'AP_ALT_VAR_SET_ENGLISH', + 1, + Simplane.getAutoPilotDisplayedAltitudeLockValue(), + this._forceNextAltitudeUpdate, + ).catch(console.error); + } + + private _onModeManagedAltitude() { + SimVar.SetSimVarValue('K:ALTITUDE_SLOT_INDEX_SET', 'number', 2); + Coherent.call( + 'AP_ALT_VAR_SET_ENGLISH', + 1, + Simplane.getAutoPilotDisplayedAltitudeLockValue(), + this._forceNextAltitudeUpdate, + ).catch(console.error); + Coherent.call( + 'AP_ALT_VAR_SET_ENGLISH', + 2, + Simplane.getAutoPilotDisplayedAltitudeLockValue(), + this._forceNextAltitudeUpdate, + ).catch(console.error); + if (!Simplane.getAutoPilotGlideslopeHold()) { + requestAnimationFrame(() => { + SimVar.SetSimVarValue('L:A320_NEO_FCU_FORCE_IDLE_VS', 'Number', 1); + }); + } + } + + private _onStepClimbDescent() { + if ( + !( + this.flightPhaseManager.phase === FmgcFlightPhase.Climb || + this.flightPhaseManager.phase === FmgcFlightPhase.Cruise + ) + ) { + return; + } + + const _targetFl = Simplane.getAutoPilotDisplayedAltitudeLockValue() / 100; + + if ( + (this.flightPhaseManager.phase === FmgcFlightPhase.Climb && _targetFl > this.cruiseLevel) || + (this.flightPhaseManager.phase === FmgcFlightPhase.Cruise && _targetFl !== this.cruiseLevel) + ) { + this.deleteOutdatedCruiseSteps(this.cruiseLevel, _targetFl); + this.addMessageToQueue(NXSystemMessages.newCrzAlt.getModifiedMessage(_targetFl * 100)); + + this.cruiseLevel = _targetFl; + } + } + + private deleteOutdatedCruiseSteps(oldCruiseLevel, newCruiseLevel) { + const isClimbVsDescent = newCruiseLevel > oldCruiseLevel; + + const activePlan = this.flightPlanService.active; + + for (let i = activePlan.activeLegIndex; i < activePlan.legCount; i++) { + const element = activePlan.elementAt(i); + + if (!element || element.isDiscontinuity === true || !element.cruiseStep) { + continue; + } + + const stepLevel = Math.round(element.cruiseStep.toAltitude / 100); + + if ( + (isClimbVsDescent && stepLevel >= oldCruiseLevel && stepLevel <= newCruiseLevel) || + (!isClimbVsDescent && stepLevel <= oldCruiseLevel && stepLevel >= newCruiseLevel) + ) { + element.cruiseStep = undefined; // TODO call a method on FPS so that we sync this (fms-v2) + this.removeMessageFromQueue(NXSystemMessages.stepAhead.text); + } + } + } + + /*** + * Executed on every alt knob turn, checks whether or not the crz fl can be changed to the newly selected fcu altitude + * It creates a timeout to simulate real life delay which resets every time the fcu knob alt increases or decreases. + * @private + */ + private _onTrySetCruiseFlightLevel() { + if ( + !( + this.flightPhaseManager.phase === FmgcFlightPhase.Climb || + this.flightPhaseManager.phase === FmgcFlightPhase.Cruise + ) + ) { + return; + } + + const activeVerticalMode = SimVar.GetSimVarValue('L:A32NX_FMA_VERTICAL_MODE', 'enum'); + + if ( + (activeVerticalMode >= 11 && activeVerticalMode <= 15) || + (activeVerticalMode >= 21 && activeVerticalMode <= 23) + ) { + const fcuFl = Simplane.getAutoPilotDisplayedAltitudeLockValue() / 100; + + if ( + (this.flightPhaseManager.phase === FmgcFlightPhase.Climb && fcuFl > this.cruiseLevel) || + (this.flightPhaseManager.phase === FmgcFlightPhase.Cruise && fcuFl !== this.cruiseLevel) + ) { + if (this.cruiseFlightLevelTimeOut) { + clearTimeout(this.cruiseFlightLevelTimeOut); + this.cruiseFlightLevelTimeOut = undefined; + } + + this.cruiseFlightLevelTimeOut = setTimeout(() => { + if ( + fcuFl === Simplane.getAutoPilotDisplayedAltitudeLockValue() / 100 && + ((this.flightPhaseManager.phase === FmgcFlightPhase.Climb && fcuFl > this.cruiseLevel) || + (this.flightPhaseManager.phase === FmgcFlightPhase.Cruise && fcuFl !== this.cruiseLevel)) + ) { + this.addMessageToQueue(NXSystemMessages.newCrzAlt.getModifiedMessage(fcuFl * 100)); + this.cruiseLevel = fcuFl; + + if (this.page.Current === this.page.ProgressPage) { + CDUProgressPage.ShowPage(this.mcdu); + } + } + }, 3000); + } + } + } + + /* END OF FMS EVENTS */ + /* FMS CHECK ROUTINE */ + + private checkDestData() { + this.addMessageToQueue(NXSystemMessages.enterDestData, () => { + return ( + isFinite(this.perfApprQNH) && + isFinite(this.perfApprTemp) && + isFinite(this.perfApprWindHeading) && + isFinite(this.perfApprWindSpeed) + ); + }); + } + + private checkGWParams() { + const fmGW = SimVar.GetSimVarValue('L:A32NX_FM_GROSS_WEIGHT', 'Number'); + const eng1state = SimVar.GetSimVarValue('L:A32NX_ENGINE_STATE:1', 'Number'); + const eng2state = SimVar.GetSimVarValue('L:A32NX_ENGINE_STATE:2', 'Number'); + const gs = SimVar.GetSimVarValue('GPS GROUND SPEED', 'knots'); + const actualGrossWeight = SimVar.GetSimVarValue('TOTAL WEIGHT', 'Kilograms') / 1000; //TO-DO Source to be replaced with FAC-GW + const gwMismatch = Math.abs(fmGW - actualGrossWeight) > 7 ? true : false; + + if (eng1state == 2 || eng2state == 2) { + if (this._gwInitDisplayed < 1 && this.flightPhaseManager.phase < FmgcFlightPhase.Takeoff) { + this._initMessageSettable = true; + } + } + //INITIALIZE WEIGHT/CG + if (this.isAnEngineOn() && fmGW === 0 && this._initMessageSettable) { + this.addMessageToQueue(NXSystemMessages.initializeWeightOrCg); + this._gwInitDisplayed++; + this._initMessageSettable = false; + } + + //CHECK WEIGHT + //TO-DO Ground Speed used for redundancy and to simulate delay (~10s) for FAC parameters to be calculated, remove once FAC is available. + if (!this.isOnGround() && gwMismatch && this._checkWeightSettable && gs > 180) { + this.addMessageToQueue(NXSystemMessages.checkWeight); + this._checkWeightSettable = false; + } else if (!gwMismatch) { + this.removeMessageFromQueue(NXSystemMessages.checkWeight.text); + this._checkWeightSettable = true; + } + } + + /* END OF FMS CHECK ROUTINE */ + /* MCDU GET/SET METHODS */ + + public setCruiseFlightLevelAndTemperature(input: string): boolean { + if (input === Keypad.clrValue) { + this.cruiseLevel = null; + this.cruiseTemperature = undefined; + return true; + } + const flString = input.split('/')[0].replace('FL', ''); + const tempString = input.split('/')[1]; + const onlyTemp = flString.length === 0; + + if (!!flString && !onlyTemp && this.trySetCruiseFl(parseFloat(flString))) { + if ( + SimVar.GetSimVarValue('L:A32NX_CRZ_ALT_SET_INITIAL', 'bool') === 1 && + SimVar.GetSimVarValue('L:A32NX_GOAROUND_PASSED', 'bool') === 1 + ) { + SimVar.SetSimVarValue('L:A32NX_NEW_CRZ_ALT', 'number', this.cruiseLevel); + } else { + SimVar.SetSimVarValue('L:A32NX_CRZ_ALT_SET_INITIAL', 'bool', 1); + } + if (!tempString) { + return true; + } + } + if (tempString) { + const temp = parseInt(tempString.replace('M', '-')); + console.log('tS: ' + tempString); + console.log('ti: ' + temp); + if (isFinite(temp) && this.cruiseLevel) { + if (temp > -270 && temp < 100) { + this.cruiseTemperature = temp; + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } else { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + } + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + public tryUpdateCostIndex(costIndex: string): boolean { + const value = parseInt(costIndex); + if (isFinite(value)) { + if (value >= 0) { + if (value < 1000) { + this.costIndex = value; + this.updateManagedSpeeds(); + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } + } + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + /** + * Any tropopause altitude up to 60,000 ft is able to be entered + * @param tropo Format: NNNN or NNNNN Leading 0’s must be included. Entry is rounded to the nearest 10 ft + * @return Whether tropopause could be set or not + */ + public tryUpdateTropo(tropo: string): boolean { + if (tropo === Keypad.clrValue) { + if (this.tropo) { + this.tropo = undefined; + return true; + } + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + if (!tropo.match(/^(?=(\D*\d){4,5}\D*$)/g)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const value = parseInt(tropo); + if (isFinite(value) && value >= 0 && value <= 60000) { + this.tropo = Math.round(value / 10) * 10; + return true; + } + + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + //----------------------------------------------------------------------------------- + // TODO:FPM REWRITE: Start of functions to refactor + //----------------------------------------------------------------------------------- + + private resetCoroute() { + this.coRoute.routeNumber = undefined; + this.coRoute.routes = []; + } + + /** MCDU Init page method for FROM/TO, NOT for programmatic use */ + public tryUpdateFromTo(fromTo: string, callback = EmptyCallback.Boolean) { + if (fromTo === Keypad.clrValue) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return callback(false); + } + + const match = fromTo.match(/^([A-Z]{4})\/([A-Z]{4})$/); + if (match === null) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return callback(false); + } + const [, from, to] = match; + + this.resetCoroute(); + + this.setFromTo(from, to) + .then(() => { + this.getCoRouteList() + .then(() => callback(true)) + .catch(console.log); + }) + .catch((e) => { + if (e instanceof McduMessage) { + this.setScratchpadMessage(e); + } else { + console.warn(e); + } + callback(false); + }); + } + + /** + * Programmatic method to set from/to + * @param from 4-letter icao code for origin airport + * @param to 4-letter icao code for destination airport + * @throws NXSystemMessage on error (you are responsible for pushing to the scratchpad if appropriate) + */ + private async setFromTo(from: string, to: string) { + let airportFrom, airportTo; + try { + airportFrom = await this.navigationDatabaseService.activeDatabase.searchAirport(from); + airportTo = await this.navigationDatabaseService.activeDatabase.searchAirport(to); + + if (!airportFrom || !airportTo) { + throw NXSystemMessages.notInDatabase; + } + } catch (e) { + console.log(e); + throw NXSystemMessages.notInDatabase; + } + + this.atsu.resetAtisAutoUpdate(); + + return this.flightPlanService.newCityPair(from, to).then(() => { + this.setGroundTempFromOrigin(); + }); + } + + /** + * Computes distance between destination and alternate destination + */ + private tryUpdateDistanceToAlt() { + const activePlan = this.flightPlanService.active; + + if (activePlan && activePlan.destinationAirport && activePlan.alternateDestinationAirport) { + this._DistanceToAlt = Avionics.Utils.computeGreatCircleDistance( + activePlan.destinationAirport.location, + activePlan.alternateDestinationAirport.location, + ); + } else { + this._DistanceToAlt = 0; + } + } + + //----------------------------------------------------------------------------------- + // TODO:FPM REWRITE: End of functions to refactor + //----------------------------------------------------------------------------------- + + // only used by trySetRouteAlternateFuel + private isAltFuelInRange(fuel) { + if (Number.isFinite(this.blockFuel)) { + return 0 < fuel && fuel < this.blockFuel - this._routeTripFuelWeight; + } + + return 0 < fuel; + } + + public async trySetRouteAlternateFuel(altFuel: string): Promise { + if (altFuel === Keypad.clrValue) { + this._routeAltFuelEntered = false; + return true; + } + if ( + !this.flightPlanService || + !this.flightPlanService.active || + !this.flightPlanService.active.alternateDestinationAirport + ) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + const value = NXUnits.userToKg(parseFloat(altFuel)); + if (isFinite(value)) { + if (this.isAltFuelInRange(value)) { + this._routeAltFuelEntered = true; + this._routeAltFuelWeight = value; + this._routeAltFuelTime = null; + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + public async trySetMinDestFob(fuel: string): Promise { + if (fuel === Keypad.clrValue) { + this._minDestFobEntered = false; + return true; + } + if (!this.representsDecimalNumber(fuel)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const value = NXUnits.userToKg(parseFloat(fuel)); + if (isFinite(value)) { + if (this.isMinDestFobInRange(value)) { + this._minDestFobEntered = true; + if (value < this._routeAltFuelWeight + this.getRouteFinalFuelWeight()) { + this.addMessageToQueue(NXSystemMessages.checkMinDestFob); + } + this._minDestFob = value; + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + public async tryUpdateAltDestination(altDestIdent: string): Promise { + if (!altDestIdent || altDestIdent === 'NONE' || altDestIdent === Keypad.clrValue) { + this.atsu.resetAtisAutoUpdate(); + this.flightPlanService.setAlternate(undefined); + this._DistanceToAlt = 0; + return true; + } + + const airportAltDest = await this.navigationDatabaseService.activeDatabase.searchAirport(altDestIdent); + if (airportAltDest) { + this.atsu.resetAtisAutoUpdate(); + await this.flightPlanService.setAlternate(altDestIdent); + this.tryUpdateDistanceToAlt(); + return true; + } + + this.setScratchpadMessage(NXSystemMessages.notInDatabase); + return false; + } + + /** + * Updates the Fuel weight cell to tons. Uses a place holder FL120 for 30 min + */ + public tryUpdateRouteFinalFuel() { + if (this._routeFinalFuelTime <= 0) { + this._routeFinalFuelTime = this._defaultRouteFinalTime; + } + this._routeFinalFuelWeight = A32NX_FuelPred.computeHoldingTrackFF(this.zeroFuelWeight, 120) / 1000; + this._rteFinalCoeffecient = A32NX_FuelPred.computeHoldingTrackFF(this.zeroFuelWeight, 120) / 30; + } + + /** + * Updates the alternate fuel and time values using a place holder FL of 330 until that can be set + */ + public tryUpdateRouteAlternate() { + if (this._DistanceToAlt < 20) { + this._routeAltFuelWeight = 0; + this._routeAltFuelTime = 0; + } else { + const placeholderFl = 120; + const airDistance = A32NX_FuelPred.computeAirDistance(Math.round(this._DistanceToAlt), this.averageWind); + + const deviation = + (this.zeroFuelWeight + this._routeFinalFuelWeight - A32NX_FuelPred.refWeight) * + A32NX_FuelPred.computeNumbers(airDistance, placeholderFl, A32NX_FuelPred.computations.CORRECTIONS, true); + if (20 < airDistance && airDistance < 200 && 100 < placeholderFl && placeholderFl < 290) { + //This will always be true until we can setup alternate routes + this._routeAltFuelWeight = + (A32NX_FuelPred.computeNumbers(airDistance, placeholderFl, A32NX_FuelPred.computations.FUEL, true) + + deviation) / + 1000; + this._routeAltFuelTime = this._routeAltFuelEntered + ? null + : A32NX_FuelPred.computeNumbers(airDistance, placeholderFl, A32NX_FuelPred.computations.TIME, true); + } + } + } + + /** + * Attempts to calculate trip information. Is dynamic in that it will use liveDistanceTo the destination rather than a + * static distance. Works down to 20NM airDistance and FL100 Up to 3100NM airDistance and FL390, anything out of those ranges and values + * won't be updated. + */ + public tryUpdateRouteTrip(_dynamic = false) { + // TODO Use static distance for `dynamic = false` (fms-v2) + const groundDistance = Number.isFinite(this.getDistanceToDestination()) ? this.getDistanceToDestination() : -1; + const airDistance = A32NX_FuelPred.computeAirDistance(groundDistance, this.averageWind); + + let altToUse = this.cruiseLevel; + // Use the cruise level for calculations otherwise after cruise use descent altitude down to 10,000 feet. + if (this.flightPhaseManager.phase >= FmgcFlightPhase.Descent) { + altToUse = SimVar.GetSimVarValue('PLANE ALTITUDE', 'Feet') / 100; + } + + if (20 <= airDistance && airDistance <= 3100 && 100 <= altToUse && altToUse <= 390) { + const deviation = + (this.zeroFuelWeight + this._routeFinalFuelWeight + this._routeAltFuelWeight - A32NX_FuelPred.refWeight) * + A32NX_FuelPred.computeNumbers(airDistance, altToUse, A32NX_FuelPred.computations.CORRECTIONS, false); + + this._routeTripFuelWeight = + (A32NX_FuelPred.computeNumbers(airDistance, altToUse, A32NX_FuelPred.computations.FUEL, false) + deviation) / + 1000; + this._routeTripTime = A32NX_FuelPred.computeNumbers( + airDistance, + altToUse, + A32NX_FuelPred.computations.TIME, + false, + ); + } + } + + public tryUpdateMinDestFob() { + this._minDestFob = this._routeAltFuelWeight + this.getRouteFinalFuelWeight(); + } + + public tryUpdateTOW() { + this.takeOffWeight = this.zeroFuelWeight + this.blockFuel - this.taxiFuelWeight; + } + + public tryUpdateLW() { + this.landingWeight = this.takeOffWeight - this._routeTripFuelWeight; + } + + /** + * Computes extra fuel + * @param useFOB - States whether to use the FOB rather than block fuel when computing extra fuel + */ + public tryGetExtraFuel(useFOB: boolean = false): number { + if (useFOB) { + return ( + this.getFOB() - + this.getTotalTripFuelCons() - + this._minDestFob - + this.taxiFuelWeight - + this.getRouteReservedWeight() + ); + } else { + return ( + this.blockFuel - + this.getTotalTripFuelCons() - + this._minDestFob - + this.taxiFuelWeight - + this.getRouteReservedWeight() + ); + } + } + + /**getRouteReservedWeight + * EXPERIMENTAL + * Attempts to calculate the extra time + */ + public tryGetExtraTime(useFOB = false) { + if (this.tryGetExtraFuel(useFOB) <= 0) { + return 0; + } + const tempWeight = this.getGW() - this._minDestFob; + const tempFFCoefficient = A32NX_FuelPred.computeHoldingTrackFF(tempWeight, 180) / 30; + return (this.tryGetExtraFuel(useFOB) * 1000) / tempFFCoefficient; + } + + public getRouteAltFuelWeight() { + return this._routeAltFuelWeight; + } + + public getRouteAltFuelTime() { + return this._routeAltFuelTime; + } + + //----------------------------------------------------------------------------------- + // TODO:FPM REWRITE: Start of functions to refactor + //----------------------------------------------------------------------------------- + + // FIXME remove A32NX_FM_LS_COURSE + private async updateIlsCourse() { + let course = -1; + const mmr = this.navigation.getNavaidTuner().getMmrRadioTuningStatus(1); + if (mmr.course !== null) { + course = mmr.course; + } else if (mmr.frequency !== null && SimVar.GetSimVarValue('L:A32NX_RADIO_RECEIVER_LOC_IS_VALID', 'number') === 1) { + course = SimVar.GetSimVarValue('NAV LOCALIZER:3', 'degrees'); + } + + return SimVar.SetSimVarValue('L:A32NX_FM_LS_COURSE', 'number', course); + } + + public async updateFlightNo(flightNo: string, callback = EmptyCallback.Boolean): Promise { + if (flightNo.length > 7) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return callback(false); + } + + this.flightNumber = flightNo; + await SimVar.SetSimVarValue('ATC FLIGHT NUMBER', 'string', flightNo, 'FMC'); + + // FIXME move ATSU code to ATSU + const code = await this.atsu.connectToNetworks(flightNo); + if (code !== AtsuStatusCodes.Ok) { + this.addNewAtsuMessage(code); + } + + return callback(true); + } + + public async updateCoRoute(coRouteNum, callback = EmptyCallback.Boolean) { + try { + if (coRouteNum.length > 2 && coRouteNum !== Keypad.clrValue) { + if (coRouteNum.length < 10) { + if (coRouteNum === 'NONE') { + this.resetCoroute(); + } else { + const { success, data } = await CompanyRoute.getCoRoute(coRouteNum); + if (success) { + this.coRoute['originIcao'] = data.origin.icao_code; + this.coRoute['destinationIcao'] = data.destination.icao_code; + this.coRoute['route'] = data.general.route; + if (data.alternate) { + this.coRoute['alternateIcao'] = data.alternate.icao_code; + } + this.coRoute['navlog'] = data.navlog.fix; + + // FIXME this whole thing is a mess. Create proper functions to create a CoRoute from whatever CompanyRoute.getCoRoute returns + // and untangle uplinks from route loading (to cater for database routes). + await CoRouteUplinkAdapter.uplinkFlightPlanFromCoRoute(this, this.flightPlanService, this.coRoute as any); + await this.flightPlanService.uplinkInsert(); + this.setGroundTempFromOrigin(); + + this.coRoute['routeNumber'] = coRouteNum; + } else { + this.setScratchpadMessage(NXSystemMessages.notInDatabase); + } + } + return callback(true); + } + } + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return callback(false); + } catch (error) { + console.error(`Error retrieving coroute from SimBridge ${error}`); + this.setScratchpadMessage(NXFictionalMessages.unknownDownlinkErr); + return callback(false); + } + } + + // FIXME bad name for something with no return value! + public async getCoRouteList(): Promise { + try { + const origin = this.flightPlanService.active.originAirport.ident; + const dest = this.flightPlanService.active.destinationAirport.ident; + const { success, data } = await CompanyRoute.getRouteList(origin, dest); + + if (success) { + data.forEach((route) => { + this.coRoute.routes.push({ + originIcao: route.origin.icao_code, + destinationIcao: route.destination.icao_code, + alternateIcao: route.alternate ? route.alternate.icao_code : undefined, + route: route.general.route, + navlog: route.navlog.fix, + routeName: route.name, + }); + }); + } else { + this.setScratchpadMessage(NXSystemMessages.notInDatabase); + } + } catch (error) { + console.info(`Error retrieving coroute list ${error}`); + } + } + + public getTotalTripTime() { + return this._routeTripTime; + } + + public getTotalTripFuelCons() { + return this._routeTripFuelWeight; + } + + public onUplinkInProgress() { + this.setScratchpadMessage(NXSystemMessages.uplinkInsertInProg); + } + + public onUplinkDone() { + this.removeMessageFromQueue(NXSystemMessages.uplinkInsertInProg.text); + this.setScratchpadMessage(NXSystemMessages.aocActFplnUplink); + } + + public deduplicateFacilities>(items: T[]): Promise { + if (items.length === 0) { + return undefined; + } + if (items.length === 1) { + return Promise.resolve(items[0]); + } + + return new Promise((resolve) => { + A320_Neo_CDU_SelectWptPage.ShowPage(this.mcdu, items, resolve); + }); + } + + /** + * Shows a scratchpad message based on the FMS error thrown + * @param type + */ + public showFmsErrorMessage(type: FmsErrorType) { + switch (type) { + case FmsErrorType.NotInDatabase: + this.setScratchpadMessage(NXSystemMessages.notInDatabase); + break; + case FmsErrorType.NotYetImplemented: + this.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + break; + case FmsErrorType.FormatError: + this.setScratchpadMessage(NXSystemMessages.formatError); + break; + case FmsErrorType.EntryOutOfRange: + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + break; + case FmsErrorType.ListOf99InUse: + this.setScratchpadMessage(NXSystemMessages.listOf99InUse); + break; + case FmsErrorType.AwyWptMismatch: + this.setScratchpadMessage(NXSystemMessages.awyWptMismatch); + break; + } + } + + public createNewWaypoint(ident: string): Promise { + return new Promise((resolve, reject) => { + CDUNewWaypoint.ShowPage( + this.mcdu, + (waypoint) => { + if (waypoint) { + resolve(waypoint.waypoint); + } else { + reject(); + } + }, + { ident }, + ); + }); + } + + public createLatLonWaypoint(coordinates, stored, ident = undefined) { + return this.dataManager.createLatLonWaypoint(coordinates, stored, ident); + } + + public createPlaceBearingPlaceBearingWaypoint(place1, bearing1, place2, bearing2, stored, ident = undefined) { + return this.dataManager.createPlaceBearingPlaceBearingWaypoint(place1, bearing1, place2, bearing2, stored, ident); + } + + public createPlaceBearingDistWaypoint(place, bearing, distance, stored, ident = undefined) { + return this.dataManager.createPlaceBearingDistWaypoint(place, bearing, distance, stored, ident); + } + + public getStoredWaypointsByIdent(ident) { + return this.dataManager.getStoredWaypointsByIdent(ident); + } + + //----------------------------------------------------------------------------------- + // TODO:FPM REWRITE: Start of functions to refactor + //----------------------------------------------------------------------------------- + + private _getOrSelectWaypoints( + getter: (ident: string) => Promise<(Fix | IlsNavaid)[]>, + ident: string, + callback: (fix: Fix | IlsNavaid) => void, + ) { + getter(ident).then((waypoints) => { + if (waypoints.length === 0) { + return callback(undefined); + } + if (waypoints.length === 1) { + return callback(waypoints[0]); + } + A320_Neo_CDU_SelectWptPage.ShowPage(this.mcdu, waypoints, callback); + }); + } + + public getOrSelectILSsByIdent(ident: string, callback: (navaid: IlsNavaid) => void): void { + this._getOrSelectWaypoints(this.navigationDatabase.searchIls.bind(this.navigationDatabase), ident, callback); + } + + public getOrSelectVORsByIdent(ident: string, callback: (navaid: VhfNavaid) => void): void { + this._getOrSelectWaypoints(this.navigationDatabase.searchVor.bind(this.navigationDatabase), ident, callback); + } + + public getOrSelectNDBsByIdent(ident: string, callback: (navaid: NdbNavaid) => void): void { + this._getOrSelectWaypoints(this.navigationDatabase.searchNdb.bind(this.navigationDatabase), ident, callback); + } + + public getOrSelectNavaidsByIdent( + ident: string, + callback: (navaid: EnrouteNdbNavaid | TerminalNdbNavaid | VhfNavaid) => void, + ): void { + this._getOrSelectWaypoints(this.navigationDatabase.searchAllNavaid.bind(this.navigationDatabase), ident, callback); + } + + /** + * This function only finds waypoints, not navaids. Some fixes may exist as a VOR and a waypoint in the database, this will only return the waypoint. + * Use @see WaypointEntryUtils.getOrCreateWaypoint instead if you don't want that + */ + public getOrSelectWaypointByIdent(ident: string, callback: (fix: Fix) => void): void { + this._getOrSelectWaypoints(this.navigationDatabase.searchWaypoint.bind(this.navigationDatabase), ident, callback); + } + + public insertWaypoint( + newWaypointTo, + fpIndex, + forAlternate, + index, + before = false, + callback = EmptyCallback.Boolean, + bypassTmpy = false, + ) { + if (newWaypointTo === '' || newWaypointTo === Keypad.clrValue) { + return callback(false); + } + try { + WaypointEntryUtils.getOrCreateWaypoint(this, newWaypointTo, true) + .then( + /** + * @param {Waypoint} waypoint + */ + (waypoint) => { + if (!waypoint) { + return callback(false); + } + if (bypassTmpy) { + if (fpIndex === FlightPlanIndex.Active && this.flightPlanService.hasTemporary) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return callback(false); + } + + if (before) { + this.flightPlanService + .insertWaypointBefore(index, waypoint, fpIndex, forAlternate) + .then(() => callback(true)); + } else { + this.flightPlanService.nextWaypoint(index, waypoint, fpIndex, forAlternate).then(() => callback(true)); + } + } else { + if (before) { + this.flightPlanService + .insertWaypointBefore(index, waypoint, fpIndex, forAlternate) + .then(() => callback(true)); + } else { + this.flightPlanService.nextWaypoint(index, waypoint, fpIndex, forAlternate).then(() => callback(true)); + } + } + }, + ) + .catch((err) => { + if (err.type !== undefined) { + this.showFmsErrorMessage(err.type); + } else if (err instanceof McduMessage) { + this.setScratchpadMessage(err); + } else if (err) { + console.error(err); + } + return callback(false); + }); + } catch (err) { + if (err.type !== undefined) { + this.showFmsErrorMessage(err.type); + } else if (err instanceof McduMessage) { + this.setScratchpadMessage(err); + } else { + console.error(err); + } + return callback(false); + } + } + + public toggleWaypointOverfly(index, fpIndex, forAlternate, callback = EmptyCallback.Void) { + if (this.flightPlanService.hasTemporary) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return callback(); + } + + this.flightPlanService.toggleOverfly(index, fpIndex, forAlternate); + callback(); + } + + public eraseTemporaryFlightPlan(callback = EmptyCallback.Void) { + if (this.flightPlanService.hasTemporary) { + this.flightPlanService.temporaryDelete(); + + SimVar.SetSimVarValue('L:FMC_FLIGHT_PLAN_IS_TEMPORARY', 'number', 0); + SimVar.SetSimVarValue('L:MAP_SHOW_TEMPORARY_FLIGHT_PLAN', 'number', 0); + callback(); + } else { + callback(); + } + } + + public insertTemporaryFlightPlan(callback = EmptyCallback.Void) { + if (this.flightPlanService.hasTemporary) { + const oldCostIndex = this.costIndex; + const oldDestination = this.currFlightPlanService.active.destinationAirport + ? this.currFlightPlanService.active.destinationAirport.ident + : undefined; + const oldCruiseLevel = this.cruiseLevel; + this.flightPlanService.temporaryInsert(); + this.checkCostIndex(oldCostIndex); + // FIXME I don't know if it is actually possible to insert TMPY with no FROM/TO, but we should not crash here, so check this for now + if (oldDestination !== undefined) { + this.checkDestination(oldDestination); + } + this.checkCruiseLevel(oldCruiseLevel); + + SimVar.SetSimVarValue('L:FMC_FLIGHT_PLAN_IS_TEMPORARY', 'number', 0); + SimVar.SetSimVarValue('L:MAP_SHOW_TEMPORARY_FLIGHT_PLAN', 'number', 0); + + this.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + callback(); + } + } + + private checkCostIndex(oldCostIndex) { + if (this.costIndex !== oldCostIndex) { + this.setScratchpadMessage(NXSystemMessages.usingCostIndex.getModifiedMessage(this.costIndex.toFixed(0))); + } + } + + private checkDestination(oldDestination) { + const newDestination = this.currFlightPlanService.active.destinationAirport.ident; + + // Enabling alternate or new DEST should sequence out of the GO AROUND phase + if (newDestination !== oldDestination) { + this.flightPhaseManager.handleNewDestinationAirportEntered(); + } + } + + private checkCruiseLevel(oldCruiseLevel) { + const newLevel = this.cruiseLevel; + // Keep simvar in sync for the flight phase manager + if (newLevel !== oldCruiseLevel) { + SimVar.SetSimVarValue( + 'L:A32NX_AIRLINER_CRUISE_ALTITUDE', + 'number', + Number.isFinite(newLevel * 100) ? newLevel * 100 : 0, + ); + } + } + + //----------------------------------------------------------------------------------- + // TODO:FPM REWRITE: End of functions to refactor + //----------------------------------------------------------------------------------- + + private vSpeedsValid() { + return ( + (!!this.v1Speed && !!this.vRSpeed ? this.v1Speed <= this.vRSpeed : true) && + (!!this.vRSpeed && !!this.v2Speed ? this.vRSpeed <= this.v2Speed : true) && + (!!this.v1Speed && !!this.v2Speed ? this.v1Speed <= this.v2Speed : true) + ); + } + + /** + * Gets the departure runway elevation in feet, if available. + * @returns departure runway elevation in feet, or null if not available. + */ + public getDepartureElevation() { + const activePlan = this.flightPlanService.active; + + let departureElevation = null; + if (activePlan.originRunway) { + departureElevation = activePlan.originRunway.thresholdLocation.alt; + } else if (activePlan.originAirport) { + departureElevation = activePlan.originAirport.location.alt; + } + + return departureElevation; + } + + /** + * Gets the gross weight, if available. + * Prior to engine start this is based on ZFW + Fuel entries, + * after engine start ZFW entry + FQI FoB. + * @returns {number | null} gross weight in tons or null if not available. + */ + public getGrossWeight() { + const fob = this.getFOB(); + + if (this.zeroFuelWeight === undefined || fob === undefined) { + return null; + } + + return this.zeroFuelWeight + fob; + } + + private getToSpeedsTooLow() { + const grossWeight = this.getGrossWeight(); + + if (this.flaps === null || grossWeight === null) { + return false; + } + + const departureElevation = this.getDepartureElevation(); + + const zp = + departureElevation !== null + ? this.getPressureAltAtElevation(departureElevation, this.getBaroCorrection1()) + : this.getPressureAlt(); + if (zp === null) { + return false; + } + + const tow = grossWeight - (this.isAnEngineOn() || this.taxiFuelWeight === undefined ? 0 : this.taxiFuelWeight); + + return ( + (this.v1Speed == null ? Infinity : this.v1Speed) < Math.trunc(NXSpeedsUtils.getVmcg(zp)) || + (this.vRSpeed == null ? Infinity : this.vRSpeed) < Math.trunc(1.05 * NXSpeedsUtils.getVmca(zp)) || + (this.v2Speed == null ? Infinity : this.v2Speed) < Math.trunc(1.1 * NXSpeedsUtils.getVmca(zp)) || + (isFinite(tow) && + (this.v2Speed == null ? Infinity : this.v2Speed) < + Math.trunc(1.13 * NXSpeedsUtils.getVs1g(tow, this.flaps, true))) + ); + } + + private toSpeedsChecks() { + const toSpeedsNotInserted = !this.v1Speed || !this.vRSpeed || !this.v2Speed; + if (toSpeedsNotInserted !== this.toSpeedsNotInserted) { + this.toSpeedsNotInserted = toSpeedsNotInserted; + } + + const toSpeedsTooLow = this.getToSpeedsTooLow(); + if (toSpeedsTooLow !== this.toSpeedsTooLow) { + this.toSpeedsTooLow = toSpeedsTooLow; + if (toSpeedsTooLow) { + this.addMessageToQueue(NXSystemMessages.toSpeedTooLow, () => !this.getToSpeedsTooLow()); + } + } + + const vSpeedDisagree = !this.vSpeedsValid(); + if (vSpeedDisagree !== this.vSpeedDisagree) { + this.vSpeedDisagree = vSpeedDisagree; + if (vSpeedDisagree) { + this.addMessageToQueue(NXSystemMessages.vToDisagree, this.vSpeedsValid.bind(this)); + } + } + + this.arincDiscreteWord3.setBitValue(16, vSpeedDisagree); + this.arincDiscreteWord3.setBitValue(17, toSpeedsTooLow); + this.arincDiscreteWord3.setBitValue(18, toSpeedsNotInserted); + this.arincDiscreteWord3.setSsm(Arinc429SignStatusMatrix.NormalOperation); + } + + public get v1Speed() { + return this.flightPlanService.active.performanceData.v1; + } + + public set v1Speed(speed) { + this.flightPlanService.setPerformanceData('v1', speed); + SimVar.SetSimVarValue('L:AIRLINER_V1_SPEED', 'knots', speed ? speed : NaN); + } + + public get vRSpeed() { + return this.flightPlanService.active.performanceData.vr; + } + + public set vRSpeed(speed) { + this.flightPlanService.setPerformanceData('vr', speed); + SimVar.SetSimVarValue('L:AIRLINER_VR_SPEED', 'knots', speed ? speed : NaN); + } + + public get v2Speed() { + return this.flightPlanService.active.performanceData.v2; + } + + public set v2Speed(speed) { + this.flightPlanService.setPerformanceData('v2', speed); + SimVar.SetSimVarValue('L:AIRLINER_V2_SPEED', 'knots', speed ? speed : NaN); + } + + public trySetV1Speed(s: string): boolean { + if (s === Keypad.clrValue) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + const v = parseInt(s); + if (!isFinite(v) || !/^\d{2,3}$/.test(s)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + if (v < 90 || v > 350) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + this.removeMessageFromQueue(NXSystemMessages.checkToData.text); + this.unconfirmedV1Speed = undefined; + this.v1Speed = v; + return true; + } + + public trySetVRSpeed(s: string): boolean { + if (s === Keypad.clrValue) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + const v = parseInt(s); + if (!isFinite(v) || !/^\d{2,3}$/.test(s)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + if (v < 90 || v > 350) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + this.removeMessageFromQueue(NXSystemMessages.checkToData.text); + this.unconfirmedVRSpeed = undefined; + this.vRSpeed = v; + return true; + } + + public trySetV2Speed(s: string): boolean { + if (s === Keypad.clrValue) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + const v = parseInt(s); + if (!isFinite(v) || !/^\d{2,3}$/.test(s)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + if (v < 90 || v > 350) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + this.removeMessageFromQueue(NXSystemMessages.checkToData.text); + this.unconfirmedV2Speed = undefined; + this.v2Speed = v; + return true; + } + + public trySetTakeOffTransAltitude(s: string): boolean { + if (s === Keypad.clrValue) { + this.flightPlanService.setPerformanceData('pilotTransitionAltitude', null); + this.updateTransitionAltitudeLevel(); + return true; + } + + let value = parseInt(s); + if (!isFinite(value) || !/^\d{4,5}$/.test(s)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + value = Math.round(value / 10) * 10; + if (value < 1000 || value > 45000) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.flightPlanService.setPerformanceData('pilotTransitionAltitude', value); + this.updateTransitionAltitudeLevel(); + return true; + } + + public async trySetThrustReductionAccelerationAltitude(s: string): Promise { + const plan = this.flightPlanService.active; + + if (this.flightPhaseManager.phase >= FmgcFlightPhase.Takeoff || !plan.originAirport) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + if (s === Keypad.clrValue) { + const hasDefaultThrRed = plan.performanceData.defaultThrustReductionAltitude !== null; + const hasDefaultAcc = plan.performanceData.defaultAccelerationAltitude !== null; + + if (hasDefaultThrRed && hasDefaultAcc) { + plan.setPerformanceData('pilotThrustReductionAltitude', null); + plan.setPerformanceData('pilotAccelerationAltitude', null); + return true; + } + + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + const match = s.match(/^(([0-9]{4,5})\/?)?(\/([0-9]{4,5}))?$/); + if (match === null || (match[2] === undefined && match[4] === undefined) || s.split('/').length > 2) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const thrRed = match[2] !== undefined ? MathUtils.round(parseInt(match[2]), 10) : null; + const accAlt = match[4] !== undefined ? MathUtils.round(parseInt(match[4]), 10) : null; + + const origin = this.flightPlanService.active.originAirport; + + let elevation = 0; + if (origin) { + elevation = origin.location.alt; + } + + const minimumAltitude = elevation + 400; + + const newThrRed = thrRed !== null ? thrRed : plan.performanceData.thrustReductionAltitude; + const newAccAlt = accAlt !== null ? accAlt : plan.performanceData.accelerationAltitude; + + if ( + (thrRed !== null && (thrRed < minimumAltitude || thrRed > 45000)) || + (accAlt !== null && (accAlt < minimumAltitude || accAlt > 45000)) || + (newThrRed !== null && newAccAlt !== null && thrRed > accAlt) + ) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + if (thrRed !== null) { + plan.setPerformanceData('pilotThrustReductionAltitude', thrRed); + } + + if (accAlt !== null) { + plan.setPerformanceData('pilotAccelerationAltitude', accAlt); + } + + return true; + } + + public async trySetEngineOutAcceleration(s: string): Promise { + const plan = this.flightPlanService.active; + + if (this.flightPhaseManager.phase >= FmgcFlightPhase.Takeoff || !plan.originAirport) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + if (s === Keypad.clrValue) { + const hasDefaultEngineOutAcc = plan.performanceData.defaultEngineOutAccelerationAltitude !== null; + + if (hasDefaultEngineOutAcc) { + plan.setPerformanceData('pilotEngineOutAccelerationAltitude', null); + return true; + } + + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + const match = s.match(/^([0-9]{4,5})$/); + if (match === null) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const accAlt = parseInt(match[1]); + + const origin = plan.originAirport; + const elevation = origin.location.alt !== undefined ? origin.location.alt : 0; + const minimumAltitude = elevation + 400; + + if (accAlt < minimumAltitude || accAlt > 45000) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + plan.setPerformanceData('pilotEngineOutAccelerationAltitude', accAlt); + + return true; + } + + public async trySetThrustReductionAccelerationAltitudeGoaround(s: string): Promise { + const plan = this.flightPlanService.active; + + if (this.flightPhaseManager.phase >= FmgcFlightPhase.GoAround || !plan.destinationAirport) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + if (s === Keypad.clrValue) { + const hasDefaultMissedThrRed = plan.performanceData.defaultMissedThrustReductionAltitude !== null; + const hasDefaultMissedAcc = plan.performanceData.defaultMissedAccelerationAltitude !== null; + + if (hasDefaultMissedThrRed && hasDefaultMissedAcc) { + plan.setPerformanceData('pilotMissedThrustReductionAltitude', null); + plan.setPerformanceData('pilotMissedAccelerationAltitude', null); + return true; + } + + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + const match = s.match(/^(([0-9]{4,5})\/?)?(\/([0-9]{4,5}))?$/); + if (match === null || (match[2] === undefined && match[4] === undefined) || s.split('/').length > 2) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const thrRed = match[2] !== undefined ? MathUtils.round(parseInt(match[2]), 10) : null; + const accAlt = match[4] !== undefined ? MathUtils.round(parseInt(match[4]), 10) : null; + + const destination = plan.destinationAirport; + const elevation = destination.location.alt !== undefined ? destination.location.alt : 0; + const minimumAltitude = elevation + 400; + + const newThrRed = thrRed !== null ? thrRed : plan.performanceData.missedThrustReductionAltitude; + const newAccAlt = accAlt !== null ? accAlt : plan.performanceData.missedAccelerationAltitude; + + if ( + (thrRed !== null && (thrRed < minimumAltitude || thrRed > 45000)) || + (accAlt !== null && (accAlt < minimumAltitude || accAlt > 45000)) || + (newThrRed !== null && newAccAlt !== null && thrRed > accAlt) + ) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + if (thrRed !== null) { + plan.setPerformanceData('pilotMissedThrustReductionAltitude', thrRed); + } + + if (accAlt !== null) { + plan.setPerformanceData('pilotMissedAccelerationAltitude', accAlt); + } + + return true; + } + + public async trySetEngineOutAccelerationAltitudeGoaround(s: string): Promise { + const plan = this.flightPlanService.active; + + if (this.flightPhaseManager.phase >= FmgcFlightPhase.GoAround || !plan.destinationAirport) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + if (s === Keypad.clrValue) { + const hasDefaultMissedEOAcc = plan.performanceData.defaultMissedEngineOutAccelerationAltitude !== null; + + if (hasDefaultMissedEOAcc) { + plan.setPerformanceData('pilotMissedEngineOutAccelerationAltitude', null); + return true; + } + + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + const match = s.match(/^([0-9]{4,5})$/); + if (match === null) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const accAlt = parseInt(match[1]); + + const destination = plan.destinationAirport; + const elevation = destination.location.alt !== undefined ? destination.location.alt : 0; + const minimumAltitude = elevation + 400; + + if (accAlt < minimumAltitude || accAlt > 45000) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + plan.setPerformanceData('pilotMissedEngineOutAccelerationAltitude', accAlt); + + return true; + } + + public thrustReductionAccelerationChecks() { + const activePlan = this.flightPlanService.active; + + if (activePlan.reconcileAccelerationWithConstraints()) { + this.addMessageToQueue( + NXSystemMessages.newAccAlt.getModifiedMessage(activePlan.performanceData.accelerationAltitude.toFixed(0)), + ); + } + + if (activePlan.reconcileThrustReductionWithConstraints()) { + this.addMessageToQueue( + NXSystemMessages.newThrRedAlt.getModifiedMessage(activePlan.performanceData.thrustReductionAltitude.toFixed(0)), + ); + } + } + + private updateThrustReductionAcceleration() { + const activePerformanceData = this.flightPlanService.active.performanceData; + + this.arincThrustReductionAltitude.setBnrValue( + activePerformanceData.thrustReductionAltitude !== null ? activePerformanceData.thrustReductionAltitude : 0, + activePerformanceData.thrustReductionAltitude !== null + ? Arinc429SignStatusMatrix.NormalOperation + : Arinc429SignStatusMatrix.NoComputedData, + 17, + 131072, + 0, + ); + this.arincAccelerationAltitude.setBnrValue( + activePerformanceData.accelerationAltitude !== null ? activePerformanceData.accelerationAltitude : 0, + activePerformanceData.accelerationAltitude !== null + ? Arinc429SignStatusMatrix.NormalOperation + : Arinc429SignStatusMatrix.NoComputedData, + 17, + 131072, + 0, + ); + this.arincEoAccelerationAltitude.setBnrValue( + activePerformanceData.engineOutAccelerationAltitude !== null + ? activePerformanceData.engineOutAccelerationAltitude + : 0, + activePerformanceData.engineOutAccelerationAltitude !== null + ? Arinc429SignStatusMatrix.NormalOperation + : Arinc429SignStatusMatrix.NoComputedData, + 17, + 131072, + 0, + ); + + this.arincMissedThrustReductionAltitude.setBnrValue( + activePerformanceData.missedThrustReductionAltitude !== null + ? activePerformanceData.missedThrustReductionAltitude + : 0, + activePerformanceData.missedThrustReductionAltitude !== null + ? Arinc429SignStatusMatrix.NormalOperation + : Arinc429SignStatusMatrix.NoComputedData, + 17, + 131072, + 0, + ); + this.arincMissedAccelerationAltitude.setBnrValue( + activePerformanceData.missedAccelerationAltitude !== null ? activePerformanceData.missedAccelerationAltitude : 0, + activePerformanceData.missedAccelerationAltitude !== null + ? Arinc429SignStatusMatrix.NormalOperation + : Arinc429SignStatusMatrix.NoComputedData, + 17, + 131072, + 0, + ); + this.arincMissedEoAccelerationAltitude.setBnrValue( + activePerformanceData.missedEngineOutAccelerationAltitude !== null + ? activePerformanceData.missedEngineOutAccelerationAltitude + : 0, + activePerformanceData.missedEngineOutAccelerationAltitude !== null + ? Arinc429SignStatusMatrix.NormalOperation + : Arinc429SignStatusMatrix.NoComputedData, + 17, + 131072, + 0, + ); + } + + private updateTransitionAltitudeLevel() { + const originTransitionAltitude = this.getOriginTransitionAltitude(); + this.arincTransitionAltitude.setBnrValue( + originTransitionAltitude !== null ? originTransitionAltitude : 0, + originTransitionAltitude !== null + ? Arinc429SignStatusMatrix.NormalOperation + : Arinc429SignStatusMatrix.NoComputedData, + 17, + 131072, + 0, + ); + + const destinationTansitionLevel = this.getDestinationTransitionLevel(); + this.arincTransitionLevel.setBnrValue( + destinationTansitionLevel !== null ? destinationTansitionLevel : 0, + destinationTansitionLevel !== null + ? Arinc429SignStatusMatrix.NormalOperation + : Arinc429SignStatusMatrix.NoComputedData, + 9, + 512, + 0, + ); + } + + public setPerfTOFlexTemp(s: string): boolean { + if (s === Keypad.clrValue) { + this.perfTOTemp = NaN; + // In future we probably want a better way of checking this, as 0 is + // in the valid flex temperature range (-99 to 99). + SimVar.SetSimVarValue('L:A32NX_AIRLINER_TO_FLEX_TEMP', 'Number', 0); + return true; + } + let value = parseInt(s); + if (!isFinite(value) || !/^[+-]?\d{1,2}$/.test(s)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + if (value < -99 || value > 99) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + // As the sim uses 0 as a sentinel value to detect that no flex + // temperature is set, we'll just use 0.1 as the actual value for flex 0 + // and make sure we never display it with decimals. + if (value === 0) { + value = 0.1; + } + this.perfTOTemp = value; + SimVar.SetSimVarValue('L:A32NX_AIRLINER_TO_FLEX_TEMP', 'Number', value); + return true; + } + + /** + * Attempts to predict required block fuel for trip + */ + //TODO: maybe make this part of an update routine? + public tryFuelPlanning(): boolean { + if (this._fuelPlanningPhase === FuelPlanningPhases.IN_PROGRESS) { + this._blockFuelEntered = true; + this._fuelPlanningPhase = FuelPlanningPhases.COMPLETED; + return true; + } + const tempRouteFinalFuelTime = this._routeFinalFuelTime; + this.tryUpdateRouteFinalFuel(); + this.tryUpdateRouteAlternate(); + this.tryUpdateRouteTrip(); + + this._routeFinalFuelTime = tempRouteFinalFuelTime; + this._routeFinalFuelWeight = (this._routeFinalFuelTime * this._rteFinalCoeffecient) / 1000; + + this.tryUpdateMinDestFob(); + + this.blockFuel = + this.getTotalTripFuelCons() + this._minDestFob + this.taxiFuelWeight + this.getRouteReservedWeight(); + this._fuelPlanningPhase = FuelPlanningPhases.IN_PROGRESS; + return true; + } + + public trySetTaxiFuelWeight(s: string): boolean { + if (s === Keypad.clrValue) { + this.taxiFuelWeight = this._defaultTaxiFuelWeight; + this._taxiEntered = false; + return true; + } + if (!this.representsDecimalNumber(s)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + const value = NXUnits.userToKg(parseFloat(s)); + if (isFinite(value)) { + if (this.isTaxiFuelInRange(value)) { + this._taxiEntered = true; + this.taxiFuelWeight = value; + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + public getRouteFinalFuelWeight() { + if (isFinite(this._routeFinalFuelWeight)) { + this._routeFinalFuelWeight = (this._routeFinalFuelTime * this._rteFinalCoeffecient) / 1000; + return this._routeFinalFuelWeight; + } + } + + public getRouteFinalFuelTime() { + return this._routeFinalFuelTime; + } + + /** + * This method is used to set initial Final Time for when INIT B is making predictions + * @param {String} s - containing time value + * @returns {boolean} + */ + public async trySetRouteFinalTime(s: string): Promise { + if (s) { + if (s === Keypad.clrValue) { + this._routeFinalFuelTime = this._routeFinalFuelTimeDefault; + this._rteFinalWeightEntered = false; + this._rteFinalTimeEntered = false; + return true; + } + // Time entry must start with '/' + if (s.startsWith('/')) { + const rteFinalTime = s.slice(1); + + if (!/^\d{1,4}$/.test(rteFinalTime)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + if (this.isFinalTimeInRange(rteFinalTime)) { + this._rteFinalWeightEntered = false; + this._rteFinalTimeEntered = true; + this._routeFinalFuelTime = FmsFormatters.hhmmToMinutes(rteFinalTime.padStart(4, '0')); + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } + } + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + public async trySetRouteFinalFuel(s: string): Promise { + if (s === Keypad.clrValue) { + this._routeFinalFuelTime = this._routeFinalFuelTimeDefault; + this._rteFinalWeightEntered = false; + this._rteFinalTimeEntered = false; + return true; + } + if (s) { + // Time entry must start with '/' + if (s.startsWith('/')) { + return this.trySetRouteFinalTime(s); + } else { + // If not time, try to parse as weight + // Weight can be entered with optional trailing slash, if so remove it before parsing the value + const enteredValue = s.endsWith('/') ? s.slice(0, -1) : s; + + if (!this.representsDecimalNumber(enteredValue)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const rteFinalWeight = NXUnits.userToKg(parseFloat(enteredValue)); + + if (this.isFinalFuelInRange(rteFinalWeight)) { + this._rteFinalWeightEntered = true; + this._rteFinalTimeEntered = false; + this._routeFinalFuelWeight = rteFinalWeight; + this._routeFinalFuelTime = (rteFinalWeight * 1000) / this._rteFinalCoeffecient; + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } + } + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + public getRouteReservedWeight() { + if (this.isFlying()) { + return 0; + } + if (!this.routeReservedEntered() && this._rteFinalCoeffecient !== 0) { + const fivePercentWeight = (this._routeReservedPercent * this._routeTripFuelWeight) / 100; + const fiveMinuteHoldingWeight = (5 * this._rteFinalCoeffecient) / 1000; + + return fivePercentWeight > fiveMinuteHoldingWeight ? fivePercentWeight : fiveMinuteHoldingWeight; + } + if (isFinite(this._routeReservedWeight) && this._routeReservedWeight !== 0) { + return this._routeReservedWeight; + } else { + return (this._routeReservedPercent * this._routeTripFuelWeight) / 100; + } + } + + public getRouteReservedPercent() { + if (this.isFlying()) { + return 0; + } + if (isFinite(this._routeReservedWeight) && isFinite(this.blockFuel) && this._routeReservedWeight !== 0) { + return (this._routeReservedWeight / this._routeTripFuelWeight) * 100; + } + return this._routeReservedPercent; + } + + public trySetRouteReservedPercent(s: string): boolean { + if (!this.isFlying()) { + if (s) { + if (s === Keypad.clrValue) { + this._rteReservedWeightEntered = false; + this._rteReservedPctEntered = false; + this._routeReservedWeight = 0; + this._routeReservedPercent = 5; + this._rteRsvPercentOOR = false; + return true; + } + // Percentage entry must start with '/' + if (s.startsWith('/')) { + const enteredValue = s.slice(1); + + if (!this.representsDecimalNumber(enteredValue)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const rteRsvPercent = parseFloat(enteredValue); + + if (!this.isRteRsvPercentInRange(rteRsvPercent)) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this._rteRsvPercentOOR = false; + this._rteReservedPctEntered = true; + this._rteReservedWeightEntered = false; + + if (isFinite(rteRsvPercent)) { + this._routeReservedWeight = NaN; + this._routeReservedPercent = rteRsvPercent; + return true; + } + } + } + } + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + /** + * Checks input and passes to trySetCruiseFl() + * @param input Altitude or FL + * @returns input passed checks + */ + public trySetCruiseFlCheckInput(input: string): boolean { + if (input === Keypad.clrValue) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + const flString = input.replace('FL', ''); + if (!flString) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + return this.trySetCruiseFl(parseFloat(flString)); + } + + /** + * Sets new Cruise FL if all conditions good + * @param fl Altitude or FL + * @returns input passed checks + */ + private trySetCruiseFl(fl: number): boolean { + if (!isFinite(fl)) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + if (fl >= 1000) { + fl = Math.floor(fl / 100); + } + if (fl > this.maxCruiseFL) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + const phase = this.flightPhaseManager.phase; + const selFl = Math.floor(Math.max(0, Simplane.getAutoPilotDisplayedAltitudeLockValue('feet')) / 100); + if ( + fl < selFl && + (phase === FmgcFlightPhase.Climb || phase === FmgcFlightPhase.Approach || phase === FmgcFlightPhase.GoAround) + ) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + if (fl <= 0 || fl > this.maxCruiseFL) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.cruiseLevel = fl; + this.onUpdateCruiseLevel(fl); + + return true; + } + + private onUpdateCruiseLevel(newCruiseLevel) { + this.cruiseTemperature = undefined; + this.updateConstraints(); + + this.flightPhaseManager.handleNewCruiseAltitudeEntered(newCruiseLevel); + } + + public getCruiseAltitude(): number { + return this.cruiseLevel * 100; + } + + public trySetRouteReservedFuel(s: string): boolean { + if (!this.isFlying()) { + if (s) { + if (s === Keypad.clrValue) { + this._rteReservedWeightEntered = false; + this._rteReservedPctEntered = false; + this._routeReservedWeight = 0; + this._routeReservedPercent = 5; + this._rteRsvPercentOOR = false; + return true; + } + // Percentage entry must start with '/' + if (s.startsWith('/')) { + return this.trySetRouteReservedPercent(s); + } else { + // If not percentage, try to parse as weight + // Weight can be entered with optional trailing slash, if so remove it before parsing the value + const enteredValue = s.endsWith('/') ? s.slice(0, -1) : s; + + if (!this.representsDecimalNumber(enteredValue)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const rteRsvWeight = NXUnits.userToKg(parseFloat(enteredValue)); + + if (!this.isRteRsvFuelInRange(rteRsvWeight)) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this._rteReservedWeightEntered = true; + this._rteReservedPctEntered = false; + + if (isFinite(rteRsvWeight)) { + this._routeReservedWeight = rteRsvWeight; + this._routeReservedPercent = 0; + + if (!this.isRteRsvPercentInRange(this.getRouteReservedPercent())) { + // Bit of a hacky method due previous tight coupling of weight and percentage calculations + this._rteRsvPercentOOR = true; + } + + return true; + } + } + } + } + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + public trySetZeroFuelWeightZFWCG(s: string): boolean { + if (s) { + if (s.includes('/')) { + const sSplit = s.split('/'); + const zfw = NXUnits.userToKg(parseFloat(sSplit[0])); + const zfwcg = parseFloat(sSplit[1]); + if (isFinite(zfw) && isFinite(zfwcg)) { + if (this.isZFWInRange(zfw) && this.isZFWCGInRange(zfwcg)) { + this._zeroFuelWeightZFWCGEntered = true; + this.zeroFuelWeight = zfw; + this.zeroFuelWeightMassCenter = zfwcg; + return true; + } + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + if (!this._zeroFuelWeightZFWCGEntered) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + if (this.isZFWInRange(zfw)) { + this.zeroFuelWeight = zfw; + return true; + } + if (this.isZFWCGInRange(zfwcg)) { + this.zeroFuelWeightMassCenter = zfwcg; + return true; + } + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + if (!this._zeroFuelWeightZFWCGEntered) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + const zfw = NXUnits.userToKg(parseFloat(s)); + if (this.isZFWInRange(zfw)) { + this.zeroFuelWeight = zfw; + return true; + } + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + /** + * + * @returns {number} Returns estimated fuel on board when arriving at the destination + */ + public getDestEFOB(useFOB = false) { + return (useFOB ? this.getFOB() : this.blockFuel) - this._routeTripFuelWeight - this.taxiFuelWeight; + } + + /** + * @returns {number} Returns EFOB when arriving at the alternate dest + */ + public getAltEFOB(useFOB = false) { + return this.getDestEFOB(useFOB) - this._routeAltFuelWeight; + } + + public trySetBlockFuel(s: string): boolean { + if (s === Keypad.clrValue) { + this.blockFuel = undefined; + this._blockFuelEntered = false; + this._fuelPredDone = false; + this._fuelPlanningPhase = FuelPlanningPhases.PLANNING; + return true; + } + const value = NXUnits.userToKg(parseFloat(s)); + if (isFinite(value) && this.isBlockFuelInRange(value)) { + if (this.isBlockFuelInRange(value)) { + this.blockFuel = value; + this._blockFuelEntered = true; + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + public trySetAverageWind(s: string): boolean { + const validDelims = ['TL', 'T', '+', 'HD', 'H', '-']; + const matchedIndex = validDelims.findIndex((element) => s.startsWith(element)); + const digits = matchedIndex >= 0 ? s.replace(validDelims[matchedIndex], '') : s; + const isNum = /^\d+$/.test(digits); + if (!isNum) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + const wind = parseInt(digits); + if (wind > 250) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + this.averageWind = matchedIndex <= 2 ? wind : -wind; + return true; + } + + public trySetPreSelectedClimbSpeed(s: string): boolean { + const isNextPhase = this.flightPhaseManager.phase === FmgcFlightPhase.Takeoff; + if (s === Keypad.clrValue) { + this.preSelectedClbSpeed = undefined; + if (isNextPhase) { + this.updatePreSelSpeedMach(undefined); + } + return true; + } + + const SPD_REGEX = /\d{1,3}/; + if (s.match(SPD_REGEX) === null) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const spd = parseInt(s); + if (!Number.isFinite(spd)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + if (spd < 100 || spd > 350) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.preSelectedClbSpeed = spd; + if (isNextPhase) { + this.updatePreSelSpeedMach(spd); + } + + return true; + } + + public trySetPreSelectedCruiseSpeed(s: string): boolean { + const isNextPhase = this.flightPhaseManager.phase === FmgcFlightPhase.Climb; + if (s === Keypad.clrValue) { + this.preSelectedCrzSpeed = undefined; + if (isNextPhase) { + this.updatePreSelSpeedMach(undefined); + } + return true; + } + + const MACH_OR_SPD_REGEX = /^(\.\d{1,2}|\d{1,3})$/; + if (s.match(MACH_OR_SPD_REGEX) === null) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const v = parseFloat(s); + if (!Number.isFinite(v)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + if (v < 1) { + const mach = Math.round(v * 100) / 100; + if (mach < 0.15 || mach > 0.82) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.preSelectedCrzSpeed = mach; + } else { + const spd = Math.round(v); + if (spd < 100 || spd > 350) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.preSelectedCrzSpeed = spd; + } + + if (isNextPhase) { + this.updatePreSelSpeedMach(this.preSelectedCrzSpeed); + } + + return true; + } + + public setPerfApprQNH(s: string): boolean { + if (s === Keypad.clrValue) { + const dest = this.flightPlanService.active.destinationAirport; + const distanceToDestination = Number.isFinite(this.getDistanceToDestination()) + ? this.getDistanceToDestination() + : -1; + + if (dest && distanceToDestination < 180) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } else { + this.perfApprQNH = NaN; + return true; + } + } + + const value = parseFloat(s); + const HPA_REGEX = /^[01]?[0-9]{3}$/; + const INHG_REGEX = /^([23][0-9]|[0-9]{2}\.)[0-9]{2}$/; + + if (HPA_REGEX.test(s)) { + if (value >= 745 && value <= 1050) { + this.perfApprQNH = value; + SimVar.SetSimVarValue('L:A32NX_DESTINATION_QNH', 'Millibar', this.perfApprQNH); + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } else if (INHG_REGEX.test(s)) { + if (value >= 2200 && value <= 3100) { + this.perfApprQNH = value / 100; + SimVar.SetSimVarValue('L:A32NX_DESTINATION_QNH', 'Millibar', this.perfApprQNH * 33.8639); + return true; + } else if (value >= 22.0 && value <= 31.0) { + this.perfApprQNH = value; + SimVar.SetSimVarValue('L:A32NX_DESTINATION_QNH', 'Millibar', this.perfApprQNH * 33.8639); + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + public setPerfApprTemp(s: string): boolean { + if (s === Keypad.clrValue) { + const dest = this.flightPlanService.active.destinationAirport; + const distanceToDestination = Number.isFinite(this.getDistanceToDestination()) + ? this.getDistanceToDestination() + : -1; + + if (dest && distanceToDestination < 180) { + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } else { + this.perfApprTemp = NaN; + return true; + } + } + + if (!/^[+-]?\d{1,2}$/.test(s)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + this.perfApprTemp = parseInt(s); + return true; + } + + public setPerfApprWind(s: string): boolean { + if (s === Keypad.clrValue) { + this.perfApprWindHeading = NaN; + this.perfApprWindSpeed = NaN; + return true; + } + + // both must be entered + if (!/^\d{1,3}\/\d{1,3}$/.test(s)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + const [dir, mag] = s.split('/').map((v) => parseInt(v)); + if (dir > 360 || mag > 500) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + this.perfApprWindHeading = dir % 360; // 360 is displayed as 0 + this.perfApprWindSpeed = mag; + return true; + } + + public setPerfApprTransAlt(s: string): boolean { + if (s === Keypad.clrValue) { + this.flightPlanService.setPerformanceData('pilotTransitionLevel', null); + this.updateTransitionAltitudeLevel(); + return true; + } + + if (!/^\d{4,5}$/.test(s)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + const value = Math.round(parseInt(s) / 10) * 10; + if (value < 1000 || value > 45000) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.flightPlanService.setPerformanceData('pilotTransitionLevel', Math.round(value / 100)); + this.updateTransitionAltitudeLevel(); + return true; + } + + /** + * VApp for _selected_ landing config + */ + private getVApp() { + if (isFinite(this.vApp)) { + return this.vApp; + } + return this.approachSpeeds.vapp; + } + + /** + * VApp for _selected_ landing config with GSMini correction + */ + private getVAppGsMini() { + let vAppTarget = this.getVApp(); + if (isFinite(this.perfApprWindSpeed) && isFinite(this.perfApprWindHeading)) { + vAppTarget = NXSpeedsUtils.getVtargetGSMini(vAppTarget, NXSpeedsUtils.getHeadWindDiff(this._towerHeadwind)); + } + return vAppTarget; + } + + public setPerfApprVApp(s: string): boolean { + if (s === Keypad.clrValue) { + if (isFinite(this.vApp)) { + this.vApp = NaN; + return true; + } + } else { + if (s.includes('.')) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + const value = parseInt(s); + if (isFinite(value) && value >= 90 && value <= 350) { + this.vApp = value; + return true; + } + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + this.setScratchpadMessage(NXSystemMessages.notAllowed); + return false; + } + + /** + * Tries to estimate the landing weight at destination + * NaN on failure + */ + public tryEstimateLandingWeight() { + const altActive = false; + const landingWeight = this.zeroFuelWeight + (altActive ? this.getAltEFOB(true) : this.getDestEFOB(true)); + return isFinite(landingWeight) ? landingWeight : NaN; + } + + public setPerfApprMDA(s: string): boolean { + if (s === Keypad.clrValue) { + this.perfApprMDA = null; + SimVar.SetSimVarValue('L:AIRLINER_MINIMUM_DESCENT_ALTITUDE', 'feet', 0); + return true; + } else if (s.match(/^[0-9]{1,5}$/) !== null) { + const value = parseInt(s); + + const activePlan = this.flightPlanService.active; + + let ldgRwy = activePlan.destinationRunway; + + if (!ldgRwy) { + if (activePlan.availableDestinationRunways.length > 0) { + ldgRwy = activePlan.availableDestinationRunways[0]; + } + } + + const limitLo = ldgRwy ? ldgRwy.thresholdLocation.alt : 0; + const limitHi = ldgRwy ? ldgRwy.thresholdLocation.alt + 5000 : 39000; + + if (value >= limitLo && value <= limitHi) { + this.perfApprMDA = value; + SimVar.SetSimVarValue('L:AIRLINER_MINIMUM_DESCENT_ALTITUDE', 'feet', this.perfApprMDA); + return true; + } + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } else { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + } + + public setPerfApprDH(s: string): boolean { + if (s === Keypad.clrValue) { + this.perfApprDH = null; + return true; + } + + if (s === 'NO' || s === 'NO DH' || s === 'NODH') { + this.perfApprDH = 'NO DH'; + SimVar.SetSimVarValue('L:AIRLINER_DECISION_HEIGHT', 'feet', -2); + return true; + } else if (s.match(/^[0-9]{1,5}$/) !== null) { + const value = parseInt(s); + if (value >= 0 && value <= 5000) { + this.perfApprDH = value; + SimVar.SetSimVarValue('L:AIRLINER_DECISION_HEIGHT', 'feet', this.perfApprDH); + return true; + } else { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + } else { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + } + + public setPerfApprFlaps3(s: boolean) { + this.perfApprFlaps3 = s; + SimVar.SetSimVarValue('L:A32NX_SPEEDS_LANDING_CONF3', 'boolean', s); + } + + /** @param icao ID of the navaid to de-select */ + public deselectNavaid(icao: string): void { + this.navigation.getNavaidTuner().deselectNavaid(icao); + } + + public reselectNavaid(icao: string): void { + this.navigation.getNavaidTuner().reselectNavaid(icao); + } + + /** @returns icaos of deselected navaids */ + public get deselectedNavaids(): string[] { + return this.navigation.getNavaidTuner().deselectedNavaids; + } + + public getVorTuningData(index: 1 | 2): VorRadioTuningStatus { + return this.navigation.getNavaidTuner().getVorRadioTuningStatus(index); + } + + /** + * Set a manually tuned VOR + * @param index + * @param facilityOrFrequency null to clear + */ + public setManualVor(index: 1 | 2, facilityOrFrequency: number | VhfNavaid | null): void { + return this.navigation.getNavaidTuner().setManualVor(index, facilityOrFrequency); + } + + /** + * Set a VOR course + * @param index + * @param course null to clear + */ + public setVorCourse(index: 1 | 2, course: number): void { + return this.navigation.getNavaidTuner().setVorCourse(index, course); + } + + public getMmrTuningData(index: 1 | 2): MmrRadioTuningStatus { + return this.navigation.getNavaidTuner().getMmrRadioTuningStatus(index); + } + + /** + * Set a manually tuned ILS + * @param facilityOrFrequency null to clear + */ + public async setManualIls(facilityOrFrequency: number | IlsNavaid | null): Promise { + return await this.navigation.getNavaidTuner().setManualIls(facilityOrFrequency); + } + + /** + * Set an ILS course + * @param course null to clear + * @param backcourse Whether the course is a backcourse/backbeam. + */ + public setIlsCourse(course: number | null, backcourse = false): void { + return this.navigation.getNavaidTuner().setIlsCourse(course, backcourse); + } + + public getAdfTuningData(index: 1 | 2): AdfRadioTuningStatus { + return this.navigation.getNavaidTuner().getAdfRadioTuningStatus(index); + } + + /** + * Set a manually tuned NDB + * @param index + * @param facilityOrFrequency null to clear + */ + public setManualAdf(index: 1 | 2, facilityOrFrequency: number | NdbNavaid | null): void { + return this.navigation.getNavaidTuner().setManualAdf(index, facilityOrFrequency); + } + + public isMmrTuningLocked() { + return this.navigation.getNavaidTuner().isMmrTuningLocked(); + } + + public isFmTuningActive() { + return this.navigation.getNavaidTuner().isFmTuningActive(); + } + + /** + * Get the currently selected navaids + */ + public getSelectedNavaids(): SelectedNavaid[] { + // FIXME 2 when serving CDU 2 + return this.navigation.getSelectedNavaids(1); + } + + /** + * Set the takeoff flap config + */ + private setTakeoffFlaps(flaps: 0 | 1 | 2 | 3 | null): void { + if (flaps !== this.flaps) { + this.flaps = flaps; + SimVar.SetSimVarValue('L:A32NX_TO_CONFIG_FLAPS', 'number', this.flaps !== null ? this.flaps : -1); + + this.arincDiscreteWord2.setBitValue(13, this.flaps === 0); + this.arincDiscreteWord2.setBitValue(14, this.flaps === 1); + this.arincDiscreteWord2.setBitValue(15, this.flaps === 2); + this.arincDiscreteWord2.setBitValue(16, this.flaps === 3); + this.arincDiscreteWord2.setSsm(Arinc429SignStatusMatrix.NormalOperation); + } + } + + /** + * Set the takeoff trim config + */ + private setTakeoffTrim(ths: number | null): void { + if (ths !== this.ths) { + this.ths = ths; + // legacy vars + SimVar.SetSimVarValue('L:A32NX_TO_CONFIG_THS', 'degree', this.ths ? this.ths : 0); + SimVar.SetSimVarValue('L:A32NX_TO_CONFIG_THS_ENTERED', 'bool', this.ths !== null); + + const ssm = + this.ths !== null ? Arinc429SignStatusMatrix.NormalOperation : Arinc429SignStatusMatrix.NoComputedData; + + this.arincTakeoffPitchTrim.setBnrValue(this.ths ? -this.ths : 0, ssm, 12, 180, -180); + } + } + + public trySetFlapsTHS(s: string): boolean { + if (s === Keypad.clrValue) { + this.setTakeoffFlaps(null); + this.setTakeoffTrim(null); + this.tryCheckToData(); + return true; + } + + let newFlaps = null; + let newThs = null; + + // eslint-disable-next-line prefer-const + let [flapStr, thsStr] = s.split('/'); + + if (flapStr && flapStr.length > 0) { + if (!/^\d$/.test(flapStr)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const flaps = parseInt(flapStr); + if (flaps < 0 || flaps > 3) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + newFlaps = flaps; + } + + if (thsStr && thsStr.length > 0) { + // allow AAN.N and N.NAA, where AA is UP or DN + if (!/^(UP|DN)(\d|\d?\.\d|\d\.\d?)|(\d|\d?\.\d|\d\.\d?)(UP|DN)$/.test(thsStr)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + let direction = null; + thsStr = thsStr.replace(/(UP|DN)/g, (substr) => { + direction = substr; + return ''; + }); + + if (direction) { + let ths = parseFloat(thsStr); + if (direction === 'DN') { + // Note that 0 *= -1 will result in -0, which is strictly + // the same as 0 (that is +0 === -0) and doesn't make a + // difference for the calculation itself. However, in order + // to differentiate between DN0.0 and UP0.0 we'll do check + // later when displaying this value using Object.is to + // determine whether the pilot entered DN0.0 or UP0.0. + ths *= -1; + } + if (!isFinite(ths) || ths < -5 || ths > 7) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + newThs = ths; + } + } + + if (newFlaps !== null) { + if (this.flaps !== null) { + this.tryCheckToData(); + } + this.setTakeoffFlaps(newFlaps); + } + if (newThs !== null) { + if (this.ths !== null) { + this.tryCheckToData(); + } + this.setTakeoffTrim(newThs); + } + return true; + } + + public checkEFOBBelowMin() { + if (this._fuelPredDone) { + if (!this._minDestFobEntered) { + this.tryUpdateMinDestFob(); + } + + if (this._minDestFob) { + // round & only use 100kgs precision since thats how it is displayed in fuel pred + const destEfob = Math.round(this.getDestEFOB(this.isAnEngineOn()) * 10) / 10; + const roundedMinDestFob = Math.round(this._minDestFob * 10) / 10; + if (!this._isBelowMinDestFob) { + if (destEfob < roundedMinDestFob) { + this._isBelowMinDestFob = true; + // TODO should be in flight only and if fuel is below min dest efob for 2 minutes + if (this.isAnEngineOn()) { + setTimeout(() => { + this.addMessageToQueue( + NXSystemMessages.destEfobBelowMin, + () => { + return this._EfobBelowMinClr === true; + }, + () => { + this._EfobBelowMinClr = true; + }, + ); + }, 120000); + } else { + this.addMessageToQueue( + NXSystemMessages.destEfobBelowMin, + () => { + return this._EfobBelowMinClr === true; + }, + () => { + this._EfobBelowMinClr = true; + }, + ); + } + } + } else { + // check if we are at least 300kgs above min dest efob to show green again & the ability to trigger the message + if (roundedMinDestFob) { + if (destEfob - roundedMinDestFob >= 0.3) { + this._isBelowMinDestFob = false; + this.removeMessageFromQueue(NXSystemMessages.destEfobBelowMin.text); + } + } + } + } + } + } + + public updateTowerHeadwind() { + if (isFinite(this.perfApprWindSpeed) && isFinite(this.perfApprWindHeading)) { + const activePlan = this.flightPlanService.active; + + if (activePlan.destinationRunway) { + this._towerHeadwind = NXSpeedsUtils.getHeadwind( + this.perfApprWindSpeed, + this.perfApprWindHeading, + activePlan.destinationRunway.magneticBearing, + ); + } + } + } + + /** + * Called after Flaps or THS change + */ + private tryCheckToData() { + if (isFinite(this.v1Speed) || isFinite(this.vRSpeed) || isFinite(this.v2Speed)) { + this.addMessageToQueue(NXSystemMessages.checkToData); + } + } + + /** + * Called after runway change + * - Sets confirmation prompt state for every entry whether it is defined or not + * - Adds message when at least one entry needs to be confirmed + * Additional: + * Only prompt the confirmation of FLEX TEMP when the TO runway was changed, not on initial insertion of the runway + */ + public onToRwyChanged() { + const activePlan = this.flightPlanService.active; + const selectedRunway = activePlan.originRunway; + + if (selectedRunway) { + const toRunway = Avionics.Utils.formatRunway(selectedRunway.ident); + if (toRunway === this.toRunway) { + return; + } + if (this.toRunway) { + this.toRunway = toRunway; + this._toFlexChecked = !isFinite(this.perfTOTemp); + this.unconfirmedV1Speed = this.v1Speed; + this.unconfirmedVRSpeed = this.vRSpeed; + this.unconfirmedV2Speed = this.v2Speed; + this.v1Speed = undefined; + this.vRSpeed = undefined; + this.v2Speed = undefined; + + if (!this.unconfirmedV1Speed && !this.unconfirmedVRSpeed && !this.unconfirmedV2Speed) { + return; + } + this.addMessageToQueue( + NXSystemMessages.checkToData, + (mcdu) => + !this.unconfirmedV1Speed && !this.unconfirmedVRSpeed && !this.unconfirmedV2Speed && mcdu._toFlexChecked, + ); + } + this.toRunway = toRunway; + } + } + + /** + * Switches to the next/new perf page (if new flight phase is in order) or reloads the current page + */ + private tryUpdatePerfPage(_old: FmgcFlightPhase, _new: FmgcFlightPhase) { + // Ensure we have a performance page selected... + if (this.page.Current < this.page.PerformancePageTakeoff || this.page.Current > this.page.PerformancePageGoAround) { + return; + } + + const curPerfPagePhase = (() => { + switch (this.page.Current) { + case this.page.PerformancePageTakeoff: + return FmgcFlightPhase.Takeoff; + case this.page.PerformancePageClb: + return FmgcFlightPhase.Climb; + case this.page.PerformancePageCrz: + return FmgcFlightPhase.Cruise; + case this.page.PerformancePageDes: + return FmgcFlightPhase.Descent; + case this.page.PerformancePageAppr: + return FmgcFlightPhase.Approach; + case this.page.PerformancePageGoAround: + return FmgcFlightPhase.GoAround; + } + })(); + + if (_new > _old) { + if (_new >= curPerfPagePhase) { + CDUPerformancePage.ShowPage(this.mcdu, _new); + } + } else if (_old === curPerfPagePhase) { + CDUPerformancePage.ShowPage(this.mcdu, _old); + } + } + + public routeReservedEntered() { + return this._rteReservedWeightEntered || this._rteReservedPctEntered; + } + + public routeFinalEntered() { + return this._rteFinalWeightEntered || this._rteFinalTimeEntered; + } + + /** + * Set the progress page bearing/dist location + * @param {string} ident ident of the waypoint or runway, will be replaced by "ENTRY" if brg/dist offset are specified + * @param {LatLongAlt} coordinates co-ordinates of the waypoint/navaid/runway, without brg/dist offset + * @param {string?} icao icao database id of the waypoint if applicable + */ + private _setProgLocation(ident, coordinates, icao) { + console.log(`progLocation: ${ident} ${coordinates}`); + this._progBrgDist = { + icao, + ident, + coordinates, + bearing: -1, + distance: -1, + }; + + this.updateProgDistance(); + } + + /** + * Try to set the progress page bearing/dist waypoint/location + * @param s scratchpad entry + * @param callback callback taking boolean arg for success/failure + */ + public trySetProgWaypoint(s: string, callback = EmptyCallback.Boolean) { + if (s === Keypad.clrValue) { + this._progBrgDist = undefined; + return callback(true); + } + + WaypointEntryUtils.getOrCreateWaypoint(this, s, false, 'ENTRY') + .then((wp) => { + this._setProgLocation(wp.ident, wp.location, wp.databaseId); + return callback(true); + }) + .catch((err) => { + // Rethrow if error is not an FMS message to display + if (err.type === undefined) { + throw err; + } + + this.showFmsErrorMessage(err.type); + return callback(false); + }); + } + + /** + * Recalculate the bearing and distance for progress page + */ + private updateProgDistance() { + if (!this._progBrgDist) { + return; + } + + const latitude = ADIRS.getLatitude(); + const longitude = ADIRS.getLongitude(); + + if (!latitude.isNormalOperation() || !longitude.isNormalOperation()) { + this._progBrgDist.distance = -1; + this._progBrgDist.bearing = -1; + return; + } + + const planeLl = new LatLong(latitude.value, longitude.value); + this._progBrgDist.distance = Avionics.Utils.computeGreatCircleDistance(planeLl, this._progBrgDist.coordinates); + this._progBrgDist.bearing = A32NX_Util.trueToMagnetic( + Avionics.Utils.computeGreatCircleHeading(planeLl, this._progBrgDist.coordinates), + ); + } + + public get progBearing() { + return this._progBrgDist ? this._progBrgDist.bearing : -1; + } + + public get progDistance() { + return this._progBrgDist ? this._progBrgDist.distance : -1; + } + + public get progWaypointIdent() { + return this._progBrgDist ? this._progBrgDist.ident : undefined; + } + + public isWaypointInUse(wpt: Waypoint): Promise { + return this.flightPlanService + .isWaypointInUse(wpt) + .then( + (inUseByFlightPlan) => inUseByFlightPlan || (this._progBrgDist && this._progBrgDist.icao === wpt.databaseId), + ); + } + + public setGroundTempFromOrigin() { + const origin = this.flightPlanService.active.originAirport; + + if (!origin) { + return; + } + + this.groundTempAuto = A32NX_Util.getIsaTemp(origin.location.alt); + } + + public trySetGroundTemp(scratchpadValue: string) { + if (this.flightPhaseManager.phase !== FmgcFlightPhase.Preflight) { + throw NXSystemMessages.notAllowed; + } + + if (scratchpadValue === Keypad.clrValue) { + this.groundTempPilot = undefined; + return; + } + + if (scratchpadValue.match(/^[+-]?[0-9]{1,2}$/) === null) { + throw NXSystemMessages.formatError; + } + + this.groundTempPilot = parseInt(scratchpadValue); + } + + public get groundTemp() { + return this.groundTempPilot !== undefined ? this.groundTempPilot : this.groundTempAuto; + } + + public navModeEngaged() { + const lateralMode = SimVar.GetSimVarValue('L:A32NX_FMA_LATERAL_MODE', 'Number'); + switch (lateralMode) { + case 20: // NAV + case 30: // LOC* + case 31: // LOC + case 32: // LAND + case 33: // FLARE + case 34: // ROLL OUT + return true; + } + return false; + } + + // FIXME check why steps alts page is the only one outside FMS/CDU calling this... + /** + * Add type 2 message to fmgc message queue + * @param _message MessageObject + * @param _isResolvedOverride Function that determines if the error is resolved at this moment (type II only). + * @param _onClearOverride Function that executes when the error is actively cleared by the pilot (type II only). + */ + public addMessageToQueue( + _message: TypeIIMessage, + _isResolvedOverride: (arg0: any) => any = undefined, + _onClearOverride: (arg0: any) => any = undefined, + ) { + if (!_message.isTypeTwo) { + return; + } + const message = + _isResolvedOverride === undefined && _onClearOverride === undefined + ? _message + : _message.getModifiedMessage('', _isResolvedOverride, _onClearOverride); + this._messageQueue.addMessage(message); + } + + /** + * Removes a message from the queue + * @param value {String} + */ + public removeMessageFromQueue(value: string) { + this._messageQueue.removeMessage(value); + } + + public updateMessageQueue() { + this._messageQueue.updateDisplayedMessage(); + } + + /* END OF MCDU GET/SET METHODS */ + /* UNSORTED CODE BELOW */ + + /** + * Generic function which returns true if engine(index) is ON (N2 > 20) + */ + private isEngineOn(index: number): boolean { + return SimVar.GetSimVarValue(`L:A32NX_ENGINE_N2:${index}`, 'number') > 20; + } + /** + * Returns true if any one engine is running (N2 > 20) + */ + // FIXME can be private when ATSU moved out of FMS + public isAnEngineOn(): boolean { + return this.isEngineOn(1) || this.isEngineOn(2); + } + + /** + * Returns true only if all engines are running (N2 > 20) + */ + //TODO: can this be an util? no + public isAllEngineOn(): boolean { + return this.isEngineOn(1) && this.isEngineOn(2); + } + + public isOnGround(): boolean { + return ( + SimVar.GetSimVarValue('L:A32NX_LGCIU_1_NOSE_GEAR_COMPRESSED', 'Number') === 1 || + SimVar.GetSimVarValue('L:A32NX_LGCIU_2_NOSE_GEAR_COMPRESSED', 'Number') === 1 + ); + } + + public isFlying(): boolean { + return ( + this.flightPhaseManager.phase >= FmgcFlightPhase.Takeoff && this.flightPhaseManager.phase < FmgcFlightPhase.Done + ); + } + /** + * Returns the maximum cruise FL for ISA temp and GW + * @param temp {number} ISA in C° + * @param gw {number} GW in t + * @returns {number} MAX FL + */ + //TODO: can this be an util? + private getMaxFL(temp = A32NX_Util.getIsaTempDeviation(), gw = this.getGW()) { + return Math.round(temp <= 10 ? -2.778 * gw + 578.667 : (temp * -0.039 - 2.389) * gw + temp * -0.667 + 585.334); + } + + /** + * Returns the maximum allowed cruise FL considering max service FL + * @param fl FL to check + * @returns maximum allowed cruise FL + */ + //TODO: can this be an util? no + public getMaxFlCorrected(fl: number = this.getMaxFL()): number { + return fl >= this.recMaxCruiseFL ? this.recMaxCruiseFL : fl; + } + + // only used by trySetMinDestFob + //TODO: Can this be util? + private isMinDestFobInRange(fuel: number) { + return 0 <= fuel && fuel <= 80.0; + } + + //TODO: Can this be util? + private isTaxiFuelInRange(taxi: number) { + return 0 <= taxi && taxi <= 9.9; + } + + //TODO: Can this be util? + private isFinalFuelInRange(fuel: number) { + return 0 <= fuel && fuel <= 100; + } + + //TODO: Can this be util? + private isFinalTimeInRange(time: string) { + const convertedTime = FmsFormatters.hhmmToMinutes(time.padStart(4, '0')); + return 0 <= convertedTime && convertedTime <= 90; + } + + //TODO: Can this be util? + private isRteRsvFuelInRange(fuel: number) { + return 0 <= fuel && fuel <= 10.0; + } + + //TODO: Can this be util? + private isRteRsvPercentInRange(value: number) { + return value >= 0 && value <= 15.0; + } + + //TODO: Can this be util? + private isZFWInRange(zfw: number) { + return 35.0 <= zfw && zfw <= 80.0; + } + + //TODO: Can this be util? + private isZFWCGInRange(zfwcg: number) { + return 8.0 <= zfwcg && zfwcg <= 50.0; + } + + //TODO: Can this be util? + private isBlockFuelInRange(fuel: number) { + return 0 <= fuel && fuel <= 80; + } + + /** + * Retrieves current fuel on boad in tons. + * @returns current fuel on board in tons, or undefined if fuel readings are not available. + */ + //TODO: Can this be util? + public getFOB(): number | undefined { + const useFqi = this.isAnEngineOn(); + + // If an engine is not running, use pilot entered block fuel to calculate fuel predictions + return useFqi ? (SimVar.GetSimVarValue('FUEL TOTAL QUANTITY WEIGHT', 'pound') * 0.4535934) / 1000 : this.blockFuel; + } + + /** + * retrieves gross weight in tons or 0 if not available + * @deprecated use getGrossWeight() instead + */ + //TODO: Can this be util? + public getGW(): number { + const fmGwOrNull = this.getGrossWeight(); + const fmGw = fmGwOrNull !== null ? fmGwOrNull : 0; + + SimVar.SetSimVarValue('L:A32NX_FM_GROSS_WEIGHT', 'Number', fmGw); + return fmGw; + } + + //TODO: Can this be util? + public getCG() { + return SimVar.GetSimVarValue('CG PERCENT', 'Percent over 100') * 100; + } + + //TODO: make this util or local var? + /** @deprecated Sim AP is not used! */ + private isAirspeedManaged() { + return SimVar.GetSimVarValue('AUTOPILOT SPEED SLOT INDEX', 'number') === 2; + } + + //TODO: make this util or local var? + /** @deprecated Sim AP is not used! */ + private isAltitudeManaged() { + return SimVar.GetSimVarValue('AUTOPILOT ALTITUDE SLOT INDEX', 'number') === 2; + } + + /** + * Check if the given string represents a decimal number. + * This may be a whole number or a number with one or more decimals. + * If the leading digit is 0 and one or more decimals are given, the leading digit may be omitted. + * @param str String to check + * @returns True if str represents a decimal value, otherwise false + */ + //TODO: Can this be util? + private representsDecimalNumber(str: string): boolean { + return /^[+-]?\d*(?:\.\d+)?$/.test(str); + } + + /** + * Gets the entered zero fuel weight, or undefined if not entered + * @returns the zero fuel weight in tonnes or undefined + */ + public getZeroFuelWeight(): number | undefined { + return this.zeroFuelWeight; + } + + public getV2Speed() { + return this.v2Speed; + } + + public getTropoPause() { + return this.tropo; + } + + public getManagedClimbSpeed() { + return this.managedSpeedClimb; + } + + public getManagedClimbSpeedMach() { + return this.managedSpeedClimbMach; + } + + public getManagedCruiseSpeed() { + return this.managedSpeedCruise; + } + + public getManagedCruiseSpeedMach() { + return this.managedSpeedCruiseMach; + } + + public getAccelerationAltitude() { + const plan = this.currFlightPlanService.active; + + if (plan) { + return plan.performanceData.accelerationAltitude; + } + + return undefined; + } + + public getThrustReductionAltitude() { + const plan = this.currFlightPlanService.active; + + if (plan) { + return plan.performanceData.thrustReductionAltitude; + } + + return undefined; + } + + public getOriginTransitionAltitude() { + const plan = this.currFlightPlanService.active; + + if (plan) { + return plan.performanceData.transitionAltitude; + } + + return undefined; + } + + public getDestinationTransitionLevel() { + const plan = this.currFlightPlanService.active; + + if (plan) { + return plan.performanceData.transitionLevel; + } + + return undefined; + } + + public get cruiseLevel() { + const plan = this.currFlightPlanService.active; + + if (plan) { + return plan.performanceData.cruiseFlightLevel; + } + + return undefined; + } + + public set cruiseLevel(level) { + const plan = this.currFlightPlanService.active; + + if (plan) { + this.currFlightPlanService.setPerformanceData('cruiseFlightLevel', level); + // used by FlightPhaseManager + SimVar.SetSimVarValue( + 'L:A32NX_AIRLINER_CRUISE_ALTITUDE', + 'number', + Number.isFinite(level * 100) ? level * 100 : 0, + ); + } + } + + public get costIndex() { + const plan = this.currFlightPlanService.active; + + if (plan) { + return plan.performanceData.costIndex; + } + + return undefined; + } + + public set costIndex(ci) { + const plan = this.currFlightPlanService.active; + + if (plan) { + this.currFlightPlanService.setPerformanceData('costIndex', ci); + } + } + + public get isCostIndexSet() { + const plan = this.currFlightPlanService.active; + + if (plan) { + return plan.performanceData.costIndex !== undefined; + } + + return false; + } + + public get tropo() { + const plan = this.currFlightPlanService.active; + + if (plan) { + return plan.performanceData.tropopause; + } + + return undefined; + } + + public get isTropoPilotEntered() { + const plan = this.currFlightPlanService.active; + + if (plan) { + return plan.performanceData.tropopauseIsPilotEntered; + } + + return false; + } + + public set tropo(tropo) { + const plan = this.currFlightPlanService.active; + + if (plan) { + this.currFlightPlanService.setPerformanceData('pilotTropopause', tropo); + } + } + + public get flightNumber() { + const plan = this.currFlightPlanService.active; + + if (plan) { + return this.currFlightPlanService.active.flightNumber; + } + + return undefined; + } + + public set flightNumber(flightNumber) { + const plan = this.currFlightPlanService.active; + + if (plan) { + this.currFlightPlanService.setFlightNumber(flightNumber); + } + } + + /** + * The maximum speed imposed by the climb speed limit in the active flight plan or null if it is not set. + */ + public get climbSpeedLimit(): number | null { + const plan = this.currFlightPlanService.active; + + // The plane follows 250 below 10'000 even without a flight plan + return plan ? plan.performanceData.climbSpeedLimitSpeed : DefaultPerformanceData.ClimbSpeedLimitSpeed; + } + + /** + * The altitude below which the climb speed limit of the active flight plan applies or null if not set. + */ + public get climbSpeedLimitAlt(): number | null { + const plan = this.currFlightPlanService.active; + + // The plane follows 250 below 10'000 even without a flight plan + return plan ? plan.performanceData.climbSpeedLimitAltitude : DefaultPerformanceData.ClimbSpeedLimitAltitude; + } + + /** + * The maximum speed imposed by the descent speed limit in the active flight plan or null if it is not set. + */ + private get descentSpeedLimit(): number | null { + const plan = this.currFlightPlanService.active; + + // The plane follows 250 below 10'000 even without a flight plan + return plan ? plan.performanceData.descentSpeedLimitSpeed : DefaultPerformanceData.DescentSpeedLimitSpeed; + } + + /** + * The altitude below which the descent speed limit of the active flight plan applies or null if not set. + */ + private get descentSpeedLimitAlt(): number | null { + const plan = this.currFlightPlanService.active; + + // The plane follows 250 below 10'000 even without a flight plan + return plan ? plan.performanceData.descentSpeedLimitAltitude : DefaultPerformanceData.DescentSpeedLimitAltitude; + } + + public getFlightPhase() { + return this.flightPhaseManager.phase; + } + + public getClimbSpeedLimit() { + return { + speed: this.climbSpeedLimit, + underAltitude: this.climbSpeedLimitAlt, + }; + } + + public getDescentSpeedLimit() { + return { + speed: this.descentSpeedLimit, + underAltitude: this.descentSpeedLimitAlt, + }; + } + + public getPreSelectedClbSpeed() { + return this.preSelectedClbSpeed; + } + + public getPreSelectedCruiseSpeed() { + return this.preSelectedCrzSpeed; + } + + public getTakeoffFlapsSetting() { + return this.flaps; + } + + public getManagedDescentSpeed() { + return this.managedSpeedDescendPilot !== undefined ? this.managedSpeedDescendPilot : this.managedSpeedDescend; + } + + public getManagedDescentSpeedMach() { + return this.managedSpeedDescendMachPilot !== undefined + ? this.managedSpeedDescendMachPilot + : this.managedSpeedDescendMach; + } + + // FIXME... ambiguous name that doesn't say if it's Vapp, GSmini, or something else + public getApproachSpeed() { + return this.approachSpeeds && this.approachSpeeds.valid ? this.approachSpeeds.vapp : 0; + } + + public getFlapRetractionSpeed() { + return this.approachSpeeds && this.approachSpeeds.valid ? this.approachSpeeds.f : 0; + } + + public getSlatRetractionSpeed() { + return this.approachSpeeds && this.approachSpeeds.valid ? this.approachSpeeds.s : 0; + } + + public getCleanSpeed() { + return this.approachSpeeds && this.approachSpeeds.valid ? this.approachSpeeds.gd : 0; + } + + public getTripWind() { + // FIXME convert vnav to use +ve for tailwind, -ve for headwind, it's the other way around at the moment + return -this.averageWind; + } + + /** @deprecated This API is not suitable and needs replaced with a proper wind manager. */ + public getWinds() { + return this.winds; + } + + public getApproachWind() { + const activePlan = this.currFlightPlanService.active; + const destination = activePlan.destinationAirport; + + if (!destination || !destination.location || !isFinite(this.perfApprWindHeading)) { + return { direction: 0, speed: 0 }; + } + + const magVar = Facilities.getMagVar(destination.location.lat, destination.location.long); + const trueHeading = A32NX_Util.magneticToTrue(this.perfApprWindHeading, magVar); + + return { direction: trueHeading, speed: this.perfApprWindSpeed }; + } + + public getApproachQnh() { + return this.perfApprQNH; + } + + public getApproachTemperature() { + return this.perfApprTemp; + } + + public getDestinationElevation() { + return Number.isFinite(this.landingElevation) ? this.landingElevation : 0; + } + + public trySetManagedDescentSpeed(value: string): boolean { + if (value === Keypad.clrValue) { + this.managedSpeedDescendPilot = undefined; + this.managedSpeedDescendMachPilot = undefined; + return true; + } + + const MACH_SLASH_SPD_REGEX = /^(\.\d{1,2})?\/(\d{3})?$/; + const machSlashSpeedMatch = value.match(MACH_SLASH_SPD_REGEX); + + const MACH_REGEX = /^\.\d{1,2}$/; + const SPD_REGEX = /^\d{1,3}$/; + + if (machSlashSpeedMatch !== null /* ".NN/" or "/NNN" entry */) { + const speed = parseInt(machSlashSpeedMatch[2]); + if (Number.isFinite(speed)) { + if (speed < 100 || speed > 350) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.managedSpeedDescendPilot = speed; + } + + const mach = Math.round(parseFloat(machSlashSpeedMatch[1]) * 1000) / 1000; + if (Number.isFinite(mach)) { + if (mach < 0.15 || mach > 0.82) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.managedSpeedDescendMachPilot = mach; + } + + return true; + } else if (value.match(MACH_REGEX) !== null /* ".NN" */) { + // Entry of a Mach number only without a slash is allowed + const mach = Math.round(parseFloat(value) * 1000) / 1000; + if (Number.isFinite(mach)) { + if (mach < 0.15 || mach > 0.82) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.managedSpeedDescendMachPilot = mach; + } + + return true; + } else if (value.match(SPD_REGEX) !== null /* "NNN" */) { + const speed = parseInt(value); + if (Number.isFinite(speed)) { + if (speed < 100 || speed > 350) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + // This is the maximum managed Mach number you can get, even with CI 100. + // Through direct testing by a pilot, it was also determined that the plane gives Mach 0.80 for all of the tested CAS entries. + const mach = 0.8; + + this.managedSpeedDescendPilot = speed; + this.managedSpeedDescendMachPilot = mach; + + return true; + } + } + + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + public trySetPerfClbPredToAltitude(value: string): boolean { + if (value === Keypad.clrValue) { + this.perfClbPredToAltitudePilot = undefined; + return true; + } + + const currentAlt = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); + const match = value.match(/^(FL\d{3}|\d{1,5})$/); + if (match === null || match.length < 1) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const altOrFlString = match[1].replace('FL', ''); + const altitude = altOrFlString.length < 4 ? 100 * parseInt(altOrFlString) : parseInt(altOrFlString); + + if (!Number.isFinite(altitude)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + if (altitude < currentAlt || (this.cruiseLevel && altitude > this.cruiseLevel * 100)) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.perfClbPredToAltitudePilot = altitude; + return true; + } + + public trySetPerfDesPredToAltitude(value: string): boolean { + if (value === Keypad.clrValue) { + this.perfDesPredToAltitudePilot = undefined; + return true; + } + + const currentAlt = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); + const match = value.match(/^(FL\d{3}|\d{1,5})$/); + if (match === null || match.length < 1) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + const altOrFlString = match[1].replace('FL', ''); + const altitude = altOrFlString.length < 4 ? 100 * parseInt(altOrFlString) : parseInt(altOrFlString); + + if (!Number.isFinite(altitude)) { + this.setScratchpadMessage(NXSystemMessages.formatError); + return false; + } + + if (altitude > currentAlt) { + this.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return false; + } + + this.perfDesPredToAltitudePilot = altitude; + return true; + } + + private updatePerfPageAltPredictions() { + const currentAlt = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); + if (this.perfClbPredToAltitudePilot !== undefined && currentAlt > this.perfClbPredToAltitudePilot) { + this.perfClbPredToAltitudePilot = undefined; + } + + if (this.perfDesPredToAltitudePilot !== undefined && currentAlt < this.perfDesPredToAltitudePilot) { + this.perfDesPredToAltitudePilot = undefined; + } + } + + // FIXME, very sussy function + public computeManualCrossoverAltitude(mach: number): number { + const maximumCrossoverAltitude = 30594; // Crossover altitude of (300, 0.8) + const mmoCrossoverAltitide = 24554; // Crossover altitude of (VMO, MMO) + + if (mach < 0.8) { + return maximumCrossoverAltitude; + } + + return maximumCrossoverAltitude + ((mmoCrossoverAltitide - maximumCrossoverAltitude) * (mach - 0.8)) / 0.02; + } + + protected getActivePlanLegCount() { + if (!this.flightPlanService.hasActive) { + return 0; + } + + return this.flightPlanService.active.legCount; + } + + public getDistanceToDestination() { + return this.guidanceController.alongTrackDistanceToDestination; + } + + /** + * Modifies the active flight plan to go direct to a specific waypoint, not necessarily in the flight plan + */ + public async directToWaypoint(waypoint: Fix) { + // FIXME fm pos + const adirLat = ADIRS.getLatitude(); + const adirLong = ADIRS.getLongitude(); + const trueTrack = ADIRS.getTrueTrack(); + + if (!adirLat.isNormalOperation() || !adirLong.isNormalOperation() || !trueTrack.isNormalOperation()) { + return; + } + + const ppos = { + lat: adirLat.value, + long: adirLong.value, + }; + + await this.flightPlanService.directToWaypoint(ppos, trueTrack.value, waypoint); + } + + /** + * Modifies the active flight plan to go direct to a specific leg + * @param legIndex index of leg to go direct to + */ + public async directToLeg(legIndex: number) { + // FIXME fm pos + const adirLat = ADIRS.getLatitude(); + const adirLong = ADIRS.getLongitude(); + const trueTrack = ADIRS.getTrueTrack(); + + if (!adirLat.isNormalOperation() || !adirLong.isNormalOperation() || !trueTrack.isNormalOperation()) { + return; + } + + const ppos = { + lat: adirLat.value, + long: adirLong.value, + }; + + await this.flightPlanService.directToLeg(ppos, trueTrack.value, legIndex); + } + + /** + * Gets the navigation database ident (including cycle info). + */ + public getNavDatabaseIdent(): DatabaseIdent | null { + return this.navDbIdent; + } + + // --------------------------- + // CDUMainDisplay Types + // --------------------------- + + protected abstract page: any; + + /** + * Set a request from a subsystem to the MCDU + */ + protected abstract setRequest(subsystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'): void; + protected abstract setScratchpadText(value: string): void; + protected abstract setScratchpadMessage(message: McduMessage): void; + protected abstract addNewAtsuMessage(code: AtsuStatusCodes): void; +} + +// const FlightPlans = Object.freeze({ +// Active: 0, +// Temporary: 1, +// }); + +const DefaultPerformanceData = Object.freeze({ + ClimbSpeedLimitSpeed: 250, + ClimbSpeedLimitAltitude: 10000, + DescentSpeedLimitSpeed: 250, + DescentSpeedLimitAltitude: 10000, +}); + +/** Writes FM output words for both FMS. */ +class FmArinc429OutputWord extends Arinc429OutputWord { + private readonly localVars = [`L:A32NX_FM1_${this.name}`, `L:A32NX_FM2_${this.name}`]; + + override async writeToSimVarIfDirty() { + if (this.isDirty) { + this.isDirty = false; + return Promise.all( + this.localVars.map((localVar) => Arinc429Word.toSimVarValue(localVar, this.word.value, this.word.ssm)), + ); + } + return Promise.resolve(); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_MessageQueue.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_MessageQueue.ts new file mode 100644 index 00000000000..1619aeb0a11 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/A32NX_MessageQueue.ts @@ -0,0 +1,80 @@ +import { TypeIIMessage } from '../messages/NXSystemMessages'; +import { ScratchpadDataLink } from './A320_Neo_CDU_Scratchpad'; + +export class A32NX_MessageQueue { + private readonly _queue = []; + + constructor(private readonly _fmgc: { fmgcScratchpad: ScratchpadDataLink }) {} + + /** + * Fmgc messages enter the queue via this void + */ + addMessage(message: TypeIIMessage) { + if (message.isResolved(this._fmgc)) { + this.updateDisplayedMessage(); + return; + } + + this._addToQueueOrUpdateQueuePosition(message); + this.updateDisplayedMessage(); + } + + removeMessage(value: string) { + for (let i = 0; i < this._queue.length; i++) { + const message = this._queue[i]; + if (message.text === value) { + message.onClear(this._fmgc); + this._queue.splice(i, 1); + if (i === 0) { + if (this._fmgc.fmgcScratchpad) { + this._fmgc.fmgcScratchpad.removeMessage(value); + } + this.updateDisplayedMessage(); + } + break; + } + } + } + + resetQueue() { + this._queue.length = 0; + } + + updateDisplayedMessage() { + if (this._queue.length > 0) { + const message = this._queue[0]; + if (message.isResolved(this._fmgc)) { + this._queue.splice(0, 1); + return this.updateDisplayedMessage(); + } + + if (this._fmgc.fmgcScratchpad) { + this._fmgc.fmgcScratchpad.setMessage(message); + } + } + } + + _addToQueueOrUpdateQueuePosition(message: TypeIIMessage) { + for (let i = 0; i < this._queue.length; i++) { + if (this._queue[i].text === message.text) { + if (i !== 0) { + this._queue.unshift(this._queue[i]); + this._queue.splice(i + 1, 1); + } + return; + } + } + + for (let i = 0; i < this._queue.length; i++) { + if (this._queue[i].isResolved(this._fmgc)) { + this._queue.splice(i, 1); + } + } + + this._queue.unshift(message); + + if (this._queue.length > 5) { + this._queue.splice(5, 1); + } + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/FmsFormatters.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/FmsFormatters.ts new file mode 100644 index 00000000000..470a5721ae1 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/FmsFormatters.ts @@ -0,0 +1,45 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +export class FmsFormatters { + public static secondsTohhmm(seconds: number) { + const h = Math.floor(seconds / 3600); + const m = Math.floor((seconds - h * 3600) / 60); + return h.toFixed(0).padStart(2, '0') + m.toFixed(0).padStart(2, '0'); + } + + public static minuteToSeconds(minutes: number) { + return minutes * 60; + } + + public static secondsToUTC(seconds: number) { + const h = Math.floor(seconds / 3600); + const m = Math.floor((seconds - h * 3600) / 60); + return (h % 24).toFixed(0).padStart(2, '0') + m.toFixed(0).padStart(2, '0'); + } + + /** + * Computes hour and minutes when given minutes + * @param minutes - minutes used to make the conversion + * @returns A string in the format "HHMM" e.g "0235" + */ + public static minutesTohhmm(minutes: number): string { + const h = Math.floor(minutes / 60); + const m = minutes - h * 60; + return h.toFixed(0).padStart(2, '0') + m.toFixed(0).padStart(2, '0'); + } + + /** + * computes minutes when given hour and minutes + * @param hhmm string used to make the conversion + * @returns numbers in minutes form + */ + public static hhmmToMinutes(hhmm: string): number { + if (!hhmm) { + return NaN; + } + const h = parseInt(hhmm.substring(0, 2)); + const m = parseInt(hhmm.substring(2, 4)); + return h * 60 + m; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyAidsPageInterface.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyAidsPageInterface.ts new file mode 100644 index 00000000000..d246369b73c --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyAidsPageInterface.ts @@ -0,0 +1,18 @@ +// Copyright (c) 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { LskCallback, LskDelayFunction } from './LegacyFmsPageInterface'; +import { McduMessage } from '../messages/NXSystemMessages'; + +interface LegacyAidsPageDrawingInterface { + clearDisplay(webSocketDraw?: boolean): void; + setTemplate(template: any[][], large?: boolean): void; + getDelaySwitchPage(): number; + onRightInput: LskCallback[]; + rightInputDelay: LskDelayFunction[]; + activeSystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'; + setScratchpadMessage(message: McduMessage): void; +} + +/** This breaks some circular refs, and tells us what we need a shim for to wrap legacy pages in future. */ +export type LegacyAidsPageInterface = LegacyAidsPageDrawingInterface; diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyAtsuPageInterface.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyAtsuPageInterface.ts new file mode 100644 index 00000000000..1887bc2ddb8 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyAtsuPageInterface.ts @@ -0,0 +1,74 @@ +// Copyright (c) 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { FmsClient } from '@atsu/fmsclient'; +import { LskCallback, LskDelayFunction } from './LegacyFmsPageInterface'; +import { AtsuStatusCodes } from '@datalink/common'; +import { McduMessage } from '../messages/NXSystemMessages'; +import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; +import { FlightPhaseManager } from '@fmgc/flightphase'; + +interface LegacyAtsuPageDrawingInterface { + clearDisplay(webSocketDraw?: boolean): void; + setTemplate(template: any[][], large?: boolean): void; + setTitle(title: string): void; + setArrows(up: boolean, down: boolean, left: boolean, right: boolean): void; + getDelaySwitchPage(): number; + getDelayBasic(): number; + getDelayMedium(): number; + getDelayHigh(): number; + getDelayFuelPred(): number; + getDelayWindLoad(): number; + requestUpdate(): void; + + pageRedrawCallback?: () => void; + onPrevPage: () => void; + onNextPage: () => void; + onUp: () => void; + onDown: () => void; + page: Record; + SelfPtr: ReturnType | false; + onLeftInput: LskCallback[]; + onRightInput: LskCallback[]; + leftInputDelay: LskDelayFunction[]; + rightInputDelay: LskDelayFunction[]; + activeSystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'; + readonly PageTimeout: { + Fast: number; + Medium: number; + Dyn: number; + Default: number; + Slow: number; + }; + + printPage(lines: any[]): void; + addNewAtsuMessage(code: AtsuStatusCodes): void; + setScratchpadMessage(message: McduMessage): void; +} + +/** These all need to be things published on the FMGC output bus. */ +interface LegacyAtsuPageFmsInterface { + // NO! + isAnEngineOn(): boolean; + // NO! + getFOB(): number | undefined; + + atsu?: FmsClient; + // NO! + flightPlanService: FlightPlanService; + // NO! + flightPhaseManager: FlightPhaseManager; + // NO! + simbrief: any; + // Move to ATSU + aocTimes: { + doors: number; + off: number; + out: number; + on: number; + in: number; + }; +} + +/** This breaks some circular refs, and tells us what we need a shim for to wrap legacy pages in future. */ +export type LegacyAtsuPageInterface = LegacyAtsuPageDrawingInterface & LegacyAtsuPageFmsInterface; diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyCfdiuPageInterface.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyCfdiuPageInterface.ts new file mode 100644 index 00000000000..770f35e198d --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyCfdiuPageInterface.ts @@ -0,0 +1,21 @@ +// Copyright (c) 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { LskCallback, LskDelayFunction } from './LegacyFmsPageInterface'; + +interface LegacyCfdiuPageDrawingInterface { + clearDisplay(webSocketDraw?: boolean): void; + setTemplate(template: any[][], large?: boolean): void; + getDelaySwitchPage(): number; + onPrevPage: () => void; + onNextPage: () => void; + onUnload: () => void; + onLeftInput: LskCallback[]; + onRightInput: LskCallback[]; + leftInputDelay: LskDelayFunction[]; + rightInputDelay: LskDelayFunction[]; + activeSystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'; +} + +/** This breaks some circular refs, and tells us what we need a shim for to wrap legacy pages in future. */ +export type LegacyCfdiuPageInterface = LegacyCfdiuPageDrawingInterface; diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyFmsPageInterface.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyFmsPageInterface.ts new file mode 100644 index 00000000000..e17dc7c9ec6 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/LegacyFmsPageInterface.ts @@ -0,0 +1,323 @@ +// Copyright (c) 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { FlightPlanInterface } from '@fmgc/flightplanning/FlightPlanInterface'; +import { McduMessage } from '../messages/NXSystemMessages'; +import { FmsDisplayInterface } from '@fmgc/flightplanning/interface/FmsDisplayInterface'; +import { FmsDataInterface } from '@fmgc/flightplanning/interface/FmsDataInterface'; +import { + DatabaseIdent, + EfisSide, + EnrouteNdbNavaid, + Fix, + IlsNavaid, + NdbNavaid, + TerminalNdbNavaid, + VhfNavaid, +} from '@flybywiresim/fbw-sdk'; +import { FuelPlanningPhases } from './A32NX_Core/A32NX_FuelPred'; +import { ScratchpadDataLink } from './A320_Neo_CDU_Scratchpad'; +import { AdfRadioTuningStatus, MmrRadioTuningStatus, VorRadioTuningStatus } from '@fmgc/navigation/NavaidTuner'; +import { NXSpeedsApp } from './NXSpeeds'; +import { Navigation, SelectedNavaid } from '@fmgc/navigation/Navigation'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; +import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; +import { NavigationDatabase } from '@fmgc/NavigationDatabase'; +import { FlightPhaseManager } from '@fmgc/flightphase'; +import { GuidanceController } from '@fmgc/guidance/GuidanceController'; +import { DataManager } from '@fmgc/flightplanning/DataManager'; +import { EfisInterface } from '@fmgc/efis/EfisInterface'; + +export type LskCallback = ( + /** The scratchpad content when the LSK was pressed. */ + value: string, + /** Pushes the value back into the scratchpad. */ + scratchpadCallback: () => void, +) => void; + +export type LskDelayFunction = () => number; + +interface LegacyFmsPageDrawingInterface { + clearDisplay(webSocketDraw?: boolean): void; + setTemplate(template: any[][], large?: boolean): void; + setTitle(title: string): void; + setArrows(up: boolean, down: boolean, left: boolean, right: boolean): void; + getDelaySwitchPage(): number; + getDelayBasic(): number; + getDelayMedium(): number; + getDelayHigh(): number; + getDelayFuelPred(): number; + getDelayWindLoad(): number; + setScratchpadText(value: string): void; + setScratchpadUserData(value: string): void; + removeMessageFromQueue(value: string): void; + activateMcduScratchpad(): void; + isSubsystemRequesting(subsystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'): boolean; + requestUpdate(): void; + + pageRedrawCallback?: () => void; + pageUpdate?: () => void; + onPrevPage: () => void; + onNextPage: () => void; + onUp: () => void; + onDown: () => void; + onUnload: () => void; + onAirport: typeof EmptyCallback.Void; + page: Record; + SelfPtr: ReturnType | false; + onLeftInput: LskCallback[]; + onRightInput: LskCallback[]; + leftInputDelay: LskDelayFunction[]; + rightInputDelay: LskDelayFunction[]; + activeSystem: 'AIDS' | 'ATSU' | 'CFDS' | 'FMGC'; + readonly PageTimeout: { + Fast: number; + Medium: number; + Dyn: number; + Default: number; + Slow: number; + }; + returnPageCallback: typeof EmptyCallback.Void | null; + mcduScratchpad: ScratchpadDataLink; +} + +interface LegacyFmsPageFmsInterface extends FmsDataInterface, FmsDisplayInterface { + getFlightPlan(index: FlightPlanIndex): ReturnType; + getAlternateFlightPlan(index: FlightPlanIndex): ReturnType['alternateFlightPlan']; + eraseTemporaryFlightPlan(callback?: typeof EmptyCallback.Void): void; + insertTemporaryFlightPlan(callback?: typeof EmptyCallback.Void): void; + updateConstraints(): void; + setScratchpadMessage(message: McduMessage): void; + logTroubleshootingError(msg: any): void; + updateTowerHeadwind(): void; + onToRwyChanged(): void; + setGroundTempFromOrigin(): void; + directToWaypoint(waypoint: Fix): Promise; + directToLeg(legIndex: number): Promise; + toggleWaypointOverfly(index, fpIndex, forAlternate, callback?: typeof EmptyCallback.Void): void; + insertWaypoint( + newWaypointTo, + fpIndex, + forAlternate, + index, + before?: boolean, + callback?: typeof EmptyCallback.Boolean, + bypassTmpy?: boolean, + ): void; + tryUpdateRouteTrip(_dynamic?: boolean): void; + navModeEngaged(): boolean; + isFlying(): boolean; + trySetZeroFuelWeightZFWCG(s: string): boolean; + /** @deprecated use getGrossWeight */ + getGW(): number; + getCG(): number; + getFOB(): number | undefined; + routeFinalEntered(): boolean; + tryUpdateRouteFinalFuel(): void; + getRouteFinalFuelWeight(): number | undefined; + getRouteFinalFuelTime(): number; + trySetRouteFinalFuel(s: string): Promise; + tryUpdateRouteAlternate(): void; + getRouteAltFuelWeight(): number | null; + getRouteAltFuelTime(): number | null; + trySetRouteAlternateFuel(altFuel: string): Promise; + getDestEFOB(useFOB?: boolean): number | null; + getAltEFOB(useFOB?: boolean): number | null; + getRouteReservedWeight(): number; + getRouteReservedPercent(): number; + routeReservedEntered(): boolean; + trySetRouteReservedFuel(s: string): boolean; + tryUpdateMinDestFob(): void; + trySetMinDestFob(fuel: string): Promise; + checkEFOBBelowMin(): void; + tryGetExtraFuel(useFOB?: boolean): number; + tryGetExtraTime(useFOB?: boolean): number; + getNavDatabaseIdent(): DatabaseIdent | null; + switchNavDatabase(): Promise; + /** This one is a mess.. */ + updateCoRoute(coRouteNum, callback?: typeof EmptyCallback.Boolean): Promise; + updateFlightNo(flightNo: string, callback?: typeof EmptyCallback.Boolean): Promise; + setCruiseFlightLevelAndTemperature(input: string): boolean; + getCoRouteList(): Promise; + tryUpdateAltDestination(altDestIdent: string): Promise; + tryUpdateTropo(tropo: string): boolean; + tryUpdateFromTo(fromTo: string, callback?: typeof EmptyCallback.Boolean): void; + trySetGroundTemp(scratchpadValue: string): void; + goToFuelPredPage(): void; + trySetBlockFuel(s: string): boolean; + tryFuelPlanning(): boolean; + trySetTaxiFuelWeight(s: string): boolean; + trySetRouteReservedPercent(s: string): boolean; + trySetRouteFinalTime(s: string): Promise; + trySetAverageWind(s: string): boolean; + tryUpdateTOW(): void; + getTotalTripFuelCons(): number; + getTotalTripTime(): number; + tryUpdateLW(): void; + getOrSelectNavaidsByIdent( + ident: string, + callback: (navaid: EnrouteNdbNavaid | TerminalNdbNavaid | VhfNavaid) => void, + ): void; + + isFmTuningActive(): boolean; + isMmrTuningLocked(): boolean; + deselectedNavaids: string[]; + getVorTuningData(index: 1 | 2): VorRadioTuningStatus; + setManualVor(index: 1 | 2, facilityOrFrequency: number | VhfNavaid | null): void; + setVorCourse(index: 1 | 2, course: number): void; + getMmrTuningData(index: 1 | 2): MmrRadioTuningStatus; + setManualIls(facilityOrFrequency: number | IlsNavaid | null): Promise; + setIlsCourse(course: number | null, backcourse?: boolean): void; + getAdfTuningData(index: 1 | 2): AdfRadioTuningStatus; + setManualAdf(index: 1 | 2, facilityOrFrequency: number | NdbNavaid | null): void; + getOrSelectILSsByIdent(ident: string, callback: (navaid: IlsNavaid) => void): void; + getOrSelectVORsByIdent(ident: string, callback: (navaid: VhfNavaid) => void): void; + getOrSelectNDBsByIdent(ident: string, callback: (navaid: NdbNavaid) => void): void; + trySetV1Speed(s: string): boolean; + trySetVRSpeed(s: string): boolean; + trySetV2Speed(s: string): boolean; + trySetTakeOffTransAltitude(s: string): boolean; + trySetThrustReductionAccelerationAltitude(s: string): Promise; + trySetEngineOutAcceleration(s: string): Promise; + trySetThrustReductionAccelerationAltitudeGoaround(s: string): Promise; + trySetEngineOutAccelerationAltitudeGoaround(s: string): Promise; + trySetFlapsTHS(s: string): boolean; + setPerfTOFlexTemp(s: string): boolean; + getOriginTransitionAltitude(): number | undefined; + getDestinationTransitionLevel(): number | undefined; + getNavModeSpeedConstraint(): number; + trySetPreSelectedClimbSpeed(s: string): boolean; + tryUpdateCostIndex(costIndex: string): boolean; + trySetPerfClbPredToAltitude(value: string): boolean; + trySetPreSelectedCruiseSpeed(s: string): boolean; + trySetPerfDesPredToAltitude(value: string): boolean; + trySetManagedDescentSpeed(value: string): boolean; + getDistanceToDestination(): number | undefined; + setPerfApprQNH(s: string): boolean; + setPerfApprTemp(s: string): boolean; + setPerfApprWind(s: string): boolean; + updatePerfSpeeds(): void; + setPerfApprTransAlt(s: string): boolean; + setPerfApprVApp(s: string): boolean; + setPerfApprFlaps3(v: boolean): void; + setPerfApprMDA(s: string): boolean; + setPerfApprDH(s: string): boolean; + computeManualCrossoverAltitude(mach: number): number; + getMaxFlCorrected(fl?: number): number; + isAllEngineOn(): boolean; + trySetCruiseFlCheckInput(input: string): boolean; + trySetProgWaypoint(s: string, callback?: typeof EmptyCallback.Boolean): void; + isOnGround(): boolean; + getSelectedNavaids(): SelectedNavaid[]; + deselectNavaid(icao: string): void; + reselectNavaid(icao: string): void; + getOrSelectWaypointByIdent(ident: string, callback: (fix: Fix) => void): void; + getIsaTemp(alt: number): number; + + flightPlanService: FlightPlanService; + navigationDatabase: NavigationDatabase; + /** This one is a mess.. */ + coRoute: any; + flightPhaseManager: FlightPhaseManager; + guidanceController?: GuidanceController; + dataManager?: DataManager; + navigation?: Navigation; + v1Speed: number | null; + vRSpeed: number | null; + v2Speed: number | null; + holdDecelReached: boolean; + holdIndex: number; + holdSpeedTarget?: number; + fmgcMesssagesListener: ViewListener.ViewListener; + efisInterfaces?: Record; + _fuelPredDone: boolean; + _checkWeightSettable: boolean; + _zeroFuelWeightZFWCGEntered: boolean; + zeroFuelWeight?: number; + zeroFuelWeightMassCenter?: number; + _rteFinalWeightEntered: boolean; + _rteFinalTimeEntered: boolean; + _routeAltFuelEntered: boolean; + _routeTripTime: number; + _rteReservedWeightEntered: boolean; + _rteRsvPercentOOR: boolean; + _rteReservedPctEntered: boolean; + _minDestFobEntered: boolean; + _minDestFob: number; + _isBelowMinDestFob: boolean; + flightNumber?: string; + /** another mess */ + simbrief: any; + isCostIndexSet: boolean; + costIndex: number | undefined; + cruiseLevel: number | undefined; + cruiseTemperature?: number; + tempCurve: any; // we don't have the MSFS SDK typings for these curves + casToMachManualCrossoverCurve: any; + machToCasManualCrossoverCurve: any; + tropo: number | undefined; + isTropoPilotEntered: boolean; + groundTemp?: number; + groundTempPilot?: number; + taxiFuelWeight: number; + blockFuel?: number; + takeOffWeight: number; + landingWeight: number; + _blockFuelEntered: boolean; + _fuelPlanningPhase: FuelPlanningPhases; + _taxiEntered: boolean; + averageWind: number; + _deltaTime: number; + unconfirmedV1Speed?: number; + unconfirmedVRSpeed?: number; + unconfirmedV2Speed?: number; + computedVgd?: number; + computedVfs?: number; + computedVss?: number; + computedVls?: number; + flaps?: 0 | 1 | 2 | 3 | null; + ths?: number | null; + perfTOTemp: number; + _toFlexChecked: boolean; + preSelectedClbSpeed?: number; + preSelectedCrzSpeed?: number; + perfClbPredToAltitudePilot?: number; + perfDesPredToAltitudePilot?: number; + managedSpeedTarget: number; + managedSpeedTargetIsMach: boolean; + managedSpeedClimb: number; + managedSpeedClimbMach: number; + climbSpeedLimit: number; + climbSpeedLimitAlt: number; + managedSpeedCruise: number; + managedSpeedCruiseMach: number; + managedSpeedDescendPilot?: number; + managedSpeedDescend: number; + managedSpeedDescendMachPilot?: number; + managedSpeedDescendMach: number; + perfApprQNH: number; + perfApprTemp: number; + perfApprWindHeading: number; + perfApprWindSpeed: number; + approachSpeeds?: NXSpeedsApp; + vApp: number; + perfApprFlaps3: boolean; + perfApprMDA: number | null; + perfApprDH: 'NO DH' | number | null; + constraintAlt: number; + _activeCruiseFlightLevelDefaulToFcu: boolean; + progBearing: number; + progDistance: number; + progWaypointIdent: string | undefined; + /** Mess.. replace with wind manager. */ + winds: { + climb: any[]; + cruise: any[]; + des: any[]; + alternate: any | null; + }; + isTrueRefMode: boolean; +} + +/** This breaks some circular refs, and tells us what we need a shim for to wrap legacy pages in future. */ +export type LegacyFmsPageInterface = LegacyFmsPageDrawingInterface & LegacyFmsPageFmsInterface; diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/NXSpeeds.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/NXSpeeds.ts new file mode 100644 index 00000000000..5e881993622 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy/NXSpeeds.ts @@ -0,0 +1,609 @@ +/** + * Stall speed table + * calls function(gross weight (t), landing gear) which returns CAS. + * Indexes: 0 - Clean config, 1 - Config 1 + F, 2 - Config 2, 3 - Config 3, 4 - Config Full, 5 - Config 1. + * Sub-Indexes: 0 to 9 represent gross weight (t) in 5t steps from 40 to 80. + */ +const vs = [ + [ + () => 124, + (m) => 124 + 1.4 * (m - 40), + (m) => 131 + 1.4 * (m - 45), + (m) => 138 + 1.4 * (m - 50), + (m) => 145 + m - 55, + (m) => 150 + 1.2 * (m - 60), + (m) => 155 + 1.2 * (m - 65), + (m) => 161 + m - 70, + (m) => 166 + 1.2 * (m - 75), + () => 172, + ], // Clean Conf + [ + () => 93, + (m) => 93 + m - 40, + (m) => 98 + m - 45, + (m) => 103 + m - 50, + (m) => 108 + 0.8 * (m - 55), + (m) => 112 + m - 60, + (m) => 117 + 0.8 + (m - 65), + (m) => 121 + 0.8 + (m - 70), + (m) => 125 + m - 75, + () => 130, + ], // Conf 1 + F + [ + () => 91, + (m) => 91 + m - 40, + (m) => 96 + m - 45, + (m) => 101 + 0.8 * (m - 50), + (m) => 105 + m - 55, + (m) => 110 + 0.8 * (m - 60), + (m) => 114 + m - 65, + (m) => 119 + 0.6 * (m - 70), + (m) => 122 + 0.8 * (m - 75), + () => 126, + ], // Conf 2 + [ + (_, ldg) => 91 - ldg * 2, + (m, ldg) => 91 + m - 40 - ldg * 2, + (m, ldg) => 96 + m - 45 - ldg * 2, + (m, ldg) => 101 + 0.8 * (m - 50) - ldg * 2, + (m, ldg) => 105 + m - 55 - ldg * 2, + (m, ldg) => 110 + 0.8 * (m - 60) - ldg * 2, + (m, ldg) => 114 + m - 65 - ldg * 2, + (m, ldg) => 119 + 0.6 * (m - 70) - ldg * 2, + (m, ldg) => 122 + 0.8 * (m - 75) - ldg * 2, + (_, ldg) => 126 - ldg * 2, + ], // Conf 3 + [ + () => 84, + (m) => 84 + 0.8 * (m - 40), + (m) => 88 + m - 45, + (m) => 93 + 0.8 * (m - 50), + (m) => 97 + 0.8 * (m - 55), + (m) => 101 + 0.8 * (m - 60), + (m) => 105 + 0.8 * (m - 65), + (m) => 109 + 0.8 * (m - 70), + (m) => 113 + 0.6 * (m - 75), + () => 116, + ], // Conf Full + [ + () => 102, + (m) => 102 + m - 40, + (m) => 107 + m - 45, + (m) => 112 + m - 50, + (m) => 117 + 1.2 * (m - 55), + (m) => 123 + 0.8 * (m - 60), + (m) => 127 + m - 65, + (m) => 132 + m - 70, + (m) => 137 + 0.8 * (m - 75), + () => 141, + ], // Conf 1 +]; + +/** + * Lowest selectable Speed Table + * calls function(gross weigh (t), landing gear) which returns CAS, automatically compensates for cg. + * Indexes: 0 - Clean config, 1 - Config 1 + F, 2 - Config 2, 3 - Config 3, 4 - Config Full, 5 - Config 1. + * Sub-Indexes: 0 to 9 represent gross weight (t) in 5t steps from 40 to 80. + */ +const vls = [ + [ + () => 159, + (m) => 159 + 1.8 * (m - 40), + (m) => 168 + 1.8 * (m - 45), + (m) => 177 + 1.8 * (m - 50), + (m) => 186 + 1.2 * (m - 55), + (m) => 192 + 1.2 * (m - 60), + (m) => 198 + 1.6 * (m - 65), + (m) => 206 + 1.2 * (m - 70), + (m) => 212 + 1.6 * (m - 75), + () => 220, + ], // Clean Config + [ + () => 114, + (m) => 114 + 1.4 * (m - 40), + (m) => 121 + 1.2 * (m - 45), + (m) => 127 + 1.2 * (m - 50), + (m) => 133 + m - 55, + (m) => 138 + 1.2 * (m - 60), + (m) => 144 + m - 65, + (m) => 149 + m - 70, + (m) => 154 + 1.2 * (m - 75), + () => 160, + ], // Config 1 + F + [ + () => 110, + (m) => 110 + 1.8 * (m - 40), + (m) => 119 + 1.2 * (m - 45), + (m) => 125 + 1.2 * (m - 50), + (m) => 131 + 1.2 * (m - 55), + (m) => 137 + m - 60, + (m) => 142 + 0.6 * (m - 65), + (m) => 145 + 0.8 * (m - 70), + (m) => 149 + m - 75, + () => 154, + ], // Config 2 + [ + (_, ldg) => 117 - ldg, + (m, ldg) => correctCg(m, (m, cg) => (cg < 25 ? 117 + 0.4 * (m - 40) : 117)) - ldg, + (m, ldg) => correctCg(m, (m, cg) => (cg < 25 ? 119 + 1.2 * (m - 45) : 117 + 1.4 * (m - 45))) - ldg, + (m, ldg) => correctCg(m, (m, cg) => (cg < 25 ? 125 + 1.2 * (m - 50) : 124 + 1.2 * (m - 50))) - ldg, + (m, ldg) => correctCg(m, (m, cg) => (cg < 25 ? 131 + 1.2 * (m - 55) : 130 + m - 55)) - ldg, + (m, ldg) => correctCg(m, (m, cg) => (cg < 25 ? 137 + m - 60 : 135 + 1.2 * (m - 60))) - ldg, + (m, ldg) => correctCg(m, (m, cg) => (cg < 25 ? 142 : 141) + m - 65) - ldg, + (m, ldg) => correctCg(m, (m, cg) => (cg < 25 ? 147 : 146) + m - 70) - ldg, + (m, ldg) => correctCg(m, (m, cg) => (cg < 25 ? 152 + 0.8 * (m - 75) : 151 + m - 65)) - ldg, + (_, ldg) => 156 - ldg, + ], // Config 3 + [ + () => 116, + () => 116, + () => 116, + (m) => 116 + correctCg(m, (m, cg) => (cg < 25 ? 0.8 : 0.6) * (m - 50)), + (m) => correctCg(m, (m, cg) => (cg < 25 ? 120 : 119) + m - 55), + (m) => correctCg(m, (m, cg) => (cg < 25 ? 125 : 124) + m - 60), + (m) => correctCg(m, (m, cg) => (cg < 25 ? 130 : 129) + m - 65), + (m) => correctCg(m, (m, cg) => (cg < 25 ? 135 + 0.8 * (m - 70) : 134 + m - 70)), + (m) => 139 + 0.8 * (m - 75), + () => 143, + ], // Config Full + [ + () => 125, + (m) => 125 + 1.4 * (m - 40), + (m) => 132 + 1.2 * (m - 45), + (m) => 138 + 1.2 * (m - 50), + (m) => 144 + 1.4 * (m - 55), + (m) => 151 + m - 60, + (m) => 156 + 1.2 * (m - 65), + (m) => 162 + 1.4 * (m - 70), + (m) => 169 + 0.8 * (m - 75), + () => 173, + ], // Config 1 +]; + +/** + * Lowest selectable Speed Table for TakeOff ONLY + * calls function(gross weight (t)) which returns CAS. + * Indexes: 0 - Clean config, 1 - Config 1 + F, 2 - Config 2, 3 - Config 3, 4 - Config Full, 5 - Config 1. + * Sub-Indexes: 0 to 9 represent gross weight (t) in 5t steps from 40 to 80. + */ +const vlsTo = [ + vls[0], // Clean Config + [ + () => 105, + (m) => 105 + 1.2 * (m - 40), + (m) => 111 + m - 45, + (m) => 116 + 1.2 * (m - 50), + (m) => 122 + m - 55, + (m) => 127 + m - 60, + (m) => 132 + m - 65, + (m) => 137 + 0.8 * (m - 70), + (m) => 141 + 1.2 * (m - 75), + () => 147, + ], // Config 1 + F + [ + (_) => 101, + (m) => 101 + 1.4 * (m - 40), + (m) => 108 + 1.2 * (m - 45), + (m) => 114 + m - 50, + (m) => 119 + 1.2 * (m - 55), + (m) => 125 + m - 60, + (m) => 130 + 0.4 * (m - 65), + (m) => 132 + 0.8 * (m - 70), + (m) => 136 + 0.8 * (m - 75), + () => 140, + ], // Config 2 + [ + () => 101, + (m) => 101 + m - 40, + (m) => 106 + 1.2 * (m - 45), + (m) => 112 + 0.8 * (m - 50), + (m) => 116 + 1.2 * (m - 55), + (m) => 122 + m - 60, + (m) => 127 + m - 65, + (m) => 132 + 0.8 * (m - 70), + (m) => 136 + 0.8 * (m - 75), + () => 140, + ], // Config 3 + vls[4], // Config Full + vls[5], // Config 1 +]; + +/** + * F-Speed Table + * calls function(gross weight (t)) which returns CAS. + * Indexes: 0 to 9 represent gross weight (t) in 5t steps from 40 to 80. + */ +const f = [ + () => 131, + () => 131, + () => 131, + (m) => 131 + 1.2 * (m - 50), + (m) => 137 + 1.4 * (m - 55), + (m) => 144 + m - 60, + (m) => 149 + 1.2 * (m - 65), + (m) => 155 + m - 70, + (m) => 160 + 1.2 * (m - 75), + () => 166, +]; + +/** + * S-Speed Table + * calls function(gross weight (t)) which returns CAS. + * Indexes: 0 to 9 represent gross weight (t) in 5t steps from 40 to 80. + */ +const s = [ + () => 152, + (m) => 152 + 1.8 * (m - 40), + (m) => 161 + 1.6 * (m - 45), + (m) => 169 + 1.8 * (m - 50), + (m) => 178 + 1.6 * (m - 55), + (m) => 186 + 1.4 * (m - 60), + (m) => 193 + 1.4 * (m - 65), + (m) => 200 + 1.4 * (m - 70), + (m) => 207 + 1.4 * (m - 75), + () => 214, +]; + +const vmca = [ + [-2000, 115], + [0, 114], + [2000, 114], + [4000, 113], + [6000, 112], + [8000, 109], + [10000, 106], + [12000, 103], + [14100, 99], + [15100, 97], +]; + +const vmcg = [ + // 1+F, 2, 3 all the same + [-2000, 117], + [0, 116], + [2000, 116], + [4000, 115], + [6000, 114], + [8000, 112], + [10000, 109], + [12000, 106], + [14100, 102], + [15100, 101], +]; + +/** + * Vfe for Flaps/Slats + * @type {number[]} + */ +const vfeFS = [ + 215, // Config 1 + F + 200, // Config 2 + 185, // Config 3 + 177, // Config Full + 230, // Config 1 +]; + +const Vmo = 350; +const Mmo = 0.82; + +/** + * Correct input function for cg + * @param m {number} gross weight (t) + * @param f {function} function to be called with cg variable + * @param cg {number} center of gravity + * @returns {number} cg corrected velocity (CAS) + */ +function correctCg(m, f, cg = SimVar.GetSimVarValue('CG PERCENT', 'percent')) { + return f(m, isNaN(cg) ? 24 : cg); +} + +/** + * Ensure gross weight (mass) is withing valid range + * @param m {number} mass: gross weight + * @returns {number} mass: gross weight + * @private + */ +function _correctMass(m) { + return Math.ceil(((m > 80 ? 80 : m) - 40) / 5); +} + +/** + * Calculate green dot speed + * Calculation: + * Gross weight (t) * 2 + 85 when below FL200 + * @returns {number} + */ +function _computeGD(m) { + return m * 2 + 85; +} + +/** + * Corrects velocity for mach effect by adding 1kt for every 1000ft above FL200 + * @param v {number} velocity in kt (CAS) + * @param alt {number} altitude in feet (baro) + * @returns {number} Mach corrected velocity in kt (CAS) + */ +function _compensateForMachEffect(v, alt) { + return Math.ceil(alt > 20000 ? v + (alt - 20000) / 1000 : v); +} + +/** + * Calculates wind component for ground speed mini + * @param vw {number} velocity wind (headwind) + * @returns {number} velocity wind [5, 15] + */ +function _addWindComponent(vw) { + return Math.max(Math.min(15, vw), 5); +} + +/** + * Get difference between angles + * @param a {number} angle a + * @param b {number} angle b + * @returns {number} angle diff + * @private + */ +function _getdiffAngle(a, b) { + return 180 - Math.abs(Math.abs(a - b) - 180); +} + +/** + * Get next flaps index for vfeFS table + * @param fi {number} current flaps index + * @returns {number} vfeFS table index + * @private + */ +function _getVfeNIdx(fi) { + switch (fi) { + case 0: + return 4; + case 5: + return 1; + default: + return fi; + } +} + +/** + * Convert degrees Celsius into Kelvin + * @param T {number} degrees Celsius + * @returns {number} degrees Kelvin + */ +function _convertCtoK(T) { + return T + 273.15; +} + +/** + * Convert Mach to True Air Speed + * @param M {number} Mach + * @param T {number} Kelvin + * @returns {number} True Air Speed + */ +function _convertMachToKTas(M, T) { + return M * 661.4786 * Math.sqrt(T / 288.15); +} + +/** + * Convert TAS to Mach + * @param Vt {number} TAS + * @param T {number} Kelvin + * @returns {number} True Air Speed + */ +function _convertKTASToMach(Vt, T) { + return Vt / 661.4786 / Math.sqrt(T / 288.15); +} + +/** + * Convert TAS to Calibrated Air Speed + * @param Vt {number} velocity true air speed + * @param T {number} current temperature Kelvin + * @param p {number} current pressure hpa + * @returns {number} Calibrated Air Speed + */ +function _convertTasToKCas(Vt, T, p) { + return ( + 1479.1 * Math.sqrt(((p / 1013) * ((1 + (1 / (T / 288.15)) * (Vt / 1479.1) ** 2) ** 3.5 - 1) + 1) ** (1 / 3.5) - 1) + ); +} + +/** + * Convert KCAS to KTAS + * @param Vc {number} velocity true air speed + * @param T {number} current temperature Kelvin + * @param p {number} current pressure hpa + * @returns {number} Calibrated Air Speed + */ +function _convertKCasToKTAS(Vc, T, p) { + return ( + 1479.1 * + Math.sqrt((T / 288.15) * (((1 / (p / 1013)) * ((1 + 0.2 * (Vc / 661.4786) ** 2) ** 3.5 - 1) + 1) ** (1 / 3.5) - 1)) + ); +} + +/** + * Convert Mach to Calibrated Air Speed + * @param M {number} Mach + * @param T {number} Kelvin + * @param p {number} current pressure hpa + * @returns {number} Calibrated Air Speed + */ +function _convertMachToKCas(M, T, p) { + return _convertTasToKCas(_convertMachToKTas(M, T), T, p); +} + +/** + * Get correct Vmax for Vmo and Mmo in knots + * @returns {number} Min(Vmo, Mmo) + * @private + */ +function _getVmo() { + return Math.min( + Vmo, + _convertMachToKCas( + Mmo, + _convertCtoK(Simplane.getAmbientTemperature()), + SimVar.GetSimVarValue('AMBIENT PRESSURE', 'millibar'), + ), + ); +} + +export class NXSpeeds { + private readonly cm = _correctMass(this.m); + public vs = vs[this.fPos][this.cm](this.m, this.gPos); + public vls = (this.isTo ? vlsTo : vls)[this.fPos][this.cm](this.m, this.gPos); + public readonly vapp = this.vls + _addWindComponent(this.wind); + public readonly f = f[this.cm](this.m); + public readonly s = s[this.cm](this.m); + public gd = _computeGD(this.m); + public readonly vmax = this.fPos === 0 ? _getVmo() : vfeFS[this.fPos - 1]; + public readonly vfeN = this.fPos === 4 ? 0 : vfeFS[_getVfeNIdx(this.fPos)]; + + /** + * Computes Vs, Vls, Vapp, F, S and GD + * @param m mass: gross weight in t + * @param fPos flaps position + * @param gPos landing gear position + * @param isTo whether in takeoff config or not + * @param wind wind speed, defaults to 0. + */ + constructor( + private m: number, + private fPos: number, + private gPos: number, + private isTo: boolean, + private wind = 0, + ) {} + + compensateForMachEffect(alt: number) { + this.vs = _compensateForMachEffect(this.vs, alt); + this.vls = _compensateForMachEffect(this.vls, alt); + this.gd = _compensateForMachEffect(this.gd, alt); + } +} + +export class NXSpeedsApp { + private readonly cm = _correctMass(this.m); + public readonly vls = vls[this.isConf3 ? 3 : 4][this.cm](this.m, 1); + public readonly vapp = this.vls + NXSpeedsUtils.addWindComponent(this.wind / 3); + public readonly f = f[this.cm](this.m); + public readonly s = s[this.cm](this.m); + public readonly gd = _computeGD(this.m); + public valid = true; + + /** + * Calculates VLS and Vapp for selected landing configuration + * @param m Projected landing mass in t + * @param isConf3 CONF3 if true, else FULL + * @param tower headwind component, defaults to 0. + */ + constructor( + private m: number, + private isConf3: boolean, + private wind = 0, + ) {} +} + +export class NXSpeedsUtils { + /** + * Calculates wind component for ground speed mini + * @param vw {number} velocity wind (1/3 steady headwind) + * @returns {number} velocity wind [5, 15] + */ + static addWindComponent(vw = (SimVar.GetSimVarValue('AIRCRAFT WIND Z', 'knots') * -1) / 3) { + return _addWindComponent(vw); + } + + /** + * Calculates headwind component + * @param v {number} velocity wind + * @param a {number} angle: a + * @param b {number} angle: b + * @returns {number} velocity headwind + */ + static getHeadwind(v, a, b) { + return v * Math.cos(_getdiffAngle(a, b) * (Math.PI / 180)); + } + + /** + * 1/3 * (current headwind - tower headwind) + * @param vTwr {number} velocity tower headwind + * @param vCur {number} velocity current headwind + * @returns {number} head wind diff + */ + static getHeadWindDiff(vTwr, vCur = SimVar.GetSimVarValue('AIRCRAFT WIND Z', 'knots') * -1) { + return Math.round((1 / 3) * (vCur - vTwr)); + } + + /** + * Returns Vtarget limited by Vapp and VFE next + * @param vapp {number} Vapp + * @param windDiff {number} ground speed mini + * @returns {number} + */ + static getVtargetGSMini(vapp, windDiff) { + return Math.max( + vapp, + Math.min( + Math.round(vapp + windDiff), + Math.round( + SimVar.GetSimVarValue('L:A32NX_FLAPS_HANDLE_INDEX', 'Number') === 4 + ? SimVar.GetSimVarValue('L:A32NX_SPEEDS_VMAX', 'Number') - 5 + : SimVar.GetSimVarValue('L:A32NX_SPEEDS_VFEN', 'Number'), + ), + ), + ); + } + + static convertKCasToMach( + Vc, + T = _convertCtoK(Simplane.getAmbientTemperature()), + p = SimVar.GetSimVarValue('AMBIENT PRESSURE', 'millibar'), + ) { + return _convertKTASToMach(_convertKCasToKTAS(Vc, T, p), T); + } + + /** @private */ + static interpolateTable(table, alt) { + if (alt <= table[0][0]) { + return vmca[0][1]; + } + if (alt >= table[table.length - 1][0]) { + table[table.length - 1][1]; + } + for (let i = 0; i < table.length - 1; i++) { + if (alt >= table[i][0] && alt <= table[i + 1][0]) { + const d = (alt - table[i][0]) / (table[i + 1][0] - table[i][0]); + return Avionics.Utils.lerpAngle(table[i][1], table[i + 1][1], d); + } + } + } + + /** + * Get VMCA (minimum airborne control speed) for a given altitude + * @param {number} altitude Altitude in feet + * @returns VMCA in knots + */ + static getVmca(altitude) { + return this.interpolateTable(vmca, altitude); + } + + /** + * Get VMCG (minimum ground control speed) for a given altitude + * @param {number} altitude Altitude in feet + * @returns VMCG in knots + */ + static getVmcg(altitude) { + return this.interpolateTable(vmcg, altitude); + } + + /** + * Get Vs1g for the given config + * + * @param {number} mass mass of the aircraft in tons + * @param {number} conf 0 - Clean config, 1 - Config 1 + F, 2 - Config 2, 3 - Config 3, 4 - Config Full, 5 - Config 1. + * @param {boolean} gearDown true if the gear is down + */ + static getVs1g(mass, conf, gearDown) { + return vs[conf][_correctMass(mass)](mass, gearDown ? 1 : 0); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AirportsMonitor.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AirportsMonitor.ts new file mode 100644 index 00000000000..a214900ffc3 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AirportsMonitor.ts @@ -0,0 +1,284 @@ +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { NearbyFacilities } from '@fmgc/navigation/NearbyFacilities'; +import { FmgcFlightPhase } from '@shared/flightphase'; +import { A32NX_Util } from '../../../../shared/src/A32NX_Util'; +import { Airport, NXUnits } from '@flybywiresim/fbw-sdk'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { bearingTo, distanceTo } from 'msfs-geo'; +import { Predictions } from '@fmgc/guidance/vnav/Predictions'; +import { UnitType } from '@microsoft/msfs-sdk'; +import { A320AircraftConfig } from '@fmgc/flightplanning/A320AircraftConfig'; + +interface AirportRow { + airport?: Airport; + + /** Bearing to the airport in degrees from true north. */ + bearing?: number; + /** Distance calculated to the airport in nautical miles. */ + distance?: number; + /** Estimated time of arrival the airport. */ + eta?: Date; + /** Estimated fuel on board upon arrival at the airport in tonnes. */ + efob?: number; +} + +export class CDUAirportsMonitor { + private static readonly NUM_AIRPORTS = 4; + private static readonly airportRows: AirportRow[] = Array.from( + { length: CDUAirportsMonitor.NUM_AIRPORTS + 1 }, + () => ({}), + ); + /** Effective wind for each airport (entered by pilot), in knots, +ve = tailwind. */ + private static readonly effectiveWinds = new Map(); + private static pilotAirport = this.airportRows[CDUAirportsMonitor.NUM_AIRPORTS]; + private static magVar = 0; + + static ShowPage(mcdu: LegacyFmsPageInterface, frozen = false, showEfob = false, isRefresh = false) { + mcdu.page.Current = mcdu.page.AirportsMonitor; + mcdu.clearDisplay(); + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + CDUAirportsMonitor.ShowPage(mcdu, frozen, showEfob, true); + }, mcdu.PageTimeout.Default); + + const template = [ + ['CLOSEST AIRPORTS'], + [ + '', + '', + showEfob + ? '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0EFOB\xa0\xa0\xa0EFF\xa0WIND\xa0' + : '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0BRG\xa0\xa0\xa0DIST\xa0\xa0UTC\xa0', + ], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ['', '', ''], + ]; + + const ppos = mcdu.navigation.getPpos(); + if (ppos === null) { + mcdu.setTemplate(template); + if (!isRefresh) { + mcdu.setScratchpadMessage(NXSystemMessages.acPositionInvalid); + } + return; + } + + if (!frozen && !showEfob) { + const newAirports = [...NearbyFacilities.getInstance().getAirports()]; + newAirports.sort((a, b) => a.distance - b.distance); + newAirports.length = CDUAirportsMonitor.NUM_AIRPORTS; + for (let i = 0; i < this.NUM_AIRPORTS; i++) { + if (newAirports[i]) { + this.airportRows[i].airport = newAirports[i]; + } else { + this.airportRows[i].airport = undefined; + } + this.airportRows[i].bearing = undefined; + this.airportRows[i].distance = undefined; + this.airportRows[i].efob = undefined; + this.airportRows[i].eta = undefined; + } + } + + // clear out old winds + for (const key of this.effectiveWinds.keys()) { + if (!this.airportRows.find((row) => row.airport?.ident === key)) { + this.effectiveWinds.delete(key); + } + } + + if (showEfob) { + for (let i = 0; i <= CDUAirportsMonitor.NUM_AIRPORTS; i++) { + if (this.airportRows[i].airport) { + // effective wind entry + mcdu.onRightInput[i] = (value) => { + if (value === Keypad.clrValue) { + this.effectiveWinds.delete(this.airportRows[i].airport.ident); + } else { + const match = value.match(/^(T|TL|\+|-|H|HD)?(\d{1,3})$/); + if (match !== null) { + const magnitude = parseInt(match[2]); + if (magnitude > 250) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + return; + } + + let sign = 1; + switch (match[1]) { + case '-': + case 'H': + case 'HD': + sign = -1; + break; + } + + this.effectiveWinds.set(this.airportRows[i].airport.ident, sign * magnitude); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + return; + } + } + CDUAirportsMonitor.ShowPage(mcdu, frozen, showEfob, true); + }; + } + } + } + + this.updatePredictions(mcdu); + + for (let i = 0; i < this.airportRows.length; i++) { + this.renderAirportRow(template, i, showEfob, mcdu.isTrueRefMode); + } + + // pilot airport entry + mcdu.onLeftInput[4] = async (value) => { + if (value === Keypad.clrValue) { + this.pilotAirport.airport = undefined; + this.pilotAirport.bearing = undefined; + this.pilotAirport.distance = undefined; + this.pilotAirport.efob = undefined; + this.pilotAirport.eta = undefined; + } else if (!value.match(/^[A-Z0-9]{4}$/)) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + return; + } else { + const airport = await mcdu.navigationDatabase.searchAirport(value); + if (!airport) { + mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); + return; + } + + this.pilotAirport.airport = airport; + } + CDUAirportsMonitor.ShowPage(mcdu, frozen, showEfob, true); + }; + + if (showEfob) { + template[11][2] = 'LIST FROZEN'; + template[12][2] = '{white}{RETURN{end}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0'; + + mcdu.onLeftInput[5] = () => CDUAirportsMonitor.ShowPage(mcdu, frozen, false, true); + } else if (frozen) { + template[11][2] = 'LIST FROZEN'; + template[12][2] = '{cyan}{UNFREEZE{end}\xa0\xa0\xa0\xa0\xa0{white}EFOB/WIND>{end}'; + + mcdu.onLeftInput[5] = () => CDUAirportsMonitor.ShowPage(mcdu, false, showEfob, true); + mcdu.onRightInput[5] = () => CDUAirportsMonitor.ShowPage(mcdu, frozen, true, true); + } else { + template[12][2] = '{cyan}{FREEZE{end}\xa0\xa0\xa0\xa0\xa0\xa0\xa0{white}EFOB/WIND>{end}'; + + mcdu.onLeftInput[5] = () => CDUAirportsMonitor.ShowPage(mcdu, true, showEfob, true); + mcdu.onRightInput[5] = () => CDUAirportsMonitor.ShowPage(mcdu, frozen, true, true); + } + + mcdu.setTemplate(template); + } + + private static updatePredictions(mcdu: LegacyFmsPageInterface) { + const ppos = mcdu.navigation.getPpos(); + const zfwLb = UnitType.POUND.convertFrom(mcdu.zeroFuelWeight ?? 0, UnitType.TONNE); + const fobT = mcdu.getFOB(); + const fobLb = UnitType.POUND.convertFrom(fobT ?? 0, UnitType.TONNE); + const sat = mcdu.navigation?.getStaticAirTemperature() ?? null; + + const doCruisePreds = + mcdu.flightPhaseManager.phase === FmgcFlightPhase.Cruise && + mcdu.cruiseLevel !== null && + isFinite(mcdu.zeroFuelWeight) && + fobT !== undefined && + zfwLb > 0 && + sat !== null; + + const isaDev = doCruisePreds ? sat - mcdu.getIsaTemp(mcdu.cruiseLevel * 100) : 0; + + for (const rowData of this.airportRows) { + if (ppos && rowData.airport) { + rowData.bearing = bearingTo(ppos, rowData.airport.location); + rowData.distance = distanceTo(ppos, rowData.airport.location); + + if (doCruisePreds) { + const desAlt = mcdu.cruiseLevel * 100 - rowData.airport.location.alt; + const desFuelCoef = 0.412; + const desBurn = (desFuelCoef * desAlt) / 41000; + const desDistCoef = 123; + const desDist = (desDistCoef * desAlt) / 41000; + const desTime = (desDist / 300) * 3600_000; + + const cruiseCas = mcdu.managedSpeedCruise ?? 290; + const cruiseMach = mcdu.managedSpeedCruiseMach ?? 0.78; + + const cruiseDist = Math.max(0, rowData.distance - desDist); + const cruisePerf = Predictions.levelFlightStep( + A320AircraftConfig, + mcdu.cruiseLevel * 100, + cruiseDist, + cruiseCas, + cruiseMach, + zfwLb, + fobLb, + -(this.effectiveWinds.get(rowData.airport.ident) ?? 0), + isaDev, + mcdu.tropo, + ); + + const cruiseBurn = UnitType.TONNE.convertFrom(cruisePerf.fuelBurned, UnitType.POUND); + rowData.efob = fobT - cruiseBurn + desBurn; + rowData.eta = new Date(Date.now() + cruisePerf.timeElapsed * 1000 + desTime); + } else { + rowData.efob = undefined; + rowData.eta = undefined; + } + } else { + rowData.bearing = undefined; + rowData.distance = undefined; + rowData.efob = undefined; + rowData.eta = undefined; + } + } + + this.magVar = ppos ? Facilities.getMagVar(ppos.lat, ppos.long) : 0; + } + + private static renderAirportRow(template: string[][], index: number, showEfob: boolean, showTrueBearing: boolean) { + const rowData = this.airportRows[index]; + const templateIndex = 2 + 2 * index; + const isPilotAirport = index === CDUAirportsMonitor.NUM_AIRPORTS; + + template[templateIndex][2] = + `{${isPilotAirport ? 'cyan' : 'green'}}${rowData.airport?.ident.padEnd(4, '\xa0') ?? (isPilotAirport ? '[\xa0\xa0]' : '\xa0\xa0\xa0\xa0')}\xa0\xa0\xa0\xa0{end}`; + + if (showEfob) { + const effectiveWind = rowData.airport ? this.effectiveWinds.get(rowData.airport.ident) : undefined; + const formattedWind = + effectiveWind !== undefined + ? effectiveWind >= 0 + ? `TL${effectiveWind.toFixed(0).padStart(3, '0')}` + : `HD${Math.abs(effectiveWind).toFixed(0).padStart(3, '0')}` + : rowData.airport + ? '[\xa0\xa0\xa0]' + : '\xa0\xa0\xa0\xa0\xa0'; + + template[templateIndex][2] += + `{green}${rowData.efob !== undefined ? NXUnits.kgToUser(rowData.efob).toFixed(1).padStart(4, '\xa0') : '\xa0\xa0\xa0\xa0'}{end}\xa0\xa0${index === 0 ? '{small}{white}[KTS]{end}{end}' : '\xa0\xa0\xa0\xa0\xa0'}{cyan}${formattedWind}{end}`; + } else { + const bearing = + rowData.bearing !== undefined && !showTrueBearing + ? A32NX_Util.trueToMagnetic(rowData.bearing, this.magVar) + : rowData.bearing; + + template[templateIndex][2] += + `{green}${bearing !== undefined ? bearing.toFixed(0).padStart(3, '0') + (showTrueBearing ? 'T' : '°') : '\xa0\xa0\xa0\xa0'}\xa0\xa0${rowData.distance !== undefined ? Math.min(rowData.distance, 9999).toFixed(0).padStart(4) : '\xa0\xa0\xa0\xa0'}\xa0\xa0${rowData.eta !== undefined ? rowData.eta.getUTCHours().toString().padStart(2, '0') : '\xa0\xa0'}${rowData.eta !== undefined ? rowData.eta.getUTCMinutes().toString().padStart(2, '0') : '\xa0\xa0'}{end}`; + } + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AirwaysFromWaypointPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AirwaysFromWaypointPage.ts new file mode 100644 index 00000000000..4ad3b54eb7d --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AirwaysFromWaypointPage.ts @@ -0,0 +1,291 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { CDUFlightPlanPage } from './A320_Neo_CDU_FlightPlanPage'; +import { CDULateralRevisionPage } from './A320_Neo_CDU_LateralRevisionPage'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { Airway, Fix } from '@flybywiresim/fbw-sdk'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +export class A320_Neo_CDU_AirwaysFromWaypointPage { + static ShowPage( + mcdu: LegacyFmsPageInterface, + reviseIndex: number, + pendingAirway: Airway, + lastIndex: number, + forPlan: number, + inAlternate: boolean, + ) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AirwaysFromWaypointPage; + + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + const waypoint = targetPlan.legElementAt(reviseIndex); + + const fpIsSec = forPlan >= FlightPlanIndex.FirstSecondary; + const fpIsTmpy = forPlan === FlightPlanIndex.Active && mcdu.flightPlanService.hasTemporary; + + let prevIcao = waypoint.definition.waypoint.databaseId; + let prevFpIndex = reviseIndex; + + if (!targetPlan.pendingAirways) { + mcdu.flightPlanService.startAirwayEntry(reviseIndex, forPlan, inAlternate); + } + + const rows = [['----'], [''], [''], [''], ['']]; + const subRows = [['VIA', ''], [''], [''], [''], ['']]; + const allRows = lastIndex ? A320_Neo_CDU_AirwaysFromWaypointPage._GetAllRows(targetPlan) : []; + + let rowBottomLine = [' { + mcdu.eraseTemporaryFlightPlan(() => { + CDULateralRevisionPage.ShowPage(mcdu, targetPlan.elementAt(reviseIndex), reviseIndex, forPlan, inAlternate); + }); + }; + + if (fpIsSec && targetPlan.pendingAirways && targetPlan.pendingAirways.elements.length > 0) { + rowBottomLine = [' { + targetPlan.pendingAirways.finalize(); // TODO replace with fps call (fms-v2) + + mcdu.updateConstraints(); + + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }; + } else if (fpIsTmpy && targetPlan.pendingAirways && targetPlan.pendingAirways.elements.length > 0) { + rowBottomLine = ['{ERASE[color]amber', 'INSERT*[color]amber']; + + mcdu.onLeftInput[5] = async () => { + mcdu.eraseTemporaryFlightPlan(() => { + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }; + + mcdu.onRightInput[5] = async () => { + mcdu.insertTemporaryFlightPlan(() => { + targetPlan.pendingAirways = undefined; + + mcdu.updateConstraints(); + + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }; + } + + let showInput = false; + for (let i = 0; i < rows.length; i++) { + if (allRows[i]) { + const [airwayIdent, termIdent, fromIcao, fromIndex] = allRows[i]; + rows[i] = [airwayIdent, termIdent]; + subRows[i] = ['\xa0VIA', 'TO\xa0']; + prevIcao = fromIcao; + prevFpIndex = fromIndex; + } else if (!showInput) { + showInput = true; + if (!pendingAirway) { + subRows[i] = ['\xa0VIA', '']; + rows[i] = ['[\xa0\xa0\xa0][color]cyan', '']; + + mcdu.onLeftInput[i] = async (value, scratchpadCallback) => { + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + + if (value.length > 0) { + const elements = targetPlan.pendingAirways.elements; + const tailElement = elements[elements.length - 1]; + + const lastFix = tailElement ? tailElement.to : targetPlan.legElementAt(prevFpIndex).terminationWaypoint(); + + const airway = await this._getAirway( + mcdu, + prevFpIndex, + tailElement ? tailElement.airway : undefined, + lastFix, + value, + ).catch(console.error); + + if (airway) { + const result = targetPlan.pendingAirways.thenAirway(airway); + + A320_Neo_CDU_AirwaysFromWaypointPage.ShowPage( + mcdu, + reviseIndex, + airway, + result ? 1 : -1, + forPlan, + inAlternate, + ); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.awyWptMismatch); + scratchpadCallback(); + } + } + }; + } else if (pendingAirway) { + subRows[i] = ['\xa0VIA', 'TO\xa0']; + rows[i] = [`${pendingAirway.ident}[color]cyan`, '[\xa0\xa0\xa0][color]cyan']; + + mcdu.onRightInput[i] = (value, scratchpadCallback) => { + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + + if (value.length > 0) { + WaypointEntryUtils.getOrCreateWaypoint(mcdu, value, false).then((wp) => { + if (wp) { + const result = targetPlan.pendingAirways.thenTo(wp); + + A320_Neo_CDU_AirwaysFromWaypointPage.ShowPage( + mcdu, + reviseIndex, + undefined, + result ? 1 : -1, + forPlan, + inAlternate, + ); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.awyWptMismatch); + scratchpadCallback(); + } + }); + } + }; + if (i + 1 < rows.length) { + rows[i + 1] = ['[\xa0\xa0\xa0][color]cyan', '']; + subRows[i + 1] = ['\xa0VIA', '']; + + mcdu.onLeftInput[i + 1] = async (value, scratchpadCallback) => { + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + + if (value.length > 0) { + const airway = await this._getFirstIntersection(mcdu, pendingAirway, prevIcao, value).catch( + console.error, + ); + if (airway) { + const result = targetPlan.pendingAirways.thenAirway(airway); + + A320_Neo_CDU_AirwaysFromWaypointPage.ShowPage( + mcdu, + reviseIndex, + airway, + result ? 1 : -1, + forPlan, + inAlternate, + ); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.awyWptMismatch); + scratchpadCallback(); + } + } + }; + } + } + } + } + + mcdu.setTemplate([ + ['AIRWAYS {small}FROM {end}{green}' + waypoint.ident + '{end}'], + subRows[0], + rows[0], + subRows[1], + rows[1], + subRows[2], + rows[2], + subRows[3], + rows[3], + subRows[4], + rows[4], + [''], + rowBottomLine, + ]); + } + + /** + * @param plan {FlightPlan} + */ + static _GetAllRows(plan) { + const allRows = []; + const elements = plan.pendingAirways.elements; + + for (let i = 0; i < elements.length; i++) { + const element = elements[i]; + + if (element.to) { + allRows.push([ + `{cyan}${element.airway.ident}{end}`, + `{cyan}${element.isAutoConnected ? '{small}' : '{big}'}${element.to.ident}{end}{end}`, + element.to.databaseId, + i, + ]); + } + } + + return allRows; + } + + static async _getAirway( + mcdu: LegacyFmsPageInterface, + fromFpIndex: number, + lastAirway: Airway, + lastFix: Fix, + value: string, + ): Promise { + const airways = await mcdu.navigationDatabase.searchAirway(value, lastFix); + + let matchingAirway = lastFix && airways.find((it) => it.fixes.some((fix) => fix.ident === lastFix.ident)); + if (!matchingAirway && lastAirway) { + matchingAirway = airways.find((it) => + it.fixes.some((fix) => lastAirway.fixes.some((endFix) => endFix.databaseId === fix.databaseId)), + ); + } + + return matchingAirway; + } + + /** + * Distance is measured in number of fixes, not a real distance unit. + * Searching around current fixes index. + */ + static async _getFirstIntersection( + mcdu: LegacyFmsPageInterface, + prevAirway: Airway, + prevAirwayDatabaseId: string, + nextAirwayIdent: string, + ) { + const prevAirwayFixes = prevAirway.fixes; + + const prevAirwayStartIndex = prevAirwayFixes.findIndex((fix) => fix.databaseId === prevAirwayDatabaseId); + + if (prevAirwayStartIndex < 0) { + throw new Error(`Cannot find waypoint ${prevAirwayDatabaseId} in airway ${prevAirway.ident}`); + } + + for (let i = 0; i < prevAirwayFixes.length; i++) { + if (prevAirwayStartIndex + i < prevAirwayFixes.length) { + const airway = await this._getRoute(mcdu, nextAirwayIdent, prevAirwayFixes[prevAirwayStartIndex + i]).catch( + console.error, + ); + + if (airway) { + return airway; + } + } + if (prevAirwayStartIndex - i >= 0) { + const airway = await this._getRoute(mcdu, nextAirwayIdent, prevAirwayFixes[prevAirwayStartIndex - i]).catch( + console.error, + ); + + if (airway) { + return airway; + } + } + } + } + + static async _getRoute(mcdu: LegacyFmsPageInterface, airwayName: string, fixOnAirway: Fix) { + const airways = await mcdu.navigationDatabase.searchAirway(airwayName, fixOnAirway); + const matchingAirway = airways.find((it) => it.fixes.some((fix) => fix.databaseId === fixOnAirway.databaseId)); + + return matchingAirway; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AvailableArrivalsPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AvailableArrivalsPage.ts new file mode 100644 index 00000000000..40da09fdb23 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AvailableArrivalsPage.ts @@ -0,0 +1,738 @@ +/* + * A32NX + * Copyright (C) 2020-2021, 2025 FlyByWire Simulations and its contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import { + Airport, + AirportSubsectionCode, + Approach, + ApproachType, + ApproachUtils, + Arrival, + Runway, + RunwayUtils, +} from '@flybywiresim/fbw-sdk'; +import { CDUFlightPlanPage } from './A320_Neo_CDU_FlightPlanPage'; +import { NXFictionalMessages, NXSystemMessages } from '../messages/NXSystemMessages'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; + +const ApproachTypeOrder = Object.freeze({ + [ApproachType.Mls]: 0, + [ApproachType.MlsTypeA]: 1, + [ApproachType.MlsTypeBC]: 2, + [ApproachType.Ils]: 3, + [ApproachType.Gls]: 4, + [ApproachType.Igs]: 5, + [ApproachType.Loc]: 6, + [ApproachType.LocBackcourse]: 7, + [ApproachType.Lda]: 8, + [ApproachType.Sdf]: 9, + [ApproachType.Fms]: 10, + [ApproachType.Gps]: 11, + [ApproachType.Rnav]: 12, + [ApproachType.VorDme]: 13, + [ApproachType.Vortac]: 13, // VORTAC and VORDME are intentionally the same + [ApproachType.Vor]: 14, + [ApproachType.NdbDme]: 15, + [ApproachType.Ndb]: 16, + [ApproachType.Unknown]: 17, +}); + +const ArrivalPagination = Object.freeze({ + ARR_PAGE: 3, + TRNS_PAGE: 2, + VIA_PAGE: 3, +}); + +const Labels = Object.freeze({ + NO_TRANS: 'NO TRANS', + NO_VIA: 'NO VIA', + NO_STAR: 'NO STAR', +}); + +export class CDUAvailableArrivalsPage { + static async ShowPage( + mcdu: LegacyFmsPageInterface, + airport: Airport, + pageCurrent = 0, + starSelection = false, + forPlan = FlightPlanIndex.Active, + inAlternate = false, + ) { + /** @type {BaseFlightPlan} */ + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + + const isTemporary = targetPlan.index === FlightPlanIndex.Temporary; + + const selectedApproachId = targetPlan.approach ? targetPlan.approach.databaseId : targetPlan.approach; + const selectedStarId = targetPlan.arrival ? targetPlan.arrival.databaseId : targetPlan.arrival; + const selectedTransitionId = targetPlan.arrivalEnrouteTransition + ? targetPlan.arrivalEnrouteTransition.databaseId + : targetPlan.arrivalEnrouteTransition; + + const flightPlanAccentColor = isTemporary ? 'yellow' : 'green'; + + const ilss = await mcdu.navigationDatabase.backendDatabase.getIlsAtAirport(targetPlan.destinationAirport.ident); + + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AvailableArrivalsPage; + let selectedApproachCell = '------'; + let selectedViasCell = '------'; + let selectedTransitionCell = '------'; + let selectedApproachCellColor = 'white'; + let selectedViasCellColor = 'white'; + let selectedTransitionCellColor = 'white'; + + let viasPageLabel = ''; + let viasPageLine = ''; + + const selectedApproach = targetPlan.approach; + + if (selectedApproach && selectedApproach.ident) { + selectedApproachCell = ApproachUtils.shortApproachName(selectedApproach); + selectedApproachCellColor = flightPlanAccentColor; + + const selectedApproachTransition = targetPlan.approachVia; + const availableVias = targetPlan.availableApproachVias; + + if (availableVias.length === 0 || selectedApproachTransition === null) { + selectedViasCell = 'NONE'; + selectedViasCellColor = flightPlanAccentColor; + } else if (selectedApproachTransition) { + selectedViasCell = selectedApproachTransition.ident; + selectedViasCellColor = flightPlanAccentColor; + } + } else if (!selectedApproach && targetPlan.destinationRunway) { + selectedApproachCell = RunwayUtils.runwayString(targetPlan.destinationRunway.ident); + selectedApproachCellColor = flightPlanAccentColor; + + // Runway-only approaches have no VIAs + selectedViasCell = 'NONE'; + selectedViasCellColor = flightPlanAccentColor; + } + + let selectedStarCell = '------'; + let selectedStarCellColor = 'white'; + + const selectedArrival = targetPlan.arrival; + const availableArrivals = targetPlan.availableArrivals; + + if (selectedArrival) { + selectedStarCell = selectedArrival.ident; + selectedStarCellColor = flightPlanAccentColor; + + const selectedTransition = targetPlan.arrivalEnrouteTransition; + const availableTransitions = selectedArrival.enrouteTransitions; + + if (selectedTransition) { + selectedTransitionCell = selectedTransition.ident; + selectedTransitionCellColor = flightPlanAccentColor; + } else if (availableTransitions.length === 0 || selectedTransition === null) { + selectedTransitionCell = 'NONE'; + selectedTransitionCellColor = flightPlanAccentColor; + } + } else if (selectedArrival === null || availableArrivals.length === 0) { + selectedStarCell = 'NONE'; + selectedStarCellColor = flightPlanAccentColor; + + selectedTransitionCell = 'NONE'; + selectedTransitionCellColor = flightPlanAccentColor; + } + + const approaches = targetPlan.availableApproaches; + const runways = targetPlan.availableDestinationRunways; + + // Sort the approaches in Honeywell's documented order + const sortedApproaches = approaches + .slice() + // The A320 cannot fly TACAN approaches + .filter(({ type }) => type !== ApproachType.Tacan) + // Filter out approaches with no matching runway + // Approaches not going to a specific runway (i.e circling approaches are filtered out at DB level) + .filter((a) => !!runways.find((rw) => rw.ident === a.runwayIdent)) + // Sort the approaches in Honeywell's documented order, and alphabetical in between + .sort((a, b) => + a.type != b.type ? ApproachTypeOrder[a.type] - ApproachTypeOrder[b.type] : a.ident.localeCompare(b.ident), + ); + const allApproaches = (sortedApproaches as (Runway | Approach)[]).concat( + // Runway-by-itself approaches + runways, + ); + const rows = [[''], [''], [''], [''], [''], [''], [''], ['']]; + + const matchingArrivals: { arrival: Arrival; arrivalIndex: number }[] = []; + + if (!starSelection) { + for (let i = 0; i < ArrivalPagination.ARR_PAGE; i++) { + const index = i + pageCurrent * ArrivalPagination.ARR_PAGE; + + const approachOrRunway = allApproaches[index]; + if (!approachOrRunway) { + break; + } + + if (approachOrRunway.subSectionCode === AirportSubsectionCode.ApproachProcedures) { + let runwayLength = '----'; + let runwayCourse = '---'; + + const isSelected = selectedApproach && selectedApproachId === approachOrRunway.databaseId; + const color = isSelected && !isTemporary ? 'green' : 'cyan'; + + const runway = targetPlan.availableDestinationRunways.find((rw) => rw.ident === approachOrRunway.runwayIdent); + if (runway) { + runwayLength = runway.length.toFixed(0); // TODO imperial length pin program + runwayCourse = Utils.leadingZeros(Math.round(runway.magneticBearing), 3); + + const finalLeg = approachOrRunway.legs[approachOrRunway.legs.length - 1]; + const matchingIls = + approachOrRunway.type === ApproachType.Ils + ? ilss.find( + (ils) => + finalLeg && + finalLeg.recommendedNavaid && + ils.databaseId === finalLeg.recommendedNavaid.databaseId, + ) + : undefined; + const hasIls = !!matchingIls; + const ilsText = hasIls ? `${matchingIls.ident.padStart(6)}/${matchingIls.frequency.toFixed(2)}` : ''; + + rows[2 * i] = [ + `{${color}}${!isSelected ? '{' : '{sp}'}${ApproachUtils.shortApproachName(approachOrRunway)}{end}`, + '', + `{sp}{sp}{sp}${runwayLength}{small}M{end}[color]${color}`, + ]; + rows[2 * i + 1] = [`{${color}}{sp}{sp}{sp}${runwayCourse}${ilsText}{end}`]; + } + + mcdu.onLeftInput[i + 2] = async (_, scratchpadCallback) => { + // Clicking the already selected approach is not allowed + if (!isSelected) { + try { + await mcdu.flightPlanService.setApproach(approachOrRunway.databaseId, forPlan, inAlternate); + + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, pageCurrent, false, forPlan, inAlternate); + }); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + + scratchpadCallback(); + } + }; + } else { + const runwayLength = approachOrRunway.length.toFixed(0); // TODO imperial length pin program + const runwayCourse = Utils.leadingZeros(Math.round(approachOrRunway.magneticBearing), 3); + + const isSelected = + !selectedApproach && + targetPlan.destinationRunway && + approachOrRunway.databaseId === targetPlan.destinationRunway.databaseId; + const color = isSelected && !isTemporary ? 'green' : 'cyan'; + + rows[2 * i] = [ + `{${color}}${!isSelected ? '{' : '{sp}'}${RunwayUtils.runwayString(approachOrRunway.ident)}{end}`, + '', + `{sp}{sp}{sp}${runwayLength}{small}M{end}[color]${color}`, + ]; + rows[2 * i + 1] = ['{sp}{sp}{sp}{sp}' + runwayCourse + '[color]cyan']; + + mcdu.onLeftInput[i + 2] = async (_, scratchpadCallback) => { + // Clicking the already selected runway is not allowed + if (!isSelected) { + try { + await mcdu.flightPlanService.setApproach(undefined, forPlan, inAlternate); + await mcdu.flightPlanService.setDestinationRunway(approachOrRunway.ident, forPlan, inAlternate); + + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, pageCurrent, false, forPlan, inAlternate); + }); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + + scratchpadCallback(); + } + }; + } + } + } else { + const destinationRunway = targetPlan.destinationRunway; + + if (destinationRunway) { + const arrivals = [...targetPlan.availableArrivals].sort((a, b) => a.ident.localeCompare(b.ident)); + + for (let i = 0; i < arrivals.length; i++) { + const arrival = arrivals[i]; + + if (arrival.runwayTransitions.length) { + for (let j = 0; j < arrival.runwayTransitions.length; j++) { + const runwayTransition = arrival.runwayTransitions[j]; + if (runwayTransition) { + // Check if selectedRunway matches a transition on the approach (and also checks for Center runways) + if ( + runwayTransition.ident === destinationRunway.ident || + (runwayTransition.ident.charAt(6) === 'B' && + runwayTransition.ident.substring(4, 6) === destinationRunway.ident.substring(4, 6)) + ) { + matchingArrivals.push({ arrival: arrival, arrivalIndex: i }); + } + } + } + } else { + //add the arrival even if it isn't runway specific + matchingArrivals.push({ arrival: arrival, arrivalIndex: i }); + } + } + } else { + for (let i = 0; i < targetPlan.availableArrivals.length; i++) { + const arrival = targetPlan.availableArrivals[i]; + matchingArrivals.push({ arrival: arrival, arrivalIndex: i }); + } + } + for (let i = 0; i < ArrivalPagination.ARR_PAGE; i++) { + let index = i + pageCurrent * ArrivalPagination.ARR_PAGE; + if (index === 0) { + const isSelected = selectedArrival === null; + const color = isSelected && !isTemporary ? 'green' : 'cyan'; + + rows[2 * i] = [`{${color}}${!isSelected ? '{' : '{sp}'}${Labels.NO_STAR}{end}`]; + + if (!isSelected) { + mcdu.onLeftInput[i + 2] = async () => { + try { + await mcdu.flightPlanService.setArrival(null, forPlan, inAlternate); + + const availableVias = targetPlan.availableApproachVias; + + if (selectedApproach !== undefined && availableVias.length > 0) { + CDUAvailableArrivalsPage.ShowViasPage(mcdu, airport, 0, forPlan, inAlternate); + } else { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); + } + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); + }); + } + }; + } + } else { + index--; + if (matchingArrivals[index]) { + const star = matchingArrivals[index].arrival; + const starDatabaseId = matchingArrivals[index].arrival.databaseId; + const isSelected = selectedStarId === starDatabaseId; + const color = isSelected && !isTemporary ? 'green' : 'cyan'; + + rows[2 * i] = [`{${color}}${!isSelected ? '{' : '{sp}'}${star.ident}{end}`]; + + mcdu.onLeftInput[i + 2] = async (_, scratchpadCallback) => { + // Clicking the already selected star is not allowed + if (!isSelected) { + const destinationRunway = targetPlan.destinationRunway; + + const arrivalRunway = destinationRunway + ? star.runwayTransitions.find((t) => { + return t.ident === destinationRunway.ident; + }) + : undefined; + + try { + if (arrivalRunway !== undefined) { + await mcdu.flightPlanService.setDestinationRunway(arrivalRunway.ident, forPlan, inAlternate); + } + + await mcdu.flightPlanService.setArrival(starDatabaseId, forPlan, inAlternate); + + const availableVias = targetPlan.availableApproachVias; + + if (selectedApproach !== undefined && availableVias.length > 0) { + CDUAvailableArrivalsPage.ShowViasPage(mcdu, airport, 0, forPlan, inAlternate); + } else { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); + } + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); + }); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + + scratchpadCallback(); + } + }; + } + } + } + + if (selectedArrival) { + if (selectedArrival.enrouteTransitions.length > 0) { + const isNoTransSelected = selectedTransitionId === null; + const color = isNoTransSelected && !isTemporary ? 'green' : 'cyan'; + + rows[0][1] = `${Labels.NO_TRANS}${!isNoTransSelected ? '}' : '{sp}'}[color]${color}`; + + mcdu.onRightInput[2] = async () => { + try { + await mcdu.flightPlanService.setArrivalEnrouteTransition(null, forPlan, inAlternate); + + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); + }); + } + }; + + for (let i = 0; i < ArrivalPagination.TRNS_PAGE; i++) { + const index = i + pageCurrent * ArrivalPagination.TRNS_PAGE; + + const transition = selectedArrival.enrouteTransitions[index]; + if (transition) { + const isSelected = selectedTransitionId === transition.databaseId; + const color = isSelected && !isTemporary ? 'green' : 'cyan'; + + rows[2 * (i + 1)][1] = `{${color}}${transition.ident}${!isSelected ? '}' : '{sp}'}{end}`; + + // Clicking the already selected transition is not allowed + mcdu.onRightInput[i + 3] = async (_, scratchpadCallback) => { + if (!isSelected) { + try { + await mcdu.flightPlanService.setArrivalEnrouteTransition( + transition.databaseId, + forPlan, + inAlternate, + ); + + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, pageCurrent, true, forPlan, inAlternate); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); + }); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + + scratchpadCallback(); + } + }; + } + } + } + } + + if (selectedApproach) { + const availableApproachVias = targetPlan.availableApproachVias; + + if (availableApproachVias.length > 0) { + viasPageLabel = '{sp}APPR'; + viasPageLine = ' { + CDUAvailableArrivalsPage.ShowViasPage(mcdu, airport, 0, forPlan, inAlternate); + }; + } + } + } + + let bottomLine = [' { + mcdu.eraseTemporaryFlightPlan(() => { + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }; + mcdu.onRightInput[5] = async () => { + mcdu.insertTemporaryFlightPlan(() => { + mcdu.updateTowerHeadwind(); + mcdu.updateConstraints(); + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }; + } else { + mcdu.onLeftInput[5] = () => { + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }; + } + let up = false; + let down = false; + const maxPage = starSelection + ? selectedArrival + ? Math.max( + Math.ceil(selectedArrival.enrouteTransitions.length / ArrivalPagination.TRNS_PAGE) - 1, + Math.ceil((matchingArrivals.length + 1) / ArrivalPagination.ARR_PAGE) - 1, + ) + : Math.ceil((matchingArrivals.length + 1) / ArrivalPagination.ARR_PAGE) - 1 + : Math.ceil(sortedApproaches.length / ArrivalPagination.ARR_PAGE) - 1; + if (pageCurrent < maxPage) { + mcdu.onUp = () => { + pageCurrent++; + if (pageCurrent < 0) { + pageCurrent = 0; + } + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, pageCurrent, starSelection, forPlan, inAlternate); + }; + up = true; + } + if (pageCurrent > 0) { + mcdu.onDown = () => { + pageCurrent--; + if (pageCurrent < 0) { + pageCurrent = 0; + } + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, pageCurrent, starSelection, forPlan, inAlternate); + }; + down = true; + } + mcdu.setArrows(up, down, true, true); + mcdu.setTemplate([ + ['ARRIVAL {small}TO{end} {green}' + airport.ident + '{sp}{end}'], + ['{sp}APPR', 'STAR{sp}', '{sp}VIA'], + [ + `{${selectedApproachCellColor}}${selectedApproachCell.padEnd(10)}{end}{${selectedViasCellColor}}${selectedViasCell}{end}`, + selectedStarCell + '[color]' + selectedStarCellColor, + ], + [viasPageLabel, 'TRANS{sp}'], + [viasPageLine, selectedTransitionCell + '[color]' + selectedTransitionCellColor], + [ + '{big}' + (starSelection ? 'STARS' : 'APPR').padEnd(5) + '{end}{sp}{sp}AVAILABLE', + starSelection ? '{big}TRANS{end}' : '', + '', + ], + rows[0], + rows[1], + rows[2], + rows[3], + rows[4], + rows[5], + bottomLine, + ]); + mcdu.onPrevPage = () => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, !starSelection, forPlan, inAlternate); + }; + mcdu.onNextPage = mcdu.onPrevPage; + } + + static ShowViasPage( + mcdu: LegacyFmsPageInterface, + airport: Airport, + pageCurrent = 0, + forPlan = FlightPlanIndex.Active, + inAlternate = false, + ) { + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + + const isTemporary = targetPlan.index === FlightPlanIndex.Temporary; + const planColor = isTemporary ? 'yellow' : 'green'; + + const availableApproachVias = targetPlan.availableApproachVias; + + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AvailableArrivalsPageVias; + let selectedApproachCell = '------'; + let selectedApproachCellColor = 'white'; + let selectedViasCell = '------'; + let selectedViasCellColor = 'white'; + + const selectedApproach = targetPlan.approach; + const selectedApproachVia = targetPlan.approachVia; + + if (selectedApproach) { + selectedApproachCell = ApproachUtils.shortApproachName(selectedApproach); + selectedApproachCellColor = planColor; + + if (selectedApproachVia === null) { + selectedViasCell = 'NONE'; + selectedViasCellColor = planColor; + } else if (selectedApproachVia) { + selectedViasCell = selectedApproachVia.ident; + selectedViasCellColor = planColor; + } + } + + let selectedStarCell = '------'; + let selectedStarCellColor = 'white'; + + const selectedArrival = targetPlan.arrival; + const availableArrivals = targetPlan.availableArrivals; + + if (selectedArrival) { + selectedStarCell = selectedArrival.ident; + selectedStarCellColor = planColor; + } else if (selectedArrival === null || availableArrivals.length === 0) { + selectedStarCell = 'NONE'; + selectedStarCellColor = planColor; + } + + const rows = [[''], [''], [''], [''], [''], ['']]; + + for (let i = 0; i < ArrivalPagination.VIA_PAGE; i++) { + const index = i + pageCurrent * ArrivalPagination.VIA_PAGE; + const via = availableApproachVias[index]; + + if (selectedApproach && via) { + const isSelected = selectedApproachVia && via.databaseId === selectedApproachVia.databaseId; + const color = isSelected && !isTemporary ? 'green' : 'cyan'; + + rows[2 * i + 1][0] = `{${color}}${!isSelected ? '{' : '{sp}'}${via.ident}{end}`; + + mcdu.onLeftInput[i + 2] = async (_, scratchpadCallback) => { + // Clicking the already selected via is not allowed + if (!isSelected) { + try { + await mcdu.flightPlanService.setApproachVia(via.databaseId, forPlan, inAlternate); + + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); + }); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + + scratchpadCallback(); + } + }; + } + } + + let bottomLine = [' { + mcdu.eraseTemporaryFlightPlan(() => { + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }; + + mcdu.onRightInput[5] = async () => { + mcdu.insertTemporaryFlightPlan(() => { + mcdu.updateTowerHeadwind(); + mcdu.updateConstraints(); + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }; + } else { + mcdu.onLeftInput[5] = () => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); + }; + } + + const isNoViaSelected = selectedApproachVia === null; + const color = isNoViaSelected && !isTemporary ? 'green' : 'cyan'; + + mcdu.setTemplate([ + ['APPROACH VIAS'], + ['{sp}APPR', 'STAR{sp}', '{sp}VIA'], + [ + `{${selectedApproachCellColor}}${selectedApproachCell.padEnd(10)}{end}{${selectedViasCellColor}}${selectedViasCell}{end}`, + selectedStarCell + '[color]' + selectedStarCellColor, + ], + ['APPR VIAS'], + [`${!isNoViaSelected ? '{' : '{sp}'}${Labels.NO_VIA}[color]${color}`], + rows[0], + rows[1], + rows[2], + rows[3], + rows[4], + rows[5], + rows[6], + bottomLine, + ]); + mcdu.onLeftInput[1] = async () => { + try { + await mcdu.flightPlanService.setApproachVia(null, forPlan, inAlternate); + + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); + }); + } + }; + let up = false; + let down = false; + + if (pageCurrent < Math.ceil(selectedApproach.transitions.length / ArrivalPagination.VIA_PAGE) - 1) { + mcdu.onUp = () => { + pageCurrent++; + if (pageCurrent < 0) { + pageCurrent = 0; + } + CDUAvailableArrivalsPage.ShowViasPage(mcdu, airport, pageCurrent, forPlan, inAlternate); + }; + up = true; + } + if (pageCurrent > 0) { + mcdu.onDown = () => { + pageCurrent--; + if (pageCurrent < 0) { + pageCurrent = 0; + } + CDUAvailableArrivalsPage.ShowViasPage(mcdu, airport, pageCurrent, forPlan, inAlternate); + }; + down = true; + } + mcdu.setArrows(up, down, true, true); + mcdu.onPrevPage = () => { + CDUAvailableArrivalsPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); + }; + mcdu.onNextPage = mcdu.onPrevPage; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AvailableDeparturesPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AvailableDeparturesPage.ts new file mode 100644 index 00000000000..c1070f11144 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AvailableDeparturesPage.ts @@ -0,0 +1,355 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { Airport, Departure, NXUnits, ProcedureTransition, Runway, RunwayUtils } from '@flybywiresim/fbw-sdk'; +import { CDUFlightPlanPage } from './A320_Neo_CDU_FlightPlanPage'; +import { NXFictionalMessages, NXSystemMessages } from '../messages/NXSystemMessages'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; + +const DeparturePagination = Object.freeze({ + DEPT_PAGE: 4, +}); + +const Labels = Object.freeze({ + NO_SID: 'NO SID', + NO_TRANS: 'NO TRANS', +}); + +export class CDUAvailableDeparturesPage { + static ShowPage( + mcdu: LegacyFmsPageInterface, + airport: Airport, + pageCurrent = -1, + sidSelection = false, + forPlan = FlightPlanIndex.Active, + inAlternate = false, + ) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AvailableDeparturesPage; + + let selectedRunwayCell = '---'; + let selectedRunwayCellColor = 'white'; + let selectedSidCell = '------'; + let selectedSidCellColor = 'white'; + let selectedTransCell = '------'; + let selectedTransCellColor = 'white'; + + // --- figure out which data is available for the page --- + + const editingTmpy = forPlan === FlightPlanIndex.Active && mcdu.flightPlanService.hasTemporary; + + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + + const selectedRunway = targetPlan.originRunway; + const selectedSid = targetPlan.originDeparture; + const selectedTransition = targetPlan.departureEnrouteTransition; + + const showEosid = selectedRunway && sidSelection; + + const availableRunways = [...targetPlan.availableOriginRunways]; + let availableSids = [...targetPlan.availableDepartures].sort((a, b) => a.ident.localeCompare(b.ident)) as ( + | Departure + | (typeof Labels)['NO_SID'] + )[]; + let availableTransitions = []; + + if (selectedRunway) { + // filter out any SIDs not compatible with this runway + availableSids = (availableSids as Departure[]).filter( + (sid) => + sid.runwayTransitions.length === 0 || findRunwayTransitionIndex(selectedRunway, sid.runwayTransitions) !== -1, + ); + } + + // NO SID option is available at the end of the list when non-zero options + if (availableSids.length > 0) { + availableSids.push(Labels.NO_SID); + } + + let selectedSidPage = -1; + if (selectedSid) { + availableTransitions = [...selectedSid.enrouteTransitions]; + + selectedSidPage = + selectedSid === undefined + ? 0 + : Math.floor( + availableSids.findIndex((sid) => + sid === Labels.NO_SID ? selectedSid === null : sid.databaseId === selectedSid.databaseId, + ) / DeparturePagination.DEPT_PAGE, + ); + } + + const selectedRunwayPage = selectedRunway + ? Math.floor( + availableRunways.findIndex((runway) => runway.ident === selectedRunway.ident) / DeparturePagination.DEPT_PAGE, + ) + : -1; + + // NO TRANS option is available at the end of the list when non-zero options + if (availableTransitions.length > 0) { + availableTransitions.push(Labels.NO_TRANS); + } + + // --- render the top part of the page --- + + const selectedColour = editingTmpy ? 'yellow' : 'green'; + + if (selectedRunway) { + selectedRunwayCell = RunwayUtils.runwayString(selectedRunway.ident); + selectedRunwayCellColor = selectedColour; + + // TODO check type of ls... but awful from raw JS + if (selectedRunway.lsIdent) { + selectedRunwayCell = `${selectedRunwayCell.padEnd(3)}{small}-ILS{end}`; + } + } + + if (selectedSid) { + selectedSidCell = selectedSid.ident; + selectedSidCellColor = selectedColour; + } else if (availableSids.length === 0 || selectedSid === null) { + selectedSidCell = 'NONE'; + selectedSidCellColor = selectedColour; + } + + if (selectedTransition) { + selectedTransCell = selectedTransition.ident; + selectedTransCellColor = selectedColour; + } else if ((selectedSid !== undefined && availableTransitions.length === 0) || selectedTransition === null) { + selectedTransCell = 'NONE'; + selectedTransCellColor = selectedColour; + } + + // --- render the rows --- + + const rows = [[''], [''], [''], [''], [''], [''], [''], ['', '', '']]; + if (!sidSelection) { + // jump to selected runway page if entering page + if (pageCurrent < 0) { + pageCurrent = Math.max(0, selectedRunwayPage); + } + + for (let i = 0; i < DeparturePagination.DEPT_PAGE; i++) { + const index = i + pageCurrent * DeparturePagination.DEPT_PAGE; + const runway = availableRunways[index]; + if (runway) { + const selected = selectedRunway && selectedRunway.ident === runway.ident; + const color = selected && !editingTmpy ? 'green' : 'cyan'; + + const hasIls = runway.lsFrequencyChannel > 0; // TODO what if not ILS + rows[2 * i] = [ + `{${color}}${selected ? '{sp}' : '{'}${RunwayUtils.runwayString(runway.ident).padEnd(3)}${hasIls ? '{small}-ILS{end}' : '{sp}{sp}{sp}{sp}'}${NXUnits.mToUser(runway.length).toFixed(0).padStart(6, '\xa0')}{small}${NXUnits.userDistanceUnit().padEnd(2)}{end}{end}`, + ]; + const ilsText = hasIls ? `${runway.lsIdent.padStart(6)}/${runway.lsFrequencyChannel.toFixed(2)}` : ''; + rows[2 * i + 1] = [ + `{${color}}{sp}{sp}{sp}${Utils.leadingZeros(Math.round(runway.magneticBearing), 3)}${ilsText}{end}`, + ]; + mcdu.onLeftInput[i + 1] = async (_, scratchpadCallback) => { + // Clicking the already selected runway is not allowed + if (!selected) { + try { + await mcdu.flightPlanService.setOriginRunway(runway.ident, forPlan, inAlternate); + + CDUAvailableDeparturesPage.ShowPage(mcdu, airport, 0, true, forPlan, inAlternate); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, false, forPlan, inAlternate); + }); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + + scratchpadCallback(); + } + }; + } + } + } else { + // jump to selected SID page if entering page + if (pageCurrent < 0) { + pageCurrent = Math.max(0, selectedSidPage); + } + + // show the available SIDs down the left side + for (let i = 0; i < DeparturePagination.DEPT_PAGE; i++) { + const sid = availableSids[pageCurrent * DeparturePagination.DEPT_PAGE + i]; + if (sid) { + const selected = + sid !== undefined && + (sid === Labels.NO_SID ? selectedSid === null : selectedSid?.databaseId === sid.databaseId); + const color = selected && !editingTmpy ? 'green' : 'cyan'; + + rows[2 * i] = [`{${color}}${selected ? '{sp}' : '{'}${typeof sid === 'string' ? sid : sid.ident}{end}`]; + mcdu.onLeftInput[1 + i] = async (_, scratchpadCallback) => { + // Clicking the already selected SID is not allowed + if (!selected) { + try { + if (sid === Labels.NO_SID) { + await mcdu.flightPlanService.setDepartureProcedure(null, forPlan, inAlternate); + } else { + await mcdu.flightPlanService.setDepartureProcedure(sid.databaseId, forPlan, inAlternate); + } + + CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, true, forPlan, inAlternate); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, false, forPlan, inAlternate); + }); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + + scratchpadCallback(); + } + }; + } + } + + // show the enroute transitions for the selected SID down the right side + if (selectedSid) { + const transPage = selectedSidPage > pageCurrent ? 0 : pageCurrent - selectedSidPage; + + for (let i = 0; i < DeparturePagination.DEPT_PAGE; i++) { + const trans = availableTransitions[transPage * DeparturePagination.DEPT_PAGE + i]; + if (trans) { + const selected = + (trans === Labels.NO_TRANS && selectedTransition === null) || + (selectedTransition && selectedTransition.ident === trans.ident); + const color = selected && !editingTmpy ? 'green' : 'cyan'; + + rows[2 * i][1] = `{${color}}${typeof trans === 'string' ? trans : trans.ident}${selected ? ' ' : '}'}{end}`; + mcdu.onRightInput[i + 1] = async (_, scratchpadCallback) => { + // Clicking the already selected transition is not allowed + if (!selected) { + try { + if (trans === Labels.NO_TRANS) { + await mcdu.flightPlanService.setDepartureEnrouteTransition(null, forPlan, inAlternate); + } else { + await mcdu.flightPlanService.setDepartureEnrouteTransition(trans.databaseId, forPlan, inAlternate); + } + + CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, true, forPlan, inAlternate); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + + mcdu.eraseTemporaryFlightPlan(() => { + CDUAvailableDeparturesPage.ShowPage(mcdu, airport, 0, false, forPlan, inAlternate); + }); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + + scratchpadCallback(); + } + }; + } + } + } + } + + // --- render arrows etc. --- + + let up = false; + let down = false; + let numPages = 0; + if (sidSelection) { + const sidPages = Math.ceil(availableSids.length / DeparturePagination.DEPT_PAGE); + const transPages = Math.ceil(availableTransitions.length / DeparturePagination.DEPT_PAGE); + numPages = Math.max(sidPages, transPages, selectedSidPage + transPages); + } else { + numPages = Math.ceil(availableRunways.length / DeparturePagination.DEPT_PAGE); + } + if (pageCurrent < numPages - 1) { + mcdu.onUp = () => { + pageCurrent++; + if (pageCurrent < 0) { + pageCurrent = 0; + } + CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, sidSelection, forPlan, inAlternate); + }; + up = true; + } + if (pageCurrent > 0) { + mcdu.onDown = () => { + pageCurrent--; + if (pageCurrent < 0) { + pageCurrent = 0; + } + CDUAvailableDeparturesPage.ShowPage(mcdu, airport, pageCurrent, sidSelection, forPlan, inAlternate); + }; + down = true; + } + mcdu.setArrows(up, down, true, true); + + if (editingTmpy) { + mcdu.onLeftInput[5] = () => { + mcdu.eraseTemporaryFlightPlan(() => { + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }; + mcdu.onRightInput[5] = () => { + mcdu.insertTemporaryFlightPlan(() => { + mcdu.updateConstraints(); + mcdu.onToRwyChanged(); + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }; + } else { + mcdu.onLeftInput[5] = () => { + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }; + } + + if (showEosid) { + rows[7][2] = 'EOSID'; + } + + mcdu.setTemplate([ + ['{sp}DEPARTURES {small}FROM{end} {green}' + airport.ident + '{sp}{sp}{sp}'], + ['{sp}RWY', 'TRANS{sp}', 'SID'], + [ + selectedRunwayCell + '[color]' + selectedRunwayCellColor, + selectedTransCell + '[color]' + selectedTransCellColor, + selectedSidCell + '{sp}[color]' + selectedSidCellColor, + ], + sidSelection ? ['SIDS', 'TRANS', 'AVAILABLE'] : ['', '', 'AVAILABLE RUNWAYS\xa0'], + rows[0], + rows[1], + rows[2], + rows[3], + rows[4], + rows[5], + rows[6], + rows[7], + [ + editingTmpy ? '{ERASE[color]amber' : '{RETURN', + editingTmpy ? 'INSERT*[color]amber' : '', + showEosid ? `{${selectedColour}}{sp}NONE{end}` : '', + ], + ]); + mcdu.onPrevPage = () => { + CDUAvailableDeparturesPage.ShowPage(mcdu, airport, -1, !sidSelection, forPlan, inAlternate); + }; + mcdu.onNextPage = mcdu.onPrevPage; + } +} + +/** + * Check if a runway transition matches with a runway + * @returns -1 if not found, else index of the transition + */ +function findRunwayTransitionIndex(runway: Runway, transitions: ProcedureTransition[]): number { + return transitions.findIndex((trans) => trans.ident === runway.ident); +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AvailableFlightPlanPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AvailableFlightPlanPage.ts new file mode 100644 index 00000000000..93200189bc3 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_AvailableFlightPlanPage.ts @@ -0,0 +1,225 @@ +import { CDUInitPage } from './A320_Neo_CDU_InitPage'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { CoRouteUplinkAdapter } from '@fmgc/flightplanning/uplink/CoRouteUplinkAdapter'; + +export class CDUAvailableFlightPlanPage { + static ShowPage(mcdu: LegacyFmsPageInterface, offset = 0, currentRoute = 1) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AvailableFlightPlanPage; + let fromTo = 'NO ORIGIN/DEST'; + + const origin = mcdu.flightPlanService.active.originAirport; + const dest = mcdu.flightPlanService.active.destinationAirport; + + if (origin && dest) { + fromTo = `${origin.ident}/${dest.ident}`; + } + + const hasCoRoutes = mcdu.coRoute.routes.length > 0; + + if (hasCoRoutes) { + const coRoutesListSize = mcdu.coRoute.routes.length; + + // Page Management + if (currentRoute < 1) { + currentRoute = coRoutesListSize; + } + if (currentRoute > coRoutesListSize) { + currentRoute = 1; + } + + const { navlog, routeName } = mcdu.coRoute.routes[currentRoute - 1]; + + let scrollText = []; + const routeArray = []; + const scrollLimit = 9; + let columnPos = 0; + let rowPos = 0; + + // Scroll Text builder + for (let i = 0; i < navlog.length; i++) { + const fix = navlog[i]; + const nextFix = navlog[i + 1]; + + if (fix.is_sid_star === '1') { + continue; + } + + if (['TOP OF CLIMB', 'TOP OF DESCENT'].includes(fix.name)) { + continue; + } + + if (!nextFix) { + continue; + } + + if (fix.via_airway === 'DCT' && nextFix.via_airway === 'DCT') { + switch (columnPos) { + case 1: + routeArray[rowPos] = [ + '', + '', + `${routeArray[rowPos][2]}` + + ' ' + + `{green}{big}${fix.via_airway.concat('@'.repeat(5 - fix.via_airway.length))}{end}{end}` + + ' ' + + `{small}${fix.ident.concat('@'.repeat(5 - fix.ident.length))}{end}`, + ]; + columnPos = 2; + break; + case 2: + routeArray[rowPos] = [ + '', + '', + `${routeArray[rowPos][2]}` + + ' ' + + `{green}{big}${fix.via_airway.concat('@'.repeat(5 - fix.via_airway.length))}{end}`, + ]; + routeArray[rowPos + 1] = ['', '', `{small}${fix.ident.concat('@'.repeat(5 - fix.ident.length))}{end}`]; + columnPos = 1; + rowPos++; + break; + } + + continue; + } + + if (nextFix.via_airway !== fix.via_airway) { + switch (columnPos) { + case 0: + routeArray[rowPos] = ['', '', `{small}${fix.ident.concat('@'.repeat(5 - fix.ident.length))}{end}`]; + columnPos = 1; + break; + case 1: + routeArray[rowPos] = [ + '', + '', + `${routeArray[rowPos][2]}` + + ' ' + + `{green}{big}${fix.via_airway.concat('@'.repeat(5 - fix.via_airway.length))}{end}{end}` + + ' ' + + `{small}${fix.ident.concat('@'.repeat(5 - fix.ident.length))}{end}`, + ]; + columnPos = 2; + break; + case 2: + routeArray[rowPos] = [ + '', + '', + `${routeArray[rowPos][2]}` + + ' ' + + `{green}{big}${fix.via_airway.concat('@'.repeat(5 - fix.via_airway.length))}{end}{end}`, + ]; + routeArray[rowPos + 1] = ['', '', `{small}${fix.ident.concat('@'.repeat(5 - fix.ident.length))}{end}`]; + columnPos = 1; + rowPos++; + break; + } + continue; + } + } + + /* row character width management, + uses @ as a delim for adding spaces in short airways/waypoints */ + routeArray.forEach((line, index) => { + const excludedLength = line[2].replace(/{small}|{green}|{end}|{big}|/g, '').length; + if (excludedLength < 23) { + // Add spaces to make up lack of row width + const adjustedLine = line[2] + '{sp}'.repeat(23 - excludedLength); + routeArray[index] = ['', '', adjustedLine]; + } + // Add spaces for short airways/waypoints smaller than 5 characters + routeArray[index] = ['', '', routeArray[index][2].replace(/@/g, '{sp}')]; + }); + + // Offset Management + const routeArrayLength = routeArray.length; + if (offset < 0) { + offset = 0; + } + if (offset > routeArrayLength - 9) { + offset = routeArrayLength - 9; + } + scrollText = + routeArrayLength > 9 + ? [...routeArray.slice(0 + offset, 9 + offset)] + : [...routeArray, ...CDUAvailableFlightPlanPage.insertEmptyRows(scrollLimit - routeArray.length)]; + + mcdu.setTemplate([ + [`{sp}{sp}{sp}{sp}{sp}ROUTE{sp}{sp}{small}${currentRoute}/${coRoutesListSize}{end}`], + ['{sp}CO RTE', 'FROM/TO{sp}{sp}'], + [`${routeName}[color]cyan`, `${fromTo}[color]cyan`], + ...scrollText, + [' { + CDUAvailableFlightPlanPage.ShowPage(mcdu, 0, currentRoute - 1); + }; + mcdu.onNextPage = () => { + CDUAvailableFlightPlanPage.ShowPage(mcdu, 0, currentRoute + 1); + }; + mcdu.onDown = () => { + //on page down decrement the page offset. + CDUAvailableFlightPlanPage.ShowPage(mcdu, offset - 1, currentRoute); + }; + mcdu.onUp = () => { + CDUAvailableFlightPlanPage.ShowPage(mcdu, offset + 1, currentRoute); + }; + + mcdu.onLeftInput[5] = () => { + mcdu.coRoute.routes = []; + CDUInitPage.ShowPage1(mcdu); + }; + + mcdu.onRightInput[5] = () => { + const selectedRoute = mcdu.coRoute.routes[currentRoute - 1]; + // FIXME This whole thing is very shady. Why is this one object doing a bunch of different things? + mcdu.coRoute.routeNumber = routeName; + mcdu.coRoute['originIcao'] = selectedRoute.originIcao; + mcdu.coRoute['destinationIcao'] = selectedRoute.destinationIcao; + mcdu.coRoute['route'] = selectedRoute.route; + if (selectedRoute.alternateIcao) { + mcdu.coRoute['alternateIcao'] = selectedRoute.alternateIcao; + } + mcdu.coRoute['navlog'] = selectedRoute.navlog; + setTimeout(async () => { + // FIXME This should not use the uplink functions, as it is not an uplink. + // Doing so causes an erroneous uplink related scratchpad message. + await CoRouteUplinkAdapter.uplinkFlightPlanFromCoRoute(mcdu, mcdu.flightPlanService, selectedRoute); + await mcdu.flightPlanService.uplinkInsert(); + mcdu.setGroundTempFromOrigin(); + + CDUInitPage.ShowPage1(mcdu); + }, 0 /* No delay because it takes long enough without artificial delay */); + }; + } else { + mcdu.setTemplate([ + [fromTo], + [''], + ['NONE[color]green'], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + [' { + CDUInitPage.ShowPage1(mcdu); + }; + } + + static insertEmptyRows(rowsToInsert: number) { + const array = []; + for (let i = 0; i < rowsToInsert; i++) { + array.push(['']); + } + return array; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_DataIndexPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_DataIndexPage.ts new file mode 100644 index 00000000000..389a9160e4a --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_DataIndexPage.ts @@ -0,0 +1,129 @@ +import { CDUAirportsMonitor } from './A320_Neo_CDU_AirportsMonitor'; +import { CDUGPSMonitor } from './A320_Neo_CDU_GPSMonitor'; +import { CDUIdentPage } from './A320_Neo_CDU_IdentPage'; +import { CDUIRSMonitor } from './A320_Neo_CDU_IRSMonitor'; +import { CDUNavaidPage } from './A320_Neo_CDU_NavaidPage'; +import { CDUPilotsWaypoint } from './A320_Neo_CDU_PilotsWaypoint'; +import { CDUPositionMonitorPage } from './A320_Neo_CDU_PositionMonitorPage'; +import { CDUWaypointPage } from './A320_Neo_CDU_WaypointPage'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class CDUDataIndexPage { + static ShowPage1(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.DataIndexPage1; + mcdu.activeSystem = 'FMGC'; + mcdu.setTemplate([ + ['DATA INDEX', '1', '2'], + ['\xa0POSITION'], + [''], + ]); + + mcdu.leftInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[0] = () => { + CDUPositionMonitorPage.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[1] = () => { + CDUIRSMonitor.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[2] = () => { + CDUGPSMonitor.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[3] = () => { + CDUIdentPage.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[4] = () => { + CDUAirportsMonitor.ShowPage(mcdu); + }; + + mcdu.onNextPage = () => { + this.ShowPage2(mcdu); + }; + mcdu.onPrevPage = () => { + this.ShowPage2(mcdu); + }; + } + static ShowPage2(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.DataIndexPage2; + mcdu.setTemplate([ + ['DATA INDEX', '2', '2'], + ['', 'STORED\xa0'], + [''], + ['', 'STORED\xa0[color]inop'], + ['[color]inop'], + ['', 'STORED\xa0[color]inop'], + ['[color]inop'], + ['', 'STORED\xa0[color]inop'], + ['[color]inop'], + ['\xa0ACTIVE F-PLAN[color]inop', ''], + [' { + CDUWaypointPage.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onRightInput[0] = () => { + CDUPilotsWaypoint.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[1] = () => { + CDUNavaidPage.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onNextPage = () => { + this.ShowPage1(mcdu); + }; + mcdu.onPrevPage = () => { + this.ShowPage1(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_DirectToPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_DirectToPage.ts new file mode 100644 index 00000000000..aa31a266648 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_DirectToPage.ts @@ -0,0 +1,187 @@ +// Copyright (c) 2020, 2022 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { CDUFlightPlanPage } from './A320_Neo_CDU_FlightPlanPage'; +import { NXFictionalMessages, NXSystemMessages } from '../messages/NXSystemMessages'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { Fix } from '@flybywiresim/fbw-sdk'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +// TODO this whole thing is thales layout... + +export class CDUDirectToPage { + static ShowPage(mcdu: LegacyFmsPageInterface, directWaypoint?: Fix, wptsListIndex = 0) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.DirectToPage; + mcdu.returnPageCallback = () => { + CDUDirectToPage.ShowPage(mcdu, directWaypoint, wptsListIndex); + }; + + mcdu.activeSystem = 'FMGC'; + + let directWaypointCell = ''; + if (directWaypoint) { + directWaypointCell = directWaypoint.ident; + } else if (mcdu.flightPlanService.hasTemporary) { + mcdu.eraseTemporaryFlightPlan(() => { + CDUDirectToPage.ShowPage(mcdu); + }); + return; + } + + const waypointsCell = ['', '', '', '', '']; + let iMax = 5; + let eraseLabel = ''; + let eraseLine = ''; + let insertLabel = ''; + let insertLine = ''; + if (mcdu.flightPlanService.hasTemporary) { + iMax--; + eraseLabel = '\xa0DIR TO[color]amber'; + eraseLine = '{ERASE[color]amber'; + insertLabel = 'TMPY\xa0[color]amber'; + insertLine = 'DIRECT*[color]amber'; + mcdu.onLeftInput[5] = () => { + mcdu.eraseTemporaryFlightPlan(() => { + CDUDirectToPage.ShowPage(mcdu); + }); + }; + mcdu.onRightInput[5] = () => { + mcdu.insertTemporaryFlightPlan(() => { + SimVar.SetSimVarValue('K:A32NX.FMGC_DIR_TO_TRIGGER', 'number', 0); + CDUFlightPlanPage.ShowPage(mcdu); + }); + }; + } + + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + mcdu.eraseTemporaryFlightPlan(() => { + CDUDirectToPage.ShowPage(mcdu, undefined, wptsListIndex); + }); + return; + } + + WaypointEntryUtils.getOrCreateWaypoint(mcdu, value, false) + .then((w) => { + if (w) { + mcdu.eraseTemporaryFlightPlan(() => { + mcdu + .directToWaypoint(w) + .then(() => { + CDUDirectToPage.ShowPage(mcdu, w, wptsListIndex); + }) + .catch((err) => { + mcdu.logTroubleshootingError(err); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + console.error(err); + }); + }); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); + } + }) + .catch((err) => { + // Rethrow if error is not an FMS message to display + if (err.type === undefined) { + throw err; + } + + mcdu.showFmsErrorMessage(err.type); + }); + }; + + mcdu.onRightInput[2] = () => { + mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + }; + mcdu.onRightInput[3] = () => { + mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + }; + mcdu.onRightInput[4] = () => { + mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + }; + + const plan = mcdu.flightPlanService.active; + + let i = 0; + let cellIter = 0; + wptsListIndex = Math.max(wptsListIndex, mcdu.flightPlanService.active.activeLegIndex); + + const totalWaypointsCount = plan.firstMissedApproachLegIndex; + + while (i < totalWaypointsCount && i + wptsListIndex < totalWaypointsCount && cellIter < iMax) { + const legIndex = i + wptsListIndex; + if (plan.elementAt(legIndex).isDiscontinuity) { + i++; + continue; + } + + const leg = plan.legElementAt(legIndex); + + if (leg) { + if (!leg.isXF()) { + i++; + continue; + } + + waypointsCell[cellIter] = '{' + leg.ident + '[color]cyan'; + if (waypointsCell[cellIter]) { + mcdu.onLeftInput[cellIter + 1] = () => { + mcdu.eraseTemporaryFlightPlan(() => { + mcdu + .directToLeg(legIndex) + .then(() => { + CDUDirectToPage.ShowPage(mcdu, leg.terminationWaypoint(), wptsListIndex); + }) + .catch((err) => { + mcdu.logTroubleshootingError(err); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + console.error(err); + }); + }); + }; + } + } else { + waypointsCell[cellIter] = '----'; + } + i++; + cellIter++; + } + if (cellIter < iMax) { + waypointsCell[cellIter] = '--END--'; + } + let up = false; + let down = false; + if (wptsListIndex < totalWaypointsCount - 5) { + mcdu.onUp = () => { + wptsListIndex++; + CDUDirectToPage.ShowPage(mcdu, directWaypoint, wptsListIndex); + }; + up = true; + } + if (wptsListIndex > 0) { + mcdu.onDown = () => { + wptsListIndex--; + CDUDirectToPage.ShowPage(mcdu, directWaypoint, wptsListIndex); + }; + down = true; + } + mcdu.setArrows(up, down, false, false); + mcdu.setTemplate([ + ['DIR TO'], + ['\xa0WAYPOINT', 'DIST\xa0', 'UTC'], + ['*[' + (directWaypointCell ? directWaypointCell : '\xa0\xa0\xa0\xa0\xa0') + '][color]cyan', '---', '----'], + ['\xa0F-PLN WPTS'], + [waypointsCell[0], 'DIRECT TO[color]cyan'], + ['', 'WITH\xa0'], + [waypointsCell[1], 'ABEAM PTS[color]cyan'], + ['', 'RADIAL IN\xa0'], + [waypointsCell[2], '[ ]°[color]cyan'], + ['', 'RADIAL OUT\xa0'], + [waypointsCell[3], '[ ]°[color]cyan'], + [eraseLabel, insertLabel], + [eraseLine ? eraseLine : waypointsCell[4], insertLine], + ]); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_FixInfoPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_FixInfoPage.ts new file mode 100644 index 00000000000..14efe07f636 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_FixInfoPage.ts @@ -0,0 +1,211 @@ +// Copyright (c) 2021 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { A32NX_Util } from '../../../../shared/src/A32NX_Util'; +import { McduMessage, NXFictionalMessages, NXSystemMessages } from '../messages/NXSystemMessages'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { FixInfoEntry } from '@fmgc/flightplanning/plans/FixInfo'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +export class CDUFixInfoPage { + static ShowPage(mcdu: LegacyFmsPageInterface, page: 1 | 2 | 3 | 4 = 1) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.FixInfoPage; + mcdu.returnPageCallback = () => { + CDUFixInfoPage.ShowPage(mcdu, page); + }; + mcdu.activeSystem = 'FMGC'; + + const fixInfo = mcdu.flightPlanService.active.fixInfos[page]; + + mcdu.onLeftInput[0] = (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + if (fixInfo.fix) { + mcdu.flightPlanService.setFixInfoEntry(page, null); + + return CDUFixInfoPage.ShowPage(mcdu, page); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + + return scratchpadCallback(); + } + } + if (WaypointEntryUtils.isPlaceFormat(value)) { + WaypointEntryUtils.parsePlace(mcdu, value) + .then((runway) => { + mcdu.flightPlanService.setFixInfoEntry(page, new FixInfoEntry(runway)); + + CDUFixInfoPage.ShowPage(mcdu, page); + }) + .catch((message) => { + if (message.type !== undefined) { + mcdu.showFmsErrorMessage(message.type); + } else if (message instanceof McduMessage) { + mcdu.setScratchpadMessage(message); + + scratchpadCallback(); + } else { + console.error(message); + } + }); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + + scratchpadCallback(); + } + }; + + const template = [ + [`\xa0\xa0\xa0\xa0\xa0FIX INFO\xa0\xa0{small}${page}/4{end}`], + [`REF FIX ${page}`], + ['{amber}_______{end}'], + [], + [], + [], + [], + [], + [], + [], + [], + [], + [], + ]; + + if (fixInfo && fixInfo.fix) { + template[2] = [`{cyan}${fixInfo.fix.ident}{end}`]; + template[3] = ['\xa0RADIAL\xa0\xa0TIME\xa0\xa0DTG\xa0\xa0ALT']; + + for (let i = 0; i <= 1; i++) { + const radial = fixInfo.radials?.[i]; + + if (radial !== undefined) { + template[4 + i * 2] = [ + `\xa0{cyan}${('' + radial.magneticBearing).padStart(3, '0')}°{end}\xa0\xa0\xa0\xa0----\xa0----\xa0----`, + ]; + } else if (i === 0 || fixInfo.radials?.[0] !== undefined) { + template[4 + i * 2] = [`\xa0{cyan}[\xa0]°{end}\xa0\xa0\xa0\xa0----\xa0----\xa0----`]; + } + + mcdu.onLeftInput[1 + i] = (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + if (radial !== undefined) { + mcdu.flightPlanService.editFixInfoEntry(page, (fixInfo) => { + fixInfo.radials.splice(i); + return fixInfo; + }); + + CDUFixInfoPage.ShowPage(mcdu, page); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + + scratchpadCallback(); + } + } else if (value.match(/^[0-9]{1,3}$/)) { + const degrees = parseInt(value); + + if (degrees <= 360) { + mcdu.flightPlanService.editFixInfoEntry(page, (fixInfo) => { + if (!fixInfo.radials) { + fixInfo.radials = []; + } + fixInfo.radials[i] = { + magneticBearing: degrees, + trueBearing: A32NX_Util.magneticToTrue(degrees, A32NX_Util.getRadialMagVar(fixInfo.fix)), + }; + return fixInfo; + }); + + CDUFixInfoPage.ShowPage(mcdu, page); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + + scratchpadCallback(); + } + } else if (value === '' && radial !== undefined) { + mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + + scratchpadCallback(); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + + scratchpadCallback(); + } + }; + } + + template[7] = ['\xa0RADIUS']; + if (fixInfo.radii?.[0] !== undefined) { + template[8] = [ + `\xa0{cyan}${('' + fixInfo.radii[0].radius).padStart(3, '\xa0')}{small}NM{end}{end}\xa0\xa0\xa0----\xa0----\xa0----`, + ]; + } else { + template[8] = [`\xa0{cyan}[\xa0]{small}NM{end}{end}\xa0\xa0\xa0----\xa0----\xa0----`]; + } + + mcdu.onLeftInput[3] = (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + if (fixInfo.radii?.[0] !== undefined) { + mcdu.flightPlanService.editFixInfoEntry(page, (fixInfo) => { + fixInfo.radii.length = 0; + return fixInfo; + }); + + CDUFixInfoPage.ShowPage(mcdu, page); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + } + } else if (value.match(/^[0-9]{1,3}$/)) { + const radius = parseInt(value); + if (radius >= 1 && radius <= 256) { + mcdu.flightPlanService.editFixInfoEntry(page, (fixInfo) => { + if (!fixInfo.radii) { + fixInfo.radii = []; + } + if (fixInfo.radii?.[0]) { + fixInfo.radii[0].radius = radius; + } else { + fixInfo.radii[0] = { radius }; + } + return fixInfo; + }); + + CDUFixInfoPage.ShowPage(mcdu, page); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + } + } else if (value === '' && fixInfo.radii?.[0] !== undefined) { + mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + + scratchpadCallback(); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + + scratchpadCallback(); + } + }; + + template[10] = ['{inop} mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + } + + mcdu.setArrows(false, false, true, true); + mcdu.setTemplate(template); + mcdu.onPrevPage = () => { + if (page >= 2) { + CDUFixInfoPage.ShowPage(mcdu, (page - 1) as 1 | 2 | 3 | 4); + } else { + CDUFixInfoPage.ShowPage(mcdu, 4); + } + }; + mcdu.onNextPage = () => { + if (page <= 3) { + CDUFixInfoPage.ShowPage(mcdu, (page + 1) as 1 | 2 | 3 | 4); + } else { + CDUFixInfoPage.ShowPage(mcdu, 1); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_FlightPlanPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_FlightPlanPage.ts new file mode 100644 index 00000000000..0d49deab213 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_FlightPlanPage.ts @@ -0,0 +1,1405 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { A32NX_Util } from '../../../../shared/src/A32NX_Util'; +import { FmgcFlightPhase } from '@shared/flightphase'; +import { CDULateralRevisionPage } from './A320_Neo_CDU_LateralRevisionPage'; +import { CDUVerticalRevisionPage } from './A320_Neo_CDU_VerticalRevisionPage'; +import { WaypointConstraintType } from '@fmgc/flightplanning/data/constraint'; +import { NXFictionalMessages, NXSystemMessages } from '../messages/NXSystemMessages'; +import { CDUHoldAtPage } from './A320_Neo_CDU_HoldAtPage'; +import { CDUInitPage } from './A320_Neo_CDU_InitPage'; +import { AltitudeDescriptor, NXUnits } from '@flybywiresim/fbw-sdk'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FlightPlanLeg, isDiscontinuity } from '@fmgc/flightplanning/legs/FlightPlanLeg'; +import { FmsFormatters } from '../legacy/FmsFormatters'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; + +const Markers = { + FPLN_DISCONTINUITY: ['---F-PLN DISCONTINUITY--'], + END_OF_FPLN: ['------END OF F-PLN------'], + NO_ALTN_FPLN: ['-----NO ALTN F-PLN------'], + END_OF_ALTN_FPLN: ['---END OF ALTN F-PLN----'], + TOO_STEEP_PATH: ['-----TOO STEEP PATH-----'], +}; + +const Altitude = Object.freeze({ + Empty: '\xa0\xa0\xa0\xa0\xa0', + NoPrediction: '-----', +}); +const Speed = Object.freeze({ + Empty: '\xa0\xa0\xa0', + NoPrediction: '---', +}); +const Time = Object.freeze({ + Empty: '\xa0\xa0\xa0\xa0', + NoPrediction: '----', +}); + +export class CDUFlightPlanPage { + static ShowPage(mcdu: LegacyFmsPageInterface, offset = 0, forPlan = 0) { + // INIT + function addLskAt(index, delay, callback) { + mcdu.leftInputDelay[index] = typeof delay === 'function' ? delay : () => delay; + mcdu.onLeftInput[index] = callback; + } + + function addRskAt(index, delay, callback) { + mcdu.rightInputDelay[index] = typeof delay === 'function' ? delay : () => delay; + mcdu.onRightInput[index] = callback; + } + + //mcdu.flightPlanManager.updateWaypointDistances(false /* approach */); + //mcdu.flightPlanManager.updateWaypointDistances(true /* approach */); + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.FlightPlanPage; + mcdu.returnPageCallback = () => { + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + }; + mcdu.activeSystem = 'FMGC'; + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.FlightPlanPage) { + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + } + }, mcdu.PageTimeout.Medium); + + const flightPhase = mcdu.flightPhaseManager.phase; + const isFlying = flightPhase >= FmgcFlightPhase.Takeoff && flightPhase != FmgcFlightPhase.Done; + + let showFrom = false; + + const forActiveOrTemporary = forPlan === 0; + const targetPlan = forActiveOrTemporary + ? mcdu.flightPlanService.activeOrTemporary + : mcdu.flightPlanService.secondary(1); + const planAccentColor = forActiveOrTemporary ? (mcdu.flightPlanService.hasTemporary ? 'yellow' : 'green') : 'white'; + + let headerText; + if (forActiveOrTemporary) { + if (mcdu.flightPlanService.hasTemporary) { + headerText = `{yellow}{sp}TMPY{end}`; + } else { + headerText = `{sp}`; + } + } else { + headerText = `{sp}{sp}{sp}{sp}{sp}{sp}{sp}{sp}{sp}{sp}{sp}SEC`; + } + + let flightNumberText = ''; + if (forActiveOrTemporary) { + flightNumberText = mcdu.flightNumber ?? ''; + } + + const waypointsAndMarkers = []; + const first = Math.max(0, targetPlan.fromLegIndex); + let destinationAirportOffset = 0; + let alternateAirportOffset = 0; + + // VNAV + const fmsGeometryProfile = mcdu.guidanceController.vnavDriver.mcduProfile; + const fmsPseudoWaypoints = mcdu.guidanceController.currentPseudoWaypoints; + + /** @type {Map} */ + let vnavPredictionsMapByWaypoint = null; + if (fmsGeometryProfile && fmsGeometryProfile.isReadyToDisplay) { + vnavPredictionsMapByWaypoint = fmsGeometryProfile.waypointPredictions; + } + + let cumulativeDistance = 0; + // Primary F-PLAN + + // In this loop, we insert pseudowaypoints between regular waypoints and compute the distances between the previous and next (pseudo-)waypoint. + for (let i = first; i < targetPlan.legCount; i++) { + const inMissedApproach = i >= targetPlan.firstMissedApproachLegIndex; + const isActiveLeg = i === targetPlan.activeLegIndex && forActiveOrTemporary; + const isBeforeActiveLeg = i < targetPlan.activeLegIndex && forActiveOrTemporary; + + const wp = targetPlan.allLegs[i]; + + if (wp.isDiscontinuity) { + waypointsAndMarkers.push({ + marker: Markers.FPLN_DISCONTINUITY, + fpIndex: i, + inAlternate: false, + inMissedApproach, + }); + continue; + } + + const pseudoWaypointsOnLeg = fmsPseudoWaypoints.filter((it) => it.displayedOnMcdu && it.alongLegIndex === i); + pseudoWaypointsOnLeg.sort((a, b) => a.distanceFromStart - b.distanceFromStart); + + for (let j = 0; j < pseudoWaypointsOnLeg.length; j++) { + const pwp = pseudoWaypointsOnLeg[j]; + const distanceFromLastLine = pwp.distanceFromStart - cumulativeDistance; + cumulativeDistance = pwp.distanceFromStart; + + // No PWP on FROM leg + if (!isBeforeActiveLeg) { + waypointsAndMarkers.push({ + pwp, + fpIndex: i, + inMissedApproach, + distanceFromLastLine, + isActive: isActiveLeg && j === 0, + }); + } + } + + if (i >= targetPlan.activeLegIndex && wp.isDiscontinuity === false && wp.definition.type === 'HM') { + waypointsAndMarkers.push({ holdResumeExit: wp, fpIndex: i, inMissedApproach, isActive: isActiveLeg }); + } + + const distanceFromLastLine = + wp.isDiscontinuity === false && wp.calculated + ? wp.calculated.cumulativeDistanceWithTransitions - cumulativeDistance + : 0; + cumulativeDistance = + wp.isDiscontinuity === false && wp.calculated + ? wp.calculated.cumulativeDistanceWithTransitions + : cumulativeDistance; + + waypointsAndMarkers.push({ + wp, + fpIndex: i, + inAlternate: false, + inMissedApproach, + distanceFromLastLine, + isActive: isActiveLeg && pseudoWaypointsOnLeg.length === 0, + }); + + if (i === targetPlan.destinationLegIndex) { + destinationAirportOffset = Math.max(waypointsAndMarkers.length - 4, 0); + } + + if (i === targetPlan.lastIndex) { + waypointsAndMarkers.push({ + marker: Markers.END_OF_FPLN, + fpIndex: i + 1, + inAlternate: false, + inMissedApproach: false, + }); + } + } + + // Primary ALTN F-PLAN + if (targetPlan.alternateDestinationAirport) { + for (let i = 0; i < targetPlan.alternateFlightPlan.legCount; i++) { + const inMissedApproach = i >= targetPlan.alternateFlightPlan.firstMissedApproachLegIndex; + + const wp = targetPlan.alternateFlightPlan.allLegs[i]; + + if (wp.isDiscontinuity) { + waypointsAndMarkers.push({ marker: Markers.FPLN_DISCONTINUITY, fpIndex: i, inAlternate: true }); + continue; + } + + if ( + i >= targetPlan.alternateFlightPlan.activeLegIndex && + wp.isDiscontinuity === false && + wp.definition.type === 'HM' + ) { + waypointsAndMarkers.push({ holdResumeExit: wp, fpIndex: i, inAlternate: true }); + } + + const distanceFromLastLine = + wp.isDiscontinuity === false && wp.calculated + ? wp.calculated.cumulativeDistanceWithTransitions - cumulativeDistance + : 0; + cumulativeDistance = + wp.isDiscontinuity === false && wp.calculated + ? wp.calculated.cumulativeDistanceWithTransitions + : cumulativeDistance; + + waypointsAndMarkers.push({ wp, fpIndex: i, inAlternate: true, inMissedApproach, distanceFromLastLine }); + + if (i === targetPlan.alternateFlightPlan.destinationLegIndex) { + alternateAirportOffset = Math.max(waypointsAndMarkers.length - 4, 0); + } + + if (i === targetPlan.alternateFlightPlan.lastIndex) { + waypointsAndMarkers.push({ + marker: Markers.END_OF_ALTN_FPLN, + fpIndex: i + 1, + inAlternate: true, + inMissedApproach: false, + }); + } + } + } else if (targetPlan.legCount > 0) { + waypointsAndMarkers.push({ marker: Markers.NO_ALTN_FPLN, fpIndex: targetPlan.legCount + 1, inAlternate: true }); + } + + const tocIndex = waypointsAndMarkers.findIndex(({ pwp }) => pwp && pwp.ident === '(T/C)'); + + // Render F-PLAN Display + + // fprow: 1 | 2 | 3 4 | 5 6 | 7 8 | 9 10 | 11 12 | + // display: SPD/ALT| R0 | R1 | R2 | R3 | R4 | DEST | SCRATCHPAD + // functions: | F[0] | F[1] | F[2] | F[3] | F[4] | F[5] | + // | FROM | TO | + let rowsCount = 5; + + if (waypointsAndMarkers.length === 0) { + rowsCount = 0; + mcdu.setTemplate([ + [`{left}{small}{sp}FROM{end}${headerText}{end}{right}{small}${flightNumberText}{sp}{sp}{sp}{end}{end}`], + ...emptyFplnPage(forPlan), + ]); + mcdu.onLeftInput[0] = () => CDULateralRevisionPage.ShowPage(mcdu, undefined, undefined, forPlan); + return; + } else if (waypointsAndMarkers.length >= 5) { + rowsCount = 5; + } else { + rowsCount = waypointsAndMarkers.length; + } + + let useTransitionAltitude = false; + + // Only examine first 5 (or less) waypoints/markers + const scrollWindow = []; + for (let rowI = 0, winI = offset; rowI < rowsCount; rowI++, winI++) { + winI = winI % waypointsAndMarkers.length; + + const { + /** @type {import('fbw-a32nx/src/systems/fmgc/src/flightplanning/legs/FlightPlanLeg').FlightPlanElement} */ + wp, + pwp, + marker, + /** @type {import('fbw-a32nx/src/systems/fmgc/src/flightplanning/legs/FlightPlanLeg').FlightPlanElement} */ + holdResumeExit, + fpIndex, + inAlternate, + inMissedApproach, + distanceFromLastLine, + isActive, + } = waypointsAndMarkers[winI]; + + const legAccentColor = inAlternate || inMissedApproach ? 'cyan' : planAccentColor; + + const wpPrev = inAlternate + ? targetPlan.alternateFlightPlan.maybeElementAt(fpIndex - 1) + : targetPlan.maybeElementAt(fpIndex - 1); + const wpNext = inAlternate + ? targetPlan.alternateFlightPlan.maybeElementAt(fpIndex - 1) + : targetPlan.maybeElementAt(fpIndex + 1); + const wpActive = inAlternate || fpIndex >= targetPlan.activeLegIndex; + + // Bearing/Track + let bearingTrack = ''; + const maybeBearingTrackTo = pwp ? targetPlan.maybeElementAt(fpIndex) : wp; + const bearingTrackTo = maybeBearingTrackTo ? maybeBearingTrackTo : wpNext; + switch (rowI) { + case 1: { + const trueBearing = SimVar.GetSimVarValue('L:A32NX_EFIS_L_TO_WPT_BEARING', 'Degrees'); + if (isActive && trueBearing !== null && trueBearing >= 0) { + bearingTrack = `BRG${trueBearing.toFixed(0).padStart(3, '0')}\u00b0`; + } + break; + } + case 2: + bearingTrack = isDiscontinuity(wpPrev) ? '' : formatTrack(wpPrev, bearingTrackTo); + break; + } + + const constraintType = wp + ? CDUVerticalRevisionPage.constraintType(mcdu, fpIndex, targetPlan.index, inAlternate) + : WaypointConstraintType.Unknown; + if (constraintType === WaypointConstraintType.CLB) { + useTransitionAltitude = true; + } else if (constraintType === WaypointConstraintType.DES) { + useTransitionAltitude = false; + } else if (tocIndex >= 0) { + // FIXME Guess because VNAV doesn't tell us whether altitudes are climb or not \o/ + useTransitionAltitude = winI <= tocIndex; + } // else we stick with the last time we were sure... + + if (wp && wp.isDiscontinuity === false) { + // Waypoint + if (offset === 0) { + showFrom = true; + } + + let ident = wp.ident; + let isOverfly = wp.definition.overfly; + const isFromLeg = !inAlternate && fpIndex === targetPlan.fromLegIndex; + + let verticalWaypoint = null; + // TODO: Alternate predictions + if (!inAlternate && vnavPredictionsMapByWaypoint) { + verticalWaypoint = vnavPredictionsMapByWaypoint.get(fpIndex); + } + + // Color + let color; + if (isActive) { + color = 'white'; + } else { + const inMissedApproach = + targetPlan.index === FlightPlanIndex.Active && fpIndex >= targetPlan.firstMissedApproachLegIndex; + + if (inMissedApproach || inAlternate) { + color = 'cyan'; + } else { + color = planAccentColor; + } + } + + // Time + let timeCell: string = Time.NoPrediction; + let timeColor = 'white'; + if (verticalWaypoint && isFinite(verticalWaypoint.secondsFromPresent)) { + const utcTime = SimVar.GetGlobalVarValue('ZULU TIME', 'seconds'); + + timeCell = `${isFromLeg ? '{big}' : '{small}'}${ + isFlying + ? FmsFormatters.secondsToUTC(utcTime + verticalWaypoint.secondsFromPresent) + : FmsFormatters.secondsTohhmm(verticalWaypoint.secondsFromPresent) + }{end}`; + + timeColor = color; + } else if (!inAlternate && fpIndex === targetPlan.originLegIndex) { + timeCell = '{big}0000{end}'; + timeColor = color; + } + + // Fix Header + const fixAnnotation = wp.annotation; + + // Distance + let distance = ''; + // Active waypoint is live distance, others are distances in the flight plan + if (isActive) { + if (Number.isFinite(mcdu.guidanceController.activeLegAlongTrackCompletePathDtg)) { + distance = Math.round( + Math.max(0, Math.min(9999, mcdu.guidanceController.activeLegAlongTrackCompletePathDtg)), + ).toFixed(0); + } + } else { + distance = Math.round(Math.max(0, Math.min(9999, distanceFromLastLine))).toFixed(0); + } + + let fpa = ''; + if (wp.definition.verticalAngle !== undefined) { + fpa = (Math.round(wp.definition.verticalAngle * 10) / 10).toFixed(1); + } + + let altColor = 'white'; + let spdColor = 'white'; + + // Should show empty speed prediction for waypoint after hold + let speedConstraint: string = wp.type === 'HM' ? Speed.Empty : Speed.NoPrediction; + let speedPrefix = ''; + + if (targetPlan.index !== FlightPlanIndex.Temporary && wp.type !== 'HM') { + if (!inAlternate && fpIndex === targetPlan.originLegIndex) { + speedConstraint = Number.isFinite(mcdu.v1Speed) + ? `{big}${Math.round(mcdu.v1Speed)}{end}` + : Speed.NoPrediction; + spdColor = Number.isFinite(mcdu.v1Speed) ? color : 'white'; + } else if (isFromLeg) { + speedConstraint = Speed.Empty; + } else if (verticalWaypoint && verticalWaypoint.speed) { + speedConstraint = `{small}${verticalWaypoint.speed < 1 ? formatMachNumber(verticalWaypoint.speed) : Math.round(verticalWaypoint.speed)}{end}`; + + if (verticalWaypoint.speedConstraint) { + speedPrefix = `${verticalWaypoint.isSpeedConstraintMet ? '{magenta}' : '{amber}'}*{end}`; + } + spdColor = color; + } else if (wp.hasPilotEnteredSpeedConstraint()) { + speedConstraint = Math.round(wp.pilotEnteredSpeedConstraint.speed).toString(); + spdColor = 'magenta'; + } else if (wp.hasDatabaseSpeedConstraint()) { + speedConstraint = `{small}${Math.round(wp.definition.speed)}{end}`; + spdColor = 'magenta'; + } + } + + speedConstraint = speedPrefix + speedConstraint; + + // Altitude + const hasAltConstraint = legHasAltConstraint(wp); + let altitudeConstraint: string = Altitude.NoPrediction; + let altSize = 'big'; + if (targetPlan.index !== FlightPlanIndex.Temporary) { + if (verticalWaypoint && verticalWaypoint.altitude) { + // Just show prediction + let altPrefix = ''; + if (hasAltConstraint && !isFromLeg) { + altPrefix = `{big}${verticalWaypoint.isAltitudeConstraintMet ? '{magenta}*{end}' : '{amber}*{end}'}{end}`; + } + + altitudeConstraint = + altPrefix + + formatAltitudeOrLevel(mcdu, verticalWaypoint.altitude, useTransitionAltitude).padStart(5, '\xa0'); + altColor = color; + altSize = isFromLeg ? 'big' : 'small'; + } else if (hasAltConstraint) { + altitudeConstraint = formatAltConstraint(mcdu, wp.altitudeConstraint, useTransitionAltitude); + altColor = 'magenta'; + altSize = wp.hasPilotEnteredAltitudeConstraint() ? 'big' : 'small'; + } else if (inAlternate && fpIndex === targetPlan.alternateFlightPlan.destinationLegIndex) { + if ( + legIsRunway(wp) && + targetPlan.alternateFlightPlan.destinationRunway && + Number.isFinite(targetPlan.alternateFlightPlan.destinationRunway.thresholdCrossingHeight) + ) { + altitudeConstraint = formatAlt(targetPlan.alternateFlightPlan.destinationRunway.thresholdCrossingHeight); + altColor = color; + altSize = 'small'; + } else if ( + legIsAirport(wp) && + targetPlan.alternateFlightPlan.destinationAirport && + Number.isFinite(targetPlan.alternateFlightPlan.destinationAirport.location.alt) + ) { + altitudeConstraint = formatAlt(targetPlan.alternateFlightPlan.destinationAirport.location.alt); + altColor = color; + altSize = 'small'; + } + } else if (inAlternate && fpIndex === targetPlan.alternateFlightPlan.originLegIndex) { + if ( + legIsRunway(wp) && + targetPlan.alternateFlightPlan.originRunway && + Number.isFinite(targetPlan.alternateFlightPlan.originRunway.thresholdLocation.alt) + ) { + altitudeConstraint = formatAlt(targetPlan.alternateFlightPlan.originRunway.thresholdLocation.alt); + altColor = color; + } else if ( + legIsAirport(wp) && + targetPlan.alternateFlightPlan.originAirport && + Number.isFinite(targetPlan.alternateFlightPlan.originAirport.location.alt) + ) { + altitudeConstraint = formatAlt(targetPlan.alternateFlightPlan.originAirport.location.alt); + altColor = color; + } + } else if (!inAlternate && fpIndex === targetPlan.destinationLegIndex) { + if ( + legIsRunway(wp) && + targetPlan.destinationRunway && + Number.isFinite(targetPlan.destinationRunway.thresholdCrossingHeight) + ) { + altitudeConstraint = formatAlt(targetPlan.destinationRunway.thresholdCrossingHeight); + altColor = color; + altSize = 'small'; + } else if ( + legIsAirport(wp) && + targetPlan.destinationAirport && + Number.isFinite(targetPlan.destinationAirport.location.alt) + ) { + altitudeConstraint = formatAlt(targetPlan.destinationAirport.location.alt); + altColor = color; + altSize = 'small'; + } + } else if (!inAlternate && fpIndex === targetPlan.originLegIndex) { + if ( + legIsRunway(wp) && + targetPlan.originRunway && + Number.isFinite(targetPlan.originRunway.thresholdLocation.alt) + ) { + altitudeConstraint = formatAlt(targetPlan.originRunway.thresholdLocation.alt); + altColor = color; + } else if ( + legIsAirport(wp) && + targetPlan.originAirport && + Number.isFinite(targetPlan.originAirport.location.alt) + ) { + altitudeConstraint = formatAlt(targetPlan.originAirport.location.alt); + altColor = color; + } + } + } + + // forced turn indication if next leg is not a course reversal + if (wpNext && wpNext.isDiscontinuity === false && legTurnIsForced(wpNext) && !legTypeIsCourseReversal(wpNext)) { + if (wpNext.definition.turnDirection === 'L') { + ident += '{'; + } else { + ident += '}'; + } + + // the overfly symbol is not shown in this case + isOverfly = false; + } + + scrollWindow[rowI] = { + fpIndex, + inAlternate: inAlternate, + active: wpActive, + ident: ident, + color, + distance, + fpa, + spdColor, + speedConstraint, + altColor, + altSize, + altitudeConstraint, + timeCell, + timeColor, + fixAnnotation: fixAnnotation ? fixAnnotation : '', + bearingTrack, + isOverfly, + }; + + if (fpIndex !== targetPlan.destinationLegIndex) { + addLskAt( + rowI, + (value) => { + if (value === '') { + return mcdu.getDelaySwitchPage(); + } + return mcdu.getDelayBasic(); + }, + (value, scratchpadCallback) => { + switch (value) { + case '': + CDULateralRevisionPage.ShowPage(mcdu, wp, fpIndex, forPlan, inAlternate); + break; + case Keypad.clrValue: + CDUFlightPlanPage.clearElement(mcdu, fpIndex, offset, forPlan, inAlternate, scratchpadCallback); + break; + case Keypad.ovfyValue: + mcdu.toggleWaypointOverfly(fpIndex, forPlan, inAlternate, () => { + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + }); + break; + default: + if (value.length > 0) { + mcdu.insertWaypoint( + value, + forPlan, + inAlternate, + fpIndex, + true, + (success) => { + if (!success) { + scratchpadCallback(); + } + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + }, + !mcdu.flightPlanService.hasTemporary, + ); + } + break; + } + }, + ); + } else { + addLskAt( + rowI, + () => mcdu.getDelaySwitchPage(), + (value, scratchpadCallback) => { + if (value === '') { + CDULateralRevisionPage.ShowPage(mcdu, wp, fpIndex, forPlan, inAlternate); + } else if (value.length > 0) { + mcdu.insertWaypoint( + value, + forPlan, + inAlternate, + fpIndex, + true, + (success) => { + if (!success) { + scratchpadCallback(); + } + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + }, + true, + ); + } + }, + ); + } + + addRskAt( + rowI, + () => mcdu.getDelaySwitchPage(), + (value, scratchpadCallback) => { + if (value === '') { + CDUVerticalRevisionPage.ShowPage( + mcdu, + wp, + fpIndex, + verticalWaypoint, + undefined, + undefined, + undefined, + forPlan, + inAlternate, + ); + } else { + CDUVerticalRevisionPage.setConstraints( + mcdu, + wp, + fpIndex, + verticalWaypoint, + value, + scratchpadCallback, + offset, + forPlan, + inAlternate, + ); + } + }, + ); + } else if (pwp) { + const baseColor = forActiveOrTemporary ? (mcdu.flightPlanService.hasTemporary ? 'yellow' : 'green') : 'white'; + const color = isActive ? 'white' : baseColor; + + // TODO: PWP should not be shown while predictions are recomputed or in a temporary flight plan, + // but if I don't show them, the flight plan jumps around because the offset is no longer correct if the number of items in the flight plan changes. + // Like this, they are still there, but have dashes for predictions. + const shouldHidePredictions = + !fmsGeometryProfile || !fmsGeometryProfile.isReadyToDisplay || !pwp.flightPlanInfo; + + let timeCell: string = Time.NoPrediction; + let timeColor = 'white'; + if (!shouldHidePredictions && Number.isFinite(pwp.flightPlanInfo.secondsFromPresent)) { + const utcTime = SimVar.GetGlobalVarValue('ZULU TIME', 'seconds'); + timeColor = color; + + timeCell = isFlying + ? `${FmsFormatters.secondsToUTC(utcTime + pwp.flightPlanInfo.secondsFromPresent)}[s-text]` + : `${FmsFormatters.secondsTohhmm(pwp.flightPlanInfo.secondsFromPresent)}[s-text]`; + } + + let speed: string = Speed.NoPrediction; + let spdColor = 'white'; + if (!shouldHidePredictions && Number.isFinite(pwp.flightPlanInfo.speed)) { + speed = `{small}${pwp.flightPlanInfo.speed < 1 ? formatMachNumber(pwp.flightPlanInfo.speed) : Math.round(pwp.flightPlanInfo.speed).toFixed(0)}{end}`; + spdColor = color; + } + + let altitudeConstraint: string = Altitude.NoPrediction; + let altColor = 'white'; + if (!shouldHidePredictions && Number.isFinite(pwp.flightPlanInfo.altitude)) { + altitudeConstraint = formatAltitudeOrLevel(mcdu, pwp.flightPlanInfo.altitude, useTransitionAltitude); + altColor = color; + } + + let distance = undefined; + if (!shouldHidePredictions) { + distance = isActive + ? mcdu.guidanceController.activeLegAlongTrackCompletePathDtg - pwp.distanceFromLegTermination + : distanceFromLastLine; + } + + scrollWindow[rowI] = { + fpIndex: fpIndex, + active: isActive, + ident: pwp.mcduIdent || pwp.ident, + color, + distance: distance !== undefined ? Math.round(Math.max(0, Math.min(9999, distance))).toFixed(0) : '', + spdColor, + speedConstraint: speed, + altColor, + altSize: 'small', + altitudeConstraint, + timeCell, + timeColor, + fixAnnotation: `{${color}}${pwp.mcduHeader || ''}{end}`, + bearingTrack, + isOverfly: false, + }; + + addLskAt(rowI, 0, (value) => { + if (value === Keypad.clrValue) { + // TODO + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + } + }); + } else if (marker) { + // Marker + scrollWindow[rowI] = waypointsAndMarkers[winI]; + addLskAt(rowI, 0, (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + CDUFlightPlanPage.clearElement(mcdu, fpIndex, offset, forPlan, inAlternate, scratchpadCallback); + return; + } else if (value === '') { + return; + } + + // Insert after last leg if we click on a marker after the flight plan + const insertionIndex = fpIndex < targetPlan.legCount ? fpIndex : targetPlan.legCount - 1; + const insertBeforeVsAfterIndex = fpIndex < targetPlan.legCount; + + // Cannot insert after MANUAL leg + const previousElement = targetPlan.maybeElementAt(fpIndex - 1); + if (previousElement && previousElement.isDiscontinuity === false && previousElement.isVectors()) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } + + mcdu.insertWaypoint( + value, + forPlan, + inAlternate, + insertionIndex, + insertBeforeVsAfterIndex, + (success) => { + if (!success) { + scratchpadCallback(); + } + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + }, + !mcdu.flightPlanService.hasTemporary, + ); + }); + } else if (holdResumeExit && holdResumeExit.isDiscontinuity === false) { + const isNext = fpIndex === targetPlan.activeLegIndex + 1; + + let color = legAccentColor; + if (isActive) { + color = 'white'; + } + + const decelReached = isActive || (isNext && mcdu.holdDecelReached); + const holdSpeed = + fpIndex === mcdu.holdIndex && mcdu.holdSpeedTarget > 0 ? mcdu.holdSpeedTarget.toFixed(0) : '\xa0\xa0\xa0'; + const turnDirection = holdResumeExit.definition.turnDirection; + // prompt should only be shown once entering decel for hold (3 - 20 NM before hold) + const immExit = decelReached && !holdResumeExit.holdImmExit; + const resumeHold = decelReached && holdResumeExit.holdImmExit; + + scrollWindow[rowI] = { + fpIndex, + holdResumeExit, + color, + immExit, + resumeHold, + holdSpeed, + turnDirection, + }; + + addLskAt(rowI, 0, (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + CDUFlightPlanPage.clearElement(mcdu, fpIndex, offset, forPlan, inAlternate, scratchpadCallback); + return; + } + + CDUHoldAtPage.ShowPage(mcdu, fpIndex, forPlan, inAlternate); + scratchpadCallback(); + }); + + addRskAt(rowI, 0, (value, scratchpadCallback) => { + // IMM EXIT, only active once reaching decel + if (isActive) { + mcdu.fmgcMesssagesListener.triggerToAllSubscribers('A32NX_IMM_EXIT', fpIndex, immExit); + setTimeout(() => { + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + }, 500); + } else if (decelReached) { + CDUFlightPlanPage.clearElement(mcdu, fpIndex, offset, forPlan, inAlternate, scratchpadCallback); + return; + } + scratchpadCallback(); + }); + } + } + + CDUFlightPlanPage.updatePlanCentre(mcdu, waypointsAndMarkers, offset, forPlan, 'L'); + CDUFlightPlanPage.updatePlanCentre(mcdu, waypointsAndMarkers, offset, forPlan, 'R'); + + const isMissedApproachLegShown = + targetPlan && + scrollWindow.some( + (row, index) => index > 0 && !row.marker && row.fpIndex >= targetPlan.firstMissedApproachLegIndex, + ); + const isAlternateLegShown = scrollWindow.some((row, index) => index > 0 && !row.marker && row.inAlternate); + const isAlternateMissedApproachLegShown = + targetPlan && + targetPlan.alternateFlightPlan && + scrollWindow.some( + (row, index) => + index > 0 && + !row.marker && + row.inAlternate && + row.fpIndex >= targetPlan.alternateFlightPlan.firstMissedApproachLegIndex, + ); + + mcdu.efisInterfaces['L'].setShownFplnLegs( + isMissedApproachLegShown, + isAlternateLegShown, + isAlternateMissedApproachLegShown, + ); + mcdu.efisInterfaces['R'].setShownFplnLegs( + isMissedApproachLegShown, + isAlternateLegShown, + isAlternateMissedApproachLegShown, + ); + + mcdu.onUnload = () => { + CDUFlightPlanPage.updatePlanCentre(mcdu, waypointsAndMarkers, 0, FlightPlanIndex.Active, 'L'); + CDUFlightPlanPage.updatePlanCentre(mcdu, waypointsAndMarkers, 0, FlightPlanIndex.Active, 'R'); + + mcdu.efisInterfaces['L'].setShownFplnLegs(false, false, false); + mcdu.efisInterfaces['R'].setShownFplnLegs(false, false, false); + }; + + // Render scrolling data to text >> add ditto marks + + let firstWp = scrollWindow.length; + const scrollText = []; + for (let rowI = 0; rowI < scrollWindow.length; rowI++) { + const { + marker: cMarker, + holdResumeExit: cHold, + spdColor: cSpdColor, + altColor: cAltColor, + speedConstraint: cSpd, + altitudeConstraint: cAlt, + ident: cIdent, + } = scrollWindow[rowI]; + let spdRpt = false; + let altRpt = false; + let showFix = true; + let showDist = true; + let showNm = false; + + if (cHold) { + const { color, immExit, resumeHold, holdSpeed, turnDirection } = scrollWindow[rowI]; + scrollText[rowI * 2] = [ + '', + `{amber}${immExit ? 'IMM\xa0\xa0' : ''}${resumeHold ? 'RESUME\xa0' : ''}{end}`, + 'HOLD\xa0\xa0\xa0\xa0', + ]; + scrollText[rowI * 2 + 1] = [ + `{${color}}HOLD ${turnDirection}{end}`, + `{amber}${immExit ? 'EXIT*' : ''}${resumeHold ? 'HOLD*' : ''}{end}`, + `\xa0{${color}}{small}{white}SPD{end}\xa0${holdSpeed}{end}{end}`, + ]; + } else if (!cMarker) { + // Waypoint + if (rowI > 0) { + const { + marker: pMarker, + pwp: pPwp, + holdResumeExit: pHold, + speedConstraint: pSpd, + altitudeConstraint: pAlt, + } = scrollWindow[rowI - 1]; + if (!pMarker && !pPwp && !pHold) { + firstWp = Math.min(firstWp, rowI); + if (rowI === firstWp) { + showNm = true; + } + if (cSpd !== Speed.NoPrediction && cSpdColor !== 'magenta' && cSpd === pSpd) { + spdRpt = true; + } + + if (cAlt !== Altitude.NoPrediction && cAltColor !== 'magenta' && cAlt === pAlt) { + altRpt = true; + } + // If previous row is a marker, clear all headers unless it's a speed limit + } else if (!pHold) { + showDist = false; + showFix = cIdent === '(LIM)'; + } + } + + scrollText[rowI * 2] = renderFixHeader(scrollWindow[rowI], showNm, showDist, showFix); + scrollText[rowI * 2 + 1] = renderFixContent(scrollWindow[rowI], spdRpt, altRpt); + + // Marker + } else { + scrollText[rowI * 2] = []; + scrollText[rowI * 2 + 1] = cMarker; + } + } + + // Destination (R6) + + const destText = []; + if (mcdu.flightPlanService.hasTemporary) { + destText[0] = [' ', ' ']; + destText[1] = ['{ERASE[color]amber', 'INSERT*[color]amber']; + + addLskAt(5, 0, async () => { + mcdu.eraseTemporaryFlightPlan(() => { + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }); + addRskAt(5, 0, async () => { + mcdu.insertTemporaryFlightPlan(() => { + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }); + } else { + let destCell = '----'; + if (targetPlan.destinationAirport) { + destCell = targetPlan.destinationAirport.ident; + + if (targetPlan.destinationRunway) { + destCell = targetPlan.destinationRunway.ident; + } + } + let destTimeCell = '----'; + let destDistCell = '----'; + let destEFOBCell = '---.-'; + + if (targetPlan.destinationAirport) { + if (CDUInitPage.fuelPredConditionsMet(mcdu) && mcdu._fuelPredDone) { + mcdu.tryUpdateRouteTrip(isFlying); + } + + const destDist = mcdu.guidanceController.alongTrackDistanceToDestination; + + if (Number.isFinite(destDist)) { + destDistCell = Math.round(destDist).toFixed(0); + } + + if (fmsGeometryProfile && fmsGeometryProfile.isReadyToDisplay) { + const destEfob = fmsGeometryProfile.getRemainingFuelAtDestination(); + + if (Number.isFinite(destEfob)) { + destEFOBCell = (NXUnits.poundsToUser(destEfob) / 1000).toFixed(1); + } + + const timeRemaining = fmsGeometryProfile.getTimeToDestination(); + if (Number.isFinite(timeRemaining)) { + const utcTime = SimVar.GetGlobalVarValue('ZULU TIME', 'seconds'); + + destTimeCell = isFlying + ? FmsFormatters.secondsToUTC(utcTime + timeRemaining) + : FmsFormatters.secondsTohhmm(timeRemaining); + } + } + } + + destText[0] = ['\xa0DEST', 'DIST\xa0\xa0EFOB', isFlying ? '\xa0UTC{sp}{sp}{sp}{sp}' : 'TIME{sp}{sp}{sp}{sp}']; + destText[1] = [ + destCell, + `{small}${destDistCell.padStart(4, '\xa0')}\xa0${destEFOBCell.padStart(5, '\xa0')}{end}`, + `{small}${destTimeCell}{end}{sp}{sp}{sp}{sp}`, + ]; + + addLskAt( + 5, + () => mcdu.getDelaySwitchPage(), + () => { + CDULateralRevisionPage.ShowPage(mcdu, targetPlan.destinationLeg, targetPlan.destinationLegIndex, forPlan); + }, + ); + + addRskAt( + 5, + () => mcdu.getDelaySwitchPage(), + () => { + CDUVerticalRevisionPage.ShowPage( + mcdu, + targetPlan.destinationLeg, + targetPlan.destinationLegIndex, + undefined, + undefined, + undefined, + undefined, + forPlan, + false, + ); + }, + ); + } + + // scrollText pad to 10 rows + while (scrollText.length < 10) { + scrollText.push(['']); + } + const allowScroll = waypointsAndMarkers.length > 4; + if (allowScroll) { + mcdu.onAirport = () => { + // Only called if > 4 waypoints + const isOnFlightPlanPage = mcdu.page.Current === mcdu.page.FlightPlanPage; + const allowCycleToOriginAirport = mcdu.flightPhaseManager.phase === FmgcFlightPhase.Preflight; + if ( + offset >= Math.max(destinationAirportOffset, alternateAirportOffset) && + allowCycleToOriginAirport && + isOnFlightPlanPage + ) { + // only show origin if still on ground + // Go back to top of flight plan page to show origin airport. + offset = 0; + } else if (offset >= destinationAirportOffset && offset < alternateAirportOffset) { + offset = alternateAirportOffset; + } else { + offset = destinationAirportOffset; + } + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + }; + mcdu.onDown = () => { + // on page down decrement the page offset. + if (offset > 0) { + // if page not on top + offset--; + } else { + // else go to the bottom + offset = waypointsAndMarkers.length - 1; + } + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + }; + mcdu.onUp = () => { + if (offset < waypointsAndMarkers.length - 1) { + // if page not on bottom + offset++; + } else { + // else go on top + offset = 0; + } + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + }; + } + mcdu.setArrows(allowScroll, allowScroll, true, true); + scrollText[0][1] = 'SPD/ALT\xa0\xa0\xa0'; + scrollText[0][2] = isFlying ? '\xa0UTC{sp}{sp}{sp}{sp}' : 'TIME{sp}{sp}{sp}{sp}'; + mcdu.setTemplate([ + [ + `{left}{small}{sp}${showFrom ? 'FROM' : '{sp}{sp}{sp}{sp}'}{end}${headerText}{end}{right}{small}${flightNumberText}{sp}{sp}{sp}{end}{end}`, + ], + ...scrollText, + ...destText, + ]); + } + + static async clearElement( + mcdu: LegacyFmsPageInterface, + fpIndex: number, + offset: number, + forPlan: number, + forAlternate: boolean, + scratchpadCallback: () => void, + ) { + if (!this.ensureCanClearElement(mcdu, fpIndex, forPlan, forAlternate, scratchpadCallback)) { + return; + } + + const targetPlan = forAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + const element = targetPlan.elementAt(fpIndex); + + const previousElement = targetPlan.maybeElementAt(fpIndex - 1); + + let insertDiscontinuity = true; + if (element.isDiscontinuity === false) { + if (element.isHX() || (!forAlternate && fpIndex <= targetPlan.activeLegIndex)) { + insertDiscontinuity = false; + } else if (previousElement.isDiscontinuity === false && previousElement.type === 'PI' && element.type === 'CF') { + insertDiscontinuity = + element.definition.waypoint?.databaseId === previousElement.definition.recommendedNavaid?.databaseId; + } + } else { + insertDiscontinuity = false; + } + + try { + await mcdu.flightPlanService.deleteElementAt(fpIndex, insertDiscontinuity, forPlan, forAlternate); + console.log('deleting element'); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + scratchpadCallback(); + } + + CDUFlightPlanPage.ShowPage(mcdu, offset, forPlan); + } + + static ensureCanClearElement( + mcdu: LegacyFmsPageInterface, + fpIndex: number, + forPlan: number, + forAlternate: boolean, + scratchpadCallback: { (): void; (): void }, + ) { + const targetPlan = forAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + + if (forPlan === FlightPlanIndex.Active && mcdu.flightPlanService.hasTemporary) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return false; + } else if (fpIndex === targetPlan.originLegIndex || fpIndex === targetPlan.destinationLegIndex) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return false; + } + + // TODO maybe move this to FMS logic ? + if (forPlan === FlightPlanIndex.Active && !forAlternate && fpIndex <= mcdu.flightPlanService.activeLegIndex) { + // 22-72-00:67 + // Stop clearing TO or FROM waypoints when NAV is engaged + if (mcdu.navModeEngaged()) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowedInNav); + scratchpadCallback(); + return false; + } else if ( + fpIndex === targetPlan.fromLegIndex /* TODO check this is ppos */ && + fpIndex + 2 === targetPlan.destinationLegIndex + ) { + const nextElement = targetPlan.elementAt(fpIndex + 1); + if (nextElement.isDiscontinuity === true) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return false; + } + } + } + + const element = targetPlan.maybeElementAt(fpIndex); + + if (!element) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return false; + } + + const previousElement = targetPlan.maybeElementAt(fpIndex - 1); + + if (element.isDiscontinuity === true) { + if (previousElement && previousElement.isDiscontinuity === false && previousElement.isVectors()) { + // Cannot clear disco after MANUAL + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return false; + } else if (fpIndex - 1 === targetPlan.fromLegIndex && forPlan === FlightPlanIndex.Active && !forAlternate) { + // Cannot clear disco after FROM leg + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return false; + } else if (fpIndex - 1 === targetPlan.originLegIndex && fpIndex + 1 === targetPlan.destinationLegIndex) { + if (targetPlan.originAirport.ident === targetPlan.destinationAirport.ident) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return false; + } + } + } + + return true; + } + + static updatePlanCentre(mcdu, waypointsAndMarkers, offset, forPlan, side) { + const forActiveOrTemporary = forPlan === 0; + const targetPlan = forActiveOrTemporary + ? mcdu.flightPlanService.activeOrTemporary + : mcdu.flightPlanService.secondary(1); + + for (let i = 0; i < waypointsAndMarkers.length; i++) { + const { wp, inAlternate, fpIndex } = waypointsAndMarkers[(offset + i + 1) % waypointsAndMarkers.length]; + if (wp) { + mcdu.efisInterfaces[side].setPlanCentre(targetPlan.index, fpIndex, inAlternate); + break; + } + } + } +} + +function renderFixHeader( + rowObj: { fixAnnotation: string; color: string; distance: string; bearingTrack: string; fpa: string }, + showNm = false, + showDist = true, + showFix = true, +) { + const { fixAnnotation, color, distance, bearingTrack, fpa } = rowObj; + let right = showDist ? `{${color}}${distance}{end}` : ''; + if (fpa) { + right += `{white}${fpa}°{end}`; + } else if (showNm) { + right += `{${color}}NM{end}\xa0\xa0\xa0`; + } else { + right += '\xa0\xa0\xa0\xa0\xa0'; + } + return [ + `${showFix ? fixAnnotation.padEnd(7, '\xa0').padStart(8, '\xa0') : ''}`, + right, + `{${color}}${bearingTrack}{end}\xa0`, + ]; +} + +function renderFixContent( + rowObj: { + ident: string; + isOverfly: boolean; + color: string; + spdColor: string; + speedConstraint: string; + altColor: string; + altSize: string; + altitudeConstraint: string; + timeCell: string; + timeColor: string; + }, + spdRepeat = false, + altRepeat = false, +) { + const { + ident, + isOverfly, + color, + spdColor, + speedConstraint, + altColor, + altSize, + altitudeConstraint, + timeCell, + timeColor, + } = rowObj; + + return [ + `${ident}${isOverfly ? Keypad.ovfyValue : ''}[color]${color}`, + `{${spdColor}}${spdRepeat ? '\xa0"\xa0' : speedConstraint}{end}{${altColor}}{${altSize}}/${altRepeat ? '\xa0\xa0\xa0"\xa0\xa0' : altitudeConstraint.padStart(6, '\xa0')}{end}{end}`, + `${timeCell}{sp}{sp}{sp}{sp}[color]${timeColor}`, + ]; +} + +function emptyFplnPage(forPlan) { + return [ + ['', 'SPD/ALT{sp}{sp}{sp}', 'TIME{sp}{sp}{sp}{sp}'], + [`PPOS[color]${forPlan === 0 ? 'green' : 'white'}`, '{sp}{sp}{sp}/ -----', '----{sp}{sp}{sp}{sp}'], + [''], + ['---F-PLN DISCONTINUITY---'], + [''], + ['------END OF F-PLN-------'], + [''], + ['-----NO ALTN F-PLN-------'], + [''], + [''], + ['\xa0DEST', 'DIST\xa0\xa0EFOB', 'TIME{sp}{sp}{sp}{sp}'], + ['-------', '----\xa0---.-[s-text]', '----{sp}{sp}{sp}{sp}[s-text]'], + ]; +} + +/** + * Check whether leg is a course reversal leg + * @returns true if leg is a course reversal leg + */ +function legTypeIsCourseReversal(leg: FlightPlanLeg) { + switch (leg.type) { + case 'HA': + case 'HF': + case 'HM': + case 'PI': + return true; + default: + } + return false; +} + +/** + * Check whether leg has a coded forced turn direction + * @returns true if leg has coded forced turn direction + */ +function legTurnIsForced(leg: FlightPlanLeg) { + // forced turns are only for straight legs + return ( + (leg.definition.turnDirection === 'L' /* Left */ || leg.definition.turnDirection === 'R') /* Right */ && + leg.type !== 'AF' && + leg.type !== 'RF' + ); +} + +function formatMachNumber(rawNumber: number) { + return (Math.round(100 * rawNumber) / 100).toFixed(2).slice(1); +} + +function legHasAltConstraint(leg: FlightPlanLeg): boolean { + return !leg.isXA() && (leg.hasPilotEnteredAltitudeConstraint() || leg.hasDatabaseAltitudeConstraint()); +} + +function legIsRunway(leg: FlightPlanLeg): boolean { + return leg.definition && leg.definition.waypointDescriptor === 4; +} + +function legIsAirport(leg: FlightPlanLeg): boolean { + return leg.definition && leg.definition.waypointDescriptor === 1; +} + +/** + * Formats an altitude as an altitude or flight level for display. + * @param mcdu Reference to the MCDU instance + * @param alt The altitude in feet. + * @param useTransAlt Whether to use transition altitude, otherwise transition level is used. + * @returns The formatted altitude/level. + */ +function formatAltitudeOrLevel(mcdu: LegacyFmsPageInterface, alt: number, useTransAlt: boolean): string { + const activePlan = mcdu.flightPlanService.active; + + let isFl = false; + if (useTransAlt) { + const transAlt = activePlan.performanceData.transitionAltitude; + isFl = transAlt !== null && alt > transAlt; + } else { + const transLevel = activePlan.performanceData.transitionLevel; + isFl = transLevel !== null && alt >= transLevel * 100; + } + + if (isFl) { + return `FL${(alt / 100).toFixed(0).padStart(3, '0')}`; + } + + return formatAlt(alt); +} + +function formatTrack(from: FlightPlanLeg, to: { definition: { waypoint: { location: LatLongData }; type: string } }) { + // TODO: Does this show something for non-waypoint terminated legs? + if ( + !from || + !from.definition || + !from.definition.waypoint || + !from.definition.waypoint.location || + !to || + !to.definition || + !to.definition.waypoint || + to.definition.type === 'HM' + ) { + return ''; + } + + const magVar = Facilities.getMagVar(from.definition.waypoint.location.lat, from.definition.waypoint.location.long); + const tr = Avionics.Utils.computeGreatCircleHeading( + from.definition.waypoint.location, + to.definition.waypoint.location, + ); + const track = A32NX_Util.trueToMagnetic(tr, magVar); + return `TRK${track.toFixed(0).padStart(3, '0')}\u00b0`; +} + +/** + * Formats a numberical altitude to a string to be displayed in the altitude column. Does not format FLs, use {@link formatAltitudeOrLevel} for this purpose + * @param alt The altitude to format + * @returns The formatted altitude string + */ +function formatAlt(alt: number): string { + return (Math.round(alt / 10) * 10).toFixed(0); +} + +function formatAltConstraint( + mcdu: LegacyFmsPageInterface, + constraint: { altitudeDescriptor: AltitudeDescriptor; altitude1: number; altitude2: number }, + useTransAlt: boolean, +) { + if (!constraint) { + return ''; + } + + // Altitude constraint types "G" and "H" are not shown in the flight plan + switch (constraint.altitudeDescriptor) { + case AltitudeDescriptor.AtAlt1: + case AltitudeDescriptor.AtAlt1GsIntcptAlt2: + case AltitudeDescriptor.AtAlt1AngleAlt2: + return formatAltitudeOrLevel(mcdu, constraint.altitude1, useTransAlt); + case AltitudeDescriptor.AtOrAboveAlt1: + case AltitudeDescriptor.AtOrAboveAlt1GsIntcptAlt2: + case AltitudeDescriptor.AtOrAboveAlt1AngleAlt2: + return '+' + formatAltitudeOrLevel(mcdu, constraint.altitude1, useTransAlt); + case AltitudeDescriptor.AtOrBelowAlt1: + case AltitudeDescriptor.AtOrBelowAlt1AngleAlt2: + return '-' + formatAltitudeOrLevel(mcdu, constraint.altitude1, useTransAlt); + case AltitudeDescriptor.BetweenAlt1Alt2: + return 'WINDOW'; + case AltitudeDescriptor.AtOrAboveAlt2: + return '+' + formatAltitudeOrLevel(mcdu, constraint.altitude2, useTransAlt); + default: + return ''; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_FuelPredPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_FuelPredPage.ts new file mode 100644 index 00000000000..bd8dd40e9ed --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_FuelPredPage.ts @@ -0,0 +1,330 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { NXUnits } from '@flybywiresim/fbw-sdk'; +import { getZfw, getZfwcg } from '../legacy/A32NX_Core/A32NX_PayloadManager'; +import { CDUInitPage } from './A320_Neo_CDU_InitPage'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FmsFormatters } from '../legacy/FmsFormatters'; + +export class CDUFuelPredPage { + static ShowPage(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.FuelPredPage; + mcdu.pageRedrawCallback = () => CDUFuelPredPage.ShowPage(mcdu); + mcdu.activeSystem = 'FMGC'; + const isFlying = mcdu.isFlying(); + + const destination = mcdu.flightPlanService.active ? mcdu.flightPlanService.active.destinationAirport : undefined; + const alternate = mcdu.flightPlanService.active + ? mcdu.flightPlanService.active.alternateDestinationAirport + : undefined; + + let destIdentCell = 'NONE'; + let destTimeCell = '----'; + let destTimeCellColor = '[color]white'; + let destEFOBCell = '---.-'; + let destEFOBCellColor = '[color]white'; + + let altIdentCell = 'NONE'; + let altTimeCell = '----'; + let altTimeCellColor = '[color]white'; + let altEFOBCell = '---.-'; + let altEFOBCellColor = '[color]white'; + + let rteRsvWeightCell = '---.-'; + let rteRsvPercentCell = '---.-'; + let rteRSvCellColor = '[color]white'; + let rteRsvPctColor = '{white}'; + + let zfwCell = '___._'; + let zfwCgCell = ' __._'; + let zfwColor = '[color]amber'; + mcdu.onRightInput[2] = async (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } else if (value === '') { + mcdu.setScratchpadText( + (isFinite(getZfw()) ? (getZfw() / 1000).toFixed(1) : '') + + '/' + + (isFinite(getZfwcg()) ? getZfwcg().toFixed(1) : ''), + ); + } else { + if (mcdu.trySetZeroFuelWeightZFWCG(value)) { + CDUFuelPredPage.ShowPage(mcdu); + CDUInitPage.trySetFuelPred(mcdu); + mcdu.removeMessageFromQueue(NXSystemMessages.initializeWeightOrCg.text); + mcdu.removeMessageFromQueue(NXSystemMessages.checkWeight.text); + mcdu._checkWeightSettable = true; + } else { + scratchpadCallback(); + } + } + }; + + let altFuelCell = '---.-'; + let altFuelTimeCell = '----'; + let altFuelColor = '[color]white'; + let altTimeColor = '{white}'; + + let fobCell = '---.-'; + let fobOtherCell = '-----'; + let fobCellColor = '[color]white'; + + let finalFuelCell = '---.-'; + let finalTimeCell = '----'; + let finalColor = '[color]white'; + + let gwCell = '---.-'; + let cgCell = ' --.-'; + let gwCgCellColor = '[color]white'; + + let minDestFobCell = '---.-'; + let minDestFobCellColor = '[color]white'; + + let extraFuelCell = '---.-'; + let extraTimeCell = '----'; + let extraCellColor = '[color]white'; + let extraTimeColor = '{white}'; + + if (mcdu._zeroFuelWeightZFWCGEntered) { + if (isFinite(mcdu.zeroFuelWeight)) { + zfwCell = NXUnits.kgToUser(mcdu.zeroFuelWeight).toFixed(1); + zfwColor = '[color]cyan'; + } + if (isFinite(mcdu.zeroFuelWeightMassCenter)) { + zfwCgCell = getZfwcg().toFixed(1); + } + if (isFinite(mcdu.zeroFuelWeight) && isFinite(getZfwcg())) { + zfwColor = '[color]cyan'; + } + + if (alternate) { + altIdentCell = alternate.ident; + } + + if (destination) { + destIdentCell = destination.ident; + } + + gwCell = '{small}' + NXUnits.kgToUser(mcdu.getGW()).toFixed(1); + cgCell = mcdu.getCG().toFixed(1) + '{end}'; + gwCgCellColor = '[color]green'; + + fobCell = '{small}' + NXUnits.kgToUser(mcdu.getFOB()).toFixed(1) + '{end}'; + fobOtherCell = '{inop}FF{end}'; + fobCellColor = '[color]cyan'; + } + + if (CDUInitPage.fuelPredConditionsMet(mcdu)) { + const utcTime = SimVar.GetGlobalVarValue('ZULU TIME', 'seconds'); + + if (mcdu._fuelPredDone) { + if (!mcdu.routeFinalEntered()) { + mcdu.tryUpdateRouteFinalFuel(); + } + if (isFinite(mcdu.getRouteFinalFuelWeight()) && isFinite(mcdu.getRouteFinalFuelTime())) { + if (mcdu._rteFinalWeightEntered) { + finalFuelCell = '{sp}{sp}' + NXUnits.kgToUser(mcdu.getRouteFinalFuelWeight()).toFixed(1); + } else { + finalFuelCell = '{sp}{sp}{small}' + NXUnits.kgToUser(mcdu.getRouteFinalFuelWeight()).toFixed(1) + '{end}'; + } + if (mcdu._rteFinalTimeEntered || !mcdu.routeFinalEntered()) { + finalTimeCell = FmsFormatters.minutesTohhmm(mcdu.getRouteFinalFuelTime()); + } else { + finalTimeCell = '{small}' + FmsFormatters.minutesTohhmm(mcdu.getRouteFinalFuelTime()) + '{end}'; + } + finalColor = '[color]cyan'; + } + mcdu.onLeftInput[4] = async (value, scratchpadCallback) => { + if (await mcdu.trySetRouteFinalFuel(value)) { + CDUFuelPredPage.ShowPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + if (alternate) { + const altnFuelEntered = mcdu._routeAltFuelEntered; + if (!altnFuelEntered) { + mcdu.tryUpdateRouteAlternate(); + } + if (isFinite(mcdu.getRouteAltFuelWeight())) { + altFuelCell = + '{sp}{sp}' + + (altnFuelEntered ? '' : '{small}') + + NXUnits.kgToUser(mcdu.getRouteAltFuelWeight()).toFixed(1); + altFuelColor = '[color]cyan'; + const time = mcdu.getRouteAltFuelTime(); + if (time) { + altFuelTimeCell = '{small}' + FmsFormatters.minutesTohhmm(time) + '{end}'; + altTimeColor = '{green}'; + } else { + altFuelTimeCell = '----'; + altTimeColor = '{white}'; + } + } + } else { + altFuelCell = '{sp}{sp}{small}0.0{end}'; + altFuelTimeCell = '----'; + altFuelColor = '[color]green'; + altTimeColor = '{white}'; + } + mcdu.onLeftInput[3] = async (value, scratchpadCallback) => { + if (await mcdu.trySetRouteAlternateFuel(value)) { + CDUFuelPredPage.ShowPage(mcdu); + } else { + scratchpadCallback(); + } + }; + if (alternate) { + altIdentCell = alternate.ident; + altEFOBCell = NXUnits.kgToUser(mcdu.getAltEFOB(true)).toFixed(1); + altEFOBCellColor = '[color]green'; + } + + mcdu.tryUpdateRouteTrip(isFlying); + + const dest = mcdu.flightPlanService.active.destinationAirport; + + if (dest) { + destIdentCell = dest.ident; + } + const efob = mcdu.getDestEFOB(true); + destEFOBCell = NXUnits.kgToUser(efob).toFixed(1); + // Should we use predicted values or liveETATo and liveUTCto? + destTimeCell = isFlying + ? FmsFormatters.secondsToUTC(utcTime + FmsFormatters.minuteToSeconds(mcdu._routeTripTime)) + : (destTimeCell = FmsFormatters.minutesTohhmm(mcdu._routeTripTime)); + + if (alternate) { + if (mcdu.getRouteAltFuelTime()) { + altTimeCell = isFlying + ? FmsFormatters.secondsToUTC( + utcTime + + FmsFormatters.minuteToSeconds(mcdu._routeTripTime) + + FmsFormatters.minuteToSeconds(mcdu.getRouteAltFuelTime()), + ) + : FmsFormatters.minutesTohhmm(mcdu.getRouteAltFuelTime()); + altTimeCellColor = '[color]green'; + } else { + altTimeCell = '----'; + altTimeCellColor = '[color]white'; + } + } + + destTimeCellColor = '[color]green'; + + rteRsvWeightCell = '{sp}{sp}' + NXUnits.kgToUser(mcdu.getRouteReservedWeight()).toFixed(1); + if (!mcdu._rteReservedWeightEntered) { + rteRsvWeightCell = '{small}' + rteRsvWeightCell + '{end}'; + } + + if (mcdu._rteRsvPercentOOR) { + rteRsvPercentCell = '--.-'; + rteRSvCellColor = '[color]cyan'; + rteRsvPctColor = '{white}'; + } else { + rteRsvPercentCell = mcdu.getRouteReservedPercent().toFixed(1); + if (isFlying || (!mcdu._rteReservedPctEntered && mcdu.routeReservedEntered())) { + rteRsvPercentCell = '{small}' + rteRsvPercentCell + '{end}'; + } + rteRsvPctColor = isFlying ? '{green}' : '{cyan}'; + rteRSvCellColor = isFlying ? '[color]green' : '[color]cyan'; + } + + mcdu.onLeftInput[2] = async (value, scratchpadCallback) => { + if (await mcdu.trySetRouteReservedFuel(value)) { + CDUFuelPredPage.ShowPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + if (mcdu._minDestFobEntered) { + minDestFobCell = '{sp}{sp}' + NXUnits.kgToUser(mcdu._minDestFob).toFixed(1); + minDestFobCellColor = '[color]cyan'; + } else { + mcdu.tryUpdateMinDestFob(); + minDestFobCell = '{sp}{sp}{small}' + NXUnits.kgToUser(mcdu._minDestFob).toFixed(1) + '{end}'; + minDestFobCellColor = '[color]cyan'; + } + mcdu.onLeftInput[5] = async (value, scratchpadCallback) => { + if (await mcdu.trySetMinDestFob(value)) { + CDUFuelPredPage.ShowPage(mcdu); + } else { + scratchpadCallback(); + } + }; + mcdu.checkEFOBBelowMin(); + + extraFuelCell = '{small}' + NXUnits.kgToUser(mcdu.tryGetExtraFuel(true)).toFixed(1); + if (mcdu.tryGetExtraFuel(true) < 0) { + extraTimeCell = '----{end}'; + extraTimeColor = '{white}'; + } else { + extraTimeCell = FmsFormatters.minutesTohhmm(mcdu.tryGetExtraTime(true)) + '{end}'; + extraTimeColor = '{green}'; + } + extraCellColor = '[color]green'; + + // Currently not updating as there's no simvar to retrieve this. + if (isFinite(mcdu.zeroFuelWeight)) { + zfwCell = NXUnits.kgToUser(mcdu.zeroFuelWeight).toFixed(1); + zfwColor = '[color]cyan'; + } + if (isFinite(mcdu.zeroFuelWeightMassCenter)) { + zfwCgCell = mcdu.zeroFuelWeightMassCenter.toFixed(1); + } + + destEFOBCellColor = mcdu._isBelowMinDestFob ? '[color]amber' : '[color]green'; + } + } + + mcdu.setTemplate([ + ['FUEL PRED{sp}'], + ['\xa0AT', 'EFOB', isFlying ? '{sp}UTC' : 'TIME'], + [destIdentCell + '[color]green', destEFOBCell + destEFOBCellColor, destTimeCell + destTimeCellColor], + [''], + [altIdentCell + '[color]green', altEFOBCell + altEFOBCellColor, altTimeCell + altTimeCellColor], + ['RTE RSV/%', 'ZFW/ZFWCG'], + [ + rteRsvWeightCell + rteRsvPctColor + '/' + rteRsvPercentCell + '{end}' + rteRSvCellColor, + zfwCell + '/' + zfwCgCell + '{sp}' + zfwColor, + ], + ['ALTN\xa0/TIME', 'FOB{sp}{sp}{sp}{sp}{sp}{sp}'], + [ + altFuelCell + altTimeColor + '/' + altFuelTimeCell + '{end}' + altFuelColor, + fobCell + '/' + fobOtherCell + '{sp}{sp}{sp}' + fobCellColor, + ], + ['FINAL/TIME', 'GW/{sp}{sp} CG'], + [finalFuelCell + '/' + finalTimeCell + finalColor, gwCell + '/ ' + cgCell + gwCgCellColor], + ['MIN DEST FOB', 'EXTRA/TIME'], + [ + minDestFobCell + minDestFobCellColor, + extraFuelCell + extraTimeColor + '/' + extraTimeCell + '{end}' + extraCellColor, + ], + ]); + + mcdu.setArrows(false, false, true, true); + + mcdu.onPrevPage = () => { + CDUInitPage.ShowPage1(mcdu); + }; + mcdu.onNextPage = () => { + CDUInitPage.ShowPage1(mcdu); + }; + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.FuelPredPage) { + CDUFuelPredPage.ShowPage(mcdu); + } + }, mcdu.PageTimeout.Dyn); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_GPSMonitor.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_GPSMonitor.ts new file mode 100644 index 00000000000..3c4b169b00e --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_GPSMonitor.ts @@ -0,0 +1,66 @@ +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class CDUGPSMonitor { + static ShowPage(mcdu: LegacyFmsPageInterface, merit1?, merit2?, sat1?, sat2?) { + let currPos = new LatLong( + SimVar.GetSimVarValue('GPS POSITION LAT', 'degree latitude'), + SimVar.GetSimVarValue('GPS POSITION LON', 'degree longitude'), + ).toShortDegreeString(); + let currPosSplit: string[]; + let sep: string; + if (currPos.includes('N')) { + currPosSplit = currPos.split('N'); + sep = 'N/'; + } else { + currPosSplit = currPos.split('S'); + sep = 'S/'; + } + const latStr = currPosSplit[0]; + const lonStr = currPosSplit[1]; + currPos = latStr + sep + lonStr; + const TTRK = SimVar.GetSimVarValue('GPS GROUND MAGNETIC TRACK', 'radians'); + const GROUNDSPEED = SimVar.GetSimVarValue('GPS GROUND SPEED', 'Knots'); + // Should be corrected from WGS84 to EGM96... when MMR GPS receiver is implemented + const ALTITUDE = Math.min(131072, Math.max(-131072, SimVar.GetSimVarValue('GPS POSITION ALT', 'Feet'))); + + const UTC_SECONDS = Math.floor(SimVar.GetGlobalVarValue('ZULU TIME', 'seconds')); + const hours = Math.floor(UTC_SECONDS / 3600) || 0; + const minutes = Math.floor((UTC_SECONDS % 3600) / 60) || 0; + const seconds = Math.floor((UTC_SECONDS % 3600) % 60) || 0; + + const UTC = `${hours.toString().padStart(2, '0') || '00'}:${minutes.toString().padStart(2, '0') || '00'}:${seconds.toString().padStart(2, '0') || '00'}`; + + if (typeof merit1 == 'undefined') { + merit1 = Math.floor(Math.random() * 10) + 40; + merit2 = Math.floor(Math.random() * 10) + 40; + sat1 = Math.floor(Math.random() * 5) + 8; + sat2 = Math.floor(Math.random() * 5) + 8; + } + + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.GPSMonitor; + mcdu.setTemplate([ + ['GPS MONITOR'], + ['GPS1 POSITION'], + [`${currPos}[color]green`], + ['TTRK', 'GS', 'UTC'], + [`${Math.round(TTRK)}[color]green`, `${Math.round(GROUNDSPEED)}[color]green`, `${UTC}[color]green`], + ['MERIT', 'MODE/SAT', 'GPS ALT'], + [`${merit1}FT[color]green`, `NAV/${sat1}[color]green`, `${Math.round(ALTITUDE)}[color]green`], + ['GPS2 POSITION'], + [`${currPos}[color]green`], + ['TTRK', 'GS', 'UTC'], + [`${Math.round(TTRK)}[color]green`, `${Math.round(GROUNDSPEED)}[color]green`, `${UTC}[color]green`], + ['MERIT', 'MODE/SAT', 'GPS ALT'], + [`${merit2}FT[color]green`, `NAV/${sat2}[color]green`, `${Math.round(ALTITUDE)}[color]green`], + ]); + + // ideally, this would update with the same frequency (is it known?) as the A320 GPS + // updates fast as it shows seconds + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.GPSMonitor) { + CDUGPSMonitor.ShowPage(mcdu, merit1, merit2, sat1, sat2); + } + }, mcdu.PageTimeout.Fast); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_HoldAtPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_HoldAtPage.ts new file mode 100644 index 00000000000..e406ae117cd --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_HoldAtPage.ts @@ -0,0 +1,396 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { CDUFlightPlanPage } from './A320_Neo_CDU_FlightPlanPage'; +import { CDULateralRevisionPage } from './A320_Neo_CDU_LateralRevisionPage'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { WaypointArea } from '@flybywiresim/fbw-sdk'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { NavigationDatabaseService } from '@fmgc/flightplanning/NavigationDatabaseService'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; + +const TurnDirection = Object.freeze({ + Unknown: 'U', + Left: 'L', + Right: 'R', + Either: 'E', +}); + +const HoldType = Object.freeze({ + Computed: 0, + Database: 1, + Modified: 2, +}); + +export class CDUHoldAtPage { + static ShowPage(mcdu: LegacyFmsPageInterface, waypointIndexFP, forPlan, inAlternate) { + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + const waypoint = targetPlan.legElementAt(waypointIndexFP); + + if (!waypoint) { + return CDUFlightPlanPage.ShowPage(mcdu); + } + + const editingHm = waypoint.type === 'HM'; // HM + + if (editingHm) { + CDUHoldAtPage.DrawPage(mcdu, waypointIndexFP, waypointIndexFP, forPlan, inAlternate); + } else { + const editingHx = waypoint.isHX(); + const alt = waypoint.definition.altitude1 + ? waypoint.definition.altitude1 + : SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); + + let defaultHold; + let modifiedHold; + if (editingHx) { + defaultHold = waypoint.defaultHold; + modifiedHold = waypoint.modifiedHold; + } else { + const previousLeg = targetPlan.maybeElementAt(waypointIndexFP - 1); + + let inboundMagneticCourse = 100; + if (previousLeg && previousLeg.isDiscontinuity === false && previousLeg.isXF()) { + inboundMagneticCourse = Avionics.Utils.computeGreatCircleHeading( + previousLeg.terminationWaypoint().location, + waypoint.terminationWaypoint().location, + ); + } + + defaultHold = { + inboundMagneticCourse, + turnDirection: TurnDirection.Right, + time: alt <= 14000 ? 1 : 1.5, + type: HoldType.Computed, + }; + modifiedHold = {}; + + const fix = waypoint.terminationWaypoint(); + const promises = []; + // Due to the way MSFS stores holds, we have to try all these possibilities. + if (targetPlan.originAirport) { + promises.push(NavigationDatabaseService.activeDatabase.getHolds(fix.ident, targetPlan.originAirport.ident)); + } + if (targetPlan.destinationAirport) { + promises.push( + NavigationDatabaseService.activeDatabase.getHolds(fix.ident, targetPlan.destinationAirport.ident), + ); + } + if (fix && fix.area === WaypointArea.Terminal && 'airportIdent' in fix && fix.airportIdent.length > 0) { + promises.push(NavigationDatabaseService.activeDatabase.getHolds(fix.ident, fix.airportIdent)); + } + Promise.all(promises) + .then((resolvedPromises) => { + // Pick a hold based on altitude suitability and inbound course + // Missing in the navdata is the duplicate indicator that would help us pick the right area/airspace type. + const holds = resolvedPromises + .reduce((allHolds, holds) => { + allHolds.push(...holds); + return allHolds; + }) + .filter((v) => v.waypoint.databaseId === fix.databaseId) + .sort((a, b) => { + let ret = + CDUHoldAtPage.holdVerticalDistanceFromAlt(alt, a) - CDUHoldAtPage.holdVerticalDistanceFromAlt(alt, b); + if (ret === 0) { + ret = + Math.abs(a.magneticCourse - inboundMagneticCourse) - + Math.abs(b.magneticCourse - inboundMagneticCourse); + } + return ret; + }); + + if (holds[0]) { + defaultHold = { + inboundMagneticCourse: holds[0].magneticCourse, + turnDirection: holds[0].turnDirection, + time: holds[0].lengthTime ? holds[0].lengthTime : undefined, + distance: holds[0].length ? holds[0].length : undefined, + type: HoldType.Database, + }; + } + + CDUHoldAtPage.addOrEditManualHold( + mcdu, + waypointIndexFP, + // eslint-disable-next-line prefer-object-spread + Object.assign({}, defaultHold), + modifiedHold, + defaultHold, + forPlan, + inAlternate, + ); + }) + .catch(() => { + CDUHoldAtPage.addOrEditManualHold( + mcdu, + waypointIndexFP, + // eslint-disable-next-line prefer-object-spread + Object.assign({}, defaultHold), + modifiedHold, + defaultHold, + forPlan, + inAlternate, + ); + }); + return; + } + + CDUHoldAtPage.addOrEditManualHold( + mcdu, + waypointIndexFP, + // eslint-disable-next-line prefer-object-spread + Object.assign({}, defaultHold), + modifiedHold, + defaultHold, + forPlan, + inAlternate, + ); + } + } + + static holdVerticalDistanceFromAlt(altitude, hold) { + switch (hold.altitudeDescriptor) { + case 'B': + if (altitude <= hold.altitude1 && altitude >= hold.altitude2) { + return 0; + } + if (altitude > hold.altitude1) { + return altitude - hold.altitude1; + } + return hold.altitude2 - altitude; + case '+': + return Math.max(0, hold.altitude1 - altitude); + case '-': + return Math.max(0, altitude - hold.altitude1); + default: + // no restriction, so always suitable + return 0; + } + } + + static addOrEditManualHold( + mcdu: LegacyFmsPageInterface, + atIndex, + desiredHold, + modifiedHold, + defaultHold, + planIndex, + alternate, + ) { + mcdu.flightPlanService + .addOrEditManualHold(atIndex, desiredHold, modifiedHold, defaultHold, planIndex, alternate) + .then((holdIndex) => { + CDUHoldAtPage.DrawPage(mcdu, holdIndex, atIndex, planIndex, alternate); + }); + } + + static DrawPage(mcdu: LegacyFmsPageInterface, waypointIndexFP, originalFpIndex, forPlan, inAlternate) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.HoldAtPage; + + const tmpy = forPlan === FlightPlanIndex.Active && mcdu.flightPlanService.hasTemporary; + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + + const leg = targetPlan.legElementAt(waypointIndexFP); + + const speed = waypointIndexFP === mcdu.holdIndex && mcdu.holdSpeedTarget > 0 ? mcdu.holdSpeedTarget : 180; + + const modifiedHold = leg.modifiedHold; + const defaultHold = leg.defaultHold; + const currentHold = CDUHoldAtPage.computeDesiredHold(leg); + + // TODO this doesn't account for wind... we really need to access the actual hold leg once the ts/js barrier is broken + const displayTime = currentHold.time === undefined ? (currentHold.distance * 60) / speed : currentHold.time; + const displayDistance = currentHold.distance === undefined ? (speed * currentHold.time) / 60 : currentHold.distance; + + const defaultType = defaultHold.type === HoldType.Database ? 'DATABASE' : 'COMPUTED'; + const defaultTitle = `${defaultType}\xa0`; + const defaultRevert = `${defaultType}}`; + + const ident = leg.ident.replace('T-P', 'PPOS').padEnd(7, '\xa0'); + const rows = []; + rows.push([ + `${currentHold.type !== HoldType.Modified ? defaultTitle : ''}HOLD\xa0{small}AT{end}\xa0{green}${ident}{end}`, + ]); + rows.push(['INB CRS', '', '']); + rows.push([ + `{${tmpy ? 'yellow' : 'cyan'}}${modifiedHold && modifiedHold.inboundMagneticCourse !== undefined ? '{big}' : '{small}'}${currentHold.inboundMagneticCourse.toFixed(0).padStart(3, '0')}°{end}{end}`, + ]); + rows.push(['TURN', currentHold.type === HoldType.Modified ? 'REVERT TO' : '']); + rows.push([ + `{${tmpy ? 'yellow' : 'cyan'}}${modifiedHold && modifiedHold.turnDirection !== undefined ? '{big}' : '{small}'}${currentHold.turnDirection === TurnDirection.Left ? 'L' : 'R'}{end}`, + `{cyan}${currentHold.type === HoldType.Modified ? defaultRevert : ''}{end}`, + ]); + rows.push(['TIME/DIST']); + rows.push([ + `{${tmpy ? 'yellow' : 'cyan'}}${modifiedHold && modifiedHold.time !== undefined ? '{big}' : '{small}'}${displayTime.toFixed(1).padStart(4, '\xa0')}{end}/${modifiedHold && modifiedHold.distance !== undefined ? '{big}' : '{small}'}${displayDistance.toFixed(1)}{end}{end}`, + ]); + rows.push(['', '', '\xa0LAST EXIT']); + rows.push(['', '', '{small}UTC\xa0\xa0\xa0FUEL{end}']); + rows.push(['', '', '----\xa0\xa0----']); + rows.push(['']); + rows.push(['']); + rows.push([tmpy ? '{amber}{ERASE{end}' : '', tmpy ? '{amber}INSERT*{end}' : '', '']); + + mcdu.setTemplate([...rows]); + + // TODO what happens if CLR attemped? + // change course + mcdu.onLeftInput[0] = (value, scratchpadCallback) => { + if (value.match(/^[0-9]{1,3}$/) === null) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + const magCourse = parseInt(value); + if (magCourse > 360) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return; + } + + CDUHoldAtPage.modifyHold( + mcdu, + waypointIndexFP, + leg, + 'inboundMagneticCourse', + magCourse, + forPlan, + inAlternate, + ).then(() => { + CDUHoldAtPage.DrawPage(mcdu, waypointIndexFP, originalFpIndex, forPlan, inAlternate); + }); + }; + + // change turn direction + mcdu.onLeftInput[1] = async (value, scratchpadCallback) => { + if (value !== 'L' && value !== 'R') { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + await CDUHoldAtPage.modifyHold( + mcdu, + waypointIndexFP, + leg, + 'turnDirection', + value === 'L' ? TurnDirection.Left : TurnDirection.Right, + forPlan, + inAlternate, + ); + + CDUHoldAtPage.DrawPage(mcdu, waypointIndexFP, originalFpIndex, forPlan, inAlternate); + }; + + // change time or distance + mcdu.onLeftInput[2] = (value, scratchpadCallback) => { + const m = value.match(/^(([0-9]{0,1}(\.[0-9])?)\/?|\/([0-9]{0,2}(\.[0-9])?))$/); + if (m === null || m[0].length === 0 || m[0] === '/') { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + const time = m[2]; + const dist = m[4]; + + const param = dist ? 'distance' : 'time'; + const newValue = dist ? parseFloat(dist) : parseFloat(time); + + CDUHoldAtPage.modifyHold(mcdu, waypointIndexFP, leg, param, newValue, forPlan, inAlternate).then(() => { + CDUHoldAtPage.DrawPage(mcdu, waypointIndexFP, originalFpIndex, forPlan, inAlternate); + }); + }; + + // revert to computed/database + if (currentHold.type === HoldType.Modified) { + mcdu.onRightInput[1] = async () => { + mcdu.flightPlanService.revertHoldToComputed(waypointIndexFP); + + CDUHoldAtPage.DrawPage(mcdu, waypointIndexFP, originalFpIndex, forPlan, inAlternate); + }; + } + + // erase + mcdu.onLeftInput[5] = () => { + if (tmpy) { + mcdu.eraseTemporaryFlightPlan(() => { + CDULateralRevisionPage.ShowPage( + mcdu, + targetPlan.maybeElementAt(originalFpIndex), + originalFpIndex, + forPlan, + inAlternate, + ); + }); + } + }; + + // insert + mcdu.onRightInput[5] = () => { + if (tmpy) { + mcdu.insertTemporaryFlightPlan(() => { + CDUFlightPlanPage.ShowPage(mcdu, waypointIndexFP, forPlan); + }); + } + }; + } + + static computeDesiredHold(/** @type {FlightPlanLeg} */ leg) { + const modifiedHold = leg.modifiedHold; + const defaultHold = leg.defaultHold; + + const pilotTimeOrDistance = + modifiedHold && (modifiedHold.time !== undefined || modifiedHold.distance !== undefined); + + return { + inboundMagneticCourse: + modifiedHold && modifiedHold.inboundMagneticCourse !== undefined + ? modifiedHold.inboundMagneticCourse + : defaultHold.inboundMagneticCourse, + turnDirection: + modifiedHold && modifiedHold.turnDirection !== undefined + ? modifiedHold.turnDirection + : defaultHold.turnDirection, + distance: pilotTimeOrDistance ? modifiedHold.distance : defaultHold.distance, + time: pilotTimeOrDistance ? modifiedHold.time : defaultHold.time, + type: modifiedHold !== undefined ? modifiedHold.type : defaultHold.type, + }; + } + + static async modifyHold( + mcdu: LegacyFmsPageInterface, + waypointIndexFP, + /** @type {FlightPlanLeg} */ waypointData, + param, + value, + forPlan, + inAlternate, + ) { + if (waypointData.modifiedHold === undefined) { + waypointData.modifiedHold = {}; + } + + waypointData.modifiedHold.type = HoldType.Modified; + + if (param === 'time') { + waypointData.modifiedHold.distance = undefined; + } else if (param === 'distance') { + waypointData.modifiedHold.time = undefined; + } + + waypointData.modifiedHold[param] = value; + + await mcdu.flightPlanService.addOrEditManualHold( + waypointIndexFP, + CDUHoldAtPage.computeDesiredHold(waypointData), + waypointData.modifiedHold, + waypointData.defaultHold, + forPlan, + inAlternate, + ); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSInit.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSInit.ts new file mode 100644 index 00000000000..56165baf71a --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSInit.ts @@ -0,0 +1,316 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { CDUInitPage } from './A320_Neo_CDU_InitPage'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class CDUIRSInit { + static ShowPage( + mcdu: LegacyFmsPageInterface, + lon?, + originAirportLat?, + originAirportLon?, + referenceName?, + originAirportCoordinates?, + alignMsg = 'ALIGN ON REF}[color]cyan', + ) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.IRSInit; + mcdu.setTitle('IRS INIT'); + + const adiru1State = SimVar.GetSimVarValue('L:A32NX_ADIRS_ADIRU_1_STATE', 'Enum'); + const adiru2State = SimVar.GetSimVarValue('L:A32NX_ADIRS_ADIRU_2_STATE', 'Enum'); + const adiru3State = SimVar.GetSimVarValue('L:A32NX_ADIRS_ADIRU_3_STATE', 'Enum'); + const areAllAligned = adiru1State === 2 && adiru2State === 2 && adiru3State === 2; + + if (adiru1State === 0 || adiru2State === 0 || adiru3State === 0) { + SimVar.SetSimVarValue('L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF', 'Enum', 0); + alignMsg = 'ALIGN ON REF}[color]cyan'; + } + const emptyIRSGpsString = '--°--.--/---°--.--'; + const arrowupdwn = '↑↓'; + let larrowupdwn = arrowupdwn; + let rarrowupdwn = ''; + if (lon) { + rarrowupdwn = arrowupdwn; + larrowupdwn = ''; + } + let statusIRS1; + let statusIRS2; + let statusIRS3; + let alignType = '---'; + // Ref coordinates are taken based on origin airport + const activeOriginAirport = mcdu.flightPlanService.active.originAirport; + + if (!originAirportLat && !originAirportLon) { + const airportCoordinates = activeOriginAirport.location; + originAirportLat = CDUInitPage.ConvertDDToDMS(airportCoordinates['lat'], false); + originAirportLon = CDUInitPage.ConvertDDToDMS(airportCoordinates['long'], true); + originAirportLat['sec'] = Math.ceil(Number(originAirportLat['sec'] / 100)); + originAirportLon['sec'] = Math.ceil(Number(originAirportLon['sec'] / 100)); + // Must be string for consistency since leading 0's are not allowed in Number + originAirportLat['min'] = originAirportLat['min'].toString(); + originAirportLon['min'] = originAirportLon['min'].toString(); + referenceName = activeOriginAirport.ident + ' [color]green'; + originAirportCoordinates = JSON.stringify(originAirportLat) + JSON.stringify(originAirportLon); + } + if (originAirportCoordinates === JSON.stringify(originAirportLat) + JSON.stringify(originAirportLon)) { + referenceName = activeOriginAirport.ident + ' [color]green'; + } + const currentGPSLat = CDUInitPage.ConvertDDToDMS( + SimVar.GetSimVarValue('GPS POSITION LAT', 'degree latitude'), + false, + ); + const currentGPSLon = CDUInitPage.ConvertDDToDMS( + SimVar.GetSimVarValue('GPS POSITION LON', 'degree longitude'), + true, + ); + let GPSPosAlign; + let originAirportTitle; + let GPSPosTitle; + let originAirportString; + + if (!areAllAligned) { + GPSPosTitle = ['LAT', 'LONG', 'GPS POSITION']; + originAirportTitle = ['LAT' + larrowupdwn, rarrowupdwn + 'LONG', 'REFERENCE']; + GPSPosAlign = ['--°--.--', '--°--.--', ' ']; + originAirportString = [ + originAirportLat['deg'] + + '°{small}' + + originAirportLat['min'] + + '.' + + originAirportLat['sec'] + + '{end}' + + originAirportLat['dir'] + + '[color]cyan', + originAirportLon['deg'] + + '°{small}' + + originAirportLon['min'] + + '.' + + originAirportLon['sec'] + + '{end}' + + originAirportLon['dir'] + + '[color]cyan', + referenceName, + ]; + if ( + SimVar.GetSimVarValue('L:A32NX_OVHD_ADIRS_IR_1_MODE_SELECTOR_KNOB', 'Enum') || + SimVar.GetSimVarValue('L:A32NX_OVHD_ADIRS_IR_2_MODE_SELECTOR_KNOB', 'Enum') || + SimVar.GetSimVarValue('L:A32NX_OVHD_ADIRS_IR_3_MODE_SELECTOR_KNOB', 'Enum') + ) { + GPSPosAlign = [ + currentGPSLat['deg'] + + '°{small}' + + currentGPSLat['min'] + + '.' + + Math.ceil(Number(currentGPSLat['sec'] / 100)) + + '{end}' + + currentGPSLat['dir'] + + '[color]green', + currentGPSLon['deg'] + + '°{small}' + + currentGPSLon['min'] + + '.' + + Math.ceil(Number(currentGPSLon['sec'] / 100)) + + '{end}' + + currentGPSLon['dir'] + + '[color]green', + '', + ]; + alignType = 'GPS'; + } + } + + let IRSAlignOnPos = + currentGPSLat['deg'] + + '°{small}' + + currentGPSLat['min'] + + '.' + + Math.ceil(Number(currentGPSLat['sec'] / 100)) + + '{end}' + + currentGPSLat['dir'] + + '/' + + currentGPSLon['deg'] + + '°{small}' + + currentGPSLon['min'] + + '.' + + Math.ceil(Number(currentGPSLon['sec'] / 100)) + + '{end}' + + currentGPSLon['dir']; + if (SimVar.GetSimVarValue('L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF', 'Enum') === 1) { + alignMsg = ''; + alignType = 'REF'; + if (!areAllAligned) { + IRSAlignOnPos = + originAirportLat['deg'] + + '°{small}' + + originAirportLat['min'] + + '.' + + originAirportLat['sec'] + + '{end}' + + originAirportLat['dir'] + + '/' + + originAirportLon['deg'] + + '°{small}' + + originAirportLon['min'] + + '.' + + originAirportLon['sec'] + + '{end}' + + originAirportLon['dir']; + } + } + + if (areAllAligned) { + alignMsg = ''; + if (SimVar.GetSimVarValue('L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF', 'Enum') === 0) { + alignType = 'GPS'; + } + } + + let IRS1GpsString = emptyIRSGpsString; + if (SimVar.GetSimVarValue('L:A32NX_OVHD_ADIRS_IR_1_MODE_SELECTOR_KNOB', 'Enum') === 1) { + if (adiru1State === 2) { + statusIRS1 = 'IRS1 ALIGNED ON ' + alignType; + } else { + statusIRS1 = 'IRS1 ALIGNING ON ' + alignType; + } + IRS1GpsString = IRSAlignOnPos + '[color]green'; + } else { + statusIRS1 = 'IRS1 OFF'; + } + let IRS2GpsString = emptyIRSGpsString; + if (SimVar.GetSimVarValue('L:A32NX_OVHD_ADIRS_IR_2_MODE_SELECTOR_KNOB', 'Enum') === 1) { + if (adiru2State === 2) { + statusIRS2 = 'IRS2 ALIGNED ON ' + alignType; + } else { + statusIRS2 = 'IRS2 ALIGNING ON ' + alignType; + } + IRS2GpsString = IRSAlignOnPos + '[color]green'; + } else { + statusIRS2 = 'IRS2 OFF'; + } + let IRS3GpsString = emptyIRSGpsString; + if (SimVar.GetSimVarValue('L:A32NX_OVHD_ADIRS_IR_3_MODE_SELECTOR_KNOB', 'Enum') === 1) { + if (adiru3State === 2) { + statusIRS3 = 'IRS3 ALIGNED ON ' + alignType; + } else { + statusIRS3 = 'IRS3 ALIGNING ON ' + alignType; + } + IRS3GpsString = IRSAlignOnPos + '[color]green'; + } else { + statusIRS3 = 'IRS3 OFF'; + } + + mcdu.setTemplate([ + ['IRS INIT'], + originAirportTitle, + originAirportString, + GPSPosTitle, + GPSPosAlign, + ['', '', statusIRS1], + ['', '', IRS1GpsString], + ['', '', statusIRS2], + ['', '', IRS2GpsString], + ['', '', statusIRS3], + ['', '', IRS3GpsString], + [], + [' { + lon = false; + }; + + mcdu.onRightInput[0] = () => { + lon = true; + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[5] = () => { + CDUInitPage.ShowPage1(mcdu); + }; + + mcdu.onRightInput[5] = () => { + if (!areAllAligned) { + if (alignMsg.includes('CONFIRM')) { + SimVar.SetSimVarValue('L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF', 'Enum', 1); + } else { + alignMsg = 'CONFIRM ALIGN* [color]amber'; + } + } + }; + + mcdu.onUp = () => { + if (!areAllAligned && SimVar.GetSimVarValue('L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF', 'Enum') !== 1) { + referenceName = '----'; + let activeReference = originAirportLat; + if (lon) { + activeReference = originAirportLon; + } + if ((activeReference['deg'] >= 90 && !lon) || (activeReference['deg'] >= 180 && lon)) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + } else { + activeReference['sec'] = activeReference['sec'] + 1; + if (activeReference['sec'] >= 9) { + activeReference['min'] = (Number(activeReference['min']) + 1).toString().padStart(2, '0'); + activeReference['sec'] = 0; + } + if (activeReference['min'] >= 60) { + activeReference['min'] = (0).toString().padStart(2, '0'); + activeReference['deg'] = !lon + ? activeReference['deg'] + 1 + : (Number(activeReference['deg']) + 1).toString().padStart(3, '0'); + } + } + } + }; + + mcdu.onDown = () => { + if (!areAllAligned && SimVar.GetSimVarValue('L:A32XN_Neo_ADIRS_ALIGN_TYPE_REF', 'Enum') !== 1) { + referenceName = '----'; + let activeReference = originAirportLat; + if (lon) { + activeReference = originAirportLon; + } + if ((activeReference['deg'] <= -90 && !lon) || (activeReference['deg'] <= -180 && lon)) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + } else { + activeReference['sec'] = activeReference['sec'] - 1; + if (activeReference['sec'] < 0) { + activeReference['min'] = (Number(activeReference['min']) - 1).toString().padStart(2, '0'); + activeReference['sec'] = 9; + } + if (activeReference['min'] < 0) { + activeReference['min'] = 59; + activeReference['deg'] = !lon + ? activeReference['deg'] - 1 + : (Number(activeReference['deg']) - 1).toString().padStart(3, '0'); + } + } + } + }; + + // This page auto-refreshes based on source material. Function will stop auto-refreshing when page has been changed. + autoRefresh(); + function autoRefresh() { + setTimeout(() => { + if (mcdu.page.Current === mcdu.page.IRSInit) { + CDUIRSInit.ShowPage( + mcdu, + lon, + originAirportLat, + originAirportLon, + referenceName, + originAirportCoordinates, + alignMsg, + ); + } + }, 1000); + } + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSMonitor.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSMonitor.ts new file mode 100644 index 00000000000..e5f850ab021 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSMonitor.ts @@ -0,0 +1,49 @@ +import { CDUIRSStatus } from './A320_Neo_CDU_IRSStatus'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class CDUIRSMonitor { + static ShowPage(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.IRSMonitor; + mcdu.setTemplate([ + ['IRS MONITOR'], + [''], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = () => { + CDUIRSStatus.ShowPage(mcdu, 1); + }; + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = () => { + CDUIRSStatus.ShowPage(mcdu, 2); + }; + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = () => { + CDUIRSStatus.ShowPage(mcdu, 3); + }; + } + + static getAdiruStateMessage(number) { + const state = SimVar.GetSimVarValue(`L:A32NX_ADIRS_ADIRU_${number}_STATE`, 'Enum'); + switch (state) { + case 1: + return 'ALIGN'; + case 2: + return 'NAV'; + default: + return ''; + } + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSStatus.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSStatus.ts new file mode 100644 index 00000000000..13ae17c9206 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSStatus.ts @@ -0,0 +1,86 @@ +import { CDUIRSMonitor } from './A320_Neo_CDU_IRSMonitor'; +import { CDUIRSStatusFrozen } from './A320_Neo_CDU_IRSStatusFrozen'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class CDUIRSStatus { + static ShowPage(mcdu: LegacyFmsPageInterface, index, prev_wind_dir?) { + let currPos = new LatLong( + SimVar.GetSimVarValue('GPS POSITION LAT', 'degree latitude'), + SimVar.GetSimVarValue('GPS POSITION LON', 'degree longitude'), + ).toShortDegreeString(); + let currPosSplit: string[]; + let sep: string; + if (currPos.includes('N')) { + currPosSplit = currPos.split('N'); + sep = 'N/'; + } else { + currPosSplit = currPos.split('S'); + sep = 'S/'; + } + const latStr = currPosSplit[0]; + const lonStr = currPosSplit[1]; + currPos = latStr + sep + lonStr; + const GROUNDSPEED = SimVar.GetSimVarValue('GPS GROUND SPEED', 'Knots') || '0'; + const THDG = SimVar.GetSimVarValue('GPS GROUND TRUE HEADING', 'radians') || '000'; + const TTRK = SimVar.GetSimVarValue('GPS GROUND MAGNETIC TRACK', 'radians') || '000'; + const MHDG = SimVar.GetSimVarValue('GPS GROUND TRUE TRACK', 'radians') || '000'; + const WIND_VELOCITY = SimVar.GetSimVarValue('AMBIENT WIND VELOCITY', 'Knots') || '00'; + // wind direction smoothing like A32NX_NDInfo.js:setWind() + let wind_dir = Math.round(Simplane.getWindDirection()); + if (typeof prev_wind_dir == 'undefined') { + prev_wind_dir = wind_dir; + } + let startAngle = prev_wind_dir; + let endAngle = wind_dir; + const delta = endAngle - startAngle; + if (delta > 180) { + startAngle += 360; + } else if (delta < -180) { + endAngle += 360; + } + // FIXME filtering inside the page doesn't make a lot of sense! + const smoothedAngle = Utils.SmoothSin(startAngle, endAngle, 0.25, mcdu._deltaTime / 1000); + wind_dir = smoothedAngle % 360; + + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.IRSStatus; + mcdu.setTemplate([ + [`IRS${index}`], + ['POSITION'], + [`${currPos}[color]green`], + ['TTRK', 'GS'], + [`${Math.round(TTRK)}[color]green`, `${Math.round(GROUNDSPEED)}[color]green`], + [`THDG`, 'MHDG'], + [`${Math.round(THDG)}[color]green`, `${Math.round(MHDG)}[color]green`], + ['WIND', 'GPIRS ACCUR'], + [`${Math.round(wind_dir)}°/${Math.round(WIND_VELOCITY)}[color]green`, `200FT[color]green`], + ['GPIRS POSITION'], + [`${currPos}[color]green`], + ['', ''], + ['{FREEZE[color]cyan', `${index < 3 ? 'NEXT IRS>' : 'RETURN>'}`], + ]); + + mcdu.onLeftInput[5] = () => { + CDUIRSStatusFrozen.ShowPage(mcdu, index, wind_dir); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onRightInput[5] = () => { + if (index > 2) { + CDUIRSMonitor.ShowPage(mcdu); + } else { + this.ShowPage(mcdu, index + 1); + } + }; + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.IRSStatus) { + CDUIRSStatus.ShowPage(mcdu, index, wind_dir); + } + }, mcdu.PageTimeout.Default); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSStatusFrozen.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSStatusFrozen.ts new file mode 100644 index 00000000000..6b7cac1d7f2 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IRSStatusFrozen.ts @@ -0,0 +1,63 @@ +import { CDUIRSMonitor } from './A320_Neo_CDU_IRSMonitor'; +import { CDUIRSStatus } from './A320_Neo_CDU_IRSStatus'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class CDUIRSStatusFrozen { + static ShowPage(mcdu: LegacyFmsPageInterface, index, wind_dir) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.IRSStatusFrozen; + let currPos = new LatLong( + SimVar.GetSimVarValue('GPS POSITION LAT', 'degree latitude'), + SimVar.GetSimVarValue('GPS POSITION LON', 'degree longitude'), + ).toShortDegreeString(); + let currPosSplit: string[]; + let sep: string; + if (currPos.includes('N')) { + currPosSplit = currPos.split('N'); + sep = 'N/'; + } else { + currPosSplit = currPos.split('S'); + sep = 'S/'; + } + const latStr = currPosSplit[0]; + const lonStr = currPosSplit[1]; + currPos = latStr + sep + lonStr; + const GROUNDSPEED = SimVar.GetSimVarValue('GPS GROUND SPEED', 'Meters per second') || '0'; + const THDG = SimVar.GetSimVarValue('GPS GROUND TRUE HEADING', 'radians') || '000'; + const TTRK = SimVar.GetSimVarValue('GPS GROUND MAGNETIC TRACK', 'radians') || '000'; + const MHDG = SimVar.GetSimVarValue('GPS GROUND TRUE TRACK', 'radians') || '000'; + const WIND_VELOCITY = SimVar.GetSimVarValue('AMBIENT WIND VELOCITY', 'Knots') || '00'; + const UTC_SECONDS = Math.floor(SimVar.GetGlobalVarValue('ZULU TIME', 'seconds')); + const hours = Math.floor(UTC_SECONDS / 3600) || 0; + const minutes = Math.floor((UTC_SECONDS % 3600) / 60) || 0; + const hhmm = `${hours.toString().padStart(2, '0') || '00'}${minutes.toString().padStart(2, '0') || '00'}`; + + mcdu.setTemplate([ + [`IRS${index} FROZEN AT ${hhmm}`], + ['POSITION'], + [`${currPos}[color]green`], + ['TTRK', 'GS'], + [`${Math.round(TTRK)}[color]green`, `${Math.round(GROUNDSPEED)}[color]green`], + [`THDG`, 'MHDG'], + [`${Math.round(THDG)}[color]green`, `${Math.round(MHDG)}[color]green`], + ['WIND', 'GPIRS ACCUR'], + [`${Math.round(wind_dir)}°/${Math.round(WIND_VELOCITY)}[color]green`, `200FT[color]green`], + ['GPIRS POSITION'], + [`${currPos}[color]green`], + ['', ''], + ['{UNFREEZE[color]cyan', `${index < 3 ? 'NEXT IRS>' : 'RETURN>'}`], + ]); + + mcdu.onLeftInput[5] = () => { + CDUIRSStatus.ShowPage(mcdu, index); + }; + + mcdu.onRightInput[5] = () => { + if (index > 2) { + CDUIRSMonitor.ShowPage(mcdu); + } else { + CDUIRSStatus.ShowPage(mcdu, index + 1); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IdentPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IdentPage.ts new file mode 100644 index 00000000000..ee081c1ad4b --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_IdentPage.ts @@ -0,0 +1,150 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +const DB_MONTHS = Object.freeze({ + '01': 'JAN', + '02': 'FEB', + '03': 'MAR', + '04': 'APR', + '05': 'MAY', + '06': 'JUN', + '07': 'JUL', + '08': 'AUG', + '09': 'SEP', + '10': 'OCT', + '11': 'NOV', + '12': 'DEC', +}); + +function calculateActiveDate(dbIdent) { + const effDay = dbIdent.effectiveFrom.substring(8); + const effMonth = dbIdent.effectiveFrom.substring(5, 7); + const expDay = dbIdent.effectiveTo.substring(8); + const expMonth = dbIdent.effectiveTo.substring(5, 7); + + return `${effDay}${DB_MONTHS[effMonth]}-${expDay}${DB_MONTHS[expMonth]}`; +} + +function calculateSecondDate(dbIdent) { + const [effYear, effMonth, effDay] = dbIdent.effectiveFrom.split('-'); + const [expYear, expMonth, expDay] = dbIdent.effectiveTo.split('-'); + + return `${effDay}${DB_MONTHS[effMonth]}${effYear}-${expDay}${DB_MONTHS[expMonth]}${expYear}`; +} + +async function switchDataBase(mcdu: LegacyFmsPageInterface) { + await mcdu.switchNavDatabase(); +} + +const ConfirmType = { + NoConfirm: 0, + DeleteStored: 1, + SwitchDataBase: 2, +}; + +export class CDUIdentPage { + static ShowPage(mcdu: LegacyFmsPageInterface, confirmType = ConfirmType.NoConfirm) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.IdentPage; + mcdu.activeSystem = 'FMGC'; + + const stored = mcdu.dataManager.numberOfStoredElements(); + + let storedTitleCell = ''; + let storedRoutesRunwaysCell = ''; + let storedWaypointsNavaidsCell = ''; + let storedDeleteCell = ''; + let secondaryDBSubLine = ''; + let secondaryDBTopLine = ''; + if (stored.routes + stored.runways + stored.waypoints + stored.navaids > 0) { + storedTitleCell = 'STORED\xa0\xa0\xa0\xa0'; + storedRoutesRunwaysCell = `{green}${stored.routes + .toFixed(0) + .padStart(2, '0')}{end}{small}RTES{end}\xa0{green}${stored.runways + .toFixed(0) + .padStart(2, '0')}{end}{small}RWYS{end}`; + storedWaypointsNavaidsCell = `{green}{big}${stored.waypoints + .toFixed(0) + .padStart(2, '0')}{end}{end}{small}WPTS{end}\xa0{green}{big}${stored.navaids + .toFixed(0) + .padStart(2, '0')}{end}{end}{small}NAVS{end}`; + storedDeleteCell = + confirmType === ConfirmType.DeleteStored ? '{amber}CONFIRM DEL*{end}' : '{cyan}DELETE ALL}{end}'; + + // DELETE ALL + mcdu.onRightInput[4] = () => { + if (confirmType == ConfirmType.DeleteStored) { + mcdu.dataManager.deleteAllStoredWaypoints().then((allDeleted) => { + if (!allDeleted) { + mcdu.setScratchpadMessage(NXSystemMessages.fplnElementRetained); + } + + CDUIdentPage.ShowPage(mcdu); + }); + } else { + CDUIdentPage.ShowPage(mcdu, ConfirmType.DeleteStored); + } + }; + } + + const dbCycle = mcdu.getNavDatabaseIdent(); + const activeCycleDates = dbCycle === null ? '' : calculateActiveDate(dbCycle); + const secondCycleDates = dbCycle === null ? '' : calculateSecondDate(dbCycle); + const navSerial = + dbCycle === null ? '' : `${dbCycle.provider.substring(0, 2).toUpperCase()}${dbCycle.airacCycle}0001`; + + // H4+ only confirm prompt + year on second dates + secondaryDBTopLine = + confirmType === ConfirmType.SwitchDataBase + ? `{amber}{small}\xa0${secondCycleDates}{end}` + : '\xa0SECOND\xa0NAV\xa0DATA\xa0BASE'; + secondaryDBSubLine = + confirmType === ConfirmType.SwitchDataBase + ? '{amber}{CANCEL\xa0\xa0\xa0SWAP\xa0\xa0CONFIRM*{end}' + : `{small}{cyan}{${secondCycleDates}{end}{end}`; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[2] = () => { + if (confirmType === ConfirmType.SwitchDataBase) { + CDUIdentPage.ShowPage(mcdu); + } else { + CDUIdentPage.ShowPage(mcdu, ConfirmType.SwitchDataBase); + } + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onRightInput[2] = () => { + if (confirmType === ConfirmType.SwitchDataBase) { + switchDataBase(mcdu).then(() => { + CDUIdentPage.ShowPage(mcdu); + }); + } + }; + + mcdu.setTemplate([ + ['A320-200\xa0\xa0\xa0\xa0'], //This aircraft code is correct and does not include the engine type. + ['\xa0ENG'], + ['LEAP-1A26[color]green'], + ['\xa0ACTIVE NAV DATA BASE'], + [`{cyan}\xa0${activeCycleDates}{end}`, `{green}${navSerial}{end}`], + [secondaryDBTopLine], + [secondaryDBSubLine], + ['', storedTitleCell], + ['', storedRoutesRunwaysCell], + ['CHG CODE', storedWaypointsNavaidsCell], + ['[\xa0][color]inop', storedDeleteCell], + ['IDLE/PERF', 'SOFTWARE\xa0\xa0'], + ['{small}{green}+0.0/+0.0{end}{end}', 'STATUS/XLOAD>[color]inop'], + ]); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_InitPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_InitPage.ts new file mode 100644 index 00000000000..2849acb3657 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_InitPage.ts @@ -0,0 +1,788 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { getSimBriefOfp } from '../legacy/A32NX_Core/A32NX_ATSU'; +import { Column, FormatTemplate } from '../legacy/A320_Neo_CDU_Format'; +import { CDU_SingleValueField } from '../legacy/A320_Neo_CDU_Field'; +import { McduMessage, NXFictionalMessages, NXSystemMessages } from '../messages/NXSystemMessages'; +import { CDUAvailableFlightPlanPage } from './A320_Neo_CDU_AvailableFlightPlanPage'; +import { CDUIRSInit } from './A320_Neo_CDU_IRSInit'; +import { CDUWindPage } from './A320_Neo_CDU_WindPage'; +import { NXUnits } from '@flybywiresim/fbw-sdk'; +import { getZfw, getZfwcg } from '../legacy/A32NX_Core/A32NX_PayloadManager'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FuelPlanningPhases } from '../legacy/A32NX_Core/A32NX_FuelPred'; +import { FmsFormatters } from '../legacy/FmsFormatters'; +import { SimBriefUplinkAdapter } from '@fmgc/flightplanning/uplink/SimBriefUplinkAdapter'; + +export class CDUInitPage { + static ShowPage1(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.InitPageA; + mcdu.pageRedrawCallback = () => CDUInitPage.ShowPage1(mcdu); + mcdu.activeSystem = 'FMGC'; + mcdu.coRoute.routes = []; + + const haveFlightPlan = + mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport; + + const fromTo = new Column(23, '____|____', Column.amber, Column.right); + const [coRouteAction, coRouteText, coRouteColor] = new CDU_SingleValueField( + mcdu, + 'string', + mcdu.coRoute.routeNumber, + { + emptyValue: haveFlightPlan ? '' : '__________[color]amber', + suffix: '[color]cyan', + maxLength: 10, + }, + async (value) => { + await mcdu.updateCoRoute(value, (result) => { + if (result) { + CDUInitPage.ShowPage1(mcdu); + } + }); + }, + ).getFieldAsColumnParameters(); + + const [flightNoAction, flightNoText, flightNoColor] = new CDU_SingleValueField( + mcdu, + 'string', + mcdu.flightNumber, + { + emptyValue: '________[color]amber', + suffix: '[color]cyan', + maxLength: 7, + }, + (value: string) => { + mcdu.updateFlightNo(value, (result) => { + if (result) { + CDUInitPage.ShowPage1(mcdu); + } else { + mcdu.setScratchpadUserData(value); + } + }); + }, + ).getFieldAsColumnParameters(); + + const altnAirport = mcdu.flightPlanService.active.alternateDestinationAirport; + const altDest = new Column(0, `${altnAirport ? altnAirport.ident : '----'}|----------`); + let costIndexText = '---'; + let costIndexAction; + let costIndexColor = Column.white; + + const cruiseFl = new Column(0, '-----'); + const cruiseTemp = new Column(10, '---°', Column.right); + const cruiseFlTempSeparator = new Column(6, '/'); + + let alignOption; + const tropo = new Column(23, '36090', Column.small, Column.cyan, Column.right); + let requestButton = 'REQUEST*'; + let requestButtonLabel = 'INIT'; + let requestEnable = true; + + if (mcdu.simbrief.sendStatus === 'REQUESTING') { + requestEnable = false; + requestButton = 'REQUEST '; + } + + const origin = mcdu.flightPlanService.active.originAirport; + const dest = mcdu.flightPlanService.active.destinationAirport; + + if (origin) { + if (dest) { + fromTo.update(origin.ident + '/' + dest.ident, Column.cyan); + + // If an active SimBrief OFP matches the FP, hide the request option + // This allows loading a new OFP via INIT/REVIEW loading a different orig/dest to the current one + if ( + mcdu.simbrief.sendStatus != 'DONE' || + (mcdu.simbrief['originIcao'] === origin.ident && mcdu.simbrief['destinationIcao'] === dest.ident) + ) { + requestEnable = false; + requestButtonLabel = ''; + requestButton = ''; + } + + // Cost index + [costIndexAction, costIndexText, costIndexColor] = new CDU_SingleValueField( + mcdu, + 'int', + mcdu.isCostIndexSet ? mcdu.costIndex : null, + { + clearable: true, + emptyValue: '___[color]amber', + minValue: 0, + maxValue: 999, + suffix: '[color]cyan', + }, + (value) => { + if (typeof value === 'number') { + mcdu.costIndex = value; + // mcdu.isCostIndexSet = true; + } else { + // mcdu.isCostIndexSet = false; + mcdu.costIndex = undefined; + } + CDUInitPage.ShowPage1(mcdu); + }, + ).getFieldAsColumnParameters(); + + mcdu.onLeftInput[4] = costIndexAction; + + cruiseFl.update('_____', Column.amber); + cruiseTemp.update('|___°', Column.amber); + cruiseFlTempSeparator.updateAttributes(Column.amber); + + //This is done so pilot enters a FL first, rather than using the computed one + if (mcdu.cruiseLevel) { + cruiseFl.update('FL' + mcdu.cruiseLevel.toFixed(0).padStart(3, '0'), Column.cyan); + if (mcdu.cruiseTemperature) { + cruiseTemp.update(mcdu.cruiseTemperature.toFixed(0) + '°', Column.cyan); + cruiseFlTempSeparator.updateAttributes(Column.cyan); + } else { + cruiseTemp.update(mcdu.tempCurve.evaluate(mcdu.cruiseLevel).toFixed(0) + '°', Column.cyan, Column.small); + cruiseFlTempSeparator.updateAttributes(Column.cyan, Column.small); + } + } + + // CRZ FL / FLX TEMP + mcdu.onLeftInput[5] = (value, scratchpadCallback) => { + if (mcdu.setCruiseFlightLevelAndTemperature(value)) { + CDUInitPage.ShowPage1(mcdu); + } else { + scratchpadCallback(); + } + }; + + if (mcdu.flightPlanService.active.originAirport) { + alignOption = 'IRS INIT>'; + } + + altDest.update(altnAirport ? altnAirport.ident : 'NONE', Column.cyan); + + mcdu.onLeftInput[1] = async (value, scratchpadCallback) => { + try { + if (value === '') { + await mcdu.getCoRouteList(); + CDUAvailableFlightPlanPage.ShowPage(mcdu); + } else { + if (await mcdu.tryUpdateAltDestination(value)) { + CDUInitPage.ShowPage1(mcdu); + } else { + scratchpadCallback(); + } + } + } catch (error) { + console.error(error); + mcdu.logTroubleshootingError(error); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + } + }; + } + } + + mcdu.onLeftInput[0] = coRouteAction; + + if (mcdu.tropo) { + tropo.update(mcdu.tropo.toString(), mcdu.isTropoPilotEntered ? Column.big : Column.small); + } + mcdu.onRightInput[4] = (value, scratchpadCallback) => { + if (mcdu.tryUpdateTropo(value)) { + CDUInitPage.ShowPage1(mcdu); + } else { + scratchpadCallback(); + } + }; + + /** + * If scratchpad is filled, attempt to update city pair + * else show route selection pair if city pair is displayed + * Ref: FCOM 4.03.20 P6 + */ + mcdu.onRightInput[0] = (value, scratchpadCallback) => { + if (value !== '') { + mcdu.tryUpdateFromTo(value, (result) => { + if (result) { + CDUAvailableFlightPlanPage.ShowPage(mcdu); + } else { + scratchpadCallback(); + } + }); + } else if (mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport) { + mcdu.getCoRouteList().then(() => { + CDUAvailableFlightPlanPage.ShowPage(mcdu); + }); + } + }; + mcdu.onRightInput[1] = () => { + if (requestEnable) { + getSimBriefOfp(mcdu, () => { + if (mcdu.page.Current === mcdu.page.InitPageA) { + CDUInitPage.ShowPage1(mcdu); + } + }) + .then((data) => { + SimBriefUplinkAdapter.uplinkFlightPlanFromSimbrief(mcdu, mcdu.flightPlanService, data, { + doUplinkProcedures: false, + }) + .then(() => { + console.log('SimBrief data uplinked.'); + + mcdu.flightPlanService.uplinkInsert(); + + const plan = mcdu.flightPlanService.active; + mcdu.updateFlightNo(plan.flightNumber); + mcdu.setGroundTempFromOrigin(); + + if (mcdu.page.Current === mcdu.page.InitPageA) { + CDUInitPage.ShowPage1(mcdu); + } + }) + .catch((error) => { + console.error(error); + mcdu.logTroubleshootingError(error); + mcdu.setScratchpadMessage(NXSystemMessages.invalidFplnUplink); + }); + }) + .catch((error) => { + console.error(error); + mcdu.logTroubleshootingError(error); + mcdu.setScratchpadMessage(NXSystemMessages.invalidFplnUplink); + }); + } + }; + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = () => { + if (alignOption) { + CDUIRSInit.ShowPage(mcdu); + } + }; + + const groundTemp = new Column(23, '---°', Column.right); + if (mcdu.groundTemp !== undefined) { + groundTemp.update( + mcdu.groundTemp.toFixed(0) + '°', + Column.cyan, + mcdu.groundTempPilot !== undefined ? Column.big : Column.small, + ); + } + + mcdu.onRightInput[5] = (scratchpadValue, scratchpadCallback) => { + try { + mcdu.trySetGroundTemp(scratchpadValue); + CDUInitPage.ShowPage1(mcdu); + } catch (msg) { + if (msg instanceof McduMessage) { + mcdu.setScratchpadMessage(msg); + scratchpadCallback(); + } else { + throw msg; + } + } + }; + + mcdu.onLeftInput[2] = flightNoAction; + + mcdu.setArrows(false, false, true, true); + + mcdu.setTemplate( + FormatTemplate([ + [new Column(10, 'INIT')], + [new Column(1, 'CO RTE'), new Column(21, 'FROM/TO', Column.right)], + [new Column(0, coRouteText, coRouteColor), fromTo], + [new Column(0, 'ALTN/CO RTE'), new Column(22, requestButtonLabel, Column.amber, Column.right)], + [altDest, new Column(23, requestButton, Column.amber, Column.right)], + [new Column(0, 'FLT NBR')], + [new Column(0, flightNoText, flightNoColor), new Column(23, alignOption || '', Column.right)], + [], + [new Column(23, 'WIND/TEMP>', Column.right)], + [new Column(0, 'COST INDEX'), new Column(23, 'TROPO', Column.right)], + [new Column(0, costIndexText, costIndexColor), tropo], + [new Column(0, 'CRZ FL/TEMP'), new Column(23, 'GND TEMP', Column.right)], + [cruiseFl, cruiseFlTempSeparator, cruiseTemp, groundTemp], + ]), + ); + + mcdu.onPrevPage = () => { + mcdu.goToFuelPredPage(); + }; + mcdu.onNextPage = () => { + mcdu.goToFuelPredPage(); + }; + + mcdu.onRightInput[3] = () => { + CDUWindPage.Return = () => { + CDUInitPage.ShowPage1(mcdu); + }; + CDUWindPage.ShowPage(mcdu); + }; + + mcdu.onUp = () => {}; + try { + Coherent.trigger('AP_ALT_VAL_SET', 4200); + Coherent.trigger('AP_VS_VAL_SET', 300); + Coherent.trigger('AP_HDG_VAL_SET', 180); + } catch (e) { + console.error(e); + } + } + // Does not refresh page so that other things can be performed first as necessary + static updateTowIfNeeded(mcdu: LegacyFmsPageInterface) { + if (isFinite(mcdu.taxiFuelWeight) && isFinite(mcdu.zeroFuelWeight) && isFinite(mcdu.blockFuel)) { + mcdu.takeOffWeight = mcdu.zeroFuelWeight + mcdu.blockFuel - mcdu.taxiFuelWeight; + } + } + static fuelPredConditionsMet(mcdu: LegacyFmsPageInterface) { + const fob = mcdu.getFOB(); + + return ( + Number.isFinite(fob) && + Number.isFinite(mcdu.zeroFuelWeightMassCenter) && + Number.isFinite(mcdu.zeroFuelWeight) && + mcdu.flightPlanService.active && + mcdu.flightPlanService.active.legCount > 0 && + mcdu._zeroFuelWeightZFWCGEntered + ); + } + static trySetFuelPred(mcdu: LegacyFmsPageInterface) { + if (CDUInitPage.fuelPredConditionsMet(mcdu) && !mcdu._fuelPredDone) { + setTimeout(() => { + if (CDUInitPage.fuelPredConditionsMet(mcdu) && !mcdu._fuelPredDone) { + //Double check as user can clear block fuel during timeout + mcdu._fuelPredDone = true; + if (mcdu.page.Current === mcdu.page.InitPageB) { + CDUInitPage.ShowPage2(mcdu); + } + } + }, mcdu.getDelayFuelPred()); + } + } + static ShowPage2(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.InitPageB; + mcdu.activeSystem = 'FMGC'; + mcdu.pageRedrawCallback = () => CDUInitPage.ShowPage2(mcdu); + + const alternate = mcdu.flightPlanService.active + ? mcdu.flightPlanService.active.alternateDestinationAirport + : undefined; + + const zfwCell = new Column(17, '___._', Column.amber, Column.right); + const zfwCgCell = new Column(22, '__._', Column.amber, Column.right); + const zfwCgCellDivider = new Column(18, '|', Column.amber, Column.right); + + if (mcdu._zeroFuelWeightZFWCGEntered) { + if (isFinite(mcdu.zeroFuelWeight)) { + zfwCell.update(NXUnits.kgToUser(mcdu.zeroFuelWeight).toFixed(1), Column.cyan); + } + if (isFinite(mcdu.zeroFuelWeightMassCenter)) { + zfwCgCell.update(mcdu.zeroFuelWeightMassCenter.toFixed(1), Column.cyan); + } + if (isFinite(mcdu.zeroFuelWeight) && isFinite(mcdu.zeroFuelWeightMassCenter)) { + zfwCgCellDivider.updateAttributes(Column.cyan); + } + } + mcdu.onRightInput[0] = async (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } else if (value === '') { + let zfw = undefined; + let zfwCg = undefined; + const a32nxBoarding = SimVar.GetSimVarValue('L:A32NX_BOARDING_STARTED_BY_USR', 'bool'); + const gsxBoarding = SimVar.GetSimVarValue('L:FSDT_GSX_BOARDING_STATE', 'number'); + if (a32nxBoarding || (gsxBoarding >= 4 && gsxBoarding < 6)) { + zfw = NXUnits.kgToUser(SimVar.GetSimVarValue('L:A32NX_AIRFRAME_ZFW_DESIRED', 'number')); + zfwCg = SimVar.GetSimVarValue('L:A32NX_AIRFRAME_ZFW_CG_PERCENT_MAC_DESIRED', 'number'); + } else if (isFinite(getZfw()) && isFinite(getZfwcg())) { + zfw = getZfw(); + zfwCg = getZfwcg(); + } + + // ZFW/ZFWCG auto-fill helper + if (zfw && zfwCg) { + mcdu.setScratchpadText(`${(zfw / 1000).toFixed(1)}/${zfwCg.toFixed(1)}`); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } else { + if (mcdu.trySetZeroFuelWeightZFWCG(value)) { + CDUInitPage.updateTowIfNeeded(mcdu); + CDUInitPage.ShowPage2(mcdu); + CDUInitPage.trySetFuelPred(mcdu); + } else { + scratchpadCallback(); + } + } + }; + + const blockFuel = new Column(23, '__._', Column.amber, Column.right); + if (mcdu._blockFuelEntered || mcdu._fuelPlanningPhase === FuelPlanningPhases.IN_PROGRESS) { + if (isFinite(mcdu.blockFuel)) { + blockFuel.update(NXUnits.kgToUser(mcdu.blockFuel).toFixed(1), Column.cyan); + } + } + mcdu.onRightInput[1] = async (value, scratchpadCallback) => { + if (mcdu._zeroFuelWeightZFWCGEntered && value !== Keypad.clrValue) { + //Simulate delay if calculating trip data + if (await mcdu.trySetBlockFuel(value)) { + CDUInitPage.updateTowIfNeeded(mcdu); + CDUInitPage.ShowPage2(mcdu); + CDUInitPage.trySetFuelPred(mcdu); + } else { + scratchpadCallback(); + } + } else { + if (await mcdu.trySetBlockFuel(value)) { + CDUInitPage.updateTowIfNeeded(mcdu); + CDUInitPage.ShowPage2(mcdu); + } else { + scratchpadCallback(); + } + } + }; + + const fuelPlanTopTitle = new Column(23, '', Column.amber, Column.right); + const fuelPlanBottomTitle = new Column(23, '', Column.amber, Column.right); + if (mcdu._zeroFuelWeightZFWCGEntered && !mcdu._blockFuelEntered) { + fuelPlanTopTitle.text = 'FUEL '; + fuelPlanBottomTitle.text = 'PLANNING }'; + mcdu.onRightInput[2] = async () => { + if (await mcdu.tryFuelPlanning()) { + CDUInitPage.updateTowIfNeeded(mcdu); + CDUInitPage.ShowPage2(mcdu); + } + }; + } + if (mcdu._fuelPlanningPhase === FuelPlanningPhases.IN_PROGRESS) { + fuelPlanTopTitle.update('BLOCK ', Column.green); + fuelPlanBottomTitle.update('CONFIRM', Column.green); + mcdu.onRightInput[2] = async () => { + if (await mcdu.tryFuelPlanning()) { + CDUInitPage.updateTowIfNeeded(mcdu); + CDUInitPage.ShowPage2(mcdu); + CDUInitPage.trySetFuelPred(mcdu); + } + }; + } + + const towCell = new Column(17, '---.-', Column.right); + const lwCell = new Column(23, '---.-', Column.right); + const towLwCellDivider = new Column(18, '/'); + const taxiFuelCell = new Column(0, '0.4', Column.cyan, Column.small); + + if (isFinite(mcdu.taxiFuelWeight)) { + if (mcdu._taxiEntered) { + taxiFuelCell.update(NXUnits.kgToUser(mcdu.taxiFuelWeight).toFixed(1), Column.big); + } else { + taxiFuelCell.text = NXUnits.kgToUser(mcdu.taxiFuelWeight).toFixed(1); + } + } + mcdu.onLeftInput[0] = async (value, scratchpadCallback) => { + if (mcdu._fuelPredDone) { + setTimeout(async () => { + if (mcdu.trySetTaxiFuelWeight(value)) { + CDUInitPage.updateTowIfNeeded(mcdu); + if (mcdu.page.Current === mcdu.page.InitPageB) { + CDUInitPage.ShowPage2(mcdu); + } + } else { + scratchpadCallback(); + } + }, mcdu.getDelayHigh()); + } else { + if (mcdu.trySetTaxiFuelWeight(value)) { + CDUInitPage.updateTowIfNeeded(mcdu); + CDUInitPage.ShowPage2(mcdu); + } else { + scratchpadCallback(); + } + } + }; + + const tripWeightCell = new Column(4, '---.-', Column.right); + const tripTimeCell = new Column(9, '----', Column.right); + const tripCellDivider = new Column(5, '/'); + const rteRsvWeightCell = new Column(4, '---.-', Column.right); + const rteRsvPercentCell = new Column(6, '5.0', Column.cyan); + const rteRsvCellDivider = new Column(5, '/', Column.cyan); + + if (isFinite(mcdu.getRouteReservedPercent())) { + rteRsvPercentCell.text = mcdu.getRouteReservedPercent().toFixed(1); + } + mcdu.onLeftInput[2] = async (value, scratchpadCallback) => { + if (await mcdu.trySetRouteReservedPercent(value)) { + CDUInitPage.ShowPage2(mcdu); + } else { + scratchpadCallback(); + } + }; + + const altnWeightCell = new Column(4, '---.-', Column.right); + const altnTimeCell = new Column(9, '----', Column.right); + const altnCellDivider = new Column(5, '/'); + const finalWeightCell = new Column(4, '---.-', Column.right); + const finalTimeCell = new Column(9, '----', Column.right); + const finalCellDivider = new Column(5, '/'); + + if (mcdu.getRouteFinalFuelTime() > 0) { + finalTimeCell.update(FmsFormatters.minutesTohhmm(mcdu.getRouteFinalFuelTime()), Column.cyan); + finalCellDivider.updateAttributes(Column.cyan); + } + mcdu.onLeftInput[4] = async (value, scratchpadCallback) => { + if (await mcdu.trySetRouteFinalTime(value)) { + CDUInitPage.ShowPage2(mcdu); + } else { + scratchpadCallback(); + } + }; + + const extraWeightCell = new Column(18, '---.-', Column.right); + const extraTimeCell = new Column(23, '----', Column.right); + const extraCellDivider = new Column(19, '/'); + const minDestFob = new Column(4, '---.-', Column.right); + const tripWindDirCell = new Column(19, '--'); + const tripWindAvgCell = new Column(21, '---'); + + if (mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport) { + tripWindDirCell.update(CDUInitPage.formatWindDirection(mcdu.averageWind), Column.cyan, Column.small); + tripWindAvgCell.update(CDUInitPage.formatWindComponent(mcdu.averageWind), Column.cyan); + + mcdu.onRightInput[4] = (value, scratchpadCallback) => { + if (mcdu.trySetAverageWind(value)) { + CDUInitPage.ShowPage2(mcdu); + } else { + scratchpadCallback(); + } + }; + } + + if (CDUInitPage.fuelPredConditionsMet(mcdu)) { + fuelPlanTopTitle.text = ''; + fuelPlanBottomTitle.text = ''; + + mcdu.tryUpdateTOW(); + if (isFinite(mcdu.takeOffWeight)) { + towCell.update(NXUnits.kgToUser(mcdu.takeOffWeight).toFixed(1), Column.green, Column.small); + } + + if (mcdu._fuelPredDone) { + if (!mcdu.routeFinalEntered()) { + mcdu.tryUpdateRouteFinalFuel(); + } + if (isFinite(mcdu.getRouteFinalFuelWeight()) && isFinite(mcdu.getRouteFinalFuelTime())) { + if (mcdu._rteFinalWeightEntered) { + finalWeightCell.update(NXUnits.kgToUser(mcdu.getRouteFinalFuelWeight()).toFixed(1), Column.cyan); + } else { + finalWeightCell.update( + NXUnits.kgToUser(mcdu.getRouteFinalFuelWeight()).toFixed(1), + Column.cyan, + Column.small, + ); + } + if (mcdu._rteFinalTimeEntered || !mcdu.routeFinalEntered()) { + finalTimeCell.update(FmsFormatters.minutesTohhmm(mcdu.getRouteFinalFuelTime()), Column.cyan); + } else { + finalTimeCell.update(FmsFormatters.minutesTohhmm(mcdu.getRouteFinalFuelTime()), Column.cyan, Column.small); + finalCellDivider.updateAttributes(Column.small); + } + finalCellDivider.updateAttributes(Column.cyan); + } + mcdu.onLeftInput[4] = async (value, scratchpadCallback) => { + setTimeout(async () => { + if (await mcdu.trySetRouteFinalFuel(value)) { + if (mcdu.page.Current === mcdu.page.InitPageB) { + CDUInitPage.ShowPage2(mcdu); + } + } else { + scratchpadCallback(); + } + }, mcdu.getDelayHigh()); + }; + + if (alternate) { + const altFuelEntered = mcdu._routeAltFuelEntered; + if (!altFuelEntered) { + mcdu.tryUpdateRouteAlternate(); + } + if (isFinite(mcdu.getRouteAltFuelWeight())) { + altnWeightCell.update( + NXUnits.kgToUser(mcdu.getRouteAltFuelWeight()).toFixed(1), + Column.cyan, + altFuelEntered ? Column.big : Column.small, + ); + const time = mcdu.getRouteAltFuelTime(); + if (time) { + altnTimeCell.update(FmsFormatters.minutesTohhmm(mcdu.getRouteAltFuelTime()), Column.green, Column.small); + altnCellDivider.updateAttributes(Column.green, Column.small); + } else { + altnTimeCell.update('----', Column.white); + altnCellDivider.updateAttributes(Column.white, altFuelEntered ? Column.big : Column.small); + } + } + } else { + altnWeightCell.update('0.0', Column.green, Column.small); + } + + mcdu.onLeftInput[3] = async (value, scratchpadCallback) => { + setTimeout(async () => { + if (await mcdu.trySetRouteAlternateFuel(value)) { + if (mcdu.page.Current === mcdu.page.InitPageB) { + CDUInitPage.ShowPage2(mcdu); + } + } else { + scratchpadCallback(); + } + }, mcdu.getDelayHigh()); + }; + + mcdu.tryUpdateRouteTrip(); + if (isFinite(mcdu.getTotalTripFuelCons()) && isFinite(mcdu.getTotalTripTime())) { + tripWeightCell.update(NXUnits.kgToUser(mcdu.getTotalTripFuelCons()).toFixed(1), Column.green, Column.small); + tripTimeCell.update(FmsFormatters.minutesTohhmm(mcdu._routeTripTime), Column.green, Column.small); + tripCellDivider.updateAttributes(Column.green, Column.small); + } + + if (isFinite(mcdu.getRouteReservedWeight())) { + if (mcdu._rteReservedWeightEntered) { + rteRsvWeightCell.update(NXUnits.kgToUser(mcdu.getRouteReservedWeight()).toFixed(1), Column.cyan); + } else { + rteRsvWeightCell.update( + NXUnits.kgToUser(mcdu.getRouteReservedWeight()).toFixed(1), + Column.cyan, + Column.small, + ); + } + } + + if (mcdu._rteRsvPercentOOR) { + rteRsvPercentCell.update('--.-', Column.white); + rteRsvCellDivider.updateAttributes(Column.white); + } else if (isFinite(mcdu.getRouteReservedPercent())) { + if (mcdu._rteReservedPctEntered || !mcdu.routeReservedEntered()) { + rteRsvPercentCell.update(mcdu.getRouteReservedPercent().toFixed(1), Column.cyan); + } else { + rteRsvPercentCell.update(mcdu.getRouteReservedPercent().toFixed(1), Column.cyan, Column.small); + rteRsvCellDivider.updateAttributes(Column.small); + } + } + + mcdu.onLeftInput[2] = async (value, scratchpadCallback) => { + setTimeout(async () => { + if (await mcdu.trySetRouteReservedFuel(value)) { + if (mcdu.page.Current === mcdu.page.InitPageB) { + CDUInitPage.ShowPage2(mcdu); + } + } else { + scratchpadCallback(); + } + }, mcdu.getDelayMedium()); + }; + + mcdu.tryUpdateLW(); + lwCell.update(NXUnits.kgToUser(mcdu.landingWeight).toFixed(1), Column.green, Column.small); + towLwCellDivider.updateAttributes(Column.green, Column.small); + + const windComponent = Number.isFinite(mcdu.averageWind) ? mcdu.averageWind : 0; + + tripWindDirCell.update(CDUInitPage.formatWindDirection(windComponent), Column.small); + tripWindAvgCell.update(CDUInitPage.formatWindComponent(windComponent), Column.big); + + mcdu.onRightInput[4] = async (value, scratchpadCallback) => { + setTimeout(() => { + if (mcdu.trySetAverageWind(value)) { + if (mcdu.page.Current === mcdu.page.InitPageB) { + CDUInitPage.ShowPage2(mcdu); + } + } else { + scratchpadCallback(); + } + }, mcdu.getDelayWindLoad()); + }; + + if (mcdu._minDestFobEntered) { + minDestFob.update(NXUnits.kgToUser(mcdu._minDestFob).toFixed(1), Column.cyan); + } else { + mcdu.tryUpdateMinDestFob(); + minDestFob.update(NXUnits.kgToUser(mcdu._minDestFob).toFixed(1), Column.cyan, Column.small); + } + mcdu.onLeftInput[5] = async (value, scratchpadCallback) => { + setTimeout(async () => { + if (await mcdu.trySetMinDestFob(value)) { + if (mcdu.page.Current === mcdu.page.InitPageB) { + CDUInitPage.ShowPage2(mcdu); + } + } else { + scratchpadCallback(); + } + }, mcdu.getDelayHigh()); + }; + mcdu.checkEFOBBelowMin(); + + extraWeightCell.update(NXUnits.kgToUser(mcdu.tryGetExtraFuel()).toFixed(1), Column.green, Column.small); + if (mcdu.tryGetExtraFuel() >= 0) { + extraTimeCell.update(FmsFormatters.minutesTohhmm(mcdu.tryGetExtraTime()), Column.green, Column.small); + extraCellDivider.updateAttributes(Column.green, Column.small); + } + } + } + + mcdu.setArrows(false, false, true, true); + + mcdu.setTemplate( + FormatTemplate([ + [new Column(5, 'INIT FUEL PRED')], + [new Column(0, 'TAXI'), new Column(15, 'ZFW/ZFWCG')], + [taxiFuelCell, zfwCell, zfwCgCellDivider, zfwCgCell], + [new Column(0, 'TRIP'), new Column(5, '/TIME'), new Column(19, 'BLOCK')], + [tripWeightCell, tripCellDivider, tripTimeCell, blockFuel], + [new Column(0, 'RTE RSV/%'), fuelPlanTopTitle], + [rteRsvWeightCell, rteRsvCellDivider, rteRsvPercentCell, fuelPlanBottomTitle], + [new Column(0, 'ALTN'), new Column(5, '/TIME'), new Column(15, 'TOW/'), new Column(22, 'LW')], + [altnWeightCell, altnCellDivider, altnTimeCell, towCell, towLwCellDivider, lwCell], + [new Column(0, 'FINAL/TIME'), new Column(15, 'TRIP WIND')], + [finalWeightCell, finalCellDivider, finalTimeCell, tripWindDirCell, tripWindAvgCell], + [new Column(0, 'MIN DEST FOB'), new Column(14, 'EXTRA/TIME')], + [minDestFob, extraWeightCell, extraCellDivider, extraTimeCell], + ]), + ); + + mcdu.onPrevPage = () => { + CDUInitPage.ShowPage1(mcdu); + }; + mcdu.onNextPage = () => { + CDUInitPage.ShowPage1(mcdu); + }; + } + + // Defining as static here to avoid duplicate code in CDUIRSInit + static ConvertDDToDMS(deg, lng) { + // converts decimal degrees to degrees minutes seconds + const M = 0 | ((deg % 1) * 60e7); + let degree; + if (lng) { + degree = (0 | (deg < 0 ? -deg : deg)).toString().padStart(3, '0'); + } else { + degree = 0 | (deg < 0 ? -deg : deg); + } + return { + dir: deg < 0 ? (lng ? 'W' : 'S') : lng ? 'E' : 'N', + deg: degree, + min: Math.abs(0 | (M / 1e7)), + sec: Math.abs((0 | (((M / 1e6) % 1) * 6e4)) / 100), + }; + } + + static formatWindDirection(tailwindComponent) { + return Math.round(tailwindComponent) > 0 ? 'TL' : 'HD'; + } + + static formatWindComponent(tailwindComponent) { + return Math.round(Math.abs(tailwindComponent)).toFixed(0).padStart(3, '0'); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_LateralRevisionPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_LateralRevisionPage.ts new file mode 100644 index 00000000000..30bcfb06c94 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_LateralRevisionPage.ts @@ -0,0 +1,230 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { FmgcFlightPhase } from '@shared/flightphase'; +import { A320_Neo_CDU_AirwaysFromWaypointPage } from './A320_Neo_CDU_AirwaysFromWaypointPage'; +import { CDUAvailableArrivalsPage } from './A320_Neo_CDU_AvailableArrivalsPage'; +import { CDUAvailableDeparturesPage } from './A320_Neo_CDU_AvailableDeparturesPage'; +import { CDUFixInfoPage } from './A320_Neo_CDU_FixInfoPage'; +import { CDUFlightPlanPage } from './A320_Neo_CDU_FlightPlanPage'; +import { CDUHoldAtPage } from './A320_Neo_CDU_HoldAtPage'; +import { CDUInitPage } from './A320_Neo_CDU_InitPage'; +import { NXFictionalMessages } from '../messages/NXSystemMessages'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; + +export class CDULateralRevisionPage { + /** + * + * @param mcdu + * @param leg {FlightPlanLeg} + * @param legIndexFP + * @constructor + */ + static ShowPage( + mcdu: LegacyFmsPageInterface, + leg, + legIndexFP, + forPlan = FlightPlanIndex.Active, + inAlternate = false, + ) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.LateralRevisionPage; + + let coordinates = ''; + if (leg && leg.definition && leg.definition.waypoint && leg.definition.waypoint.location) { + const lat = CDUInitPage.ConvertDDToDMS(leg.definition.waypoint.location['lat'], false); + const long = CDUInitPage.ConvertDDToDMS(leg.definition.waypoint.location['long'], true); + coordinates = `${lat.deg}°${lat.min}.${Math.ceil(Number(lat.sec / 100))}${lat.dir}/${long.deg}°${long.min}.${Math.ceil(Number(long.sec / 100))}${long.dir}[color]green`; + } + /** @type {BaseFlightPlan} */ + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + + const isPpos = leg === undefined || (legIndexFP === 0 && leg !== targetPlan.originLeg); + const isFrom = legIndexFP === targetPlan.fromLegIndex && forPlan === FlightPlanIndex.Active && !inAlternate; + const isDeparture = legIndexFP === targetPlan.originLegIndex && !isPpos; // TODO this is bogus... compare icaos + const isDestination = legIndexFP === targetPlan.destinationLegIndex && !isPpos; // TODO this is bogus... compare icaos + const isWaypoint = !isDeparture && !isDestination && !isPpos; + const isManual = leg && leg.isVectors(); + + let waypointIdent = isPpos ? 'PPOS' : '---'; + + if (leg) { + if (isDestination && targetPlan.destinationRunway) { + waypointIdent = targetPlan.destinationRunway.ident; + } else { + waypointIdent = leg.ident; + } + } + + let departureCell = ''; + if (isDeparture) { + departureCell = ' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = () => { + CDUAvailableDeparturesPage.ShowPage(mcdu, targetPlan.originAirport, -1, false, forPlan, inAlternate); + }; + } + + let arrivalFixInfoCell = ''; + if (isDestination) { + arrivalFixInfoCell = 'ARRIVAL>'; + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = () => { + CDUAvailableArrivalsPage.ShowPage(mcdu, targetPlan.destinationAirport, 0, false, forPlan, inAlternate); + }; + } else if (isDeparture || isPpos || isFrom) { + arrivalFixInfoCell = 'FIX INFO>'; + mcdu.onRightInput[0] = () => { + CDUFixInfoPage.ShowPage(mcdu); + }; + } + + let crossingLabel = ''; + let crossingCell = ''; + if (!isDestination) { + crossingLabel = 'LL XING/INCR/NO[color]inop'; + crossingCell = '[{sp}{sp}]°/[{sp}]°/[][color]inop'; + } + + let offsetCell = ''; + if (isDeparture || isWaypoint) { + offsetCell = ' { + mcdu.insertWaypoint(value, forPlan, inAlternate, legIndexFP, false, (success) => { + if (!success) { + scratchpadCallback(); + } + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }); + }; + } + + let holdCell = ''; + if (isWaypoint) { + holdCell = ' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = () => { + const nextLeg = targetPlan.maybeElementAt(legIndexFP + 1); + + if (nextLeg && nextLeg.isDiscontinuity === false && nextLeg.isHX()) { + CDUHoldAtPage.ShowPage(mcdu, legIndexFP + 1, forPlan, inAlternate); + } else { + CDUHoldAtPage.ShowPage(mcdu, legIndexFP, forPlan, inAlternate); + } + }; + } + + let enableAltnLabel = ''; + let enableAltnCell = ''; + if (targetPlan['alternateDestinationAirport'] && !isDeparture && !inAlternate) { + enableAltnLabel = '{sp}ENABLE[color]cyan'; + enableAltnCell = '{ALTN[color]cyan'; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = async () => { + // TODO fm position + const ppos = { + lat: SimVar.GetSimVarValue('PLANE LATITUDE', 'Degrees'), + long: SimVar.GetSimVarValue('PLANE LONGITUDE', 'Degrees'), + }; + + const flightPlan = mcdu.getFlightPlan(forPlan); + const alternateAirport = flightPlan.alternateDestinationAirport; + if (alternateAirport) { + const distance = Avionics.Utils.computeGreatCircleDistance(ppos, alternateAirport.location); + const cruiseLevel = CDULateralRevisionPage.determineAlternateFlightLevel(distance); + + try { + await mcdu.flightPlanService.enableAltn(legIndexFP, cruiseLevel, forPlan); + } catch (e) { + console.error(e); + mcdu.logTroubleshootingError(e); + mcdu.setScratchpadMessage(NXFictionalMessages.internalError); + } + + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + } + }; + } + + let newDestLabel = ''; + let newDestCell = ''; + if (!isDestination && !isPpos && !isManual) { + newDestLabel = 'NEW DEST{sp}'; + newDestCell = '[{sp}{sp}][color]cyan'; + + mcdu.onRightInput[3] = async (value) => { + await mcdu.flightPlanService.newDest(legIndexFP, value, forPlan, inAlternate); + + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }; + } + + let airwaysCell = ''; + if (isWaypoint) { + airwaysCell = 'AIRWAYS>'; + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + A320_Neo_CDU_AirwaysFromWaypointPage.ShowPage(mcdu, legIndexFP, undefined, undefined, forPlan, inAlternate); + }; + } + + let altnCell = ''; + if (isDestination) { + altnCell = ' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUFlightPlanPage.ShowPage(mcdu, 0, forPlan); + }; + } + + static determineAlternateFlightLevel(distance) { + if (distance > 200) { + return 310; + } else if (distance > 100) { + return 220; + } else { + return 100; + } + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_MenuPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_MenuPage.ts new file mode 100644 index 00000000000..776d910f61a --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_MenuPage.ts @@ -0,0 +1,143 @@ +import { Column, FormatTemplate } from '../legacy/A320_Neo_CDU_Format'; +import { CDUIdentPage } from './A320_Neo_CDU_IdentPage'; +import { CDU_AIDS_MainMenu } from './aids/A320_Neo_CDU_AIDS_Menu'; +import { CDUAtsuMenu } from './atsu/A320_Neo_CDU_ATSU_Menu'; +import { CDUCfdsMainMenu } from './cfdiu/A320_Neo_CDU_CFDS_Menu'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { LegacyAtsuPageInterface } from '../legacy/LegacyAtsuPageInterface'; + +export class CDUMenuPage { + static ShowPage(mcdu: LegacyFmsPageInterface & LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.MenuPage; + // The MCDU MENU does not maintain an editable scratchpad... subsystems and the backup nav do that. + mcdu.activateMcduScratchpad(); + + const fmActive = mcdu.activeSystem === 'FMGC'; + const atsuActive = mcdu.activeSystem === 'ATSU'; + const aidsActive = mcdu.activeSystem === 'AIDS'; + const cfdsActive = mcdu.activeSystem === 'CFDS'; + + // delay to get text and draw already connected subsystem page + const connectedSubsystemDelay = 200; + // delay to establish initial communication with disconnect systems on low speed ports + const disconnectedSubsystemDelay = Math.floor(Math.random() * 800) + 500; + + /** + * Updates the page text. + * @param {"FMGC" | "ATSU" | "AIDS" | "CFDS" | null} selectedSystem Newly selected system establishing comms, or null if none. + */ + const updateView = (selectedSystem = null) => { + const getText = (name, isRequesting = false, isLeft = true) => { + let flag = null; + if (selectedSystem !== null) { + if (selectedSystem === name) { + flag = '(SEL)'; + } + } else if (isRequesting) { + flag = '(REQ)'; + } + if (isLeft) { + return `${name}\xa0${flag !== null ? flag : ''}`; + } else { + return `${flag !== null ? flag : ''}\xa0${name}`; + } + }; + const getColor = (isActive, isSelected) => + isSelected ? Column.cyan : isActive && selectedSystem === null ? Column.green : Column.white; + + mcdu.setTemplate( + FormatTemplate([ + [new Column(7, 'MCDU MENU')], + [new Column(22, 'SELECT', Column.right, Column.inop)], + [ + new Column( + 0, + getText('', Column.right, Column.inop), + ], + [], + [ + new Column( + 0, + getText(' { + mcdu.mcduScratchpad.setMessage(NXSystemMessages.waitForSystemResponse); + updateView('FMGC'); + setTimeout(() => { + mcdu.mcduScratchpad.removeMessage(NXSystemMessages.waitForSystemResponse.text); + CDUIdentPage.ShowPage(mcdu); + }, connectedSubsystemDelay); // FMGCs are on high-speed port... always fast + }; + + mcdu.onLeftInput[1] = () => { + mcdu.mcduScratchpad.setMessage(NXSystemMessages.waitForSystemResponse); + updateView('ATSU'); + setTimeout( + () => { + mcdu.mcduScratchpad.removeMessage(NXSystemMessages.waitForSystemResponse.text); + CDUAtsuMenu.ShowPage(mcdu); + }, + atsuActive ? connectedSubsystemDelay : disconnectedSubsystemDelay, + ); + }; + + mcdu.onLeftInput[2] = () => { + mcdu.mcduScratchpad.setMessage(NXSystemMessages.waitForSystemResponse); + updateView('AIDS'); + setTimeout( + () => { + mcdu.mcduScratchpad.removeMessage(NXSystemMessages.waitForSystemResponse.text); + CDU_AIDS_MainMenu.ShowPage(mcdu); + }, + aidsActive ? connectedSubsystemDelay : disconnectedSubsystemDelay, + ); + }; + + mcdu.onLeftInput[3] = () => { + mcdu.mcduScratchpad.setMessage(NXSystemMessages.waitForSystemResponse); + updateView('CFDS'); + setTimeout( + () => { + mcdu.mcduScratchpad.removeMessage(NXSystemMessages.waitForSystemResponse.text); + CDUCfdsMainMenu.ShowPage(mcdu); + }, + cfdsActive ? connectedSubsystemDelay : disconnectedSubsystemDelay, + ); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_NavRadioPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_NavRadioPage.ts new file mode 100644 index 00000000000..4efa0b87d7b --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_NavRadioPage.ts @@ -0,0 +1,420 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { RadioUtils } from '@flybywiresim/fbw-sdk'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { FmgcFlightPhase } from '@shared/flightphase'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { MmrRadioTuningStatus } from '@fmgc/navigation/NavaidTuner'; + +export class CDUNavRadioPage { + static ShowPage(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.NavRadioPage; + mcdu.activeSystem = 'FMGC'; + mcdu.pageRedrawCallback = () => { + CDUNavRadioPage.ShowPage(mcdu); + }; + setTimeout(mcdu.requestUpdate.bind(mcdu), 500); + mcdu.returnPageCallback = () => { + CDUNavRadioPage.ShowPage(mcdu); + }; + + const lsk1Row = []; + const lsk2Row = []; + const lsk3Row = []; + const lsk4Title = ['CRS']; + const lsk4Row = []; + const lsk5Row = []; + const lsk6Row = []; + + // this is the state when FM radio tuning is not active + const template = [ + ['RADIO NAV'], + ['VOR1/FREQ', 'FREQ/VOR2'], + lsk1Row, + ['CRS', 'CRS'], + lsk2Row, + ['\xa0LS\xa0/FREQ'], + lsk3Row, + lsk4Title, + lsk4Row, + ['ADF1/FREQ', 'FREQ/ADF2'], + lsk5Row, + [''], + lsk6Row, + ]; + + if (!mcdu.isFmTuningActive()) { + mcdu.setTemplate(template); + return; + } + + // VOR 1 + lsk1Row[0] = CDUNavRadioPage.renderVor(mcdu, 1); + mcdu.onLeftInput[0] = CDUNavRadioPage.handleVorLsk.bind(this, mcdu, 1); + lsk2Row[0] = CDUNavRadioPage.renderVorCrs(mcdu, 1); + mcdu.onLeftInput[1] = CDUNavRadioPage.handleVorCrsLsk.bind(this, mcdu, 1); + + // VOR 2 + lsk1Row[1] = CDUNavRadioPage.renderVor(mcdu, 2); + mcdu.onRightInput[0] = CDUNavRadioPage.handleVorLsk.bind(this, mcdu, 2); + lsk2Row[1] = CDUNavRadioPage.renderVorCrs(mcdu, 2); + mcdu.onRightInput[1] = CDUNavRadioPage.handleVorCrsLsk.bind(this, mcdu, 2); + + // LS + lsk3Row[0] = CDUNavRadioPage.renderMmr(mcdu); + mcdu.onLeftInput[2] = CDUNavRadioPage.handleMmrLsk.bind(this, mcdu); + lsk4Row[0] = CDUNavRadioPage.renderMmrCrs(mcdu); + mcdu.onLeftInput[3] = CDUNavRadioPage.handleMmrCrsLsk.bind(this, mcdu); + lsk4Title[0] = CDUNavRadioPage.renderMmrCrsTitle(mcdu); + + // ADF 1 + lsk5Row[0] = CDUNavRadioPage.renderAdf(mcdu, 1); + mcdu.onLeftInput[4] = CDUNavRadioPage.handleAdfLsk.bind(this, mcdu, 1); + // FIXME BFO + + // ADF 2 + lsk5Row[1] = CDUNavRadioPage.renderAdf(mcdu, 2); + mcdu.onRightInput[4] = CDUNavRadioPage.handleAdfLsk.bind(this, mcdu, 2); + // FIXME BFO + + mcdu.setTemplate(template); + } + + static handleVorLsk( + mcdu: LegacyFmsPageInterface, + receiverIndex: 1 | 2, + input: string, + scratchpadCallback: () => void, + ) { + if (input === Keypad.clrValue) { + const vor = mcdu.getVorTuningData(receiverIndex); + if (vor.manual) { + mcdu.setManualVor(receiverIndex, null); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } + } else if (input.match(/^\d{3}(\.\d{1,2})?$/) !== null) { + const freq = parseInt(input.replace('.', '').padEnd(5, '0'), 16) << 8; + + if (!RadioUtils.isValidRange(freq, RadioUtils.RadioChannelType.VhfNavaid50)) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return false; + } else if (!RadioUtils.isValidSpacing(freq, RadioUtils.RadioChannelType.VhfNavaid50)) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return false; + } + + mcdu.setManualVor(receiverIndex, RadioUtils.unpackBcd32(freq) / 1e6); + } else if (input.length >= 1 && input.length <= 4) { + // ident + mcdu.getOrSelectVORsByIdent(input, (navaid) => { + if (navaid) { + if (mcdu.deselectedNavaids.find((databaseId) => databaseId === navaid.databaseId)) { + mcdu.setScratchpadMessage(NXSystemMessages.xxxIsDeselected.getModifiedMessage(navaid.ident)); + scratchpadCallback(); + return; + } + mcdu.setManualVor(receiverIndex, navaid); + requestAnimationFrame(() => { + CDUNavRadioPage.ShowPage(mcdu); + }); + } else { + // FIXME new navaid page when it's built + mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); + scratchpadCallback(); + } + }); + return; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + requestAnimationFrame(() => { + CDUNavRadioPage.ShowPage(mcdu); + }); + } + + static renderVor(mcdu: LegacyFmsPageInterface, receiverIndex: 1 | 2) { + const vor = mcdu.getVorTuningData(receiverIndex); + let identText: string; + let freqText: string; + if (vor.frequency !== null) { + const ident = vor.ident !== null ? vor.ident : '[\xa0\xa0]'; + identText = `{${vor.manual ? 'big' : 'small'}}${receiverIndex === 2 ? ident.padEnd(4, '\xa0') : ident.padStart(4, '\xa0')}{end}`; + freqText = `{${vor.manual && vor.ident === null ? 'big' : 'small'}}${vor.frequency.toFixed(2)}{end}`; + } else { + identText = '[\xa0\xa0]'; + freqText = '[\xa0\xa0.\xa0]'; + } + + return `{cyan}${receiverIndex === 2 ? freqText : identText}{${vor.manual ? 'big' : 'small'}}/{end}${receiverIndex === 2 ? identText : freqText}{end}`; + } + + static handleVorCrsLsk( + mcdu: LegacyFmsPageInterface, + receiverIndex: 1 | 2, + input: string, + scratchpadCallback: () => void, + ) { + if (input === Keypad.clrValue) { + mcdu.setVorCourse(receiverIndex, null); + } else if (input.match(/^\d{1,3}$/) !== null) { + const course = parseInt(input); + if (course < 0 || course > 360) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return; + } + mcdu.setVorCourse(receiverIndex, course % 360); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + requestAnimationFrame(() => { + CDUNavRadioPage.ShowPage(mcdu); + }); + } + + static renderVorCrs(mcdu: LegacyFmsPageInterface, receiverIndex: 1 | 2) { + // FIXME T suffix for true-ref VORs + const vor = mcdu.getVorTuningData(receiverIndex); + if (vor.dmeOnly) { + return ''; + } + if (vor.course !== null) { + return `{cyan}${vor.course.toFixed(0).padStart(3, '0')}{end}`; + } + return '{cyan}[\xa0]{end}'; + } + + static handleMmrLsk(mcdu: LegacyFmsPageInterface, input: string, scratchpadCallback: () => void) { + if (mcdu.isMmrTuningLocked()) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + return; + } + + const onDone = () => + requestAnimationFrame(() => { + CDUNavRadioPage.ShowPage(mcdu); + }); + + if (input === Keypad.clrValue) { + const mmr = mcdu.getMmrTuningData(1); + if (mmr.manual) { + mcdu.setManualIls(null).then(onDone); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } + } else if (input.match(/^\d{3}(\.\d{1,2})?$/) !== null) { + const freq = parseInt(input.replace('.', '').padEnd(5, '0'), 16) << 8; + + if (!RadioUtils.isValidRange(freq, RadioUtils.RadioChannelType.IlsNavaid50)) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return false; + } else if (!RadioUtils.isValidSpacing(freq, RadioUtils.RadioChannelType.IlsNavaid50)) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return false; + } + + mcdu.setManualIls(RadioUtils.unpackBcd32(freq) / 1e6).then(onDone); + } else if (input.length >= 1 && input.length <= 4) { + // ident + mcdu.getOrSelectILSsByIdent(input, (navaid) => { + if (navaid) { + if (mcdu.deselectedNavaids.find((databaseId) => databaseId === navaid.databaseId)) { + mcdu.setScratchpadMessage(NXSystemMessages.xxxIsDeselected.getModifiedMessage(navaid.ident)); + scratchpadCallback(); + return; + } + + mcdu.setManualIls(navaid).then(onDone); + } else { + // FIXME new navaid page when it's built + mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); + scratchpadCallback(); + } + }); + return; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + } + + static renderMmr(mcdu: LegacyFmsPageInterface) { + const mmr = mcdu.getMmrTuningData(1); + let identText: string; + let freqText: string; + if (mmr.frequency !== null) { + const ident = mmr.ident !== null ? mmr.ident : '[\xa0\xa0]'; + identText = `{${mmr.manual ? 'big' : 'small'}}${ident.padStart(4, '\xa0')}{end}`; + freqText = `{${mmr.manual && mmr.ident === null ? 'big' : 'small'}}${mmr.frequency.toFixed(2)}{end}`; + } else { + identText = '[\xa0\xa0]'; + freqText = '[\xa0\xa0\xa0\xa0]'; + } + + return `{cyan}${identText}{${mmr.manual ? 'big' : 'small'}}/{end}${freqText}{end}`; + } + + static handleMmrCrsLsk(mcdu: LegacyFmsPageInterface, input: string, scratchpadCallback: () => void) { + if (mcdu.isMmrTuningLocked()) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + return; + } + + if (input === Keypad.clrValue) { + const mmr = mcdu.getMmrTuningData(1); + if (mmr.courseManual) { + mcdu.setIlsCourse(null); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } + } else if (input === 'F' || input === 'B') { + // change existing course between front course and back course + const mmr = mcdu.getMmrTuningData(1); + if (mmr.course === null) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + return; + } + const backcourse = input.charAt(0) === 'B'; + mcdu.setIlsCourse(mmr.course, backcourse); + } else if (input.match(/^[BF]?\d{1,3}$/) !== null) { + const backcourse = input.charAt(0) === 'B'; + const course = input.charAt(0) === 'F' || backcourse ? parseInt(input.slice(1)) : parseInt(input); + if (course < 0 || course > 360) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return; + } + mcdu.setIlsCourse(course % 360, backcourse); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + requestAnimationFrame(() => { + CDUNavRadioPage.ShowPage(mcdu); + }); + } + + static showSlope(mcdu: LegacyFmsPageInterface, mmr: MmrRadioTuningStatus) { + const takeoff = mcdu.flightPhaseManager.phase <= FmgcFlightPhase.Takeoff; + const ilsAppr = mcdu.flightPlanService.active.approach && mcdu.flightPlanService.active.approach.type === 5; // ILS + return mmr.manual || (!takeoff && ilsAppr); + } + + static renderMmrCrs(mcdu: LegacyFmsPageInterface) { + const mmr = mcdu.getMmrTuningData(1); + const showSlope = CDUNavRadioPage.showSlope(mcdu, mmr); + const slope = + mmr.slope !== null ? `\xa0\xa0{small}{green}${mmr.slope.toFixed(1)}{end}{end}` : '{white}\xa0\xa0\xa0-.-{end}'; + if (mmr.course !== null) { + return `{${mmr.courseManual ? 'big' : 'small'}}{cyan}${mmr.backcourse ? 'B' : 'F'}${mmr.course.toFixed(0).padStart(3, '0')}{end}{end}${showSlope ? slope : ''}`; + } + if (mmr.frequency !== null) { + return `{amber}____{end}${slope}`; + } + return '{cyan}[\xa0\xa0]{end}'; + } + + static renderMmrCrsTitle(mcdu) { + const mmr = mcdu.getMmrTuningData(1); + const showSlope = CDUNavRadioPage.showSlope(mcdu, mmr); + if (showSlope && mmr.frequency !== null) { + return 'CRS\xa0\xa0\xa0SLOPE'; + } + return 'CRS'; + } + + static handleAdfLsk( + mcdu: LegacyFmsPageInterface, + receiverIndex: 1 | 2, + input: string, + scratchpadCallback: () => void, + ) { + if (input === Keypad.clrValue) { + const adf = mcdu.getAdfTuningData(receiverIndex); + if (adf.manual) { + mcdu.setManualAdf(receiverIndex, null); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } + } else if (input.match(/^\d{3,4}(\.\d)?$/) !== null) { + const freq = parseFloat(input); + // 190.0 - 1750.0 with some tolerance for FP precision + if (freq <= 189.95 || freq >= 1750.05) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return false; + } + + mcdu.setManualAdf(receiverIndex, freq); + } else if (input.length >= 1 && input.length <= 4) { + // ident + mcdu.getOrSelectNDBsByIdent(input, (navaid) => { + if (navaid) { + if (mcdu.deselectedNavaids.find((databaseId) => databaseId === navaid.databaseId)) { + mcdu.setScratchpadMessage(NXSystemMessages.xxxIsDeselected.getModifiedMessage(navaid.ident)); + scratchpadCallback(); + return; + } + mcdu.setManualAdf(receiverIndex, navaid); + requestAnimationFrame(() => { + CDUNavRadioPage.ShowPage(mcdu); + }); + } else { + // FIXME new navaid page when it's built + mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); + scratchpadCallback(); + } + }); + return; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + requestAnimationFrame(() => { + CDUNavRadioPage.ShowPage(mcdu); + }); + } + + static renderAdf(mcdu: LegacyFmsPageInterface, receiverIndex: 1 | 2) { + const adf = mcdu.getAdfTuningData(receiverIndex); + let identText; + let freqText; + if (adf.frequency !== null) { + const ident = adf.ident !== null ? adf.ident : '[\xa0\xa0]'; + identText = `{${adf.manual ? 'big' : 'small'}}${receiverIndex === 2 ? ident.padEnd(4, '\xa0') : ident.padStart(4, '\xa0')}{end}`; + freqText = `{${adf.manual && adf.ident === null ? 'big' : 'small'}}${adf.frequency.toFixed(1)}{end}`; + } else { + identText = '[\xa0\xa0]'; + freqText = '[\xa0\xa0\xa0.]'; + } + + return `{cyan}${receiverIndex === 2 ? freqText : identText}{${adf.manual ? 'big' : 'small'}}/{end}${receiverIndex === 2 ? identText : freqText}{end}`; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_NavaidPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_NavaidPage.ts new file mode 100644 index 00000000000..4288484a31e --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_NavaidPage.ts @@ -0,0 +1,194 @@ +import { + IlsNavaid, + NdbNavaid, + VhfNavaid, + LsCategory, + VhfNavaidType, + isIlsNavaid, + isNdbNavaid, + VorClass, + NavaidSubsectionCode, +} from '@flybywiresim/fbw-sdk'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { CDUPilotsWaypoint } from './A320_Neo_CDU_PilotsWaypoint'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class CDUNavaidPage { + /** + * @param mcdu MCDU + * @param facility MSFS facility to show + * @param returnPage Callback for the RETURN LSK... only for use by SELECTED NAVAIDS + */ + static ShowPage(mcdu: LegacyFmsPageInterface, facility?: VhfNavaid | NdbNavaid | IlsNavaid, returnPage?) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.NavaidPage; + mcdu.returnPageCallback = () => { + CDUNavaidPage.ShowPage(mcdu); + }; + + const template = [ + ['NAVAID'], + ['\xa0IDENT'], + ['____[color]amber'], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + ]; + + if (facility) { + let latLon = isIlsNavaid(facility) ? facility.locLocation : facility.location; + if (facility.sectionCode === 4 && facility.subSectionCode === 7) { + // airport && ils + CDUNavaidPage.renderIls(facility, template); + latLon = facility.locLocation; + } else if (facility.sectionCode === 1 && facility.subSectionCode === 0) { + // navaid && vhf + CDUNavaidPage.renderVorDme(facility, template); + } + + template[2][0] = `{cyan}${facility.ident}{end}`; + + // 3L + template[3][0] = '\xa0\xa0\xa0\xa0LAT/LONG'; + template[4][0] = `{green}${CDUPilotsWaypoint.formatLatLong(latLon)}{end}`; + + // 4L + template[5][0] = '\xa0FREQ'; + template[6][0] = `{green}${CDUNavaidPage.formatFrequency(facility)}{end}`; + } + + if (returnPage !== undefined) { + template[12][1] = 'RETURN>'; + mcdu.onRightInput[5] = () => returnPage(); + } + + mcdu.setTemplate(template); + + mcdu.onLeftInput[0] = (value, scratchpadCallback) => { + // FIXME this does not get ILS navaids + mcdu.getOrSelectNavaidsByIdent(value, (res) => { + if (res) { + CDUNavaidPage.ShowPage(mcdu, res, returnPage); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + } + }); + }; + } + + static renderIls(facility: IlsNavaid, template: string[][]) { + let cat = 0; + switch (facility.category) { + case LsCategory.Category1: + cat = 1; + break; + case LsCategory.Category2: + cat = 2; + break; + case LsCategory.Category3: + cat = 3; + break; + } + + // 1R + template[1][1] = 'RWY IDENT'; + // FIXME + //template[2][1] = `{green}${facility.runwayIdent}{end}`; + template[2][1] = ``; + + // 2L + template[3][0] = 'CLASS'; + // FIXME should show "NONCOLLOCATED" for ILS without DME + template[4][0] = '{green}ILSDME{end}'; + + // 2R + if (cat > 0) { + template[3][1] = 'CATEGORY'; + template[4][1] = `{green}${cat}{end}`; + } + + // 3R + template[5][1] = 'COURSE'; + template[6][1] = `{green}${facility.locBearing.toFixed(0).padStart(3, '0')}{end}`; + + // 4R + if (facility.gsSlope) { + template[7][1] = 'SLOPE'; + template[8][1] = `{green}${facility.gsSlope.toFixed(1)}{end}`; + } + } + + static renderVorDme(facility: VhfNavaid, template) { + const isTrueRef = facility.stationDeclination < 1e-6 && Math.abs(facility.location.lat) > 63; + const suffix = isTrueRef ? 'T' : facility.stationDeclination < 0 ? 'W' : 'E'; + + // 1R + template[1][1] = 'STATION DEC'; + template[2][1] = `{green}${Math.abs(facility.stationDeclination).toFixed(0).padStart(3, '0')}${suffix}{end}`; + + // 2L + template[3][0] = 'CLASS'; + template[4][0] = + facility.type === VhfNavaidType.VorDme || facility.type === VhfNavaidType.Vortac + ? '{green}VORTAC{end}' + : '{green}NONCOLLOCATED{end}'; + + // 5L + if (facility.dmeLocation && facility.dmeLocation.alt !== undefined) { + template[9][0] = 'ELV'; + template[10][0] = `{green}${(10 * Math.round(facility.dmeLocation.alt / 10)).toFixed(0)}{end}`; + } + + // 6L + const fom = CDUNavaidPage.formatFigureOfMerit(facility); + if (fom) { + template[11][0] = '\xa0FIG OF MERIT'; + template[12][0] = `{green}${fom}{end}`; + } + } + + /** + * @param facility Navaid + * @returns formatted frequency + */ + static formatFrequency(facility: NdbNavaid | VhfNavaid | IlsNavaid): string { + if (isNdbNavaid(facility)) { + return facility.frequency.toFixed(0); + } + return facility.frequency.toFixed(2); + } + + /** + * Format the figure of merit if possible + * @param facility Navaid + * @returns formatted FoM or blank + */ + static formatFigureOfMerit(facility: VhfNavaid): string { + if ( + (facility.subSectionCode === NavaidSubsectionCode.VhfNavaid && facility.type === VhfNavaidType.Dme) || + facility.type === VhfNavaidType.Vor || + facility.type === VhfNavaidType.VorDme || + facility.type === VhfNavaidType.Vortac + ) { + switch (facility.class) { + case VorClass.HighAlt: + return '3'; + case VorClass.Unknown: + return '2'; + case VorClass.LowAlt: + return '1'; + case VorClass.Terminal: + return '0'; + } + } + return ''; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_NewWaypoint.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_NewWaypoint.ts new file mode 100644 index 00000000000..7b2b9fd3473 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_NewWaypoint.ts @@ -0,0 +1,291 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +// FIXME fix circular ref with clrValue +import { PilotWaypoint, PilotWaypointType } from '@fmgc/flightplanning/DataManager'; +// FIXME fix circular ref +import { CDUPilotsWaypoint } from './A320_Neo_CDU_PilotsWaypoint'; +import { McduMessage, NXFictionalMessages, NXSystemMessages } from '../messages/NXSystemMessages'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { Coordinates } from '@fmgc/flightplanning/data/geo'; +import { Fix, Waypoint } from '@flybywiresim/fbw-sdk'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +type NewWaypointDoneCallback = (waypoint: PilotWaypoint | undefined | null) => void; +interface InProgressData { + ident: string; + type: PilotWaypointType; + wp: Waypoint; + coordinates: Coordinates; + place: Fix; + bearing: number; + distance: number; + place1: Fix; + place2: Fix; + bearing1: number; + bearing2: number; +} + +export class CDUNewWaypoint { + /** + * New Waypoint Page + * @param doneCallback callback when the user is finished with the page + * @param _inProgressData private data used by the page + */ + static ShowPage( + mcdu: LegacyFmsPageInterface, + doneCallback: NewWaypointDoneCallback = undefined, + _inProgressData: Partial = {}, + ) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.NewWaypoint; + mcdu.returnPageCallback = () => { + CDUNewWaypoint.ShowPage(mcdu, doneCallback, _inProgressData); + }; + + const template = [ + ['NEW WAYPOINT'], + ['IDENT'], + [_inProgressData.ident !== undefined ? `{cyan}${_inProgressData.ident}{end}` : '_______[color]amber'], + ['LAT/LONG'], + ['____.__|_____.__[color]amber'], + ['PLACE/BRG /DIST'], + ['_______|___° |___. _[color]amber'], + ['PLACE-BRG /PLACE-BRG'], + ['{amber}_____-___° |_____-___°{end}'], + [''], + ['', 'RETURN>'], + [''], + ['', _inProgressData.type !== undefined ? '{amber}STORE}{end}' : ''], + ]; + + switch (_inProgressData.type) { + case PilotWaypointType.LatLon: + template[4][0] = `{cyan}${CDUPilotsWaypoint.formatLatLong(_inProgressData.wp.location)}{end}`; + template[5].length = 0; + template[6].length = 0; + template[7].length = 0; + template[8].length = 0; + break; + case PilotWaypointType.Pbd: + template[4][0] = `{cyan}{small}${CDUPilotsWaypoint.formatLatLong(_inProgressData.wp.location)}{end}{end}`; + template[5][0] = 'PLACE\xa0\xa0/BRG\xa0/DIST'; + template[6][0] = `{cyan}${_inProgressData.place.ident.padEnd(7, '\xa0')}/${CDUPilotsWaypoint.formatBearing(_inProgressData.bearing)}/${_inProgressData.distance.toFixed(1)}{end}`; + template[7].length = 0; + template[8].length = 0; + break; + case PilotWaypointType.Pbx: + template[4][0] = `{cyan}{small}${CDUPilotsWaypoint.formatLatLong(_inProgressData.wp.location)}{end}{end}`; + template[5].length = 0; + template[6].length = 0; + template[7][0] = 'PLACE-BRG\xa0\xa0/PLACE-BRG'; + template[8][0] = `{cyan}${_inProgressData.place1.ident.padEnd(5, '\xa0')}-${CDUPilotsWaypoint.formatBearing(_inProgressData.bearing1)}/${_inProgressData.place2.ident.padEnd(5, '\xa0')}-${CDUPilotsWaypoint.formatBearing(_inProgressData.bearing2)}{end}`; + break; + default: + } + + mcdu.setTemplate(template); + + // ident + mcdu.onLeftInput[0] = (value, scratchpadCallback) => { + if (/^[A-Z0-9]{2,7}$/.test(value)) { + if (_inProgressData === undefined) { + _inProgressData = {}; + } + _inProgressData.ident = value; + requestAnimationFrame(() => CDUNewWaypoint.ShowPage(mcdu, doneCallback, _inProgressData)); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + } + }; + + // lat/lon + mcdu.onLeftInput[1] = (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + if (_inProgressData.type === PilotWaypointType.LatLon) { + requestAnimationFrame(() => CDUNewWaypoint.ShowPage(mcdu, doneCallback, { ident: _inProgressData.ident })); + } else { + return scratchpadCallback(); + } + } + + if (_inProgressData.type !== undefined) { + return scratchpadCallback(); + } + + if (WaypointEntryUtils.isLatLonFormat(value)) { + try { + const coordinates = WaypointEntryUtils.parseLatLon(value); + requestAnimationFrame(() => + CDUNewWaypoint.ShowPage(mcdu, doneCallback, { + ident: _inProgressData.ident, + type: PilotWaypointType.LatLon, + wp: mcdu.dataManager.createLatLonWaypoint(coordinates, false, _inProgressData.ident).waypoint, + coordinates, + }), + ); + } catch (err) { + if (err instanceof McduMessage) { + mcdu.setScratchpadMessage(err); + } else { + console.error(err); + } + scratchpadCallback(); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + } + }; + + // place/bearing/dist + mcdu.onLeftInput[2] = (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + if (_inProgressData.type === PilotWaypointType.Pbd) { + requestAnimationFrame(() => CDUNewWaypoint.ShowPage(mcdu, doneCallback, { ident: _inProgressData.ident })); + } else { + return scratchpadCallback(); + } + } + + if (_inProgressData.type !== undefined) { + return scratchpadCallback(); + } + + if (WaypointEntryUtils.isPbdFormat(value)) { + try { + WaypointEntryUtils.parsePbd(mcdu, value).then(([place, bearing, distance]) => { + requestAnimationFrame(() => + CDUNewWaypoint.ShowPage(mcdu, doneCallback, { + ident: _inProgressData.ident, + type: PilotWaypointType.Pbd, + wp: mcdu.dataManager.createPlaceBearingDistWaypoint( + place, + bearing, + distance, + false, + _inProgressData.ident, + ).waypoint, + place, + bearing, + distance, + }), + ); + }); + } catch (err) { + if (err instanceof McduMessage) { + mcdu.setScratchpadMessage(err); + } else { + console.error(err); + } + scratchpadCallback(); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + } + }; + + // place-bearing/place-bearing + mcdu.onLeftInput[3] = (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + if (_inProgressData.type === PilotWaypointType.Pbx) { + requestAnimationFrame(() => CDUNewWaypoint.ShowPage(mcdu, doneCallback, { ident: _inProgressData.ident })); + } else { + return scratchpadCallback(); + } + } + + if (_inProgressData.type !== undefined) { + return scratchpadCallback(); + } + + if (WaypointEntryUtils.isPbxFormat(value)) { + try { + WaypointEntryUtils.parsePbx(mcdu, value).then(([place1, bearing1, place2, bearing2]) => { + requestAnimationFrame(() => + CDUNewWaypoint.ShowPage(mcdu, doneCallback, { + ident: _inProgressData.ident, + type: PilotWaypointType.Pbx, + wp: mcdu.dataManager.createPlaceBearingPlaceBearingWaypoint( + place1, + bearing1, + place2, + bearing2, + false, + _inProgressData.ident, + ).waypoint, + place1, + bearing1, + place2, + bearing2, + }), + ); + }); + } catch (err) { + if (err instanceof McduMessage) { + mcdu.setScratchpadMessage(err); + } else { + console.error(err); + } + scratchpadCallback(); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + } + }; + + if (_inProgressData !== undefined) { + mcdu.onRightInput[5] = () => { + let stored; + switch (_inProgressData.type) { + case PilotWaypointType.LatLon: + stored = mcdu.dataManager.createLatLonWaypoint(_inProgressData.coordinates, true, _inProgressData.ident); + break; + case PilotWaypointType.Pbd: + stored = mcdu.dataManager.createPlaceBearingDistWaypoint( + _inProgressData.place, + _inProgressData.bearing, + _inProgressData.distance, + true, + _inProgressData.ident, + ); + break; + case PilotWaypointType.Pbx: + stored = mcdu.dataManager.createPlaceBearingPlaceBearingWaypoint( + _inProgressData.place1, + _inProgressData.bearing1, + _inProgressData.place2, + _inProgressData.bearing2, + true, + _inProgressData.ident, + ); + break; + default: + mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + return; + } + requestAnimationFrame(() => { + if (doneCallback !== undefined) { + doneCallback(stored.waypoint); + } else { + CDUPilotsWaypoint.ShowPage(mcdu, stored.storedIndex); + } + }); + }; + } + + mcdu.onRightInput[4] = () => { + requestAnimationFrame(() => { + if (doneCallback !== undefined) { + doneCallback(undefined); + } else { + CDUPilotsWaypoint.ShowPage(mcdu); + } + }); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PerformancePage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PerformancePage.ts new file mode 100644 index 00000000000..451d3bf998c --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PerformancePage.ts @@ -0,0 +1,1399 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { ApproachUtils, NXUnits, RunwayUtils } from '@flybywiresim/fbw-sdk'; +import { FmgcFlightPhase } from '@shared/flightphase'; +import { CDUStepAltsPage } from './A320_Neo_CDU_StepAltsPage'; +import { NXFictionalMessages, NXSystemMessages } from '../messages/NXSystemMessages'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FmsFormatters } from '../legacy/FmsFormatters'; + +export class CDUPerformancePage { + private static _timer; + private static _lastPhase; + + static ShowPage(mcdu: LegacyFmsPageInterface, _phase = undefined) { + mcdu.activeSystem = 'FMGC'; + + switch (_phase || mcdu.flightPhaseManager.phase) { + case FmgcFlightPhase.Preflight: + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + break; + case FmgcFlightPhase.Takeoff: + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + break; + case FmgcFlightPhase.Climb: + CDUPerformancePage.ShowCLBPage(mcdu); + break; + case FmgcFlightPhase.Cruise: + CDUPerformancePage.ShowCRZPage(mcdu); + break; + case FmgcFlightPhase.Descent: + CDUPerformancePage.ShowDESPage(mcdu); + break; + case FmgcFlightPhase.Approach: + CDUPerformancePage.ShowAPPRPage(mcdu); + break; + case FmgcFlightPhase.GoAround: + CDUPerformancePage.ShowGOAROUNDPage(mcdu); + break; + } + } + static ShowTAKEOFFPage(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.PerformancePageTakeoff; + CDUPerformancePage._timer = 0; + CDUPerformancePage._lastPhase = mcdu.flightPhaseManager.phase; + mcdu.pageUpdate = () => { + CDUPerformancePage._timer++; + if (CDUPerformancePage._timer >= 50) { + if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + CDUPerformancePage.ShowPage(mcdu); + } + } + }; + + // TODO SEC F-PLN + const targetPlan = mcdu.flightPlanService.active; + + let titleColor = 'white'; + if (mcdu.flightPhaseManager.phase === FmgcFlightPhase.Takeoff) { + titleColor = 'green'; + } + + // check if we even have an airport + const hasOrigin = !!targetPlan.originAirport; + + // runway + let runway = ''; + let hasRunway = false; + if (hasOrigin) { + const runwayObj = targetPlan.originRunway; + + if (runwayObj) { + runway = RunwayUtils.runwayString(runwayObj.ident); + hasRunway = true; + } + } + + // v speeds + let v1 = '---'; + let vR = '---'; + let v2 = '---'; + let v1Check = '{small}\xa0\xa0\xa0{end}'; + let vRCheck = '{small}\xa0\xa0\xa0{end}'; + let v2Check = '{small}\xa0\xa0\xa0{end}'; + if (mcdu.flightPhaseManager.phase < FmgcFlightPhase.Takeoff) { + v1 = '{amber}___{end}'; + if (mcdu.unconfirmedV1Speed) { + v1Check = `{small}{cyan}${('' + mcdu.unconfirmedV1Speed).padEnd(3)}{end}{end}`; + } else if (mcdu.v1Speed) { + v1 = `{cyan}${('' + mcdu.v1Speed).padEnd(3)}{end}`; + } + mcdu.onLeftInput[0] = (value, scratchpadCallback) => { + if (value === '') { + if (mcdu.unconfirmedV1Speed) { + mcdu.v1Speed = mcdu.unconfirmedV1Speed; + mcdu.unconfirmedV1Speed = undefined; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + } + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + if (mcdu.trySetV1Speed(value)) { + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + scratchpadCallback(); + } + } + }; + vR = '{amber}___{end}'; + if (mcdu.unconfirmedVRSpeed) { + vRCheck = `{small}{cyan}${('' + mcdu.unconfirmedVRSpeed).padEnd(3)}{end}{end}`; + } else if (mcdu.vRSpeed) { + vR = `{cyan}${('' + mcdu.vRSpeed).padEnd(3)}{end}`; + } + mcdu.onLeftInput[1] = (value, scratchpadCallback) => { + if (value === '') { + if (mcdu.unconfirmedVRSpeed) { + mcdu.vRSpeed = mcdu.unconfirmedVRSpeed; + mcdu.unconfirmedVRSpeed = undefined; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + } + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + if (mcdu.trySetVRSpeed(value)) { + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + scratchpadCallback(); + } + } + }; + v2 = '{amber}___{end}'; + if (mcdu.unconfirmedV2Speed) { + v2Check = `{small}{cyan}${('' + mcdu.unconfirmedV2Speed).padEnd(3)}{end}{end}`; + } else if (mcdu.v2Speed) { + v2 = `{cyan}${('' + mcdu.v2Speed).padEnd(3)}{end}`; + } + mcdu.onLeftInput[2] = (value, scratchpadCallback) => { + if (value === '') { + if (mcdu.unconfirmedV2Speed) { + mcdu.v2Speed = mcdu.unconfirmedV2Speed; + mcdu.unconfirmedV2Speed = undefined; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + } + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + if (mcdu.trySetV2Speed(value)) { + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + scratchpadCallback(); + } + } + }; + } else { + v1 = '\xa0\xa0\xa0'; + vR = '\xa0\xa0\xa0'; + v2 = '\xa0\xa0\xa0'; + if (mcdu.v1Speed) { + v1 = `{green}${('' + mcdu.v1Speed).padEnd(3)}{end}`; + } + if (mcdu.vRSpeed) { + vR = `{green}${('' + mcdu.vRSpeed).padEnd(3)}{end}`; + } + if (mcdu.v2Speed) { + v2 = `{green}${('' + mcdu.v2Speed).padEnd(3)}{end}`; + } + mcdu.onLeftInput[0] = (value, scratchpadCallback) => { + if (value !== '') { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + } + }; + mcdu.onLeftInput[1] = (value, scratchpadCallback) => { + if (value !== '') { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + } + }; + mcdu.onLeftInput[2] = (value, scratchpadCallback) => { + if (value !== '') { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + } + }; + } + + // transition altitude - remains editable during take off + let transAltCell = ''; + if (hasOrigin) { + transAltCell = '[\xa0'.padEnd(4, '\xa0') + ']'; + + const transAlt = targetPlan.performanceData.transitionAltitude; + const transAltitudeIsFromDatabase = targetPlan.performanceData.transitionAltitudeIsFromDatabase; + + if (transAlt !== null) { + transAltCell = `{cyan}${transAlt}{end}`; + if (transAltitudeIsFromDatabase) { + transAltCell += '[s-text]'; + } + } + + mcdu.onLeftInput[3] = (value, scratchpadCallback) => { + if (mcdu.trySetTakeOffTransAltitude(value)) { + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + scratchpadCallback(); + } + }; + } + + // thrust reduction / acceleration altitude + const altitudeColour = hasOrigin + ? mcdu.flightPhaseManager.phase >= FmgcFlightPhase.Takeoff + ? 'green' + : 'cyan' + : 'white'; + + const plan = mcdu.flightPlanService.active; + const thrRed = plan.performanceData.thrustReductionAltitude; + const thrRedPilot = plan.performanceData.thrustReductionAltitudeIsPilotEntered; + const acc = plan.performanceData.accelerationAltitude; + const accPilot = plan.performanceData.accelerationAltitudeIsPilotEntered; + const eoAcc = plan.performanceData.engineOutAccelerationAltitude; + const eoAccPilot = plan.performanceData.engineOutAccelerationAltitudeIsPilotEntered; + + const thrRedAcc = `{${thrRedPilot ? 'big' : 'small'}}${thrRed !== null ? thrRed.toFixed(0).padStart(5, '\xa0') : '-----'}{end}/{${accPilot ? 'big' : 'small'}}${acc !== null ? acc.toFixed(0).padEnd(5, '\xa0') : '-----'}{end}`; + + mcdu.onLeftInput[4] = (value, scratchpadCallback) => { + if (mcdu.trySetThrustReductionAccelerationAltitude(value)) { + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + // eng out acceleration altitude + const engOut = `{${eoAccPilot ? 'big' : 'small'}}${eoAcc !== null ? eoAcc.toFixed(0).padStart(5, '\xa0') : '-----'}{end}`; + mcdu.onRightInput[4] = (value, scratchpadCallback) => { + if (mcdu.trySetEngineOutAcceleration(value)) { + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + // center column + let flpRetrCell = '---'; + let sltRetrCell = '---'; + let cleanCell = '---'; + if (isFinite(mcdu.zeroFuelWeight)) { + const flapSpeed = mcdu.computedVfs; + if (flapSpeed !== 0) { + flpRetrCell = `{green}${flapSpeed.toFixed(0)}{end}`; + } + const slatSpeed = mcdu.computedVss; + if (slatSpeed !== 0) { + sltRetrCell = `{green}${slatSpeed.toFixed(0)}{end}`; + } + const cleanSpeed = mcdu.computedVgd; + if (cleanSpeed !== 0) { + cleanCell = `{green}${cleanSpeed.toFixed(0)}{end}`; + } + } + // takeoff shift + let toShiftCell = '{inop}----{end}\xa0'; + if (hasOrigin && hasRunway) { + toShiftCell = '{inop}{small}[M]{end}[\xa0\xa0]*{end}'; + // TODO store and show TO SHIFT + } + + // flaps / trim horizontal stabilizer + let flapsThs = '[]/[\xa0\xa0\xa0][color]cyan'; + // The following line uses a special Javascript concept that is signed + // zeroes. In Javascript -0 is strictly equal to 0, so for most cases we + // don't care about that difference. But here, we use that fact to show + // the pilot the precise value they entered: DN0.0 or UP0.0. The only + // way to figure that difference out is using Object.is, as + // Object.is(+0, -0) returns false. Alternatively we could use a helper + // variable (yuck) or encode it using a very small, but negative value + // such as -0.001. + const formattedThs = + mcdu.ths !== null + ? mcdu.ths >= 0 && !Object.is(mcdu.ths, -0) + ? `UP${Math.abs(mcdu.ths).toFixed(1)}` + : `DN${Math.abs(mcdu.ths).toFixed(1)}` + : ''; + if (mcdu.flightPhaseManager.phase < FmgcFlightPhase.Takeoff) { + const flaps = mcdu.flaps !== null ? mcdu.flaps : '[]'; + const ths = formattedThs ? formattedThs : '[\xa0\xa0\xa0]'; + flapsThs = `${flaps}/${ths}[color]cyan`; + mcdu.onRightInput[2] = (value, scratchpadCallback) => { + if (mcdu.trySetFlapsTHS(value)) { + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + scratchpadCallback(); + } + }; + } else { + const flaps = mcdu.flaps !== null ? mcdu.flaps : ''; + const ths = formattedThs ? formattedThs : '\xa0\xa0\xa0\xa0\xa0'; + flapsThs = `${flaps}/${ths}[color]green`; + } + + // flex takeoff temperature + let flexTakeOffTempCell = '[\xa0\xa0]°[color]cyan'; + if (mcdu.flightPhaseManager.phase < FmgcFlightPhase.Takeoff) { + if (isFinite(mcdu.perfTOTemp)) { + if (mcdu._toFlexChecked) { + flexTakeOffTempCell = `${mcdu.perfTOTemp.toFixed(0)}°[color]cyan`; + } else { + flexTakeOffTempCell = `{small}${mcdu.perfTOTemp.toFixed(0)}{end}${flexTakeOffTempCell}[color]cyan`; + } + } + mcdu.onRightInput[3] = (value, scratchpadCallback) => { + if (mcdu._toFlexChecked) { + if (mcdu.setPerfTOFlexTemp(value)) { + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + scratchpadCallback(); + } + } else { + if (value === '' || mcdu.setPerfTOFlexTemp(value)) { + mcdu._toFlexChecked = true; + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + } else { + scratchpadCallback(); + } + } + }; + } else { + if (isFinite(mcdu.perfTOTemp)) { + flexTakeOffTempCell = `${mcdu.perfTOTemp.toFixed(0)}°[color]green`; + } else { + flexTakeOffTempCell = ''; + } + } + + let next = 'NEXT\xa0'; + let nextPhase = 'PHASE>'; + if ( + (mcdu.unconfirmedV1Speed || mcdu.unconfirmedVRSpeed || mcdu.unconfirmedV2Speed || !mcdu._toFlexChecked) && + mcdu.flightPhaseManager.phase < FmgcFlightPhase.Takeoff + ) { + next = 'CONFIRM\xa0'; + nextPhase = 'TO DATA*'; + mcdu.onRightInput[5] = () => { + mcdu.v1Speed = mcdu.unconfirmedV1Speed ? mcdu.unconfirmedV1Speed : mcdu.v1Speed; + mcdu.vRSpeed = mcdu.unconfirmedVRSpeed ? mcdu.unconfirmedVRSpeed : mcdu.vRSpeed; + mcdu.v2Speed = mcdu.unconfirmedV2Speed ? mcdu.unconfirmedV2Speed : mcdu.v2Speed; + mcdu.unconfirmedV1Speed = undefined; + mcdu.unconfirmedVRSpeed = undefined; + mcdu.unconfirmedV2Speed = undefined; + mcdu._toFlexChecked = true; + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + }; + } else { + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + CDUPerformancePage.ShowCLBPage(mcdu); + }; + } + + mcdu.setTemplate([ + ['TAKE OFF RWY\xa0{green}' + runway.padStart(3, '\xa0') + '{end}[color]' + titleColor], + ['\xa0V1\xa0\xa0FLP RETR', ''], + [v1 + v1Check + '\xa0F=' + flpRetrCell, ''], + ['\xa0VR\xa0\xa0SLT RETR', 'TO SHIFT\xa0'], + [vR + vRCheck + '\xa0S=' + sltRetrCell, toShiftCell], + ['\xa0V2\xa0\xa0\xa0\xa0\xa0CLEAN', 'FLAPS/THS'], + [v2 + v2Check + '\xa0O=' + cleanCell, flapsThs], + ['TRANS ALT', 'FLEX TO TEMP'], + [`{cyan}${transAltCell}{end}`, flexTakeOffTempCell], + ['THR\xa0RED/ACC', 'ENG\xa0OUT\xa0ACC'], + [`{${altitudeColour}}${thrRedAcc}{end}`, `{${altitudeColour}}${engOut}{end}`], + ['\xa0UPLINK[color]inop', next], + [' { + CDUPerformancePage._timer++; + if (CDUPerformancePage._timer >= 100) { + if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { + CDUPerformancePage.ShowCLBPage(mcdu); + } else { + CDUPerformancePage.ShowPage(mcdu); + } + } + }; + + const hasFromToPair = + mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport; // TODO use the right flight plan + const showManagedSpeed = hasFromToPair && mcdu.isCostIndexSet && Number.isFinite(mcdu.costIndex); + const isPhaseActive = mcdu.flightPhaseManager.phase === FmgcFlightPhase.Climb; + const isTakeoffOrClimbActive = isPhaseActive || mcdu.flightPhaseManager.phase === FmgcFlightPhase.Takeoff; + const titleColor = isPhaseActive ? 'green' : 'white'; + const isSelected = + (isPhaseActive && Simplane.getAutoPilotAirspeedSelected()) || + (!isPhaseActive && mcdu.preSelectedClbSpeed !== undefined); + const actModeCell = isSelected ? 'SELECTED' : 'MANAGED'; + const costIndexCell = CDUPerformancePage.formatCostIndexCell(mcdu, hasFromToPair, true); + const canClickManagedSpeed = showManagedSpeed && mcdu.preSelectedClbSpeed !== undefined && !isPhaseActive; + + // Predictions to altitude + const vnavDriver = mcdu.guidanceController.vnavDriver; + + const cruiseAltitude = mcdu.cruiseLevel * 100; + const fcuAltitude = SimVar.GetSimVarValue('AUTOPILOT ALTITUDE LOCK VAR:3', 'feet'); + const altitudeToPredict = + mcdu.perfClbPredToAltitudePilot !== undefined + ? mcdu.perfClbPredToAltitudePilot + : Math.min(cruiseAltitude, fcuAltitude); + + const predToLabel = isTakeoffOrClimbActive ? '\xa0\xa0\xa0\xa0\xa0{small}PRED TO{end}' : ''; + const predToCell = isTakeoffOrClimbActive + ? `${CDUPerformancePage.formatAltitudeOrLevel(altitudeToPredict, mcdu.getOriginTransitionAltitude())}[color]cyan` + : ''; + + let predToDistanceCell = ''; + let predToTimeCell = ''; + + let expeditePredToDistanceCell = ''; + let expeditePredToTimeCell = ''; + + if (isTakeoffOrClimbActive && vnavDriver) { + [predToDistanceCell, predToTimeCell] = CDUPerformancePage.getTimeAndDistancePredictionsFromGeometryProfile( + vnavDriver.ndProfile, + altitudeToPredict, + true, + ); + [expeditePredToDistanceCell, expeditePredToTimeCell] = + CDUPerformancePage.getTimeAndDistancePredictionsFromGeometryProfile( + vnavDriver.expediteProfile, + altitudeToPredict, + true, + true, + ); + } + + let managedSpeedCell = ''; + if (isPhaseActive) { + if (mcdu.managedSpeedTarget === mcdu.managedSpeedClimb) { + managedSpeedCell = `\xa0${mcdu.managedSpeedClimb.toFixed(0)}/${mcdu.managedSpeedClimbMach.toFixed(2).replace('0.', '.')}`; + } else if (mcdu.managedSpeedTargetIsMach) { + managedSpeedCell = `\xa0${mcdu.managedSpeedClimbMach.toFixed(2).replace('0.', '.')}`; + } else { + managedSpeedCell = `\xa0${mcdu.managedSpeedTarget.toFixed(0)}`; + } + } else { + let climbSpeed = Math.min(mcdu.managedSpeedClimb, mcdu.getNavModeSpeedConstraint()); + if ( + mcdu.climbSpeedLimit !== undefined && + SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet') < mcdu.climbSpeedLimitAlt + ) { + climbSpeed = Math.min(climbSpeed, mcdu.climbSpeedLimit); + } + + managedSpeedCell = `${canClickManagedSpeed ? '*' : '\xa0'}${climbSpeed.toFixed(0)}`; + + mcdu.onLeftInput[3] = (value, scratchpadCallback) => { + if (mcdu.trySetPreSelectedClimbSpeed(value)) { + CDUPerformancePage.ShowCLBPage(mcdu); + } else { + scratchpadCallback(); + } + }; + } + const [selectedSpeedTitle, selectedSpeedCell] = CDUPerformancePage.getClbSelectedTitleAndValue( + mcdu, + isPhaseActive, + isSelected, + mcdu.preSelectedClbSpeed, + ); + + if (hasFromToPair) { + mcdu.onLeftInput[1] = (value, scratchpadCallback) => { + if (mcdu.tryUpdateCostIndex(value)) { + CDUPerformancePage.ShowCLBPage(mcdu); + } else { + scratchpadCallback(); + } + }; + } + + if (canClickManagedSpeed) { + mcdu.onLeftInput[2] = (_, scratchpadCallback) => { + if (mcdu.trySetPreSelectedClimbSpeed(Keypad.clrValue)) { + CDUPerformancePage.ShowCLBPage(mcdu); + } + + scratchpadCallback(); + }; + } + + if (isTakeoffOrClimbActive) { + mcdu.onRightInput[1] = (value, scratchpadCallback) => { + if (mcdu.trySetPerfClbPredToAltitude(value)) { + CDUPerformancePage.ShowCLBPage(mcdu); + } else { + scratchpadCallback(); + } + }; + } + + const [toUtcLabel, toDistLabel] = isTakeoffOrClimbActive ? ['\xa0UTC', 'DIST'] : ['', '']; + + const bottomRowLabels = ['\xa0PREV', 'NEXT\xa0']; + const bottomRowCells = ['']; + mcdu.leftInputDelay[5] = () => mcdu.getDelaySwitchPage(); + if (isPhaseActive) { + if (confirmAppr) { + bottomRowLabels[0] = '\xa0CONFIRM[color]amber'; + bottomRowCells[0] = '*APPR PHASE[color]amber'; + mcdu.onLeftInput[5] = async () => { + if (mcdu.flightPhaseManager.tryGoInApproachPhase()) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } + }; + } else { + bottomRowLabels[0] = '\xa0ACTIVATE[color]cyan'; + bottomRowCells[0] = '{APPR PHASE[color]cyan'; + mcdu.onLeftInput[5] = () => { + CDUPerformancePage.ShowCLBPage(mcdu, true); + }; + } + } else { + mcdu.onLeftInput[5] = () => { + CDUPerformancePage.ShowTAKEOFFPage(mcdu); + }; + } + + mcdu.rightInputDelay[5] = () => mcdu.getDelaySwitchPage(); + mcdu.onRightInput[5] = () => { + CDUPerformancePage.ShowCRZPage(mcdu); + }; + mcdu.setTemplate([ + [`\xa0CLB[color]${titleColor}`], + ['ACT MODE'], + [`${actModeCell}[color]green`], + ['CI'], + [costIndexCell, predToCell, predToLabel], + ['MANAGED', toDistLabel, toUtcLabel], + [ + `{small}${showManagedSpeed ? managedSpeedCell : '\xa0---/---'}{end}[color]${showManagedSpeed ? 'green' : 'white'}`, + !isSelected ? predToDistanceCell : '', + !isSelected ? predToTimeCell : '', + ], + [selectedSpeedTitle], + [selectedSpeedCell, isSelected ? predToDistanceCell : '', isSelected ? predToTimeCell : ''], + [''], + isPhaseActive ? ['{small}EXPEDITE{end}[color]green', expeditePredToDistanceCell, expeditePredToTimeCell] : [''], + bottomRowLabels, + bottomRowCells, + ]); + } + + static ShowCRZPage(mcdu: LegacyFmsPageInterface, confirmAppr = false) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.PerformancePageCrz; + CDUPerformancePage._timer = 0; + CDUPerformancePage._lastPhase = mcdu.flightPhaseManager.phase; + mcdu.pageUpdate = () => { + CDUPerformancePage._timer++; + if (CDUPerformancePage._timer >= 100) { + if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { + CDUPerformancePage.ShowCRZPage(mcdu); + } else { + CDUPerformancePage.ShowPage(mcdu); + } + } + }; + + const hasFromToPair = + mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport; // TODO use the right flight plan + const isPhaseActive = mcdu.flightPhaseManager.phase === FmgcFlightPhase.Cruise; + const titleColor = isPhaseActive ? 'green' : 'white'; + const isSelected = + (isPhaseActive && Simplane.getAutoPilotAirspeedSelected()) || + (!isPhaseActive && mcdu.preSelectedCrzSpeed !== undefined); + const isFlying = mcdu.flightPhaseManager.phase >= FmgcFlightPhase.Takeoff; + const actModeCell = isSelected ? 'SELECTED' : 'MANAGED'; + const costIndexCell = CDUPerformancePage.formatCostIndexCell(mcdu, hasFromToPair, true); + + // TODO: Figure out correct condition + const showManagedSpeed = hasFromToPair && mcdu.isCostIndexSet && Number.isFinite(mcdu.costIndex); + const canClickManagedSpeed = showManagedSpeed && mcdu.preSelectedCrzSpeed !== undefined && !isPhaseActive; + let managedSpeedCell = '{small}\xa0---/---{end}[color]white'; + if ( + showManagedSpeed && + mcdu.cruiseLevel && + Number.isFinite(mcdu.managedSpeedCruise) && + Number.isFinite(mcdu.managedSpeedCruiseMach) + ) { + const shouldShowCruiseMach = mcdu.cruiseLevel > 250; + managedSpeedCell = `{small}${canClickManagedSpeed ? '*' : '\xa0'}${shouldShowCruiseMach ? mcdu.managedSpeedCruiseMach.toFixed(2).replace('0.', '.') : mcdu.managedSpeedCruise.toFixed(0)}{end}[color]green`; + } + const preselTitle = isPhaseActive ? '' : 'PRESEL'; + let preselCell = ''; + if (!isPhaseActive) { + const hasPreselectedSpeedOrMach = mcdu.preSelectedCrzSpeed !== undefined; + if (hasPreselectedSpeedOrMach) { + preselCell = `\xa0${mcdu.preSelectedCrzSpeed < 1 ? mcdu.preSelectedCrzSpeed.toFixed(2).replace('0.', '.') : mcdu.preSelectedCrzSpeed.toFixed(0)}[color]cyan`; + } else { + preselCell = '{small}*{end}[ ][color]cyan'; + } + } + + if (hasFromToPair) { + mcdu.onLeftInput[1] = (value, scratchpadCallback) => { + if (mcdu.tryUpdateCostIndex(value)) { + CDUPerformancePage.ShowCRZPage(mcdu); + } else { + scratchpadCallback(); + } + }; + } + + const timeLabel = isFlying ? '\xa0UTC' : 'TIME'; + + const [destEfobCell, destTimeCell] = CDUPerformancePage.formatDestEfobAndTime(mcdu, isFlying); + const [toUtcLabel, toDistLabel] = isFlying ? ['\xa0UTC', 'DIST'] : ['', '']; + const [toReasonCell, toDistCell, toTimeCell] = isFlying + ? CDUPerformancePage.formatToReasonDistanceAndTime(mcdu) + : ['', '', '']; + const desCabinRateCell = '{small}-350{end}'; + const shouldShowStepAltsOption = + mcdu.cruiseLevel && + (mcdu.flightPhaseManager.phase < FmgcFlightPhase.Descent || + mcdu.flightPhaseManager.phase > FmgcFlightPhase.GoAround); + + const bottomRowLabels = ['\xa0PREV', 'NEXT\xa0']; + const bottomRowCells = ['']; + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + if (isPhaseActive) { + if (confirmAppr) { + bottomRowLabels[0] = '\xa0CONFIRM[color]amber'; + bottomRowCells[0] = '*APPR PHASE[color]amber'; + mcdu.onLeftInput[5] = async () => { + if (mcdu.flightPhaseManager.tryGoInApproachPhase()) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } + }; + } else { + bottomRowLabels[0] = '\xa0ACTIVATE[color]cyan'; + bottomRowCells[0] = '{APPR PHASE[color]cyan'; + mcdu.onLeftInput[5] = () => { + CDUPerformancePage.ShowCRZPage(mcdu, true); + }; + } + } else { + mcdu.onLeftInput[3] = (value, scratchpadCallback) => { + if (mcdu.trySetPreSelectedCruiseSpeed(value)) { + CDUPerformancePage.ShowCRZPage(mcdu); + } else { + scratchpadCallback(); + } + }; + mcdu.onLeftInput[5] = () => { + CDUPerformancePage.ShowCLBPage(mcdu); + }; + } + if (canClickManagedSpeed) { + mcdu.onLeftInput[2] = (_, scratchpadCallback) => { + if (mcdu.trySetPreSelectedCruiseSpeed(Keypad.clrValue)) { + CDUPerformancePage.ShowCRZPage(mcdu); + } + + scratchpadCallback(); + }; + } + mcdu.onRightInput[3] = () => { + // DES CABIN RATE + mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + }; + if (shouldShowStepAltsOption) { + CDUStepAltsPage.Return = () => { + CDUPerformancePage.ShowCRZPage(mcdu, false); + }; + mcdu.onRightInput[4] = () => { + CDUStepAltsPage.ShowPage(mcdu); + }; + } + mcdu.rightInputDelay[5] = () => mcdu.getDelaySwitchPage(); + mcdu.onRightInput[5] = () => { + CDUPerformancePage.ShowDESPage(mcdu); + }; + mcdu.setTemplate([ + [`\xa0CRZ[color]${titleColor}`], + ['ACT MODE', 'DEST EFOB', timeLabel], + [`${actModeCell}[color]green`, destEfobCell, destTimeCell], + ['CI'], + [costIndexCell, toReasonCell], + ['MANAGED', toDistLabel, toUtcLabel], + [managedSpeedCell, toDistCell, toTimeCell], + [preselTitle, 'DES CABIN RATE'], + [preselCell, `\xa0{cyan}${desCabinRateCell}{end}{white}{small}FT/MN{end}{end}`], + [''], + ['', shouldShowStepAltsOption ? 'STEP ALTS>' : ''], + bottomRowLabels, + bottomRowCells, + ]); + } + + static ShowDESPage(mcdu: LegacyFmsPageInterface, confirmAppr = false) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.PerformancePageDes; + CDUPerformancePage._timer = 0; + CDUPerformancePage._lastPhase = mcdu.flightPhaseManager.phase; + mcdu.pageUpdate = () => { + CDUPerformancePage._timer++; + if (CDUPerformancePage._timer >= 100) { + if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { + CDUPerformancePage.ShowDESPage(mcdu); + } else { + CDUPerformancePage.ShowPage(mcdu); + } + } + }; + + const hasFromToPair = + mcdu.flightPlanService.active.originAirport && mcdu.flightPlanService.active.destinationAirport; // TODO use the right flight plan + const isPhaseActive = mcdu.flightPhaseManager.phase === FmgcFlightPhase.Descent; + const titleColor = isPhaseActive ? 'green' : 'white'; + const isFlying = mcdu.flightPhaseManager.phase >= FmgcFlightPhase.Takeoff; + const isSelected = isPhaseActive && Simplane.getAutoPilotAirspeedSelected(); + const actModeCell = isSelected ? 'SELECTED' : 'MANAGED'; + + // Predictions to altitude + const vnavDriver = mcdu.guidanceController.vnavDriver; + const fcuAltitude = SimVar.GetSimVarValue('AUTOPILOT ALTITUDE LOCK VAR:3', 'feet'); + const altitudeToPredict = + mcdu.perfDesPredToAltitudePilot !== undefined ? mcdu.perfDesPredToAltitudePilot : fcuAltitude; + + const predToLabel = isPhaseActive ? '\xa0\xa0\xa0\xa0\xa0{small}PRED TO{end}' : ''; + const predToCell = isPhaseActive + ? `${CDUPerformancePage.formatAltitudeOrLevel(altitudeToPredict, mcdu.getDestinationTransitionLevel() * 100)}[color]cyan` + : ''; + + let predToDistanceCell = ''; + let predToTimeCell = ''; + + if (isPhaseActive && vnavDriver) { + [predToDistanceCell, predToTimeCell] = CDUPerformancePage.getTimeAndDistancePredictionsFromGeometryProfile( + vnavDriver.ndProfile, + altitudeToPredict, + false, + ); + } + + const costIndexCell = CDUPerformancePage.formatCostIndexCell(mcdu, hasFromToPair, !isPhaseActive); + + const econDesPilotEntered = mcdu.managedSpeedDescendPilot !== undefined; + const econDes = econDesPilotEntered ? mcdu.managedSpeedDescendPilot : mcdu.managedSpeedDescend; + const econDesMachPilotEntered = mcdu.managedSpeedDescendMachPilot !== undefined; + const econDesMach = econDesMachPilotEntered ? mcdu.managedSpeedDescendMachPilot : mcdu.managedSpeedDescendMach; + + // TODO: Figure out correct condition + const showManagedSpeed = + hasFromToPair && + mcdu.isCostIndexSet && + Number.isFinite(mcdu.costIndex) && + econDesMach !== undefined && + econDes !== undefined; + const managedDescentSpeedCellMach = `{${econDesMachPilotEntered ? 'big' : 'small'}}${econDesMach.toFixed(2).replace('0.', '.')}{end}`; + const managedDescentSpeedCellSpeed = `{${econDesPilotEntered ? 'big' : 'small'}}/${econDes.toFixed(0)}{end}`; + + const managedDescentSpeedCell = showManagedSpeed + ? `\xa0${managedDescentSpeedCellMach}${managedDescentSpeedCellSpeed}[color]cyan` + : '\xa0{small}---/---{end}[color]white'; + + const [selectedSpeedTitle, selectedSpeedCell] = CDUPerformancePage.getDesSelectedTitleAndValue( + mcdu, + isPhaseActive, + isSelected, + ); + const timeLabel = isFlying ? '\xa0UTC' : 'TIME'; + const [destEfobCell, destTimeCell] = CDUPerformancePage.formatDestEfobAndTime(mcdu, isFlying); + const [toUtcLabel, toDistLabel] = isPhaseActive ? ['\xa0UTC', 'DIST'] : ['', '']; + + const bottomRowLabels = ['\xa0PREV', 'NEXT\xa0']; + const bottomRowCells = ['']; + mcdu.leftInputDelay[5] = () => mcdu.getDelaySwitchPage(); + if (isPhaseActive) { + mcdu.onRightInput[1] = (value, scratchpadCallback) => { + if (mcdu.trySetPerfDesPredToAltitude(value)) { + CDUPerformancePage.ShowDESPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + if (confirmAppr) { + bottomRowLabels[0] = '\xa0CONFIRM[color]amber'; + bottomRowCells[0] = '*APPR PHASE[color]amber'; + mcdu.onLeftInput[5] = async () => { + if (mcdu.flightPhaseManager.tryGoInApproachPhase()) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } + }; + } else { + bottomRowLabels[0] = '\xa0ACTIVATE[color]cyan'; + bottomRowCells[0] = '{APPR PHASE[color]cyan'; + mcdu.onLeftInput[5] = () => { + CDUPerformancePage.ShowDESPage(mcdu, true); + }; + } + } else { + mcdu.onLeftInput[5] = () => { + CDUPerformancePage.ShowCRZPage(mcdu); + }; + } + // Can only modify cost index until the phase is active + if (hasFromToPair && !isPhaseActive) { + mcdu.onLeftInput[1] = (value, scratchpadCallback) => { + if (mcdu.tryUpdateCostIndex(value)) { + CDUPerformancePage.ShowDESPage(mcdu); + } else { + scratchpadCallback(); + } + }; + } + + if (showManagedSpeed) { + mcdu.onLeftInput[2] = (value, scratchpadCallback) => { + if (mcdu.trySetManagedDescentSpeed(value)) { + CDUPerformancePage.ShowDESPage(mcdu); + } else { + scratchpadCallback(); + } + }; + } + + mcdu.rightInputDelay[5] = () => mcdu.getDelaySwitchPage(); + mcdu.onRightInput[5] = () => { + CDUPerformancePage.ShowAPPRPage(mcdu); + }; + mcdu.setTemplate([ + [`\xa0DES[color]${titleColor}`], + ['ACT MODE', 'DEST EFOB', timeLabel], + [`${actModeCell}[color]green`, destEfobCell, destTimeCell], + ['CI'], + [costIndexCell, predToCell, predToLabel], + ['MANAGED', toDistLabel, toUtcLabel], + [managedDescentSpeedCell, !isSelected ? predToDistanceCell : '', !isSelected ? predToTimeCell : ''], + [selectedSpeedTitle], + [selectedSpeedCell, isSelected ? predToDistanceCell : '', isSelected ? predToTimeCell : ''], + [''], + [''], + bottomRowLabels, + bottomRowCells, + ]); + } + + static ShowAPPRPage(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.PerformancePageAppr; + + const plan = mcdu.flightPlanService.active; + + CDUPerformancePage._timer = 0; + CDUPerformancePage._lastPhase = mcdu.flightPhaseManager.phase; + mcdu.pageUpdate = () => { + CDUPerformancePage._timer++; + if (CDUPerformancePage._timer >= 100) { + if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } + } + }; + + const distanceToDest = mcdu.getDistanceToDestination(); + const closeToDest = distanceToDest !== undefined && distanceToDest <= 180; + + let qnhCell = '[\xa0\xa0][color]cyan'; + if (isFinite(mcdu.perfApprQNH)) { + if (mcdu.perfApprQNH < 500) { + qnhCell = mcdu.perfApprQNH.toFixed(2) + '[color]cyan'; + } else { + qnhCell = mcdu.perfApprQNH.toFixed(0) + '[color]cyan'; + } + } else if (closeToDest) { + qnhCell = '____[color]amber'; + } + mcdu.onLeftInput[0] = (value, scratchpadCallback) => { + if (mcdu.setPerfApprQNH(value)) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + let tempCell = '{cyan}[\xa0]°{end}'; + if (isFinite(mcdu.perfApprTemp)) { + tempCell = + '{cyan}' + + (mcdu.perfApprTemp >= 0 ? '+' : '-') + + ('' + Math.abs(mcdu.perfApprTemp).toFixed(0)).padStart(2).replace(/ /g, '\xa0') + + '°{end}'; + } else if (closeToDest) { + tempCell = '{amber}___°{end}'; + } + mcdu.onLeftInput[1] = (value, scratchpadCallback) => { + if (mcdu.setPerfApprTemp(value)) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } else { + scratchpadCallback(); + } + }; + let magWindHeadingCell = '[\xa0]'; + if (isFinite(mcdu.perfApprWindHeading)) { + magWindHeadingCell = ('' + mcdu.perfApprWindHeading.toFixed(0)).padStart(3, '0'); + } + let magWindSpeedCell = '[\xa0]'; + if (isFinite(mcdu.perfApprWindSpeed)) { + magWindSpeedCell = mcdu.perfApprWindSpeed.toFixed(0).padStart(3, '0'); + } + mcdu.onLeftInput[2] = (value, scratchpadCallback) => { + if (mcdu.setPerfApprWind(value)) { + mcdu.updateTowerHeadwind(); + mcdu.updatePerfSpeeds(); + CDUPerformancePage.ShowAPPRPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + let transAltCell = '\xa0'.repeat(5); + const hasDestination = !!plan.destinationAirport; + + if (hasDestination) { + const transitionLevel = plan.performanceData.transitionLevel; + + if (transitionLevel !== null) { + transAltCell = (transitionLevel * 100).toFixed(0).padEnd(5, '\xa0'); + + if (plan.performanceData.transitionLevelIsFromDatabase) { + transAltCell = `{small}${transAltCell}{end}`; + } + } else { + transAltCell = '[\xa0]'.padEnd(5, '\xa0'); + } + } + mcdu.onLeftInput[3] = (value, scratchpadCallback) => { + if (mcdu.setPerfApprTransAlt(value)) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + let vappCell = '---'; + let vlsCell = '---'; + let flpRetrCell = '---'; + let sltRetrCell = '---'; + let cleanCell = '---'; + if (isFinite(mcdu.zeroFuelWeight) && mcdu.approachSpeeds && mcdu.approachSpeeds.valid) { + vappCell = `{cyan}{small}${mcdu.approachSpeeds.vapp.toFixed(0)}{end}{end}`; + vlsCell = `{green}${mcdu.approachSpeeds.vls.toFixed(0)}{end}`; + flpRetrCell = `{green}${mcdu.approachSpeeds.f.toFixed(0)}{end}`; + sltRetrCell = `{green}${mcdu.approachSpeeds.s.toFixed(0)}{end}`; + cleanCell = `{green}${mcdu.approachSpeeds.gd.toFixed(0)}{end}`; + } + if (isFinite(mcdu.vApp)) { + // pilot override + vappCell = `{cyan}${mcdu.vApp.toFixed(0).padStart(3, '\xa0')}{end}`; + } + mcdu.onLeftInput[4] = (value, scratchpadCallback) => { + if (mcdu.setPerfApprVApp(value)) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } else { + scratchpadCallback(); + } + }; + mcdu.onRightInput[4] = () => { + mcdu.setPerfApprFlaps3(!mcdu.perfApprFlaps3); + mcdu.updatePerfSpeeds(); + CDUPerformancePage.ShowAPPRPage(mcdu); + }; + + let baroCell = '[\xa0\xa0\xa0]'; + if (mcdu.perfApprMDA !== null) { + baroCell = mcdu.perfApprMDA.toFixed(0); + } + mcdu.onRightInput[1] = (value, scratchpadCallback) => { + if (mcdu.setPerfApprMDA(value) && mcdu.setPerfApprDH(Keypad.clrValue)) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + const approach = plan.approach; + const isILS = approach && approach.type === 5; + let radioLabel = ''; + let radioCell = ''; + if (isILS) { + radioLabel = 'RADIO'; + if (typeof mcdu.perfApprDH === 'number') { + radioCell = mcdu.perfApprDH.toFixed(0); + } else if (mcdu.perfApprDH === 'NO DH') { + radioCell = 'NO DH'; + } else { + radioCell = '[\xa0]'; + } + mcdu.onRightInput[2] = (value, scratchpadCallback) => { + if (mcdu.setPerfApprDH(value) && mcdu.setPerfApprMDA(Keypad.clrValue)) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } else { + scratchpadCallback(); + } + }; + } + + const bottomRowLabels = ['\xa0PREV', 'NEXT\xa0']; + const bottomRowCells = ['']; + let titleColor = 'white'; + if (mcdu.flightPhaseManager.phase === FmgcFlightPhase.Approach) { + bottomRowLabels[0] = ''; + bottomRowCells[0] = ''; + titleColor = 'green'; + } else { + if (mcdu.flightPhaseManager.phase === FmgcFlightPhase.GoAround) { + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUPerformancePage.ShowGOAROUNDPage(mcdu); + }; + } else { + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUPerformancePage.ShowDESPage(mcdu); + }; + } + } + if (mcdu.flightPhaseManager.phase === FmgcFlightPhase.GoAround) { + bottomRowLabels[1] = ''; + bottomRowCells[1] = ''; + } else { + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + CDUPerformancePage.ShowGOAROUNDPage(mcdu); + }; + } + + let titleCell = `${'\xa0'.repeat(5)}{${titleColor}}APPR{end}\xa0`; + if (approach) { + const approachName = ApproachUtils.shortApproachName(approach); + titleCell += `{green}${approachName}{end}` + '\xa0'.repeat(24 - 10 - approachName.length); + } else { + titleCell += '\xa0'.repeat(24 - 10); + } + + mcdu.setTemplate([ + /* t */ [titleCell], + /* 1l */ ['QNH'], + /* 1L */ [qnhCell], + /* 2l */ ['TEMP', 'BARO'], + /* 2L */ [`${tempCell}${'\xa0'.repeat(6)}O=${cleanCell}`, baroCell + '[color]cyan'], + /* 3l */ ['MAG WIND', radioLabel], + /* 3L */ [ + `{cyan}${magWindHeadingCell}°/${magWindSpeedCell}{end}\xa0\xa0S=${sltRetrCell}`, + radioCell + '[color]cyan', + ], + /* 4l */ ['TRANS ALT'], + /* 4L */ [`{cyan}${transAltCell}{end}${'\xa0'.repeat(5)}F=${flpRetrCell}`], + /* 5l */ ['VAPP\xa0\xa0\xa0VLS', 'LDG CONF\xa0'], + /* 5L */ [ + `${vappCell}${'\xa0'.repeat(4)}${vlsCell}`, + mcdu.perfApprFlaps3 ? '{cyan}CONF3/{end}{small}FULL{end}*' : '{cyan}FULL/{end}{small}CONF3{end}*', + ], + /* 6l */ bottomRowLabels, + /* 6L */ bottomRowCells, + ]); + } + + static ShowGOAROUNDPage(mcdu: LegacyFmsPageInterface, confirmAppr = false) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.PerformancePageGoAround; + CDUPerformancePage._timer = 0; + CDUPerformancePage._lastPhase = mcdu.flightPhaseManager.phase; + mcdu.pageUpdate = () => { + CDUPerformancePage._timer++; + if (CDUPerformancePage._timer >= 100) { + if (mcdu.flightPhaseManager.phase === CDUPerformancePage._lastPhase) { + CDUPerformancePage.ShowGOAROUNDPage(mcdu); + } else { + CDUPerformancePage.ShowPage(mcdu); + } + } + }; + + const haveDestination = mcdu.flightPlanService.active.destinationAirport !== undefined; + + const titleColor = mcdu.flightPhaseManager.phase === FmgcFlightPhase.GoAround ? 'green' : 'white'; + const altitudeColour = haveDestination + ? mcdu.flightPhaseManager.phase >= FmgcFlightPhase.GoAround + ? 'green' + : 'cyan' + : 'white'; + + const plan = mcdu.flightPlanService.active; + const thrRed = plan.performanceData.missedThrustReductionAltitude; + const thrRedPilot = plan.performanceData.missedThrustReductionAltitudeIsPilotEntered; + const acc = plan.performanceData.missedAccelerationAltitude; + const accPilot = plan.performanceData.missedAccelerationAltitudeIsPilotEntered; + const eoAcc = plan.performanceData.missedEngineOutAccelerationAltitude; + const eoAccPilot = plan.performanceData.missedEngineOutAccelerationAltitudeIsPilotEntered; + + const thrRedAcc = `{${thrRedPilot ? 'big' : 'small'}}${thrRed !== null ? thrRed.toFixed(0).padStart(5, '\xa0') : '-----'}{end}/{${accPilot ? 'big' : 'small'}}${acc !== null ? acc.toFixed(0).padEnd(5, '\xa0') : '-----'}{end}`; + const engOut = `{${eoAccPilot ? 'big' : 'small'}}${eoAcc !== null ? eoAcc.toFixed(0).padStart(5, '\xa0') : '-----'}{end}`; + + mcdu.onLeftInput[4] = (value, scratchpadCallback) => { + if (mcdu.trySetThrustReductionAccelerationAltitudeGoaround(value)) { + CDUPerformancePage.ShowGOAROUNDPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + mcdu.onRightInput[4] = (value, scratchpadCallback) => { + if (mcdu.trySetEngineOutAccelerationAltitudeGoaround(value)) { + CDUPerformancePage.ShowGOAROUNDPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + let flpRetrCell = '---'; + let sltRetrCell = '---'; + let cleanCell = '---'; + if (isFinite(mcdu.zeroFuelWeight)) { + const flapSpeed = mcdu.computedVfs; + if (isFinite(flapSpeed)) { + flpRetrCell = `{green}${flapSpeed.toFixed(0).padEnd(3, '\xa0')}{end}`; + } + const slatSpeed = mcdu.computedVss; + if (isFinite(slatSpeed)) { + sltRetrCell = `{green}${slatSpeed.toFixed(0).padEnd(3, '\xa0')}{end}`; + } + const cleanSpeed = mcdu.computedVgd; + if (isFinite(cleanSpeed)) { + cleanCell = `{green}${cleanSpeed.toFixed(0).padEnd(3, '\xa0')}{end}`; + } + } + + const bottomRowLabels = [ + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0', + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0', + ]; + const bottomRowCells = [ + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0', + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0', + ]; + if (mcdu.flightPhaseManager.phase === FmgcFlightPhase.GoAround) { + if (confirmAppr) { + bottomRowLabels[0] = '\xa0{amber}CONFIRM{amber}\xa0\xa0\xa0\xa0'; + bottomRowCells[0] = '{amber}*APPR\xa0PHASE{end}\xa0'; + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = async () => { + if (mcdu.flightPhaseManager.tryGoInApproachPhase()) { + CDUPerformancePage.ShowAPPRPage(mcdu); + } + }; + } else { + bottomRowLabels[0] = '\xa0{cyan}ACTIVATE{end}\xa0\xa0\xa0'; + bottomRowCells[0] = '{cyan}{APPR\xa0PHASE{end}\xa0'; + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUPerformancePage.ShowGOAROUNDPage(mcdu, true); + }; + } + bottomRowLabels[1] = '\xa0\xa0\xa0\xa0\xa0\xa0\xa0{white}NEXT{end}\xa0'; + bottomRowCells[1] = '\xa0\xa0\xa0\xa0\xa0\xa0{white}PHASE>{end}'; + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + CDUPerformancePage.ShowAPPRPage(mcdu); + }; + } else { + bottomRowLabels[0] = '\xa0{white}PREV{end}\xa0\xa0\xa0\xa0\xa0\xa0\xa0'; + bottomRowCells[0] = '{white} { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUPerformancePage.ShowAPPRPage(mcdu); + }; + } + + mcdu.setTemplate([ + [`{${titleColor}}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0GO\xa0AROUND\xa0\xa0\xa0\xa0\xa0\xa0{end}`], + ['', '', '\xa0\xa0\xa0\xa0\xa0FLP\xa0RETR\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0'], + ['', '', `\xa0\xa0\xa0\xa0\xa0\xa0\xa0F=${flpRetrCell}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0`], + ['', '', '\xa0\xa0\xa0\xa0\xa0SLT RETR\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0'], + ['', '', `\xa0\xa0\xa0\xa0\xa0\xa0\xa0S=${sltRetrCell}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0`], + ['', '', '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0CLEAN\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0'], + ['', '', `\xa0\xa0\xa0\xa0\xa0\xa0\xa0O=${cleanCell}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0`], + [''], + [''], + ['', '', 'THR\xa0RED/ACC\xa0\xa0ENG\xa0OUT\xa0ACC'], + ['', '', `{${altitudeColour}}${thrRedAcc}\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0${engOut}{end}`], + ['', '', bottomRowLabels.join('')], + ['', '', bottomRowCells.join('')], + ]); + } + + static getClbSelectedTitleAndValue(mcdu: LegacyFmsPageInterface, isPhaseActive, isSelected, preSel) { + if (!isPhaseActive) { + return ['PRESEL', (isFinite(preSel) ? '\xa0' + preSel : '*[ ]') + '[color]cyan']; + } + + if (!isSelected) { + return ['', '']; + } + + const aircraftAltitude = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); + const selectedSpdMach = SimVar.GetSimVarValue('L:A32NX_AUTOPILOT_SPEED_SELECTED', 'number'); + + if (selectedSpdMach < 1) { + return ['SELECTED', `\xa0${selectedSpdMach.toFixed(2).replace('0.', '.')}[color]green`]; + } else { + const machAtManualCrossoverAlt = mcdu.casToMachManualCrossoverCurve.evaluate(selectedSpdMach); + const manualCrossoverAltitude = mcdu.computeManualCrossoverAltitude(machAtManualCrossoverAlt); + const shouldShowMach = + aircraftAltitude < manualCrossoverAltitude && + (!mcdu.cruiseLevel || manualCrossoverAltitude < mcdu.cruiseLevel * 100); + + return [ + 'SELECTED', + `\xa0${Math.round(selectedSpdMach)}${shouldShowMach ? '{small}/' + machAtManualCrossoverAlt.toFixed(2).replace('0.', '.') + '{end}' : ''}[color]green`, + ]; + } + } + + static getDesSelectedTitleAndValue(mcdu: LegacyFmsPageInterface, isPhaseActive, isSelected) { + if (!isPhaseActive || !isSelected) { + return ['', '']; + } + + const aircraftAltitude = SimVar.GetSimVarValue('INDICATED ALTITUDE', 'feet'); + const selectedSpdMach = SimVar.GetSimVarValue('L:A32NX_AUTOPILOT_SPEED_SELECTED', 'number'); + + if (selectedSpdMach < 1) { + const casAtCrossoverAltitude = mcdu.machToCasManualCrossoverCurve.evaluate(selectedSpdMach); + const manualCrossoverAltitude = mcdu.computeManualCrossoverAltitude(selectedSpdMach); + const shouldShowCas = aircraftAltitude > manualCrossoverAltitude; + + return [ + 'SELECTED', + `\xa0${shouldShowCas ? '{small}' + Math.round(casAtCrossoverAltitude) + '/{end}' : ''}${selectedSpdMach.toFixed(2).replace('0.', '.')}[color]green`, + ]; + } else { + return ['SELECTED', `\xa0${Math.round(selectedSpdMach)}[color]green`]; + } + } + + static formatAltitudeOrLevel(altitudeToFormat, transitionAltitude) { + if (transitionAltitude >= 100 && altitudeToFormat > transitionAltitude) { + return `FL${(altitudeToFormat / 100).toFixed(0).toString().padStart(3, '0')}`; + } + + return (10 * Math.round(altitudeToFormat / 10)).toFixed(0).toString().padStart(5, '\xa0'); + } + + static getTimeAndDistancePredictionsFromGeometryProfile( + geometryProfile, + altitudeToPredict, + isClimbVsDescent, + printSmall = false, + ) { + let predToDistanceCell = '---'; + let predToTimeCell = '----'; + + if (!geometryProfile || !geometryProfile.isReadyToDisplay) { + return [predToTimeCell, predToDistanceCell]; + } + + const predictions = isClimbVsDescent + ? geometryProfile.computeClimbPredictionToAltitude(altitudeToPredict) + : geometryProfile.computeDescentPredictionToAltitude(altitudeToPredict); + + if (predictions) { + if (Number.isFinite(predictions.distanceFromStart)) { + if (printSmall) { + predToDistanceCell = '{small}' + predictions.distanceFromStart.toFixed(0) + '{end}[color]green'; + } else { + predToDistanceCell = predictions.distanceFromStart.toFixed(0) + '[color]green'; + } + } + + if (Number.isFinite(predictions.secondsFromPresent)) { + const utcTime = SimVar.GetGlobalVarValue('ZULU TIME', 'seconds'); + const predToTimeCellText = FmsFormatters.secondsToUTC(utcTime + predictions.secondsFromPresent); + + if (printSmall) { + predToTimeCell = '{small}' + predToTimeCellText + '{end}[color]green'; + } else { + predToTimeCell = predToTimeCellText + '[color]green'; + } + } + } + + return [predToDistanceCell, predToTimeCell]; + } + + static formatDestEfobAndTime(mcdu: LegacyFmsPageInterface, isFlying) { + const destinationPrediction = mcdu.guidanceController.vnavDriver.getDestinationPrediction(); + + let destEfobCell = '---.-'; + let destTimeCell = '----'; + + if (destinationPrediction) { + if (Number.isFinite(destinationPrediction.estimatedFuelOnBoard)) { + destEfobCell = + (NXUnits.poundsToUser(destinationPrediction.estimatedFuelOnBoard) / 1000).toFixed(1) + '[color]green'; + } + + if (Number.isFinite(destinationPrediction.secondsFromPresent)) { + const utcTime = SimVar.GetGlobalVarValue('ZULU TIME', 'seconds'); + + const predToTimeCellText = isFlying + ? FmsFormatters.secondsToUTC(utcTime + destinationPrediction.secondsFromPresent) + : FmsFormatters.secondsTohhmm(destinationPrediction.secondsFromPresent); + + destTimeCell = predToTimeCellText + '[color]green'; + } + } + + return [destEfobCell, destTimeCell]; + } + + static formatToReasonDistanceAndTime(mcdu: LegacyFmsPageInterface) { + const toPrediction = mcdu.guidanceController.vnavDriver.getPerfCrzToPrediction(); + + let reasonCell = '(T/D)'; + let distCell = '---'; + let timeCell = '----'; + + if (toPrediction) { + if (Number.isFinite(toPrediction.distanceFromPresentPosition)) { + distCell = Math.round(toPrediction.distanceFromPresentPosition) + '[color]green'; + } + + if (Number.isFinite(toPrediction.secondsFromPresent)) { + const utcTime = SimVar.GetGlobalVarValue('ZULU TIME', 'seconds'); + + timeCell = FmsFormatters.secondsToUTC(utcTime + toPrediction.secondsFromPresent) + '[color]green'; + } + + if (toPrediction.reason === 'StepClimb') { + reasonCell = '(S/C)'; + } else if (toPrediction.reason === 'StepDescent') { + reasonCell = '(S/D)'; + } + } + + return ['{small}TO{end}\xa0{green}' + reasonCell + '{end}', distCell, timeCell]; + } + + static formatCostIndexCell(mcdu: LegacyFmsPageInterface, hasFromToPair, allowModification) { + let costIndexCell = '---'; + if (hasFromToPair) { + if (mcdu.isCostIndexSet && Number.isFinite(mcdu.costIndex)) { + costIndexCell = `${mcdu.costIndex.toFixed(0)}[color]${allowModification ? 'cyan' : 'green'}`; + } else { + costIndexCell = '___[color]amber'; + } + } + + return costIndexCell; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PilotsWaypoint.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PilotsWaypoint.ts new file mode 100644 index 00000000000..bfa88afad88 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PilotsWaypoint.ts @@ -0,0 +1,127 @@ +import { PilotWaypointType } from '@fmgc/flightplanning/DataManager'; +import { CDUNewWaypoint } from './A320_Neo_CDU_NewWaypoint'; +import { CDUDataIndexPage } from './A320_Neo_CDU_DataIndexPage'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class CDUPilotsWaypoint { + static ShowPage(mcdu: LegacyFmsPageInterface, index = 0, confirmDeleteAll = false) { + if (mcdu.dataManager.numberOfStoredWaypoints() < 1) { + return CDUNewWaypoint.ShowPage(mcdu, () => CDUDataIndexPage.ShowPage2(mcdu)); + } + if (mcdu.dataManager.getStoredWaypoint(index) === undefined) { + index = mcdu.dataManager.prevStoredWaypointIndex(index); + } + const number = mcdu.dataManager.storedWaypointNumber(index); + + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.PilotsWaypoint; + + const template = [ + [`STORED WAYPOINT\xa0{small}${number}/99{end}\xa0`], + ['\xa0IDENT'], + [''], + ['\xa0\xa0\xa0\xa0LAT/LONG'], + [''], + [''], + [''], + [''], + [''], + ['', 'NEW\xa0'], + ['', 'WAYPOINT>'], + ['', confirmDeleteAll ? '{amber}CONFIRM\xa0{end}' : ''], + ['', `{${confirmDeleteAll ? 'amber' : 'cyan'}}DELETE ALL${confirmDeleteAll ? '*' : '}'}{end}`], + ]; + + const storedWp = mcdu.dataManager.getStoredWaypoint(index); + if (storedWp !== undefined) { + template[2][0] = `{green}${storedWp.waypoint.ident}{end}`; + + switch (storedWp.type) { + case PilotWaypointType.LatLon: + template[4][0] = `{green}${CDUPilotsWaypoint.formatLatLong(storedWp.waypoint.location)}{end}`; + break; + case PilotWaypointType.Pbd: + template[4][0] = `{green}{small}${CDUPilotsWaypoint.formatLatLong(storedWp.waypoint.location)}{end}{end}`; + template[5][0] = 'PLACE\xa0\xa0/BRG\xa0/DIST'; + template[6][0] = `{green}${storedWp.pbdPlace.padEnd(7, '\xa0')}/${CDUPilotsWaypoint.formatBearing(storedWp.pbdBearing)}/${storedWp.pbdDistance.toFixed(1)}{end}`; + break; + case PilotWaypointType.Pbx: + template[4][0] = `{green}{small}${CDUPilotsWaypoint.formatLatLong(storedWp.waypoint.location)}{end}{end}`; + template[7][0] = 'PLACE-BRG\xa0\xa0/PLACE-BRG'; + template[8][0] = `{green}${storedWp.pbxPlace1.substr(0, 5).padStart(5, '\xa0')}-${CDUPilotsWaypoint.formatBearing(storedWp.pbxBearing1)}\xa0/${storedWp.pbxPlace2.substr(0, 5).padStart(5, '\xa0')}-${CDUPilotsWaypoint.formatBearing(storedWp.pbxBearing2)}{end}`; + break; + default: + } + } + + mcdu.setTemplate(template); + + // delete the waypoint on ident LSK + mcdu.onLeftInput[0] = (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + mcdu.dataManager.deleteStoredWaypoint(index).then((deleted) => { + if (!deleted) { + mcdu.setScratchpadMessage(NXSystemMessages.fplnElementRetained); + } else if (mcdu.dataManager.numberOfStoredWaypoints() < 1) { + CDUNewWaypoint.ShowPage(mcdu, () => CDUDataIndexPage.ShowPage2(mcdu)); + } else { + CDUPilotsWaypoint.ShowPage(mcdu, mcdu.dataManager.nextStoredWaypointIndex(index)); + } + }); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + } + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onRightInput[4] = () => { + CDUNewWaypoint.ShowPage(mcdu); + }; + + // DELETE ALL + mcdu.onRightInput[5] = () => { + if (confirmDeleteAll) { + mcdu.dataManager.deleteAllStoredWaypoints().then((allDeleted) => { + if (!allDeleted) { + mcdu.setScratchpadMessage(NXSystemMessages.fplnElementRetained); + } + + CDUPilotsWaypoint.ShowPage(mcdu, index); + }); + } else { + CDUPilotsWaypoint.ShowPage(mcdu, index, true); + } + }; + + const canScroll = mcdu.dataManager.numberOfStoredWaypoints() > 1; + mcdu.setArrows(false, false, canScroll, canScroll); + if (canScroll) { + mcdu.onPrevPage = () => { + CDUPilotsWaypoint.ShowPage(mcdu, mcdu.dataManager.prevStoredWaypointIndex(index)); + }; + mcdu.onNextPage = () => { + CDUPilotsWaypoint.ShowPage(mcdu, mcdu.dataManager.nextStoredWaypointIndex(index)); + }; + } + } + + static formatAngle(angle, digits) { + const mins = (Math.abs(angle) % 1) * 60; + return `${Math.abs(Math.trunc(angle)).toFixed(0).padStart(digits, '0')}${Math.trunc(mins).toFixed(0).padStart(2, '0')}.${((mins % 1) * 10).toFixed(0)}`; + } + + // TODO is this already existing? + static formatLatLong(coordinates) { + return `${CDUPilotsWaypoint.formatAngle(coordinates.lat, 2)}${coordinates.lat < 0 ? 'S' : 'N'}/${CDUPilotsWaypoint.formatAngle(coordinates.long, 3)}${coordinates.long < 0 ? 'W' : 'E'}`; + } + + static formatBearing(bearing) { + return `${bearing.toFixed(0).padStart(3, '0')}°`; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PositionFrozen.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PositionFrozen.ts new file mode 100644 index 00000000000..3f7874a8fcf --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PositionFrozen.ts @@ -0,0 +1,37 @@ +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { CDUPositionMonitorPage } from './A320_Neo_CDU_PositionMonitorPage'; +import { CDUSelectedNavaids } from './A320_Neo_CDU_SelectedNavaids'; + +export class CDUPosFrozen { + static ShowPage(mcdu: LegacyFmsPageInterface, currPos) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.PosFrozen; + const UTC_SECONDS = Math.floor(SimVar.GetGlobalVarValue('ZULU TIME', 'seconds')); + const hours = Math.floor(UTC_SECONDS / 3600) || 0; + const minutes = Math.floor((UTC_SECONDS % 3600) / 60) || 0; + const hhmm = `${hours.toString().padStart(2, '0') || '00'}${minutes.toString().padStart(2, '0') || '00'}`; + mcdu.setTemplate([ + [`POSITION FROZEN AT ${hhmm}`], + [''], + ['{small}FMS1{end}', `${currPos}[color]green`], + ['\xa0\xa0\xa0\xa0\xa0\xa03IRS/GPS'], + ['{small}FMS2{end}', `${currPos}[color]green`], + ['\xa0\xa0\xa0\xa0\xa0\xa03IRS/GPS'], + ['{small}GPIRS{end}', `${currPos}[color]green`], + [''], + ['{small}MIX IRS{end}', `${currPos}[color]green`], + ['\xa0\xa0IRS1', 'IRS3\xa0', '\xa0IRS2'], + ['{small}NAV 0.0{end}[color]green', '{small}NAV 0.0{end}[color]green', '{small}NAV 0.0{end}[color]green'], + ['', 'SEL\xa0'], + ['{UNFREEZE[color]cyan', 'NAVAIDS>'], + ]); + + mcdu.onLeftInput[5] = () => { + CDUPositionMonitorPage.ShowPage(mcdu); + }; + + mcdu.onRightInput[5] = () => { + CDUSelectedNavaids.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PositionMonitorPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PositionMonitorPage.ts new file mode 100644 index 00000000000..1df97c90085 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_PositionMonitorPage.ts @@ -0,0 +1,62 @@ +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { CDUPosFrozen } from './A320_Neo_CDU_PositionFrozen'; +import { CDUSelectedNavaids } from './A320_Neo_CDU_SelectedNavaids'; + +export class CDUPositionMonitorPage { + static ShowPage(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.PositionMonitorPage; + + let currPos = new LatLong( + SimVar.GetSimVarValue('GPS POSITION LAT', 'degree latitude'), + SimVar.GetSimVarValue('GPS POSITION LON', 'degree longitude'), + ).toShortDegreeString(); + let currPosSplit: string[]; + let sep: string; + if (currPos.includes('N')) { + currPosSplit = currPos.split('N'); + sep = 'N/'; + } else { + currPosSplit = currPos.split('S'); + sep = 'S/'; + } + const latStr = currPosSplit[0]; + const lonStr = currPosSplit[1]; + currPos = latStr + sep + lonStr; + + mcdu.setTemplate([ + ['POSITION MONITOR'], + [''], + ['{small}FMS1{end}', currPos + '[color]green'], + ['\xa0\xa0\xa0\xa0\xa0\xa03IRS/GPS'], + ['{small}FMS2{end}', currPos + '[color]green'], + ['\xa0\xa0\xa0\xa0\xa0\xa03IRS/GPS'], + ['{small}GPIRS{end}', currPos + '[color]green'], + [''], + ['{small}MIX IRS{end}', currPos + '[color]green'], + ['\xa0\xa0IRS1', 'IRS3\xa0', '\xa0IRS2'], + ['{small}NAV 0.0{end}[color]green', '{small}NAV 0.0{end}[color]green', '{small}NAV 0.0{end}[color]green'], + ['', 'SEL\xa0'], + ['{FREEZE[color]cyan', 'NAVAIDS>'], + ]); + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onRightInput[5] = () => { + CDUSelectedNavaids.ShowPage(mcdu); + }; + + mcdu.onLeftInput[5] = () => { + CDUPosFrozen.ShowPage(mcdu, currPos); + }; + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.PositionMonitorPage) { + CDUPositionMonitorPage.ShowPage(mcdu); + } + }, mcdu.PageTimeout.Default); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_ProgressPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_ProgressPage.ts new file mode 100644 index 00000000000..7c7edd9fa2f --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_ProgressPage.ts @@ -0,0 +1,308 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { FmgcFlightPhase } from '@shared/flightphase'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class CDUProgressPage { + static ShowPage(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ProgressPage; + mcdu.returnPageCallback = () => { + CDUProgressPage.ShowPage(mcdu); + }; + mcdu.activeSystem = 'FMGC'; + const flightNo = mcdu.flightNumber ?? ''; + const flMax = mcdu.getMaxFlCorrected(); + const flOpt = + mcdu._zeroFuelWeightZFWCGEntered && mcdu._blockFuelEntered && (mcdu.isAllEngineOn() || mcdu.isOnGround()) + ? '{green}FL' + (Math.floor(flMax / 5) * 5).toString() + '{end}' + : '-----'; + const adirsUsesGpsAsPrimary = SimVar.GetSimVarValue('L:A32NX_ADIRS_USES_GPS_AS_PRIMARY', 'Bool'); + const gpsPrimaryStatus = adirsUsesGpsAsPrimary ? '{green}GPS PRIMARY{end}' : ''; + let flCrz = '-----'; + let vDevCell = ''; + switch (mcdu.flightPhaseManager.phase) { + case FmgcFlightPhase.Preflight: + case FmgcFlightPhase.Takeoff: { + if (mcdu.cruiseLevel) { + flCrz = 'FL' + mcdu.cruiseLevel.toFixed(0).padStart(3, '0') + '[color]cyan'; + } + break; + } + case FmgcFlightPhase.Climb: { + const alt = Math.round(Simplane.getAutoPilotSelectedAltitudeLockValue('feet') / 100); + const altCtn = Math.round(mcdu.constraintAlt / 100); + if (!mcdu.cruiseLevel && !mcdu._activeCruiseFlightLevelDefaulToFcu) { + flCrz = + 'FL' + + (altCtn && alt > altCtn ? altCtn.toFixed(0).padStart(3, '0') : alt.toFixed(0).padStart(3, '0')) + + '[color]cyan'; + } else { + flCrz = 'FL' + mcdu.cruiseLevel.toFixed(0).padStart(3, '0') + '[color]cyan'; + } + break; + } + case FmgcFlightPhase.Cruise: { + // TODO check if this is correct + // We can get here by taking off without FROM/TO entered, and climbing to the FCU altitude (which will then be used as cruise altitude) + // to enter the cruise phase. We then enter a new FROM/TO which resets the cruise altitude, but I don't know if it puts us in the CLB phase + // or keeps us in CRZ. + if (mcdu.cruiseLevel) { + flCrz = 'FL' + mcdu.cruiseLevel.toFixed(0).padStart(3, '0') + '[color]cyan'; + } + break; + } + case FmgcFlightPhase.Descent: { + const vDev = mcdu.guidanceController.vnavDriver.getLinearDeviation(); + let vDevFormattedNumber = '{small}-----{end}'; + + if (vDev && isFinite(vDev)) { + const paddedVdev = (10 * Math.round(vDev / 10)).toFixed(0).padStart(4, '\xa0'); + const vDevSign = vDev > 0 ? '+' : ' '; + const extraSpace = paddedVdev.length > 4 ? '' : '\xa0'; + + vDevFormattedNumber = '{green}' + extraSpace + vDevSign + paddedVdev + '{end}'; + } + + vDevCell = '{small}VDEV={end}' + vDevFormattedNumber + '{small}FT{end}'; + } + } + let flightPhase; + switch (mcdu.flightPhaseManager.phase) { + case FmgcFlightPhase.Preflight: + case FmgcFlightPhase.Takeoff: + flightPhase = 'TO'; + break; + case FmgcFlightPhase.Climb: + flightPhase = 'CLB'; + break; + case FmgcFlightPhase.Cruise: + flightPhase = 'CRZ'; + break; + case FmgcFlightPhase.Descent: + flightPhase = 'DES'; + break; + case FmgcFlightPhase.Approach: + flightPhase = 'APPR'; + break; + case FmgcFlightPhase.GoAround: + flightPhase = 'GA'; + break; + default: + flightPhase = ''; + break; + } + + mcdu.onLeftInput[0] = (value, scratchpadCallback) => { + if (mcdu.trySetCruiseFlCheckInput(value)) { + CDUProgressPage.ShowPage(mcdu); + } else { + scratchpadCallback(); + } + }; + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = () => { + CDUProgressPage.ShowReportPage(mcdu); + }; + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUProgressPage.ShowPredictiveGPSPage(mcdu); + }; + + let progBearingDist = '{small}\xa0---°\xa0/----.-{end}'; + let progWaypoint = '[\xa0\xa0\xa0\xa0\xa0]'; + if (mcdu.progWaypointIdent !== undefined) { + progWaypoint = mcdu.progWaypointIdent.padEnd(7, '\xa0'); + if (mcdu.progBearing > 0 && mcdu.progDistance > 0) { + const distDigits = mcdu.progDistance > 9999 ? 0 : 1; + progBearingDist = `{small}{green}\xa0${mcdu.progBearing.toFixed(0).padStart(3, '0')}°\xa0/${mcdu.progDistance.toFixed(distDigits).padStart(3)}{end}{end}`; + } + } + // the actual query takes long enough... + mcdu.rightInputDelay[3] = () => 0; + mcdu.onRightInput[3] = (input, scratchpadCallback) => { + mcdu.trySetProgWaypoint(input, (success) => { + if (!success) { + scratchpadCallback(); + } + + CDUProgressPage.ShowPage(mcdu); + }); + }; + + let rnpCell = '-.-'; + const rnpSize = mcdu.navigation.requiredPerformance.manualRnp ? 'big' : 'small'; + const rnp = mcdu.navigation.requiredPerformance.activeRnp; + // TODO check 2 decimal cut-off + if (rnp > 1) { + rnpCell = rnp.toFixed(1).padStart(4); + } else if (rnp !== undefined) { + rnpCell = rnp.toFixed(2); + } + + mcdu.onLeftInput[5] = (input, scratchpadCallback) => { + if (input === Keypad.clrValue) { + mcdu.navigation.requiredPerformance.clearPilotRnp(); + return CDUProgressPage.ShowPage(mcdu); + } + + const match = input.match(/^\d{1,2}(\.\d{1,2})?$/); + if (match === null) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + const rnp = parseFloat(input); + if (rnp < 0.01 || rnp > 20) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return; + } + + mcdu.navigation.requiredPerformance.setPilotRnp(rnp); + CDUProgressPage.ShowPage(mcdu); + }; + + let anpCell = '-.-'; + const anp = mcdu.navigation.currentPerformance; + // TODO check 2 decimal cut-off + if (anp > 1) { + anpCell = anp.toFixed(1).padStart(4); + } else if (anp !== undefined) { + anpCell = anp.toFixed(2); + } + + mcdu.setTemplate([ + ['{green}' + flightPhase.padStart(15, '\xa0') + '{end}\xa0' + flightNo.padEnd(11, '\xa0')], + ['\xa0' + 'CRZ\xa0', 'OPT\xa0\xa0\xa0\xa0REC MAX'], + [flCrz, flOpt + '\xa0\xa0\xa0\xa0' + '{magenta}FL' + flMax.toString() + '\xa0{end}'], + [''], + [' { + if (mcdu.page.Current === mcdu.page.ProgressPage) { + CDUProgressPage.ShowPage(mcdu); + } + }, mcdu.PageTimeout.Default); + } + + static ShowReportPage(mcdu) { + const plan = mcdu.flightPlanService.active; + + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ProgressPageReport; + let altCell = '---'; + if (isFinite(mcdu.cruiseLevel)) { + altCell = mcdu.cruiseLevel.toFixed(0); + } + mcdu.onRightInput[0] = (value, scratchpadCallback) => { + if (mcdu.setCruiseFlightLevelAndTemperature(value)) { + CDUProgressPage.ShowReportPage(mcdu); + } else { + scratchpadCallback(); + } + }; + + const toLeg = plan.activeLeg; + let toWaypointCell = ''; + const toWaypointUTCCell = '---'; + const toWaypointAltCell = '----'; + let nextWaypointCell = ''; + const nextWaypointUTCCell = '----'; + const nextWaypointAltCell = '---'; + if (toLeg && toLeg.isDiscontinuity === false) { + toWaypointCell = toLeg.ident; + // toWaypointUTCCell = FMCMainDisplay.secondsTohhmm(toLeg.infos.etaInFP); TODO port over + const nextLeg = plan.maybeElementAt(plan.activeLegIndex + 1); + + if (nextLeg && nextLeg.isDiscontinuity === false) { + nextWaypointCell = nextLeg.ident; + // nextWaypointUTCCell = FMCMainDisplay.secondsTohhmm(nextLeg.infos.etaInFP); TODO port over + } + } + + let destCell = ''; + const destUTCCell = '---'; + const destDistCell = '----'; + if (plan.destinationAirport) { + destCell = plan.destinationRunway ? plan.destinationRunway.ident : plan.destinationAirport.ident; + } + + mcdu.setTemplate([ + ['REPORT'], + ['\xa0OVHD', 'ALT\xa0', 'UTC'], + ['', altCell + '[color]cyan'], + ['\xa0TO'], + [toWaypointCell + '[color]green', toWaypointAltCell + '[color]green', toWaypointUTCCell + '[color]green'], + ['\xa0NEXT'], + [nextWaypointCell + '[color]green', nextWaypointAltCell + '[color]green', nextWaypointUTCCell + '[color]green'], + ['\xa0SAT', 'FOB\xa0', 'T. WIND'], + ['[][color]cyan'], + ['\xa0S/C', '', 'UTC DIST'], + [''], + ['\xa0DEST', 'EFOB', 'UTC DIST'], + [destCell, '', destUTCCell + ' ' + destDistCell], + ]); + } + + static ShowPredictiveGPSPage(mcdu, overrideDestETA = '') { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ProgressPagePredictiveGPS; + + const plan = mcdu.flightPlanService.active; + + let destIdentCell = ''; + let destETACell = ''; + if (plan.destinationAirport) { + destIdentCell = plan.destinationAirport.ident + '[color]green'; + + if (overrideDestETA) { + destETACell = overrideDestETA; + } else { + // destETACell = FMCMainDisplay.secondsTohhmm(mcdu.flightPlanManager.getDestination().infos.etaInFP); TODO port over (fms-v2) + } + + mcdu.onRightInput[0] = (value) => { + CDUProgressPage.ShowPredictiveGPSPage(mcdu, value); + }; + } + + mcdu.setTemplate([ + ['PREDICTIVE GPS'], + ['DEST', 'ETA'], + [destIdentCell, destETACell + '[color]cyan', '{small}PRIMARY{end}'], + ['\xa0\xa0-15 -10 -5 ETA+5 +10 +15'], + ['{small}\xa0\xa0\xa0\xa0Y\xa0\xa0Y\xa0\xa0\xa0Y\xa0\xa0Y\xa0\xa0Y\xa0\xa0\xa0Y\xa0\xa0Y{end}[color]green'], + ['WPT', 'ETA'], + ['[ ][color]cyan', '', '{small}PRIMARY{end}'], + ['\xa0\xa0-15 -10 -5 ETA+5 +10 +15'], + [''], + ['', '', 'DESELECTED SATELLITES'], + ['[ ][color]cyan'], + [''], + [''], + ]); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_SecFplnMain.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_SecFplnMain.ts new file mode 100644 index 00000000000..d7e603a9973 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_SecFplnMain.ts @@ -0,0 +1,58 @@ +// Copyright (c) 2021-2024 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { CDUFlightPlanPage } from './A320_Neo_CDU_FlightPlanPage'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; + +export class CDUSecFplnMain { + static ShowPage(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.activeSystem = 'FMGC'; + + mcdu.efisInterfaces.L.setSecRelatedPageOpen(true); + mcdu.efisInterfaces.R.setSecRelatedPageOpen(true); + mcdu.onUnload = () => { + mcdu.efisInterfaces.L.setSecRelatedPageOpen(false); + mcdu.efisInterfaces.R.setSecRelatedPageOpen(false); + }; + + mcdu.onLeftInput[0] = () => { + return; + //mcdu.flightPlanService.flightPlanManager.copy(FlightPlanIndex.Active, FlightPlanIndex.FirstSecondary); + CDUFlightPlanPage.ShowPage(mcdu, 0, FlightPlanIndex.FirstSecondary); + }; + + mcdu.onLeftInput[1] = () => { + return; + CDUFlightPlanPage.ShowPage(mcdu, 0, FlightPlanIndex.FirstSecondary); + }; + + mcdu.onLeftInput[2] = () => { + return; + mcdu.flightPlanService.secondaryReset(1); + }; + + mcdu.onLeftInput[5] = () => { + return; + //mcdu.flightPlanService.flightPlanManager.swap(FlightPlanIndex.FirstSecondary, FlightPlanIndex.Active); + }; + + mcdu.setTemplate([ + ['SEC INDEX'], + [''], + ['{COPY ACTIVE[color]inop', 'INIT>[color]inop'], + [''], + ['[color]inop'], + [''], + ['{DELETE SEC[color]inop'], + [''], + ['*ACTIVATE SEC[color]inop'], + [''], + [''], + [''], + ['*SWAP ACTIVE[color]inop'], + ]); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_SelectWptPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_SelectWptPage.ts new file mode 100644 index 00000000000..9d6c8b99db0 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_SelectWptPage.ts @@ -0,0 +1,84 @@ +import { DatabaseItem, isFix, isIlsNavaid, isNdbNavaid, isVhfNavaid } from '@flybywiresim/fbw-sdk'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class A320_Neo_CDU_SelectWptPage { + /** + * @param mcdu + * @param fixes + * @param callback + * @param page + * @constructor + */ + static ShowPage>(mcdu: LegacyFmsPageInterface, fixes: T[], callback, page = 0) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.SelectWptPage; + const rows = [['', 'FREQ', 'LAT/LONG'], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], [' calculateDistance(a) - calculateDistance(b)); + + for (let i = 0; i < 5; i++) { + const w = orderedWaypoints[i + 5 * page]; + if (w) { + let freq = ''; + + if (isVhfNavaid(w) || isNdbNavaid(w) || isIlsNavaid(w)) { + freq = 'frequency' in w ? fastToFixed(w.frequency, 2) : ' '; + } + + const lat = isIlsNavaid(w) ? w.locLocation.lat : isFix(w) ? w.location.lat : NaN; + const long = isIlsNavaid(w) ? w.locLocation.long : isFix(w) ? w.location.long : NaN; + + const latString = `${Math.abs(lat).toFixed(0).padStart(2, '0')}${lat >= 0 ? 'N' : 'S'}`; + const longString = `${Math.abs(long).toFixed(0).padStart(3, '0')}${long >= 0 ? 'E' : 'W'}`; + + const dist = Math.min(calculateDistance(w), 9999); + + rows[2 * i].splice(0, 1, '{green}' + dist.toFixed(0) + '{end}NM'); + rows[2 * i + 1] = [ + '*' + w.ident + '[color]cyan', + freq + '[color]green', + `${latString}/${longString}[color]green`, + ]; + mcdu.onLeftInput[i] = () => { + callback(w); + }; + mcdu.onRightInput[i] = () => { + callback(w); + }; + mcdu.onLeftInput[5] = () => { + if (mcdu.returnPageCallback) { + mcdu.returnPageCallback(); + } else { + console.error( + 'A return page callback was expected but not declared. Add a returnPageCallback to page: ' + + mcdu.page.Current, + ); + } + }; + } + } + mcdu.setTemplate([ + ['DUPLICATE NAMES', (page + 1).toFixed(0), Math.ceil(orderedWaypoints.length / 5).toFixed(0)], + ...rows, + [''], + ]); + mcdu.onPrevPage = () => { + if (page > 0) { + A320_Neo_CDU_SelectWptPage.ShowPage(mcdu, orderedWaypoints, callback, page - 1); + } + }; + mcdu.onNextPage = () => { + if (page < Math.floor(fixes.length / 5)) { + A320_Neo_CDU_SelectWptPage.ShowPage(mcdu, orderedWaypoints, callback, page + 1); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_SelectedNavaids.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_SelectedNavaids.ts new file mode 100644 index 00000000000..2e671f89525 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_SelectedNavaids.ts @@ -0,0 +1,145 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { CDUNavaidPage } from './A320_Neo_CDU_NavaidPage'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { SelectedNavaid, SelectedNavaidMode, SelectedNavaidType } from '@fmgc/navigation/Navigation'; +import { CDUPositionMonitorPage } from './A320_Neo_CDU_PositionMonitorPage'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +const NAVAID_TYPE_STRINGS = Object.freeze({ + [SelectedNavaidType.None]: '', + [SelectedNavaidType.Dme]: 'DME', + [SelectedNavaidType.Vor]: 'VOR', + [SelectedNavaidType.VorDme]: 'VORDME', + [SelectedNavaidType.VorTac]: 'VORTAC', + [SelectedNavaidType.Tacan]: 'TACAN', + [SelectedNavaidType.Ils]: 'ILSDME', + [SelectedNavaidType.Gls]: 'GLS', + [SelectedNavaidType.Mls]: 'MLS', +}); + +const NAVAID_MODE_STRINGS = Object.freeze({ + [SelectedNavaidMode.Auto]: 'AUTO', + [SelectedNavaidMode.Manual]: 'MAN', + [SelectedNavaidMode.Rmp]: 'RMP', +}); + +export class CDUSelectedNavaids { + static ShowPage(mcdu: LegacyFmsPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.SelectedNavaids; + mcdu.returnPageCallback = () => CDUSelectedNavaids.ShowPage(mcdu); + mcdu.pageRedrawCallback = () => CDUSelectedNavaids.ShowPage(mcdu); + setTimeout(mcdu.requestUpdate.bind(mcdu), 500); + + const template = [ + ['\xa0SELECTED NAVAIDS'], + ['', 'DESELECT'], + ['', '', ''], + [''], + [''], + [''], + [''], + [''], + [''], + ['\xa0RADIONAV SELECTED[color]cyan'], + ['{DESELECT[color]inop'], + ['\xa0GPS SELECTED[color]cyan'], + ['{DESELECT[color]inop', 'RETURN>'], + ]; + + /** @type {SelectedNavaid[]} */ + const selectedNavaids: SelectedNavaid[] = mcdu.getSelectedNavaids(); + + for (const [i, navaid] of selectedNavaids.entries()) { + if (navaid.frequency < 1) { + continue; + } + + const labelRow = 2 * i + 1; + const lineRow = labelRow + 1; + + template[labelRow][0] = + `\xa0${NAVAID_TYPE_STRINGS[navaid.type].padEnd(9, '\xa0')}${NAVAID_MODE_STRINGS[navaid.mode]}`; + template[lineRow][0] = + `{cyan}${navaid.facility !== null ? '{' : '\xa0'}${(navaid.ident !== null ? navaid.ident : '').padEnd(6, '\xa0')}{end}{small}{green}${navaid.frequency.toFixed(2)}{end}{end}`; + + if (navaid.facility !== null) { + mcdu.onLeftInput[i] = (text, scratchpadCallback) => { + if (text === '') { + CDUNavaidPage.ShowPage(mcdu, navaid.facility, () => CDUSelectedNavaids.ShowPage(mcdu)); + } else { + scratchpadCallback(); + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + } + }; + } + } + + const deselected = mcdu.deselectedNavaids; + for (let i = 0; i < 4; i++) { + const icao = deselected[i]; + if (!icao) { + break; + } + + const lineRow = 2 * i + 2; + + // FIXME take facilities rather than database idents + template[lineRow][1] = `{cyan}${icao.substring(7).trim()}{end}`; + + mcdu.onRightInput[i] = (text, scratchpadCallback) => { + if (text === Keypad.clrValue) { + mcdu.reselectNavaid(icao); + mcdu.requestUpdate(); + } else if (text.match(/^[A-Z0-9]{1,4}$/) !== null) { + mcdu.getOrSelectNavaidsByIdent(text, (navaid) => { + if (navaid) { + mcdu.reselectNavaid(icao); + mcdu.deselectNavaid(navaid.databaseId); + CDUSelectedNavaids.ShowPage(mcdu); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); + } + }); + } else { + scratchpadCallback(); + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + }; + } + if (deselected.length < 4) { + const lineRow = 2 * deselected.length + 2; + template[lineRow][1] = '{cyan}[\xa0\xa0]{small}*{end}{end}'; + + mcdu.onRightInput[deselected.length] = (text, scratchpadCallback) => { + if (text.match(/^[A-Z0-9]{1,4}$/) !== null) { + mcdu.getOrSelectNavaidsByIdent(text, (navaid) => { + if (navaid) { + mcdu.deselectNavaid(navaid.databaseId); + CDUSelectedNavaids.ShowPage(mcdu); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); + } + }); + } else { + scratchpadCallback(); + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + }; + } + + mcdu.setTemplate(template); + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onRightInput[5] = () => { + CDUPositionMonitorPage.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_StepAltsPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_StepAltsPage.ts new file mode 100644 index 00000000000..c71ef9aeea1 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_StepAltsPage.ts @@ -0,0 +1,369 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { NXFictionalMessages, NXSystemMessages } from '../messages/NXSystemMessages'; +import { FmgcFlightPhase } from '@shared/flightphase'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FmsFormatters } from '../legacy/FmsFormatters'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; + +export class CDUStepAltsPage { + static Return() {} + + static ShowPage(mcdu: LegacyFmsPageInterface) { + mcdu.pageUpdate = () => {}; + + mcdu.page.Current = mcdu.page.StepAltsPage; + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.StepAltsPage) { + CDUStepAltsPage.ShowPage(mcdu); + } + }, mcdu.PageTimeout.Medium); + + const activePlan = mcdu.flightPlanService.active; + + /** @type {FlightPlanLeg[]} */ + const legsWithSteps = activePlan.allLegs.filter((it) => it.isDiscontinuity === false && it.cruiseStep); + + const isFlying = + mcdu.flightPhaseManager.phase >= FmgcFlightPhase.Takeoff && mcdu.flightPhaseManager.phase < FmgcFlightPhase.Done; + const transitionAltitude = activePlan.performanceData.transitionAltitude; + + const predictions = + mcdu.guidanceController.vnavDriver.mcduProfile && mcdu.guidanceController.vnavDriver.mcduProfile.isReadyToDisplay + ? mcdu.guidanceController.vnavDriver.mcduProfile.waypointPredictions + : null; + + mcdu.setTemplate([ + ['STEP ALTS {small}FROM{end} {green}FL' + mcdu.cruiseLevel + '{end}'], + ['\xa0ALT\xa0/\xa0WPT', 'DIST\xa0TIME'], + CDUStepAltsPage.formatStepClimbLine(mcdu, legsWithSteps, 0, predictions, isFlying, transitionAltitude), + [''], + CDUStepAltsPage.formatStepClimbLine(mcdu, legsWithSteps, 1, predictions, isFlying, transitionAltitude), + [''], + CDUStepAltsPage.formatStepClimbLine(mcdu, legsWithSteps, 2, predictions, isFlying, transitionAltitude), + [''], + CDUStepAltsPage.formatStepClimbLine(mcdu, legsWithSteps, 3, predictions, isFlying, transitionAltitude), + [''], + CDUStepAltsPage.formatOptStepLine(legsWithSteps), + [''], + [' + CDUStepAltsPage.tryAddOrUpdateCruiseStepFromLeftInput(mcdu, scratchpadCallback, legsWithSteps, i, value); + } + + mcdu.onLeftInput[4] = () => {}; + + mcdu.onLeftInput[5] = () => { + CDUStepAltsPage.Return(); + }; + + mcdu.onRightInput[0] = () => {}; + mcdu.onRightInput[1] = () => {}; + mcdu.onRightInput[2] = () => {}; + mcdu.onRightInput[3] = () => {}; + mcdu.onRightInput[4] = () => {}; + mcdu.onRightInput[5] = () => {}; + } + + static formatFl(altitude, transAlt) { + if (transAlt >= 100 && altitude > transAlt) { + return 'FL' + Math.round(altitude / 100); + } + return altitude; + } + + static formatOptStepLine(steps) { + if (steps.length > 0) { + return ['', '']; + } + + return ['{small}OPT STEP:{end}', '{small}ENTER ALT ONLY{end}']; + } + + // TODO: I think it should not allow entries of step climbs after step descents, but I'm not sure if it rejects it entirely + // or gives you an IGNORED. + /** + * @param legsWithSteps {FlightPlanLeg[]} + */ + static formatStepClimbLine(mcdu, legsWithSteps, index, predictions, isFlying, transitionAltitude) { + if (!legsWithSteps || index > legsWithSteps.length) { + return ['']; + } else if (index === legsWithSteps.length) { + return ['{cyan}[\xa0\xa0\xa0]/[\xa0\xa0\xa0\xa0\xa0]{end}']; + } else { + const waypoint = legsWithSteps[index]; + const step = legsWithSteps[index].cruiseStep; + + const prediction = predictions ? predictions.get(step.waypointIndex) : null; + + // Cases: + // 1. Step above MAX FL (on PROG page) + // 2. IGNORED (If too close to T/D or before T/C) + // 3. STEP AHEAD + // 4. Distance and time< + + let lastColumn = '----\xa0----'; + if (this.checkIfStepAboveMaxFl(mcdu, step.toAltitude)) { + lastColumn = 'ABOVE\xa0MAX[s-text]'; + } else if (step.isIgnored) { + lastColumn = 'IGNORED\xa0[s-text]'; + } else if (prediction) { + const { distanceFromAircraft, secondsFromPresent } = prediction; + + if (Number.isFinite(distanceFromAircraft) && Number.isFinite(secondsFromPresent)) { + if (distanceFromAircraft < 20) { + lastColumn = 'STEP\xa0AHEAD[s-text]'; + } else { + const distanceCell = '{green}' + Math.round(distanceFromAircraft).toFixed(0) + '{end}'; + + const utcTime = SimVar.GetGlobalVarValue('ZULU TIME', 'seconds'); + const timeCell = isFlying + ? `{green}${FmsFormatters.secondsToUTC(utcTime + secondsFromPresent)}[s-text]{end}` + : `{green}${FmsFormatters.secondsTohhmm(secondsFromPresent)}[s-text]{end}`; + + lastColumn = distanceCell + '\xa0' + timeCell; + } + } + } + + return [ + '{cyan}' + CDUStepAltsPage.formatFl(step.toAltitude, transitionAltitude) + '/' + waypoint.ident + '{end}', + lastColumn, + ]; + } + } + + /** + * @param stepLegs {FlightPlanLeg[]} + */ + static tryAddOrUpdateCruiseStepFromLeftInput(mcdu, scratchpadCallback, stepLegs, index, input) { + if (index < stepLegs.length) { + this.onClickExistingStepClimb(mcdu, scratchpadCallback, stepLegs, index, input); + + return; + } + + // Create new step altitude + if (stepLegs.length >= 4) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } + + const splitInputs = input.split('/'); + const rawAltitudeInput = splitInputs[0]; + const rawIdentInput = splitInputs[1]; + + if (!rawIdentInput) { + // OPT STEP + mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + return false; + } + + const alt = this.tryParseAltitude(rawAltitudeInput); + if (!alt) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + const plan = mcdu.flightPlanService.active; // TODO allow other plans, maybe (fms-v2) + const legIndex = plan.findLegIndexByFixIdent(rawIdentInput); + + if (legIndex < 0) { + // Waypoint ident not found in flightplan + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } else if (legIndex < plan.activeLegIndex) { + // Don't allow step on FROM waypoint + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } else if (!this.checkStepInsertionRules(mcdu, stepLegs, legIndex, alt)) { + // Step too small or step descent after step climb + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } + + const leg = plan.legElementAt(legIndex); + + // It is not allowed to have two steps on the same waypoint (FCOM) + if (leg.cruiseStep !== undefined) { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } + + mcdu.flightPlanService.addOrUpdateCruiseStep(legIndex, alt, FlightPlanIndex.Active); + + if (CDUStepAltsPage.checkIfStepAboveMaxFl(mcdu, alt)) { + mcdu.addMessageToQueue(NXSystemMessages.stepAboveMaxFl); + } + } + + static tryParseAltitude(altitudeInput) { + if (!altitudeInput) { + return false; + } + + const match = altitudeInput.match(/^(FL)?(\d{1,3})$/); + + if (!match) { + return false; + } + + const altValue = parseInt(match[2]) * 100; + if (altValue < 1000 || altValue > 39000) { + return false; + } + + return altValue; + } + + /** + * @param stepLegs {FlightPlanLeg[]} + */ + static onClickExistingStepClimb(mcdu, scratchpadCallback, stepLegs, index, input) { + const plan = mcdu.flightPlanService.active; + + const stepWaypoint = stepLegs[index]; + const clickedStep = stepWaypoint.cruiseStep; + + if (input === Keypad.clrValue) { + mcdu.flightPlanService.removeCruiseStep(clickedStep.waypointIndex); + + return true; + } + + // Edit step + const splitInputs = input.split('/'); + if (splitInputs.length === 1 || (splitInputs.length === 2 && splitInputs[1] === '')) { + // Altitude + const altitude = this.tryParseAltitude(splitInputs[0]); + if (!altitude) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } else if (!this.checkStepInsertionRules(mcdu, stepLegs, clickedStep.waypointIndex, clickedStep.toAltitude)) { + // Step too small or step descent after step climb + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + return; + } + + mcdu.flightPlanService.addOrUpdateCruiseStep(clickedStep.waypointIndex, altitude); + + if (this.checkIfStepAboveMaxFl(mcdu, altitude)) { + mcdu.addMessageToQueue(NXSystemMessages.stepAboveMaxFl); + } + } else if (splitInputs.length === 2) { + const rawAltitudeInput = splitInputs[0]; + const rawIdentInput = splitInputs[1]; + + const legIndex = plan.findLegIndexByFixIdent(rawIdentInput); + + if (legIndex < 0) { + // Waypoint ident not found in flightplan + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + if (rawAltitudeInput === '') { + // /Waypoint + mcdu.flightPlanService.addOrUpdateCruiseStep(legIndex, clickedStep.toAltitude); + mcdu.flightPlanService.removeCruiseStep(clickedStep.waypointIndex); + } else { + // Altitude/waypoint + const altitude = this.tryParseAltitude(rawAltitudeInput); + + if (!altitude) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + mcdu.flightPlanService.addOrUpdateCruiseStep(legIndex, altitude); + mcdu.flightPlanService.removeCruiseStep(clickedStep.waypointIndex); + + if (this.checkIfStepAboveMaxFl(mcdu, altitude)) { + mcdu.addMessageToQueue(NXSystemMessages.stepAboveMaxFl); + } + } + } else if (splitInputs.length === 3) { + // Altitude/place/distance or + // /Place/distance + mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + scratchpadCallback(); + return; + } + } + + static checkIfStepAboveMaxFl(mcdu, altitude) { + const maxFl = mcdu.getMaxFlCorrected(); + return Number.isFinite(maxFl) && altitude > maxFl * 100; + } + + /** + * Check a couple of rules about insertion of step: + * - Minimum step size is 1000ft + * - S/C follows step descent + * TODO: It's possible that the insertion of a step in between already inserted steps causes a step descent after step climb + * I don't know how the plane handles this. + * @param {*} mcdu + * @param {FlightPlanLeg[]} stepLegs Existing steps + * @param {*} insertAtIndex Index of waypoint to insert step at + * @param {*} toAltitude Altitude of step + */ + static checkStepInsertionRules(mcdu, stepLegs, insertAtIndex, toAltitude) { + let altitude = mcdu.cruiseLevel * 100; + let doesHaveStepDescent = false; + + let i = 0; + for (; i < stepLegs.length; i++) { + const step = stepLegs[i].cruiseStep; + if (step.waypointIndex > insertAtIndex) { + break; + } + + const stepAltitude = step.toAltitude; + if (stepAltitude < altitude) { + doesHaveStepDescent = true; + } + + altitude = stepAltitude; + } + + const isStepSizeValid = Math.abs(toAltitude - altitude) >= 1000; + if (!isStepSizeValid) { + return false; + } + + const isClimbVsDescent = toAltitude > altitude; + if (!isClimbVsDescent) { + doesHaveStepDescent = true; + } else if (doesHaveStepDescent) { + return false; + } + + if (i < stepLegs.length) { + const stepAfter = stepLegs[i].cruiseStep; + const isStepSizeValid = Math.abs(stepAfter.toAltitude - toAltitude) >= 1000; + const isClimbVsDescent = stepAfter.toAltitude > toAltitude; + + const isClimbAfterDescent = isClimbVsDescent && doesHaveStepDescent; + + return isStepSizeValid && !isClimbAfterDescent; + } + + return true; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_VerticalRevisionPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_VerticalRevisionPage.ts new file mode 100644 index 00000000000..89ce4c260bb --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_VerticalRevisionPage.ts @@ -0,0 +1,803 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { WaypointConstraintType } from '@fmgc/flightplanning/data/constraint'; +import { FmgcFlightPhase } from '@shared/flightphase'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { CDUFlightPlanPage } from './A320_Neo_CDU_FlightPlanPage'; +import { CDUStepAltsPage } from './A320_Neo_CDU_StepAltsPage'; +import { CDUWindPage } from './A320_Neo_CDU_WindPage'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { AltitudeDescriptor } from '@flybywiresim/fbw-sdk'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; + +export class CDUVerticalRevisionPage { + /** + * @param mcdu + * @param {FlightPlanLeg} waypoint + * @param verticalWaypoint + * @param confirmSpeed + * @param confirmAlt + * @param confirmCode + */ + static ShowPage( + mcdu: LegacyFmsPageInterface, + waypoint, + wpIndex, + verticalWaypoint, + confirmSpeed = undefined, + confirmAlt = undefined, + confirmCode = undefined, + forPlan = FlightPlanIndex.Active, + inAlternate = false, + ) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.VerticalRevisionPage; + + const targetPlan = inAlternate ? mcdu.getAlternateFlightPlan(forPlan) : mcdu.getFlightPlan(forPlan); + const mainTargetPlan = mcdu.getFlightPlan(forPlan); + // Use performance data of primary for waypoints in alternate + const performanceData = mainTargetPlan.performanceData; + + const confirmConstraint = Number.isFinite(confirmSpeed) || Number.isFinite(confirmAlt); + const constraintType = CDUVerticalRevisionPage.constraintType(mcdu, wpIndex, forPlan, inAlternate); + const isOrigin = wpIndex === 0; + const isDestination = wpIndex === targetPlan.destinationLegIndex; + + let waypointIdent = '---'; + if (waypoint) { + if (isDestination && targetPlan.destinationRunway) { + waypointIdent = targetPlan.destinationRunway.ident; + } else { + waypointIdent = waypoint.ident; + } + } + + const showSpeedLim = + mcdu._fuelPredDone || isOrigin || isDestination || constraintType !== WaypointConstraintType.Unknown; + // the conditions other than isDestination are a workaround for no ToC + const showDesSpeedLim = + showSpeedLim && + (isDestination || + constraintType === WaypointConstraintType.DES || + (mcdu.flightPhaseManager.phase > FmgcFlightPhase.Cruise && + mcdu.flightPhaseManager.phase < FmgcFlightPhase.GoAround)); + + const climbSpeedLimitSpeed = inAlternate + ? performanceData.alternateClimbSpeedLimitSpeed + : performanceData.climbSpeedLimitSpeed; + const climbSpeedLimitAltitude = inAlternate + ? performanceData.alternateClimbSpeedLimitAltitude + : performanceData.climbSpeedLimitAltitude; + const isClimbSpeedLimitPilotEntered = inAlternate + ? performanceData.isAlternateClimbSpeedLimitPilotEntered + : performanceData.isClimbSpeedLimitPilotEntered; + + const descentSpeedLimitSpeed = inAlternate + ? performanceData.alternateDescentSpeedLimitSpeed + : performanceData.descentSpeedLimitSpeed; + const descentSpeedLimitAltitude = inAlternate + ? performanceData.alternateDescentSpeedLimitAltitude + : performanceData.descentSpeedLimitAltitude; + const isDescentSpeedLimitPilotEntered = inAlternate + ? performanceData.isAlternateDescentSpeedLimitPilotEntered + : performanceData.isDescentSpeedLimitPilotEntered; + + let speedLimitTitle = ''; + let speedLimitCell = ''; + if (showDesSpeedLim) { + speedLimitTitle = '\xa0DES SPD LIM'; + if (descentSpeedLimitSpeed !== null) { + speedLimitCell = `{magenta}{${isDescentSpeedLimitPilotEntered ? 'big' : 'small'}}${descentSpeedLimitSpeed.toFixed(0).padStart(3, '0')}/${this.formatFl(descentSpeedLimitAltitude, performanceData.transitionLevel * 100)}{end}{end}`; + } else { + speedLimitCell = '{cyan}*[ ]/[ ]{end}'; + } + } else if (showSpeedLim) { + speedLimitTitle = '\xa0CLB SPD LIM'; + if (climbSpeedLimitSpeed !== null) { + speedLimitCell = `{magenta}{${isClimbSpeedLimitPilotEntered ? 'big' : 'small'}}${climbSpeedLimitSpeed.toFixed(0).padStart(3, '0')}/${this.formatFl(climbSpeedLimitAltitude, performanceData.transitionAltitude)}{end}{end}`; + } else { + speedLimitCell = '{cyan}*[ ]/[ ]{end}'; + } + } + + const speedConstraint = waypoint.speedConstraint + ? Math.round(waypoint.speedConstraint.speed).toFixed(0) + : undefined; + const transAltLevel = + constraintType === WaypointConstraintType.DES + ? performanceData.transitionLevel * 100 + : performanceData.transitionAltitude; + const altitudeConstraint = this.formatAltConstraint(waypoint.altitudeConstraint, transAltLevel); + const canHaveAltConstraint = !isDestination && !waypoint.isXA(); + + let r3Title = canHaveAltConstraint ? 'ALT CSTR\xa0' : ''; + let r3Cell = canHaveAltConstraint ? '{cyan}[\xa0\xa0\xa0\xa0]*{end}' : ''; + let l3Title = '\xa0SPD CSTR'; + let l3Cell = '{cyan}*[\xa0\xa0\xa0]{end}'; + let l4Title = 'MACH/START WPT[color]inop'; + let l4Cell = `\xa0{inop}[\xa0]/{small}${waypointIdent}{end}{end}`; + let r4Title = ''; + let r4Cell = ''; + let r5Cell = ''; + + if (isDestination) { + const hasGsIntercept = + targetPlan.approach && (targetPlan.approach.type === 5 /* ILS */ || targetPlan.approach.type === 6); /* GLS */ + const gsIntercept = hasGsIntercept ? targetPlan.glideslopeIntercept() : 0; + + if (hasGsIntercept && gsIntercept > 0) { + r3Title = 'G/S INTCP\xa0'; + r3Cell = `{green}{small}${gsIntercept.toFixed(0)}{end}{end}`; + } else { + r3Title = ''; + r3Cell = ''; + } + + const distanceToDest = mcdu.getDistanceToDestination(); + const closeToDest = distanceToDest !== undefined && distanceToDest <= 180; + l4Title = '\xa0QNH'; + if (isFinite(mcdu.perfApprQNH)) { + if (mcdu.perfApprQNH < 500) { + l4Cell = `{cyan}${mcdu.perfApprQNH.toFixed(2)}{end}`; + } else { + l4Cell = `{cyan}${mcdu.perfApprQNH.toFixed(0)}{end}`; + } + } else if (closeToDest) { + l4Cell = '{amber}____{end}'; + } else { + l4Cell = '{cyan}[\xa0\xa0]{end}'; + } + mcdu.onLeftInput[3] = (value, scratchpadCallback) => { + if (mcdu.setPerfApprQNH(value)) { + CDUVerticalRevisionPage.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + confirmSpeed, + confirmAlt, + confirmCode, + forPlan, + inAlternate, + ); + } else { + scratchpadCallback(); + } + }; + + l3Title = ''; + l3Cell = ''; + r5Cell = ''; + } else { + if (canHaveAltConstraint && altitudeConstraint) { + r3Cell = `{magenta}${altitudeConstraint}{end}`; + } + if (speedConstraint) { + l3Cell = `{magenta}${speedConstraint}{end}`; + } + + [r4Title, r4Cell] = this.formatAltErrorTitleAndValue(waypoint, verticalWaypoint); + + if ( + mcdu.cruiseLevel && + (mcdu.flightPhaseManager.phase < FmgcFlightPhase.Descent || + mcdu.flightPhaseManager.phase > FmgcFlightPhase.GoAround) + ) { + r5Cell = 'STEP ALTS>'; + } + } + + mcdu.setTemplate([ + ['VERT REV {small}AT{end}{green} ' + waypointIdent + '{end}'], + [], + [''], + [speedLimitTitle, ''], + [speedLimitCell, 'RTA>[color]inop'], + [l3Title, r3Title], + [l3Cell, r3Cell], + [l4Title, r4Title], + [l4Cell, r4Cell], + [''], + [' { + if (!showSpeedLim) { + scratchpadCallback(); + return; + } + + if (value === Keypad.clrValue) { + if (showDesSpeedLim) { + mainTargetPlan.setPerformanceData( + inAlternate ? 'alternateDescentSpeedLimitSpeed' : 'descentSpeedLimitSpeed', + null, + ); + mainTargetPlan.setPerformanceData( + inAlternate ? 'alternateDescentSpeedLimitAltitude' : 'descentSpeedLimitAltitude', + null, + ); + mainTargetPlan.setPerformanceData( + inAlternate ? 'isAlternateDescentSpeedLimitPilotEntered' : 'isDescentSpeedLimitPilotEntered', + false, + ); + } else { + mainTargetPlan.setPerformanceData( + inAlternate ? 'alternateClimbSpeedLimitSpeed' : 'climbSpeedLimitSpeed', + null, + ); + mainTargetPlan.setPerformanceData( + inAlternate ? 'alternateClimbSpeedLimitAltitude' : 'climbSpeedLimitAltitude', + null, + ); + mainTargetPlan.setPerformanceData( + inAlternate ? 'isAlternateClimbSpeedLimitPilotEntered' : 'isClimbSpeedLimitPilotEntered', + false, + ); + } + CDUVerticalRevisionPage.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + undefined, + undefined, + undefined, + forPlan, + inAlternate, + ); + return; + } + + const matchResult = value.match(/^([0-9]{1,3})\/(((FL)?([0-9]{1,3}))|([0-9]{4,5}))$/); + if (matchResult === null) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + const speed = parseInt(matchResult[1]); + let alt = matchResult[5] !== undefined ? parseInt(matchResult[5]) * 100 : parseInt(matchResult[6]); + + if (speed < 90 || speed > 350 || alt > 45000) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return; + } + + alt = Math.round(alt / 10) * 10; + + if (showDesSpeedLim) { + mainTargetPlan.setPerformanceData( + inAlternate ? 'alternateDescentSpeedLimitSpeed' : 'descentSpeedLimitSpeed', + speed, + ); + mainTargetPlan.setPerformanceData( + inAlternate ? 'alternateDescentSpeedLimitAltitude' : 'descentSpeedLimitAltitude', + alt, + ); + mainTargetPlan.setPerformanceData( + inAlternate ? 'isAlternateDescentSpeedLimitPilotEntered' : 'isDescentSpeedLimitPilotEntered', + true, + ); + } else { + mainTargetPlan.setPerformanceData( + inAlternate ? 'alternateClimbSpeedLimitSpeed' : 'climbSpeedLimitSpeed', + speed, + ); + mainTargetPlan.setPerformanceData( + inAlternate ? 'alternateClimbSpeedLimitAltitude' : 'climbSpeedLimitAltitude', + alt, + ); + mainTargetPlan.setPerformanceData( + inAlternate ? 'isAlternateClimbSpeedLimitPilotEntered' : 'isClimbSpeedLimitPilotEntered', + true, + ); + } + + CDUVerticalRevisionPage.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + undefined, + undefined, + undefined, + forPlan, + inAlternate, + ); + }; // SPD LIM + mcdu.onRightInput[1] = () => {}; // RTA + mcdu.onLeftInput[2] = async (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt( + wpIndex, + constraintType === WaypointConstraintType.DES, + undefined, + forPlan, + inAlternate, + ); + + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); + return; + } + + if (value.match(/^[0-9]{1,3}$/) === null) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + const speed = parseInt(value); + + if (speed < 90 || speed > 350) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return; + } + + if (constraintType === WaypointConstraintType.Unknown) { + CDUVerticalRevisionPage.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + speed, + undefined, + undefined, + forPlan, + inAlternate, + ); + return; + } + + await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt( + wpIndex, + constraintType === WaypointConstraintType.DES, + speed, + forPlan, + inAlternate, + ); + + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); + }; // SPD CSTR + if (canHaveAltConstraint) { + mcdu.onRightInput[2] = async (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt( + wpIndex, + constraintType === WaypointConstraintType.DES, + undefined, + forPlan, + inAlternate, + ); + + mcdu.updateConstraints(); + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + this.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + undefined, + undefined, + undefined, + forPlan, + inAlternate, + ); + + return; + } + + const matchResult = value.match(/^([+-])?(((FL)?([0-9]{1,3}))|([0-9]{4,5}))$/); + + if (matchResult === null) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + const altitude = matchResult[5] !== undefined ? parseInt(matchResult[5]) * 100 : parseInt(matchResult[6]); + const code = + matchResult[1] === undefined + ? AltitudeDescriptor.AtAlt1 + : matchResult[1] === '-' + ? AltitudeDescriptor.AtOrBelowAlt1 + : AltitudeDescriptor.AtOrAboveAlt1; + + if (altitude > 45000) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return; + } + + if (constraintType === WaypointConstraintType.Unknown) { + CDUVerticalRevisionPage.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + undefined, + altitude, + code, + forPlan, + inAlternate, + ); + return; + } + + await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt( + wpIndex, + constraintType === WaypointConstraintType.DES, + { + altitudeDescriptor: code, + altitude1: altitude, + }, + forPlan, + inAlternate, + ); + + mcdu.updateConstraints(); + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + this.ShowPage(mcdu, waypoint, wpIndex, verticalWaypoint, undefined, undefined, undefined, forPlan, inAlternate); + }; // ALT CSTR + } + mcdu.onLeftInput[4] = () => { + //TODO: show appropriate wind page based on waypoint + CDUWindPage.Return = () => { + CDUVerticalRevisionPage.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + undefined, + undefined, + undefined, + forPlan, + inAlternate, + ); + }; + CDUWindPage.ShowPage(mcdu); + }; // WIND + mcdu.onRightInput[4] = () => { + if (!mcdu.cruiseLevel) { + return; + } + CDUStepAltsPage.Return = () => { + CDUVerticalRevisionPage.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + undefined, + undefined, + undefined, + forPlan, + inAlternate, + ); + }; + CDUStepAltsPage.ShowPage(mcdu); + }; // STEP ALTS + if (!confirmConstraint) { + mcdu.onLeftInput[5] = () => { + CDUFlightPlanPage.ShowPage(mcdu); + }; + } else { + mcdu.onLeftInput[5] = async () => { + if (Number.isFinite(confirmSpeed)) { + await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt( + wpIndex, + false, + confirmSpeed, + forPlan, + inAlternate, + ); + + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + this.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + undefined, + undefined, + undefined, + forPlan, + inAlternate, + ); + } + + if (Number.isFinite(confirmAlt)) { + await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt( + wpIndex, + false, + { + altitudeDescriptor: confirmCode, + altitude1: confirmAlt, + }, + forPlan, + inAlternate, + ); + + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + this.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + undefined, + undefined, + undefined, + forPlan, + inAlternate, + ); + } + }; + + mcdu.onRightInput[5] = async () => { + if (Number.isFinite(confirmSpeed)) { + await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt( + wpIndex, + true, + confirmSpeed, + forPlan, + inAlternate, + ); + + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + this.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + undefined, + undefined, + undefined, + forPlan, + inAlternate, + ); + } + if (Number.isFinite(confirmAlt)) { + await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt( + wpIndex, + true, + { + altitudeDescriptor: confirmCode, + altitude1: confirmAlt, + }, + forPlan, + inAlternate, + ); + + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + this.ShowPage( + mcdu, + waypoint, + wpIndex, + verticalWaypoint, + undefined, + undefined, + undefined, + forPlan, + inAlternate, + ); + } + }; + } + } + + static formatFl(constraint, transAlt) { + if (transAlt >= 100 && constraint > transAlt) { + return 'FL' + Math.round(constraint / 100); + } + return constraint; + } + + static formatAltConstraint(constraint, transAltLvl) { + if (!constraint) { + return ''; + } + + switch (constraint.altitudeDescriptor) { + case '@': // AtAlt1 + case 'I': // AtAlt1GsIntcptAlt2 + case 'X': // AtAlt1AngleAlt2 + return this.formatFl(Math.round(constraint.altitude1), transAltLvl); + case '+': // AtOrAboveAlt1 + case 'J': // AtOrAboveAlt1GsIntcptAlt2 + case 'V': // AtOrAboveAlt1AngleAlt2 + return '+' + this.formatFl(Math.round(constraint.altitude1), transAltLvl); + case '-': // AtOrBelowAlt1 + case 'Y': // AtOrBelowAlt1AngleAlt2 + return '-' + this.formatFl(Math.round(constraint.altitude1), transAltLvl); + case 'B': // range + if (constraint.altitude1 < constraint.altitude2) { + return ( + '+' + + this.formatFl(Math.round(constraint.altitude1), transAltLvl) + + '/-' + + this.formatFl(Math.round(constraint.altitude2), transAltLvl) + ); + } else { + return ( + '+' + + this.formatFl(Math.round(constraint.altitude2), transAltLvl) + + '/-' + + this.formatFl(Math.round(constraint.altitude1), transAltLvl) + ); + } + case 'C': // AtOrAboveAlt2: + return '+' + this.formatFl(Math.round(constraint.altitude2), transAltLvl); + default: + return ''; + } + } + + /** + * @param mcdu + * @param legIndex {number} + * @param forPlan {number} + * @param inAlternate {boolean} + * @returns {number|(function(*, *): (*))|*|WaypointConstraintType|VerticalWaypointType} + */ + static constraintType(mcdu, legIndex, forPlan, inAlternate) { + const planAtIndex = mcdu.flightPlanService.get(forPlan); + const plan = inAlternate ? planAtIndex.alternateFlightPlan : planAtIndex; + + const leg = plan.legElementAt(legIndex); + + if (leg.constraintType !== 3 /* Unknown */) { + return leg.constraintType; + } + + return plan.autoConstraintTypeForLegIndex(legIndex); + } + + // constraints can be set directly by LSK on f-pln page + static async setConstraints( + mcdu, + leg, + legIndex, + verticalWaypoint, + value, + scratchpadCallback, + offset = 0, + forPlan = 0, + inAlternate = false, + ) { + const type = CDUVerticalRevisionPage.constraintType(mcdu, legIndex, forPlan, inAlternate); + const isDescentConstraint = type === WaypointConstraintType.DES; + + if (value === Keypad.clrValue) { + await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt( + legIndex, + isDescentConstraint, + undefined, + forPlan, + inAlternate, + ); + await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt( + legIndex, + isDescentConstraint, + undefined, + forPlan, + inAlternate, + ); + + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + CDUFlightPlanPage.ShowPage(mcdu, offset); + return; + } + + const matchResult = value.match(/^(([0-9]{1,3})\/?)?(\/([+-])?(((FL)?([0-9]{1,3}))|([0-9]{4,5})))?$/); + if (matchResult === null) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + return; + } + + let speed; + let alt; + + if (matchResult[2] !== undefined) { + speed = parseInt(matchResult[2]); + } + + const code = matchResult[4] === undefined ? '@' : matchResult[4] === '-' ? '-' : '+'; + + if (matchResult[8] !== undefined) { + alt = parseInt(matchResult[8]) * 100; + } + + if (matchResult[9] !== undefined) { + alt = parseInt(matchResult[9]); + } + + if ((speed !== undefined && (speed < 90 || speed > 350)) || (alt !== undefined && alt > 45000)) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + scratchpadCallback(); + return; + } + + if (type === WaypointConstraintType.Unknown) { + CDUVerticalRevisionPage.ShowPage(mcdu, leg, legIndex, verticalWaypoint, speed, alt, code, forPlan, inAlternate); + return; + } + + if (speed !== undefined) { + await mcdu.flightPlanService.setPilotEnteredSpeedConstraintAt( + legIndex, + isDescentConstraint, + speed, + forPlan, + inAlternate, + ); + + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + CDUFlightPlanPage.ShowPage(mcdu, offset); + } + + if (alt !== undefined) { + await mcdu.flightPlanService.setPilotEnteredAltitudeConstraintAt( + legIndex, + isDescentConstraint, + { + altitudeDescriptor: code, + altitude1: alt, + }, + forPlan, + inAlternate, + ); + + mcdu.guidanceController.vnavDriver.invalidateFlightPlanProfile(); + + CDUFlightPlanPage.ShowPage(mcdu, offset); + } + } + + static formatAltErrorTitleAndValue(waypoint, verticalWaypoint) { + const empty = ['', '']; + + if (!waypoint || !verticalWaypoint) { + return empty; + } + + // No constraint + if (!verticalWaypoint.altitudeConstraint || verticalWaypoint.isAltitudeConstraintMet) { + return empty; + } + + // Weird prediction error + if (!isFinite(verticalWaypoint.altError)) { + return empty; + } + + let formattedAltError = (Math.round(verticalWaypoint.altError / 10) * 10).toFixed(0); + if (verticalWaypoint.altError > 0) { + formattedAltError = '+' + formattedAltError; + } + + return ['ALT ERROR\xa0', '{green}{small}' + formattedAltError + '{end}{end}']; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_WaypointPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_WaypointPage.ts new file mode 100644 index 00000000000..c4dbaa3bade --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_WaypointPage.ts @@ -0,0 +1,64 @@ +// Copyright (c) 2021-2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { CDUPilotsWaypoint } from './A320_Neo_CDU_PilotsWaypoint'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +/* + Displays blank waypoint field, when waypoint inputted, LAT, LONG will show. + Derives from Data Index PG2 +*/ + +export class CDUWaypointPage { + static ShowPage(mcdu: LegacyFmsPageInterface, waypoint = undefined) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.WaypointPage; + mcdu.returnPageCallback = () => { + CDUWaypointPage.ShowPage(mcdu); + }; + + let identValue = '_______[color]amber'; + let latLongLabel = ''; + let latLongValue = ''; + + if (waypoint) { + identValue = `${waypoint.ident}[color]cyan`; + latLongLabel = '\xa0\xa0\xa0\xa0LAT/LONG'; + latLongValue = `${CDUPilotsWaypoint.formatLatLong(waypoint.location)}[color]green`; + } + + mcdu.onLeftInput[0] = (value, scratchpadCallback) => { + if (value === Keypad.clrValue) { + CDUWaypointPage.ShowPage(mcdu, undefined); + return; + } + + mcdu.getOrSelectWaypointByIdent(value, (res) => { + if (res) { + CDUWaypointPage.ShowPage(mcdu, res); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notAllowed); + scratchpadCallback(); + } + }); + }; + + mcdu.setTemplate([ + ['WAYPOINT'], + ['\xa0IDENT'], + [identValue], + [latLongLabel], + [latLongValue], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + ]); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_WindPage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_WindPage.ts new file mode 100644 index 00000000000..4da534043fc --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/A320_Neo_CDU_WindPage.ts @@ -0,0 +1,466 @@ +import { getSimBriefOfp } from '../legacy/A32NX_Core/A32NX_ATSU'; +import { Keypad } from '../legacy/A320_Neo_CDU_Keypad'; +import { NXSystemMessages } from '../messages/NXSystemMessages'; +import { LegacyFmsPageInterface } from '../legacy/LegacyFmsPageInterface'; + +export class CDUWindPage { + static Return() {} + + static ShowPage(mcdu: LegacyFmsPageInterface) { + CDUWindPage.ShowCLBPage(mcdu); + } + + static ShowCLBPage(mcdu: LegacyFmsPageInterface, offset = 0) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ClimbWind; + + let requestButton = 'REQUEST*[color]amber'; + let requestEnable = true; + if (mcdu.simbrief.sendStatus === 'REQUESTING') { + requestEnable = false; + requestButton = 'REQUEST [color]amber'; + } + + const template = [ + ['CLIMB WIND'], + ['TRU WIND/ALT', 'HISTORY[color]inop'], + ['', 'WIND>[color]inop'], + ['', ''], + ['', ''], + ['', 'WIND{sp}[color]amber'], + ['', requestButton], + ['', ''], + ['', ''], + ['', 'NEXT{sp}'], + ['', 'PHASE>'], + ['', ''], + [' { + CDUWindPage.ShowCRZPage(mcdu); + }; + + mcdu.onLeftInput[5] = () => { + CDUWindPage.Return(); + }; + + mcdu.onRightInput[2] = () => { + if (requestEnable) { + CDUWindPage.WindRequest(mcdu, 'CLB', CDUWindPage.ShowCLBPage); + } + }; + } + + static ShowCRZPage(mcdu: LegacyFmsPageInterface, offset = 0) { + //TODO: allow wind to be set for each waypoint + + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.CruiseWind; + + let requestButton = 'REQUEST*[color]amber'; + let requestEnable = true; + if (mcdu.simbrief.sendStatus === 'REQUESTING') { + requestEnable = false; + requestButton = 'REQUEST [color]amber'; + } + + const template = [ + //["CRZ WIND {small}AT{end} {green}WAYPOINT{end}"], + ['CRZ WIND'], + ['TRU WIND/ALT', ''], + ['', ''], + ['', ''], + ['', ''], + ['', 'WIND{sp}[color]amber'], + ['', requestButton], + ['', 'PREV{sp}'], + ['', 'PHASE>'], + ['{small}SAT / ALT{end}[color]inop', 'NEXT{sp}'], + ['[ ]°/[{sp}{sp}{sp}][color]inop', 'PHASE>'], + ['', ''], + [' { + CDUWindPage.ShowCLBPage(mcdu); + }; + mcdu.onRightInput[4] = () => { + CDUWindPage.ShowDESPage(mcdu); + }; + + mcdu.onLeftInput[5] = () => { + CDUWindPage.Return(); + }; + + mcdu.onRightInput[2] = () => { + if (requestEnable) { + CDUWindPage.WindRequest(mcdu, 'CRZ', CDUWindPage.ShowCRZPage); + } + }; + } + + static ShowDESPage(mcdu: LegacyFmsPageInterface, offset = 0) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.DescentWind; + + const alternateAirport = mcdu.flightPlanService.active + ? mcdu.flightPlanService.active.alternateDestinationAirport + : undefined; + + let requestButton = 'REQUEST*[color]amber'; + let requestEnable = true; + if (mcdu.simbrief.sendStatus === 'REQUESTING') { + requestEnable = false; + requestButton = 'REQUEST [color]amber'; + } + + let alternateHeader = ''; + let alternateCell = ''; + let altFLightlevel = ''; + + if (alternateAirport) { + alternateHeader = 'ALTN WIND'; + alternateCell = '[ ]°/[ ][color]cyan'; + altFLightlevel = '{green}{small}FL100{end}{end}'; + if (mcdu.winds.alternate != null) { + alternateCell = `${CDUWindPage.FormatNumber(mcdu.winds.alternate.direction)}°/${CDUWindPage.FormatNumber(mcdu.winds.alternate.speed)}[color]cyan`; + } + } + const template = [ + ['DESCENT WIND'], + ['TRU WIND/ALT', alternateHeader], + ['', alternateCell], + ['', altFLightlevel], + ['', ''], + ['', 'WIND{sp}[color]amber'], + ['', requestButton], + ['', 'PREV{sp}'], + ['', 'PHASE>'], + ['', ''], + ['', ''], + ['', ''], + [' { + if (value == Keypad.clrValue) { + mcdu.winds.alternate = null; + CDUWindPage.ShowDESPage(mcdu, offset); + return; + } + const wind = CDUWindPage.ParseWind(value); + if (wind == null) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(); + } else { + mcdu.winds.alternate = wind; + CDUWindPage.ShowDESPage(mcdu, offset); + } + }; + } + + mcdu.onRightInput[3] = () => { + CDUWindPage.ShowCRZPage(mcdu); + }; + + mcdu.onLeftInput[5] = () => { + CDUWindPage.Return(); + }; + + mcdu.onRightInput[2] = () => { + if (requestEnable) { + CDUWindPage.WindRequest(mcdu, 'DSC', CDUWindPage.ShowDESPage); + } + }; + } + + static FormatNumber(n: number, leadingZeroes = 0) { + let output = `${n.toFixed(0)}`; + for (let i = 0; i < leadingZeroes; i++) { + if (n < 10 ** (leadingZeroes - i)) { + output = `0${output}`; + } + } + return output; + } + + static ShowWinds(rows, mcdu: LegacyFmsPageInterface, _showPage, _winds, _offset, _max = 3) { + let entries = 0; + for (let i = 0; i < _winds.length - _offset; i++) { + if (i < _max) { + const wind = _winds[i + _offset]; + rows[i * 2 + 2][0] = + `${CDUWindPage.FormatNumber(wind.direction, 2)}°/${CDUWindPage.FormatNumber(wind.speed, 2)}/FL${CDUWindPage.FormatNumber(wind.altitude, 2)}[color]cyan`; + entries = i + 1; + mcdu.onLeftInput[i] = (value) => { + if (value == Keypad.clrValue) { + _winds.splice(i + _offset, 1); + _showPage(mcdu, _offset); + } + }; + } + } + if (entries < _max) { + rows[entries * 2 + 2][0] = '{cyan}[ ]°/[ ]/[{sp}{sp}{sp}]{end}'; + mcdu.onLeftInput[entries] = (value, scratchpadCallback) => { + CDUWindPage.TryAddWind(mcdu, _winds, value, () => _showPage(mcdu, _offset), scratchpadCallback); + }; + } + + let up = false; + let down = false; + + if (_winds.length > _max - 1 && _offset > 0) { + mcdu.onDown = () => { + _showPage(mcdu, _offset - 1); + }; + down = true; + } + + if (_offset < _winds.length - (_max - 1)) { + mcdu.onUp = () => { + _showPage(mcdu, _offset + 1); + }; + up = true; + } + + mcdu.setArrows(up, down, false, false); + + return rows; + } + + static ParseTrueWindAlt(_input) { + const elements = _input.split('/'); + if (elements.length != 3) { + return null; + } + + let direction = parseInt(elements[0]); + if (direction == 360) { + direction = 0; + } + if (!isFinite(direction) || direction < 0 || direction > 359) { + return null; + } + + const speed = parseInt(elements[1]); + if (!isFinite(speed) || speed < 0 || speed > 999) { + return null; + } + + const altitude = parseInt(elements[2]); + if (!isFinite(altitude) || altitude < 0 || altitude > 450) { + return null; + } + + return { + direction: direction, + speed: speed, + altitude: altitude, + }; + } + + static TryAddWind(mcdu: LegacyFmsPageInterface, _windArray, _input, _showPage, scratchpadCallback) { + const data = CDUWindPage.ParseTrueWindAlt(_input); + if (data == null) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + scratchpadCallback(_input); + } else { + _windArray.push(data); + _showPage(); + } + } + + static ParseWind(_input) { + const elements = _input.split('/'); + if (elements.length != 2) { + return null; + } + + let direction = parseInt(elements[0]); + if (direction == 360) { + direction = 0; + } + if (!isFinite(direction) || direction < 0 || direction > 359) { + return null; + } + + const speed = parseInt(elements[1]); + if (!isFinite(speed) || speed < 0 || speed > 999) { + return null; + } + + return { + direction: direction, + speed: speed, + }; + } + + static WindRequest(mcdu: LegacyFmsPageInterface, stage, _showPage) { + getSimBriefOfp( + mcdu, + () => {}, + () => { + let windData = []; + let lastAltitude = 0; + switch (stage) { + case 'CLB': { + const clbWpts = mcdu.simbrief.navlog.filter((val) => val.stage === stage); + + // iterate through each clbWpt grabbing the wind data + clbWpts.forEach((clbWpt, wptIdx) => { + if (wptIdx == 0) { + let altIdx = 0; + // we need to backfill from altitude 0 to below clbWpt.altitude_feet in windData + while (lastAltitude < clbWpt.altitude_feet) { + const altitude = parseInt(clbWpt.wind_data.level[altIdx].altitude); + const speed = parseInt(clbWpt.wind_data.level[altIdx].wind_spd); + const direction = parseInt(clbWpt.wind_data.level[altIdx].wind_dir); + + windData.push({ + direction, + speed, + altitude: altitude / 100, + }); + lastAltitude = altitude; + altIdx++; + } + } + // Now we add the closest wind data to the altitude of the clbWpt + clbWpt.wind_data.level.forEach((wind, levelIdx) => { + const altitude = parseInt(wind.altitude); + + let deltaPrevLevel = 0; + let deltaThisLevel = 0; + // Look backwards for the closest level + if (levelIdx > 0 && levelIdx < clbWpt.wind_data.level.length - 1) { + deltaPrevLevel = Math.abs( + clbWpt.altitude_feet - parseInt(clbWpt.wind_data.level[levelIdx - 1].altitude), + ); + deltaThisLevel = Math.abs(clbWpt.altitude_feet - altitude); + } + + // Check that altitude isn't backtracking + if (altitude > lastAltitude && lastAltitude <= clbWpt.altitude_feet) { + const idx = deltaPrevLevel > deltaThisLevel ? levelIdx : levelIdx - 1; + + const idxAltitude = parseInt(clbWpt.wind_data.level[idx].altitude); + const direction = parseInt(clbWpt.wind_data.level[idx].wind_dir); + const speed = parseInt(clbWpt.wind_data.level[idx].wind_spd); + + // Check again that we didn't backtrack + if (idxAltitude > lastAltitude) { + windData.push({ + direction, + speed, + altitude: idxAltitude / 100, + }); + lastAltitude = idxAltitude; + } + } + }); + }); + mcdu.winds.climb = windData; + break; + } + case 'CRZ': { + const toc = mcdu.simbrief.navlog.find((val) => val.ident === 'TOC'); + mcdu.winds.cruise = []; + toc.wind_data.level.forEach((val) => { + const direction = parseInt(val.wind_dir); + const speed = parseInt(val.wind_spd); + const altitude = parseInt(val.altitude) / 100; + mcdu.winds.cruise.push({ + direction, + speed, + altitude, + }); + lastAltitude = altitude; + }); + break; + } + case 'DSC': { + // TOD is marked as cruise stage, but we want it's topmost wind data + const tod = mcdu.simbrief.navlog.find((val) => val.ident === 'TOD'); + const desWpts = [tod, ...mcdu.simbrief.navlog.filter((val) => val.stage === stage)]; + + if (isFinite(mcdu.simbrief.alternateAvgWindDir) && isFinite(mcdu.simbrief.alternateAvgWindSpd)) { + mcdu.winds.alternate = { + direction: mcdu.simbrief.alternateAvgWindDir, + speed: mcdu.simbrief.alternateAvgWindSpd, + }; + } else { + mcdu.winds.alternate = null; + } + // iterate through each clbWpt grabbing the wind data + windData = []; + lastAltitude = 45000; + desWpts.forEach((desWpt, wptIdx) => { + if (wptIdx == 0) { + let altIdx = desWpt.wind_data.level.length - 1; + // we need to backfill from crz altitude to above next clbWpt.altitude_feet in windData + while (lastAltitude > desWpt.altitude_feet) { + const altitude = parseInt(desWpt.wind_data.level[altIdx].altitude); + const speed = parseInt(desWpt.wind_data.level[altIdx].wind_spd); + const direction = parseInt(desWpt.wind_data.level[altIdx].wind_dir); + + windData.push({ + direction, + speed, + altitude: altitude / 100, + }); + lastAltitude = altitude; + altIdx--; + } + } + // Now we add the closest wind data to the altitude of the desWpt + desWpt.wind_data.level.reverse().forEach((wind, levelIdx) => { + const altitude = parseInt(wind.altitude); + + let deltaNextLevel = 0; + let deltaThisLevel = 0; + // Look forwards for the closest level + if (levelIdx < desWpt.wind_data.level.length - 2) { + deltaNextLevel = Math.abs( + desWpt.altitude_feet - parseInt(desWpt.wind_data.level[levelIdx + 1].altitude), + ); + deltaThisLevel = Math.abs(desWpt.altitude_feet - altitude); + } + + // Check that altitude isn't backtracking + if (altitude >= lastAltitude && lastAltitude > desWpt.altitude_feet) { + const idx = deltaNextLevel > deltaThisLevel ? levelIdx : levelIdx + 1; + + const idxAltitude = parseInt(desWpt.wind_data.level[idx].altitude); + const direction = parseInt(desWpt.wind_data.level[idx].wind_dir); + const speed = parseInt(desWpt.wind_data.level[idx].wind_spd); + + // Check again that we didn't backtrack + if (idxAltitude < lastAltitude) { + windData.push({ + direction, + speed, + altitude: idxAltitude / 100, + }); + lastAltitude = idxAltitude; + } + } + }); + }); + mcdu.winds.des = windData; + break; + } + } + _showPage(mcdu); + }, + ); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/aids/A320_Neo_CDU_AIDS_Menu.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/aids/A320_Neo_CDU_AIDS_Menu.ts new file mode 100644 index 00000000000..dec6a45bb58 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/aids/A320_Neo_CDU_AIDS_Menu.ts @@ -0,0 +1,31 @@ +import { NXFictionalMessages } from '../../messages/NXSystemMessages'; +import { LegacyAidsPageInterface } from '../../legacy/LegacyAidsPageInterface'; + +export class CDU_AIDS_MainMenu { + static ShowPage(mcdu: LegacyAidsPageInterface) { + mcdu.clearDisplay(); + mcdu.activeSystem = 'AIDS'; + mcdu.setTemplate([ + ['AIDS'], + ['CALL-UP[color]inop'], + ['[color]inop'], + [''], + ['[color]inop'], + ['', 'STORED[color]inop'], + ['', 'REPORTS>[color]inop'], + ['ASSIGNMENT[color]inop', 'MAN REQST[color]inop'], + ['[color]inop'], + ['', 'POST[color]cyan'], + ['DAR = RUNNING[color]green', 'STOP*[color]cyan'], + ]); + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + mcdu.setScratchpadMessage(NXFictionalMessages.notYetImplemented); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_FreeText.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_FreeText.ts new file mode 100644 index 00000000000..e2b862da1dd --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_FreeText.ts @@ -0,0 +1,210 @@ +import { AtsuMessageNetwork, AtsuStatusCodes, FreetextMessage } from '@datalink/common'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAocMenu } from './A320_Neo_CDU_AOC_Menu'; +import { NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAocFreeText { + static ShowPage( + mcdu: LegacyAtsuPageInterface, + store = { + msg_to: '', + reqID: SimVar.GetSimVarValue('L:A32NX_HOPPIE_ACTIVE', 'number') !== 0 ? 0 : 1, + msg_line1: '', + msg_line2: '', + msg_line3: '', + msg_line4: '', + sendStatus: '', + }, + ) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AOCFreeText; + const networkTypes = ['HOPPIE', 'FBW']; + + const updateView = () => { + let oneLineFilled = false; + if ( + store['msg_line1'] !== '' || + store['msg_line2'] !== '' || + store['msg_line3'] !== '' || + store['msg_line4'] !== '' + ) { + oneLineFilled = true; + } + let sendValid = oneLineFilled === true && store['msg_to'] !== ''; + if (store['sendStatus'] === 'SENDING' || store['sendStatus'] === 'SENT') { + sendValid = false; + } + + mcdu.setTemplate([ + ['AOC FREE TEXT'], + ['\xa0TO', 'NETWORK\xa0'], + [ + `${store['msg_to'] !== '' ? store['msg_to'] + '[color]cyan' : '________[color]amber'}`, + `↓${networkTypes[store['reqID']]}[color]cyan`, + ], + [''], + [ + `${store['msg_line1'] !== '' ? store['msg_line1'] : '['}[color]cyan`, + `${store['msg_line1'] != '' ? '' : ']'}[color]cyan`, + ], + [''], + [ + `${store['msg_line2'] !== '' ? store['msg_line2'] : '['}[color]cyan`, + `${store['msg_line2'] != '' ? '' : ']'}[color]cyan`, + ], + [''], + [ + `${store['msg_line3'] !== '' ? store['msg_line3'] : '['}[color]cyan`, + `${store['msg_line3'] != '' ? '' : ']'}[color]cyan`, + ], + [''], + [ + `${store['msg_line4'] !== '' ? store['msg_line4'] : '['}[color]cyan`, + `${store['msg_line4'] != '' ? '' : ']'}[color]cyan`, + ], + ['\xa0RETURN TO', `${store['sendStatus']}\xa0`], + [' { + if (value === Keypad.clrValue) { + store['msg_to'] = ''; + } else { + store['msg_to'] = value; + } + CDUAocFreeText.ShowPage(mcdu, store); + }; + + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + store['msg_line1'] = ''; + } else { + store['msg_line1'] = value; + } + CDUAocFreeText.ShowPage(mcdu, store); + }; + + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + store['msg_line2'] = ''; + } else { + store['msg_line2'] = value; + } + CDUAocFreeText.ShowPage(mcdu, store); + }; + + mcdu.onLeftInput[3] = (value) => { + if (value === Keypad.clrValue) { + store['msg_line3'] = ''; + } else { + store['msg_line3'] = value; + } + CDUAocFreeText.ShowPage(mcdu, store); + }; + + mcdu.onLeftInput[4] = (value) => { + if (value === Keypad.clrValue) { + store['msg_line4'] = ''; + } else { + store['msg_line4'] = value; + } + CDUAocFreeText.ShowPage(mcdu, store); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = () => { + store['reqID'] = (store['reqID'] + 1) % 2; + updateView(); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = async () => { + // do not send two times + if (store['sendStatus'] === 'SENDING' || store['sendStatus'] === 'SENT') { + return; + } + + let oneLineFilled = false; + if ( + store['msg_line1'] !== '' || + store['msg_line2'] !== '' || + store['msg_line3'] !== '' || + store['msg_line4'] !== '' + ) { + oneLineFilled = true; + } + const sendValid = oneLineFilled === true && store['msg_to'] !== ''; + + if (sendValid === false) { + mcdu.setScratchpadMessage(NXSystemMessages.mandatoryFields); + return; + } + + store['sendStatus'] = 'SENDING'; + if (mcdu.page.Current === mcdu.page.AOCFreeText) { + updateView(); + } + + // create the message + const message = new FreetextMessage(); + if (store['reqID'] === 0) { + message.Network = AtsuMessageNetwork.Hoppie; + } else { + message.Network = AtsuMessageNetwork.FBW; + } + message.Station = store['msg_to']; + if (store['msg_line1'] !== '') { + message.Message += store['msg_line1'] + '\n'; + } + if (store['msg_line2'] !== '') { + message.Message += store['msg_line2'] + '\n'; + } + if (store['msg_line3'] !== '') { + message.Message += store['msg_line3'] + '\n'; + } + if (store['msg_line4'] !== '') { + message.Message += store['msg_line4'] + '\n'; + } + message.Message = message.Message.substring(0, message.Message.length - 1); + + // send the message + mcdu.atsu.sendMessage(message).then((code) => { + if (code === AtsuStatusCodes.Ok) { + store['sendStatus'] = 'SENT'; + store['msg_line1'] = ''; + store['msg_line2'] = ''; + store['msg_line3'] = ''; + store['msg_line4'] = ''; + + setTimeout(() => { + store['sendStatus'] = ''; + if (mcdu.page.Current === mcdu.page.AOCFreeText) { + CDUAocFreeText.ShowPage(mcdu, store); + } + }, 5000); + } else { + store['sendStatus'] = 'FAILED'; + mcdu.addNewAtsuMessage(code); + } + if (mcdu.page.Current === mcdu.page.AOCFreeText) { + updateView(); + } + }); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[5] = () => { + CDUAocMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_Init.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_Init.ts new file mode 100644 index 00000000000..b3338563484 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_Init.ts @@ -0,0 +1,205 @@ +import { NXUnits } from '@flybywiresim/fbw-sdk'; +import { getSimBriefOfp } from '../../legacy/A32NX_Core/A32NX_ATSU'; +import { CDUAocMenu } from './A320_Neo_CDU_AOC_Menu'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; +import { FmsFormatters } from '../../legacy/FmsFormatters'; + +/** + * Value is rounded to 1000 and fixed to 1 decimal + * @param {number | string} value + */ +function formatWeight(value) { + return (+value).toFixed(1); +} + +export class CDUAocInit { + static ShowPage(mcdu: LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AOCInit; + mcdu.pageRedrawCallback = () => CDUAocInit.ShowPage(mcdu); + mcdu.activeSystem = 'ATSU'; + + let fltNbr = '_______[color]amber'; + let originIcao = '____[color]amber'; + let destinationIcao = '____[color]amber'; + let ete = '____[color]amber'; + let fob = `{small}---.-{end}[color]white`; + let requestButton = 'INIT DATA REQ*[color]cyan'; + let gmt = '0000[color]green'; + + const seconds = Math.floor(SimVar.GetGlobalVarValue('ZULU TIME', 'seconds')); + gmt = `{small}${FmsFormatters.secondsTohhmm(seconds)}{end}[color]green`; + + function updateView() { + if (mcdu.page.Current === mcdu.page.AOCInit) { + CDUAocInit.ShowPage(mcdu); + } + } + + // regular update due to showing time on this page + mcdu.SelfPtr = setTimeout(() => { + updateView(); + }, mcdu.PageTimeout.Default); + + if (mcdu.simbrief.sendStatus !== 'READY' && mcdu.simbrief.sendStatus !== 'DONE') { + requestButton = 'INIT DATA REQ [color]cyan'; + } + if (mcdu.simbrief.originIcao) { + originIcao = `${mcdu.simbrief.originIcao}[color]cyan`; + } + if (mcdu.simbrief.destinationIcao) { + destinationIcao = `${mcdu.simbrief.destinationIcao}[color]cyan`; + } + if (mcdu.simbrief.callsign) { + fltNbr = `{small}${mcdu.simbrief.callsign}{end}[color]green`; + } + if (mcdu.simbrief.ete) { + ete = `${FmsFormatters.secondsTohhmm(mcdu.simbrief.ete)}[color]cyan`; + } + if (mcdu.isAnEngineOn()) { + // should only get if an engine running + const currentFob = formatWeight(NXUnits.kgToUser(mcdu.getFOB())); + if (currentFob) { + fob = `{small}${currentFob}{end}[color]green`; + } + } + mcdu.setTemplate([ + ['INIT/REVIEW', '1', '2', 'AOC'], + ['\xa0FMC FLT NO', 'GMT\xa0'], + [fltNbr, gmt], + ['\xa0DEP'], + [originIcao], + ['\xa0DEST'], + [destinationIcao, 'CREW DETAILS>[color]inop'], + ['\xa0FOB'], + [' ' + fob], + ['\xa0ETE'], + [ete, requestButton], + ['', 'ADVISORY\xa0'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = () => { + // Crew Details + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelayBasic(); + }; + mcdu.onRightInput[4] = () => { + getSimBriefOfp(mcdu, updateView); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAocMenu.ShowPage(mcdu); + }; + + mcdu.onNextPage = () => { + CDUAocInit.ShowPage2(mcdu); + }; + } + + static ShowPage2(mcdu: LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AOCInit2; + mcdu.activeSystem = 'ATSU'; + /** + GMT: is the current zulu time + FLT time: is wheels up to wheels down... so basically shows 0000 as soon as you are wheels up, counts up and then stops timing once you are weight on wheels again + Out: is when you set the brakes to off... + Doors: When the last door closes + Off: remains blank until Take off time + On: remains blank until Landing time + In: remains blank until brakes set to park AND the first door opens + */ + let fob = `{small}---.-{end}[color]white`; + let fltTime = `----[color]white`; + let outTime = `----[color]white`; + let doorsTime = `----[color]white`; + let offTime = `----[color]white`; + let onTime = `----[color]white`; + let inTime = `----[color]white`; + let blockTime = `----[color]white`; + let gmt = '0000[color]green'; + + const seconds = Math.floor(SimVar.GetGlobalVarValue('ZULU TIME', 'seconds')); + gmt = `{small}${FmsFormatters.secondsTohhmm(seconds)}{end}[color]green`; + if (mcdu.isAnEngineOn()) { + const currentFob = formatWeight(NXUnits.kgToUser(mcdu.getFOB())); + if (currentFob) { + fob = `{small}${currentFob}{end}[color]green`; + } + } + if (mcdu.aocTimes.out) { + outTime = `${FmsFormatters.secondsTohhmm(mcdu.aocTimes.out)}[color]green`; + } + if (mcdu.aocTimes.doors) { + doorsTime = `${FmsFormatters.secondsTohhmm(mcdu.aocTimes.doors)}[color]green`; + } + if (mcdu.aocTimes.off) { + offTime = `${FmsFormatters.secondsTohhmm(mcdu.aocTimes.off)}[color]green`; + let currentfltTime = 0; + if (mcdu.aocTimes.on) { + currentfltTime = mcdu.aocTimes.on - mcdu.aocTimes.off; + } else { + currentfltTime = seconds - mcdu.aocTimes.off; + } + fltTime = `${FmsFormatters.secondsTohhmm(currentfltTime)}[color]green`; + } + if (mcdu.aocTimes.on) { + onTime = `${FmsFormatters.secondsTohhmm(mcdu.aocTimes.on)}[color]green`; + } + if (mcdu.aocTimes.in) { + inTime = `${FmsFormatters.secondsTohhmm(mcdu.aocTimes.in)}[color]green`; + } + if (mcdu.aocTimes.in && mcdu.aocTimes.out) { + blockTime = `${FmsFormatters.secondsTohhmm(mcdu.aocTimes.in - mcdu.aocTimes.out)}[color]green`; + } + + function updateView() { + if (mcdu.page.Current !== mcdu.page.AOCInit2) { + return; + } + const display = [ + ['INIT/REVIEW', '2', '2', 'AOC'], + [' OUT', 'OFF ', 'DOORS'], + [outTime, offTime, doorsTime], + [' ON', 'IN ', 'GMT'], + [onTime, inTime, gmt], + [' BLK TIME', 'FLT TIME '], + [blockTime, fltTime], + [' FUEL REM', 'LDG PILOT '], + [' ' + fob, '-------'], + ['', ''], + ['*AUTOLAND <{small}n{end}>[color]cyan'], + ['', 'ADVISORY '], + [' { + updateView(); + }, mcdu.PageTimeout.Default); + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAocMenu.ShowPage(mcdu); + }; + + mcdu.onPrevPage = () => { + CDUAocInit.ShowPage(mcdu); + }; + + updateView(); + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_Menu.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_Menu.ts new file mode 100644 index 00000000000..bd4b8fbf998 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_Menu.ts @@ -0,0 +1,70 @@ +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; +import { CDUAocFreeText } from './A320_Neo_CDU_AOC_FreeText'; +import { CDUAocInit } from './A320_Neo_CDU_AOC_Init'; +import { CDUAocMessagesReceived } from './A320_Neo_CDU_AOC_MessagesReceived'; +import { CDUAocMessagesSent } from './A320_Neo_CDU_AOC_MessagesSent'; +import { CDUAocRequestsAtis } from './A320_Neo_CDU_AOC_RequestsAtis'; +import { CDUAocRequestsWeather } from './A320_Neo_CDU_AOC_RequestsWeather'; +import { CDUAtsuMenu } from './A320_Neo_CDU_ATSU_Menu'; + +export class CDUAocMenu { + static ShowPage(mcdu: LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AOCMenu; + mcdu.setTemplate([ + ['AOC MENU'], + [''], + [''], + ['', ''], + [''], + ['', 'SENT\xa0'], + ['', 'MESSAGES>'], + [''], + ['', 'DIVERSION>[color]inop'], + ['\xa0ATSU DLK'], + ['[color]inop'], + ]); + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = () => { + CDUAocRequestsWeather.ShowPage(mcdu); + }; + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = () => { + CDUAocRequestsAtis.ShowPage(mcdu); + }; + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtsuMenu.ShowPage(mcdu); + }; + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = () => { + CDUAocFreeText.ShowPage(mcdu); + }; + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = () => { + CDUAocMessagesReceived.ShowPage(mcdu); + }; + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = () => { + CDUAocMessagesSent.ShowPage(mcdu); + }; + mcdu.onLeftInput[0] = () => { + CDUAocInit.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_MessageSentDetail.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_MessageSentDetail.ts new file mode 100644 index 00000000000..93817c8662b --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_MessageSentDetail.ts @@ -0,0 +1,85 @@ +import { AtsuMessageSerializationFormat } from '@datalink/common'; +import { CDUAocMessagesSent } from './A320_Neo_CDU_AOC_MessagesSent'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAocMessageSentDetail { + static ShowPage(mcdu: LegacyAtsuPageInterface, messages, messageIndex, offset = 0) { + mcdu.clearDisplay(); + const message = messages[messageIndex]; + const lines = message.serialize(AtsuMessageSerializationFormat.FmsDisplay).split('\n'); + + // mark message as read + mcdu.atsu.messageRead(message.UniqueMessageID, true); + + const msgArrows = messages.length > 1 ? ' {}' : ''; + + if (lines.length > 8) { + let up = false; + let down = false; + if (lines[offset + 1]) { + mcdu.onUp = () => { + offset += 1; + CDUAocMessageSentDetail.ShowPage(mcdu, messages, messageIndex, offset); + }; + up = true; + } + if (lines[offset - 1]) { + mcdu.onDown = () => { + offset -= 1; + CDUAocMessageSentDetail.ShowPage(mcdu, messages, messageIndex, offset); + }; + down = true; + } + mcdu.setArrows(up, down, false, false); + } + + mcdu.setTemplate([ + ['AOC SENT MSG'], + [ + `[b-text]${message.Timestamp.fmsTimestamp()} TO ${message.Station}[color]green`, + `${messageIndex + 1}/${messages.length}${msgArrows}`, + ], + [`[s-text]${lines[offset] ? lines[offset] : ''}`], + [`[b-text]${lines[offset + 1] ? lines[offset + 1] : ''}`], + [`[s-text]${lines[offset + 2] ? lines[offset + 2] : ''}`], + [`[b-text]${lines[offset + 3] ? lines[offset + 3] : ''}`], + [`[s-text]${lines[offset + 4] ? lines[offset + 4] : ''}`], + [`[b-text]${lines[offset + 5] ? lines[offset + 5] : ''}`], + [`[s-text]${lines[offset + 6] ? lines[offset + 6] : ''}`], + [`[b-text]${lines[offset + 7] ? lines[offset + 7] : ''}`], + [`[s-text]${lines[offset + 8] ? lines[offset + 8] : ''}`], + ['\xa0SENT MSGS'], + [' { + const nextMesssageIndex = messageIndex + 1; + if (nextMesssageIndex < messages.length) { + CDUAocMessageSentDetail.ShowPage(mcdu, messages, nextMesssageIndex); + } + }; + + mcdu.onPrevPage = () => { + const previousMesssageIndex = messageIndex - 1; + if (previousMesssageIndex >= 0) { + CDUAocMessageSentDetail.ShowPage(mcdu, messages, previousMesssageIndex); + } + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[5] = () => { + CDUAocMessagesSent.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onRightInput[5] = () => { + mcdu.atsu.printMessage(message); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_MessagesReceived.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_MessagesReceived.ts new file mode 100644 index 00000000000..81fde0dee6a --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_MessagesReceived.ts @@ -0,0 +1,104 @@ +import { AtsuMessageType } from '@datalink/common'; +import { translateAtsuMessageType } from '../../legacy/A32NX_Core/A32NX_ATSU'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAocMenu } from './A320_Neo_CDU_AOC_Menu'; +import { CDUAocRequestsMessage } from './A320_Neo_CDU_AOC_RequestsMessage'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAocMessagesReceived { + static ShowPage(mcdu: LegacyAtsuPageInterface, messages = null, page = 0) { + if (!messages) { + messages = mcdu.atsu.aocInputMessages(); + } + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AOCRcvdMsgs; + + page = Math.max(0, Math.min(Math.floor((messages.length - 1) / 5), page)); + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.AOCRcvdMsgs) { + CDUAocMessagesReceived.ShowPage(mcdu, null, page); + } + }, mcdu.PageTimeout.Slow); + + const offset = 5 + page * 5; + + const msgTimeHeaders = []; + msgTimeHeaders.length = 6; + for (let i = 5; i > 0; i--) { + let headerLeft = ''; + let headerRight = ''; + + if (messages.length > offset - i && messages[offset - i]) { + let sender = messages[offset - i].Station; + if (messages[offset - i].Type === AtsuMessageType.ATIS) { + sender = messages[offset - i].Reports[0].airport; + } + headerLeft += `${messages[offset - i].Timestamp.fmsTimestamp()} FROM ${sender}[color]green`; + if (!messages[offset - i].Confirmed) { + headerRight = 'NEW[color]green'; + } + } + + msgTimeHeaders[i] = [headerLeft, headerRight]; + } + + let left = false, + right = false; + if (messages.length > (page + 1) * 5) { + mcdu.onNextPage = () => { + CDUAocMessagesReceived.ShowPage(mcdu, messages, page + 1); + }; + right = true; + } + if (page > 0) { + mcdu.onPrevPage = () => { + CDUAocMessagesReceived.ShowPage(mcdu, messages, page - 1); + }; + left = true; + } + mcdu.setArrows(false, false, left, right); + + mcdu.setTemplate([ + ['AOC RCVD MSGS'], + [msgTimeHeaders[5][0], msgTimeHeaders[5][1]], + [`${messages[offset - 5] ? '<' + translateAtsuMessageType(messages[offset - 5].Type) : 'NO MESSAGES'}`], + [msgTimeHeaders[4][0], msgTimeHeaders[4][1]], + [`${messages[offset - 4] ? '<' + translateAtsuMessageType(messages[offset - 4].Type) : ''}`], + [msgTimeHeaders[3][0], msgTimeHeaders[3][1]], + [`${messages[offset - 3] ? '<' + translateAtsuMessageType(messages[offset - 3].Type) : ''}`], + [msgTimeHeaders[2][0], msgTimeHeaders[2][1]], + [`${messages[offset - 2] ? '<' + translateAtsuMessageType(messages[offset - 2].Type) : ''}`], + [msgTimeHeaders[1][0], msgTimeHeaders[1][1]], + [`${messages[offset - 1] ? '<' + translateAtsuMessageType(messages[offset - 1].Type) : ''}`], + ['\xa0AOC MENU'], + [' { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[i] = (value) => { + if (messages[offset - 5 + i]) { + if (value === Keypad.clrValue) { + mcdu.atsu.removeMessage(messages[offset - 5 + i].UniqueMessageID, true); + CDUAocMessagesReceived.ShowPage(mcdu, null, page); + } else { + CDUAocRequestsMessage.ShowPage(mcdu, messages, offset - 5 + i); + } + } + }; + } + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[5] = () => { + CDUAocMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_MessagesSent.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_MessagesSent.ts new file mode 100644 index 00000000000..d51e5d3e384 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_MessagesSent.ts @@ -0,0 +1,93 @@ +import { translateAtsuMessageType } from '../../legacy/A32NX_Core/A32NX_ATSU'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; +import { CDUAocMenu } from './A320_Neo_CDU_AOC_Menu'; +import { CDUAocMessageSentDetail } from './A320_Neo_CDU_AOC_MessageSentDetail'; + +export class CDUAocMessagesSent { + static ShowPage(mcdu: LegacyAtsuPageInterface, messages = null, page = 0) { + if (!messages) { + messages = mcdu.atsu.aocOutputMessages(); + } + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AOCSentMsgs; + + page = Math.max(0, Math.min(Math.floor((messages.length - 1) / 5), page)); + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.AOCSentMsgs) { + CDUAocMessagesSent.ShowPage(mcdu, null, page); + } + }, mcdu.PageTimeout.Slow); + + const offset = 5 + page * 5; + + const msgTimeHeaders = []; + msgTimeHeaders.length = 6; + for (let i = 5; i > 0; i--) { + let header = ''; + if (messages.length > offset - i && messages[offset - i]) { + header += `${messages[offset - i].Timestamp.fmsTimestamp()} TO ${messages[offset - i].Station}[color]green`; + } + msgTimeHeaders[i] = header; + } + + let left = false, + right = false; + if (messages.length > (page + 1) * 5) { + mcdu.onNextPage = () => { + CDUAocMessagesSent.ShowPage(mcdu, messages, page + 1); + }; + right = true; + } + if (page > 0) { + mcdu.onPrevPage = () => { + CDUAocMessagesSent.ShowPage(mcdu, messages, page - 1); + }; + left = true; + } + mcdu.setArrows(false, false, left, right); + + mcdu.setTemplate([ + ['AOC SENT MSGS'], + [msgTimeHeaders[5]], + [`${messages[offset - 5] ? '<' + translateAtsuMessageType(messages[offset - 5].Type) : 'NO MESSAGES'}`], + [msgTimeHeaders[4]], + [`${messages[offset - 4] ? '<' + translateAtsuMessageType(messages[offset - 4].Type) : ''}`], + [msgTimeHeaders[3]], + [`${messages[offset - 3] ? '<' + translateAtsuMessageType(messages[offset - 3].Type) : ''}`], + [msgTimeHeaders[2]], + [`${messages[offset - 2] ? '<' + translateAtsuMessageType(messages[offset - 2].Type) : ''}`], + [msgTimeHeaders[1]], + [`${messages[offset - 1] ? '<' + translateAtsuMessageType(messages[offset - 1].Type) : ''}`], + ['\xa0AOC MENU'], + [' { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[i] = (value) => { + if (messages[offset - 5 + i]) { + if (value === Keypad.clrValue) { + mcdu.atsu.removeMessage(messages[offset - 5 + i].UniqueMessageID, true); + CDUAocMessagesSent.ShowPage(mcdu); + } else { + CDUAocMessageSentDetail.ShowPage(mcdu, messages, offset - 5 + i); + } + } + }; + } + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[5] = () => { + CDUAocMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_RequestsAtis.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_RequestsAtis.ts new file mode 100644 index 00000000000..1332782f483 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_RequestsAtis.ts @@ -0,0 +1,206 @@ +// Copyright (c) 2021-2023 FlyByWire Simulations +// +// SPDX-License-Identifier: GPL-3.0 + +import { AtisType, AtsuStatusCodes } from '@datalink/common'; +import { FmgcFlightPhase } from '@shared/flightphase'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAocMenu } from './A320_Neo_CDU_AOC_Menu'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +// FIXME cannot access the flight phase manager! + +export class CDUAocRequestsAtis { + static CreateDataBlock(mcdu: LegacyAtsuPageInterface): any { + const retval = { + requestId: mcdu.flightPhaseManager.phase === FmgcFlightPhase.Preflight ? AtisType.Departure : AtisType.Arrival, + departure: '', + arrival: '', + selected: '', + manual: false, + formatID: 1, + sendStatus: '', + }; + + const activePlan = mcdu.flightPlanService.active; + + if (activePlan.originAirport) { + retval.departure = activePlan.originAirport.ident; + + if (mcdu.flightPhaseManager.phase === FmgcFlightPhase.Preflight) { + retval.selected = retval.departure; + } + } + + if (activePlan.destinationAirport) { + retval.arrival = activePlan.destinationAirport.ident; + + if (mcdu.flightPhaseManager.phase !== FmgcFlightPhase.Preflight) { + retval.selected = retval.arrival; + } + } + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, store = CDUAocRequestsAtis.CreateDataBlock(mcdu)) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AOCRequestAtis; + let labelTimeout; + let formatString; + + if (store.formatID === 0) { + formatString = 'PRINTER*[color]cyan'; + } else { + formatString = 'MCDU*[color]cyan'; + } + + let arrivalText = '{ARRIVAL[color]cyan'; + let departureText = '{DEPARTURE[color]cyan'; + let enrouteText = 'ENROUTE}[color]cyan'; + + if (store.requestId === AtisType.Arrival) { + arrivalText = 'ARRIVAL[color]cyan'; + } else if (store.requestId === AtisType.Departure) { + departureText = 'DEPARTURE[color]cyan'; + } else { + enrouteText = 'ENROUTE[color]cyan'; + } + + let arrText = '[ ]'; + if (store.selected !== '') { + arrText = store.selected; + if (!store.manual) { + arrText += '[s-text]'; + } + } + + const updateView = () => { + if (mcdu.page.Current === mcdu.page.AOCRequestAtis) { + let sendMessage = 'SEND*[color]cyan'; + if (store.selected === '' || store.sendStatus === 'SENDING') { + sendMessage = 'SEND\xa0[color]cyan'; + } + + mcdu.setTemplate([ + ['AOC ATIS REQUEST'], + ['\xa0AIRPORT', '↓FORMAT FOR\xa0'], + [`${arrText}[color]cyan`, formatString], + ['', '', '-------SELECT ONE-------'], + [arrivalText, enrouteText], + [''], + [departureText], + [''], + [''], + [''], + [''], + ['\xa0RETURN TO', `${store.sendStatus}\xa0`], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + store.selected = ''; + CDUAocRequestsAtis.ShowPage(mcdu, store); + } else if (value) { + store.selected = value; + store.manual = true; + + if (mcdu.page.Current === mcdu.page.AOCRequestAtis) { + CDUAocRequestsAtis.ShowPage(mcdu, store); + } + } + }; + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = () => { + if (store.reqID !== AtisType.Arrival) { + if (!store.manual) { + store.selected = store.arrival; + } + store.requestId = AtisType.Arrival; + } + CDUAocRequestsAtis.ShowPage(mcdu, store); + }; + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = () => { + if (store.reqID !== AtisType.Departure) { + if (!store.manual) { + store.selected = store.departure; + } + store.requestId = AtisType.Departure; + } + CDUAocRequestsAtis.ShowPage(mcdu, store); + }; + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + clearTimeout(labelTimeout); + CDUAocMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = () => { + store.formatID = (store.formatID + 1) % 2; + CDUAocRequestsAtis.ShowPage(mcdu, store); + }; + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = () => { + if (store.reqID !== AtisType.Enroute) { + store.requestId = AtisType.Enroute; + } + CDUAocRequestsAtis.ShowPage(mcdu, store); + }; + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = async () => { + store.sendStatus = 'SENDING'; + updateView(); + + const onRequestSent = () => { + store.sendStatus = 'SENT'; + if (mcdu.page.Current === mcdu.page.AOCRequestAtis) { + updateView(); + } + }; + + mcdu.atsu.receiveAocAtis(store.selected, store.requestId, onRequestSent).then((retval) => { + if (retval[0] === AtsuStatusCodes.Ok) { + retval[1].Confirmed = store.formatID === 0; + mcdu.atsu.registerMessages([retval[1]]); + store.sendStatus = ''; + if (mcdu.page.Current === mcdu.page.AOCRequestAtis) { + CDUAocRequestsAtis.ShowPage(mcdu, store); + } + + // print the message + if (store.formatID === 0) { + mcdu.atsu.printAocAtis(retval[1]); + } + } else { + mcdu.addNewAtsuMessage(retval[0]); + + if (mcdu.page.Current === mcdu.page.AOCRequestAtis) { + store.sendStatus = 'FAILED'; + CDUAocRequestsAtis.ShowPage(mcdu, store); + } + } + }); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_RequestsMessage.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_RequestsMessage.ts new file mode 100644 index 00000000000..354cd70c0a0 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_RequestsMessage.ts @@ -0,0 +1,85 @@ +import { AtsuMessageSerializationFormat, AtsuMessageType } from '@datalink/common'; +import { CDUAocMessagesReceived } from './A320_Neo_CDU_AOC_MessagesReceived'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAocRequestsMessage { + static ShowPage(mcdu: LegacyAtsuPageInterface, messages, messageIndex, offset = 0) { + mcdu.clearDisplay(); + const message = messages[messageIndex]; + const lines = message.serialize(AtsuMessageSerializationFormat.FmsDisplay).split('\n'); + + // mark message as read + mcdu.atsu.messageRead(message.UniqueMessageID, true); + + const msgArrows = messages.length > 1 ? ' {}' : ''; + + if (lines.length > 8) { + let up = false; + let down = false; + if (lines[offset + 1]) { + mcdu.onUp = () => { + offset += 1; + CDUAocRequestsMessage.ShowPage(mcdu, messages, messageIndex, offset); + }; + up = true; + } + if (lines[offset - 1]) { + mcdu.onDown = () => { + offset -= 1; + CDUAocRequestsMessage.ShowPage(mcdu, messages, messageIndex, offset); + }; + down = true; + } + mcdu.setArrows(up, down, false, false); + } + + let from = message.Station; + if (message.Type === AtsuMessageType.ATIS) { + from = message.Reports[0].airport; + } + + mcdu.setTemplate([ + ['AOC MSG DISPLAY'], + [ + `${message.Timestamp.fmsTimestamp()} FROM ${from}[color]green`, + `${messageIndex + 1}/${messages.length}${msgArrows}`, + ], + [`{small}${lines[offset] ? lines[offset] : ''}{end}`], + [`${lines[offset + 1] ? lines[offset + 1] : ''}`], + [`{small}${lines[offset + 2] ? lines[offset + 2] : ''}{end}`], + [`${lines[offset + 3] ? lines[offset + 3] : ''}`], + [`{small}${lines[offset + 4] ? lines[offset + 4] : ''}{end}`], + [`${lines[offset + 5] ? lines[offset + 5] : ''}`], + [`{small}${lines[offset + 6] ? lines[offset + 6] : ''}{end}`], + [`${lines[offset + 7] ? lines[offset + 7] : ''}`], + [`{small}${lines[offset + 8] ? lines[offset + 8] : ''}{end}`], + ['\xa0RCVD MSGS'], + [' { + const nextMesssageIndex = messageIndex + 1; + if (nextMesssageIndex < messages.length) { + CDUAocRequestsMessage.ShowPage(mcdu, messages, nextMesssageIndex); + } + }; + + mcdu.onPrevPage = () => { + const previousMesssageIndex = messageIndex - 1; + if (previousMesssageIndex >= 0) { + CDUAocRequestsMessage.ShowPage(mcdu, messages, previousMesssageIndex); + } + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAocMessagesReceived.ShowPage(mcdu); + }; + + mcdu.onRightInput[5] = () => { + mcdu.atsu.printMessage(message); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_RequestsWeather.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_RequestsWeather.ts new file mode 100644 index 00000000000..94b10a10e11 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_AOC_RequestsWeather.ts @@ -0,0 +1,156 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { AtsuStatusCodes } from '@datalink/common'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAocMenu } from './A320_Neo_CDU_AOC_Menu'; +import { NXFictionalMessages, NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAocRequestsWeather { + static CreateDataBlock(mcdu: LegacyAtsuPageInterface) { + const retval = { + airports: ['', '', '', ''], + managed: [true, true, true, true], + sendStatus: '', + requestId: 0, + }; + + const activePlan = mcdu.flightPlanService.active; + + if (activePlan.originAirport) { + retval.airports[0] = activePlan.originAirport.ident; + } + + if (activePlan.destinationAirport) { + retval.airports[1] = activePlan.destinationAirport.ident; + } + + if (activePlan.alternateDestinationAirport) { + retval.airports[2] = activePlan.alternateDestinationAirport.ident; + } + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, data = CDUAocRequestsWeather.CreateDataBlock(mcdu)) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.AOCRequestWeather; + let labelTimeout; + const reqTypes = ['METAR', 'TAF']; + + // get the airports + const airports = ['[ ][color]green', '[ ][color]green', '[ ][color]green', '[ ][color]green']; + for (let i = 0; i < 4; ++i) { + if (data.airports[i] !== '') { + airports[i] = data.airports[i]; + + if (data.managed[i]) { + airports[i] += '[color]cyan'; + } else { + airports[i] += '[color]green'; + } + } + } + + const updateView = () => { + if (mcdu.page.Current === mcdu.page.AOCRequestWeather) { + let sendMessage = 'SEND\xa0[color]cyan'; + if (data.airports.filter((n) => n).length !== 0 && data.sendStatus !== 'SENDING') { + sendMessage = 'SEND*[color]cyan'; + } + + mcdu.setTemplate([ + ['AOC WEATHER REQUEST'], + ['\xa0WX TYPE', 'AIRPORTS\xa0'], + [`↓${reqTypes[data.requestId]}[color]cyan`, airports[0]], + [''], + ['', airports[1]], + [''], + ['', airports[2]], + [''], + ['', airports[3]], + [''], + [''], + ['AOC MENU', `${data.sendStatus}\xa0`], + [' { + if (value === Keypad.clrValue) { + data.airports[i] = ''; + CDUAocRequestsWeather.ShowPage(mcdu, data); + } else { + if (!/^[A-Z0-9]{4}$/.test(value)) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } else { + data.airports[i] = value; + data.managed[i] = false; + + if (mcdu.page.Current === mcdu.page.AOCRequestWeather) { + CDUAocRequestsWeather.ShowPage(mcdu, data); + } + } + } + }; + } + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onRightInput[5] = async () => { + const icaos = data.airports.filter((n) => n); + if (icaos.length === 0) { + mcdu.setScratchpadMessage(NXFictionalMessages.noAirportSpecified); + return; + } + data.sendStatus = 'SENDING'; + updateView(); + + const sentRequest = () => { + data.sendStatus = 'SENT'; + if (mcdu.page.Current === mcdu.page.AOCRequestWeather) { + updateView(); + } + }; + + mcdu.atsu.receiveWeather(data.requestId === 0, icaos, sentRequest).then((retval) => { + if (retval[0] === AtsuStatusCodes.Ok) { + mcdu.atsu.registerMessages([retval[1]]); + data.sendStatus = ''; + + if (mcdu.page.Current === mcdu.page.AOCRequestWeather) { + updateView(); + } + } else { + mcdu.addNewAtsuMessage(retval[0]); + + if (mcdu.page.Current === mcdu.page.AOCRequestWeather) { + data.sendStatus = 'FAILED'; + updateView(); + } + } + }); + }; + + mcdu.leftInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = () => { + data.requestId = (data.requestId + 1) % 2; + CDUAocRequestsWeather.ShowPage(mcdu, data); + }; + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + clearTimeout(labelTimeout); + CDUAocMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_AtisAutoUpdate.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_AtisAutoUpdate.ts new file mode 100644 index 00000000000..4b207cb5c45 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_AtisAutoUpdate.ts @@ -0,0 +1,110 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { AtisType, AtsuStatusCodes } from '@datalink/common'; +import { CDUAtcAtisMenu } from './A320_Neo_CDU_ATC_AtisMenu'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcAtisAutoUpdate { + static ToggleAutoUpdate(mcdu: LegacyAtsuPageInterface, icao, reloadPage) { + if (mcdu.atsu.atisAutoUpdateActive(icao)) { + mcdu.atsu.deactivateAtisAutoUpdate(icao).then((status) => { + if (status !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(status); + } + if (reloadPage) { + CDUAtcAtisAutoUpdate.ShowPage(mcdu); + } + }); + } else { + mcdu.atsu.activateAtisAutoUpdate(icao, AtisType.Arrival).then((status) => { + if (status !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(status); + } + if (reloadPage) { + CDUAtcAtisAutoUpdate.ShowPage(mcdu); + } + }); + } + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, updateInProgress = false) { + mcdu.clearDisplay(); + + const activeDestinationAirport = mcdu.flightPlanService.active.destinationAirport; + const activeAlternateAirport = mcdu.flightPlanService.active.alternateDestinationAirport; + + let arrAtis = '{inop}\xa0[ ]/[ ]{end}'; + let arrAtisState = ''; + let arrAtisButton = '{cyan}ON\xa0{end}'; + let altAtis = '{inop}\xa0[ ]/[ ]{end}'; + let altAtisState = ''; + let altAtisButton = '{cyan}ON\xa0{end}'; + if (activeDestinationAirport) { + arrAtis = `{cyan}\xa0${activeDestinationAirport.ident}/ARR{end}`; + if (mcdu.atsu.atisAutoUpdateActive(activeDestinationAirport.ident)) { + arrAtisState = '\x3a ON'; + arrAtisButton = `{cyan}OFF${updateInProgress ? '\xa0' : '*'}{end}`; + } else { + arrAtisState = '\x3a OFF'; + arrAtisButton = `{cyan}ON${updateInProgress ? '\xa0' : '*'}{end}`; + } + } + if (activeAlternateAirport && activeAlternateAirport.ident) { + altAtis = `{cyan}\xa0${activeAlternateAirport.ident}/ARR{end}`; + if (mcdu.atsu.atisAutoUpdateActive(activeAlternateAirport.ident)) { + altAtisState = '\x3a ON'; + altAtisButton = '{cyan}OFF*{end}'; + } else { + altAtisState = '\x3a OFF'; + altAtisButton = '{cyan}ON*{end}'; + } + } + + mcdu.setTemplate([ + ['ATIS AUTO UPDATE'], + [''], + [''], + ['', '{cyan}SET\xa0{end}'], + [arrAtis, arrAtisButton, arrAtisState], + ['', '{cyan}SET\xa0{end}'], + [altAtis, altAtisButton, altAtisState], + [''], + [''], + [''], + [''], + ['\xa0ATIS MENU'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcAtisMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = () => { + const activeDestinationAirport = mcdu.flightPlanService.active.destinationAirport; + + if (updateInProgress === false && activeDestinationAirport) { + CDUAtcAtisAutoUpdate.ToggleAutoUpdate(mcdu, activeDestinationAirport.ident, true); + CDUAtcAtisAutoUpdate.ShowPage(mcdu, true); + } + }; + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = () => { + const activeAlternateAirport = mcdu.flightPlanService.active.alternateDestinationAirport; + + if (updateInProgress === false && activeAlternateAirport) { + CDUAtcAtisAutoUpdate.ToggleAutoUpdate(mcdu, activeAlternateAirport.ident, true); + CDUAtcAtisAutoUpdate.ShowPage(mcdu, true); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_AtisMenu.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_AtisMenu.ts new file mode 100644 index 00000000000..275c122f9cd --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_AtisMenu.ts @@ -0,0 +1,260 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { AtisType, AtsuStatusCodes } from '@datalink/common'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcAtisAutoUpdate } from './A320_Neo_CDU_ATC_AtisAutoUpdate'; +import { CDUAtcMenu } from './A320_Neo_CDU_ATC_Menu'; +import { CDUAtcReportAtis } from './A320_Neo_CDU_ATC_ReportAtis'; +import { NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcAtisMenu { + static CreateDataBlock(mcdu: LegacyAtsuPageInterface) { + const airports = [ + { icao: '', type: AtisType.Departure, requested: false, autoupdate: false }, + { icao: '', type: AtisType.Arrival, requested: false, autoupdate: false }, + { icao: '', type: AtisType.Arrival, requested: false, autoupdate: false }, + { icao: '', type: AtisType.Arrival, requested: false, autoupdate: false }, + ]; + + const activePlan = mcdu.flightPlanService.active; + + if (activePlan.originAirport) { + airports[0].icao = activePlan.originAirport.ident; + airports[0].autoupdate = mcdu.atsu.atisAutoUpdateActive(airports[0].icao); + } + + if (activePlan.destinationAirport) { + airports[1].icao = activePlan.destinationAirport.ident; + airports[1].autoupdate = mcdu.atsu.atisAutoUpdateActive(airports[1].icao); + } + + if (activePlan.alternateDestinationAirport) { + airports[2].icao = activePlan.alternateDestinationAirport.ident; + airports[2].autoupdate = mcdu.atsu.atisAutoUpdateActive(airports[2].icao); + } + + return airports; + } + + static InterpretLSK(mcdu, value, airports, idx, updateAtisPrintInProgress = false) { + if (!value) { + const reports = mcdu.atsu.atisReports(airports[idx].icao); + if (reports.length !== 0) { + CDUAtcReportAtis.ShowPage( + mcdu, + `${airports[idx].icao}/${airports[idx].type === AtisType.Departure ? 'DEP' : 'ARR'}`, + reports, + 0, + ); + } + } else if (value === Keypad.clrValue) { + airports[idx].icao = ''; + airports[idx].requested = false; + airports[idx].autoupdate = false; + CDUAtcAtisMenu.ShowPage(mcdu, airports, updateAtisPrintInProgress); + } else { + // validate the generic format and if the airport is described by four characters + const entries = value.split('/'); + if (entries.length !== 2 || !/^[A-Z0-9]{4}$/.test(entries[0])) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + return; + } + + // validate the ATIS type + let type = null; + if (entries[1] === 'ARR' || entries[1] === 'AR' || entries[1] === 'A') { + type = AtisType.Arrival; + } else if (entries[1] === 'DEP' || entries[1] === 'DDE' || entries[1] === 'D') { + type = AtisType.Departure; + } + if (type === null) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + return; + } + + // check that this is setup is not already set + const currentIndex = airports.findIndex((elem) => elem.icao === entries[0] && elem.type === type); + if (currentIndex !== -1 && idx !== currentIndex) { + mcdu.setScratchpadMessage(NXSystemMessages.arptTypeAlreadyInUse); + return; + } + + // validate the airport + mcdu.navigationDatabaseService.activeDatabase.searchAirport(entries[0]).then((airport) => { + if (airport) { + airports[idx].autoupdate = mcdu.atsu.atisAutoUpdateActive(entries[0]); + airports[idx].icao = entries[0]; + airports[idx].type = type; + CDUAtcAtisMenu.ShowPage(mcdu, airports, updateAtisPrintInProgress); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.notInDatabase); + } + }); + } + } + + // FIXME what is an RSK? Is this meant to be right-side LSK? + static InterpretRSK(mcdu: LegacyAtsuPageInterface, airports, idx, updateAtisPrintInProgress?: boolean) { + if (airports[idx].icao === '') { + return; + } + + if (airports[idx].autoupdate) { + const reports = mcdu.atsu.atisReports(airports[idx].icao); + if (reports.length !== 0) { + CDUAtcAtisAutoUpdate.ToggleAutoUpdate(mcdu, airports[idx].icao, false); + airports[0].autoupdate = false; + CDUAtcAtisMenu.ShowPage(mcdu, airports, updateAtisPrintInProgress); + } else if (!airports[idx].requested) { + CDUAtcAtisMenu.RequestAtis(mcdu, airports, idx); + } + } else if (!airports[idx].requested) { + CDUAtcAtisMenu.RequestAtis(mcdu, airports, idx); + } + } + + static CreateLineData(mcdu: LegacyAtsuPageInterface, airport) { + if (airport.icao !== '') { + const reports = mcdu.atsu.atisReports(airport.icao); + + let prefix = '\xa0'; + let middle = ''; + if (reports.length !== 0) { + middle = `\xa0\xa0${reports[0].Information} ${reports[0].Timestamp.mailboxTimestamp()}`; + middle = middle.substring(0, middle.length - 1); + prefix = '{white}<{end}'; + } + + let suffix = '\xa0'; + if (!airport.requested) { + suffix = '*'; + } + + let right = 'SEND*'; + let rightTitle = 'REQ\xa0'; + const left = `{cyan}${prefix}${airport.icao}/${airport.type === AtisType.Arrival ? 'ARR' : 'DEP'}{end}`; + + if (airport.autoupdate) { + rightTitle = 'UPDATE\xa0'; + if (reports.length !== 0) { + right = 'CANCEL*'; + } else { + right = `SEND${suffix}`; + } + } else { + right = `SEND${suffix}`; + } + + return [left, middle, rightTitle, right]; + } else { + return ['{cyan}\xa0[ ]/[ ]{end}', '', 'REQ\xa0', 'SEND\xa0']; + } + } + + static RequestAtis(mcdu: LegacyAtsuPageInterface, airports, idx, updateAtisPrintInProgress = false) { + if (airports[idx].icao !== '' && !airports[idx].requested) { + airports[idx].requested = true; + + mcdu.atsu.receiveAtcAtis(airports[idx].icao, airports[idx].type).then((response) => { + if (response !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(response); + } + + airports[idx].requested = false; + if (mcdu.page.Current === mcdu.page.ATCAtis) { + CDUAtcAtisMenu.ShowPage(mcdu, airports, updateAtisPrintInProgress); + } + }); + + CDUAtcAtisMenu.ShowPage(mcdu, airports, updateAtisPrintInProgress); + } + } + + static ShowPage( + mcdu: LegacyAtsuPageInterface, + airports = CDUAtcAtisMenu.CreateDataBlock(mcdu), + updateAtisPrintInProgress = false, + ) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCAtis; + + const lines = [ + CDUAtcAtisMenu.CreateLineData(mcdu, airports[0]), + CDUAtcAtisMenu.CreateLineData(mcdu, airports[1]), + CDUAtcAtisMenu.CreateLineData(mcdu, airports[2]), + CDUAtcAtisMenu.CreateLineData(mcdu, airports[3]), + ]; + + let printTitle = 'PRINT:MANUAL\xa0'; + let printButton = `SET AUTO${updateAtisPrintInProgress ? '\xa0' : '*'}`; + if (mcdu.atsu.printAtisReportsPrint()) { + printTitle = 'PRINT:AUTO\xa0'; + printButton = `SET MANUAL${updateAtisPrintInProgress ? '\xa0' : '*'}`; + } + + mcdu.setTemplate([ + ['ATIS MENU'], + ['\xa0ARPT/TYPE', `{cyan}${lines[0][2]}{end}`], + [lines[0][0], `{cyan}${lines[0][3]}{end}`, `{small}${lines[0][1]}{end}`], + ['', `{cyan}${lines[1][2]}{end}`], + [lines[1][0], `{cyan}${lines[1][3]}{end}`, `{small}${lines[1][1]}{end}`], + ['', `{cyan}${lines[2][2]}{end}`], + [lines[2][0], `{cyan}${lines[2][3]}{end}`, `{small}${lines[2][1]}{end}`], + ['', `{cyan}${lines[3][2]}{end}`], + [lines[3][0], `{cyan}${lines[3][3]}{end}`, `{small}${lines[3][1]}{end}`], + ['', 'AUTO\xa0'], + ['', 'UPDATE>'], + ['\xa0ATC MENU', printTitle], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[i] = (value) => { + CDUAtcAtisMenu.InterpretLSK(mcdu, value, airports, i); + }; + } + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + for (let i = 0; i < 4; ++i) { + mcdu.rightInputDelay[i] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[i] = () => { + CDUAtcAtisMenu.InterpretRSK(mcdu, airports, i); + }; + } + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + CDUAtcAtisAutoUpdate.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (updateAtisPrintInProgress === false) { + mcdu.atsu.togglePrintAtisReports().then((status) => { + if (status !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(status); + } + CDUAtcAtisMenu.ShowPage(mcdu, airports); + }); + CDUAtcAtisMenu.ShowPage(mcdu, airports, true); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ClearanceReq.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ClearanceReq.ts new file mode 100644 index 00000000000..ef921de82e9 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ClearanceReq.ts @@ -0,0 +1,114 @@ +import { CpdlcMessage, CpdlcMessagesDownlink, FansMode } from '@datalink/common'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcDepartReq } from './A320_Neo_CDU_ATC_DepartReq'; +import { CDUAtcMenu } from './A320_Neo_CDU_ATC_Menu'; +import { CDUAtcTextFansA } from './FansA/A320_Neo_CDU_ATC_Text'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcClearanceReq { + static CreateDataBlock() { + return { + clearance: false, + }; + } + + static CanSendData(data) { + return data.clearance; + } + + static CreateRequest(mcdu: LegacyAtsuPageInterface) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink['DM25'][1].deepCopy()); + retval.Content[0].Content[0].Value = 'DEPARTURE'; + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, title, data = CDUAtcClearanceReq.CreateDataBlock()) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCGroundRequest; + + let addText = 'ADD TEXT\xa0'; + let clearance = '{cyan}{{end}CLEARANCE'; + let transfer = ['{cyan}XFR TO\xa0{end}', '{cyan}DCDU\xa0{end}']; + let erase = ['\xa0ALL FIELDS', '\xa0ERASE']; + if (mcdu.atsu.fansMode() !== FansMode.FansA) { + clearance = ' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (mcdu.atsu.fansMode() !== FansMode.FansA) { + CDUAtcDepartReq.ShowPage1(mcdu); + return; + } else if (value === Keypad.clrValue) { + data.clearance = false; + } else { + data.clearance = true; + } + + CDUAtcClearanceReq.ShowPage(mcdu, title, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcClearanceReq.ShowPage(mcdu, title); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA && CDUAtcClearanceReq.CanSendData(data)) { + const message = CDUAtcClearanceReq.CreateRequest(mcdu); + CDUAtcTextFansA.ShowPage1(mcdu, [message]); + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA && CDUAtcClearanceReq.CanSendData(data)) { + const message = CDUAtcClearanceReq.CreateRequest(mcdu); + mcdu.atsu.registerMessages([message]); + CDUAtcClearanceReq.ShowPage(mcdu, title); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_Connection.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_Connection.ts new file mode 100644 index 00000000000..bee0dabdcd7 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_Connection.ts @@ -0,0 +1,62 @@ +import { FansMode } from '@datalink/common'; +import { CDUAtcConnectionNotification } from './A320_Neo_CDU_ATC_ConnectionNotification'; +import { CDUAtcConnectionStatus } from './A320_Neo_CDU_ATC_ConnectionStatus'; +import { CDUAtcMaxUplinkDelay } from './A320_Neo_CDU_ATC_MaxUplinkDelay'; +import { CDUAtcMenu } from './A320_Neo_CDU_ATC_Menu'; +import { NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcConnection { + static ShowPage(mcdu: LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCConnection; + + mcdu.setTemplate([ + ['\xa0CONNECTION'], + [''], + [''], + ['\xa0ATC MENU'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = () => { + CDUAtcConnectionNotification.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = () => { + CDUAtcConnectionStatus.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcMaxUplinkDelay.ShowPage(mcdu); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.keyNotActive); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ConnectionNotification.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ConnectionNotification.ts new file mode 100644 index 00000000000..69ca3cde1d3 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ConnectionNotification.ts @@ -0,0 +1,182 @@ +import { AtsuStatusCodes } from '@datalink/common'; +import { CDUAtcConnection } from './A320_Neo_CDU_ATC_Connection'; +import { CDUAtcConnectionStatus } from './A320_Neo_CDU_ATC_ConnectionStatus'; +import { NXFictionalMessages, NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcConnectionNotification { + static ShowPage(mcdu: LegacyAtsuPageInterface, store = { atcCenter: '', logonAllowed: false, loginState: 0 }) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCNotification; + + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.ATCNotification) { + CDUAtcConnectionNotification.ShowPage(mcdu, store); + } + }, mcdu.PageTimeout.Default); + + let flightNo = '-------[color]white'; + let atcStation = '____[color]amber'; + let atcStationAvail = false; + let flightNoAvail = false; + let fromToAvail = false; + let centerTitleLeft = '\xa0ATC CENTER[color]white'; + let centerTitleRight = ''; + let notificationStatus = ''; + + if (store['loginState'] === 1) { + centerTitleLeft = '\xa0ATC CENTER-NOTIFYING[color]white'; + } else if (store['loginState'] === 2) { + centerTitleLeft = '\xa0ATC-CENTER-[color]white'; + centerTitleRight = 'NOTIF FAILED[color]red'; + } + if (store['atcCenter'] !== '' && store['loginState'] === 0) { + atcStation = `${store['atcCenter']}[color]cyan`; + atcStationAvail = true; + } + if (mcdu.atsu.flightNumber().length !== 0) { + flightNo = mcdu.atsu.flightNumber() + '[color]green'; + flightNoAvail = true; + } + if (mcdu.flightPlanService.active.destinationAirport && mcdu.flightPlanService.active.destinationAirport.ident) { + fromToAvail = true; + } + + let notifyButton; + if (atcStationAvail && flightNoAvail && fromToAvail && store['loginState'] === 0) { + notifyButton = 'NOTIFY*[color]cyan'; + store['logonAllowed'] = true; + } else { + notifyButton = 'NOTIFY\xa0[color]cyan'; + store['logonAllowed'] = false; + } + if (!flightNoAvail || !fromToAvail) { + notificationStatus = 'NOTIFICATION UNAVAILABLE'; + } + + let linesColor; + if (atcStationAvail && flightNoAvail && fromToAvail) { + linesColor = '[color]cyan'; + } else { + linesColor = '[color]white'; + } + + let notificationMessage = ''; + if (mcdu.atsu.logonInProgress()) { + const seconds = Math.floor(mcdu.atsu.nextStationNotificationTime()); + const hours = Math.floor(seconds / 3600); + const minutes = Math.floor((seconds - hours * 3600) / 60); + const zeroPad = (num, places) => String(num).padStart(places, '0'); + + // check if the page is loaded again + if (store['atcCenter'] !== mcdu.atsu.nextStation()) { + store['atcCenter'] = mcdu.atsu.nextStation(); + } + + notificationMessage = `${store['atcCenter']} NOTIFIED ${`${zeroPad(hours, 2)}${zeroPad(minutes, 2)}Z`}[color]green`; + } else if (mcdu.atsu.currentStation() !== '') { + notificationMessage = `${mcdu.atsu.currentStation()}[color]green`; + } + + mcdu.setTemplate([ + ['NOTIFICATION'], + ['\xa0ATC FLT NBR'], + [flightNo], + [centerTitleLeft, centerTitleRight], + [atcStation, notifyButton, `---------${linesColor}`], + [''], + [''], + [''], + [notificationMessage], + [notificationStatus], + [''], + ['\xa0CONNECTION', 'CONNECTION\xa0'], + [''], + ]); + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (store['loginState'] === 1 && mcdu.atsu.nextStation() !== store['atcCenter']) { + mcdu.setScratchpadMessage(NXSystemMessages.systemBusy); + return; + } + + store['loginState'] = 0; + if (/^[A-Z0-9]{4}$/.test(value) === false) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } else if (mcdu.atsu.flightNumber().length === 0) { + mcdu.setScratchpadMessage(NXFictionalMessages.fltNbrMissing); + } else { + store['atcCenter'] = ''; + + mcdu.atsu.isRemoteStationAvailable(value).then((code) => { + if (code !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(code); + store['atcCenter'] = ''; + } else { + store['atcCenter'] = value; + } + + if (mcdu.page.Current === mcdu.page.ATCNotification) { + CDUAtcConnectionNotification.ShowPage(mcdu, store); + } + }); + } + + CDUAtcConnectionNotification.ShowPage(mcdu, store); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcConnection.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = async () => { + if (store['logonAllowed'] === true) { + store['loginState'] = 1; + + mcdu.atsu.logon(store['atcCenter']).then((code) => { + if (code === AtsuStatusCodes.Ok) { + // check if the login was successful + const interval = setInterval(() => { + if (!mcdu.atsu.logonInProgress()) { + if (mcdu.atsu.currentStation() === store['atcCenter']) { + store['loginState'] = 0; + } else { + store['loginState'] = 2; + } + + store['atcCenter'] = ''; + clearInterval(interval); + + if (mcdu.page.Current === mcdu.page.ATCNotification) { + CDUAtcConnectionNotification.ShowPage(mcdu, store); + } + } + }, 5000); + } else { + mcdu.addNewAtsuMessage(code); + } + }); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.mandatoryFields); + } + + CDUAtcConnectionNotification.ShowPage(mcdu, store); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + CDUAtcConnectionStatus.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ConnectionStatus.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ConnectionStatus.ts new file mode 100644 index 00000000000..9ffe4338a2a --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ConnectionStatus.ts @@ -0,0 +1,100 @@ +import { AtsuStatusCodes } from '@datalink/common'; +import { CDUAtcConnection } from './A320_Neo_CDU_ATC_Connection'; +import { CDUAtcConnectionNotification } from './A320_Neo_CDU_ATC_ConnectionNotification'; +import { NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcConnectionStatus { + static ShowPage( + mcdu: LegacyAtsuPageInterface, + store = { disconnectInProgress: false, disconnectAvail: false, disconnectConfirm: false }, + ) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCConnectionStatus; + + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.ATCConnectionStatus) { + CDUAtcConnectionStatus.ShowPage(mcdu, store); + } + }, mcdu.PageTimeout.Default); + + let currentStation = '-----------[color]white'; + let atcDisconnectHeadline = 'ALL ATC\xa0[color]cyan'; + let atcDisconnect = 'DISCONNECT\xa0[color]cyan'; + if (!store['disconnectInProgress']) { + if (mcdu.atsu.currentStation() !== '') { + currentStation = `${mcdu.atsu.currentStation()}[color]green`; + store['disconnectAvail'] = true; + + if (!store['disconnectConfirm']) { + atcDisconnect = 'DISCONNECT*[color]cyan'; + } else { + atcDisconnectHeadline = 'DISCONNECT\xa0[color]amber'; + atcDisconnect = 'CONFIRM*[color]amber'; + } + } else { + store['disconnectAvail'] = false; + } + } + + let nextStation = '-----------'; + if (mcdu.atsu.nextStation() !== '') { + nextStation = `${mcdu.atsu.nextStation()}[color]green`; + } + + mcdu.setTemplate([ + ['CONNECTION STATUS'], + ['\xa0ACTIVE ATC'], + [currentStation], + ['\xa0NEXT ATC', atcDisconnectHeadline], + [nextStation, atcDisconnect], + [''], + [''], + ['-------ADS-C: ARMED-------'], + ['\xa0SET OFF[color]inop'], + [''], + ['', 'ADS-C DETAIL>[color]inop'], + ['\xa0CONNECTION', ''], + [''], + ]); + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcConnection.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = () => { + if (!store['disconnectAvail']) { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else if (!store['disconnectConfirm']) { + store['disconnectConfirm'] = true; + CDUAtcConnectionStatus.ShowPage(mcdu, store); + } else if (!store['disconnectInProgress']) { + store['disconnectInProgress'] = true; + store['disconnectAvail'] = false; + CDUAtcConnectionStatus.ShowPage(mcdu, store); + + mcdu.atsu.logoff().then((code) => { + store['disconnectInProgress'] = false; + if (code !== AtsuStatusCodes.Ok) { + store['disconnectAvail'] = true; + mcdu.addNewAtsuMessage(code); + } else { + CDUAtcConnectionStatus.ShowPage(mcdu, store); + } + }); + } + }; + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + CDUAtcConnectionNotification.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_DepartReq.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_DepartReq.ts new file mode 100644 index 00000000000..9bc361dadcd --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_DepartReq.ts @@ -0,0 +1,289 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { AtsuStatusCodes, DclMessage } from '@datalink/common'; +import { CDU_SingleValueField } from '../../legacy/A320_Neo_CDU_Field'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcClearanceReq } from './A320_Neo_CDU_ATC_ClearanceReq'; +import { NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcDepartReq { + static CreateDataBlock() { + return { + firstCall: true, + callsign: '', + station: '', + stationManual: false, + from: '', + to: '', + atis: '', + gate: '', + freetext: ['', '', '', '', '', ''], + }; + } + + static CanSendData(store) { + return store.callsign !== '' && store.station !== '' && store.from !== '' && store.to !== '' && store.atis !== ''; + } + + static CreateMessage(store) { + const retval = new DclMessage(); + + retval.Callsign = store.callsign; + retval.Origin = store.from; + retval.Destination = store.to; + retval.AcType = 'A20N'; + retval.Atis = store.atis; + retval.Gate = store.gate; + retval.Freetext = store.freetext.filter((n) => n); + retval.Station = store.station; + + return retval; + } + + static ShowPage1(mcdu: LegacyAtsuPageInterface, store = CDUAtcDepartReq.CreateDataBlock()) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCDepartReq; + + if (store.firstCall && store.callsign === '') { + if (mcdu.atsu.flightNumber().length !== 0) { + store.callsign = mcdu.atsu.flightNumber(); + } + } + + // FIXME YIKES! ATSU cannot access this. + const activePlan = mcdu.flightPlanService.active; + + if (store.firstCall && store.from === '') { + if (activePlan.originAirport) { + store.from = activePlan.originAirport.ident; + } + } + + if (store.firstCall && store.to === '') { + if (activePlan.destinationAirport) { + store.to = activePlan.destinationAirport.ident; + } + } + + if (store.firstCall && store.station === '') { + if (mcdu.atsu.currentStation() !== '') { + store.station = mcdu.atsu.currentStation(); + } + } + + store.firstCall = false; + + let flightNo = '--------'; + let fromTo = '{amber}____/____{end}'; + const atis = new CDU_SingleValueField( + mcdu, + 'string', + store.atis, + { + clearable: true, + emptyValue: '{amber}_{end}', + suffix: '[color]cyan', + maxLength: 1, + isValid: (value: string) => { + return /^[A-Z()]*$/.test(value) === true; + }, + }, + (value: string) => { + store.atis = value; + CDUAtcDepartReq.ShowPage1(mcdu, store); + }, + ); + const gate = new CDU_SingleValueField( + mcdu, + 'string', + store.gate, + { + clearable: true, + emptyValue: '{cyan}[\xa0\xa0\xa0\xa0]{end}', + suffix: '[color]cyan', + maxLength: 4, + }, + (value: string) => { + store.gate = value; + CDUAtcDepartReq.ShowPage1(mcdu, store); + }, + ); + const freetext = new CDU_SingleValueField( + mcdu, + 'string', + store.freetext[0], + { + clearable: store.freetext[0].length !== 0, + emptyValue: + '{cyan}[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0]{end}', + suffix: '[color]white', + maxLength: 22, + }, + (value: string) => { + store.freetext[0] = value; + CDUAtcDepartReq.ShowPage1(mcdu, store); + }, + ); + + if (store.callsign) { + flightNo = `{green}${store.callsign}{end}`; + } + if (store.from !== '' && store.to !== '') { + fromTo = `{cyan}${store.from}/${store.to}{end}`; + + const atisReports = mcdu.atsu.atisReports(store.from); + if (atisReports.length !== 0 && atisReports[0].Information !== '') { + store.atis = atisReports[0].Information; + atis.setValue(store.atis); + } + } + + let station = '{amber}____{end}'; + if (store.station !== '') { + station = `{cyan}${store.station}{end}`; + if (!store.stationManual) { + station = `{small}${station}{end}`; + } + } + + // check if all required information are available to prepare the PDC message + let reqDisplButton = '{cyan}DCDU\xa0{end}'; + if (CDUAtcDepartReq.CanSendData(store)) { + reqDisplButton = '{cyan}DCDU*{end}'; + } + + mcdu.setTemplate([ + ['DEPART REQ'], + ['\xa0ATC FLT NBR', 'A/C TYPE\xa0'], + [flightNo, '{cyan}A20N{end}'], + ['\xa0FROM/TO', 'STATION\xa0'], + [fromTo, station], + ['\xa0GATE', 'ATIS CODE\xa0'], + [gate, atis], + ['-------FREE TEXT--------'], + [freetext], + ['', 'MORE\xa0'], + ['', 'FREE TEXT>'], + ['\xa0GROUND REQ', '{cyan}XFR TO\xa0{end}'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + store.from = ''; + store.to = ''; + CDUAtcDepartReq.ShowPage1(mcdu, store); + } else if (value) { + const airports = value.split('/'); + if (airports.length !== 2 || !/^[A-Z0-9]{4}$/.test(airports[0]) || !/^[A-Z0-9]{4}$/.test(airports[1])) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } else { + store.from = airports[0]; + store.to = airports[1]; + CDUAtcDepartReq.ShowPage1(mcdu, store); + } + } + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + CDUAtcDepartReq.ShowPage2(mcdu, store); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcClearanceReq.ShowPage(mcdu, 'GROUND'); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = async (value) => { + if (value === Keypad.clrValue) { + store.station = ''; + } else if (/^[A-Z0-9]{4}$/.test(value)) { + mcdu.atsu.isRemoteStationAvailable(value).then((code) => { + if (code !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(code); + } else { + store.station = value; + store.stationManual = true; + } + + if (mcdu.page.Current === mcdu.page.ATCDepartReq) { + CDUAtcDepartReq.ShowPage1(mcdu, store); + } + }); + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcDepartReq.CanSendData(store)) { + mcdu.atsu.registerMessages([CDUAtcDepartReq.CreateMessage(store)]); + CDUAtcDepartReq.ShowPage1(mcdu); + } + }; + } + + static ShowPage2(mcdu: LegacyAtsuPageInterface, store) { + mcdu.clearDisplay(); + + const freetextLines = []; + for (let i = 0; i < 5; ++i) { + freetextLines.push( + new CDU_SingleValueField( + mcdu, + 'string', + store.freetext[i + 1], + { + clearable: store.freetext[i + 1].length !== 0, + emptyValue: + '{cyan}[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0]{end}', + suffix: '[color]white', + maxLength: 22, + }, + (value) => { + store.freetext[i + 1] = value; + CDUAtcDepartReq.ShowPage2(mcdu, store); + }, + ), + ); + } + + // define the template + mcdu.setTemplate([ + ['FREE TEXT'], + [''], + [freetextLines[0]], + [''], + [freetextLines[1]], + [''], + [freetextLines[2]], + [''], + [freetextLines[3]], + [''], + [freetextLines[4]], + ['\xa0DEPART REQ'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcDepartReq.ShowPage1(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_FlightReq.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_FlightReq.ts new file mode 100644 index 00000000000..f1da9c2fa49 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_FlightReq.ts @@ -0,0 +1,126 @@ +import { FansMode } from '@datalink/common'; +import { CDUAtcClearanceReq } from './A320_Neo_CDU_ATC_ClearanceReq'; +import { CDUAtcMenu } from './A320_Neo_CDU_ATC_Menu'; +import { CDUAtcOceanicReq } from './A320_Neo_CDU_ATC_OceanicReq'; +import { CDUAtcSpeedRequest } from './A320_Neo_CDU_ATC_SpeedReq'; +import { CDUAtcContactRequest } from './FansA/A320_Neo_CDU_ATC_ContactRequest'; +import { CDUAtcLatRequestFansA } from './FansA/A320_Neo_CDU_ATC_LatRequest'; +import { CDUAtcProcedureRequest } from './FansA/A320_Neo_CDU_ATC_ProcedureRequest'; +import { CDUAtcTextFansA } from './FansA/A320_Neo_CDU_ATC_Text'; +import { CDUAtcVertRequestFansA } from './FansA/A320_Neo_CDU_ATC_VertRequest'; +import { CDUAtcLatRequestFansB } from './FansB/A320_Neo_CDU_ATC_LatRequest'; +import { CDUAtcVertRequestFansB } from './FansB/A320_Neo_CDU_ATC_VertRequest'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcFlightReq { + static ShowPage(mcdu: LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCFlightRequest; + + let procedure = ''; + let freeText = ''; + let contact = ''; + let clearance = ''; + if (mcdu.atsu.fansMode() === FansMode.FansA) { + procedure = ''], + [''], + [''], + [''], + [freeText], + [''], + ['', clearance], + ['\xa0ATC MENU'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcLatRequestFansA.ShowPage1(mcdu); + } else { + CDUAtcLatRequestFansB.ShowPage(mcdu); + } + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = () => { + CDUAtcSpeedRequest.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcProcedureRequest.ShowPage(mcdu); + } + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcTextFansA.ShowPage1(mcdu); + } + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcVertRequestFansA.ShowPage1(mcdu); + } else { + CDUAtcVertRequestFansB.ShowPage(mcdu); + } + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcContactRequest.ShowPage(mcdu); + } + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = () => { + CDUAtcOceanicReq.ShowPage1(mcdu); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcClearanceReq.ShowPage(mcdu, 'CLEARANCE'); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MaxUplinkDelay.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MaxUplinkDelay.ts new file mode 100644 index 00000000000..4c6dff471cf --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MaxUplinkDelay.ts @@ -0,0 +1,80 @@ +import { AtsuStatusCodes } from '@datalink/common'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcMenu } from './A320_Neo_CDU_ATC_Menu'; +import { NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcMaxUplinkDelay { + static ShowPage(mcdu: LegacyAtsuPageInterface, updateInProgress = false) { + mcdu.clearDisplay(); + + let activeAtc = '----'; + if (mcdu.atsu.currentStation() !== '') { + activeAtc = mcdu.atsu.currentStation(); + } + + let currentDelay = '\xa0NONE[color]cyan'; + if (mcdu.atsu.maxUplinkDelay !== -1) { + currentDelay = `\xa0${mcdu.atsu.maxUplinkDelay}[color]cyan`; + } + + mcdu.setTemplate([ + ['MAX UPLINK DELAY'], + [''], + [''], + ['MODIFY ONLY ON DEMAND OF'], + [`ACTIVE ATC : {green}${activeAtc}{end}`], + [''], + [''], + ['\xa0MAX UPLINK DELAY'], + [currentDelay], + [''], + [''], + ['\xa0ATC MENU'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else if (updateInProgress === false) { + if (value === Keypad.clrValue) { + mcdu.atsu.setMaxUplinkDelay(-1).then((status) => { + if (status !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(status); + } + CDUAtcMaxUplinkDelay.ShowPage(mcdu); + }); + CDUAtcMaxUplinkDelay.ShowPage(mcdu, true); + } else if (value) { + if (/^[0-9]{3}(S)*$/.test(value)) { + const delay = parseInt(value.replace('S', '')); + if (delay < 5 || delay > 999) { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + } else { + mcdu.atsu.setMaxUplinkDelay(delay).then((status) => { + if (status !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(status); + } + CDUAtcMaxUplinkDelay.ShowPage(mcdu); + }); + CDUAtcMaxUplinkDelay.ShowPage(mcdu, true); + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + } + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_Menu.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_Menu.ts new file mode 100644 index 00000000000..74cbb59a0a3 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_Menu.ts @@ -0,0 +1,142 @@ +import { FansMode } from '@datalink/common'; +import { CDUAtcAtisMenu } from './A320_Neo_CDU_ATC_AtisMenu'; +import { CDUAtcClearanceReq } from './A320_Neo_CDU_ATC_ClearanceReq'; +import { CDUAtcConnection } from './A320_Neo_CDU_ATC_Connection'; +import { CDUAtcFlightReq } from './A320_Neo_CDU_ATC_FlightReq'; +import { CDUAtcMessageModify } from './A320_Neo_CDU_ATC_MessageModify'; +import { CDUAtcMessageMonitoring } from './A320_Neo_CDU_ATC_MessageMonitoring'; +import { CDUAtcMessagesRecord } from './A320_Neo_CDU_ATC_MessagesRecord'; +import { CDUAtsuMenu } from './A320_Neo_CDU_ATSU_Menu'; +import { CDUAtcEmergencyFansA } from './FansA/A320_Neo_CDU_ATC_Emergency'; +import { CDUAtcUsualRequestFansA } from './FansA/A320_Neo_CDU_ATC_UsualRequest'; +import { CDUAtcEmergencyFansB } from './FansB/A320_Neo_CDU_ATC_Emergency'; +import { CDUAtcUsualRequestFansB } from './FansB/A320_Neo_CDU_ATC_UsualRequest'; +import { NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcMenu { + static ShowPage(mcdu: LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCMenu; + mcdu.activeSystem = 'ATSU'; + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.ATCMenu) { + CDUAtcMenu.ShowPage(mcdu); + } + }, mcdu.PageTimeout.Slow); + + let modif = ''; + if (mcdu.atsu.modificationMessage) { + modif = 'MODIFY>'; + } + + mcdu.setTemplate([ + ['ATC MENU'], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + ['[color]amber'], + ]); + + mcdu.leftInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = () => { + CDUAtcClearanceReq.ShowPage(mcdu, 'GROUND'); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = () => { + CDUAtcMessagesRecord.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = () => { + CDUAtcMessageMonitoring.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcConnection.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtsuMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcUsualRequestFansA.ShowPage(mcdu); + } else { + CDUAtcUsualRequestFansB.ShowPage(mcdu); + } + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = () => { + CDUAtcAtisMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcAtisMenu.ShowPage(mcdu); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.keyNotActive); + } + }; + + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = () => { + if (mcdu.atsu.modificationMessage) { + CDUAtcMessageModify.ShowPage(mcdu, mcdu.atsu.modificationMessage); + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcEmergencyFansA.ShowPage1(mcdu); + } else { + CDUAtcEmergencyFansB.ShowPage(mcdu); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_Message.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_Message.ts new file mode 100644 index 00000000000..03d59653ec2 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_Message.ts @@ -0,0 +1,125 @@ +import { AtsuMessageComStatus, AtsuMessageSerializationFormat } from '@datalink/common'; +import { CDUAtcMessageMonitoring } from './A320_Neo_CDU_ATC_MessageMonitoring'; +import { CDUAtcMessagesRecord } from './A320_Neo_CDU_ATC_MessagesRecord'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcMessage { + static TranslateCpdlcResponse(message) { + let retval; + + switch (message.Content[0].TypeId) { + case 'DM0': + retval = 'WILC'; + break; + case 'UM0': + case 'DM1': + retval = 'UNBL'; + break; + case 'UM1': + case 'DM2': + retval = 'STBY'; + break; + case 'UM3': + case 'DM3': + retval = 'ROGR'; + break; + case 'UM4': + case 'DM4': + retval = 'AFRM'; + break; + case 'UM5': + case 'DM5': + retval = 'NEG'; + break; + default: + return ''; + } + + let color = '{cyan}'; + if (message.ComStatus === AtsuMessageComStatus.Failed) { + color = '{red}'; + } + retval = `${color}{small}${retval}{end}{end}`; + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, messages, messageIndex, messageList, offset = 0) { + mcdu.clearDisplay(); + const message = messages[messageIndex]; + const lines = message.serialize(AtsuMessageSerializationFormat.FmsDisplay).split('\n'); + + // mark message as read + mcdu.atsu.messageRead(message.UniqueMessageID, false); + + const msgArrows = messages.length > 1 ? ' {}' : ''; + + if (lines.length > 8) { + let up = false; + let down = false; + if (lines[offset + 1]) { + mcdu.onUp = () => { + offset += 1; + CDUAtcMessage.ShowPage(mcdu, messages, messageIndex, messageList, offset); + }; + up = true; + } + if (lines[offset - 1]) { + mcdu.onDown = () => { + offset -= 1; + CDUAtcMessage.ShowPage(mcdu, messages, messageIndex, messageList, offset); + }; + down = true; + } + mcdu.setArrows(up, down, false, false); + } + + mcdu.setTemplate([ + ['ATC MSG DISPLAY'], + ['', `${messageIndex + 1}/${messages.length}${msgArrows}`], + [ + `{small}${lines[offset] ? lines[offset] : ''}{end}`, + `${offset === 0 ? CDUAtcMessage.TranslateCpdlcResponse(message) : ''}`, + ], + [`${lines[offset + 1] ? lines[offset + 1] : ''}`], + [`{small}${lines[offset + 2] ? lines[offset + 2] : ''}{end}`], + [`${lines[offset + 3] ? lines[offset + 3] : ''}`], + [`{small}${lines[offset + 4] ? lines[offset + 4] : ''}{end}`], + [`${lines[offset + 5] ? lines[offset + 5] : ''}`], + [`{small}${lines[offset + 6] ? lines[offset + 6] : ''}{end}`], + [`${lines[offset + 7] ? lines[offset + 7] : ''}`], + [`{small}${lines[offset + 8] ? lines[offset + 8] : ''}{end}`], + [`\xa0${messageList ? 'MSG RECORD' : 'MONITORED MSG'}`], + [' { + const nextMesssageIndex = messageIndex + 1; + if (nextMesssageIndex < messages.length) { + CDUAtcMessage.ShowPage(mcdu, messages, messageList, nextMesssageIndex); + } + }; + + mcdu.onPrevPage = () => { + const previousMesssageIndex = messageIndex - 1; + if (previousMesssageIndex >= 0) { + CDUAtcMessage.ShowPage(mcdu, messages, messageList, previousMesssageIndex); + } + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + if (messageList) { + CDUAtcMessagesRecord.ShowPage(mcdu); + } else { + CDUAtcMessageMonitoring.ShowPage(mcdu); + } + }; + + mcdu.onRightInput[5] = () => { + mcdu.atsu.printMessage(message); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MessageModify.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MessageModify.ts new file mode 100644 index 00000000000..44d45f569fb --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MessageModify.ts @@ -0,0 +1,618 @@ +import { + AtsuStatusCodes, + CpdlcMessageContentType, + CpdlcMessagesDownlink, + FansMode, + InputValidation, +} from '@datalink/common'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcMenu } from './A320_Neo_CDU_ATC_Menu'; +import { CDUAtcPositionReport } from './FansA/A320_Neo_CDU_ATC_PositionReport'; +import { CDUAtcTextFansA } from './FansA/A320_Neo_CDU_ATC_Text'; +import { CDUAtcTextFansB } from './FansB/A320_Neo_CDU_ATC_Text'; +import { CDUAtcMessageModifyUM131 } from './MessageModify/A320_Neo_CDU_ATC_MessageModifyUM131'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +const ModifyLookupTable = { + UM132: [ + { + response: 'DM33', + text: 'PRESENT POSITION', + type: CpdlcMessageContentType.Position, + textIndex: null, + valueIndex: 0, + emptyLength: 5, + }, + ], + UM133: [ + { + response: 'DM32', + text: 'PRESENT LEVEL', + type: CpdlcMessageContentType.Level, + textIndex: null, + valueIndex: 0, + emptyLength: 5, + }, + ], + UM134: [ + { + response: 'DM34', + text: 'PRESENT SPEED', + type: CpdlcMessageContentType.Speed, + textIndex: null, + valueIndex: 0, + emptyLength: 4, + }, + ], + UM135: [ + { + response: 'DM38', + text: 'ASSIGNED LEVEL', + type: CpdlcMessageContentType.Level, + textIndex: null, + valueIndex: 0, + emptyLength: 5, + }, + ], + UM136: [ + { + response: 'DM39', + text: 'ASSIGNED SPEED', + type: CpdlcMessageContentType.Speed, + textIndex: null, + valueIndex: 0, + emptyLength: 4, + }, + ], + UM137: [ + { + response: 'DM45', + text: 'ASSIGNED ROUTE', + type: CpdlcMessageContentType.Freetext, + textIndex: null, + valueIndex: 0, + emptyLength: 6, + }, + ], + UM138: [ + { + response: 'DM46', + text: 'REPORTED TIME', + type: CpdlcMessageContentType.Time, + textIndex: null, + valueIndex: 0, + emptyLength: 4, + }, + ], + UM139: [ + { + response: 'DM45', + text: 'REPORTED WAYPOINT', + type: CpdlcMessageContentType.Position, + textIndex: null, + valueIndex: 0, + emptyLength: 5, + }, + ], + UM140: [ + { + response: 'DM42', + text: 'NEXT WAYPOINT', + type: CpdlcMessageContentType.Position, + textIndex: null, + valueIndex: 0, + emptyLength: 5, + }, + ], + UM141: [ + { + response: 'DM43', + text: 'NEXT WAYPOINT ETA', + type: CpdlcMessageContentType.Time, + textIndex: null, + valueIndex: 0, + emptyLength: 4, + }, + ], + UM142: [ + { + response: 'DM44', + text: 'ENSUING WAYPOINT', + type: CpdlcMessageContentType.Position, + textIndex: null, + valueIndex: 0, + emptyLength: 5, + }, + ], + UM144: [ + { + response: 'DM47', + text: 'PRESENT SQUAWK', + type: CpdlcMessageContentType.Squawk, + textIndex: null, + valueIndex: 0, + emptyLength: 4, + }, + ], + UM145: [ + { + response: 'DM35', + text: 'PRESENT HEADING', + type: CpdlcMessageContentType.Degree, + textIndex: null, + valueIndex: 0, + emptyLength: 3, + }, + ], + UM146: [ + { + response: 'DM36', + text: 'PRESENT GROUND TRACK', + type: CpdlcMessageContentType.Degree, + textIndex: null, + valueIndex: 0, + emptyLength: 3, + }, + ], + UM148: [ + { + response: 'DM81', + text: 'CAN %s AT', + type: CpdlcMessageContentType.Time, + textIndex: 0, + valueIndex: 1, + emptyLength: 5, + }, + { + response: 'DM67', + text: 'CAN %s NOW', + type: CpdlcMessageContentType.Unknown, + textIndex: 0, + valueIndex: null, + emptyLength: 0, + freetext: 'WE CAN ACCEPT %s NOW', + }, + { + response: 'DM82', + text: 'CANNOT %s', + type: CpdlcMessageContentType.Unknown, + textIndex: 0, + valueIndex: null, + emptyLength: 0, + }, + ], + UM151: [ + { + response: 'DM83', + text: 'CAN %s AT', + type: CpdlcMessageContentType.Time, + textIndex: 0, + valueIndex: 1, + emptyLength: 5, + }, + { + response: 'DM67', + text: 'CAN %s NOW', + type: CpdlcMessageContentType.Unknown, + textIndex: 0, + valueIndex: null, + emptyLength: 0, + freetext: 'WE CAN ACCEPT %s NOW', + }, + { + response: 'DM94', + text: 'CANNOT %s', + type: CpdlcMessageContentType.Unknown, + textIndex: 0, + valueIndex: null, + emptyLength: 0, + }, + ], + UM152: [ + { + response: 'DM85', + text: 'CAN %s AT', + type: CpdlcMessageContentType.Time, + textIndex: 0, + valueIndex: 1, + emptyLength: 5, + }, + { + response: 'DM67', + text: 'CAN %s NOW', + type: CpdlcMessageContentType.Unknown, + textIndex: 0, + valueIndex: null, + emptyLength: 0, + freetext: 'WE CAN ACCEPT %s NOW', + }, + { + response: 'DM86', + text: 'CANNOT %s', + type: CpdlcMessageContentType.Unknown, + textIndex: 0, + valueIndex: null, + emptyLength: 0, + }, + ], + UM181: [ + { + response: 'DM67', + text: 'DISTANCE TO %s', + type: CpdlcMessageContentType.Distance, + textIndex: 0, + valueIndex: 0, + emptyLength: 3, + freetext: 'DISTANCE TO %s %v', + }, + ], + UM182: [ + { + response: 'DM79', + text: 'ATIS', + type: CpdlcMessageContentType.Atis, + textIndex: null, + valueIndex: 0, + emptyLength: 1, + }, + ], + UM184: [ + { + response: 'DM67', + text: 'DISTANCE TO %s', + type: CpdlcMessageContentType.Distance, + textIndex: 1, + valueIndex: 0, + emptyLength: 3, + freetext: 'DISTANCE TO %s IS %v', + }, + ], + UM228: [ + { + response: 'DM104', + text: 'ETA TO %s', + type: CpdlcMessageContentType.Time, + textIndex: 0, + valueIndex: 0, + emptyLength: 4, + }, + ], + UM231: [ + { + response: 'DM106', + text: 'PREFERRED LEVEL', + type: CpdlcMessageContentType.Level, + textIndex: null, + valueIndex: 0, + emptyLength: 5, + }, + ], + UM232: [ + { + response: 'DM109', + text: 'TIME TO TOP OF DESCENT', + type: CpdlcMessageContentType.Time, + textIndex: null, + valueIndex: 0, + emptyLength: 5, + }, + ], +}; + +export class CDUAtcMessageModify { + static CreateDataBlock(message) { + const lutEntry = ModifyLookupTable[message.Content[0].TypeId]; + + return { + value: + message.Response.Content[0].Content.length > lutEntry[0].valueIndex && + message.Response.Content[0].TypeId !== 'DM67' + ? message.Response.Content[0].Content[lutEntry[0].valueIndex].Value + : '', + modified: false, + multiSelection: lutEntry.length > 1, + selectedToggles: [false, false], + }; + } + + static CreateDescriptionLine(message, entry) { + if (entry.textIndex !== null) { + return entry.text.replace('%s', message.Content[0].Content[entry.textIndex].Value); + } + return entry.text; + } + + static CreateToggleBasedField(message, data, entry, index) { + const text = CDUAtcMessageModify.CreateDescriptionLine(message, entry); + + if (data.selectedToggles[index - 1]) { + return `{cyan}\xa0${text}{end}`; + } + return `{cyan}{{end}{white}${text}{end}`; + } + + static CreateVisualization( + message: { Content: { TypeId: string | number }[] }, + data: { selectedToggles: any[]; value: string; modified: any }, + ) { + const lutEntry = ModifyLookupTable[message.Content[0].TypeId]; + const visualization: [string[], string, string] = [['', ''], '', '']; + + const color = !data.selectedToggles[0] && !data.selectedToggles[1] ? '{cyan}' : '{white}'; + visualization[0][0] = `${color}\xa0${CDUAtcMessageModify.CreateDescriptionLine(message, lutEntry[0])}{end}`; + if (data.value !== '') { + const prefix = !data.modified ? '{small}' : ''; + const suffix = !data.modified ? '{end}' : ''; + visualization[0][1] = `{cyan}${prefix}${data.value}${suffix}{end}`; + } else { + visualization[0][1] = `{amber}${'_'.repeat(lutEntry[0].emptyLength)}{end}`; + } + + if (lutEntry.length > 1) { + visualization[1] = CDUAtcMessageModify.CreateToggleBasedField(message, data, lutEntry[1], 1); + visualization[2] = CDUAtcMessageModify.CreateToggleBasedField(message, data, lutEntry[2], 2); + } + + return visualization; + } + + static ValidateScratchpadValue(message, value) { + const lutEntry = ModifyLookupTable[message.Content[0].TypeId]; + + if (lutEntry[0].type === CpdlcMessageContentType.Position) { + return InputValidation.validateScratchpadPosition(value); + } + if (lutEntry[0].type === CpdlcMessageContentType.Speed) { + return InputValidation.validateScratchpadSpeed(value); + } + if (lutEntry[0].type === CpdlcMessageContentType.Level) { + return InputValidation.validateScratchpadAltitude(value); + } + if (lutEntry[0].type === CpdlcMessageContentType.Time) { + return InputValidation.validateScratchpadTime(value); + } + if (lutEntry[0].type === CpdlcMessageContentType.Freetext) { + return AtsuStatusCodes.Ok; + } + if (lutEntry[0].type === CpdlcMessageContentType.Squawk) { + return InputValidation.validateScratchpadSquawk(value); + } + if (lutEntry[0].type === CpdlcMessageContentType.Degree) { + return InputValidation.validateScratchpadDegree(value); + } + if (lutEntry[0].type === CpdlcMessageContentType.Atis) { + return InputValidation.validateScratchpadAtis(value); + } + if (lutEntry[0].type === CpdlcMessageContentType.Distance) { + return InputValidation.validateScratchpadDistance(value); + } + + return AtsuStatusCodes.UnknownMessage; + } + + static FormatScratchpadValue(message, value) { + const lutEntry = ModifyLookupTable[message.Content[0].TypeId]; + + if (lutEntry[0].type === CpdlcMessageContentType.Speed) { + return InputValidation.formatScratchpadSpeed(value); + } + if (lutEntry[0].type === CpdlcMessageContentType.Level) { + return InputValidation.formatScratchpadAltitude(value); + } + if (lutEntry[0].type === CpdlcMessageContentType.Distance) { + return InputValidation.formatScratchpadDistance(value); + } + + return value; + } + + static CanUpdateMessage(data) { + return data.value !== '' || data.selectedToggles[0] || data.selectedToggles[1]; + } + + static UpdateResponseMessage(message, data) { + const lutEntry = ModifyLookupTable[message.Content[0].TypeId]; + + let lutIndex = 0; + if (data.selectedToggles[0]) { + lutIndex = 1; + } else if (data.selectedToggles[1]) { + lutIndex = 2; + } + + const newContent = CpdlcMessagesDownlink[lutEntry[lutIndex].response][1].deepCopy(); + if (newContent.TypeId === 'DM67') { + let freetext = lutEntry[lutIndex].freetext; + if (lutEntry[lutIndex].textIndex !== null) { + freetext = freetext.replace('%s', message.Content[0].Content[lutEntry[lutIndex].textIndex].Value); + } + if (lutEntry[lutIndex].valueIndex !== null) { + freetext = freetext.replace('%v', data.value); + } + newContent.Content[0].Value = freetext; + } else if (newContent.TypeId === 'DM104') { + newContent.Content[0].Value = message.Content[0].Content[lutEntry[lutIndex].textIndex].Value; + newContent.Content[1].Value = data.value; + } else { + newContent.Content[lutEntry[lutIndex].valueIndex].Value = data.value; + for (let i = 0; i < lutEntry[lutIndex].valueIndex; ++i) { + newContent.Content[i].Value = message.Content[0].Content[lutEntry[lutIndex].textIndex + i].Value; + } + } + + message.Response.Content = [newContent]; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, message, data = null) { + mcdu.page.Current = mcdu.page.ATCModify; + + if (message.Content[0].TypeId === 'UM147') { + // modify the position report + CDUAtcPositionReport.ShowPage1(mcdu, message); + return; + } else if (message.Content[0].TypeId === 'UM131') { + // report persons on board and fuel remaining + CDUAtcMessageModifyUM131.ShowPage(mcdu, message); + return; + } + + if (!data) { + data = CDUAtcMessageModify.CreateDataBlock(message); + } + const visualization = CDUAtcMessageModify.CreateVisualization(message, data); + + let cancel = '\xa0CANCEL'; + let addText = 'ADD TEXT\xa0'; + let transfer = 'DCDU\xa0'; + if (CDUAtcMessageModify.CanUpdateMessage(data)) { + cancel = '*CANCEL'; + addText = 'ADD TEXT>'; + transfer = 'DCDU*'; + } + + mcdu.setTemplate([ + ['MODIFY'], + [data.multiSelection ? visualization[0][0] : ''], + [data.multiSelection ? visualization[0][1] : ''], + [!data.multiSelection ? visualization[0][0] : ''], + [!data.multiSelection ? visualization[0][1] : ''], + [''], + [visualization[1]], + [''], + [visualization[2]], + ['{cyan}\xa0PAGE{end}'], + [`{cyan}${cancel}{end}`, `{white}${addText}{end}`], + ['\xa0ATC MENU', '{cyan}XFR TO\xa0{end}'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (data.multiSelection) { + if (value === Keypad.clrValue) { + data.modified = true; + data.value = ''; + } else if (value) { + const error = CDUAtcMessageModify.ValidateScratchpadValue(message, value); + if (error === AtsuStatusCodes.Ok) { + data.value = CDUAtcMessageModify.FormatScratchpadValue(message, value); + data.modified = true; + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcMessageModify.ShowPage(mcdu, message, data); + } + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (!data.multiSelection) { + if (value === Keypad.clrValue) { + data.modified = true; + data.value = ''; + } else if (value) { + const error = CDUAtcMessageModify.ValidateScratchpadValue(message, value); + if (error === AtsuStatusCodes.Ok) { + data.value = CDUAtcMessageModify.FormatScratchpadValue(message, value); + data.modified = true; + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcMessageModify.ShowPage(mcdu, message, data); + } + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (data.multiSelection) { + if (value === Keypad.clrValue) { + data.selectedToggles[0] = false; + } else { + data.selectedToggles[0] = true; + data.selectedToggles[1] = false; + } + CDUAtcMessageModify.ShowPage(mcdu, message, data); + } + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + if (data.multiSelection) { + if (value === Keypad.clrValue) { + data.selectedToggles[1] = false; + } else { + data.selectedToggles[0] = false; + data.selectedToggles[1] = true; + } + CDUAtcMessageModify.ShowPage(mcdu, message, data); + } + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + if (data.multiSelection) { + if (value === Keypad.clrValue) { + data.selectedToggles[1] = false; + } else { + data.selectedToggles[0] = false; + data.selectedToggles[1] = true; + } + CDUAtcMessageModify.ShowPage(mcdu, message, data); + } + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + if (CDUAtcMessageModify.CanUpdateMessage(data)) { + mcdu.atsu.updateMessage(message); + CDUAtcMenu.ShowPage(mcdu); + } + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcMessageModify.CanUpdateMessage(data)) { + CDUAtcMessageModify.UpdateResponseMessage(message, data); + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcTextFansA.ShowPage1(mcdu, [message]); + } else { + CDUAtcTextFansB.ShowPage(mcdu, [message]); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcMessageModify.CanUpdateMessage(data)) { + CDUAtcMessageModify.UpdateResponseMessage(message, data); + mcdu.atsu.updateMessage(message); + CDUAtcMenu.ShowPage(mcdu); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MessageMonitoring.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MessageMonitoring.ts new file mode 100644 index 00000000000..83959bf7043 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MessageMonitoring.ts @@ -0,0 +1,174 @@ +//import { UplinkMessageStateMachine } from '@datalink/atc'; +import { AtsuMessageDirection, AtsuMessageSerializationFormat } from '@datalink/common'; +import { CDUAtcMenu } from './A320_Neo_CDU_ATC_Menu'; +import { CDUAtcMessage } from './A320_Neo_CDU_ATC_Message'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcMessageMonitoring { + static TranslateCpdlcResponse(response) { + if (response) { + switch (response.Content[0].TypeId) { + case 'DM0': + return 'WILC'; + case 'UM0': + case 'DM1': + return 'UNBL'; + case 'UM1': + case 'DM2': + return 'STBY'; + case 'UM3': + case 'DM3': + return 'ROGR'; + case 'UM4': + case 'DM4': + return 'AFRM'; + case 'UM5': + case 'DM5': + return 'NEG'; + default: + return ''; + } + } + + return ''; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, messages = null, offset = 0, cancelIndex = -1) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCMessageMonitoring; + + if (!messages) { + messages = mcdu.atsu.monitoredMessages(); + } + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.ATCMessageMonitoring) { + CDUAtcMessageMonitoring.ShowPage(mcdu, messages, offset, cancelIndex); + } + }, mcdu.PageTimeout.Slow); + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.ATCMessageMonitoring) { + CDUAtcMessageMonitoring.ShowPage(mcdu, messages, offset, cancelIndex); + } + }, mcdu.PageTimeout.Slow); + + let cancelHeader = ''; + let cancelMessage = ''; + if (cancelIndex > -1) { + cancelHeader = '{yellow}CONFIRM CANCEL\xa0{end}'; + cancelMessage = '{yellow}MONITORING*{end}'; + } + + const msgHeadersLeft = [], + msgHeadersRight = [], + msgStart = []; + msgHeadersLeft.length = msgHeadersRight.length = msgStart.length = 4; + for (let i = 0; i < 5; ++i) { + let headerLeft = '', + headerRight = '', + contentStart = ''; + + if (messages.length > offset + i && messages[offset + i]) { + headerLeft = `${messages[offset + i].Timestamp.mailboxTimestamp()} ${messages[offset + i].Direction === AtsuMessageDirection.Uplink ? 'FROM' : 'TO'} `; + headerLeft += messages[offset + i].Station; + headerRight = CDUAtcMessageMonitoring.TranslateCpdlcResponse(messages[offset + i].Response); + + // ignore the headline with the station and the timestamp + const lines = messages[offset + i].serialize(AtsuMessageSerializationFormat.FmsDisplayMonitored).split('\n'); + let firstLine = 'CPDLC'; + if (lines.length >= 2) { + firstLine = messages[offset + i].serialize(AtsuMessageSerializationFormat.FmsDisplayMonitored).split('\n')[1]; + } + if (firstLine.length <= 19) { + contentStart = firstLine; + } else { + firstLine.split(' ').forEach((word) => { + if (contentStart.length + word.length + 1 < 19) { + contentStart += `${word}\xa0`; + } + }); + } + } + + msgHeadersLeft[i] = headerLeft; + msgHeadersRight[i] = headerRight; + msgStart[i] = `${contentStart.length !== 0 ? '<' : ''}${contentStart}`; + } + + let left = false, + right = false; + if (messages.length > offset + 4) { + mcdu.onNextPage = () => { + CDUAtcMessageMonitoring.ShowPage(mcdu, messages, offset + 4, -1); + }; + right = true; + } + if (offset > 0) { + mcdu.onPrevPage = () => { + CDUAtcMessageMonitoring.ShowPage(mcdu, messages, offset - 4, -1); + }; + left = true; + } + mcdu.setArrows(false, false, left, right); + + mcdu.setTemplate([ + ['MONITORED MSG'], + [msgHeadersLeft[0], msgHeadersRight[0]], + [`${messages.length !== 0 ? msgStart[0] : 'NO MESSAGES'}`, `${msgStart[0] !== '' ? '{cyan}CANCEL*{end}' : ''}`], + [msgHeadersLeft[1], msgHeadersRight[1]], + [msgStart[1], `${msgStart[1] !== '' ? '{cyan}CANCEL*{end}' : ''}`], + [msgHeadersLeft[2], msgHeadersRight[2]], + [msgStart[2], `${msgStart[2] !== '' ? '{cyan}CANCEL*{end}' : ''}`], + [msgHeadersLeft[3], msgHeadersRight[3]], + [msgStart[3], `${msgStart[3] !== '' ? '{cyan}CANCEL*{end}' : ''}`], + [''], + [''], + ['\xa0ATC MENU', cancelHeader], + [' { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[i] = () => { + if (messages[offset + i]) { + CDUAtcMessage.ShowPage(mcdu, messages, offset + i, false); + } + }; + + mcdu.rightInputDelay[i] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onRightInput[i] = () => { + if (messages[offset + i]) { + CDUAtcMessageMonitoring.ShowPage(mcdu, messages, offset, offset + i); + } + }; + } + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (cancelIndex > -1) { + // FIXME this should only live on the systems host? + //UplinkMessageStateMachine.update(mcdu.atsu, messages[cancelIndex], false); + mcdu.atsu.updateMessage(messages[cancelIndex]); + CDUAtcMessageMonitoring.ShowPage(mcdu); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MessagesRecord.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MessagesRecord.ts new file mode 100644 index 00000000000..3ca4ff17cbf --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_MessagesRecord.ts @@ -0,0 +1,160 @@ +import { AtsuMessageDirection, AtsuMessageSerializationFormat } from '@datalink/common'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcMenu } from './A320_Neo_CDU_ATC_Menu'; +import { CDUAtcMessage } from './A320_Neo_CDU_ATC_Message'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcMessagesRecord { + static TranslateCpdlcResponse(response) { + if (response) { + if (response.Content[0].TypeId === 'DM0') { + return 'WILC'; + } + if (response.Content[0].TypeId === 'UM0' || response.Content[0].TypeId === 'DM1') { + return 'UNBL'; + } + if (response.Content[0].TypeId === 'UM1' || response.Content[0].TypeId === 'DM2') { + return 'STBY'; + } + if (response.Content[0].TypeId === 'UM3' || response.Content[0].TypeId === 'DM3') { + return 'ROGR'; + } + if (response.Content[0].TypeId === 'UM4' || response.Content[0].TypeId === 'DM4') { + return 'AFRM'; + } + if (response.Content[0].TypeId === 'UM5' || response.Content[0].TypeId === 'DM5') { + return 'NEG'; + } + } + + return ''; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, messages = null, offset = 0, confirmErase = false) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCMessageRecord; + + if (!messages) { + messages = mcdu.atsu.atcMessages(); + } + + // regular update due to showing dynamic data on this page + mcdu.SelfPtr = setTimeout(() => { + if (mcdu.page.Current === mcdu.page.ATCMessageRecord) { + CDUAtcMessagesRecord.ShowPage(mcdu, messages, offset, confirmErase); + } + }, mcdu.PageTimeout.Slow); + + let eraseRecordTitle = '\xa0MSG RECORD'; + let eraseRecordButton = '*ERASE'; + if (confirmErase) { + eraseRecordTitle = '\xa0ERASE MSG RECORD'; + eraseRecordButton = '*CONFIRM'; + } + + const msgHeadersLeft = [], + msgHeadersRight = [], + msgStart = []; + msgHeadersLeft.length = msgHeadersRight.length = msgStart.length = 4; + for (let i = 0; i < 5; ++i) { + let headerLeft = '', + headerRight = '', + contentStart = ''; + + if (messages.length > offset + i && messages[offset + i]) { + headerLeft = `${messages[offset + i].Timestamp.mailboxTimestamp()} ${messages[offset + i].Direction === AtsuMessageDirection.Uplink ? 'FROM' : 'TO'} `; + headerLeft += messages[offset + i].Station; + headerRight = CDUAtcMessagesRecord.TranslateCpdlcResponse(messages[offset + i].Response); + + // ignore the headline with the station and the timestamp + const lines = messages[offset + i].serialize(AtsuMessageSerializationFormat.Printer).split('\n'); + let firstLine = 'CPDLC'; + if (lines.length >= 2) { + firstLine = messages[offset + i].serialize(AtsuMessageSerializationFormat.Printer).split('\n')[1]; + } + if (firstLine.length <= 24) { + contentStart = firstLine; + } else { + firstLine.split(' ').forEach((word) => { + if (contentStart.length + word.length + 1 < 24) { + contentStart += `${word}\xa0`; + } + }); + } + } + + msgHeadersLeft[i] = headerLeft; + msgHeadersRight[i] = headerRight; + msgStart[i] = `${contentStart.length !== 0 ? '<' : ''}${contentStart}`; + } + + let left = false, + right = false; + if (messages.length > offset + 4) { + mcdu.onNextPage = () => { + CDUAtcMessagesRecord.ShowPage(mcdu, messages, offset + 4, false); + }; + right = true; + } + if (offset > 0) { + mcdu.onPrevPage = () => { + CDUAtcMessagesRecord.ShowPage(mcdu, messages, offset - 4, false); + }; + left = true; + } + mcdu.setArrows(false, false, left, right); + + mcdu.setTemplate([ + ['MSG RECORD'], + [msgHeadersLeft[0], `{big}${msgHeadersRight[0]}{end}`], + [`${messages.length !== 0 ? msgStart[0] : 'NO MESSAGES'}`], + [msgHeadersLeft[1], `{big}${msgHeadersRight[1]}{end}`], + [msgStart[1]], + [msgHeadersLeft[2], `{big}${msgHeadersRight[2]}{end}`], + [msgStart[2]], + [msgHeadersLeft[3], `{big}${msgHeadersRight[3]}{end}`], + [msgStart[3]], + [eraseRecordTitle], + [eraseRecordButton], + ['\xa0ATC MENU', 'MSG RECORD\xa0[color]inop'], + [' { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[i] = (value) => { + if (messages[offset + i]) { + if (value === Keypad.clrValue) { + mcdu.atsu.removeMessage(messages[offset + i].UniqueMessageID, false); + CDUAtcMessagesRecord.ShowPage(mcdu, null, offset, false); + } else { + CDUAtcMessage.ShowPage(mcdu, messages, offset + i, true); + } + } + }; + } + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + if (messages.length !== 0) { + if (!confirmErase) { + CDUAtcMessagesRecord.ShowPage(mcdu, messages, offset, true); + } else { + mcdu.atsu.cleanupAtcMessages(); + CDUAtcMessagesRecord.ShowPage(mcdu, null, 0, false); + } + } + }; + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_OceanicReq.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_OceanicReq.ts new file mode 100644 index 00000000000..efe5fb717a7 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_OceanicReq.ts @@ -0,0 +1,379 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { OclMessage } from '@datalink/common'; +import { CDU_SingleValueField } from '../../legacy/A320_Neo_CDU_Field'; +import { CDUAtcFlightReq } from './A320_Neo_CDU_ATC_FlightReq'; +import { McduMessage, NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +export class CDUAtcOceanicReq { + static CreateDataBlock() { + return { + firstCall: true, + callsign: null, + entryPoint: null, + entryTime: null, + requestedMach: null, + requestedFlightlevel: null, + freetext: ['', '', '', '', '', ''], + }; + } + + static CanSendData(mcdu, data) { + if (!data.callsign) { + return false; + } + if (!mcdu.flightPlanService.active.destinationAirport) { + return false; + } + if (mcdu.atsu.currentStation() === '') { + return false; + } + return data.entryPoint && data.entryTime && data.requestedMach && data.requestedFlightlevel; + } + + static CreateMessage(mcdu, data) { + const retval = new OclMessage(); + + retval.Callsign = data.callsign; + retval.Destination = mcdu.flightPlanService.active.destinationAirport.ident; + retval.EntryPoint = data.entryPoint; + retval.EntryTime = data.entryTime; + retval.RequestedMach = data.requestedMach; + retval.RequestedFlightlevel = data.requestedFlightlevel; + retval.Freetext = data.freetext.filter((n) => n); + retval.Station = mcdu.atsu.currentStation(); + + return retval; + } + + static WaypointOnRoute(mcdu, ident) { + const activePlan = mcdu.flightPlanService.active; + + const totalWaypointsCount = activePlan.legCount; + const wptsListIndex = activePlan.activeLegIndex; + + let i = 0; + + while (i < totalWaypointsCount && i + wptsListIndex < totalWaypointsCount) { + const leg = activePlan.elementAt(i + wptsListIndex); + + if (leg && leg.isDiscontinuity === false && leg.ident === ident) { + return true; + } + + i++; + } + + return false; + } + + static CalculateEntryPointETA(mcdu, ident) { + // TODO this currently does not work as computeWaypointStatistics returns dummy values. Needs a refactor of predictions (fms-v2) + + const activePlan = mcdu.flightPlanService.active; + + let retval = ''; + const stats = activePlan.computeWaypointStatistics(); + stats.forEach((value) => { + if (value.ident === ident && retval === '') { + const eta = value.etaFromPpos; + const hours = Math.floor(eta / 3600); + const minutes = Math.floor(eta / 60) % 60; + retval = `${hours.toString().padStart(2, '0')}${minutes.toString().padStart(2, '0')}Z`; + } + }); + + return retval; + } + + static ShowPage1(mcdu: LegacyAtsuPageInterface, store = CDUAtcOceanicReq.CreateDataBlock()) { + mcdu.clearDisplay(); + + let flightNo = '{white}-------{end}'; + let atcStation = '{white}----{end}'; + + const entryTime = new CDU_SingleValueField( + mcdu, + 'string', + store.entryTime, + { + clearable: true, + emptyValue: '{amber}_____{end}', + suffix: '[color]cyan', + maxLength: 5, + isValid: (value: string) => { + if (value.length !== 4 && value.length !== 5) { + return false; + } + + let check = value; + if (value.length === 5) { + if (value[4] !== 'Z') { + return false; + } + check = value.substring(0, 4); + } + if (!/^[0-9()]*$/.test(check)) { + return false; + } + + const asInt = parseInt(check); + return asInt <= 2359 && asInt >= 0; + }, + }, + (value: string | null) => { + if (value.length === 4) { + store.entryTime = `${value}Z`; + } else { + store.entryTime = value; + } + CDUAtcOceanicReq.ShowPage1(mcdu, store); + }, + ); + const entryPoint = new CDU_SingleValueField( + mcdu, + 'string', + store.entryPoint, + { + clearable: true, + emptyValue: '{amber}_______{end}', + suffix: '[color]cyan', + }, + (value: string | null) => { + const type = CDUAtcOceanicReq.waypointType(value); + if (type[0] === -1) { + mcdu.setScratchpadMessage(type[1]); + } else if (type[0] === 1) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } else { + store.entryPoint = value; + if (CDUAtcOceanicReq.WaypointOnRoute(mcdu, value)) { + store.entryTime = CDUAtcOceanicReq.CalculateEntryPointETA(mcdu, value); + if (store.entryTime !== '') { + entryTime.setValue(store.entryTime); + } else { + entryTime.clearValue(); + } + } else { + store.entryTime = ''; + entryTime.clearValue(); + } + } + + CDUAtcOceanicReq.ShowPage1(mcdu, store); + }, + ); + const requestedMach = new CDU_SingleValueField( + mcdu, + 'string', + store.requestedMach, + { + clearable: true, + emptyValue: '{amber}___{end}', + suffix: '[color]cyan', + maxLength: 3, + isValid: (value: string) => { + if (value && /^M*.[0-9]{1,2}$/.test(value)) { + const split = value.split('.'); + let number = parseInt(split.length > 0 && split[1]); + if (number < 10) { + number *= 10; + } + return number >= 61 && number <= 92; + } + return false; + }, + }, + (value: string | null) => { + if (value) { + const split = value.split('.'); + let number = parseInt(split.length > 0 && split[1]); + if (number < 10) { + number *= 10; + } + store.requestedMach = `M.${number}`; + CDUAtcOceanicReq.ShowPage1(mcdu, store); + } + }, + ); + const requestedFlightlevel = new CDU_SingleValueField( + mcdu, + 'string', + store.requestedFlightlevel, + { + clearable: true, + emptyValue: '{amber}_____{end}', + suffix: '[color]cyan', + maxLength: 5, + isValid: (value: string) => { + if (/^(FL)*[0-9]{2,3}$/.test(value)) { + let level = 0; + if (value.startsWith('FL')) { + level = parseInt(value.substring(2, value.length)); + } else { + level = parseInt(value); + } + return level >= 30 && level <= 410; + } + return false; + }, + }, + (value: string | null) => { + if (value.startsWith('FL')) { + store.requestedFlightlevel = value; + } else { + const zeroPad = (str, places) => str.padStart(places, '0'); + store.requestedFlightlevel = `FL${zeroPad(value, 3)}`; + } + CDUAtcOceanicReq.ShowPage1(mcdu, store); + }, + ); + const freetext = new CDU_SingleValueField( + mcdu, + 'string', + store.freetext[0], + { + clearable: store.freetext[0].length !== 0, + emptyValue: + '{cyan}[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0]{end}', + suffix: '[color]white', + maxLength: 22, + }, + (value: string | null) => { + store.freetext[0] = value; + CDUAtcOceanicReq.ShowPage1(mcdu, store); + }, + ); + + if (store.firstCall && !store.callsign) { + if (mcdu.atsu.flightNumber().length !== 0) { + store.callsign = mcdu.atsu.flightNumber(); + } + } + store.firstCall = false; + + if (store.callsign) { + flightNo = `{green}${store.callsign}{end}`; + } + if (mcdu.atsu.currentStation() !== '') { + atcStation = `{cyan}${mcdu.atsu.currentStation()}{end}`; + } + + // check if all required information are available to prepare the PDC message + let reqDisplButton = '{cyan}DCDU\xa0{end}'; + if (CDUAtcOceanicReq.CanSendData(mcdu, store)) { + reqDisplButton = '{cyan}DCDU*{end}'; + } + + mcdu.setTemplate([ + ['OCEANIC REQ'], + ['\xa0ATC FLT NBR', 'OCEAN ATC\xa0'], + [flightNo, atcStation], + ['\xa0ENTRY-POINT', 'AT TIME\xa0'], + [entryPoint, entryTime], + ['\xa0REQ MACH', 'REQ FL\xa0'], + [requestedMach, requestedFlightlevel], + ['---------FREE TEXT---------'], + [freetext], + ['', 'MORE\xa0'], + ['', 'FREE TEXT>'], + ['\xa0FLIGHT REQ', '{cyan}XFR TO\xa0{end}'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + CDUAtcOceanicReq.ShowPage2(mcdu, store); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcOceanicReq.CanSendData(mcdu, store)) { + mcdu.atsu.registerMessages([CDUAtcOceanicReq.CreateMessage(mcdu, store)]); + CDUAtcOceanicReq.ShowPage1(mcdu); + } + }; + } + + static ShowPage2(mcdu: LegacyAtsuPageInterface, store) { + mcdu.clearDisplay(); + + const freetextLines = []; + for (let i = 0; i < 5; ++i) { + freetextLines.push( + new CDU_SingleValueField( + mcdu, + 'string', + store.freetext[i + 1], + { + clearable: store.freetext[i + 1].length !== 0, + emptyValue: + '{cyan}[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0]{end}', + suffix: '[color]white', + maxLength: 22, + }, + (value: string | null) => { + store.freetext[i + 1] = value; + CDUAtcOceanicReq.ShowPage2(mcdu, store); + }, + ), + ); + } + + // define the template + mcdu.setTemplate([ + ['FREE TEXT'], + [''], + [freetextLines[0]], + [''], + [freetextLines[1]], + [''], + [freetextLines[2]], + [''], + [freetextLines[3]], + [''], + [freetextLines[4]], + ['\xa0OCEANIC REQ'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcOceanicReq.ShowPage1(mcdu, store); + }; + } + + private static waypointType(waypoint: string): [number, McduMessage | null] { + if (WaypointEntryUtils.isLatLonFormat(waypoint)) { + return [0, null]; + } + + // time formatted + if (/([0-2][0-4][0-5][0-9]Z?)/.test(waypoint) && waypoint.length <= 5) { + return [1, null]; + } + + // place formatted + if (/^[A-Z0-9]{2,7}/.test(waypoint)) { + return [2, null]; + } + + return [-1, NXSystemMessages.formatError]; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ReportAtis.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ReportAtis.ts new file mode 100644 index 00000000000..edddfc8a4f3 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_ReportAtis.ts @@ -0,0 +1,148 @@ +import { AtsuMessageSerializationFormat } from '@datalink/common'; +import { CDUAtcAtisMenu } from './A320_Neo_CDU_ATC_AtisMenu'; +import { NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcReportAtis { + static ConvertAtisInformation(info) { + switch (info) { + case 'A': + return 'ALPHA'; + case 'B': + return 'BRAVO'; + case 'C': + return 'CHARLIE'; + case 'D': + return 'DELTA'; + case 'E': + return 'ECHO'; + case 'F': + return 'FOXTROT'; + case 'G': + return 'GOLF'; + case 'H': + return 'HOTEL'; + case 'I': + return 'INDIA'; + case 'J': + return 'JULIETT'; + case 'K': + return 'KILO'; + case 'L': + return 'LIMA'; + case 'M': + return 'MIKE'; + case 'N': + return 'NOVEMBER'; + case 'O': + return 'OSCAR'; + case 'P': + return 'PAPA'; + case 'Q': + return 'QUEBEC'; + case 'R': + return 'ROMEO'; + case 'S': + return 'SIERRA'; + case 'T': + return 'TANGO'; + case 'U': + return 'UNIFORM'; + case 'V': + return 'VICTOR'; + case 'W': + return 'WHISKEY'; + case 'X': + return 'XRAY'; + case 'Y': + return 'YANKEE'; + case 'Z': + return 'ZULU'; + default: + return ''; + } + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, title, messages, messageIndex, offset = 0) { + mcdu.clearDisplay(); + + const message = messages[messageIndex]; + let serialized = message.serialize(AtsuMessageSerializationFormat.FmsDisplay); + serialized = serialized.replace(/{green}|{amber}|{white}|{end}/gi, ''); + const lines = serialized.split('\n'); + lines.shift(); + lines.pop(); + + if (lines.length > 8) { + let up = false; + let down = false; + if (lines[offset - 8]) { + mcdu.onUp = () => { + offset -= 8; + CDUAtcReportAtis.ShowPage(mcdu, title, messages, messageIndex, offset); + }; + up = true; + } + if (lines[offset + 8]) { + mcdu.onDown = () => { + offset += 8; + CDUAtcReportAtis.ShowPage(mcdu, title, messages, messageIndex, offset); + }; + down = true; + } + mcdu.setArrows(up, down, false, false); + } + + const pageCount = Math.round(lines.length / 8 + 0.5); + const currentPage = Math.floor(offset / 8) + 1; + + let prevAtis = '\xa0PREV ATIS'; + if (messages.length > messageIndex + 1) { + prevAtis = ' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + if (messages.length > messageIndex + 1) { + CDUAtcReportAtis.ShowPage(mcdu, title, messages, messageIndex + 1, offset); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.noPreviousAtis); + } + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcAtisMenu.ShowPage(mcdu); + }; + + mcdu.onRightInput[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + mcdu.atsu.printMessage(message); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_SpeedReq.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_SpeedReq.ts new file mode 100644 index 00000000000..ffb324f3910 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATC_SpeedReq.ts @@ -0,0 +1,220 @@ +import { AtsuStatusCodes, CpdlcMessage, CpdlcMessagesDownlink, FansMode, InputValidation } from '@datalink/common'; +import { Keypad } from '../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcFlightReq } from './A320_Neo_CDU_ATC_FlightReq'; +import { CDUAtcTextFansA } from './FansA/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcSpeedRequest { + static CreateDataBlock(): any { + return { + speed: null, + whenSpeed: null, + }; + } + + static CanSendData(data) { + return data.speed || data.whenSpeed; + } + + static CanEraseData(data) { + return data.speed || data.whenSpeed; + } + + static CreateRequest(mcdu: LegacyAtsuPageInterface, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu: LegacyAtsuPageInterface, data) { + const retval = []; + + if (data.speed) { + retval.push(CDUAtcSpeedRequest.CreateRequest(mcdu, 'DM18', [data.speed])); + } + if (data.whenSpeed) { + retval.push(CDUAtcSpeedRequest.CreateRequest(mcdu, 'DM49', [data.whenSpeed])); + } + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, data = CDUAtcSpeedRequest.CreateDataBlock()) { + mcdu.clearDisplay(); + + let speed = '[ ][color]cyan'; + if (data.speed) { + speed = `${data.speed}[color]cyan`; + } + + let speedWhenSmall = ''; + let speedWhen = ''; + if (mcdu.atsu.fansMode() === FansMode.FansA) { + speedWhenSmall = '\xa0WHEN CAN WE EXPECT SPD'; + speedWhen = '[ ][color]cyan'; + if (data.whenSpeed) { + speedWhen = `${data.whenSpeed}[color]cyan`; + } + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcSpeedRequest.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + } + if (CDUAtcSpeedRequest.CanEraseData(data)) { + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['ATC SPEED REQ'], + ['\xa0SPEED[color]white'], + [speed], + [speedWhenSmall], + [speedWhen], + [''], + [''], + [''], + [''], + ['\xa0ALL FIELDS'], + [erase, text], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.speed = null; + } else if (value) { + const error = InputValidation.validateScratchpadSpeed(value); + if (error === AtsuStatusCodes.Ok) { + data.speed = InputValidation.formatScratchpadSpeed(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcSpeedRequest.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (mcdu.atsu.fansMode() === FansMode.FansA) { + if (value === Keypad.clrValue) { + data.whenSpeed = null; + } else if (value) { + const error = InputValidation.validateScratchpadSpeed(value); + if (error === AtsuStatusCodes.Ok) { + data.whenSpeed = InputValidation.formatScratchpadSpeed(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcSpeedRequest.ShowPage(mcdu, data); + } + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcSpeedRequest.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.offset = null; + } else if (value) { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.offset = InputValidation.formatScratchpadOffset(value); + data.offsetStart = null; + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcSpeedRequest.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = async (value) => { + if (value === Keypad.clrValue) { + data.weatherDeviation = null; + } else if (value) { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.weatherDeviation = InputValidation.formatScratchpadOffset(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcSpeedRequest.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.backOnTrack = false; + } else { + data.backOnTrack = true; + } + CDUAtcSpeedRequest.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcSpeedRequest.CanSendData(data)) { + const messages = CDUAtcSpeedRequest.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcSpeedRequest.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcSpeedRequest.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcSpeedRequest.ShowPage(mcdu); + } + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATSU_DatalinkStatus.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATSU_DatalinkStatus.ts new file mode 100644 index 00000000000..e756d2874e7 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATSU_DatalinkStatus.ts @@ -0,0 +1,91 @@ +import { CDUAtsuMenu } from './A320_Neo_CDU_ATSU_Menu'; +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtsuDatalinkStatus { + static ShowPage(mcdu: LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATSUDatalinkStatus; + mcdu.activeSystem = 'ATSU'; + + function updateView() { + if (mcdu.page.Current === mcdu.page.ATSUDatalinkStatus) { + CDUAtsuDatalinkStatus.ShowPage(mcdu); + } + } + + mcdu.pageRedrawCallback = () => { + updateView(); + }; + setTimeout(mcdu.requestUpdate.bind(mcdu), 500); + SimVar.SetSimVarValue('L:FMC_UPDATE_CURRENT_PAGE', 'number', 1); + + const vhfStatusCode = mcdu.atsu.getDatalinkStatus('vhf'); + const vhfModeCode = mcdu.atsu.getDatalinkMode('vhf'); + const satcomStatusCode = mcdu.atsu.getDatalinkStatus('satcom'); + const satcomModeCode = mcdu.atsu.getDatalinkMode('satcom'); + const hfStatusCode = mcdu.atsu.getDatalinkStatus('hf'); + const hfModeCode = mcdu.atsu.getDatalinkMode('hf'); + + const statusCodeToString = { + [-1]: '{red}INOP{end}', + [0]: '{small}NOT INSTALLED{end}', + [1]: '{small}DLK NOT AVAIL{end}', + [2]: '{green}DLK AVAIL{end}', + }; + + const modeCodeToString = { + [1]: 'ATC/AOC', + [2]: 'AOC ONLY', + [3]: 'ATC ONLY', + [0]: ' ', + }; + + const vhfStatus = statusCodeToString[vhfStatusCode] || 'ERROR'; + const vhfMode = modeCodeToString[vhfModeCode] || 'ERROR'; + const satcomStatus = statusCodeToString[satcomStatusCode] || 'ERROR'; + const satcomMode = modeCodeToString[satcomModeCode] || 'ERROR'; + const hfStatus = statusCodeToString[hfStatusCode] || 'ERROR'; + const hfMode = modeCodeToString[hfModeCode] || 'ERROR'; + + mcdu.setTemplate([ + ['DATALINK STATUS'], + [''], + [`VHF3 : ${vhfStatus}`], + [`\xa0\xa0\xa0\xa0\xa0\xa0\xa0${vhfMode}`], + [`SATCOM : ${satcomStatus}`], + [`\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0${satcomMode}`], + [`HF : ${hfStatus}`], + [`\xa0\xa0\xa0\xa0\xa0${hfMode}`], + [''], + [''], + [''], + [''], + [' { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[5] = () => { + CDUAtsuMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onRightInput[5] = () => { + const lines = [ + 'DATALINK STATUS', + `VHF3 : ${vhfStatus}`, + `\xa0\xa0\xa0\xa0\xa0\xa0\xa0${vhfMode}`, + `SATCOM : ${satcomStatus}`, + `\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0${satcomMode}`, + `HF : ${hfStatus}`, + `\xa0\xa0\xa0\xa0\xa0${hfMode}`, + ]; + mcdu.printPage(lines); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATSU_Menu.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATSU_Menu.ts new file mode 100644 index 00000000000..afd08cc3a0b --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_ATSU_Menu.ts @@ -0,0 +1,58 @@ +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; +import { CDUAocMenu } from './A320_Neo_CDU_AOC_Menu'; +import { CDUAtcMenu } from './A320_Neo_CDU_ATC_Menu'; +import { CDUAtsuDatalinkStatus } from './A320_Neo_CDU_ATSU_DatalinkStatus'; +import { CDUCommMenu } from './A320_Neo_CDU_Comm_Menu'; + +export class CDUAtsuMenu { + static ShowPage(mcdu: LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATSUMenu; + mcdu.activeSystem = 'ATSU'; + + const display = [ + ['ATSU DATALINK'], + [''], + [''], + [''], + [''], + [''], + [''], + ['', 'DATALINK\xa0'], + ['', 'STATUS>'], + [''], + ['', 'COMM MENU>'], + ]; + mcdu.setTemplate(display); + + mcdu.leftInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = () => { + CDUAocMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + CDUAtsuDatalinkStatus.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + CDUCommMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_Comm_Menu.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_Comm_Menu.ts new file mode 100644 index 00000000000..e17cbeb445c --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/A320_Neo_CDU_Comm_Menu.ts @@ -0,0 +1,30 @@ +import { LegacyAtsuPageInterface } from '../../legacy/LegacyAtsuPageInterface'; +import { CDUAtsuMenu } from './A320_Neo_CDU_ATSU_Menu'; + +export class CDUCommMenu { + static ShowPage(mcdu: LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.setTemplate([ + ['COMM MENU'], + ['\xa0VHF3[color]inop', 'COMM\xa0[color]inop'], + ['[color]inop'], + [''], + [''], + [''], + [''], + [''], + ['', 'MAINTENANCE>[color]inop'], + [''], + [''], + ['\xa0ATC MENU', 'AUTO PRINT\xa0[color]inop'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtsuMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_ContactRequest.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_ContactRequest.ts new file mode 100644 index 00000000000..494ca2d59f8 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_ContactRequest.ts @@ -0,0 +1,151 @@ +import { AtsuStatusCodes, CpdlcMessage, CpdlcMessagesDownlink, InputValidation } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcFlightReq } from '../A320_Neo_CDU_ATC_FlightReq'; +import { CDUAtcTextFansA } from '../FansA/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcContactRequest { + static CreateDataBlock(): any { + return { + requestContact: false, + }; + } + + static CanSendData(data) { + return data.requestContact; + } + + static CreateRequest(mcdu: LegacyAtsuPageInterface, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu, data) { + const retval = []; + + if (data.requestContact) { + retval.push(CDUAtcContactRequest.CreateRequest(mcdu, 'DM20')); + } + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, data = CDUAtcContactRequest.CreateDataBlock()) { + mcdu.clearDisplay(); + + let requestContact = '{cyan}{{end}REQ VOICE CONTACT'; + if (data.altitude) { + requestContact = '{cyan}\xa0REQ VOICE CONTACT{end]'; + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcContactRequest.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['CONTACT REQ'], + [''], + [requestContact], + [''], + [''], + [''], + [''], + [''], + [''], + ['\xa0ALL FIELDS'], + [erase, text], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.climb = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.climb = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcContactRequest.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.altitude = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.altitude = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcContactRequest.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcContactRequest.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcContactRequest.CanSendData(data)) { + const messages = CDUAtcContactRequest.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcContactRequest.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcContactRequest.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcContactRequest.ShowPage(mcdu); + } + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_Emergency.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_Emergency.ts new file mode 100644 index 00000000000..b5de3f21956 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_Emergency.ts @@ -0,0 +1,493 @@ +import { AtsuStatusCodes, CpdlcMessage, CpdlcMessagesDownlink, InputValidation } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcMenu } from '../A320_Neo_CDU_ATC_Menu'; +import { CDUAtcTextFansA } from '../FansA/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcEmergencyFansA { + static CanSendData(data) { + return ( + data.mayday || + data.panpan || + data.descendingTo || + data.personsOnBoard || + data.endurance || + data.cancelEmergency || + data.deviating || + data.climbingTo || + data.diverting || + data.divertingVia || + data.voiceContact + ); + } + + static CanEraseData(data) { + return ( + data.mayday || + data.panpan || + data.descendingTo || + data.personsOnBoard || + data.endurance || + data.cancelEmergency || + data.deviating || + data.climbingTo || + data.diverting || + data.divertingVia || + data.voiceContact + ); + } + + static CreateDataBlock() { + return { + mayday: false, + panpan: false, + descendingTo: null, + personsOnBoard: null, + endurance: null, + cancelEmergency: false, + deviating: null, + climbingTo: null, + diverting: null, + divertingVia: null, + voiceContact: false, + }; + } + + static CreateRequest(mcdu: LegacyAtsuPageInterface, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu: LegacyAtsuPageInterface, data) { + const retval = []; + + if (data.mayday === true) { + retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, 'DM56')); + } + if (data.panpan === true) { + retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, 'DM55')); + } + if (data.descendingTo) { + retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, 'DM55', [data.descendingTo])); + } + if (data.personsOnBoard && data.endurance) { + retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, 'DM57', [data.endurance, data.personsOnBoard])); + } + if (data.cancelEmergency === true) { + retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, 'DM58')); + } + if (data.deviating) { + const elements = InputValidation.expandLateralOffset(data.deviating).split(' '); + retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, 'DM80', [elements[0], elements[1]])); + } + if (data.climbingTo) { + retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, 'DM29', [data.climbingTo])); + } + if (data.diverting && data.divertingVia) { + retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, 'DM59', [data.diverting, data.divertingVia])); + } + if (data.voiceContact === true) { + retval.push(CDUAtcEmergencyFansA.CreateRequest(mcdu, 'DM20')); + } + + return retval; + } + + static ShowPage1(mcdu: LegacyAtsuPageInterface, data = CDUAtcEmergencyFansA.CreateDataBlock()) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCEmergency; + + let mayday = '{cyan}{{end}{red}MAYDAY{end}'; + let panpan = '{cyan}{{end}{amber}PANPAN{end}'; + if (data.panpan === true) { + panpan = '\xa0{amber}PANPAN{end}'; + } + if (data.mayday === true) { + mayday = '\xa0{red}MAYDAY{end}'; + } + + let descendingTo = '{cyan}[ ]{end}'; + if (data.descendingTo) { + descendingTo = `{cyan}${data.descendingTo}{end}`; + } + + let personsOnBoard = '{cyan}[ ]{end}'; + let endurance = '{cyan}[ ]{end}'; + if (data.personsOnBoard) { + personsOnBoard = `{cyan}${data.personsOnBoard}{end}`; + } + if (data.endurance) { + endurance = `{cyan}${data.endurance}{end}`; + } + + let cancelEmergency = '{amber}CANCEL EMERGENCY{end}{cyan}}{end}'; + if (data.cancelEmergency === true) { + cancelEmergency = '{amber}CANCEL EMERGENCY{end}\xa0'; + } + + let addText = 'ADD TEXT\xa0'; + let eraseData = '\xa0ERASE'; + let sendData = 'DCDU\xa0[color]cyan'; + if (CDUAtcEmergencyFansA.CanEraseData(data)) { + eraseData = '*ERASE'; + addText = 'ADD TEXT>'; + } + if (CDUAtcEmergencyFansA.CanSendData(data)) { + sendData = 'DCDU*[color]cyan'; + } + + mcdu.setTemplate([ + ['EMERGENCY[color]amber', '1', '2'], + ['', 'EMERG ADS-C:OFF\xa0'], + [mayday, 'SET ON*[color]inop'], + ['', 'DESCENDING TO\xa0'], + [panpan, descendingTo], + ['\xa0POB', 'ENDURANCE\xa0'], + [personsOnBoard, endurance], + [''], + ['', cancelEmergency], + ['\xa0ALL FIELDS'], + [eraseData, addText], + ['\xa0ATC MENU', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.mayday = false; + } else { + data.panpan = false; + data.mayday = true; + } + CDUAtcEmergencyFansA.ShowPage1(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.panpan = false; + } else { + data.panpan = true; + data.mayday = false; + } + CDUAtcEmergencyFansA.ShowPage1(mcdu, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.personsOnBoard = null; + } else { + const error = InputValidation.validateScratchpadPersonsOnBoard(value); + if (error === AtsuStatusCodes.Ok) { + data.personsOnBoard = parseInt(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcEmergencyFansA.ShowPage1(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcEmergencyFansA.ShowPage1(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.descendingTo = null; + } else { + const error = InputValidation.validateScratchpadAltitude(value); + if (error === AtsuStatusCodes.Ok) { + data.descendingTo = InputValidation.formatScratchpadAltitude(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcEmergencyFansA.ShowPage1(mcdu, data); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.endurance = null; + } else { + const error = InputValidation.validateScratchpadEndurance(value); + if (error === AtsuStatusCodes.Ok) { + data.endurance = InputValidation.formatScratchpadEndurance(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcEmergencyFansA.ShowPage1(mcdu, data); + }; + + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.cancelEmergency = false; + } else { + data.cancelEmergency = true; + } + CDUAtcEmergencyFansA.ShowPage1(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcEmergencyFansA.CanSendData(data)) { + const messages = CDUAtcEmergencyFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcEmergencyFansA.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcEmergencyFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcEmergencyFansA.ShowPage1(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcEmergencyFansA.ShowPage2(mcdu, data); + }; + mcdu.onNextPage = () => { + CDUAtcEmergencyFansA.ShowPage2(mcdu, data); + }; + } + + static ShowPage2(mcdu: LegacyAtsuPageInterface, data = CDUAtcEmergencyFansA.CreateDataBlock()) { + mcdu.clearDisplay(); + + let deviating = '{cyan}[ ]{end}'; + let climbingTo = '{cyan}[ ]{end}'; + if (data.deviating) { + deviating = `{cyan}${data.deviating}{end}`; + } + if (data.climbingTo) { + climbingTo = `{cyan}${data.climbingTo}{end}`; + } + + let diverting = '{cyan}[ ]/[ ]{end}'; + if (data.diverting && data.divertingVia) { + diverting = `{cyan}${data.diverting}/${data.divertingVia}{end}`; + } + + let reqVoice = '{cyan}{{end}REQ VOICE CONTACT'; + if (data.voiceContact) { + reqVoice = '{cyan}\xa0REQ VOICE CONTACT{end}'; + } + + let addText = 'ADD TEXT\xa0'; + let eraseData = '\xa0ERASE'; + let sendData = 'DCDU\xa0[color]cyan'; + if (CDUAtcEmergencyFansA.CanEraseData(data)) { + eraseData = '*ERASE'; + addText = 'ADD TEXT>'; + } + if (CDUAtcEmergencyFansA.CanSendData(data)) { + sendData = 'DCDU*[color]cyan'; + } + + mcdu.setTemplate([ + ['EMERGENCY[color]amber', '2', '2'], + ['\xa0DEVIATING', 'CLIMBING TO\xa0'], + [deviating, climbingTo], + ['\xa0DIVERTING/VIA'], + [diverting], + [''], + [reqVoice], + [''], + [''], + ['\xa0ALL FIELDS'], + [eraseData, addText], + ['\xa0ATC MENU', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.deviating = null; + } else { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.deviating = InputValidation.formatScratchpadOffset(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcEmergencyFansA.ShowPage2(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.diverting = null; + data.divertingVia = null; + } else if (value.length !== 0) { + const split = value.split('/'); + if (split.length === 2) { + if (split[0].length !== 0) { + const error = InputValidation.validateScratchpadPosition(split[0]); + if (error === AtsuStatusCodes.Ok) { + data.diverting = split[0]; + } else { + mcdu.addNewAtsuMessage(error); + return; + } + } + + const error = InputValidation.validateScratchpadPosition(split[1]); + if (error === AtsuStatusCodes.Ok) { + data.divertingVia = split[1]; + } else { + mcdu.addNewAtsuMessage(error); + } + } else if (split.length === 1) { + const error = InputValidation.validateScratchpadPosition(value); + if (error === AtsuStatusCodes.Ok) { + if (data.diverting) { + data.divertingVia = value; + } else { + data.diverting = value; + data.divertingVia = value; + } + } else { + mcdu.addNewAtsuMessage(error); + } + } else { + mcdu.addNewAtsuMessage(AtsuStatusCodes.FormatError); + } + } + CDUAtcEmergencyFansA.ShowPage2(mcdu, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.voiceContact = false; + } else { + data.voiceContact = true; + } + CDUAtcEmergencyFansA.ShowPage2(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcEmergencyFansA.ShowPage2(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.climbingTo = null; + } else { + const error = InputValidation.validateScratchpadAltitude(value); + if (error === AtsuStatusCodes.Ok) { + data.climbingTo = InputValidation.formatScratchpadAltitude(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcEmergencyFansA.ShowPage2(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcEmergencyFansA.CanSendData(data)) { + const messages = CDUAtcEmergencyFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcEmergencyFansA.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcEmergencyFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcEmergencyFansA.ShowPage2(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcEmergencyFansA.ShowPage1(mcdu, data); + }; + mcdu.onNextPage = () => { + CDUAtcEmergencyFansA.ShowPage1(mcdu, data); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_LatRequest.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_LatRequest.ts new file mode 100644 index 00000000000..97b38f32aa4 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_LatRequest.ts @@ -0,0 +1,481 @@ +import { + AtsuStatusCodes, + AtsuTimestamp, + CpdlcMessage, + CpdlcMessagesDownlink, + InputValidation, + InputWaypointType, +} from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcFlightReq } from '../A320_Neo_CDU_ATC_FlightReq'; +import { CDUAtcTextFansA } from '../FansA/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +export class CDUAtcLatRequestFansA { + static CreateDataBlock() { + return { + directTo: null, + weatherDeviation: null, + offset: null, + offsetStart: null, + heading: null, + track: null, + backOnTrack: false, + }; + } + + static CanSendData(data) { + return data.directTo || data.weatherDeviation || data.offset || data.heading || data.track || data.backOnTrack; + } + + static CanEraseData(data) { + return data.directTo || data.weatherDeviation || data.offset || data.heading || data.track || data.backOnTrack; + } + + static CreateRequest(mcdu: LegacyAtsuPageInterface, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu: LegacyAtsuPageInterface, data) { + const retval = []; + + if (data.directTo) { + retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, 'DM22', [data.directTo])); + } + if (data.weatherDeviation) { + const elements = InputValidation.expandLateralOffset(data.weatherDeviation).split(' '); + retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, 'DM27', [elements[0], elements[1]])); + } + if (data.offset) { + const elements = InputValidation.expandLateralOffset(data.offset).split(' '); + + if (!data.offsetStart || /$[0-9]{4}Z^/.test(data.offsetStart)) { + retval.push( + CDUAtcLatRequestFansA.CreateRequest(mcdu, 'DM17', [ + !data.offsetStart ? new AtsuTimestamp().mailboxTimestamp() : data.offsetStart, + elements[0], + elements[1], + ]), + ); + } else { + retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, 'DM16', [data.offsetStart, elements[0], elements[1]])); + } + } + if (data.heading) { + retval.push( + CDUAtcLatRequestFansA.CreateRequest(mcdu, 'DM70', [data.heading === 0 ? '360' : data.heading.toString()]), + ); + } + if (data.track) { + retval.push( + CDUAtcLatRequestFansA.CreateRequest(mcdu, 'DM71', [data.track === 0 ? '360' : data.track.toString()]), + ); + } + if (data.backOnTrack) { + retval.push(CDUAtcLatRequestFansA.CreateRequest(mcdu, 'DM51')); + } + + return retval; + } + + static ShowPage1(mcdu: LegacyAtsuPageInterface, data = CDUAtcLatRequestFansA.CreateDataBlock()) { + mcdu.clearDisplay(); + + let weatherDeviation = '{cyan}[ ]{end}'; + if (data.weatherDeviation) { + weatherDeviation = `${data.weatherDeviation}[color]cyan`; + } + let heading = '[ ]°[color]cyan'; + if (data.heading !== null) { + heading = `${data.heading}°[color]cyan`; + } + let grdTrack = '[ ]°[color]cyan'; + if (data.track !== null) { + grdTrack = `${data.track}°[color]cyan`; + } + let directTo = '{cyan}[ ]{end}'; + if (data.directTo) { + directTo = `${data.directTo}[color]cyan`; + } + let offsetDistance = '[ ]'; + if (data.offset && data.offsetStart === null) { + offsetDistance = data.offset; + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcLatRequestFansA.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + } + if (CDUAtcLatRequestFansA.CanEraseData(data)) { + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['ATC LAT REQ', '1', '2'], + ['\xa0DIR TO[color]white'], + [directTo], + ['\xa0HDG', 'OFFSET\xa0'], + [heading, `{cyan}${offsetDistance}{end}`], + ['\xa0GND TRK', 'WX DEV\xa0'], + [grdTrack, weatherDeviation], + [''], + [''], + ['\xa0ALL FIELDS'], + [erase, text], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.directTo = null; + } else if (value) { + if (WaypointEntryUtils.isLatLonFormat(value)) { + // format: DDMM.MB/EEEMM.MC + try { + WaypointEntryUtils.parseLatLon(value); + data.directTo = value; + } catch (err) { + if (err === NXSystemMessages.formatError) { + mcdu.setScratchpadMessage(err); + } + } + } else if (/^[A-Z0-9]{2,7}/.test(value)) { + // place format + data.directTo = value; + CDUAtcLatRequestFansA.ShowPage1(mcdu, data); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + + CDUAtcLatRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.heading = null; + } else if (value) { + const error = InputValidation.validateScratchpadDegree(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.heading = parseInt(value) % 360; + } + } + + CDUAtcLatRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.track = null; + } else if (value) { + const error = InputValidation.validateScratchpadDegree(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.track = parseInt(value) % 360; + } + } + + CDUAtcLatRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcLatRequestFansA.ShowPage1(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.offset = null; + } else if (value) { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.offset = InputValidation.formatScratchpadOffset(value); + data.offsetStart = null; + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcLatRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = async (value) => { + if (value === Keypad.clrValue) { + data.weatherDeviation = null; + } else if (value) { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.weatherDeviation = InputValidation.formatScratchpadOffset(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcLatRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.backOnTrack = false; + } else { + data.backOnTrack = true; + } + CDUAtcLatRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcLatRequestFansA.CanSendData(data)) { + const messages = CDUAtcLatRequestFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcLatRequestFansA.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcLatRequestFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcLatRequestFansA.ShowPage1(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcLatRequestFansA.ShowPage2(mcdu, data); + }; + mcdu.onNextPage = () => { + CDUAtcLatRequestFansA.ShowPage2(mcdu, data); + }; + } + + static ShowPage2(mcdu: LegacyAtsuPageInterface, data = CDUAtcLatRequestFansA.CreateDataBlock()) { + mcdu.clearDisplay(); + + let offsetDistance = '[ ]'; + let offsetStartPoint = '[ ]'; + if (data.offset && data.offsetStart) { + offsetDistance = data.offset; + offsetStartPoint = data.offsetStart; + } + let whenCanWe = '{big}\xa0WHEN CAN WE\xa0{end}'; + let backOnRoute = '{cyan}{{end}EXPECT BACK ON ROUTE'; + if (data.backOnTrack) { + backOnRoute = '{cyan}\xa0EXPECT BACK ON ROUTE{end}'; + whenCanWe = '{cyan}{big}\xa0WHEN CAN WE\xa0{end}{end}'; + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcLatRequestFansA.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + } + if (CDUAtcLatRequestFansA.CanEraseData(data)) { + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['ATC LAT REQ', '2', '2'], + ['', 'OFFSET/START AT'], + ['', `{cyan}${offsetDistance}/${offsetStartPoint}{end}`], + [''], + ['------------------------'], + [whenCanWe], + [backOnRoute], + ['------------------------'], + [''], + ['\xa0ALL FIELDS'], + [erase, text], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.backOnTrack = false; + } else { + data.backOnTrack = true; + } + + CDUAtcLatRequestFansA.ShowPage2(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcLatRequestFansA.ShowPage2(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = async (value) => { + if (value === Keypad.clrValue) { + data.offset = null; + data.offsetStart = null; + } else if (value) { + const entries = value.split('/'); + let updatedOffset = false; + let offsetStart = null; + let offset = null; + + const error = InputValidation.validateScratchpadOffset(entries[0]); + if (error === AtsuStatusCodes.Ok) { + updatedOffset = true; + offset = InputValidation.formatScratchpadOffset(entries[0]); + entries.shift(); + } + + if (entries.length !== 0) { + const startingPoint = entries.join('/'); + + const type = InputValidation.classifyScratchpadWaypointType(startingPoint, true); + if (offset || data.offset) { + switch (type[0]) { + case InputWaypointType.GeoCoordinate: + case InputWaypointType.Place: + offsetStart = startingPoint; + break; + case InputWaypointType.Timepoint: + if (startingPoint.endsWith('Z')) { + offsetStart = startingPoint; + } else { + offsetStart = `${startingPoint}Z`; + } + break; + default: + mcdu.addNewAtsuMessage(type[1]); + offsetStart = null; + if (updatedOffset) { + offset = null; + } + break; + } + } + + if (offset || offsetStart) { + const oldOffsetStart = data.offsetStart; + const oldOffset = data.offset; + + data.offset = offset ? offset : oldOffset; + data.offsetStart = offsetStart ? offsetStart : oldOffsetStart; + } + + CDUAtcLatRequestFansA.ShowPage2(mcdu, data); + } else if (updatedOffset) { + if (data.offsetStart) { + data.offset = offset; + } else { + mcdu.addNewAtsuMessage(AtsuStatusCodes.FormatError); + } + } else if (error) { + mcdu.addNewAtsuMessage(error); + } + } + + CDUAtcLatRequestFansA.ShowPage2(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcLatRequestFansA.CanSendData(data)) { + const requests = CDUAtcLatRequestFansA.CreateRequests(mcdu, data); + if (requests.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, requests); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcLatRequestFansA.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcLatRequestFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcLatRequestFansA.ShowPage2(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcLatRequestFansA.ShowPage1(mcdu, data); + }; + mcdu.onNextPage = () => { + CDUAtcLatRequestFansA.ShowPage1(mcdu, data); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_PositionReport.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_PositionReport.ts new file mode 100644 index 00000000000..6799c5358d5 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_PositionReport.ts @@ -0,0 +1,1160 @@ +import { + AtsuStatusCodes, + coordinateToString, + CpdlcMessage, + CpdlcMessagesDownlink, + InputValidation, +} from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcMenu } from '../A320_Neo_CDU_ATC_Menu'; +import { CDUAtcReports } from '../FansA/A320_Neo_CDU_ATC_Reports'; +import { CDUAtcTextFansA } from '../FansA/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +export class CDUAtcPositionReport { + static SecondsToString(seconds) { + const hours = Math.floor(seconds / 3600); + const minutes = Math.floor(seconds / 60) % 60; + return `${hours.toString().padStart(2, '0')}${minutes.toString().padStart(2, '0')}`; + } + + static AltitudeToString(altitude) { + if (Simplane.getPressureSelectedMode(Aircraft.A320_NEO) === 'STD') { + return InputValidation.formatScratchpadAltitude(`FL${Math.round(altitude / 100)}`); + } + return InputValidation.formatScratchpadAltitude(`${altitude}FT`); + } + + static FillDataBlock(mcdu: LegacyAtsuPageInterface, data) { + const current = data.atsuFlightStateData; + const target = data.atsuAutopilotData; + const lastWp = data.atsuLastWaypoint; + const activeWp = data.atsuActiveWaypoint; + const nextWp = data.atsuNextWaypoint; + + if (lastWp && !data.passedWaypoint[3]) { + data.passedWaypoint[0] = lastWp.ident; + data.passedWaypoint[1] = CDUAtcPositionReport.SecondsToString(lastWp.utc); + data.passedWaypoint[2] = CDUAtcPositionReport.AltitudeToString(lastWp.altitude); + } + + // will be abreviated during the rendering + if (!data.currentPosition[2]) { + mcdu.setScratchpadMessage(NXSystemMessages.latLonAbreviated); + } + data.currentPosition = !data.currentPosition[2] ? [current.lat, current.lon, false] : data.currentPosition; + data.currentUtc[0] = !data.currentUtc[1] + ? CDUAtcPositionReport.SecondsToString(SimVar.GetSimVarValue('E:ZULU TIME', 'seconds')) + : data.currentUtc[0]; + data.currentAltitude[0] = !data.currentAltitude[1] + ? CDUAtcPositionReport.AltitudeToString(current.altitude) + : data.currentAltitude[0]; + + if (activeWp && !data.activeWaypoint[2]) { + data.activeWaypoint[0] = activeWp.ident; + data.activeWaypoint[1] = CDUAtcPositionReport.SecondsToString(activeWp.utc); + } + if (nextWp && !data.nextWaypoint[1]) { + data.nextWaypoint[0] = nextWp.ident; + } + if (data.atsuDestination && !data.eta[1]) { + data.eta[0] = CDUAtcPositionReport.SecondsToString(data.atsuDestination.utc); + } + + if (!data.wind[1]) { + const windDirection = data.atsuEnvironmentData.windDirection; + const windSpeed = data.atsuEnvironmentData.windSpeed; + + const wind = `${Math.round(windDirection.value)}/${Math.round(windSpeed.value)}`; + if (InputValidation.validateScratchpadWind(wind) === AtsuStatusCodes.Ok) { + data.wind[0] = InputValidation.formatScratchpadWind(wind); + } + } + if (!data.sat[1]) { + const sat = data.atsuEnvironmentData.temperature; + if (InputValidation.validateScratchpadTemperature(sat.value) === AtsuStatusCodes.Ok) { + data.sat[0] = Math.round(sat.value); + } + } + + data.indicatedAirspeed[0] = !data.indicatedAirspeed[1] + ? InputValidation.formatScratchpadSpeed(`${current.indicatedAirspeed}`) + : data.indicatedAirspeed[0]; + data.groundSpeed[0] = !data.groundSpeed[1] + ? InputValidation.formatScratchpadSpeed(`${current.groundSpeed}`) + : data.groundSpeed[0]; + data.verticalSpeed[0] = !data.verticalSpeed[1] + ? InputValidation.formatScratchpadVerticalSpeed(`${current.verticalSpeed}`) + : data.verticalSpeed[0]; + // TODO add deviating + data.heading[0] = !data.heading[1] ? current.heading : data.heading[0]; + data.track[0] = !data.track[1] ? current.track : data.track[0]; + if (target.apActive && current.altitude > target.altitude) { + data.descending = CDUAtcPositionReport.AltitudeToString(target.altitude); + } else if (target.apActive && current.altitude < target.altitude) { + data.climbing = CDUAtcPositionReport.AltitudeToString(target.altitude); + } + } + + static CreateDataBlock(mcdu, requestMessage, autoFill) { + const retval = { + atsuFlightStateData: null, + atsuAutopilotData: null, + atsuEnvironmentData: null, + atsuLastWaypoint: null, + atsuActiveWaypoint: null, + atsuNextWaypoint: null, + atsuDestination: null, + passedWaypoint: [null, null, null, !autoFill] as [string | null, string | null, string | null, boolean], + activeWaypoint: [null, null, !autoFill] as [string | null, string | null, boolean], + nextWaypoint: [null, !autoFill] as [string | null, boolean], + currentPosition: [null, !autoFill] as [number[] | null, boolean], + currentUtc: [null, !autoFill] as [string | null, boolean], + currentAltitude: [null, !autoFill] as [string | null, boolean], + wind: [null, !autoFill] as [string | null, boolean], + sat: [null, !autoFill] as [string | null, boolean], + icing: [null, !autoFill] as [string | null, boolean], + turbulence: [null, !autoFill] as [string | null, boolean], + eta: [null, !autoFill] as [string | null, boolean], + endurance: [null, !autoFill] as [string | null, boolean], + indicatedAirspeed: [null, !autoFill] as [string | null, boolean], + groundSpeed: [null, !autoFill] as [string | null, boolean], + verticalSpeed: [null, !autoFill] as [string | null, boolean], + deviating: [null, !autoFill] as [string | null, boolean], + heading: [null, !autoFill] as [string | null, boolean], + track: [null, !autoFill] as [string | null, boolean], + descending: [null, !autoFill] as [string | null, boolean], + climbing: [null, !autoFill] as [string | null, boolean], + }; + + if (autoFill === true) { + mcdu.atsu.receivePositionReportData().then((data) => { + retval.atsuFlightStateData = data.flightState; + retval.atsuAutopilotData = data.autopilot; + retval.atsuEnvironmentData = data.environment; + retval.atsuLastWaypoint = data.lastWaypoint; + retval.atsuActiveWaypoint = data.activeWaypoint; + retval.atsuNextWaypoint = data.nextWaypoint; + retval.atsuDestination = data.destination; + + CDUAtcPositionReport.FillDataBlock(mcdu, retval); + if (mcdu.page.Current === mcdu.page.ATCPositionReport1) { + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, retval); + } else if (mcdu.page.Current === mcdu.page.ATCPositionReport2) { + CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, retval); + } else if (mcdu.page.Current === mcdu.page.ATCPositionReport3) { + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, retval); + } + }); + } + + return retval; + } + + static CanEraseData(data) { + return ( + data.passedWaypoint[0] || + data.passedWaypoint[1] || + data.passedWaypoint[2] || + data.activeWaypoint[0] || + data.activeWaypoint[1] || + data.nextWaypoint[0] || + data.currentPosition[0] || + data.currentUtc[0] || + data.currentAltitude[0] || + data.wind[0] || + data.sat[0] || + data.icing[0] || + data.turbulence[0] || + data.eta[0] || + data.endurance[0] || + data.indicatedAirspeed[0] || + data.groundSpeed[0] || + data.verticalSpeed[0] || + data.deviating[0] || + data.heading[0] || + data.track[0] || + data.descending[0] || + data.climbing[0] + ); + } + + static CanSendData(data) { + return ( + data.passedWaypoint[0] && + data.passedWaypoint[1] && + data.passedWaypoint[2] && + data.activeWaypoint[0] && + data.activeWaypoint[1] && + data.nextWaypoint[0] && + data.currentPosition[0] && + data.currentUtc[0] && + data.currentAltitude[0] + ); + } + + static CreateReport(mcdu, data) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink['DM48'][1].deepCopy()); + + // define the overhead + let extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `OVHD: ${data.passedWaypoint[0]}`; + retval.Content.push(extension); + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `AT ${data.passedWaypoint[1]}Z/${data.passedWaypoint[2]}`; + retval.Content.push(extension); + // define the present position + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `PPOS: ${coordinateToString({ lat: data.currentPosition[0][0], lon: data.currentPosition[0][1] }, false)}`; + retval.Content.push(extension); + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `AT ${data.currentUtc[0]}Z/${data.currentAltitude[0]}`; + retval.Content.push(extension); + // define the active position + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `TO :${data.activeWaypoint[0]} AT ${data.activeWaypoint[1]}Z`; + retval.Content.push(extension); + // define the next position + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `NEXT: ${data.nextWaypoint[0]}`; + retval.Content.push(extension); + + // create wind and temperature data + if (data.wind[0] && data.sat[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `WIND: ${data.wind[0]} SAT: ${data.sat[0]}`; + retval.Content.push(extension); + } else if (data.wind[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `WIND: ${data.wind[0]}`; + retval.Content.push(extension); + } else if (data.sat[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `SAT: ${data.sat[0]}`; + retval.Content.push(extension); + } + + // create the initial data + if (data.eta[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `DEST ETA: ${data.eta[0]}Z`; + retval.Content.push(extension); + } + if (data.descending[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `DESCENDING TO ${data.descending[0]}`; + retval.Content.push(extension); + } else if (data.climbing[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `CLIMBING TO ${data.climbing[0]}`; + retval.Content.push(extension); + } + if (data.endurance[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `ENDURANCE: ${data.endurance[0]}`; + retval.Content.push(extension); + } + if (data.icing[0] && data.turbulence[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `ICING: ${data.icing[0]} TURBULENCE: ${data.turbulence[0]}`; + retval.Content.push(extension); + } else if (data.icing[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `ICING: ${data.icing[0]}`; + retval.Content.push(extension); + } else if (data.turbulence[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `TURBULENCE: ${data.turbulence[0]}`; + retval.Content.push(extension); + } + if (data.indicatedAirspeed[0] && data.groundSpeed[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `SPD: ${data.indicatedAirspeed[0]} GS: ${data.groundSpeed[0]}`; + retval.Content.push(extension); + } else if (data.indicatedAirspeed[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `SPD: ${data.indicatedAirspeed[0]}`; + retval.Content.push(extension); + } else if (data.groundSpeed[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `GS: ${data.groundSpeed[0]}`; + retval.Content.push(extension); + } + if (data.verticalSpeed[0] && data.verticalSpeed[0] !== '0FTM') { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `VS: ${data.verticalSpeed[0]}`; + retval.Content.push(extension); + } + if (data.heading[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `HDG: ${data.heading[0]}°TRUE`; + retval.Content.push(extension); + } + if (data.track[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `TRK: ${data.track[0]}°`; + retval.Content.push(extension); + } + + if (data.deviating[0]) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = `DEVIATING ${InputValidation.expandLateralOffset(data.deviating[0])}`; + retval.Content.push(extension); + } + + return retval; + } + + static ShowPage1( + mcdu: LegacyAtsuPageInterface, + requestMessage = null, + data = CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, true), + ) { + mcdu.page.Current = mcdu.page.ATCPositionReport1; + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcPositionReport.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + } + if (CDUAtcPositionReport.CanEraseData(data)) { + erase = '*ERASE'; + } + + const overhead = ['[ ]', '[ ]', '[ ]']; + if (data.passedWaypoint[0]) { + overhead[0] = data.passedWaypoint[0]; + } + if (data.passedWaypoint[1] && data.passedWaypoint[2]) { + overhead[1] = data.passedWaypoint[1]; + overhead[2] = data.passedWaypoint[2]; + } + if (data.passedWaypoint[3] === false) { + if (data.passedWaypoint[0]) { + overhead[0] = `{small}${overhead[0]}{end}`; + } + if (data.passedWaypoint[1] && data.passedWaypoint[2]) { + overhead[1] = `{small}${overhead[1]}{end}`; + overhead[2] = `{small}${overhead[2]}{end}`; + } + } + + const ppos = ['_______[color]amber', '____/______[color]amber']; + if (data.currentPosition[0]) { + ppos[0] = `{cyan}${coordinateToString({ lat: data.currentPosition[0][0], lon: data.currentPosition[0][1] }, true)}{end}`; + if (data.currentPosition[1] === false) { + ppos[0] = `{small}${ppos[0]}{end}`; + } + } + if (data.currentUtc[0] && data.currentAltitude[0]) { + ppos[1] = `{cyan}${data.currentUtc[0]}/${data.currentAltitude[0]}{end}`; + if (data.currentUtc[1] === false) { + ppos[1] = `{small}${ppos[1]}{end}`; + } + } + + const to = ['[ ]', '[ ]']; + if (data.activeWaypoint[0]) { + to[0] = data.activeWaypoint[0]; + if (data.activeWaypoint[2] === false) { + to[0] = `{small}${to[0]}{end}`; + } + } + if (data.activeWaypoint[1]) { + to[1] = data.activeWaypoint[1]; + if (data.activeWaypoint[2] === false) { + to[1] = `{small}${to[1]}{end}`; + } + } + + let next = '[ ]'; + if (data.nextWaypoint[0]) { + next = data.nextWaypoint[0]; + if (data.nextWaypoint[1] === false) { + next = `{small}${next}{end}`; + } + } + + mcdu.setTemplate([ + ['POSITION REPORT', '1', '3'], + ['\xa0OVHD-----------UTC/ALT'], + [`{cyan}${overhead[0]}{end}`, `{cyan}${overhead[1]}/${overhead[2]}`], + ['\xa0PPOS-----------UTC/ALT'], + [ppos[0], ppos[1]], + ['\xa0TO-----------------UTC'], + [`{cyan}${to[0]}{end}`, `{cyan}${to[1]}{end}`], + ['\xa0NEXT'], + [`{cyan}${next}{end}`], + ['\xa0ALL FIELDS'], + [erase, text], + [`${requestMessage ? '\xa0ATC MENU' : '\xa0ATC REPORTS'}`, 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.passedWaypoint[0] = null; + data.passedWaypoint[3] = true; + } else if (value) { + if (WaypointEntryUtils.isLatLonFormat(value)) { + // format: DDMM.MB/EEEMM.MC + try { + WaypointEntryUtils.parseLatLon(value); + data.passedWaypoint[0] = value; + data.passedWaypoint[3] = true; + } catch (err) { + if (err === NXSystemMessages.formatError) { + mcdu.setScratchpadMessage(err); + } + } + } else if (/^[A-Z0-9]{2,7}/.test(value)) { + // place format + data.passedWaypoint[0] = value; + data.passedWaypoint[3] = true; + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.currentPosition[0] = null; + data.currentPosition[1] = true; + } else if (value && WaypointEntryUtils.isLatLonFormat(value)) { + // format: DDMM.MB/EEEMM.MC + try { + const pos = WaypointEntryUtils.parseLatLon(value); + data.currentPosition[0] = [pos.lat, pos.long]; + data.currentPosition[1] = true; + } catch (err) { + if (err === NXSystemMessages.formatError) { + mcdu.setScratchpadMessage(err); + } + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.activeWaypoint[0] = null; + data.activeWaypoint[2] = true; + } else if (value) { + if (WaypointEntryUtils.isLatLonFormat(value)) { + // format: DDMM.MB/EEEMM.MC + try { + WaypointEntryUtils.parseLatLon(value); + data.activeWaypoint[0] = value; + data.activeWaypoint[2] = true; + } catch (err) { + if (err === NXSystemMessages.formatError) { + mcdu.setScratchpadMessage(err); + } + } + } else if (/^[A-Z0-9]{2,7}/.test(value)) { + // place format + data.activeWaypoint[0] = value; + data.activeWaypoint[2] = true; + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.nextWaypoint[0] = null; + data.nextWaypoint[1] = true; + } else if (value) { + if (WaypointEntryUtils.isLatLonFormat(value)) { + // format: DDMM.MB/EEEMM.MC + try { + WaypointEntryUtils.parseLatLon(value); + data.nextWaypoint[0] = value; + data.nextWaypoint[1] = true; + } catch (err) { + if (err === NXSystemMessages.formatError) { + mcdu.setScratchpadMessage(err); + } + } + } else if (/^[A-Z0-9]{2,7}/.test(value)) { + // place format + data.nextWaypoint[0] = value; + data.nextWaypoint[1] = true; + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcPositionReport.ShowPage1( + mcdu, + requestMessage, + CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, false), + ); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + if (requestMessage) { + CDUAtcMenu.ShowPage(mcdu); + } else { + CDUAtcReports.ShowPage(mcdu); + } + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.passedWaypoint[1] = null; + data.passedWaypoint[2] = null; + data.passedWaypoint[3] = true; + } else { + const elements = value.split('/'); + if (elements.length === 2) { + const timeError = InputValidation.validateScratchpadTime(elements[0], false); + const altError = InputValidation.validateScratchpadAltitude(elements[1]); + + if (timeError !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(timeError); + } else if (altError !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(altError); + } else { + data.passedWaypoint[1] = elements[0].length === 5 ? elements[0].substring(0, 4) : elements[0]; + data.passedWaypoint[2] = InputValidation.formatScratchpadAltitude(elements[1]); + data.passedWaypoint[3] = true; + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.currentUtc = [null, true]; + data.currentAltitude = [null, true]; + } else { + const elements = value.split('/'); + if (elements.length === 2) { + const timeError = InputValidation.validateScratchpadTime(elements[0], false); + const altError = InputValidation.validateScratchpadAltitude(elements[1]); + + if (timeError !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(timeError); + } else if (altError !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(altError); + } else { + data.currentUtc = [elements[0].length === 5 ? elements[0].substring(0, 4) : elements[0], true]; + data.currentAltitude = [elements[1], true]; + } + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.activeWaypoint[1] = null; + data.activeWaypoint[2] = true; + } else { + const error = InputValidation.validateScratchpadTime(value, false); + + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.activeWaypoint[1] = value.length === 5 ? value.substring(0, 4) : value; + data.activeWaypoint[2] = true; + } + } + + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcPositionReport.CanSendData(data)) { + const report = CDUAtcPositionReport.CreateReport(mcdu, data); + if (requestMessage) { + requestMessage.Response = report; + CDUAtcTextFansA.ShowPage1(mcdu, [requestMessage]); + } else { + CDUAtcTextFansA.ShowPage1(mcdu, [report]); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcPositionReport.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const report = CDUAtcPositionReport.CreateReport(mcdu, data); + if (requestMessage) { + requestMessage.Response = report; + mcdu.atsu.updateMessage(requestMessage); + } else { + mcdu.atsu.registerMessages([report]); + } + CDUAtcPositionReport.ShowPage1(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); + }; + mcdu.onNextPage = () => { + CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); + }; + } + + static ShowPage2( + mcdu: LegacyAtsuPageInterface, + requestMessage = null, + data = CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, true), + ) { + mcdu.page.Current = mcdu.page.ATCPositionReport2; + + const wind = data.wind[0] ? data.wind[0].split('/') : ['[ ]', '[ ]']; + const sat = data.sat[0] ? data.sat[0] : '[ ]'; + const turbulence = data.turbulence[0] ? data.turbulence[0] : '[ ]'; + const icing = data.icing[0] ? data.icing[0] : '[ ]'; + let eta = data.eta[0] ? data.eta[0] : '[ ]'; + if (data.eta[1] === false && data.eta[0]) { + eta = `{small}${eta}{end}`; + } + const endurance = data.endurance[0] ? data.endurance[0] : '[ ]'; + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcPositionReport.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + } + if (CDUAtcPositionReport.CanEraseData(data)) { + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['POSITION REPORT', '2', '3'], + ['\xa0WIND', 'SAT\xa0'], + [ + `{cyan}${data.wind[1] === false && data.wind[0] ? '{small}' : ''}${wind[0]}/${wind[1]}${data.wind[1] === false && data.wind[0] ? '{end}' : ''}{end}`, + `{cyan}${data.sat[1] === false && data.sat[0] ? '{small}' : ''}${sat}${data.sat[1] === false && data.sat[0] ? '{end}' : ''}{end}`, + ], + ['\xa0ICING(TLMS)', 'TURB(LMS)\xa0'], + [`{cyan}${icing}{end}`, `{cyan}${turbulence}{end}`], + ['\xa0ETA', 'ENDURANCE\xa0'], + [`{cyan}${eta}{end}`, `{cyan}${endurance}{end}`], + [''], + [''], + ['\xa0ALL FIELDS'], + [erase, text], + [`${requestMessage ? '\xa0ATC MENU' : '\xa0ATC REPORTS'}`, 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.wind = [null, true]; + } else { + const error = InputValidation.validateScratchpadWind(value); + if (error === AtsuStatusCodes.Ok) { + data.wind = [InputValidation.formatScratchpadWind(value), true]; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.icing = [null, true]; + } else if (value === 'T' || value === 'L' || value === 'M' || value === 'S') { + data.icing = [value, true]; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + } + CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.eta = [null, true]; + } else if (InputValidation.validateScratchpadTime(value)) { + data.eta = [value, true]; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcPositionReport.ShowPage2( + mcdu, + requestMessage, + CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, false), + ); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + if (requestMessage) { + CDUAtcMenu.ShowPage(mcdu); + } else { + CDUAtcReports.ShowPage(mcdu); + } + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.sat = [null, true]; + } else { + const error = InputValidation.validateScratchpadTemperature(value); + if (error === AtsuStatusCodes.Ok) { + data.sat = [InputValidation.formatScratchpadTemperature(value), true]; + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.turbulence = [null, true]; + } else if (value === 'L' || value === 'M' || value === 'S') { + data.turbulence = [value, true]; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.entryOutOfRange); + } + CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.endurance = [null, true]; + } else if (InputValidation.validateScratchpadEndurance(value)) { + data.endurance = [InputValidation.formatScratchpadEndurance(value), true]; + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcPositionReport.CanSendData(data)) { + const report = CDUAtcPositionReport.CreateReport(mcdu, data); + if (requestMessage) { + requestMessage.Response = report; + CDUAtcTextFansA.ShowPage1(mcdu, [requestMessage]); + } else { + CDUAtcTextFansA.ShowPage1(mcdu, [report]); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcPositionReport.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const report = CDUAtcPositionReport.CreateReport(mcdu, data); + if (requestMessage) { + requestMessage.Response = report; + mcdu.atsu.updateMessage(requestMessage); + } else { + mcdu.atsu.registerMessages([report]); + } + CDUAtcPositionReport.ShowPage1(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + }; + mcdu.onNextPage = () => { + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); + }; + } + + static ShowPage3( + mcdu: LegacyAtsuPageInterface, + requestMessage = null, + data = CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, true), + ) { + mcdu.page.Current = mcdu.page.ATCPositionReport3; + + let indicatedAirspeed = data.indicatedAirspeed[0] ? data.indicatedAirspeed[0] : '[ ]'; + if (data.indicatedAirspeed[0] && data.indicatedAirspeed[1] === false) { + indicatedAirspeed = `{small}${indicatedAirspeed}{end}`; + } + let groundSpeed = data.groundSpeed[0] ? data.groundSpeed[0] : '[ ]'; + if (data.groundSpeed[0] && data.groundSpeed[1] === false) { + groundSpeed = `{small}${groundSpeed}{end}`; + } + let verticalSpeed = data.verticalSpeed[0] ? data.verticalSpeed[0] : '[ ]'; + if (data.verticalSpeed[0] && data.verticalSpeed[1] === false) { + verticalSpeed = `{small}${verticalSpeed}{end}`; + } + const deviating = data.deviating[0] ? data.deviating[0] : '[ ]'; + let heading = data.heading[0] ? `${data.heading[0]}°TRUE` : '[ ]'; + if (data.heading[0] && data.heading[1] === false) { + heading = `{small}${heading}{end}`; + } + let track = data.track[0] ? `${data.track[0]}{white}°{end}` : '[ ]'; + if (data.track[0] && data.track[1] === false) { + track = `{small}${track}{end}`; + } + const descending = ['\xa0DSCENDING TO', '[ ]']; + const climbing = ['CLBING TO\xa0', '[ ]']; + + const current = data.atsuFlightStateData; + const target = data.atsuAutopilotData; + if (target.apActive && target.altitude === current.altitude) { + descending[0] = descending[1] = ''; + climbing[0] = climbing[1] = ''; + } else if (data.climbing[0]) { + descending[0] = descending[1] = ''; + climbing[1] = data.climbing[0]; + } else if (data.descending[0]) { + climbing[0] = climbing[1] = ''; + // FIXME bug (won't compile) + // descending = data.descending[0]; + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcPositionReport.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + } + if (CDUAtcPositionReport.CanEraseData(data)) { + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['POSITION REPORT', '3', '3'], + ['\xa0SPEED', 'GROUND SPD\xa0'], + [`{cyan}${indicatedAirspeed}{end}`, `{cyan}${groundSpeed}{end}`], + ['\xa0VERT SPEED', 'DEVIATING\xa0'], + [`{cyan}${verticalSpeed}{end}`, `{cyan}${deviating}{end}`], + ['\xa0HEADING', 'TRACK ANGLE\xa0'], + [`{cyan}${heading}{end}`, `{cyan}${track}{end}`], + [descending[0], climbing[0]], + [`{cyan}${descending[1]}{end}`, `{cyan}${climbing[1]}{end}`], + ['\xa0ALL FIELDS'], + [erase, text], + [`${requestMessage ? '\xa0ATC MENU' : '\xa0ATC REPORTS'}`, 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.indicatedAirspeed = [null, true]; + } else { + const error = InputValidation.validateScratchpadSpeed(value); + if (error === AtsuStatusCodes.Ok) { + data.indicatedAirspeed = [InputValidation.formatScratchpadSpeed(value), true]; + } else { + mcdu.addNewAtsuMessage(error); + } + } + + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.verticalSpeed = [null, true]; + } else { + const error = InputValidation.validateScratchpadVerticalSpeed(value); + if (error === AtsuStatusCodes.Ok) { + data.verticalSpeed = [InputValidation.formatScratchpadVerticalSpeed(value), true]; + } else { + mcdu.addNewAtsuMessage(error); + } + } + + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.heading = [null, true]; + } else { + const error = InputValidation.validateScratchpadDegree(value); + if (error === AtsuStatusCodes.Ok) { + data.heading = [value, true]; + } else { + mcdu.addNewAtsuMessage(error); + } + } + + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + const current = data.atsuFlightStateData; + const target = data.atsuAutopilotData; + + if (!target.apActive || (target.apActive && target.altitude !== current.altitude)) { + if (value === Keypad.clrValue) { + data.descending = [null, true]; + } else { + const error = InputValidation.validateScratchpadAltitude(value); + if (error === AtsuStatusCodes.Ok) { + data.descending = [InputValidation.formatScratchpadAltitude(value), true]; + } else { + mcdu.addNewAtsuMessage(error); + } + } + } + + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcPositionReport.ShowPage3( + mcdu, + requestMessage, + CDUAtcPositionReport.CreateDataBlock(mcdu, requestMessage, false), + ); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + if (requestMessage) { + CDUAtcMenu.ShowPage(mcdu); + } else { + CDUAtcReports.ShowPage(mcdu); + } + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.groundSpeed = [null, true]; + } else { + const error = InputValidation.validateScratchpadSpeed(value); + if (error === AtsuStatusCodes.Ok) { + data.groundSpeed = [InputValidation.formatScratchpadSpeed(value), true]; + } else { + mcdu.addNewAtsuMessage(error); + } + } + + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.deviating = [null, true]; + } else { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.deviating = [InputValidation.formatScratchpadOffset(value), true]; + } else { + mcdu.addNewAtsuMessage(error); + } + } + + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.track = [null, true]; + } else { + const error = InputValidation.validateScratchpadDegree(value); + if (error === AtsuStatusCodes.Ok) { + data.track = [value, true]; + } else { + mcdu.addNewAtsuMessage(error); + } + } + + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); + }; + + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = (value) => { + // FIXME these functions don't exist + //const current = mcdu.atsu.currentFlightState(); + //const target = mcdu.atsu.targetFlightState(); + + if (!target.apActive || (target.apActive && target.altitude !== current.altitude)) { + if (value === Keypad.clrValue) { + data.climbing = [null, true]; + } else { + const error = InputValidation.validateScratchpadAltitude(value); + if (error === AtsuStatusCodes.Ok) { + data.climbing = [InputValidation.formatScratchpadAltitude(value), true]; + } else { + mcdu.addNewAtsuMessage(error); + } + } + } + + CDUAtcPositionReport.ShowPage3(mcdu, requestMessage, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcPositionReport.CanSendData(data)) { + const report = CDUAtcPositionReport.CreateReport(mcdu, data); + if (requestMessage) { + requestMessage.Response = report; + CDUAtcTextFansA.ShowPage1(mcdu, [requestMessage]); + } else { + CDUAtcTextFansA.ShowPage1(mcdu, [report]); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcPositionReport.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const report = CDUAtcPositionReport.CreateReport(mcdu, data); + if (requestMessage) { + requestMessage.Response = report; + mcdu.atsu.updateMessage(requestMessage); + } else { + mcdu.atsu.registerMessages([report]); + } + CDUAtcPositionReport.ShowPage1(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcPositionReport.ShowPage2(mcdu, requestMessage, data); + }; + mcdu.onNextPage = () => { + CDUAtcPositionReport.ShowPage1(mcdu, requestMessage, data); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_ProcedureRequest.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_ProcedureRequest.ts new file mode 100644 index 00000000000..6d373fdc64a --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_ProcedureRequest.ts @@ -0,0 +1,210 @@ +import { AtsuStatusCodes, CpdlcMessage, CpdlcMessagesDownlink, InputValidation } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcFlightReq } from '../A320_Neo_CDU_ATC_FlightReq'; +import { CDUAtcTextFansA } from '../FansA/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcProcedureRequest { + static CreateDataBlock() { + return { + sid: null, + departureTransition: null, + star: null, + arrivalTransition: null, + }; + } + + static CanSendData(data) { + return data.sid || data.departureTransition || data.star || data.arrivalTransition; + } + + static CreateRequest(mcdu, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu, data) { + const retval = []; + + if (data.sid) { + retval.push(CDUAtcProcedureRequest.CreateRequest(mcdu, 'DM23', [data.sid])); + } + if (data.departureTransition) { + retval.push(CDUAtcProcedureRequest.CreateRequest(mcdu, 'DM23', [data.departureTransition])); + } + if (data.star) { + retval.push(CDUAtcProcedureRequest.CreateRequest(mcdu, 'DM23', [data.star])); + } + if (data.arrivalTransition) { + retval.push(CDUAtcProcedureRequest.CreateRequest(mcdu, 'DM23', [data.arrivalTransition])); + } + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, data = CDUAtcProcedureRequest.CreateDataBlock()) { + mcdu.clearDisplay(); + + let sid = '[ ][color]cyan'; + let star = '[ ][color]cyan'; + let arrivalTransition = '[ ][color]cyan'; + let departureTransition = '[ ][color]cyan'; + + if (data.sid) { + sid = `${data.sid}[color]cyan`; + } + if (data.star) { + star = `${data.star}[color]cyan`; + } + if (data.arrivalTransition) { + arrivalTransition = `${data.arrivalTransition}[color]cyan`; + } + if (data.departureTransition) { + departureTransition = `${data.departureTransition}[color]cyan`; + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcProcedureRequest.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['PROCEDURE REQ'], + ['\xa0SID--------------TRANS\xa0'], + [sid, departureTransition], + ['\xa0STAR---------------VIA\xa0'], + [star, arrivalTransition], + [''], + [''], + [''], + [''], + ['\xa0ALL FIELDS'], + [erase, text], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.sid = null; + } else if (value) { + const error = InputValidation.validateScratchpadPosition(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.sid = value; + } + } + CDUAtcProcedureRequest.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.star = null; + } else if (value) { + const error = InputValidation.validateScratchpadPosition(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.star = value; + } + } + CDUAtcProcedureRequest.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcProcedureRequest.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.departureTransition = null; + } else if (value) { + const error = InputValidation.validateScratchpadPosition(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.departureTransition = value; + } + } + CDUAtcProcedureRequest.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.arrivalTransition = null; + } else if (value) { + const error = InputValidation.validateScratchpadPosition(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.arrivalTransition = value; + } + } + CDUAtcProcedureRequest.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcProcedureRequest.CanSendData(data)) { + const messages = CDUAtcProcedureRequest.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcProcedureRequest.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcProcedureRequest.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcProcedureRequest.ShowPage(mcdu); + } + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_Reports.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_Reports.ts new file mode 100644 index 00000000000..654566bd6cc --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_Reports.ts @@ -0,0 +1,184 @@ +import { AtsuStatusCodes, CpdlcMessage, CpdlcMessagesDownlink, InputValidation } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcMenu } from '../A320_Neo_CDU_ATC_Menu'; +import { CDUAtcPositionReport } from '../FansA/A320_Neo_CDU_ATC_PositionReport'; +import { CDUAtcTextFansA } from '../FansA/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcReports { + static CreateDataBlock() { + return { + backOnRoute: false, + deviating: null, + updateInProgress: false, + }; + } + + static CanSendData(data) { + return data.requestContact || data.deviating; + } + + static CreateRequest(mcdu: LegacyAtsuPageInterface, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu: LegacyAtsuPageInterface, data) { + const retval = []; + + if (data.requestContact) { + retval.push(CDUAtcReports.CreateRequest(mcdu, 'DM20')); + } + if (data.deviating) { + const elements = InputValidation.expandLateralOffset(data.deviating).split(' '); + retval.push(CDUAtcReports.CreateRequest(mcdu, 'DM80', [elements[0], elements[1]])); + } + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, data = CDUAtcReports.CreateDataBlock()) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCReports; + + let backOnRoute = '{cyan}{{end}BACK ON RTE'; + if (data.backOnRoute) { + backOnRoute = '{cyan}\xa0BACK ON RTE{end}'; + } + let deviating = '{cyan}[ ]{end}'; + if (data.deviating) { + deviating = `{cyan}${data.deviating}{end}`; + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcReports.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['REPORTS'], + [''], + [backOnRoute], + ['\xa0DEVIATING'], + [deviating], + [''], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.backOnRoute = false; + } else { + data.backOnRoute = true; + } + CDUAtcReports.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.deviating = null; + } else { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.deviating = InputValidation.formatScratchpadOffset(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcReports.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = () => { + CDUAtcPositionReport.ShowPage1(mcdu); + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = () => { + mcdu.atsu.toggleAutomaticPositionReport().then((status) => { + if (status !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(status); + } + + data.updateInProgress = false; + CDUAtcReports.ShowPage(mcdu, data); + }); + + data.updateInProgress = true; + CDUAtcReports.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcReports.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcReports.CanSendData(data)) { + const messages = CDUAtcReports.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcReports.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcReports.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcReports.ShowPage(mcdu); + } + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_Text.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_Text.ts new file mode 100644 index 00000000000..614713d881b --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_Text.ts @@ -0,0 +1,423 @@ +import { CpdlcMessage, CpdlcMessagesDownlink, FansMode } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcFlightReq } from '../A320_Neo_CDU_ATC_FlightReq'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcTextFansA { + static CreateDataBlock(): any { + return { + performance: false, + weather: false, + turbulence: false, + discretion: false, + icing: false, + freetext: ['', '', '', '', ''], + }; + } + + static CanSendData(messages, data) { + if (messages.length !== 0) { + return true; + } + + const freetext = data.freetext.filter((n) => n); + return freetext.length !== 0; + } + + static CanEraseData(data) { + if (data.performance || data.weather || data.turbulence || data.discretion || data.icing) { + return true; + } + const freetext = data.freetext.filter((n) => n); + return freetext.length !== 0; + } + + static CreateMessages(mcdu, messages, data) { + const freetextLines = data.freetext.filter((n) => n); + let freetextElement = null; + let updateFreetext = true; + + // create the freetext elements + if (freetextLines.length !== 0) { + if (mcdu.atsu.fansMode() === FansMode.FansB) { + freetextElement = CpdlcMessagesDownlink['DM98'][1].deepCopy(); + } else { + freetextElement = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + } + freetextElement.Content[0].Value = freetextLines.join('\n'); + } + + // create the extensions + let extension = null; + if (data.performance) { + extension = CpdlcMessagesDownlink['DM66'][1].deepCopy(); + } else if (data.weather) { + extension = CpdlcMessagesDownlink['DM65'][1].deepCopy(); + } else if (data.turbulence) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = 'DUE TO TURBULENCE'; + } else if (data.discretion) { + extension = CpdlcMessagesDownlink['DM75'][1].deepCopy(); + } else if (data.icing) { + extension = CpdlcMessagesDownlink['DM67'][1].deepCopy(); + extension.Content[0].Value = 'DUE TO ICING'; + } + + if (messages.length === 0) { + // the freetext is set (guaranteed due to CanSendData) + messages.push(new CpdlcMessage()); + messages[0].Content.push(freetextElement); + messages[0].Station = mcdu.atsu.currentStation(); + updateFreetext = false; + } + + // update all messages, if needed + if (extension || (updateFreetext && freetextElement)) { + messages.forEach((message) => { + if (message.Content[0].TypeId.includes('UM')) { + if (updateFreetext && freetextElement) { + message.Response.Content.push(freetextElement); + } + if (extension) { + message.Response.Content.push(extension); + } + } else { + if (updateFreetext && freetextElement) { + message.Content.push(freetextElement); + } + if (extension) { + message.Content.push(extension); + } + } + }); + } + + return messages; + } + + static ShowPage1(mcdu: LegacyAtsuPageInterface, messages = [], data = CDUAtcTextFansA.CreateDataBlock()) { + mcdu.clearDisplay(); + + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcTextFansA.CanSendData(messages, data)) { + reqDisplay = 'DCDU*[color]cyan'; + } + if (CDUAtcTextFansA.CanEraseData(data)) { + erase = '*ERASE'; + } + + const acPerform = ['\xa0DUE TO', '{cyan}{{end}A/C PERFORM']; + if (data.performance) { + acPerform[0] += '[color]cyan'; + acPerform[1] = '\xa0A/C PERFORM[color]cyan'; + } + const weather = ['DUE TO\xa0', 'WEATHER{cyan}}{end}']; + if (data.weather) { + weather[0] += '[color]cyan'; + weather[1] = 'WEATHER\xa0[color]cyan'; + } + const turbulence = ['DUE TO\xa0', 'TURBULENCE{cyan}}{end}']; + if (data.turbulence) { + turbulence[0] += '[color]cyan'; + turbulence[1] = 'TURBULENCE\xa0[color]cyan'; + } + const discretion = ['\xa0AT PILOTS', '{cyan}{{end}DISCRETION']; + if (data.discretion) { + discretion[0] += '[color]cyan'; + discretion[1] = '\xa0DISCRETION[color]cyan'; + } + const icing = ['DUE TO\xa0', 'ICING{cyan}}{end}']; + if (data.icing) { + icing[0] += '[color]cyan'; + icing[1] = 'ICING\xa0[color]cyan'; + } + let freetext = + '[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0][color]cyan'; + if (data.freetext[0] !== '') { + freetext = data.freetext[0]; + } + + mcdu.setTemplate([ + ['FREE TEXT', '1', '2'], + [acPerform[0], weather[0]], + [acPerform[1], weather[1]], + [discretion[0], turbulence[0]], + [discretion[1], turbulence[1]], + ['', icing[0]], + ['', icing[1]], + ['---------FREE TEXT---------'], + [freetext], + ['\xa0ALL FIELDS'], + [erase], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.performance = false; + } else { + const oldFreetext = data.freetext; + data = CDUAtcTextFansA.CreateDataBlock(); + data.performance = true; + data.freetext = oldFreetext; + } + CDUAtcTextFansA.ShowPage1(mcdu, messages, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.discretion = false; + } else { + const oldFreetext = data.freetext; + data = CDUAtcTextFansA.CreateDataBlock(); + data.discretion = true; + data.freetext = oldFreetext; + } + CDUAtcTextFansA.ShowPage1(mcdu, messages, data); + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.freetext[0] = ''; + } else if (value) { + data.freetext[0] = value; + } + CDUAtcTextFansA.ShowPage1(mcdu, messages, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcTextFansA.ShowPage1(mcdu, messages); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.weather = false; + } else { + const oldFreetext = data.freetext; + data = CDUAtcTextFansA.CreateDataBlock(); + data.weather = true; + data.freetext = oldFreetext; + } + CDUAtcTextFansA.ShowPage1(mcdu, messages, data); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.turbulence = false; + } else { + const oldFreetext = data.freetext; + data = CDUAtcTextFansA.CreateDataBlock(); + data.turbulence = true; + data.freetext = oldFreetext; + } + CDUAtcTextFansA.ShowPage1(mcdu, messages, data); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.icing = false; + } else { + const oldFreetext = data.freetext; + data = CDUAtcTextFansA.CreateDataBlock(); + data.icing = true; + data.freetext = oldFreetext; + } + CDUAtcTextFansA.ShowPage1(mcdu, messages, data); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcTextFansA.CanSendData(messages, data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const prepMessages = CDUAtcTextFansA.CreateMessages(mcdu, messages, data); + if (prepMessages && prepMessages[0].Content[0].TypeId.includes('UM')) { + mcdu.atsu.updateMessage(prepMessages[0]); + } else if (prepMessages) { + mcdu.atsu.registerMessages(prepMessages); + } + CDUAtcTextFansA.ShowPage1(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcTextFansA.ShowPage2(mcdu, messages, data); + }; + mcdu.onNextPage = () => { + CDUAtcTextFansA.ShowPage2(mcdu, messages, data); + }; + } + + static ShowPage2(mcdu: LegacyAtsuPageInterface, messages = [], data = CDUAtcTextFansA.CreateDataBlock()) { + mcdu.clearDisplay(); + + let freetext1 = + '[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0][color]cyan'; + if (data.freetext[1] !== '') { + freetext1 = data.freetext[1]; + } + let freetext2 = + '[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0][color]cyan'; + if (data.freetext[2] !== '') { + freetext2 = data.freetext[2]; + } + let freetext3 = + '[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0][color]cyan'; + if (data.freetext[3] !== '') { + freetext3 = data.freetext[3]; + } + let freetext4 = + '[\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0][color]cyan'; + if (data.freetext[4] !== '') { + freetext4 = data.freetext[4]; + } + + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcTextFansA.CanSendData(messages, data)) { + reqDisplay = 'DCDU*[color]cyan'; + } + if (CDUAtcTextFansA.CanEraseData(data)) { + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['FREE TEXT', '2', '2'], + [''], + [freetext1], + [''], + [freetext2], + [''], + [freetext3], + [''], + [freetext4], + ['\xa0ALL FIELDS'], + [erase], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.freetext[1] = ''; + } else if (value) { + data.freetext[1] = value; + } + CDUAtcTextFansA.ShowPage2(mcdu, messages, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.freetext[2] = ''; + } else if (value) { + data.freetext[2] = value; + } + CDUAtcTextFansA.ShowPage2(mcdu, messages, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.freetext[3] = ''; + } else if (value) { + data.freetext[3] = value; + } + CDUAtcTextFansA.ShowPage2(mcdu, messages, data); + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.freetext[4] = ''; + } else if (value) { + data.freetext[4] = value; + } + CDUAtcTextFansA.ShowPage2(mcdu, messages, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcTextFansA.ShowPage2(mcdu, messages); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcTextFansA.CanSendData(messages, data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const prepMessages = CDUAtcTextFansA.CreateMessages(mcdu, messages, data); + if (prepMessages && prepMessages[0].Content[0].TypeId.includes('UM')) { + mcdu.atsu.updateMessage(prepMessages[0]); + } else if (prepMessages) { + mcdu.atsu.registerMessages(prepMessages); + } + CDUAtcTextFansA.ShowPage2(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcTextFansA.ShowPage1(mcdu, messages, data); + }; + mcdu.onNextPage = () => { + CDUAtcTextFansA.ShowPage1(mcdu, messages, data); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_UsualRequest.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_UsualRequest.ts new file mode 100644 index 00000000000..34b37081ca7 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_UsualRequest.ts @@ -0,0 +1,350 @@ +import { AtsuStatusCodes, CpdlcMessage, CpdlcMessagesDownlink, InputValidation } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcMenu } from '../A320_Neo_CDU_ATC_Menu'; +import { CDUAtcTextFansA } from '../FansA/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +export class CDUAtcUsualRequestFansA { + static CreateDataBlock() { + return { + directTo: null, + speed: null, + heading: null, + weatherDeviation: null, + climbTo: null, + descentTo: null, + dueToWeather: false, + requestDescent: false, + }; + } + + static CanSendData(data) { + return ( + data.directTo || + data.speed || + data.heading || + data.weatherDeviation || + data.climbTo || + data.descentTo || + data.requestDescent + ); + } + + static CanEraseData(data) { + return ( + data.directTo || + data.speed || + data.heading || + data.weatherDeviation || + data.climbTo || + data.descentTo || + data.dueToWeather || + data.requestDescent + ); + } + + static CreateRequest(mcdu, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu, data) { + const retval = []; + + let extension = null; + if (data.dueToWeather) { + extension = CpdlcMessagesDownlink['DM65'][1].deepCopy(); + } + + if (data.directTo) { + retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, 'DM22', [data.directTo])); + } + if (data.weatherDeviation) { + const elements = InputValidation.expandLateralOffset(data.weatherDeviation).split(' '); + retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, 'DM27', [elements[0], elements[1]])); + } + if (data.heading) { + retval.push( + CDUAtcUsualRequestFansA.CreateRequest(mcdu, 'DM70', [data.heading === 0 ? '360' : data.heading.toString()]), + ); + } + if (data.climbTo) { + retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, 'DM9', [data.climbTo])); + } + if (data.descentTo) { + retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, 'DM10', [data.descentTo])); + } + if (data.requestDescent) { + retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, 'DM67')); + retval[retval.length - 1].Content[0].Content[0].Value = 'REQUEST DESCENT'; + } + if (data.speed) { + retval.push(CDUAtcUsualRequestFansA.CreateRequest(mcdu, 'DM18', [data.speed])); + } + + if (extension) { + retval.forEach((message) => { + if (message.Content[0].TypeId !== 'DM27') { + message.Content.push(extension); + } + }); + } + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, data = CDUAtcUsualRequestFansA.CreateDataBlock()) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCUsualRequest; + + let addText = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcUsualRequestFansA.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + addText = 'ADD TEXT>'; + } + if (CDUAtcUsualRequestFansA.CanEraseData(data)) { + erase = '*ERASE'; + } + + let directTo = '{cyan}[ ]{end}'; + if (data.directTo) { + directTo = `${data.directTo}[color]cyan`; + } + let heading = '[ ]°[color]cyan'; + if (data.heading !== null) { + heading = `${data.heading}°[color]cyan`; + } + let weatherDeviation = '{cyan}[ ]{end}'; + if (data.weatherDeviation) { + weatherDeviation = `${data.weatherDeviation}[color]cyan`; + } + let speed = '[ ][color]cyan'; + if (data.speed) { + speed = `${data.speed}[color]cyan`; + } + let climbTo = '[ ][color]cyan'; + let descentTo = '[ ][color]cyan'; + if (data.climbTo) { + climbTo = `${data.climbTo}[color]cyan`; + } + if (data.descentTo) { + descentTo = `${data.descentTo}[color]cyan`; + } + + const dueToWeather = ['\xa0DUE TO', '{cyan}{{end}WEATHER']; + const requestDescent = ['REQUEST\xa0', 'DESCENT{cyan}}{end}']; + if (data.dueToWeather) { + dueToWeather[0] = '{cyan}\xa0DUE TO{end}'; + dueToWeather[1] = '{cyan}\xa0WEATHER{end}'; + } + if (data.requestDescent) { + requestDescent[0] = '{cyan}REQUEST\xa0{end}'; + requestDescent[1] = '{cyan}DESCENT\xa0{end}'; + } + + mcdu.setTemplate([ + ['USUAL REQ'], + ['\xa0DIR TO', 'SPEED\xa0'], + [directTo, speed], + ['\xa0HDG', 'WX DEV\xa0'], + [heading, weatherDeviation], + ['\xa0CLB TO', 'DES TO\xa0'], + [climbTo, descentTo], + [dueToWeather[0], requestDescent[0]], + [dueToWeather[1], requestDescent[1]], + ['\xa0ALL FIELDS'], + [erase, addText], + ['\xa0ATC MENU', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.directTo = null; + } else if (value) { + if (WaypointEntryUtils.isLatLonFormat(value)) { + // format: DDMM.MB/EEEMM.MC + try { + WaypointEntryUtils.parseLatLon(value); + data.directTo = value; + } catch (err) { + if (err === NXSystemMessages.formatError) { + mcdu.setScratchpadMessage(err); + } + } + } else if (/^[A-Z0-9]{2,7}/.test(value)) { + // place format + data.directTo = value; + CDUAtcUsualRequestFansA.ShowPage(mcdu, data); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + + CDUAtcUsualRequestFansA.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.heading = null; + } else if (value) { + const error = InputValidation.validateScratchpadDegree(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.heading = parseInt(value) % 360; + } + } + + CDUAtcUsualRequestFansA.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.climbTo = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.climbTo = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcUsualRequestFansA.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.dueToWeather = false; + } else { + data.dueToWeather = true; + } + CDUAtcUsualRequestFansA.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcUsualRequestFansA.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.speed = null; + } else if (value) { + const error = InputValidation.validateScratchpadSpeed(value); + if (error === AtsuStatusCodes.Ok) { + data.speed = InputValidation.formatScratchpadSpeed(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcUsualRequestFansA.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = async (value) => { + if (value === Keypad.clrValue) { + data.weatherDeviation = null; + } else if (value) { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.weatherDeviation = InputValidation.formatScratchpadOffset(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcUsualRequestFansA.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.descentTo = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.descentTo = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcUsualRequestFansA.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.requestDescent = false; + } else { + data.requestDescent = true; + } + CDUAtcUsualRequestFansA.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcUsualRequestFansA.CanSendData(data)) { + const requests = CDUAtcUsualRequestFansA.CreateRequests(mcdu, data); + if (requests.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, requests); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcUsualRequestFansA.CanSendData(data)) { + const requests = CDUAtcUsualRequestFansA.CreateRequests(mcdu, data); + if (requests.length !== 0) { + mcdu.atsu.registerMessages(requests); + CDUAtcUsualRequestFansA.ShowPage(mcdu); + } + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_VertRequest.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_VertRequest.ts new file mode 100644 index 00000000000..fa1a453a2dd --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansA/A320_Neo_CDU_ATC_VertRequest.ts @@ -0,0 +1,583 @@ +import { + AtsuStatusCodes, + CpdlcMessage, + CpdlcMessagesDownlink, + InputValidation, + InputWaypointType, +} from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcFlightReq } from '../A320_Neo_CDU_ATC_FlightReq'; +import { CDUAtcTextFansA } from '../FansA/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcVertRequestFansA { + static CreateDataBlock(): any { + return { + climb: null, + climbStart: null, + descend: null, + descendStart: null, + startAt: null, + whenHigher: false, + whenLower: false, + altitude: null, + blockAltitudeLow: null, + blockAltitudeHigh: null, + requestDescent: false, + cruise: null, + }; + } + + static CanSendData(data) { + return ( + data.climb || + data.climbStart || + data.descendStart || + data.descend || + data.startAt || + data.altitude || + data.whenHigher || + data.whenLower || + data.blockAltitudeLow || + data.blockAltitudeHigh || + data.cruise || + data.requestDescent + ); + } + + static HandleClbDesStart(mcdu, value, data, climbRequest) { + if (value === Keypad.clrValue || !value) { + if (climbRequest) { + data.climbStart = null; + } else { + data.descendStart = null; + } + data.startAt = null; + } else { + const entries = value.split('/'); + let updateAlt = false; + let altitude = null; + let start = null; + + const error = InputValidation.validateScratchpadAltitude(entries[0]); + if (!error) { + updateAlt = true; + altitude = InputValidation.formatScratchpadAltitude(entries[0]); + entries.shift(); + } + + if (entries.length !== 0) { + const startingPoint = entries.join('/'); + + const type = InputValidation.classifyScratchpadWaypointType(startingPoint, true); + if (altitude || (data.climb && climbRequest) || (data.descend && !climbRequest)) { + switch (type[0]) { + case InputWaypointType.GeoCoordinate: + case InputWaypointType.Place: + start = startingPoint; + break; + case InputWaypointType.Timepoint: + if (startingPoint.endsWith('Z')) { + start = startingPoint; + } else { + start = `${startingPoint}Z`; + } + break; + default: + mcdu.addNewAtsuMessage(type[1]); + start = null; + if (updateAlt) { + altitude = null; + } + break; + } + } + + if (altitude || start) { + if (altitude && start) { + data.startAt = start; + if (climbRequest) { + data.climbStart = altitude; + } else { + data.descendStart = altitude; + } + } else if (altitude) { + // update the altitude and keep the start at + const lastStart = data.startAt; + data.startAt = lastStart; + if (climbRequest) { + data.climbStart = altitude; + } else { + data.descendStart = altitude; + } + } else if (start && (data.climbStart || data.descendStart)) { + // update start at if climb or descend are set + data.startAt = start; + } + } + + CDUAtcVertRequestFansA.ShowPage2(mcdu, data); + } else if (updateAlt) { + if (climbRequest) { + data.climbStart = altitude; + } else { + data.descendStart = altitude; + } + } else if (error) { + mcdu.addNewAtsuMessage(error); + } + } + + CDUAtcVertRequestFansA.ShowPage2(mcdu, data); + } + + static CreateRequest(mcdu, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu, data) { + const retval = []; + + if (data.climb) { + retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, 'DM9', [data.climb])); + } + if (data.climbStart) { + retval.push( + CDUAtcVertRequestFansA.CreateRequest(mcdu, /$[0-9]{4}Z^/.test(data.startAt) ? 'DM13' : 'DM11', [ + data.startAt, + data.climbStart, + ]), + ); + } + if (data.descend) { + retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, 'DM10', [data.descend])); + } + if (data.descendStart) { + retval.push( + CDUAtcVertRequestFansA.CreateRequest(mcdu, /$[0-9]{4}Z^/.test(data.startAt) ? 'DM14' : 'DM12', [ + data.startAt, + data.descendStart, + ]), + ); + } + if (data.altitude) { + retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, 'DM6', [data.altitude])); + } + if (data.whenHigher) { + retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, 'DM53')); + } + if (data.whenLower) { + retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, 'DM52')); + } + if (data.blockAltitudeLow && data.blockAltitudeHigh) { + retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, 'DM7', [data.blockAltitudeLow, data.blockAltitudeHigh])); + } + if (data.requestDescent) { + retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, 'DM67')); + retval[retval.length - 1].Content[0].Content[0].Value = 'REQUEST DESCENT'; + } + if (data.cruise) { + retval.push(CDUAtcVertRequestFansA.CreateRequest(mcdu, data.whenCruise ? 'DM54' : 'DM8', [data.cruise])); + } + + return retval; + } + + static ShowPage1(mcdu: LegacyAtsuPageInterface, data = CDUAtcVertRequestFansA.CreateDataBlock()) { + mcdu.clearDisplay(); + + let climbTo = '[ ][color]cyan'; + let descentTo = '[ ][color]cyan'; + if (data.climb) { + climbTo = `${data.climb}[color]cyan`; + } + if (data.descend) { + descentTo = `${data.descend}[color]cyan`; + } + let altitude = '[ ][color]cyan'; + if (data.altitude) { + altitude = `${data.altitude}[color]cyan`; + } + let requestDescentSmall = 'REQUEST\xa0'; + let requestDescent = 'DESCENT{cyan}}{end}'; + if (data.requestDescent) { + requestDescentSmall += '[color]cyan'; + requestDescent = 'DESCENT\xa0[color]cyan'; + } + let blockAlt = '[ ]/[ ][color]cyan'; + if (data.blockAltitudeLow && data.blockAltitudeHigh) { + blockAlt = `${data.blockAltitudeLow}/${data.blockAltitudeHigh}[color]cyan`; + } + let crzClimb = '[ ][color]cyan'; + if (data.cruise && !data.whenCruise) { + crzClimb = `${data.cruise}[color]cyan`; + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcVertRequestFansA.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['ATC VERT REQ', '1', '2'], + ['\xa0CLB TO', 'DES TO\xa0'], + [climbTo, descentTo], + ['\xa0ALT', requestDescentSmall], + [altitude, requestDescent], + ['\xa0BLOCK ALT/ALT'], + [blockAlt], + ['\xa0CRUISE CLB TO'], + [crzClimb], + ['\xa0ALL FIELDS'], + [erase, text], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.climb = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.climb = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcVertRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.altitude = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.altitude = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcVertRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.blockAltitudeLow = null; + data.blockAltitudeHigh = null; + } else if (value) { + const entries = value.split('/'); + if (entries.length !== 2) { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } else { + const error = InputValidation.validateAltitudeRange(entries[0], entries[1]); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.blockAltitudeLow = InputValidation.formatScratchpadAltitude(entries[0]); + data.blockAltitudeHigh = InputValidation.formatScratchpadAltitude(entries[1]); + CDUAtcVertRequestFansA.ShowPage1(mcdu, data); + } + } + } + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.cruise = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.cruise = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcVertRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.cruise = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.cruise = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcVertRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcVertRequestFansA.ShowPage1(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.descend = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.descend = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcVertRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.requestDescent = false; + } else { + data.requestDescent = true; + } + CDUAtcVertRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = (value) => { + if (value === Keypad.clrValue) { + if (data.whenSpeedRange) { + data.speedLow = null; + data.speedHigh = null; + data.whenSpeedRange = false; + } + } else if (value) { + const range = InputValidation.validateScratchpadSpeedRanges(value); + if (range[0] !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(range[0]); + } else { + if (range[1].length === 2) { + data.speedLow = range[1][0]; + data.speedHigh = range[1][1]; + data.whenSpeedRange = true; + } + } + } + CDUAtcVertRequestFansA.ShowPage1(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcVertRequestFansA.CanSendData(data)) { + const messages = CDUAtcVertRequestFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcVertRequestFansA.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcVertRequestFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcVertRequestFansA.ShowPage1(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcVertRequestFansA.ShowPage2(mcdu, data); + }; + mcdu.onNextPage = () => { + CDUAtcVertRequestFansA.ShowPage2(mcdu, data); + }; + } + + static ShowPage2(mcdu: LegacyAtsuPageInterface, data = CDUAtcVertRequestFansA.CreateDataBlock()) { + mcdu.clearDisplay(); + + let climbStart = '[ ]/[ ][color]cyan'; + if (data.climbStart) { + climbStart = `${data.climbStart}/${data.startAt ? data.startAt : '[ ]'}[color]cyan`; + } + let descendStart = '[ ]/[ ][color]cyan'; + if (data.descendStart) { + descendStart = `${data.descendStart}/${data.startAt ? data.startAt : '[ ]'}[color]cyan`; + } + + let higherAlt = '{cyan}{{end}HIGHER ALT'; + if (data.whenHigher) { + higherAlt = '\xa0HIGHER ALT[color]cyan'; + } + let lowerAlt = 'LOWER ALT{cyan}}{end}'; + if (data.whenLower) { + lowerAlt = 'LOWER ALT\xa0[color]cyan'; + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcVertRequestFansA.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['ATC VERT REQ', '2', '2'], + ['\xa0CLB TO/START AT'], + [climbStart], + ['\xa0DES TO/START AT'], + [descendStart], + ['---WHEN CAN WE EXPECT---'], + [higherAlt, lowerAlt], + [''], + [''], + ['\xa0ALL FIELDS'], + [erase, text], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + CDUAtcVertRequestFansA.HandleClbDesStart(mcdu, value, data, true); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + CDUAtcVertRequestFansA.HandleClbDesStart(mcdu, value, data, false); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.whenHigher = false; + } else { + data.whenHigher = true; + data.whenLower = false; + } + CDUAtcVertRequestFansA.ShowPage2(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcVertRequestFansA.ShowPage2(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.whenLower = false; + } else { + data.whenHigher = false; + data.whenLower = true; + } + CDUAtcVertRequestFansA.ShowPage2(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcVertRequestFansA.CanSendData(data)) { + const messages = CDUAtcVertRequestFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansA.ShowPage2(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcVertRequestFansA.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcVertRequestFansA.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcVertRequestFansA.ShowPage2(mcdu); + } + } + }; + + mcdu.onPrevPage = () => { + CDUAtcVertRequestFansA.ShowPage1(mcdu, data); + }; + mcdu.onNextPage = () => { + CDUAtcVertRequestFansA.ShowPage1(mcdu, data); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_Emergency.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_Emergency.ts new file mode 100644 index 00000000000..05912a955cd --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_Emergency.ts @@ -0,0 +1,32 @@ +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; +import { CDUAtcMenu } from '../A320_Neo_CDU_ATC_Menu'; + +export class CDUAtcEmergencyFansB { + static ShowPage(mcdu: LegacyAtsuPageInterface) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCEmergency; + + mcdu.setTemplate([ + ['{amber}EMERGENCY{end}'], + ['', 'EMERG ADS-C:OFF\xa0'], + ['', '{inop}SET ON*{end}'], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + ['\xa0ATC MENU'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_LatRequest.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_LatRequest.ts new file mode 100644 index 00000000000..717361240dd --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_LatRequest.ts @@ -0,0 +1,245 @@ +import { AtsuStatusCodes, CpdlcMessage, CpdlcMessagesDownlink, InputValidation } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcFlightReq } from '../A320_Neo_CDU_ATC_FlightReq'; +import { CDUAtcTextFansA } from '../FansA/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +export class CDUAtcLatRequestFansB { + static CreateDataBlock(): any { + return { + directTo: null, + weatherDeviation: null, + }; + } + + static CanSendData(data) { + return data.directTo || data.weatherDeviation; + } + + static CanEraseData(data) { + return data.directTo || data.weatherDeviation; + } + + static CreateRequest(mcdu: LegacyAtsuPageInterface, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu: LegacyAtsuPageInterface, data) { + const retval = []; + + if (data.directTo) { + retval.push(CDUAtcLatRequestFansB.CreateRequest(mcdu, 'DM22', [data.directTo])); + } + if (data.weatherDeviation) { + const elements = InputValidation.expandLateralOffset(data.weatherDeviation).split(' '); + retval.push(CDUAtcLatRequestFansB.CreateRequest(mcdu, 'DM27', [elements[0], elements[1]])); + } + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, data = CDUAtcLatRequestFansB.CreateDataBlock()) { + mcdu.clearDisplay(); + + let weatherDeviation = '{cyan}[ ]{end}'; + if (data.weatherDeviation) { + weatherDeviation = `${data.weatherDeviation}[color]cyan`; + } + let directTo = '{cyan}[ ]{end}'; + if (data.directTo) { + directTo = `${data.directTo}[color]cyan`; + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcLatRequestFansB.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + } + if (CDUAtcLatRequestFansB.CanEraseData(data)) { + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['ATC LAT REQ'], + ['\xa0DIR TO[color]white'], + [directTo], + [''], + [''], + ['', 'WX DEV'], + ['', weatherDeviation], + [''], + [''], + ['\xa0ALL FIELDS'], + [erase, text], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.directTo = null; + } else if (value) { + if (WaypointEntryUtils.isLatLonFormat(value)) { + // format: DDMM.MB/EEEMM.MC + try { + WaypointEntryUtils.parseLatLon(value); + data.directTo = value; + } catch (err) { + if (err === NXSystemMessages.formatError) { + mcdu.setScratchpadMessage(err); + } + } + } else if (/^[A-Z0-9]{2,7}/.test(value)) { + // place format + data.directTo = value; + CDUAtcLatRequestFansB.ShowPage(mcdu, data); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + + CDUAtcLatRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.heading = null; + } else if (value) { + const error = InputValidation.validateScratchpadDegree(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.heading = parseInt(value) % 360; + } + } + + CDUAtcLatRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.track = null; + } else if (value) { + const error = InputValidation.validateScratchpadDegree(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.track = parseInt(value) % 360; + } + } + + CDUAtcLatRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcLatRequestFansB.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.offset = null; + } else if (value) { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.offset = InputValidation.formatScratchpadOffset(value); + data.offsetStart = null; + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcLatRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = async (value) => { + if (value === Keypad.clrValue) { + data.weatherDeviation = null; + } else if (value) { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.weatherDeviation = InputValidation.formatScratchpadOffset(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcLatRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.backOnTrack = false; + } else { + data.backOnTrack = true; + } + CDUAtcLatRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcLatRequestFansB.CanSendData(data)) { + const messages = CDUAtcLatRequestFansB.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansA.ShowPage1(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcLatRequestFansB.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcLatRequestFansB.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcLatRequestFansB.ShowPage(mcdu); + } + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_Text.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_Text.ts new file mode 100644 index 00000000000..d33e280088e --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_Text.ts @@ -0,0 +1,136 @@ +import { CpdlcMessagesDownlink } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcFlightReq } from '../A320_Neo_CDU_ATC_FlightReq'; +import { CDUAtcMenu } from '../A320_Neo_CDU_ATC_Menu'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcTextFansB { + static CreateDataBlock() { + return { + performance: false, + weather: false, + }; + } + + static CanSendData(data) { + return data.performance || data.weather; + } + + static CreateMessages(messages, data) { + let extension = null; + if (data.performance) { + extension = CpdlcMessagesDownlink['DM66'][1].deepCopy(); + } else if (data.weather) { + extension = CpdlcMessagesDownlink['DM65'][1].deepCopy(); + } + + let updated = false; + for (const message of messages) { + if (message.Content[0].TypeId.includes('UM')) { + message.Response.Content.push(extension); + updated = true; + } else { + message.Content.push(extension); + } + } + + return updated; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, messages, data = CDUAtcTextFansB.CreateDataBlock()) { + mcdu.clearDisplay(); + + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcTextFansB.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + erase = '*ERASE'; + } + + const acPerform = ['\xa0DUE TO', '{cyan}{{end}A/C PERFORM']; + if (data.performance) { + acPerform[0] += '[color]cyan'; + acPerform[1] = '\xa0A/C PERFORM[color]cyan'; + } + const weather = ['\xa0DUE TO', '{cyan}{{end}WEATHER']; + if (data.weather) { + weather[0] += '[color]cyan'; + weather[1] = '\xa0WEATHER[color]cyan'; + } + + mcdu.setTemplate([ + ['FREE TEXT'], + [acPerform[0], weather[0]], + [acPerform[1], weather[1]], + [''], + [''], + [''], + [''], + [''], + [''], + ['ALL FIELDS'], + [erase], + ['\xa0ATC MENU', `XFR TO\xa0[color]cyan`], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.performance = false; + } else { + data.performance = true; + data.weather = false; + } + CDUAtcTextFansB.ShowPage(mcdu, messages, data); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.performance = false; + } else { + data.performance = true; + data.weather = false; + } + CDUAtcTextFansB.ShowPage(mcdu, messages, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcTextFansB.ShowPage(mcdu, messages); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcTextFansB.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + if (CDUAtcTextFansB.CreateMessages(messages, data)) { + mcdu.atsu.updateMessage(messages[0]); + } else { + mcdu.atsu.registerMessages(messages); + } + CDUAtcMenu.ShowPage(mcdu); + } + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_UsualRequest.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_UsualRequest.ts new file mode 100644 index 00000000000..237024ee47c --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_UsualRequest.ts @@ -0,0 +1,283 @@ +import { AtsuStatusCodes, CpdlcMessage, CpdlcMessagesDownlink, InputValidation } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcMenu } from '../A320_Neo_CDU_ATC_Menu'; +import { CDUAtcTextFansB } from '../FansB/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; + +export class CDUAtcUsualRequestFansB { + static CreateDataBlock() { + return { + directTo: null, + speed: null, + weatherDeviation: null, + climbTo: null, + descentTo: null, + dueToWeather: false, + }; + } + + static CanSendData(data) { + return data.directTo || data.speed || data.weatherDeviation || data.climbTo || data.descentTo; + } + + static CanEraseData(data) { + return data.directTo || data.speed || data.weatherDeviation || data.climbTo || data.descentTo || data.dueToWeather; + } + + static CreateRequest(mcdu: LegacyAtsuPageInterface, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu: LegacyAtsuPageInterface, data) { + const retval = []; + + let extension = null; + if (data.dueToWeather) { + extension = CpdlcMessagesDownlink['DM65'][1].deepCopy(); + } + + if (data.directTo) { + retval.push(CDUAtcUsualRequestFansB.CreateRequest(mcdu, 'DM22', [data.directTo])); + } + if (data.weatherDeviation) { + const elements = InputValidation.expandLateralOffset(data.weatherDeviation).split(' '); + retval.push(CDUAtcUsualRequestFansB.CreateRequest(mcdu, 'DM27', [elements[0], elements[1]])); + } + if (data.climbTo) { + retval.push(CDUAtcUsualRequestFansB.CreateRequest(mcdu, 'DM9', [data.climbTo])); + } + if (data.descentTo) { + retval.push(CDUAtcUsualRequestFansB.CreateRequest(mcdu, 'DM10', [data.descentTo])); + } + if (data.speed) { + retval.push(CDUAtcUsualRequestFansB.CreateRequest(mcdu, 'DM18', [data.speed])); + } + + if (extension) { + retval.forEach((message) => { + if (message.Content[0].TypeId !== 'DM27') { + message.Content.push(extension); + } + }); + } + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, data = CDUAtcUsualRequestFansB.CreateDataBlock()) { + mcdu.clearDisplay(); + mcdu.page.Current = mcdu.page.ATCUsualRequest; + + let addText = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcUsualRequestFansB.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + addText = 'ADD TEXT>'; + } + if (CDUAtcUsualRequestFansB.CanEraseData(data)) { + erase = '*ERASE'; + } + + let directTo = '{cyan}[ ]{end}'; + if (data.directTo) { + directTo = `${data.directTo}[color]cyan`; + } + let weatherDeviation = '{cyan}[ ]{end}'; + if (data.weatherDeviation) { + weatherDeviation = `${data.weatherDeviation}[color]cyan`; + } + let speed = '[ ][color]cyan'; + if (data.speed) { + speed = `${data.speed}[color]cyan`; + } + let climbTo = '[ ][color]cyan'; + let descentTo = '[ ][color]cyan'; + if (data.climbTo) { + climbTo = `${data.climbTo}[color]cyan`; + } + if (data.descentTo) { + descentTo = `${data.descentTo}[color]cyan`; + } + + const dueToWeather = ['\xa0DUE TO', '{cyan}{{end}WEATHER']; + if (data.dueToWeather) { + dueToWeather[0] = '{cyan}\xa0DUE TO{end}'; + dueToWeather[1] = '{cyan}\xa0WEATHER{end}'; + } + + mcdu.setTemplate([ + ['USUAL REQ'], + ['\xa0DIR TO', 'SPEED\xa0'], + [directTo, speed], + ['', 'WX DEV\xa0'], + ['', weatherDeviation], + ['\xa0CLB TO', 'DES TO\xa0'], + [climbTo, descentTo], + [dueToWeather[0]], + [dueToWeather[1]], + ['\xa0ALL FIELDS'], + [erase, addText], + ['\xa0ATC MENU', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.directTo = null; + } else if (value) { + if (WaypointEntryUtils.isLatLonFormat(value)) { + // format: DDMM.MB/EEEMM.MC + try { + WaypointEntryUtils.parseLatLon(value); + data.directTo = value; + } catch (err) { + if (err === NXSystemMessages.formatError) { + mcdu.setScratchpadMessage(err); + } + } + } else if (/^[A-Z0-9]{2,7}/.test(value)) { + // place format + data.directTo = value; + CDUAtcUsualRequestFansB.ShowPage(mcdu, data); + } else { + mcdu.setScratchpadMessage(NXSystemMessages.formatError); + } + } + + CDUAtcUsualRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.climbTo = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.climbTo = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcUsualRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = (value) => { + if (value === Keypad.clrValue) { + data.dueToWeather = false; + } else { + data.dueToWeather = true; + } + CDUAtcUsualRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcUsualRequestFansB.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.speed = null; + } else if (value) { + const error = InputValidation.validateScratchpadSpeed(value); + if (error === AtsuStatusCodes.Ok) { + data.speed = InputValidation.formatScratchpadSpeed(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcUsualRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = async (value) => { + if (value === Keypad.clrValue) { + data.weatherDeviation = null; + } else if (value) { + const error = InputValidation.validateScratchpadOffset(value); + if (error === AtsuStatusCodes.Ok) { + data.weatherDeviation = InputValidation.formatScratchpadOffset(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcUsualRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = (value) => { + if (value === Keypad.clrValue) { + data.descentTo = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.descentTo = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcUsualRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcUsualRequestFansB.CanSendData(data)) { + const requests = CDUAtcUsualRequestFansB.CreateRequests(mcdu, data); + if (requests.length !== 0) { + CDUAtcTextFansB.ShowPage(mcdu, requests); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcUsualRequestFansB.CanSendData(data)) { + const requests = CDUAtcUsualRequestFansB.CreateRequests(mcdu, data); + if (requests.length !== 0) { + mcdu.atsu.registerMessages(requests); + CDUAtcUsualRequestFansB.ShowPage(mcdu); + } + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_VertRequest.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_VertRequest.ts new file mode 100644 index 00000000000..e09a293a701 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/FansB/A320_Neo_CDU_ATC_VertRequest.ts @@ -0,0 +1,184 @@ +import { AtsuStatusCodes, CpdlcMessage, CpdlcMessagesDownlink, InputValidation } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcFlightReq } from '../A320_Neo_CDU_ATC_FlightReq'; +import { CDUAtcTextFansB } from '../FansB/A320_Neo_CDU_ATC_Text'; +import { NXSystemMessages } from '../../../messages/NXSystemMessages'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcVertRequestFansB { + static CreateDataBlock(): any { + return { + climb: null, + descend: null, + altitude: null, + }; + } + + static CanSendData(data) { + return data.climb || data.descend || data.altitude; + } + + static CreateRequest(mcdu: LegacyAtsuPageInterface, type, values = []) { + const retval = new CpdlcMessage(); + retval.Station = mcdu.atsu.currentStation(); + retval.Content.push(CpdlcMessagesDownlink[type][1].deepCopy()); + + for (let i = 0; i < values.length; ++i) { + retval.Content[0].Content[i].Value = values[i]; + } + + return retval; + } + + static CreateRequests(mcdu: LegacyAtsuPageInterface, data) { + const retval = []; + + if (data.climb) { + retval.push(CDUAtcVertRequestFansB.CreateRequest(mcdu, 'DM9', [data.climb])); + } + if (data.descend) { + retval.push(CDUAtcVertRequestFansB.CreateRequest(mcdu, 'DM10', [data.descend])); + } + if (data.altitude) { + retval.push(CDUAtcVertRequestFansB.CreateRequest(mcdu, 'DM6', [data.altitude])); + } + + return retval; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, data = CDUAtcVertRequestFansB.CreateDataBlock()) { + mcdu.clearDisplay(); + + let climbTo = '[ ][color]cyan'; + let descentTo = '[ ][color]cyan'; + if (data.climb) { + climbTo = `${data.climb}[color]cyan`; + } + if (data.descend) { + descentTo = `${data.descend}[color]cyan`; + } + let altitude = '[ ][color]cyan'; + if (data.altitude) { + altitude = `${data.altitude}[color]cyan`; + } + + let text = 'ADD TEXT\xa0'; + let erase = '\xa0ERASE'; + let reqDisplay = 'DCDU\xa0[color]cyan'; + if (CDUAtcVertRequestFansB.CanSendData(data)) { + reqDisplay = 'DCDU*[color]cyan'; + text = 'ADD TEXT>'; + erase = '*ERASE'; + } + + mcdu.setTemplate([ + ['ATC VERT REQ'], + ['\xa0CLB TO', 'DES TO\xa0'], + [climbTo, descentTo], + ['\xa0ALT'], + [altitude], + [''], + [''], + [''], + [''], + ['\xa0ALL FIELDS'], + [erase, text], + ['\xa0FLIGHT REQ', 'XFR TO\xa0[color]cyan'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.climb = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.climb = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcVertRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.altitude = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.altitude = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcVertRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUAtcVertRequestFansB.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcFlightReq.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[0] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[0] = (value) => { + if (value === Keypad.clrValue) { + data.descend = null; + } else if (value) { + const error = InputValidation.validateScratchpadAltitude(value); + if (error !== AtsuStatusCodes.Ok) { + mcdu.addNewAtsuMessage(error); + } else { + data.descend = InputValidation.formatScratchpadAltitude(value); + } + } + CDUAtcVertRequestFansB.ShowPage(mcdu, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcVertRequestFansB.CanSendData(data)) { + const messages = CDUAtcVertRequestFansB.CreateRequests(mcdu, data); + if (messages.length !== 0) { + CDUAtcTextFansB.ShowPage(mcdu, messages); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcVertRequestFansB.CanSendData(data)) { + if (mcdu.atsu.currentStation() === '') { + mcdu.setScratchpadMessage(NXSystemMessages.noAtc); + } else { + const messages = CDUAtcVertRequestFansB.CreateRequests(mcdu, data); + if (messages.length !== 0) { + mcdu.atsu.registerMessages(messages); + } + CDUAtcVertRequestFansB.ShowPage(mcdu); + } + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/MessageModify/A320_Neo_CDU_ATC_MessageModifyUM131.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/MessageModify/A320_Neo_CDU_ATC_MessageModifyUM131.ts new file mode 100644 index 00000000000..c4240425dad --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/atsu/MessageModify/A320_Neo_CDU_ATC_MessageModifyUM131.ts @@ -0,0 +1,138 @@ +import { AtsuStatusCodes, FansMode, InputValidation } from '@datalink/common'; +import { Keypad } from '../../../legacy/A320_Neo_CDU_Keypad'; +import { CDUAtcMenu } from '../A320_Neo_CDU_ATC_Menu'; +import { CDUAtcTextFansA } from '../FansA/A320_Neo_CDU_ATC_Text'; +import { CDUAtcTextFansB } from '../FansB/A320_Neo_CDU_ATC_Text'; +import { LegacyAtsuPageInterface } from '../../../legacy/LegacyAtsuPageInterface'; + +export class CDUAtcMessageModifyUM131 { + static CreateDataBlock(message) { + return { + personsOnBoard: + message.Response.Content[0].Content[1].Value !== '' ? message.Response.Content[0].Content[1].Value : null, + endurance: + message.Response.Content[0].Content[0].Value !== '' ? message.Response.Content[0].Content[0].Value : null, + }; + } + + static CanUpdateMessage(data) { + return data.personsOnBoard && data.endurance; + } + + static UpdateResponseMessage(message, data) { + message.Response.Content[0].Content[0].Value = data.endurance; + message.Response.Content[0].Content[1].Value = data.personsOnBoard; + } + + static ShowPage(mcdu: LegacyAtsuPageInterface, message, data = CDUAtcMessageModifyUM131.CreateDataBlock(message)) { + let cancel = '\xa0CANCEL'; + let addText = 'ADD TEXT\xa0'; + let transfer = 'DCDU\xa0'; + if (CDUAtcMessageModifyUM131.CanUpdateMessage(data)) { + cancel = '*CANCEL'; + addText = 'ADD TEXT>'; + transfer = 'DCDU*'; + } + + let personsOnBoard = '{cyan}[ ]{end}'; + let endurance = '{cyan}[ ]{end}'; + if (data.personsOnBoard) { + personsOnBoard = `{cyan}${data.personsOnBoard}{end}`; + } + if (data.endurance) { + endurance = `{cyan}${data.endurance}{end}`; + } + + mcdu.setTemplate([ + ['MODIFY'], + [''], + [''], + ['\xa0POB', 'ENDURANCE\xa0'], + [personsOnBoard, endurance], + [''], + [''], + [''], + [''], + ['{cyan}\xa0PAGE{end}'], + [`{cyan}${cancel}{end}`, `{white}${addText}{end}`], + ['\xa0ATC MENU', '{cyan}XFR TO\xa0{end}'], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.personsOnBoard = null; + } else { + const error = InputValidation.validateScratchpadPersonsOnBoard(value); + if (error === AtsuStatusCodes.Ok) { + data.personsOnBoard = parseInt(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcMessageModifyUM131.ShowPage(mcdu, message, data); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + if (CDUAtcMessageModifyUM131.CanUpdateMessage(data)) { + mcdu.atsu.updateMessage(message); + CDUAtcMenu.ShowPage(mcdu); + } + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUAtcMenu.ShowPage(mcdu); + }; + + mcdu.rightInputDelay[1] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = (value) => { + if (value === Keypad.clrValue) { + data.endurance = null; + } else { + const error = InputValidation.validateScratchpadEndurance(value); + if (error === AtsuStatusCodes.Ok) { + data.endurance = InputValidation.formatScratchpadEndurance(value); + } else { + mcdu.addNewAtsuMessage(error); + } + } + CDUAtcMessageModifyUM131.ShowPage(mcdu, message, data); + }; + + mcdu.rightInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[4] = () => { + if (CDUAtcMessageModifyUM131.CanUpdateMessage(data)) { + CDUAtcMessageModifyUM131.UpdateResponseMessage(message, data); + if (mcdu.atsu.fansMode() === FansMode.FansA) { + CDUAtcTextFansA.ShowPage1(mcdu, [message]); + } else { + CDUAtcTextFansB.ShowPage(mcdu, [message]); + } + } + }; + + mcdu.rightInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[5] = () => { + if (CDUAtcMessageModifyUM131.CanUpdateMessage(data)) { + CDUAtcMessageModifyUM131.UpdateResponseMessage(message, data); + mcdu.atsu.updateMessage(message); + CDUAtcMenu.ShowPage(mcdu); + } + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/A320_Neo_CDU_CFDS_Avionics_Menu.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/A320_Neo_CDU_CFDS_Avionics_Menu.ts new file mode 100644 index 00000000000..80b760bab57 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/A320_Neo_CDU_CFDS_Avionics_Menu.ts @@ -0,0 +1,74 @@ +import { LegacyCfdiuPageInterface } from '../../legacy/LegacyCfdiuPageInterface'; +import { CDUCfdsMainMenu } from './A320_Neo_CDU_CFDS_Menu'; + +export class CDUCfdsAvionicsMenu { + static ShowPage(mcdu: LegacyCfdiuPageInterface) { + mcdu.clearDisplay(); + mcdu.setTemplate([ + ['AVIONICS STATUS', '1', '2'], + [''], + ['NO GPCU DATA'], + [''], + ['ADF 1 (CLASS 3)'], + [''], + ['FMGC'], + [''], + ['VHF'], + [''], + ['AIDS'], + [''], + [' { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[5] = () => { + CDUCfdsMainMenu.ShowPage(mcdu); + }; + + // PAGE SWITCHING + mcdu.onPrevPage = () => { + CDUCfdsAvionicsMenu.ShowPage2(mcdu); + }; + mcdu.onNextPage = () => { + CDUCfdsAvionicsMenu.ShowPage2(mcdu); + }; + } + + static ShowPage2(mcdu: LegacyCfdiuPageInterface) { + mcdu.clearDisplay(); + mcdu.setTemplate([ + ['AVIONICS STATUS', '2', '2'], + [''], + ['NO ILS DATA'], + [''], + ['DMC (CLASS 3)'], + [''], + [''], + [''], + [''], + [''], + [''], + [''], + [' { + return mcdu.getDelaySwitchPage(); + }; + + mcdu.onLeftInput[5] = () => { + CDUCfdsMainMenu.ShowPage(mcdu); + }; + + // PAGE SWITCHING + mcdu.onPrevPage = () => { + CDUCfdsAvionicsMenu.ShowPage(mcdu); + }; + mcdu.onNextPage = () => { + CDUCfdsAvionicsMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/A320_Neo_CDU_CFDS_Menu.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/A320_Neo_CDU_CFDS_Menu.ts new file mode 100644 index 00000000000..3f09e54059b --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/A320_Neo_CDU_CFDS_Menu.ts @@ -0,0 +1,75 @@ +import { CDUCfdsTestMenu } from './A320_Neo_CDU_CFDS_Test_Menu'; +import { LegacyCfdiuPageInterface } from '../../legacy/LegacyCfdiuPageInterface'; +import { CDUCfdsAvionicsMenu } from './A320_Neo_CDU_CFDS_Avionics_Menu'; + +export class CDUCfdsMainMenu { + static ShowPage(mcdu: LegacyCfdiuPageInterface) { + mcdu.clearDisplay(); + mcdu.activeSystem = 'CFDS'; + mcdu.setTemplate([ + ['CFDS', '1', '2'], + [''], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[3] = () => { + CDUCfdsAvionicsMenu.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[4] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[4] = () => { + CDUCfdsTestMenu.ShowPage(mcdu); + }; + + // PAGE SWITCHING + mcdu.onPrevPage = () => { + CDUCfdsMainMenu.ShowPage2(mcdu); + }; + mcdu.onNextPage = () => { + CDUCfdsMainMenu.ShowPage2(mcdu); + }; + } + + static ShowPage2(mcdu: LegacyCfdiuPageInterface) { + mcdu.clearDisplay(); + + mcdu.setTemplate([ + ['CFDS', '2', '2'], + [''], + [' { + CDUCfdsMainMenu.ShowPage(mcdu); + }; + mcdu.onNextPage = () => { + CDUCfdsMainMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/A320_Neo_CDU_CFDS_Test_Menu.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/A320_Neo_CDU_CFDS_Test_Menu.ts new file mode 100644 index 00000000000..316d40d76fe --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/A320_Neo_CDU_CFDS_Test_Menu.ts @@ -0,0 +1,80 @@ +import { LegacyCfdiuPageInterface } from '../../legacy/LegacyCfdiuPageInterface'; +import { CDUCfdsMainMenu } from './A320_Neo_CDU_CFDS_Menu'; +import { CDUCfdsTestInst } from './instruments/A320_Neo_CDU_CFDS_Test_Inst'; + +export class CDUCfdsTestMenu { + static ShowPage(mcdu: LegacyCfdiuPageInterface) { + mcdu.clearDisplay(); + mcdu.setTemplate([ + ['SYSTEM REPORT / TEST }'], + [''], + ['[color]inop'], + [''], + ['[color]inop'], + [''], + ['[color]inop'], + [''], + [''], + [''], + ['[color]inop'], + [''], + ['[color]inop'], + ]); + + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = () => { + CDUCfdsTestInst.ShowPage(mcdu); + }; + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUCfdsMainMenu.ShowPage(mcdu); + }; + + // PAGE SWITCHING + mcdu.onPrevPage = () => { + CDUCfdsTestMenu.ShowPage2(mcdu); + }; + mcdu.onNextPage = () => { + CDUCfdsTestMenu.ShowPage2(mcdu); + }; + } + + static ShowPage2(mcdu: LegacyCfdiuPageInterface) { + mcdu.clearDisplay(); + mcdu.setTemplate([ + ['SYSTEM REPORT / TEST }'], + [''], + ['[color]inop'], + [''], + ['[color]inop'], + [''], + ['[color]inop'], + [''], + [''], + [''], + [''], + [''], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUCfdsMainMenu.ShowPage(mcdu); + }; + + // PAGE SWITCHING + mcdu.onPrevPage = () => { + CDUCfdsTestMenu.ShowPage(mcdu); + }; + mcdu.onNextPage = () => { + CDUCfdsTestMenu.ShowPage(mcdu); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/A320_Neo_CDU_CFDS_Test_Inst.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/A320_Neo_CDU_CFDS_Test_Inst.ts new file mode 100644 index 00000000000..14f78c47750 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/A320_Neo_CDU_CFDS_Test_Inst.ts @@ -0,0 +1,42 @@ +import { LegacyCfdiuPageInterface } from '../../../legacy/LegacyCfdiuPageInterface'; +import { CDU_CFDS_Test_Inst_EIS_Menu } from './eis/A320_Neo_CDU_CFDS_Test_Inst_EIS_Menu'; + +export class CDUCfdsTestInst { + static ShowPage(mcdu: LegacyCfdiuPageInterface) { + mcdu.clearDisplay(); + mcdu.setTemplate([ + ['SYSTEM REPORT / TEST'], + ['', '', 'INST'], + ['{inop}{end}'], + [''], + ['{inop}'], + [''], + ['{inop}'], + [''], + ['', 'EIS 3>'], + [''], + [''], + [''], + [' { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[1] = () => { + CDU_CFDS_Test_Inst_EIS_Menu.ShowPage(mcdu, 1); + }; + mcdu.rightInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[2] = () => { + CDU_CFDS_Test_Inst_EIS_Menu.ShowPage(mcdu, 2); + }; + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = () => { + CDU_CFDS_Test_Inst_EIS_Menu.ShowPage(mcdu, 3); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/eis/A320_Neo_CDU_CFDS_Test_Inst_EIS_Menu.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/eis/A320_Neo_CDU_CFDS_Test_Inst_EIS_Menu.ts new file mode 100644 index 00000000000..77d1c46c8fd --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/eis/A320_Neo_CDU_CFDS_Test_Inst_EIS_Menu.ts @@ -0,0 +1,41 @@ +import { LegacyCfdiuPageInterface } from '../../../../legacy/LegacyCfdiuPageInterface'; +import { CDUCfdsTestInst } from '../A320_Neo_CDU_CFDS_Test_Inst'; +import { CDU_CFDS_Test_Inst_EIS_Tests } from './A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests'; + +export class CDU_CFDS_Test_Inst_EIS_Menu { + static ShowPage(mcdu: LegacyCfdiuPageInterface, eisIndex) { + mcdu.clearDisplay(); + SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, 'Enum', 1); + const title = 'EIS ( DMC ' + eisIndex + ' )'; + mcdu.setTemplate([ + [title], + [''], + [''], + [''], + [' SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, 'Enum', 0); + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDUCfdsTestInst.ShowPage(mcdu); + }; + mcdu.rightInputDelay[3] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onRightInput[3] = () => { + CDU_CFDS_Test_Inst_EIS_Tests.ShowPage(mcdu, eisIndex); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/eis/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/eis/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests.ts new file mode 100644 index 00000000000..4033b5d7d7d --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/eis/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests.ts @@ -0,0 +1,41 @@ +import { LegacyCfdiuPageInterface } from '../../../../legacy/LegacyCfdiuPageInterface'; +import { CDU_CFDS_Test_Inst_EIS_Menu } from './A320_Neo_CDU_CFDS_Test_Inst_EIS_Menu'; +import { CDU_CFDS_Test_Inst_EIS_Tests_Display } from './A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests_Display'; + +export class CDU_CFDS_Test_Inst_EIS_Tests { + static ShowPage(mcdu: LegacyCfdiuPageInterface, eisIndex) { + mcdu.clearDisplay(); + SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, 'Enum', 1); + const title = 'EIS ( DMC ' + eisIndex + ' )'; + mcdu.setTemplate([ + [title], + ['', '', 'TEST'], + [''], + [''], + [' SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, 'Enum', 0); + + mcdu.leftInputDelay[2] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[2] = () => { + CDU_CFDS_Test_Inst_EIS_Tests_Display.ShowPage(mcdu, eisIndex); + }; + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDU_CFDS_Test_Inst_EIS_Menu.ShowPage(mcdu, eisIndex); + }; + } +} diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/eis/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests_Display.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/eis/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests_Display.ts new file mode 100644 index 00000000000..721959660dd --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/legacy_pages/cfdiu/instruments/eis/A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests_Display.ts @@ -0,0 +1,34 @@ +import { LegacyCfdiuPageInterface } from '../../../../legacy/LegacyCfdiuPageInterface'; +import { CDU_CFDS_Test_Inst_EIS_Tests } from './A320_Neo_CDU_CFDS_Test_Inst_EIS_Tests'; + +export class CDU_CFDS_Test_Inst_EIS_Tests_Display { + static ShowPage(mcdu: LegacyCfdiuPageInterface, eisIndex) { + mcdu.clearDisplay(); + SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, 'Enum', 2); + const title = 'EIS ( DMC ' + eisIndex + ' )'; + mcdu.setTemplate([ + [title], + [''], + [''], + [''], + [''], + [''], + ['', '', 'DISPLAY TEST'], + [''], + ['', '', 'IN'], + [''], + ['', '', 'PROGRESS '], + [''], + [' SimVar.SetSimVarValue(`L:A32NX_DMC_DISPLAYTEST:${eisIndex}`, 'Enum', 0); + + mcdu.leftInputDelay[5] = () => { + return mcdu.getDelaySwitchPage(); + }; + mcdu.onLeftInput[5] = () => { + CDU_CFDS_Test_Inst_EIS_Tests.ShowPage(mcdu, eisIndex); + }; + } +} diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU.css b/fbw-a32nx/src/systems/instruments/src/MCDU/mcdu.scss similarity index 91% rename from fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU.css rename to fbw-a32nx/src/systems/instruments/src/MCDU/mcdu.scss index f304adee68e..70773be6463 100644 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/Airliners/FlyByWire_A320_Neo/CDU/A320_Neo_CDU.css +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/mcdu.scss @@ -1,7 +1,17 @@ -@import url("/CSS/A32NX_Display_Common.css"); +@import "../MsfsAvionicsCommon/definitions"; :root { font-family: "HoneywellMCDU" !important; + + --mcduWhite: #ffffff; + --mcduLightGrey: #666666; + --mcduGrey: #787878; + --mcduGreen: #00ff00; + --mcduAmber: #ff9a00; + --mcduCyan: #00ffff; + --mcduYellow: #ffff00; + --mcduRed: #ff0000; + --mcduMagenta: #ff94ff; } @font-face { @@ -68,20 +78,23 @@ span:empty:before { content: " "; } -a320-neo-cdu-main-display { +#MCDU_CONTENT { position: absolute; width: 100%; height: 100%; +} + +a32nx-mcdu { font-family: "HoneywellMCDU" !important; font-size: 5.3vw !important; color: var(--displayWhite); } -a320-neo-cdu-main-display div { +a32nx-mcdu div { font-family: "HoneywellMCDU" !important; } -a320-neo-cdu-main-display text { +a32nx-mcdu text { font-family: "HoneywellMCDU" !important; } diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/messages/NXSystemMessages.ts b/fbw-a32nx/src/systems/instruments/src/MCDU/messages/NXSystemMessages.ts new file mode 100644 index 00000000000..8b9b3fa5329 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/messages/NXSystemMessages.ts @@ -0,0 +1,142 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +// FIXME move into FMGC + +export class McduMessage { + public isTypeTwo = false; + + constructor( + public text, + public isAmber = false, + public replace = '', + ) {} +} + +export class TypeIMessage extends McduMessage { + constructor(text: string, isAmber = false, replace = '') { + super(text, isAmber, replace); + } + + /** + * Only returning a "copy" of the object to ensure thread safety when trying to edit the original message + * t {string} replaces defined elements, see this.replace + */ + getModifiedMessage(t?: string | number) { + return new McduMessage(t ? this.text.replace(this.replace, '' + t) : this.text, this.isAmber, this.replace); + } +} + +export class TypeIIMessage extends McduMessage { + public isTypeTwo = true; + + constructor( + text: string, + isAmber = false, + replace = '', + public isResolved: (mcdu: any) => boolean = () => false, + public onClear = () => {}, + ) { + super(text, isAmber, replace); + } + + /** + * Only returning a "copy" of the object to ensure thread safety when trying to edit the original message + * t {string} replaces defined elements, see this.replace + * isResolved {function} overrides present function + * onClear {function} overrides present function + */ + getModifiedMessage(t?: string | number, isResolved = undefined, onClear = undefined) { + return new TypeIIMessage( + t ? this.text.replace(this.replace, '' + t) : this.text, + this.isAmber, + this.replace, + isResolved || this.isResolved, + onClear || this.onClear, + ); + } +} + +/** + NXSystemMessages only holds real messages + */ +export const NXSystemMessages = { + acPositionInvalid: new TypeIIMessage('A/C POSITION INVALID', true), + aocActFplnUplink: new TypeIIMessage('AOC ACT F-PLN UPLINK'), + arptTypeAlreadyInUse: new TypeIMessage('ARPT/TYPE ALREADY USED'), // FIXME move out of FMS + awyWptMismatch: new TypeIMessage('AWY/WPT MISMATCH'), + cancelAtisUpdate: new TypeIMessage('CANCEL UPDATE BEFORE'), // FIXME move out of FMS + checkMinDestFob: new TypeIIMessage('CHECK MIN DEST FOB'), + checkSpeedMode: new TypeIIMessage('CHECK SPEED MODE'), + checkToData: new TypeIIMessage('CHECK TAKE OFF DATA', true), + checkWeight: new TypeIIMessage('CHECK WEIGHT', true), + comUnavailable: new TypeIMessage('COM UNAVAILABLE'), // FIXME move out of FMS + cstrDelUpToWpt: new TypeIIMessage('CSTR DEL UP TO WWWWW', false, 'WWWWW'), + databaseCodingError: new TypeIIMessage('DATABASE CODING ERROR'), + dcduFileFull: new TypeIMessage('DCDU FILE FULL'), // FIXME move out of FMS + destEfobBelowMin: new TypeIIMessage('DEST EFOB BELOW MIN', true), + enterDestData: new TypeIIMessage('ENTER DEST DATA', true), + entryOutOfRange: new TypeIMessage('ENTRY OUT OF RANGE'), + invalidFplnUplink: new TypeIIMessage('INVALID F-PLN UPLINK', false), + mandatoryFields: new TypeIMessage('ENTER MANDATORY FIELDS'), // FIXME move out of FMS + formatError: new TypeIMessage('FORMAT ERROR'), + fplnElementRetained: new TypeIMessage('F-PLN ELEMENT RETAINED'), + initializeWeightOrCg: new TypeIIMessage('INITIALIZE WEIGHT/CG', true), + keyNotActive: new TypeIMessage('KEY NOT ACTIVE'), + latLonAbreviated: new TypeIMessage('LAT/LON DISPL ABREVIATED'), + listOf99InUse: new TypeIMessage('LIST OF 99 IN USE'), + newAccAlt: new TypeIIMessage('NEW ACC ALT-HHHH', false, 'HHHH'), + newAtisReceived: new TypeIMessage('NEW ATIS: READ AGAIN'), // FIXME move out of FMS + newCrzAlt: new TypeIIMessage('NEW CRZ ALT - HHHHH', false, 'HHHHH'), + newThrRedAlt: new TypeIIMessage('NEW THR RED ALT-HHHH', false, 'HHHH'), + noAtc: new TypeIMessage('NO ACTIVE ATC'), // FIXME move out of FMS + noAtisReceived: new TypeIMessage('NO ATIS REPORT RECEIVED'), // FIXME move out of FMS + noIntersectionFound: new TypeIMessage('NO INTERSECTION FOUND'), + noPreviousAtis: new TypeIMessage('NO PREVIOUS ATIS STORED'), // FIXME move out of FMS + notAllowed: new TypeIMessage('NOT ALLOWED'), + notAllowedInNav: new TypeIMessage('NOT ALLOWED IN NAV'), + notInDatabase: new TypeIMessage('NOT IN DATABASE'), + rwyLsMismatch: new TypeIIMessage('RWY/LS MISMATCH', true), + selectDesiredSystem: new TypeIMessage('SELECT DESIRED SYSTEM'), // FIXME move out of FMS (is part of MCDU itself) + setHoldSpeed: new TypeIIMessage('SET HOLD SPEED'), + spdLimExceeded: new TypeIIMessage('SPD LIM EXCEEDED', true), + systemBusy: new TypeIMessage('SYSTEM BUSY-TRY LATER'), // FIXME move out of FMS + toSpeedTooLow: new TypeIIMessage('TO SPEEDS TOO LOW', true), + uplinkInsertInProg: new TypeIIMessage('UPLINK INSERT IN PROG'), + usingCostIndex: new TypeIMessage('USING COST INDEX: NNN', false, 'NNN'), + vToDisagree: new TypeIIMessage('V1/VR/V2 DISAGREE', true), + waitForSystemResponse: new TypeIMessage('WAIT FOR SYSTEM RESPONSE'), // FIXME move out of FMS (is part of MCDU itself) + xxxIsDeselected: new TypeIMessage('XXXX IS DESELECTED', false, 'XXXX'), + stepAboveMaxFl: new TypeIIMessage('STEP ABOVE MAX FL'), + stepAhead: new TypeIIMessage('STEP AHEAD'), + stepDeleted: new TypeIIMessage('STEP DELETED'), +}; + +// FIXME move ATSU messages out of FMS +export const NXFictionalMessages = { + noNavigraphUser: new TypeIMessage('NO NAVIGRAPH USER'), + internalError: new TypeIMessage('INTERNAL ERROR'), + noAirportSpecified: new TypeIMessage('NO AIRPORT SPECIFIED'), + fltNbrInUse: new TypeIMessage('FLT NBR IN USE'), + fltNbrMissing: new TypeIMessage('ENTER ATC FLT NBR'), + notYetImplemented: new TypeIMessage('NOT YET IMPLEMENTED'), + recipientNotFound: new TypeIMessage('RECIPIENT NOT FOUND'), + authErr: new TypeIMessage('AUTH ERR'), + invalidMsg: new TypeIMessage('INVALID MSG'), + unknownDownlinkErr: new TypeIMessage('UNKNOWN DOWNLINK ERR'), + telexNotEnabled: new TypeIMessage('TELEX NOT ENABLED'), + freeTextDisabled: new TypeIMessage('FREE TEXT DISABLED'), + freetextEnabled: new TypeIMessage('FREE TEXT ENABLED'), + enabledFltNbrInUse: new TypeIMessage('ENABLED. FLT NBR IN USE'), + noOriginApt: new TypeIMessage('NO ORIGIN AIRPORT'), + noOriginSet: new TypeIMessage('NO ORIGIN SET'), + secondIndexNotFound: new TypeIMessage('2ND INDEX NOT FOUND'), + firstIndexNotFound: new TypeIMessage('1ST INDEX NOT FOUND'), + noRefWpt: new TypeIMessage('NO REF WAYPOINT'), + noWptInfos: new TypeIMessage('NO WAYPOINT INFOS'), + emptyMessage: new TypeIMessage(''), + reloadPlaneApply: new TypeIIMessage('RELOAD A/C TO APPLY', true), + noHoppieConnection: new TypeIMessage('NO HOPPIE CONNECTION'), + unknownAtsuMessage: new TypeIMessage('UNKNOWN ATSU MESSAGE'), + reverseProxy: new TypeIMessage('REVERSE PROXY ERROR'), +}; diff --git a/fbw-a32nx/src/systems/instruments/src/MCDU/tsconfig.json b/fbw-a32nx/src/systems/instruments/src/MCDU/tsconfig.json new file mode 100644 index 00000000000..70e1d2513a4 --- /dev/null +++ b/fbw-a32nx/src/systems/instruments/src/MCDU/tsconfig.json @@ -0,0 +1,31 @@ + { + "extends": "../../tsconfig.json", + + "compilerOptions": { + "moduleResolution": "node" /* Enables compatibility with MSFS SDK bare global imports */, + "jsxFactory": "FSComponent.buildComponent" /* Required for FSComponent framework JSX */, + "jsxFragmentFactory": "FSComponent.Fragment" /* Required for FSComponent framework JSX */, + "jsx": "react" /* Required for FSComponent framework JSX */, + "paths": { + "@atsu/fmsclient": ["./atsu/fmsclient/src/index.ts"], + "@datalink/aoc": ["../../../fbw-common/src/systems/datalink/aoc/src/index.ts"], + "@datalink/atc": ["../../../fbw-common/src/systems/datalink/atc/src/index.ts"], + "@datalink/common": ["../../../fbw-common/src/systems/datalink/common/src/index.ts"], + "@datalink/router": ["../../../fbw-common/src/systems/datalink/router/src/index.ts"], + "@failures": ["./failures/src/index.ts"], + "@fmgc/*": ["./fmgc/src/*"], + "@instruments/common/*": ["./instruments/src/Common/*"], + "@localization/*": ["../localization/*"], + "@sentry/*": ["./sentry-client/src/*"], + "@simbridge/*": ["./simbridge-client/src/*"], + "@shared/*": ["./shared/src/*"], + "@tcas/*": ["./tcas/src/*"], + "@typings/*": ["../../../fbw-common/src/typings/*"], + "@flybywiresim/fbw-sdk": ["../../../fbw-common/src/systems/index-no-react.ts"], + "@flybywiresim/flypad": ["../../../fbw-common/src/systems/instruments/src/EFB/index.ts"], + "@flybywiresim/clock": ["../../../fbw-common/src/systems/instruments/src/Clock/index.ts"], + "@flybywiresim/bat": ["../../../fbw-common/src/systems/instruments/src/BAT/index.ts"], + "@flybywiresim/pfd": ["../../../fbw-common/src/systems/instruments/src/PFD/index.ts"] + } + } + } diff --git a/fbw-a32nx/src/systems/shared/src/A32NX_Util.ts b/fbw-a32nx/src/systems/shared/src/A32NX_Util.ts new file mode 100644 index 00000000000..14be5d0e37b --- /dev/null +++ b/fbw-a32nx/src/systems/shared/src/A32NX_Util.ts @@ -0,0 +1,195 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + +import { UpdateThrottler } from '@flybywiresim/fbw-sdk'; +import * as math from 'mathjs'; + +export class A32NX_Util { + public static createDeltaTimeCalculator(startTime = Date.now()) { + let lastTime = startTime; + + return () => { + const nowTime = Date.now(); + const deltaTime = nowTime - lastTime; + lastTime = nowTime; + + return deltaTime; + }; + } + + public static createFrameCounter(interval = 5) { + let count = 0; + return () => { + const c = count++; + if (c == interval) { + count = 0; + } + return c; + }; + } + + public static createMachine(machineDef) { + const machine = { + value: machineDef.init, + action(event) { + const currStateDef = machineDef[machine.value]; + const destTransition = currStateDef.transitions[event]; + if (!destTransition) { + return; + } + const destState = destTransition.target; + + machine.value = destState; + }, + setState(newState) { + const valid = machineDef[newState]; + if (valid) { + machine.value = newState; + } + }, + }; + return machine; + } + + /** + * Compute a true heading from a magnetic heading + * @param {Number} heading true heading + * @param {Number=} magVar falls back to current aircraft position magvar + * @returns magnetic heading + */ + public static trueToMagnetic(heading: number, magVar?: number) { + return (720 + heading - (magVar || SimVar.GetSimVarValue('MAGVAR', 'degree'))) % 360; + } + + /** + * Compute a magnetic heading from a true heading + * @param {Number} heading magnetic heading + * @param {Number=} magVar falls back to current aircraft position magvar + * @returns true heading + */ + public static magneticToTrue(heading, magVar) { + return (720 + heading + (magVar || SimVar.GetSimVarValue('MAGVAR', 'degree'))) % 360; + } + + /** + * Takes a LatLongAlt or LatLong and returns a vector of spherical co-ordinates + * @param {(LatLong | LatLongAlt)} ll + */ + public static latLonToSpherical(ll) { + return [ + Math.cos(ll.lat * Avionics.Utils.DEG2RAD) * Math.cos(ll.long * Avionics.Utils.DEG2RAD), + Math.cos(ll.lat * Avionics.Utils.DEG2RAD) * Math.sin(ll.long * Avionics.Utils.DEG2RAD), + Math.sin(ll.lat * Avionics.Utils.DEG2RAD), + ]; + } + + /** + * Takes a vector of spherical co-ordinates and returns a LatLong + * @param {[x: number, y: number, z: number]} s + * @returns {LatLong} + */ + public static sphericalToLatLon(s) { + return new LatLong(Math.asin(s[2]) * Avionics.Utils.RAD2DEG, Math.atan2(s[1], s[0]) * Avionics.Utils.RAD2DEG); + } + + /** + * Computes the intersection point of two (true) bearings on a great circle + * @param {(LatLong | LatLongAlt)} latlon1 + * @param {number} brg1 + * @param {(LatLong | LatLongAlt)} latlon2 + * @param {number} brg2 + * @returns {LatLong} + */ + public static greatCircleIntersection(latlon1, brg1, latlon2, brg2) { + // c.f. https://blog.mbedded.ninja/mathematics/geometry/spherical-geometry/finding-the-intersection-of-two-arcs-that-lie-on-a-sphere/ + const Pa11 = A32NX_Util.latLonToSpherical(latlon1); + const latlon12 = Avionics.Utils.bearingDistanceToCoordinates(brg1 % 360, 100, latlon1.lat, latlon1.long); + const Pa12 = A32NX_Util.latLonToSpherical(latlon12); + const Pa21 = A32NX_Util.latLonToSpherical(latlon2); + const latlon22 = Avionics.Utils.bearingDistanceToCoordinates(brg2 % 360, 100, latlon2.lat, latlon2.long); + const Pa22 = A32NX_Util.latLonToSpherical(latlon22); + + const N1 = math.cross(Pa11, Pa12); + const N2 = math.cross(Pa21, Pa22); + + const L = math.cross(N1, N2); + const l = math.norm(L); + + const I1 = math.divide(L, l); + const I2 = math.multiply(I1, -1); + + const s1 = A32NX_Util.sphericalToLatLon(I1); + const s2 = A32NX_Util.sphericalToLatLon(I2); + + const brgTos1 = Avionics.Utils.computeGreatCircleHeading(latlon1, s1); + const brgTos2 = Avionics.Utils.computeGreatCircleHeading(latlon1, s2); + + const delta1 = Math.abs(brg1 - brgTos1); + const delta2 = Math.abs(brg1 - brgTos2); + + return delta1 < delta2 ? s1 : s2; + } + + public static bothGreatCircleIntersections(latlon1, brg1, latlon2, brg2) { + // c.f. https://blog.mbedded.ninja/mathematics/geometry/spherical-geometry/finding-the-intersection-of-two-arcs-that-lie-on-a-sphere/ + const Pa11 = A32NX_Util.latLonToSpherical(latlon1); + const latlon12 = Avionics.Utils.bearingDistanceToCoordinates(brg1 % 360, 100, latlon1.lat, latlon1.long); + const Pa12 = A32NX_Util.latLonToSpherical(latlon12); + const Pa21 = A32NX_Util.latLonToSpherical(latlon2); + const latlon22 = Avionics.Utils.bearingDistanceToCoordinates(brg2 % 360, 100, latlon2.lat, latlon2.long); + const Pa22 = A32NX_Util.latLonToSpherical(latlon22); + + const N1 = math.cross(Pa11, Pa12); + const N2 = math.cross(Pa21, Pa22); + + const L = math.cross(N1, N2); + const l = math.norm(L); + + const I1 = math.divide(L, l); + const I2 = math.multiply(I1, -1); + + const s1 = A32NX_Util.sphericalToLatLon(I1); + const s2 = A32NX_Util.sphericalToLatLon(I2); + + return [s1, s2]; + } + + /** + * Returns the ISA temperature for a given altitude + * @param alt {number} altitude in ft + * @returns {number} ISA temp in C° + */ + public static getIsaTemp(alt = Simplane.getAltitude()) { + return Math.min(alt, 36089) * -0.0019812 + 15; + } + + /** + * Returns the deviation from ISA temperature and OAT at given altitude + * @param alt {number} altitude in ft + * @returns {number} ISA temp deviation from OAT in C° + */ + public static getIsaTempDeviation(alt = Simplane.getAltitude(), sat = Simplane.getAmbientTemperature()) { + return sat - A32NX_Util.getIsaTemp(alt); + } + + /** + * Get the magvar to use for radials from a wp. + * @param {VhfNavaid} facility The waypoint. + */ + public static getRadialMagVar(facility) { + if (facility.subSectionCode === 0 /* VhfNavaid */) { + if (facility.stationDeclination !== undefined) { + return facility.stationDeclination; + } + } + + return Facilities.getMagVar(facility.location.lat, facility.location.long); + } + + public static meterToFeet(meterValue: number): number { + return meterValue / 0.3048; + } + + /** @deprecated Use UpdateThrottler directly! */ + public static UpdateThrottler = UpdateThrottler; +} diff --git a/fbw-a32nx/src/systems/shared/src/AutoCallOuts.ts b/fbw-a32nx/src/systems/shared/src/AutoCallOuts.ts index bc4099291f5..6e5cdd82014 100644 --- a/fbw-a32nx/src/systems/shared/src/AutoCallOuts.ts +++ b/fbw-a32nx/src/systems/shared/src/AutoCallOuts.ts @@ -1,6 +1,3 @@ -// Note there is a copy of these flags in `fbw-a32nx\src\base\flybywire-aircraft-a320-neo\html_ui\Pages\A32NX_Core\A32NX_GPWS.js` for legacy JS. -// Please keep that up to date if making any changes here. - /** Bit flags for the radio auto call outs (for CONFIG_A32NX_FWC_RADIO_AUTO_CALL_OUT_PINS). */ export enum A32NXRadioAutoCallOutFlags { TwoThousandFiveHundred = 1 << 0, diff --git a/fbw-a32nx/src/systems/shared/src/NxNotif.ts b/fbw-a32nx/src/systems/shared/src/NxNotif.ts new file mode 100644 index 00000000000..6dd4f4478c1 --- /dev/null +++ b/fbw-a32nx/src/systems/shared/src/NxNotif.ts @@ -0,0 +1,204 @@ +interface NotificationParams { + __Type: 'SNotificationParams'; + buttons: NotificationButton[]; + closePopupTitle?: string; + title: string; + description?: string; + contentUrl?: string; + style: string; + contentTemplate?: string; + contentData: string; + duration: number; +} + +/** + * NXPopUp utility class to create a pop-up UI element + */ +export class NXPopUp { + private title = 'A32NX POPUP'; + private id = `${this.title}_${Date.now()}`; + + private params: NotificationParams = { + __Type: 'SNotificationParams', + buttons: [ + new NotificationButton('TT:MENU.YES', 'A32NX_POP_' + this.id + '_YES'), + new NotificationButton('TT:MENU.NO', 'A32NX_POP_' + this.id + '_NO'), + ], + title: this.title, + style: 'small', + contentData: 'Default Message', + duration: 10000, + }; + + private popupListener?: ViewListener.ViewListener; + + constructor() {} + + _showPopUp(params: NotificationParams) { + try { + Coherent.trigger('SHOW_POP_UP', params); + } catch (e) { + console.error(e); + } + } + + /** + * Show popup with given or already initiated parameters + * @param title Title for popup - will show in menu bar + * @param message Popup message + * @param style Style/Type of popup. Valid types are small|normal|big|big-help + * @param callbackYes Callback function -> YES button is clicked. + * @param callbackNo Callback function -> NO button is clicked. + */ + showPopUp(title: string, message: string, style: string, callbackYes: () => void, callbackNo: () => void) { + if (title) { + this.params.title = title; + } + if (message) { + this.params.contentData = message; + } + if (style) { + this.params.style = style; + } + if (callbackYes) { + const yes = typeof callbackYes === 'function' ? callbackYes : () => callbackYes; + Coherent.on(`A32NX_POP_${this.id}_YES`, () => { + Coherent.off(`A32NX_POP_${this.id}_YES`, null, null); + yes(); + }); + } + if (callbackNo) { + const no = typeof callbackNo === 'function' ? callbackNo : () => callbackNo; + Coherent.on(`A32NX_POP_${this.id}_NO`, () => { + Coherent.off(`A32NX_POP_${this.id}_NO`, null, null); + no(); + }); + } + + if (!this.popupListener) { + this.popupListener = RegisterViewListener('JS_LISTENER_POPUP', this._showPopUp.bind(this, this.params), true); + } else { + this._showPopUp(this.params); + } + } +} + +/** + * NXNotif utility class to create a notification event and element + */ + +export class NXNotifManager { + private notifications = []; + constructor() { + Coherent.on('keyIntercepted', (key) => this.registerIntercepts(key)); + Coherent.call('INTERCEPT_KEY_EVENT', 'PAUSE_TOGGLE', 0); + Coherent.call('INTERCEPT_KEY_EVENT', 'PAUSE_ON', 0); + Coherent.call('INTERCEPT_KEY_EVENT', 'PAUSE_OFF', 0); + Coherent.call('INTERCEPT_KEY_EVENT', 'PAUSE_SET', 0); + } + + registerIntercepts(key) { + switch (key) { + case 'PAUSE_TOGGLE': + case 'PAUSE_ON': + case 'PAUSE_OFF': + case 'PAUSE_SET': + this.notifications.forEach((notif) => { + notif.hideNotification(); + }); + this.notifications.length = 0; + break; + default: + break; + } + } + + showNotification(params = {}) { + const notif = new NXNotif(); + notif.showNotification(params); + this.notifications.push(notif); + } +} + +interface NotificationData { + __Type: 'NotificationData'; + type: string; + id: number; + title: string; + description: string; + theme: string; + image: string; + style?: string; + buttons?: NotificationButton[]; + hasGauge?: boolean; + priority?: number; + params?: NotificationParams; + sound?: string; +} + +class NXNotif { + private title = 'A32NX ALERT'; + private timeout = 10000; + + private params: NotificationData = { + __Type: 'NotificationData', + id: 0, + title: this.title, + type: 'MESSAGE', + theme: 'GAMEPLAY', + image: 'IMAGE_NOTIFICATION', + description: 'Default Message', + }; + + private nxNotificationsListener?: ViewListener.ViewListener; + + constructor() {} + + setData(params: Partial = {}) { + if (params.title) { + this.params.title = params.title; + } + if (params.type) { + this.params.type = params.type; + } + if (params.theme) { + this.params.theme = params.theme; + } + if (params.image) { + this.params.image = params.image; + } + if (params.description) { + this.params.description = params.description; + } + } + + setTimeout(timeout: number) { + this.timeout = timeout; + } + + /** + * Show notification with given or already initiated parametrs. + * @param {string} params.title Title for notification - will show as the message header + * @param {string} params.type Type of Notification - Valid types are MESSAGE|SUBTITLES + * @param {string} params.theme Theme of Notification. Valid types are TIPS|GAMEPLAY|SYSTEM + * @param {string} params.image Notification image. Valid types are IMAGE_NOTIFICATION|IMAGE_SCORE + * @param {string} params.message Notification message + * @param {number} params.timeout Time in ms before notification message will disappear + */ + showNotification(params: Partial = {}) { + this.setData(params); + + if (!this.nxNotificationsListener) { + this.nxNotificationsListener = RegisterViewListener('JS_LISTENER_NOTIFICATIONS', () => {}, true); + } + this.nxNotificationsListener.triggerToAllSubscribers('SendNewNotification', this.params); + setTimeout(() => { + this.hideNotification(); + }, this.timeout); + } + + // TODO FIXME: May break in the future, check every update + hideNotification() { + this.nxNotificationsListener.triggerToAllSubscribers('HideNotification', this.params.type, null, this.params.id); + } +} diff --git a/fbw-a32nx/src/systems/simbridge-client/src/components/Coroute.ts b/fbw-a32nx/src/systems/simbridge-client/src/components/Coroute.ts index bfd8e29c895..e442f007794 100644 --- a/fbw-a32nx/src/systems/simbridge-client/src/components/Coroute.ts +++ b/fbw-a32nx/src/systems/simbridge-client/src/components/Coroute.ts @@ -5,9 +5,9 @@ import { fetchWithTimeout, getSimBridgeUrl } from '../common'; import { CoRouteDto } from '../Coroute/coroute'; import { ClientState } from './ClientState'; -type coRouteCall = { +type coRouteCall = { success: boolean; - data: CoRouteDto | CoRouteDto[]; + data: T; }; /** @@ -19,7 +19,7 @@ export class CompanyRoute { * @param route The routename in question * @returns Returns the CoRoute DTO */ - public static async getCoRoute(route: String): Promise { + public static async getCoRoute(route: String): Promise> { if (!ClientState.getInstance().isConnected()) { throw new Error('SimBridge is not connected.'); } @@ -46,7 +46,7 @@ export class CompanyRoute { * @param dest the destination * @returns Returns a list of CoRoute DTOs */ - public static async getRouteList(origin: String, dest: String): Promise { + public static async getRouteList(origin: String, dest: String): Promise> { if (!ClientState.getInstance().isConnected()) { throw new Error('SimBridge is not connected.'); } diff --git a/fbw-a32nx/src/systems/tsconfig.json b/fbw-a32nx/src/systems/tsconfig.json index c447ddcdeda..78f9b18adc4 100644 --- a/fbw-a32nx/src/systems/tsconfig.json +++ b/fbw-a32nx/src/systems/tsconfig.json @@ -12,6 +12,7 @@ "allowSyntheticDefaultImports": true, "jsx": "react", "paths": { + "@atsu/fmsclient": ["./atsu/fmsclient/src/index.ts"], "@datalink/aoc": ["../../../fbw-common/src/systems/datalink/aoc/src/index.ts"], "@datalink/atc": ["../../../fbw-common/src/systems/datalink/atc/src/index.ts"], "@datalink/common": ["../../../fbw-common/src/systems/datalink/common/src/index.ts"], diff --git a/fbw-a380x/src/systems/instruments/src/Common/flightplan.tsx b/fbw-a380x/src/systems/instruments/src/Common/flightplan.tsx index 0f473ea922d..39e7a41d76b 100644 --- a/fbw-a380x/src/systems/instruments/src/Common/flightplan.tsx +++ b/fbw-a380x/src/systems/instruments/src/Common/flightplan.tsx @@ -1,15 +1,15 @@ import React, { useContext, useEffect, useState } from 'react'; -import { ExternalBackend, Database } from 'msfs-navdata'; import { useUpdate } from '@instruments/common/hooks'; import { FlightPlan } from '@fmgc/flightplanning/plans/FlightPlan'; import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; import { NavigationDatabaseService } from '@fmgc/flightplanning/NavigationDatabaseService'; import { NavigationDatabase } from '@fmgc/NavigationDatabase'; +import { Database, MsfsBackend } from '@flybywiresim/fbw-sdk'; const FlightPlanContext = React.createContext<{ database: Database }>(undefined as any); export const FlightPlanProvider: React.FC = ({ children }) => { - const [database] = useState(() => new Database(new ExternalBackend('http://localhost:5000'))); + const [database] = useState(() => new Database(new MsfsBackend())); return {children}; }; diff --git a/fbw-a380x/src/systems/instruments/src/MFD/FMC/FlightManagementComputer.ts b/fbw-a380x/src/systems/instruments/src/MFD/FMC/FlightManagementComputer.ts index eed7279bc6d..1201321f929 100644 --- a/fbw-a380x/src/systems/instruments/src/MFD/FMC/FlightManagementComputer.ts +++ b/fbw-a380x/src/systems/instruments/src/MFD/FMC/FlightManagementComputer.ts @@ -3,18 +3,6 @@ import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; import { GuidanceController } from '@fmgc/guidance/GuidanceController'; -import { - A320FlightPlanPerformanceData, - DataManager, - EfisInterface, - EfisSymbols, - FlightPhaseManager, - FlightPlanIndex, - Navigation, - NavigationDatabase, - NavigationDatabaseBackend, - NavigationDatabaseService, -} from '@fmgc/index'; import { A380AircraftConfig } from '@fmgc/flightplanning/A380AircraftConfig'; import { ArraySubject, @@ -49,7 +37,7 @@ import { TypeIIMessage, TypeIMessage, } from 'instruments/src/MFD/shared/NXSystemMessages'; -import { PilotWaypoint } from '@fmgc/flightplanning/DataManager'; +import { DataManager, PilotWaypoint } from '@fmgc/flightplanning/DataManager'; import { distanceTo, Coordinates } from 'msfs-geo'; import { FmsDisplayInterface } from '@fmgc/flightplanning/interface/FmsDisplayInterface'; import { MfdDisplayInterface } from 'instruments/src/MFD/MFD'; @@ -57,9 +45,16 @@ import { FmcIndex } from 'instruments/src/MFD/FMC/FmcServiceInterface'; import { FmsErrorType } from '@fmgc/FmsError'; import { A380Failure } from '@failures'; import { FpmConfigs } from '@fmgc/flightplanning/FpmConfig'; -import { FlightPhaseManagerEvents } from '@fmgc/flightphase'; +import { FlightPhaseManager, FlightPhaseManagerEvents } from '@fmgc/flightphase'; import { MfdUIData } from 'instruments/src/MFD/shared/MfdUIData'; import { ActiveUriInformation } from 'instruments/src/MFD/pages/common/MfdUiService'; +import { A320FlightPlanPerformanceData } from '@fmgc/flightplanning/plans/performance/FlightPlanPerformanceData'; +import { EfisInterface } from '@fmgc/efis/EfisInterface'; +import { Navigation } from '@fmgc/navigation/Navigation'; +import { EfisSymbols } from '@fmgc/efis/EfisSymbols'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; +import { NavigationDatabase, NavigationDatabaseBackend } from '@fmgc/NavigationDatabase'; +import { NavigationDatabaseService } from '@fmgc/flightplanning/NavigationDatabaseService'; export interface FmsErrorMessage { message: McduMessage; @@ -101,6 +96,7 @@ export class FlightManagementComputer implements FmcInterface { this.#operatingMode = value; } + // FIXME A320 data #flightPlanService = new FlightPlanService(this.bus, new A320FlightPlanPerformanceData(), FpmConfigs.A380); get flightPlanService() { @@ -660,8 +656,8 @@ export class FlightManagementComputer implements FmcInterface { /** * This method is called by the FlightPhaseManager after a flight phase change * This method initializes AP States, initiates CDUPerformancePage changes and other set other required states - * @param prevPhase {FmgcFlightPhases} Previous FmgcFlightPhase - * @param nextPhase {FmgcFlightPhases} New FmgcFlightPhase + * @param prevPhase Previous FmgcFlightPhase + * @param nextPhase New FmgcFlightPhase */ onFlightPhaseChanged(prevPhase: FmgcFlightPhase, nextPhase: FmgcFlightPhase) { this.acInterface.updateConstraints(); diff --git a/fbw-a380x/src/systems/instruments/src/MFD/FMC/FmcAircraftInterface.ts b/fbw-a380x/src/systems/instruments/src/MFD/FMC/FmcAircraftInterface.ts index f9284a4bfc7..4cef756bd6c 100644 --- a/fbw-a380x/src/systems/instruments/src/MFD/FMC/FmcAircraftInterface.ts +++ b/fbw-a380x/src/systems/instruments/src/MFD/FMC/FmcAircraftInterface.ts @@ -4,7 +4,6 @@ import { ConsumerValue, EventBus, GameStateProvider, SimVarValueType, Subject, UnitType } from '@microsoft/msfs-sdk'; import { Arinc429SignStatusMatrix, Arinc429Word, FmsOansData, MathUtils, NXDataStore } from '@flybywiresim/fbw-sdk'; import { FlapConf } from '@fmgc/guidance/vnav/common'; -import { FlightPlanService } from '@fmgc/index'; import { MmrRadioTuningStatus } from '@fmgc/navigation/NavaidTuner'; import { Vmcl, Vmo, maxCertifiedAlt, maxZfw } from '@shared/PerformanceConstants'; import { FmgcFlightPhase } from '@shared/flightphase'; @@ -16,6 +15,7 @@ import { FmcInterface } from 'instruments/src/MFD/FMC/FmcInterface'; import { FlightPhaseManagerEvents } from '@fmgc/flightphase'; import { FGVars } from 'instruments/src/MsfsAvionicsCommon/providers/FGDataPublisher'; import { VerticalMode } from '@shared/autopilot'; +import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; import { FmsMfdVars } from 'instruments/src/MsfsAvionicsCommon/providers/FmsMfdPublisher'; import { MfdFmsFplnVertRev } from 'instruments/src/MFD/pages/FMS/F-PLN/MfdFmsFplnVertRev'; diff --git a/fbw-a380x/src/systems/instruments/src/MFD/FMC/FmcInterface.ts b/fbw-a380x/src/systems/instruments/src/MFD/FMC/FmcInterface.ts index 19502eaad89..bdce464e263 100644 --- a/fbw-a380x/src/systems/instruments/src/MFD/FMC/FmcInterface.ts +++ b/fbw-a380x/src/systems/instruments/src/MFD/FMC/FmcInterface.ts @@ -1,7 +1,6 @@ import { FmsErrorType } from '@fmgc/FmsError'; import { FmsDataInterface } from '@fmgc/flightplanning/interface/FmsDataInterface'; import { FmsDisplayInterface } from '@fmgc/flightplanning/interface/FmsDisplayInterface'; -import { DataManager, FlightPlanIndex, FlightPlanService, GuidanceController } from '@fmgc/index'; import { NavaidTuner } from '@fmgc/navigation/NavaidTuner'; import { NavigationProvider } from '@fmgc/navigation/NavigationProvider'; import { ArraySubject, Subject } from '@microsoft/msfs-sdk'; @@ -11,6 +10,10 @@ import { MfdDisplayInterface } from 'instruments/src/MFD/MFD'; import { FmgcDataService } from 'instruments/src/MFD/FMC/fmgc'; import { TypeIMessage, TypeIIMessage } from 'instruments/src/MFD/shared/NXSystemMessages'; import { EfisSide, Fix, Waypoint } from '@flybywiresim/fbw-sdk'; +import { FlightPlanService } from '@fmgc/flightplanning/FlightPlanService'; +import { GuidanceController } from '@fmgc/guidance/GuidanceController'; +import { DataManager } from '@fmgc/flightplanning/DataManager'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; export enum FmcOperatingModes { Master, diff --git a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/DATA/MfdFmsDataStatus.tsx b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/DATA/MfdFmsDataStatus.tsx index e56ce7c417c..0b1783e4abd 100644 --- a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/DATA/MfdFmsDataStatus.tsx +++ b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/DATA/MfdFmsDataStatus.tsx @@ -10,9 +10,9 @@ import { MfdSimvars } from 'instruments/src/MFD/shared/MFDSimvarPublisher'; import { TopTabNavigator, TopTabNavigatorPage } from 'instruments/src/MsfsAvionicsCommon/UiWidgets/TopTabNavigator'; import { Button } from 'instruments/src/MsfsAvionicsCommon/UiWidgets/Button'; import { AirlineModifiableInformation } from '@shared/AirlineModifiableInformation'; -import { NavigationDatabaseService } from '@fmgc/index'; import { DatabaseIdent } from '@flybywiresim/fbw-sdk'; import { ConfirmationDialog } from '../../../../MsfsAvionicsCommon/UiWidgets/ConfirmationDialog'; +import { NavigationDatabaseService } from '@fmgc/flightplanning/NavigationDatabaseService'; import './MfdFmsDataStatus.scss'; diff --git a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/FplnRevisionsMenu.tsx b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/FplnRevisionsMenu.tsx index b97b7316807..f6c32953985 100644 --- a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/FplnRevisionsMenu.tsx +++ b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/FplnRevisionsMenu.tsx @@ -1,7 +1,7 @@ import { TurnDirection, WaypointDescriptor } from '@flybywiresim/fbw-sdk'; import { HoldType } from '@fmgc/flightplanning/data/flightplan'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; import { SegmentClass } from '@fmgc/flightplanning/segments/SegmentClass'; -import { FlightPlanIndex } from '@fmgc/index'; import { MfdFmsFpln } from 'instruments/src/MFD/pages/FMS/F-PLN/MfdFmsFpln'; import { ContextMenuElement } from 'instruments/src/MsfsAvionicsCommon/UiWidgets/ContextMenu'; import { BitFlags } from '@microsoft/msfs-sdk'; diff --git a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/InsertNextWptFrom.tsx b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/InsertNextWptFrom.tsx index b0e9b6e74e5..c73d85e7cbe 100644 --- a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/InsertNextWptFrom.tsx +++ b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/InsertNextWptFrom.tsx @@ -14,10 +14,10 @@ import { coordinateToString } from '@flybywiresim/fbw-sdk'; import { DropdownMenu } from 'instruments/src/MsfsAvionicsCommon/UiWidgets/DropdownMenu'; import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; import { FmcServiceInterface } from 'instruments/src/MFD/FMC/FmcServiceInterface'; -import { FlightPlanIndex } from '@fmgc/index'; import { FmsDisplayInterface } from '@fmgc/flightplanning/interface/FmsDisplayInterface'; import { MfdDisplayInterface } from 'instruments/src/MFD/MFD'; import { FmsError } from '@fmgc/FmsError'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; export type NextWptInfo = { ident: string; diff --git a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/MfdFmsFplnArr.tsx b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/MfdFmsFplnArr.tsx index ffc834fab7d..92b59efef0a 100644 --- a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/MfdFmsFplnArr.tsx +++ b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/MfdFmsFplnArr.tsx @@ -3,8 +3,9 @@ import { AbstractMfdPageProps } from 'instruments/src/MFD/MFD'; import { Footer } from 'instruments/src/MFD/pages/common/Footer'; import { Button, ButtonMenuItem } from 'instruments/src/MsfsAvionicsCommon/UiWidgets/Button'; import { FmsPage } from 'instruments/src/MFD/pages/common/FmsPage'; -import { ApproachType, LandingSystemUtils } from '@fmgc/index'; import { getApproachName } from '../../../shared/utils'; +import { ApproachType } from '@flybywiresim/fbw-sdk'; +import { LandingSystemUtils } from '@fmgc/flightplanning/data/landingsystem'; import './MfdFmsFpln.scss'; diff --git a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/MfdFmsFplnDirectTo.tsx b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/MfdFmsFplnDirectTo.tsx index b68516f552d..92a14bb6eda 100644 --- a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/MfdFmsFplnDirectTo.tsx +++ b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/F-PLN/MfdFmsFplnDirectTo.tsx @@ -7,9 +7,10 @@ import { Button } from 'instruments/src/MsfsAvionicsCommon/UiWidgets/Button'; import { FmsPage } from 'instruments/src/MFD/pages/common/FmsPage'; import { DropdownMenu } from 'instruments/src/MsfsAvionicsCommon/UiWidgets/DropdownMenu'; import { FlightPlanLeg } from '@fmgc/flightplanning/legs/FlightPlanLeg'; -import { FlightPlanIndex, WaypointEntryUtils } from '@fmgc/index'; import { RadioButtonColor, RadioButtonGroup } from 'instruments/src/MsfsAvionicsCommon/UiWidgets/RadioButtonGroup'; import { ADIRS } from 'instruments/src/MFD/shared/Adirs'; +import { FlightPlanIndex } from '@fmgc/flightplanning/FlightPlanManager'; +import { WaypointEntryUtils } from '@fmgc/flightplanning/WaypointEntryUtils'; interface MfdFmsFplnDirectToProps extends AbstractMfdPageProps {} diff --git a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/POSITION/MfdFmsPositionNavaids.tsx b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/POSITION/MfdFmsPositionNavaids.tsx index f6fb63f4e97..0c3f3f62c42 100644 --- a/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/POSITION/MfdFmsPositionNavaids.tsx +++ b/fbw-a380x/src/systems/instruments/src/MFD/pages/FMS/POSITION/MfdFmsPositionNavaids.tsx @@ -15,11 +15,12 @@ import { MfdSimvars } from 'instruments/src/MFD/shared/MFDSimvarPublisher'; import { NXSystemMessages } from 'instruments/src/MFD/shared/NXSystemMessages'; import { coordinateToString, NavaidSubsectionCode } from '@flybywiresim/fbw-sdk'; -import { NavigationDatabaseService, SelectedNavaidType } from '@fmgc/index'; import { NavRadioTuningStatus } from '@fmgc/navigation/NavaidTuner'; import { ClockEvents, FSComponent, SimVarValueType, Subject, VNode } from '@microsoft/msfs-sdk'; import './MfdFmsPositionNavaids.scss'; +import { SelectedNavaidType } from '@fmgc/navigation/Navigation'; +import { NavigationDatabaseService } from '@fmgc/flightplanning/NavigationDatabaseService'; interface MfdFmsPositionNavaidsProps extends AbstractMfdPageProps {} diff --git a/fbw-a380x/src/systems/instruments/src/ND/OansControlPanel.tsx b/fbw-a380x/src/systems/instruments/src/ND/OansControlPanel.tsx index bf8db36a391..2770a9da5e9 100644 --- a/fbw-a380x/src/systems/instruments/src/ND/OansControlPanel.tsx +++ b/fbw-a380x/src/systems/instruments/src/ND/OansControlPanel.tsx @@ -54,10 +54,11 @@ import { IconButton } from 'instruments/src/MsfsAvionicsCommon/UiWidgets/IconBut import { TopTabNavigator, TopTabNavigatorPage } from 'instruments/src/MsfsAvionicsCommon/UiWidgets/TopTabNavigator'; import { Coordinates, distanceTo, placeBearingDistance } from 'msfs-geo'; import { AdirsSimVars } from 'instruments/src/MsfsAvionicsCommon/SimVarTypes'; -import { NavigationDatabase, NavigationDatabaseBackend, NavigationDatabaseService } from '@fmgc/index'; import { InteractionMode, InternalKccuKeyEvent } from 'instruments/src/MFD/shared/MFDSimvarPublisher'; import { NDSimvars } from 'instruments/src/ND/NDSimvarPublisher'; import { Position } from '@turf/turf'; +import { NavigationDatabaseService } from '@fmgc/flightplanning/NavigationDatabaseService'; +import { NavigationDatabase, NavigationDatabaseBackend } from '@fmgc/NavigationDatabase'; export interface OansProps extends ComponentProps { bus: EventBus; diff --git a/fbw-a380x/src/systems/systems-host/systems/BrakeToVacateDistanceUpdater.ts b/fbw-a380x/src/systems/systems-host/systems/BrakeToVacateDistanceUpdater.ts index 8feea1c24d1..69d519c8d62 100644 --- a/fbw-a380x/src/systems/systems-host/systems/BrakeToVacateDistanceUpdater.ts +++ b/fbw-a380x/src/systems/systems-host/systems/BrakeToVacateDistanceUpdater.ts @@ -10,9 +10,10 @@ import { pointDistance, } from '@flybywiresim/oanc'; import { Arinc429Register, Arinc429SignStatusMatrix, MathUtils } from '@flybywiresim/fbw-sdk'; -import { NavigationDatabase, NavigationDatabaseBackend, NavigationDatabaseService } from '@fmgc/index'; import { placeBearingDistance } from 'msfs-geo'; import { Position } from '@turf/turf'; +import { NavigationDatabaseService } from '@fmgc/flightplanning/NavigationDatabaseService'; +import { NavigationDatabase, NavigationDatabaseBackend } from '@fmgc/NavigationDatabase'; /** * Utility class for brake to vacate (BTV) functions on the A380 diff --git a/fbw-common/src/systems/datalink/atc/src/components/UplinkMessageStateMachine.ts b/fbw-common/src/systems/datalink/atc/src/components/UplinkMessageStateMachine.ts index 11e7eb7c8c8..c12295116d8 100644 --- a/fbw-common/src/systems/datalink/atc/src/components/UplinkMessageStateMachine.ts +++ b/fbw-common/src/systems/datalink/atc/src/components/UplinkMessageStateMachine.ts @@ -27,7 +27,7 @@ export class UplinkMessageStateMachine { } } - public static update(atc: Atc, message: CpdlcMessage, uiEvent: boolean, positive: boolean): void { + public static update(atc: Atc, message: CpdlcMessage, uiEvent: boolean, positive = false): void { if (positive) { if (message.MessageMonitoring === CpdlcMessageMonitoringState.Required) { message.MessageMonitoring = CpdlcMessageMonitoringState.Monitoring; diff --git a/fbw-common/src/systems/datalink/common/src/messages/AtsuMessage.ts b/fbw-common/src/systems/datalink/common/src/messages/AtsuMessage.ts index 7864336dbbf..9d484b894a6 100644 --- a/fbw-common/src/systems/datalink/common/src/messages/AtsuMessage.ts +++ b/fbw-common/src/systems/datalink/common/src/messages/AtsuMessage.ts @@ -34,7 +34,9 @@ export enum AtsuMessageComStatus { } export enum AtsuMessageSerializationFormat { + // FIXME CDU? FmsDisplay, + // FIXME CDU? FmsDisplayMonitored, Mailbox, Printer, diff --git a/fbw-common/src/systems/instruments/src/ND/shared/map/FixInfoLayer.ts b/fbw-common/src/systems/instruments/src/ND/shared/map/FixInfoLayer.ts index 645dcdc5d6a..035f1491dbd 100644 --- a/fbw-common/src/systems/instruments/src/ND/shared/map/FixInfoLayer.ts +++ b/fbw-common/src/systems/instruments/src/ND/shared/map/FixInfoLayer.ts @@ -30,15 +30,19 @@ export class FixInfoLayer implements MapLayer { context.setLineDash(FIX_INFO_DASHES); context.beginPath(); - for (const radial of radials) { - if (Number.isFinite(radial)) { - this.drawFixInfoRadial(context, rx, ry, Math.round(radial), mapParameters, '#000', 3.25); + if (radials) { + for (const radial of radials) { + if (Number.isFinite(radial)) { + this.drawFixInfoRadial(context, rx, ry, Math.round(radial), mapParameters, '#000', 3.25); + } } } - for (const radius of radii) { - if (Number.isFinite(radius)) { - this.drawFixInfoRadius(context, rx, ry, radius * mapParameters.nmToPx, '#000', 3.25); + if (radii) { + for (const radius of radii) { + if (Number.isFinite(radius)) { + this.drawFixInfoRadius(context, rx, ry, radius * mapParameters.nmToPx, '#000', 3.25); + } } } @@ -64,15 +68,19 @@ export class FixInfoLayer implements MapLayer { context.setLineDash(FIX_INFO_DASHES); context.beginPath(); - for (const radial of radials) { - if (Number.isFinite(radial)) { - this.drawFixInfoRadial(context, rx, ry, Math.round(radial), mapParameters, '#0ff', 1.75); + if (radials) { + for (const radial of radials) { + if (Number.isFinite(radial)) { + this.drawFixInfoRadial(context, rx, ry, Math.round(radial), mapParameters, '#0ff', 1.75); + } } } - for (const radius of radii) { - if (Number.isFinite(radius)) { - this.drawFixInfoRadius(context, rx, ry, radius * mapParameters.nmToPx, '#0ff', 1.75); + if (radii) { + for (const radius of radii) { + if (Number.isFinite(radius)) { + this.drawFixInfoRadius(context, rx, ry, radius * mapParameters.nmToPx, '#0ff', 1.75); + } } } diff --git a/fbw-common/src/systems/instruments/src/OANC/OansBrakeToVacateSelection.ts b/fbw-common/src/systems/instruments/src/OANC/OansBrakeToVacateSelection.ts index c5ffdacca57..ace45292c08 100644 --- a/fbw-common/src/systems/instruments/src/OANC/OansBrakeToVacateSelection.ts +++ b/fbw-common/src/systems/instruments/src/OANC/OansBrakeToVacateSelection.ts @@ -28,7 +28,7 @@ import { pointToLineDistance, } from './OancMapUtils'; import { Coordinates, placeBearingDistance } from 'msfs-geo'; -import { NavigationDatabaseService } from '@fmgc/index'; +import { NavigationDatabaseService } from '@fmgc/flightplanning/NavigationDatabaseService'; export const MIN_TOUCHDOWN_ZONE_DISTANCE = 400; // Minimum distance from threshold to touch down zone const CLAMP_DRY_STOPBAR_DISTANCE = 100; // If stop bar is <> meters behind end of runway, clamp to this distance behind end of runway diff --git a/fbw-common/src/systems/navdata/client/Database.ts b/fbw-common/src/systems/navdata/client/Database.ts index 5e78f79db71..f2f5396370b 100644 --- a/fbw-common/src/systems/navdata/client/Database.ts +++ b/fbw-common/src/systems/navdata/client/Database.ts @@ -19,6 +19,9 @@ import { DatabaseIdent, DataInterface, RestrictiveAirspace, + Fix, + SectionCode, + AirportSubsectionCode, } from '../shared'; import { AirportCommunication } from '../shared/types/Communication'; import { ControlledAirspace } from '../shared/types/Airspace'; @@ -129,7 +132,10 @@ export class Database { return this.backend.getAirways(idents); } - public async getAirwaysByFix(fix: Waypoint | NdbNavaid | VhfNavaid, airwayIdent?: string): Promise { + public async getAirwaysByFix(fix: Fix, airwayIdent?: string): Promise { + if (fix.sectionCode === SectionCode.Airport && fix.subSectionCode === AirportSubsectionCode.ReferencePoints) { + return []; + } return this.backend.getAirwaysByFix(fix.ident, fix.icaoCode, airwayIdent); } diff --git a/fbw-common/src/systems/navdata/client/backends/Msfs/Mapping.ts b/fbw-common/src/systems/navdata/client/backends/Msfs/Mapping.ts index 3d637e45603..e58216dd896 100644 --- a/fbw-common/src/systems/navdata/client/backends/Msfs/Mapping.ts +++ b/fbw-common/src/systems/navdata/client/backends/Msfs/Mapping.ts @@ -1256,7 +1256,6 @@ export class MsfsMapping { public mapFacilityToWaypoint(facility: T): FacilityType { const airportIdent = facility.icao.substring(3, 7).trim(); - // TODO this is a hack const isTerminalVsEnroute = airportIdent.length > 0; const databaseItem = { @@ -1276,11 +1275,12 @@ export class MsfsMapping { const ndb = facility as any as JS_FacilityNDB; return { ...databaseItem, - sectionCode: SectionCode.Navaid, - subSectionCode: NavaidSubsectionCode.NdbNavaid, + sectionCode: isTerminalVsEnroute ? SectionCode.Airport : SectionCode.Navaid, + subSectionCode: isTerminalVsEnroute ? AirportSubsectionCode.TerminalNdb : NavaidSubsectionCode.NdbNavaid, frequency: ndb.freqMHz, // actually kHz class: this.mapNdbType(ndb.type), bfoOperation: false, // TODO can we? + airportIdent: isTerminalVsEnroute ? airportIdent : undefined, } as unknown as FacilityType; } case 'V': { diff --git a/fbw-common/src/systems/navdata/client/backends/Msfs/Msfs.ts b/fbw-common/src/systems/navdata/client/backends/Msfs/Msfs.ts index b62443255e7..2163ea36265 100644 --- a/fbw-common/src/systems/navdata/client/backends/Msfs/Msfs.ts +++ b/fbw-common/src/systems/navdata/client/backends/Msfs/Msfs.ts @@ -4,7 +4,7 @@ /* eslint-disable no-await-in-loop */ -import { Coordinates, NauticalMiles } from 'msfs-geo'; +import { Coordinates, distanceTo, NauticalMiles } from 'msfs-geo'; // FIXME remove msfs-sdk dependency import { FacilitySearchType, @@ -434,6 +434,8 @@ export class MsfsBackend implements DataInterface { if (nearbyFacilities.removed.includes(dbItem.databaseId)) { this.facilitySearchTypeToCachedSearchResultsMap[type].splice(i, 1); i--; + } else { + dbItem.distance = distanceTo(center, dbItem.location); } } diff --git a/fbw-common/src/systems/navdata/shared/types/NdbNavaid.ts b/fbw-common/src/systems/navdata/shared/types/NdbNavaid.ts index a5d78f35371..28a23866147 100644 --- a/fbw-common/src/systems/navdata/shared/types/NdbNavaid.ts +++ b/fbw-common/src/systems/navdata/shared/types/NdbNavaid.ts @@ -1,10 +1,16 @@ import { NauticalMiles } from 'msfs-geo'; -import { BaseFix } from '..'; -import { KiloHertz } from './Common'; +import { BaseFix, WaypointArea } from '..'; +import { DatabaseItem, KiloHertz } from './Common'; import { AirportSubsectionCode, NavaidSubsectionCode, SectionCode } from './SectionCode'; +import { Coordinates } from '@fmgc/flightplanning/data/geo'; -export interface NdbNavaid extends BaseFix { - subSectionCode: NavaidSubsectionCode.NdbNavaid; +interface BaseNdbNavaid extends DatabaseItem { + sectionCode: SectionCode.Airport | SectionCode.Navaid; + subSectionCode: AirportSubsectionCode.TerminalNdb | NavaidSubsectionCode.NdbNavaid; + + location: Coordinates; + + area: WaypointArea; frequency: KiloHertz; name?: string; @@ -20,14 +26,39 @@ export interface NdbNavaid extends BaseFix { distance?: NauticalMiles; } -export function isNdbNavaid(o: any): o is NdbNavaid { +export interface TerminalNdbNavaid extends BaseNdbNavaid, BaseFix { + sectionCode: SectionCode.Airport; + subSectionCode: AirportSubsectionCode.TerminalNdb; + airportIdent: string; + area: WaypointArea.Terminal; +} + +export interface EnrouteNdbNavaid extends BaseNdbNavaid, BaseFix { + sectionCode: SectionCode.Navaid; + subSectionCode: NavaidSubsectionCode.NdbNavaid; + area: WaypointArea.Enroute; +} + +export type NdbNavaid = TerminalNdbNavaid | EnrouteNdbNavaid; + +export function isTerminalNdbNavaid(o: any): o is TerminalNdbNavaid { return ( typeof o === 'object' && - ((o.sectionCode === SectionCode.Navaid && o.subSectionCode === NavaidSubsectionCode.NdbNavaid) || - (o.sectionCode === SectionCode.Airport && o.subSectionCode === AirportSubsectionCode.TerminalNdb)) + o.sectionCode === SectionCode.Airport && + o.subSectionCode === AirportSubsectionCode.TerminalNdb + ); +} + +export function isEnrouteNdbNavaid(o: any): o is TerminalNdbNavaid { + return ( + typeof o === 'object' && o.sectionCode === SectionCode.Navaid && o.subSectionCode === NavaidSubsectionCode.NdbNavaid ); } +export function isNdbNavaid(o: any): o is NdbNavaid { + return isTerminalNdbNavaid(o) || isEnrouteNdbNavaid(o); +} + export enum NdbClass { Unknown = 1 << 0, /** diff --git a/fbw-common/src/systems/navdata/shared/types/Waypoint.ts b/fbw-common/src/systems/navdata/shared/types/Waypoint.ts index b4b2b1d5989..bb157b5e407 100644 --- a/fbw-common/src/systems/navdata/shared/types/Waypoint.ts +++ b/fbw-common/src/systems/navdata/shared/types/Waypoint.ts @@ -1,5 +1,5 @@ import { NauticalMiles } from 'msfs-geo'; -import { SectionCode } from './SectionCode'; +import { AirportSubsectionCode, EnrouteSubsectionCode, SectionCode } from './SectionCode'; import { BaseFix } from './BaseFix'; /** @@ -16,10 +16,13 @@ export enum WaypointArea { export type Waypoint = EnrouteWaypoint | TerminalWaypoint; export interface EnrouteWaypoint extends BaseWaypoint { + subSectionCode: EnrouteSubsectionCode.Waypoints; area: WaypointArea.Enroute; } export interface TerminalWaypoint extends BaseWaypoint { + // FIXME hack for runways + subSectionCode: AirportSubsectionCode.TerminalWaypoints | AirportSubsectionCode.Runways; area: WaypointArea.Terminal; airportIdent: string; } diff --git a/fbw-common/src/systems/shared/src/ApproachUtils.ts b/fbw-common/src/systems/shared/src/ApproachUtils.ts index bbc2dfc5d99..38218a65a00 100644 --- a/fbw-common/src/systems/shared/src/ApproachUtils.ts +++ b/fbw-common/src/systems/shared/src/ApproachUtils.ts @@ -54,7 +54,7 @@ export class ApproachUtils { public static shortApproachName: { /** * Format an approach name in short format (max 7 chars) - * @param approach An msfs-navdata approach object + * @param approach An approach object * @returns An approach name in short format (e.g. RNV23LY) */ (approach: Approach): string; @@ -71,7 +71,7 @@ export class ApproachUtils { public static longApproachName: { /* * Format an approach name in long format (max 9 chars) - * @param approach an msfs-navdata approach object + * @param approach an approach object * @returns An approach name in long format (e.g. RNAV23L-Y) */ (approach: Approach): string; diff --git a/fbw-common/src/systems/shared/src/UpdateThrottler.ts b/fbw-common/src/systems/shared/src/UpdateThrottler.ts index 3b0d7b15acd..a7f6a7e1c11 100644 --- a/fbw-common/src/systems/shared/src/UpdateThrottler.ts +++ b/fbw-common/src/systems/shared/src/UpdateThrottler.ts @@ -1,41 +1,33 @@ +// Copyright (c) 2021-2023, 2025 FlyByWire Simulations +// SPDX-License-Identifier: GPL-3.0 + /** * Utility class to throttle instrument updates */ export class UpdateThrottler { - private intervalMs: number; - - private currentTime: number; - - private lastUpdateTime: number; + private currentTime = 0; + private lastUpdateTime = 0; - private refreshOffset: number; - - private refreshNumber: number; + // Take a random offset to space out updates from different instruments among different + // frames as much as possible. + private refreshOffset = Math.floor(Math.random() * this.intervalMs); + private refreshNumber = 0; /** * @param {number} intervalMs Interval between updates, in milliseconds */ - constructor(intervalMs) { - this.intervalMs = intervalMs; - this.currentTime = 0; - this.lastUpdateTime = 0; - - // Take a random offset to space out updates from different instruments among different - // frames as much as possible. - this.refreshOffset = Math.floor(Math.random() * intervalMs); - this.refreshNumber = 0; - } + constructor(private readonly intervalMs: number) {} /** * Checks whether the instrument should be updated in the current frame according to the * configured update interval. * - * @param {number} deltaTime - * @param {boolean} [forceUpdate = false] - True if you want to force an update during this frame. + * @param deltaTime + * @param forceUpdate True if you want to force an update during this frame. * @returns -1 if the instrument should not update, or the time elapsed since the last * update in milliseconds */ - canUpdate(deltaTime, forceUpdate = false) { + canUpdate(deltaTime: number, forceUpdate = false): number { this.currentTime += deltaTime; const number = Math.floor((this.currentTime + this.refreshOffset) / this.intervalMs); const update = number > this.refreshNumber; @@ -44,7 +36,8 @@ export class UpdateThrottler { const accumulatedDelta = this.currentTime - this.lastUpdateTime; this.lastUpdateTime = this.currentTime; return accumulatedDelta; + } else { + return -1; } - return -1; } } diff --git a/fbw-common/src/systems/shared/src/arinc429.ts b/fbw-common/src/systems/shared/src/arinc429.ts index 906e107e6e5..aafc0227ce1 100644 --- a/fbw-common/src/systems/shared/src/arinc429.ts +++ b/fbw-common/src/systems/shared/src/arinc429.ts @@ -183,33 +183,22 @@ export class Arinc429Register implements Arinc429WordData { /** * A utility class specifically for writing Arinc429 words to a simvar. + * BNR values are quantised according to the specified bitwidth and range. * Optimized to only write when the value changes more than some quantization. */ -export class Arinc429OutputWord implements Arinc429WordData { - private word: Arinc429Word; +export class Arinc429OutputWord { + protected word: Arinc429Word; - private isDirty: boolean = true; + protected isDirty: boolean = true; - constructor( - private name: string, - value = 0, + public constructor( + protected name: string, + rawValue = 0, ) { - this.word = new Arinc429Word(value); + this.word = new Arinc429Word(rawValue); } - static empty(name: string) { - return new Arinc429OutputWord(name); - } - - get rawWord() { - return this.word.rawWord; - } - - get value() { - return this.word.value; - } - - set value(value) { + public setRawValue(value: number) { if (this.word.value !== value) { this.isDirty = true; } @@ -217,11 +206,7 @@ export class Arinc429OutputWord implements Arinc429WordData { this.word.value = value; } - get ssm() { - return this.word.ssm; - } - - set ssm(ssm) { + public setSsm(ssm: number) { if (this.word.ssm !== ssm) { this.isDirty = true; } @@ -229,48 +214,46 @@ export class Arinc429OutputWord implements Arinc429WordData { this.word.ssm = ssm; } - isFailureWarning() { - return this.word.isFailureWarning(); - } - - isNoComputedData() { - return this.word.isNoComputedData(); - } - - isFunctionalTest() { - return this.word.isFunctionalTest(); - } - - isNormalOperation() { - return this.word.isNormalOperation(); - } - - valueOr(defaultValue: number | undefined | null): number { + public valueOr(defaultValue: number | undefined | null): number { return this.word.valueOr(defaultValue); } - bitValue(bit: number): boolean { + public bitValue(bit: number): boolean { return this.word.bitValue(bit); } - bitValueOr(bit: number, defaultValue: boolean | undefined | null): boolean { + public bitValueOr(bit: number, defaultValue: boolean | undefined | null): boolean { return this.word.bitValueOr(bit, defaultValue); } - async writeToSimVarIfDirty() { + public async writeToSimVarIfDirty() { if (this.isDirty) { this.isDirty = false; - return Arinc429Word.toSimVarValue(this.name, this.value, this.ssm); + return Arinc429Word.toSimVarValue(this.name, this.word.value, this.word.ssm); } return Promise.resolve(); } - setBnrValue(value: number, ssm: Arinc429SignStatusMatrix, bits: number, rangeMax: number, rangeMin: number = 0) { + public setBnrValue( + value: number, + ssm: Arinc429SignStatusMatrix, + bits: number, + rangeMax: number, + rangeMin: number = 0, + ) { const quantum = Math.max(Math.abs(rangeMin), rangeMax) / 2 ** bits; const data = Math.max(rangeMin, Math.min(rangeMax, Math.round(value / quantum) * quantum)); - this.value = data; - this.ssm = ssm; + this.setRawValue(data); + this.setSsm(ssm); + } + + public setBitValue(bit: number, value: boolean): void { + if (value) { + this.setRawValue(this.word.value | (1 << (bit - 1))); + } else { + this.setRawValue(this.word.value & ~(1 << (bit - 1))); + } } } diff --git a/fbw-common/src/systems/shared/src/simbridge/components/Coroute.ts b/fbw-common/src/systems/shared/src/simbridge/components/Coroute.ts index bfd8e29c895..e442f007794 100644 --- a/fbw-common/src/systems/shared/src/simbridge/components/Coroute.ts +++ b/fbw-common/src/systems/shared/src/simbridge/components/Coroute.ts @@ -5,9 +5,9 @@ import { fetchWithTimeout, getSimBridgeUrl } from '../common'; import { CoRouteDto } from '../Coroute/coroute'; import { ClientState } from './ClientState'; -type coRouteCall = { +type coRouteCall = { success: boolean; - data: CoRouteDto | CoRouteDto[]; + data: T; }; /** @@ -19,7 +19,7 @@ export class CompanyRoute { * @param route The routename in question * @returns Returns the CoRoute DTO */ - public static async getCoRoute(route: String): Promise { + public static async getCoRoute(route: String): Promise> { if (!ClientState.getInstance().isConnected()) { throw new Error('SimBridge is not connected.'); } @@ -46,7 +46,7 @@ export class CompanyRoute { * @param dest the destination * @returns Returns a list of CoRoute DTOs */ - public static async getRouteList(origin: String, dest: String): Promise { + public static async getRouteList(origin: String, dest: String): Promise> { if (!ClientState.getInstance().isConnected()) { throw new Error('SimBridge is not connected.'); } diff --git a/fbw-common/src/typings/flybywire-vcockpits-instruments/html_ui/Pages/VCockpit/Instruments/A32NX/legacy.d.ts b/fbw-common/src/typings/flybywire-vcockpits-instruments/html_ui/Pages/VCockpit/Instruments/A32NX/legacy.d.ts index 47de5506a20..7b7b70fc37b 100644 --- a/fbw-common/src/typings/flybywire-vcockpits-instruments/html_ui/Pages/VCockpit/Instruments/A32NX/legacy.d.ts +++ b/fbw-common/src/typings/flybywire-vcockpits-instruments/html_ui/Pages/VCockpit/Instruments/A32NX/legacy.d.ts @@ -20,53 +20,6 @@ declare global { setState(newState: StateMachineState): void, } - - // eslint-disable-next-line camelcase - namespace A32NX_Util { - function createDeltaTimeCalculator(startTime: number): () => number - - function createFrameCounter(interval: number): number - - function createMachine(machineDef: StateMachineDefinition): StateMachine - - function trueToMagnetic(heading: Degrees, magVar?: Degrees): Degrees - - function magneticToTrue(heading: Degrees, magVar?: Degrees): Degrees - - function latLonToSpherical(ll: LatLongData): Spherical - - function sphericalToLatLon(s: Spherical): LatLongData - - function greatCircleIntersection(latlon1: LatLongData, brg1: Degrees, latlon2: LatLongData, brg2: Degrees): LatLongData - - function bothGreatCircleIntersections(latlon1: LatLongData, brg1: Degrees, latlon2: LatLongData, brg2: Degrees): [LatLongData, LatLongData] - - function getIsaTemp(alt?: Feet): number; - - function getIsaTempDeviation(alt?: Feet, sat?: Celsius): Celsius; - - function getRadialMagVar(wp: VhfNavaid): number; - - class UpdateThrottler { - constructor(intervalMs: number); - - /** - * Checks whether the instrument should be updated in the current frame according to the - * configured update interval. - * - * @param deltaTime - * @param forceUpdate - True if you want to force an update during this frame. - * @returns -1 if the instrument should not update, or the time elapsed since the last - * update in milliseconds - */ - canUpdate(deltaTime: number, forceUpdate?: boolean): number; - } - } - - class A32NX_TipsManager { - static instance: A32NX_TipsManager; - showNavRadioTuningTip(): void; - } } export {}; diff --git a/fbw-common/src/typings/fs-base-ui/html_ui/JS/Avionics.d.ts b/fbw-common/src/typings/fs-base-ui/html_ui/JS/Avionics.d.ts index 8e2bef36c05..ba6fbd102a4 100644 --- a/fbw-common/src/typings/fs-base-ui/html_ui/JS/Avionics.d.ts +++ b/fbw-common/src/typings/fs-base-ui/html_ui/JS/Avionics.d.ts @@ -371,9 +371,6 @@ declare global { protected updateAlwaysList(): void; protected clearAlwaysList(): void; registerInstrument(_instrumentName: string, _instrumentClass: CustomElementConstructor): void; - guidanceController?: any; - - navigation?: any; } declare function registerInstrument(_instrumentName: string, _instrumentClass: CustomElementConstructor): void; diff --git a/igniter.config.mjs b/igniter.config.mjs index 4cbd5342d6d..beccc6a245b 100644 --- a/igniter.config.mjs +++ b/igniter.config.mjs @@ -35,49 +35,16 @@ export default new TaskOfTasks('all', [ 'fbw-a32nx/src/behavior', 'fbw-a32nx/out/flybywire-aircraft-a320-neo/ModelBehaviorDefs/A32NX/generated', ]), - - new TaskOfTasks('atsu', [ - new ExecTask('common', 'npm run build-a32nx:atsu-common', [ - 'fbw-a32nx/src/systems/atsu/common', - 'fbw-a32nx/out/flybywire-aircraft-a320-neo/html_ui/JS/fbw-a32nx/atsu/common.js', - ]), - new ExecTask('fmsclient', 'npm run build-a32nx:atsu-fms-client', [ - 'fbw-a32nx/src/systems/atsu/common', - 'fbw-a32nx/src/systems/atsu/fmsclient', - 'fbw-a32nx/out/flybywire-aircraft-a320-neo/html_ui/JS/fbw-a32nx/atsu/fmsclient.js', - ]), - ]), new ExecTask('extras-host', 'npm run build-a32nx:extras-host', [ 'fbw-a32nx/src/systems/extras-host', 'fbw-a32nx/out/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/A32NX/ExtrasHost', 'fbw-common/src/systems/shared/src/extras', ]), - new ExecTask('failures', 'npm run build-a32nx:failures', [ - 'fbw-a32nx/src/systems/failures', - 'fbw-a32nx/out/flybywire-aircraft-a320-neo/html_ui/JS/fbw-a32nx/failures/failures.js', - ]), - new ExecTask('fmgc', 'npm run build-a32nx:fmgc', [ - 'fbw-a32nx/src/systems/fmgc', - 'fbw-a32nx/out/flybywire-aircraft-a320-neo/html_ui/JS/fbw-a32nx/fmgc', - ]), - new ExecTask('sentry-client', 'npm run build-a32nx:sentry-client', [ - 'fbw-a32nx/src/systems/sentry-client', - 'fbw-a32nx/out/flybywire-aircraft-a320-neo/html_ui/JS/fbw-a32nx/sentry-client', - ]), - new ExecTask('simbridge-client', 'npm run build-a32nx:simbridge-client', [ - 'fbw-a32nx/src/systems/simbridge-client', - 'fbw-a32nx/out/flybywire-aircraft-a320-neo/html_ui/JS/fbw-a32nx/simbridge-client', - ]), new ExecTask('systems-host', 'npm run build-a32nx:systems-host', [ 'fbw-a32nx/src/systems/systems-host', 'fbw-common/src/systems/datalink', 'fbw-a32nx/out/flybywire-aircraft-a320-neo/html_ui/Pages/VCockpit/Instruments/A32NX/SystemsHost', ]), - new ExecTask('tcas', 'npm run build-a32nx:tcas', [ - 'fbw-a32nx/src/systems/tcas', - 'fbw-a32nx/out/flybywire-aircraft-a320-neo/html_ui/JS/fbw-a32nx/tcas', - ]), - new TaskOfTasks('instruments', getA320InstrumentsIgniterTasks(), true), ], true, diff --git a/package.json b/package.json index 1aa5cde4aff..586c35a5f43 100644 --- a/package.json +++ b/package.json @@ -110,12 +110,12 @@ "eslint": "^8.56.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-jest": "^27.4.2", + "eslint-plugin-jsdoc": "^50.2.3", "eslint-plugin-jsx-a11y": "^6.7.1", "eslint-plugin-prettier": "^5.1.3", "eslint-plugin-react": "^7.33.2", "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-tailwindcss": "^3.17.3", - "eslint-plugin-jsdoc": "^50.2.3", "fs-extra": "^10.1.0", "jest": "^29.7.0", "jsdom": "^16.4.0", @@ -138,7 +138,6 @@ }, "dependencies": { "@flybywiresim/api-client": "^0.16.2", - "@turf/turf": "^6.5.0", "@flybywiresim/react-components": "^0.3.1", "@flybywiresim/tailwind-config": "^0.5.0", "@localazy/cli": "^1.6.0", @@ -150,8 +149,8 @@ "@sentry/integrations": "^6.17.7", "@sentry/tracing": "^6.17.7", "@tabler/icons": "^1.41.2", + "@turf/turf": "^6.5.0", "@types/react-canvas-draw": "^1.1.1", - "navigraph": "^1.2.35", "byte-data": "^19.0.1", "classnames": "^2.2.6", "esbuild-plugin-inline-image": "^0.0.8", @@ -160,9 +159,10 @@ "i18next": "^21.6.14", "json5": "^2.2.3", "lodash": "^4.17.20", + "mathjs": "^12.4.3", "msfs-geo": "^0.1.0-alpha3", - "msfs-navdata": "^1.1.0", "nanoid": "^3.3.1", + "navigraph": "^1.2.35", "network": "^0.6.1", "qrcode.react": "^1.0.1", "rc-slider": "^9.7.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2ef7d1008fd..e9e2cbafb9f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -71,12 +71,12 @@ dependencies: lodash: specifier: ^4.17.20 version: 4.17.21 + mathjs: + specifier: ^12.4.3 + version: 12.4.3 msfs-geo: specifier: ^0.1.0-alpha3 version: 0.1.0-alpha5 - msfs-navdata: - specifier: ^1.1.0 - version: 1.2.4 nanoid: specifier: ^3.3.1 version: 3.3.7 @@ -1632,6 +1632,12 @@ packages: dependencies: regenerator-runtime: 0.14.0 + /@babel/runtime@7.26.7: + resolution: {integrity: sha512-AOPI3D+a8dXnja+iwsUqGRjr1BbZIe771sXdapOtYI531gSqpi92vXivKcq2asu/DFpdl1ceFAKZyRzK2PCVcQ==} + engines: {node: '>=6.9.0'} + dependencies: + regenerator-runtime: 0.14.0 + /@babel/template@7.22.15: resolution: {integrity: sha512-QPErUVm4uyJa60rkI73qneDacvdvzxshT3kksGqlGWYdOTIUOwJ7RDUL8sGqslY1uXWSL6xMFKEXDS3ox2uF0w==} engines: {node: '>=6.9.0'} @@ -5065,6 +5071,10 @@ packages: resolution: {integrity: sha512-8njCHOTtFFLtegk6zQo0kkVX1rngygb/KQI6z1qZxlFI3scluC+LVTCFbrkWjBv4vvLlbQ9t88IPMC6k95VTTg==} dev: false + /complex.js@2.4.2: + resolution: {integrity: sha512-qtx7HRhPGSCBtGiST4/WGHuW+zeaND/6Ld+db6PbrulIB1i2Ev/2UPiqcmpQNPSyfBKraC0EOvOKCB5dGZKt3g==} + dev: false + /concat-map@0.0.1: resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} dev: true @@ -6513,6 +6523,10 @@ packages: mime-types: 2.1.35 dev: true + /fraction.js@4.3.4: + resolution: {integrity: sha512-pwiTgt0Q7t+GHZA4yaLjObx4vXmmdcS0iSJ19o8d/goUGgItX9UZWKWNnLHehxviD8wU2IWRsnR8cD5+yOJP2Q==} + dev: false + /fraction.js@4.3.7: resolution: {integrity: sha512-ZsDfxO51wGAXREY55a7la9LScWpwv9RxIrYABrlvOFBlH/ShPnrtsXeuUIfXKKOVicNxQ+o8JTbJvjS4M89yew==} @@ -6805,7 +6819,7 @@ packages: /history@4.10.1: resolution: {integrity: sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==} dependencies: - '@babel/runtime': 7.23.5 + '@babel/runtime': 7.26.7 loose-envify: 1.4.0 resolve-pathname: 3.0.0 tiny-invariant: 1.3.1 @@ -6816,7 +6830,7 @@ packages: /history@5.3.0: resolution: {integrity: sha512-ZqaKwjjrAYUYfLG+htGaIIZ4nioX2L70ZUMIFysS3xvBsSG4x/n1V6TXV3N8ZYNuFGlDirFg32T7B6WOUPDYcQ==} dependencies: - '@babel/runtime': 7.23.5 + '@babel/runtime': 7.26.7 dev: true /hoist-non-react-statics@3.3.2: @@ -8043,6 +8057,22 @@ packages: typed-function: 2.1.0 dev: false + /mathjs@12.4.3: + resolution: {integrity: sha512-oHdGPDbp7gO873xxG90RLq36IuicuKvbpr/bBG5g9c8Obm/VsKVrK9uoRZZHUodohzlnmCEqfDzbR3LH6m+aAQ==} + engines: {node: '>= 18'} + hasBin: true + dependencies: + '@babel/runtime': 7.26.7 + complex.js: 2.4.2 + decimal.js: 10.4.3 + escape-latex: 1.2.0 + fraction.js: 4.3.4 + javascript-natural-sort: 0.7.1 + seedrandom: 3.0.5 + tiny-emitter: 2.1.0 + typed-function: 4.2.1 + dev: false + /mdn-data@2.0.14: resolution: {integrity: sha512-dn6wd0uw5GsdswPFfsgMp5NSB0/aDe6fK94YJV/AJDYXL6HVLWBsxeq7js7Ad+mU2K9LAlwpk6kN2D5mwCPVow==} dev: true @@ -8088,7 +8118,7 @@ packages: prop-types: ^15.0.0 react: ^0.14.0 || ^15.0.0 || ^16.0.0 || ^17.0.0 dependencies: - '@babel/runtime': 7.23.5 + '@babel/runtime': 7.26.7 prop-types: 15.8.1 react: 17.0.2 tiny-warning: 1.0.3 @@ -8140,12 +8170,6 @@ packages: mathjs: 10.6.4 dev: false - /msfs-navdata@1.2.4: - resolution: {integrity: sha512-A4nvJ8AZ58n8/0cbbAG+3SchChAW35FnzH1Bw9k9DfzK5fKbMWZwlcCWWuUGuKzoPV2CHdqJyn2xn1kWoRIH3g==} - dependencies: - msfs-geo: 0.1.0-alpha5 - dev: false - /mz@2.7.0: resolution: {integrity: sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==} dependencies: @@ -9151,7 +9175,7 @@ packages: react: '>=16.9.0' react-dom: '>=16.9.0' dependencies: - '@babel/runtime': 7.23.5 + '@babel/runtime': 7.26.7 classnames: 2.3.2 dom-align: 1.12.4 rc-util: 5.38.1(react-dom@17.0.2)(react@17.0.2) @@ -9166,7 +9190,7 @@ packages: react: '>=16.9.0' react-dom: '>=16.9.0' dependencies: - '@babel/runtime': 7.23.5 + '@babel/runtime': 7.26.7 classnames: 2.3.2 rc-util: 5.38.1(react-dom@17.0.2)(react@17.0.2) react: 17.0.2 @@ -9195,7 +9219,7 @@ packages: react: '>=16.9.0' react-dom: '>=16.9.0' dependencies: - '@babel/runtime': 7.23.5 + '@babel/runtime': 7.26.7 classnames: 2.3.2 rc-trigger: 5.3.4(react-dom@17.0.2)(react@17.0.2) react: 17.0.2 @@ -9209,7 +9233,7 @@ packages: react: '>=16.9.0' react-dom: '>=16.9.0' dependencies: - '@babel/runtime': 7.23.5 + '@babel/runtime': 7.26.7 classnames: 2.3.2 rc-align: 4.0.15(react-dom@17.0.2)(react@17.0.2) rc-motion: 2.9.0(react-dom@17.0.2)(react@17.0.2) @@ -9224,7 +9248,7 @@ packages: react: '>=16.9.0' react-dom: '>=16.9.0' dependencies: - '@babel/runtime': 7.23.5 + '@babel/runtime': 7.26.7 react: 17.0.2 react-dom: 17.0.2(react@17.0.2) react-is: 18.2.0 @@ -9452,7 +9476,7 @@ packages: /regenerator-transform@0.15.2: resolution: {integrity: sha512-hfMp2BoF0qOk3uc5V20ALGDS2ddjQaLrdl7xrGXvAIow7qeWRM2VA2HuCHkUKk9slq3VwEwLNK3DFBqDfPGYtg==} dependencies: - '@babel/runtime': 7.23.5 + '@babel/runtime': 7.26.7 dev: true /regexp.prototype.flags@1.5.1: @@ -10380,6 +10404,11 @@ packages: engines: {node: '>= 10'} dev: false + /typed-function@4.2.1: + resolution: {integrity: sha512-EGjWssW7Tsk4DGfE+5yluuljS1OGYWiI1J6e8puZz9nTMM51Oug8CD5Zo4gWMsOhq5BI+1bF+rWTm4Vbj3ivRA==} + engines: {node: '>= 18'} + dev: false + /typescript@5.5.2: resolution: {integrity: sha512-NcRtPEOsPFFWjobJEtfihkLCZCXZt/os3zf8nTxjVH3RvTSxjrCamJpbExGvYOF+tFHc3pA65qpdwPbzjohhew==} engines: {node: '>=14.17'}