Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let vxm = {};
- vxm.ExprID;
- vxm.TableState = {};
- vxm.Scaling = true;
- Object.defineProperties(vxm.TableState, {
- INVALID : {
- value: 0,
- writable: false,
- enumerable: true,
- configurable: true
- },
- NO_TRAIL : {
- value: 1,
- writable: false,
- enumerable: true,
- configurable: true
- },
- HAS_TRAIL : {
- value: 2,
- writable: false,
- enumerable: true,
- configurable: true
- }
- }); // !TableState defineProperties
- Object.assign(vxm, {
- initialize : function () {
- if (typeof vxm.ExprID !== 'undefined') {
- throw Error('Cannot initialize twice');
- }
- vxm.ExprID = null;
- let element = (elem => elem[elem.length - 1])(document.getElementsByClassName('dcg-graph-outer'));
- element.addEventListener('mousedown', (e) => {
- let client = element.getBoundingClientRect();
- let graphPix = Calc.graphpaperBounds.pixelCoordinates;
- let graphMath = Calc.graphpaperBounds.mathCoordinates;
- let x, y;
- x = (e.x - client.x) / graphPix.width * graphMath.width + graphMath.left;
- y = -(e.y - client.y) / graphPix.height * graphMath.height + graphMath.top;
- if (
- vxm.getIndex() !== -1 &&
- e.altKey &&
- e.buttons === 1
- ) {
- let TS = vxm.TableState;
- let expr = Calc.getExpressions()[vxm.getIndex()];
- let lastIdx = expr.columns[0].values.length - 1;
- let canModify = false;
- if (vxm.Scaling) {
- let scaleFactor = Math.pow(10,
- Math.ceil(
- Math.log10(
- graphPix.width / graphMath.width
- )
- )
- );
- x = Math.round(x * scaleFactor) / scaleFactor;
- y = Math.round(y * scaleFactor) / scaleFactor;
- }
- switch (vxm.validateExpression(expr)) {
- case TS.NO_TRAIL:
- expr.columns[0].values.push(x);
- expr.columns[1].values.push(y);
- canModify = true;
- break;
- case TS.HAS_TRAIL:
- expr.columns[0].values[lastIdx] = x;
- expr.columns[1].values[lastIdx] = y;
- canModify = true;
- break;
- case TS.INVALID:
- //nothing to do
- break;
- default:
- //nothing to do
- }
- if (canModify) {
- Calc.setExpression(expr);
- }
- }
- }); // !element_mouseup
- }, // !initialize ()
- bindExpression : function (idx) {
- --idx;
- vxm.ExprID = null;
- let expList = Calc.getExpressions();
- let message = (str) => {console.log(str); return -1};
- if ( idx < 0 || idx >= expList.length )
- return message('input error: expression does not exist');
- let msg = vxm.validateExpression(expList[idx]);
- if (msg === vxm.TableState.INVALID)
- return msg;
- vxm.ExprID = expList[idx].id;
- }, // !bindExpression ()
- validateExpression : function (expr) {
- let message = (str, val) => {console.log(str); return val};
- if (expr.type !== 'table'){
- vxm.ExprID = null;
- return message(
- 'expression error: expression must be a table', vxm.TableState.INVALID
- );
- }
- if (expr.columns.length !== 2) {
- return message(
- 'expression error: table must have 2 columns',
- vxm.TableState.INVALID
- );
- }
- let lastIdx = expr.columns[0].values.length;
- if (lastIdx !== expr.columns[1].values.length) {
- return message(
- 'expression error: table columns must have the same size',
- vxm.TableState.INVALID
- );
- }
- --lastIdx;
- if (
- expr.columns[0].values[lastIdx] &&
- expr.columns[1].values[lastIdx]
- ) {
- return vxm.TableState.NO_TRAIL;
- }
- return vxm.TableState.HAS_TRAIL;
- }, // !validateExpression ()
- getIndex : function () {
- let calcExpressions = Calc.getExpressions();
- return calcExpressions.findIndex((elem) => {
- return elem.id === vxm.ExprID;
- });
- }, // !getIndex ()
- addPolygon : function () {
- if (vxm.getIndex() !== -1) {
- let expr = Calc.getExpressions()[vxm.getIndex()];
- let msg = vxm.validateExpression(expr);
- if (msg === vxm.TableState.INVALID)
- return msg;
- let x = expr.columns[0].latex;
- let y = expr.columns[1].latex;
- Calc.setExpression({
- latex : `\\operatorname{polygon}\\left(${x},${y}\\right)`
- });
- } else {
- console.log('There is not bound expression');
- } // !if-else
- },
- unbindExpression : function () {
- vxm.ExprID = null;
- }
- }); // !vxm assign
- vxm.initialize();
Advertisement
Add Comment
Please, Sign In to add comment