This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathKDFormView.coffee
More file actions
executable file
·139 lines (113 loc) · 3.92 KB
/
Copy pathKDFormView.coffee
File metadata and controls
executable file
·139 lines (113 loc) · 3.92 KB
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
class KDFormView extends KDView
@findChildInputs = (parent)->
inputs = []
subViews = parent.getSubViews()
if subViews.length > 0
subViews.forEach (subView)->
inputs.push subView if subView instanceof KDInputView
inputs = inputs.concat KDFormView.findChildInputs subView
return inputs
###
INSTANCE LEVEL
###
constructor:(options,data)->
options = $.extend
callback : noop # a Function
customData : {} # an Object of key/value pairs
,options
super options,data
@valid = null
@setCallback options.callback
@customData = {}
childAppended:(child)->
child.associateForm? @
if child instanceof KDInputView
@propagateEvent KDEventType: 'inputWasAdded', child
super
bindEvents:()->
@getDomElement().bind "submit",(event)=>
@handleEvent event
super()
setDomElement:()->
cssClass = @getOptions().cssClass ? ""
@domElement = $ "<form class='kdformview #{cssClass}'></form>"
getCustomData:(path)->
if path
JsPath.getAt @customData, path
else
@customData
addCustomData:(path, value)->
if 'string' is typeof path
JsPath.setAt @customData, path, value
else
for own key, value of path
JsPath.setAt @customData, key, value
removeCustomData:(path)->
path = path.split '.' if 'string' is typeof path
[pathUntil..., last] = path
isArrayElement = not isNaN +last
if isArrayElement
JsPath.spliceAt @customData, pathUntil, last
else
JsPath.deleteAt @customData, path
serializeFormData:(data={})->
for inputData in @getDomElement().serializeArray()
data[inputData.name] = inputData.value
data
getData: ->
formData = $.extend {},@getCustomData()
@serializeFormData formData
formData
getFormData: ->
inputs = KDFormView.findChildInputs @
formData = @getCustomData() or {}
inputs.forEach (input)->
formData[input.getName()] = input.getValue() if input.getName()
formData
focusFirstElement:-> KDFormView.findChildInputs(@)[0].$().trigger "focus"
setCallback:(callback)-> @formCallback = callback
getCallback:()-> @formCallback
reset:-> @$()[0].reset()
submit:(event)->
if event
event.stopPropagation()
event.preventDefault()
form = @
inputs = KDFormView.findChildInputs @
validationCount = 0
toBeValidatedInputs = []
validInputs = []
formData = @getCustomData() or {}
@once "FormValidationFinished", (isValid = yes)->
form.valid = isValid
if isValid
form.getCallback()?.call form, formData, event
form.emit "FormValidationPassed"
else
form.emit "FormValidationFailed"
# put to be validated inputs in a queue
inputs.forEach (input)->
if input.getOptions().validate
toBeValidatedInputs.push input
else
# put regular input values to formdata
formData[input.getName()] = input.getValue() if input.getName()
toBeValidatedInputs.forEach (inputToBeValidated)->
# wait for the validation result of each input
do ->
inputToBeValidated.once "ValidationResult", (result)->
validationCount++
validInputs.push inputToBeValidated if result
# check if all inputs were validated
if toBeValidatedInputs.length is validationCount
# check if all inputs were valid
if validInputs.length is toBeValidatedInputs.length
# put valid inputs to formdata
formData[input.getName()] = input.getValue() for input in validInputs
else
valid = no
# tell validation was finished
form.emit "FormValidationFinished", valid
inputToBeValidated.validate null, event
# if no validation is required mimic as all were validated
form.emit "FormValidationFinished" if toBeValidatedInputs.length is 0