Home | History | Annotate | Download | only in sdk
      1 /*
      2  * Copyright (C) 2012 Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /**
     32  * @constructor
     33  * @extends {WebInspector.Object}
     34  */
     35 WebInspector.SnippetStorage = function(settingPrefix, namePrefix)
     36 {
     37     this._snippets = {};
     38 
     39     this._lastSnippetIdentifierSetting = WebInspector.settings.createSetting(settingPrefix + "Snippets_lastIdentifier", 0);
     40     this._snippetsSetting = WebInspector.settings.createSetting(settingPrefix + "Snippets", []);
     41     this._namePrefix = namePrefix;
     42 
     43     this._loadSettings();
     44 }
     45 
     46 WebInspector.SnippetStorage.prototype = {
     47     get namePrefix()
     48     {
     49         return this._namePrefix;
     50     },
     51 
     52     _saveSettings: function()
     53     {
     54         var savedSnippets = [];
     55         for (var id in this._snippets)
     56             savedSnippets.push(this._snippets[id].serializeToObject());
     57         this._snippetsSetting.set(savedSnippets);
     58     },
     59 
     60     /**
     61      * @return {!Array.<!WebInspector.Snippet>}
     62      */
     63     snippets: function()
     64     {
     65         var result = [];
     66         for (var id in this._snippets)
     67             result.push(this._snippets[id]);
     68         return result;
     69     },
     70 
     71     /**
     72      * @param {string} id
     73      * @return {!WebInspector.Snippet}
     74      */
     75     snippetForId: function(id)
     76     {
     77         return this._snippets[id];
     78     },
     79 
     80     /**
     81      * @param {string} name
     82      * @return {?WebInspector.Snippet}
     83      */
     84     snippetForName: function(name)
     85     {
     86         var snippets = Object.values(this._snippets);
     87         for (var i = 0; i < snippets.length; ++i)
     88             if (snippets[i].name === name)
     89                 return snippets[i];
     90         return null;
     91     },
     92 
     93     _loadSettings: function()
     94     {
     95         var savedSnippets = this._snippetsSetting.get();
     96         for (var i = 0; i < savedSnippets.length; ++i)
     97             this._snippetAdded(WebInspector.Snippet.fromObject(this, savedSnippets[i]));
     98     },
     99 
    100     /**
    101      * @param {!WebInspector.Snippet} snippet
    102      */
    103     deleteSnippet: function(snippet)
    104     {
    105         delete this._snippets[snippet.id];
    106         this._saveSettings();
    107     },
    108 
    109     /**
    110      * @return {!WebInspector.Snippet}
    111      */
    112     createSnippet: function()
    113     {
    114         var nextId = this._lastSnippetIdentifierSetting.get() + 1;
    115         var snippetId = String(nextId);
    116         this._lastSnippetIdentifierSetting.set(nextId);
    117         var snippet = new WebInspector.Snippet(this, snippetId);
    118         this._snippetAdded(snippet);
    119         this._saveSettings();
    120         return snippet;
    121     },
    122 
    123     /**
    124      * @param {!WebInspector.Snippet} snippet
    125      */
    126     _snippetAdded: function(snippet)
    127     {
    128         this._snippets[snippet.id] = snippet;
    129     },
    130 
    131     __proto__: WebInspector.Object.prototype
    132 }
    133 
    134 /**
    135  * @constructor
    136  * @extends {WebInspector.Object}
    137  * @param {!WebInspector.SnippetStorage} storage
    138  * @param {string} id
    139  * @param {string=} name
    140  * @param {string=} content
    141  */
    142 WebInspector.Snippet = function(storage, id, name, content)
    143 {
    144     this._storage = storage;
    145     this._id = id;
    146     this._name = name || storage.namePrefix + id;
    147     this._content = content || "";
    148 }
    149 
    150 /**
    151  * @param {!WebInspector.SnippetStorage} storage
    152  * @param {!Object} serializedSnippet
    153  * @return {!WebInspector.Snippet}
    154  */
    155 WebInspector.Snippet.fromObject = function(storage, serializedSnippet)
    156 {
    157     return new WebInspector.Snippet(storage, serializedSnippet.id, serializedSnippet.name, serializedSnippet.content);
    158 }
    159 
    160 WebInspector.Snippet.prototype = {
    161     /**
    162      * @return {string}
    163      */
    164     get id()
    165     {
    166         return this._id;
    167     },
    168 
    169     /**
    170      * @return {string}
    171      */
    172     get name()
    173     {
    174         return this._name;
    175     },
    176 
    177     set name(name)
    178     {
    179         if (this._name === name)
    180             return;
    181 
    182         this._name = name;
    183         this._storage._saveSettings();
    184     },
    185 
    186     /**
    187      * @return {string}
    188      */
    189     get content()
    190     {
    191         return this._content;
    192     },
    193 
    194     set content(content)
    195     {
    196         if (this._content === content)
    197             return;
    198 
    199         this._content = content;
    200         this._storage._saveSettings();
    201     },
    202 
    203     /**
    204      * @return {!Object}
    205      */
    206     serializeToObject: function()
    207     {
    208         var serializedSnippet = {};
    209         serializedSnippet.id = this.id;
    210         serializedSnippet.name = this.name;
    211         serializedSnippet.content = this.content;
    212         return serializedSnippet;
    213     },
    214 
    215     __proto__: WebInspector.Object.prototype
    216 }
    217