Paul: Check-in [d72c6fa209]

Online event coordination and survey application

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

Overview
Comment:feat: Continue work on survey creation
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:d72c6fa2091e0c298779e237c54312f11803c68cab549892d63f66b3e582b70e
User & Date: milouse 2018-10-23 11:50:15
Context
2018-10-24
19:03
feat: Add mail and sqlite3 support check-in: 6286f96edf user: milouse tags: trunk
2018-10-23
11:50
feat: Continue work on survey creation check-in: d72c6fa209 user: milouse tags: trunk
07:19
chore: Upgrade npm check-in: c47e7ac78b user: milouse tags: trunk
Changes

Changes to assets/coffee/main.coffee.

1
2
3
4
5
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
class SurveyManager
  constructor: (@poll_form) ->
    pt = @poll_form.elements.poll_type
    return null unless pt?
    nb = document.getElementById('new_props')
    return null unless nb?
    nb.addEventListener 'click', (event) ->
      currentSurveyManager.insert_template()
    @poll_type = pt.value
    pt.addEventListener 'change', (event) ->
      currentSurveyManager.poll_type = event.target.value



































  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', "props#{prop}field")
    label.textContent = 'Proposal '
    newProp.appendChild label
    input = document.createElement('INPUT')

    input.setAttribute('type', 'text')
    input.setAttribute('name', 'proposals[]')


    input.setAttribute('id', "props#{prop}field")







    newProp.appendChild input

    remLink = document.createElement('input')
    remLink.type = 'button'
    remLink.value = '×'
    remLink.title = 'Remove proposal'

    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)




|





|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>











|

|
|


|
>
|
|
>
>
|
>
>
>
>
>
>
>

>
|



>












1
2
3
4
5
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
class SurveyManager
  constructor: (@poll_form) ->
    pt = @poll_form.elements.poll_type
    return null unless pt?
    nb = document.getElementById 'new_prop'
    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)

Changes to lib/survey.rb.

65
66
67
68
69
70
71
72

73







74
75
76
  def extract_data(opts)
    @data[:title] = opts['title'] if opts.has_key? 'title'
    @data[:author] = opts['author'] if opts.has_key? 'author'
    @data[:poll_type] = opts['poll_type'] || 'yesno'
    @data[:maybe] = opts['maybe'].to_s.to_bool if opts.has_key? 'maybe'
    return unless opts.has_key? 'proposals'
    @data[:proposals] ||= []
    opts['proposals'].each do |p|

      @data[:proposals] << p







    end
  end
end







|
>
|
>
>
>
>
>
>
>



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  def extract_data(opts)
    @data[:title] = opts['title'] if opts.has_key? 'title'
    @data[:author] = opts['author'] if opts.has_key? 'author'
    @data[:poll_type] = opts['poll_type'] || 'yesno'
    @data[:maybe] = opts['maybe'].to_s.to_bool if opts.has_key? 'maybe'
    return unless opts.has_key? 'proposals'
    @data[:proposals] ||= []
    opts['proposals'].each do |prop|
      if prop.is_a? String
        @data[:proposals] << prop
      elsif prop.has_key?('label')
        @data[:proposals] << prop['label']
      elsif prop.has_key?('date')
        loc_time = '00:00'
        loc_time = prop['time'] if prop['time'] && prop['time'] != ''
        @data[:proposals] << [prop['date'], loc_time].join(' ')
      end
    end
  end
end

Changes to paul.rb.

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
      end
      uri << "#{uri}#{request.script_name}" if request.script_name != ''
      "#{uri}/p/#{survey.id}"
    end
  end

  post '/create' do
    @poll = Survey.new(params)
    @poll.save
    @title = @poll.data[:title]
    slim :poll
  end

  get '/p/:poll_id' do
    begin
      @poll = Survey.new(params[:poll_id])
    rescue NameError => e
      logger.error e







|
|
|
<







43
44
45
46
47
48
49
50
51
52

53
54
55
56
57
58
59
      end
      uri << "#{uri}#{request.script_name}" if request.script_name != ''
      "#{uri}/p/#{survey.id}"
    end
  end

  post '/create' do
    p = Survey.new(params)
    p.save
    redirect "/p/#{p.id}"

  end

  get '/p/:poll_id' do
    begin
      @poll = Survey.new(params[:poll_id])
    rescue NameError => e
      logger.error e

Changes to views/form.slim.

14
15
16
17
18
19
20
21
22
23
24
25
26
27
    div.form_group
      label
        input type='checkbox' name='maybe' value='yes'
        | Add a `maybe` option
  fieldset
    ol#survey_fields
      li.form_group
        label> for='props1field' Proposal
        input#props1field type='text' name='proposals[]' required=true
    div.add_props
      input#new_props type='button' value='Add a new proposal'
  fieldset
    div.form_group.submit
      input type='submit'







|
|
|
|



14
15
16
17
18
19
20
21
22
23
24
25
26
27
    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'