AdvancedTable.vue 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  1. <template>
  2. <div>
  3. <div
  4. class="table-outer-container"
  5. @mousemove="columnResizing($event)"
  6. @touchmove="columnResizing($event)"
  7. >
  8. <div class="table-header">
  9. <div>
  10. <tippy
  11. v-if="filters.length > 0"
  12. :touch="true"
  13. :interactive="true"
  14. placement="bottom-start"
  15. theme="search"
  16. ref="search"
  17. trigger="click"
  18. @show="
  19. () => {
  20. showFiltersDropdown = true;
  21. }
  22. "
  23. @hide="
  24. () => {
  25. showFiltersDropdown = false;
  26. }
  27. "
  28. >
  29. <div class="control has-addons" ref="trigger">
  30. <button class="button is-primary">
  31. <i class="material-icons icon-with-button"
  32. >filter_list</i
  33. >
  34. Filters
  35. </button>
  36. <button class="button">
  37. <i class="material-icons">
  38. {{
  39. showFiltersDropdown
  40. ? "expand_more"
  41. : "expand_less"
  42. }}
  43. </i>
  44. </button>
  45. </div>
  46. <template #content>
  47. <div class="control is-grouped input-with-button">
  48. <p class="control select is-expanded">
  49. <select v-model="addFilterValue">
  50. <option
  51. v-for="type in filters"
  52. :key="type.name"
  53. :value="type"
  54. >
  55. {{ type.displayName }}
  56. </option>
  57. </select>
  58. </p>
  59. <p class="control">
  60. <button
  61. :disabled="!addFilterValue"
  62. class="button material-icons is-success"
  63. @click="addFilterItem()"
  64. >
  65. control_point
  66. </button>
  67. </p>
  68. </div>
  69. <div
  70. v-for="(filter, index) in editingFilters"
  71. :key="`filter-${index}`"
  72. class="
  73. advanced-filter
  74. control
  75. is-grouped is-expanded
  76. "
  77. >
  78. <div class="control select">
  79. <select
  80. v-model="filter.filter"
  81. @change="changeFilterType(index)"
  82. >
  83. <option
  84. v-for="type in filters"
  85. :key="type.name"
  86. :value="type"
  87. >
  88. {{ type.displayName }}
  89. </option>
  90. </select>
  91. </div>
  92. <div class="control select">
  93. <select
  94. v-model="filter.filterType"
  95. :disabled="!filter.filterType"
  96. >
  97. <option
  98. v-for="filterType in filterTypes(
  99. filter.filter
  100. )"
  101. :key="filterType.name"
  102. :value="filterType"
  103. :selected="
  104. filter.filter
  105. .defaultFilterType ===
  106. filterType.name
  107. "
  108. >
  109. {{ filterType.displayName }}
  110. </option>
  111. </select>
  112. </div>
  113. <div
  114. v-if="
  115. filter.filterType.name &&
  116. filter.filterType.name === 'array'
  117. "
  118. class="control is-expanded select"
  119. >
  120. <select
  121. v-model="filter.data"
  122. :disabled="!filter.filterType"
  123. >
  124. <option
  125. v-for="filterValue in filter.filter
  126. .filterValues"
  127. :key="filterValue"
  128. :value="filterValue"
  129. >
  130. {{ filterValue }}
  131. </option>
  132. </select>
  133. </div>
  134. <p v-else class="control is-expanded">
  135. <input
  136. v-if="
  137. filter.filterType.name &&
  138. filter.filterType.name.startsWith(
  139. 'datetime'
  140. )
  141. "
  142. v-model="filter.data"
  143. class="input"
  144. type="datetime-local"
  145. />
  146. <input
  147. v-else-if="
  148. filter.filterType.name &&
  149. filter.filterType.name.startsWith(
  150. 'number'
  151. )
  152. "
  153. v-model="filter.data"
  154. class="input"
  155. type="number"
  156. :disabled="!filter.filterType"
  157. @keydown.enter="applyFilterAndGetData()"
  158. />
  159. <input
  160. v-else
  161. v-model="filter.data"
  162. class="input"
  163. type="text"
  164. placeholder="Search value"
  165. :disabled="!filter.filterType"
  166. @keydown.enter="applyFilterAndGetData()"
  167. />
  168. </p>
  169. <div class="control">
  170. <button
  171. class="button material-icons is-danger"
  172. @click="removeFilterItem(index)"
  173. >
  174. remove_circle_outline
  175. </button>
  176. </div>
  177. </div>
  178. <div
  179. v-if="editingFilters.length > 0"
  180. class="control is-expanded is-grouped"
  181. >
  182. <label class="control label"
  183. >Filter operator</label
  184. >
  185. <div class="control select is-expanded">
  186. <select v-model="filterOperator">
  187. <option
  188. v-for="operator in filterOperators"
  189. :key="operator.name"
  190. :value="operator.name"
  191. >
  192. {{ operator.displayName }}
  193. </option>
  194. </select>
  195. </div>
  196. </div>
  197. <div
  198. class="advanced-filter-bottom"
  199. v-if="editingFilters.length > 0"
  200. >
  201. <div class="control is-expanded">
  202. <button
  203. class="button is-info"
  204. @click="applyFilterAndGetData()"
  205. >
  206. <i
  207. class="
  208. material-icons
  209. icon-with-button
  210. "
  211. >filter_list</i
  212. >
  213. Apply filters
  214. </button>
  215. </div>
  216. </div>
  217. <div
  218. class="advanced-filter-bottom"
  219. v-else-if="editingFilters.length === 0"
  220. >
  221. <div class="control is-expanded">
  222. <button
  223. class="button is-info"
  224. @click="applyFilterAndGetData()"
  225. >
  226. <i
  227. class="
  228. material-icons
  229. icon-with-button
  230. "
  231. >filter_list</i
  232. >
  233. Apply filters
  234. </button>
  235. </div>
  236. </div>
  237. </template>
  238. </tippy>
  239. <tippy
  240. v-if="appliedFilters.length > 0"
  241. :touch="true"
  242. :interactive="true"
  243. theme="info"
  244. ref="activeFilters"
  245. >
  246. <div class="filters-indicator">
  247. {{ appliedFilters.length }}
  248. <i class="material-icons" @click.prevent="true"
  249. >filter_list</i
  250. >
  251. </div>
  252. <template #content>
  253. <p
  254. v-for="(filter, index) in appliedFilters"
  255. :key="`filter-${index}`"
  256. >
  257. {{ filter.filter.displayName }}
  258. {{
  259. appliedFilters.length === 1 &&
  260. appliedFilterOperator === "nor"
  261. ? "not"
  262. : ""
  263. }}
  264. {{
  265. filter.filterType.displayName.toLowerCase()
  266. }}
  267. "{{ filter.data }}"
  268. {{
  269. appliedFilters.length === index + 1
  270. ? ""
  271. : appliedFilterOperator
  272. }}
  273. </p>
  274. </template>
  275. </tippy>
  276. <i
  277. v-else
  278. class="filters-indicator material-icons"
  279. content="No active filters"
  280. v-tippy="{ theme: 'info' }"
  281. >
  282. filter_list_off
  283. </i>
  284. </div>
  285. <div>
  286. <tippy
  287. v-if="hidableSortedColumns.length > 0"
  288. :touch="true"
  289. :interactive="true"
  290. placement="bottom-end"
  291. theme="dropdown"
  292. ref="editColumns"
  293. trigger="click"
  294. @show="
  295. () => {
  296. showColumnsDropdown = true;
  297. }
  298. "
  299. @hide="
  300. () => {
  301. showColumnsDropdown = false;
  302. }
  303. "
  304. >
  305. <div class="control has-addons" ref="trigger">
  306. <button class="button is-primary">
  307. <i class="material-icons icon-with-button"
  308. >tune</i
  309. >
  310. Columns
  311. </button>
  312. <button class="button">
  313. <i class="material-icons">
  314. {{
  315. showColumnsDropdown
  316. ? "expand_more"
  317. : "expand_less"
  318. }}
  319. </i>
  320. </button>
  321. </div>
  322. <template #content>
  323. <draggable
  324. item-key="name"
  325. v-model="orderedColumns"
  326. v-bind="columnDragOptions"
  327. tag="div"
  328. draggable=".item-draggable"
  329. class="nav-dropdown-items"
  330. @change="columnOrderChanged"
  331. >
  332. <template #item="{ element: column }">
  333. <button
  334. v-if="
  335. column.name !== 'select' &&
  336. column.name !== 'placeholder'
  337. "
  338. :class="{
  339. sortable: column.sortable,
  340. 'item-draggable': column.draggable,
  341. 'nav-item': true
  342. }"
  343. @click.prevent="
  344. toggleColumnVisibility(column)
  345. "
  346. >
  347. <p
  348. class="
  349. control
  350. is-expanded
  351. checkbox-control
  352. "
  353. >
  354. <label class="switch">
  355. <input
  356. type="checkbox"
  357. :id="index"
  358. :checked="
  359. shownColumns.indexOf(
  360. column.name
  361. ) !== -1
  362. "
  363. @click="
  364. toggleColumnVisibility(
  365. column
  366. )
  367. "
  368. />
  369. <span
  370. :class="{
  371. slider: true,
  372. round: true,
  373. disabled:
  374. !column.hidable
  375. }"
  376. ></span>
  377. </label>
  378. <label :for="index">
  379. <span></span>
  380. <p>{{ column.displayName }}</p>
  381. </label>
  382. </p>
  383. </button>
  384. </template>
  385. </draggable>
  386. </template>
  387. </tippy>
  388. </div>
  389. </div>
  390. <div class="table-container">
  391. <table
  392. :class="{
  393. table: true,
  394. 'has-checkboxes': hasCheckboxes
  395. }"
  396. >
  397. <thead>
  398. <draggable
  399. item-key="name"
  400. v-model="orderedColumns"
  401. v-bind="columnDragOptions"
  402. tag="tr"
  403. handle=".handle"
  404. draggable=".item-draggable"
  405. @change="columnOrderChanged"
  406. >
  407. <template #item="{ element: column }">
  408. <th
  409. v-if="
  410. !(
  411. column.name === 'select' &&
  412. rows.length === 0
  413. ) &&
  414. shownColumns.indexOf(column.name) !== -1
  415. "
  416. :class="{
  417. sortable: column.sortable,
  418. 'item-draggable': column.draggable
  419. }"
  420. :style="{
  421. minWidth: Number.isNaN(column.minWidth)
  422. ? column.minWidth
  423. : `${column.minWidth}px`,
  424. width: Number.isNaN(column.width)
  425. ? column.width
  426. : `${column.width}px`,
  427. maxWidth: Number.isNaN(column.maxWidth)
  428. ? column.maxWidth
  429. : `${column.maxWidth}px`
  430. }"
  431. >
  432. <div v-if="column.name === 'select'">
  433. <p class="checkbox">
  434. <input
  435. type="checkbox"
  436. :checked="
  437. rows.length ===
  438. selectedRows.length
  439. "
  440. @click="toggleAllRows()"
  441. />
  442. </p>
  443. </div>
  444. <div v-else class="handle">
  445. <span>
  446. {{ column.displayName }}
  447. </span>
  448. <span
  449. v-if="column.sortable"
  450. :content="`Sort by ${column.displayName}`"
  451. v-tippy
  452. >
  453. <span
  454. v-if="
  455. !sort[column.sortProperty]
  456. "
  457. class="material-icons"
  458. @click="changeSort(column)"
  459. >
  460. unfold_more
  461. </span>
  462. <span
  463. v-if="
  464. sort[
  465. column.sortProperty
  466. ] === 'ascending'
  467. "
  468. class="material-icons active"
  469. @click="changeSort(column)"
  470. >
  471. expand_more
  472. </span>
  473. <span
  474. v-if="
  475. sort[
  476. column.sortProperty
  477. ] === 'descending'
  478. "
  479. class="material-icons active"
  480. @click="changeSort(column)"
  481. >
  482. expand_less
  483. </span>
  484. </span>
  485. </div>
  486. <div
  487. class="resizer"
  488. v-if="column.resizable"
  489. @mousedown.prevent.stop="
  490. columnResizingStart(column, $event)
  491. "
  492. @touchstart.prevent.stop="
  493. columnResizingStart(column, $event)
  494. "
  495. @mouseup="columnResizingStop()"
  496. @touchend="columnResizingStop()"
  497. @dblclick="columnResetWidth(column)"
  498. ></div>
  499. </th>
  500. </template>
  501. </draggable>
  502. </thead>
  503. <tbody>
  504. <tr
  505. v-for="(item, itemIndex) in rows"
  506. :key="item._id"
  507. :class="{
  508. selected: item.selected,
  509. highlighted: item.highlighted,
  510. updated: item.updated,
  511. removed: item.removed
  512. }"
  513. >
  514. <td
  515. v-for="column in sortedFilteredColumns"
  516. :key="`${item._id}-${column.name}`"
  517. >
  518. <slot
  519. :name="`column-${column.name}`"
  520. :item="item"
  521. v-if="
  522. column.properties.length === 0 ||
  523. column.properties.every(property =>
  524. property
  525. .split('.')
  526. .reduce(
  527. (previous, current) =>
  528. (previous &&
  529. previous[
  530. current
  531. ]) ||
  532. null,
  533. item
  534. )
  535. )
  536. "
  537. ></slot>
  538. <p
  539. class="checkbox"
  540. v-if="column.name === 'select'"
  541. >
  542. <input
  543. type="checkbox"
  544. :checked="item.selected"
  545. @click="
  546. toggleSelectedRow(itemIndex, $event)
  547. "
  548. />
  549. </p>
  550. <div
  551. class="resizer"
  552. v-if="column.resizable"
  553. @mousedown.prevent.stop="
  554. columnResizingStart(column, $event)
  555. "
  556. @touchstart.prevent.stop="
  557. columnResizingStart(column, $event)
  558. "
  559. @mouseup="columnResizingStop()"
  560. @touchend="columnResizingStop()"
  561. @dblclick="columnResetWidth(column)"
  562. ></div>
  563. </td>
  564. </tr>
  565. </tbody>
  566. </table>
  567. </div>
  568. <div class="table-footer">
  569. <div class="page-controls">
  570. <button
  571. :class="{ disabled: page === 1 }"
  572. class="button is-primary material-icons"
  573. :disabled="page === 1"
  574. @click="changePage(1)"
  575. content="First Page"
  576. v-tippy
  577. >
  578. skip_previous
  579. </button>
  580. <button
  581. :class="{ disabled: page === 1 }"
  582. class="button is-primary material-icons"
  583. :disabled="page === 1"
  584. @click="changePage(page - 1)"
  585. content="Previous Page"
  586. v-tippy
  587. >
  588. fast_rewind
  589. </button>
  590. <p>Page {{ page }} / {{ lastPage }}</p>
  591. <button
  592. :class="{ disabled: page === lastPage }"
  593. class="button is-primary material-icons"
  594. :disabled="page === lastPage"
  595. @click="changePage(page + 1)"
  596. content="Next Page"
  597. v-tippy
  598. >
  599. fast_forward
  600. </button>
  601. <button
  602. :class="{ disabled: page === lastPage }"
  603. class="button is-primary material-icons"
  604. :disabled="page === lastPage"
  605. @click="changePage(lastPage)"
  606. content="Last Page"
  607. v-tippy
  608. >
  609. skip_next
  610. </button>
  611. </div>
  612. <div class="page-size">
  613. <div class="control">
  614. <label class="label">Items per page</label>
  615. <p class="control select">
  616. <select
  617. v-model.number="pageSize"
  618. @change="changePageSize()"
  619. >
  620. <option value="10">10</option>
  621. <option value="25">25</option>
  622. <option value="50">50</option>
  623. <option value="100">100</option>
  624. <option value="250">250</option>
  625. <option value="500">500</option>
  626. <option value="1000">1000</option>
  627. </select>
  628. </p>
  629. </div>
  630. </div>
  631. </div>
  632. </div>
  633. <div
  634. v-if="hasCheckboxes && selectedRows.length > 0"
  635. class="bulk-popup"
  636. :style="{
  637. top: bulkPopup.top + 'px',
  638. left: bulkPopup.left + 'px'
  639. }"
  640. >
  641. <button
  642. class="button is-primary"
  643. :content="
  644. selectedRows.length === 1
  645. ? `${selectedRows.length} row selected`
  646. : `${selectedRows.length} rows selected`
  647. "
  648. v-tippy="{ theme: 'info' }"
  649. >
  650. {{ selectedRows.length }}
  651. </button>
  652. <slot name="bulk-actions" :item="selectedRows" />
  653. <div class="right">
  654. <slot name="bulk-actions-right" :item="selectedRows" />
  655. <span
  656. class="material-icons drag-icon"
  657. @mousedown.left="onDragBox"
  658. @touchstart="onDragBox"
  659. @dblclick="resetBulkActionsPosition()"
  660. >
  661. drag_indicator
  662. </span>
  663. </div>
  664. </div>
  665. </div>
  666. </template>
  667. <script>
  668. import { mapGetters } from "vuex";
  669. import draggable from "vuedraggable";
  670. import Toast from "toasters";
  671. import ws from "@/ws";
  672. export default {
  673. components: {
  674. draggable
  675. },
  676. props: {
  677. /*
  678. Column properties:
  679. name: Unique lowercase name
  680. displayName: Nice name for the column header
  681. properties: The properties this column needs to show data
  682. sortable: Boolean for whether the order of a particular column can be changed
  683. sortProperty: The property the backend will sort on if this column gets sorted, e.g. title
  684. hidable: Boolean for whether a column can be hidden
  685. defaultVisibility: Default visibility for a column, either "shown" or "hidden"
  686. draggable: Boolean for whether a column can be dragged/reordered,
  687. resizable: Boolean for whether a column can be resized
  688. minWidth: Minimum width of column, e.g. 50px
  689. width: Width of column, e.g. 100px
  690. maxWidth: Maximum width of column, e.g. 150px
  691. */
  692. columnDefault: { type: Object, default: () => {} },
  693. columns: { type: Array, default: null },
  694. filters: { type: Array, default: null },
  695. dataAction: { type: String, default: null },
  696. name: { type: String, default: null },
  697. maxWidth: { type: Number, default: 1880 },
  698. query: { type: Boolean, default: true },
  699. events: { type: Object, default: () => {} }
  700. },
  701. data() {
  702. return {
  703. page: 1,
  704. pageSize: 10,
  705. rows: [],
  706. count: 0, // TODO Rename
  707. sort: {},
  708. orderedColumns: [],
  709. shownColumns: [],
  710. columnDragOptions() {
  711. return {
  712. animation: 200,
  713. group: "columns",
  714. disabled: false,
  715. ghostClass: "draggable-list-ghost",
  716. filter: ".ignore-elements",
  717. fallbackTolerance: 50
  718. };
  719. },
  720. editingFilters: [],
  721. appliedFilters: [],
  722. filterOperator: "or",
  723. appliedFilterOperator: "or",
  724. filterOperators: [
  725. {
  726. name: "or",
  727. displayName: "OR"
  728. },
  729. {
  730. name: "and",
  731. displayName: "AND"
  732. },
  733. {
  734. name: "nor",
  735. displayName: "NOR"
  736. }
  737. ],
  738. resizing: {},
  739. allFilterTypes: {
  740. contains: {
  741. name: "contains",
  742. displayName: "Contains"
  743. },
  744. exact: {
  745. name: "exact",
  746. displayName: "Exact"
  747. },
  748. regex: {
  749. name: "regex",
  750. displayName: "Regex"
  751. },
  752. datetimeBefore: {
  753. name: "datetimeBefore",
  754. displayName: "Before"
  755. },
  756. datetimeAfter: {
  757. name: "datetimeAfter",
  758. displayName: "After"
  759. },
  760. numberLesser: {
  761. name: "numberLesser",
  762. displayName: "Less than"
  763. },
  764. numberGreater: {
  765. name: "numberGreater",
  766. displayName: "Greater than"
  767. },
  768. numberEquals: {
  769. name: "numberEquals",
  770. displayName: "Equals"
  771. },
  772. array: {
  773. name: "array",
  774. displayName: "Is"
  775. }
  776. },
  777. bulkPopup: {
  778. top: 0,
  779. left: 0,
  780. pos1: 0,
  781. pos2: 0,
  782. pos3: 0,
  783. pos4: 0
  784. },
  785. addFilterValue: null,
  786. showFiltersDropdown: false,
  787. showColumnsDropdown: false,
  788. lastColumnResizerTapped: null,
  789. lastColumnResizerTappedDate: 0,
  790. lastBulkActionsTappedDate: 0
  791. };
  792. },
  793. computed: {
  794. properties() {
  795. return Array.from(
  796. new Set(
  797. this.sortedFilteredColumns.flatMap(
  798. column => column.properties
  799. )
  800. )
  801. );
  802. },
  803. lastPage() {
  804. return Math.ceil(this.count / this.pageSize);
  805. },
  806. sortedFilteredColumns() {
  807. return this.orderedColumns.filter(
  808. column => this.shownColumns.indexOf(column.name) !== -1
  809. );
  810. },
  811. hidableSortedColumns() {
  812. return this.orderedColumns.filter(column => column.hidable);
  813. },
  814. lastSelectedItemIndex() {
  815. return this.rows.findIndex(item => item.highlighted);
  816. },
  817. selectedRows() {
  818. return this.rows.filter(row => row.selected);
  819. },
  820. hasCheckboxes() {
  821. return (
  822. this.$slots["bulk-actions"] != null ||
  823. this.$slots["bulk-actions-right"] != null
  824. );
  825. },
  826. ...mapGetters({
  827. socket: "websockets/getSocket"
  828. })
  829. },
  830. watch: {
  831. selectedRows(newSelectedRows, oldSelectedRows) {
  832. // If selected rows goes from zero to one or more selected, trigger onWindowResize, as otherwise the popup could be out of bounds
  833. if (oldSelectedRows.length === 0 && newSelectedRows.length > 0)
  834. this.onWindowResize();
  835. }
  836. },
  837. mounted() {
  838. const tableSettings = this.getTableSettings();
  839. this.orderedColumns = [
  840. {
  841. name: "select",
  842. displayName: "",
  843. properties: [],
  844. sortable: false,
  845. hidable: false,
  846. draggable: false,
  847. resizable: false,
  848. minWidth: 47,
  849. defaultWidth: 47,
  850. maxWidth: 47
  851. },
  852. ...this.columns.map(column => ({
  853. ...this.columnDefault,
  854. ...column
  855. })),
  856. {
  857. name: "placeholder",
  858. displayName: "",
  859. properties: [],
  860. sortable: false,
  861. hidable: false,
  862. draggable: false,
  863. resizable: false,
  864. minWidth: "auto",
  865. width: "auto",
  866. maxWidth: "auto"
  867. }
  868. ].sort((columnA, columnB) => {
  869. // Always places select column in the first position
  870. if (columnA.name === "select") return -1;
  871. // Always places placeholder column in the last position
  872. if (columnB.name === "placeholder") return -1;
  873. // If there are no table settings stored, use default ordering
  874. if (!tableSettings || !tableSettings.columnOrder) return 0;
  875. const indexA = tableSettings.columnOrder.indexOf(columnA.name);
  876. const indexB = tableSettings.columnOrder.indexOf(columnB.name);
  877. // If either of the columns is not stored in the table settings, use default ordering
  878. if (indexA === -1 || indexB === -1) return 0;
  879. return indexA - indexB;
  880. });
  881. this.shownColumns = this.orderedColumns
  882. .filter(column => {
  883. // If table settings exist, use shownColumns from settings to determine which columns to show
  884. if (tableSettings && tableSettings.shownColumns)
  885. return (
  886. tableSettings.shownColumns.indexOf(column.name) !== -1
  887. );
  888. // Table settings don't exist, only show if the default visibility isn't hidden
  889. return column.defaultVisibility !== "hidden";
  890. })
  891. .map(column => column.name);
  892. this.recalculateWidths();
  893. if (tableSettings) {
  894. // If table settings' page is an integer, use it for the page
  895. if (Number.isInteger(tableSettings?.page))
  896. this.page = tableSettings.page;
  897. // If table settings' pageSize is an integer, use it for the pageSize
  898. if (Number.isInteger(tableSettings?.pageSize))
  899. this.pageSize = tableSettings.pageSize;
  900. // If table settings' columnSort exists, sort all still existing columns based on table settings' columnSort object
  901. if (tableSettings.columnSort) {
  902. Object.entries(tableSettings.columnSort).forEach(
  903. ([columnName, sortDirection]) => {
  904. if (
  905. this.columns.find(
  906. column => column.name === columnName
  907. )
  908. )
  909. this.sort[columnName] = sortDirection;
  910. }
  911. );
  912. }
  913. // If table settings' columnWidths exists, load the stored widths into the columns
  914. if (tableSettings.columnWidths) {
  915. this.orderedColumns = this.orderedColumns.map(orderedColumn => {
  916. const columnWidth = tableSettings.columnWidths.find(
  917. column => column.name === orderedColumn.name
  918. )?.width;
  919. if (columnWidth)
  920. return { ...orderedColumn, width: columnWidth };
  921. return orderedColumn;
  922. });
  923. }
  924. if (
  925. tableSettings.filter &&
  926. tableSettings.filter.appliedFilters &&
  927. tableSettings.filter.appliedFilterOperator
  928. ) {
  929. const { appliedFilters, appliedFilterOperator } =
  930. tableSettings.filter;
  931. // Set the applied filter operator and filter operator to the value stored in table settings
  932. this.appliedFilterOperator = this.filterOperator =
  933. appliedFilterOperator;
  934. // Set the applied filters and editing filters to the value stored in table settings, for all filters that are allowed
  935. this.appliedFilters = appliedFilters.filter(appliedFilter =>
  936. this.filters.find(
  937. filter => appliedFilter.filter.name === filter.name
  938. )
  939. );
  940. this.editingFilters = appliedFilters.filter(appliedFilter =>
  941. this.filters.find(
  942. filter => appliedFilter.filter.name === filter.name
  943. )
  944. );
  945. }
  946. }
  947. this.resetBulkActionsPosition();
  948. this.$nextTick(() => {
  949. this.onWindowResize();
  950. window.addEventListener("resize", this.onWindowResize);
  951. });
  952. ws.onConnect(this.init);
  953. if (this.events && this.events.updated)
  954. this.socket.on(`event:${this.events.updated.event}`, res => {
  955. const index = this.rows
  956. .map(row => row._id)
  957. .indexOf(
  958. this.events.updated.id
  959. .split(".")
  960. .reduce(
  961. (previous, current) =>
  962. (previous && previous[current]) || null,
  963. res.data
  964. )
  965. );
  966. const row = this.events.updated.item
  967. .split(".")
  968. .reduce(
  969. (previous, current) =>
  970. (previous && previous[current]) || null,
  971. res.data
  972. );
  973. this.updateData(index, row);
  974. });
  975. if (this.events && this.events.removed)
  976. this.socket.on(`event:${this.events.removed.event}`, res => {
  977. const index = this.rows
  978. .map(row => row._id)
  979. .indexOf(
  980. this.events.removed.id
  981. .split(".")
  982. .reduce(
  983. (previous, current) =>
  984. (previous && previous[current]) || null,
  985. res.data
  986. )
  987. );
  988. this.removeData(index);
  989. });
  990. },
  991. unmounted() {
  992. window.removeEventListener("resize", this.onWindowResize);
  993. if (this.storeTableSettingsDebounceTimeout)
  994. clearTimeout(this.storeTableSettingsDebounceTimeout);
  995. },
  996. methods: {
  997. init() {
  998. this.getData();
  999. if (this.query) this.setQuery();
  1000. if (this.events && this.events.room) {
  1001. this.socket.dispatch(
  1002. "apis.joinAdminRoom",
  1003. this.events.room,
  1004. () => {}
  1005. );
  1006. }
  1007. },
  1008. getData() {
  1009. this.socket.dispatch(
  1010. this.dataAction,
  1011. this.page,
  1012. this.pageSize,
  1013. this.properties,
  1014. this.sort,
  1015. JSON.parse(
  1016. JSON.stringify(
  1017. this.appliedFilters.map(filter => ({
  1018. ...filter,
  1019. filterType: filter.filterType.name
  1020. }))
  1021. )
  1022. ),
  1023. this.appliedFilterOperator,
  1024. res => {
  1025. console.log(111, res);
  1026. if (res.status === "success") {
  1027. const { data, count } = res.data;
  1028. this.rows = data.map(row => ({
  1029. ...row,
  1030. selected: false
  1031. }));
  1032. this.count = count;
  1033. } else {
  1034. new Toast(res.message);
  1035. }
  1036. }
  1037. );
  1038. },
  1039. changePageSize() {
  1040. this.page = 1;
  1041. this.getData();
  1042. this.storeTableSettings();
  1043. },
  1044. changePage(page) {
  1045. if (page < 1) return;
  1046. if (page > this.lastPage) return;
  1047. if (page === this.page) return;
  1048. this.page = page;
  1049. this.getData();
  1050. if (this.query) this.setQuery();
  1051. },
  1052. changeSort(column) {
  1053. if (column.sortable) {
  1054. const { sortProperty } = column;
  1055. if (this.sort[sortProperty] === undefined)
  1056. this.sort[sortProperty] = "ascending";
  1057. else if (this.sort[sortProperty] === "ascending")
  1058. this.sort[sortProperty] = "descending";
  1059. else if (this.sort[sortProperty] === "descending")
  1060. delete this.sort[sortProperty];
  1061. this.getData();
  1062. this.storeTableSettings();
  1063. }
  1064. },
  1065. toggleColumnVisibility(column) {
  1066. if (!column.hidable) return false;
  1067. if (this.shownColumns.indexOf(column.name) !== -1) {
  1068. if (this.shownColumns.length <= 3)
  1069. return new Toast(
  1070. `Unable to hide column ${column.displayName}, there must be at least 1 visibile column`
  1071. );
  1072. this.shownColumns.splice(
  1073. this.shownColumns.indexOf(column.name),
  1074. 1
  1075. );
  1076. } else {
  1077. this.shownColumns.push(column.name);
  1078. }
  1079. this.recalculateWidths();
  1080. this.getData();
  1081. return this.storeTableSettings();
  1082. },
  1083. toggleSelectedRow(itemIndex, event) {
  1084. const { shiftKey, ctrlKey } = event;
  1085. // Shift was pressed, so attempt to select all items between the clicked item and last clicked item
  1086. if (shiftKey) {
  1087. // If the clicked item is already selected, prevent default, otherwise the checkbox will be unchecked
  1088. if (this.rows[itemIndex].selected) event.preventDefault();
  1089. // If there is a last clicked item
  1090. if (this.lastSelectedItemIndex >= 0) {
  1091. // Clicked item is lower than last item, so select upwards until it reaches the last selected item
  1092. if (itemIndex > this.lastSelectedItemIndex) {
  1093. for (
  1094. let itemIndexUp = itemIndex;
  1095. itemIndexUp > this.lastSelectedItemIndex;
  1096. itemIndexUp -= 1
  1097. ) {
  1098. this.rows[itemIndexUp].selected = true;
  1099. }
  1100. }
  1101. // Clicked item is higher than last item, so select downwards until it reaches the last selected item
  1102. else if (itemIndex < this.lastSelectedItemIndex) {
  1103. for (
  1104. let itemIndexDown = itemIndex;
  1105. itemIndexDown < this.lastSelectedItemIndex;
  1106. itemIndexDown += 1
  1107. ) {
  1108. this.rows[itemIndexDown].selected = true;
  1109. }
  1110. }
  1111. }
  1112. }
  1113. // Ctrl was pressed, so attempt to unselect all items between the clicked item and last clicked item
  1114. else if (ctrlKey) {
  1115. // If the clicked item is already unselected, prevent default, otherwise the checkbox will be checked
  1116. if (!this.rows[itemIndex].selected) event.preventDefault();
  1117. // If there is a last clicked item
  1118. if (this.lastSelectedItemIndex >= 0) {
  1119. // Clicked item is lower than last item, so unselect upwards until it reaches the last selected item
  1120. if (itemIndex > this.lastSelectedItemIndex) {
  1121. for (
  1122. let itemIndexUp = itemIndex;
  1123. itemIndexUp >= this.lastSelectedItemIndex;
  1124. itemIndexUp -= 1
  1125. ) {
  1126. this.rows[itemIndexUp].selected = false;
  1127. }
  1128. }
  1129. // Clicked item is higher than last item, so unselect downwards until it reaches the last selected item
  1130. else if (itemIndex < this.lastSelectedItemIndex) {
  1131. for (
  1132. let itemIndexDown = itemIndex;
  1133. itemIndexDown <= this.lastSelectedItemIndex;
  1134. itemIndexDown += 1
  1135. ) {
  1136. this.rows[itemIndexDown].selected = false;
  1137. }
  1138. }
  1139. }
  1140. }
  1141. // Neither ctrl nor shift were pressed, so toggle clicked item
  1142. else {
  1143. this.rows[itemIndex].selected = !this.rows[itemIndex].selected;
  1144. }
  1145. // Set the last clicked item to no longer be highlighted, if it exists
  1146. if (this.lastSelectedItemIndex >= 0)
  1147. this.rows[this.lastSelectedItemIndex].highlighted = false;
  1148. // Set the clicked item to be highlighted
  1149. this.rows[itemIndex].highlighted = true;
  1150. },
  1151. toggleAllRows() {
  1152. if (this.rows.length > this.selectedRows.length) {
  1153. this.rows = this.rows.map(row => ({ ...row, selected: true }));
  1154. } else {
  1155. this.rows = this.rows.map(row => ({ ...row, selected: false }));
  1156. }
  1157. },
  1158. addFilterItem() {
  1159. this.editingFilters.push({
  1160. data: "",
  1161. filter: this.addFilterValue,
  1162. filterType:
  1163. this.allFilterTypes[this.addFilterValue.defaultFilterType]
  1164. });
  1165. },
  1166. removeFilterItem(index) {
  1167. this.editingFilters.splice(index, 1);
  1168. },
  1169. columnResizingStart(column, event) {
  1170. const eventIsTouch = event.type === "touchstart";
  1171. if (eventIsTouch) {
  1172. // Handle double click from touch (if this method is called for the same column twice in a row within one second)
  1173. if (
  1174. this.lastColumnResizerTapped === column &&
  1175. Date.now() - this.lastColumnResizerTappedDate <= 1000
  1176. ) {
  1177. this.columnResetWidth(column);
  1178. this.lastColumnResizerTapped = null;
  1179. this.lastColumnResizerTappedDate = 0;
  1180. return;
  1181. }
  1182. this.lastColumnResizerTapped = column;
  1183. this.lastColumnResizerTappedDate = Date.now();
  1184. }
  1185. this.resizing.resizing = true;
  1186. this.resizing.resizingColumn = column;
  1187. this.resizing.width = event.target.parentElement.offsetWidth;
  1188. this.resizing.lastX = eventIsTouch
  1189. ? event.targetTouches[0].clientX
  1190. : event.x;
  1191. },
  1192. columnResizing(event) {
  1193. if (this.resizing.resizing) {
  1194. const eventIsTouch = event.type === "touchmove";
  1195. if (!eventIsTouch && event.buttons !== 1) {
  1196. this.resizing.resizing = false;
  1197. this.storeTableSettings();
  1198. }
  1199. const x = eventIsTouch
  1200. ? event.changedTouches[0].clientX
  1201. : event.x;
  1202. this.resizing.width -= this.resizing.lastX - x;
  1203. this.resizing.lastX = x;
  1204. if (
  1205. this.resizing.resizingColumn.minWidth &&
  1206. this.resizing.resizingColumn.maxWidth
  1207. ) {
  1208. this.resizing.resizingColumn.width = Math.max(
  1209. Math.min(
  1210. this.resizing.resizingColumn.maxWidth,
  1211. this.resizing.width
  1212. ),
  1213. this.resizing.resizingColumn.minWidth
  1214. );
  1215. } else if (this.resizing.resizingColumn.minWidth) {
  1216. this.resizing.resizingColumn.width = Math.max(
  1217. this.resizing.width,
  1218. this.resizing.resizingColumn.minWidth
  1219. );
  1220. } else if (this.resizing.resizingColumn.maxWidth) {
  1221. this.resizing.resizingColumn.width = Math.min(
  1222. this.resizing.resizingColumn.maxWidth,
  1223. this.resizing.width
  1224. );
  1225. } else {
  1226. this.resizing.resizingColumn.width = this.resizing.width;
  1227. }
  1228. this.resizing.width = this.resizing.resizingColumn.width;
  1229. console.log(`New width: ${this.resizing.width}px`);
  1230. this.storeTableSettings();
  1231. }
  1232. },
  1233. columnResizingStop() {
  1234. this.resizing.resizing = false;
  1235. this.storeTableSettings();
  1236. },
  1237. columnResetWidth(column) {
  1238. const index = this.orderedColumns.indexOf(column);
  1239. if (column.defaultWidth && !Number.isNaN(column.defaultWidth))
  1240. this.orderedColumns[index].width = column.defaultWidth;
  1241. else if (
  1242. column.calculatedWidth &&
  1243. !Number.isNaN(column.calculatedWidth)
  1244. )
  1245. this.orderedColumns[index].width = column.calculatedWidth;
  1246. this.storeTableSettings();
  1247. },
  1248. filterTypes(filter) {
  1249. if (!filter || !filter.filterTypes) return [];
  1250. return filter.filterTypes.map(
  1251. filterType => this.allFilterTypes[filterType]
  1252. );
  1253. },
  1254. changeFilterType(index) {
  1255. this.editingFilters[index].filterType =
  1256. this.allFilterTypes[
  1257. this.editingFilters[index].filter.defaultFilterType
  1258. ];
  1259. },
  1260. onDragBox(e) {
  1261. const e1 = e || window.event;
  1262. const e1IsTouch = e1.type === "touchstart";
  1263. e1.preventDefault();
  1264. if (e1IsTouch) {
  1265. // Handle double click from touch (if this method is twice in a row within one second)
  1266. if (Date.now() - this.lastBulkActionsTappedDate <= 1000) {
  1267. this.resetBulkActionsPosition();
  1268. this.lastBulkActionsTappedDate = 0;
  1269. return;
  1270. }
  1271. this.lastBulkActionsTappedDate = Date.now();
  1272. }
  1273. this.bulkPopup.pos3 = e1IsTouch
  1274. ? e1.changedTouches[0].clientX
  1275. : e1.clientX;
  1276. this.bulkPopup.pos4 = e1IsTouch
  1277. ? e1.changedTouches[0].clientY
  1278. : e1.clientY;
  1279. document.onmousemove = document.ontouchmove = e => {
  1280. const e2 = e || window.event;
  1281. const e2IsTouch = e2.type === "touchmove";
  1282. if (!e2IsTouch) e2.preventDefault();
  1283. // Get the clientX and clientY
  1284. const e2ClientX = e2IsTouch
  1285. ? e2.changedTouches[0].clientX
  1286. : e2.clientX;
  1287. const e2ClientY = e2IsTouch
  1288. ? e2.changedTouches[0].clientY
  1289. : e2.clientY;
  1290. // calculate the new cursor position:
  1291. this.bulkPopup.pos1 = this.bulkPopup.pos3 - e2ClientX;
  1292. this.bulkPopup.pos2 = this.bulkPopup.pos4 - e2ClientY;
  1293. this.bulkPopup.pos3 = e2ClientX;
  1294. this.bulkPopup.pos4 = e2ClientY;
  1295. // set the element's new position:
  1296. this.bulkPopup.top -= this.bulkPopup.pos2;
  1297. this.bulkPopup.left -= this.bulkPopup.pos1;
  1298. if (this.bulkPopup.top < 0) this.bulkPopup.top = 0;
  1299. if (this.bulkPopup.top > document.body.clientHeight - 50)
  1300. this.bulkPopup.top = document.body.clientHeight - 50;
  1301. if (this.bulkPopup.left < 0) this.bulkPopup.left = 0;
  1302. if (this.bulkPopup.left > document.body.clientWidth - 400)
  1303. this.bulkPopup.left = document.body.clientWidth - 400;
  1304. };
  1305. document.onmouseup = document.ontouchend = () => {
  1306. document.onmouseup = null;
  1307. document.ontouchend = null;
  1308. document.onmousemove = null;
  1309. document.ontouchmove = null;
  1310. };
  1311. },
  1312. resetBulkActionsPosition() {
  1313. this.bulkPopup.top = document.body.clientHeight - 56;
  1314. this.bulkPopup.left = document.body.clientWidth / 2 - 200;
  1315. },
  1316. applyFilterAndGetData() {
  1317. this.appliedFilters = JSON.parse(
  1318. JSON.stringify(this.editingFilters)
  1319. );
  1320. this.appliedFilterOperator = this.filterOperator;
  1321. this.getData();
  1322. this.storeTableSettings();
  1323. },
  1324. recalculateWidths() {
  1325. let noWidthCount = 0;
  1326. let calculatedWidth = 0;
  1327. this.orderedColumns.forEach(column => {
  1328. if (
  1329. this.shownColumns.indexOf(column.name) !== -1 &&
  1330. (column.name !== "select" || this.hasCheckboxes)
  1331. )
  1332. if (
  1333. Number.isFinite(column.width) &&
  1334. !Number.isFinite(column.calculatedWidth)
  1335. ) {
  1336. calculatedWidth += column.width;
  1337. } else if (Number.isFinite(column.defaultWidth)) {
  1338. calculatedWidth += column.defaultWidth;
  1339. } else {
  1340. noWidthCount += 1;
  1341. }
  1342. });
  1343. calculatedWidth = Math.floor(
  1344. (Math.min(this.maxWidth, document.body.clientWidth) -
  1345. calculatedWidth) /
  1346. (noWidthCount - 1)
  1347. );
  1348. this.orderedColumns = this.orderedColumns.map(column => {
  1349. const orderedColumn = column;
  1350. if (this.shownColumns.indexOf(orderedColumn.name) !== -1) {
  1351. let newWidth;
  1352. if (Number.isFinite(orderedColumn.defaultWidth)) {
  1353. newWidth = orderedColumn.defaultWidth;
  1354. } else {
  1355. // eslint-disable-next-line no-param-reassign
  1356. newWidth = orderedColumn.calculatedWidth = Math.min(
  1357. Math.max(
  1358. orderedColumn.minWidth || 100, // fallback 100px min width
  1359. calculatedWidth
  1360. ),
  1361. orderedColumn.maxWidth || 1000 // fallback 1000px max width
  1362. );
  1363. }
  1364. if (newWidth && !Number.isFinite(orderedColumn.width))
  1365. orderedColumn.width = newWidth;
  1366. }
  1367. return orderedColumn;
  1368. });
  1369. },
  1370. columnOrderChanged() {
  1371. this.storeTableSettings();
  1372. },
  1373. getTableSettings() {
  1374. const urlTableSettings = {};
  1375. if (this.query) {
  1376. if (this.$route.query.page)
  1377. urlTableSettings.page = Number.parseInt(
  1378. this.$route.query.page
  1379. );
  1380. if (this.$route.query.pageSize)
  1381. urlTableSettings.pageSize = Number.parseInt(
  1382. this.$route.query.pageSize
  1383. );
  1384. if (this.$route.query.shownColumns)
  1385. urlTableSettings.shownColumns = JSON.parse(
  1386. this.$route.query.shownColumns
  1387. );
  1388. if (this.$route.query.columnOrder)
  1389. urlTableSettings.columnOrder = JSON.parse(
  1390. this.$route.query.columnOrder
  1391. );
  1392. if (this.$route.query.columnWidths)
  1393. urlTableSettings.columnWidths = JSON.parse(
  1394. this.$route.query.columnWidths
  1395. );
  1396. if (this.$route.query.columnSort)
  1397. urlTableSettings.columnSort = JSON.parse(
  1398. this.$route.query.columnSort
  1399. );
  1400. if (this.$route.query.filter)
  1401. urlTableSettings.filter = JSON.parse(
  1402. this.$route.query.filter
  1403. );
  1404. }
  1405. const localStorageTableSettings = JSON.parse(
  1406. localStorage.getItem(`advancedTableSettings:${this.name}`)
  1407. );
  1408. return {
  1409. ...localStorageTableSettings,
  1410. ...urlTableSettings
  1411. };
  1412. },
  1413. storeTableSettings() {
  1414. // Clear debounce timeout
  1415. if (this.storeTableSettingsDebounceTimeout)
  1416. clearTimeout(this.storeTableSettingsDebounceTimeout);
  1417. // Resizing calls this function a lot, so rather than saving dozens of times a second, use debouncing
  1418. this.storeTableSettingsDebounceTimeout = setTimeout(() => {
  1419. if (this.query) this.setQuery();
  1420. this.setLocalStorage();
  1421. }, 250);
  1422. },
  1423. setQuery() {
  1424. const queryObject = {
  1425. ...this.$route.query,
  1426. page: this.page,
  1427. pageSize: this.pageSize,
  1428. filter: JSON.stringify({
  1429. appliedFilters: this.appliedFilters,
  1430. appliedFilterOperator: this.appliedFilterOperator
  1431. }),
  1432. columnSort: JSON.stringify(this.sort),
  1433. columnOrder: JSON.stringify(
  1434. this.orderedColumns.map(column => column.name)
  1435. ),
  1436. columnWidths: JSON.stringify(
  1437. this.orderedColumns.map(column => ({
  1438. name: column.name,
  1439. width: column.width
  1440. }))
  1441. ),
  1442. shownColumns: JSON.stringify(this.shownColumns)
  1443. };
  1444. const queryString = `?${Object.keys(queryObject)
  1445. .map(key => `${key}=${queryObject[key]}`)
  1446. .join("&")}`;
  1447. window.history.replaceState(null, null, queryString);
  1448. },
  1449. setLocalStorage() {
  1450. localStorage.setItem(
  1451. `advancedTableSettings:${this.name}`,
  1452. JSON.stringify({
  1453. pageSize: this.pageSize,
  1454. filter: {
  1455. appliedFilters: this.appliedFilters,
  1456. appliedFilterOperator: this.appliedFilterOperator
  1457. },
  1458. columnSort: this.sort,
  1459. columnOrder: this.orderedColumns.map(column => column.name),
  1460. columnWidths: this.orderedColumns.map(column => ({
  1461. name: column.name,
  1462. width: column.width
  1463. })),
  1464. shownColumns: this.shownColumns
  1465. })
  1466. );
  1467. },
  1468. onWindowResize() {
  1469. // Only change the position if the popup is actually visible
  1470. if (this.selectedRows.length === 0) return;
  1471. if (this.bulkPopup.top < 0) this.bulkPopup.top = 0;
  1472. if (this.bulkPopup.top > document.body.clientHeight - 50)
  1473. this.bulkPopup.top = document.body.clientHeight - 50;
  1474. if (this.bulkPopup.left < 0) this.bulkPopup.left = 0;
  1475. if (this.bulkPopup.left > document.body.clientWidth - 400)
  1476. this.bulkPopup.left = document.body.clientWidth - 400;
  1477. },
  1478. updateData(index, data) {
  1479. this.rows[index] = { ...this.rows[index], ...data, updated: true };
  1480. },
  1481. removeData(index) {
  1482. this.rows[index] = { ...this.rows[index], removed: true };
  1483. }
  1484. }
  1485. };
  1486. </script>
  1487. <style lang="scss" scoped>
  1488. .night-mode {
  1489. .table-outer-container {
  1490. .table-container .table {
  1491. &,
  1492. thead th {
  1493. background-color: var(--dark-grey-3);
  1494. color: var(--light-grey-2);
  1495. }
  1496. tr {
  1497. th,
  1498. td {
  1499. border-color: var(--dark-grey) !important;
  1500. &:first-child {
  1501. background-color: var(--dark-grey-3) !important;
  1502. }
  1503. }
  1504. &:nth-child(even) {
  1505. &,
  1506. td:first-child {
  1507. background-color: var(--dark-grey-2) !important;
  1508. }
  1509. }
  1510. &:hover,
  1511. &:focus,
  1512. &.highlighted {
  1513. th,
  1514. td {
  1515. &,
  1516. &:first-child {
  1517. background-color: var(--dark-grey-4) !important;
  1518. }
  1519. }
  1520. }
  1521. }
  1522. }
  1523. .table-header,
  1524. .table-footer {
  1525. background-color: var(--dark-grey-3);
  1526. color: var(--light-grey-2);
  1527. }
  1528. .label.control {
  1529. background-color: var(--dark-grey) !important;
  1530. border-color: var(--grey-3) !important;
  1531. color: var(--white) !important;
  1532. }
  1533. }
  1534. .bulk-popup {
  1535. border: 0;
  1536. background-color: var(--dark-grey-2);
  1537. color: var(--white);
  1538. .material-icons {
  1539. color: var(--white);
  1540. }
  1541. }
  1542. }
  1543. .table-outer-container {
  1544. border-radius: 5px;
  1545. box-shadow: 0 2px 3px rgba(10, 10, 10, 0.1), 0 0 0 1px rgba(10, 10, 10, 0.1);
  1546. margin: 10px 0;
  1547. overflow: hidden;
  1548. .table-container {
  1549. overflow-x: auto;
  1550. table {
  1551. border-collapse: separate;
  1552. table-layout: fixed;
  1553. thead {
  1554. tr {
  1555. th {
  1556. height: 40px;
  1557. line-height: 40px;
  1558. border: 1px solid var(--light-grey-2);
  1559. border-width: 1px 1px 1px 0;
  1560. padding: 0;
  1561. &:last-child {
  1562. border-width: 1px 0 1px;
  1563. }
  1564. &.sortable {
  1565. cursor: pointer;
  1566. }
  1567. & > div {
  1568. display: flex;
  1569. white-space: nowrap;
  1570. padding: 8px 10px;
  1571. & > span {
  1572. margin-left: 5px;
  1573. &:first-child {
  1574. margin-left: 0;
  1575. margin-right: auto;
  1576. }
  1577. & > .material-icons {
  1578. font-size: 22px;
  1579. position: relative;
  1580. top: 6px;
  1581. cursor: pointer;
  1582. &.active {
  1583. color: var(--primary-color);
  1584. }
  1585. &:hover,
  1586. &:focus {
  1587. filter: brightness(90%);
  1588. }
  1589. }
  1590. }
  1591. }
  1592. }
  1593. }
  1594. }
  1595. tbody {
  1596. tr {
  1597. &.highlighted {
  1598. background-color: var(--light-grey);
  1599. }
  1600. td {
  1601. border: 1px solid var(--light-grey-2);
  1602. border-width: 0 1px 1px 0;
  1603. &:last-child {
  1604. border-width: 0 0 1px;
  1605. }
  1606. /deep/ .row-options {
  1607. display: flex;
  1608. justify-content: space-between;
  1609. .icon-with-button {
  1610. height: 30px;
  1611. width: 30px;
  1612. }
  1613. }
  1614. }
  1615. }
  1616. }
  1617. }
  1618. table {
  1619. thead tr,
  1620. tbody tr {
  1621. th,
  1622. td {
  1623. position: relative;
  1624. white-space: nowrap;
  1625. text-overflow: ellipsis;
  1626. overflow: hidden;
  1627. &:first-child {
  1628. display: none;
  1629. }
  1630. .resizer {
  1631. height: 100%;
  1632. width: 5px;
  1633. background-color: transparent;
  1634. cursor: col-resize;
  1635. position: absolute;
  1636. right: 0;
  1637. top: 0;
  1638. }
  1639. }
  1640. &:nth-child(even) td:first-child {
  1641. background-color: #fafafa;
  1642. }
  1643. &:hover,
  1644. &:focus,
  1645. &.highlighted {
  1646. th,
  1647. td {
  1648. &,
  1649. &:first-child {
  1650. background-color: var(--light-grey);
  1651. }
  1652. }
  1653. }
  1654. }
  1655. &.has-checkboxes {
  1656. thead tr,
  1657. tbody tr {
  1658. th,
  1659. td {
  1660. &:first-child {
  1661. display: table-cell;
  1662. position: sticky;
  1663. left: 0;
  1664. background-color: var(--white);
  1665. z-index: 2;
  1666. }
  1667. }
  1668. }
  1669. }
  1670. }
  1671. }
  1672. .table-header,
  1673. .table-footer {
  1674. display: flex;
  1675. flex-direction: row;
  1676. flex-wrap: wrap;
  1677. justify-content: space-between;
  1678. line-height: 36px;
  1679. background-color: var(--white);
  1680. }
  1681. .table-header > div {
  1682. display: flex;
  1683. flex-direction: row;
  1684. > span > .control {
  1685. margin: 5px;
  1686. }
  1687. .filters-indicator {
  1688. line-height: 46px;
  1689. display: flex;
  1690. align-items: center;
  1691. column-gap: 4px;
  1692. }
  1693. }
  1694. .table-footer {
  1695. .page-controls,
  1696. .page-size > .control {
  1697. display: flex;
  1698. flex-direction: row;
  1699. margin-bottom: 0 !important;
  1700. button {
  1701. margin: 5px;
  1702. font-size: 20px;
  1703. }
  1704. p,
  1705. label {
  1706. margin: 5px;
  1707. font-size: 14px;
  1708. font-weight: 600;
  1709. }
  1710. &.select::after {
  1711. top: 18px;
  1712. }
  1713. }
  1714. }
  1715. }
  1716. .control.is-grouped {
  1717. display: flex;
  1718. & > .control {
  1719. &.label {
  1720. height: 36px;
  1721. background-color: var(--white);
  1722. border: 1px solid var(--light-grey-2);
  1723. color: var(--dark-grey-2);
  1724. appearance: none;
  1725. border-radius: 3px;
  1726. font-size: 14px;
  1727. line-height: 34px;
  1728. padding-left: 8px;
  1729. padding-right: 8px;
  1730. }
  1731. &.select.is-expanded > select {
  1732. width: 100%;
  1733. }
  1734. & > input,
  1735. & > select,
  1736. & > .button,
  1737. &.label {
  1738. border-radius: 0;
  1739. }
  1740. &:first-child {
  1741. & > input,
  1742. & > select,
  1743. & > .button,
  1744. &.label {
  1745. border-radius: 5px 0 0 5px;
  1746. }
  1747. }
  1748. &:last-child {
  1749. & > input,
  1750. & > select,
  1751. & > .button,
  1752. &.label {
  1753. border-radius: 0 5px 5px 0;
  1754. }
  1755. }
  1756. & > .button {
  1757. font-size: 22px;
  1758. }
  1759. }
  1760. @media screen and (max-width: 500px) {
  1761. &.advanced-filter {
  1762. flex-wrap: wrap;
  1763. .control.select {
  1764. width: 50%;
  1765. select {
  1766. width: 100%;
  1767. }
  1768. }
  1769. .control {
  1770. margin-bottom: 0 !important;
  1771. &:nth-child(1) select {
  1772. border-radius: 5px 0 0 0;
  1773. }
  1774. &:nth-child(2) select {
  1775. border-radius: 0 5px 0 0;
  1776. }
  1777. &:nth-child(3) input {
  1778. border-radius: 0 0 0 5px;
  1779. }
  1780. &:nth-child(4) button {
  1781. border-radius: 0 0 5px 0;
  1782. }
  1783. }
  1784. }
  1785. }
  1786. }
  1787. .advanced-filter-bottom {
  1788. display: flex;
  1789. .button {
  1790. font-size: 16px !important;
  1791. width: 100%;
  1792. }
  1793. .control {
  1794. margin: 0 !important;
  1795. }
  1796. }
  1797. /deep/ .bulk-popup {
  1798. display: flex;
  1799. position: fixed;
  1800. flex-direction: row;
  1801. width: 100%;
  1802. max-width: 400px;
  1803. line-height: 36px;
  1804. z-index: 5;
  1805. border: 1px solid var(--light-grey-3);
  1806. border-radius: 5px;
  1807. box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  1808. background-color: var(--white);
  1809. color: var(--dark-grey);
  1810. padding: 5px;
  1811. .right {
  1812. display: flex;
  1813. flex-direction: row;
  1814. margin-left: auto;
  1815. }
  1816. .drag-icon {
  1817. position: relative;
  1818. top: 6px;
  1819. color: var(--dark-grey);
  1820. cursor: move;
  1821. }
  1822. .bulk-actions {
  1823. display: flex;
  1824. flex-direction: row;
  1825. width: 100%;
  1826. justify-content: space-evenly;
  1827. .material-icons {
  1828. position: relative;
  1829. top: 6px;
  1830. margin-left: 5px;
  1831. cursor: pointer;
  1832. color: var(--primary-color);
  1833. &:hover,
  1834. &:focus {
  1835. filter: brightness(90%);
  1836. }
  1837. }
  1838. .delete-icon {
  1839. color: var(--dark-red);
  1840. }
  1841. }
  1842. }
  1843. </style>