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 */ 35WebInspector.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 46WebInspector.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 reset: function() 132 { 133 this._lastSnippetIdentifierSetting.set(0); 134 this._snippetsSetting.set([]); 135 this._snippets = {}; 136 }, 137 138 __proto__: WebInspector.Object.prototype 139} 140 141/** 142 * @constructor 143 * @extends {WebInspector.Object} 144 * @param {!WebInspector.SnippetStorage} storage 145 * @param {string} id 146 * @param {string=} name 147 * @param {string=} content 148 */ 149WebInspector.Snippet = function(storage, id, name, content) 150{ 151 this._storage = storage; 152 this._id = id; 153 this._name = name || storage.namePrefix + id; 154 this._content = content || ""; 155} 156 157/** 158 * @param {!WebInspector.SnippetStorage} storage 159 * @param {!Object} serializedSnippet 160 * @return {!WebInspector.Snippet} 161 */ 162WebInspector.Snippet.fromObject = function(storage, serializedSnippet) 163{ 164 return new WebInspector.Snippet(storage, serializedSnippet.id, serializedSnippet.name, serializedSnippet.content); 165} 166 167WebInspector.Snippet.prototype = { 168 /** 169 * @return {string} 170 */ 171 get id() 172 { 173 return this._id; 174 }, 175 176 /** 177 * @return {string} 178 */ 179 get name() 180 { 181 return this._name; 182 }, 183 184 set name(name) 185 { 186 if (this._name === name) 187 return; 188 189 this._name = name; 190 this._storage._saveSettings(); 191 }, 192 193 /** 194 * @return {string} 195 */ 196 get content() 197 { 198 return this._content; 199 }, 200 201 set content(content) 202 { 203 if (this._content === content) 204 return; 205 206 this._content = content; 207 this._storage._saveSettings(); 208 }, 209 210 /** 211 * @return {!Object} 212 */ 213 serializeToObject: function() 214 { 215 var serializedSnippet = {}; 216 serializedSnippet.id = this.id; 217 serializedSnippet.name = this.name; 218 serializedSnippet.content = this.content; 219 return serializedSnippet; 220 }, 221 222 __proto__: WebInspector.Object.prototype 223} 224