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.
 
 
 
 

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