Skip to content

Instantly share code, notes, and snippets.

@krawaller
Created September 28, 2011 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krawaller/1247639 to your computer and use it in GitHub Desktop.
Save krawaller/1247639 to your computer and use it in GitHub Desktop.
mathparser UI code
# ----------------------------- jquerified DOM references ---------------------------
ui = {}
ui[id] = $("##{id}") for id in ["input","output","response","history","current","savebtn","examples","clearhistorybtn","clearbtn"]
ui.currentstring = $ "#current .string"
ui.currentobjprint = $ "#current .objprint"
# -------------------------- variables/constans -----------------------------------
pre = "<p>Interpreted from:</p>"
prelength = pre.replace(/<[^>]*>/g,"").length
lastclickeditem = $()
# ----------------------------------- functions -----------------------------------
setVal = (val)->
ui.input.val(val).focus()
updateCurrent()
setMsg = (msg,isError=false)->
ui.response.animate {opacity:0.2},100,->
ui.response.text(msg).toggleClass("error",isError).animate {opacity:1},100
containsVoid = (o)->
return true if o.type is "void"
if o.objs and o.objs.length
for child in o.objs
return true if containsVoid child
false
updateString = (el,used,rest)->
txt = el.text()
str = txt.substr(prelength)
if used is undefined or rest is undefined
el.html (if txt.length then pre else "")+str
else
start = str.length-(used.length+rest.length)
length = used.length
el.html pre+str.substr(0,start)+"<span>"+str.substr(start,length)+"</span>"+str.substr start+length
saveToHistory = ->
if ui.input.val().length
setMsg "Saved expression to history list."
$("#current > .entry").clone().hide().prependTo(ui.history).slideDown 150
ui.currentstring.empty()
ui.currentobjprint.empty()
setVal ""
else
setMsg "Cannot save empty string!",true
updateCurrent = ->
expr = ui.input.val()
ui.currentobjprint.empty()
ui.currentstring.empty()
return if expr is ""
try
o = parser.ensureSource parser.parseExpression expr
catch e
err = "Warning, parser error: "+e
o.root = "root"
ui.currentstring.html pre+expr
ui.currentobjprint.append objprinter o
if o.rest.length and o.rest.match(/^\s*\S/g) # non-whitestring rest!
err = "Warning, couldn't parse this part: "+o.rest
else if containsVoid o
err = "Warning, expression contains void!"
setMsg err ? "Successfully parsed "+expr,!!err
# ----------------------------------- event hookups -----------------------------------
ui.output.delegate ".obj","click",(e)->
clicktarget = $(e.target).closest ".obj"
root = clicktarget.closest ".root"
currentmark = if root.hasClass "markedobj" then root else root.find ".markedobj"
if currentmark.length is 0 or root.hasClass("markedobj") or not clicktarget.is(lastclickeditem)
newmark = clicktarget
else
newmark = currentmark.parent().closest ".obj"
currentmark.removeClass "markedobj"
newmark.addClass "markedobj"
updateString root.closest(".entry").find(".string"),newmark.attr("data-used"), newmark.attr "data-rest"
lastclickeditem = clicktarget
false
ui.history.delegate ".delete","click",(e)-> $(e.target).closest(".entry").animate {height:0},150,-> $(this).remove()
ui.history.delegate ".load","click",(e)-> setVal $(e.target).closest(".entry").find(".root").attr("data-used")
ui.savebtn.click saveToHistory
ui.clearhistorybtn.click -> ui.history.animate {height:0},150, -> $(this).empty().css height:"auto"
ui.clearbtn.click ->
setVal ""
setMsg ""
ui.input.bind "keyup", (e)-> do if e.keyCode is 13 then saveToHistory else updateCurrent
ui.examples.delegate "li","click",(e)->
newstr = $(e.target).text()
setVal newstr unless ui.input.val() is newstr
# ---------------------------------- initiation ------------------------------------------
ui.input.focus()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment