summaryrefslogtreecommitdiffstats
path: root/script/install.js
blob: 719fab413d12bff455d4e303fa31c2e968d448d0 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var onceOnlyGoddammit = false;
var slxModules = {};
var slxTries = {};
var slxCurrent = false;
var slxTimer = false;

function slxRunInstall() {
	if (onceOnlyGoddammit)
		return;
	onceOnlyGoddammit = true;
	$('.install-btn').prop('disabled', true);
	var first = false;
	$('.id-col').each(function () {
		var id = $(this).text();
		slxModules[id] = 'IDLE';
		slxTries[id] = 0;
		if (first === false) {
			first = id;
		}
	});
	if (first !== false) {
		slxRun(first);
	}
}

function makeCallback(callback, userData) {
	return function (firstParam) {
		callback(this, userData, firstParam);
	};
}

function slxRun(moduleName) {
	var dest = $('#mod-' + moduleName);
	if (dest.length !== 1 || typeof slxModules[moduleName] === 'undefined') {
		alert('No such module: ' + moduleName);
		return;
	}
	if (slxModules[moduleName] === 'IDLE' || slxModules[moduleName] === 'UPDATE_RETRY') {
		if (slxTries[moduleName]++ > 3)
			return;
		slxModules[moduleName] = 'WORKING';
		dest.text('Working.....');
		slxCancelTimer();
		slxCurrent = moduleName;
		$.post('install.php', {module: moduleName}, makeCallback(slxDone, moduleName), 'json')
			.always(makeCallback(slxTrigger, moduleName));
		slxTimer = window.setTimeout(slxWatchdog, 5000);
	}
}

var slxDone = function (elem, moduleName, jsonReply) {
	if (!jsonReply) {
		jsonReply = {};
	}
	if (!jsonReply.status) {
		jsonReply.status = 'UPDATE_FAILED';
		jsonReply.message = 'Unknown/no status code received from server';
	}
	var status = jsonReply.status;
	if (jsonReply.message) {
		status = status + ' (' + jsonReply.message + ')';
	}
	slxModules[moduleName] = jsonReply.status;
	$('#mod-' + moduleName).text(status);
	if (jsonReply.status === 'UPDATE_DONE') {
		$('#mod-' + moduleName).css('color', '#0c0');
	}
	if (jsonReply.status === 'UPDATE_FAILED') {
		$('#mod-' + moduleName).css('color', '#c00');
	}
	if (jsonReply.status === 'UPDATE_RETRY') {
		$('#mod-' + moduleName).css('color', '#c50');
	}
}

var slxTrigger = function (elem, moduleName) {
	//alert('always: ' + moduleName + ', status: ' + slxModules[moduleName] + ', current: ' + slxCurrent);
	if (slxModules[moduleName] === 'WORKING') {
		slxModules[moduleName] = 'UPDATE_FAILED';
		$('#mod-' + moduleName).text('UPDATE_FAILED (No response from server)').css('color', '#c00');
	}
	if (slxCurrent === moduleName) {
		slxCurrent = false;
		slxRunNext(moduleName);
	}
}

function slxRunNext(lastModule) {
	var next = false;
	var first = false;
	for (var key in slxModules) {
		if (!slxModules.hasOwnProperty(key))
			continue;
		//
		if (slxTries[key] < 3 && (slxModules[key] === 'IDLE' || slxModules[key] === 'UPDATE_RETRY')) {
			if (next === true) {
				next = key;
				break;
			}
			if (first === false) {
				first = key;
			}
		}
		if (next === false && key === lastModule) {
			next = true;
		}
	}
	if (next === false || next === true) {
		next = first;
	}
	if (next !== false) {
		slxRun(next);
	} else {
		slxCancelTimer();
		setTimeout(function() {
			alert('Done.');
		}, 10);
	}
}

function slxCancelTimer()
{
	if (slxTimer !== false) {
		window.clearTimeout(slxTimer);
		slxTimer = false;
	}
}

var slxWatchdog = function()
{
	slxTimer = false;
	slxRunNext(slxCurrent);
}