Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

335 Zeilen
8.9 KiB

  1. const $ = require('jquery');
  2. const Cookies = require('js-cookie');
  3. // const Typeahead = require("typeahead.js/dist/typeahead.jquery.js");
  4. class IHKSwitch {
  5. constructor(section) {
  6. const t = this;
  7. this.serviceUrlBase = "/blueprint/servlet/serviceport/ihkfinder";
  8. this.ihklListUrl = this.serviceUrlBase + "/ihk/all";
  9. this.communitiesUrl = this.serviceUrlBase + "/community/all";
  10. this.zipsUrl = this.serviceUrlBase + "/zip/";
  11. this.ihkList = null;
  12. this.nearestIhk = null;
  13. this.formLocked = false;
  14. section.addClass("initialized");
  15. t.section = section;
  16. t.form = section.find(".ihk-finder");
  17. this.landingChangerBtn = $('.ihk-landing-changer');
  18. if (this.landingChangerBtn.length) {
  19. this.landingChangerBtn.on("click", (e) => {
  20. e.preventDefault();
  21. // lösche cookie so dass der User die Möglichkeit hat auf die neue IHK zu wechseln (Mit cookie wird es es nur weitergeleitet)
  22. Cookies.remove('my-ihk', {expires: 365});
  23. // clear session cookie just in case
  24. if (window.sessionStorage && window.sessionStorage.getItem("my-ihk-ignore") === "true") {
  25. window.sessionStorage.removeItem("my-ihk-ignore");
  26. }
  27. window.location = "/";
  28. })
  29. }
  30. this.checkState();
  31. this.initClosing()
  32. this.initStayHereZipBtn(section);
  33. }
  34. initStayHereZipBtn(section) {
  35. let stayHereBtn = section.find('button.stay-here-zip');
  36. if (stayHereBtn) {
  37. stayHereBtn.on('click', (e) => {
  38. e.preventDefault();
  39. this.getCurrentIhkAndSetCookie(true);
  40. });
  41. }
  42. }
  43. isSessionStorageKeySet() {
  44. try {
  45. if (window.sessionStorage && window.sessionStorage.getItem("my-ihk-ignore") === "true") {
  46. return true;
  47. }
  48. } catch (e) {
  49. console.info("Something is wrong with session cookie", e)
  50. }
  51. return false;
  52. }
  53. initClosing() {
  54. this.section.find('.closer').on('click', () => {
  55. this.setSessionStorageCookie();
  56. this.hideSwitch();
  57. });
  58. }
  59. hideSwitch() {
  60. this.section.addClass('hide');
  61. setTimeout(() => {
  62. this.section.removeAttr('data-show-step');
  63. }, 400);
  64. }
  65. checkState() {
  66. if (this.isSessionStorageKeySet()) {
  67. return;
  68. }
  69. const cookie = Cookies.get('my-ihk');
  70. if (cookie) {
  71. this.initSwitch();
  72. } else {
  73. this.initSetCurrentIHK();
  74. }
  75. }
  76. initTypeahead() {
  77. const t = this;
  78. var src = {
  79. name: 'communities',
  80. displayKey: 'name',
  81. source: function (query, syncResults, asyncResults) {
  82. return $.getJSON(t.zipsUrl + query, function (response) {
  83. console.log(response);
  84. if (response.jsonResponse && response.status === 200) {
  85. if (response.jsonResponse.length > 0) {
  86. return asyncResults(response.jsonResponse);
  87. }
  88. }
  89. return asyncResults([]);
  90. });
  91. },
  92. templates: {
  93. empty: ['<p class="no-result">Leider nichts gefunden</p>'].join('\n'),
  94. suggestion: function (e) {
  95. return ('<div data-id="' + e.ihknr + '" ><span class="name">' + e.name + '</span/><span class="zip">' + (e.zips ? e.zips.join(', ') : ' ') + '</span></div>');
  96. }
  97. }
  98. }
  99. t.zipInput = t.form.find('#zipSwitchInput');
  100. t.zipInput.typeahead({
  101. highlight: true,
  102. ttl_ms: 0,
  103. minLength: 5,
  104. },
  105. src
  106. );
  107. t.zipInput.bind('typeahead:selected', (e, item, sourceName) => {
  108. if (item) {
  109. if (!t.myLocation) {
  110. t.myLocation = {}
  111. }
  112. t.setMyLocationAsObj(item);
  113. t.setCookie();
  114. t.initSuccess();
  115. }
  116. });
  117. t.form.on('mouseenter', '.tt-suggestion', function () {
  118. $('.marker#ihk-' + $(this).attr('data-id')).addClass('hover').siblings('hover').removeClass('hover');
  119. })
  120. t.form.on('mouseleave', '.tt-suggestion', function () {
  121. $('.marker#ihk-' + $(this).attr('data-id')).removeClass('hover');
  122. })
  123. }
  124. initForm() {
  125. this.section.attr('data-show-step', 'form');
  126. this.initTypeahead();
  127. }
  128. initSetCurrentIHK() {
  129. this.section.attr('data-show-step', 'form');
  130. this.section.find('.stay-here').on('click', () => {
  131. this.getCurrentIhkAndSetCookie(false);
  132. this.hideSwitch();
  133. });
  134. this.section.find('.set-session-cookie').on('click', () => {
  135. this.setSessionStorageCookie();
  136. this.hideSwitch();
  137. })
  138. }
  139. setMyLocationAsObj(item) {
  140. this.myLocation = {
  141. city: item.city,
  142. country: item.country,
  143. ihknr: item.ihknr,
  144. zip: item.zip,
  145. distance: item.distance || 0,
  146. geodata: item.geodata,
  147. homepage: item.homepage,
  148. key: item.key,
  149. longname: item.longname,
  150. name: item.name
  151. };
  152. }
  153. initSwitch() {
  154. let myIHK;
  155. const currIHK = parseInt(this.section.find('.current-ihk').attr('data-ihknr'));
  156. const myihkCookie = Cookies.get('my-ihk');
  157. if (this.myLocation && this.myLocation.ihknr && this.ihkList) {
  158. myIHK = this.ihkList.find(ihk => {
  159. return ihk.ihknr === this.myLocation.ihknr
  160. })
  161. } else if (myihkCookie) {
  162. myIHK = JSON.parse(myihkCookie);
  163. this.myLocation = myIHK;
  164. }
  165. if (myIHK && myIHK.ihknr && (currIHK !== myIHK.ihknr)) {
  166. this.section.find('.my-ihk').text(myIHK.longname);
  167. this.section.attr('data-show-step', 'switch');
  168. } else {
  169. this.hideSwitch();
  170. }
  171. this.section.find('.stay-here').on('click', () => {
  172. this.getCurrentIhkAndSetCookie(false);
  173. this.hideSwitch();
  174. });
  175. // this.section.find('.change-ihk').on('click', () => {
  176. // this.initSuccess();
  177. // })
  178. this.section.find('.set-session-cookie').on('click', () => {
  179. this.setSessionStorageCookie();
  180. this.hideSwitch();
  181. })
  182. }
  183. setSessionStorageCookie() {
  184. try {
  185. window.sessionStorage.setItem("my-ihk-ignore", "true");
  186. } catch (e) {
  187. console.info("Unable to set session Storage cookie, ", e)
  188. }
  189. }
  190. getCurrentIhkAndSetCookie(withReload) {
  191. this.getIhkList().then(() => {
  192. let myIHK;
  193. const currIHK = parseInt(this.section.find('.current-ihk').attr('data-ihknr'));
  194. myIHK = this.ihkList.find(ihk => {
  195. return ihk.ihknr === currIHK
  196. });
  197. this.setMyLocationAsObj(myIHK);
  198. this.setCookie();
  199. if (withReload) {
  200. window.location.reload();
  201. }
  202. });
  203. }
  204. initSuccess() {
  205. this.section.attr('data-show-step', 'success');
  206. if (this.myLocation && this.myLocation.homepage) {
  207. setTimeout(() => {
  208. window.location = this.myLocation.homepage;
  209. }, 2000)
  210. }
  211. }
  212. initGeolocation() {
  213. const t = this;
  214. if (navigator.geolocation) {
  215. navigator.geolocation.getCurrentPosition((pos) => {
  216. this.getIhkList(pos)
  217. }, (err) => {
  218. this.locationError(err)
  219. });
  220. }
  221. this.section.find('.change-location').on('click', () => {
  222. t.initForm();
  223. });
  224. this.section.find('.accept-location').on('click', () => {
  225. t.setCookie();
  226. });
  227. this.section.find('.toggle-location-info').on('click', (e) => {
  228. e.preventDefault();
  229. t.section.find('#location-info').slideToggle(250, 'swing');
  230. });
  231. }
  232. locationError() {
  233. this.section.attr('data-show-step', 'form');
  234. this.initForm();
  235. }
  236. setCookie() {
  237. try {
  238. if (this.myLocation) {
  239. Cookies.set('my-ihk', JSON.stringify(this.myLocation).toString(), {expires: 365});
  240. Cookies.set('ihknr', this.myLocation.ihknr, {expires: 365});
  241. }
  242. } catch (e) {
  243. console.log("Unable to set cookie");
  244. }
  245. this.initSwitch();
  246. }
  247. calculateDistance(ihk, currentPosition) {
  248. const p = 0.017453292519943295;
  249. const c = Math.cos;
  250. const a = 0.5 - c((ihk.latitude - currentPosition.latitude) * p) / 2 + c(currentPosition.latitude * p) * c(ihk.latitude * p) * (1 - c((ihk.longitude - currentPosition.longitude) * p)) / 2;
  251. return 12742 * Math.asin(Math.sqrt(a));
  252. }
  253. getIhkList(position) {
  254. return $.getJSON(this.ihklListUrl, (response) => {
  255. if (response.jsonResponse && response.status === 200) {
  256. this.ihkList = response.jsonResponse;
  257. if (position) {
  258. this.showGeolocationStep(position);
  259. }
  260. }
  261. })
  262. }
  263. showGeolocationStep(position) {
  264. const t = this;
  265. t.setNearestIhkFromJson(position);
  266. t.section.find('.my-location').text(t.nearestIhk.zip + ' ' + t.nearestIhk.city);
  267. t.setMyLocationAsObj(t.nearestIhk);
  268. t.section.find('.my-ihk').text('IHK ' + t.nearestIhk.name);
  269. t.section.attr('data-show-step', 'geolocation');
  270. }
  271. setNearestIhkFromJson(position) {
  272. const t = this;
  273. $.each(t.ihkList, function (index, ihk) {
  274. const distance = t.calculateDistance(ihk.geodata, position.coords);
  275. if (t.nearestIhk) {
  276. if (t.nearestIhk.distance > distance) {
  277. ihk.distance = distance;
  278. t.nearestIhk = ihk;
  279. }
  280. } else {
  281. ihk.distance = distance;
  282. t.nearestIhk = ihk;
  283. }
  284. });
  285. }
  286. }
  287. export default IHKSwitch;
  288. $('body').on('ihk-init', () => {
  289. $('.ihk-switch:not(.initialized)').each((el) => {
  290. new IHKSwitch($(el));
  291. });
  292. });