source: tmcsimulator/trunk/webapps/EInotebook/scripts/LoadEvents.js @ 359

Revision 359, 5.0 KB checked in by jdalbey, 7 years ago (diff)

Add EINotebook source

Line 
1var xmlDoc;
2function loadJSON(inFile, callback)
3{
4    var xobj = new XMLHttpRequest();
5    // Assume XML unless filename ends with .json
6    if (inFile.endsWith(".json"))
7    {
8        xobj.overrideMimeType("application/json");
9    }
10    xobj.open('GET', inFile, true);
11    xobj.onreadystatechange = function()
12    {
13        if (xobj.readyState == 4 && xobj.status == "200")
14        {
15            callback(xobj.responseText);
16        }
17    };
18    // We want ajax to ignore any cached responses
19    xobj.setRequestHeader('If-Modified-Since', 'Sat, 01 Jan 2000 01:01:01 GMT')
20    xobj.send(null);
21}
22
23function parseXml(response)
24{
25        var parser = new DOMParser();
26        xmlDoc = parser.parseFromString(response,"text/xml");
27        var eventTags = xmlDoc.getElementsByTagName("SCRIPT_EVENT");
28
29        // Process each event tag
30        for (var i = 0; i < eventTags.length; i++)
31        {
32            var currEvt = eventTags[i];
33            // Process all the children of one event
34            var timeFields; var evtTime; var incidentNum; var cadProp; var telProp;
35            var proparray = new Array();
36            var evalarray = new Array();
37            for (var child = 1; child < currEvt.childNodes.length; child++)
38            {
39                if (currEvt.childNodes[child].localName != undefined)
40                {
41                    switch(currEvt.childNodes[child].localName)
42                    {
43                        case "TIME_INDEX": 
44                            timeFields = currEvt.childNodes[child].textContent.split(":"); 
45                            evtTime = new Time(Number(timeFields[0]), Number(timeFields[1]), Number(timeFields[2]));
46                            break;
47                        case "INCIDENT":
48                            incidentNum = Number(currEvt.childNodes[child].attributes["LogNum"].value); 
49                            break;
50                        case "CAD_DATA": 
51                            var caddata = parseCAD(currEvt.childNodes[child]); 
52                            if (caddata.length > 0)
53                            {
54                                cadProp = new Property("CHP CAD", caddata ); 
55                                proparray.push(cadProp);
56                            }
57                            break;
58                        case "TELEPHONE": 
59                            telProp = new Evaluation("Telephone Conversation", 
60                                     parseTelephone(currEvt.childNodes[child]) ); 
61                            evalarray.push(telProp);
62                            break;
63                        case "CHP_RADIO": 
64                            var chpradio = parseCHPradio(currEvt.childNodes[child]); 
65                            if (chpradio.length > 0)
66                            {
67                                cadProp = new Property("CHP RADIO", chpradio ); 
68                                proparray.push(cadProp);
69                            }
70                            break;
71                        case "TMT_RADIO":  break;
72                        case "MAINTENANCE_RADIO": break;
73                        // case *_EVALUATION: break;
74
75                    }
76                }
77            }
78            //console.log(evtTime.format(), incidentNum, proparray.length, evalarray.length);
79            if (incidentNum != undefined)
80            {
81              events.add(new Event(evtTime, incidents.get(incidentNum), new Properties(proparray), 
82                    new Evaluations(evalarray)) );
83            }
84        }
85
86}
87function parseCAD(element)
88{
89    var result = new Array();
90    var details = element.getElementsByTagName("DETAIL");
91    if (details.length > 0)
92    {
93        for (detail in details)
94        {
95            if (details[detail].textContent != undefined)
96            {               
97                result.push("Detail:");
98                result.push(details[detail].textContent);
99            }
100        }
101    }
102    return result;
103}
104function parseTelephone(element)
105{
106    var result = new Array();
107    for (var child = 1; child < element.childNodes.length; child++)
108    {
109        if (element.childNodes[child].localName != undefined)
110        {
111            if (element.childNodes[child].localName == "INSTRUCTOR")
112            {
113                result.push(element.childNodes[child].attributes["Role"].value);
114            }
115            else
116            {
117                result.push(element.childNodes[child].localName);   
118            }
119            result.push(element.childNodes[child].textContent);
120        }
121    }
122    return result;
123}
124function parseCHPradio(element)
125{
126    var result = new Array();
127    var dialog = element.getElementsByTagName("LINE");
128    if (dialog.length > 0)
129    {
130        for (line in dialog)
131        {
132            if (dialog[line].textContent != undefined)
133            {               
134                result.push(dialog[line].attributes["Role"].value);
135                result.push(dialog[line].textContent);
136            }
137        }
138    }
139    return result;
140}
141
142// MAIN
143loadJSON("full_script.xml", parseXml)
144// Note: the script has the Media Log event removed
145
Note: See TracBrowser for help on using the repository browser.