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