all files / js/ datatableview.js

62.5% Statements 45/72
33.33% Branches 6/18
46.15% Functions 6/13
63.38% Lines 45/71
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 150 151                                                                                                                                                                                                                    
var $ = require('jquery');
var Cookie = require('js-cookie');
require('datatables');
 
/* Modified version of datatableviw.js from datatable. */
 
var datatableview = {
    auto_initialize: false,
    defaults: {
        "bPaginate": true,
        "bServerSide": true,
    },
 
    getCookie: function(name) {
        return Cookie.get(name);
    },
 
    initialize: function($$, opts) {
        Iif (typeof window.console === "undefined" || typeof window.console.log === "undefined") {
            console = {
                log: function(){},
                info: function(){}
            };
        }
        var options_name_map = {
            'config-sortable': 'bSortable',
            'config-sorting': 'aaSorting',
            'config-visible': 'bVisible'
        };
 
        var template_clear_button = $('<a href="#" class="clear-search">Clear</a>');
 
        var initialized_datatables = [];
        $$.each(function(){
            var datatable = $(this);
            var column_options = [];
            var sorting_options = [];
 
            datatable.find('thead th').each(function(){
                var header = $(this);
                datatableview.options = {};
                for (var i = 0; i < header[0].attributes.length; i++) {
                    var attr = header[0].attributes[i];
                    if (attr.specified && /^data-/.test(attr.name)) {
                        var name = attr.name.replace(/^data-/, '');
                        var value = attr.value;
 
                        // Typecasting out of string
                        name = options_name_map[name];
                        if (/^b/.test(name)) {
                            value = (value === 'true');
                        }
 
                        if (name == 'aaSorting') {
                            // This doesn't go in the column_options
                            var sort_info = value.split(',');
                            sort_info[1] = parseInt(sort_info[1]);
                            sorting_options.push(sort_info);
                            continue;
                        }
 
                        datatableview.options[name] = value;
                    }
                }
                column_options.push(datatableview.options);
            });
 
            // Arrange the sorting column requests and strip the priority information
            sorting_options.sort(function(a, b){ return a[0] - b[0]; });
            for (var i = 0; i < sorting_options.length; i++) {
                sorting_options[i] = sorting_options[i].slice(1);
            }
 
            var sEcho_count = 0;
            datatableview.options = $.extend({}, datatableview.defaults, opts, {
                "aaSorting": sorting_options,
                "aoColumns": column_options,
                "ajax": function (data, callback, settings) {
                    var table = $(opts.tableID).data('Table');
                    var client_params = $(opts.tableID).data('Table:client_params');
                    var table_params = {
                        filter_query: client_params,
                        datatables_params: {
                            iDisplayStart: data.start,
                            iDisplayLength: data.length,
                            sEcho: sEcho_count++
                        },
                    };
                    $.extend(table_params.datatables_params, get_ordering_params(data.order));
                    var widget_params = $(opts.tableID).data('Table:widget_params');
                    var table_params_string = JSON.stringify(table_params);
                    table.retrieveData(table_params_string, widget_params, function(response) {
                        callback({data: response.data.aaData,
                                  recordsTotal: response.data.iTotalRecords,
                                  recordsFiltered: response.data.iTotalDisplayRecords});
                    });
                },
                "iDisplayLength": datatable.attr('data-page-length'),
                "fnInfoCallback": function(oSettings, iStart, iEnd, iMax, iTotal, sPre){
                    $("#" + datatable.attr('data-result-counter-id')).html(parseInt(iTotal).toLocaleString());
                    var infoString = oSettings.oLanguage.sInfo.replace('_START_',iStart).replace('_END_',iEnd).replace('_TOTAL_',iTotal);
                    Iif (iMax != iTotal) {
                        infoString += oSettings.oLanguage.sInfoFiltered.replace('_MAX_',iMax);
                    }
                    return infoString;
                },
                "bFilter": false
            });
            try {
                datatableview.options = confirm_datatable_options(datatableview.options, datatable);
            } catch (e) {
 
            }
 
            var initialized_datatable = datatable.dataTable(datatableview.options);
            initialized_datatables.push(initialized_datatable[0]);
 
            try {
                initialized_datatable.fnSetFilteringDelay();
            } catch (e) {
                console.info("datatable plugin fnSetFilteringDelay not available");
            }
 
            var search_input = initialized_datatable.closest('.dataTables_wrapper').find('.dataTables_filter input');
            var clear_button = template_clear_button.clone().click(function(){
                $(this).trigger('clear.datatable', [initialized_datatable]);
                return false;
            }).bind('clear.datatable', function(){
                search_input.val('').keyup();
            });
            search_input.after(clear_button).after(' ');
        });
        return $(initialized_datatables).dataTable();
    }
};
 
function get_ordering_params(order_data) {
    Iif (order_data.length > 1)
        console.warn("Multiple columns ordering not supported");
    Iif (order_data.length > 0) {
        return {
            iSortingCols: order_data.length,
            iSortCol_0: order_data[0].column,
            sSortDir_0: order_data[0].dir,
        };
    }
    else return { iSortingCols: 0 };
}
 
module.exports = datatableview;