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

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

Add EINotebook source

  • Property svn:executable set to *
Line 
1/**
2 * Represents an event.
3 * @param time {Time}
4 * @param incident {Incident}
5 * @param properties {Properties}
6 * @param properties {Evaluations}
7 */
8function Event(time, incident, properties, evaluations)
9{
10        //========== private static members ==========//
11        Event.invalidID = -1;
12       
13        //========== private static members ==========//
14        Event.id = (typeof Event.id == 'undefined') ? 0 : ++Event.id;
15       
16        //========== public constants ==========//
17        this.id = Event.id;
18        this.expandOptionID = "eventExpandOption" + this.id;
19        this.dataID = "eventData" + this.id;
20        this.eventHeaderID = "eventHeader" + this.id;
21       
22        //========== public read-only members ==========//
23        this.expanded = true;
24        this.highlighted = false;
25        this.time = time;
26        this.incident = incident;
27        this.evaluations = evaluations;
28        this.properties = properties;
29       
30        //========== public methods ==========//
31        this.html = html;
32        this.expandAction = expandAction;
33        this.highlight = highlight;
34        this.unhighlight = unhighlight;
35        this.focus = focus;
36       
37        //========== private methods ==========//
38        this.expandOptionSymbol = expandOptionSymbol;
39        this.headerHTML = headerHTML;
40        this.expandOptionHTML = expandOptionHTML;
41        this.refresh = refresh;
42        this.dataHTML = dataHTML;
43        this.expandAction = expandAction;
44        this.html = html;
45
46    // Color constants (TODO: assign incident numbers dynamically)
47    var incidentColor = {
48187: "PowderBlue",   
49188: "#afcfaf",        // DarkSeaGreen
50189: "#ffe680",        // CornSilk
51190: "MistyRose", 
52191: "Moccasin" }
53
54       
55        //========== function definitions ===========//
56        /**
57         * Returns the symbol for the expand/collapse option.
58         * @return "+" or "-" depending on whether this event is collapsed or expanded.
59         */
60        function expandOptionSymbol() 
61        { 
62                return this.expanded ? "–" : "+"; 
63        }
64         
65        /**
66         * @return The rectangle containing the expand/collapse symbol,
67         *         incident time, incident number and
68         *         incident title that this event belongs to.
69         */
70        function headerHTML()
71        {
72        if (this.incident == undefined)
73        {
74            console.log(this.time.format());
75        }
76                return "<table class='eventHeader'>" +
77                                "<tr class='eventHeader'>" +
78                                "<td class='eventExpandOption'>" + this.expandOptionHTML() + "</td>" +
79                                "<td class='eventTime'>Time: " + this.time.format() + "</td>" +
80                                "<td class='eventNumber'>" + this.incident.number + "</td>" +
81                                "<td class='eventTitle'>" + this.incident.title + "</td>" + 
82                                "</tr>" +
83                                "</table>";
84        }
85       
86        /**
87         * @return The html of the expand/collapse symbol.
88         */
89        function expandOptionHTML()
90        {
91        return "&nbsp;"
92/*  In 2019 we don't think we want expand option any longer
93                return "<button class='eventExpandButton' id='" + this.expandOptionID +
94                                "' onclick='events.get(" +
95                                this.id + ").expandAction();'>" + this.expandOptionSymbol()
96                                + "</button>";
97*/
98        }
99
100        /**
101         * Updates the expand option symbol and the visibility of the event data depending on
102         * whether this event is collapsed or expanded.
103         */
104        function refresh()
105        {
106                events.doc.getElementById(this.dataID).innerHTML = 
107                        this.expanded ? this.dataHTML() : "";
108                       
109                // IF the event is highlighted THEN
110                if (this.highlighted)
111                {
112                        this.highlight();
113                }
114                else
115                {
116                        this.unhighlight();
117                }
118                       
119                events.doc.getElementById(this.expandOptionID).innerHTML = 
120                        this.expandOptionSymbol();
121        }
122       
123        /**
124         * @return The html for the properties and the evaluations.
125         */
126        function dataHTML()
127        {
128                return properties.html() + evaluations.html();
129        }
130
131        /**
132         * Expands or collapses this event depending on whether this event is
133         * already expanded or collapsed.
134         */
135        function expandAction() 
136        { 
137                this.expanded = !this.expanded; 
138                this.refresh(); 
139        }
140
141        /**
142         * @return The html representation of this event.
143         */
144        function html()
145        {
146        //console.log("building html for event: ", this.eventHeaderID, incidentColor[this.incident.number]);
147
148                return "<table class='event'>" +
149                           "<tr>" +
150                           "<td class='eventHeader' style='background-color:" + incidentColor[this.incident.number] + "'" +
151               "id='" + this.eventHeaderID + "'>" + 
152                              this.headerHTML() + "</td>" +
153                           "</tr>" +
154                           "<tr>" + 
155                           "<td class='eventData' style='background-color:" + incidentColor[this.incident.number] + "' id='" + this.dataID + "'>" + 
156                           (this.expanded ? this.dataHTML() : "") + 
157                           "</td>" +
158                           "</tr>" +
159                           "</table>";
160        }
161       
162        /**
163         * Scrolls the window to this Event.
164         * @param window The window to scroll.
165         */
166        function focus()
167        {
168                /* This method was discarded because it moved the scroll bar of the parent of
169                 * the given window in addition to the scroll bar of the given window..
170                var positionOfPound = window.location.indexOf("#");
171                var rootLocation = window.location.substring(0, positionOfPound);
172                window.location = rootLocation + "#" + eventAnchorName;
173                */
174                events.win.scrollTo(0, absoluteTop(events.doc.getElementById(this.eventHeaderID)));
175        }
176       
177        /**
178         * Highlights this Event.
179         */
180        function highlight()
181        {
182                this.highlighted = true;
183                events.doc.getElementById(this.eventHeaderID).style.borderColor = "blue";
184                events.doc.getElementById(this.eventHeaderID).style.backgroundColor = "yellow";
185               
186                // IF this event is expanded THEN
187                if (this.expanded) 
188                {
189                        events.doc.getElementById(this.dataID).style.border = "2px solid blue";
190                }
191                else
192                {
193                        events.doc.getElementById(this.dataID).style.border = "none";
194                }
195               
196        }
197       
198        /**
199         * Unhighlights this event.
200         * @return
201         */
202        function unhighlight()
203        {
204                this.highlighted = false;
205                events.doc.getElementById(this.eventHeaderID).style.backgroundColor = "white";
206                events.doc.getElementById(this.eventHeaderID).style.borderColor = "gainsboro";
207                events.doc.getElementById(this.eventHeaderID).style.color = "gray";
208                events.doc.getElementById(this.dataID).style.border = "0px solid red";
209                events.doc.getElementById(this.dataID).style.backgroundColor = "white";
210                events.doc.getElementById(this.dataID).style.color = "gray";
211        }
212       
213        /**
214         * Returns the number of pixels from the top of the given element to the top
215         * of the body element.
216         * @param elem The element whose y-coordinate will be found.
217         * @return The y-coordinate of the given element.
218         */
219        function absoluteTop(elem)
220        {
221                var offset = elem.offsetTop;
222                var parent = elem.offsetParent;
223               
224                if (parent.tagName == "BODY")
225                {
226                        return offset;
227                }
228                else
229                {
230                        return offset + absoluteTop(parent);
231                }
232        }
233}
Note: See TracBrowser for help on using the repository browser.