adapt-authoring-ui 1.4.1 → 1.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app/core/helpers.js
CHANGED
|
@@ -324,20 +324,35 @@ define(['handlebars', 'moment', 'core/origin'], function(Handlebars, Moment, Ori
|
|
|
324
324
|
|
|
325
325
|
submitForm($form, options = {}) {
|
|
326
326
|
return new Promise(async (resolve, reject) => {
|
|
327
|
+
if($form.$el) $form = $form.$el; // for Scaffold forms
|
|
328
|
+
const method = options.method || $form.attr('method');
|
|
329
|
+
const url = options.url || $form.attr('action');
|
|
330
|
+
if(!method || !url) {
|
|
331
|
+
console.error('Helpers#submitForm: method and URL must be specified either as form attributes or options');
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
327
334
|
const body = new FormData($form[0]);
|
|
328
|
-
if(options.
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
335
|
+
if(options.data) {
|
|
336
|
+
Object.entries(options.data).forEach(d => body.append(...d));
|
|
337
|
+
delete options.data;
|
|
338
|
+
}
|
|
339
|
+
if(typeof options.beforeSubmit === 'function') {
|
|
340
|
+
options.beforeSubmit(body);
|
|
341
|
+
delete options.beforeSubmit;
|
|
332
342
|
}
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
|
|
343
|
+
const xhr = new XMLHttpRequest();
|
|
344
|
+
xhr.open(method, url, true);
|
|
345
|
+
xhr.onprogress = typeof options.onProgress === 'function' ? options.onProgress : () => {};
|
|
346
|
+
xhr.onreadystatechange = () => {
|
|
347
|
+
if(xhr.readyState !== XMLHttpRequest.DONE) return;
|
|
348
|
+
let responseData = xhr.response;
|
|
349
|
+
try { responseData = JSON.parse(responseData); } catch(e) {}
|
|
350
|
+
xhr.status > 299 ? reject(responseData) : resolve(responseData);
|
|
336
351
|
}
|
|
337
|
-
|
|
352
|
+
xhr.send(body);
|
|
338
353
|
});
|
|
339
354
|
},
|
|
340
|
-
|
|
355
|
+
|
|
341
356
|
parseLinksHeader(res) {
|
|
342
357
|
const header = res.xhr.getResponseHeader('Link');
|
|
343
358
|
const links = {};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// LICENCE https://github.com/adaptlearning/adapt_authoring/blob/master/LICENSE
|
|
2
2
|
define([
|
|
3
3
|
'core/origin',
|
|
4
|
+
'core/helpers',
|
|
4
5
|
'core/views/originView',
|
|
5
6
|
'modules/scaffold/views/scaffoldFileView'
|
|
6
|
-
], function(Origin, OriginView, ScaffoldFileView){
|
|
7
|
+
], function(Origin, Helpers, OriginView, ScaffoldFileView){
|
|
7
8
|
var AssetManagementEditAssetView = OriginView.extend({
|
|
8
9
|
className: 'asset-management-edit-asset',
|
|
9
10
|
events: {
|
|
@@ -47,75 +48,51 @@ define([
|
|
|
47
48
|
return message;
|
|
48
49
|
},
|
|
49
50
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if(!d.value.length) dataArr.splice(i--, 1);
|
|
59
|
-
} else if(d.name === "url" && d.value === "") {
|
|
60
|
-
dataArr.splice(i--, 1);
|
|
51
|
+
async save() {
|
|
52
|
+
this.form.commit();
|
|
53
|
+
const model = this.form.model;
|
|
54
|
+
const hasFile = !!$('input[name="file"]', this.form.$el).val();
|
|
55
|
+
const hasChanged = Object.keys(model.changedAttributes()).filter(a => a !== '_type').length > 0;
|
|
56
|
+
try {
|
|
57
|
+
if(model.isNew() && !hasFile) {
|
|
58
|
+
return Origin.Notify.toast({ type: 'error', text: Origin.l10n.t('app.pleaseaddfile') });
|
|
61
59
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
60
|
+
if(hasChanged) {
|
|
61
|
+
if(!hasFile) { // don't upload empty file
|
|
62
|
+
$('input[type="file"]', this.form.$el).remove();
|
|
63
|
+
}
|
|
64
|
+
const validationErrors = this.form.validate();
|
|
65
|
+
if(validationErrors) {
|
|
66
|
+
return Origin.Notify.toast({
|
|
67
|
+
type: 'error',
|
|
68
|
+
title: Origin.l10n.t('app.validationfailed'),
|
|
69
|
+
text: Object.values(validationErrors).map(e => `${e.title} ${e.type}`).join('<br/>')
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const newData = await Helpers.submitForm(this.form, {
|
|
73
|
+
method: model.isNew() ? 'POST' : 'PATCH',
|
|
74
|
+
url: model.url(),
|
|
75
|
+
beforeSubmit: this.sanitiseData
|
|
76
|
+
});
|
|
77
|
+
const _id = newData && newData._id;
|
|
78
|
+
|
|
79
|
+
if(this.model.get('isModal')) {
|
|
80
|
+
if(_id) Origin.trigger('assetManagement:collection:refresh', null, true, _id);
|
|
81
|
+
Origin.trigger('assetManagement:modalEdit:remove');
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
Origin.router.navigateTo('assetManagement');
|
|
85
85
|
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
method: this.model.isNew() ? 'POST' : 'PATCH',
|
|
90
|
-
data: this.sanitiseData(new FormData(this.form)),
|
|
91
|
-
};
|
|
92
|
-
fetch(`/api/assets/${this.model.get('_id') ? this.model.get('_id') : ''}`,
|
|
93
|
-
submitData)
|
|
94
|
-
.then(res => res.json())
|
|
95
|
-
.then(callbacks.success)
|
|
96
|
-
.catch(callbacks.error);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
this.form.commit();
|
|
100
|
-
const data = this.sanitiseData(this.getAttributesToSave());
|
|
101
|
-
data ? this.model.save(data, Object.assign({ patch: true }, callbacks)) : this.onSaveSuccess();
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
onSaveSuccess: async function(data) {
|
|
105
|
-
const modelData = data ? Array.isArray(data) ? data[0] : data : undefined;
|
|
106
|
-
const _id = modelData && modelData._id;
|
|
107
|
-
|
|
108
|
-
if(this.model.get('isModal')) {
|
|
109
|
-
if(_id) Origin.trigger('assetManagement:collection:refresh', null, true, _id);
|
|
110
|
-
Origin.trigger('assetManagement:modalEdit:remove');
|
|
111
|
-
return;
|
|
86
|
+
} catch(e) {
|
|
87
|
+
Origin.trigger('sidebar:resetButtons');
|
|
88
|
+
Origin.Notify.alert({ type: 'error', text: e.message });
|
|
112
89
|
}
|
|
113
|
-
Origin.router.navigateTo('assetManagement');
|
|
114
90
|
},
|
|
115
91
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
92
|
+
sanitiseData(formData) {
|
|
93
|
+
const tags = formData.get('tags');
|
|
94
|
+
tags.length ? formData.set('tags', JSON.stringify(tags.split(','))) : formData.delete('tags');
|
|
95
|
+
if(!formData.get('url')) formData.delete('url');
|
|
119
96
|
}
|
|
120
97
|
|
|
121
98
|
}, {
|
|
@@ -27,8 +27,8 @@ define(function(require) {
|
|
|
27
27
|
},
|
|
28
28
|
|
|
29
29
|
onFilterButtonClicked: function(event) {
|
|
30
|
-
$currentTarget = $(event.currentTarget);
|
|
31
|
-
|
|
30
|
+
const $currentTarget = $(event.currentTarget);
|
|
31
|
+
const filterType = $currentTarget.attr('data-filter-type');
|
|
32
32
|
|
|
33
33
|
// If this filter is already selected - remove filter
|
|
34
34
|
// else add the filter
|
package/package.json
CHANGED