All files / addon/components/bootstrap/tables table-with-pagination.js

47.37% Statements 9/19
25% Branches 2/8
60% Functions 3/5
47.37% Lines 9/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                  1x                                                                                                                                                   2x 2x 2x 2x                                                             2x 2x 2x       2x                  
import Component from '@ember/component';
import layout from '../../../templates/components/bootstrap/tables/table-with-pagination';
import { PropTypes } from 'ember-prop-types';
import { BuilderForPropTypes, BuilderForPropDefaults } from 'ember-bootstrap-controls/utils/prop-definition-tools';
import { notEmpty, alias } from '@ember/object/computed';
import { get } from '@ember/object';
import { task } from 'ember-concurrency';
import { isPresent } from '@ember/utils';
 
export const propDefinitions = {
  tableClassNames:{
    default: ['table', 'table-striped', 'table-sm'],
    description: 'Table class names to be adeed to the list of class names',
    type: PropTypes.arrayOf(PropTypes.string),
  },
  responsive: {
    default: true,
    description: 'Indicates whether the table is responsive or not',
    type: PropTypes.bool
  },
  onSort: {
    description: 'Function to be excecuted on sort',
    type: PropTypes.func
  },
  onEdit: {
    description: 'Function to be executed on edit',
    type: PropTypes.func
  },
  onShow: {
    description: 'Function to be executed on show',
    type: PropTypes.func
  },
  onDelete: {
    description: 'Function to be executed on delete',
    type: PropTypes.func
  },
  reloadingRowsData: {
    description: 'Inidicates whether the table row data will be reloaded or not',
    type: PropTypes.bool
  },
  sortField: {
    description: 'Inidicates the sorting criteria',
    type: PropTypes.string
  },
  sortReverse: {
    description: 'Inidicates whether the table can be sorted in reverse or not',
    type: PropTypes.bool
  },
  rowsData: {
    description: 'The table data that will be rendered in the rows',
    type: PropTypes.arrayOf(PropTypes.EmberObject),
  },
  noRowsMessage: {
    default: 'No data',
    description: 'The string to be displayed if the data has no rows.',
    type: PropTypes.string,
  },
  columns: {
    description: 'The table columns that will be rednered',
    type: PropTypes.arrayOf(PropTypes.shape({
      headerLabel: PropTypes.string,
      cellValueKey: PropTypes.string,
      cellValueTask: PropTypes.string,
      sorting: PropTypes.shape({
      }),
    }))
  }
};
 
export default Component.extend({
  layout,
  propTypes: BuilderForPropTypes(propDefinitions),
  tagName: '',
  pageSize: 25,
  pageNumber: 1,
  totalPages: 1,
 
  movingPages: alias('onPageTask.isRunning'),
 
  onPageProvided: notEmpty('onPage'),
  paginationEnabled: alias('onPageProvided'),
 
  calculatePaginationTask: task(function * () {
    const rowsData = yield this.get('rowsData');
    Eif (isPresent(rowsData)) {
      const paginationInformation = yield get(rowsData, 'meta.page');
      Iif (isPresent(paginationInformation)) {
        const pageNumberString = yield get(paginationInformation, 'offset');
        const pageNumber = (isPresent(pageNumberString) ? Number.parseInt(pageNumberString) : null);
        const countEnd = yield get(paginationInformation, 'iteration_count_end');
        const countTotal = yield get(paginationInformation, 'total_records');
        this.setProperties({
          pageSize: yield get(paginationInformation, 'size'),
          totalPages: yield get(paginationInformation, 'total_pages'),
          pageNumber: pageNumber,
          pageRecordsMeta: {
            countStart: yield get(paginationInformation, 'iteration_count_start'),
            countEnd: Math.min(countEnd, countTotal),
            countTotal: countTotal,
          },
        });
      }
    }
  }).restartable(),
 
  onPageTask: task(function * (pageNumber, pageSize) {
    const onPage = yield this.get('onPage');
    this.setProperties({
      pageNumber: pageNumber,
      pageSize: pageSize,
    });
    if (isPresent(onPage)) {
      return yield onPage(pageNumber, pageSize);
    }
  }).restartable(),
 
  didReceiveAttrs() {
    this.get('calculatePaginationTask').perform();
    const tableClassNames = this.get('tableClassNames');
    this.set('tableClassNames', propDefinitions.tableClassNames.default.concat(tableClassNames));
  },
 
  getDefaultProps() {
    return BuilderForPropDefaults(propDefinitions)
  },
 
  actions: {
    onPage(pageNumber, pageSize) {
      return this.get('onPageTask').perform(pageNumber, pageSize);
    },
  }
});