Copy the selected file name in the object name field.

In the containers panel, when uploading a object, if the user
selects a file and the object name field is empty, copy the
file name in the object name field. The filename string is
saved in the file input as a attribute for comparison of any
future changes to the file input.

Moved the file input to improve the UX

Change-Id: I608ebd572a745780fb59af852b182d75ab300ce7
Closes-Bug: #1319888
Co-Authored-By: Robert Mizielski <robert.mizielski@cloudwatt.com>
This commit is contained in:
George Peristerakis 2014-05-15 11:28:38 -04:00
parent 79088c8eea
commit 7c1076dce0
2 changed files with 32 additions and 4 deletions

View File

@ -56,6 +56,34 @@ horizon.forms = {
});
},
/**
* In the container's upload object form, copy the selected file name in the
* object name field if the field is empty. The filename string is stored in
* the input as an attribute "filename". The value is used as comparison to
* compare with the value of the new filename string.
*/
handle_object_upload_source: function() {
$("div.table_wrapper, #modal_wrapper").on("change", "input#id_object_file", function(evt) {
if (typeof($(this).attr("filename")) == 'undefined') {
$(this).attr("filename", "");
}
var $form = $(this).closest("form");
var $obj_name = $form.find("input#id_name");
var $fullPath = $(this).val();
var $startIndex = ($fullPath.indexOf('\\') >= 0 ? $fullPath.lastIndexOf('\\') : $fullPath.lastIndexOf('/'));
var $filename = $fullPath.substring($startIndex);
if ($filename.indexOf('\\') === 0 || $filename.indexOf('/') === 0) {
$filename = $filename.substring(1);
}
if (typeof($obj_name.val()) == 'undefined' || $(this).attr("filename").localeCompare($obj_name.val()) == 0) {
$obj_name.val($filename);
$(this).attr("filename", $filename);
}
});
},
datepicker: function() {
var startDate = $('input#id_start').datepicker()
.on('changeDate', function(ev) {
@ -133,6 +161,7 @@ horizon.addInitFunction(function () {
horizon.forms.handle_snapshot_source();
horizon.forms.handle_volume_source();
horizon.forms.handle_image_source();
horizon.forms.handle_object_upload_source();
horizon.forms.datepicker();
// Bind event handlers to confirm dangerous actions.

View File

@ -83,7 +83,9 @@ class UploadObject(forms.SelfHandlingForm):
path = forms.CharField(max_length=255,
required=False,
widget=forms.HiddenInput)
object_file = forms.FileField(label=_("File"),
required=False,
allow_empty_file=True)
name = forms.CharField(max_length=255,
label=_("Object Name"),
help_text=_("Slashes are allowed, and are treated "
@ -93,9 +95,6 @@ class UploadObject(forms.SelfHandlingForm):
attrs={"ng-model": "name",
"not-blank": ""}
))
object_file = forms.FileField(label=_("File"),
required=False,
allow_empty_file=True)
container_name = forms.CharField(widget=forms.HiddenInput())
def _set_object_path(self, data):