index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <!-- Search for member structures -->
  2. <template>
  3. <LayoutContainer>
  4. <!-- Header -->
  5. <v-row>
  6. <v-layout>
  7. <h2 class="flex">
  8. {{ $t("member_companies") }}
  9. </h2>
  10. <v-btn-toggle mandatory dense :value="mapview ? 0 : 1" @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
  23. v-if="mapview"
  24. cols="12"
  25. sm="6"
  26. >
  27. <no-ssr>
  28. <UiMapStructures
  29. ref="map"
  30. :structures="filteredStructures"
  31. :zoom-at-start="mapZoomAtStart"
  32. @boundsUpdated="mapBoundsFilterChanged"
  33. />
  34. </no-ssr>
  35. </v-col>
  36. <!-- Results column -->
  37. <v-col
  38. cols="12"
  39. :sm="mapview ? 6 : 12"
  40. >
  41. <!-- Search form -->
  42. <v-row>
  43. <v-form method="get" class="mt-8 w100">
  44. <v-container>
  45. <v-row>
  46. <v-col cols="12" md="6" class="py-2 px-1">
  47. <v-text-field
  48. v-model="textFilter"
  49. type="text"
  50. outlined
  51. clearable
  52. hide-details
  53. append-icon="mdi-magnify"
  54. :label="$t('what') + ' ?'"
  55. @click:append="search"
  56. @keydown.enter="search"
  57. />
  58. </v-col>
  59. <v-col cols="12" md="6" class="py-2 px-1">
  60. <UiSearchAddress
  61. ref="addressSearch"
  62. type="municipality"
  63. @change="locationFilterChanged"
  64. />
  65. </v-col>
  66. </v-row>
  67. <v-row>
  68. <v-col v-if="listview && $vuetify.breakpoint.mdAndUp" cols="2" class="py-2 px-1">
  69. <v-btn class="h100" @click="reinitializeFilters">
  70. {{ $t('reinitialize') }}
  71. </v-btn>
  72. </v-col>
  73. <v-col :cols="(listview && $vuetify.breakpoint.mdAndUp) ? 8 : 12">
  74. <v-row class="filters">
  75. <v-col lg="3" :md="listview ? 3 : 6" sm="6" cols="12" class="py-2 px-1">
  76. <v-autocomplete
  77. v-model="practicesFilter"
  78. :label="$t('type')"
  79. :items="translatedPractices"
  80. item-value="id"
  81. item-text="label"
  82. filled
  83. hide-details
  84. hide-no-data
  85. :filter="enhancedAutocompleteFilter"
  86. @change="search"
  87. />
  88. </v-col>
  89. <v-col lg="3" :md="listview ? 3 : 6" sm="6" cols="12" class="py-2 px-1">
  90. <v-autocomplete
  91. v-model="departmentFilter"
  92. :items="departments"
  93. item-value="code"
  94. item-text="label"
  95. :label="$t('department')"
  96. filled
  97. hide-details
  98. hide-no-data
  99. :filter="enhancedAutocompleteFilter"
  100. @change="search"
  101. />
  102. </v-col>
  103. <v-col lg="3" :md="listview ? 3 : 6" sm="6" cols="12" class="py-2 px-1">
  104. <v-autocomplete
  105. v-model="federationFilter"
  106. :items="federations"
  107. item-value="id"
  108. item-text="name"
  109. :label="$t('federation')"
  110. filled
  111. hide-details
  112. hide-no-data
  113. :filter="enhancedAutocompleteFilter"
  114. @change="search"
  115. />
  116. </v-col>
  117. <v-col lg="3" :md="listview ? 3 : 6" sm="6" cols="12" class="py-2 px-1">
  118. <v-select
  119. v-model="distanceFilter"
  120. :label="$t('distance')"
  121. :items="[
  122. {distance: 10, label: '10km'},
  123. {distance: 30, label: '30km'},
  124. {distance: 100, label: '100km'},
  125. {distance: 200, label: '200km'}
  126. ]"
  127. item-value="distance"
  128. item-text="label"
  129. filled
  130. hide-details
  131. @change="search"
  132. />
  133. </v-col>
  134. </v-row>
  135. </v-col>
  136. <v-col v-if="listview && $vuetify.breakpoint.mdAndUp" cols="2" class="py-2 px-1 d-flex justify-end">
  137. <v-btn class="h100">
  138. {{ $t('search') }}
  139. </v-btn>
  140. </v-col>
  141. </v-row>
  142. <v-row v-show="mapview || $vuetify.breakpoint.smAndDown" class="px-2 pt-2">
  143. <v-btn @click="reinitializeFilters">
  144. {{ $t('reinitialize') }}
  145. </v-btn>
  146. <v-spacer />
  147. <v-btn @click="search">
  148. {{ $t('search') }}
  149. </v-btn>
  150. </v-row>
  151. </v-container>
  152. </v-form>
  153. </v-row>
  154. <div class="pt-4 mt-6">
  155. <!-- loading skeleton -->
  156. <v-container v-if="$fetchState.pending">
  157. <v-row v-for="i in 3" :key="i" justify="space-between" class="mt-1 mb-3">
  158. <v-col v-for="j in 2" :key="j" cols="12" :md="mapview ? 6 : 12" class="py-2 px-1">
  159. <v-skeleton-loader type="card" :loading="true" />
  160. </v-col>
  161. </v-row>
  162. </v-container>
  163. <!-- Results -->
  164. <v-data-iterator
  165. v-else
  166. :items="onMapFilteredStructures"
  167. :page.sync="page"
  168. :items-per-page="itemsPerPage"
  169. sort-by="name"
  170. hide-default-footer
  171. no-data-text=""
  172. >
  173. <template #header>
  174. <i class="results-count">{{ totalRecords }} {{ $t('results') }}</i>
  175. </template>
  176. <template #default="props">
  177. <v-row justify="space-between" class="mt-1 mb-3">
  178. <v-col
  179. v-for="structure in props.items"
  180. :key="structure.name"
  181. cols="12"
  182. sm="12"
  183. :lg="mapview ? 6 : 12"
  184. class="py-2 px-1"
  185. >
  186. <v-card
  187. elevation="1"
  188. outlined
  189. :class="'structure-card pa-3 d-flex ' + ((mapview || $vuetify.breakpoint.smAndDown) ? 'flex-column' : 'flex-row align-items-center')"
  190. >
  191. <div class="d-flex justify-center max-w100" >
  192. <v-img
  193. v-if="structure.logoId"
  194. :src="'https://api.opentalent.fr/app.php/_internal/secure/files/' + structure.logoId"
  195. alt="poster"
  196. height="80"
  197. width="240"
  198. max-height="100%"
  199. :contain="true"
  200. style="margin: 12px;"
  201. />
  202. <div v-else style="height: 104px; width: 264px"></div>
  203. </div>
  204. <div :class="'d-flex flex-column' + (listview ? ' flex-grow-1' : '')">
  205. <v-card-title class="title">
  206. <nuxt-link :to="{path: '/structures/' + structure.id, query: { parent: parent, view: view, theme: theme }}">
  207. {{ structure.name }}
  208. </nuxt-link>
  209. </v-card-title>
  210. <v-card-text class="infos">
  211. <table>
  212. <tr>
  213. <td>
  214. <font-awesome-icon class="icon" :icon="['fas', 'map-marker-alt']" />
  215. </td>
  216. <td>
  217. <span v-if="structure.streetAddress">{{ structure.streetAddress }}<br></span>
  218. <span v-if="structure.postalCode" class="postalCode">{{ structure.postalCode }} </span>
  219. {{ structure.addressCity }}
  220. </td>
  221. </tr>
  222. <tr>
  223. <td>
  224. <font-awesome-icon class="icon" :icon="['fas', 'project-diagram']" />
  225. </td>
  226. <td>
  227. <NuxtLink
  228. v-if="structure.n1Id !== parent"
  229. class="neutral"
  230. :to="{path: '/structures/' + structure.n1Id, query: { parent: parent, view: view, theme: theme }}"
  231. nuxt
  232. >
  233. {{ structure.n1Name }}
  234. </NuxtLink>
  235. <div v-else>
  236. {{ structure.n1Name }}
  237. </div>
  238. </td>
  239. </tr>
  240. </table>
  241. </v-card-text>
  242. </div>
  243. <div
  244. v-if="structure.practices"
  245. class="d-grid flex-wrap"
  246. :style="listview ? 'width: 25%;min-width: 25%;' : ''"
  247. >
  248. <v-chip
  249. v-for="practice in structure.practices"
  250. :key="practice"
  251. class="ma-1"
  252. label
  253. small
  254. >
  255. {{ $t(practice) }}
  256. </v-chip>
  257. </div>
  258. <span v-if="mapview" class="flex-fill" />
  259. <v-card-actions :class="listview ? 'align-self-end' : ''">
  260. <v-btn
  261. class="see"
  262. :to="{path: '/structures/' + structure.id, query: { parent: parent, view: view, theme: theme }}"
  263. nuxt
  264. >
  265. <span style="margin-right: 6px;">{{ $t("see_more") }}</span>
  266. <font-awesome-icon :icon="['fa', 'caret-right']" />
  267. </v-btn>
  268. </v-card-actions>
  269. </v-card>
  270. </v-col>
  271. </v-row>
  272. </template>
  273. <template #footer>
  274. <v-pagination
  275. v-model="page"
  276. :length="pageCount"
  277. total-visible="9"
  278. color="primary"
  279. />
  280. </template>
  281. </v-data-iterator>
  282. </div>
  283. </v-col>
  284. </v-row>
  285. </LayoutContainer>
  286. </template>
  287. <script lang="ts">
  288. import Vue from 'vue'
  289. import { LatLngBounds } from 'leaflet'
  290. import departments from '@/enums/departments'
  291. import practices from '@/enums/practices'
  292. import sphericDistance from '@/services/utils/geo'
  293. import StructuresProvider from '~/services/data/StructuresProvider'
  294. const CMF_ID = 12097
  295. export default Vue.extend({
  296. validate ({ query }) {
  297. if (!/^\d+$/.test(query.parent as string ?? '')) {
  298. // eslint-disable-next-line no-console
  299. console.error('Missing parameter: parent')
  300. return false
  301. }
  302. if (query.view && !['map', 'list'].includes(query.view as string)) {
  303. // eslint-disable-next-line no-console
  304. console.error('Invalid parameter: view')
  305. return false
  306. }
  307. return true
  308. },
  309. data () {
  310. return {
  311. parent: parseInt(this.$route.query.parent as string),
  312. view: this.$route.query.view ?? 'map',
  313. theme: this.$route.query.theme ?? 'orange',
  314. structures: [] as Array<Structure>,
  315. filteredStructures: [] as Array<Structure>,
  316. federations: [] as Array<{ id: number | null, name: string | null }>,
  317. loading: true,
  318. page: 1,
  319. itemsPerPage: 8,
  320. departments: departments as {code: string, label: string}[],
  321. practices: practices as {id: string}[],
  322. textFilter: null as string | null,
  323. locationFilter: null as Coordinates | null,
  324. practicesFilter: null as string | null,
  325. departmentFilter: null as string | null,
  326. federationFilter: null as number | null,
  327. distanceFilter: null as number | null,
  328. mapBoundsFilter: null as LatLngBounds | null,
  329. mapBoundsFilterStarted: false, // map bounds filter is only activated when the map bounds are updated
  330. mapZoomAtStart: false
  331. }
  332. },
  333. async fetch () {
  334. await new StructuresProvider(this.$axios).getAll(this.parent).then(
  335. (res) => {
  336. this.structures = res
  337. this.filteredStructures = res
  338. // populate federations filter
  339. for (const s of res) {
  340. const f = {
  341. id: s.n1Id,
  342. name: s.n1Name
  343. }
  344. if (!this.federations.includes(f)) {
  345. this.federations.push(f)
  346. }
  347. }
  348. // zoom on map markers (except if the parent structure is the CMF)
  349. if (this.parent !== CMF_ID) {
  350. this.mapZoomAtStart = true
  351. }
  352. })
  353. },
  354. computed: {
  355. onMapFilteredStructures (): Array<Structure> {
  356. if (this.mapview && this.mapBoundsFilterStarted) {
  357. return this.filteredStructures.filter((s) => {
  358. return this.matchMapBounds(s)
  359. })
  360. } else {
  361. return this.filteredStructures
  362. }
  363. },
  364. totalRecords (): number {
  365. return this.onMapFilteredStructures.length
  366. },
  367. pageCount (): number {
  368. return Math.floor(this.totalRecords / this.itemsPerPage) + 1
  369. },
  370. mapview (): boolean {
  371. return this.view === 'map'
  372. },
  373. listview (): boolean {
  374. return this.view === 'list'
  375. },
  376. translatedPractices (): Array<{ id: string, label: string }> {
  377. const tPractices = []
  378. for (const practice of this.practices) {
  379. tPractices.push({ id: practice.id, label: this.$t(practice.id) as string })
  380. }
  381. return tPractices
  382. }
  383. },
  384. methods: {
  385. viewChanged (e: number) {
  386. this.view = (e === 0) ? 'map' : 'list'
  387. if (this.mapview) {
  388. this.mapZoomAtStart = true
  389. }
  390. },
  391. textFilterChanged (newVal: string) {
  392. this.textFilter = newVal
  393. },
  394. locationFilterChanged (newVal: Coordinates) {
  395. this.locationFilter = newVal
  396. if (this.distanceFilter === null) {
  397. this.distanceFilter = 10
  398. }
  399. this.search()
  400. },
  401. resetMap (): void {
  402. const map = this.$refs.map as any
  403. map.resetBounds()
  404. if (this.parent !== CMF_ID) {
  405. this.fitMapToResults()
  406. }
  407. this.mapBoundsFilterStarted = false
  408. },
  409. fitMapToResults (): void {
  410. (this.$refs.map as any).zoomOnResults()
  411. },
  412. mapBoundsFilterChanged (newBounds: LatLngBounds) {
  413. this.mapBoundsFilterStarted = true
  414. this.mapBoundsFilter = newBounds
  415. },
  416. reinitializeFilters (): void {
  417. this.textFilter = null
  418. this.locationFilter = null
  419. this.practicesFilter = null
  420. this.departmentFilter = null
  421. this.federationFilter = null
  422. this.distanceFilter = null
  423. this.mapBoundsFilter = null
  424. const addressSearch = this.$refs.addressSearch as any
  425. addressSearch.clear()
  426. this.resetMap()
  427. this.filteredStructures = this.structures
  428. },
  429. /**
  430. * Does the structure match the current textFilter
  431. * @param structure
  432. * @returns {boolean}
  433. */
  434. matchTextFilter (structure: Structure): boolean {
  435. if (!this.textFilter) { return true }
  436. return structure.name.toLowerCase().includes(this.textFilter.toLowerCase())
  437. },
  438. /**
  439. * Does the structure match the current locationFilter
  440. * @param structure
  441. * @returns {boolean}
  442. */
  443. matchLocationFilter (structure: Structure): boolean {
  444. if (!this.locationFilter) { return true }
  445. if (!structure.latitude || !structure.longitude) { return false }
  446. const radius = Number(this.distanceFilter) ?? 5
  447. return sphericDistance(
  448. this.locationFilter.latitude, this.locationFilter.longitude, structure.latitude, structure.longitude
  449. ) <= radius
  450. },
  451. /**
  452. * Does the structure match the current practicesFilter
  453. * @param structure
  454. * @returns {boolean}
  455. */
  456. matchPracticesFilter (structure: Structure): boolean {
  457. if (!this.practicesFilter) { return true }
  458. return structure.practices && structure.practices.includes(this.practicesFilter)
  459. },
  460. /**
  461. * Does the structure match the current departmentFilter
  462. * @param structure
  463. * @returns {boolean}
  464. */
  465. matchDepartmentFilter (structure: Structure): boolean {
  466. if (!this.departmentFilter) { return true }
  467. return structure.postalCode !== null &&
  468. (
  469. structure.postalCode.startsWith(this.departmentFilter) ||
  470. (['2A', '2B'].includes(this.departmentFilter) && structure.postalCode.startsWith('20'))
  471. )
  472. },
  473. /**
  474. * Does the structure match the current federationFilter
  475. * @param structure
  476. * @returns {boolean}
  477. */
  478. matchFederationFilter (structure: Structure): boolean {
  479. if (!this.federationFilter) { return true }
  480. return structure.parents.includes(this.federationFilter)
  481. },
  482. /**
  483. * Does the structure match the current federationFilter
  484. * @param structure
  485. * @returns {boolean}
  486. */
  487. matchMapBounds (structure: Structure): boolean {
  488. if (!this.mapBoundsFilter) { return true }
  489. if (!(structure.latitude && structure.longitude)) { return false }
  490. return this.mapBoundsFilter.getSouth() <= structure.latitude &&
  491. structure.latitude <= this.mapBoundsFilter.getNorth() &&
  492. this.mapBoundsFilter.getWest() <= structure.longitude &&
  493. structure.longitude <= this.mapBoundsFilter.getEast()
  494. },
  495. /**
  496. * Does the structure match each of the page filters
  497. * @param structure
  498. * @returns {boolean}
  499. */
  500. matchFilters (structure: Structure): boolean {
  501. return this.matchTextFilter(structure) &&
  502. this.matchLocationFilter(structure) &&
  503. this.matchPracticesFilter(structure) &&
  504. this.matchDepartmentFilter(structure) &&
  505. this.matchFederationFilter(structure)
  506. },
  507. /**
  508. * Update the filteredStructures array
  509. */
  510. search (): void {
  511. this.filteredStructures = this.structures.filter((s) => { return this.matchFilters(s) })
  512. if (this.mapview) {
  513. this.fitMapToResults()
  514. }
  515. },
  516. autocompleteSearchNormalize (s: string): string {
  517. return s
  518. .toLowerCase()
  519. .replace(/[éèẽëê]/g, 'e')
  520. .replace(/[ç]/g, 'c')
  521. .replace(/[îïĩ]/g, 'i')
  522. .replace(/[àã]/g, 'a')
  523. .replace(/[öôõ]/g, 'o')
  524. .replace(/[ûüũ]/g, 'u')
  525. .replace(/[-]/g, ' ')
  526. .trim()
  527. },
  528. /**
  529. * Enhanced filter for v-autocomplete components
  530. *
  531. * @param _
  532. * @param queryText
  533. * @param itemText
  534. */
  535. enhancedAutocompleteFilter (_: any, queryText: string, itemText: string): boolean {
  536. return this.autocompleteSearchNormalize(itemText).includes(this.autocompleteSearchNormalize(queryText))
  537. }
  538. }
  539. })
  540. </script>
  541. <style scoped lang="scss">
  542. @import 'assets/style/variables.scss';
  543. h2 {
  544. color: var(--v-primary-base);
  545. }
  546. .structure-card {
  547. height: 100%;
  548. color: #666666;
  549. }
  550. .infos .col {
  551. padding: 6px 12px;
  552. }
  553. .infos td {
  554. padding: 4px;
  555. vertical-align: top;
  556. }
  557. .infos td:first-child {
  558. padding-top: 6px;
  559. text-align: center;
  560. }
  561. .title {
  562. word-break: normal;
  563. color: var(--v-primary-base);
  564. font-size: 18px;
  565. font-weight: 500;
  566. line-height: 1.6rem;
  567. }
  568. .title a {
  569. text-decoration: none;
  570. }
  571. .icon {
  572. color: var(--v-primary-base);
  573. }
  574. .results-count {
  575. font-size: .8em;
  576. color: #666;
  577. }
  578. </style>