Created
May 28, 2012 08:16
-
-
Save runvnc/2817967 to your computer and use it in GitHub Desktop.
wizard in coffeescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wizardForm = | |
title: 'Submit Your Timecard' | |
help: 'To submit your timecard for your supervisor to approve, simply follow the steps below' | |
steps: [ | |
'Step 1': | |
type: 'choice' | |
question: 'Where would you like to pick up your check, if you dont want it mailed?' | |
items: [ | |
'Hold my check in blah' | |
'Hold my check in bleh' | |
'Mail my check' | |
] | |
'Step 2': | |
type: 'yesno' | |
question: 'Did you have an on-the-job accident or injury this week?' | |
followup: | |
answer: 'Yes' | |
form: 'medicalAttention' | |
'Step 3': | |
type: 'yesno' | |
question: 'Do you have vacation/holiday hours, or issues to report?' | |
followup: | |
answer: 'Yes' | |
form: 'timecardNotes' | |
] | |
############ | |
s4 = -> | |
(((1+Math.random())*0x10000)|0).toString(16).substring(1) | |
guid = -> | |
S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4() | |
class Wizard | |
constructor: (@formdata, @outID) -> | |
@currentStep = 0 | |
@steps = [] | |
for item in @formdata | |
id = guid() | |
$('body').append "<div id=\"#{id}\"></div>" | |
switch item.type | |
when 'choice' | |
step = new MultipleChoiceQuestion(id, item, @next) | |
when 'yesno' then step = new YesNoQuestion(id, item, @next) | |
else step = new Question(id, item, @next) | |
@steps.push step | |
@next() | |
next: -> | |
if @currentStep < @formdata.length | |
@currentStep++ | |
@steps[currentStep-1].display() | |
class Question | |
constructor: (@outID, @data, @imdone) -> | |
renderInput: -> | |
display: -> | |
str = "<li>" | |
str += "<div class=\"question\">#{@data.question}</div>" | |
str += @renderInput | |
str += "</li>" | |
$("#{@outID}").html str | |
@attachEvents?() | |
checkFollowUp: -> | |
if @data.followup? and @response is @data.followup.answer | |
new Form(@data.followup.form) | |
class MultipleChoiceQuestion extends Question | |
renderInput: -> | |
str = '' | |
for item in @data.items | |
str += "<input type=\"radio\" value=\"#{item}\">#{item}<br/>" | |
attachEvents: -> | |
$("#{@outID}").find('input').change -> | |
@response = $("#{@outID}").find('input:radio:checked').val() | |
@checkFollowUp?() | |
@imdone() | |
class YesNoQuestion extends MultipleChoiceQuestion | |
constructor: (@outID, @data) -> | |
@data.items = [ 'Yes', 'No' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment