| 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 |
1
1
1
54
54
3
17
17
17
17
54
48
7
7
22
22
3
7
5
5
2
2
3
4
10
10
3
3
1
1
1
1
3
1
23
23
23
23
4
4
23
4
23
19
| "use strict";
;require.register("models/form", function (exports, require, module) {
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var App = require('app');
// move this to models cause some errors
App.Form = Em.View.extend({
/**
* generating fields from fieldsOptions
*/
classNames: ["form-horizontal"],
attributeBindings: ['autocomplete'],
autocomplete: 'off',
i18nprefix: 'form.',
fields: [],
field: {},
messages: [],
object: false,
result: 0, // save result var (-1 - error; 0 - init; 1 - success)
templateName: require('templates/common/form'),
tagName: 'form',
init: function init() {
var thisForm = this;
if (!this.fields.length) {
this.fieldsOptions.forEach(function (options) {
var field = App.FormField.create(options);
field.set('form', thisForm);
thisForm.fields.push(field);
thisForm.set("field." + field.get('name'), field);
});
}
this._super();
},
/**
* get field of form by name
* @param name
* @return {Object}
*/
getField: function getField(name) {
return this.get('fields').findProperty('name', name);
},
isValid: function isValid() {
var isValid = true;
$.each(this.fields, function () {
this.validate();
if (!this.get('isValid')) {
isValid = false;
}
});
return isValid;
},
updateValues: function () {
var object = this.get('object');
if (object instanceof Em.Object) {
$.each(this.fields, function () {
this.set('value', this.get('displayType') == 'password' ? '' : object.get(this.get('name')));
});
} else {
this.clearValues();
}
}.observes("object"),
/**
* reset values to default of every field in the form
*/
clearValues: function clearValues() {
this.get('fields').forEach(function (field) {
var value = field.get('defaultValue') === undefined ? '' : field.get('defaultValue');
field.set('value', value);
}, this);
},
visibleFields: Em.computed.filterBy('fields', 'isHiddenField', false),
resultText: function () {
var text = "";
switch (this.get('result')) {
case -1:
text = this.t("form.saveError");
break;
case 1:
text = this.t("form.saveSuccess");
break;
}
return text;
}.property('result')
});
App.FormField = Em.Object.extend({ // try to realize this as view
name: '',
displayName: '',
// defaultValue:'', NOT REALIZED YET
description: '',
disabled: false,
displayType: 'string', // string, digits, number, directories, textarea, checkbox
disableRequiredOnPresent: false,
errorMessage: '',
warnMessage: '',
form: false,
isRequired: true, // by default a config property is required
unit: '',
value: '',
isValid: Em.computed.equal('errorMessage', ''),
viewClass: function () {
var options = {};
var element = Em.TextField;
switch (this.get('displayType')) {
case 'checkbox':
element = App.CheckboxView;
options.checkedBinding = "value";
break;
case 'select':
element = Em.Select;
options.content = this.get('values');
options.valueBinding = "value";
options.optionValuePath = "content.value";
options.optionLabelPath = "content.label";
break;
case 'password':
options['type'] = 'password';
break;
case 'textarea':
element = Em.TextArea;
break;
case 'hidden':
options.type = "hidden";
break;
}
return element.extend(options);
}.property('displayType'),
validate: function validate() {
var value = this.get('value');
var isError = false;
this.set('errorMessage', '');
if (this.get('isRequired') && typeof value === 'string' && value.trim().length === 0) {
this.set('errorMessage', 'This is required');
isError = true;
}
if (typeof value === 'string' && value.trim().length === 0) {
// this is not to validate empty field.
isError = true;
}
if (!isError) {
this.set('errorMessage', '');
}
},
isHiddenField: Em.computed.equal('displayType', 'hidden')
});
}); |