Skip to content

Commit

Permalink
Fix title population from file name in media chooser / add media
Browse files Browse the repository at this point in the history
+ tags init too when using the generic media chooser
  • Loading branch information
zerolab committed Jul 19, 2023
1 parent 99a2693 commit ae77a20
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
29 changes: 14 additions & 15 deletions src/wagtailmedia/static/wagtailmedia/js/media-chooser-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,26 @@ MEDIA_CHOOSER_MODAL_ONLOAD_HANDLERS = {
return false;
});

function populateTitle(context) {
// Note: There are two inputs with `#id_title` on the page.
// The page title and media title. Select the input inside the modal body.
var fileWidget = $('#id_file', context);
// Note: There are two inputs with `#id_title` on the page.
// The page title and media title. Select the input inside the modal body.
$('[name="media-chooser-upload-file"]', modal.body).each(function() {
const fileWidget = $(this);
fileWidget.on('change', function () {
var titleWidget = $('#id_title', context);
var title = titleWidget.val();
if (title === '') {
let titleWidget = $('#id_media-chooser-upload-title', fileWidget.closest('form'));
if (titleWidget.val() === '') {
// The file widget value example: `C:\fakepath\media.jpg`
var parts = fileWidget.val().split('\\');
var fileName = parts[parts.length - 1];
titleWidget.val(fileName);
const parts = fileWidget.val().split('\\');
const filename = parts[parts.length - 1];
titleWidget.val(filename.replace(/\.[^.]+$/, ''));
}
});
}

populateTitle(modal.body);
});

/* Add tag entry interface (with autocompletion) to the tag field of the media upload form */
$('#id_tags', modal.body).tagit({
autocomplete: {source: jsonData['tag_autocomplete_url']}
$('[name="media-chooser-upload-tags"]', modal.body).each(function() {
$(this).tagit({
autocomplete: {source: jsonData['tag_autocomplete_url']}
});
});
},
'media_chosen': function(modal, jsonData) {
Expand Down
11 changes: 11 additions & 0 deletions src/wagtailmedia/templates/wagtailmedia/media/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
$('#id_tags').tagit({
autocomplete: {source: "{{ autocomplete_url|addslashes }}"}
});

const fileWidget = document.getElementById("id_file");
const titleWidget = document.getElementById("id_title");
fileWidget.addEventListener('change', function () {
if (titleWidget.value === '') {
// The file widget value example: `C:\fakepath\media.jpg`
var parts = fileWidget.value.split('\\');
var filename = parts[parts.length - 1].replace(/\.[^.]+$/, '');
titleWidget.value = filename;
}
});
});
</script>
{% endblock %}
Expand Down
1 change: 0 additions & 1 deletion src/wagtailmedia/templates/wagtailmedia/media/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

{% block extra_js %}
{{ block.super }}

{{ form.media.js }}

{% url 'wagtailadmin_tag_autocomplete' as autocomplete_url %}
Expand Down

0 comments on commit ae77a20

Please sign in to comment.