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
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
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.
33 * @extends {WebInspector.Object}
35 WebInspector
.SnippetStorage = function(settingPrefix
, namePrefix
)
39 this._lastSnippetIdentifierSetting
= WebInspector
.settings
.createSetting(settingPrefix
+ "Snippets_lastIdentifier", 0);
40 this._snippetsSetting
= WebInspector
.settings
.createSetting(settingPrefix
+ "Snippets", []);
41 this._namePrefix
= namePrefix
;
46 WebInspector
.SnippetStorage
.prototype = {
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
);
61 * @return {!Array.<!WebInspector.Snippet>}
66 for (var id
in this._snippets
)
67 result
.push(this._snippets
[id
]);
73 * @return {!WebInspector.Snippet}
75 snippetForId: function(id
)
77 return this._snippets
[id
];
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
)
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();
124 * @param {!WebInspector.Snippet} snippet
126 _snippetAdded: function(snippet
)
128 this._snippets
[snippet
.id
] = snippet
;
131 __proto__
: WebInspector
.Object
.prototype
136 * @extends {WebInspector.Object}
137 * @param {!WebInspector.SnippetStorage} storage
139 * @param {string=} name
140 * @param {string=} content
142 WebInspector
.Snippet = function(storage
, id
, name
, content
)
144 this._storage
= storage
;
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 = {
179 if (this._name
=== name
)
183 this._storage
._saveSettings();
191 return this._content
;
196 if (this._content
=== content
)
199 this._content
= content
;
200 this._storage
._saveSettings();
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