+ function add_wikidata_hint($input, val) {
+ if (val && val != $input.val()) {
+ let already_set = false;
+ let el = $('<span class="wikidata-hint">');
+
+ if (val.wd) {
+ let iv = $input.val();
+ if (val.id) {
+ if (Array.isArray(iv)) {
+ if (iv.indexOf(val.id.toString()) != -1) {
+ already_set = true;
+ }
+ } else if (val.id == iv) {
+ already_set = true;
+ }
+ }
+
+ if (!already_set) {
+ // A representation of a WD Entity.
+ el.on('click', function() {
+ $(this).addClass('wikidata-processing');
+ set_value_from_wikidata_id(
+ $input, val.model, val.wd,
+ () => {$(this).remove();}
+ );
+ });
+ el.text(val.label);
+ }
+ } else if (val.img) {
+ // A downloadable remote image.
+ let img = $('<img height="32">');
+ img.attr('src', val.img);
+ el.append(img);
+ el.on('click', function() {
+ set_file_from_url(
+ $input, val.download,
+ () => {$(this).remove();}
+ );
+ });
+ } else if (val.action == 'append') {
+ el.on('click', function() {
+ $input.val(
+ $input.val() + '\n' + val.value
+ );
+ $(this).remove();
+ });
+ el.text('+ ' + val.value);
+ } else {
+ // A plain literal.
+ el.on('click', function() {
+ $input.val(val);
+ $(this).remove();
+ });
+ el.text(val);
+ }
+ if (!already_set) {
+ $input.parent().append(el);
+ }
+ }
+ }
+