Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / snippets / SnippetStorage.js
blob93acff5ce9009d53f92f57c97e846b665c6e865c
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 /**
32 * @constructor
33 * @extends {WebInspector.Object}
35 WebInspector.SnippetStorage = function(settingPrefix, namePrefix)
37 this._snippets = {};
39 this._lastSnippetIdentifierSetting = WebInspector.settings.createSetting(settingPrefix + "Snippets_lastIdentifier", 0);
40 this._snippetsSetting = WebInspector.settings.createSetting(settingPrefix + "Snippets", []);
41 this._namePrefix = namePrefix;
43 this._loadSettings();
46 WebInspector.SnippetStorage.prototype = {
47 get namePrefix()
49 return this._namePrefix;
52 _saveSettings: function()
54 var savedSnippets = [];
55 for (var id in this._snippets)
56 savedSnippets.push(this._snippets[id].serializeToObject());
57 this._snippetsSetting.set(savedSnippets);
60 /**
61 * @return {!Array.<!WebInspector.Snippet>}
63 snippets: function()
65 var result = [];
66 for (var id in this._snippets)
67 result.push(this._snippets[id]);
68 return result;
71 /**
72 * @param {string} id
73 * @return {!WebInspector.Snippet}
75 snippetForId: function(id)
77 return this._snippets[id];
80 /**
81 * @param {string} name
82 * @return {?WebInspector.Snippet}
84 snippetForName: function(name)
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;
93 _loadSettings: function()
95 var savedSnippets = this._snippetsSetting.get();
96 for (var i = 0; i < savedSnippets.length; ++i)
97 this._snippetAdded(WebInspector.Snippet.fromObject(this, savedSnippets[i]));
101 * @param {!WebInspector.Snippet} snippet
103 deleteSnippet: function(snippet)
105 delete this._snippets[snippet.id];
106 this._saveSettings();
110 * @return {!WebInspector.Snippet}
112 createSnippet: function()
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;
124 * @param {!WebInspector.Snippet} snippet
126 _snippetAdded: function(snippet)
128 this._snippets[snippet.id] = snippet;
131 __proto__: WebInspector.Object.prototype
135 * @constructor
136 * @extends {WebInspector.Object}
137 * @param {!WebInspector.SnippetStorage} storage
138 * @param {string} id
139 * @param {string=} name
140 * @param {string=} content
142 WebInspector.Snippet = function(storage, id, name, content)
144 this._storage = storage;
145 this._id = id;
146 this._name = name || storage.namePrefix + id;
147 this._content = content || "";
151 * @param {!WebInspector.SnippetStorage} storage
152 * @param {!Object} serializedSnippet
153 * @return {!WebInspector.Snippet}
155 WebInspector.Snippet.fromObject = function(storage, serializedSnippet)
157 return new WebInspector.Snippet(storage, serializedSnippet.id, serializedSnippet.name, serializedSnippet.content);
160 WebInspector.Snippet.prototype = {
162 * @return {string}
164 get id()
166 return this._id;
170 * @return {string}
172 get name()
174 return this._name;
177 set name(name)
179 if (this._name === name)
180 return;
182 this._name = name;
183 this._storage._saveSettings();
187 * @return {string}
189 get content()
191 return this._content;
194 set content(content)
196 if (this._content === content)
197 return;
199 this._content = content;
200 this._storage._saveSettings();
204 * @return {!Object}
206 serializeToObject: function()
208 var serializedSnippet = {};
209 serializedSnippet.id = this.id;
210 serializedSnippet.name = this.name;
211 serializedSnippet.content = this.content;
212 return serializedSnippet;
215 __proto__: WebInspector.Object.prototype