all files / js/ table.js

96.36% Statements 53/55
91.67% Branches 22/24
100% Functions 15/15
92.86% Lines 26/28
2 statements, 4 branches Ignored     
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                                                                                                          
import $ from 'jquery';
import datatableview from './datatableview';
 
Iif (datatableview.auto_initialize) {
    datatableview.initialize($('.datatable'));
}
 
/** Class representing an AJAX datatable.
 * @constructor
 * @param containerID {string} - DOM element ID assigned to the widget
 * @param formID {string} - ID of the FilterForm, optional
 * @param endpointName {string} - name of the widget used for routing requests
 * @param api {QuerybuilderAPI} - data source
 * @param widgetParams {string} - manually defined data that will be passed
 to the API together with the request
 */
class Table {
    constructor(containerID, formID, endpointName, api, widgetParams) {
        this.containerID = containerID;
        this.endpointName = endpointName;
        this.api = api;
        this.widgetParams = widgetParams;
        this.formID = formID;
        this._linkWithFilterForm(formID);
        this._storeTableInDOM();
        this._initializeTableView();
    }
 
    retrieveData(client_params, widget_params, callback) {
        this.api.retrieveData(this.endpointName, client_params, widget_params, callback);
    }
 
    _linkWithFilterForm(formID) {
        if (formID) {
            this.form = this._getFilterForm(formID);
            this.form.onSubmit(this._refreshData.bind(this));
        }
    }
 
    _getFilterForm(formID) {
        return $(formID).data('FilterForm');
    }
 
    _refreshData(event) {
        event.preventDefault();
        this.tableview._fnAjaxUpdate();
    }
 
    _getFilterParameters() {
        if (this.form) {
            return this.form.serialize();
        } else {
            return '';
        }
    }
 
    _storeTableInDOM() {
        $(this.containerID).data('Table', this);
    }
 
    _initializeTableView() {
        this.tableview = datatableview.initialize($(this.containerID + '_t'), {
            tableID: this.containerID,
            endpointName: this.endpointName,
            initComplete: this._initSubmit.bind(this)
        });
    }
 
    _initSubmit() {
        Iif (this.formID && this.tableview) {
            $(this.formID).trigger("submit");
        }
    }
 
}
 
export default Table;