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 |
(...skipping 16 matching lines...) Expand all Loading... |
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 | 30 |
31 /** | 31 /** |
32 * @constructor | 32 * @constructor |
33 * @extends {WebInspector.Object} | 33 * @extends {WebInspector.Object} |
34 */ | 34 */ |
35 WebInspector.SnippetStorage = function(settingPrefix, namePrefix) | 35 WebInspector.SnippetStorage = function(settingPrefix, namePrefix) |
36 { | 36 { |
37 this._snippets = {}; | 37 /** @type {!Map<string,!WebInspector.Snippet>} */ |
| 38 this._snippets = new Map(); |
38 | 39 |
39 this._lastSnippetIdentifierSetting = WebInspector.settings.createSetting(set
tingPrefix + "Snippets_lastIdentifier", 0); | 40 this._lastSnippetIdentifierSetting = WebInspector.settings.createSetting(set
tingPrefix + "Snippets_lastIdentifier", 0); |
40 this._snippetsSetting = WebInspector.settings.createSetting(settingPrefix +
"Snippets", []); | 41 this._snippetsSetting = WebInspector.settings.createSetting(settingPrefix +
"Snippets", []); |
41 this._namePrefix = namePrefix; | 42 this._namePrefix = namePrefix; |
42 | 43 |
43 this._loadSettings(); | 44 this._loadSettings(); |
44 } | 45 } |
45 | 46 |
46 WebInspector.SnippetStorage.prototype = { | 47 WebInspector.SnippetStorage.prototype = { |
47 get namePrefix() | 48 get namePrefix() |
48 { | 49 { |
49 return this._namePrefix; | 50 return this._namePrefix; |
50 }, | 51 }, |
51 | 52 |
52 _saveSettings: function() | 53 _saveSettings: function() |
53 { | 54 { |
54 var savedSnippets = []; | 55 var savedSnippets = []; |
55 for (var id in this._snippets) | 56 for (var snippet of this._snippets.values()) |
56 savedSnippets.push(this._snippets[id].serializeToObject()); | 57 savedSnippets.push(snippet.serializeToObject()); |
57 this._snippetsSetting.set(savedSnippets); | 58 this._snippetsSetting.set(savedSnippets); |
58 }, | 59 }, |
59 | 60 |
60 /** | 61 /** |
61 * @return {!Array.<!WebInspector.Snippet>} | 62 * @return {!Array<!WebInspector.Snippet>} |
62 */ | 63 */ |
63 snippets: function() | 64 snippets: function() |
64 { | 65 { |
65 var result = []; | 66 return this._snippets.valuesArray(); |
66 for (var id in this._snippets) | |
67 result.push(this._snippets[id]); | |
68 return result; | |
69 }, | 67 }, |
70 | 68 |
71 /** | 69 /** |
72 * @param {string} id | 70 * @param {string} id |
73 * @return {!WebInspector.Snippet} | 71 * @return {?WebInspector.Snippet} |
74 */ | 72 */ |
75 snippetForId: function(id) | 73 snippetForId: function(id) |
76 { | 74 { |
77 return this._snippets[id]; | 75 return this._snippets.get(id); |
78 }, | 76 }, |
79 | 77 |
80 /** | 78 /** |
81 * @param {string} name | 79 * @param {string} name |
82 * @return {?WebInspector.Snippet} | 80 * @return {?WebInspector.Snippet} |
83 */ | 81 */ |
84 snippetForName: function(name) | 82 snippetForName: function(name) |
85 { | 83 { |
86 var snippets = Object.values(this._snippets); | 84 for (var snippet of this._snippets.values()) { |
87 for (var i = 0; i < snippets.length; ++i) | 85 if (snippet.name === name) |
88 if (snippets[i].name === name) | 86 return snippet; |
89 return snippets[i]; | 87 } |
90 return null; | 88 return null; |
91 }, | 89 }, |
92 | 90 |
93 _loadSettings: function() | 91 _loadSettings: function() |
94 { | 92 { |
95 var savedSnippets = this._snippetsSetting.get(); | 93 var savedSnippets = this._snippetsSetting.get(); |
96 for (var i = 0; i < savedSnippets.length; ++i) | 94 for (var i = 0; i < savedSnippets.length; ++i) |
97 this._snippetAdded(WebInspector.Snippet.fromObject(this, savedSnippe
ts[i])); | 95 this._snippetAdded(WebInspector.Snippet.fromObject(this, savedSnippe
ts[i])); |
98 }, | 96 }, |
99 | 97 |
100 /** | 98 /** |
101 * @param {!WebInspector.Snippet} snippet | 99 * @param {!WebInspector.Snippet} snippet |
102 */ | 100 */ |
103 deleteSnippet: function(snippet) | 101 deleteSnippet: function(snippet) |
104 { | 102 { |
105 delete this._snippets[snippet.id]; | 103 this._snippets.delete(snippet.id); |
106 this._saveSettings(); | 104 this._saveSettings(); |
107 }, | 105 }, |
108 | 106 |
109 /** | 107 /** |
110 * @return {!WebInspector.Snippet} | 108 * @return {!WebInspector.Snippet} |
111 */ | 109 */ |
112 createSnippet: function() | 110 createSnippet: function() |
113 { | 111 { |
114 var nextId = this._lastSnippetIdentifierSetting.get() + 1; | 112 var nextId = this._lastSnippetIdentifierSetting.get() + 1; |
115 var snippetId = String(nextId); | 113 var snippetId = String(nextId); |
116 this._lastSnippetIdentifierSetting.set(nextId); | 114 this._lastSnippetIdentifierSetting.set(nextId); |
117 var snippet = new WebInspector.Snippet(this, snippetId); | 115 var snippet = new WebInspector.Snippet(this, snippetId); |
118 this._snippetAdded(snippet); | 116 this._snippetAdded(snippet); |
119 this._saveSettings(); | 117 this._saveSettings(); |
120 return snippet; | 118 return snippet; |
121 }, | 119 }, |
122 | 120 |
123 /** | 121 /** |
124 * @param {!WebInspector.Snippet} snippet | 122 * @param {!WebInspector.Snippet} snippet |
125 */ | 123 */ |
126 _snippetAdded: function(snippet) | 124 _snippetAdded: function(snippet) |
127 { | 125 { |
128 this._snippets[snippet.id] = snippet; | 126 this._snippets.set(snippet.id, snippet); |
129 }, | 127 }, |
130 | 128 |
131 __proto__: WebInspector.Object.prototype | 129 __proto__: WebInspector.Object.prototype |
132 } | 130 } |
133 | 131 |
134 /** | 132 /** |
135 * @constructor | 133 * @constructor |
136 * @extends {WebInspector.Object} | 134 * @extends {WebInspector.Object} |
137 * @param {!WebInspector.SnippetStorage} storage | 135 * @param {!WebInspector.SnippetStorage} storage |
138 * @param {string} id | 136 * @param {string} id |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 { | 205 { |
208 var serializedSnippet = {}; | 206 var serializedSnippet = {}; |
209 serializedSnippet.id = this.id; | 207 serializedSnippet.id = this.id; |
210 serializedSnippet.name = this.name; | 208 serializedSnippet.name = this.name; |
211 serializedSnippet.content = this.content; | 209 serializedSnippet.content = this.content; |
212 return serializedSnippet; | 210 return serializedSnippet; |
213 }, | 211 }, |
214 | 212 |
215 __proto__: WebInspector.Object.prototype | 213 __proto__: WebInspector.Object.prototype |
216 } | 214 } |
OLD | NEW |