Create JavaScript's PluginManager class

This commit is contained in:
Dmitriy Simushev 2012-10-04 09:20:26 +00:00
parent 53e9400dde
commit c61789c2d7
4 changed files with 78 additions and 0 deletions

View File

@ -16,5 +16,9 @@
<script type="text/javascript" src="test_cases/mibewapi_tests.js"></script>
<!-- End of the mibew_api.js tests -->
<!-- Start of the PluginManager class tests -->
<script type="text/javascript" src="/webim/js/164/pluginmanager.js"></script>
<script type="text/javascript" src="test_cases/pluginmanager_tests.js"></script>
<!-- End of the mibew_api.js class tests -->
</body>
</html>

View File

@ -0,0 +1,21 @@
// Testing PluginManager class
module('PluginManager');
test('getPlugin', function() {
var pluginManager = new PluginManager();
// Try to load not stored plugin
equal(
pluginManager.getPlugin('WrongPlugin'),
false,
'Test loading not stored plugin'
)
// Try save and load test plugin
var testPlugin = {'testField': 'testValue'}
pluginManager.addPlugin('TestPlugin', testPlugin);
deepEqual(
pluginManager.getPlugin('TestPlugin'),
testPlugin,
'Test loading stored plugin'
);
});

View File

@ -0,0 +1,8 @@
/*
This file is part of Mibew Messenger project.
http://mibew.org
Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php
*/
var PluginManager=function(){var b={};this.addPlugin=function(a,c){b[a]=c};this.getPlugin=function(a){return b[a]?b[a]:!1}};

View File

@ -0,0 +1,45 @@
/**
* @preserve This file is part of Mibew Messenger project.
* http://mibew.org
*
* Copyright (c) 2005-2011 Mibew Messenger Community
* License: http://mibew.org/license.php
*/
/**
* Create an instance of plugin manager
* @constructor
*/
var PluginManager = function() {
/**
* Contains all added plugins
* @type Array
* @private
*/
var pluginsList = {}
/**
* Add plugin to internal plugins list
*
* @param {String} pluginName Name of the added plugin. Uses to get plugin
* by the PluginManager.getPlugin() method
* @param {Object} plugin A plugin object
*/
this.addPlugin = function(pluginName, plugin) {
pluginsList[pluginName] = plugin;
}
/**
* Get plugin object from internal storage
*
* @returns {Object|Boolean} Plugin object if it was added by the
* PluginManager.addPlugin method and boolean false otherwise.
*/
this.getPlugin = function(pluginName) {
if (pluginsList[pluginName]) {
return pluginsList[pluginName];
}
return false;
}
}