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

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

einotebook fix to #131

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