25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

316 satır
11 KiB

  1. const $ = require('jquery');
  2. const Bloodhound = require("typeahead.js/dist/bloodhound.js");
  3. const Typeahead = require("typeahead.js/dist/typeahead.jquery.js");
  4. class Finder {
  5. constructor(section) {
  6. const t = this;
  7. this.serviceUrlBase = "/blueprint/servlet/serviceport/ihkfinder";
  8. this.ihklListUrl = 'services/ihkList.json';//this.serviceUrlBase + "/ihk/all";
  9. this.communitiesUrl = 'services/communities.json';//this.serviceUrlBase + "/community/all";
  10. // berechnen d
  11. // center ist geolocation aus js api
  12. // item die IHK
  13. /*
  14. const p = 0.017453292519943295;
  15. const c = Math.cos;
  16. const a = 0.5 - c((item.lat - center.lat) * p) / 2 + c(center.lat * p) * c(item.lat * p) * (1 - c((item.lng - center.lng) * p)) / 2;
  17. const distance = 12742 * Math.asin(Math.sqrt(a));
  18. */
  19. $(section).addClass("initialized");
  20. t.section = section;
  21. t.map = section.find('.map');
  22. t.form = section.find('.ihk-finder');
  23. t.charMap = {
  24. 'a': /[àáâ]/gi,
  25. 'c': /[ç]/gi,
  26. 'e': /[èéêë]/gi,
  27. 'i': /[ï]/gi,
  28. 'o': /[ô]/gi,
  29. 'oe': /[œ]/gi,
  30. 'u': /[ü]/gi,
  31. 's': /[ß]/gi
  32. };
  33. t.loadLocations();
  34. }
  35. loadLocations() {
  36. const t = this;
  37. $('.page-header .toggle-ihk-list').on('click', function (e) {
  38. e.preventDefault();
  39. $('.page-header').toggleClass('nav-open');
  40. });
  41. t.mapBox = {
  42. top: 55.1,
  43. right: 15.2,
  44. bottom: 47.1,
  45. left: 5.8
  46. };
  47. t.indexLetter = "";
  48. t.ihkIndex = $('<ul class="ihk-index" />').appendTo($('.page-header .ihk-list'));
  49. $.getJSON(this.ihklListUrl, function (response) {
  50. if (response.jsonResponse && response.status === 200) {
  51. t.ihkList = response.jsonResponse.sort((a, b) => {
  52. const countryNameA = a.country.toUpperCase(); // ignore upper and lowercase
  53. const countryNameB = b.country.toUpperCase(); // ignore upper and lowercase
  54. // Sort by country
  55. if (countryNameA < countryNameB) {
  56. return -1;
  57. }
  58. if (countryNameA > countryNameB) {
  59. return 1;
  60. }
  61. const ihkNameA = a.name.toUpperCase(); // ignore upper and lowercase
  62. const ihkNameB = b.name.toUpperCase(); // ignore upper and lowercase
  63. // Sort by name
  64. if (ihkNameA < ihkNameB) {
  65. return -1;
  66. }
  67. if (ihkNameA > ihkNameB) {
  68. return 1;
  69. }
  70. // names must be equal
  71. return 0;
  72. });
  73. $.each(response.jsonResponse, function () {
  74. t.setMarker(this);
  75. t.buildListItem(this);
  76. });
  77. t.initForm();
  78. }
  79. })
  80. }
  81. /*
  82. buildListItem(json) {
  83. const t = this;
  84. const li = $('<li />').appendTo(t.ihkIndex);
  85. //todo urls
  86. // $('<a />').attr('data-ihknr', json.ihknr).text(json.name).attr('href', json.link[0].url).appendTo(li);
  87. $('<a />').attr('data-ihknr', json.ihknr).text(json.name).appendTo(li);
  88. if (t.indexLetter != json.country) {
  89. t.indexLetter = json.country;
  90. li.prepend($('<span class="index-letter" />').text(t.indexLetter));
  91. }
  92. }
  93. */
  94. buildListItem(json) {
  95. const t = this;
  96. //todo urls, API muss noch erweitert
  97. // $('<a />').attr('data-ihknr', json.ihknr).text(json.name).attr('href', json.link[0].url).appendTo(li);
  98. // generiere parent Element, Bundesland
  99. if (t.indexLetter !== json.country) {
  100. const parentLi = $('<li />');
  101. const itemLi = $('<li />');
  102. const ul =$('<ul />').appendTo(parentLi);
  103. t.indexLetter = json.country;
  104. const a = $('<a />').attr('data-ihknr', json.ihknr).text(json.name).appendTo(itemLi);
  105. parentLi.addClass("with-index");
  106. parentLi.prepend($('<span class="index-letter" />').text(t.indexLetter));
  107. itemLi.appendTo(ul);
  108. parentLi.appendTo(t.ihkIndex);
  109. }
  110. // Generiere einzelne IHKS
  111. else {
  112. const liSecond = $('<li />');
  113. const parentIndex = $(t.ihkIndex).find(".with-index").last().find("ul");
  114. const a = $('<a />').attr('data-ihknr', json.ihknr).text(json.name).appendTo(liSecond);
  115. liSecond.appendTo(parentIndex);
  116. }
  117. }
  118. setMarker(json) {
  119. const t = this;
  120. const marker = $('<div class="marker" id="ihk-' + json.ihknr + '" data-title="' + json.key + '" />');
  121. const opener = $('<a href="tooltip_' + json.ihknr + '" tabindex="-1" />').appendTo(marker);
  122. const tooltip = $('<div class="tooltip" id="tooltip_' + json.ihknr + '" />').appendTo(marker);
  123. const contents = $('<div class="contents" />');
  124. // const name = json.name.replace('Industrie- und Handelskammer', 'IHK');
  125. const name = json.longname;
  126. const closer = $('<button class="closer" tabindex="-1" />');
  127. tooltip.append($('<p class="title" />').text(name));
  128. tooltip.append(closer);
  129. tooltip.append(contents);
  130. if (name.length > 65) {
  131. tooltip.addClass('extra-long-name');
  132. }
  133. else if (name.length > 40) {
  134. tooltip.addClass('long-name');
  135. }
  136. contents.append($('<p class="details" />').html(json.street + '<br>' + json.zip + ' ' + json.city));
  137. //todo url nachtragen
  138. // contents.append($('<a class="btn has-icon icon-pfeil-rechts" tabindex="-1" data-ihknr="' + json.ihknr +'" />').attr('href', json.link[0].url).text('Zu Ihrer IHK'));
  139. contents.append($('<a class="btn has-icon icon-pfeil-rechts" tabindex="-1" data-ihknr="' + json.ihknr + '" />').text('Zu Ihrer IHK'));
  140. marker.css({
  141. 'left': Math.round((json.geodata.latitude - t.mapBox.left) / (t.mapBox.right - t.mapBox.left) * 10000) / 100 + '%',
  142. 'top': Math.round((json.geodata.longitude - t.mapBox.top) / (t.mapBox.bottom - t.mapBox.top) * 10000) / 100 + '%'
  143. });
  144. t.map.append(marker);
  145. opener.on('click', (e) => {
  146. e.preventDefault();
  147. const marker = $(e.currentTarget).closest('.marker');
  148. if (marker.hasClass('open')) {
  149. marker.removeClass('open');
  150. }
  151. else {
  152. this.openMarker(marker);
  153. }
  154. });
  155. closer.on('click', function (e) {
  156. const marker = $(this).closest('.marker').removeClass('open');
  157. t.setMarkerTabindex(marker);
  158. });
  159. marker.find('.btn').on('click', function (e) {
  160. e.preventDefault();
  161. t.navigateTo($(this));
  162. })
  163. }
  164. openMarker(marker) {
  165. marker.addClass('open');
  166. const prevMarker = marker.siblings('.open');
  167. const tooltip = marker.find('.tooltip');
  168. let maxOffset = 280;
  169. if (tooltip.hasClass('long-name')) {
  170. maxOffset = 310;
  171. }
  172. if (tooltip.hasClass('extra-long-name')) {
  173. maxOffset = 340;
  174. }
  175. if (marker.offset().top < maxOffset) {
  176. marker.addClass('sideways');
  177. }
  178. else {
  179. marker.removeClass('sideways');
  180. }
  181. this.setMarkerTabindex(marker);
  182. if (prevMarker.length) {
  183. prevMarker.removeClass('open');
  184. this.setMarkerTabindex(prevMarker);
  185. }
  186. }
  187. initForm() {
  188. const t = this;
  189. $.getJSON(this.communitiesUrl, function (response) {
  190. if (response.jsonResponse && response.status === 200) {
  191. t.communities = response.jsonResponse;
  192. t.initBloodhound(t.communities);
  193. }
  194. t.form.on('submit', function () {
  195. if ($('.tt-cursor').length) {
  196. $('.tt-cursor').trigger('click');
  197. } else if ($('.tt-suggestion').length > 1) {
  198. $('.tt-suggestion').first().trigger('click');
  199. }
  200. return false;
  201. });
  202. });
  203. }
  204. initBloodhound(data) {
  205. const t = this;
  206. $.each(data, function (index, value) {
  207. data[index]['normName'] = t.normalize(data[index].name);
  208. });
  209. data.sort(function (a, b) {
  210. if (a.name < b.name) return -1;
  211. if (a.name > b.name) return 1;
  212. return 0;
  213. });
  214. t.communities = new Bloodhound({
  215. datumTokenizer: function (d) {
  216. var titleTokens = Bloodhound.tokenizers.whitespace(d.normName);
  217. return titleTokens.concat(d.zips)
  218. },
  219. queryTokenizer: function (q) {
  220. var normalized = t.normalize(q);
  221. return Bloodhound.tokenizers.whitespace(normalized);
  222. },
  223. local: data,
  224. limit: 50
  225. });
  226. t.communities.initialize();
  227. var src = {
  228. name: 'communities',
  229. displayKey: 'name',
  230. source: t.communities.ttAdapter(),
  231. templates: {
  232. empty: ['<p class="no-result">Leider nichts gefunden</p>'].join('\n'),
  233. suggestion: function (e) {
  234. return ('<div data-id="' + e.ihknr + '" ><span class="name">' + e.name + '</span/><span class="zip">' + (e.zips ? e.zips.join(', ') : ' ') + '</span></div>');
  235. }
  236. }
  237. }
  238. t.initTypeahead(src);
  239. }
  240. initTypeahead(source) {
  241. const t = this;
  242. const input = t.form.find('input[type="text"]');
  243. input.typeahead({
  244. highlight: true,
  245. ttl_ms: 0
  246. },
  247. source
  248. );
  249. input.bind('typeahead:selected', (e, item, sourceName) => {
  250. const marker = $('.marker#ihk-' + item.ihknr);
  251. this.openMarker(marker);
  252. const prevMarker = marker.siblings('.open').removeClass('open');
  253. marker.siblings().removeClass('hover');
  254. t.setMarkerTabindex(marker);
  255. if (prevMarker.length) {
  256. t.setMarkerTabindex(prevMarker);
  257. }
  258. });
  259. input.bind('typeahead:autocompleted', function (e, item) {
  260. //console.log(item);
  261. })
  262. t.form.on('mouseenter', '.tt-suggestion', function () {
  263. $('.marker#ihk-' + $(this).attr('data-id')).addClass('hover').siblings('hover').removeClass('hover');
  264. })
  265. t.form.on('mouseleave', '.tt-suggestion', function () {
  266. $('.marker#ihk-' + $(this).attr('data-id')).removeClass('hover');
  267. })
  268. t.form.on('keyup', function () {
  269. if (t.form.find('.tt-cursor').length) {
  270. $('.marker#ihk-' + t.form.find('.tt-cursor').attr('data-id')).addClass('hover').siblings('.hover').removeClass('hover');
  271. const openMarker = $('.marker.open').removeClass('open');
  272. if (openMarker.length) {
  273. t.setMarkerTabindex(openMarker);
  274. }
  275. } else {
  276. $('.marker.hover').removeClass('hover');
  277. }
  278. })
  279. }
  280. setMarkerTabindex(marker) {
  281. const t = this;
  282. if (marker.hasClass('open')) {
  283. marker.find('.tooltip a, .tooltip button').removeAttr('tabindex');
  284. marker.find('.btn').focus();
  285. marker.removeClass('hover').siblings('.hover').removeClass('hover');
  286. if (!marker.is(':visible')) {
  287. t.navigateTo(marker.find('.btn'));
  288. }
  289. } else {
  290. marker.find('.tooltip a, .tooltip button').attr('tabindex', '-1');
  291. }
  292. }
  293. navigateTo(link) {
  294. console.log('SET COOKIE: ' + link.attr('data-ihknr'));
  295. window.location = link.attr('href');
  296. }
  297. normalize(str) {
  298. const t = this;
  299. $.each(t.charMap, function (normalized, regex) {
  300. str = str.replace(regex, normalized);
  301. });
  302. return str;
  303. }
  304. }
  305. $(document).ready(function () {
  306. $('.ihk-selection:not(.initialized)').each(function (i) {
  307. new Finder($(this));
  308. });
  309. });