From b9881c114767f8c4e9f8c9e4ed0c7e2451232ff6 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 1 Sep 2014 23:44:35 -0300 Subject: [PATCH] add LocalStorage plugin --- Gruntfile.js | 3 ++- config.js | 3 ++- js/controllers/home.js | 2 +- js/models/core/PluginManager.js | 18 +++++++------- js/models/core/WalletFactory.js | 6 +++-- js/models/storage/Encrypted.js | 2 ++ js/services/pluginManager.js | 4 +++- plugins/LocalStorage.js | 42 +++++++++++++++++++++++++++++++++ plugins/googleDrive.js | 33 +++++++++++++++++++++++++- util/build.js | 4 ++++ 10 files changed, 101 insertions(+), 16 deletions(-) create mode 100644 plugins/LocalStorage.js diff --git a/Gruntfile.js b/Gruntfile.js index 04078ba62..099cd23bd 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -44,7 +44,8 @@ module.exports = function(grunt) { files: [ 'js/models/**/*.js', 'plugins/*.js', - 'copay.js' + 'copay.js', + 'utils/*.js' ], tasks: ['shell:dev'] }, diff --git a/config.js b/config.js index 6b7e5f30f..10b5dafe7 100644 --- a/config.js +++ b/config.js @@ -53,7 +53,8 @@ var defaultConfig = { verbose: 1, plugins: { - GoogleDrive: true, + LocalStorage: true, + // GoogleDrive: true, }, GoogleDrive: { diff --git a/js/controllers/home.js b/js/controllers/home.js index a8d367d44..2ac3e7381 100644 --- a/js/controllers/home.js +++ b/js/controllers/home.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('copayApp.controllers').controller('HomeController', - function($scope, $rootScope, $location, walletFactory, notification, controllerUtils, pluginManager) { + function($scope, $rootScope, $location, walletFactory, notification, controllerUtils) { controllerUtils.redirIfLogged(); diff --git a/js/models/core/PluginManager.js b/js/models/core/PluginManager.js index 995470481..23b3c2a77 100644 --- a/js/models/core/PluginManager.js +++ b/js/models/core/PluginManager.js @@ -10,11 +10,11 @@ function PluginManager(config) { if (!config.plugins[pluginName]) continue; - console.log('Loading ' + pluginName); + console.log('Loading plugin: ' + pluginName); var pluginClass = require('../plugins/' + pluginName); var pluginObj = new pluginClass(); pluginObj.init(); - this._register(pluginObj); + this._register(pluginObj, pluginName); } }; @@ -24,14 +24,12 @@ var KIND_MULTIPLE = PluginManager.KIND_MULTIPLE = 2; PluginManager.TYPE = {}; PluginManager.TYPE['STORAGE'] = KIND_UNIQUE; -PluginManager.prototype._register = function(obj) { - preconditions.checkArgument(obj.type,'Plugin has not type'); +PluginManager.prototype._register = function(obj, name) { + preconditions.checkArgument(obj.type,'Plugin has not type:' + name); var type = obj.type; -console.log('[PluginManager.js.29:type:]',type); //TODO - var kind = PluginManager.TYPE[type]; - preconditions.checkArgument(kind, 'Plugin has unkown type'); - preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered'); + preconditions.checkArgument(kind, 'Plugin has unkown type' + name); + preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered: ' + name); if (kind === PluginManager.KIND_UNIQUE) { this.registered[type] = obj; @@ -41,6 +39,8 @@ console.log('[PluginManager.js.29:type:]',type); //TODO } }; -PluginManager.prototype.getOne = function(type) {}; +PluginManager.prototype.get = function(type) { + return this.registered[type]; +}; module.exports = PluginManager; diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index adf542ae1..ef88b6a52 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -36,11 +36,13 @@ function WalletFactory(config, version) { var self = this; config = config || {}; - this.Storage = config.Storage || StorageEncrypted; + this.pluginManager = new copay.PluginManager(config); + + this.Storage = config.Storage || StorageEncrypted; this.Network = config.Network || Async; this.Blockchain = config.Blockchain || Insight; + this.storage = new this.Storage({storage: this.pluginManager.get('STORAGE')}); - this.storage = new this.Storage(config.storage); this.networks = { 'livenet': new this.Network(config.network.livenet), 'testnet': new this.Network(config.network.testnet), diff --git a/js/models/storage/Encrypted.js b/js/models/storage/Encrypted.js index 1997f7841..6a0c378e7 100644 --- a/js/models/storage/Encrypted.js +++ b/js/models/storage/Encrypted.js @@ -130,6 +130,8 @@ Storage.prototype.getWalletIds = function() { var walletIds = []; var uniq = {}; +console.log('[Encrypted.js.144]', this.storage); //TODO +console.log('[Encrypted.js.144]', this.storage.length); //TODO for (var i = 0; i < this.storage.length; i++) { var key = this.storage.key(i); var split = key.split('::'); diff --git a/js/services/pluginManager.js b/js/services/pluginManager.js index b3371c0cd..c67b16846 100644 --- a/js/services/pluginManager.js +++ b/js/services/pluginManager.js @@ -1,3 +1,5 @@ 'use strict'; -angular.module('copayApp.services').value('pluginManager', new copay.PluginManager(config)); +angular.module('copayApp.services').factory('pluginManager', function(angularLoad){ + return new copay.PluginManager(config); +}); diff --git a/plugins/LocalStorage.js b/plugins/LocalStorage.js new file mode 100644 index 000000000..3f050f64f --- /dev/null +++ b/plugins/LocalStorage.js @@ -0,0 +1,42 @@ +'use strict'; + +function LocalStorage() { + this.type = 'STORAGE'; +}; + +LocalStorage.prototype.init = function() { + console.log(' init LocalStorage'); //TODO +}; + + +LocalStorage.prototype.getItem = function(k) { + return localStorage.getItem(k); +}; + +LocalStorage.prototype.setItem = function(k,v) { + localStorage.setItem(k,v); +}; + +LocalStorage.prototype.removeItem = function(k) { + localStorage.removeItem(k); +}; + +LocalStorage.prototype.clear = function() { + localStorage.clear(); +}; + +delete LocalStorage.prototype.length; + +Object.defineProperty(LocalStorage.prototype, 'length', { + get: function() { + return localStorage.length; + } +}); + +LocalStorage.prototype.key = function(k) { + var v = localStorage.key(k); + return v; +}; + + +module.exports = LocalStorage; diff --git a/plugins/googleDrive.js b/plugins/googleDrive.js index 6be0c2c55..7d7544b79 100644 --- a/plugins/googleDrive.js +++ b/plugins/googleDrive.js @@ -5,7 +5,38 @@ function GoogleDrive() { }; GoogleDrive.prototype.init = function() { - console.log('[googleDrive.js.3] init'); //TODO + console.log('[googleDrive.js.3] init GoogleDrive'); //TODO }; + +GoogleDrive.prototype.getItem = function(k) { + return localStorage.getItem(k); +}; + +GoogleDrive.prototype.setItem = function(k,v) { + localStorage.setItem(k,v); +}; + +GoogleDrive.prototype.removeItem = function(k) { + localStorage.removeItem(k); +}; + +GoogleDrive.prototype.clear = function() { + localStorage.clear(); +}; + +delete GoogleDrive.prototype.length; + +Object.defineProperty(GoogleDrive.prototype, 'length', { + get: function() { + return localStorage.length; + } +}); + +GoogleDrive.prototype.key = function(k) { + var v = localStorage.key(k); + return v; +}; + + module.exports = GoogleDrive; diff --git a/util/build.js b/util/build.js index b9160cc7f..080c1e47e 100644 --- a/util/build.js +++ b/util/build.js @@ -91,6 +91,10 @@ var createBundle = function(opts) { b.require('./plugins/GoogleDrive', { expose: '../plugins/GoogleDrive' }); + b.require('./plugins/LocalStorage', { + expose: '../plugins/LocalStorage' + }); + } b.require('./config', {