Header.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. define([ "require", "hbs!tmpl/site/Header", "utils/CommonViewFunction", "utils/Globals", "utils/Utils", "utils/UrlLinks", "jquery-ui" ], function(require, tmpl, CommonViewFunction, Globals, Utils, UrlLinks) {
  2. "use strict";
  3. var Header = Marionette.LayoutView.extend({
  4. template: tmpl,
  5. regions: {},
  6. templateHelpers: function() {
  7. return {
  8. glossaryImportTempUrl: UrlLinks.glossaryImportTempUrl(),
  9. businessMetadataImportTempUrl: UrlLinks.businessMetadataImportTempUrl(),
  10. apiDocUrl: UrlLinks.apiDocUrl(),
  11. isDebugMetricsEnabled: Globals.isDebugMetricsEnabled
  12. };
  13. },
  14. ui: {
  15. backButton: "[data-id='backButton']",
  16. menuHamburger: "[data-id='menuHamburger']",
  17. globalSearch: "[data-id='globalSearch']",
  18. clearGlobalSearch: "[data-id='clearGlobalSearch']",
  19. signOut: "[data-id='signOut']",
  20. administrator: "[data-id='administrator']",
  21. showDebug: "[data-id='showDebug']",
  22. uiSwitch: "[data-id='uiSwitch']",
  23. glossaryImport: "[data-id='glossaryImport']",
  24. businessMetadataImport: "[data-id='businessMetadataImport']"
  25. },
  26. events: function() {
  27. var events = {};
  28. return events["click " + this.ui.backButton] = function() {
  29. Utils.backButtonClick();
  30. }, events["click " + this.ui.clearGlobalSearch] = function() {
  31. this.ui.globalSearch.val(""), this.ui.globalSearch.autocomplete("search"), this.ui.clearGlobalSearch.removeClass("in");
  32. }, events["click " + this.ui.menuHamburger] = function() {
  33. this.setSearchBoxWidth({
  34. updateWidth: function(atlasHeaderWidth) {
  35. return $("body").hasClass("full-screen") ? atlasHeaderWidth - 350 : atlasHeaderWidth + 350;
  36. }
  37. }), $("body").toggleClass("full-screen");
  38. }, events["click " + this.ui.signOut] = function() {
  39. Utils.localStorage.setValue("last_ui_load", "v1");
  40. var path = Utils.getBaseUrl(window.location.pathname);
  41. window.location = path + "/logout.html";
  42. }, events["click " + this.ui.uiSwitch] = function() {
  43. var path = Utils.getBaseUrl(window.location.pathname) + "/n/index.html";
  44. window.location.hash.length > 2 && (path += window.location.hash), window.location.href = path;
  45. }, events["click " + this.ui.administrator] = function() {
  46. Utils.setUrl({
  47. url: "#!/administrator",
  48. mergeBrowserUrl: !1,
  49. trigger: !0,
  50. updateTabState: !0
  51. });
  52. }, events["click " + this.ui.glossaryImport] = function() {
  53. this.onClickImport(!0);
  54. }, events["click " + this.ui.businessMetadataImport] = function() {
  55. this.onClickImport();
  56. }, events["click " + this.ui.showDebug] = function() {
  57. Utils.setUrl({
  58. url: "#!/debugMetrics",
  59. mergeBrowserUrl: !1,
  60. trigger: !0,
  61. updateTabState: !0
  62. });
  63. }, events;
  64. },
  65. initialize: function(options) {
  66. this.bindEvent(), this.options = options;
  67. },
  68. setSearchBoxWidth: function(options) {
  69. var atlasHeaderWidth = this.$el.find(".atlas-header").width(), minusWidth = Utils.getUrlState.isDetailPage() || Utils.getUrlState.isBSDetail() ? 360 : 210;
  70. options && options.updateWidth && (atlasHeaderWidth = options.updateWidth(atlasHeaderWidth)),
  71. atlasHeaderWidth > minusWidth && this.$el.find(".global-search-container").width(atlasHeaderWidth - minusWidth);
  72. },
  73. bindEvent: function() {
  74. var that = this;
  75. $(window).resize(function() {
  76. that.setSearchBoxWidth();
  77. });
  78. },
  79. onRender: function() {
  80. var that = this;
  81. Globals.userLogedIn.status && that.$(".userName").html(Globals.userLogedIn.response.userName),
  82. this.initializeGlobalSearch();
  83. },
  84. onShow: function() {
  85. this.setSearchBoxWidth();
  86. },
  87. onBeforeDestroy: function() {
  88. this.ui.globalSearch.atlasAutoComplete("destroy");
  89. },
  90. manualRender: function() {
  91. this.setSearchBoxWidth();
  92. },
  93. fetchSearchData: function(options) {
  94. var request = options.request, response = options.response, inputEl = options.inputEl, term = request.term, data = {}, sendResponse = function() {
  95. var query = data.query, suggestions = data.suggestions;
  96. void 0 !== query && void 0 !== suggestions && (inputEl.siblings("span.fa-refresh").removeClass("fa-refresh fa-spin-custom").addClass("fa-search"),
  97. response(data));
  98. };
  99. $.ajax({
  100. url: UrlLinks.searchApiUrl("quick"),
  101. contentType: "application/json",
  102. data: {
  103. query: term,
  104. limit: 5,
  105. offset: 0
  106. },
  107. cache: !0,
  108. success: function(response) {
  109. var rData = response.searchResults.entities || [];
  110. data.query = {
  111. category: "entities",
  112. data: rData,
  113. order: 1
  114. }, sendResponse();
  115. }
  116. }), $.ajax({
  117. url: UrlLinks.searchApiUrl("suggestions"),
  118. contentType: "application/json",
  119. data: {
  120. prefixString: term
  121. },
  122. cache: !0,
  123. success: function(response) {
  124. var rData = response.suggestions || [];
  125. data.suggestions = {
  126. category: "suggestions",
  127. data: rData,
  128. order: 2
  129. }, sendResponse();
  130. }
  131. });
  132. },
  133. getSearchString: function(str) {
  134. return str && str.length && null === str.match(/[+\-&|!(){}[\]^"~*?:/]/g) ? str + "*" : str;
  135. },
  136. triggerBuasicSearch: function(query) {
  137. Utils.setUrl({
  138. url: "#!/search/searchResult?query=" + encodeURIComponent(query) + "&searchType=basic",
  139. mergeBrowserUrl: !1,
  140. trigger: !0,
  141. updateTabState: !0
  142. });
  143. },
  144. initializeGlobalSearch: function() {
  145. var that = this;
  146. this.ui.globalSearch.atlasAutoComplete({
  147. minLength: 1,
  148. autoFocus: !1,
  149. search: function() {
  150. $(this).siblings("span.fa-search").removeClass("fa-search").addClass("fa-refresh fa-spin-custom");
  151. },
  152. select: function(event, ui) {
  153. var item = ui && ui.item;
  154. event.preventDefault(), event.stopPropagation();
  155. var $el = $(this);
  156. return _.isString(item) ? ($el.val(item), $el.data("valSelected", !0), that.triggerBuasicSearch(item)) : _.isObject(item) && item.guid && Utils.setUrl({
  157. url: "#!/detailPage/" + item.guid,
  158. mergeBrowserUrl: !1,
  159. trigger: !0
  160. }), $el.blur(), !0;
  161. },
  162. source: function(request, response) {
  163. that.fetchSearchData({
  164. request: request,
  165. response: response,
  166. inputEl: this.element
  167. });
  168. }
  169. }).focus(function() {
  170. $(this).atlasAutoComplete("search");
  171. }).keyup(function(event) {
  172. "" === $(this).val().trim() ? that.ui.clearGlobalSearch.removeClass("in") : (that.ui.clearGlobalSearch.addClass("in"),
  173. 13 == event.keyCode && ($(this).data("valSelected") !== !0 ? that.triggerBuasicSearch($(this).val()) : $(this).data("valSelected", !1)));
  174. }).atlasAutoComplete("instance")._renderItem = function(ul, searchItem) {
  175. if (searchItem) {
  176. var data = searchItem.data, searchTerm = this.term, getHighlightedTerm = function(resultStr) {
  177. try {
  178. return resultStr.replace(new RegExp(searchTerm, "gi"), function(foundStr) {
  179. return "<span class='searched-term'>" + foundStr + "</span>";
  180. });
  181. } catch (error) {
  182. return resultStr;
  183. }
  184. };
  185. if (data) {
  186. if (0 == data.length) return $("<li class='empty'></li>").append("<span class='empty-message'>No " + searchItem.category + " found</span>").appendTo(ul);
  187. var items = [];
  188. return _.each(data, function(item) {
  189. var li = null;
  190. if (_.isObject(item)) {
  191. item.itemText = Utils.getName(item) + " (" + item.typeName + ")";
  192. var options = {};
  193. if (void 0 === item.serviceType) {
  194. if (void 0 === Globals.serviceTypeMap[item.typeName] && that.entityDefCollection) {
  195. var defObj = that.entityDefCollection.fullCollection.find({
  196. name: item.typeName
  197. });
  198. defObj && (Globals.serviceTypeMap[item.typeName] = defObj.get("serviceType"));
  199. }
  200. } else void 0 === Globals.serviceTypeMap[item.typeName] && (Globals.serviceTypeMap[item.typeName] = item.serviceType);
  201. item.serviceType = Globals.serviceTypeMap[item.typeName], options.entityData = item;
  202. var img = $('<img src="' + Utils.getEntityIconPath(options) + '">').on("error", function(error, s) {
  203. this.src = Utils.getEntityIconPath(_.extend(options, {
  204. errorUrl: this.src
  205. }));
  206. }), span = $("<span>" + getHighlightedTerm(item.itemText) + "</span>").prepend(img);
  207. li = $("<li class='with-icon'>").append(span);
  208. } else li = $("<li>").append("<span>" + getHighlightedTerm(_.escape(item)) + "</span>");
  209. li.data("ui-autocomplete-item", item), searchItem.category && items.push(li.attr("aria-label", searchItem.category + " : " + (_.isObject(item) ? item.itemText : item)));
  210. }), ul.append(items);
  211. }
  212. }
  213. };
  214. },
  215. onClickImport: function(isGlossary) {
  216. var that = this;
  217. require([ "views/import/ImportLayoutView" ], function(ImportLayoutView) {
  218. new ImportLayoutView({
  219. callback: function() {
  220. that.options.importVent && (isGlossary ? that.options.importVent.trigger("Import:Glossary:Update") : that.options.importVent.trigger("Import:BM:Update"));
  221. },
  222. isGlossary: isGlossary
  223. });
  224. });
  225. }
  226. });
  227. return Header;
  228. });