4 let model = $('body').attr('class').match(/model-([^\s]*)/)[1];
5 $("#id_wikidata").each(show_wikidata_hints).on('change', show_wikidata_hints);
7 function add_wikidata_hint($input, val) {
8 if (val && val != $input.val()) {
9 let already_set = false;
10 let el = $('<span class="wikidata-hint">');
13 let iv = $input.val();
15 if (Array.isArray(iv)) {
16 if (iv.indexOf(val.id.toString()) != -1) {
19 } else if (val.id == iv) {
25 // A representation of a WD Entity.
26 el.on('click', function() {
27 $(this).addClass('wikidata-processing');
28 set_value_from_wikidata_id(
29 $input, val.model, val.wd,
30 () => {$(this).remove();}
36 // A downloadable remote image.
37 let img = $('<img height="32">');
38 img.attr('src', val.img);
40 el.on('click', function() {
43 () => {$(this).remove();}
46 } else if (val.action == 'append') {
47 el.on('click', function() {
49 $input.val() + '\n' + val.value
53 el.text('+ ' + val.value);
56 el.on('click', function() {
63 $input.parent().append(el);
68 function show_wikidata_hints() {
69 $(".wikidata-hint").remove();
71 let qid = $wdinput.val();
73 $wdinput.addClass('wikidata-processing');
75 '/catalogue/wikidata/' + model + '/' + qid,
77 success: function(result) {
79 let val = result[att];
80 let $input = $("#id_" + att);
82 if (Array.isArray(val)) {
83 for (singleValue of val) {
84 add_wikidata_hint($input, singleValue);
87 add_wikidata_hint($input, val);
92 complete: function() {
93 $wdinput.removeClass('wikidata-processing');
99 function set_value_from_wikidata_id($input, model, wikidata_id, callback) {
101 url: '/catalogue/wikidata/' + model + '/' + wikidata_id,
103 csrfmiddlewaretoken: $('[name=csrfmiddlewaretoken]').val(),
105 success: function(result) {
106 $input.append($('<option>').attr('value', result.id).text(result.__str__));
107 $input.val(result.id).trigger('change');
113 function set_file_from_url($input, url, callback) {
114 filename = decodeURIComponent(url.match(/.*\/(.*)/)[1]);
117 success: function(content) {
118 let file = new File([content], filename);
119 let container = new DataTransfer();
120 container.items.add(file);
121 $input[0].files = container.files;