You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

332 lines
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. if (this.isSessionStorageKeySet()) {
  57. return;
  58. }
  59. const cookie = Cookies.get('my-ihk');
  60. if (cookie) {
  61. this.initSwitch();
  62. } else {
  63. this.initSetCurrentIHK();
  64. }
  65. }
  66. initTypeahead() {
  67. const t = this;
  68. var src = {
  69. name: 'communities',
  70. displayKey: 'name',
  71. source: function (query, syncResults, asyncResults) {
  72. return $.getJSON(t.zipsUrl + query, function (response) {
  73. console.log(response);
  74. if (response.jsonResponse && response.status === 200) {
  75. if (response.jsonResponse.length > 0) {
  76. return asyncResults(response.jsonResponse);
  77. }
  78. }
  79. return asyncResults([]);
  80. });
  81. },
  82. templates: {
  83. empty: ['<p class="no-result">Leider nichts gefunden</p>'].join('\n'),
  84. suggestion: function (e) {
  85. return ('<div data-id="' + e.ihknr + '" ><span class="name">' + e.name + '</span/><span class="zip">' + (e.zips ? e.zips.join(', ') : ' ') + '</span></div>');
  86. }
  87. }
  88. }
  89. const input = t.form.find('#zipSwitchInput');
  90. t.zipInput = input;
  91. t.zipInput.typeahead({
  92. highlight: true,
  93. ttl_ms: 0,
  94. minLength: 5,
  95. },
  96. src
  97. );
  98. t.zipInput.bind('typeahead:selected', (e, item, sourceName) => {
  99. if (item) {
  100. if (!t.myLocation) {
  101. t.myLocation = {}
  102. }
  103. t.setMyLocationAsObj(item);
  104. t.setCookie();
  105. t.initSuccess();
  106. }
  107. });
  108. t.form.on('mouseenter', '.tt-suggestion', function () {
  109. $('.marker#ihk-' + $(this).attr('data-id')).addClass('hover').siblings('hover').removeClass('hover');
  110. })
  111. t.form.on('mouseleave', '.tt-suggestion', function () {
  112. $('.marker#ihk-' + $(this).attr('data-id')).removeClass('hover');
  113. })
  114. }
  115. initForm() {
  116. this.section.attr('data-show-step', 'form');
  117. this.initTypeahead();
  118. }
  119. initSetCurrentIHK() {
  120. this.section.attr('data-show-step', 'form');
  121. this.section.find('.stay-here').on('click', () => {
  122. this.getCurrentIhkAndSetCookie();
  123. this.hideSwitch();
  124. });
  125. this.section.find('.set-session-cookie').on('click', () => {
  126. this.setSessionStorageCookie();
  127. this.hideSwitch();
  128. })
  129. }
  130. // todo: delete this method after qs tests
  131. fakeIHKsForTest(json) {
  132. if (json.ihknr === 107 && ihk.settings.bIhkTestUrl !== "") {
  133. json.homepage = ihk.settings.bIhkTestUrl;
  134. } else if (json.ihknr === 118 && ihk.settings.doIhkTestUrl !== "") {
  135. json.homepage = ihk.settings.doIhkTestUrl;
  136. }
  137. }
  138. setMyLocationAsObj(item) {
  139. this.fakeIHKsForTest(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();
  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() {
  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. });
  200. }
  201. initSuccess() {
  202. this.section.attr('data-show-step', 'success');
  203. if (this.myLocation && this.myLocation.homepage) {
  204. setTimeout(() => {
  205. window.location = this.myLocation.homepage;
  206. }, 2000)
  207. }
  208. }
  209. initGeolocation() {
  210. const t = this;
  211. if (navigator.geolocation) {
  212. navigator.geolocation.getCurrentPosition((pos) => {
  213. this.getIhkList(pos)
  214. }, (err) => {
  215. this.locationError(err)
  216. });
  217. }
  218. this.section.find('.change-location').on('click', () => {
  219. t.initForm();
  220. });
  221. this.section.find('.accept-location').on('click', () => {
  222. t.setCookie();
  223. });
  224. this.section.find('.toggle-location-info').on('click', (e) => {
  225. e.preventDefault();
  226. t.section.find('#location-info').slideToggle(250, 'swing');
  227. });
  228. }
  229. locationError() {
  230. this.section.attr('data-show-step', 'form');
  231. this.initForm();
  232. }
  233. setCookie() {
  234. try {
  235. if (this.myLocation) {
  236. Cookies.set('my-ihk', JSON.stringify(this.myLocation).toString(), {expires: 365});
  237. }
  238. } catch (e) {
  239. console.log("Unable to set cookie");
  240. }
  241. this.initSwitch();
  242. }
  243. calculateDistance(ihk, currentPosition) {
  244. const p = 0.017453292519943295;
  245. const c = Math.cos;
  246. 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;
  247. const distance = 12742 * Math.asin(Math.sqrt(a));
  248. return distance;
  249. }
  250. getIhkList(position) {
  251. return $.getJSON(this.ihklListUrl, (response) => {
  252. if (response.jsonResponse && response.status === 200) {
  253. this.ihkList = response.jsonResponse;
  254. if (position) {
  255. this.showGeolocationStep(position);
  256. }
  257. }
  258. })
  259. }
  260. showGeolocationStep(position) {
  261. const t = this;
  262. t.setNearestIhkFromJson(position);
  263. t.section.find('.my-location').text(t.nearestIhk.zip + ' ' + t.nearestIhk.city);
  264. t.setMyLocationAsObj(t.nearestIhk);
  265. t.section.find('.my-ihk').text('IHK ' + t.nearestIhk.name);
  266. t.section.attr('data-show-step', 'geolocation');
  267. }
  268. setNearestIhkFromJson(position) {
  269. const t = this;
  270. $.each(t.ihkList, function (index, ihk) {
  271. const distance = t.calculateDistance(ihk.geodata, position.coords);
  272. if (t.nearestIhk) {
  273. if (t.nearestIhk.distance > distance) {
  274. ihk.distance = distance;
  275. t.nearestIhk = ihk;
  276. }
  277. } else {
  278. ihk.distance = distance;
  279. t.nearestIhk = ihk;
  280. }
  281. });
  282. }
  283. }
  284. export default IHKSwitch;
  285. $('body').on('ihk-init', () => {
  286. $('.ihk-switch:not(.initialized)').each((el) => {
  287. new IHKSwitch($(el));
  288. });
  289. });