d2af5f70939693e7404710155063721d132b5ec2
[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 add_wikidata_hint($input, val) {
8             if (val && val != $input.val()) {
9                 let already_set = false;
10                 let el = $('<span class="wikidata-hint">');
11
12                 if (val.wd) {
13                     let iv = $input.val();
14                     if (val.id) {
15                         if (Array.isArray(iv)) {
16                             if (iv.indexOf(val.id.toString()) != -1) {
17                                 already_set = true;
18                             }
19                         } else if (val.id == iv) {
20                             already_set = true;
21                         }
22                     }
23
24                     if (!already_set) {
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();}
31                             );
32                         });
33                         el.text(val.label);
34                     }
35                 } else if (val.img) {
36                     // A downloadable remote image.
37                     let img = $('<img height="32">');
38                     img.attr('src', val.img);
39                     el.append(img);
40                     el.on('click', function() {
41                         set_file_from_url(
42                             $input, val.download,
43                             () => {$(this).remove();}
44                         );
45                     });
46                 } else if (val.action == 'append') {
47                     el.on('click', function() {
48                         $input.val(
49                             $input.val() + '\n' + val.value
50                         );
51                         $(this).remove();
52                     });
53                     el.text('+ ' + val.value);
54                 } else {
55                     // A plain literal.
56                     el.on('click', function() {
57                         $input.val(val);
58                         $(this).remove();
59                     });
60                     el.text(val);
61                 }
62                 if (!already_set) {
63                     $input.parent().append(el);
64                 }
65             }
66         }
67         
68         function show_wikidata_hints() {
69             $(".wikidata-hint").remove();
70             $wdinput = $(this);
71             let qid = $wdinput.val();
72             if (!qid) return;
73             $wdinput.addClass('wikidata-processing');
74             $.ajax(
75                 '/catalogue/wikidata/' + model + '/' + qid,
76                 {
77                     success: function(result) {
78                         for (att in result) {
79                             let val = result[att];
80                             let $input = $("#id_" + att);
81
82                             if (Array.isArray(val)) {
83                                 for (singleValue of val) {
84                                     add_wikidata_hint($input, singleValue);
85                                 }
86                             } else {
87                                 add_wikidata_hint($input, val);
88                             }
89                         };
90
91                     },
92                     complete: function() {
93                         $wdinput.removeClass('wikidata-processing');
94                     },
95                 }
96             );
97         }
98
99         function set_value_from_wikidata_id($input, model, wikidata_id, callback) {
100             $.post({
101                 url: '/catalogue/wikidata/' + model + '/' + wikidata_id,
102                 data: {
103                     csrfmiddlewaretoken: $('[name=csrfmiddlewaretoken]').val(),
104                 },
105                 success: function(result) {
106                     $input.append($('<option>').attr('value', result.id).text(result.__str__));                   
107                     $input.val(result.id).trigger('change');
108                     callback();
109                 },
110             })
111         }
112
113         function set_file_from_url($input, url, callback) {
114             filename = decodeURIComponent(url.match(/.*\/(.*)/)[1]);
115             $.ajax({
116                 url: url,
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;
122                     callback()
123                 }
124             });
125         }
126     });
127 })(jQuery);