Skip to content

Commit

Permalink
Update async wikidata tag if graph unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
bhousel committed May 26, 2016
1 parent 0446275 commit 0135fbe
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dist/iD.js: \
js/id/services/mapillary.js \
js/id/services/nominatim.js \
js/id/services/taginfo.js \
js/id/services/wikidata.js \
js/id/services/wikipedia.js \
js/id/util.js \
js/id/util/session_mutex.js \
Expand Down
2 changes: 1 addition & 1 deletion data/presets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ en:
# 'width=*'
label: Width (Meters)
wikipedia:
# 'wikipedia=*'
# 'wikipedia=*, wikidata=*'
label: Wikipedia
presets:
address:
Expand Down
4 changes: 4 additions & 0 deletions data/presets/fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,10 @@
},
"wikipedia": {
"key": "wikipedia",
"keys": [
"wikipedia",
"wikidata"
],
"type": "wikipedia",
"icon": "wikipedia",
"universal": true,
Expand Down
1 change: 1 addition & 0 deletions data/presets/fields/wikipedia.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"key": "wikipedia",
"keys": ["wikipedia", "wikidata"],
"type": "wikipedia",
"icon": "wikipedia",
"universal": true,
Expand Down
41 changes: 29 additions & 12 deletions js/id/ui/preset/wikipedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ iD.ui.preset.wikipedia = function(field, context) {
wikidata = iD.services.wikidata(),
link, entity, lang, title;

function i(selection) {
function wiki(selection) {
var langcombo = d3.combobox()
.fetcher(function(value, cb) {
var v = value.toLowerCase();
Expand Down Expand Up @@ -109,22 +109,37 @@ iD.ui.preset.wikipedia = function(field, context) {
title.value(value);
}

syncTags[field.key] = value ? language()[2] + ':' + value : undefined;
syncTags.wikipedia = value ? language()[2] + ':' + value : undefined;
syncTags.wikidata = undefined;

dispatch.change(syncTags);


// attempt asynchronous update of wikidata tag..
if (value && language()[2]) {
wikidata.itemsByTitle(language()[2], value, function (title, entities) {
var qids = entities && Object.keys(entities),
asyncTags = {};
asyncTags.wikidata = qids && _.find(qids, function (id) {
var initGraph = context.graph();
wikidata.itemsByTitle(language()[2], value, function (title, data) {
var qids = data && Object.keys(data),
currGraph = context.graph();

// only do this if graph is unchanged..
if (initGraph !== currGraph) return;

var currEntity = context.entity(entity.id),
currTags = _.clone(currEntity.tags);

currTags.wikidata = qids && _.find(qids, function (id) {
return id.match(/^Q\d+$/);
});
dispatch.change(asyncTags);

var annotation = t('operations.change_tags.annotation');
context.overwrite(iD.actions.ChangeTags(currEntity.id, currTags), annotation);
dispatch.change(currTags);
});
}
}

i.tags = function(tags) {
wiki.tags = function(tags) {
var value = tags[field.key] || '',
m = value.match(/([^:]+):([^#]+)(?:#(.+))?/),
l = m && _.find(iD.data.wikipedia, function(d) { return m[1] === d[2]; }),
Expand All @@ -143,7 +158,7 @@ iD.ui.preset.wikipedia = function(field, context) {
}
}
link.attr('href', 'https://' + m[1] + '.wikipedia.org/wiki/' +
m[2].replace(/ /g, '_') + (anchor ? ('#' + anchor) : ''));
m[2].replace(/ /g, '_') + (anchor ? ('#' + anchor) : ''));

// unrecognized value format
} else {
Expand All @@ -155,13 +170,15 @@ iD.ui.preset.wikipedia = function(field, context) {
}
};

i.entity = function(_) {
wiki.entity = function(_) {
if (!arguments.length) return entity;
entity = _;
return wiki;
};

i.focus = function() {
wiki.focus = function() {
title.node().focus();
};

return d3.rebind(i, dispatch, 'on');
return d3.rebind(wiki, dispatch, 'on');
};
19 changes: 11 additions & 8 deletions test/spec/ui/preset/wikipedia.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
describe('iD.ui.preset.wikipedia', function() {
var selection, field;
var entity, context, selection, field, entity;

beforeEach(function() {
entity = iD.Node({id: 'n12345'});
context = iD();
context.history().merge([entity]);
selection = d3.select(document.createElement('div'));
field = iD().presets(iD.data.presets).presets().field('wikipedia');
field = context.presets(iD.data.presets).presets().field('wikipedia');
});

it('recognizes lang:title format', function() {
var wikipedia = iD.ui.preset.wikipedia(field, {});
var wikipedia = iD.ui.preset.wikipedia(field, context).entity(entity);
selection.call(wikipedia);
wikipedia.tags({wikipedia: 'en:Title'});
expect(selection.selectAll('.wiki-lang').value()).to.equal('English');
Expand All @@ -16,11 +19,11 @@ describe('iD.ui.preset.wikipedia', function() {
});

it('sets a new value', function() {
var wikipedia = iD.ui.preset.wikipedia(field, {});
var wikipedia = iD.ui.preset.wikipedia(field, context).entity(entity);
selection.call(wikipedia);

wikipedia.on('change', function(tags) {
expect(tags).to.eql({wikipedia: undefined});
expect(tags).to.eql({wikipedia: undefined, wikidata: undefined});
});

selection.selectAll('.wiki-lang').value('Deutsch');
Expand All @@ -37,7 +40,7 @@ describe('iD.ui.preset.wikipedia', function() {
});

it('recognizes pasted URLs', function() {
var wikipedia = iD.ui.preset.wikipedia(field, {});
var wikipedia = iD.ui.preset.wikipedia(field, context).entity(entity);
selection.call(wikipedia);

selection.selectAll('.wiki-title').value('http://de.wikipedia.org/wiki/Title');
Expand All @@ -48,10 +51,10 @@ describe('iD.ui.preset.wikipedia', function() {
});

it('preserves existing language', function() {
selection.call(iD.ui.preset.wikipedia(field, {}));
selection.call(iD.ui.preset.wikipedia(field, context).entity(entity));
selection.selectAll('.wiki-lang').value('Deutsch');

var wikipedia = iD.ui.preset.wikipedia(field, {});
var wikipedia = iD.ui.preset.wikipedia(field, context).entity(entity);
selection.call(wikipedia);
wikipedia.tags({});

Expand Down

0 comments on commit 0135fbe

Please sign in to comment.