From c0360e7beb7915580f27c9bb761fe2464c92140f Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 1 Sep 2014 16:31:35 -0300 Subject: [PATCH] plugin system v0 --- Gruntfile.js | 1 + config.js | 4 +-- copay.js | 1 + js/app.js | 6 ----- js/controllers/home.js | 2 +- js/models/core/PluginManager.js | 46 +++++++++++++++++++++++++++++++++ js/services/pluginManager.js | 3 +++ plugins/googleDrive.js | 11 ++++++++ util/build.js | 10 +++++-- 9 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 js/models/core/PluginManager.js create mode 100644 js/services/pluginManager.js create mode 100644 plugins/googleDrive.js diff --git a/Gruntfile.js b/Gruntfile.js index fc4b77dee..04078ba62 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -44,6 +44,7 @@ module.exports = function(grunt) { files: [ 'js/models/**/*.js', 'plugins/*.js', + 'copay.js' ], tasks: ['shell:dev'] }, diff --git a/config.js b/config.js index fe4031e42..6b7e5f30f 100644 --- a/config.js +++ b/config.js @@ -53,10 +53,10 @@ var defaultConfig = { verbose: 1, plugins: { - googleDrive: true, + GoogleDrive: true, }, - googleDrive: { + GoogleDrive: { clientId: '1', }, }; diff --git a/copay.js b/copay.js index e821f7918..2f0107816 100644 --- a/copay.js +++ b/copay.js @@ -16,6 +16,7 @@ var StorageEncrypted = module.exports.StorageEncrypted = require('./js/models/st module.exports.WalletFactory = require('./js/models/core/WalletFactory'); module.exports.Wallet = require('./js/models/core/Wallet'); module.exports.WalletLock = require('./js/models/core/WalletLock'); +module.exports.PluginManager = require('./js/models/core/PluginManager'); module.exports.version = require('./version').version; module.exports.commitHash = require('./version').commitHash; diff --git a/js/app.js b/js/app.js index e7b038516..e1a3826ee 100644 --- a/js/app.js +++ b/js/app.js @@ -40,12 +40,6 @@ var modules = [ if (config.plugins.length) modules.push('angularLoad'); -if (config.plugins.googleDrive) { - var googleDrive = require('../plugins/googleDrive'); - var a = new googleDrive(); - a.init(); -console.log('[app.js.41:new:]',a); //TODO -} var copayApp = window.copayApp = angular.module('copayApp', modules); diff --git a/js/controllers/home.js b/js/controllers/home.js index 2ac3e7381..a8d367d44 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) { + function($scope, $rootScope, $location, walletFactory, notification, controllerUtils, pluginManager) { controllerUtils.redirIfLogged(); diff --git a/js/models/core/PluginManager.js b/js/models/core/PluginManager.js new file mode 100644 index 000000000..995470481 --- /dev/null +++ b/js/models/core/PluginManager.js @@ -0,0 +1,46 @@ +'use strict'; +var preconditions = require('preconditions').singleton(); + +function PluginManager(config) { + this.registered = {}; + + for(var ii in config.plugins){ + var pluginName = ii; + + if (!config.plugins[pluginName]) + continue; + + console.log('Loading ' + pluginName); + var pluginClass = require('../plugins/' + pluginName); + var pluginObj = new pluginClass(); + pluginObj.init(); + this._register(pluginObj); + } +}; + +var KIND_UNIQUE = PluginManager.KIND_UNIQUE = 1; +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'); + 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'); + + if (kind === PluginManager.KIND_UNIQUE) { + this.registered[type] = obj; + } else { + this.registered[type] = this.registered[type] || []; + this.registered[type].push(obj); + } +}; + +PluginManager.prototype.getOne = function(type) {}; + +module.exports = PluginManager; diff --git a/js/services/pluginManager.js b/js/services/pluginManager.js new file mode 100644 index 000000000..b3371c0cd --- /dev/null +++ b/js/services/pluginManager.js @@ -0,0 +1,3 @@ +'use strict'; + +angular.module('copayApp.services').value('pluginManager', new copay.PluginManager(config)); diff --git a/plugins/googleDrive.js b/plugins/googleDrive.js new file mode 100644 index 000000000..6be0c2c55 --- /dev/null +++ b/plugins/googleDrive.js @@ -0,0 +1,11 @@ +'use strict'; + +function GoogleDrive() { + this.type = 'STORAGE'; +}; + +GoogleDrive.prototype.init = function() { + console.log('[googleDrive.js.3] init'); //TODO +}; + +module.exports = GoogleDrive; diff --git a/util/build.js b/util/build.js index 21e2afda8..b9160cc7f 100644 --- a/util/build.js +++ b/util/build.js @@ -83,9 +83,15 @@ var createBundle = function(opts) { b.require('./js/models/core/HDPath', { expose: '../js/models/core/HDPath' }); - b.require('./plugins/googleDrive', { - expose: '../plugins/googleDrive' + b.require('./js/models/core/PluginManager', { + expose: '../js/models/core/PluginManager' }); + + if (!opts.disablePlugins) { + b.require('./plugins/GoogleDrive', { + expose: '../plugins/GoogleDrive' + }); + } b.require('./config', { expose: '../config'