从单个链接打开的两个模态

时间:2015-12-17 20:50:54

标签: javascript jquery twitter-bootstrap leaflet

我有一些代码可以打开一个模式,当标记放在传单中的某个图层上时,该模式包含一个表单。这一切都很好地解决了任何问题。我复制了这段代码,所以我可以制作第二个按钮来控制另一个包含不同形式的模态的开放。我更改了链接信息,但是当点击按钮时

L.DomEvent.on(link, 'click', L.DomEvent.stop)
                  .on(link, 'click', L.bind(this.startCreating, this));

同时打开两个表单或打开一个然后关闭并再次单击将打开两个表单。我不确定最新情况,因为每个模态都有不同的ID。

以下是带有模态的地图的实例。单击缩放控件下左侧的小元素/按钮,然后拖动新市场并再次单击以在地图上设置其位置。 Leaflet Map with modals

这是应该调用名为eventDataModal的第一个模态的JS代码。代码中的主要链接靠近顶部:

L.MarkerControl = L.Control.extend({

    options: {
        position: 'topleft',
        url: 'http://echostorm.mynetgear.com:8080/geoserver/usa/ows',
        userName: 'wfspost',
        password: 'createfeature'
    },

    map: null,
    marker: null,
    binded: false,


    onAdd: function (map) {

        this.map = map;

        var container = L.DomUtil.create('div', 'leaflet-control leaflet-bar'),
            // link = L.DomUtil.create('a', 'glyphicon glyphicon-map-marker', container);
            link = L.DomUtil.create('a', 'glyphicon glyphicon-eye-open', container);

        link.href = '#';
        link.title = 'Create a new visual report';

        link.innerHTML = '';
        L.DomEvent.on(link, 'click', L.DomEvent.stop)
                  .on(link, 'click', L.bind(this.startCreating, this));

        map.on("editable:drawing:commit", L.bind(this.onMarkerAdd, this));                    

        return container;
    },

    startCreating: function() {
        this.marker = this.map.editTools.startMarker();     
    },

    onMarkerAdd: function(e) {
        this.nearestFeature = null;
        this.findNearestPoints(e.layer.getLatLng());
    },

    onFormClosed: function() {
        if (this.marker!=null) {
            this.map.editTools.featuresLayer.removeLayer(this.marker);
            this.marker = null;
        }
    },

    findNearestPoints: function(latlng) {

         var defaultParameters = {
            service: 'WFS',
            version: '1.0.0',
            request: 'GetFeature',
            typeName: 'usa:within5mi',
            maxFeatures: 5,
            outputFormat: 'text/javascript', 
            format_options: 'callback: nearestPoints',
            srsName: 'EPSG:4326',
            viewparams: 'radius:802;lon:'+latlng.lng+';lat:'+latlng.lat,

        };

        var parameters = L.Util.extend(defaultParameters);

        $.ajax({
            jsonp: false,
            url: this.options.url + L.Util.getParamString(parameters),
            dataType: 'jsonp',
            jsonpCallback: 'nearestPoints',
            success: L.bind(this.handleNearestPoints, this)
        });         
    },


    handleNearestPoints: function(data) {           
        this.clearModal();
        this.setModalDate();

        if (data && data.type=="FeatureCollection"
            && data.features && data.features.length
            && data.features.length > 0) {
            this.fillModal(data.features[0]);
        }

        this.openPopup();
    },

    formatDate: function(date) {
        var day = date.getDate();
        return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+(day < 10 ? "0"+day : day);
    },

    clearModal: function() {
        $('#eventDataModal').find('input').each(
            function() {
                $(this).val("");
            }
        );

        $('#eventDataModal').find('textarea').each(
            function() {
                $(this).val("");
            }
        );
    },

    setModalDate: function() {
        $("#form-patrolDate").val(this.formatDate(new Date()));
    },

    fillModal: function(feature) {
        var props = feature.properties;

        $("#form-region").val(props["Region"]);
        $("#form-circuitID").val(props["Circuit_ID"]);
        $("#form-circuitName").val(props["Circuit_Name"]);
        $("#form-vendor").val(props["Vendor"]);
        $("#form-quad").val(props["Quad"]);

    },

    openPopup: function() {
        this.bindEvents();
        $('#eventDataModal').modal('show');
    },

    bindEvents: function() {
        if (!this.binded) {
            $('#eventDataModal').on('hidden.bs.modal', L.bind(this.onFormClosed, this));
            $("#saveEventDataModal").on('click', L.bind(this.onSave, this))
            this.binded = true;
        }
    },

    onSave: function() {
        var properties = this.getFormData(),
            request = this.wfsBody("usa", "usa:pecotest", "geom", properties);
            console.log(request);
            this.makeRequest(request);          
    },

    makeRequest: function(body) {

        $.ajax({
            method: "POST",
            crossDomain: true,
            url: this.options.url,
            contentType: "text/xml",
            dataType: "text",
            data: body,
            headers: {
                "Authorization": "Basic " + btoa(this.options.userName + ":" + this.options.password)
            },
            jsonpCallback: 'datasave',
            success: L.bind(this.onDataSave, this),
            failure: function(r){console.log("AJAX Failure!");console.log(r);}
        });                 
    },

    onDataSave: function(data) {
        console.log(data);
    },

    getFormData: function() {
        var result = {};

        $('#eventDataModal').find('input').each(
            function() {
                if (this.type=="checkbox") {
                    result[this.name] = this.checked ? "Yes" : "No";    
                } else {
                    result[this.name]=$(this).val();
                }
            }
        );

        $('#eventDataModal').find('textarea').each(
            function() {
                result[this.name]=$(this).val();
            }
        );

        return result;
    },

    wfsBody: function(namesapce, typeName, geometryField, properties) {
        var xml;
        xml ='<wfs:Transaction service="WFS" version="1.0.0"';
        xml += ' xmlns:wfs="http://www.opengis.net/wfs"';
        xml += ' xmlns:usa="http://census.gov"';
        xml += ' xmlns:gml="http://www.opengis.net/gml">\n';
        xml += '   <wfs:Insert>\n';
        xml += '      <'+typeName+'>\n';
        xml += '        <usa:'+geometryField+'>\n';
        xml += this.marker.toGML()+"\n";
        xml += '        </usa:'+geometryField+'>\n';

        for (var k in properties) {
            if (properties.hasOwnProperty(k)) {
                xml += "<"+namesapce+":"+k+">";
                xml += properties[k];
                xml += "</"+namesapce+":"+k+">\n";
            }
        }

        xml += '      </'+typeName+'>\n';
        xml += '   </wfs:Insert>\n';
        xml += '</wfs:Transaction>\n';

        return xml;
    }

});

L.Marker.include({
toGML: function(){
    var latlng = this.getLatLng(),
        xml;

    xml = '<gml:Point srsName="EPSG:4326"><gml:coordinates cs=","                
 decimal="." ts=" ">';
    xml += latlng.lng + ',' + latlng.lat;
    xml += '</gml:coordinates></gml:Point>';

    return xml;
 }
});

这是模态evenDataModal

的相应HTML
<div class="modal fade" id="eventDataModal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-         
        label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Create New Visual Report</h4>
      </div>
      <div class="modal-body">
        <form action="php\new_peco_visual_report_db_insert.php                 
            method="POST"><div class="form-group">
            <label for="form-priority">ID Code</label>
            <input type="number" class="form-control" id="form-priority"     
             name="ID_Code" placeholder="ID_Code">
          </div>
          <div class="form-group">
            <label for="form-region">Region</label>
            <input type="text" class="form-control" id="form-region"           
              name="Region" placeholder="region">
          </div>
          <div class="form-group">
            <label for="form-circuitID">Circuit ID</label>
            <input type="number" class="form-control" id="form-circuitID"      
             name="Circuit_ID" placeholder="Circuit ID">
          </div>
          <div class="form-group">
            <label for="form-circuitName">Circuit Name</label>
            <input type="text" class="form-control" id="form-circuitName"      
             name="Circuit_Name" placeholder="Circuit Name">
          </div>
          <div class="form-group">
            <label for="form-patrolDate">Patrol Date</label>
            <input type="date" class="form-control" id="form-patrolDate"       
              name="Patrol_Date" placeholder="Patrol Date">
          </div>                                                                                                            
          <div class="form-group">
            <label for="form-vendor">Vendor</label>
            <input type="text" class="form-control" id="form-vendor"            
             name="Vendor" placeholder="Vendor">
          </div> 
          <div class="form-group">
            <label for="form-repairNumber">Repair Number</label>
            <input type="text" class="form-control" id="form-repairNumber"     
              name="Repair_Number" placeholder="Repair Number">
          </div>                                                                                                
          <div class="form-group">
            <label for="form-problemDescription"> Problem                      
              Description</label>
            <input type="text" class="form-control" id="form-                   
             problemDescription" name="Problem_Description"                                
              placeholder="Problem      
              Description">
          </div>                                                                                               
          <div class="form-group">
            <label for="form-urgency">Priority</label>
            <input type="number" class="form-control" id="form-urgency"         
              name="Urgency" placeholder="Urgency">
          </div>
          <div class="form-group">
            <label for="form-circuitPortion">Circuit Portion</label>
            <input type="text" class="form-control" id="form-circuitPortion"    
              name="Circuit_Portion" placeholder="Circuit Portion">
          </div>
          <div class="form-group">
            <label for="form-quad">Quad</label>
            <input type="text" class="form-control" id="form-quad"                
             name="Quad" placeholder="Quad">
          </div>       
           <div class="form-group">
            <label for="form-location">Location</label>
            <input type="text" class="form-control" id="form-location"                
             name="Location" placeholder="location">
          </div>                                
          <div class="form-group">
            <label for="form-poleSub">PolSub</label>
            <input type="number" class="form-control" id="form-polSub"             
              name="PolSub" placeholder="PolSub">
          </div> 
          <div class="form-group">
            <label for="form-poleNumber">Pole Number</label>
            <input type="number" class="form-control" id="form-poleNumber"    
             name="Pole_Number" placeholder="Pole Number">                                        
          </div>   
          <div class="form-group">
            <label for="form-aerialName">Aerial Name</label>
            <input type="text" class="form-control" id="form-aerialName"      
             name="Aerial_Name" placeholder="Aerial Name">
          </div>
          <div class="form-group">
            <label for="form-workRequired">Work Required?</label>
            <input type="text" class="form-control" id="form-workRequired"     
             name="Work_Required" placeholder="Work Required">
          </div>
          <div class="form-group">
            <label for="form-long">GPS Longitude</label>
            <input type="text" class="form-control" id="form-long"             
             name="long" placeholder="long">
          </div>   
          <div class="form-group">
            <label for="form-lat">GPS Latitude</label>
            <input type="text" class="form-control" id="form-lat" name="lat"       
              placeholder="lat">
          </div>
          <div class="form-group">
            <label for="form-notes">Comments</label>
            <textarea class="form-control" id="form-comments"                   
             name="Comments" rows="3"></textarea>
          </div>         
       </form>                                                                    
    </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-                         
          dismiss="modal">Close</button>
        <button type="sumbit" class="btn btn-primary"                              
          id="saveEventDataModal">Save</button>
      </div>
    </div><!-- /.modal-content -->
 </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

这是应该调用第二个模式的代码,名为evenDataModal2

L.MarkerControl2 = L.Control.extend({

    options: {
        position: 'topleft',
        url: 'http://echostorm.mynetgear.com:8080/geoserver/usa/ows',
        userName: 'wfspost',
        password: 'createfeature'
    },

    map: null,
    marker: null,
    binded: false,


    onAdd: function (map) {

        this.map = map;

        var container = L.DomUtil.create('div', 'leaflet-control leaflet-bar'),
            // link = L.DomUtil.create('a', 'glyphicon glyphicon-map-marker', container);
            link = L.DomUtil.create('a', 'glyphicon glyphicon-fire', container);

        link.href = '#';
        link.title = 'Create a new thermal report';

        link.innerHTML = '';
        L.DomEvent.on(link, 'click', L.DomEvent.stop)
                  .on(link, 'click', L.bind(this.startCreating, this));

        map.on("editable:drawing:commit", L.bind(this.onMarkerAdd, this));                    

        return container;
    },

    startCreating: function() {
        this.marker = this.map.editTools.startMarker();     
    },

    onMarkerAdd: function(e) {
        this.nearestFeature = null;
        this.findNearestPoints(e.layer.getLatLng());
    },

    onFormClosed: function() {
        if (this.marker!=null) {
            this.map.editTools.featuresLayer.removeLayer(this.marker);
            this.marker = null;
        }
    },

    findNearestPoints: function(latlng) {

         var defaultParameters = {
            service: 'WFS',
            version: '1.0.0',
            request: 'GetFeature',
            typeName: 'usa:within5mi',
            maxFeatures: 5,
            outputFormat: 'text/javascript', 
            format_options: 'callback: nearestPoints',
            srsName: 'EPSG:4326',
            viewparams: 'radius:802;lon:'+latlng.lng+';lat:'+latlng.lat,

        };

        var parameters = L.Util.extend(defaultParameters);

        $.ajax({
            jsonp: false,
            url: this.options.url + L.Util.getParamString(parameters),
            dataType: 'jsonp',
            jsonpCallback: 'nearestPoints',
            success: L.bind(this.handleNearestPoints, this)
        });         
    },


    handleNearestPoints: function(data) {           
        this.clearModal();
        this.setModalDate();

        if (data && data.type=="FeatureCollection"
            && data.features && data.features.length
            && data.features.length > 0) {
            this.fillModal(data.features[0]);
        }

        this.openPopup();
    },

    formatDate: function(date) {
        var day = date.getDate();
        return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+(day < 10 ? "0"+day : day);
    },

    clearModal: function() {
        $('#eventDataModal2').find('input').each(
            function() {
                $(this).val("");
            }
        );

        $('#eventDataModal2').find('textarea').each(
            function() {
                $(this).val("");
            }
        );
    },

    setModalDate: function() {
        $("#form-patrolDate").val(this.formatDate(new Date()));
    },

    fillModal: function(feature) {
        var props = feature.properties;

        $("#form-region").val(props["Region"]);
        $("#form-circuitID").val(props["Circuit_ID"]);
        $("#form-circuitName").val(props["Circuit_Name"]);
        $("#form-vendor").val(props["Vendor"]);
        $("#form-quad").val(props["Quad"]);

    },

    openPopup: function() {
        this.bindEvents();
        $('#eventDataModal2').modal('show');
    },

    bindEvents: function() {
        if (!this.binded) {
            $('#eventDataModal2').on('hidden.bs.modal', L.bind(this.onFormClosed, this));
            $("#saveEventDataModal2").on('click', L.bind(this.onSave, this))
            this.binded = true;
        }
    },

    onSave: function() {
        var properties = this.getFormData(),
            request = this.wfsBody("usa", "usa:pecotest", "geom", properties);
            console.log(request);
            this.makeRequest(request);          
    },

    makeRequest: function(body) {

        $.ajax({
            method: "POST",
            crossDomain: true,
            url: this.options.url,
            contentType: "text/xml",
            dataType: "text",
            data: body,
            headers: {
                "Authorization": "Basic " + btoa(this.options.userName + ":" + this.options.password)
            },
            jsonpCallback: 'datasave',
            success: L.bind(this.onDataSave, this),
            failure: function(r){console.log("AJAX Failure!");console.log(r);}
        });                 
    },

    onDataSave: function(data) {
        console.log(data);
    },

    getFormData: function() {
        var result = {};

        $('#eventDataModal2').find('input').each(
            function() {
                if (this.type=="checkbox") {
                    result[this.name] = this.checked ? "Yes" : "No";    
                } else {
                    result[this.name]=$(this).val();
                }
            }
        );

        $('#eventDataModal2').find('textarea').each(
            function() {
                result[this.name]=$(this).val();
            }
        );

        return result;
    },

    wfsBody: function(namesapce, typeName, geometryField, properties) {
        var xml;
        xml ='<wfs:Transaction service="WFS" version="1.0.0"';
        xml += ' xmlns:wfs="http://www.opengis.net/wfs"';
        xml += ' xmlns:usa="http://census.gov"';
        xml += ' xmlns:gml="http://www.opengis.net/gml">\n';
        xml += '   <wfs:Insert>\n';
        xml += '      <'+typeName+'>\n';
        xml += '        <usa:'+geometryField+'>\n';
        xml += this.marker.toGML()+"\n";
        xml += '        </usa:'+geometryField+'>\n';

        for (var k in properties) {
            if (properties.hasOwnProperty(k)) {
                xml += "<"+namesapce+":"+k+">";
                xml += properties[k];
                xml += "</"+namesapce+":"+k+">\n";
            }
        }

        xml += '      </'+typeName+'>\n';
        xml += '   </wfs:Insert>\n';
        xml += '</wfs:Transaction>\n';

        return xml;
    }

});

eventDataModal的相应HTML与上面的html代码完全相同,只是id已更改为eventDataModal2

0 个答案:

没有答案