source: tmcsimulator/trunk/webapps/cptms/js/harLayer.js @ 413

Revision 413, 5.7 KB checked in by jdalbey, 7 years ago (diff)

cmsLayer.js,harLayer.js modified to use POST requests

Line 
1function initHARbutton()
2{
3    var harBtnDiv = document.getElementById('harButton');
4    map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(harBtnDiv)
5    harBtnDiv.title = 'Click to toggle har view';
6    // Setup the click event listeners to toggle icon display
7    harBtnDiv.addEventListener('click', function()
8    {
9        har_showing = !har_showing;
10        // Determine which button image to show
11        if (har_showing)
12        {
13            pic = "images/btnDepressed_HAR.png"
14            // It's nice when icons become visible that the messages have been refreshed.
15            loadAllharMessages();
16        }
17        else
18        {
19            pic = "images/btnReady_HAR.png"
20        }
21        document.getElementById('harBtnImg').src = pic;
22        // reveal or hide all the icons
23        harLayer.forEach(function(feature)
24        {
25            harLayer.overrideStyle(feature,
26            {
27                visible: har_showing
28            });
29        });
30    });
31}
32function loadHARlayer()
33{
34    harLayer = new google.maps.Data();
35    harLayer.setMap(map);
36    harLayer.loadGeoJson(kHARfile); 
37    harLayer.setStyle(function(feature)
38    {
39        // return the StyleOptions
40        return {
41            icon: iconHARactive,
42            title: feature.getId()+ " " +feature.getProperty("location")+ " " 
43                    + feature.getProperty("street"),
44            visible: false
45        };
46    });
47   
48    harLayer.addListener('click', function(event)
49    {
50        var dialog = document.getElementById('har-dialog');
51        // Note: If the dialog is already being displayed when someone else
52        // updates the message, it won't be reflected in the dialog, until
53        // you close and reopen it.
54        dialog.style.display = 'block';
55        harID = event.feature.getId();
56        // Assign to the hidden field
57        document.getElementById('harID').value = harID;
58        showHARMessage(harID); // note: this is async
59        document.getElementById('har-info-label').innerHTML = "HAR ID: " +
60            harID + "   LOCATION: " + event.feature.getProperty("location")
61            + "  " + event.feature.getProperty("street");
62        // clear input fields
63        document.getElementById('har-msgcontent1').value = "";
64        document.getElementById('har-msgcontent1').focus();
65        var span = document.getElementById('har-close');
66            // When the user clicks on <span> (x), close the modal
67        span.addEventListener('click',function()
68        {
69            hideElementById('har-dialog');
70        });
71    });
72}
73
74    function handleHARsubmit()
75    {
76        // recover the user's response
77        var response1 = document.getElementById('har-msgcontent1').value.trim();
78
79        var newMsg = response1.replace(/;/gi, "");  // remove semicolons from input
80
81        if (newMsg.length == 0)
82        {
83            alert("Nothing to Send ... input fields are empty.");
84        }
85        else
86        {
87            document.getElementById('har-msgdisplay1').value = response1;
88
89            saveHARMessage(response1);
90        }
91    }
92
93    function handleHARclear()
94    {
95        document.getElementById('har-msgdisplay1').value = "";
96        saveHARMessage("");
97    }
98
99    // Save an updated har message to the file
100    function saveHARMessage(outMessage)
101    {
102        // Fetch harID from hidden field where it was put when dialog opened.
103        var harID = document.getElementById('harID').value;
104        HARmessageDict[harID].har.message.phase1.Line1 = outMessage;
105        // Set icon to reflect message state
106        if (outMessage == "")
107        {
108            currentIcon = {icon: iconHARidle};
109        }
110        else
111        {
112            currentIcon = {icon: iconHARactive};
113        }
114        harLayer.overrideStyle(harLayer.getFeatureById(harID), currentIcon)
115        // convert messasge to json string
116        jsonstring = JSON.stringify(Object.values(HARmessageDict));
117        outString = "{\"data\":" + jsonstring + "}";
118
119        //var xhttp = new XMLHttpRequest();
120        //xhttp.open("GET", "../cgi-bin/saveHARmessage.py?msg=" + outString, true);
121        //xhttp.send();
122        // Using POST to send the data
123        var xhr = new XMLHttpRequest();
124        xhr.open("POST", "../cgi-bin/saveHARmessage.py", true);
125        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
126        // send the collected data as JSON
127        xhr.send("msg="+outString);
128    }
129
130    // retrieve the current har message file
131    function showHARMessage(harID)
132    {
133        loadAllharMessages();  // because someone else may have made a recent update
134        // lookup the message for this har ID
135        var message = HARmessageDict[harID].har.message;
136        // show the message in the display
137        document.getElementById('har-msgdisplay1').value = message.phase1.Line1.toUpperCase();
138
139    }
140    function loadAllharMessages()
141    {
142        loadJSON("../dynamicdata/har_messages.json", function(response)
143        {
144            // Parse JSON string into object
145            messagejson = JSON.parse(response);
146            // Add each message to a lookup dictionary
147            for (var i = 0; i < messagejson.data.length; i++)
148            {
149                var item = messagejson.data[i];
150                HARmessageDict[item.har.index] = item;
151                // Set the appropriate icon on the har icon
152                //  if there's currently no message
153                if (item.har.message.phase1.Line1 == "")
154                {
155                    harLayer.overrideStyle(harLayer.getFeatureById(item.har.index), {icon: iconHARidle})
156                }
157                else
158                {
159                    harLayer.overrideStyle(harLayer.getFeatureById(item.har.index), {icon: iconHARactive})
160                }   
161            }
162        });
163    }
164
165
Note: See TracBrowser for help on using the repository browser.