Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Swift bridge setup
  2.  
  3. function setupWKWebViewJavascriptBridge(callback) {
  4.     if (window.WKWebViewJavascriptBridge) {
  5.         return callback(window.WKWebViewJavascriptBridge);
  6.     }
  7.     if (window.WKWVJBCallbacks) {
  8.         return window.WKWVJBCallbacks.push(callback);
  9.     }
  10.     window.WKWVJBCallbacks = [callback];
  11.     window.webkit.messageHandlers.iOS_Native_InjectJavascript.postMessage(null);
  12. }
  13.  
  14. // Logic
  15.  
  16. setupWKWebViewJavascriptBridge(function(bridge) {
  17.     freshBridge = bridge;
  18.  
  19.     function startSVGLoadingTimer() {
  20.         if (!document.querySelector('svg')) {
  21.             setTimeout(test, 200);
  22.         } else {
  23.             bridge.callHandler('JSSVGLoadedEvent', {},
  24.                 function(response) {}
  25.             );
  26.         }
  27.     }
  28.  
  29.     startSVGLoadingTimer()
  30.  
  31.     bridge.registerHandler('InitialSetupSVGViewEvent', function(
  32.         data,
  33.         responseCallback
  34.     ) {
  35.         const places = JSON.parse(data);
  36.  
  37.         places.forEach(element => {
  38.             const place = document.getElementById(element.svgId);
  39.  
  40.             if (place) {
  41.                 const childs = place.childNodes;
  42.                 childs.forEach(child => {
  43.                     child.style.fill = element.hexColor;
  44.                 });
  45.  
  46.                 place.addEventListener('click', handleClick(element.svgId));
  47.             }
  48.         });
  49.     });
  50.  
  51.     bridge.registerHandler('SelectPlaceSVGViewEvent', function(
  52.         data,
  53.         responseCallback
  54.     ) {
  55.         const places = JSON.parse(data);
  56.  
  57.         places.forEach(element => {
  58.             const place = document.getElementById(element.svgId);
  59.  
  60.             if (place) {
  61.                 const childs = place.childNodes;
  62.                 childs.forEach(child => {
  63.                     child.style.fill = element.hexColor;
  64.                 });
  65.             }
  66.         });
  67.     });
  68. });
  69.  
  70. const handleClick = id => e => {
  71.     freshBridge.callHandler(
  72.         'JSPlaceSelectionStateChangedEvent', {
  73.             id: id
  74.         },
  75.         function(response) {}
  76.     );
  77. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement