Skip to content

developers todo javascript

nhmkdev edited this page Oct 10, 2015 · 5 revisions

Developers - todo - JavaScript

Proposal

This is the proposed design for supporting JavaScript for Definition values and reference values. This functionality would be an alternative to the existing scripting functionality.

General Information

Key points:

  • ClearScript will be used to execute the JavaScript
  • Incept is the name given to the original scripting system used by CardMaker (made this up just now)
  • JavaScript and Incept will co-exist

Disallow Mixing of Scripting Types

For simplicity (any my sanity) a project will either be in JavaScript or Incept. This will be a setting on the project and CardMaker will have a default setting for all new projects so users do not have to set it each time they create a new project.

Script Generation

The Definition value will be a script intended to be executed without the creation of a JavaScript function. The last executed line determines the final output value. There is no need to return.

Example 1

'hello world'

Result hello world

Example 2

x = 20;
if(x > 15) 'wow';
if(x > 25) 'great';

Result wow

Special Variables

Variable Description
deckIndex The overall index of the card in the Layout
cardIndex The index of the card based on the current card's count column

(likely more!)

Reference Variables

Reference variables will be accessible by the column name (lower case!). The only exception is if the column name includes a space. In this case an underscore(_) will be used to replace any spaces in the name.

Defines will be accessible via the same naming scheme.

JavaScript Functions

A reference value can itself be a fully defined function with or without parameters.

Post-processing

Replacements (TBD)

Likely the standard replacements like newlines etc.

Usage

Raw String

As with JavaScript a raw string must be quoted. This quoting only applies to values specified in the CardMaker Definition value so it can be interpreted as a raw string when passed into the JavaScript interpreter.

Standard reference

Current row values:

Reference Value
name William

The Definition required for the name reference is name

Functional reference

Current row values:

Reference Value
name William
color function(){ if(name == 'William') return 'red'; }

The Definition required for the color reference is color()

Internally CardMaker would generate a script that looks like the following.

name = 'William';
color = function(){ if (name == 'William') return 'red'; }
color()

Complex Reference

A complex reference is one that combines

Reference Value Escape Sequence

~ is used as a special case to avoid Excel/Calc breaking entries that start with a single quote ('). Normally Excel/Calc would interpret these as special characters and potentially trash the value specified by the user. By entering a ~ as the first character before a single quote Calc/Excel leave the value as-is. CardMaker will detect and remove the ~ if it is the first character and immediately followed by a single quote.

Detecting a function used as a variable

CardMaker should help users that make mistakes. For example if the user accidentally enters color instead of color() the returned type is not an int/string/float and a log message should indicate this.

Numeric Operations

parseInt and parseFloat will be required for numeric operations involving values assigned from references. This is necessary because the value pulled from the reference is always assumed to be a string.

Example:

Reference Value
points 45
color function(){ if(parseInt(points) > 30) return 'awesome'; }

UI Changes

Multiline Definition

The application has always had a no-multiline Definition field to avoid confusing users. When a project is in JavaScript mode the Definition control should allow multiline input.