f13510bca5a201d6e0160ddbad3e97f7103c79da
[redakcja.git] / src / catalogue / static / catalogue / wikidata_admin.js
1 (function($) {
2     $(function() {
3     
4         let model = $('body').attr('class').match(/model-([^\s]*)/)[1];
5         $("#id_wikidata").each(show_wikidata_hints).on('change', show_wikidata_hints);
6
7         function show_wikidata_hints() {
8             $(".wikidata-hint").remove();
9             $wdinput = $(this);
10             let qid = $wdinput.val();
11             if (!qid) return;
12             $wdinput.addClass('wikidata-processing');
13             $.ajax(
14                 '/catalogue/wikidata/' + model + '/' + qid,
15                 {
16                     success: function(result) {
17                         for (att in 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">');
23
24                                 if (val.wd) {
25                                     if (val.id && val.id == $input.val()) {
26                                         already_set = true;
27                                     } else {
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();}
33                                             );
34                                         });
35                                         el.text(val.label);
36                                     }
37                                 } else if (val.img) {
38                                     // A downloadable remote image.
39                                     let img = $('<img height="32">');
40                                     img.attr('src', val.img);
41                                     el.append(img);
42                                     el.on('click', function() {
43                                         set_file_from_url(
44                                             $input, val.download,
45                                             () => {$(this).remove();}
46                                         );
47                                     });
48                                 } else {
49                                     // A plain literal.
50                                     el.on('click', function() {
51                                         $input.val(val);
52                                         $(this).remove();
53                                     });
54                                     el.text(val);
55                                 }
56                                 if (!already_set) {
57                                     $input.parent().append(el);
58                                 }
59                             }
60                         };
61
62                     },
63                     complete: function() {
64                         $wdinput.removeClass('wikidata-processing');
65                     },
66                 }
67             );
68         }
69
70         function set_value_from_wikidata_id($input, model, wikidata_id, callback) {
71             $.post({
72                 url: '/catalogue/wikidata/' + model + '/' + wikidata_id,
73                 data: {
74                     csrfmiddlewaretoken: $('[name=csrfmiddlewaretoken]').val(),
75                 },
76                 success: function(result) {
77                     $input.append($('<option>').attr('value', result.id).text(result.__str__));                   
78                     $input.val(result.id).trigger('change');
79                     callback();
80                 },
81             })
82         }
83
84         function set_file_from_url($input, url, callback) {
85             filename = decodeURIComponent(url.match(/.*\/(.*)/)[1]);
86             $.ajax({
87                 url: url,
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;
93                     callback()
94                 }
95             });
96         }
97     });
98 })(jQuery);