filters-controllet.html 14.3 KB
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
<link rel="import" href="../../bower_components/polymer/polymer.html" />

<link rel="import" href="../../bower_components/paper-material/paper-material.html" />

<link rel="import" href="../../bower_components/paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="../../bower_components/paper-menu/paper-menu.html">
<link rel="import" href="../../bower_components/paper-item/paper-item.html">

<link rel="import" href="../../bower_components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="../../bower_components/iron-icons/iron-icons.html">
<link rel="import" href="../../bower_components/iron-icon/iron-icon.html">

<link rel="import" href="../../bower_components/paper-button/paper-button.html">

<dom-module id="filters-controllet">

    <template>

        <style is="custom-style">

            #filters_container {
                height: 48px;
                width: 100%;
            }

            #filters_container * {
                font-family: 'Roboto', 'Helvetica Neue', Helvetica, Arial, sans-serif;
                font-size: 16px;
                line-height: 24px;
            }

            #filters_container #header {
                height: 32px;
                padding-top: 16px;
                text-align: center;
                font-weight: 700;

                background: #FFFFFF;
                color: #00BCD4;
                cursor: pointer;
            }

            #filters_container #header.hide{
                color: #00BCD4;
                background: #FFFFFF;
            }

            /*#filters_container #header.hide:hover{*/
            /*}*/

            #filters_container #header.show{
                color: #000000;
                background: #B6B6B6;
            }

            #filters_container #panel {
                display: none;
                position: relative;
                height: calc(100% - 48px);
                background: #FFFFFF;
            }

            paper-dropdown-menu {
                width: 100%;
                --paper-input-container-focus-color: #2196F3;
            }

            :host {
            --paper-dropdown-menu-icon: {
                 color: #2196F3;
             };
            }

            paper-item {
                min-width: 128px;
            }

            paper-item.iron-selected {
                background-color: #2196F3;
                color: #FFFFFF;
            }

            paper-input {
                width: 100%;
                --paper-input-container-focus-color: #2196F3;
            }

            paper-button {
                margin: 0px;
                height: 44px;
                color: #FFFFFF;
                background: #2196F3;
                font-weight: 700;
            }

            paper-button:hover {
                box-shadow: 0px 8px 12px #888;
                -webkit-box-shadow: 0px 8px 12px #888;
                -moz-box-shadow: 0px 8px 12px #888;
            }

            paper-icon-button {
                padding: 4px;
            }

            paper-icon-button.add {
                color: #2196F3;
                --paper-icon-button-ink-color: #2196F3;
            }

            paper-icon-button.cancel {
                color: #F44336;
                --paper-icon-button-ink-color: #F44336;
            }

            #filters_container .row {
                display: flex;
                height: 60px;
                width: 100%;
            }

            #filters_container .ddl_container {
                display: flex;
                height: 60px;
                width: 25%;
                padding: 0px 8px;
            }

            #filters_container .add_container {
                height: 40px;
                width: 40px;
                padding: 10px 0px;
            }

            #filters_container .andOr_container {
                display: inline-block;
                height: 44px;
                width: calc(100% - 40px);
                padding: 8px 0px;
                text-align: right;
            }

            #filters_container .row2 {
                display: flex;
                height: 48px;
                width: 100%;
            }

            #filters_container .row2 .highlighted {
                color: #2196F3;
                font-weight: 700;
            }

            #filters_container .filter {
                height: 24px;
                width: calc(75% - 16px);
                padding: 12px 8px;
            }

            #filters_container .remove_container {
                height: 40px;
                width: 40px;
                padding: 4px 8px;

            }

    </style>

        <paper-material id="filters_container" elevation="5">

            <div id="header" class="hide" on-click="_showFiltersPanel"><span id="filters"><span id="addFilters"></span></span></div>

            <div id="panel">

                <div class="row">
                    <div class="ddl_container">
                        <paper-dropdown-menu id="filter_field" label="Field">
                            <paper-menu id="filter_field_menu" class="dropdown-content">
                                <template is="dom-repeat" items={{fields}}>
                                    <paper-item id={{index}}>{{_fieldName(item)}}</paper-item>
                                </template>
                            </paper-menu>
                        </paper-dropdown-menu>
                    </div>
                    <div class="ddl_container">
                        <paper-dropdown-menu id="filter_operation" label="Operation">
                            <paper-menu id="filter_operation_menu" class="dropdown-content">
                                <template is="dom-repeat" items={{operations}}>
                                    <paper-item id={{index}}>{{_getOperationlName(item)}}</paper-item>
                                </template>
                            </paper-menu>
                        </paper-dropdown-menu>
                    </div>
                    <div class="ddl_container">
                        <paper-input id="filter_value" label="Value" class="base_input" maxlength="32" auto-validate pattern="^[.:,;+-_ a-zA-Z0-9]*" error-message="Invalid value!"></paper-input>
                    </div>
                    <div class="ddl_container">
                        <div class="add_container">
                            <paper-icon-button on-click="_addFilter" icon="add-circle" class="add"></paper-icon-button>
                        </div>
                        <div class="andOr_container">
                            <paper-button raised on-click="_changeLogicalOperator"><span id="andOr">{{logicalOperator}}</span></paper-button>
                        </div>
                    </div>
                </div>

                <template is="dom-repeat" items={{filters}}>
                    <template is="dom-if" if="{{index}}"><!--excludes logicalOperator-->
                        <div class="row2">
                            <div class="filter">
                                {{item.field}}&nbsp;&nbsp;&nbsp;<span class="highlighted">{{item.operation}}</span>&nbsp;&nbsp;&nbsp;"{{item.value}}"&nbsp;&nbsp;&nbsp;<span class="highlighted">{{_getLogicalOperator(index)}}</span>
                            </div>
                            <div class="remove_container">
                                <paper-icon-button on-click="_deleteFilter" icon="cancel" class="cancel"></paper-icon-button>
                            </div>
                        </div>
                    </template>
                </template>

            </div>

        </paper-material>

    </template>

    <script>

        Polymer({

            is : 'filters-controllet',

            properties : {

                fields : {
                    type : Array,
                    value : []
                },

                operations : {
                    type : Array,
                    value : ["=", "!=", ">", ">=", "<", "<=", "contains", "not contains", "start", "ends"]
                },

                filters : {
                    type : Array,
                    value :  [{logicalOperator: "AND"}]
                },

                show : {
                    type  : Boolean,
                    value : false
                }

            },

            ready : function() {
                $(this.$.panel).perfectScrollbar();
//                this._showFiltersPanel();
            },

            attached : function() {
                this.logicalOperator = this.filters[0].logicalOperator;

                this._translate();
            },

            setFields : function(fields) {
//                this.fields = this._copy(fields);
                this.fields = fields.slice();
                this.fields = fields;
            },

            setFilters : function(filters) {
                this.filters = filters.slice();
                this.logicalOperator = this.filters[0].logicalOperator;
                this._fire();
            },

            getFilters : function() {
                return this.filters;
            },

            reset : function() {
                this.fields = [];
                this.filters = [{logicalOperator: "AND"}];
            },

            _translate : function() {
                this.$.addFilters.innerHTML = ln["expertAddFilters_" + ln["localization"]];
                this.$.filter_field.setAttribute("label", ln["filterField_" + ln["localization"]]);
                this.$.filter_operation.setAttribute("label", ln["filterOperation_" + ln["localization"]]);
                this.$.filter_value.setAttribute("label", ln["filterValue_" + ln["localization"]]);
            },

            _fieldName : function(field) {
                return field.substring(field.lastIndexOf(",")+1, field.length);
            },

            _showFiltersPanel : function() {
//                ? filter class hiddend & hover:

                if(!this.show) {
                    this.$.filters_container.style.height = "100%";
                    $(this.$.header).removeClass("hide");
                    $(this.$.header).addClass("show");
                    this.$.panel.style.display = "block";

                }
                else {
                    this.$.filters_container.style.height = "48px";
                    $(this.$.header).removeClass("show");
                    $(this.$.header).addClass("hide");
                    this.$.panel.style.display = "none";
                }

                this.fire('filters-controllet_show', {show: this.show});

                this.show = !this.show;
            },

            _fire : function() {
                if (this.filters.length > 1)
                    this.fire('filters-controllet_filters', {filters: this.filters});
                else
                    this.fire('filters-controllet_filters', {filters: []});
            },

            _addFilter : function() {
                if (this.$.filter_field.selectedItem && this.$.filter_operation_menu.selectedItem && this.$.filter_value.value != "" && !this.$.filter_value.invalid) {
                    var field = this.$.filter_field.value;
                    var id = this.$.filter_operation.selectedItem.id;
                    var operation = this.operations[id];
//                    var operation = this.$.filter_operation.value;
                    var value = this.$.filter_value.value;

//                    var filters = this.filters;
//                    filters.push({"field": field, "operation": operation, "value": value});
//                    this.filters = filters;

                    this.filters.push({"field": field, "operation": operation, "value": value});

                    this.$.filter_field_menu.select(-1);
                    this.$.filter_operation_menu.select(-1);
                    this.$.filter_value.value = "";

                    this._fire();

                    this._renderFilters();
                }
            },

            _deleteFilter : function(e) {
                var index = e.model.index;

//                var filters = this.filters;
//                filters.splice(index, 1);
//                this.filters = this._copy(filters);

                this.filters.splice(index, 1);

                this._fire();

                this._renderFilters();
            },

            _getOperationlName: function(operation) {
                if(operation.indexOf("not") > -1)
                    return ln["notContains_" + ln["localization"]];
                if(operation.indexOf("contains") > -1)
                    return ln["contains_" + ln["localization"]];
                if(operation.indexOf("start") > -1)
                    return ln["start_" + ln["localization"]];
                if(operation.indexOf("ends") > -1)
                    return ln["ends_" + ln["localization"]];
                return operation;
            },

//            _changeLogicalOperator : function() {
//                if(this.logicalOperator == "AND")
//                    this.logicalOperator = "OR";
//                else
//                    this.logicalOperator = "AND";
//
//                this._fire();
//
//                this._renderFilters();
//            },

            _changeLogicalOperator : function() {
                if(this.logicalOperator == "AND")
                    this.logicalOperator = "OR";
                else
                    this.logicalOperator = "AND";

                this.filters[0] = {logicalOperator: this.logicalOperator}

                this._fire();

                this._renderFilters();
            },

            _renderFilters : function() {
                var filters = this.filters.slice();
                this.filters = [];
                this.async(function() {
                    this.filters = filters;
                }, 0);
            },

            _getLogicalOperator : function(index) {
                if(index == this.filters.length -1)
                    return "";
                else
                    return this.filters[0].logicalOperator;
            },

//            _copy : function(o) {
//                var out, v, key;
//                out = Array.isArray(o) ? [] : {};
//                for (key in o) {
//                    v = o[key];
//                    out[key] = (typeof v === "object") ? this._copy(v) : v;
//                }
//                return out;
//            }

        });

    </script>

</dom-module>