CCUserDefault.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * <p>cc.UserDefault acts as a tiny localStorage. You can save and get base type values by it. <br/>
  24. * For example, setBoolForKey("played", true) will add a bool value true into the localStorage. <br/>
  25. * Its key is "played". You can get the value of the key by getBoolForKey("played").</p>
  26. *
  27. * <p>It supports the following base types: <br/>
  28. * bool, int, float, double, string</p>
  29. *
  30. * @class
  31. * @extends cc.Class
  32. */
  33. cc.UserDefault = cc.Class.extend(/** @lends cc.UserDefault# */{
  34. _db:null,
  35. /*
  36. * init user default
  37. * */
  38. init:function () {
  39. this._db = this._getLocalStorage();
  40. return true;
  41. },
  42. _getLocalStorage:function () {
  43. try {
  44. if (!!sys.localStorage) {
  45. return sys.localStorage;
  46. }
  47. } catch (e) {
  48. return undefined;
  49. }
  50. },
  51. _getWebSqlDatabase:function () {
  52. },
  53. /**
  54. * Get bool value by key, if the key doesn't exist, a default value will return. <br/>
  55. * You can set the default value, or it is false.
  56. *
  57. * @param {String} key
  58. * @param {Boolean} defaultValue
  59. * @return {Boolean}
  60. */
  61. getBoolForKey:function (key, defaultValue) {
  62. cc.log("getBoolForKey is deprecated. Use sys.localStorage.getItem instead.");
  63. var value = this._getValueForKey(key);
  64. var ret = defaultValue || false;
  65. if (value == "true") {
  66. return true;
  67. }
  68. else if (value == "false") {
  69. return false;
  70. }
  71. else if (value) {
  72. return Boolean(value);
  73. }
  74. return ret;
  75. },
  76. /**
  77. * Get integer value by key, if the key doesn't exist, a default value will return.<br/>
  78. * You can set the default value, or it is 0.
  79. *
  80. * @param {String} key
  81. * @param {Number} defaultValue
  82. * @return {Number}
  83. */
  84. getIntegerForKey:function (key, defaultValue) {
  85. cc.log("getIntegerForKey is deprecated. Use sys.localStorage.getItem instead.");
  86. var value = this._getValueForKey(key);
  87. var ret = defaultValue || 0;
  88. if (value) {
  89. return parseInt(value);
  90. }
  91. return ret;
  92. },
  93. /**
  94. * Get float value by key, if the key doesn't exist, a default value will return.<br/>
  95. * You can set the default value, or it is 0.0f.
  96. *
  97. * @param {String} key
  98. * @param {Number} defaultValue
  99. * @return {Number}
  100. */
  101. getFloatForKey:function (key, defaultValue) {
  102. cc.log("getFloatForKey is deprecated. Use sys.localStorage.getItem instead.");
  103. var value = this._getValueForKey(key);
  104. var ret = defaultValue || 0;
  105. if (value) {
  106. return parseFloat(value);
  107. }
  108. return ret;
  109. },
  110. /**
  111. * Get double value by key, if the key doesn't exist, a default value will return.<br/>
  112. * You can set the default value, or it is 0.0.
  113. *
  114. * @param {String} key
  115. * @param {Number} defaultValue
  116. * @return {Number}
  117. */
  118. getDoubleForKey:function (key, defaultValue) {
  119. cc.log("getDoubleForKey is deprecated. Use sys.localStorage.getItem instead.");
  120. return this.getFloatForKey(key, defaultValue);
  121. },
  122. /**
  123. * Get string value by key, if the key doesn't exist, a default value will return.<br/>
  124. * You can set the default value, or it is "".
  125. *
  126. * @param {String} key
  127. * @param {String} defaultValue
  128. * @return {String}
  129. */
  130. getStringForKey:function (key, defaultValue) {
  131. cc.log("getStringForKey is deprecated. Use sys.localStorage.getItem instead.");
  132. var value = this._getValueForKey(key);
  133. var ret = defaultValue || "";
  134. if (value) {
  135. return String(value);
  136. }
  137. return ret;
  138. },
  139. _getValueForKey:function (key) {
  140. var ret;
  141. if (this._db) {
  142. ret = this._db.getItem(key);
  143. }
  144. return ret;
  145. },
  146. /**
  147. * Set bool value by key.
  148. *
  149. * @param {String} key
  150. * @param {Boolean} value
  151. */
  152. setBoolForKey:function (key, value) {
  153. cc.log("setBoolForKey is deprecated. Use sys.localStorage.setItem instead.");
  154. // save bool value as sring
  155. this.setStringForKey(key, String(value));
  156. },
  157. /**
  158. * Set integer value by key.
  159. *
  160. * @param {String} key
  161. * @param {Number} value
  162. */
  163. setIntegerForKey:function (key, value) {
  164. cc.log("setIntegerForKey is deprecated. Use sys.localStorage.setItem instead.");
  165. // check key
  166. if (!key) {
  167. return;
  168. }
  169. this._setValueForKey(key, parseInt(value));
  170. },
  171. /**
  172. * Set float value by key.
  173. *
  174. * @param {String} key
  175. * @param {Number} value
  176. */
  177. setFloatForKey:function (key, value) {
  178. cc.log("setFloatForKey is deprecated. Use sys.localStorage.setItem instead.");
  179. // check key
  180. if (!key) {
  181. return;
  182. }
  183. this._setValueForKey(key, parseFloat(value));
  184. },
  185. /**
  186. * Set double value by key.
  187. *
  188. * @param {String} key
  189. * @param {Number} value
  190. */
  191. setDoubleForKey:function (key, value) {
  192. cc.log("setDoubleForKey is deprecated. Use sys.localStorage.setItem instead.");
  193. return this.setFloatForKey(key, value);
  194. },
  195. /**
  196. * Set string value by key.
  197. *
  198. * @param {String} key
  199. * @param {String} value
  200. */
  201. setStringForKey:function (key, value) {
  202. cc.log("setStringForKey is deprecated. Use sys.localStorage.setItem instead.");
  203. // check key
  204. if (!key) {
  205. return;
  206. }
  207. this._setValueForKey(key, String(value));
  208. },
  209. _setValueForKey:function (key, value) {
  210. if (this._db) {
  211. this._db.setItem(key, value);
  212. }
  213. }
  214. });
  215. /**
  216. * returns a shared instance of the UserDefault
  217. * @function
  218. * @return {cc.UserDefault|}
  219. */
  220. cc.UserDefault.getInstance = function () {
  221. cc.log("cc.UserDefault is deprecated. Use sys.localStorage instead.");
  222. if (!this._sUserDefault) {
  223. this._sUserDefault = new cc.UserDefault();
  224. this._sUserDefault.init();
  225. }
  226. return this._sUserDefault;
  227. };
  228. /**
  229. * purge a shared instance of the UserDefault
  230. * @function
  231. * @return {cc.UserDefault|}
  232. */
  233. cc.UserDefault.purgeInstanceUserDefault = function () {
  234. if (cc.Browser) { //TODO: clear() is not implemented in JSB
  235. if (this._db) {
  236. this._db.clear();
  237. }
  238. }
  239. };
  240. cc.UserDefault._sUserDefault = null;
  241. cc.UserDefault._isFilePathInitialized = false;