All files / web/bundles/pimui/js/filter/attribute date.js

100% Statements 42/42
100% Branches 22/22
100% Functions 7/7
100% Lines 42/42

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 150 151 152 153 154 155 156 157 158 159    2x                                       2x                                                 7x 37x     7x             129x   129x     1x     128x     24x     104x             24x 24x   24x   24x 22x       24x 10x                   24x 24x 24x 24x   24x 9x 9x     24x                               14x 14x   14x 14x   14x 13x 13x 13x 13x   13x   13x 8x 8x   8x       14x           14x        
'use strict';
 
define([
    'jquery',
    'underscore',
    'oro/translator',
    'pim/filter/attribute/attribute',
    'pim/template/filter/attribute/date',
    'datepicker',
    'pim/formatter/date',
    'pim/date-context',
    'jquery.select2'
], function (
    $,
    _,
    __,
    BaseFilter,
    template,
    Datepicker,
    DateFormatter,
    DateContext
) {
    return BaseFilter.extend({
        shortname: 'date',
        template: _.template(template),
        events: {
            'change [name^="filter-"]': 'updateState'
        },
 
        /**
         * Date widget options
         */
        datetimepickerOptions: {
            format: DateContext.get('date').format,
            defaultFormat: DateContext.get('date').defaultFormat,
            language: DateContext.get('language')
        },
 
        /**
         * Model date format
         */
        modelDateFormat: 'yyyy-MM-dd',
 
        /**
         * {@inheritdoc}
         */
        configure: function () {
            this.listenTo(this.getRoot(), 'pim_enrich:form:entity:pre_update', function (data) {
                _.defaults(data, {field: this.getCode(), operator: _.first(_.values(this.config.operators))});
            }.bind(this));
 
            return BaseFilter.prototype.configure.apply(this, arguments);
        },
 
        /**
         * {@inherit}
         */
        isEmpty: function () {
            var value = this.getValue();
 
            if (_.contains(['BETWEEN', 'NOT BETWEEN'], this.getOperator()) &&
                (undefined === value || !_.isArray(value) || _.isEmpty(value[0]) || _.isEmpty(value[1]))
            ) {
                return true;
            }
 
            if (!_.contains(['EMPTY', 'NOT EMPTY'], this.getOperator()) &&
                (undefined === value || '' === value)
            ) {
                return true;
            }
 
            return false;
        },
 
        /**
         * Initializes select2 and datepicker after rendering.
         */
        postRender: function () {
            var startDate = this.$('.start-date-wrapper:first');
            var endDate = this.$('.end-date-wrapper:first');
 
            this.$('[name="filter-operator"]').select2();
 
            if (0 !== startDate.length) {
                Datepicker
                    .init(startDate, this.datetimepickerOptions)
                    .on('changeDate', this.updateState.bind(this));
            }
            if (0 !== endDate.length) {
                Datepicker
                    .init(endDate, this.datetimepickerOptions)
                    .on('changeDate', this.updateState.bind(this));
            }
        },
 
        /**
         * {@inherit}
         */
        renderInput: function () {
            var dateFormat = DateContext.get('date').format;
            var value = this.getValue();
            var startValue = DateFormatter.format(value, this.modelDateFormat, dateFormat);
            var endValue = null;
 
            if (_.isArray(value)) {
                startValue = DateFormatter.format(value[0], this.modelDateFormat, dateFormat);
                endValue = DateFormatter.format(value[1], this.modelDateFormat, dateFormat);
            }
 
            return this.template({
                isEditable: this.isEditable(),
                __: __,
                shortName: this.shortname,
                field: this.getField(),
                operator: this.getOperator(),
                startValue: startValue,
                endValue: endValue,
                operators: this.getLabelledOperatorChoices(this.shortname)
            });
        },
 
        /**
         * {@inherit}
         */
        updateState: function () {
            this.$('.start-date-wrapper').datetimepicker('hide');
            this.$('.end-date-wrapper').datetimepicker('hide');
 
            var value    = null;
            var operator = this.$('[name="filter-operator"]').val();
 
            if (!_.contains(['EMPTY', 'NOT EMPTY'], operator)) {
                var dateFormat = DateContext.get('date').format;
                var startValue = this.$('[name="filter-value-start"]').val();
                var formattedStartVal = DateFormatter.format(startValue, dateFormat, this.modelDateFormat);
                var valueEndField = this.$('[name="filter-value-end"]');
 
                value = formattedStartVal;
 
                if (0 !== valueEndField.length) {
                    var endValue = valueEndField.val();
                    var formattedEndVal = DateFormatter.format(endValue, dateFormat, this.modelDateFormat);
 
                    value = [formattedStartVal, formattedEndVal];
                }
            }
 
            this.setData({
                field: this.getField(),
                operator: operator,
                value: value
            });
 
            this.render();
        }
    });
});