1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
WebInspector.ContentViewContainer = function(element)
{
WebInspector.Object.call(this);
this._element = element || document.createElement("div");
this._element.classList.add(WebInspector.ContentViewContainer.StyleClassName);
this._backForwardList = [];
this._currentIndex = -1;
};
WebInspector.ContentViewContainer.StyleClassName = "content-view-container";
WebInspector.ContentViewContainer.Event = {
CurrentContentViewDidChange: "content-view-container-current-content-view-did-change"
};
WebInspector.ContentViewContainer.prototype = {
constructor: WebInspector.ContentViewContainer,
// Public
get element()
{
return this._element;
},
get currentIndex()
{
return this._currentIndex;
},
get backForwardList()
{
return this._backForwardList;
},
get currentContentView()
{
if (this._currentIndex < 0 || this._currentIndex > this._backForwardList.length - 1)
return null;
return this._backForwardList[this._currentIndex];
},
updateLayout: function()
{
var currentContentView = this.currentContentView;
if (currentContentView)
currentContentView.updateLayout();
},
contentViewForRepresentedObject: function(representedObject, onlyExisting)
{
console.assert(representedObject);
if (!representedObject)
return null;
// Iterate over all the known content views for the representedObject (if any) and find one that doesn't
// have a parent container or has this container as its parent.
var contentView = null;
for (var i = 0; representedObject.__contentViews && i < representedObject.__contentViews.length; ++i) {
var currentContentView = representedObject.__contentViews[i];
if (!currentContentView._parentContainer || currentContentView._parentContainer === this) {
contentView = currentContentView;
break;
}
}
console.assert(!contentView || contentView instanceof WebInspector.ContentView);
if (contentView instanceof WebInspector.ContentView)
return contentView;
// Return early to avoid creating a new content view when onlyExisting is true.
if (onlyExisting)
return null;
try {
// No existing content view found, make a new one.
contentView = new WebInspector.ContentView(representedObject);
} catch (e) {
console.error(e);
return null;
}
// Remember this content view for future calls.
if (!representedObject.__contentViews)
representedObject.__contentViews = [];
representedObject.__contentViews.push(contentView);
return contentView;
},
showContentViewForRepresentedObject: function(representedObject)
{
var contentView = this.contentViewForRepresentedObject(representedObject);
if (!contentView)
return null;
this.showContentView(contentView);
return contentView;
},
showContentView: function(contentView)
{
console.assert(contentView instanceof WebInspector.ContentView);
if (!(contentView instanceof WebInspector.ContentView))
return null;
// Don't allow showing a content view that is already associated with another container.
// Showing a content view that is already associated with this container is allowed.
console.assert(!contentView.parentContainer || contentView.parentContainer === this);
if (contentView.parentContainer && contentView.parentContainer !== this)
return null;
// Don't do anything if the content view is already the current content view.
var currentContentView = this.currentContentView;
if (currentContentView === contentView)
return contentView;
// Showing a content view will truncate the back/forward list after the current index and insert the content view
// at the end of the list. Finally, the current index will be updated to point to the end of the back/forward list.
// Increment the current index to where we will insert the content view.
var newIndex = this._currentIndex + 1;
// Insert the content view at the new index. This will remove any content views greater than or equal to the index.
var removedItems = this._backForwardList.splice(newIndex, this._backForwardList.length - newIndex, contentView);
console.assert(newIndex === this._backForwardList.length - 1);
console.assert(this._backForwardList[newIndex] === contentView);
// Disassociate with the removed content views.
for (var i = 0; i < removedItems.length; ++i) {
// Skip disassociation if this content view is still in the back/forward list.
if (this._backForwardList.contains(contentView))
continue;
this._disassociateFromContentView(removedItems[i]);
}
// Associate with the new content view.
contentView._parentContainer = this;
this.showBackForwardEntry(newIndex);
return contentView;
},
showBackForwardEntry: function(index)
{
console.assert(index >= 0 && index <= this._backForwardList.length - 1);
if (index < 0 || index > this._backForwardList.length - 1)
return;
if (this._currentIndex === index)
return;
// Hide the currently visible content view.
var currentContentView = this.currentContentView;
if (currentContentView)
this._hideContentView(currentContentView);
this._currentIndex = index;
this._showContentView(this.currentContentView);
this.dispatchEventToListeners(WebInspector.ContentViewContainer.Event.CurrentContentViewDidChange);
},
replaceContentView: function(oldContentView, newContentView)
{
console.assert(oldContentView instanceof WebInspector.ContentView);
if (!(oldContentView instanceof WebInspector.ContentView))
return;
console.assert(newContentView instanceof WebInspector.ContentView);
if (!(newContentView instanceof WebInspector.ContentView))
return;
console.assert(oldContentView.parentContainer === this);
if (oldContentView.parentContainer !== this)
return;
console.assert(!newContentView.parentContainer || newContentView.parentContainer === this);
if (newContentView.parentContainer && newContentView.parentContainer !== this)
return;
var currentlyShowing = (this.currentContentView === oldContentView);
if (currentlyShowing)
this._hideContentView(oldContentView);
// Disassociate with the old content view.
this._disassociateFromContentView(oldContentView);
// Associate with the new content view.
newContentView._parentContainer = this;
// Replace all occurrences of oldContentView with newContentView in the back/forward list.
for (var i = 0; i < this._backForwardList.length; ++i) {
if (this._backForwardList[i] === oldContentView)
this._backForwardList[i] = newContentView;
}
if (currentlyShowing) {
this._showContentView(newContentView);
this.dispatchEventToListeners(WebInspector.ContentViewContainer.Event.CurrentContentViewDidChange);
}
},
closeAllContentViewsOfPrototype: function(constructor)
{
if (!this._backForwardList.length) {
console.assert(this._currentIndex === -1);
return;
}
// Do a check to see if all the content views are instances of this prototype.
// If they all are we can use the quicker closeAllContentViews method.
var allSamePrototype = true;
for (var i = this._backForwardList.length - 1; i >= 0; --i) {
if (!(this._backForwardList[i] instanceof constructor)) {
allSamePrototype = false;
break;
}
}
if (allSamePrototype) {
this.closeAllContentViews();
return;
}
var oldCurrentContentView = this.currentContentView;
// Hide and disassociate with all the content views that are instances of the constructor.
for (var i = this._backForwardList.length - 1; i >= 0; --i) {
var contentView = this._backForwardList[i];
if (!(contentView instanceof constructor))
continue;
if (contentView === oldCurrentContentView)
this._hideContentView(contentView);
if (this._currentIndex >= i) {
// Decrement the currentIndex since we will remove an item in the back/forward array
// that it the current index or comes before it.
--this._currentIndex;
}
this._disassociateFromContentView(contentView);
// Remove the item from the back/forward list.
this._backForwardList.splice(i, 1);
}
var currentContentView = this.currentContentView;
console.assert(currentContentView || (!currentContentView && this._currentIndex === -1));
if (currentContentView && currentContentView !== oldCurrentContentView) {
this._showContentView(currentContentView);
this.dispatchEventToListeners(WebInspector.ContentViewContainer.Event.CurrentContentViewDidChange);
}
},
closeAllContentViews: function()
{
if (!this._backForwardList.length) {
console.assert(this._currentIndex === -1);
return;
}
// Hide and disassociate with all the content views.
for (var i = 0; i < this._backForwardList.length; ++i) {
var contentView = this._backForwardList[i];
if (i === this._currentIndex)
this._hideContentView(contentView);
this._disassociateFromContentView(contentView);
}
this._backForwardList = [];
this._currentIndex = -1;
this.dispatchEventToListeners(WebInspector.ContentViewContainer.Event.CurrentContentViewDidChange);
},
canGoBack: function()
{
return this._currentIndex > 0;
},
canGoForward: function()
{
return this._currentIndex < this._backForwardList.length - 1;
},
goBack: function()
{
if (!this.canGoBack())
return;
this.showBackForwardEntry(this._currentIndex - 1);
},
goForward: function()
{
if (!this.canGoForward())
return;
this.showBackForwardEntry(this._currentIndex + 1);
},
shown: function()
{
var currentContentView = this.currentContentView;
if (!currentContentView)
return;
this._showContentView(currentContentView);
},
hidden: function()
{
var currentContentView = this.currentContentView;
if (!currentContentView)
return;
this._hideContentView(currentContentView);
},
// Private
_addContentViewElement: function(contentView)
{
if (contentView.element.parentNode !== this._element)
this._element.appendChild(contentView.element);
},
_removeContentViewElement: function(contentView)
{
if (contentView.element.parentNode)
contentView.element.parentNode.removeChild(contentView.element);
},
_disassociateFromContentView: function(contentView)
{
console.assert(!contentView.visible);
contentView._parentContainer = null;
var representedObject = contentView.representedObject;
if (!representedObject || !representedObject.__contentViews)
return;
representedObject.__contentViews.remove(contentView);
contentView.closed();
},
_saveScrollPositionsForContentView: function(contentView)
{
var scrollableElements = contentView.scrollableElements || [];
for (var i = 0; i < scrollableElements.length; ++i) {
var element = scrollableElements[i];
if (!element)
continue;
if (contentView.shouldKeepElementsScrolledToBottom)
element._savedIsScrolledToBottom = element.isScrolledToBottom();
element._savedScrollTop = element.scrollTop;
element._savedScrollLeft = element.scrollLeft;
}
},
_restoreScrollPositionsForContentView: function(contentView)
{
var scrollableElements = contentView.scrollableElements || [];
for (var i = 0; i < scrollableElements.length; ++i) {
var element = scrollableElements[i];
if (!element)
continue;
// Restore the top scroll position by either scrolling to the bottom or to the saved position.
element.scrollTop = element._savedIsScrolledToBottom ? element.scrollHeight : element._savedScrollTop;
// Don't restore the left scroll position when scrolled to the bottom. This way the when content changes
// the user won't be left in a weird horizontal position.
element.scrollLeft = element._savedIsScrolledToBottom ? 0 : element._savedScrollLeft;
}
},
_showContentView: function(contentView)
{
if (contentView.visible)
return;
this._addContentViewElement(contentView);
this._prepareContentViewToShow(contentView);
},
_prepareContentViewToShow: function(contentView)
{
this._restoreScrollPositionsForContentView(contentView);
contentView.visible = true;
contentView.shown();
contentView.updateLayout();
},
_hideContentView: function(contentView)
{
if (!contentView.visible)
return;
this._prepareContentViewToHide(contentView);
this._removeContentViewElement(contentView);
},
_prepareContentViewToHide: function(contentView)
{
contentView.visible = false;
contentView.hidden();
this._saveScrollPositionsForContentView(contentView);
}
};
WebInspector.ContentViewContainer.prototype.__proto__ = WebInspector.Object.prototype;
|