Home | History | Annotate | Download | only in front-end
      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.createResourceView = 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 new WebInspector.ResourceSourceFrame(resource);
     54     case WebInspector.resourceCategories.images:
     55         return new WebInspector.ImageView(resource);
     56     case WebInspector.resourceCategories.fonts:
     57         return new WebInspector.FontView(resource);
     58     default:
     59         return new WebInspector.ResourceView(resource);
     60     }
     61 }
     62 
     63 WebInspector.ResourceView.resourceViewTypeMatchesResource = function(resource)
     64 {
     65     var resourceView = resource._resourceView;
     66     switch (resource.category) {
     67     case WebInspector.resourceCategories.documents:
     68     case WebInspector.resourceCategories.scripts:
     69     case WebInspector.resourceCategories.xhr:
     70     case WebInspector.resourceCategories.stylesheets:
     71         return resourceView.__proto__ === WebInspector.ResourceSourceFrame.prototype;
     72     case WebInspector.resourceCategories.images:
     73         return resourceView.__proto__ === WebInspector.ImageView.prototype;
     74     case WebInspector.resourceCategories.fonts:
     75         return resourceView.__proto__ === WebInspector.FontView.prototype;
     76     default:
     77         return resourceView.__proto__ === WebInspector.ResourceView.prototype;
     78     }
     79 }
     80 
     81 WebInspector.ResourceView.resourceViewForResource = function(resource)
     82 {
     83     if (!resource)
     84         return null;
     85     if (!resource._resourceView)
     86         resource._resourceView = WebInspector.ResourceView.createResourceView(resource);
     87     return resource._resourceView;
     88 }
     89 
     90 WebInspector.ResourceView.recreateResourceView = function(resource)
     91 {
     92     var newView = WebInspector.ResourceView.createResourceView(resource);
     93 
     94     var oldView = resource._resourceView;
     95     var oldViewParentNode = oldView.visible ? oldView.element.parentNode : null;
     96     var scrollTop = oldView.scrollTop;
     97 
     98     resource._resourceView.detach();
     99     delete resource._resourceView;
    100 
    101     resource._resourceView = newView;
    102 
    103     if (oldViewParentNode)
    104         newView.show(oldViewParentNode);
    105     if (scrollTop)
    106         newView.scrollTop = scrollTop;
    107 
    108     return newView;
    109 }
    110 
    111 WebInspector.ResourceView.existingResourceViewForResource = function(resource)
    112 {
    113     if (!resource)
    114         return null;
    115     return resource._resourceView;
    116 }
    117 
    118 
    119 WebInspector.ResourceSourceFrame = function(resource)
    120 {
    121     WebInspector.SourceFrame.call(this, new WebInspector.SourceFrameDelegate(), resource.url);
    122     this._resource = resource;
    123 }
    124 
    125 //This is a map from resource.type to mime types
    126 //found in WebInspector.SourceTokenizer.Registry.
    127 WebInspector.ResourceSourceFrame.DefaultMIMETypeForResourceType = {
    128     0: "text/html",
    129     1: "text/css",
    130     4: "text/javascript"
    131 }
    132 
    133 WebInspector.ResourceSourceFrame.prototype = {
    134     get resource()
    135     {
    136         return this._resource;
    137     },
    138 
    139     isContentEditable: function()
    140     {
    141         return this._resource.isEditable();
    142     },
    143 
    144     editContent: function(newText, callback)
    145     {
    146         this._clearIncrementalUpdateTimer();
    147         var majorChange = true;
    148         this._resource.setContent(newText, majorChange, callback);
    149     },
    150 
    151     endEditing: function(oldRange, newRange)
    152     {
    153         function commitIncrementalEdit()
    154         {
    155             var majorChange = false;
    156             this._resource.setContent(this._textModel.text, majorChange, function() {});
    157         }
    158         const updateTimeout = 200;
    159         this._incrementalUpdateTimer = setTimeout(commitIncrementalEdit.bind(this), updateTimeout);
    160     },
    161 
    162     _clearIncrementalUpdateTimer: function()
    163     {
    164         if (this._incrementalUpdateTimer)
    165             clearTimeout(this._incrementalUpdateTimer);
    166         delete this._incrementalUpdateTimer;
    167     },
    168 
    169     requestContent: function(callback)
    170     {
    171         function contentLoaded(text)
    172         {
    173             var mimeType = WebInspector.ResourceSourceFrame.DefaultMIMETypeForResourceType[this._resource.type] || this._resource.mimeType;
    174             callback(mimeType, text);
    175         }
    176         this._resource.requestContent(contentLoaded.bind(this));
    177     }
    178 }
    179 
    180 WebInspector.ResourceSourceFrame.prototype.__proto__ = WebInspector.SourceFrame.prototype;
    181 
    182 WebInspector.RevisionSourceFrame = function(revision)
    183 {
    184     WebInspector.SourceFrame.call(this, new WebInspector.SourceFrameDelegate(), revision.resource.url);
    185     this._revision = revision;
    186 }
    187 
    188 WebInspector.RevisionSourceFrame.prototype = {
    189     get resource()
    190     {
    191         return this._revision.resource;
    192     },
    193 
    194     isContentEditable: function()
    195     {
    196         return false;
    197     },
    198 
    199     requestContent: function(callback)
    200     {
    201         function contentLoaded(text)
    202         {
    203             var mimeType = WebInspector.ResourceSourceFrame.DefaultMIMETypeForResourceType[this._revision.resource.type] || this._revision.resource.mimeType;
    204             callback(mimeType, text);
    205         }
    206         this._revision.requestContent(contentLoaded.bind(this));
    207     }
    208 }
    209 
    210 WebInspector.RevisionSourceFrame.prototype.__proto__ = WebInspector.SourceFrame.prototype;
    211