-
-
Save adardesign/158cff64a61fb4348183 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
adrma = window.adrma || {}; | |
adrma.storage = { | |
supported: function() { | |
try { | |
return 'localStorage' in window && window['localStorage'] !== null; | |
} catch (e) { | |
return false; | |
} | |
}, | |
get: function(type, key, obj) { | |
if (!this.isSupported) { | |
return; | |
} | |
var item = window[type + "Storage"].getItem(key); | |
if (item && obj) { | |
return JSON.parse(item); | |
} | |
return item; | |
}, | |
set: function(type, key, val) { | |
if (!this.isSupported) { | |
return; | |
} | |
if ($.isPlainObject(val) || $.isArray(val)) { | |
val = JSON.stringify(val); | |
} | |
try { | |
window[type + "Storage"].setItem(key, val); | |
return true; | |
} catch (err) { | |
//Handle errors here | |
return false; | |
} | |
}, | |
add: function(options) { // need to add sopport for string concat | |
if (!this.isSupported) { | |
return; | |
} | |
if (typeof options === "string") { | |
options = { | |
key: options, | |
value: arguments[1] | |
}; | |
} | |
var defualts = { | |
type: "local", | |
key: "", | |
val: "", | |
isObj: false, | |
propsAreObjs: false, | |
comparePropName: "", | |
ifFoundStillUpdate: false, | |
limit: null, | |
addDirection: "push" // or unshift | |
}; | |
options = $.extend({}, defualts, options); | |
var storageedKey = adrma.storage.get(options.type, options.key, options.isObj), | |
isInArray; | |
if (storageedKey) { | |
if ($.isArray(storageedKey)) { | |
if (options.propsAreObjs) { | |
var matchedArray = $.grep(storageedKey, function(ele) { | |
return (options.val[options.comparePropName] === ele[options.comparePropName]); | |
}); | |
if (matchedArray.length) { | |
if (!options.ifFoundStillUpdate) { | |
return false; | |
} | |
if ($.isArray(storageedKey)) { //IE8 issue! | |
storageedKey.splice(storageedKey.indexOf(matchedArray[0]), 1); | |
} | |
} | |
} else { | |
isInArray = $.inArray(options.val, storageedKey); | |
if (isInArray !== -1) { | |
return false; | |
} | |
} | |
if (options.limit && storageedKey.length >= options.limit) { | |
// just do the opposite of addDirection | |
storageedKey[options.addDirection === "push" ? "pop" : "shift"](); | |
} | |
storageedKey[options.addDirection](options.val); | |
options.val = storageedKey; | |
} | |
} else { | |
options.val = [options.val]; | |
} | |
// either way add/update | |
return adrma.storage.set(options.type, options.key, options.val, options.isObj); | |
}, | |
remove: function(type, key, val) { | |
if (!this.isSupported) { | |
return; | |
} | |
var storageedKey = adrma.storage.get(key, true), | |
isInArray; | |
if (storageedKey) { | |
if ($.isArray(storageedKey)) { | |
// check if its already there. | |
//isInArray returns the index of the found result in the aaray. | |
isInArray = $.inArray(val, storageedKey); | |
adrma.noop = isInArray !== -1 && storageedKey.splice(isInArray, 1); | |
val = storageedKey; | |
//console.log(storageedKey); | |
adrma.storage.set(key, val); | |
} | |
} | |
// either way add/update | |
}, | |
"delete": function(type, key) { | |
if (!this.isSupported) { | |
return; | |
} | |
window[type + "Storage"].removeItem(key); | |
}, | |
sync: function(key, url, direction) { | |
// sync key, and optional direction... | |
//adrma.fetchData({}); | |
}, | |
compare: function(key, url) {}, | |
getAll: function(type, format) { | |
if (!this.isSupported) { | |
return; | |
} | |
var storage = window[type + "Storage"]; | |
if (format === "string") { | |
return JSON.stringify(storage); | |
} | |
return storage; | |
}, | |
clearAll: function(type) { | |
if (!this.isSupported) { | |
return; | |
} | |
window[type + "Storage"].clear(); | |
} | |
}; | |
adrma.storage.isSupported = (function() { // since the supported() is already used, needed a new method.. | |
return adrma.storage.supported(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment