OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 var RenderManager = (function () { |
| 7 "use strict"; |
| 8 |
| 9 |
| 10 function timer(millis) { |
| 11 var lastTime = (new Date()).getTime(); |
| 12 |
| 13 // A function that if called will return true |
| 14 // if |millis| time has passed. If so, it resets |
| 15 // to be used again. |
| 16 var toReturn = function () { |
| 17 var curTime = (new Date()).getTime(); |
| 18 if (curTime - lastTime >= millis) { |
| 19 lastTime = curTime; |
| 20 return true; |
| 21 } |
| 22 |
| 23 return false; |
| 24 }; |
| 25 |
| 26 // Forces the next call to the timer to succeeded. |
| 27 toReturn.trigger = function () { |
| 28 lastTime -= millis; |
| 29 return toReturn; |
| 30 }; |
| 31 |
| 32 // The timer is reset. |
| 33 toReturn.postpone = function () { |
| 34 lastTime = (new Date()).getTime(); |
| 35 return toReturn; |
| 36 }; |
| 37 |
| 38 // The original time that the timer is set for. |
| 39 toReturn.time = millis; |
| 40 |
| 41 return toReturn; |
| 42 } |
| 43 |
| 44 // A painter is a class that has three methods: |
| 45 // paintNew(playerInfo, div, properties) |
| 46 // paintExist(playerInfo, div, properties) |
| 47 // invalidate(playerInfo, properties) |
| 48 // where |playerInfo| is a PlayerInfo instance, |div| |
| 49 // is the div that is going to be rendered into, and |
| 50 // |properties| is an object containing other properties |
| 51 // that the painter might want. |
| 52 function RenderManager(playerManager) { |
| 53 // A model is an object that contains the following properties |
| 54 this.models = { |
| 55 log: { |
| 56 // The name of the model (used for errors and such) |
| 57 name: "log", |
| 58 // The div that the model can modify anything inside of |
| 59 div: document.querySelector("#log"), |
| 60 // The element (if any) that could be hiding the |div| |
| 61 collapse: document.querySelector("#log-wrapper>.collapse"), |
| 62 // If the element needs to be completely rewritten from scratch |
| 63 dirty: true, |
| 64 // An object containing functions that |
| 65 // write html to the inside of |div| |
| 66 painter: logPainter, |
| 67 // A timer for how often it should be redrawn |
| 68 timer: timer(500).trigger() |
| 69 }, |
| 70 graph: { |
| 71 name: "graph", |
| 72 div: document.querySelector("#graph"), |
| 73 collapse: document.querySelector("#graph-wrapper>.collapse"), |
| 74 dirty: true, |
| 75 painter: graphPainter, |
| 76 timer: timer(100).trigger() |
| 77 }, |
| 78 prop: { |
| 79 name: "prop", |
| 80 div: document.querySelector("#properties"), |
| 81 collapse: null, |
| 82 dirty: true, |
| 83 painter: propertyPainter, |
| 84 timer: timer(200).trigger() |
| 85 }, |
| 86 playerList: { |
| 87 name: "player list", |
| 88 div: document.querySelector("#stream"), |
| 89 collapse: null, |
| 90 dirty: true, |
| 91 painter: playerListPainter, |
| 92 timer: timer(1000).trigger() |
| 93 } |
| 94 }; |
| 95 |
| 96 // All the models that contain information about a Player. |
| 97 // Non-standard models include the player-list and the system properties |
| 98 this.standardModels = [this.models.log, this.models.graph, |
| 99 this.models.prop]; |
| 100 |
| 101 // The id of the current player |
| 102 this.cachedId = -1; |
| 103 // The playerInfo instance of the current player |
| 104 this.current = null; |
| 105 // The playerManager that owns this class |
| 106 this.playerManager = playerManager; |
| 107 |
| 108 // The text being used to filter logs |
| 109 this.filterText = ""; |
| 110 |
| 111 // If the user is scrolling through the log, don't update it. |
| 112 this.models.log.div.parentNode.onscroll = function () { |
| 113 this.models.log.timer.postpone(); |
| 114 }.bind(this); |
| 115 |
| 116 // When <enter> is pressed inside the input, set |this.filterText|. |
| 117 var filterField = document.querySelector("#filter-input"); |
| 118 filterField.onkeydown = function (event) { |
| 119 // 13 is the keyCode for <enter> |
| 120 if (event.keyCode === 13) { |
| 121 this.filterText = filterField.value; |
| 122 // We need to trigger a repaint immediately |
| 123 this.models.log.dirty = true; |
| 124 this.models.log.timer.trigger(); |
| 125 this.update(); |
| 126 } |
| 127 }.bind(this); |
| 128 } |
| 129 |
| 130 // Checks to see if a collapsible div is open or not |
| 131 function isOpen(div) { |
| 132 // If there is no parent, we have to assume that |
| 133 // this is viewable |
| 134 if (!div) { |
| 135 return true; |
| 136 } |
| 137 |
| 138 return div.className.indexOf("open") !== -1; |
| 139 } |
| 140 |
| 141 // Ran when a player is selected in the playerList |
| 142 RenderManager.prototype.select = function (id) { |
| 143 // If we are already viewing it, don't do anything |
| 144 if (this.cachedId === id) { |
| 145 return; |
| 146 } |
| 147 this.cachedId = id; |
| 148 this.current = this.playerManager.players[id]; |
| 149 |
| 150 // All the models need to be reset and triggered so that they |
| 151 // are redrawn completely and immediately; |
| 152 this.standardModels.forEach(function (m) { |
| 153 m.dirty = true; |
| 154 m.timer.trigger(); |
| 155 }); |
| 156 this.update(); |
| 157 }; |
| 158 |
| 159 // Ran whenever a property is updated |
| 160 RenderManager.prototype.update = function () { |
| 161 // To get passed into the painters |
| 162 var extra = {filterText: this.filterText}; |
| 163 |
| 164 // If we are actually looking at something |
| 165 if (this.current) { |
| 166 this.standardModels.forEach(function (model) { |
| 167 if (model.timer() && isOpen(model.collapse)) { |
| 168 if (model.dirty) { |
| 169 console.warn("writing " + model.name + |
| 170 " from scratch."); |
| 171 model.painter.paintNew(this.current, model.div, extra); |
| 172 model.dirty = false; |
| 173 } else { |
| 174 model.painter.paintExist(this.current, |
| 175 model.div, extra); |
| 176 } |
| 177 } |
| 178 }, this); |
| 179 } |
| 180 }; |
| 181 |
| 182 // Redraws the playerList |
| 183 RenderManager.prototype.redrawList = function () { |
| 184 var m = this.models.playerList; |
| 185 var prop = {playerManager: this.playerManager}; |
| 186 |
| 187 m.painter.paintNew(null, m.div, prop); |
| 188 }; |
| 189 |
| 190 return RenderManager; |
| 191 }()); |
OLD | NEW |