2013-09-25 13:19:12 +02:00
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
|
* This file is part of Icinga Web 2.
|
2013-09-25 13:19:12 +02:00
|
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2013-09-25 13:19:12 +02:00
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
2013-10-23 15:10:33 +02:00
|
|
|
|
*
|
2013-09-25 13:19:12 +02:00
|
|
|
|
*/
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
2013-06-13 17:29:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* {{LICENSE_HEADER}}
|
|
|
|
|
* {{LICENSE_HEADER}}
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This node module acts as a mock for requirejs and allows you to
|
|
|
|
|
* define your own dependencies in your tests. It also removes the
|
|
|
|
|
* asynchronous character of dependency loading, so you don't need
|
|
|
|
|
* to handle everthing in the callbacks.
|
|
|
|
|
*
|
|
|
|
|
* Per default it resolves the 'logging' dependency by routing it
|
|
|
|
|
* to console.
|
|
|
|
|
*
|
|
|
|
|
**/
|
2013-06-21 15:33:06 +02:00
|
|
|
|
var path = require('path');
|
2013-06-13 17:29:38 +02:00
|
|
|
|
var registeredDependencies = {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mock for the requirejs(dependencyList, callback) function, loads
|
|
|
|
|
* all dependencies that have been registered under the given names
|
|
|
|
|
* in dependencies and calls fn with them as the parameter
|
|
|
|
|
*
|
|
|
|
|
**/
|
2013-06-21 15:33:06 +02:00
|
|
|
|
var debug = false;
|
2013-06-13 17:29:38 +02:00
|
|
|
|
var requireJsMock = function(dependencies, fn) {
|
|
|
|
|
var fnArgs = [];
|
|
|
|
|
for (var i=0;i<dependencies.length;i++) {
|
|
|
|
|
if (typeof registeredDependencies[dependencies[i]] === "undefined") {
|
2013-06-21 15:33:06 +02:00
|
|
|
|
if (debug === true)
|
|
|
|
|
console.warn("Unknown dependency "+dependencies[i]+" in define()");
|
2013-06-13 17:29:38 +02:00
|
|
|
|
}
|
|
|
|
|
fnArgs.push(registeredDependencies[dependencies[i]]);
|
|
|
|
|
}
|
|
|
|
|
fn.apply(this,fnArgs);
|
|
|
|
|
};
|
|
|
|
|
|
2013-08-20 12:00:41 +02:00
|
|
|
|
/**
|
|
|
|
|
* Mock the Logger
|
|
|
|
|
*/
|
|
|
|
|
var logger = {
|
|
|
|
|
debug: function() {},
|
|
|
|
|
warn: function() {},
|
|
|
|
|
error: function() {},
|
|
|
|
|
emergency: function() {}
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-13 17:29:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* Mock for the 'define' function of requireJS, behaves exactly the same
|
2013-09-11 17:11:13 +02:00
|
|
|
|
* except that it looks up the dependencies in the list provided by registerDependencies()
|
2013-06-13 17:29:38 +02:00
|
|
|
|
* A module that hasn't been defined with a name can be fetched with getDefined() (without parameter)
|
|
|
|
|
*
|
|
|
|
|
**/
|
|
|
|
|
var defineMock = function() {
|
|
|
|
|
var fn = function() {},
|
|
|
|
|
fnargs = [],
|
|
|
|
|
currentArg = 0,
|
|
|
|
|
scopeName = '__define__';
|
|
|
|
|
do {
|
|
|
|
|
var argType = typeof arguments[currentArg];
|
|
|
|
|
if( argType === "string") {
|
|
|
|
|
scopeName = arguments[currentArg];
|
|
|
|
|
currentArg++;
|
|
|
|
|
continue;
|
|
|
|
|
} else if (argType === "function") {
|
|
|
|
|
fn = arguments[currentArg];
|
|
|
|
|
} else if (Array.isArray(arguments[currentArg])) {
|
|
|
|
|
var argList = arguments[currentArg];
|
|
|
|
|
fn = arguments[currentArg+1];
|
|
|
|
|
for (var i=0;i<argList.length;i++) {
|
2013-06-21 15:33:06 +02:00
|
|
|
|
if (typeof registerDependencies[argList[i]] === "undefined" && debug) {
|
2013-06-27 11:19:04 +02:00
|
|
|
|
// console.warn("Unknown dependency "+argList[i]+" in define()");
|
2013-06-13 17:29:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fnargs.push(registeredDependencies[argList[i]]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
} while(true);
|
|
|
|
|
registeredDependencies[scopeName] = fn.apply(this,fnargs);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called on module initialisation, will register the
|
|
|
|
|
* requirejs, define and jquery '$' methods globally
|
|
|
|
|
* and also purge any module-global dependencies
|
|
|
|
|
*
|
|
|
|
|
**/
|
|
|
|
|
function initRequireMethods() {
|
|
|
|
|
GLOBAL.$ = require('jquery');
|
2013-09-11 17:11:13 +02:00
|
|
|
|
GLOBAL.jQuery = GLOBAL.$;
|
2013-06-13 17:29:38 +02:00
|
|
|
|
GLOBAL.requirejs = requireJsMock;
|
|
|
|
|
GLOBAL.define = defineMock;
|
|
|
|
|
registeredDependencies = {
|
|
|
|
|
'jquery' : GLOBAL.$,
|
2013-08-20 12:00:41 +02:00
|
|
|
|
'logging' : logger
|
2013-06-13 17:29:38 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
initRequireMethods();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets all additional dependencies, i.e. all dependencies
|
|
|
|
|
* without a name
|
|
|
|
|
**/
|
|
|
|
|
function purgeDependencies() {
|
|
|
|
|
registeredDependencies = {
|
|
|
|
|
'jquery' : GLOBAL.$,
|
2013-08-20 12:00:41 +02:00
|
|
|
|
'logging' : logger
|
2013-06-13 17:29:38 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
// helper to log debug messages with console
|
2013-06-27 11:19:04 +02:00
|
|
|
|
console.debug = function() {};
|
2013-06-13 17:29:38 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Registers a name=>object map of dependencies
|
|
|
|
|
* for lookup with requirejs()/define()
|
|
|
|
|
**/
|
|
|
|
|
function registerDependencies(obj) {
|
|
|
|
|
for(var name in obj) {
|
|
|
|
|
registeredDependencies[name] = obj[name];
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-21 15:33:06 +02:00
|
|
|
|
var base = path.normalize(__dirname+"../../../../public/js");
|
|
|
|
|
GLOBAL.requireNew = function(key) {
|
|
|
|
|
key = path.normalize(base+"/"+key);
|
|
|
|
|
delete require.cache[key];
|
|
|
|
|
return require(key);
|
|
|
|
|
};
|
2013-06-13 17:29:38 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The API for this module
|
|
|
|
|
**/
|
|
|
|
|
module.exports = {
|
|
|
|
|
purgeDependencies: purgeDependencies,
|
|
|
|
|
registerDependencies: registerDependencies,
|
|
|
|
|
getDefine: function(name) {
|
2013-06-14 09:50:06 +02:00
|
|
|
|
if (typeof name === "undefined") {
|
2013-06-13 17:29:38 +02:00
|
|
|
|
return registeredDependencies.__define__;
|
|
|
|
|
} else {
|
|
|
|
|
return registeredDependencies[name];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-06-21 15:33:06 +02:00
|
|
|
|
|