archematics/src/adapptlet.civet

101 lines
3.2 KiB
Plaintext

import https://code.jquery.com/jquery-3.7.1.js
import type {AppletObject} from ./deps/geogebra/api.ts
type AppletDescription
html: string
children: HTMLCollection
id: string
width: number
height: number
joyceApplets: AppletDescription[] := []
$('applet[code="Geometry"]').before (i, html) ->
id := `joyceApplet${i}`
joyceApplets.push { html, this.children, id,
width: parseInt(this.getAttribute('width') ?? '200'),
height: parseInt(this.getAttribute('height') ?? '200') }
`<div id="${id}"></div>`
jQuery.getScript 'https://www.geogebra.org/apps/deployggb.js', =>
for each jApp of joyceApplets
params := {
appName: 'classic',
jApp.width,
jApp.height,
appletOnLoad: (api: AppletObject) =>
for child of jApp.children
dispatchJcommand api, child
api.setCoordSystem(-10, 10 + jApp.width, -10, 10 + jApp.height)
} as const
geoApp := new GGBApplet params
geoApp.inject jApp.id
type GeogebraCallback = (api: AppletObject) => void
type Commander
command: string
callbacks: GeogebraCallback[]
type ClassHandler = (
name: string, m: string, data: string, colors: string[]) => Commander
function dispatchJcommand(api: AppletObject, param: Element): void
val := param.getAttribute 'value'
unless val return
switch param.getAttribute 'name'
'background'
api.setGraphicsOptions 1, bgColor: `#${val}`
'title'
api.evalCommand `TitlePoint = Corner(1,1)
Text("${val}", TitlePoint + (2,5))`
/e\[\d+\]/
{command, callbacks} := jToG val
if command
if api.evalCommand command
for each cb of callbacks
cb(api)
else
console.log `Geogebra command '${command}' translated from`,
val, 'failed.'
else
console.log `Unknown command '${val}'`
else
console.log `Unkown param ${param}`
function jToG(jCom: string): Commander
[name, klass, method, data, ...colors] := jCom.split(';')
if klass in classHandler
return classHandler[klass] name, method, data, colors
console.log `Unknown entity class ${klass}`
command: '', callbacks: []
classHandler: Record<string, ClassHandler> :=
point: (name, method, data, colors) =>
command .= ''
callbacks: GeogebraCallback[] .= []
switch method
/free|fixed/
command += `${name} = (${data})`
if method is 'fixed'
callbacks.push (api: AppletObject) => api.setFixed(name, true)
'perpendicular'
[center, direction] := data.split(',')
command += `${name} = Rotate(${direction}, 3*pi/2, ${center})`
return {command, callbacks}
line: (name, method, data, colors) =>
command .= ''
callbacks: GeogebraCallback[] .= []
switch method
'connect'
command += `${name} = Segment(${data})`
return {command, callbacks}
circle: (name, method, data, colors) =>
command .= ''
callbacks: GeogebraCallback[] .= []
switch method
'radius'
[center, point] := data.split(',')
command += `${name} = Circle(${center}, ${point})`
return {command, callbacks}