OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @constructor | 6 * @constructor |
7 * @extends {WebInspector.Widget} | 7 * @extends {WebInspector.Widget} |
8 * @implements {WebInspector.TargetManager.Observer} | 8 * @implements {WebInspector.TargetManager.Observer} |
9 * @param {function():number} getWidthCallback | 9 * @param {function():number} getWidthCallback |
10 * @param {function(number)} setWidthCallback | 10 * @param {function(number)} setWidthCallback |
11 */ | 11 */ |
12 WebInspector.MediaQueryInspector = function(getWidthCallback, setWidthCallback) | 12 WebInspector.MediaQueryInspector = function(getWidthCallback, setWidthCallback) |
13 { | 13 { |
14 WebInspector.Widget.call(this, true); | 14 WebInspector.Widget.call(this, true); |
15 this.registerRequiredCSS("emulation/mediaQueryInspector.css"); | 15 this.registerRequiredCSS("emulation/mediaQueryInspector.css"); |
16 this.contentElement.classList.add("media-inspector-view", "media-inspector-v
iew-empty"); | 16 this.contentElement.classList.add("media-inspector-view"); |
17 this.contentElement.addEventListener("click", this._onMediaQueryClicked.bind
(this), false); | 17 this.contentElement.addEventListener("click", this._onMediaQueryClicked.bind
(this), false); |
18 this.contentElement.addEventListener("contextmenu", this._onContextMenu.bind
(this), false); | 18 this.contentElement.addEventListener("contextmenu", this._onContextMenu.bind
(this), false); |
19 this._mediaThrottler = new WebInspector.Throttler(0); | 19 this._mediaThrottler = new WebInspector.Throttler(0); |
20 | 20 |
21 this._getWidthCallback = getWidthCallback; | 21 this._getWidthCallback = getWidthCallback; |
22 this._setWidthCallback = setWidthCallback; | 22 this._setWidthCallback = setWidthCallback; |
23 this._offset = 0; | 23 this._offset = 0; |
24 this._scale = 1; | 24 this._scale = 1; |
25 this._lastReportedCount = 0; | |
26 | 25 |
27 WebInspector.targetManager.observeTargets(this); | 26 WebInspector.targetManager.observeTargets(this); |
28 | |
29 WebInspector.zoomManager.addEventListener(WebInspector.ZoomManager.Events.Zo
omChanged, this._renderMediaQueries.bind(this), this); | 27 WebInspector.zoomManager.addEventListener(WebInspector.ZoomManager.Events.Zo
omChanged, this._renderMediaQueries.bind(this), this); |
30 } | 28 } |
31 | 29 |
32 /** | 30 /** |
33 * @enum {number} | 31 * @enum {number} |
34 */ | 32 */ |
35 WebInspector.MediaQueryInspector.Section = { | 33 WebInspector.MediaQueryInspector.Section = { |
36 Max: 0, | 34 Max: 0, |
37 MinMax: 1, | 35 MinMax: 1, |
38 Min: 2 | 36 Min: 2 |
39 } | 37 } |
40 | 38 |
41 WebInspector.MediaQueryInspector.Events = { | |
42 CountUpdated: "CountUpdated" | |
43 } | |
44 | |
45 WebInspector.MediaQueryInspector.prototype = { | 39 WebInspector.MediaQueryInspector.prototype = { |
46 /** | 40 /** |
47 * @override | 41 * @override |
48 * @param {!WebInspector.Target} target | 42 * @param {!WebInspector.Target} target |
49 */ | 43 */ |
50 targetAdded: function(target) | 44 targetAdded: function(target) |
51 { | 45 { |
52 // FIXME: adapt this to multiple targets. | 46 // FIXME: adapt this to multiple targets. |
53 if (this._cssModel) | 47 if (this._cssModel) |
54 return; | 48 return; |
(...skipping 28 matching lines...) Expand all Loading... |
83 setAxisTransform: function(offset, scale) | 77 setAxisTransform: function(offset, scale) |
84 { | 78 { |
85 if (this._offset === offset && Math.abs(this._scale - scale) < 1e-8) | 79 if (this._offset === offset && Math.abs(this._scale - scale) < 1e-8) |
86 return; | 80 return; |
87 this._offset = offset; | 81 this._offset = offset; |
88 this._scale = scale; | 82 this._scale = scale; |
89 this._renderMediaQueries(); | 83 this._renderMediaQueries(); |
90 }, | 84 }, |
91 | 85 |
92 /** | 86 /** |
93 * @param {boolean} enabled | |
94 */ | |
95 setEnabled: function(enabled) | |
96 { | |
97 this._enabled = enabled; | |
98 this._scheduleMediaQueriesUpdate(); | |
99 }, | |
100 | |
101 /** | |
102 * @param {!Event} event | 87 * @param {!Event} event |
103 */ | 88 */ |
104 _onMediaQueryClicked: function(event) | 89 _onMediaQueryClicked: function(event) |
105 { | 90 { |
106 var mediaQueryMarker = event.target.enclosingNodeOrSelfWithClass("media-
inspector-marker"); | 91 var mediaQueryMarker = event.target.enclosingNodeOrSelfWithClass("media-
inspector-marker"); |
107 if (!mediaQueryMarker) | 92 if (!mediaQueryMarker) |
108 return; | 93 return; |
109 | 94 |
110 var model = mediaQueryMarker._model; | 95 var model = mediaQueryMarker._model; |
111 if (model.section() === WebInspector.MediaQueryInspector.Section.Max) { | 96 if (model.section() === WebInspector.MediaQueryInspector.Section.Max) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 /** | 143 /** |
159 * @param {!WebInspector.UILocation} location | 144 * @param {!WebInspector.UILocation} location |
160 */ | 145 */ |
161 _revealSourceLocation: function(location) | 146 _revealSourceLocation: function(location) |
162 { | 147 { |
163 WebInspector.Revealer.reveal(location); | 148 WebInspector.Revealer.reveal(location); |
164 }, | 149 }, |
165 | 150 |
166 _scheduleMediaQueriesUpdate: function() | 151 _scheduleMediaQueriesUpdate: function() |
167 { | 152 { |
168 if (!this._enabled) | 153 if (!this.isShowing()) |
169 return; | 154 return; |
170 this._mediaThrottler.schedule(this._refetchMediaQueries.bind(this)); | 155 this._mediaThrottler.schedule(this._refetchMediaQueries.bind(this)); |
171 }, | 156 }, |
172 | 157 |
173 _refetchMediaQueries: function() | 158 _refetchMediaQueries: function() |
174 { | 159 { |
175 if (!this._enabled || !this._cssModel) | 160 if (!this.isShowing() || !this._cssModel) |
176 return Promise.resolve(); | 161 return Promise.resolve(); |
177 | 162 |
178 return this._cssModel.mediaQueriesPromise() | 163 return this._cssModel.mediaQueriesPromise() |
179 .then(this._rebuildMediaQueries.bind(this)) | 164 .then(this._rebuildMediaQueries.bind(this)) |
180 }, | 165 }, |
181 | 166 |
182 /** | 167 /** |
183 * @param {!Array.<!WebInspector.MediaQueryInspector.MediaQueryUIModel>} mod
els | 168 * @param {!Array.<!WebInspector.MediaQueryInspector.MediaQueryUIModel>} mod
els |
184 * @return {!Array.<!WebInspector.MediaQueryInspector.MediaQueryUIModel>} | 169 * @return {!Array.<!WebInspector.MediaQueryInspector.MediaQueryUIModel>} |
185 */ | 170 */ |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 * @return {number} | 213 * @return {number} |
229 */ | 214 */ |
230 function compareModels(model1, model2) | 215 function compareModels(model1, model2) |
231 { | 216 { |
232 return model1.compareTo(model2); | 217 return model1.compareTo(model2); |
233 } | 218 } |
234 }, | 219 }, |
235 | 220 |
236 _renderMediaQueries: function() | 221 _renderMediaQueries: function() |
237 { | 222 { |
238 if (!this._cachedQueryModels) | 223 if (!this._cachedQueryModels || !this.isShowing()) |
239 return; | 224 return; |
240 | 225 |
241 var markers = []; | 226 var markers = []; |
242 var lastMarker = null; | 227 var lastMarker = null; |
243 for (var i = 0; i < this._cachedQueryModels.length; ++i) { | 228 for (var i = 0; i < this._cachedQueryModels.length; ++i) { |
244 var model = this._cachedQueryModels[i]; | 229 var model = this._cachedQueryModels[i]; |
245 if (lastMarker && lastMarker.model.dimensionsEqual(model)) { | 230 if (lastMarker && lastMarker.model.dimensionsEqual(model)) { |
246 lastMarker.locations.push(model.rawLocation()); | 231 lastMarker.locations.push(model.rawLocation()); |
247 lastMarker.active = lastMarker.active || model.active(); | 232 lastMarker.active = lastMarker.active || model.active(); |
248 } else { | 233 } else { |
249 lastMarker = { | 234 lastMarker = { |
250 active: model.active(), | 235 active: model.active(), |
251 model: model, | 236 model: model, |
252 locations: [ model.rawLocation() ] | 237 locations: [ model.rawLocation() ] |
253 }; | 238 }; |
254 markers.push(lastMarker); | 239 markers.push(lastMarker); |
255 } | 240 } |
256 } | 241 } |
257 | 242 |
258 if (markers.length !== this._lastReportedCount) { | |
259 this._lastReportedCount = markers.length; | |
260 this.dispatchEventToListeners(WebInspector.MediaQueryInspector.Event
s.CountUpdated, markers.length); | |
261 } | |
262 | |
263 if (!this.isShowing()) | |
264 return; | |
265 | |
266 var oldChildrenCount = this.contentElement.children.length; | |
267 this.contentElement.removeChildren(); | 243 this.contentElement.removeChildren(); |
268 | 244 |
269 var container = null; | 245 var container = null; |
270 for (var i = 0; i < markers.length; ++i) { | 246 for (var i = 0; i < markers.length; ++i) { |
271 if (!i || markers[i].model.section() !== markers[i - 1].model.sectio
n()) | 247 if (!i || markers[i].model.section() !== markers[i - 1].model.sectio
n()) |
272 container = this.contentElement.createChild("div", "media-inspec
tor-marker-container"); | 248 container = this.contentElement.createChild("div", "media-inspec
tor-marker-container"); |
273 var marker = markers[i]; | 249 var marker = markers[i]; |
274 var bar = this._createElementFromMediaQueryModel(marker.model); | 250 var bar = this._createElementFromMediaQueryModel(marker.model); |
275 bar._model = marker.model; | 251 bar._model = marker.model; |
276 bar._locations = marker.locations; | 252 bar._locations = marker.locations; |
277 bar.classList.toggle("media-inspector-marker-inactive", !marker.acti
ve); | 253 bar.classList.toggle("media-inspector-marker-inactive", !marker.acti
ve); |
278 container.appendChild(bar); | 254 container.appendChild(bar); |
279 } | 255 } |
280 }, | 256 }, |
281 | 257 |
282 /** | 258 /** |
283 * @return {number} | 259 * @return {number} |
284 */ | 260 */ |
285 _zoomFactor: function() | 261 _zoomFactor: function() |
286 { | 262 { |
287 return WebInspector.zoomManager.zoomFactor() / this._scale; | 263 return WebInspector.zoomManager.zoomFactor() / this._scale; |
288 }, | 264 }, |
289 | 265 |
290 wasShown: function() | 266 wasShown: function() |
291 { | 267 { |
292 this._renderMediaQueries(); | 268 this._scheduleMediaQueriesUpdate(); |
293 }, | 269 }, |
294 | 270 |
295 /** | 271 /** |
296 * @param {!WebInspector.MediaQueryInspector.MediaQueryUIModel} model | 272 * @param {!WebInspector.MediaQueryInspector.MediaQueryUIModel} model |
297 * @return {!Element} | 273 * @return {!Element} |
298 */ | 274 */ |
299 _createElementFromMediaQueryModel: function(model) | 275 _createElementFromMediaQueryModel: function(model) |
300 { | 276 { |
301 var zoomFactor = this._zoomFactor(); | 277 var zoomFactor = this._zoomFactor(); |
302 var minWidthValue = model.minWidthExpression() ? model.minWidthExpressio
n().computedLength() : 0; | 278 var minWidthValue = model.minWidthExpression() ? model.minWidthExpressio
n().computedLength() : 0; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 }, | 460 }, |
485 | 461 |
486 /** | 462 /** |
487 * @return {boolean} | 463 * @return {boolean} |
488 */ | 464 */ |
489 active: function() | 465 active: function() |
490 { | 466 { |
491 return this._active; | 467 return this._active; |
492 } | 468 } |
493 } | 469 } |
OLD | NEW |