SnippetStorage.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  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.
  17. *
  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.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.Object}
  33. */
  34. WebInspector.SnippetStorage = function(settingPrefix, namePrefix)
  35. {
  36. this._snippets = {};
  37. this._lastSnippetIdentifierSetting = WebInspector.settings.createSetting(settingPrefix + "Snippets_lastIdentifier", 0);
  38. this._snippetsSetting = WebInspector.settings.createSetting(settingPrefix + "Snippets", []);
  39. this._namePrefix = namePrefix;
  40. this._loadSettings();
  41. }
  42. WebInspector.SnippetStorage.prototype = {
  43. get namePrefix()
  44. {
  45. return this._namePrefix;
  46. },
  47. _saveSettings: function()
  48. {
  49. var savedSnippets = [];
  50. for (var id in this._snippets)
  51. savedSnippets.push(this._snippets[id].serializeToObject());
  52. this._snippetsSetting.set(savedSnippets);
  53. },
  54. /**
  55. * @return {Array.<WebInspector.Snippet>}
  56. */
  57. snippets: function()
  58. {
  59. var result = [];
  60. for (var id in this._snippets)
  61. result.push(this._snippets[id]);
  62. return result;
  63. },
  64. /**
  65. * @param {string} id
  66. * @return {WebInspector.Snippet}
  67. */
  68. snippetForId: function(id)
  69. {
  70. return this._snippets[id];
  71. },
  72. _loadSettings: function()
  73. {
  74. var savedSnippets = this._snippetsSetting.get();
  75. for (var i = 0; i < savedSnippets.length; ++i)
  76. this._snippetAdded(WebInspector.Snippet.fromObject(this, savedSnippets[i]));
  77. },
  78. /**
  79. * @param {WebInspector.Snippet} snippet
  80. */
  81. deleteSnippet: function(snippet)
  82. {
  83. delete this._snippets[snippet.id];
  84. this._saveSettings();
  85. },
  86. /**
  87. * @return {WebInspector.Snippet}
  88. */
  89. createSnippet: function()
  90. {
  91. var nextId = this._lastSnippetIdentifierSetting.get() + 1;
  92. var snippetId = String(nextId);
  93. this._lastSnippetIdentifierSetting.set(nextId);
  94. var snippet = new WebInspector.Snippet(this, snippetId);
  95. this._snippetAdded(snippet);
  96. this._saveSettings();
  97. return snippet;
  98. },
  99. /**
  100. * @param {WebInspector.Snippet} snippet
  101. */
  102. _snippetAdded: function(snippet)
  103. {
  104. this._snippets[snippet.id] = snippet;
  105. },
  106. reset: function()
  107. {
  108. this._lastSnippetIdentifierSetting.set(0);
  109. this._snippetsSetting.set([]);
  110. this._snippets = {};
  111. },
  112. __proto__: WebInspector.Object.prototype
  113. }
  114. /**
  115. * @constructor
  116. * @extends {WebInspector.Object}
  117. * @param {WebInspector.SnippetStorage} storage
  118. * @param {string} id
  119. * @param {string=} name
  120. * @param {string=} content
  121. */
  122. WebInspector.Snippet = function(storage, id, name, content)
  123. {
  124. this._storage = storage;
  125. this._id = id;
  126. this._name = name || storage.namePrefix + id;
  127. this._content = content || "";
  128. }
  129. /**
  130. * @param {WebInspector.SnippetStorage} storage
  131. * @param {Object} serializedSnippet
  132. * @return {WebInspector.Snippet}
  133. */
  134. WebInspector.Snippet.fromObject = function(storage, serializedSnippet)
  135. {
  136. return new WebInspector.Snippet(storage, serializedSnippet.id, serializedSnippet.name, serializedSnippet.content);
  137. }
  138. WebInspector.Snippet.prototype = {
  139. /**
  140. * @return {string}
  141. */
  142. get id()
  143. {
  144. return this._id;
  145. },
  146. /**
  147. * @return {string}
  148. */
  149. get name()
  150. {
  151. return this._name;
  152. },
  153. set name(name)
  154. {
  155. if (this._name === name)
  156. return;
  157. this._name = name;
  158. this._storage._saveSettings();
  159. },
  160. /**
  161. * @return {string}
  162. */
  163. get content()
  164. {
  165. return this._content;
  166. },
  167. set content(content)
  168. {
  169. if (this._content === content)
  170. return;
  171. this._content = content;
  172. this._storage._saveSettings();
  173. },
  174. /**
  175. * @return {Object}
  176. */
  177. serializeToObject: function()
  178. {
  179. var serializedSnippet = {};
  180. serializedSnippet.id = this.id;
  181. serializedSnippet.name = this.name;
  182. serializedSnippet.content = this.content;
  183. return serializedSnippet;
  184. },
  185. __proto__: WebInspector.Object.prototype
  186. }