You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.0 KiB
77 lines
2.0 KiB
7 years ago
|
'use strict'
|
||
|
|
||
|
function Storage (prefix) {
|
||
|
this.exists = function (name) {
|
||
7 years ago
|
if (typeof window !== 'undefined') {
|
||
|
return this.get(name) !== null
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
this.get = function (name) {
|
||
7 years ago
|
if (typeof window !== 'undefined') {
|
||
|
return window.localStorage.getItem(prefix + name)
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
this.set = function (name, content) {
|
||
|
try {
|
||
7 years ago
|
if (typeof window !== 'undefined') {
|
||
|
window.localStorage.setItem(prefix + name, content)
|
||
|
}
|
||
7 years ago
|
} catch (exception) {
|
||
|
return false
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
this.remove = function (name) {
|
||
7 years ago
|
if (typeof window !== 'undefined') {
|
||
|
window.localStorage.removeItem(prefix + name)
|
||
|
}
|
||
4 years ago
|
return true
|
||
7 years ago
|
}
|
||
|
|
||
|
this.rename = function (originalName, newName) {
|
||
5 years ago
|
const content = this.get(originalName)
|
||
7 years ago
|
if (!this.set(newName, content)) {
|
||
|
return false
|
||
|
}
|
||
|
this.remove(originalName)
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
function safeKeys () {
|
||
|
// NOTE: this is a workaround for some browsers
|
||
7 years ago
|
if (typeof window !== 'undefined') {
|
||
|
return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined })
|
||
|
}
|
||
4 years ago
|
return []
|
||
7 years ago
|
}
|
||
|
|
||
|
this.keys = function () {
|
||
|
return safeKeys()
|
||
|
// filter any names not including the prefix
|
||
|
.filter(function (item) { return item.indexOf(prefix, 0) === 0 })
|
||
|
// remove prefix from filename and add the 'browser' path
|
||
|
.map(function (item) { return item.substr(prefix.length) })
|
||
|
}
|
||
|
|
||
|
// on startup, upgrade the old storage layout
|
||
7 years ago
|
if (typeof window !== 'undefined') {
|
||
|
safeKeys().forEach(function (name) {
|
||
|
if (name.indexOf('sol-cache-file-', 0) === 0) {
|
||
|
var content = window.localStorage.getItem(name)
|
||
|
window.localStorage.setItem(name.replace(/^sol-cache-file-/, 'sol:'), content)
|
||
|
window.localStorage.removeItem(name)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
// remove obsolete key
|
||
7 years ago
|
if (typeof window !== 'undefined') {
|
||
|
window.localStorage.removeItem('editor-size-cache')
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
module.exports = Storage
|