index.html

<!DOCTYPE html>
<html>
<head>
    <title>ConcreteAction Planning Data Map - Beta</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
    <link rel="stylesheet" href="css/leaflet-search.css" />
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin=""/>
    <link rel="stylesheet" href="css/jquery-ui.css" />
    <script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js" integrity="sha512-QVftwZFqvtRNi0ZyCtsznlKSWOStnDORoefr1enyq5mVL4tmKB3S/EnC3rRJcxCPavG10IcrVGSmPh6Qw5lwrg==" crossorigin=""></script>
    <link rel="stylesheet" href="css/style.css" /> 
    <script src="js/leaflet-search.js"></script>
    <script src="js/caml_lddlayer.js"></script>
    <script src="js/guide.js"></script>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>

<body>
    <div id='lMap' class='map'></div>
         <div id='mapSelect' class='mapSelect'>
         <p>
            <strong>Add map layer:</strong>
            <select name="layerDropDown" id="layerDropdown">
                <option value="eshpa" label="Social Housing Planning Applications (existing)">
                      All planning applications in the London Development Database that affect already existing social housing units (applications
                      that only provide new units will not be displayed)
                </option>
                <option value="alahpa" label="Local Authority Planning Applications (all)">
                      All planning applications in the London Development Database that build or remove local authority housing units
                      (i.e. council housing)
                </option>
                <option value="b1toc3prior" label="Office to Resi Convs (B1toC3) Prior Approvals">
                      Prior approvals for office to residential conversions.
                </option>
                <option value="b1toc3full" label="Office to Resi Convs (B1toC3) Applications">
                      Full applications for office to residential conversions.
                </option>
            </select>
            <button type="button"  id="addLayer">+</button>
        </p>
        <p id='maptext' class='mapGuide'></p></div>
        <div class="tab" id="tabs">
        </div>
    <interface>
        <div id='layerCtls'></div>
        <div id='interface'></div>
    </interface>
<script>
    //Initates the leaflet map.
    var map = L.map('lMap').setView([51.505, -0.09], 11);
    L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.{ext}', {
        attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
        subdomains: 'abcd',
        minZoom: 4,
        maxZoom: 18,
        ext: 'png'
    }).addTo(map);
    var pointer = new focusPointer(L, map); //initiates the point object
    var tabCtl = new tabControl(L, map, document, pointer, "tabCtl"); //initates the tab contrl
    tabCtl.setupGuide(); //instructs the tab control to add the 'guide' layer, which gives an overview of the map. 
                         //Code for the guide layer is in js/guide.js.

    function tabControl(L, map, doc, pointer, varName){
    /*  Sets up the 'tabControl' object, which is the main interface control for the software. 
    */
        var self = this;
        self.maxLayers = 4; // It is hard for the interface to show more than 4 layers. But by all means increase.
        self.layers = [];   // Where active layers go
        self.doc = doc;
        self.varName = varName;
        self.tabdiv = doc.getElementById("tabs");
        self.lp = setupLayers();  // Creates layer palette, from which layers can be added.
        var addLayerButton = doc.getElementById("addLayer");
        self.layerDd = doc.getElementById("layerDropdown");
        self.activeTab;
        self.layerDd.onchange = function(){ 
        /*  Sets the layer dropdown to change the guide text when a different
            option is selected.
        */
            label = self.layerDd.options[self.layerDd.selectedIndex].label;
            descr = self.layerDd.options[self.layerDd.selectedIndex].innerHTML;
            self.layers[0].layerObj.mapguide.label = label;
            self.layers[0].layerObj.mapguide.descr = descr;
            self.openTabById('guide');
            self.layers[0].layerObj.initControls(doc);
        }
        addLayerButton.onclick = function(){
        /*  
        */
            console.log("Layer button clicked");
            var sl = self.layerDd.options[self.layerDd.selectedIndex].value;
            if (self.layers.length >= self.maxLayers){// prevents more layers being added if max layers is met
                console.log("Max number of layers reached");
            }else{
                var present = false; 
                for (var i=0; i<self.layers.length; i++){ // checks to see if layer is already present.
                    if (self.layers[i].id == sl){
                        console.log("Layer is already present");
                        present = true;
                    }
                }
                if (!present){ //if layer is not present, adds layer.
                    console.log("Adding layer");
                    self.lp[sl].add(); //gets layer for palette and adds it.
                    self.refreshTabs();
                    doc.getElementById(sl).onclick();
                }
            }
        }

        function setupLayers(){
        /*  This sets up the layer palette for the tab controls. The 'add map layer' control uses this to create each new layer - the
            name in the dictionary corresponds to the id in its dropdown. If you wish to produce additional map layers, you need to add
            these to the layer palette, following the instructions in README.md. 
        */
            self.meta = {  leaflet: L,
                          map: map,
                          doc: doc,
                          tabs: self,
                          pointer: pointer,
                }; 
            var layerPalette = {
              "guide":
                  { add: function(){
                      var ctlLayer = new userGuide(self.meta);
                      self.layers.push({id:"guide", layerType: "guide", tabName: "About", layerObj: ctlLayer, mapActive: false});
                    }
                  }, 
              "eshpa":
                  { add: function(){ 
                      lddLayer = new lddPlanningMapLayer(self.meta, "datasets/planapp_sh.json.min", "eshpa", "Planning applications affecting existing social housing in London", "housingPcShift", "o");
                      self.layers.push({id: "eshpa", layerType: "data", tabName: "LDD-SH", layerObj: lddLayer, mapActive: false});
                    }
                  },
               "alahpa":
                  { add: function(){ 
                      lddLayer = new lddPlanningMapLayer(self.meta, "datasets/planapp_la.json.min", "alahpa", "Planning applications affecting all local authority housing in London", "housingPcShift", "pp");
                      self.layers.push({id: "alahpa", layerType: "data", tabName: "LDD-LA", layerObj: lddLayer, mapActive: false});
                    }
                },
               "b1toc3prior":
                  { add: function(){ 
                      lddLayer = new lddPlanningMapLayer(self.meta, "datasets/planapp_b1toc3_prior.json", "b1toc3prior", "Prior approval of office to residential conversion (B1 to C3)", "housingImpact", "dg");
                      self.layers.push({id: "b1toc3prior", layerType: "data", tabName: "LDD-B1toC3pr", layerObj: lddLayer, mapActive: false});
                    }
                  }, 
               "b1toc3full":
                  { add: function(){ 
                      lddLayer = new lddPlanningMapLayer(self.meta, "datasets/planapp_b1toc3_fullapp.json", "b1toc3full", "Planning applications for office to residential conversion (B1 to C3)", "housingImpact", "pk");
                      self.layers.push({id: "b1toc3full", layerType: "data", tabName: "LDD-B1toC3app", layerObj: lddLayer, mapActive: false});
                    }
                  } 
            }
            return layerPalette;
        }

        self.setupGuide = function(){
        /*  Controls initial setup of guide tab
        */
            self.lp["guide"].add();
            label = self.layerDd.options[self.layerDd.selectedIndex].label;
            descr = self.layerDd.options[self.layerDd.selectedIndex].innerHTML;
            self.layers[0].layerObj.mapguide.label = label;
            self.layers[0].layerObj.mapguide.descr = descr;
            self.refreshTabs();
            self.doc.getElementById("guide").onclick();
        }

        self.refreshTabs = function(){
        /*  Refreshes tabs, usually called on creation of new layer to ensure new tab is laid out with existing ones.
        */
            console.log("Refreshing " + self.layers.length + " tabs");
            tabhtml = "";
            for(var i=0; i<self.layers.length; i++){
                console.log("Setting up tag: " + self.layers[i].tabName);
                var closebtn = '<div class="tabclose"></div>';
                var vischk = '<div class="tabvis"></div>';
                if (self.layers[i].layerType == "data"){
                    closebtn = '<button class="tabclose" id="close-' + self.layers[i] + '" onclick="' + self.varName + '.closeTab(' + i + ')">x</button>';
                    check = "";
                    cssclass = "tablinks";
                    if(self.layers[i].layerObj.state.visible){
                        check = "checked";
                    }
                    vischk = '<div class="tabvis chkCont"><input type="checkbox" id="layerVisible' + self.layers[i].id + '" name="layerVisible" value="layerVisible" oninput="' 
                              + self.varName + '.layerVisibleClicked(\''+ self.layers[i].id +'\')" ' + check + '></input></div>';
                }
                tabhtml += '<button class="tablinks" id="' + self.layers[i].id + '" onclick="' + self.varName + '.openTab(' + i + ')">' + self.layers[i].tabName + vischk + closebtn + '</button>';
            }
            self.tabdiv.innerHTML = tabhtml;
        }

        self.closeTab = function(i){
        /*  Removes tab from display, and removes active tab from layer list. 
        */
            console.log("Close tab clicked: " + i);
            self.layers[i].layerObj.deleteThis();
            self.layers.splice(i, 1);
            self.doc.getElementById('layerCtls').innerHTML = "";
            self.doc.getElementById("interface").innerHTML = "";
            self.refreshTabs();
            self.activeTab = 99; // This is set to an absurd number to ensure it is not the number of the next tab that will be opened.
            if (self.layers.length > 0){
                self.openTab(0);
            }
        }

        self.openTabById = function(id){
            var tabNo = 0;
            for(var i=0; i<self.layers.length; i++){
                if (self.layers[i].id == id){
                    tabNo = i;
                }
            }
            self.openTab(tabNo);
        }

        self.openTab = function(i){
        /* opens a tab that has been clicked on, or for other reasons (e.g. loaded, or another
           open tab has been closed. 
        */
            console.log("Open tab clicked: " + i);
            if (self.activeTab != i){ //if this tab is not already open...
                console.log("Setting up layers controls: " + self.layers[i].id);
                lctl = self.doc.getElementById('layerCtls');
                check = "";
                var lddCtl = self.layers[i].layerObj.outputControls(); // gets the controls for this layer
                self.doc.getElementById("interface").innerHTML = lddCtl; // and outputs them into the interface
                self.layers[i].layerObj.initControls(self.doc); // activates the controls
                if (self.layers[i].layerType == "data" ){
                  if (self.layers[i].layerObj.state.ready == true){   // notes the layer is in a ready state
                    console.log("Activating summary and listing tabs");
                    self.layers[i].layerObj.activateInnerTab("tabSummary"); //activates summary data
                    self.layers[i].layerObj.activateInnerTab("tabListing"); //activates listing data
                  }
                  self.layers[i].layerObj.activateInnerTab("tabFilter");  //actives filters
                  if (!self.layers[i].mapActive){
                      self.layers[i].layerObj.getData();  // if the map is not active, get the data and activate it!
                      self.layers[i].mapActive = true;
                  }
                }
            }else{
                console.log("We are already on this layer: " + self.layers[i].id);
            }
            //cycles through existing tabs, to set them as inactive
            tablinks = self.doc.getElementsByClassName("tablinks");
            tabclose = self.doc.getElementsByClassName("tabclose");
            tabvis = self.doc.getElementsByClassName("tabvis");
            for (k = 0; k < tablinks.length; k++) {
                tablinks[k].className = tablinks[k].className.replace(" active", "");
                tabclose[k].className = tabclose[k].className.replace(" active", "");
                tabvis[k].className = tabvis[k].className.replace(" sel", "");
            }
            //sets open tab as active
            tablinks[i].className += " active";
            tabclose[i].className += " active";
            tabvis[i].className += " sel";
            self.activeTab = i;
        }

        self.layerVisibleClicked = function(id){
        /*  Responds to when the visibility toggle for each layer is clicked
        */
            console.log("layer visible clicked - " + id + " should disappear");
            var check = self.doc.getElementById("layerVisible" + id).checked;
            for(var i=0; i<self.layers.length; i++){
                if (self.layers[i].id == id){
                    self.layers[i].layerObj.setVisible(check);
                }
            }
        }
    }

    function controlLayer (){
        self.outputControls()
    }

    function focusPointer (L, map){
    /*  This subroutine creates a pointer object, which can be placed on the map
        to show which planning application is selected. It is passed to the ldd map
        objects via the 'meta' dictionary. It is defined here because there needs to
        be one pointer for all map layers to prevent confusion
    */
        var self = this;
        self.icon = L.icon({
            iconUrl: 'images/pointer.png',
            iconSize:     [43, 31], // size of the icon
            iconAnchor:   [40, 29], // point of the icon which will correspond to marker's location
            popupAnchor:  [21, 21] // point from which the popup should open relative to the iconAnchor
        });
        self.L = L;
        self.map = map;
        self.active = false;

        self.refocus = function(lat, lon){
            if (self.active){
                self.remove(self.mark);
            }else{
                self.active = true;
            }
            self.add(lat, lon);
        }

        self.add = function(lat, lon){
            self.mark = self.L.marker([lat, lon], {icon: self.icon}).addTo(self.map);
        }

        self.remove = function(){
            self.map.removeLayer(self.mark);
        }
    }

</script>
</body>
</html>