index.vue 20 KB

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