source: tmcsimulator/trunk/webapps/einotebook/script/script.js @ 415

Revision 415, 6.1 KB checked in by jdalbey, 7 years ago (diff)

einotebook fix to #131

  • Property svn:executable set to *
Line 
1/**
2 * Holds references to the events and incidents.
3 */
4function Script()
5{
6        Script.events = null;
7        Script.incidents = null;
8}
9
10/**
11 * Scrolls to the last event executed based on time.
12 */
13function jumpToLastExecutedEvent()
14{       
15        var lastEventArray = Script.events.getLastExecutedEvent(readCookie("time"));
16
17        // IF a current event exists THEN
18        if (lastEventArray != null)
19        {
20        // Move window focus to first event of the current ones
21        var last = lastEventArray.length - 1;
22            lastEventArray[last].focus();
23        }
24        else
25        {
26                alert("No events have been executed yet.");
27        }
28}
29
30/**
31 * Finds the Event that was last executed by based on time and highlights it.
32 * Removes highlighting from old events.
33 * @return
34 */
35function highlightLatestEvent() 
36{
37        var currentEventArray = Script.events.getLastExecutedEvent(readCookie("time"));
38
39        // IF the old event is has not been set THEN
40        if (typeof highlightLatestEvent.oldEventArray == 'undefined')
41        {
42                highlightLatestEvent.oldEventArray = []; /* set up array of old events */
43                var currentEvent = new Object(); 
44                highlightLatestEvent.oldEventArray.push(currentEvent);
45                highlightLatestEvent.oldEventArray[0].id = Event.invalidID;
46        }
47
48        // IF a current event exists THEN
49        if (currentEventArray !== null)
50        {
51                var found = false; /* check if a current event is found in the old event array */
52
53                /* traverse the array of current event to check
54                if there is event already in the old event array */
55                // IF there is a new current event THEN
56                for (j=0; j<highlightLatestEvent.oldEventArray.length; j++) {
57                        var oldEvent = highlightLatestEvent.oldEventArray[j];
58                        found = false;
59                        for (i=0; i<currentEventArray.length; i++) {
60                                var currentEvent = currentEventArray[i];
61                                if (oldEvent.id === currentEvent.id)
62                                {
63                                        found = true;   
64                                        break;         
65                                } 
66                        }
67                        /* unhighlight all the events that are no longer current */
68                        if (!found && oldEvent.id !== Event.invalidID) {
69                                oldEvent.unhighlight();
70                        }               
71                }
72
73                /* highlight the current event in the array */
74                for (i=0; i<currentEventArray.length; i++) {
75                        currentEventArray[i].highlight();
76                }
77
78                /* update the old events array with current event array */
79                highlightLatestEvent.oldEventArray = currentEventArray;
80        }
81
82    // Set timer to do this again in one second
83        setTimeout("highlightLatestEvent()", 1000);
84}
85
86/**
87 * Loads the the script tab for the given document.
88 * @pre script.html must be completely loaded
89 * @param aDocument
90 */
91function loadScript(theEvents, theIncidents)
92{
93        Script.incidents = theIncidents;
94        Script.events = theEvents;
95        Script.events.win = document.getElementById("view").contentWindow;
96        Script.events.doc = getDocumentFromFrame('view');
97
98        var html = "";
99       
100        // FOR each Event
101        for (var i = 0; i < Script.events.length; i++)
102        {
103                // add the Event's html
104                html += Script.events[i].html();
105        //console.log(Script.events[i].html());
106        }
107       
108        // display events in iframe
109        getDocumentFromFrame('view').body.innerHTML = html;     
110       
111        // resize iframe to appropriate height
112        resizeIframe();
113        window.onresize = resizeIframe;
114    // This line is causing an error
115        //window.frames['view'].setEvents(Script.events);
116       
117        Script.events.win.scrollTo(0, readCookie('scriptScrollY'));
118       
119        highlightLatestEvent();
120
121}
122
123/**
124 * @param id The id of the frame element.
125 * @return The document of the frame element.
126 */
127function getDocumentFromFrame(id)
128{
129    var frame=document.getElementById(id);
130    var doc=(frame.contentWindow || frame.contentDocument);
131   
132    if (doc.document)doc=doc.document;
133   
134    return doc;
135}
136
137/**
138 * Finds the height of an element relative to the BODY tag.
139 * @param elem The element to find the height of.
140 * @return The y-coordinate of the given element relative to the BODY tag.
141 */
142function pageY(elem) 
143{
144    return elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop;
145}
146
147/**
148 * Resizes the view for the events so that the view's height is at the bottom of its
149 * container.
150 */
151function resizeIframe() 
152{
153    var height = document.documentElement.clientHeight;
154    height -= pageY(document.getElementById('view'));
155    height = (height < 0) ? 0 : height - 10;
156    document.getElementById('view').style.height = height + 'px';
157}
158
159/**
160 * Highlights the text of an input text element after a small delay.
161 * This method was made because using the code "onFocus='this.focus()'" did not work.
162 * @pre textField must have an 'id' attribute
163 * @param textField An input text element.
164 */
165function highlightTextField(textField)
166{
167        setTimeout("document.getElementById('" + textField.id + "').select();", 100);
168}
169
170/**
171 * Formats a two digit textbox. If the text box contains only one digit, then a zero
172 * is appended to the front.
173 * @param textField An input text element.
174 */
175function formatTimeTextfield(textField)
176{
177        // IF the text field contains 1 digits THEN
178        if (textField.value.length == 1)
179        {
180                textField.value = "0".concat(textField.value);
181        }
182}
183
184/**
185 * Scrolls to the event latest executed event if the current simulation time was given
186 * by text fields 'timeTextSeconds', 'timeTextMinutes', 'timeTextHours'.
187 */
188function jumpToTime()
189{
190        var seconds = parseInt(document.getElementById('timeTextSeconds').value, 10);
191        var minutes = parseInt(document.getElementById('timeTextMinutes').value, 10);
192        var hours = parseInt(document.getElementById('timeTextHours').value, 10);
193
194        var lastEvent = Script.events.getLastExecutedEvent(new Time(hours, minutes, seconds).getSeconds());
195
196        // IF an event was executed THEN
197        if (lastEvent != null)
198        {
199                lastEvent.focus();
200        }
201}
202
203/**
204 * Collapses the elements based on the value of the dropdownbox with id 'domain'.
205 */
206function collapse()
207{
208        var selection = document.getElementById('domain');
209        var selectedIndex = selection.selectedIndex;
210        var selectedValue = selection.options[selectedIndex].value;
211       
212        eval("Script.events.collapseAll" + selectedValue + "();");
213}
214
215/**
216 * Expands the elements based on the value of the dropdownbox with id 'domain'.
217 */
218function expand()
219{
220        var selection = document.getElementById('domain');
221        var selectedIndex = selection.selectedIndex;
222        var selectedValue = selection.options[selectedIndex].value;
223       
224        eval("Script.events.expandAll" + selectedValue + "();");
225}
226
Note: See TracBrowser for help on using the repository browser.