source: tmcsimulator/trunk/webapps/js/harLayer.js @ 333

Revision 333, 5.4 KB checked in by jdalbey, 7 years ago (diff)

Update cptms code to reflect image name changes.

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        // clear input fields
62        document.getElementById('har-msgcontent1').value = "";
63        document.getElementById('har-msgcontent1').focus();
64        var span = document.getElementById('har-close');
65            // When the user clicks on <span> (x), close the modal
66        span.addEventListener('click',function()
67        {
68            handleDialogClose('har-dialog');
69        });
70    });
71}
72
73    function handleHARsubmit()
74    {
75        // recover the user's response
76        var response1 = document.getElementById('har-msgcontent1').value.trim();
77
78        var newMsg = response1;
79        if (newMsg.length == 0)
80        {
81            alert("Nothing to Send ... input fields are empty.");
82        }
83        else
84        {
85            document.getElementById('har-msgdisplay1').value = response1;
86
87            saveHARMessage(response1);
88        }
89    }
90
91    function handleHARclear()
92    {
93        document.getElementById('har-msgdisplay1').value = "";
94        saveMessage("");
95    }
96
97    // Save an updated har message to the file
98    function saveHARMessage(outMessage)
99    {
100        // Fetch harID from hidden field where it was put when dialog opened.
101        var harID = document.getElementById('harID').value;
102        HARmessageDict[harID].har.message.phase1.Line1 = outMessage;
103
104        // TODO Set icon to reflect message state
105        if (outMessage == "")
106        {
107        }
108        else
109        {
110        }
111        //harLayer.overrideStyle(harLayer.getFeatureById(harID), currentIcon)
112        // break the json string into lines for readability
113        jsonstring = JSON.stringify(Object.values(HARmessageDict));
114        outString = "{\"data\":" + jsonstring + "}";
115
116        var xhttp = new XMLHttpRequest();
117        xhttp.open("GET", "cgi-bin/saveHARmessage.py?msg=" + outString, true);
118        xhttp.send();
119        // Using POST might be a better idea ... haven't tried this yet
120        //      var xhr = new XMLHttpRequest();
121        //      xhr.open("POST", "/cgi-bin/saveMessage.py?", true);
122        //      xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
123        // send the collected data as JSON
124        //      xhr.send(JSON.stringify(messageList));
125    }
126
127    // retrieve the current har message file
128    function showHARMessage(harID)
129    {
130        loadAllharMessages();  // because someone else may have made a recent update
131        // lookup the message for this har ID
132        var message = HARmessageDict[harID].har.message;
133        // show the message in the display
134        document.getElementById('har-msgdisplay1').value = message.phase1.Line1;
135
136    }
137    function loadAllharMessages()
138    {
139        loadJSON("har_messages.json", function(response)
140        {
141            // Parse JSON string into object
142            messagejson = JSON.parse(response);
143            // Add each message to a lookup dictionary
144            for (var i = 0; i < messagejson.data.length; i++)
145            {
146                var item = messagejson.data[i];
147                HARmessageDict[item.har.index] = item;
148                // TODO Set the appropriate icon on the har icon
149                //  if there's currently no message
150                if (item.har.message.phase1.Line1 == "")
151                {
152                }
153                else
154                {
155                }
156            }
157        });
158    }
159
160
Note: See TracBrowser for help on using the repository browser.