WebInspector.SnippetStorage = function(settingPrefix, namePrefix)
{
this._snippets = {};
this._lastSnippetIdentifierSetting = WebInspector.settings.createSetting(settingPrefix + "Snippets_lastIdentifier", 0);
this._snippetsSetting = WebInspector.settings.createSetting(settingPrefix + "Snippets", []);
this._namePrefix = namePrefix;
this._loadSettings();
}
WebInspector.SnippetStorage.prototype = {
get namePrefix()
{
return this._namePrefix;
},
_saveSettings: function()
{
var savedSnippets = [];
for (var id in this._snippets)
savedSnippets.push(this._snippets[id].serializeToObject());
this._snippetsSetting.set(savedSnippets);
},
snippets: function()
{
var result = [];
for (var id in this._snippets)
result.push(this._snippets[id]);
return result;
},
snippetForId: function(id)
{
return this._snippets[id];
},
_loadSettings: function()
{
var savedSnippets = this._snippetsSetting.get();
for (var i = 0; i < savedSnippets.length; ++i)
this._snippetAdded(WebInspector.Snippet.fromObject(this, savedSnippets[i]));
},
deleteSnippet: function(snippet)
{
delete this._snippets[snippet.id];
this._saveSettings();
},
createSnippet: function()
{
var nextId = this._lastSnippetIdentifierSetting.get() + 1;
var snippetId = String(nextId);
this._lastSnippetIdentifierSetting.set(nextId);
var snippet = new WebInspector.Snippet(this, snippetId);
this._snippetAdded(snippet);
this._saveSettings();
return snippet;
},
_snippetAdded: function(snippet)
{
this._snippets[snippet.id] = snippet;
},
reset: function()
{
this._lastSnippetIdentifierSetting.set(0);
this._snippetsSetting.set([]);
this._snippets = {};
},
__proto__: WebInspector.Object.prototype
}
WebInspector.Snippet = function(storage, id, name, content)
{
this._storage = storage;
this._id = id;
this._name = name || storage.namePrefix + id;
this._content = content || "";
}
WebInspector.Snippet.fromObject = function(storage, serializedSnippet)
{
return new WebInspector.Snippet(storage, serializedSnippet.id, serializedSnippet.name, serializedSnippet.content);
}
WebInspector.Snippet.prototype = {
get id()
{
return this._id;
},
get name()
{
return this._name;
},
set name(name)
{
if (this._name === name)
return;
this._name = name;
this._storage._saveSettings();
},
get content()
{
return this._content;
},
set content(content)
{
if (this._content === content)
return;
this._content = content;
this._storage._saveSettings();
},
serializeToObject: function()
{
var serializedSnippet = {};
serializedSnippet.id = this.id;
serializedSnippet.name = this.name;
serializedSnippet.content = this.content;
return serializedSnippet;
},
__proto__: WebInspector.Object.prototype
}