romo 0.19.10 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,103 +1,104 @@
1
- $.fn.romoDropdownForm = function() {
2
- return $.map(this, function(element) {
3
- return new RomoDropdownForm(element);
4
- });
5
- }
1
+ var RomoDropdownForm = RomoComponent(function(elem) {
2
+ this.elem = elem;
6
3
 
7
- var RomoDropdownForm = function(element) {
8
- this.elem = $(element);
4
+ this.doInit();
5
+ this._bindElem()
9
6
 
10
- this.dropdown = this.elem.romoDropdown()[0];
11
- this.doBindDropdown();
7
+ Romo.trigger(this.elem, 'romoDropdownForm:ready', [this]);
8
+ });
12
9
 
13
- this.form = undefined;
14
- this.elem.on('dropdownForm:form:triggerSubmit', $.proxy(function(e) {
15
- if (this.form != undefined) {
16
- this.form.elem.trigger('form:triggerSubmit', []);
10
+ // private
11
+
12
+ RomoDropdownForm.prototype._bindElem = function() {
13
+ Romo.on(this.elem, 'romoDropdownForm:romoForm:triggerSubmit', Romo.proxy(function(e) {
14
+ if (this.romoForm !== undefined) {
15
+ Romo.trigger(this.romoForm.elem, 'romoForm:triggerSubmit', []);
17
16
  }
18
17
  }, this));
19
- this.doBindForm();
20
- this.elem.on('dropdown:loadBodySuccess', $.proxy(function(e, data, dropdown) {
21
- this.doBindForm();
22
- this.elem.trigger('dropdownForm:formReady', [this.form, this]);
18
+ Romo.on(this.elem, 'romoDropdown:loadBodySuccess', Romo.proxy(function(e, data, romoDropdown) {
19
+ this._bindForm();
20
+ Romo.trigger(this.elem, 'romoDropdownForm:formReady', [this.romoForm, this]);
23
21
  }, this));
24
22
 
25
- this.doInit();
26
- this.elem.trigger('dropdownForm:ready', [this]);
23
+ this._bindDropdown();
24
+ this._bindForm();
27
25
  }
28
26
 
29
- RomoDropdownForm.prototype.doInit = function() {
30
- // override as needed
31
- }
27
+ RomoDropdownForm.prototype._bindDropdown = function() {
28
+ this.romoDropdown = new RomoDropdown(this.elem);
32
29
 
33
- RomoDropdownForm.prototype.doBindDropdown = function() {
34
- if (this.elem.data('romo-dropdown-clear-content') === undefined) {
35
- this.elem.attr('data-romo-dropdown-clear-content', 'true');
30
+ if (Romo.data(this.elem, 'romo-dropdown-clear-content') === undefined) {
31
+ Romo.setData(this.elem, 'romo-dropdown-clear-content', 'true');
36
32
  }
37
33
 
38
- this.elem.on('dropdown:ready', $.proxy(function(e, dropdown) {
39
- this.elem.trigger('dropdownForm:dropdown:ready', [dropdown, this]);
34
+ Romo.on(this.elem, 'romoDropdown:ready', Romo.proxy(function(e, romoDropdown) {
35
+ Romo.trigger(this.elem, 'romoDropdownForm:romoDropdown:ready', [romoDropdown, this]);
40
36
  }, this));
41
- this.elem.on('dropdown:toggle', $.proxy(function(e, dropdown) {
42
- this.elem.trigger('dropdownForm:dropdown:toggle', [dropdown, this]);
37
+ Romo.on(this.elem, 'romoDropdown:toggle', Romo.proxy(function(e, romoDropdown) {
38
+ Romo.trigger(this.elem, 'romoDropdownForm:romoDropdown:toggle', [romoDropdown, this]);
43
39
  }, this));
44
- this.elem.on('dropdown:popupOpen', $.proxy(function(e, dropdown) {
45
- this.elem.trigger('dropdownForm:dropdown:popupOpen', [dropdown, this]);
40
+ Romo.on(this.elem, 'romoDropdown:popupOpen', Romo.proxy(function(e, romoDropdown) {
41
+ Romo.trigger(this.elem, 'romoDropdownForm:romoDropdown:popupOpen', [romoDropdown, this]);
46
42
  }, this));
47
- this.elem.on('dropdown:popupClose', $.proxy(function(e, dropdown) {
48
- this.elem.trigger('dropdownForm:dropdown:popupClose', [dropdown, this]);
43
+ Romo.on(this.elem, 'romoDropdown:popupClose', Romo.proxy(function(e, romoDropdown) {
44
+ Romo.trigger(this.elem, 'romoDropdownForm:romoDropdown:popupClose', [romoDropdown, this]);
49
45
  }, this));
50
- this.elem.on('dropdown:loadBodyStart', $.proxy(function(e, dropdown) {
51
- this.elem.trigger('dropdownForm:dropdown:loadBodyStart', [dropdown, this]);
46
+ Romo.on(this.elem, 'romoDropdown:loadBodyStart', Romo.proxy(function(e, romoDropdown) {
47
+ Romo.trigger(this.elem, 'romoDropdownForm:romoDropdown:loadBodyStart', [romoDropdown, this]);
52
48
  }, this));
53
- this.elem.on('dropdown:loadBodySuccess', $.proxy(function(e, data, dropdown) {
54
- this.elem.trigger('dropdownForm:dropdown:loadBodySuccess', [data, dropdown, this]);
49
+ Romo.on(this.elem, 'romoDropdown:loadBodySuccess', Romo.proxy(function(e, data, romoDropdown) {
50
+ Romo.trigger(this.elem, 'romoDropdownForm:romoDropdown:loadBodySuccess', [data, romoDropdown, this]);
55
51
  }, this));
56
- this.elem.on('dropdown:loadBodyError', $.proxy(function(e, xhr, dropdown) {
57
- this.elem.trigger('dropdownForm:dropdown:loadBodyError', [xhr, dropdown, this]);
52
+ Romo.on(this.elem, 'romoDropdown:loadBodyError', Romo.proxy(function(e, xhr, romoDropdown) {
53
+ Romo.trigger(this.elem, 'romoDropdownForm:romoDropdown:loadBodyError', [xhr, romoDropdown, this]);
58
54
  }, this));
59
- this.elem.on('dropdown:dismiss', $.proxy(function(e, dropdown) {
60
- this.elem.trigger('dropdownForm:dropdown:dismiss', [dropdown, this]);
55
+ Romo.on(this.elem, 'romoDropdown:dismiss', Romo.proxy(function(e, romoDropdown) {
56
+ Romo.trigger(this.elem, 'romoDropdownForm:romoDropdown:dismiss', [romoDropdown, this]);
61
57
  }, this));
62
58
  }
63
59
 
64
- RomoDropdownForm.prototype.doBindForm = function() {
65
- var formElem = this.dropdown.popupElem.find('[data-romo-form-auto="dropdownForm"]');
60
+ RomoDropdownForm.prototype._bindForm = function() {
61
+ this.romoForm = undefined;
62
+ var formElem = Romo.find(this.romoDropdown.popupElem, '[data-romo-form-auto="dropdownForm"]')[0];
66
63
 
67
- formElem.on('form:clearMsgs', $.proxy(function(e, form) {
68
- this.elem.trigger('dropdownForm:form:clearMsgs', [form, this]);
69
- }, this));
70
- formElem.on('form:ready', $.proxy(function(e, form) {
71
- this.elem.trigger('dropdownForm:form:ready', [form, this]);
72
- }, this));
73
- formElem.on('form:confirmSubmit', $.proxy(function(e, form) {
74
- this.elem.trigger('dropdownForm:form:confirmSubmit', [form, this]);
75
- }, this));
76
- formElem.on('form:beforeSubmit', $.proxy(function(e, form) {
77
- this.elem.trigger('dropdownForm:form:beforeSubmit', [form, this]);
78
- }, this));
79
- formElem.on('form:submitSuccess', $.proxy(function(e, data, form) {
80
- this.elem.trigger('dropdownForm:form:submitSuccess', [data, form, this]);
81
- }, this));
82
- formElem.on('form:submitInvalidMsgs', $.proxy(function(e, msgs, xhr, form) {
83
- this.elem.trigger('dropdownForm:form:submitInvalidMsgs', [msgs, xhr, form, this]);
84
- }, this));
85
- formElem.on('form:submitXhrError', $.proxy(function(e, xhr, form) {
86
- this.elem.trigger('dropdownForm:form:submitXhrError', [xhr, form, this]);
87
- }, this));
88
- formElem.on('form:submitError', $.proxy(function(e, xhr, form) {
89
- this.elem.trigger('dropdownForm:form:submitError', [xhr, form, this]);
90
- }, this));
91
- formElem.on('form:browserSubmit', $.proxy(function(e, form) {
92
- this.elem.trigger('dropdownForm:form:browserSubmit', [form, this]);
93
- }, this));
64
+ if (formElem !== undefined) {
65
+ Romo.on(formElem, 'romoForm:clearMsgs', Romo.proxy(function(e, romoForm) {
66
+ Romo.trigger(this.elem, 'romoDropdownForm:romoForm:clearMsgs', [romoForm, this]);
67
+ }, this));
68
+ Romo.on(formElem, 'romoForm:ready', Romo.proxy(function(e, romoForm) {
69
+ Romo.trigger(this.elem, 'romoDropdownForm:romoForm:ready', [romoForm, this]);
70
+ }, this));
71
+ Romo.on(formElem, 'romoForm:confirmSubmit', Romo.proxy(function(e, romoForm) {
72
+ Romo.trigger(this.elem, 'romoDropdownForm:romoForm:confirmSubmit', [romoForm, this]);
73
+ }, this));
74
+ Romo.on(formElem, 'romoForm:beforeSubmit', Romo.proxy(function(e, romoForm) {
75
+ Romo.trigger(this.elem, 'romoDropdownForm:romoForm:beforeSubmit', [romoForm, this]);
76
+ }, this));
77
+ Romo.on(formElem, 'romoForm:submitSuccess', Romo.proxy(function(e, data, romoForm) {
78
+ Romo.trigger(this.elem, 'romoDropdownForm:romoForm:submitSuccess', [data, romoForm, this]);
79
+ }, this));
80
+ Romo.on(formElem, 'romoForm:submitInvalidMsgs', Romo.proxy(function(e, msgs, xhr, romoForm) {
81
+ Romo.trigger(this.elem, 'romoDropdownForm:romoForm:submitInvalidMsgs', [msgs, xhr, romoForm, this]);
82
+ }, this));
83
+ Romo.on(formElem, 'romoForm:submitXhrError', Romo.proxy(function(e, xhr, romoForm) {
84
+ Romo.trigger(this.elem, 'romoDropdownForm:romoForm:submitXhrError', [xhr, romoForm, this]);
85
+ }, this));
86
+ Romo.on(formElem, 'romoForm:submitError', Romo.proxy(function(e, xhr, romoForm) {
87
+ Romo.trigger(this.elem, 'romoDropdownForm:romoForm:submitError', [xhr, romoForm, this]);
88
+ }, this));
89
+ Romo.on(formElem, 'romoForm:browserSubmit', Romo.proxy(function(e, romoForm) {
90
+ Romo.trigger(this.elem, 'romoDropdownForm:romoForm:browserSubmit', [romoForm, this]);
91
+ }, this));
94
92
 
95
- var submitElement = this.dropdown.popupElem.find('[data-romo-form-submit]')[0];
96
- var indicatorElements = this.dropdown.popupElem.find('[data-romo-indicator-auto="true"]');
97
- this.form = formElem.romoForm(submitElement, indicatorElements)[0];
93
+ var submitElems = Romo.find(this.romoDropdown.popupElem, '[data-romo-form-submit]');
94
+ var spinnerElems = Romo.find(this.romoDropdown.popupElem, '[data-romo-spinner-auto="true"]');
95
+
96
+ this.romoForm = new RomoForm(formElem, submitElems, spinnerElems);
97
+ }
98
98
  }
99
99
 
100
- Romo.onInitUI(function(e) {
101
- Romo.initUIElems(e, '[data-romo-dropdownForm-auto="true"]').romoDropdownForm();
102
- });
100
+ // event functions
101
+
102
+ // init
103
103
 
104
+ Romo.addElemsInitSelector('[data-romo-dropdownForm-auto="true"]', RomoDropdownForm);
@@ -1,231 +1,287 @@
1
- $.fn.romoForm = function(givenSubmitElement, givenIndicatorElements) {
2
- return $.map(this, function(element) {
3
- return new RomoForm(element, givenSubmitElement, givenIndicatorElements);
4
- });
5
- }
1
+ var RomoForm = RomoComponent(function(elem, givenSubmitElems, givenSpinnerElems) {
2
+ this.elem = elem;
3
+
4
+ var defaultSubmitElems = Romo.find(
5
+ this.elem,
6
+ 'button[type="submit"], input[type="submit"], [data-romo-form-submit]'
7
+ );
8
+ this.submitElems = (givenSubmitElems || []).concat(defaultSubmitElems || []);
6
9
 
7
- var RomoForm = function(element, givenSubmitElement, givenIndicatorElements) {
8
- this.elem = $(element);
9
- this.defaultSubmitElem = this.elem.find('button[type="submit"], input[type="submit"], [data-romo-form-submit]');
10
- this.submitElem = $(givenSubmitElement || this.defaultSubmitElem);
11
- this.defaultIndicatorElems = this.elem.find('[data-romo-indicator-auto="true"]');
12
- this.indicatorElems = $(givenIndicatorElements || this.defaultIndicatorElems);
13
- this.changeSubmitElems = this.elem.find('[data-romo-form-change-submit="true"]');
14
- this.onkeySubmitElems = this.elem.find('[data-romo-form-onkey-submit="true"]');
10
+ var defaultSpinnerElems = Romo.find(this.elem, '[data-romo-spinner-auto="true"]');
11
+ this.spinnerElems = (givenSpinnerElems || []).concat(defaultSpinnerElems || []);
15
12
 
16
- this.defaultListValuesDelim = ',';
13
+ this.changeSubmitElems = Romo.find(this.elem, '[data-romo-form-change-submit="true"]');
14
+ this.onkeySubmitElems = Romo.find(this.elem, '[data-romo-form-onkey-submit="true"]');
15
+
16
+ this.defaultListValuesDelim = ',';
17
17
  this.onkeyDefaultSubmitDelay = 300; // 0.3 secs
18
- this.submitQueued = false;
19
- this.submitRunning = false;
18
+ this.submitQueued = false;
19
+ this.submitRunning = false;
20
20
 
21
- this.removeEmptyGetParams = this.elem.data('romo-form-remove-empty-get-params')
21
+ this.removeEmptyGetParams = Romo.data(this.elem, 'romo-form-remove-empty-get-params');
22
22
  if (this.removeEmptyGetParams === undefined) {
23
23
  this.removeEmptyGetParams = true;
24
24
  }
25
25
 
26
- this.decodeParams = this.elem.data('romo-form-decode-params')
26
+ this.decodeParams = Romo.data(this.elem, 'romo-form-decode-params');
27
27
  if (this.decodeParams === undefined) {
28
28
  this.decodeParams = true;
29
29
  }
30
30
 
31
31
  this.doInit();
32
- this.doBindForm();
33
- this.elem.trigger('form:clearMsgs', [this]);
34
- this.elem.trigger('form:ready', [this]);
35
- }
36
-
37
- RomoForm.prototype.doInit = function() {
38
- // override as needed
39
- }
40
-
41
- RomoForm.prototype.doBindForm = function() {
42
- this.defaultSubmitElem.unbind('click');
43
- this.submitElem.unbind('click');
44
- this.submitElem.on('click', $.proxy(this.onSubmitClick, this));
45
-
46
- this.changeSubmitElems.on('change', $.proxy(function(e) {
47
- this.elem.trigger('form:triggerSubmit');
48
- }, this));
49
- this.onkeySubmitElems.on('onkey:trigger', $.proxy(function(e, triggerEvent, onkey) {
50
- clearTimeout(this.onkeySubmitTimeout);
51
- this.onkeySubmitTimeout = setTimeout($.proxy(function() {
52
- this.elem.trigger('form:triggerSubmit');
53
- }, this), onkey.elem.data('romo-form-onkey-submit-delay') || this.onkeyDefaultSubmitDelay);
54
- }, this));
55
- this.elem.on('form:triggerSubmit', $.proxy(this.onSubmitClick, this));
56
-
57
- this.elem.on('keypress', $.proxy(this.onFormKeyPress, this));
58
-
59
- if (this.elem.data('romo-form-reload-page') === true) {
60
- this.elem.on('form:submitSuccess', function(e, data, form) {
61
- Romo.reloadPage();
62
- })
63
- }
64
-
65
- }
66
-
67
- RomoForm.prototype.onFormKeyPress = function(e) {
68
- if (this.elem.data('romo-form-disable-keypress') !== true) {
69
- var targetElem = $(e.target);
70
- if (targetElem.is(':not(TEXTAREA)') && e.keyCode === 13 /* Enter */) {
71
- e.preventDefault();
72
- if (this.elem.data('romo-form-disable-enter-submit') !== true &&
73
- targetElem.data('romo-form-disable-enter-submit') !== true) {
74
- this.onSubmitClick();
75
- }
76
- }
77
- }
78
- }
32
+ this._bindElem();
79
33
 
80
- RomoForm.prototype.onSubmitClick = function(e) {
81
- if (e !== undefined) {
82
- e.preventDefault();
83
- }
84
-
85
- if (this.submitElem.data('romo-form-submit') === 'confirm') {
86
- this.elem.trigger('form:confirmSubmit', [this]);
87
- } else if (this.submitElem.hasClass('disabled') === false) {
88
- this.doSubmit();
89
- }
90
- }
34
+ Romo.trigger(this.elem, 'romoForm:clearMsgs', [this]);
35
+ Romo.trigger(this.elem, 'romoForm:ready', [this]);
36
+ });
91
37
 
92
38
  RomoForm.prototype.doSubmit = function() {
93
39
  this.submitQueued = true;
94
40
  if (this.submitRunning === false) {
95
- this._doSubmit();
41
+ this._submit();
96
42
  }
97
43
  }
98
44
 
99
- RomoForm.prototype.onSubmitSuccess = function(data, status, xhr) {
100
- this.elem.trigger('form:clearMsgs');
101
- this.elem.trigger('form:submitSuccess', [data, this]);
102
- this._doCompleteSubmit();
103
- }
45
+ // private
104
46
 
105
- RomoForm.prototype.onSubmitError = function(xhr, errorType, error) {
106
- this.elem.trigger('form:clearMsgs');
47
+ RomoForm.prototype._bindElem = function() {
48
+ Romo.on(this.submitElems, 'click', Romo.proxy(this._onSubmitClick, this));
107
49
 
108
- if(xhr.status === 422) {
109
- this.elem.trigger('form:submitInvalidMsgs', [$.parseJSON(xhr.responseText), xhr, this]);
110
- } else {
111
- this.elem.trigger('form:submitXhrError', [xhr, this]);
112
- }
113
- this.elem.trigger('form:submitError', [xhr, this]);
114
- this.indicatorElems.trigger('indicator:triggerStop');
115
- this._doCompleteSubmit();
116
- }
50
+ Romo.on(this.changeSubmitElems, 'change', Romo.proxy(function(e) {
51
+ Romo.trigger(this.elem, 'romoForm:triggerSubmit');
52
+ }, this));
117
53
 
118
- // private
54
+ Romo.on(this.onkeySubmitElems, 'romoOnkey:trigger', Romo.proxy(function(e, triggerEvent, romoOnkey) {
55
+ Romo.trigger(this.elem, 'romoForm:triggerSubmit');
56
+ }, this));
119
57
 
120
- RomoForm.prototype._doCompleteSubmit = function() {
121
- this.elem.trigger('form:submitComplete', [this]);
122
- if (this.submitQueued === true) {
123
- this._doSubmit();
124
- } else {
125
- this.submitRunning = false;
58
+ Romo.on(this.elem, 'romoForm:triggerSubmit', Romo.proxy(this._onTriggerSubmit, this));
59
+ Romo.on(this.elem, 'keypress', Romo.proxy(this._onFormKeyPress, this));
60
+
61
+ if (Romo.data(this.elem, 'romo-form-reload-page') === true) {
62
+ Romo.on(this.elem, 'romoForm:submitSuccess', function(e, data, romoForm) {
63
+ Romo.reloadPage();
64
+ });
126
65
  }
127
66
  }
128
67
 
129
- RomoForm.prototype._doSubmit = function() {
68
+ RomoForm.prototype._submit = function() {
130
69
  this.submitQueued = false;
131
70
  this.submitRunning = true;
132
- this.indicatorElems.trigger('indicator:triggerStart');
133
- this.elem.trigger('form:beforeSubmit', [this]);
134
71
 
135
- if(this.elem.data('romo-form-browser-submit') === true) {
136
- this._doBrowserSubmit();
137
- } else if (this.elem.attr('method').toUpperCase() === 'GET') {
138
- this._doNonBrowserGetSubmit();
72
+ Romo.trigger(this.spinnerElems, 'romoSpinner:triggerStart');
73
+ Romo.trigger(this.elem, 'romoForm:beforeSubmit', [this]);
74
+
75
+ if(Romo.data(this.elem, 'romo-form-browser-submit') === true) {
76
+ this._browserSubmit();
77
+ } else if (Romo.attr(this.elem, 'method').toUpperCase() === 'GET') {
78
+ this._nonBrowserGetSubmit();
139
79
  } else {
140
- this._doNonBrowserNonGetSubmit();
80
+ this._nonBrowserNonGetSubmit();
141
81
  }
142
82
  }
143
83
 
144
- RomoForm.prototype._doBrowserSubmit = function() {
84
+ RomoForm.prototype._browserSubmit = function() {
145
85
  this.elem.submit();
146
- this.elem.trigger('form:browserSubmit', [this]);
86
+ Romo.trigger(this.elem, 'romoForm:browserSubmit', [this]);
147
87
  }
148
88
 
149
- RomoForm.prototype._doNonBrowserGetSubmit = function() {
150
- var data = this._getSerializeObj();
89
+ RomoForm.prototype._nonBrowserGetSubmit = function() {
90
+ var formValues = this._getFormValues({ includeFiles: false });
151
91
 
152
- if (this.elem.data('romo-form-redirect-page') === true) {
153
- var paramString = Romo.param(data, {
92
+ if (Romo.data(this.elem, 'romo-form-redirect-page') === true) {
93
+ var paramString = Romo.param(formValues, {
154
94
  removeEmpty: this.removeEmptyGetParams,
155
95
  decodeValues: this.decodeParams
156
96
  });
157
97
  if (paramString !== '') {
158
- Romo.redirectPage(this.elem.attr('action') + '?' + paramString);
98
+ Romo.redirectPage(Romo.attr(this.elem, 'action')+'?'+paramString);
159
99
  } else {
160
- Romo.redirectPage(this.elem.attr('action'));
100
+ Romo.redirectPage(Romo.attr(this.elem, 'action'));
161
101
  }
162
-
163
102
  } else {
164
- this._doAjaxSubmit(data, true);
103
+ this._ajaxSubmit(formValues);
165
104
  }
166
105
  }
167
106
 
168
- RomoForm.prototype._doNonBrowserNonGetSubmit = function() {
169
- this._doAjaxSubmit(this._getFormData(), false);
107
+ RomoForm.prototype._nonBrowserNonGetSubmit = function() {
108
+ var formValues = this._getFormValues({ includeFiles: true });
109
+
110
+ this._ajaxSubmit(formValues);
170
111
  }
171
112
 
172
- RomoForm.prototype._doAjaxSubmit = function(data, process) {
173
- $.ajax({
174
- url: this.elem.attr('action'),
175
- type: this.elem.attr('method'),
176
- dataType: this._getXhrDataType(),
177
- data: data,
178
- processData: process,
179
- contentType: false,
180
- success: $.proxy(this.onSubmitSuccess, this),
181
- error: $.proxy(this.onSubmitError, this)
113
+ RomoForm.prototype._ajaxSubmit = function(formValues) {
114
+ Romo.ajax({
115
+ url: Romo.attr(this.elem, 'action'),
116
+ type: Romo.attr(this.elem, 'method'),
117
+ data: formValues,
118
+ success: Romo.proxy(this._onSubmitSuccess, this),
119
+ error: Romo.proxy(this._onSubmitError, this)
182
120
  });
183
121
  }
184
122
 
185
- RomoForm.prototype._getFormData = function() {
186
- var formData = new FormData();
187
123
 
188
- $.each(this._getSerializeObj(), function(k, v){ formData.append(k, v) });
189
- $.each(this.elem.find('INPUT[type="file"]'), function(i, fileInput) {
190
- var attrName = $(fileInput).attr('name')
191
- $.each(fileInput.files, function(i, file) { formData.append(attrName, file) });
124
+ RomoForm.prototype._completeSubmit = function() {
125
+ Romo.trigger(this.elem, 'romoForm:submitComplete', [this]);
126
+ if (this.submitQueued === true) {
127
+ this._submit();
128
+ } else {
129
+ this.submitRunning = false;
130
+ }
131
+ }
132
+
133
+ RomoForm.prototype._getFormValues = function(opts) {
134
+ if (opts === undefined) {
135
+ opts = { includeFiles: false };
136
+ }
137
+ var formValues = {};
138
+
139
+ // build formValues from the form elements
140
+ // { "inputName1": ["inputValue1"],
141
+ // "inputName2": ["inputValue1", "inputValue2"],
142
+ // ...
143
+ // }
144
+ Romo.array(this.elem.elements).forEach(function(inputElem) {
145
+ if ( inputElem.nodeName.toLowerCase() !== 'fieldset' &&
146
+ inputElem.name &&
147
+ !inputElem.disabled &&
148
+ inputElem.type !== 'submit' &&
149
+ inputElem.type !== 'reset' &&
150
+ inputElem.type !== 'button' &&
151
+ (opts.includeFiles || inputElem.type !== 'file') &&
152
+ (inputElem.checked || (inputElem.type !== 'radio' && inputElem.type !== 'checkbox'))
153
+ ) {
154
+ if (formValues[inputElem.name] === undefined) {
155
+ formValues[inputElem.name] = [];
156
+ }
157
+ if (inputElem.nodeName.toLowerCase() === 'select') {
158
+ Romo.find(inputElem, 'option').filter(function(optElem){
159
+ return optElem.selected;
160
+ }).forEach(function(selectedOptElem) {
161
+ formValues[inputElem.name].push(selectedOptElem.value);
162
+ });
163
+ } else if (inputElem.type === 'file') {
164
+ Array.prototype.forEach.call(inputElem.files, function(file) {
165
+ formValues[inputElem.name].push(file);
166
+ });
167
+ } else {
168
+ formValues[inputElem.name].push(inputElem.value);
169
+ }
170
+ }
192
171
  });
193
172
 
194
- return formData;
173
+ // process any list value inputs (if any)
174
+ // { inputName1: ["inputValue1"],
175
+ // inputName2: ["inputValue1,inputValue2"],
176
+ // ...
177
+ // }
178
+ var listDelims = Romo.find(this.elem, '[data-romo-form-list-values="true"]').reduce(
179
+ function(delims, inputElem) {
180
+ delims[Romo.attr(inputElem, 'name')] = (
181
+ Romo.data(inputElem, 'romo-form-list-values-delim') ||
182
+ this.defaultListValuesDelim
183
+ );
184
+ return delims;
185
+ },
186
+ {}
187
+ );
188
+ for (var name in listDelims) {
189
+ if (formValues[name]) {
190
+ formValues[name] = [formValues[name].join(listDelims[name])];
191
+ }
192
+ }
193
+
194
+ // remove the array from any single item array values
195
+ for(var key in formValues) {
196
+ if (formValues[key].length === 1) {
197
+ formValues[key] = formValues[key][0]
198
+ }
199
+ }
200
+
201
+ return formValues;
195
202
  }
196
203
 
197
- RomoForm.prototype._getSerializeObj = function() {
198
- var listNamesDelims = this._getListValueInputNamesDelims();
204
+ RomoForm.prototype._getXhrDataType = function() {
205
+ var dataType = Romo.data(this.elem, 'romo-form-xhr-data-type');
206
+ return ((dataType === undefined) ? 'json' : dataType);
207
+ }
208
+
209
+ // event functions
210
+
211
+ RomoForm.prototype.romoEvFn._onSubmitClick = function(e) {
212
+ e.preventDefault();
213
+
214
+ var submitElem = e.target;
215
+ if (!Romo.hasClass(submitElem, 'disabled')) {
216
+ if (Romo.data(submitElem, 'romo-form-submit') === 'confirm') {
217
+ Romo.trigger(this.elem, 'romoForm:confirmSubmit', [this]);
218
+ } else {
219
+ this.doSubmit();
220
+ }
221
+ }
222
+ }
199
223
 
200
- return this.elem.serializeArray().reduce(function(prev, curr) {
201
- if (listNamesDelims[curr.name] !== undefined) {
202
- prev[curr.name] = $.map([prev[curr.name], curr.value], function(v) {
203
- return v; // $.map removes null/undefined vals, this acts like a compact function
204
- }).join(listNamesDelims[curr.name])
224
+ RomoForm.prototype.romoEvFn._onTriggerSubmit = function() {
225
+ var disabled = this.submitElems.reduce(function(disabled, submitElem) {
226
+ return disabled || Romo.hasClass(submitElem, 'disabled');
227
+ }, false);
228
+ if (!disabled) {
229
+ var confirm = this.submitElems.reduce(function(confirm, submitElem) {
230
+ return confirm || Romo.data(submitElem, 'romo-form-submit') === 'confirm';
231
+ }, false);
232
+ if (confirm) {
233
+ Romo.trigger(this.elem, 'romoForm:confirmSubmit', [this]);
205
234
  } else {
206
- prev[curr.name] = curr.value;
235
+ this.doSubmit();
207
236
  }
237
+ }
238
+ }
208
239
 
209
- return prev;
210
- }, {});
240
+ RomoForm.prototype.romoEvFn._onFormKeyPress = function(e) {
241
+ if (Romo.data(this.elem, 'romo-form-disable-keypress') !== true) {
242
+ var targetElem = e.target;
243
+ if (targetElem.nodeName.toLowerCase() !== 'textarea' && e.keyCode === 13 /* Enter */) {
244
+ e.preventDefault();
245
+ if (Romo.data(this.elem, 'romo-form-disable-enter-submit') !== true &&
246
+ Romo.data(targetElem, 'romo-form-disable-enter-submit') !== true) {
247
+ this._onTriggerSubmit();
248
+ }
249
+ }
250
+ }
211
251
  }
212
252
 
213
- RomoForm.prototype._getListValueInputNamesDelims = function() {
214
- return Romo.toArray(this.elem.find('[data-romo-form-list-values="true"]')).reduce($.proxy(function(prev, curr) {
215
- prev[$(curr).attr('name')] = $(curr).data('romo-form-list-values-delim') || this.defaultListValuesDelim;
216
- return prev;
217
- }, this), {});
253
+ RomoForm.prototype.romoEvFn._onSubmitSuccess = function(response, status, xhr) {
254
+ Romo.trigger(this.elem, 'romoForm:clearMsgs');
255
+
256
+ var dataType = this._getXhrDataType();
257
+ Romo.trigger(
258
+ this.elem,
259
+ 'romoForm:submitSuccess',
260
+ [(dataType === 'json' ? JSON.parse(response) : response), this]
261
+ );
262
+
263
+ this._completeSubmit();
218
264
  }
219
265
 
220
- RomoForm.prototype._getXhrDataType = function() {
221
- if(this.elem.data('romo-form-xhr-data-type') !== undefined) {
222
- return this.elem.data('romo-form-xhr-data-type');
266
+ RomoForm.prototype.romoEvFn._onSubmitError = function(statusText, status, xhr) {
267
+ Romo.trigger(this.elem, 'romoForm:clearMsgs');
268
+
269
+ if(status === 422) {
270
+ var dataType = this._getXhrDataType();
271
+ Romo.trigger(
272
+ this.elem,
273
+ 'romoForm:submitInvalidMsgs',
274
+ [(dataType === 'json' ? JSON.parse(xhr.responseText) : xhr.responseText), xhr, this]
275
+ );
223
276
  } else {
224
- return 'json';
277
+ Romo.trigger(this.elem, 'romoForm:submitXhrError', [xhr, this]);
225
278
  }
279
+ Romo.trigger(this.elem, 'romoForm:submitError', [xhr, this]);
280
+ Romo.trigger(this.spinnerElems, 'romoSpinner:triggerStop');
281
+
282
+ this._completeSubmit();
226
283
  }
227
284
 
228
- Romo.onInitUI(function(e) {
229
- Romo.initUIElems(e, '[data-romo-form-auto="true"]').romoForm();
230
- });
285
+ // init
231
286
 
287
+ Romo.addElemsInitSelector('[data-romo-form-auto="true"]', RomoForm);