AdvancedTable.vue 52 KB

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