index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <!-- Search for member structures -->
  2. <template>
  3. <LayoutContainer class="map-view">
  4. <!-- Header -->
  5. <v-row>
  6. <v-layout>
  7. <h2 class="flex">
  8. {{ $t("member_companies") }}
  9. </h2>
  10. <v-btn-toggle mandatory dense @change="viewChanged">
  11. <v-btn>
  12. {{ $t("map") }}
  13. </v-btn>
  14. <v-btn>
  15. {{ $t("list") }}
  16. </v-btn>
  17. </v-btn-toggle>
  18. </v-layout>
  19. </v-row>
  20. <v-row>
  21. <!-- Map Column (hidden in 'list-view' mode)-->
  22. <v-col v-show="mapview" cols="6">
  23. <UiMapStructures
  24. ref="map"
  25. :structures="filteredStructures"
  26. @boundsUpdated="mapBoundsFilterChanged"
  27. />
  28. </v-col>
  29. <!-- Results column -->
  30. <v-col :cols="mapview ? 6 : 12">
  31. <!-- Search form -->
  32. <v-row>
  33. <v-form method="get" class="mt-8 w100">
  34. <v-container>
  35. <v-row>
  36. <v-col cols="6" class="py-2 px-1">
  37. <v-text-field
  38. v-model="textFilter"
  39. type="text"
  40. outlined
  41. clearable
  42. hide-details
  43. append-icon="mdi-magnify"
  44. :label="$t('what') + ' ?'"
  45. @click:append="search"
  46. @keydown.enter="search"
  47. />
  48. </v-col>
  49. <v-col cols="6" class="py-2 px-1">
  50. <UiSearchAddress
  51. ref="addressSearch"
  52. type="municipality"
  53. @change="locationFilterChanged"
  54. />
  55. </v-col>
  56. </v-row>
  57. <v-row>
  58. <v-col v-if="listview" cols="2" class="py-2 px-1">
  59. <v-btn class="h100" @click="reinitializeFilters">
  60. {{ $t('reinitialize') }}
  61. </v-btn>
  62. </v-col>
  63. <v-col :cols="listview ? 8 : 12">
  64. <v-row class="filters">
  65. <v-col :cols="3" class="py-2 px-1">
  66. <v-select
  67. v-model="practicesFilter"
  68. :label="$t('type')"
  69. :items="translatedPractices"
  70. item-value="id"
  71. item-text="label"
  72. filled
  73. hide-details
  74. @change="search"
  75. />
  76. </v-col>
  77. <v-col :cols="3" class="py-2 px-1">
  78. <v-select
  79. v-model="departmentFilter"
  80. :items="departments"
  81. item-value="code"
  82. item-text="label"
  83. :label="$t('department')"
  84. filled
  85. hide-details
  86. @change="search"
  87. />
  88. </v-col>
  89. <v-col :cols="3" class="py-2 px-1">
  90. <v-select
  91. v-model="federationFilter"
  92. :items="federations"
  93. item-value="id"
  94. item-text="name"
  95. :label="$t('federation')"
  96. filled
  97. hide-details
  98. @change="search"
  99. />
  100. </v-col>
  101. <v-col :cols="3" class="py-2 px-1">
  102. <v-select
  103. v-model="distanceFilter"
  104. :label="$t('distance')"
  105. :items="[
  106. {distance: 10, label: '10km'},
  107. {distance: 30, label: '30km'},
  108. {distance: 100, label: '100km'},
  109. {distance: 200, label: '200km'}
  110. ]"
  111. item-value="distance"
  112. item-text="label"
  113. filled
  114. hide-details
  115. @change="search"
  116. />
  117. </v-col>
  118. </v-row>
  119. </v-col>
  120. <v-col v-if="listview" cols="2" class="py-2 px-1 d-flex justify-end">
  121. <v-btn class="h100">
  122. {{ $t('search') }}
  123. </v-btn>
  124. </v-col>
  125. </v-row>
  126. <v-row v-show="mapview" class="px-2 pt-2">
  127. <v-btn @click="reinitializeFilters">
  128. {{ $t('reinitialize') }}
  129. </v-btn>
  130. <v-spacer />
  131. <v-btn @click="search">
  132. {{ $t('search') }}
  133. </v-btn>
  134. </v-row>
  135. </v-container>
  136. </v-form>
  137. </v-row>
  138. <!-- loading skeleton -->
  139. <div class="pt-4 mt-6">
  140. <v-container v-if="$fetchState.pending">
  141. <v-row v-for="i in 3" :key="i" justify="space-between" class="mt-1 mb-3">
  142. <v-col
  143. v-for="j in 2"
  144. :key="j"
  145. cols="12"
  146. sm="12"
  147. :md="mapview ? 6 : 12"
  148. class="py-2 px-1"
  149. >
  150. <v-skeleton-loader
  151. type="card"
  152. :loading="true"
  153. />
  154. </v-col>
  155. </v-row>
  156. </v-container>
  157. <!-- Results -->
  158. <v-data-iterator
  159. v-else
  160. :items="onMapFilteredStructures"
  161. :page.sync="page"
  162. :items-per-page="itemsPerPage"
  163. sort-by="name"
  164. hide-default-footer
  165. no-data-text=""
  166. >
  167. <template #header>
  168. <i class="results-count">{{ totalRecords }} {{ $t('results') }}</i>
  169. </template>
  170. <template #default="props">
  171. <v-row justify="space-between" class="mt-1 mb-3">
  172. <v-col
  173. v-for="structure in props.items"
  174. :key="structure.name"
  175. cols="12"
  176. sm="12"
  177. :md="mapview ? 6 : 12"
  178. class="py-2 px-1"
  179. >
  180. <v-card
  181. elevation="1"
  182. outlined
  183. :class="'structure-card pa-3 d-flex ' + (mapview ? 'flex-column' : 'flex-row align-items-center')"
  184. >
  185. <div class="d-flex justify-center">
  186. <v-img
  187. :src="structure.logoId ? ('https://api.opentalent.fr/app.php/_internal/secure/files/' + structure.logoId) : '/images/default.jpg'"
  188. alt="poster"
  189. height="80px"
  190. min-width="160px"
  191. max-width="80%"
  192. max-height="100%"
  193. :contain="true"
  194. style="margin: 12px;"
  195. />
  196. </div>
  197. <div class="d-flex flex-column">
  198. <v-chip-group v-if="structure.practices" active-class="primary--text">
  199. <v-chip v-for="practice in structure.practices" :key="practice" outlined small pill>
  200. {{ $t(practice) }}
  201. </v-chip>
  202. </v-chip-group>
  203. <v-card-title class="title">
  204. {{ structure.name }}
  205. </v-card-title>
  206. <v-card-text class="infos">
  207. <table>
  208. <tr>
  209. <td class="py-1 pr-2">
  210. <font-awesome-icon class="icon" :icon="['fas', 'map-marker-alt']" />
  211. </td>
  212. <td class="py-1">
  213. {{ structure.address }}
  214. </td>
  215. </tr>
  216. <tr>
  217. <td class="py-1 pr-2">
  218. <font-awesome-icon class="icon" :icon="['fas', 'project-diagram']" />
  219. </td>
  220. <td class="py-1">
  221. {{ structure.n1Name }}
  222. </td>
  223. </tr>
  224. </table>
  225. </v-card-text>
  226. </div>
  227. <span class="flex-fill" />
  228. <v-card-actions :class="listview ? 'align-self-end' : ''">
  229. <v-btn class="see" :to="'/structures/' + structure.id" nuxt>
  230. <span style="margin-right: 6px;">{{ $t("see_more") }}</span>
  231. <font-awesome-icon :icon="['fa', 'caret-right']" />
  232. </v-btn>
  233. </v-card-actions>
  234. </v-card>
  235. </v-col>
  236. </v-row>
  237. </template>
  238. <template #footer>
  239. <v-pagination
  240. v-model="page"
  241. :length="pageCount"
  242. total-visible="9"
  243. color="#e4611b"
  244. />
  245. </template>
  246. </v-data-iterator>
  247. </div>
  248. </v-col>
  249. </v-row>
  250. </LayoutContainer>
  251. </template>
  252. <script>
  253. import departments from '@/enums/departments'
  254. import practices from '@/enums/practices'
  255. import sphericDistance from '@/lib/geo'
  256. export default {
  257. data () {
  258. return {
  259. structures: [],
  260. filteredStructures: [],
  261. federations: [],
  262. loading: true,
  263. page: 1,
  264. itemsPerPage: 8,
  265. mapview: true,
  266. departments,
  267. practices,
  268. textFilter: '',
  269. locationFilter: null,
  270. practicesFilter: null,
  271. departmentFilter: null,
  272. federationFilter: null,
  273. distanceFilter: null,
  274. mapBoundsFilter: null,
  275. mapBoundsFilterStarted: false // map bounds filter is only activated when the map bounds are updated
  276. }
  277. },
  278. async fetch () {
  279. await fetch(
  280. `${this.$config.baseURL}/api/public/federation_structures/all`
  281. ).then(
  282. res => res.json()
  283. ).then(
  284. res => res.map((s) => {
  285. s.n1Id = s.n1Id ? parseInt(s.n1Id) : null
  286. s.n2Id = s.n2Id ? parseInt(s.n2Id) : null
  287. s.n3Id = s.n3Id ? parseInt(s.n3Id) : null
  288. s.n4Id = s.n4Id ? parseInt(s.n4Id) : null
  289. s.n5Id = s.n5Id ? parseInt(s.n5Id) : null
  290. s.practices = s.practices !== null ? s.practices.split(',') : []
  291. s.latitude = s.latitude ? parseFloat(s.latitude) : null
  292. s.longitude = s.longitude ? parseFloat(s.longitude) : null
  293. s.address = [s.streetAddress, s.postalCode, s.addressCity].join(' ')
  294. return s
  295. })
  296. ).then((res) => {
  297. this.structures = res
  298. this.filteredStructures = res
  299. // populate federations filter
  300. for (const s of res) {
  301. const f = {
  302. id: s.n1Id,
  303. name: s.n1Name
  304. }
  305. if (!this.federations.includes(f)) {
  306. this.federations.push(f)
  307. }
  308. }
  309. })
  310. },
  311. computed: {
  312. totalRecords () {
  313. return this.onMapFilteredStructures.length
  314. },
  315. pageCount () {
  316. return Math.floor(this.totalRecords / this.itemsPerPage) + 1
  317. },
  318. listview () {
  319. return !this.mapview
  320. },
  321. translatedPractices () {
  322. const tPractices = []
  323. for (const practice of this.practices) {
  324. tPractices.push({ id: practice.id, label: this.$t(practice.id) })
  325. }
  326. return tPractices
  327. },
  328. onMapFilteredStructures () {
  329. if (this.mapBoundsFilterStarted) {
  330. return this.filteredStructures.filter((s) => {
  331. return this.matchMapBounds(s)
  332. })
  333. } else {
  334. return this.filteredStructures
  335. }
  336. }
  337. },
  338. methods: {
  339. viewChanged (e) {
  340. this.mapview = (e === 0)
  341. },
  342. textFilterChanged (newVal) {
  343. this.textFilter = newVal
  344. },
  345. locationFilterChanged (newVal) {
  346. this.locationFilter = newVal
  347. if (this.distanceFilter === null) {
  348. this.distanceFilter = 10
  349. }
  350. this.search()
  351. },
  352. fitMapToResults () {
  353. this.$refs.map.zoomOnResults()
  354. },
  355. mapBoundsFilterChanged (newBounds) {
  356. this.mapBoundsFilterStarted = true
  357. this.mapBoundsFilter = newBounds
  358. },
  359. reinitializeFilters () {
  360. this.textFilter = null
  361. this.locationFilter = null
  362. this.practicesFilter = null
  363. this.departmentFilter = null
  364. this.federationFilter = null
  365. this.distanceFilter = null
  366. this.mapBoundsFilter = null
  367. this.$refs.addressSearch.clear()
  368. this.$refs.map.resetBounds()
  369. this.filteredStructures = this.structures
  370. },
  371. /**
  372. * Does the structure match the current textFilter
  373. * @param structure
  374. * @returns {boolean}
  375. */
  376. matchTextFilter (structure) {
  377. if (!this.textFilter) { return true }
  378. return structure.name.toLowerCase().includes(this.textFilter.toLowerCase())
  379. },
  380. /**
  381. * Does the structure match the current locationFilter
  382. * @param structure
  383. * @returns {boolean}
  384. */
  385. matchLocationFilter (structure) {
  386. if (!this.locationFilter) { return true }
  387. if (!structure.latitude || !structure.longitude) { return false }
  388. const radius = Number(this.distanceFilter) ?? 5
  389. return sphericDistance(
  390. this.locationFilter.latitude, this.locationFilter.longitude, structure.latitude, structure.longitude
  391. ) <= radius
  392. },
  393. /**
  394. * Does the structure match the current practicesFilter
  395. * @param structure
  396. * @returns {boolean}
  397. */
  398. matchPracticesFilter (structure) {
  399. if (!this.practicesFilter) { return true }
  400. return structure.practices && structure.practices.includes(this.practicesFilter)
  401. },
  402. /**
  403. * Does the structure match the current departmentFilter
  404. * @param structure
  405. * @returns {boolean}
  406. */
  407. matchDepartmentFilter (structure) {
  408. if (!this.departmentFilter) { return true }
  409. return structure.postalCode.startsWith(this.departmentFilter)
  410. },
  411. /**
  412. * Does the structure match the current federationFilter
  413. * @param structure
  414. * @returns {boolean}
  415. */
  416. matchFederationFilter (structure) {
  417. if (!this.federationFilter) { return true }
  418. return structure.parents.includes(Number(this.federationFilter))
  419. },
  420. /**
  421. * Does the structure match the current federationFilter
  422. * @param structure
  423. * @returns {boolean}
  424. */
  425. matchMapBounds (structure) {
  426. if (!this.mapBoundsFilter) { return true }
  427. return this.mapBoundsFilter.getSouth() <= structure.latitude &&
  428. structure.latitude <= this.mapBoundsFilter.getNorth() &&
  429. this.mapBoundsFilter.getWest() <= structure.longitude &&
  430. structure.longitude <= this.mapBoundsFilter.getEast()
  431. },
  432. /**
  433. * Does the structure match each of the page filters
  434. * @param structure
  435. * @returns {boolean}
  436. */
  437. matchFilters (structure) {
  438. return this.matchTextFilter(structure) &&
  439. this.matchLocationFilter(structure) &&
  440. this.matchPracticesFilter(structure) &&
  441. this.matchDepartmentFilter(structure) &&
  442. this.matchFederationFilter(structure)
  443. // this.matchMapBounds(structure)
  444. },
  445. /**
  446. * Update the filteredStructures array
  447. */
  448. search () {
  449. this.filteredStructures = this.structures.filter((s) => { return this.matchFilters(s) })
  450. this.fitMapToResults()
  451. }
  452. }
  453. }
  454. </script>
  455. <style scoped lang="scss">
  456. @import 'assets/style/variables.scss';
  457. h2 {
  458. color: $theme;
  459. }
  460. .structure-card {
  461. height: 100%;
  462. }
  463. .infos .col {
  464. padding: 6px 12px;
  465. }
  466. .title {
  467. word-break: normal;
  468. color: $theme;
  469. font-size: 18px;
  470. font-weight: 500;
  471. line-height: 1.6rem;
  472. }
  473. .icon {
  474. color: $theme;
  475. }
  476. .results-count {
  477. font-size: .8em;
  478. color: #666;
  479. }
  480. </style>