-
Notifications
You must be signed in to change notification settings - Fork 21
/
plugins.js
63 lines (58 loc) · 1.94 KB
/
plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Orange angular-swagger-ui - v0.3.0
*
* (C) 2015 Orange, all right reserved
* MIT Licensed
*/
'use strict';
angular
.module('sw.plugins', []).factory('plugins', function ($q) {
var modules = {};
return {
BEFORE_LOAD: 'BEFORE_LOAD',
BEFORE_PARSE: 'BEFORE_PARSE',
PARSE: 'PARSE',
BEFORE_DISPLAY: 'BEFORE_DISPLAY',
BEFORE_EXPLORER_LOAD: 'BEFORE_EXPLORER_LOAD',
AFTER_EXPLORER_LOAD: 'AFTER_EXPLORER_LOAD',
/**
* Adds a new module to swagger-ui
*/
add: function (phase, module) {
if (!modules[phase]) {
modules[phase] = [];
}
if (modules[phase].indexOf(module) < 0) {
modules[phase].push(module);
}
},
/**
* Executes modules' phase
*/
execute: function () {
var args = Array.prototype.slice.call(arguments);
var phase = args.splice(0, 1);
var deferred = $q.defer();
var phaseModules = modules[phase] || [];
executeAll(deferred, [].concat(phaseModules), args);
return deferred.promise;
}
};
/**
* Runs modules' "execute" function one by one
*/
function executeAll (deferred, phaseModules, args, phaseExecuted) {
var module = phaseModules.shift();
if (module) {
module
.execute.apply(module, args)
.then(function (executed) {
phaseExecuted = phaseExecuted || executed;
executeAll(deferred, phaseModules, args, phaseExecuted);
})
.catch(deferred.reject);
} else {
deferred.resolve(phaseExecuted);
}
}
});