Organize WebInspectorUI/UserInterface into sub-directories.
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / ResourceContentView.js
1 /*
2  * Copyright (C) 2013 Apple Inc. 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.ResourceContentView = function(resource, styleClassName)
27 {
28     console.assert(resource);
29     console.assert(styleClassName);
30
31     WebInspector.ContentView.call(this, resource);
32
33     this._resource = resource;
34
35     this.element.classList.add(WebInspector.ResourceContentView.StyleClassName);
36     this.element.classList.add(styleClassName);
37
38     // Append a spinner while waiting for contentAvailable. The subclasses are responsible for removing
39     // the spinner before showing the resource content.
40     var spinner = new WebInspector.IndeterminateProgressSpinner;
41     this.element.appendChild(spinner.element);
42
43     this.element.addEventListener("click", this._mouseWasClicked.bind(this), false);
44
45     // Request content last so the spinner will always be removed in case the content is immediately available.
46     resource.requestContent(this._contentAvailable.bind(this));
47
48     if (!this.managesOwnIssues) {
49         WebInspector.issueManager.addEventListener(WebInspector.IssueManager.Event.IssueWasAdded, this._issueWasAdded, this);
50
51         var issues = WebInspector.issueManager.issuesForSourceCode(resource);
52         for (var i = 0; i < issues.length; ++i)
53             this.addIssue(issues[i]);
54     }
55 };
56
57 WebInspector.ResourceContentView.StyleClassName = "resource";
58
59 WebInspector.ResourceContentView.prototype = {
60     constructor: WebInspector.ResourceContentView,
61
62     // Public
63
64     get resource()
65     {
66         return this._resource;
67     },
68
69     contentAvailable: function(content, base64Encoded)
70     {
71         // Implemented by subclasses.
72     },
73
74     addIssue: function(issue)
75     {
76         // This generically shows only the last issue, subclasses can override for better handling.
77         this.element.removeChildren();
78         this.element.appendChild(WebInspector.createMessageTextView(issue.text, issue.level === WebInspector.IssueMessage.Level.Error));
79     },
80
81     closed: function()
82     {
83         if (!this.managesOwnIssues)
84             WebInspector.issueManager.removeEventListener(WebInspector.IssueManager.Event.IssueWasAdded, this._issueWasAdded, this);
85     },
86
87     // Private
88
89     _contentAvailable: function(resource, content, base64Encoded)
90     {
91         // Check for failed loads.
92         if (this.resource.failed) {
93             // Don't show an error message if there is already an error message showing (like one added by addIssue.)
94             if (this.element.querySelector(".message-text-view.error"))
95                 return;
96
97             this.element.removeChildren();
98             this.element.appendChild(WebInspector.createMessageTextView(WebInspector.UIString("An error occurred trying to load the resource."), true));
99             return;
100         }
101
102         // Content is ready to show, call the public method now.
103         this.contentAvailable(content, base64Encoded);
104     },
105
106     _issueWasAdded: function(event)
107     {
108         console.assert(!this.managesOwnIssues);
109
110         var issue = event.data.issue;
111
112         // FIXME: Check more than just the issue URL (the document could have multiple resources with the same URL).
113         if (issue.url !== this.resource.url)
114             return;
115
116         this.addIssue(issue);
117     },
118
119     _mouseWasClicked: function(event)
120     {
121         WebInspector.handlePossibleLinkClick(event, this.resource.parentFrame);
122     }
123 };
124
125 WebInspector.ResourceContentView.prototype.__proto__ = WebInspector.ContentView.prototype;