Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2648] Re-jig resource upload module to use new syntax
- Loading branch information
Showing
1 changed file
with
61 additions
and
50 deletions.
There are no files selected for viewing
111 changes: 61 additions & 50 deletions
111
ckan/public/base/javascript/modules/resource-upload-field.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,72 @@ | ||
this.ckan.module('resource-upload-field', function (sandbox, options) { | ||
var upload = jQuery(options.template); | ||
|
||
this.ckan.module('resource-upload-field', { | ||
options: { | ||
form: { | ||
action: 'http://ckanext-storage.commondatastorage.googleapis.com/', | ||
method: 'POST', | ||
params: { | ||
'x-goog-meta-uploaded-by': 'c03155ce-8319-46ef-b4b2-4bf3303735d3' | ||
} | ||
}, | ||
template: [ | ||
'<span class="resource-upload-field">', | ||
'<i class="ckan-icon ckan-icon-link-plugin"></i>', | ||
'<input id="field-resource-type-file" type="file" />', | ||
'<label class="radio inline" for="field-resource-type-file">Upload a file</label>', | ||
'</span>' | ||
].join('\n') | ||
}, | ||
|
||
initialize: function () { | ||
var options = this.options; | ||
var upload = this.sandbox.jQuery(options.template); | ||
|
||
upload.find('input').fileupload({ | ||
url: options.form.action, | ||
upload.find('input').fileupload({ | ||
url: options.form.action, | ||
type: options.form.method, | ||
forceIframeTransport: true, // Required for XDomain request. | ||
replaceFileInput: false, | ||
autoUpload: false, | ||
add: this._onUploadAdd, | ||
send: this._onUploadSend, | ||
done: this._onUploadDone, | ||
fail: this._onUploadFail | ||
}); | ||
|
||
type: options.form.method, | ||
this.el.append(upload); | ||
}, | ||
|
||
// needed because we are posting to remote url | ||
forceIframeTransport: true, | ||
loading: function (show) { | ||
this.el.addClass(); | ||
}, | ||
|
||
replaceFileInput: false, | ||
notify: function (message, type) { | ||
var title = this.sandbox.translate('An Error Occurred').fetch(); | ||
this.sandbox.notify(title, message, type); | ||
}, | ||
|
||
autoUpload: false, | ||
_onUploadAdd: function (event, data) { | ||
var request = this.sandbox.client.getStorageAuth(data.files[0].name); | ||
request.then(function (response) { | ||
data.formData = response.fields; | ||
data.submit(); | ||
}, this.onAuthFail); | ||
}, | ||
|
||
fail: function (e, data) { | ||
console.log(data); | ||
}, | ||
_onUploadFail: function () { | ||
var _ = this.sandbox.translate; | ||
this.notify(_('Unable to upload file')); | ||
}, | ||
|
||
add: function(e, data) { | ||
jQuery.ajax({ | ||
url: '/api/storage/auth/form/' + data.files[0].name, | ||
success: function(response) { | ||
data.formData = response.fields; | ||
data.submit(); | ||
}, | ||
error: function(jqXHR, textStatus, errorThrown) { | ||
// TODO: more graceful error handling (e.g. of 409) | ||
} | ||
}); | ||
}, | ||
_onUploadSend: function () { | ||
this.loading(); | ||
}, | ||
|
||
send: function (e, data) { | ||
data.url = options.form.action; | ||
console.log(data); | ||
}, | ||
_onUploadDone: function (event, data) { | ||
this.sandbox.publish('resource:uploaded', data); | ||
}, | ||
|
||
done: function (e, data) { | ||
console.log(data); | ||
} | ||
}); | ||
|
||
sandbox.el.append(upload); | ||
}, { | ||
form: { | ||
action: 'http://ckanext-storage.commondatastorage.googleapis.com/', | ||
method: 'POST', | ||
params: { | ||
'x-goog-meta-uploaded-by': 'c03155ce-8319-46ef-b4b2-4bf3303735d3' | ||
} | ||
}, | ||
template: [ | ||
'<span class="resource-upload-field">', | ||
'<i class="ckan-icon ckan-icon-link-plugin"></i>', | ||
'<input id="field-resource-type-file" type="file" />', | ||
'<label class="radio inline" for="field-resource-type-file">Upload a file</label>', | ||
'</span>' | ||
].join('\n') | ||
_onAuthFail: function () { | ||
var _ = this.sandbox.translate; | ||
this.notify(_('Unable to authenticate upload')); | ||
} | ||
}); |