UNPKG

4.36 kBJavaScriptView Raw
1(function (ng) {
2 /**
3 * @ngdoc module
4 * @module apeman-ui-contrib-angular-storaging
5 * @name apeman-ui-contrib-angular-storaging
6 * @description
7 * apeman ui module for angular to work with localstorage.
8 */
9
10 "use strict";
11
12 ng
13 .module('apeman-ui-contrib-angular-storaging', []);
14
15})(angular);
16(function (ng, localStorage) {
17 /**
18 * @ngdoc service
19 * @module apeman-ui-contrib-angular-storaging
20 * @name apStorage
21 * @description
22 * Local storage wrapper.
23 */
24
25 "use strict";
26
27 ng
28 .module('apeman-ui-contrib-angular-storaging')
29 .service("apStorage", function () {
30 var s = this;
31
32 /**
33 * @ngdoc method
34 * @name apStorage#get
35 * @description Get data with key from local storage.
36 * @param {string} key - Key for storage.
37 * @returns {object|string|null} - Acquired value.
38 */
39 s.get = function get(key) {
40 var s = this;
41 return s.getObj(key) || s.getStr(key) || null;
42 };
43 /**
44 * @ngdoc method
45 * @name apStorage#getObj
46 * @description Get value as an object.
47 * @param {string} key - Key for storage.
48 * @returns {object|null} - Acquired value.
49 */
50 s.getObj = function getObj(key) {
51 var s = this,
52 val = s._getItem(key);
53 try {
54 return JSON.parse(val);
55 } catch (e) {
56 // Ignore error.
57 return null;
58 }
59 };
60 /**
61 * @ngdoc method
62 * @name apStorage#getStr
63 * @description Get value as string.
64 * @param {string} key - Key for storage.
65 * @returns {string|null} - Acquired value.
66 */
67 s.getStr = function getStr(key) {
68 var s = this,
69 val = s._getItem(key);
70 return val && String(val) || null;
71 };
72 /**
73 * @ngdoc method
74 * @name apStorage#set
75 * @description Set a value to save localstorage.
76 * @param {string} key - Key for storage.
77 * @param {string|object} val - Value to get.
78 */
79 s.set = function set(key, val) {
80 var s = this;
81 switch (typeof val) {
82 case 'object':
83 s.setObj(key, val);
84 break;
85 default:
86 s.setStr(key, val);
87 break;
88 }
89 };
90 /**
91 * @ngdoc method
92 * @name apStorage#remove
93 * @description Remove an item from localstorage.
94 * @param {string} key - Key to remove.
95 */
96 s.remove = function (key) {
97 var s = this;
98 s._removeItem(key);
99 };
100 /**
101 * @ngdoc method
102 * @name apStorage#setObj
103 * @description Set an object.
104 * @param {string} key - Key for storage.
105 * @param {object} obj - Object to set.
106 */
107 s.setObj = function (key, obj) {
108 var s = this;
109 try {
110 s._setItem(key, JSON.stringify(obj));
111 } catch (e) {
112 s._setItem(key, obj);
113 }
114 };
115 /**
116 * @ngdoc method
117 * @name apStorage#setStr
118 * @description Set string.
119 * @param {string} key - Key for storage.
120 * @param {string} str - String to save.
121 */
122 s.setStr = function (key, str) {
123 var s = this;
124 s._setItem(key, String(str));
125 };
126 s._getItem = function (key) {
127 return localStorage.getItem(key);
128 };
129 s._setItem = function (key, val) {
130 localStorage.setItem(key, val);
131 };
132 s._removeItem = function (key) {
133 localStorage.removeItem(key);
134 };
135 });
136})
137(angular, window.localStorage);