memory.service.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @fileOverview
  3. *
  4. * UI 状态的 LocalStorage 的存取文件,未来可能在离线编辑的时候升级
  5. *
  6. * @author: zhangbobell
  7. * @email : zhangbobell@163.com
  8. *
  9. * @copyright: Baidu FEX, 2015
  10. */
  11. angular.module('kityminderEditor')
  12. .service('memory', function() {
  13. function isQuotaExceeded(e) {
  14. var quotaExceeded = false;
  15. if (e) {
  16. if (e.code) {
  17. switch (e.code) {
  18. case 22:
  19. quotaExceeded = true;
  20. break;
  21. case 1014:
  22. // Firefox
  23. if (e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
  24. quotaExceeded = true;
  25. }
  26. break;
  27. }
  28. } else if (e.number === -2147024882) {
  29. // Internet Explorer 8
  30. quotaExceeded = true;
  31. }
  32. }
  33. return quotaExceeded;
  34. }
  35. return {
  36. get: function(key) {
  37. var value = window.localStorage.getItem(key);
  38. return null || JSON.parse(value);
  39. },
  40. set: function(key, value) {
  41. try {
  42. window.localStorage.setItem(key, JSON.stringify(value));
  43. return true;
  44. } catch(e) {
  45. if (isQuotaExceeded(e)) {
  46. return false;
  47. }
  48. }
  49. },
  50. remove: function(key) {
  51. var value = window.localStorage.getItem(key);
  52. window.localStorage.removeItem(key);
  53. return value;
  54. },
  55. clear: function() {
  56. window.localStorage.clear();
  57. }
  58. }
  59. });