ATLAS-1914: clear button in search UI to clear filter attributes

Signed-off-by: Madhan Neethiraj <madhan@apache.org>
diff --git a/dashboardv2/public/js/router/Router.js b/dashboardv2/public/js/router/Router.js
index 8a1d241..f705843 100644
--- a/dashboardv2/public/js/router/Router.js
+++ b/dashboardv2/public/js/router/Router.js
@@ -53,6 +53,10 @@
                 'enumDefCollection': this.enumDefCollection,
                 'classificationDefCollection': this.classificationDefCollection
             }
+            this.filterObj = {
+                'tagFilters': JSON.parse(Utils.localStorage.getValue('tagFilters')),
+                'entityFilters': JSON.parse(Utils.localStorage.getValue('entityFilters'))
+            }
         },
         bindCommonEvents: function() {
             var that = this;
@@ -125,6 +129,7 @@
                         App.rSideNav.show(new SideNavLayoutView(
                             _.extend({
                                 'url': url,
+                                'filterObj': that.filterObj
                             }, that.preFetchedCollectionLists)
                         ));
                     } else {
@@ -156,7 +161,7 @@
                     App.rNHeader.show(new Header());
                     if (!App.rSideNav.currentView) {
                         App.rSideNav.show(new SideNavLayoutView(
-                            _.extend({}, that.preFetchedCollectionLists)
+                            _.extend({ 'filterObj': that.filterObj }, that.preFetchedCollectionLists)
                         ));
                     } else {
                         App.rSideNav.currentView.selectTab();
@@ -193,7 +198,8 @@
                     }
                     App.rSideNav.show(new SideNavLayoutView(
                         _.extend({
-                            'tag': tagName
+                            'tag': tagName,
+                            'filterObj': that.filterObj
                         }, that.preFetchedCollectionLists)
                     ));
                 } else {
@@ -231,17 +237,13 @@
                 'views/business_catalog/SideNavLayoutView',
                 'views/search/SearchDetailLayoutView',
             ], function(Header, BusinessCatalogLayoutView, SideNavLayoutView, SearchDetailLayoutView) {
-                var paramObj = Utils.getUrlState.getQueryParams(),
-                    filterObj = {
-                        'tagFilters': JSON.parse(Utils.localStorage.getValue('tagFilters')),
-                        'entityFilters': JSON.parse(Utils.localStorage.getValue('entityFilters'))
-                    }
+                var paramObj = Utils.getUrlState.getQueryParams();
                 App.rNHeader.show(new Header());
                 if (!App.rSideNav.currentView) {
                     App.rSideNav.show(new SideNavLayoutView(
                         _.extend({
                             'searchVent': that.searchVent,
-                            'filterObj': filterObj
+                            'filterObj': that.filterObj
                         }, that.preFetchedCollectionLists)
                     ));
                 } else {
@@ -257,7 +259,7 @@
                         _.extend({
                             'value': paramObj,
                             'initialView': true,
-                            'filterObj': filterObj,
+                            'filterObj': that.filterObj,
                             'searchVent': that.searchVent
                         }, that.preFetchedCollectionLists)
                     ));
@@ -276,10 +278,7 @@
                 'views/search/SearchDetailLayoutView'
             ], function(Header, BusinessCatalogLayoutView, SideNavLayoutView, SearchDetailLayoutView) {
                 var paramObj = Utils.getUrlState.getQueryParams(),
-                    filterObj = {
-                        'tagFilters': JSON.parse(Utils.localStorage.getValue('tagFilters')),
-                        'entityFilters': JSON.parse(Utils.localStorage.getValue('entityFilters'))
-                    }
+                    filterObj = that.filterObj
                 if (paramObj && paramObj.searchType === "basic") {
                     if (paramObj.type) {
                         if (_.has(filterObj.entityFilters, paramObj.type)) {
@@ -310,7 +309,7 @@
                         _.extend({
                             'value': paramObj,
                             'searchVent': that.searchVent,
-                            'filterObj': filterObj
+                            'filterObj': that.filterObj
                         }, that.preFetchedCollectionLists)
                     ));
                 } else {
@@ -321,7 +320,7 @@
                     _.extend({
                         'value': paramObj,
                         'searchVent': that.searchVent,
-                        'filterObj': filterObj,
+                        'filterObj': that.filterObj,
                         'initialView': (paramObj.type || (paramObj.dslChecked == "true" ? "" : paramObj.tag) || (paramObj.query ? paramObj.query.trim() : "")).length === 0
                     }, that.preFetchedCollectionLists)
                 ));
@@ -342,4 +341,4 @@
         }
     });
     return AppRouter;
-});
+});
\ No newline at end of file
diff --git a/dashboardv2/public/js/views/business_catalog/SideNavLayoutView.js b/dashboardv2/public/js/views/business_catalog/SideNavLayoutView.js
index 013c72e..8f41399 100644
--- a/dashboardv2/public/js/views/business_catalog/SideNavLayoutView.js
+++ b/dashboardv2/public/js/views/business_catalog/SideNavLayoutView.js
@@ -96,6 +96,8 @@
                 that.RTagLayoutView.show(new TagLayoutView({
                     collection: that.classificationDefCollection,
                     tag: that.tag,
+                    value: that.value,
+                    filterObj: that.filterObj,
                     typeHeaders: that.typeHeaders
                 }));
             });
@@ -128,4 +130,4 @@
         },
     });
     return SideNavLayoutView;
-});
+});
\ No newline at end of file
diff --git a/dashboardv2/public/js/views/search/QueryBuilderView.js b/dashboardv2/public/js/views/search/QueryBuilderView.js
index 468a2d7..ec20734 100644
--- a/dashboardv2/public/js/views/search/QueryBuilderView.js
+++ b/dashboardv2/public/js/views/search/QueryBuilderView.js
@@ -138,13 +138,27 @@
                         }
                     }
                 }
-
                 _.each(this.attrObj, function(obj) {
                     var returnObj = that.getObjDef(obj, rules_widgets);
                     if (returnObj) {
                         filters.push(returnObj);
                     }
                 });
+                filters = _.uniq(filters, 'id');
+                if (rules_widgets) {
+                    for (var i = 0; i < rules_widgets.rules.length; i++) {
+                        if (!_.find(filters, { id: rules_widgets.rules[i].id })) {
+                            var type = (this.tag ? 'tagFilters' : 'entityFilters');
+                            var list = JSON.parse(Utils.localStorage.getValue(type));
+                            delete list[this.value.tag];
+                            list = _.isEmpty(list) ? null : list;
+                            Utils.localStorage.setValue(type, JSON.stringify(list));
+                            this.filterObj[type] = list;
+                            rules_widgets = null;
+                            break;
+                        }
+                    }
+                }
                 if (filters && !_.isEmpty(filters)) {
                     this.ui.builder.queryBuilder({
                         plugins: ['bt-tooltip-errors'],
@@ -178,4 +192,4 @@
             }
         });
     return QueryBuilderView;
-});
+});
\ No newline at end of file
diff --git a/dashboardv2/public/js/views/search/SearchLayoutView.js b/dashboardv2/public/js/views/search/SearchLayoutView.js
index cb8844d..76d6f77 100644
--- a/dashboardv2/public/js/views/search/SearchLayoutView.js
+++ b/dashboardv2/public/js/views/search/SearchLayoutView.js
@@ -282,32 +282,36 @@
                 var filtertype = this.attrModal.tag ? 'tagFilters' : 'entityFilters',
                     rule = this.attrModal.RQueryBuilder.currentView.ui.builder.queryBuilder('getRules'),
                     result = this.getQueryBuilderParsData(rule);
-                if (result && !_.isEmpty(result.criterion)) {
-                    this.query[this.type][filtertype] = +new Date();
-                    if (result) {
-                        var filterObj = this.filterObj ? this.filterObj[filtertype] : null;
-                        if (!filterObj) {
-                            filterObj = {};
+
+                if (result) {
+                    if (!_.isEmpty(result.criterion)) {
+                        this.query[this.type][filtertype] = +new Date();
+                        if (result) {
+                            var filterObj = this.filterObj ? this.filterObj[filtertype] : null;
+                            if (!filterObj) {
+                                filterObj = {};
+                            }
+                            var temp = {}; // IE fix
+                            temp[(this.attrModal.tag ? this.value.tag : this.value.type)] = { 'result': result, 'rule': rule };
+                            _.extend(filterObj, temp);
+                            this.filterObj[filtertype] = filterObj;
+                            this.makeFilterButtonActive(this.attrModal.tag ? 'tag' : 'type');
+                            Utils.localStorage.setValue((filtertype), JSON.stringify(filterObj));
+                        } else {
+                            this.filterObj[filtertype] = null;
+                            this.query[this.type][filtertype] = null;
+                            this.makeFilterButtonActive(this.attrModal.tag ? 'tag' : 'type');
+                            Utils.localStorage.removeValue(filtertype);
                         }
-                        var temp = {}; // IE fix
-                        temp[(this.attrModal.tag ? this.value.tag : this.value.type)] = { 'result': result, 'rule': rule };
-                        _.extend(filterObj, temp);
-                        this.filterObj[filtertype] = filterObj;
-                        this.makeFilterButtonActive(this.attrModal.tag ? 'tag' : 'type');
-                        Utils.localStorage.setValue((filtertype), JSON.stringify(filterObj));
-                    } else {
-                        this.filterObj[filtertype] = null;
-                        this.query[this.type][filtertype] = null;
-                        this.makeFilterButtonActive(this.attrModal.tag ? 'tag' : 'type');
-                        Utils.localStorage.removeValue(filtertype);
+
                     }
+                    this.attrModal.modal.close();
                 } else {
                     this.filterObj[filtertype] = null;
                     this.query[this.type][filtertype] = null;
                     this.makeFilterButtonActive(this.attrModal.tag ? 'tag' : 'type');
                     Utils.localStorage.removeValue(filtertype);
                 }
-                this.attrModal.modal.close();
             },
             getQueryBuilderParsData: function(obj) {
                 if (obj) {
@@ -378,17 +382,26 @@
                     } else if (this.value.dslChecked == "false" && this.dsl == true) {
                         this.ui.searchType.prop("checked", false).trigger("change");
                     }
+                    this.ui.typeLov.val(this.value.type);
                     if (this.ui.typeLov.data('select2')) {
-                        this.ui.typeLov.val(this.value.type).trigger('change');
-                    } else {
-                        this.ui.typeLov.val(this.value.type);
+                        if (this.ui.typeLov.val() !== this.value.type) {
+                            this.value.type = null;
+                            this.ui.typeLov.val("").trigger("change");
+                        } else {
+                            this.ui.typeLov.trigger("change");
+                        }
                     }
 
                     if (!this.dsl) {
+                        this.ui.tagLov.val(this.value.tag);
                         if (this.ui.tagLov.data('select2')) {
-                            this.ui.tagLov.val(this.value.tag).trigger('change');
-                        } else {
-                            this.ui.typeLov.val(this.value.tag);
+                            // 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");
+                            }
                         }
                     }
                     this.ui.searchInput.val(this.value.query || "");
@@ -468,7 +481,11 @@
                 this.ui.typeLov.val("").trigger("change");
                 this.ui.tagLov.val("").trigger("change");
                 this.ui.searchInput.val("");
-                this.checkForButtonVisiblity()
+                this.checkForButtonVisiblity();
+                Utils.localStorage.removeValue('tagFilters');
+                Utils.localStorage.removeValue('entityFilters');
+                this.filterObj.tagFilters = null;
+                this.filterObj.entityFilters = null;
                 Utils.setUrl({
                     url: '#!/search/searchResult',
                     urlParams: {
@@ -481,4 +498,4 @@
             }
         });
     return SearchLayoutView;
-});
+});
\ No newline at end of file
diff --git a/dashboardv2/public/js/views/tag/TagLayoutView.js b/dashboardv2/public/js/views/tag/TagLayoutView.js
index b8a1cd1..2714770 100644
--- a/dashboardv2/public/js/views/tag/TagLayoutView.js
+++ b/dashboardv2/public/js/views/tag/TagLayoutView.js
@@ -59,7 +59,7 @@
              * @constructs
              */
             initialize: function(options) {
-                _.extend(this, _.pick(options, 'tag', 'collection', 'typeHeaders'));
+                _.extend(this, _.pick(options, 'tag', 'collection', 'typeHeaders', 'filterObj', 'value'));
             },
             bindEvents: function() {
                 var that = this;
@@ -417,10 +417,19 @@
                         }
                         that.collection.remove(deleteTagData);
                         // to update tag list of search tab fetch typeHeaders.
+                        var tagList = JSON.parse(Utils.localStorage.getValue('tagFilters'));
+                        if (tagList) {
+                            delete tagList[that.tagName];
+                        }
+                        tagList = _.isEmpty(tagList) ? null : tagList;
+                        if (that.filterObj['tagFilters'] && that.filterObj['tagFilters'][that.tagName]) {
+                            delete that.filterObj['tagFilters'][that.tagName];
+                        }
+                        Utils.localStorage.setValue('tagFilters', JSON.stringify(tagList));
                         that.typeHeaders.fetch({ reset: true });
                     }
                 });
             }
         });
     return TagLayoutView;
-});
+});
\ No newline at end of file