-
Notifications
You must be signed in to change notification settings - Fork 532
/
Copy pathfieldSelect.vue
137 lines (113 loc) · 3.6 KB
/
fieldSelect.vue
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
<template lang="pug">
select.form-control(v-model="value", :disabled="disabled", :name="schema.inputName", :id="getFieldID(schema)", :class="schema.fieldClasses", v-attributes="'input'")
option(v-if="!selectOptions.hideNoneSelectedText", :disabled="schema.required", :value="null") {{ selectOptions.noneSelectedText || "<Nothing selected>" }}
template(v-for="item in items")
optgroup(v-if="item.group", :label="getGroupName(item)")
option(v-if="item.ops", v-for="i in item.ops", :value="getItemValue(i)") {{ getItemName(i) }}
option(v-if="!item.group", :value="getItemValue(item)") {{ getItemName(item) }}
</template>
<script>
import { isObject, isNil, find } from "lodash";
import abstractField from "../abstractField";
export default {
mixins: [abstractField],
computed: {
selectOptions() {
return this.schema.selectOptions || {};
},
items() {
let values = this.schema.values;
if (typeof values == "function") {
return this.groupValues(values.apply(this, [this.model, this.schema]));
} else return this.groupValues(values);
}
},
methods: {
formatValueToField(value) {
if (isNil(value)) {
return null;
}
return value;
},
groupValues(values) {
let array = [];
let arrayElement = {};
values.forEach(item => {
arrayElement = null;
if (item.group && isObject(item)) {
// There is in a group.
// Find element with this group.
arrayElement = find(array, i => i.group === item.group);
if (arrayElement) {
// There is such a group.
arrayElement.ops.push({
id: item.id,
name: item.name
});
} else {
// There is not such a group.
// Initialising.
arrayElement = {
group: "",
ops: []
};
// Set group.
arrayElement.group = item.group;
// Set Group element.
arrayElement.ops.push({
id: item.id,
name: item.name
});
// Add array.
array.push(arrayElement);
}
} else {
// There is not in a group.
array.push(item);
}
});
// With Groups.
return array;
},
getGroupName(item) {
if (item && item.group) {
return item.group;
}
throw "Group name is missing! https://icebob.gitbooks.io/vueformgenerator/content/fields/select.html#select-field-with-object-items";
},
getItemValue(item) {
if (isObject(item)) {
if (typeof this.schema["selectOptions"] !== "undefined" && typeof this.schema["selectOptions"]["value"] !== "undefined") {
return item[this.schema.selectOptions.value];
} else {
// Use 'id' instead of 'value' cause of backward compatibility
if (typeof item["id"] !== "undefined") {
return item.id;
} else {
throw "`id` is not defined. If you want to use another key name, add a `value` property under `selectOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/select.html#select-field-with-object-items";
}
}
} else {
return item;
}
},
getItemName(item) {
if (isObject(item)) {
if (typeof this.schema["selectOptions"] !== "undefined" && typeof this.schema["selectOptions"]["name"] !== "undefined") {
return item[this.schema.selectOptions.name];
} else {
if (typeof item["name"] !== "undefined") {
return item.name;
} else {
throw "`name` is not defined. If you want to use another key name, add a `name` property under `selectOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/select.html#select-field-with-object-items";
}
}
} else {
return item;
}
}
}
};
</script>
<style lang="sass">
</style>