All files / src/components datatable-jquery.ts

98.04% Statements 50/51
66.67% Branches 6/9
91.67% Functions 11/12
98% Lines 49/50

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 1501x 1x 1x 1x 1x   1x 1x 1x   1x 1x   1x 1x 1x 1x                 1x   1x     1x     1x     1x                   9x     9x 9x   9x 9x     1x 9x   9x   9x                 1x 8x                           1x 8x   8x                     1x 8x   8x   8x                   1x 9x   9x                         1x 10x     1x     1x       1x 1x     1x 1x   1x  
import { bindable } from 'aurelia-templating';
import { Repository, Entity } from 'aurelia-orm';
import DatatableConfiguration from '../configuration/datatable-configuration';
import DOMUtils from '../utils/DOM-utils';
import DataGenerator from '../utils/data-generator';
import DatatableColumns from '../configuration/datatable-columns';
import DatatableResponsive from '../configuration/datatable-responsive';
import DatatableConfigurator from '../configuration/datatable-configurator';
import DatatableButtons from '../configuration/datatable-buttons';
 
import * as $ from 'jquery';
import 'datatables.net';
 
export const DATATABLE_PREFIX = 'datatable';
export const JOIN_CHAR = '-';
export const DATATABLE_ID = DATATABLE_PREFIX + JOIN_CHAR;
export const ERROR_INVALID_ID = 'The given id is not valid:';
 
/**
 * Datatable component view-model that allow the use
 * of aurelia-orm with jQuery dataTables.
 *
 * @export
 * DatatableJquery
 */
export class DatatableJquery {
  @bindable
  public repository: Repository;
 
  @bindable
  public columns: DatatableColumns[];
 
  @bindable
  public buttons: DatatableButtons;
 
  @bindable
  public responsive: DatatableResponsive;
 
  private _data: Entity[];
  private _id: number;
 
  private _datatableConfigurator: DatatableConfigurator;
  private _datatableConfiguration: DatatableConfiguration;
 
  private _table: any;
 
  private _domUtils: DOMUtils = new DOMUtils();
 
  public constructor() {
    this._datatableConfiguration = new DatatableConfiguration();
    this._datatableConfigurator = new DatatableConfigurator();
 
    this._data = [];
    this._id = -1;
  }
 
  public attached(): void {
    this.configureDatatable();
 
    this.initializeDatatable();
 
    this.fetchData();
  }
 
  /**
   * Configure datatable as user want.
   *
   * @private
   * @memberof DatatableJquery
   */
  private configureDatatable(): void {
    this._datatableConfiguration = this._datatableConfigurator.generateDatatableConfiguration(
      this.repository,
      this.columns,
      this.buttons,
      this.responsive
    );
  }
 
  /**
   * Initialize a Datatables instance.
   *
   * @private
   * @memberof DatatableJquery
   */
  private initializeDatatable(): void {
    this._id = this.generateId();
 
    this._table = $('#' + this._id);
    // .DataTable(this._datatableConfiguration);
  }
 
  /**
   * Generate an id that can be used to instanciate a new datatables.
   *
   * @private
   * @returns {number}
   * @memberof DatatableJquery
   */
  private generateId(): number {
    let usedIds: number[] = this.getUsedIds();
 
    let desiredId = DataGenerator.generateNumberThatIsNotIn(usedIds);
 
    return desiredId;
  }
 
  /**
   * Get all ids used by other datatables.
   *
   * @private
   * @returns {number[]}
   * @memberof DatatableJquery
   */
  private getUsedIds(): number[] {
    let instanciatedDatatables = this.getInstanciatedDatatables();
 
    return this._domUtils.extractNumbersFromIdsOfNodes(
      DATATABLE_ID,
      instanciatedDatatables
    );
  }
 
  /**
   * Get all DOM nodes that have an id that begin with DATATABLE_ID.
   *
   * @private
   * @returns {NodeListOf<Element>}
   * @memberof DatatableJquery
   */
  private getInstanciatedDatatables(): NodeListOf<Element> {
    return this._domUtils.getAllNodesWithIdThatBeginsWith(DATATABLE_ID);
  }
 
  private fetchData(): void {}
 
  // GETTERS AND SETTERS
  public get datatableConfiguration(): DatatableConfiguration {
    return this._datatableConfiguration;
  }
 
  public get id(): string {
    return DATATABLE_ID + this._id;
  }
 
  public get data(): any[] {
    return this._data;
  }
}