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

Revision 418, 4.9 KB checked in by jdalbey, 7 years ago (diff)

Fix #124

  • 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    Script.events.setEmphasis();  // Set text colors on all events
38
39    var currentEventArray = Script.events.getLastExecutedEvent(readCookie("time"));
40        /* highlight the items in the current event array */
41    for (i=0; i<currentEventArray.length; i++) 
42    {
43                currentEventArray[i].highlight();
44        }
45
46    // Set timer to do this again in one second
47        setTimeout("highlightLatestEvent()", 1000);
48}
49
50/**
51 * Loads the the script tab for the given document.
52 * @pre script.html must be completely loaded
53 * @param aDocument
54 */
55function loadScript(theEvents, theIncidents)
56{
57        Script.incidents = theIncidents;
58        Script.events = theEvents;
59        Script.events.win = document.getElementById("view").contentWindow;
60        Script.events.doc = getDocumentFromFrame('view');
61
62        var html = "";
63       
64        // FOR each Event
65        for (var i = 0; i < Script.events.length; i++)
66        {
67                // add the Event's html
68                html += Script.events[i].html();
69        //console.log(Script.events[i].html());
70        }
71       
72        // display events in iframe
73        getDocumentFromFrame('view').body.innerHTML = html;     
74       
75        // resize iframe to appropriate height
76        resizeIframe();
77        window.onresize = resizeIframe;
78    // This line is causing an error
79        //window.frames['view'].setEvents(Script.events);
80       
81        Script.events.win.scrollTo(0, readCookie('scriptScrollY'));
82       
83        highlightLatestEvent();
84
85}
86
87/**
88 * @param id The id of the frame element.
89 * @return The document of the frame element.
90 */
91function getDocumentFromFrame(id)
92{
93    var frame=document.getElementById(id);
94    var doc=(frame.contentWindow || frame.contentDocument);
95   
96    if (doc.document)doc=doc.document;
97   
98    return doc;
99}
100
101/**
102 * Finds the height of an element relative to the BODY tag.
103 * @param elem The element to find the height of.
104 * @return The y-coordinate of the given element relative to the BODY tag.
105 */
106function pageY(elem) 
107{
108    return elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop;
109}
110
111/**
112 * Resizes the view for the events so that the view's height is at the bottom of its
113 * container.
114 */
115function resizeIframe() 
116{
117    var height = document.documentElement.clientHeight;
118    height -= pageY(document.getElementById('view'));
119    height = (height < 0) ? 0 : height - 10;
120    document.getElementById('view').style.height = height + 'px';
121}
122
123/**
124 * Highlights the text of an input text element after a small delay.
125 * This method was made because using the code "onFocus='this.focus()'" did not work.
126 * @pre textField must have an 'id' attribute
127 * @param textField An input text element.
128 */
129function highlightTextField(textField)
130{
131        setTimeout("document.getElementById('" + textField.id + "').select();", 100);
132}
133
134/**
135 * Formats a two digit textbox. If the text box contains only one digit, then a zero
136 * is appended to the front.
137 * @param textField An input text element.
138 */
139function formatTimeTextfield(textField)
140{
141        // IF the text field contains 1 digits THEN
142        if (textField.value.length == 1)
143        {
144                textField.value = "0".concat(textField.value);
145        }
146}
147
148/**
149 * Scrolls to the event latest executed event if the current simulation time was given
150 * by text fields 'timeTextSeconds', 'timeTextMinutes', 'timeTextHours'.
151 */
152function jumpToTime()
153{
154        var seconds = parseInt(document.getElementById('timeTextSeconds').value, 10);
155        var minutes = parseInt(document.getElementById('timeTextMinutes').value, 10);
156        var hours = parseInt(document.getElementById('timeTextHours').value, 10);
157
158        var lastEvent = Script.events.getLastExecutedEvent(new Time(hours, minutes, seconds).getSeconds());
159
160        // IF an event was executed THEN
161        if (lastEvent != null)
162        {
163                lastEvent.focus();
164        }
165}
166
167/**
168 * Collapses the elements based on the value of the dropdownbox with id 'domain'.
169 */
170function collapse()
171{
172        var selection = document.getElementById('domain');
173        var selectedIndex = selection.selectedIndex;
174        var selectedValue = selection.options[selectedIndex].value;
175       
176        eval("Script.events.collapseAll" + selectedValue + "();");
177}
178
179/**
180 * Expands the elements based on the value of the dropdownbox with id 'domain'.
181 */
182function expand()
183{
184        var selection = document.getElementById('domain');
185        var selectedIndex = selection.selectedIndex;
186        var selectedValue = selection.options[selectedIndex].value;
187       
188        eval("Script.events.expandAll" + selectedValue + "();");
189}
190
Note: See TracBrowser for help on using the repository browser.