Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

384 righe
11 KiB

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