GlobalSearchLayoutView.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. define([ "require", "backbone", "hbs!tmpl/search/GlobalSearchLayoutView_tmpl", "utils/Utils", "utils/UrlLinks", "utils/Globals", "jquery-ui" ], function(require, Backbone, GlobalSearchLayoutViewTmpl, Utils, UrlLinks, Globals) {
  2. "use strict";
  3. var GlobalSearchLayoutView = Backbone.Marionette.LayoutView.extend({
  4. _viewName: "GlobalSearchLayoutView",
  5. template: GlobalSearchLayoutViewTmpl,
  6. regions: {
  7. RSearchLayoutView: "#r_searchLayout"
  8. },
  9. ui: {
  10. globalSearch: "[data-id='globalSearch']",
  11. clearGlobalSearch: "[data-id='clearGlobalSearch']",
  12. detailSearch: "[data-id='detailSearch']",
  13. searchLayoutView: ".searchLayoutView"
  14. },
  15. events: function() {
  16. var events = {};
  17. return events["click " + this.ui.clearGlobalSearch] = function() {
  18. this.ui.globalSearch.val(""), this.ui.globalSearch.atlasAutoComplete("search"),
  19. this.ui.clearGlobalSearch.removeClass("in");
  20. }, events["click " + this.ui.detailSearch] = function() {
  21. this.ui.searchLayoutView.toggleClass("open"), this.fromDefaultSearch !== !0 && $("body").toggleClass("global-search-active");
  22. }, events;
  23. },
  24. initialize: function(options) {
  25. _.extend(this, _.pick(options, "value", "closeOnSubmit", "fromDefaultSearch", "initialView", "classificationDefCollection", "entityDefCollection", "typeHeaders", "searchVent", "enumDefCollection", "searchTableColumns")),
  26. this.bindEvents();
  27. },
  28. bindEvents: function() {
  29. var that = this;
  30. $("body").on("click", function(e) {
  31. that.isDestroyed || "detailSearch" === that.$(e.target).data("id") || 0 === $(e.target).parents(".searchLayoutView").length && that.ui.searchLayoutView.hasClass("open") && that.ui.searchLayoutView.removeClass("open");
  32. });
  33. },
  34. onRender: function() {
  35. this.initializeGlobalSearch(), this.renderSearchLayoutView();
  36. },
  37. onBeforeDestroy: function() {
  38. this.ui.searchLayoutView.removeClass("open"), this.ui.globalSearch.atlasAutoComplete("destroy");
  39. },
  40. fetchSearchData: function(options) {
  41. var request = options.request, response = options.response, term = request.term, data = {}, sendResponse = function() {
  42. var query = data.query, suggestions = data.suggestions;
  43. void 0 !== query && void 0 !== suggestions && response(data);
  44. };
  45. $.ajax({
  46. url: UrlLinks.searchApiUrl("quick"),
  47. contentType: "application/json",
  48. data: {
  49. query: term,
  50. limit: 5,
  51. offset: 0
  52. },
  53. cache: !0,
  54. success: function(response) {
  55. var rData = response.searchResults.entities || [];
  56. data.query = {
  57. category: "entities",
  58. data: rData,
  59. order: 1
  60. }, sendResponse();
  61. }
  62. }), $.ajax({
  63. url: UrlLinks.searchApiUrl("suggestions"),
  64. contentType: "application/json",
  65. data: {
  66. prefixString: term
  67. },
  68. cache: !0,
  69. success: function(response) {
  70. var rData = response.suggestions || [];
  71. data.suggestions = {
  72. category: "suggestions",
  73. data: rData,
  74. order: 2
  75. }, sendResponse();
  76. }
  77. });
  78. },
  79. getSearchString: function(str) {
  80. return str && str.length && null === str.match(/[+\-&|!(){}[\]^"~*?:/]/g) ? str + "*" : str;
  81. },
  82. triggerBasicSearch: function(query) {
  83. Utils.setUrl({
  84. url: "#!/search/searchResult?query=" + encodeURIComponent(query) + "&searchType=basic",
  85. mergeBrowserUrl: !1,
  86. trigger: !0,
  87. updateTabState: !0
  88. });
  89. },
  90. renderSearchLayoutView: function() {
  91. var that = this;
  92. require([ "views/search/SearchLayoutView" ], function(SearchLayoutView) {
  93. that.RSearchLayoutView.show(new SearchLayoutView(_.extend({
  94. isHeaderSearch: !0,
  95. onSubmit: function() {
  96. that.ui.searchLayoutView.removeClass("open");
  97. }
  98. }, that.options)));
  99. });
  100. },
  101. initializeSearchValue: function() {
  102. if (this.options.value) {
  103. CommonViewFunction.generateQueryOfFilter(this.options.value);
  104. }
  105. },
  106. closeSearch: function() {
  107. this.ui.globalSearch.atlasAutoComplete("close");
  108. },
  109. initializeGlobalSearch: function() {
  110. var that = this;
  111. this.ui.globalSearch.atlasAutoComplete({
  112. minLength: 1,
  113. autoFocus: !1,
  114. search: function() {
  115. that.ui.searchLayoutView.hasClass("open") || $(this).siblings("span.fa-search").removeClass("fa-search").addClass("fa-refresh fa-spin-custom");
  116. },
  117. focus: function(event, ui) {
  118. return !1;
  119. },
  120. open: function() {
  121. $(this).siblings("span.fa-refresh").removeClass("fa-refresh fa-spin-custom").addClass("fa-search");
  122. },
  123. select: function(event, ui) {
  124. var item = ui && ui.item;
  125. event.preventDefault(), event.stopPropagation();
  126. var $el = $(this);
  127. return _.isString(item) ? ($el.val(item), $el.data("valSelected", !0), that.triggerBasicSearch(item)) : _.isObject(item) && item.guid && Utils.setUrl({
  128. url: "#!/detailPage/" + item.guid,
  129. mergeBrowserUrl: !1,
  130. trigger: !0
  131. }), $el.blur(), !0;
  132. },
  133. source: function(request, response) {
  134. that.ui.searchLayoutView.hasClass("open") || that.fetchSearchData({
  135. request: request,
  136. response: response
  137. });
  138. }
  139. }).focus(function() {
  140. that.ui.searchLayoutView.hasClass("open") || $(this).atlasAutoComplete("search");
  141. }).keyup(function(event) {
  142. "" === $(this).val().trim() ? 13 == event.keyCode ? (this.value = "*", that.triggerBasicSearch("*")) : that.ui.clearGlobalSearch.removeClass("in") : (that.ui.clearGlobalSearch.addClass("in"),
  143. 13 == event.keyCode && ($(this).data("valSelected") !== !0 ? (that.closeSearch(),
  144. that.triggerBasicSearch($(this).val())) : $(this).data("valSelected", !1)));
  145. }).atlasAutoComplete("instance")._renderItem = function(ul, searchItem) {
  146. if (searchItem) {
  147. var data = searchItem.data, searchTerm = this.term, getHighlightedTerm = function(resultStr) {
  148. try {
  149. return resultStr.replace(new RegExp(searchTerm, "gi"), function(foundStr) {
  150. return "<span class='searched-term'>" + foundStr + "</span>";
  151. });
  152. } catch (error) {
  153. return resultStr;
  154. }
  155. };
  156. if (data) {
  157. if (0 == data.length) return $("<li class='empty'></li>").append("<span class='empty-message'>No " + searchItem.category + " found</span>").appendTo(ul);
  158. var items = [];
  159. return _.each(data, function(item) {
  160. var li = null;
  161. if (_.isObject(item)) {
  162. item.itemText = Utils.getName(item) + " (" + item.typeName + ")";
  163. var options = {};
  164. if (void 0 === item.serviceType) {
  165. if (void 0 === Globals.serviceTypeMap[item.typeName] && that.entityDefCollection) {
  166. var defObj = that.entityDefCollection.fullCollection.find({
  167. name: item.typeName
  168. });
  169. defObj && (Globals.serviceTypeMap[item.typeName] = defObj.get("serviceType"));
  170. }
  171. } else void 0 === Globals.serviceTypeMap[item.typeName] && (Globals.serviceTypeMap[item.typeName] = item.serviceType);
  172. item.serviceType = Globals.serviceTypeMap[item.typeName], options.entityData = item;
  173. var img = $('<img src="' + Utils.getEntityIconPath(options) + '">').on("error", function(error, s) {
  174. this.src = Utils.getEntityIconPath(_.extend(options, {
  175. errorUrl: this.src
  176. }));
  177. }), span = $("<span>" + getHighlightedTerm(item.itemText) + "</span>").prepend(img);
  178. li = $("<li class='with-icon'>").append(span);
  179. } else li = $("<li>").append("<span>" + getHighlightedTerm(_.escape(item)) + "</span>");
  180. li.data("ui-autocomplete-item", item), searchItem.category && items.push(li.attr("aria-label", searchItem.category + " : " + (_.isObject(item) ? item.itemText : item)));
  181. }), ul.append(items);
  182. }
  183. }
  184. };
  185. }
  186. });
  187. return GlobalSearchLayoutView;
  188. });