Skip to content

Commit

Permalink
[#2648] Re-jig resource upload module to use new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Jul 12, 2012
1 parent e28011e commit fa1b1e9
Showing 1 changed file with 61 additions and 50 deletions.
111 changes: 61 additions & 50 deletions ckan/public/base/javascript/modules/resource-upload-field.js
@@ -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'));
}
});

0 comments on commit fa1b1e9

Please sign in to comment.