Web Inspector: Make closing ContentViews more leak proof
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / ContentFlowDOMTreeContentView.js
1 /*
2  * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above
9  *    copyright notice, this list of conditions and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above
12  *    copyright notice, this list of conditions and the following
13  *    disclaimer in the documentation and/or other materials
14  *    provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 WebInspector.ContentFlowDOMTreeContentView = function(contentFlow)
31 {
32     console.assert(contentFlow);
33
34     WebInspector.DOMTreeContentView.call(this, contentFlow);
35
36     contentFlow.addEventListener(WebInspector.ContentFlow.Event.ContentNodeWasAdded, this._contentNodeWasAdded, this);
37     contentFlow.addEventListener(WebInspector.ContentFlow.Event.ContentNodeWasRemoved, this._contentNodeWasRemoved, this);
38
39     this._createContentTrees();
40 };
41
42 WebInspector.ContentFlowDOMTreeContentView.prototype = {
43     constructor: WebInspector.ContentFlowDOMTreeContentView,
44     __proto__: WebInspector.DOMTreeContentView.prototype,
45
46     // Public
47
48     closed: function()
49     {
50         this.representedObject.removeEventListener(null, null, this);
51
52         WebInspector.DOMTreeContentView.prototype.closed.call(this);
53     },
54
55     getSearchContextNodes: function(callback)
56     {
57         callback(this.domTreeOutline.children.map(function(treeOutline) {
58             return treeOutline.representedObject.id;
59         }));
60     },
61
62     // Private
63
64     _createContentTrees: function()
65     {
66         var contentNodes = this.representedObject.contentNodes;
67         for (var contentNode of contentNodes)
68             this.domTreeOutline.appendChild(new WebInspector.DOMTreeElement(contentNode));
69
70         var documentURL = contentNodes.length ? contentNodes[0].ownerDocument.documentURL : null;
71         this._restoreSelectedNodeAfterUpdate(documentURL, contentNodes[0]);
72     },
73
74     _contentNodeWasAdded: function(event)
75     {
76         var treeElement = new WebInspector.DOMTreeElement(event.data.node);
77         if (!event.data.before) {
78             this.domTreeOutline.appendChild(treeElement);
79             return;
80         }
81
82         var beforeElement = this.domTreeOutline.findTreeElement(event.data.before);
83         console.assert(beforeElement);
84
85         var index = this.domTreeOutline.children.indexOf(beforeElement);
86         console.assert(index !== -1);
87
88         this.domTreeOutline.insertChild(treeElement, index);
89     },
90
91     _contentNodeWasRemoved: function(event)
92     {
93         var treeElement = this.domTreeOutline.findTreeElement(event.data.node);
94         console.assert(treeElement);
95         this.domTreeOutline.removeChild(treeElement);
96     }
97 };