Skip to content

Commit

Permalink
Autocomplete labels in the Wikidata field (close #5544)
Browse files Browse the repository at this point in the history
  • Loading branch information
quincylvania committed Mar 28, 2019
1 parent 2728f07 commit 5a0f8b3
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 107 deletions.
42 changes: 24 additions & 18 deletions css/80_app.css
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ button.preset-favorite-button.active .icon {

/* Buttons inside fields */
.form-field-button {
flex: 0 0 32px;
flex: 0 0 auto;
height: 30px;
width: 32px;
position: relative;
Expand Down Expand Up @@ -1635,40 +1635,44 @@ button.preset-favorite-button.active .icon {

/* Field - lists with labeled input items
------------------------------------------------------- */
.form-field ul.labeled-inputs {
.form-field ul.rows {
flex: 1 1 auto;
border: 1px solid #ccc;
border-top: 0;
border-radius: 0 0 4px 4px;
overflow: hidden;
width: 100%;
}
.form-field ul.labeled-inputs li {
.form-field ul.rows li {
border-top: 1px solid #ccc;
display: flex;
flex-flow: row nowrap;
}
.form-field ul.labeled-inputs li:first-child {
.form-field ul.rows li:first-child {
border-top: 0;
}
.form-field ul.labeled-inputs li > span,
.form-field ul.labeled-inputs li > div {
.form-field ul.rows li {
display: flex;
flex-flow: row nowrap;
}
.form-field ul.rows li.labeled-input > span,
.form-field ul.rows li.labeled-input > div {
flex: 1 1 auto;
width: 100%;
border-radius: 0;
}
.form-field ul.labeled-inputs li input {
.form-field ul.rows li input {
border-radius: 0;
border-width: 0;
width: 100%;
}
.form-field ul.labeled-inputs li input,
.form-field ul.labeled-inputs li button {
.form-field ul.rows li button {
border-width: 0;
}
[dir='ltr'] .form-field ul.rows li.labeled-input input,
[dir='ltr'] .form-field ul.rows li button {
border-left-width: 1px;
}
[dir='rtl'] .form-field ul.labeled-inputs li input,
[dir='rtl'] .form-field ul.labeled-inputs li button {
border-left-width: 0;
[dir='rtl'] .form-field ul.rows li.labeled-input input,
[dir='rtl'] .form-field ul.rows li button {
border-right-width: 1px;
}

Expand All @@ -1683,7 +1687,7 @@ button.preset-favorite-button.active .icon {
border-top: 0px;
border-radius: 0 0 4px 4px;
}
.structure-extras-wrap > ul.labeled-inputs {
.structure-extras-wrap > ul.rows {
border: 1px solid #ccc;
border-radius: 4px;
}
Expand Down Expand Up @@ -2058,10 +2062,12 @@ button.preset-favorite-button.active .icon {
[dir='rtl'] .wiki-title-container > input.wiki-title {
border-radius: 0 0 4px 0;
}
.wiki-title-container > button.wiki-link {
.wiki-title-container > button.wiki-link,
.form-field-wikidata ul.rows li:last-child button.form-field-button:last-child {
border-radius: 0 0 4px 0;
}
[dir='rtl'] .wiki-title-container > button.wiki-link {
[dir='rtl'] .wiki-title-container > button.wiki-link,
[dir='rtl'] .form-field-wikidata ul.rows li:last-child button.form-field-button:last-child {
border-radius: 0 0 0 4px;
}

Expand Down Expand Up @@ -2218,7 +2224,7 @@ div.combobox {
display: inline-block;
position: relative;
height: 30px;
width: 30px;
width: 30px !important;
margin-left: -30px;
vertical-align: top;
cursor: pointer;
Expand Down
48 changes: 38 additions & 10 deletions modules/services/wikidata.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@ export default {
},


// Search for Wikidata items matching the query
itemsForSearchQuery: function(query, callback) {
if (!query) {
callback('No query', {});
return;
}

d3_json(apibase + utilQsString({
action: 'wbsearchentities',
format: 'json',
formatversion: 2,
search: query,
type: 'item',
language: this.languagesToQuery()[0],
limit: 10,
origin: '*'
}), function(err, data) {
if (data && data.error) {
err = data.error;
}
if (err) {
callback(err, {});
} else {
callback(null, data.search || {});
}
});
},


// Given a Wikipedia language and article title, return an array of
// corresponding Wikidata entities.
itemsByTitle: function(lang, title, callback) {
Expand Down Expand Up @@ -46,6 +75,13 @@ export default {
});
},

languagesToQuery: function() {
return utilArrayUniq([
currentLocale.toLowerCase(),
currentLocale.split('-', 2)[0].toLowerCase(),
'en'
]);
},

entityByQID: function(qid, callback) {
if (!qid) {
Expand All @@ -57,11 +93,7 @@ export default {
return;
}

var langs = utilArrayUniq([
currentLocale.toLowerCase(),
currentLocale.split('-', 2)[0].toLowerCase(),
'en'
]);
var langs = this.languagesToQuery();

d3_json(apibase + utilQsString({
action: 'wbgetentities',
Expand Down Expand Up @@ -144,11 +176,7 @@ export default {

if (entity.sitelinks) {
// must be one of these that we requested..
var langs = utilArrayUniq([
currentLocale.toLowerCase(),
currentLocale.split('-', 2)[0].toLowerCase(),
'en'
]);
var langs = this.languagesToQuery();
var englishLocale = (currentLocale.split('-', 2)[0].toLowerCase() === 'en');

for (i = 0; i < langs.length; i++) { // check each, in order of preference
Expand Down
4 changes: 2 additions & 2 deletions modules/ui/fields/access.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function uiFieldAccess(field, context) {

list = list.enter()
.append('ul')
.attr('class', 'labeled-inputs')
.attr('class', 'rows')
.merge(list);


Expand All @@ -33,7 +33,7 @@ export function uiFieldAccess(field, context) {
// Enter
var enter = items.enter()
.append('li')
.attr('class', function(d) { return 'preset-access-' + d; });
.attr('class', function(d) { return 'labeled-input preset-access-' + d; });

enter
.append('span')
Expand Down
4 changes: 2 additions & 2 deletions modules/ui/fields/cycleway.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function uiFieldCycleway(field, context) {

div = div.enter()
.append('ul')
.attr('class', 'labeled-inputs')
.attr('class', 'rows')
.merge(div);

var keys = ['cycleway:left', 'cycleway:right'];
Expand All @@ -41,7 +41,7 @@ export function uiFieldCycleway(field, context) {

var enter = items.enter()
.append('li')
.attr('class', function(d) { return 'preset-cycleway-' + stripcolon(d); });
.attr('class', function(d) { return 'labeled-input preset-cycleway-' + stripcolon(d); });

enter
.append('span')
Expand Down
6 changes: 3 additions & 3 deletions modules/ui/fields/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function uiFieldRadio(field, context) {

list = list.enter()
.append('ul')
.attr('class', 'labeled-inputs')
.attr('class', 'rows')
.merge(list);


Expand All @@ -122,7 +122,7 @@ export function uiFieldRadio(field, context) {
// Enter
var typeEnter = typeItem.enter()
.insert('li', ':first-child')
.attr('class', 'structure-type-item');
.attr('class', 'labeled-input structure-type-item');

typeEnter
.append('span')
Expand Down Expand Up @@ -167,7 +167,7 @@ export function uiFieldRadio(field, context) {
// Enter
var layerEnter = layerItem.enter()
.append('li')
.attr('class', 'structure-layer-item');
.attr('class', 'labeled-input structure-layer-item');

layerEnter
.append('span')
Expand Down
Loading

0 comments on commit 5a0f8b3

Please sign in to comment.