summaryrefslogtreecommitdiff
path: root/examples/catering.html
blob: f9596168ec72f57c179f2a00493c7cdd5bc9b3c7 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!DOCTYPE html>
<html>
<head>
    <title>Catering</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <link rel="stylesheet" href="../src/leaflet-soton.css" />

    <link rel="stylesheet" href="../resources/leaflet-sidebar/src/L.Control.Sidebar.css" />
    <link rel="stylesheet" href="../resources/leaflet/dist/leaflet.css" />

    <style>
        body {
          padding: 0;
          margin: 0;
        }

        html, body, #map {
          height: 100%;
        }
        .typeahead, .tt-query, .tt-hint {
            border: 2px solid #ccc;
            border-radius: 8px;
            font-size: 18px;
            height: 30px;
            line-height: 30px;
            outline: medium none;
            padding: 8px 12px;
            width: 90%;
        }
        .typeahead {
            background-color: #fff;
        }
        .twitter-typeahead {
            width: 100%
        }
        .typeahead:focus {
            border: 2px solid #0097cf;
        }
        .tt-query {
            box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
        }
        .tt-hint {
            color: #999;
        }
        .tt-dropdown-menu {
            background-color: #fff;
            border: 1px solid rgba(0, 0, 0, 0.2);
            border-radius: 8px;
            box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
            margin-top: 12px;
            padding: 8px 0;
            width: 385px;
            max-height: 150px;
            overflow-y: auto;
        }
        .tt-suggestion {
            font-size: 18px;
            line-height: 24px;
            padding: 3px 20px;
        }
        .tt-suggestion.tt-cursor {
            background-color: #0097cf;
            color: #fff;
        }
        .tt-suggestion p {
            margin: 0;
        }
    </style>
</head>
<body>
    <div id="map"></div>

    <div id="sidebar">
        <h1>Catering</h1>

        <input id="search" class="typeahead" type="text" placeholder="What would you like to eat or drink?">

        <div id="availablefrom">
        </div>

        <div id="info">

        </div>
    </div>

    <script src="../resources/leaflet/dist/leaflet.js"></script>
    <script src="../resources/leaflet-markercluster/dist/leaflet.markercluster.js"></script>
    <script src="../resources/leaflet-indoor/leaflet-indoor.js"></script>
    <script src="../resources/leaflet-sidebar/src/L.Control.Sidebar.js"></script>
    <script src="../resources/jquery/dist/jquery.min.js"></script>
    <script src="../resources/typeahead.js/dist/typeahead.bundle.min.js"></script>

    <script src="../src/leaflet-soton.js"></script>

    <script type="text/javascript">
        LS.imagePath = '../resources/images/';
        LS.dataPath = '../data.json';

        (function() {
            var map = LS.map('map', {
                indoor: false,
            });

            var sidebar = L.control.sidebar('sidebar', {
                position: 'left'
            });

            map.addControl(sidebar);

            setTimeout(function () {
                sidebar.show();
            }, 500);

            var info = document.getElementById("info");

            map.showInfo = function(content, latlng, options) {
                info.innerHTML = "";
                info.appendChild(content);
                sidebar.show();
            };

            LS.getData(function(data) { var layer = LS.getPointOfServiceLayer();
                layer.addTo(map);

                var substringMatcher = function(strs) {
                    return function findMatches(q, cb) {
                        var matches, substrRegex;
                        // an array that will be populated with substring matches
                        matches = [];

                        // regex used to determine if a string contains the substring `q`
                        substrRegex = new RegExp(q, 'i');

                        // iterate through the pool of strings and for any string that
                        // contains the substring `q`, add it to the `matches` array
                        $.each(strs, function(i, str) {
                            if (substrRegex.test(str)) {
                                // the typeahead jQuery plugin expects suggestions to a
                                // JavaScript object, refer to typeahead docs for more info
                                matches.push({ value: str });
                            }
                        });

                    cb(matches);
                    };
                };

                var itemMap = {};

                data.pointsOfService.features.forEach(function(feature) {
                    if ("offerings" in feature.properties) {
                        var offerings = feature.properties.offerings;

                        var sections = Object.keys(offerings);

                        sections.forEach(function(sectionURI) {
                            var section = offerings[sectionURI];

                            section.items.forEach(function(item) {
                                var obj = {
                                    uri: item.uri,
                                    feature: feature
                                };

                                if (item.label in itemMap) {
                                    itemMap[item.label].push(obj);
                                } else {
                                    itemMap[item.label] = [ obj ];
                                }
                            });
                        });
                    }
                });

                var items = Object.keys(itemMap);

                var $search = $('#search');

                $search.typeahead(
                    {
                        hint: true,
                        highlight: true,
                        minLength: 1
                    },
                    {
                        name: 'states',
                        displayKey: 'value',
                        source: substringMatcher(items)
                    }
                );

                var availableFrom = document.getElementById("availablefrom");

                $search.keyup(function() {
                    var val = $search.val();

                    if (val in itemMap) {
                        var uris = itemMap[val];

                        availableFrom.innerHTML = "";

                        var ul = document.createElement("ul");

                        uris.forEach(function(result) {
                            var feature = result.feature;
                            console.log(feature.properties);
                            var li = document.createElement("li");

                            var a = document.createElement("a");
                            a.textContent = feature.properties.name; //+ " (" + result.uri + ")";
                            a.href = "#";
                            a.onclick = function() {
                                console.log(feature.properties.uri);
                                map.show(feature.properties.uri);
                                return false;
                            };

                            li.appendChild(a);
                            ul.appendChild(li);
                        });

                        availableFrom.appendChild(ul);
                    }
                });
            });
        })();
    </script>
</body>
</html>