initial import
[vuplus_webkit] / Source / WebCore / inspector / front-end / ResourceView.js
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc.  All rights reserved.
3  * Copyright (C) IBM Corp. 2009  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 WebInspector.ResourceView = function(resource)
31 {
32     WebInspector.View.call(this);
33     this.element.addStyleClass("resource-view");
34     this.resource = resource;
35 }
36
37 WebInspector.ResourceView.prototype = {
38     hasContent: function()
39     {
40         return false;
41     }
42 }
43
44 WebInspector.ResourceView.prototype.__proto__ = WebInspector.View.prototype;
45
46 WebInspector.ResourceView.hasTextContent = function(resource)
47 {
48     switch (resource.category) {
49     case WebInspector.resourceCategories.documents:
50     case WebInspector.resourceCategories.scripts:
51     case WebInspector.resourceCategories.xhr:
52     case WebInspector.resourceCategories.stylesheets:
53         return true;
54     case WebInspector.resourceCategories.other:
55         return resource.content && !resource.contentEncoded;
56     default:
57         return false;
58     }
59 }
60
61 WebInspector.ResourceView.nonSourceViewForResource = function(resource)
62 {
63     switch (resource.category) {
64     case WebInspector.resourceCategories.images:
65         return new WebInspector.ImageView(resource);
66     case WebInspector.resourceCategories.fonts:
67         return new WebInspector.FontView(resource);
68     default:
69         return new WebInspector.ResourceView(resource);
70     }
71 }
72
73 WebInspector.ResourceSourceFrame = function(resource)
74 {
75     WebInspector.SourceFrame.call(this, new WebInspector.SourceFrameDelegate(resource), resource.url);
76     this._resource = resource;
77 }
78
79 //This is a map from resource.type to mime types
80 //found in WebInspector.SourceTokenizer.Registry.
81 WebInspector.ResourceSourceFrame.DefaultMIMETypeForResourceType = {
82     0: "text/html",
83     1: "text/css",
84     4: "text/javascript"
85 }
86
87 WebInspector.ResourceSourceFrame.mimeTypeForResource = function(resource) {
88     return WebInspector.ResourceSourceFrame.DefaultMIMETypeForResourceType[resource.type] || resource.mimeType;
89 }
90
91 WebInspector.ResourceSourceFrame.prototype = {
92     get resource()
93     {
94         return this._resource;
95     },
96
97     requestContent: function(callback)
98     {
99         function contentLoaded(text)
100         {
101             var mimeType = WebInspector.ResourceSourceFrame.mimeTypeForResource(this.resource);
102             callback(mimeType, text);
103         }
104
105         this.resource.requestContent(contentLoaded.bind(this));
106     },
107
108     suggestedFileName: function()
109     {
110         return this.resource.displayName;
111     }
112 }
113
114 WebInspector.ResourceSourceFrame.prototype.__proto__ = WebInspector.SourceFrame.prototype;
115
116 WebInspector.EditableResourceSourceFrame = function(resource)
117 {
118     WebInspector.ResourceSourceFrame.call(this, resource);
119 }
120
121 WebInspector.EditableResourceSourceFrame.prototype = {
122     canEditSource: function()
123     {
124         return this.resource.isEditable() && !this._commitEditingInProgress;
125     },
126
127     editContent: function(newText, callback)
128     {
129         this._clearIncrementalUpdateTimer();
130         var majorChange = true;
131         this.resource.setContent(newText, majorChange, callback);
132     },
133
134     cancelEditing: function()
135     {
136         this._clearIncrementalUpdateTimer();
137         const majorChange = false;
138         if (this._viewerState)
139             this.resource.setContent(this._viewerState.textModelContent, majorChange);
140         WebInspector.SourceFrame.prototype.cancelEditing.call(this);
141     },
142
143     afterTextChanged: function(oldRange, newRange)
144     {
145         function commitIncrementalEdit()
146         {
147             var majorChange = false;
148             this.resource.setContent(this._textModel.text, majorChange, function() {});
149         }
150         const updateTimeout = 200;
151         this._incrementalUpdateTimer = setTimeout(commitIncrementalEdit.bind(this), updateTimeout);
152     },
153
154     _clearIncrementalUpdateTimer: function()
155     {
156         if (this._incrementalUpdateTimer)
157             clearTimeout(this._incrementalUpdateTimer);
158         delete this._incrementalUpdateTimer;
159     },
160 }
161
162 WebInspector.EditableResourceSourceFrame.prototype.__proto__ = WebInspector.ResourceSourceFrame.prototype;
163
164 WebInspector.ResourceRevisionSourceFrame = function(revision)
165 {
166     WebInspector.ResourceSourceFrame.call(this, revision.resource);
167     this._revision = revision;
168 }
169
170 WebInspector.ResourceRevisionSourceFrame.prototype = {
171     get resource()
172     {
173         return this._revision.resource;
174     },
175
176     requestContent: function(callback)
177     {
178         function contentLoaded(text)
179         {
180             var mimeType = WebInspector.ResourceSourceFrame.mimeTypeForResource(this.resource);
181             callback(mimeType, text);
182         }
183
184         this._revision.requestContent(contentLoaded.bind(this));
185     },
186 }
187
188 WebInspector.ResourceRevisionSourceFrame.prototype.__proto__ = WebInspector.ResourceSourceFrame.prototype;