Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

418 wiersze
12 KiB

  1. import $ from 'jquery';
  2. require('jquery.easing');
  3. import {StudioPreviewUtil} from '../../_global/scripts/utils/StudioPreviewUtil';
  4. class IHKHeader {
  5. constructor(header) {
  6. this.header = header.addClass('initiated');
  7. this.window = $(window);
  8. this.body = $('body');
  9. this.overlayOpen = false;
  10. this.showOpenedNav = this.header.find('.primary').data("show-opened-menu");
  11. const t = this;
  12. this.initLogo();
  13. this.initTopLink();
  14. this.initScroll();
  15. this.initSearch();
  16. this.initPrimaryNav();
  17. this.initSecondaryNav();
  18. this.initToggling();
  19. this.initLanguage();
  20. if (this.showOpenedNav) {
  21. t.header.find('.toggle-nav').click();
  22. }
  23. this.body.on('open-navigation', function (event, data) {
  24. if (data.openMenu) {
  25. t.header.find('.toggle-nav').click();
  26. }
  27. t.initPrimaryNav(data.rootUrl);
  28. });
  29. }
  30. initLogo() {
  31. const t = this;
  32. const logo = t.header.find('.logo');
  33. const img = logo.find('img');
  34. const overlay = $('<span class="logo-overlay" />').appendTo(logo);
  35. const logoObserver = new ResizeObserver(() => {
  36. if (img.height() < 44) {
  37. overlay.css('left', img.height() * 2.1);
  38. } else {
  39. overlay.removeAttr('style');
  40. }
  41. })
  42. logoObserver.observe(img.get(0));
  43. };
  44. initTopLink() {
  45. const t = this;
  46. const toplink = $('<a href="#" class="toplink" tabindex="-1" />').text('Top').prependTo(t.header);
  47. toplink.on('click', function (e) {
  48. e.preventDefault();
  49. $('html, body').animate({'scrollTop': 0}, Math.round($(window).scrollTop() / 12 + 300), 'easeOutQuad');
  50. })
  51. }
  52. initSearch() {
  53. const t = this;
  54. const formNav = $('<div class="form-nav" />').appendTo(t.header.find('.search form'));
  55. t.header.find('nav .secondary').clone().appendTo(formNav);
  56. t.header.find('nav .meta').clone().appendTo(formNav);
  57. t.header.find('.open-search, .close-search').on('click', function (e) {
  58. t.header.toggleClass('search-open').removeClass('nav-open');
  59. if (t.header.hasClass('search-open')) {
  60. setTimeout(function () {
  61. t.header.find('.search-field').focus();
  62. }, 200)
  63. }
  64. t.toggleContentScroll();
  65. })
  66. };
  67. initScroll() {
  68. const t = this;
  69. window.addEventListener("scroll", function () {
  70. window.requestAnimationFrame(function () {
  71. t.checkScroll();
  72. })
  73. }, {passive: true});
  74. };
  75. checkScroll() {
  76. const t = this;
  77. const st = this.window.scrollTop();
  78. if (t.overlayOpen || $('.gallery-popup.open').length) {
  79. return false;
  80. }
  81. if (st > 59) {
  82. this.header.addClass('scrolled');
  83. this.body.addClass('header-scrolled');
  84. } else {
  85. this.header.removeClass('scrolled');
  86. this.body.removeClass('header-scrolled');
  87. }
  88. if (st > this.window.height() * 1.2) {
  89. this.header.addClass('show-toplink');
  90. } else {
  91. this.header.removeClass('show-toplink');
  92. }
  93. }
  94. initToggling() {
  95. this.header.find('.overlay-holder').on('click touch', this.toggleNavigation.bind(this));
  96. this.header.find('.toggle-nav').on('click touch', this.toggleNavigation.bind(this));
  97. };
  98. toggleNavigation() {
  99. const t = this;
  100. if (StudioPreviewUtil && StudioPreviewUtil.isStudioPreview()) {
  101. $("html").stop().animate({'scrollTop': 0}, 300, 'swing', function () {
  102. t.header.toggleClass('nav-open');
  103. t.toggleContentScroll();
  104. });
  105. } else {
  106. t.header.toggleClass('nav-open');
  107. t.toggleContentScroll();
  108. }
  109. };
  110. initPrimaryNav(optionalRootUrl) {
  111. const t = this;
  112. const primary = t.header.find('.primary');
  113. $(primary).empty();
  114. t.baseUrl = primary.attr('data-base-url');
  115. t.rootUrl = primary.attr('data-root-url');
  116. if (optionalRootUrl) {
  117. t.rootUrl = optionalRootUrl;
  118. primary.off('click', 'a');
  119. }
  120. primary.append($('<ul class="current" />').attr('data-json', t.baseUrl + t.rootUrl));
  121. primary.on('click', 'a', function (e) {
  122. const a = $(this);
  123. const li = a.parent();
  124. const ul = li.parent('ul');
  125. if (li.hasClass('deep')) {
  126. e.preventDefault();
  127. if (a.siblings('ul').length) {
  128. ul.removeClass('current');
  129. t.toTop(ul);
  130. a.siblings('ul').addClass('current');
  131. li.addClass('open');
  132. t.manageTabIndex();
  133. } else {
  134. const deepUl = $('<ul />').attr('data-json', a.attr('href')).appendTo(li);
  135. t.loadNav(deepUl);
  136. }
  137. } else if (li.hasClass('back')) {
  138. e.preventDefault();
  139. t.toTop(ul);
  140. if (ul.closest('li').length) {
  141. ul.removeClass('current')
  142. .closest('li').removeClass('open')
  143. .parent('ul').addClass('current');
  144. t.manageTabIndex();
  145. } else {
  146. const backUl = $('<ul />').attr('data-json', a.attr('href')).insertBefore(ul);
  147. t.loadNav(backUl);
  148. }
  149. }
  150. });
  151. t.loadNav(t.header.find('.primary ul'));
  152. }
  153. openMenuByUrlParam(anchor, t) {
  154. const li = anchor.parent();
  155. const ul = li.parent('ul');
  156. if (li.hasClass('deep')) {
  157. if (anchor.siblings('ul').length) {
  158. ul.removeClass('current');
  159. t.toTop(ul);
  160. anchor.siblings('ul').addClass('current');
  161. li.addClass('open');
  162. t.manageTabIndex();
  163. } else {
  164. const deepUl = $('<ul />').attr('data-json', anchor.attr('href')).appendTo(li);
  165. t.loadNav(deepUl);
  166. }
  167. }
  168. }
  169. initSecondaryNav() {
  170. // secondary-nav-item
  171. const t = this;
  172. const $secondary = t.header.find("nav").find(".secondary");
  173. const $secondaryItems = $secondary.find(".secondary-nav-item");
  174. $.each($secondaryItems, function (index, element) {
  175. $(element).on("click", function (e) {
  176. if ($(element).data("is-channel")) {
  177. e.preventDefault();
  178. const elementRootUrl = $(this).data("root-url");
  179. t.initPrimaryNav(elementRootUrl);
  180. } else {
  181. return true;
  182. }
  183. });
  184. });
  185. }
  186. loadNav(ul) {
  187. const t = this;
  188. const currentPageId = t.header.find('.primary').data("page-content-id");
  189. if (ul.parent('li').length) {
  190. ul.addClass('current');
  191. setTimeout(function () {
  192. const parentUl = ul.addClass('loading').parent('li').addClass('open')
  193. .closest('.current').removeClass('current');
  194. t.toTop(parentUl);
  195. }, 20)
  196. }
  197. $.ajax({
  198. url: ul.attr('data-json'),
  199. type: "GET",
  200. dataType: "json",
  201. xhrFields: {withCredentials: true}
  202. }).done(function (data) {
  203. ul.attr('data-cm-metadata', '[{"_":{"$Ref":"content/' + data.tocListId + '"}}]');
  204. if (data.parentId) {
  205. $('<li class="back" />').appendTo(ul)
  206. .append($('<a />').attr('href', t.baseUrl + data.parentId).text(data.title));
  207. }
  208. if (!data.hideOverview) {
  209. if (data.showOverview) {
  210. const overviewElement = $('<li class="overview" />');
  211. overviewElement.appendTo(ul)
  212. .append($('<a />').attr('href', data.url).text(window.ihk.translations.overview));
  213. if (data.contentId === currentPageId.toString()) {
  214. overviewElement.find("a").addClass("active");
  215. }
  216. }
  217. }
  218. const parentElement = data;
  219. $.each(data.items, function () {
  220. const itemsBaseUrl = ihk.settings.navigationItemsUrl;
  221. let itemUrl = this.url;
  222. if (itemsBaseUrl) {
  223. itemUrl = itemsBaseUrl + itemUrl;
  224. }
  225. let li = $('<li />').attr('data-id', this.contentId).attr('data-cm-metadata', '[{"_":{"$Ref":"content/' + this.contentId + '"}}]').appendTo(ul),
  226. a = $('<a />').html(this.title).appendTo(li);
  227. switch (this.type) {
  228. case "CMChannel" :
  229. li.addClass('deep');
  230. a.attr('href', t.baseUrl + this.contentId);
  231. break;
  232. case "CMArticle" :
  233. li.addClass('link');
  234. a.attr('href', itemUrl);
  235. break;
  236. default :
  237. li.addClass('miscellaneous');
  238. li.attr('data-type', this.type);
  239. a.attr('href', itemUrl);
  240. }
  241. if (this.linktype == 'external') {
  242. li.addClass('external');
  243. $(li).find("a").attr("target", "_blank")
  244. }
  245. if (this.viewType === 'onlinemagazinstart') {
  246. // todo als link
  247. li.removeClass('deep');
  248. li.addClass('magazine-nav');
  249. $(li).find("a").attr("href", itemUrl);
  250. if (this.titleImage) {
  251. a.text('').append($('<img src="' + this.titleImage + '" alt="' + this.title + '" />'))
  252. }
  253. }
  254. if (this.viewType === 'themenseite' && !parentElement.root) {
  255. li.addClass('overview');
  256. li.removeClass('deep');
  257. a.attr('href', itemUrl);
  258. }
  259. if (this.trackCode && this.trackCode.length > 0) {
  260. $(li).find("a").attr("onmousedown", this.trackCode);
  261. }
  262. if (this.restrictedTo && this.restrictedTo.indexOf('extranet') > -1) {
  263. li.addClass('extranet');
  264. } else if (this.restrictedTo && this.restrictedTo.indexOf('intranet') > -1) {
  265. li.addClass('intranet');
  266. }
  267. if (this.doctype &&
  268. (this.doctype.indexOf('Datei') > -1 || this.doctype.indexOf('PDF') > -1 || this.doctype.indexOf('PIC') > -1)) {
  269. li.addClass('download');
  270. }
  271. if (this.contentId === currentPageId.toString()) {
  272. $(li).find("a").addClass("active");
  273. }
  274. });
  275. ul.addClass('current').attr('data-id', data.contentId);
  276. if (ul.parent('li').length) {
  277. setTimeout(function () {
  278. const parentUl = ul.removeClass('loading').parent('li').addClass('open')
  279. .closest('.current').removeClass('current');
  280. t.toTop(parentUl);
  281. t.manageTabIndex();
  282. }, 20)
  283. } else if (ul.next('ul').length) {
  284. const innerUl = ul.next('ul');
  285. let wrapperLi = ul.find('li[data-id="' + innerUl.attr('data-id') + '"]').addClass('open');
  286. if (!wrapperLi.length) {
  287. wrapperLi = $('<li />').addClass('open').appendTo(ul);
  288. }
  289. wrapperLi.append(innerUl);
  290. setTimeout(function () {
  291. innerUl.removeClass('current');
  292. wrapperLi.removeClass('open');
  293. t.manageTabIndex();
  294. }, 20)
  295. }
  296. let hash = window.location.hash;
  297. if (hash) {
  298. let menuId = hash.substring(1);
  299. let li = ul.find('li[data-id=' + menuId + ']');
  300. if (li.length > 0) {
  301. t.toggleNavigation();
  302. let anchor = li.find('a');
  303. if (anchor.length > 0) {
  304. t.openMenuByUrlParam(anchor, t);
  305. }
  306. }
  307. }
  308. })
  309. }
  310. toggleContentScroll() {
  311. const t = this;
  312. if (t.header.hasClass('nav-open') || t.header.hasClass('search-open')) {
  313. t.overlayOpen = true;
  314. this.body.trigger('overlay-open');
  315. t.body.css('top', (t.window.scrollTop() * -1) + 'px').addClass('nav-open');
  316. if (t.header.hasClass('search-open')) {
  317. t.body.addClass('search-open');
  318. }
  319. } else {
  320. t.overlayOpen = false;
  321. this.body.trigger('overlay-close');
  322. const top = Math.abs(parseInt(t.body.css('top')));
  323. t.body.removeClass('nav-open').removeClass('search-open').removeAttr('style');
  324. t.window.scrollTop(top);
  325. }
  326. }
  327. initLanguage() {
  328. const lang = this.header.find('.language');
  329. lang.find('button').on('click', function (e) {
  330. lang.toggleClass('open');
  331. })
  332. lang.on('click', function (e) {
  333. e.stopPropagation();
  334. })
  335. this.body.on('click', function () {
  336. lang.removeClass('open');
  337. })
  338. lang.siblings().find('a').on('focus', function () {
  339. lang.removeClass('open');
  340. })
  341. }
  342. manageTabIndex() {
  343. this.header.find('.primary a').attr('tabindex', '-1');
  344. this.header.find('.primary .current > li > a').removeAttr('tabindex');
  345. }
  346. toTop(ul) {
  347. if (ul.scrollTop() > 0) {
  348. ul.animate({'scrollTop': 0}, 300, 'swing');
  349. }
  350. }
  351. }
  352. export default IHKHeader;
  353. $('body').on('ihk-init', function () {
  354. $('.page-header:not(.initiated)').each((i, el) => {
  355. new IHKHeader($(el));
  356. });
  357. });