SearchLayoutView.js 21.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

define(['require',
    'backbone',
    'hbs!tmpl/search/SearchLayoutView_tmpl',
    'utils/Utils',
23 24
    'utils/UrlLinks',
    'utils/Globals',
25 26
    'utils/CommonViewFunction'
], function(require, Backbone, SearchLayoutViewTmpl, Utils, UrlLinks, Globals, CommonViewFunction) {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    'use strict';

    var SearchLayoutView = Backbone.Marionette.LayoutView.extend(
        /** @lends SearchLayoutView */
        {
            _viewName: 'SearchLayoutView',

            template: SearchLayoutViewTmpl,

            /** Layout sub regions */
            regions: {},

            /** ui selector cache */
            ui: {
                searchInput: '[data-id="searchInput"]',
42 43
                searchType: 'input[name="queryType"]',
                searchBtn: '[data-id="searchBtn"]',
44
                clearSearch: '[data-id="clearSearch"]',
45
                typeLov: '[data-id="typeLOV"]',
46 47
                tagLov: '[data-id="tagLOV"]',
                refreshBtn: '[data-id="refreshBtn"]',
48 49 50
                advancedInfoBtn: '[data-id="advancedInfo"]',
                typeAttrFilter: '[data-id="typeAttrFilter"]',
                tagAttrFilter: '[data-id="tagAttrFilter"]'
51 52
            },

53 54 55 56 57 58 59
            /** ui events hash */
            events: function() {
                var events = {},
                    that = this;
                events["keyup " + this.ui.searchInput] = function(e) {
                    var code = e.which;
                    if (code == 13) {
60
                        that.findSearchResult();
61
                    }
62
                    this.checkForButtonVisiblity();
63
                };
64 65 66
                events["change " + this.ui.searchType] = 'dslFulltextToggle';
                events["click " + this.ui.searchBtn] = 'findSearchResult';
                events["click " + this.ui.clearSearch] = 'clearSearchData';
67 68
                events["change " + this.ui.typeLov] = 'checkForButtonVisiblity';
                events["change " + this.ui.tagLov] = 'checkForButtonVisiblity';
69
                events["click " + this.ui.refreshBtn] = 'onRefreshButton';
70
                events["click " + this.ui.advancedInfoBtn] = 'advancedInfo';
71 72 73 74 75 76
                events["click " + this.ui.typeAttrFilter] = function() {
                    this.openAttrFilter('type');
                };
                events["click " + this.ui.tagAttrFilter] = function() {
                    this.openAttrFilter('tag');
                };
77 78 79 80 81 82 83
                return events;
            },
            /**
             * intialize a new SearchLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
84
                _.extend(this, _.pick(options, 'value', 'typeHeaders', 'searchVent', 'entityDefCollection', 'enumDefCollection', 'classificationDefCollection', 'searchTableColumns', 'searchTableFilters'));
85
                this.type = "basic";
86 87 88
                var param = Utils.getUrlState.getQueryParams();
                this.query = {
                    dsl: {
89 90
                        query: null,
                        type: null
91
                    },
92
                    basic: {
93 94
                        query: null,
                        type: null,
95 96
                        tag: null,
                        attributes: null
97 98
                    }
                };
99 100 101
                if (!this.value) {
                    this.value = {};
                }
102
                this.dsl = false;
103
                if (param && param.searchType) {
104 105
                    this.type = param.searchType;
                    this.updateQueryObject(param);
106
                }
107
                this.bindEvents();
108 109
            },
            bindEvents: function(param) {
110
                this.listenTo(this.typeHeaders, "reset", function(value) {
111
                    this.renderTypeTagList();
112 113
                    this.setValues();
                    this.ui.typeLov.select2({
114
                        placeholder: "Select",
115 116
                        allowClear: true
                    });
117 118 119 120 121
                    this.ui.tagLov.select2({
                        placeholder: "Select",
                        allowClear: true
                    });
                    this.checkForButtonVisiblity();
122 123
                }, this);
            },
124 125 126 127 128 129 130 131 132 133
            makeFilterButtonActive: function(filtertypeParam) {
                var filtertype = ['entityFilters', 'tagFilters'],
                    that = this;
                if (filtertypeParam) {
                    if (_.isArray(filtertypeParam)) {
                        filtertype = filtertypeParam;
                    } else if (_.isString(filtertypeParam)) {
                        filtertype = [filtertypeParam];
                    }
                }
134
                var typeCheck = function(filterObj, type) {
135 136 137
                    if (that.value.type) {
                        if (filterObj && filterObj.length) {
                            that.ui.typeAttrFilter.addClass('active');
138
                        } else {
139
                            that.ui.typeAttrFilter.removeClass('active');
140
                        }
141 142 143 144
                        that.ui.typeAttrFilter.prop('disabled', false);
                    } else {
                        that.ui.typeAttrFilter.removeClass('active');
                        that.ui.typeAttrFilter.prop('disabled', true);
145
                    }
146
                }
147
                var tagCheck = function(filterObj, type) {
148 149 150 151
                    if (that.value.tag) {
                        that.ui.tagAttrFilter.prop('disabled', false);
                        if (filterObj && filterObj.length) {
                            that.ui.tagAttrFilter.addClass('active');
152
                        } else {
153
                            that.ui.tagAttrFilter.removeClass('active');
154
                        }
155 156 157
                    } else {
                        that.ui.tagAttrFilter.removeClass('active');
                        that.ui.tagAttrFilter.prop('disabled', true);
158 159
                    }
                }
160
                _.each(filtertype, function(type) {
161
                    var filterObj = that.searchTableFilters[type];
162
                    if (type == "entityFilters") {
163
                        typeCheck(filterObj[that.value.type], type);
164 165
                    }
                    if (type == "tagFilters") {
166
                        tagCheck(filterObj[that.value.tag], type);
167 168
                    }
                });
169
            },
170 171 172 173 174 175 176 177 178 179
            checkForButtonVisiblity: function(e) {
                if (this.type == "basic" && e && e.currentTarget) {
                    var $el = $(e.currentTarget),
                        isTagEl = $el.data('id') == "tagLOV" ? true : false;
                    if (e.type == "change" && $el.select2('data')) {
                        var value = $el.val(),
                            key = (isTagEl ? 'tag' : 'type'),
                            filterType = (isTagEl ? 'tagFilters' : 'entityFilters'),
                            value = value.length ? value : null;
                        if (this.value) {
180 181
                            //On Change handle
                            if (this.value[key] !== value || (!value && !this.value[key])) {
182 183 184
                                var temp = {};
                                temp[key] = value;
                                _.extend(this.value, temp);
185 186 187 188 189 190
                            } else {
                                // Initial loading handle.
                                var filterObj = this.searchTableFilters[filterType];
                                if (filterObj && this.value[key]) {
                                    this.searchTableFilters[filterType][this.value[key]] = this.value[filterType] ? this.value[filterType] : null;
                                }
191
                            }
192
                            this.makeFilterButtonActive(filterType);
193 194 195 196 197 198
                        } else {
                            this.ui.tagAttrFilter.prop('disabled', true);
                            this.ui.typeAttrFilter.prop('disabled', true);
                        }
                    }
                }
199 200
                var that = this,
                    value = this.ui.searchInput.val() || this.ui.typeLov.val();
201
                if (!this.dsl && !value) {
202 203 204 205 206 207 208 209 210 211 212
                    value = this.ui.tagLov.val();
                }
                if (value && value.length) {
                    this.ui.searchBtn.removeAttr("disabled");
                    setTimeout(function() {
                        that.ui.searchInput.focus();
                    }, 0);
                } else {
                    this.ui.searchBtn.attr("disabled", "true");
                }
            },
213 214
            onRender: function() {
                // array of tags which is coming from url
215
                this.renderTypeTagList();
216 217
                this.setValues();
                this.ui.typeLov.select2({
218 219 220 221 222
                    placeholder: "Select",
                    allowClear: true
                });
                this.ui.tagLov.select2({
                    placeholder: "Select",
223 224
                    allowClear: true
                });
225
            },
226 227 228 229
            updateQueryObject: function(param) {
                if (param && param.searchType) {
                    this.type = param.searchType;
                }
230 231 232 233 234
                _.extend(this.query[this.type],
                    (this.type == "dsl" ? {
                        query: null,
                        type: null
                    } : {
235 236
                        query: null,
                        type: null,
237
                        tag: null,
238
                        attributes: null
239
                    }), param);
240
            },
241
            fetchCollection: function(value) {
242
                this.typeHeaders.fetch({ reset: true });
243
            },
244 245
            onRefreshButton: function() {
                this.fetchCollection();
246 247 248
                //to check url query param contain type or not 
                var checkURLValue = Utils.getUrlState.getQueryParams(this.url);
                if (this.searchVent && (_.has(checkURLValue, "tag") || _.has(checkURLValue, "type") || _.has(checkURLValue, "query"))) {
249 250
                    this.searchVent.trigger('search:refresh');
                }
251
            },
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
            advancedInfo: function(e) {
                require([
                    'views/search/AdvancedSearchInfoView',
                    'modules/Modal'
                ], function(AdvancedSearchInfoView, Modal) {
                    var view = new AdvancedSearchInfoView();
                    var modal = new Modal({
                        title: 'Advanced Search Queries',
                        content: view,
                        okCloses: true,
                        showFooter: true,
                        allowCancel: false
                    }).open();
                    view.on('closeModal', function() {
                        modal.trigger('cancel');
                    });
                });
            },
270 271 272 273 274 275 276 277 278 279 280
            openAttrFilter: function(filterType) {
                var that = this;
                require(['views/search/SearchQueryView'], function(SearchQueryView) {
                    that.attrModal = new SearchQueryView({
                        value: that.value,
                        tag: (filterType === "tag" ? true : false),
                        type: (filterType === "type" ? true : false),
                        searchVent: that.searchVent,
                        typeHeaders: that.typeHeaders,
                        entityDefCollection: that.entityDefCollection,
                        enumDefCollection: that.enumDefCollection,
281 282
                        classificationDefCollection: that.classificationDefCollection,
                        searchTableFilters: that.searchTableFilters
283
                    });
284 285
                    that.attrModal.on('ok', function(scope, e) {
                        that.okAttrFilterButton(e);
286 287 288
                    });
                });
            },
289
            okAttrFilterButton: function(e) {
290 291
                var isTag = this.attrModal.tag ? true : false,
                    filtertype = isTag ? 'tagFilters' : 'entityFilters',
292 293 294 295
                    queryBuilderRef = this.attrModal.RQueryBuilder.currentView.ui.builder;
                if (queryBuilderRef.data('queryBuilder')) {
                    var rule = queryBuilderRef.queryBuilder('getRules');
                }
296
                if (rule) {
297 298
                    var ruleUrl = CommonViewFunction.attributeFilter.generateUrl(rule.rules);
                    this.searchTableFilters[filtertype][(isTag ? this.value.tag : this.value.type)] = ruleUrl;
299
                    this.makeFilterButtonActive(filtertype);
300
                    if (!isTag && this.value && this.value.type && this.searchTableColumns) {
301 302 303 304 305
                        if (!this.searchTableColumns[this.value.type]) {
                            this.searchTableColumns[this.value.type] = ["selected", "name", "owner", "description", "tag", "typeName"]
                        }
                        this.searchTableColumns[this.value.type] = _.sortBy(_.union(this.searchTableColumns[this.value.type], _.pluck(rule.rules, 'id')));
                    }
306
                    this.attrModal.modal.close();
307 308
                    if ($(e.currentTarget).hasClass('search')) {
                        this.findSearchResult();
309 310 311
                    }
                }
            },
312
            manualRender: function(paramObj) {
313
                this.updateQueryObject(paramObj);
314
                this.setValues(paramObj);
315
            },
316
            renderTypeTagList: function() {
317 318
                var that = this;
                this.ui.typeLov.empty();
319 320
                var typeStr = '<option></option>',
                    tagStr = typeStr;
321 322
                this.typeHeaders.fullCollection.comparator = function(model) {
                    return Utils.getName(model.toJSON(), 'name').toLowerCase();
323
                }
324
                this.typeHeaders.fullCollection.sort().each(function(model) {
325
                    var name = Utils.getName(model.toJSON(), 'name');
326
                    if (model.get('category') == 'ENTITY') {
327
                        typeStr += '<option>' + (name) + '</option>';
328 329
                    }
                    if (model.get('category') == 'CLASSIFICATION') {
330 331 332 333
                        var checkTagOrTerm = Utils.checkTagOrTerm(name);
                        if (checkTagOrTerm.tag) {
                            tagStr += '<option>' + (name) + '</option>';
                        }
334
                    }
335
                });
336 337
                that.ui.typeLov.html(typeStr);
                that.ui.tagLov.html(tagStr);
338
            },
339
            setValues: function(paramObj) {
340 341
                var arr = [],
                    that = this;
342 343 344 345
                if (paramObj) {
                    this.value = paramObj;
                }
                if (this.value) {
346
                    if (this.value.dslChecked == "true") {
347 348 349
                        if (!this.ui.searchType.prop("checked")) {
                            this.ui.searchType.prop("checked", true).trigger("change");
                        }
350
                    } else {
351 352 353
                        if (this.ui.searchType.prop("checked")) {
                            this.ui.searchType.prop("checked", false).trigger("change");
                        }
354
                    }
355
                    this.ui.typeLov.val(this.value.type);
356
                    if (this.ui.typeLov.data('select2')) {
357 358 359 360 361 362
                        if (this.ui.typeLov.val() !== this.value.type) {
                            this.value.type = null;
                            this.ui.typeLov.val("").trigger("change");
                        } else {
                            this.ui.typeLov.trigger("change");
                        }
363
                    }
364

365
                    if (!this.dsl) {
366
                        this.ui.tagLov.val(this.value.tag);
367
                        if (this.ui.tagLov.data('select2')) {
368 369 370 371 372 373 374
                            // To handle delete scenario.
                            if (this.ui.tagLov.val() !== this.value.tag) {
                                this.value.tag = null;
                                this.ui.tagLov.val("").trigger("change");
                            } else {
                                this.ui.tagLov.trigger("change");
                            }
375 376
                        }
                    }
377 378 379 380
                    this.ui.searchInput.val(this.value.query || "");
                    setTimeout(function() {
                        that.ui.searchInput.focus();
                    }, 0);
381 382 383
                }
            },
            findSearchResult: function() {
384 385 386
                this.triggerSearch(this.ui.searchInput.val());
            },
            triggerSearch: function(value) {
387
                this.query[this.type].query = value || null;
388 389
                var params = {
                    searchType: this.type,
390 391
                    dslChecked: this.ui.searchType.is(':checked'),
                    tagFilters: null,
392 393 394
                    entityFilters: null,
                    attributes: null,
                    includeDE: null
395
                }
396
                this.query[this.type].type = this.ui.typeLov.select2('val') || null;
397
                if (!this.dsl) {
398
                    this.query[this.type].tag = this.ui.tagLov.select2('val') || null;
399 400 401 402 403 404 405 406
                    var entityFilterObj = this.searchTableFilters['entityFilters'],
                        tagFilterObj = this.searchTableFilters['tagFilters'];
                    if (this.value.tag) {
                        params['tagFilters'] = tagFilterObj[this.value.tag]
                    }
                    if (this.value.type) {
                        params['entityFilters'] = entityFilterObj[this.value.type]
                    }
407
                    var columnList = this.value && this.value.type && this.searchTableColumns ? this.searchTableColumns[this.value.type] : null;
408
                    if (columnList) {
409
                        params['attributes'] = columnList.join(',');
410 411 412
                    }
                    if (this.value.includeDE) {
                        params['includeDE'] = this.value.includeDE
413 414 415
                    }
                }

416 417
                Utils.setUrl({
                    url: '#!/search/searchResult',
418
                    urlParams: _.extend(this.query[this.type], params),
419 420 421 422 423 424 425 426
                    updateTabState: function() {
                        return { searchUrl: this.url, stateChanged: true };
                    },
                    mergeBrowserUrl: false,
                    trigger: true
                });
            },
            dslFulltextToggle: function(e) {
427
                var paramObj = Utils.getUrlState.getQueryParams();
428 429
                if (e.currentTarget.checked) {
                    this.type = "dsl";
430
                    this.dsl = true;
431
                    this.$('.tagBox').hide();
432
                    this.$('.temFilterBtn').hide();
433 434
                    this.$('.temFilter').addClass('col-sm-12');
                    this.$('.temFilter').removeClass('col-sm-10');
435
                } else {
436 437
                    this.$('.temFilter').addClass('col-sm-10');
                    this.$('.temFilter').removeClass('col-sm-12');
438
                    this.$('.temFilterBtn').show();
439
                    this.$('.tagBox').show();
440
                    this.dsl = false;
441
                    this.type = "basic";
442
                }
443 444 445
                if (paramObj && this.type == paramObj.searchType) {
                    this.updateQueryObject(paramObj);
                }
446
                if (paramObj && this.type == "basic") {
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
                    this.query[this.type].attributes = paramObj.attributes ? paramObj.attributes : null;
                }
                if (Utils.getUrlState.isSearchTab()) {
                    Utils.setUrl({
                        url: '#!/search/searchResult',
                        urlParams: _.extend(this.query[this.type], {
                            searchType: this.type,
                            dslChecked: this.ui.searchType.is(':checked'),
                            includeDE: this.value.includeDE
                        }),
                        updateTabState: function() {
                            return { searchUrl: this.url, stateChanged: true };
                        },
                        mergeBrowserUrl: false,
                        trigger: true
                    });
463
                }
464 465
            },
            clearSearchData: function() {
466
                this.updateQueryObject();
467
                this.ui.typeLov.val("").trigger("change");
468
                this.ui.tagLov.val("").trigger("change");
469
                this.ui.searchInput.val("");
470
                if (!this.dsl) {
471 472 473
                    this.searchTableFilters.tagFilters = {};
                    this.searchTableFilters.entityFilters = {};
                }
474
                this.checkForButtonVisiblity();
475
                Utils.setUrl({
476 477 478 479 480
                    url: '#!/search/searchResult',
                    urlParams: {
                        searchType: this.type,
                        dslChecked: this.ui.searchType.is(':checked')
                    },
481 482 483
                    mergeBrowserUrl: false,
                    trigger: true
                });
484
            }
485 486
        });
    return SearchLayoutView;
487
});