This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
152 lines (125 loc) · 3.42 KB
/
index.js
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
(function(factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
})(function($) {
'use strict';
$.fn.cascadingSelect = function(options) {
var defaults = {
placeholder: false,
placeholderWhenEmpty: false
};
var settings = $.extend({
}, defaults, options);
var _data = normalizeData(settings);
_data.nodeAtLevel = nodeAtLevel;
var _selects = [this].
concat(settings.subSelects.map(function(s) { return $(s); })).
map(function(s) {
return s[0];
});
$(_selects).change(function(e) {
// console.log('changed', $(this).val(), e.target);
if (e.target === _selects[_selects.length - 1]) {
// console.log('ignored');
return;
}
var curLevel = _selects.indexOf(e.target);
var curNode = _data.nodeAtLevel(curLevel);
var nextSelect = _selects.slice(curLevel + 1)[0];
var entries;
// console.log('curNode', curNode);
if (curNode && curNode.children && curNode.children.length) {
entries = curNode.children;
} else if (settings.placeholderWhenEmpty) {
entries = [{text: settings.placeholderWhenEmpty, value: ''}];
} else {
entries = [];
}
$(nextSelect).
empty().
append(genOptions(entries)).
change();
});
init.call(this);
return this;
function init() {
if (this.val() === null) {
this.
empty().
append(genOptions(_data)).
change();
} else {
preselect();
}
}
function preselect() {
$(_selects).each(function(i) {
var s = $(this);
var v = s.val();
if (v === null) {
return false;
}
if (i >= 1 && nodeAtLevel(i - 1).children.length === 0) {
return false;
}
s.
empty().
append(genOptions(i === 0 ? _data : nodeAtLevel(i - 1).children)).
val(v);
});
}
function nodeAtLevel(level) {
var path = [];
for (var i = 0; i <= level; i++) {
path.push(selectedIndex(_selects[i]));
}
return nodeAtPath(path);
}
function nodeAtPath(path) {
return path.reduce(function(n, i) {
if (typeof n !== 'undefined') {
return n.children[i];
}
}, { children: _data });
}
}
function normalizeData(settings) {
return normalizeChildren(settings.data);
function normalizeChildren(children) {
var normalizedData = children.map(function(c) {
if (typeof c === 'object') {
return $.extend(
{ value: c.text }, c,
{ children: c.children ? normalizeChildren(c.children) : []}
);
} else {
var text = c.toString();
return { text: text, value: text, children: [] };
}
});
if (settings.placeholder) {
normalizedData.unshift({text: settings.placeholder, value: '', children: []});
}
return normalizedData;
}
}
function selectedIndex(selectEl) {
var result;
$(selectEl).find('option').each(function(i) {
if (this.selected) {
result = i;
return false;
}
});
return result;
}
function genOptions(children) {
return children.map(function(c) {
return $('<option>').attr('value', c.value).text(c.text);
});
}
});