<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Custom Widget for mapbox in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/custom-widget-for-mapbox/qaa-p/12636579#M4740279</link>
    <description>&lt;P&gt;Thanks for this Bob - I did initially try a document.getElementById("map") which returned a null inside the constructor so that rings true.&lt;/P&gt;&lt;P&gt;I had a look at the doc but I could do with an example of where and how to implement connectedCallback  if possible?&lt;/P&gt;</description>
    <pubDate>Wed, 12 Oct 2022 14:54:52 GMT</pubDate>
    <dc:creator>andrewbarlow1</dc:creator>
    <dc:date>2022-10-12T14:54:52Z</dc:date>
    <item>
      <title>Custom Widget for mapbox</title>
      <link>https://community.sap.com/t5/technology-q-a/custom-widget-for-mapbox/qaq-p/12636577</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
  &lt;P&gt;I was just throwing this out there to see if anybody had been able to successfully embed the mapbox js library inside a custom widget inside SAC?&lt;/P&gt;
  &lt;P&gt;I can load the library ok but when I instantiate the map through the constructor it fails to find the div which is defined at the beginning?&lt;/P&gt;
  &lt;P&gt;I was wondering if anybody had been able to successfully do this? &lt;/P&gt;
  &lt;P&gt;This is the Javascript I am using...&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;(function () {
    let template = document.createElement("template");
    template.innerHTML = `
        &amp;lt;meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"&amp;gt;
        &amp;lt;link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v2.10.0/mapbox-gl.css"&amp;gt;
        &amp;lt;style&amp;gt;
            #map { 
                    width: 100%;
                    height:100%;
                }
        &amp;lt;/style&amp;gt;
        &amp;lt;div id='map'&amp;gt;&amp;lt;/div&amp;gt;
`;
    class Map extends HTMLElement {
        constructor() {
            super();
            this.appendChild(template.content.cloneNode(true));
            this._props = {};
            let that = this;
            mapboxgl.accessToken = "OUR MAPBOX TOKEN";
            that._map = new mapboxgl.Map({
                container: "map",
                style: 'mapbox://styles/mapbox/streets-v11',
                center: [-74.5, 40],
                zoom: 9
            });
        }
        onCustomWidgetBeforeUpdate(changedProperties) {
            this._props = { ...this._props, ...changedProperties };
        }
        onCustomWidgetAfterUpdate(changedProperties) {
        }
    }

    let scriptSrc = "https://api.mapbox.com/mapbox-gl-js/v2.10.0/mapbox-gl.js";
    let onScriptLoaded = function () {
        customElements.define("com-test-map", Map);
    }

    let customElementScripts = window.sessionStorage.getItem("customElementScripts") || [];
    let scriptStatus = customElementScripts.find(function (element) {
       return element.src == scriptSrc;
    });

    if (scriptStatus) {
        if (scriptStatus.status == "ready") {
            onScriptLoaded();
        } else {
            scriptStatus.callbacks.push(onScriptLoaded);
        }
    } else {
        let scriptObject = {
            "src": scriptSrc,
            "status": "loading",
            "callbacks": [onScriptLoaded]
        }

        customElementScripts.push(scriptObject);
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = scriptSrc;
        script.onload = function () {
            scriptObject.status = "ready";
            scriptObject.callbacks.forEach((callbackFn) =&amp;gt; callbackFn.call());
        };
        document.head.appendChild(script);
       }
})();

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Oct 2022 13:42:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/custom-widget-for-mapbox/qaq-p/12636577</guid>
      <dc:creator>andrewbarlow1</dc:creator>
      <dc:date>2022-10-12T13:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Widget for mapbox</title>
      <link>https://community.sap.com/t5/technology-q-a/custom-widget-for-mapbox/qaa-p/12636578#M4740278</link>
      <description>&lt;P&gt;I assume mapbox tries to find the target div by something like document.getElementById("map"). However at this point in time your custom element was not added to the DOM (see &lt;A href="https://help.sap.com/docs/SAP_ANALYTICS_CLOUD/0ac8c6754ff84605a4372468d002f2bf/8b4c4c845b6643879e1bf79bbece44e1.html#web-component-lifecycle)" target="test_blank"&gt;https://help.sap.com/docs/SAP_ANALYTICS_CLOUD/0ac8c6754ff84605a4372468d002f2bf/8b4c4c845b6643879e1bf79bbece44e1.html#web-component-lifecycle)&lt;/A&gt;. You should defer the creation of the map to "connectedCallback" which is the point in time where the custom element was added to the DOM. You can check by calling getElementById from your constructor.&lt;/P&gt;&lt;P&gt;Also, do you want to allow your users to place more than one  instance of this widget in an app &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; ? If yes you should either give your map div a unique ID or use a shadow root (depends on whether mapbox supports this).&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 14:28:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/custom-widget-for-mapbox/qaa-p/12636578#M4740278</guid>
      <dc:creator>Bob0001</dc:creator>
      <dc:date>2022-10-12T14:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Widget for mapbox</title>
      <link>https://community.sap.com/t5/technology-q-a/custom-widget-for-mapbox/qaa-p/12636579#M4740279</link>
      <description>&lt;P&gt;Thanks for this Bob - I did initially try a document.getElementById("map") which returned a null inside the constructor so that rings true.&lt;/P&gt;&lt;P&gt;I had a look at the doc but I could do with an example of where and how to implement connectedCallback  if possible?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 14:54:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/custom-widget-for-mapbox/qaa-p/12636579#M4740279</guid>
      <dc:creator>andrewbarlow1</dc:creator>
      <dc:date>2022-10-12T14:54:52Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Widget for mapbox</title>
      <link>https://community.sap.com/t5/technology-q-a/custom-widget-for-mapbox/qaa-p/12636580#M4740280</link>
      <description>&lt;P&gt;You definitely put me on the right path Bob - thankyou very much... &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;(function () {
    let template = document.createElement("template");
    template.innerHTML = `
        &amp;lt;meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"&amp;gt;
        &amp;lt;link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v2.10.0/mapbox-gl.css"&amp;gt;
        &amp;lt;style&amp;gt;
            #map{ 
                    width: 100%;
                    height:100%;
                }
        &amp;lt;/style&amp;gt;
        &amp;lt;div id='map'&amp;gt;&amp;lt;/div&amp;gt;
`;

    class Map extends HTMLElement {
        constructor() {
            debugger
            super();
            this._shadowRoot = this.attachShadow({ mode: "open" });
            this.appendChild(template.content.cloneNode(true));
            this._props = {};
            this._firstConnection = false;
            //let that = this;
        }

        connectedCallback() {
            this._firstConnection = true;
            mapboxgl.accessToken = "THE TOKEN";
            this._map = new mapboxgl.Map({
                container: "map",
                style: 'mapbox://styles/mapbox/streets-v11',
                center: [-74.5, 40],
                zoom: 9
            });
        }

        onCustomWidgetBeforeUpdate(changedProperties) {
            this._props = { ...this._props, ...changedProperties };
        }
        onCustomWidgetAfterUpdate(changedProperties) {
        }
    }

    let scriptSrc = "https://api.mapbox.com/mapbox-gl-js/v2.10.0/mapbox-gl.js";
    let onScriptLoaded = function () {
        customElements.define("com-test-map", Map);
    }
    
    //SHARED FUNCTION: reuse between widgets
    //function(src, callback) {
    let customElementScripts = window.sessionStorage.getItem("customElementScripts") || [];

    let scriptStatus = customElementScripts.find(function (element) {
        return element.src == scriptSrc;
    });

    if (scriptStatus) {
        if (scriptStatus.status == "ready") {
            onScriptLoaded();
            // console.log("SCRIPT IS LOADED");
        } else {
            // console.log("SCRIPT NOT LOADED");
            scriptStatus.callbacks.push(onScriptLoaded);
        }
    } else {
        // debugger
        let scriptObject = {
            "src": scriptSrc,
            "status": "loading",
            "callbacks": [onScriptLoaded]
        }

        customElementScripts.push(scriptObject);
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = scriptSrc;
        script.onload = function () {
            scriptObject.status = "ready";
            scriptObject.callbacks.forEach((callbackFn) =&amp;gt; callbackFn.call());
        };
        document.head.appendChild(script);
    }

    // }, false);
})();

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Oct 2022 15:55:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/custom-widget-for-mapbox/qaa-p/12636580#M4740280</guid>
      <dc:creator>andrewbarlow1</dc:creator>
      <dc:date>2022-10-12T15:55:30Z</dc:date>
    </item>
  </channel>
</rss>

