// (c) 2017 NS BASIC Corporation. All rights reserved.
/**
* @file Storage Plug-in Client Library
*
* @license
* (c) 2017 NS BASIC Corporation. All rights reserved.
*/
(function () {
'use strict';
/**
* Creates a new VoltStorage object
*
* This object uses an object and property name to access the default
* scope to deal with changing scope (for instance, if the user
* changes). You cannot use this directly.
*
* Each method allows an undocumented scope (userId or appId)
* argument before the callback. End users should not need this, but
* the dashboard may.
*
* @private
*
* @class
* @memberOf $volt.storage
*
* @param {object} core - a reference to the volt core ($volt)
* @param {object} state - a reference to the volt client state
* @param {string} defaultScope - the default scope to use (the name of a property in the state, e.g. appId, userId)
*/
function VoltStorage(core, state, defaultScope) {
this.core = core;
this.state = state;
this.defaultScope = defaultScope;
}
/**
* Returns key value map of all stored items.
*
* @param {voltCallback} [callback] - upon success data will be an object with each property corresponding to a stored key/value - if not passed, a promise is returned
*/
VoltStorage.prototype.getAllItems = function (scope, callback) {
if (typeof scope !== 'string') {
callback = scope;
scope = this.state[this.defaultScope];
}
return this.core.api('GET', '/storage/{{scope}}', {
scope: scope
}, undefined, callback);
};
/**
* Clears all items.
*
* @param {voltCallback} [callback] - no data is returned upon success - if not passed, a promise is returned
*/
VoltStorage.prototype.clear = function (scope, callback) {
if (typeof scope !== 'string') {
callback = scope;
scope = this.state[this.defaultScope];
}
return this.core.api('DELETE', '/storage/{{scope}}', {
scope: scope
}, undefined, callback);
};
/**
* Returns the value of the item associated with a key.
*
* @param {string} key - the name of the key to retrieve
* @param {voltCallback} [callback] - upon success, data will be the value of the key - if not passed, a promise is returned
*/
VoltStorage.prototype.getItem = function (key, scope, callback) {
if (typeof scope !== 'string') {
callback = scope;
scope = this.state[this.defaultScope];
}
return this.core.api('GET', '/storage/{{scope}}/key/{{key}}', {
scope: scope,
key: key
}, undefined, callback);
};
/**
* Set a value for a new or existing key.
*
* @param {string} key - the name of the key to create
* @param {*} value - the value to associate with the key
* @param {voltCallback} [callback] - no data is returned upon success - if not passed, a promise is returned
*/
VoltStorage.prototype.setItem = function (key, value, scope, callback) {
if (typeof scope !== 'string') {
callback = scope;
scope = this.state[this.defaultScope];
}
return this.core.api('PUT', '/storage/{{scope}}/key/{{key}}', {
scope: scope,
key: key
}, value, callback);
};
/**
* Removes the item associated with key.
*
* @param {string} key - the name of the key to remove
* @param {voltCallback} [callback] - no data is returned upon success - if not passed, a promise is returned
*/
VoltStorage.prototype.removeItem = function (key, scope, callback) {
if (typeof scope !== 'string') {
callback = scope;
scope = this.state[this.defaultScope];
}
return this.core.api('DELETE', '/storage/{{scope}}/key/{{key}}', {
scope: scope,
key: key
}, undefined, callback);
};
/**
* VoltStorage Plug-in Library
*
* Also aliased as `window.appStorage`.
*
* @namespace $volt.storage
* @borrows $volt.storage.VoltStorage#getAllItems as getAllItems
* @borrows $volt.storage.VoltStorage#clear as clear
* @borrows $volt.storage.VoltStorage#getItem as getItem
* @borrows $volt.storage.VoltStorage#setItem as setItem
* @borrows $volt.storage.VoltStorage#removeItem as removeItem
*/
$volt.register('storage', function (core, state) {
var storage = new VoltStorage(core, state, 'appId');
// alias for AppStudio users
window.appStorage = storage;
return storage;
});
/**
* VoltStorage Plug-in Library
*
* Also aliased as `window.serverStorage`.
*
* @namespace $volt.user.storage
* @borrows $volt.storage.VoltStorage#getAllItems as getAllItems
* @borrows $volt.storage.VoltStorage#clear as clear
* @borrows $volt.storage.VoltStorage#getItem as getItem
* @borrows $volt.storage.VoltStorage#setItem as setItem
* @borrows $volt.storage.VoltStorage#removeItem as removeItem
*/
$volt.register('user.storage', function (core, state) {
var userStorage = new VoltStorage(core, state, 'userId');
// alias for AppStudio users
window.serverStorage = userStorage;
return userStorage;
});
})();