define([ "require", "backbone", "hbs!tmpl/search/SearchLayoutView_tmpl", "utils/Utils", "utils/UrlLinks", "utils/Globals", "utils/Enums", "collection/VSearchList", "utils/CommonViewFunction", "modules/Modal" ], function(require, Backbone, SearchLayoutViewTmpl, Utils, UrlLinks, Globals, Enums, VSearchList, CommonViewFunction, Modal) { "use strict"; var SearchLayoutView = Backbone.Marionette.LayoutView.extend({ _viewName: "SearchLayoutView", template: SearchLayoutViewTmpl, regions: { RSaveSearchBasic: "[data-id='r_saveSearchBasic']", RSaveSearchAdvance: "[data-id='r_saveSearchAdvance']" }, ui: { searchInput: '[data-id="searchInput"]', searchType: 'input[name="queryType"]', searchBtn: '[data-id="searchBtn"]', clearSearch: '[data-id="clearSearch"]', typeLov: '[data-id="typeLOV"]', tagLov: '[data-id="tagLOV"]', termLov: '[data-id="termLOV"]', refreshBtn: '[data-id="refreshBtn"]', advancedInfoBtn: '[data-id="advancedInfo"]', typeAttrFilter: '[data-id="typeAttrFilter"]', tagAttrFilter: '[data-id="tagAttrFilter"]' }, events: function() { var events = {}, that = this; return events["keyup " + this.ui.searchInput] = function(e) { var code = e.which; this.value.query = e.currentTarget.value, this.query[this.type].query = this.value.query, 13 == code && that.findSearchResult(), this.checkForButtonVisiblity(); }, events["change " + this.ui.searchType] = "dslFulltextToggle", events["click " + this.ui.searchBtn] = "findSearchResult", events["click " + this.ui.clearSearch] = "clearSearchData", events["change " + this.ui.typeLov] = "checkForButtonVisiblity", events["change " + this.ui.tagLov] = "checkForButtonVisiblity", events["change " + this.ui.termLov] = "checkForButtonVisiblity", events["click " + this.ui.refreshBtn] = "onRefreshButton", events["click " + this.ui.advancedInfoBtn] = "advancedInfo", events["click " + this.ui.typeAttrFilter] = function() { this.openAttrFilter("type"); }, events["click " + this.ui.tagAttrFilter] = function() { this.openAttrFilter("tag"); }, events; }, initialize: function(options) { _.extend(this, _.pick(options, "value", "typeHeaders", "searchVent", "entityDefCollection", "enumDefCollection", "classificationDefCollection", "businessMetadataDefCollection", "searchTableColumns", "searchTableFilters", "metricCollection", "classificationAndMetricEvent")), this.type = "basic", this.entityCountObj = _.first(this.metricCollection.toJSON()) || { entity: { entityActive: {}, entityDeleted: {} }, tag: { tagEntities: {} } }, this.selectedFilter = { basic: [], dsl: [] }; var param = Utils.getUrlState.getQueryParams(); this.query = { dsl: { query: this.value ? this.value.query : null, type: this.value ? this.value.type : null, pageOffset: this.value ? this.value.pageOffset : null, pageLimit: this.value ? this.value.pageLimit : null }, basic: { query: this.value ? this.value.query : null, type: this.value ? this.value.type : null, tag: this.value ? this.value.tag : null, term: this.value ? this.value.term : null, attributes: this.value ? this.value.attributes : null, tagFilters: this.value ? this.value.tagFilters : null, pageOffset: this.value ? this.value.pageOffset : null, pageLimit: this.value ? this.value.pageLimit : null, entityFilters: this.value ? this.value.entityFilters : null, includeDE: this.value ? this.value.includeDE : null, excludeST: this.value ? this.value.excludeST : null, excludeSC: this.value ? this.value.excludeSC : null } }, this.value || (this.value = {}), this.dsl = !1, param && param.searchType && (this.type = param.searchType, this.updateQueryObject(param)), this.value && this.value.type || this.value && this.value.tag && "basic" === this.value.searchType ? this.setInitialEntityVal = !1 : this.setInitialEntityVal = !0, this.tagEntityCheck = !1, this.typeEntityCheck = !1, this.bindEvents(); }, renderSaveSearch: function() { var that = this; require([ "views/search/save/SaveSearchView" ], function(SaveSearchView) { function fetchSaveSearchCollection() { saveSearchCollection.fetch({ success: function(collection, data) { saveSearchAdvanceCollection.fullCollection.reset(_.where(data, { searchType: "ADVANCED" })), saveSearchBaiscCollection.fullCollection.reset(_.where(data, { searchType: "BASIC" })); }, silent: !0 }); } var saveSearchBaiscCollection = new VSearchList(), saveSearchAdvanceCollection = new VSearchList(), saveSearchCollection = new VSearchList(); saveSearchCollection.url = UrlLinks.saveSearchApiUrl(), saveSearchBaiscCollection.fullCollection.comparator = function(model) { return model.get("name").toLowerCase(); }, saveSearchAdvanceCollection.fullCollection.comparator = function(model) { return model.get("name").toLowerCase(); }; var obj = { value: that.value, searchVent: that.searchVent, typeHeaders: that.typeHeaders, fetchCollection: fetchSaveSearchCollection, classificationDefCollection: that.classificationDefCollection, entityDefCollection: that.entityDefCollection, getValue: function() { var queryObj = that.query[that.type], entityObj = that.searchTableFilters.entityFilters, tagObj = that.searchTableFilters.tagFilters, urlObj = Utils.getUrlState.getQueryParams(); return urlObj && (urlObj.includeDE = "true" == urlObj.includeDE, urlObj.excludeSC = "true" == urlObj.excludeSC, urlObj.excludeST = "true" == urlObj.excludeST), _.extend({}, queryObj, urlObj, { entityFilters: entityObj ? entityObj[queryObj.type] : null, tagFilters: tagObj ? tagObj[queryObj.tag] : null, type: queryObj.type, query: queryObj.query, term: queryObj.term, tag: queryObj.tag }); }, applyValue: function(model, searchType) { that.manualRender(_.extend(searchType, CommonViewFunction.generateUrlFromSaveSearchObject({ value: { searchParameters: model.get("searchParameters"), uiParameters: model.get("uiParameters") }, classificationDefCollection: that.classificationDefCollection, entityDefCollection: that.entityDefCollection }))); } }; that.RSaveSearchBasic.show(new SaveSearchView(_.extend(obj, { isBasic: !0, collection: saveSearchBaiscCollection.fullCollection }))), that.RSaveSearchAdvance.show(new SaveSearchView(_.extend(obj, { isBasic: !1, collection: saveSearchAdvanceCollection.fullCollection }))), fetchSaveSearchCollection(); }); }, bindEvents: function(param) { var that = this; this.listenTo(this.typeHeaders, "reset", function(value) { this.initializeValues(); }, this), this.listenTo(this.searchVent, "entityList:refresh", function(model, response) { this.onRefreshButton(); }, this), this.classificationAndMetricEvent.on("classification:Update:Search", function(options) { that.entityCountObj = _.first(that.metricCollection.toJSON()), that.value = Utils.getUrlState.getQueryParams() || {}, that.value.type || (that.setInitialEntityVal = !0), that.initializeValues(); }); }, initializeValues: function() { this.renderTypeTagList(), this.renderTermList(), this.setValues(), this.checkForButtonVisiblity(), this.renderSaveSearch(), this.setInitialEntityVal && (this.setInitialEntityVal = !1); }, makeFilterButtonActive: function(filtertypeParam) { var filtertype = [ "entityFilters", "tagFilters" ], that = this; filtertypeParam && (_.isArray(filtertypeParam) ? filtertype = filtertypeParam : _.isString(filtertypeParam) && (filtertype = [ filtertypeParam ])); var typeCheck = function(filterObj, type) { that.value.type ? (filterObj && filterObj.length ? that.ui.typeAttrFilter.addClass("active") : that.ui.typeAttrFilter.removeClass("active"), that.ui.typeAttrFilter.prop("disabled", !1)) : (that.ui.typeAttrFilter.removeClass("active"), that.ui.typeAttrFilter.prop("disabled", !0)); }, tagCheck = function(filterObj, type) { var filterAddOn = Enums.addOnClassification.filter(function(a) { a !== Enums.addOnClassification[0]; }); that.value.tag && !_.contains(filterAddOn, that.value.tag) ? (that.ui.tagAttrFilter.prop("disabled", !1), filterObj && filterObj.length ? that.ui.tagAttrFilter.addClass("active") : that.ui.tagAttrFilter.removeClass("active")) : (that.ui.tagAttrFilter.removeClass("active"), that.ui.tagAttrFilter.prop("disabled", !0)); }; _.each(filtertype, function(type) { var filterObj = that.searchTableFilters[type]; "entityFilters" == type && typeCheck(filterObj[that.value.type], type), "tagFilters" == type && tagCheck(filterObj[that.value.tag], type); }); }, checkForButtonVisiblity: function(e, options) { var that = this, isBasicSearch = "basic" == this.type; if (e && e.currentTarget) { var $el = $(e.currentTarget), isTagEl = "tagLOV" == $el.data("id"), isTermEl = "termLOV" == $el.data("id"), isTypeEl = "typeLOV" == $el.data("id"), select2Data = $el.select2("data"); if ("change" == e.type && select2Data) { var value = _.isEmpty(select2Data) ? select2Data : _.first(select2Data).id, key = "tag", filterType = isBasicSearch ? "tagFilters" : null, value = value && value.length ? value : null; if (isTagEl || (key = isTermEl ? "term" : "type", isBasicSearch && (filterType = isTypeEl ? "entityFilters" : null)), this.value) { if (this.value[key] !== value || !value && !this.value[key]) { var temp = {}; temp[key] = value, _.extend(this.value, temp), _.isUndefined(options) && (this.value.pageOffset = 0), _.extend(this.query[this.type], temp); } else if (isBasicSearch) { if (filterType) { var filterObj = this.searchTableFilters[filterType]; filterObj && this.value[key] && (this.searchTableFilters[filterType][this.value[key]] = this.value[filterType] ? this.value[filterType] : null); } if (this.value.type) if (this.value.attributes) { var attributes = _.sortBy(this.value.attributes.split(",")), tableColumn = this.searchTableColumns[this.value.type]; _.isEmpty(this.searchTableColumns) || !tableColumn ? this.searchTableColumns[this.value.type] = attributes : tableColumn.join(",") !== attributes.join(",") && (this.searchTableColumns[this.value.type] = attributes); } else this.searchTableColumns[this.value.type] && (this.searchTableColumns[this.value.type] = void 0); } isBasicSearch && filterType && this.makeFilterButtonActive(filterType); } else isBasicSearch && (this.ui.tagAttrFilter.prop("disabled", !0), this.ui.typeAttrFilter.prop("disabled", !0)); } } var value = this.ui.searchInput.val() || _.first($(this.ui.typeLov).select2("data")).id; if (!this.dsl && _.isEmpty(value)) { var termData = _.first($(this.ui.termLov).select2("data")); value = _.first($(this.ui.tagLov).select2("data")).id || (termData ? termData.id : ""); } value && value.length ? (this.ui.searchBtn.removeAttr("disabled"), setTimeout(function() { that.ui.searchInput.focus(); }, 0)) : this.ui.searchBtn.attr("disabled", "true"); }, onRender: function() { this.initializeValues(); }, updateQueryObject: function(param) { param && param.searchType && (this.type = param.searchType), _.extend(this.query[this.type], "dsl" == this.type ? { query: null, type: null, pageOffset: null, pageLimit: null } : { query: null, type: null, tag: null, term: null, attributes: null, tagFilters: null, pageOffset: null, pageLimit: null, entityFilters: null, includeDE: null }, param); }, onRefreshButton: function() { var that = this, apiCount = 2, updateSearchList = function() { if (0 === apiCount) { that.initializeValues(); var checkURLValue = Utils.getUrlState.getQueryParams(that.url); that.searchVent && (_.has(checkURLValue, "tag") || _.has(checkURLValue, "type") || _.has(checkURLValue, "query")) && that.searchVent.trigger("search:refresh"); } }; this.metricCollection.fetch({ complete: function() { --apiCount, that.entityCountObj = _.first(that.metricCollection.toJSON()), updateSearchList(); } }), this.typeHeaders.fetch({ silent: !0, complete: function() { --apiCount, updateSearchList(); } }); }, advancedInfo: function(e) { require([ "views/search/AdvancedSearchInfoView", "modules/Modal" ], function(AdvancedSearchInfoView, Modal) { var view = new AdvancedSearchInfoView(), modal = new Modal({ title: "高级搜索查询", content: view, okCloses: !0, showFooter: !0, allowCancel: !1 }).open(); view.on("closeModal", function() { modal.trigger("cancel"); }); }); }, openAttrFilter: function(filterType) { var that = this; require([ "views/search/SearchQueryView" ], function(SearchQueryView) { that.attrModal = new SearchQueryView({ value: that.value, tag: "tag" === filterType, type: "type" === filterType, searchVent: that.searchVent, typeHeaders: that.typeHeaders, entityDefCollection: that.entityDefCollection, enumDefCollection: that.enumDefCollection, classificationDefCollection: that.classificationDefCollection, businessMetadataDefCollection: that.businessMetadataDefCollection, searchTableFilters: that.searchTableFilters }), that.attrModal.on("ok", function(scope, e) { that.okAttrFilterButton(e); }); }); }, okAttrFilterButton: function(e) { function getIdFromRuleObject(rule) { return _.map(rule.rules, function(obj, key) { return _.has(obj, "condition") ? getIdFromRuleObject(obj) : col.push(obj.id); }), col; } var that = this, isTag = !!this.attrModal.tag, filtertype = isTag ? "tagFilters" : "entityFilters", queryBuilderRef = this.attrModal.RQueryBuilder.currentView.ui.builder, col = []; if (queryBuilderRef.data("queryBuilder")) var rule = queryBuilderRef.queryBuilder("getRules"); if (rule) { var ruleUrl = CommonViewFunction.attributeFilter.generateUrl({ value: rule, formatedDateToLong: !0 }); if (this.searchTableFilters[filtertype][isTag ? this.value.tag : this.value.type] = ruleUrl, this.makeFilterButtonActive(filtertype), !isTag && this.value && this.value.type && this.searchTableColumns && (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], getIdFromRuleObject(rule)))), rule.rules) { if (!isTag && !that.tagEntityCheck) { var state = _.find(rule.rules, { id: "__state" }); state ? (that.typeEntityCheck = !("ACTIVE" === state.value && "=" === state.operator || "DELETED" === state.value && "!=" === state.operator), that.value.includeDE = that.typeEntityCheck) : that.typeEntityCheck && (that.typeEntityCheck = !1, that.tagEntityCheck || (that.value.includeDE = !1)); } if (isTag && !that.typeEntityCheck) { var entityStatus = _.find(rule.rules, { id: "__entityStatus" }); entityStatus ? (that.tagEntityCheck = !("ACTIVE" === entityStatus.value && "=" === entityStatus.operator || "DELETED" === entityStatus.value && "!=" === entityStatus.operator), that.value.includeDE = that.tagEntityCheck) : that.tagEntityCheck && (that.tagEntityCheck = !1, that.typeEntityCheck || (that.value.includeDE = !1)); } } this.attrModal.modal.close(), $(e.currentTarget).hasClass("search") && this.findSearchResult(); } }, manualRender: function(paramObj) { paramObj && (this.value = paramObj), this.updateQueryObject(paramObj), this.renderTypeTagList(), this.setValues(paramObj); }, getFilterBox: function() { var serviceStr = "", serviceArr = [], that = this; this.typeHeaders.fullCollection.each(function(model) { var serviceType = model.toJSON().serviceType; serviceType && serviceArr.push(serviceType); }), _.each(_.uniq(serviceArr), function(service) { serviceStr += '