indexer.js
3.11 KB
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
var FinderIndexer = function(){
var totalItems= null;
var batchSize= null;
var offset= null;
var progress= null;
var optimized= false;
var path = 'index.php?option=com_finder&tmpl=component&format=json';
var initialize = function () {
offset = 0;
progress = 0;
path = path + '&' + jQuery('#finder-indexer-token').attr('name') + '=1';
getRequest('indexer.start');
};
var getRequest= function (task) {
jQuery.ajax({
type : "GET",
url : path,
data : 'task=' + task,
dataType : 'json',
success : handleResponse,
error : handleFailure
});
};
var handleResponse = function (json, resp) {
try {
if (json === null) {
throw resp;
}
if (json.error) {
throw json;
}
if (json.start) {
totalItems = json.totalItems;
}
offset += json.batchOffset;
updateProgress(json.header, json.message);
if (offset < totalItems) {
getRequest('indexer.batch');
} else if (!optimized) {
optimized = true;
getRequest('indexer.optimize');
}
} catch (error) {
jQuery('#progress').remove();
try {
if (json.error) {
jQuery('#finder-progress-header').text(json.header).addClass('finder-error');
jQuery('#finder-progress-message').html(json.message).addClass('finder-error');
}
} catch (ignore) {
if (error === '') {
error = Joomla.JText._('COM_FINDER_NO_ERROR_RETURNED');
}
jQuery('#finder-progress-header').text(Joomla.JText._('COM_FINDER_AN_ERROR_HAS_OCCURRED')).addClass('finder-error');
jQuery('#finder-progress-message').html(error).addClass('finder-error');
}
}
return true;
};
var handleFailure= function (xhr) {
json = (typeof xhr == 'object' && xhr.responseText) ? xhr.responseText : null;
json = json ? jQuery.parseJSON(json) : null;
jQuery('#progress').remove();
if (json) {
json = json.responseText != null ? Json.evaluate(json.responseText, true) : json;
}
var header = json ? json.header : Joomla.JText._('COM_FINDER_AN_ERROR_HAS_OCCURRED');
var message = json ? json.message : Joomla.JText._('COM_FINDER_MESSAGE_RETURNED') + ' <br />' + json;
jQuery('#finder-progress-header').text(header).addClass('finder-error');
jQuery('#finder-progress-message').html(message).addClass('finder-error');
};
var updateProgress = function (header, message) {
progress = (offset / totalItems) * 100;
jQuery('#finder-progress-header').text(header);
jQuery('#finder-progress-message').html(message);
if (progress < 100) {
jQuery('#progress-bar').css('width', progress + '%').attr('aria-valuenow', progress);
}
else {
jQuery('#progress-bar').removeClass('bar-success').addClass('bar-warning').attr('aria-valuemin', 100).attr('aria-valuemax', 200);
jQuery('#progress-bar').css('width', progress + '%').attr('aria-valuenow', progress);
}
if (message == msg) {
jQuery('#progress').remove();
window.parent.jQuery('#modal-archive', parent.document).modal('hide');
}
};
initialize();
};
jQuery(function () {
Indexer = new FinderIndexer();
if (typeof window.parent.SqueezeBox == 'object') {
jQuery(window.parent.SqueezeBox).on('close', function () {
window.parent.location.reload(true);
});
}
});