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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/schedulable/form_helper.rb', line 27
def schedule_select(attribute, input_options = {})
template = @template
weekdays = Date::DAYNAMES.map(&:downcase)
weekdays = weekdays.slice(1..7) << weekdays.slice(0)
day_names = I18n.t('date.day_names', default: "")
day_names = day_names.blank? ? weekdays.map { |day| day.capitalize } : day_names.slice(1..7) << day_names.slice(0)
day_labels = Hash[weekdays.zip(day_names)]
month_names = I18n.t('date.month_names', default: "")
month_names = month_names.blank? ? Date::MONTHNAMES : month_names
date_order = I18n.t('date.order', default: [:year, :month, :day])
date_order = date_order.map { |order|
order.to_sym
}
date_options = {
order: date_order,
use_month_names: month_names
}
config_options = Schedulable.config.form_helper.present? ? Schedulable.config.form_helper : {style: :default}
input_options = config_options.merge(input_options)
if input_options[:style].is_a?(Symbol) || input_options[:style].is_a?(String)
style_options = STYLES.has_key?(input_options[:style]) ? STYLES[input_options[:style]] : STYLES[:default]
elsif input_options[:style].is_a?(Hash)
style_options = input_options[:style]
else
style_options = STYLES[:default]
end
style_options = style_options.merge(input_options)
style_options[:field_html]||= {}
style_options[:label_html]||= {}
style_options[:label_wrapper]||= {}
style_options[:input_html]||= {}
style_options[:input_wrapper]||= {}
style_options[:number_field_html]||= {}
style_options[:number_field_wrapper]||= {}
style_options[:date_select_html]||= {}
style_options[:date_select_wrapper]||= {}
style_options[:collection_select_html]||= {}
style_options[:collection_select_wrapper]||= {}
style_options[:collection_check_boxes_item_html]||= {}
style_options[:collection_check_boxes_item_wrapper]||= {}
style_options[:number_field_html] = style_options[:input_html].merge(style_options[:number_field_html])
style_options[:number_field_wrapper] = style_options[:input_wrapper].merge(style_options[:number_field_wrapper])
style_options[:date_select_html] = style_options[:input_html].merge(style_options[:date_select_html])
style_options[:date_select_wrapper] = style_options[:input_wrapper].merge(style_options[:date_select_wrapper])
style_options[:collection_select_html] = style_options[:input_html].merge(style_options[:collection_select_html])
style_options[:collection_select_wrapper] = style_options[:input_wrapper].merge(style_options[:collection_select_wrapper])
style_options[:collection_check_boxes_item_html] = style_options[:input_html].merge(style_options[:collection_check_boxes_item_html])
style_options[:collection_check_boxes_item_wrapper] = style_options[:input_wrapper].merge(style_options[:collection_check_boxes_item_wrapper])
field_id = @object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/,"_").sub(/_$/,"") + "_" + attribute.to_s
@template.content_tag("div", {id: field_id}) do
self.fields_for(attribute, @object.send(attribute.to_s) || @object.send("build_" + attribute.to_s)) do |f|
@template.content_tag("div", style_options[:field_html]) do
select_output = f.collection_select(:rule, ['singular', 'daily', 'weekly', 'monthly'], lambda { |v| return v}, lambda { |v| I18n.t("schedulable.rules.#{v}", default: v.capitalize) }, {include_blank: false}, style_options[:collection_select_html])
content_wrap(@template, select_output, style_options[:collection_select_wrapper])
end <<
@template.content_tag("div", style_options[:field_html].merge({data: {group: 'singular'}})) do
content_wrap(@template, f.label(:date, style_options[:label_html]), style_options[:label_wrapper]) <<
content_wrap(@template, f.date_select(:date, date_options, style_options[:date_select_html]), style_options[:date_select_wrapper])
end <<
@template.content_tag("div", style_options[:field_html].merge({data: {group: 'weekly'}})) do
content_wrap(@template, f.label(:day), style_options[:label_wrapper]) <<
f.collection_check_boxes(:day, weekdays, lambda { |v| return v}, lambda { |v| (" " + day_labels[v]).html_safe}) do |cb|
check_box_output = cb.check_box(style_options[:collection_check_boxes_item_html])
text = cb.text
nested_output = cb.label({}) do |l|
check_box_output + text
end
content_wrap(@template, nested_output, style_options[:collection_check_boxes_item_wrapper])
end
end <<
@template.content_tag("div", style_options[:field_html].merge({data: {group: 'monthly'}})) do
f.fields_for :day_of_week, OpenStruct.new(f.object.day_of_week || {}) do |db|
content_wrap(@template, f.label(:day_of_week), style_options[:label_wrapper]) <<
@template.content_tag("div", nil, style: 'min-width: 280px; display: table') do
@template.content_tag("div", nil, style: 'display: table-row') do
@template.content_tag("span", nil, style: 'display: table-cell;') <<
['1st', '2nd', '3rd', '4th', 'last'].reduce(''.html_safe) { | content, item |
content << @template.content_tag("span", I18n.t("schedulable.monthly_week_names.#{item}", default: item.to_s), style: 'display: table-cell; text-align: center')
}
end <<
weekdays.reduce(''.html_safe) do | content, weekday |
content << @template.content_tag("div", nil, style: 'display: table-row') do
@template.content_tag("span", day_labels[weekday] || weekday, style: 'display: table-cell') <<
db.collection_check_boxes(weekday.to_sym, [1, 2, 3, 4, -1], lambda { |i| i} , lambda { |i| " ".html_safe}, checked: db.object.send(weekday)) do |cb|
@template.content_tag("span", style: 'display: table-cell; text-align: center') { cb.check_box() }
end
end
end
end
end
end <<
@template.content_tag("div", style_options[:field_html].merge({data: {group: 'singular,daily,weekly,monthly'}})) do
content_wrap(@template, f.label(:time, style_options[:label_html]), style_options[:label_wrapper]) <<
content_wrap(@template, f.time_select(:time, date_options, style_options[:date_select_html]), style_options[:date_select_wrapper])
end <<
(if input_options[:interval]
@template.content_tag("div", style_options[:field_html].merge({data: {group: 'daily,weekly,monthly'}})) do
content_wrap(@template, f.label(:interval, style_options[:label_html]), style_options[:label_wrapper]) <<
content_wrap(@template, f.number_field(:interval, style_options[:number_field_html]), style_options[:number_field_wrapper])
end
else
f.hidden_field(:interval, value: 1)
end) <<
(if input_options[:until]
@template.content_tag("div", style_options[:field_html].merge({data: {group: 'daily,weekly,monthly'}})) do
content_wrap(@template, f.label(:until, style_options[:label_html]), style_options[:label_wrapper]) <<
content_wrap(@template, f.datetime_select(:until, date_options, style_options[:date_select_html]), style_options[:date_select_wrapper])
end
else
f.hidden_field(:until, value: nil)
end) <<
if input_options[:count]
@template.content_tag("div", style_options[:field_html].merge({data: {group: 'daily,weekly,monthly'}})) do
content_wrap(@template, f.label(:count, style_options[:label_html]), style_options[:label_wrapper]) <<
content_wrap(@template, f.number_field(:count, style_options[:number_field_html]), style_options[:number_field_wrapper])
end
else
f.hidden_field(:count, value: 0)
end
end
end <<
template.javascript_tag(
"(function() {" <<
" var container = document.querySelectorAll('##{field_id}'); container = container[container.length - 1]; " <<
" var select = container.querySelector(\"select[name*='rule']\"); " <<
" function update() {" <<
" var value = this.value;" <<
" [].slice.call(container.querySelectorAll(\"*[data-group]\")).forEach(function(elem) { " <<
" var groups = elem.getAttribute('data-group').split(',');" <<
" if (groups.indexOf(value) >= 0) {" <<
" elem.style.display = ''" <<
" } else {" <<
" elem.style.display = 'none'" <<
" }" <<
" });" <<
" }" <<
" if (jQuery) { jQuery(select).on('change', update); } else { select.addEventListener('change', update); }" <<
" update.call(select);" <<
"})()"
)
end
|