all files / js/ filterui.js

100% Statements 85/85
100% Branches 18/18
100% Functions 29/29
100% Lines 58/58
2 statements, 5 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 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                                       15× 15× 15× 15× 15×   15× 15× 15× 15×                     15×       15× 15×       16×       15× 15×                                               15×       15× 15× 15×       15× 15× 15×       15× 15× 15×       15× 15× 15×       45×       45×       45×            
import $ from 'jquery';
import 'jquery-ui';
import 'jquery-deserialize';
import 'jquery-ui-timepicker-addon';
import { freewall as Freewall } from 'freewall';
 
const TIME_PICKER_SETTINGS = {
    showButtonPanel: true,
    controlType: 'select',
    oneLine: true,
    dateFormat: "yy-mm-dd"
};
 
const DATE_PICKER_SETTINGS = {
    showButtonPanel: true,
    dateFormat: "yy-mm-dd"
};
 
/** Set of filters.
 * @constructor
 * @param {string} containerID - DOM element ID of the widget
 * @param {string[]} [tabs] -
 list of selectors pointing to divs corresponding to each tab
 */
class FilterForm {
 
    constructor(containerID, tabs) {
        this.containerID = containerID;
        this.tabs = tabs;
        this.walls = [];
        if (tabs && tabs instanceof Array) {
            this._setFreewall();
            this._setTabs();
        }
        this._deserializeForms();
        this._setSerializationOnChangeEvent();
        this._addCustomPickers();
        this._saveReferenceInDOM();
    }
 
    serialize() {
        return $(this.containerID).serialize();
    }
 
    onSubmit(callback) {
        $(this.containerID).on("submit", (event) => {
            event.preventDefault();
            event.stopPropagation();
            callback(event);
        });
    }
 
    _getHashString() {
        return window.location.hash.substring(1);
    }
 
    _deserializeForms() {
        var data = this._getHashString();
        $('filter-form').find('form').deserialize(data);
    }
 
    _serializeForms() {
        var nonEmptyInputFields = $("filter-form input").filter(function () {
            return !!this.value;
        });
        var newHash = nonEmptyInputFields.serialize();
        window.location.hash = newHash;
    }
 
    _setSerializationOnChangeEvent() {
        $(this.containerID).on("change", () => {
            this._serializeForms();
        });
    }
 
    _setTabs() {
        $(this.containerID + '_ff').tabs({
            create: this._rearrangeAllColumns.bind(this),
            activate: this._rearrangeAllColumns.bind(this)
        });
    }
 
    _rearrangeAllColumns() {
        this.walls.forEach((wall) => wall.fitWidth());
    }
 
    _setFreewall() {
        this.tabs.forEach((tab) => {
            var wall = new Freewall(tab);
            this.walls.push(wall);
            wall.reset({
                selector: '.ff-group',
                animate: true,
                cellW: 170,
                cellH: 'auto',
                gutterY: 0,
                onResize: this._rearrangeAllColumns
            });
            this._rearrangeAllColumns();
        });
    }
 
    _saveReferenceInDOM() {
        $(this.containerID).data('FilterForm', this);
    }
 
    _addCustomPickers() {
        this._addCustomDatePicker();
        this._addCustiomTimeDateTimePicker();
        this._addCustomTimePicker();
    }
 
    _addCustomDatePicker() {
        var dateFields = this._getInputFieldsOfType('date');
        dateFields.datepicker(DATE_PICKER_SETTINGS);
        this._convertInputToTextType(dateFields);
    }
 
    _addCustiomTimeDateTimePicker() {
        var dateTimeFields = this._getInputFieldsOfType('datetime-local');
        dateTimeFields.datetimepicker(TIME_PICKER_SETTINGS);
        this._convertInputToTextType(dateTimeFields);
    }
 
    _addCustomTimePicker() {
        var timeFields = this._getInputFieldsOfType('time');
        timeFields.timepicker(TIME_PICKER_SETTINGS);
        this._convertInputToTextType(timeFields);
    }
 
    _getInputFieldsOfType(inputType) {
        return $("filter-form").find(this._getInputTypeSelector(inputType));
    }
 
    _getInputTypeSelector(inputType) {
        return "input[type='" + inputType + "']";
    }
 
    _convertInputToTextType($field) {
        $field.attr('type', 'text');
    }
 
}
 
export default FilterForm;