index.vue 20 KB

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