Paul: Check-in [f9c38e4f0a]

Online event coordination and survey application

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

Overview
Comment:Add support for multiple time per day
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:f9c38e4f0a2d5580e957d67c160cb016895a2cb959147e1e7508af8c71d48a4b
User & Date: milouse 2018-11-19 22:29:09
Context
2018-12-26
08:56
Upgrade ruby version check-in: 2ef7dcd6f6 user: milouse tags: trunk
2018-11-19
22:29
Add support for multiple time per day check-in: f9c38e4f0a user: milouse tags: trunk
20:52
Add moment.js and some default values check-in: 8e47d9ffd1 user: milouse tags: trunk
Changes

Changes to assets/coffee/main.coffee.

21
22
23
24
25
26
27
28
29




















30
31
32
33
34
35











36
37
38
39
40
41
42
43
44
..
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
..
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    input.className = 'prop-field'

  make_date_input: (input, cur_date) ->
    input['oldTextValue'] = input.value
    cur_date ?= moment().format('YYYY-MM-DD')
    input.value = input['oldDateValue'] ? cur_date
    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)
................................................................................
  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'
................................................................................
    if @poll_type == 'yesno'
      @make_question_input input
    else
      last_date = null
      ldd = allProps.lastElementChild?.querySelector('input[type=date]')
      if ldd?
        last_date = moment(ldd.value, 'YYYY-MM-DD') \
                    .add(1, 'days').format('YYYY-MM-DD')
      @make_date_input input, last_date
      @add_time_input input
    if allProps.children.length > 0
      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()







|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|

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







 







<







 







|

<










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
..
89
90
91
92
93
94
95

96
97
98
99
100
101
102
...
121
122
123
124
125
126
127
128
129

130
131
132
133
134
135
136
137
138
139
    input.className = 'prop-field'

  make_date_input: (input, cur_date) ->
    input['oldTextValue'] = input.value
    cur_date ?= moment().format('YYYY-MM-DD')
    input.value = input['oldDateValue'] ? cur_date
    input.setAttribute 'type', 'date'
    input.setAttribute 'name', 'proposals[date][]'
    input.className = 'prop-field'
    input.addEventListener 'change', (event) ->
      event.preventDefault()
      event.stopPropagation()
      date_rel = event.target
      time_els = date_rel.parentElement.querySelectorAll \
        'input.prop-time-field[type=time]'
      for te in time_els
        te.setAttribute 'name', "proposals[#{date_rel.value}][]"
    add_time = document.createElement 'INPUT'
    add_time.type = 'button'
    add_time.value = '⏰'
    add_time.title = 'Add time'
    add_time.className = 'add-time-prop'
    add_time.addEventListener 'click', (event) ->
      event.preventDefault()
      event.stopPropagation()
      date_rel = event.target.parentElement \
        .querySelector('input.prop-field[type=date]')
      currentSurveyManager.add_time_input(date_rel) if date_rel?
    input.parentNode.insertBefore(add_time, input.nextElementSibling)

  add_time_input: (date_rel) ->
    input = document.createElement 'INPUT'
    input.setAttribute 'type', 'time'
    input.setAttribute 'name', "proposals[#{date_rel.value}][]"
    input.className = 'prop-time-field'
    atf = date_rel.parentElement.querySelectorAll \
      'input.prop-time-field[type=time]'
    last_val = atf[atf.length - 1]
    if last_val?
      sibling = last_val
      last_val = moment(last_val.value, 'HH:mm') \
        .add(1, 'hours').format('HH:mm')

    else
      sibling = date_rel
      last_val = moment().format('HH:mm')
    input.value = date_rel['oldTimeValue'] ? last_val
    date_rel.parentNode.insertBefore(input, sibling.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)
................................................................................
  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

        continue
      continue if old_type == 'text'
      @make_question_input p
      @remove_time_input p

  remove_template: (elem) ->
    allProps = document.getElementById 'survey_fields'
................................................................................
    if @poll_type == 'yesno'
      @make_question_input input
    else
      last_date = null
      ldd = allProps.lastElementChild?.querySelector('input[type=date]')
      if ldd?
        last_date = moment(ldd.value, 'YYYY-MM-DD') \
          .add(1, 'days').format('YYYY-MM-DD')
      @make_date_input input, last_date

    if allProps.children.length > 0
      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 lib/survey.rb.

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
    @data[:email] = opts['email']
    @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'
    extract_proposals(opts)
  end

  def extract_proposals(opts)
    @data[:proposals] ||= []
    opts['proposals'].each_with_index do |prop, idx|
      new_prop = { id: idx + 1 }
      if prop.is_a? String
        new_prop[:label] = prop
      elsif prop.has_key?('label')
        new_prop[:label] = prop['label']
      elsif prop.has_key?('date')
        loc_time = '00:00'
        loc_time = prop['time'] if prop['time'] && prop['time'] != ''
        new_prop[:label] = [prop['date'], loc_time].join(' ')
      end
      @data[:proposals] << new_prop
    end





















    @data
  end
end







|
<






<
<
<
<



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



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
104
105
106
107
108
    @data[:email] = opts['email']
    @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'
    extract_proposals(opts)
  end

  def extract_questions(opts)

    opts['proposals'].each_with_index do |prop, idx|
      new_prop = { id: idx + 1 }
      if prop.is_a? String
        new_prop[:label] = prop
      elsif prop.has_key?('label')
        new_prop[:label] = prop['label']




      end
      @data[:proposals] << new_prop
    end
  end

  def extract_dates(opts)
    return unless opts['proposals'].has_key? 'date'
    opts['proposals']['date'].each do |date|
      next unless opts['proposals'].has_key? date
      opts['proposals'][date].each do |time|
        next if time.nil? || time == ''
        dt_str = [date, time].join(' ')
        @data[:proposals] << { id: dt_str, label: dt_str }
      end
    end
  end

  def extract_proposals(opts)
    @data[:proposals] ||= []
    if opts['proposals'].is_a? Hash
      extract_dates opts
    else
      extract_questions opts
    end
    @data
  end
end