Repository navigation
Expand file tree
/
Copy pathByteForge-bulk-processor.js
More file actions
253 lines (225 loc) · 9.71 KB
/
Copy pathByteForge-bulk-processor.js
File metadata and controls
253 lines (225 loc) · 9.71 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
(function (window, $) {
'use strict';
/**
* Creates a reusable preview-then-apply bulk processor controller.
* @param {object} options Bulk processing configuration.
* @param {string} options.previewUrl Server endpoint used for non-mutating previews.
* @param {string} options.applyUrl Server endpoint used for applying the last previewed import.
* @param {string} options.antiForgeryToken ASP.NET anti-forgery token value.
* @param {string} options.actionButtonSelector Selector for the preview/apply button.
* @param {string} options.errorSelector Selector for the inline error element.
* @param {string} options.summarySelector Selector for the preview summary element.
* @param {string} options.previewText Button text used before previewing.
* @param {string} options.previewingText Button text used while previewing.
* @param {string} options.applyText Button text used when the last preview can be applied.
* @param {string} options.applyingText Button text used while applying.
* @param {string} options.noChangesText Button text used when preview has no changes.
* @param {Function} options.getPayload Callback that returns the POST payload for preview/apply.
* @param {Function} options.getSignature Callback that returns a stable signature for current input.
* @param {Function} options.hasChanges Callback that returns whether the preview has applyable changes.
* @param {Function} options.renderPreview Callback that renders the preview payload.
* @param {Function} options.renderAppliedSummary Callback that renders the apply result summary.
* @param {Function} options.onApplied Callback invoked after a successful apply.
* @param {object} options.summaryClasses Optional CSS classes for the default applied summary.
* @returns {object} Public controller methods.
*/
function create(options) {
options = options || {};
var lastPreview = null;
var lastPreviewSignature = '';
/**
* Gets the configured action button.
* @returns {jQuery} The action button.
*/
function getButton() {
return $(options.actionButtonSelector || '#bulkApplyBtn');
}
/**
* Gets the configured error element.
* @returns {jQuery} The error element.
*/
function getError() {
return $(options.errorSelector || '#bulkModalError');
}
/**
* Gets the configured summary element.
* @returns {jQuery} The summary element.
*/
function getSummary() {
return $(options.summarySelector || '#bulkSummary');
}
/**
* Builds a POST payload with the anti-forgery token included.
* @returns {object} The POST payload.
*/
function buildPayload() {
var payload = {};
if (typeof options.getPayload === 'function') {
payload = options.getPayload() || {};
}
payload.__RequestVerificationToken = options.antiForgeryToken || '';
return payload;
}
/**
* Gets a stable signature for the current import inputs.
* @returns {string} Input signature.
*/
function getSignature() {
if (typeof options.getSignature === 'function') {
return String(options.getSignature() || '');
}
return JSON.stringify(buildPayload());
}
/**
* Resets the controller to preview mode after import input changes.
* @returns {void}
*/
function reset() {
lastPreview = null;
lastPreviewSignature = '';
getButton().prop('disabled', false).text(options.previewText || 'Preview');
}
/**
* Determines whether a preview contains rows that can be applied.
* @param {object} preview Preview payload from the server.
* @returns {boolean} True when the preview has inserts or updates.
*/
function hasChanges(preview) {
if (typeof options.hasChanges === 'function') {
return !!options.hasChanges(preview);
}
preview = preview || {};
return (parseInt(preview.inserts, 10) || 0) > 0 || (parseInt(preview.updates, 10) || 0) > 0;
}
/**
* Requests a server-side preview for the current import text.
* @returns {void}
*/
function preview() {
var $btn = getButton();
$btn.prop('disabled', true).text(options.previewingText || 'Previewing...');
getError().text('');
$.ajax({
url: options.previewUrl,
type: 'POST',
dataType: 'json',
data: buildPayload(),
success: function (result) {
if (!result.success) {
$btn.prop('disabled', false).text(options.previewText || 'Preview');
getError().text(result.error || 'Preview failed.');
return;
}
lastPreview = result.preview;
lastPreviewSignature = getSignature();
if (typeof options.renderPreview === 'function') {
options.renderPreview(result.preview);
}
if (hasChanges(result.preview)) {
$btn.prop('disabled', false).text(options.applyText || 'Apply Upsert');
} else {
$btn.prop('disabled', true).text(options.noChangesText || 'No Changes');
}
},
error: function (xhr) {
$btn.prop('disabled', false).text(options.previewText || 'Preview');
getError().text('Request failed (' + xhr.status + ').');
}
});
}
/**
* Applies the last previewed import when the input signature still matches.
* @returns {void}
*/
function apply() {
if (!lastPreview || lastPreviewSignature !== getSignature()) {
preview();
return;
}
var $btn = getButton();
$btn.prop('disabled', true).text(options.applyingText || 'Applying...');
getError().text('');
$.ajax({
url: options.applyUrl,
type: 'POST',
dataType: 'json',
data: buildPayload(),
success: function (result) {
$btn.prop('disabled', false).text(options.applyText || 'Apply Upsert');
if (!result.success) {
getError().text(result.error || 'Bulk apply failed.');
return;
}
renderAppliedSummary(result.result || {});
if (typeof options.onApplied === 'function') {
options.onApplied(result.result || {});
}
},
error: function (xhr) {
$btn.prop('disabled', false).text(options.applyText || 'Apply Upsert');
getError().text('Request failed (' + xhr.status + ').');
}
});
}
/**
* Renders the standard applied summary line.
* @param {object} applyResult Apply result counters.
* @returns {void}
*/
function renderAppliedSummary(applyResult) {
if (typeof options.renderAppliedSummary === 'function') {
options.renderAppliedSummary(applyResult, getSummary());
return;
}
var classes = options.summaryClasses || {};
var $summary = getSummary();
$summary.empty();
$('<strong>').text('Applied.').appendTo($summary);
$summary.append(' ');
appendCount($summary, classes.inserts || 'bf-bulk-processor-count bf-bulk-processor-count-inserts', applyResult.inserts, 'inserts');
$summary.append(' ');
appendCount($summary, classes.updates || 'bf-bulk-processor-count bf-bulk-processor-count-updates', applyResult.updates, 'updates');
$summary.append(' ');
appendCount($summary, classes.skipped || 'bf-bulk-processor-count bf-bulk-processor-count-skipped', applyResult.skipped, 'skipped');
}
/**
* Appends one sanitized count span to the summary.
* @param {jQuery} $summary Summary element.
* @param {string} className CSS class list for the count span.
* @param {*} value Raw count value.
* @param {string} label Count label.
* @returns {void}
*/
function appendCount($summary, className, value, label) {
var count = parseInt(value, 10);
if (isNaN(count)) {
count = 0;
}
$('<span>')
.addClass(className)
.text(count + ' ' + label)
.appendTo($summary);
}
/**
* Performs preview first, then apply when the current input matches the preview.
* @returns {void}
*/
function handleAction() {
if (!lastPreview || lastPreviewSignature !== getSignature()) {
preview();
return;
}
apply();
}
return {
reset: reset,
preview: preview,
apply: apply,
handleAction: handleAction,
renderAppliedSummary: renderAppliedSummary
};
}
window.ByteForgeBulkProcessor = {
create: create
};
}(window, window.jQuery));