NEW 'Choose another file' button on upload field AAS-917

This commit is contained in:
jean 2013-03-27 10:56:08 +13:00 committed by Sam Minnee
parent 2d8812dbd2
commit 6018bdd631
3 changed files with 38 additions and 10 deletions

View File

@ -282,7 +282,8 @@ class UploadField extends FileField {
'UploadFieldThumbnailURL' => $this->getThumbnailURLForFile($file),
'UploadFieldRemoveLink' => $this->getItemHandler($file->ID)->RemoveLink(),
'UploadFieldDeleteLink' => $this->getItemHandler($file->ID)->DeleteLink(),
'UploadFieldEditLink' => $this->getItemHandler($file->ID)->EditLink()
'UploadFieldEditLink' => $this->getItemHandler($file->ID)->EditLink(),
'UploadField' => $this
));
// we do this in a second customise to have the access to the previous customisations
return $file->customise(array(
@ -602,11 +603,15 @@ class UploadField extends FileField {
* @param File
*/
protected function attachFile($file) {
$replaceFileID = $this->getRequest()->requestVar('ReplaceFileID');
$record = $this->getRecord();
$name = $this->getName();
if ($record && $record->exists()) {
if ($record->has_many($name) || $record->many_many($name)) {
if (($record->has_many($name) || $record->many_many($name))) {
if(!$record->isInDB()) $record->write();
if ($replaceFileID){
$record->{$name}()->removebyId($replaceFileID);
}
$record->{$name}()->add($file);
} elseif($record->has_one($name)) {
$record->{$name . 'ID'} = $file->ID;
@ -988,6 +993,8 @@ class UploadField_SelectHandler extends RequestHandler {
// Generate the folder selection field.
$folderField = new TreeDropdownField('ParentID', _t('HtmlEditorField.FOLDER', 'Folder'), 'Folder');
$folderField->setValue($folderID);
// Generate the file list field.
$config = GridFieldConfig::create();
@ -1016,6 +1023,11 @@ class UploadField_SelectHandler extends RequestHandler {
$folderField,
$fileField
);
//Existing file to replace
if ($replaceFileID = $this->parent->getRequest()->requestVar('ReplaceFileID')) {
$selectComposite->push(new HiddenField('ReplaceFileID','ReplaceFileID', $replaceFileID));
}
return $selectComposite;
}

View File

@ -169,13 +169,21 @@
onunmatch: function() {
this._super();
},
openSelectDialog: function() {
openSelectDialog: function(uploadedFile) {
// Create dialog and load iframe
var self = this, config = this.getConfig(), dialogId = 'ss-uploadfield-dialog-' + this.attr('id'), dialog = jQuery('#' + dialogId);
if(!dialog.length) dialog = jQuery('<div class="ss-uploadfield-dialog" id="' + dialogId + '" />');
// If user selected 'Choose another file', we need the ID of the file to replace
var iframeUrl = config['urlSelectDialog'];
var uploadedFileId = null;
if (uploadedFile && uploadedFile.attr('data-fileid') > 0){
uploadedFileId = uploadedFile.attr('data-fileid');
iframeUrl = iframeUrl + '?ReplaceFileID=' + uploadedFileId;
}
// Show dialog
dialog.ssdialog({iframeUrl: config['urlSelectDialog'], height: 550});
dialog.ssdialog({iframeUrl: iframeUrl, height: 550});
// TODO Allow single-select
dialog.find('iframe').bind('load', function(e) {
@ -191,7 +199,7 @@
contents.find('input[name=action_doAttach]').unbind('click.openSelectDialog').bind('click.openSelectDialog', function() {
// TODO Fix entwine method calls across iframe/document boundaries
var ids = $.map(gridField.find('.ss-gridfield-item.ui-selected'), function(el) {return $(el).data('id');});
if(ids && ids.length) self.attachFiles(ids);
if(ids && ids.length) self.attachFiles(ids, uploadedFileId);
dialog.ssdialog('close');
return false;
@ -199,14 +207,18 @@
});
dialog.ssdialog('open');
},
attachFiles: function(ids) {
attachFiles: function(ids, uploadedFileId) {
var self = this, config = this.getConfig();
$.post(
config['urlAttach'],
{'ids': ids},
{'ids': ids, 'ReplaceFileID': uploadedFileId},
function(data, status, xhr) {
var fn = self.fileupload('option', 'downloadTemplate');
self.find('.ss-uploadfield-files').append(fn({
var container = self.find('.ss-uploadfield-files');
if (config['allowedMaxFileNumber'] == 1){
container.empty();
}
container.append(fn({
files: data,
formatFileSize: function (bytes) {
if (typeof bytes !== 'number') return '';
@ -403,7 +415,7 @@
$('div.ss-upload .ss-uploadfield-fromfiles').entwine({
onclick: function(e) {
e.preventDefault();
this.getUploadField().openSelectDialog();
this.getUploadField().openSelectDialog(this.closest('.ss-uploadfield-item'));
}
});
});

View File

@ -13,4 +13,8 @@
<% if canDelete %>
<button data-href="$UploadFieldDeleteLink" class="ss-uploadfield-item-delete ss-ui-button ui-corner-all" title="<% _t('UploadField.DELETEINFO', 'Permanently delete this file from the file store') %>" data-icon="minus-circle"><% _t('UploadField.DELETE', 'Delete from files') %></button>
<% end_if %>
<% if UploadFieldHasRelation && UploadField.canAttachExisting %>
<button class="ss-uploadfield-item-choose-another ss-uploadfield-fromfiles ss-ui-button ui-corner-all" title="<% _t('UploadField.CHOOSEANOTHERINFO', 'Replace this file with another one from the file store') %>" data-icon="network-cloud">
<% _t('UploadField.CHOOSEANOTHERFILE', 'Choose another file') %></button>
<% end_if %>