Repository navigation
Expand file tree
/
Copy pathform.js
More file actions
179 lines (149 loc) · 4.67 KB
/
Copy pathform.js
File metadata and controls
179 lines (149 loc) · 4.67 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
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
var _ = require("hr.utils");
var $ = require("jquery");
var View = require("hr.view");
var Model = require("hr.model");
var GETTER = {
'boolean': function() {
return $(this).is(":checked");
},
'string': function() {
return $(this).val();
},
'number': function() {
return parseInt($(this).val());
}
};
var RENDERER = {
'boolean': function(propertyName, property, value) {
return $("<input>", {
'type': "checkbox",
'checked': value === true,
'name': propertyName
});
},
'number': function(propertyName, property, value) {
return $("<input>", {
'type': "number",
'value': value,
'name': propertyName,
'min': property.minimum,
'max': property.maximum,
'step': property.multipleOf || 1
});
},
'string': function(propertyName, property, value) {
if (property['enum']) return RENDERER.select.apply(this, arguments);
return $("<input>", {
'type': "text",
'value': value,
'name': propertyName
});
},
'select': function(propertyName, property, value) {
var $select = $("<select>", {
'name': propertyName
});
$select.append(_.map(property['enum'], function(v) {
return $("<option>", {
'selected': value == v,
'text': v,
'value': v
});
}));
return $select;
}
};
var SchemaView = View.extend({
className: "form-schema",
defaults: {
prefix: "",
schema: {},
values: {}
},
events: {},
initialize: function() {
SchemaView.__super__.initialize.apply(this, arguments);
},
render: function() {
var values = this.options.values;
this.$el.empty();
_.each(this.options.schema.properties, function(property, propertyName) {
// Value for this property
var value = values[propertyName];
// prefix for the property name (as array)
var prefixs = _.compact(this.options.prefix.split(".").concat([propertyName]));
// ignore array for the moment
if (property.type == "array") return;
var $property = $("<div>", {
'class': "schema-property type-"+property.type
});
// Create label
var $title = $("<label>", {
'class': "property-label level-"+prefixs.length,
'text': property.title || property.description,
'css': {
'paddingLeft': prefixs.length*20
}
});
$title.appendTo($property);
// Handle subschema
if (property.type == "object") {
var schema = new SchemaView({
'schema': property,
'values': value || {},
'prefix': prefixs.join(".")
});
schema.$el.appendTo($property);
schema.update();
}
else
// Handle normal fields
{
var renderer = RENDERER[property.type];
var $input = renderer(prefixs.join("."), property, value);
$input.attr("data-getter", property.type);
var $inputContainer = $("<div>", {
'class': "property-input"
});
$input.appendTo($inputContainer);
$inputContainer.prependTo($property);
}
$property.appendTo(this.$el);
}, this);
return this.ready();
}
});
var FormView = View.extend({
className: "component-form",
defaults: {
schema: {},
values: {}
},
initialize: function() {
FormView.__super__.initialize.apply(this, arguments);
this.schema = new SchemaView({
prefix: "",
schema: this.options.schema,
values: this.options.values
});
this.schema.appendTo(this);
this.schema.update();
},
// Extract all new values
getValues: function() {
var m = new Model({}, this.options.values);
this.$("*[name]").each(function() {
var name = $(this).attr("name");
var getter = GETTER[$(this).data("getter")];
var value = getter.apply($(this));
m.set(name, value);
});
return m.toJSON();
},
// Update values in form
setValues: function(values) {
this.schema.options.values = values;
return this.schema.update();
}
});
module.exports = FormView;