Skip to content

Commit

Permalink
make the "input" parameter more general
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis committed Jan 22, 2011
1 parent 2ab611d commit eae0453
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ runs, or how the grid is displayed. These properties are supported:
* **class** : the HTML class attribute to add to the outermost generated
div. This is in addition to any other classes that the widget itself
assigns (e.g. "maze").
* **input** : the id of an HTML form element that contains data to be passed
to the maze via its "input" parameter. The format is assumed to be an
object literal (without the outer curly braces).
* **input** : data that should be passed to the maze object upon creation.
This should be either a string, in which case it is passed directly to the
maze constructor, or a function, in which case it is invoked first and
the return value used as the value passed to the maze. The actual format
of the string is dependent on the algorithm used.
* **callback** : a function invoked whenever a cell is updated in the grid.
The callback should accept three parameters: the maze object, and the x
and y coordinates of the cell. If not specified, a default callback
Expand Down Expand Up @@ -109,7 +111,7 @@ maze is built. The following properties are understood (and have the same
meaning as their counterparts in the widget helper):

* **callback** : the default callback does nothing.
* **input** : an object whose attributes may be used by the algorithm to
* **input** : a string used as input to the algorithm, which can be used to
customize its behavior. Not all algorithms use this parameter.
* **seed**
* **rng**
Expand Down
6 changes: 3 additions & 3 deletions examples/maze.html
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ <h1>Growing Tree Algorithm</h1>
<small>You must click "reset" before the maze will recognize your changes.</small>
</p>
<div class="row">
<script type="text/javascript">Maze.createWidget("GrowingTree", 5, 5, { "class": "small", input: "growing_tree_input" });</script>
<script type="text/javascript">Maze.createWidget("GrowingTree", 15, 15, { "class": "medium", id: "growtree_bigger", interval: 25, input: "growing_tree_input" });</script>
<script type="text/javascript">Maze.createWidget("GrowingTree", 30, 30, { "class": "large", id: "growtree_biggest", interval: 10, input: "growing_tree_input" });</script>
<script type="text/javascript">Maze.createWidget("GrowingTree", 5, 5, { "class": "small", input: function() { return document.getElementById("growing_tree_input").value; } });</script>
<script type="text/javascript">Maze.createWidget("GrowingTree", 15, 15, { "class": "medium", id: "growtree_bigger", interval: 25, input: function() { return document.getElementById("growing_tree_input").value; } });</script>
<script type="text/javascript">Maze.createWidget("GrowingTree", 30, 30, { "class": "large", id: "growtree_biggest", interval: 10, input: function() { return document.getElementById("growing_tree_input").value; } });</script>
</div>
<hr />

Expand Down
5 changes: 4 additions & 1 deletion src/widget.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ Maze.createWidget = (algorithm, width, height, options) ->
clearInterval @mazeStepInterval
@mazeStepInterval = null

value = document.getElementById(options.input).value if options.input?
if typeof options.input == "function"
value = options.input()
else
value = options.input

@maze = new Maze(width, height, Maze.Algorithms[algorithm], callback: options.callback, seed: options.seed, rng: options.rng, input: value)
@maze.element = this
Expand Down

0 comments on commit eae0453

Please sign in to comment.