source: tmcsimulator/trunk/webapps/einotebook/scripts/Events.js @ 468

Revision 468, 8.3 KB checked in by jdalbey, 7 years ago (diff)

einotebook - multifile commit. Redesign to fix timing problem on some machines as well as improve file naming. Fixed #176.

  • Property svn:executable set to *
Line 
1/**
2 * Stores the each Event. Maintains each Event sorted ascending by time.
3 *
4 * You must set
5 *   1) events.doc to the document that events will be displayed
6 *   2) events.win to the window on which the document resides that will display the events
7 * before the 'html' method of Event, Evaluations, Evaluation, Properties, or Property
8 * has been called.
9 */
10var events = new Array();
11
12//========== public members ==========//
13events.doc = null;
14events.win = null;
15
16//========== public methods ==========//
17events.get = events_get;
18events.getProperties = events_getProperties;
19events.getEvaluations = events_getEvaluations;
20events.getEvaluation = events_getEvaluation;
21events.collapseAllEvents = events_collapseAllEvents;
22events.collapseAllProperties = events_collapseAllProperties;
23events.collapseAllEvaluations = events_collapseAllEvaluations;
24events.expandAllEvents = events_expandAllEvents;
25events.expandAllProperties = events_expandAllProperties;
26events.expandAllEvaluations = events_expandAllEvaluations;
27events.setEmphasis = events_setEmphasis;
28events.expandAll = events_expandAll;
29events.collapseAll = events_collapseAll;
30events.add = events_add;
31events.getAllProperties = events_getAllProperties;
32events.getAllEvaluations = events_getAllEvaluations;
33events.getLastExecutedEvent = events_getLastExecutedEvent;
34
35//========== method definitions ==========//
36
37/**
38 * Searches through each Event to find an Event with the given id.
39 * @param id {Integer} The id of an Event.
40 * @return The Event with the given id.
41 */
42function events_get(id)
43{
44    var event;
45
46    // FOR each Event
47    for (var i = 0; i < this.length; i++)
48    {
49        // IF the Event's id matches THEN
50        if (this[i].id == id)
51        {
52                // remember the Event
53            event = this[i];
54        }
55    }
56   
57    return event;
58}
59
60/**
61 * Searches through the Properties of each event to find the Property with the given id.
62 * @param id {Integer} The id of a Property.
63 * @return The Property with the given id.
64 */
65function events_getProperties(id)
66{
67    var property;
68   
69    // FOR each Event
70    for (var i = 0; i < this.length; i++)
71    {
72        // IF the Event's id matches THEN
73        if (this[i].properties.id == id)
74        {
75                // remember the Property
76            property = this[i].properties;
77        }
78    }
79   
80    return property;
81}
82
83/**
84 * Searches through each Evaluations of each event to find the Evaluations with the given id.
85 * @param id {Integer} The id of an Evaluations.
86 * @return The Evaluations with the given id.
87 */
88function events_getEvaluations(id)
89{
90    var evaluations;
91
92    // FOR each Event
93    for (var i = 0; i < this.length; i++)
94    {
95        // IF this Evalutions's id matches THEN
96        if (this[i].evaluations.id == id)
97        {
98                // remember the Evaluations
99            evaluations = this[i].evaluations; 
100        }
101    }
102   
103    return evaluations;
104}
105
106/**
107 * Searches through each Evaluation of each Evaluations of each Event to find the
108 * Evaluation of with the given id.
109 * @param id {Integer} The id of an Evaluation.
110 * @return The Evaluation of with the given id.
111 */
112function events_getEvaluation(id)
113{
114    var evaluation;
115
116    // FOR each Event
117    for (var i = 0; i < this.length; i++)
118    {
119        // FOR each Evaluation
120        for (var j = 0; j < this[i].evaluations.evaluations.length; j++)
121        {
122                // IF the Evaluation's id matches THEN
123            if (this[i].evaluations.evaluations[j].id == id)
124            {
125                // remember the Evaluation
126                evaluation = this[i].evaluations.evaluations[j];
127            }
128        }
129    }
130   
131    return evaluation; 
132}
133
134/**
135 * Collapses each Event.
136 */
137function events_collapseAllEvents()
138{
139        // FOR each Event
140        for (var i = 0; i < this.length; i++)
141        {
142                // IF the Event is expanded THEN
143                if (this[i].expanded)
144                {
145                        // collapse Event
146                        this[i].expandAction();
147                }
148        }
149}
150
151/**
152 * Expands each event.
153 */
154function events_expandAllEvents()
155{
156        // FOR each Event
157        for (var i = 0; i < this.length; i++)
158        {
159                // IF the Event is collapsed
160                if (!this[i].expanded)
161                {
162                        // expand Event
163                        this[i].expandAction();
164                }
165        }
166}
167
168function events_collapseAllProperties()
169{
170        var list = this.getAllProperties();
171       
172        // FOR each Properties
173        for (var i = 0; i < list.length; i++)
174        {
175                // IF the Properties is expanded THEN
176                if (list[i].expanded)
177                {
178                        // collapse the Properties
179                        list[i].expandAction();
180                }
181        }
182}
183
184function events_collapseAllEvaluations()
185{
186        var list = this.getAllEvaluations();
187       
188        // FOR each Evaluations
189        for (var i = 0; i < list.length; i++)
190        {
191                // IF the Evaluations is expanded THEN
192                if (list[i].expanded)
193                {
194                        // collapse the Evaluations
195                        list[i].expandAction();
196                }
197        }       
198}
199
200function events_expandAllProperties()
201{
202        var list = this.getAllProperties();
203       
204        // FOR each Properties
205        for (var i = 0; i < list.length; i++)
206        {
207                // IF the Properties is collapsed THEN
208                if (!list[i].expanded)
209                {
210                        // expand the Properties
211                        list[i].expandAction();
212                }
213        }
214}
215
216function events_expandAllEvaluations()
217{
218        var list = this.getAllEvaluations();
219       
220        // FOR each Evaluations
221        for (var i = 0; i < list.length; i++)
222        {
223                // IF the Evaluations is collapsed THEN
224                if (!list[i].expanded)
225                {
226                        // expand the Evaluations
227                        list[i].expandAction();
228                }
229        }       
230}
231
232/**
233 * Adds an Event.
234 * @param event {Event} The Event to add.
235 */
236function events_add(event)
237{
238        // add event
239        events.push(event);
240       
241        // sort events in ascending order by time
242        events.sort(function (e1, e2) {
243                return e1.time.getSeconds() - e2.time.getSeconds();
244        });
245}
246
247/**
248 * Finds all of the Properties in each Event.
249 * @return The list of the Properties in each Event.
250 */
251function events_getAllProperties()
252{
253        var list = new Array();
254       
255        // FOR each event
256        for (var i = 0; i < this.length; i++)
257        {
258                // add properties to list
259                list.push(this[i].properties);
260        }
261       
262        return list;
263}
264
265/**
266 * Finds all of the Evaluations in each Event.
267 * @return The list of the Evaluations in each Event.
268 */
269function events_getAllEvaluations()
270{
271        var list = new Array();
272       
273        // FOR each event
274        for (var i = 0; i < this.length; i++)
275        {
276                // add evaluations to list
277                list.push(this[i].evaluations);
278        }
279       
280        return list;
281}
282
283/**
284 * Collapses each Event, Properties, and Evaluations.
285 */
286function events_collapseAll()
287{
288        events.collapseAllProperties(); 
289        events.collapseAllEvaluations(); 
290        events.collapseAllEvents();     
291}
292
293/**
294 * Expands each Event, Properties, and Evaluations.
295 * @return
296 */
297function events_expandAll()
298{
299        events.expandAllProperties(); 
300        events.expandAllEvaluations(); 
301        events.expandAllEvents();       
302}
303
304/** Set emphasis for all events. 
305    Change color of text of expired events so they appear disabled, and future
306    events appear normal.
307  */
308function events_setEmphasis()
309{
310    var currentTime = readCookie("time");
311    // FOR each Event
312    for (var i = 0; i < this.length - 1; i++)
313    {
314          // Change color of those before current time
315      if (this[i].time.getSeconds() <= currentTime) 
316      {
317              this[i].unhighlight();
318      }
319      else
320      {
321              this[i].normalize(); // normal colors for everything else
322      }         
323   }
324}
325
326/**
327 * Finds the Event that was last executed by based on given time.
328 * @param time The time in seconds.
329 * @return The array of last execute event if at least one event has executed; otherwise, null.
330 */
331function events_getLastExecutedEvent(time)
332{
333        // var event = null;
334        var event_arr = [];
335        if (this.length == 0)
336    {
337        console.log("Error: event list is empty in getLastExecutedEvent");
338        return;
339    }
340        // IF there is only one event and it has been executed THEN
341        if (this.length == 1 && this[0].time.getSeconds() <= time)
342        {
343                event.push(this[0]);
344        }
345        else
346        {
347                //NOTE: remember that the events are maintained in ascending order by time
348               
349                // FOR each Event
350                for (var i = 0; i < this.length - 1; i++)
351                {
352                        // IF the Event has executed and no subsequent event has executed THEN
353                        if (this[i].time.getSeconds() <= time &&
354                            this[i + 1].time.getSeconds() > time)
355                        {
356                                event_arr.push(this[i]);
357                                /* traverse backward to check if there is an event happen at the same time
358                                with the current event */
359                                var k = i - 1;
360                                while ( k>=0 && (this[k].time.getSeconds() == this[i].time.getSeconds())) {
361                                        event_arr.push(this[k]);
362                                        k--;
363                                }
364                        }
365                }
366               
367                // IF the last event has executed THEN
368                if (this[this.length - 1].time.getSeconds() <= time)
369                {
370                        event_arr.push(this[this.length - 1]);
371                }       
372        }
373       
374        // return an array of event
375        return event_arr;
376}
377
Note: See TracBrowser for help on using the repository browser.