-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathreindex.js
168 lines (142 loc) · 4.98 KB
/
reindex.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
*/
(
function ($) {
Craft.ElasticsearchUtility = Garnish.Base.extend({
$trigger: null,
$form: null,
$innerProgressBar: null,
formAction: null,
totalActions: null,
completedActions: null,
loadingActions: null,
queue: null,
init: function (formId) {
this.$form = $('#' + formId);
this.$trigger = $('input.submit', this.$form);
this.$status = $('.utility-status', this.$form);
this.formAction = this.$form.attr('action');
console.log(this.formAction);
this.addListener(this.$form, 'submit', this.onSubmit);
},
onSubmit: function (ev) {
ev.preventDefault();
ev.stopPropagation();
if (!this.$trigger.hasClass('disabled')) {
if (!this.progressBar) {
this.progressBar = new Craft.ProgressBar(this.$status);
} else {
this.progressBar.resetProgressBar();
}
this.totalActions = 1;
this.completedActions = 0;
this.queue = [];
this.loadingActions = 0;
this.currentEntryQueue = [];
this.progressBar.$progressBar.css({
top: Math.round(this.$status.outerHeight() / 2) - 6,
}).removeClass('hidden');
this.progressBar.$progressBar.velocity('stop').velocity(
{
opacity: 1,
},
{
complete: $.proxy(function () {
var postData = Garnish.getPostData(this.$form);
var params = Craft.expandPostArray(postData);
params.start = true;
this.loadAction({
params: params,
});
}, this),
},
);
if (this.$allDone) {
this.$allDone.css('opacity', 0);
}
this.$trigger.addClass('disabled');
this.$trigger.trigger('blur');
}
},
updateProgressBar: function () {
var width = (
100 * this.completedActions / this.totalActions
);
this.progressBar.setProgressPercentage(width);
},
loadAction: function (data) {
this.loadingActions++;
this.postActionRequest(data.params);
},
postActionRequest: function (params) {
var data = {
params: params,
};
Craft.postActionRequest(
this.formAction,
data,
$.proxy(this, 'onActionResponse'),
{
complete: $.noop,
},
);
},
onActionResponse: function (response, textStatus) {
this.loadingActions--;
this.completedActions++;
// Add any new entries to the queue?
if (textStatus === 'success' && response && response.entries) {
for (var i = 0; i < response.entries.length; i++) {
if (response.entries[ i ].length) {
this.totalActions += response.entries[ i ].length;
this.queue.push(response.entries[ i ]);
}
}
}
if (response && response.error) {
alert(response.error);
}
this.updateProgressBar();
// Load as many additional items in the current batch as possible
while (this.loadingActions < Craft.ElasticsearchUtility.maxConcurrentActions &&
this.currentEntryQueue.length) {
this.loadNextAction();
}
// Was that the end of the batch?
if (!this.loadingActions) {
// Is there another batch?
if (this.queue.length > 0) {
this.currentEntryQueue = this.queue.shift();
this.loadNextAction();
} else {
// Quick delay so things don't look too crazy.
setTimeout($.proxy(this, 'onComplete'), 300);
}
}
},
loadNextAction: function () {
var data = this.currentEntryQueue.shift();
this.loadAction(data);
},
onComplete: function () {
if (!this.$allDone) {
this.$allDone = $('<div class="alldone" data-icon="done" />').appendTo(this.$status);
this.$allDone.css('opacity', 0);
}
this.progressBar.$progressBar.velocity({ opacity: 0 }, {
duration: 'fast', complete: $.proxy(function () {
this.$allDone.velocity({ opacity: 1 }, { duration: 'fast' });
this.$trigger.removeClass('disabled');
this.$trigger.trigger('focus');
}, this),
});
},
}, {
maxConcurrentActions: 3,
});
}
)(jQuery);