new module:App(canvas, gridSize)
Initialize the Svg class
Parameters:
| Name | Type | Description |
|---|---|---|
canvas |
string | query selector of the SVG element, that will contain all SVG content of the application |
gridSize |
number | initial size of the grid in SVG pixels |
Members
-
$svg
-
jQuery element for the SVG document
-
boxes :Array
-
Array of all boxes (instances of objects derived from editorElements.Box) used on canvas
-
exportData
-
Generate an object containing export data for the canvas and all elements. Data from this function should cover all important information needed to import the network in a different session.
-
gridSize :number
-
space between grid lines in SVG pixels
-
height
-
Get the height of the main SVG element
-
leftTopPadding :number
-
distance from the left top corner to the first element in the imported network and distance from the left top corner to the imported black box in grid pixels
-
messages :Messages
-
Interface for showing messages to the user
-
tutorial :Tutorial
-
property containing an instance of Tutorial, if there is any
-
width
-
Get the width of the main SVG element
-
wires :Array
-
Array of all wires (instances of editorElements.Wire) used on canvas
-
zoom
-
Get the current zoom multiplier of the canvas
-
zoom
-
Set the zoom multiplier of the canvas. I sets the viewbox zoom and then applies the new value by calling this.applyViewbox()
Methods
-
static snapToGrid(value, gridSize) → {number}
-
static function for snapping a value to a grid
Parameters:
Name Type Description valuenumber value in SVG pixels
gridSizenumber size of the grid in SVG pixels
Returns:
number -the value rounded to the closest number divisible by the grid size
-
addPattern(pattern)
-
Add a new pattern to the definitions element in the SVG document
Parameters:
Name Type Description patternsvgObj.Pattern pattern that will be added to the
element in the SVG document -
appendElement(element, refreshopt)
-
Add an element to the canvas
Parameters:
Name Type Attributes Default Description elementeditorElements.NetworkElement Element that will be added on the canvas
refreshBoolean <optional>
true if true, the SVG document will be reloaded after adding this element
-
appendJQueryObject(object, refreshopt)
-
Append a jQuery element to the SVG document (helper for this.appendElement)
Parameters:
Name Type Attributes Default Description objectobject jQuery element that will be added to the SVG document
refreshBoolean <optional>
true if true, the SVG document will be reloaded after adding this element
-
applyViewbox()
-
Set the viewBox attribute of the SVG element and size and position attributes of the rectangle with the background grid to match the values in this.viewbox
-
cancelWireCreation()
-
helper for wireCreationHelper that cancels the wire creation process
-
cleanCanvas()
-
Remove all boxes from the canvas
-
displayContextMenu(x, y, $target)
-
Display the context menu on the specified position
Parameters:
Name Type Description xnumber horizontal position in CSS pixels
ynumber vertical position in CSS pixels
$targetjQuery.element the item user clicked on (used to display "remove this element"-type items in the menu)
-
displayCreatedWire(mousePosition)
-
helper for wireCreationHelper that displays a grey wire between the first connector and the specified mousePosition
Parameters:
Name Type Description mousePositionObject object with x and y coordinates in SVG pixels
-
getBoxByConnectorId(boxId) → {editorElements.Box}
-
Find the correct instance of editorElements.Box in the app's boxes by ID of a connector that belongs to this box
Parameters:
Name Type Description boxIdstring id of the connector
Returns:
editorElements.Box -instance of the box
-
getBoxById(boxId) → {editorElements.Box}
-
Find the correct instance of editorElements.Box in the app's boxes by the provided id
Parameters:
Name Type Description boxIdstring id of the box
Returns:
editorElements.Box -instance of the box
-
getConnectorById(connectorId, wireopt) → {editorElements.Connector}
-
Get instance of a connector based on it's ID (and also on an instance of editorElements.Wire if provided)
The wire variable is used as heuristic: When we know the wire, we have to check only two gates instead of all of them
Parameters:
Name Type Attributes Description connectorIdstring id of the connector
wireeditorElements.Wire <optional>
instance of the Wire that is connected to this connector
Returns:
editorElements.Connector -instance of the connector
-
getConnectorPosition(connector, snapToGridopt) → {Object}
-
get the coordinates of the specified connector
Parameters:
Name Type Attributes Default Description connectorConnector instance of Connector
snapToGridBoolean <optional>
true if true, the connector position will be snapped to the grid
Returns:
Object -point - object containing numeric attributes
xandy -
getInconvenientNodes() → {Set}
-
get set of nodes, that are inconvenient for wiring, but can be used, just are not preferred
Returns:
Set -set of nodes (objects containing x and y coordinates) that are not preferred for wiring
-
getNonRoutableNodes() → {Set}
-
get set of nodes, that cannot be used for wiring at any circumstances
Returns:
Set -set of nodes (objects containing x and y coordinates) that are not suitable for wiring
-
getRealJQueryTarget(target) → {target}
-
Get the logical jQuery target based on the factual jQuery target.
If the object, that user interacted with, is not a connector and is in a group, return the group jQuery object instead of the original jQuery object.
Parameters:
Name Type Description targettarget jQuery target of the object user interacted with
Returns:
target -jQuery target of the object user wanted to interact with
-
getRealTarget(target) → {editorElements.NetworkElement}
-
Get instance of some object from editorElement based on the jQuery target
Parameters:
Name Type Description targettarget jQuery target that user interacted with
Returns:
editorElements.NetworkElement -instance of an object derived from editorElements.NetworkElement that the user interacted with
-
getWireById(wireId) → {editorElements.Wire}
-
Find the correct instance of editorElements.Wire in the app's wires by the provided id
Parameters:
Name Type Description wireIdstring id of the wire
Returns:
editorElements.Wire -instance of the wire
-
getWiresByConnectorId(connectorId) → {Set}
-
Find all wires that are connected to the specified connector
Parameters:
Name Type Description connectorIdstring id of the connector
Returns:
Set -set of ID's of the wires connected to this connector
-
gridToSVG(value) → {number}
-
convert grid pixels to SVG pixels
Parameters:
Name Type Description valuenumber distance in grid pixels
Returns:
number -distance in SVG pixels
-
hideContextMenu()
-
hide the context menu
-
hideCreatedWire()
-
helper for wireCreationHelper that hides the temporary wire when wire creation is done
-
importData(data, xopt, yopt)
-
Recreate a logic network from the data provided
Parameters:
Name Type Attributes Description dataobject object containing information about the imported network
xnumber <optional>
horizontal position of the left top corner of the network in grid pixels
ynumber <optional>
vertical position of the left top corner of the network in grid pixels
-
moveToBackById(objId)
-
move an element to the back in the canvas
Parameters:
Name Type Description objIdstring id of the element
-
moveToFrontById(objId)
-
move an element to the front in the canvas
Parameters:
Name Type Description objIdstring id of the element
-
newBlackbox(x, y, inputs, outputs, table, name, refreshopt) → {editorElements.Blackbox}
-
creates a new blackbox
Parameters:
Name Type Attributes Default Description xnumber horizontal position of the blackbox in SVG pixels
ynumber vertical position of the gate in SVG pixels
inputsnumber number of input pins of this blackbox
outputsnumber number of output pins of this blackbox
tableArray Array of arrays, each inner array contains list of Logic.states, that describe the combination of input pin and output pin states in the order from the top to bottom for both input and output connectors. If we had an AND array as a blackbox, one of the states could be
[Logic.state.on, Logic.state.off, Logic.state.off]which means that if the first input connector is in theonstate and the second connector is in theoffstate, the state of the output connector will beoff. The array can be described as[state for input conn 1, state for input conn 2, ..., state for output conn 1, state for output conn 2 ...].namestring a name that will be displayed on the blackbox
refreshboolean <optional>
true if true, this.refresh() will be called after adding the gate
Returns:
editorElements.Blackbox -instance of Blackbox that has been added to the canvas
-
newBox(x, y, object, refreshopt) → {editorElements.Box}
-
Add a new Box to the canvas
Parameters:
Name Type Attributes Default Description xnumber horizontal position of the box in SVG pixels
ynumber vertical position of the box in SVG pixels
objecteditorElements.Box instance of an object derived from the editorElements.Box class
refreshBoolean <optional>
true if true, this.refresh() will be called after adding the box
Returns:
editorElements.Box -return the instance of the newly added object
-
newGate(name, x, y, refreshopt) → {editorElements.Gate}
-
Create a new gate on the specified position
Parameters:
Name Type Attributes Default Description namestring type of the gate (and, or ...)
xnumber horizontal position of the gate in SVG pixels
ynumber vertical position of the gate in SVG pixels
refreshboolean <optional>
true if true, this.refresh() will be called after adding the gate
Returns:
editorElements.Gate -instance of Gate that has been newly added
-
newInput(x, y, isOnopt, refreshopt) → {editorElements.InputBox}
-
Create an input box on the specified position
Parameters:
Name Type Attributes Default Description xnumber horizontal position of the gate in SVG pixels
ynumber vertical position of the gate in SVG pixels
isOnboolean <optional>
false state of the input box (default is false (off))
refreshboolean <optional>
true if true, this.refresh() will be called after adding the input box
Returns:
editorElements.InputBox -instance of the InputBox that has been newly added
-
newOutput(x, y, refreshopt) → {editorElements.InputBox}
-
Create an output box on the specified position
Parameters:
Name Type Attributes Default Description xnumber horizontal position of the gate in SVG pixels
ynumber vertical position of the gate in SVG pixels
refreshboolean <optional>
true if true, this.refresh() will be called after adding the output box
Returns:
editorElements.InputBox -instance of the OutputBox that has been newly added
-
newWire(fromId, toId, refreshopt) → {editorElements.Wire}
-
Create a new wire connecting the provided connectors
Parameters:
Name Type Attributes Default Description fromIdstring id of the connector that the wire is attached to
toIdstring id of the connector that the wire is attached to
refreshBoolean <optional>
true if refresh is set to true, the SVG document will be reloaded after adding the wire
Returns:
editorElements.Wire -instance of editorElements.Wire that has been added to the canvas
-
onKeyDown(event)
-
Process all keydown events that are connected to the app
Parameters:
Name Type Description eventjquery.KeyboardEvent KeyboardEvent generated by a listener
-
onKeyUp(event)
-
Process all keyup events that are connected to the app
Parameters:
Name Type Description eventjquery.KeyboardEvent KeyboardEvent generated by a listener
-
onMouseDown(event)
-
Process all mousedown events that are happening directly on the canvas
Parameters:
Name Type Description eventjquery.MouseEvent MouseEvent generated by a listener
-
onMouseMove(event)
-
Process all mousemove events that are happening directly on the canvas
Parameters:
Name Type Description eventjquery.MouseEvent MouseEvent generated by a listener
-
onMouseUp()
-
Process all mouseup events that are happening directly on the canvas
-
refresh()
-
Reload the SVG document (needed to display a newly appended jQuery object)
-
removeBox(boxId)
-
Remove a box from canvas based on the provided ID
Parameters:
Name Type Description boxIdstring id of the box that should be removed
-
removeWireById(wireId)
-
Remove wire that has the provided ID
Parameters:
Name Type Description wireIdstring ID of the wire that should be removed
-
removeWiresByConnectorId(connectorId)
-
Remove all wires that are connected to the connector provided by its ID
Parameters:
Name Type Description connectorIdstring ID of the connector
-
snapToGrid(value) → {number}
-
snap a value to a grid
Parameters:
Name Type Description valuenumber value in SVG pixels
Returns:
number -the value rounded to the closest number divisible by the grid size
-
startNewSimulation(startingConnector, state)
-
Run a logic simulation from the startingConnector. This refreshes the states of all elements in the network whose inputs are directly (or by transition) connected to startingConnector's output
Parameters:
Name Type Description startingConnectorOutputConnector run simulation from this output connector
stateLogic.state new state of the startingConnector
-
startTutorial()
-
start the tutorial
-
SVGToGrid(value) → {number}
-
convert SVG pixels to grid pixels
Parameters:
Name Type Description valuenumber distance in SVG pixels
Returns:
number -distance in grud pixels
-
wireCreationHelper(connectorId)
-
When user clicks on a connector, remember it until they click on some other connector. Than call newWire with the last two connectors ids as arguments. Visualize the process by displaying a grey wire between the first conenctor and the mouse pointer.
Parameters:
Name Type Description connectorIdstring id of the connector that the user clicked on