-
Notifications
You must be signed in to change notification settings - Fork 0
/
_form-script.js
323 lines (198 loc) · 6.63 KB
/
_form-script.js
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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<script>
var pef = {
// the DOM element
form:{},
submit_wrapper:{},
// my representation
fields:{
},
total: 0
}
var nav_buttons = '<div class="pef-nav"><div class="pef-back"><</div><div class="pef-forward">></div></div>'
// console inits
var jq = document.createElement('script');
document.getElementsByTagName('head')[0].appendChild(jq);
jq.onload=function(){
//init_pef()
}
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
// wait for jquery / doc ready
// setTimeout(function(){
function init_pef(){
$(document.head).append('<link rel="stylesheet" href="https://okonno.com/serv/pendleton/style.css?v=4">');
pef.form = $('form')
pef.submit_wrapper = pef.form.find('.form-button-wrapper')
pef.form.find('.form-item').each(function(){
// var options = return_options($(this))
if($(this).hasClass('checkbox')){
add_to_pef($(this))
}
add_nav($(this))
})
pef.submit_wrapper.addClass('pef-hidden')
render_page('direct', pef.form.find('.form-item').first())
add_nav(pef.submit_wrapper)
pef.form.on('click', '.pef-forward', function(){
render_page('f', $(this).parents().eq(1))
// console.log($(this).parents().eq(1))
})
pef.form.on('click', '.pef-back', function(){
render_page('b', $(this).parents().eq(1))
})
pef.form.on('change', 'input', function(){
var field = $(this).attr('data-item-field')
var option = $(this).attr('data-item-option')
var options = pef.fields[field].options
pef.fields[field].options[option].checked = $(this).prop('checked')
var field_total = 0
var cur_total = 0
for(o in options){
if(options[o].checked){
field_total += options[o].price
}
}
pef.fields[field].total = field_total
for(f in pef.fields){
cur_total += pef.fields[f].total
// console.log('added: ', pef.fields[f].total)
}
// pef.fields[field].total
$(this).parents().eq(2).find('.pef-field-price span').html(cur_total)
})
console.log('init pef: ', pef)
}
//, 2500)
function add_to_pef($form_item){
// var c = {}
var title = $form_item.find('.title').text().replace(/\* /g, '').replace(/\*/g, '')
title = title.replace(/ $/g, '')
var attr_title = title.replace(/ /g, '_')
pef.fields[attr_title] = {}
pef.fields[attr_title].options = {}
pef.fields[attr_title].total = 0
pef.fields[attr_title].original = title
$form_item.append('<div class="pef-field-price">Current estimate: <span>0</span></div>')
$form_item.attr('data-field-total', 0)
$form_item.find('.option input').each(function(){
// var opt = {}
var opt_title = this.value
// when ready
// var price = opt_title.substr(opt_title.match(/\$/).index).replace(/ /g, '').replace(/\$/g, '');
var price = 1000
var opt_attr_title = this.value.replace(/ /g, '_')
// for targeting when clicked
$(this).attr({
'data-item-field': attr_title,
'data-item-option': opt_attr_title
})
pef.fields[attr_title].options[opt_attr_title] = {}
pef.fields[attr_title].options[opt_attr_title].price = price
pef.fields[attr_title].options[opt_attr_title].checked = false
pef.fields[attr_title].options[opt_attr_title].original = opt_title
})
}
function add_nav($ele){
$ele.append(nav_buttons)
}
function render_page(d, $page){
var $nav_to_page = 'none'
if($page.hasClass('form-button-wrapper')){
switch(d){
case 'f':
// never happens
console.log('invalid f nav')
break;
case 'b':
pef.form.find('.form-item').addClass('pef-hidden')
pef.submit_wrapper.addClass('pef-hidden')
pef.form.find('.form-item').last().removeClass('pef-hidden')
break;
default:
console.log('invalid nav')
break;
}
}else{
$('#pef-total-display').remove()
switch(d){
case 'f':
if($page.next().length>0){
pef.form.find('.form-item').addClass('pef-hidden')
pef.submit_wrapper.addClass('pef-hidden')
$page.next().removeClass('pef-hidden')
$nav_to_page = $page.next()
}else{
pef.form.find('.form-item').addClass('pef-hidden')
pef.submit_wrapper.prepend(display_totals())
pef.submit_wrapper.removeClass('pef-hidden')
pef.submit_wrapper.find('.pef-forward').addClass('pef-hidden')
}
// pef.form.first().find('.pef-back').removeClass('pef-hidden')
break;
case 'b':
if($page.prev().length>0){
pef.form.find('.form-item').addClass('pef-hidden')
pef.submit_wrapper.addClass('pef-hidden')
$page.prev().removeClass('pef-hidden')
$nav_to_page = $page.prev()
if($page.prev().prev().length<1){
pef.form.find('.form-item').first().find('.pef-back').addClass('pef-hidden')
}
// extra but works..
pef.submit_wrapper.find('.pef-forward').addClass('pef-hidden')
}else{
pef.form.first().find('.pef-back').addClass('pef-hidden')
}
// pef.submit_wrapper.find('.pef-forward').removeClass('pef-hidden')
break;
case 'direct':
pef.form.find('.form-item').addClass('pef-hidden')
pef.submit_wrapper.addClass('pef-hidden')
$page.removeClass('pef-hidden')
$nav_to_page = $page
if($page.prev().length<1){
pef.form.find('.form-item').first().find('.pef-back').addClass('pef-hidden')
}
break;
default:
break;
}
// current total
var cur_total = 0
for(f in pef.fields){
cur_total += pef.fields[f].total
// console.log('field totals at page render: ' + pef.fields[f].total)
}
if($nav_to_page!='none'){
$nav_to_page.parents().eq(2).find('.pef-field-price span').html(cur_total)
}
}
}
function display_totals(){
var totals = document.createElement('div')
$(totals).attr('id', "pef-total-display")
var total = 0
for(field in pef.fields){
var the_field = pef.fields[field]
// var field_total = 0
field_result = document.createElement('div')
field_result.class='pef-totals-field'
field_result.id='pef-totals-'+field
field_result.innerHTML = '<div class="pef-totals-title">' + the_field.original + '</div>'
for(option in the_field.options){
var new_div = document.createElement('div')
if(the_field.options.hasOwnProperty(option) && the_field.options[option].checked){
new_div.innerHTML = '<div class="pef-totals-option">' + the_field.options[option].original + ': <span class="pef-option-price">' + the_field.options[option].price + '</span></div>'
$(field_result).append(new_div)
// field_total += the_field.options[option].price
}
}
// $(field_result).append('field total: ' + field_total)
field_result.append('field total: ' + the_field.total)
$(totals).append(field_result)
// total += field_total
total += the_field.total
}
$(totals).append('<div class="pef-form-total">Current estimate: $ '+ total + '</div>')
return totals
}
</script>