11<template lang="pug">
22 // Mapbox GUI
33 div( id ="map" ref ="map" )
4- div( class ="map-overlay" style ="margin-top: 60px; margin-left: 2%;" )
4+ div( class ="map-overlay" style ="margin-top: 60px; margin-left: 2%; width: 30%; " )
55 div( class ="map-overlay-inner" )
66 h4 Region/Province selection
77
1616 :disabled ="disabled"
1717 :options ="optionsProvince"
1818 )
19+
20+ small( v-if ="disabled" ) Please wait while loading...
21+ br
22+ br
23+
24+ div( v-if ="legends.length > 0" )
25+ h5 Legend
26+ div( class ="legend-livelihood" )
27+ div( v-for ="item in legends" )
28+ <div ><span v-bind:style =" { backgroundColor: item.color }" ></span >{{ item.text }}</div >
1929</template >
2030
2131<script >
@@ -40,6 +50,8 @@ export default {
4050 { value: null , text: ' Please select a province' }
4151 ],
4252
53+ legends: [],
54+
4355 islands: {
4456 luzon: {
4557 ' national capital region' : [' NCR, city of manila, first district' , ' NCR, second district' , ' NCR, third district' , ' NCR, fourth district' ],
@@ -104,12 +116,17 @@ export default {
104116
105117 watch: {
106118 selectedRegion () {
119+ if (! this .selectedRegion ) {
120+ this .selectedRegion = this .previousRegion
121+ return
122+ }
123+
107124 this .selectedIsland = this .getIslandFromRegion (this .selectedRegion )
108125 this .getProvinceOptions ()
109126
110127 // Hide the previous selected region
111- if (this .previousRegion ) {
112- window .MBL .toggleLayer (` ${ this .previousRegion } -layer` )
128+ if (this .previousIsland ) {
129+ window .MBL .toggleLayer (` ${ this .previousIsland } -layer` )
113130 }
114131
115132 // Build the province list filter. Format the hard-coded values
@@ -135,19 +152,23 @@ export default {
135152 }
136153 )
137154
138- // this.previousIsland = this.selectedIsland
139- this .previousRegion = this .selectedIsland
155+ this .previousRegion = this .selectedRegion
156+ this .previousIsland = this .selectedIsland
140157
141- // Disable the UI
158+ // Disable the UI on page load
142159 this .disabled = true
143160 const that = this
161+
144162 const time = setInterval (() => {
145163 if (! window .MBL .isLoading ) {
146164 console .log (' map loaded!' )
147165 that .disabled = false
166+ that .disabled = false
148167 clearInterval (time)
149168 }
150169 }, 200 )
170+
171+ this .updateLegend ()
151172 },
152173
153174 selectedProvince () {
@@ -163,6 +184,8 @@ export default {
163184 key: ' ADM2_EN' ,
164185 value: provinceValue
165186 })
187+
188+ this .updateLegend (provinceValue)
166189 }
167190 },
168191
@@ -240,6 +263,48 @@ export default {
240263 this .optionsProvince .push ({ value: item, text: provinceValue })
241264 })
242265 }
266+ },
267+
268+ updateLegend (provinceName ) {
269+ const that = this
270+ let loadedOnce = false
271+ let colorCodes = []
272+
273+ const unique = (value , index , self ) => {
274+ return self .indexOf (value) === index
275+ }
276+
277+ // Reset the legends
278+ this .legends = []
279+
280+ window .MBL .map .on (' sourcedata' , function (e ) {
281+ if (e .isSourceLoaded ) {
282+ if (e .sourceId !== ' composite' && ! loadedOnce) {
283+ const features = window .MBL .map .queryRenderedFeatures ({layers: [` ${ e .sourceId } -layer` ] })
284+
285+ if (features .length > 0 ) {
286+ window .MBL .isLoading = false
287+ loadedOnce = true
288+ console .log (` --loaded vector length: ${ features .length } ` )
289+
290+ if (provinceName) {
291+ colorCodes = Array .from (features .filter (x => x .properties [' ADM2_EN' ] === provinceName),
292+ (x ) => x .properties [' Legend_v2' ]).filter (unique)
293+ } else {
294+ colorCodes = Array .from (features, (x ) => x .properties [' Legend_v2' ]).filter (unique)
295+ }
296+
297+ for (let i = 0 ; i < colorCodes .length ; i += 1 ) {
298+ that .legends .push ({
299+ text: colorCodes[i],
300+ color: window .MBL .colorCodes [colorCodes[i]]
301+ })
302+ }
303+ }
304+ }
305+ // window.MBL.isLoading = false
306+ }
307+ })
243308 }
244309 }
245310}
0 commit comments