| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * The TreeStatusViewerApp encapsulates the application's global state, | |
| 7 * and controls its subviews. | |
| 8 * | |
| 9 * @constructor | |
| 10 */ | |
| 11 function TreeStatusViewerApp() { | |
| 12 // Factories for each of the view types. | |
| 13 this.viewFactories_ = { | |
| 14 fragmentation: CreateFragmentationView, | |
| 15 stats: CreateStatsView, | |
| 16 list: CreateListView, | |
| 17 peak: CreatePeakHoursView | |
| 18 }; | |
| 19 | |
| 20 // Views which have been created so far, keyed by their name. | |
| 21 this.liveViews_ = {}; | |
| 22 | |
| 23 // The last list of entries fetched from the server. | |
| 24 this.entries_ = null; | |
| 25 | |
| 26 // The last time range that entries were fetched from the server for. | |
| 27 this.timeRange_ = null; | |
| 28 } | |
| 29 | |
| 30 /** | |
| 31 * This is the main entry point to the application, when the page has | |
| 32 * finished loading. | |
| 33 */ | |
| 34 TreeStatusViewerApp.prototype.OnPageLoaded = function() { | |
| 35 // Look for any parameters in the query string that we recognize, | |
| 36 // and apply them to change the UI accordingly. | |
| 37 // (For example update the "Start date" input box). | |
| 38 this.ApplyQueryParameters(); | |
| 39 | |
| 40 // Trigger fetch of data from server. | |
| 41 this.OnTimeRangeChanged(); | |
| 42 } | |
| 43 | |
| 44 /** | |
| 45 * This method is called whenever the window of time has been changed | |
| 46 * and we need to fetch new data off the server to update the views. | |
| 47 */ | |
| 48 TreeStatusViewerApp.prototype.OnTimeRangeChanged = function() { | |
| 49 var timeRange = this.GetTimeRange(); | |
| 50 | |
| 51 // Invalidate data derived from the previous time range. | |
| 52 this.timeRange = null; | |
| 53 this.entries_ = null; | |
| 54 this.liveViews_ = {}; | |
| 55 | |
| 56 // Use AJAX to fetch the tree status data for the new time range. | |
| 57 // On completion it will call OnDataAvailable() with the data. | |
| 58 this.SetLoadingIndicator("Fetching data..."); | |
| 59 var callback = this.OnDataAvailable.bind(this, timeRange); | |
| 60 DataFetcher.GetTreeStatusEntries(timeRange, callback); | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * This method is called when new data has been received from the server. | |
| 65 */ | |
| 66 TreeStatusViewerApp.prototype.OnDataAvailable = function(timeRange, entries) { | |
| 67 this.SetLoadingIndicator(""); | |
| 68 | |
| 69 // Set the new data as current. | |
| 70 this.timeRange_ = timeRange; | |
| 71 this.entries_ = entries; | |
| 72 | |
| 73 // Force the view to redraw itself using the new data. | |
| 74 this.SwitchToView(this.GetCurrentViewName()); | |
| 75 } | |
| 76 | |
| 77 /** | |
| 78 * Gets the range of time the user is interested in. | |
| 79 * @return {TimeRange} | |
| 80 */ | |
| 81 TreeStatusViewerApp.prototype.GetTimeRange = function() { | |
| 82 var input = document.getElementById('startTime'); | |
| 83 var d = DateUtil.ParseStringToLocalDate(input.value); | |
| 84 if (!d) { | |
| 85 d = new Date(); // Assume current day | |
| 86 d.setHours(0); | |
| 87 d.setMinutes(0); | |
| 88 d.setMinutes(0); | |
| 89 d.setSeconds(0); | |
| 90 d.setMilliseconds(0); | |
| 91 } | |
| 92 | |
| 93 var startTime = d.getTime() + DateUtil.MILLIS_PER_DAY; | |
| 94 | |
| 95 input = document.getElementById('numDays'); | |
| 96 var numDays = parseInt(input.value, 10); | |
| 97 var endTime = startTime - DateUtil.MILLIS_PER_DAY * numDays; | |
| 98 | |
| 99 return new TimeRange(startTime, endTime); | |
| 100 } | |
| 101 | |
| 102 /** | |
| 103 * Gets the current window's URL's query parameters, as a | |
| 104 * dictionary of key/value pairs. | |
| 105 * | |
| 106 * @return {Object} Dictionary of name/value pairs. | |
| 107 */ | |
| 108 function GetQueryParameters() { | |
| 109 var values = {}; | |
| 110 if (window.location.search) { | |
| 111 params = window.location.search.substr(1).split("&"); | |
| 112 for (var i = 0; i < params.length; ++i) { | |
| 113 var parts = params[i].split("="); | |
| 114 if (parts.length == 2) { | |
| 115 values[parts[0]] = decodeURIComponent(parts[1]); | |
| 116 } | |
| 117 } | |
| 118 } | |
| 119 return values; | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * Checks the URL for query parameters, and applies any with meaning to us. | |
| 124 */ | |
| 125 TreeStatusViewerApp.prototype.ApplyQueryParameters = function() { | |
| 126 var formNames = ["startTime", "numDays", "curView"]; | |
| 127 | |
| 128 var params = GetQueryParameters(); | |
| 129 | |
| 130 for (var i = 0; i < formNames.length; ++i) { | |
| 131 var d = document.getElementById(formNames[i]); | |
| 132 if (formNames[i] in params) { | |
| 133 d.value = params[formNames[i]]; | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 /** | |
| 139 * Updates a part of the UI to show we are waiting for stuff to happen. | |
| 140 * @param {string} text The message to display. | |
| 141 */ | |
| 142 TreeStatusViewerApp.prototype.SetLoadingIndicator = function(text) { | |
| 143 var d = document.getElementById("loading"); | |
| 144 d.innerHTML = text; | |
| 145 DomUtil.DisplayNode(d, text != ""); | |
| 146 } | |
| 147 | |
| 148 /** | |
| 149 * Gets the name of the currently active view. | |
| 150 * @return {string} | |
| 151 */ | |
| 152 TreeStatusViewerApp.prototype.GetCurrentViewName = function() { | |
| 153 return document.getElementById("curView").value; | |
| 154 } | |
| 155 | |
| 156 /** | |
| 157 * Sets |viewName| as the active view. | |
| 158 * @param {string} viewName | |
| 159 */ | |
| 160 TreeStatusViewerApp.prototype.SetCurrentViewName = function(viewName) { | |
| 161 document.getElementById("curView").value = viewName; | |
| 162 } | |
| 163 | |
| 164 /** | |
| 165 * Switches |viewName| to be the active view. | |
| 166 * @param {string} viewName | |
| 167 */ | |
| 168 TreeStatusViewerApp.prototype.SwitchToView = function(viewName) { | |
| 169 var prevViewName = this.GetCurrentViewName(); | |
| 170 this.SetCurrentViewName(viewName); | |
| 171 | |
| 172 // Hide the previously active view. | |
| 173 if (this.liveViews_[prevViewName]) { | |
| 174 this.liveViews_[prevViewName].Show(false); | |
| 175 } | |
| 176 | |
| 177 // If a view hasn't been created yet for |viewName|, do so. | |
| 178 if (!this.liveViews_[viewName]) { | |
| 179 this.liveViews_[viewName] = | |
| 180 this.viewFactories_[viewName](this.timeRange_, this.entries_); | |
| 181 } | |
| 182 | |
| 183 // Show the now active view. | |
| 184 this.liveViews_[viewName].Show(true); | |
| 185 } | |
| 186 | |
| 187 /** | |
| 188 * Generic method to change the styling of the view's tab handle to indicate | |
| 189 * whether it is active, and to show/hide its content pane. | |
| 190 * This assumes we have given consistent IDs to the elements. | |
| 191 * | |
| 192 * @param {string} viewName | |
| 193 * @param {boolean} visible | |
| 194 */ | |
| 195 TreeStatusViewerApp.prototype.ShowViewContentAndTabArea = | |
| 196 function(viewName, visible) { | |
| 197 DomUtil.DisplayNode(document.getElementById(viewName + "_container"), | |
| 198 visible); | |
| 199 | |
| 200 var badgeClass = visible ? "viewBadge_selected" : "viewBadge"; | |
| 201 document.getElementById(viewName + "_badge").className = badgeClass; | |
| 202 } | |
| OLD | NEW |