4 let model = $('body').attr('class').match(/model-([^\s]*)/)[1];
5 $("#id_wikidata").each(show_wikidata_hints).on('change', show_wikidata_hints);
7 function show_wikidata_hints() {
8 $(".wikidata-hint").remove();
10 let qid = $wdinput.val();
12 $wdinput.addClass('wikidata-processing');
14 '/catalogue/wikidata/' + model + '/' + qid,
16 success: function(result) {
18 let val = result[att];
19 let $input = $("#id_" + att);
20 if (val && val != $input.val()) {
21 let already_set = false;
22 let el = $('<span class="wikidata-hint">');
25 if (val.id && val.id == $input.val()) {
28 // A representation of a WD Entity.
29 el.on('click', function() {
30 set_value_from_wikidata_id(
31 $input, val.model, val.wd,
32 () => {$(this).remove();}
38 // A downloadable remote image.
39 let img = $('<img height="32">');
40 img.attr('src', val.img);
42 el.on('click', function() {
45 () => {$(this).remove();}
50 el.on('click', function() {
57 $input.parent().append(el);
63 complete: function() {
64 $wdinput.removeClass('wikidata-processing');
70 function set_value_from_wikidata_id($input, model, wikidata_id, callback) {
72 url: '/catalogue/wikidata/' + model + '/' + wikidata_id,
74 csrfmiddlewaretoken: $('[name=csrfmiddlewaretoken]').val(),
76 success: function(result) {
77 $input.append($('<option>').attr('value', result.id).text(result.__str__));
78 $input.val(result.id).trigger('change');
84 function set_file_from_url($input, url, callback) {
85 filename = decodeURIComponent(url.match(/.*\/(.*)/)[1]);
88 success: function(content) {
89 let file = new File([content], filename);
90 let container = new DataTransfer();
91 container.items.add(file);
92 $input[0].files = container.files;