Skip to content

Commit

Permalink
add get
Browse files Browse the repository at this point in the history
  • Loading branch information
marty-wang committed Oct 25, 2011
1 parent 6623ce6 commit b7ddd0b
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 40 deletions.
74 changes: 68 additions & 6 deletions demo/app/coffee/bst_app.coffee
Expand Up @@ -25,12 +25,14 @@ do (App) ->
@_branchEnd = [@_x, @_y]
@_payload = null
@_highlightBranchTimer = null
@_highlightTimer = null

# property
@level = 0

# config
@_animDuration = 250
@_branchHighlightColor = "blue"

_render.call this
_registerEventHandler.call this
Expand Down Expand Up @@ -70,12 +72,30 @@ do (App) ->
@_centerLine.attr props

this.highlightBranch(false)

highlight: (stopOldAnim = true)->
clearTimeout @_highlightTimer
@_leaf.stop() if stopOldAnim

highlightColor = @_branchHighlightColor
@_leaf.attr {
fill: highlightColor
}

highlightAnim = Raphael.animation {
fill: "black"
}, 1000, "<>"

@_highlightTimer = setTimeout (=>
@_leaf.animate highlightAnim
), 1000


highlightBranch: (stopOldAnim = true)->
clearTimeout @_highlightBranchTimer
@_branch.stop() if stopOldAnim

highlightColor = "blue"
highlightColor = @_branchHighlightColor
@_branch.attr {
stroke: highlightColor
}
Expand Down Expand Up @@ -160,7 +180,6 @@ do (App) ->
@_leaf.attr {
stroke: "white"
fill: "black"
opacity: 0.4
}

@_label.attr {
Expand Down Expand Up @@ -290,6 +309,25 @@ do (App) ->
}

get: (key) ->
traceStr = "from"
value = @_data.get key, (item)->
branch = "found"
switch item.branch
when -1 then branch = "left=>"
when 1 then branch = "right=>"

traceStr += " Leaf #{item.key} #{branch}"
item.value.highlightBranch()

traceStr += " dead end" unless value?
found = value?
_triggerLog.call this, {
type: "get"
message: traceStr
found: found
key: key
}
value

clear: ->
# clear all leafs from @_data
Expand Down Expand Up @@ -388,19 +426,37 @@ $ ->
when "log"
$log.text e.message
if e.subtype is "create"
$log.removeClass "update" if $log.hasClass "update"
$log.removeClass "negative"
$log.addClass "positive"
else if e.subtype is "update"
$log.addClass "update" unless $log.hasClass "update"
$log.removeClass "negative positive"
when "trace"
$trace.text e.message
if e.subtype is "create"
$trace.removeClass "update" if $trace.hasClass "update"
$trace.removeClass "negative"
$trace.addClass "positive"
else if e.subtype is "update"
$trace.addClass "update" unless $trace.hasClass "update"
$trace.removeClass "negative positive"
when "get"
$trace.text e.message
found = e.found
logMessage = "found Leaf #{e.key}"
logMessage = "cannot " + logMessage unless found
$log.text logMessage
if found
$trace.removeClass "negative positive"
$log.removeClass "negative positive"
else
$trace.removeClass "positive"
$log.removeClass "positive"
$trace.addClass "negative"
$log.addClass "negative"


$key_select = $('#key_select')
$value_input = $('#value_input')
$add_button = $('#add_button')
$get_button = $('#get_button')

$log = $('#bst-demo .log')
$trace = $('#bst-demo .trace')
Expand All @@ -413,6 +469,12 @@ $ ->
value = "Leaf #{key}" if value is ""
bstDemo.put key, value

$get_button.click (e)->
e.preventDefault()
key = $key_select.val()
leaf = bstDemo.get key
leaf.highlight() if leaf?

$(document).bind "keypress", (e)->
if 48 <= e.keyCode <= 57
key = e.keyCode - 48
Expand Down
105 changes: 88 additions & 17 deletions demo/app/compiled/bst_app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions demo/binary_search_tree.html
Expand Up @@ -45,6 +45,9 @@ <h2 class="subject">Binary Search Tree Demo</h2>
<li class="item">
<input id="add_button" type="button" value="Add">
</li>
<li class="item">
<input id="get_button" type="button" value="Get">
</li>
</ul>
<p class="notes">Notes: You can press number keys 0-9 to add leafs.</p>
<p class="log"></p>
Expand Down
9 changes: 6 additions & 3 deletions demo/styles/css/main.css
Expand Up @@ -90,13 +90,16 @@
top: 0;
font-size: 20px;
font-weight: bold;
color: red;
color: yellow;
background: black;
padding: 4px;
margin: 10px;
}
#bst .log.update, #bst .trace.update {
color: yellow;
#bst .log.positive, #bst .trace.positive {
color: green;
}
#bst .log.negative, #bst .trace.negative {
color: red;
}
#bst .trace {
font-size: 16px;
Expand Down
10 changes: 7 additions & 3 deletions demo/styles/less/main.less
Expand Up @@ -112,13 +112,17 @@
top: 0;
font-size: 20px;
font-weight: bold;
color: red;
color: yellow;
background: black;
padding: 4px;
margin: 10px;

&.update {
color: yellow;
&.positive {
color: green;
}

&.negative {
color: red;
}
}

Expand Down

0 comments on commit b7ddd0b

Please sign in to comment.