Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

296 linhas
8.2 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. const Cookies = require('js-cookie');
  5. class IhkSwitch {
  6. constructor(section) {
  7. const t = this;
  8. this.serviceUrlBase = "/blueprint/servlet/serviceport/ihkfinder";
  9. this.ihklListUrl = 'services/ihkList.json';//this.serviceUrlBase + "/ihk/all";
  10. this.communitiesUrl = 'services/communities.json';//this.serviceUrlBase + "/community/all";
  11. // berechnen d
  12. // center ist geolocation aus js api
  13. // item die IHK
  14. /*
  15. const p = 0.017453292519943295;
  16. const c = Math.cos;
  17. 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;
  18. const distance = 12742 * Math.asin(Math.sqrt(a));
  19. */
  20. $(section).addClass("initialized");
  21. t.section = section;
  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.initClosing();
  34. t.checkState();
  35. }
  36. initClosing() {
  37. this.section.find('.closer').on('click', () => {
  38. this.section.addClass('hide');
  39. setTimeout(() => {
  40. this.section.removeAttr('data-show-step');
  41. }, 400);
  42. })
  43. }
  44. checkState() {
  45. const cookie = Cookies.get('my-ihk');
  46. if (cookie) {
  47. this.initSwitch();
  48. } else {
  49. this.initGeolocation();
  50. }
  51. }
  52. loadIhkList(callback) {
  53. $.getJSON(this.ihklListUrl, (response) => {
  54. if (response.jsonResponse && response.status === 200) {
  55. this.ihkList = response.jsonResponse.sort((a, b) => {
  56. const countryNameA = a.country.toUpperCase(); // ignore upper and lowercase
  57. const countryNameB = b.country.toUpperCase(); // ignore upper and lowercase
  58. // Sort by country
  59. if (countryNameA < countryNameB) {
  60. return -1;
  61. }
  62. if (countryNameA > countryNameB) {
  63. return 1;
  64. }
  65. const ihkNameA = a.name.toUpperCase(); // ignore upper and lowercase
  66. const ihkNameB = b.name.toUpperCase(); // ignore upper and lowercase
  67. // Sort by name
  68. if (ihkNameA < ihkNameB) {
  69. return -1;
  70. }
  71. if (ihkNameA > ihkNameB) {
  72. return 1;
  73. }
  74. // names must be equal
  75. return 0;
  76. });
  77. //console.log(this.ihkList);
  78. callback();
  79. }
  80. })
  81. }
  82. loadCommunities(callback) {
  83. $.getJSON(this.communitiesUrl, (communities) => {
  84. if (communities.jsonResponse && communities.status === 200) {
  85. this.communities = communities.jsonResponse;
  86. callback();
  87. }
  88. });
  89. }
  90. initForm() {
  91. this.section.attr('data-show-step', 'form');
  92. if (this.communities && this.ihkList) {
  93. this.initBloodhound(this.communities);
  94. }
  95. else {
  96. this.loadIhkList(() => { this.loadCommunities( () => { this.initBloodhound(this.communities) } ) });
  97. }
  98. this.form.on('submit', function () {
  99. /*
  100. if ($('.tt-cursor').length) {
  101. $('.tt-cursor').trigger('click');
  102. } else if ($('.tt-suggestion').length > 1) {
  103. $('.tt-suggestion').first().trigger('click');
  104. }
  105. */
  106. return false;
  107. });
  108. }
  109. initGeolocation() {
  110. navigator.geolocation.getCurrentPosition((pos) => {this.locationSuccess(pos)}, (err) => {this.locationError(err)});
  111. this.section.find('.change-location').on('click', () => {
  112. this.initForm();
  113. })
  114. this.section.find('.accept-location').on('click', () => {
  115. this.setCookie();
  116. })
  117. this.section.find('.toggle-location-info').on('click', (e) => {
  118. e.preventDefault();
  119. this.section.find('#location-info').slideToggle(250, 'swing');
  120. })
  121. }
  122. setCookie() {
  123. const myIHK = this.ihkList.find(ihk => {
  124. return ihk.ihknr === this.myLocation.ihk
  125. })
  126. delete myIHK.zips;
  127. Cookies.set('my-ihk', JSON.stringify(myIHK).toString(), { expires: 365 });
  128. this.initSwitch();
  129. }
  130. locationSuccess(position) {
  131. let url = 'https://api.bigdatacloud.net/data/reverse-geocode-client?localityLanguage=de';
  132. url += '&latitude=' + position.coords.latitude;
  133. url += '&longitude=' + position.coords.longitude;
  134. $.getJSON(url, (location) => {
  135. this.section.find('.my-location').text(location.postcode + ' ' + location.city);
  136. this.myLocation = {
  137. city: location.city,
  138. zip: location.postcode
  139. }
  140. this.loadCommunities(() => {this.checkMyLocation()});
  141. })
  142. }
  143. checkMyLocation() {
  144. this.myLocation.ihk = -1;
  145. $.each(this.communities, (index, value) => {
  146. if (this.normalize(this.myLocation.city).toUpperCase() === value.name || (value.zips && value.zips.indexOf(this.myLocation.zip) > -1)) {
  147. this.myLocation.ihk = value.ihknr;
  148. this.loadIhkList(() => {this.completeGeolocation()});
  149. return false;
  150. }
  151. });
  152. if (this.myLocation.ihk === -1) {
  153. this.initForm();
  154. }
  155. }
  156. completeGeolocation() {
  157. const myIHK = this.ihkList.find(ihk => {
  158. return ihk.ihknr === this.myLocation.ihk
  159. })
  160. if (myIHK) {
  161. this.section.find('.my-ihk').text('IHK ' + myIHK.name);
  162. this.section.attr('data-show-step', 'geolocation');
  163. }
  164. else {
  165. this.initForm();
  166. }
  167. }
  168. locationError(error) {
  169. this.section.attr('data-show-step', 'form');
  170. this.initForm();
  171. }
  172. initBloodhound(data) {
  173. const t = this;
  174. $.each(data, function (index, value) {
  175. data[index]['normName'] = t.normalize(data[index].name);
  176. });
  177. data.sort(function (a, b) {
  178. if (a.name < b.name) return -1;
  179. if (a.name > b.name) return 1;
  180. return 0;
  181. });
  182. t.communities = new Bloodhound({
  183. datumTokenizer: function (d) {
  184. var titleTokens = Bloodhound.tokenizers.whitespace(d.normName);
  185. return titleTokens.concat(d.zips)
  186. },
  187. queryTokenizer: function (q) {
  188. var normalized = t.normalize(q);
  189. return Bloodhound.tokenizers.whitespace(normalized);
  190. },
  191. local: data,
  192. limit: 50
  193. });
  194. t.communities.initialize();
  195. var src = {
  196. name: 'communities',
  197. displayKey: 'name',
  198. source: t.communities.ttAdapter(),
  199. templates: {
  200. empty: ['<p class="no-result">Leider nichts gefunden</p>'].join('\n'),
  201. suggestion: function (e) {
  202. return ('<div data-id="' + e.ihknr + '" ><span class="name">' + e.name + '</span/><span class="zip">' + (e.zips ? e.zips.join(', ') : ' ') + '</span></div>');
  203. }
  204. }
  205. }
  206. t.initTypeahead(src);
  207. }
  208. initTypeahead(source) {
  209. const t = this;
  210. const input = t.form.find('input[type="text"]');
  211. input.typeahead({
  212. highlight: true,
  213. ttl_ms: 0
  214. },
  215. source
  216. );
  217. input.bind('typeahead:selected', (e, item) => {
  218. if (!this.myLocation) {
  219. this.myLocation = {}
  220. }
  221. this.myLocation.city = item.name;
  222. this.myLocation.ihk = item.ihknr;
  223. this.initSwitch();
  224. });
  225. }
  226. initSwitch() {
  227. let myIHK;
  228. const currIHK = parseInt(this.section.find('.current-ihk').attr('data-ihknr'));
  229. if (this.myLocation && this.myLocation.ihk) {
  230. myIHK = this.ihkList.find(ihk => {
  231. return ihk.ihknr === this.myLocation.ihk
  232. })
  233. }
  234. else {
  235. myIHK = JSON.parse(Cookies.get('my-ihk'));
  236. }
  237. if (currIHK !== myIHK.ihknr) {
  238. this.section.find('.my-ihk').text('IHK ' + myIHK.name);
  239. this.section.attr('data-show-step', 'switch');
  240. }
  241. this.section.find('.stay-here').on('click', () => {
  242. this.section.find('.closer').trigger('click');
  243. })
  244. this.section.find('.change-ihk').on('click', () => {
  245. this.initSuccess();
  246. })
  247. }
  248. initSuccess() {
  249. this.section.attr('data-show-step', 'success');
  250. console.log('add redirect link!');
  251. }
  252. navigateTo(link) {
  253. //console.log('SET COOKIE: ' + link.attr('data-ihknr'));
  254. window.location = link.attr('href');
  255. }
  256. normalize(str) {
  257. const t = this;
  258. $.each(t.charMap, function (normalized, regex) {
  259. str = str.replace(regex, normalized);
  260. });
  261. return str;
  262. }
  263. }
  264. $(document).ready(function () {
  265. $('.ihk-switch:not(.initialized)').each(function (i) {
  266. new IhkSwitch($(this));
  267. });
  268. });