index.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. <v-skeleton-loader
  204. v-else
  205. type="card-avatar"
  206. boilerplate
  207. width="164px"
  208. height="84px"
  209. style="margin: 10px 50px;"
  210. />
  211. </div>
  212. <div :class="'d-flex flex-column' + (listview ? ' flex-grow-1' : '')">
  213. <v-card-title class="title">
  214. <nuxt-link :to="{path: '/structures/' + structure.id, query: { parent: parent, view: view, theme: theme }}">
  215. {{ structure.name }}
  216. </nuxt-link>
  217. </v-card-title>
  218. <v-card-text class="infos">
  219. <table>
  220. <tr>
  221. <td>
  222. <font-awesome-icon class="icon" :icon="['fas', 'map-marker-alt']" />
  223. </td>
  224. <td>
  225. <span v-if="structure.streetAddress">{{ structure.streetAddress }}<br></span>
  226. <span v-if="structure.postalCode" class="postalCode">{{ structure.postalCode }} </span>
  227. {{ structure.addressCity }}
  228. </td>
  229. </tr>
  230. <tr>
  231. <td>
  232. <font-awesome-icon class="icon" :icon="['fas', 'project-diagram']" />
  233. </td>
  234. <td>
  235. <NuxtLink
  236. v-if="structure.n1Id !== parent"
  237. class="neutral"
  238. :to="{path: '/structures/' + structure.n1Id, query: { parent: parent, view: view, theme: theme }}"
  239. nuxt
  240. >
  241. {{ structure.n1Name }}
  242. </NuxtLink>
  243. <div v-else>
  244. {{ structure.n1Name }}
  245. </div>
  246. </td>
  247. </tr>
  248. </table>
  249. </v-card-text>
  250. </div>
  251. <div
  252. v-if="structure.practices"
  253. class="d-grid flex-wrap"
  254. :style="listview ? 'width: 25%;min-width: 25%;' : ''"
  255. >
  256. <v-chip
  257. v-for="practice in structure.practices"
  258. :key="practice"
  259. class="ma-1"
  260. label
  261. small
  262. >
  263. {{ $t(practice) }}
  264. </v-chip>
  265. </div>
  266. <span v-if="mapview" class="flex-fill" />
  267. <v-card-actions :class="listview ? 'align-self-end' : ''">
  268. <v-btn
  269. class="see"
  270. :to="{path: '/structures/' + structure.id, query: { parent: parent, view: view, theme: theme }}"
  271. nuxt
  272. >
  273. <span style="margin-right: 6px;">{{ $t("see_more") }}</span>
  274. <font-awesome-icon :icon="['fa', 'caret-right']" />
  275. </v-btn>
  276. </v-card-actions>
  277. </v-card>
  278. </v-col>
  279. </v-row>
  280. </template>
  281. <template #footer>
  282. <v-pagination
  283. v-model="page"
  284. :length="pageCount"
  285. total-visible="9"
  286. color="primary"
  287. />
  288. </template>
  289. </v-data-iterator>
  290. </div>
  291. </v-col>
  292. </v-row>
  293. </LayoutContainer>
  294. </template>
  295. <script lang="ts">
  296. import Vue from 'vue'
  297. import { LatLngBounds } from 'leaflet'
  298. import departments from '@/enums/departments'
  299. import practices from '@/enums/practices'
  300. import sphericDistance from '@/services/utils/geo'
  301. import StructuresProvider from '~/services/data/StructuresProvider'
  302. const CMF_ID = 12097
  303. export default Vue.extend({
  304. validate ({ query }) {
  305. if (!/^\d+$/.test(query.parent as string ?? '')) {
  306. // eslint-disable-next-line no-console
  307. console.error('Missing parameter: parent')
  308. return false
  309. }
  310. if (query.view && !['map', 'list'].includes(query.view as string)) {
  311. // eslint-disable-next-line no-console
  312. console.error('Invalid parameter: view')
  313. return false
  314. }
  315. return true
  316. },
  317. data () {
  318. return {
  319. parent: parseInt(this.$route.query.parent as string),
  320. view: this.$route.query.view ?? 'map',
  321. theme: this.$route.query.theme ?? 'orange',
  322. structures: [] as Array<Structure>,
  323. filteredStructures: [] as Array<Structure>,
  324. federations: [] as Array<{ id: number | null, name: string | null }>,
  325. loading: true,
  326. page: 1,
  327. itemsPerPage: 8,
  328. departments: departments as {code: string, label: string}[],
  329. practices: practices as {id: string}[],
  330. textFilter: null as string | null,
  331. locationFilter: null as Coordinates | null,
  332. practicesFilter: null as string | null,
  333. departmentFilter: null as string | null,
  334. federationFilter: null as number | null,
  335. distanceFilter: null as number | null,
  336. mapBoundsFilter: null as LatLngBounds | null,
  337. mapBoundsFilterStarted: false, // map bounds filter is only activated when the map bounds are updated
  338. mapZoomExpected: false
  339. }
  340. },
  341. async fetch () {
  342. await new StructuresProvider(this.$axios).getAll(this.parent).then(
  343. (res) => {
  344. this.structures = res
  345. this.filteredStructures = res
  346. // populate federations filter
  347. for (const s of res) {
  348. const f = {
  349. id: s.n1Id,
  350. name: s.n1Name
  351. }
  352. if (!this.federations.includes(f)) {
  353. this.federations.push(f)
  354. }
  355. }
  356. })
  357. },
  358. computed: {
  359. parentIsCmf (): Boolean {
  360. return this.parent === CMF_ID
  361. },
  362. onMapFilteredStructures (): Array<Structure> {
  363. if (this.mapview && this.mapBoundsFilterStarted) {
  364. return this.filteredStructures.filter((s) => {
  365. return this.matchMapBounds(s)
  366. })
  367. } else {
  368. return this.filteredStructures
  369. }
  370. },
  371. totalRecords (): number {
  372. return this.onMapFilteredStructures.length
  373. },
  374. pageCount (): number {
  375. return Math.floor(this.totalRecords / this.itemsPerPage) + 1
  376. },
  377. mapview (): boolean {
  378. return this.view === 'map'
  379. },
  380. listview (): boolean {
  381. return this.view === 'list'
  382. },
  383. translatedPractices (): Array<{ id: string, label: string }> {
  384. const tPractices = []
  385. for (const practice of this.practices) {
  386. tPractices.push({ id: practice.id, label: this.$t(practice.id) as string })
  387. }
  388. return tPractices
  389. }
  390. },
  391. methods: {
  392. viewChanged (e: number) {
  393. this.view = (e === 0) ? 'map' : 'list'
  394. },
  395. mapMounted () {
  396. // zoom on map markers and set it as the default view (except if the parent structure is the CMF)
  397. if (!this.parentIsCmf) {
  398. this.fitMapToResults(true)
  399. }
  400. },
  401. mapPopulated () {
  402. // zoom on map markers (except if the parent structure is the CMF)
  403. if (!this.parentIsCmf) {
  404. this.fitMapToResults()
  405. }
  406. },
  407. textFilterChanged (newVal: string) {
  408. this.textFilter = newVal
  409. },
  410. locationFilterChanged (newVal: Coordinates) {
  411. this.locationFilter = newVal
  412. if (this.distanceFilter === null) {
  413. this.distanceFilter = 10
  414. }
  415. this.search()
  416. },
  417. resetMap (): void {
  418. (this.$refs.map as any).resetBounds()
  419. this.mapBoundsFilterStarted = false
  420. },
  421. fitMapToResults (setAsDefault = false): void {
  422. (this.$refs.map as any).fitNextResults(setAsDefault)
  423. },
  424. mapBoundsFilterChanged (newBounds: LatLngBounds) {
  425. this.mapBoundsFilterStarted = true
  426. this.mapBoundsFilter = newBounds
  427. },
  428. reinitializeFilters (): void {
  429. this.textFilter = null
  430. this.locationFilter = null
  431. this.practicesFilter = null
  432. this.departmentFilter = null
  433. this.federationFilter = null
  434. this.distanceFilter = null
  435. this.mapBoundsFilter = null
  436. const addressSearch = this.$refs.addressSearch as any
  437. addressSearch.clear()
  438. if (this.mapview) {
  439. this.resetMap()
  440. }
  441. this.filteredStructures = this.structures
  442. },
  443. /**
  444. * Does the structure match the current textFilter
  445. * @param structure
  446. * @returns {boolean}
  447. */
  448. matchTextFilter (structure: Structure): boolean {
  449. if (!this.textFilter) { return true }
  450. return this.searchTextNormalize(structure.name).includes(this.searchTextNormalize(this.textFilter))
  451. },
  452. /**
  453. * Does the structure match the current locationFilter
  454. * @param structure
  455. * @returns {boolean}
  456. */
  457. matchLocationFilter (structure: Structure): boolean {
  458. if (!this.locationFilter) { return true }
  459. if (!structure.latitude || !structure.longitude) { return false }
  460. const radius = Number(this.distanceFilter) ?? 5
  461. return sphericDistance(
  462. this.locationFilter.latitude, this.locationFilter.longitude, structure.latitude, structure.longitude
  463. ) <= radius
  464. },
  465. /**
  466. * Does the structure match the current practicesFilter
  467. * @param structure
  468. * @returns {boolean}
  469. */
  470. matchPracticesFilter (structure: Structure): boolean {
  471. if (!this.practicesFilter) { return true }
  472. return structure.practices && structure.practices.includes(this.practicesFilter)
  473. },
  474. /**
  475. * Does the structure match the current departmentFilter
  476. * @param structure
  477. * @returns {boolean}
  478. */
  479. matchDepartmentFilter (structure: Structure): boolean {
  480. if (!this.departmentFilter) { return true }
  481. return structure.postalCode !== null &&
  482. (
  483. structure.postalCode.startsWith(this.departmentFilter) ||
  484. (['2A', '2B'].includes(this.departmentFilter) && structure.postalCode.startsWith('20'))
  485. )
  486. },
  487. /**
  488. * Does the structure match the current federationFilter
  489. * @param structure
  490. * @returns {boolean}
  491. */
  492. matchFederationFilter (structure: Structure): boolean {
  493. if (!this.federationFilter) { return true }
  494. return structure.parents.includes(this.federationFilter)
  495. },
  496. /**
  497. * Does the structure match the current federationFilter
  498. * @param structure
  499. * @returns {boolean}
  500. */
  501. matchMapBounds (structure: Structure): boolean {
  502. if (!this.mapBoundsFilter) { return true }
  503. if (!(structure.latitude && structure.longitude)) { return false }
  504. return this.mapBoundsFilter.getSouth() <= structure.latitude &&
  505. structure.latitude <= this.mapBoundsFilter.getNorth() &&
  506. this.mapBoundsFilter.getWest() <= structure.longitude &&
  507. structure.longitude <= this.mapBoundsFilter.getEast()
  508. },
  509. /**
  510. * Does the structure match each of the page filters
  511. * @param structure
  512. * @returns {boolean}
  513. */
  514. matchFilters (structure: Structure): boolean {
  515. return this.matchTextFilter(structure) &&
  516. this.matchLocationFilter(structure) &&
  517. this.matchPracticesFilter(structure) &&
  518. this.matchDepartmentFilter(structure) &&
  519. this.matchFederationFilter(structure)
  520. },
  521. /**
  522. * Update the filteredStructures array
  523. */
  524. search (): void {
  525. this.filteredStructures = this.structures.filter((s) => { return this.matchFilters(s) })
  526. if (this.mapview) {
  527. this.fitMapToResults()
  528. }
  529. },
  530. searchTextNormalize (s: string): string {
  531. return s
  532. .toLowerCase()
  533. .replace(/[éèẽëê]/g, 'e')
  534. .replace(/[ç]/g, 'c')
  535. .replace(/[îïĩ]/g, 'i')
  536. .replace(/[àã]/g, 'a')
  537. .replace(/[öôõ]/g, 'o')
  538. .replace(/[ûüũ]/g, 'u')
  539. .replace(/[-]/g, ' ')
  540. .trim()
  541. },
  542. /**
  543. * Enhanced filter for v-autocomplete components
  544. *
  545. * @param _
  546. * @param queryText
  547. * @param itemText
  548. */
  549. enhancedAutocompleteFilter (_: any, queryText: string, itemText: string): boolean {
  550. return this.searchTextNormalize(itemText).includes(this.searchTextNormalize(queryText))
  551. }
  552. }
  553. })
  554. </script>
  555. <style scoped lang="scss">
  556. @import 'assets/style/variables.scss';
  557. h2 {
  558. color: var(--v-primary-base);
  559. }
  560. .structure-card {
  561. height: 100%;
  562. color: #666666;
  563. }
  564. .infos .col {
  565. padding: 6px 12px;
  566. }
  567. .infos td {
  568. padding: 4px;
  569. vertical-align: top;
  570. }
  571. .infos td:first-child {
  572. padding-top: 6px;
  573. text-align: center;
  574. }
  575. .title {
  576. word-break: normal;
  577. color: var(--v-primary-base);
  578. font-size: 18px;
  579. font-weight: 500;
  580. line-height: 1.6rem;
  581. }
  582. .title a {
  583. text-decoration: none;
  584. }
  585. .icon {
  586. color: var(--v-primary-base);
  587. }
  588. .results-count {
  589. font-size: .8em;
  590. color: #666;
  591. }
  592. </style>