This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RouteSelector.js
188 lines (157 loc) · 7.13 KB
/
RouteSelector.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
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
181
182
183
184
185
186
187
188
/*global define,module, require*/
// if the module has no dependencies, the above pattern can be simplified to
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(["../Route", "../RouteId"], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require("Route", "RouteId"));
} else {
// Browser globals (root is window)
root.elc = factory(root.Route, root.RouteId);
}
}(this, function (Route, RouteId) {
/**
* Creates a Route Selector UI control.
* @param {HTMLElement} root
*/
function RouteSelector(root) {
if (!(root && root instanceof HTMLElement)) {
throw new TypeError("No root element provided or not an HTML element");
}
root.classList.add("route-selector");
var progressBar = document.createElement("progress");
progressBar.classList.add("route-list-progress");
progressBar.textContent = "Loading route list...";
root.appendChild(progressBar);
var _routes = null;
var mainlineSelect = document.createElement("select");
var routeSelect = document.createElement("select");
routeSelect.name = "route";
routeSelect.required = true;
// for bootstrap
mainlineSelect.classList.add("form-control");
routeSelect.classList.add("form-control");
var cbLabel = document.createElement("label");
var checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.name = "decrease";
cbLabel.appendChild(checkbox);
cbLabel.appendChild(document.createTextNode(" Decrease"));
cbLabel.classList.add("input-group-addon");
root.appendChild(mainlineSelect);
root.appendChild(routeSelect);
root.appendChild(cbLabel);
Object.defineProperties(this, {
/**@property {HTMLElement}*/
root: {
get: function () {
return root;
}
},
/**@property {Route[]}*/
routes: {
get: function () {
return _routes;
},
set: function (routesArray) {
_routes = routesArray;
// Sort the routes by route IDs' SR then RRQ (non numeric comes before numeric).
_routes.sort(function (a, b) { return RouteId.sort(a.routeId, b.routeId); });
var srDocFrag = document.createDocumentFragment();
var isGroup = document.createElement("optgroup");
isGroup.label = "Interstate Routes";
var usGroup = document.createElement("optgroup");
usGroup.label = "US Routes";
var waGroup = document.createElement("optgroup");
waGroup.label = "WA State Routes";
// Populate the SR Select.
var route, i, l, option, rt;
for (i = 0, l = routesArray.length; i < l; i += 1) {
route = routesArray[i];
if (route.name && route.isMainline) {
option = document.createElement("option");
option.label = route.label;
option.textContent = route.label;
option.value = route.name;
option.dataset.isBoth = route.isBoth;
rt = route.routeTypeAbbreviation;
if (rt) {
switch (rt) {
case "IS":
isGroup.appendChild(option);
break;
case "US":
usGroup.appendChild(option);
break;
case "SR":
waGroup.appendChild(option);
break;
}
} else {
srDocFrag.appendChild(option);
}
}
}
if (isGroup.hasChildNodes()) {
srDocFrag.appendChild(isGroup);
}
if (usGroup.hasChildNodes()) {
srDocFrag.appendChild(usGroup);
}
if (waGroup.hasChildNodes()) {
srDocFrag.appendChild(waGroup);
}
mainlineSelect.appendChild(srDocFrag);
addOptionsForCurrentlySelectedMainline();
setRouteDirectionControls();
root.classList.add("routes-loaded");
}
}
});
/**
* Populates the route box with options associated with the currently selected mainline.
*/
function addOptionsForCurrentlySelectedMainline() {
var mainline = mainlineSelect.value;
// Remove options.
routeSelect.innerHTML = "";
var docFrag = document.createDocumentFragment();
var route, option, srRe = /^\d{3}\s/, title, label;
for (var i = 0, l = _routes.length; i < l; i += 1) {
route = _routes[i];
if (route.routeId.sr === mainline) {
option = document.createElement("option");
option.value = route.name;
label = route.isMainline ? "Mainline" : route.routeId.rrq ? [route.routeId.rrt, route.routeId.rrq].join(" ") : route.routeId.rrt;
option.label = label;
option.textContent = label;
title = route.routeId.description;
title = title.replace(srRe, "");
option.title = title;
option.dataset.isBoth = route.isBoth;
docFrag.appendChild(option);
}
}
routeSelect.appendChild(docFrag);
routeSelect.disabled = routeSelect.options.length === 1;
setRouteDirectionControls();
}
function setRouteDirectionControls() {
var option = routeSelect.options[routeSelect.selectedIndex];
if (option.dataset.isBoth === "true") {
checkbox.removeAttribute("disabled");
root.classList.add("direction-both");
} else {
root.classList.remove("direction-both");
checkbox.setAttribute("disabled", "disabled");
}
}
mainlineSelect.addEventListener("change", addOptionsForCurrentlySelectedMainline, true);
routeSelect.addEventListener("change", setRouteDirectionControls);
}
return RouteSelector;
}));