| Total Complexity | 16 |
| Complexity/F | 1.78 |
| Lines of Code | 75 |
| Function Count | 9 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | /* global browser, chrome */ |
||
| 3 | if (typeof API === "undefined") { |
||
|
|
|||
| 4 | var API = {}; |
||
| 5 | } |
||
| 6 | |||
| 7 | API.Storage = function() { |
||
| 8 | |||
| 9 | var localStorage = API.api.storage.local; |
||
| 10 | |||
| 11 | return { |
||
| 12 | /** |
||
| 13 | * Retrieves an item from the local storage |
||
| 14 | * @param string|array key The key or an array of keys to retrieve |
||
| 15 | * @returns angular_promise |
||
| 16 | */ |
||
| 17 | get: function(key) { |
||
| 18 | return new C_Promise(function(){ |
||
| 19 | if (API.promise) { |
||
| 20 | localStorage.get(key).then((function(item){ |
||
| 21 | /* jshint ignore:start */ |
||
| 22 | if (typeof key === "[object Array]") { |
||
| 23 | this.call_then(item); |
||
| 24 | } |
||
| 25 | |||
| 26 | else { |
||
| 27 | if (item[key] === undefined) { |
||
| 28 | this.call_error("Data not found"); |
||
| 29 | } |
||
| 30 | else { |
||
| 31 | this.call_then(item[key]); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | /* jshint ignore:end */ |
||
| 35 | }).bind(this), (function(error){ |
||
| 36 | this.call_error(error); |
||
| 37 | }).bind(this)); |
||
| 38 | } |
||
| 39 | else{ |
||
| 40 | localStorage.get(key, (function(item){ |
||
| 41 | /* jshint ignore:start */ |
||
| 42 | if (typeof key === "[object Array]") { |
||
| 43 | this.call_then(item); |
||
| 44 | } |
||
| 45 | |||
| 46 | else { |
||
| 47 | if (item[key] === undefined) { |
||
| 48 | this.call_error("Data not found"); |
||
| 49 | } |
||
| 50 | else { |
||
| 51 | this.call_then(item[key]); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | /* jshint ignore:end */ |
||
| 55 | }).bind(this)); |
||
| 56 | } |
||
| 57 | }); |
||
| 58 | }, |
||
| 59 | |||
| 60 | set: function(key, value) { |
||
| 61 | var o = {}; |
||
| 62 | o[key] = value; |
||
| 63 | |||
| 64 | if (API.promise) { |
||
| 65 | return localStorage.set(o); |
||
| 66 | } |
||
| 67 | else { |
||
| 68 | return new C_Promise(function() { |
||
| 69 | localStorage.set(o, (function(){ |
||
| 70 | this.call_then(); |
||
| 71 | }).bind(this)); |
||
| 72 | }); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | }; |
||
| 77 | }; |