all files / js/ table.js

94.74% Statements 36/38
75% Branches 6/8
100% Functions 9/9
94.74% Lines 36/38
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                                                                                            
var $ = require('jquery');
var datatableview = require('./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
 */
 
var Table = (function(){
    'use strict';
 
    var Table = function(containerID, formID, endpointName, api, widgetParams) {
        this.containerID = containerID;
        this.endpointName = endpointName;
        this.api = api;
        this.widgetParams = widgetParams;
 
        var _this = this;
 
        linkWithFilterForm(formID);
        storeTableInDOM();
        initializeTableView();
 
        function linkWithFilterForm(formID) {
            if (!!formID) {
                _this.form = getFilterForm(formID);
                _this.form.onSubmit(getData);
            }
        }
 
        function getFilterForm(formID) {
            return $(formID).data('FilterForm');
        }
 
        function getData(event) {
            event.preventDefault();
            var parameters = _this.form.serialize();
            $(containerID).data('Table:client_params', parameters);
            $(containerID).data('Table:widget_params', _this.widgetParams);
            _this.tableview._fnAjaxUpdate();
        }
 
        function storeTableInDOM() {
            $(containerID).data('Table', _this);
            $(containerID).data('Table:widget_params', _this.widgetParams);
        }
 
        function initializeTableView() {
            _this.tableview = datatableview.initialize($(containerID + '_t'), {
                tableID: containerID,
                endpointName: endpointName,
                initComplete: initSubmit
            });
        }
 
        function initSubmit() {
            Iif (!!formID && _this.tableview) {
                $(formID).trigger("submit");
            }
        }
    };
 
    Table.prototype = {
        retrieveData: function(client_params, widget_params, callback) {
            this.api.retrieveData(this.endpointName, client_params, widget_params, callback);
        }
    };
 
    return Table;
})();
 
module.exports = Table;