Paul: Check-in [05a8e83286]

Online event coordination and survey application

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Refactor main.coffee script
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:05a8e832862f8677b4da5d887518cce3777177098f58d6e534137a22ead48545
User & Date: milouse 2018-11-16 16:09:54
Context
2018-11-19
20:52
Add moment.js and some default values check-in: 8e47d9ffd1 user: milouse tags: trunk
2018-11-16
16:09
Refactor main.coffee script check-in: 05a8e83286 user: milouse tags: trunk
16:09
Upgrade webpack check-in: a47ba8e108 user: milouse tags: trunk
Changes

Changes to assets/coffee/main.coffee.

6
7
8
9
10
11
12
13
14
15
16
17
18
19


20
21
22
23



24
25
26
27
28



29
30
31
32
33
34
























35
36
37
38
39
40
41
42
43
44
45


46
47
48
49
50
51
52
..
53
54
55
56
57
58
59
60
61
62
63
64
65
66

67

68
69
70
71

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

    return null unless nb?
    nb.addEventListener 'click', (event) ->
      currentSurveyManager.insert_template()
    @poll_type = pt.value
    pt.addEventListener 'change', (event) ->
      currentSurveyManager.switch_type event.target.value
    @switch_type(@poll_type)

  switch_type: (poll_type) ->
    @poll_type = poll_type
    for p in @poll_form.querySelectorAll 'input.prop-field'
      old_type = p.type
      if @poll_type == 'date'
        continue if old_type == 'date'


        p['oldTextValue'] = p.value
        p.value = p['oldDateValue'] ? ''
        p.setAttribute 'type', 'date'
        p.setAttribute 'name', 'proposals[][date]'



        timeinput = document.createElement 'INPUT'
        timeinput.setAttribute 'type', 'time'
        timeinput.setAttribute 'name', 'proposals[][time]'
        timeinput.className = 'prop-time-field'
        timeinput.value = p['oldTimeValue'] ? ''



        remLink = p.parentElement.querySelector \
          'input[type=button].del-prop'
        if remLink?
          p.parentElement.insertBefore timeinput, remLink
        else
          p.parentElement.appendChild timeinput
























        continue
      continue if old_type == 'text'
      p['oldDateValue'] = p.value
      p.value = p['oldTextValue'] ? ''
      p.setAttribute 'type', 'text'
      p.setAttribute 'name', 'proposals[][label]'
      timeinput = p.parentElement.querySelector \
        'input[type=time].prop-time-field'
      if timeinput?
        p['oldTimeValue'] = timeinput.value
        p.parentElement.removeChild(timeinput)



  remove_template: (elem) ->
    allProps = document.getElementById 'survey_fields'
    tpl = elem.parentElement
    return false if !allProps? or !tpl?
    allProps.removeChild(tpl)

................................................................................
  insert_template: ->
    allProps = document.getElementById 'survey_fields'
    return false unless allProps
    prop = String(Math.floor(Math.random() * 1000000))
    newProp = document.createElement 'LI'
    newProp.className = 'form_group'
    label = document.createElement 'LABEL'
    label.setAttribute 'for', "prop#{prop}field"
    label.textContent = 'Proposal '
    newProp.appendChild label
    input = document.createElement 'INPUT'
    if @poll_type == 'yesno'
      input.setAttribute 'type', 'text'
      input.setAttribute 'name', 'proposals[][label]'

      timeinput = null

    else
      input.setAttribute 'type', 'date'
      input.setAttribute 'name', 'proposals[][date]'
      timeinput = document.createElement 'INPUT'

      timeinput.setAttribute 'type', 'time'
      timeinput.setAttribute 'name', 'proposals[][time]'
      timeinput.className = 'prop-time-field'
    input.setAttribute 'id', "prop#{prop}field"
    input.className = 'prop-field'
    newProp.appendChild input
    newProp.appendChild(timeinput) if timeinput?
    remLink = document.createElement 'INPUT'
    remLink.type = 'button'
    remLink.value = '×'
    remLink.title = 'Remove proposal'
    remLink.className = 'del-prop'
    remLink.addEventListener 'click', (event) ->
      event.preventDefault()
      event.stopPropagation()
      currentSurveyManager.remove_template(event.target)
    newProp.appendChild remLink
    allProps.appendChild(newProp)

window['currentSurveyManager'] = null
document.addEventListener 'DOMContentLoaded', ->
  cp = document.getElementById('poll_form')
  return unless cp?
  window.currentSurveyManager = new SurveyManager(cp)









|
|
|
|
|
|
>
>
|
|
|
|
>
>
>
|
|
|
|
|
>
>
>
|
|
<
<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


<
<
<
<
<
<
<
<
<
>
>







 







|



<
|
|
>
|
>

<
|
|
>
|
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<







>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38




39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64









65
66
67
68
69
70
71
72
73
..
74
75
76
77
78
79
80
81
82
83
84

85
86
87
88
89
90

91
92
93
94




95











96
97
98
99
100
101
102
103
    return null unless nb?
    nb.addEventListener 'click', (event) ->
      currentSurveyManager.insert_template()
    @poll_type = pt.value
    pt.addEventListener 'change', (event) ->
      currentSurveyManager.switch_type event.target.value
    @switch_type(@poll_type)

  make_question_input: (input) ->
    input['oldDateValue'] = input.value
    input.value = input['oldTextValue'] ? ''
    input.setAttribute 'type', 'text'
    input.setAttribute 'name', 'proposals[][label]'
    input.className = 'prop-field'

  make_date_input: (input) ->
    input['oldTextValue'] = input.value
    input.value = input['oldDateValue'] ? ''
    input.setAttribute 'type', 'date'
    input.setAttribute 'name', 'proposals[][date]'
    input.className = 'prop-field'

  add_time_input: (date_rel) ->
    input = document.createElement 'INPUT'
    input.setAttribute 'type', 'time'
    input.setAttribute 'name', 'proposals[][time]'
    input.className = 'prop-time-field'
    input.value = date_rel['oldTimeValue'] ? ''
    date_rel.parentNode.insertBefore(input, date_rel.nextElementSibling)

  remove_time_input: (date_rel) ->
    input = date_rel.parentElement.querySelector \
      'input[type=time].prop-time-field'




    return unless input?
    date_rel['oldTimeValue'] = input.value
    date_rel.parentNode.removeChild(input)

  create_remove_link: ->
    input = document.createElement 'INPUT'
    input.type = 'button'
    input.value = '×'
    input.title = 'Remove proposal'
    input.className = 'del-prop'
    input.addEventListener 'click', (event) ->
      event.preventDefault()
      event.stopPropagation()
      currentSurveyManager.remove_template(event.target)
    input

  switch_type: (poll_type) ->
    @poll_type = poll_type
    for p in @poll_form.querySelectorAll 'input.prop-field'
      old_type = p.type
      if @poll_type == 'date'
        continue if old_type == 'date'
        @make_date_input p
        @add_time_input p
        continue
      continue if old_type == 'text'









      @make_question_input p
      @remove_time_input p

  remove_template: (elem) ->
    allProps = document.getElementById 'survey_fields'
    tpl = elem.parentElement
    return false if !allProps? or !tpl?
    allProps.removeChild(tpl)

................................................................................
  insert_template: ->
    allProps = document.getElementById 'survey_fields'
    return false unless allProps
    prop = String(Math.floor(Math.random() * 1000000))
    newProp = document.createElement 'LI'
    newProp.className = 'form_group'
    label = document.createElement 'LABEL'
    label.setAttribute 'for', "prop_#{prop}_field"
    label.textContent = 'Proposal '
    newProp.appendChild label
    input = document.createElement 'INPUT'

    input.setAttribute 'id', "prop_#{prop}_field"
    input.setAttribute 'data-prop-id', prop
    newProp.appendChild input
    if @poll_type == 'yesno'
      @make_question_input input
    else

      @make_date_input input
      @add_time_input input
    if allProps.children.length > 0
      input.setAttribute('required', 'required')




      newProp.appendChild @create_remove_link()











    allProps.appendChild(newProp)

window['currentSurveyManager'] = null
document.addEventListener 'DOMContentLoaded', ->
  cp = document.getElementById('poll_form')
  return unless cp?
  window.currentSurveyManager = new SurveyManager(cp)
  window.currentSurveyManager.insert_template()

Changes to views/form.slim.

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
        option value='date' selected=(@poll_type == 'date') Date poll
    div.form_group
      label
        input type='checkbox' name='maybe' value='yes'
        | Add a `maybe` option
  fieldset
    ol#survey_fields
      li.form_group
        label> for='prop1field' Proposal
        input#prop1field.prop-field type='text' name='proposals[][label]' required=true
    div.add_prop
      input#new_prop type='button' value='Add a new proposal'
  fieldset
    div.form_group.submit
      input type='submit'







<
<
<





16
17
18
19
20
21
22



23
24
25
26
27
        option value='date' selected=(@poll_type == 'date') Date poll
    div.form_group
      label
        input type='checkbox' name='maybe' value='yes'
        | Add a `maybe` option
  fieldset
    ol#survey_fields



    div.add_prop
      input#new_prop type='button' value='Add a new proposal'
  fieldset
    div.form_group.submit
      input type='submit'