mirror of
https://github.com/FoggedLens/iD.git
synced 2026-02-25 15:05:32 +00:00
Move implementation details from uiTagReference to wikibase service
This commit is contained in:
@@ -3,6 +3,7 @@ import _forEach from 'lodash-es/forEach';
|
||||
|
||||
import { json as d3_json } from 'd3-request';
|
||||
|
||||
import { utilDetect } from '../util/detect';
|
||||
import { utilQsString } from '../util';
|
||||
|
||||
|
||||
@@ -109,6 +110,17 @@ export default {
|
||||
},
|
||||
|
||||
|
||||
//
|
||||
// Pass params object of the form:
|
||||
// {
|
||||
// key: 'string', // required
|
||||
// value: 'string' // optional
|
||||
// }
|
||||
// -or-
|
||||
// {
|
||||
// rtype: 'rtype' // relation type (e.g. 'multipolygon')
|
||||
// }
|
||||
//
|
||||
getEntity: function(params, callback) {
|
||||
var doRequest = params.debounce ? debouncedRequest : request;
|
||||
var self = this;
|
||||
@@ -202,11 +214,112 @@ export default {
|
||||
},
|
||||
|
||||
|
||||
//
|
||||
// Pass params object of the form:
|
||||
// {
|
||||
// key: 'string', // required
|
||||
// value: 'string' // optional
|
||||
// }
|
||||
// -or-
|
||||
// {
|
||||
// rtype: 'rtype' // relation type (e.g. 'multipolygon')
|
||||
// }
|
||||
//
|
||||
// Get an result object used to display tag documentation
|
||||
// {
|
||||
// title: 'string',
|
||||
// description: 'string',
|
||||
// editURL: 'string',
|
||||
// imageURL: 'string',
|
||||
// wiki: { title: 'string', text: 'string', url: 'string' }
|
||||
// }
|
||||
//
|
||||
getDocs: function(params, callback) {
|
||||
var that = this;
|
||||
var langCode = utilDetect().locale.toLowerCase();
|
||||
params.langCode = langCode;
|
||||
|
||||
this.getEntity(params, function(err, data) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
|
||||
var entity = data.tag || data.key;
|
||||
if (!entity) {
|
||||
callback('No entity');
|
||||
return;
|
||||
}
|
||||
|
||||
// prepare result
|
||||
var result = {
|
||||
title: entity.title,
|
||||
description: entity.description,
|
||||
editURL: 'https://wiki.openstreetmap.org/wiki/' + entity.title
|
||||
};
|
||||
|
||||
// add image
|
||||
if (entity.claims) {
|
||||
var imageroot;
|
||||
var image = that.claimToValue(entity, 'P4', langCode);
|
||||
if (image) {
|
||||
imageroot = 'https://commons.wikimedia.org/w/index.php';
|
||||
} else {
|
||||
image = that.claimToValue(entity, 'P28', langCode);
|
||||
if (image) {
|
||||
imageroot = 'https://wiki.openstreetmap.org/w/index.php';
|
||||
}
|
||||
}
|
||||
if (imageroot && image) {
|
||||
result.imageURL = imageroot + '?' + utilQsString({
|
||||
title: 'Special:Redirect/file/' + image,
|
||||
width: 100,
|
||||
height: 100
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Try to get a wiki page from tag data item first, followed by the corresponding key data item.
|
||||
// If neither tag nor key data item contain a wiki page in the needed language nor English,
|
||||
// get the first found wiki page from either the tag or the key item.
|
||||
var tagWiki = that.monolingualClaimToValueObj(data.tag, 'P31');
|
||||
var keyWiki = that.monolingualClaimToValueObj(data.key, 'P31');
|
||||
|
||||
// If exact language code does not exist, try to find the first part before the '-'
|
||||
// BUG: in some cases, a more elaborate fallback logic might be needed
|
||||
var langPrefix = langCode.split('-', 2)[0];
|
||||
|
||||
result.wiki =
|
||||
getWikiInfo(tagWiki, langCode, 'inspector.wiki_reference') ||
|
||||
getWikiInfo(tagWiki, langPrefix, 'inspector.wiki_reference') ||
|
||||
getWikiInfo(tagWiki, 'en', 'inspector.wiki_en_reference') ||
|
||||
getWikiInfo(keyWiki, langCode, 'inspector.wiki_reference') ||
|
||||
getWikiInfo(keyWiki, langPrefix, 'inspector.wiki_reference') ||
|
||||
getWikiInfo(keyWiki, 'en', 'inspector.wiki_en_reference');
|
||||
|
||||
callback(null, result);
|
||||
|
||||
|
||||
// Helper method to get wiki info if a given language exists
|
||||
function getWikiInfo(wiki, langCode, tKey) {
|
||||
if (wiki && wiki[langCode]) {
|
||||
return {
|
||||
title: wiki[langCode],
|
||||
text: tKey,
|
||||
url: 'https://wiki.openstreetmap.org/wiki/' + wiki[langCode]
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
addLocale: function(langCode, qid) {
|
||||
// Makes it easier to unit test
|
||||
_localeIDs[langCode] = qid;
|
||||
},
|
||||
|
||||
|
||||
apibase: function(val) {
|
||||
if (!arguments.length) return apibase;
|
||||
apibase = val;
|
||||
|
||||
@@ -4,12 +4,19 @@ import {
|
||||
} from 'd3-selection';
|
||||
|
||||
import { t } from '../util/locale';
|
||||
import { utilDetect } from '../util/detect';
|
||||
import { services } from '../services';
|
||||
import { svgIcon } from '../svg';
|
||||
import { utilQsString } from '../util';
|
||||
|
||||
|
||||
// Pass `tag` object of the form:
|
||||
// {
|
||||
// key: 'string', // required
|
||||
// value: 'string' // optional
|
||||
// }
|
||||
// -or-
|
||||
// {
|
||||
// rtype: 'rtype' // relation type (e.g. 'multipolygon')
|
||||
// }
|
||||
export function uiTagReference(tag) {
|
||||
var wikibase = services.osmWikibase;
|
||||
var tagReference = {};
|
||||
@@ -19,79 +26,14 @@ export function uiTagReference(tag) {
|
||||
var _loaded;
|
||||
var _showing;
|
||||
|
||||
/**
|
||||
* @returns {{itemTitle: String, description: String, image: String|null}|null}
|
||||
**/
|
||||
function findLocal(data) {
|
||||
var entity = data.tag || data.key;
|
||||
if (!entity) return null;
|
||||
|
||||
var result = {
|
||||
title: entity.title,
|
||||
description: entity.description,
|
||||
};
|
||||
|
||||
if (entity.claims) {
|
||||
var langCode = utilDetect().locale.toLowerCase();
|
||||
var url;
|
||||
var image = wikibase.claimToValue(entity, 'P4', langCode);
|
||||
if (image) {
|
||||
url = 'https://commons.wikimedia.org/w/index.php';
|
||||
} else {
|
||||
image = wikibase.claimToValue(entity, 'P28', langCode);
|
||||
if (image) {
|
||||
url = 'https://wiki.openstreetmap.org/w/index.php';
|
||||
}
|
||||
}
|
||||
if (image) {
|
||||
result.image = {
|
||||
url: url,
|
||||
title: 'Special:Redirect/file/' + image
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Helper method to get wiki info if a given language exists
|
||||
function getWikiInfo(wiki, langCode, msg) {
|
||||
if (wiki && wiki[langCode]) {
|
||||
return {title: wiki[langCode], text: t(msg)};
|
||||
}
|
||||
}
|
||||
|
||||
// Try to get a wiki page from tag data item first, followed by the corresponding key data item.
|
||||
// If neither tag nor key data item contain a wiki page in the needed language nor English,
|
||||
// get the first found wiki page from either the tag or the key item.
|
||||
var tagWiki = wikibase.monolingualClaimToValueObj(data.tag, 'P31');
|
||||
var keyWiki = wikibase.monolingualClaimToValueObj(data.key, 'P31');
|
||||
|
||||
// If exact language code does not exist, try to find the first part before the '-'
|
||||
// BUG: in some cases, a more elaborate fallback logic might be needed
|
||||
var langPrefix = langCode.split('-', 2)[0];
|
||||
|
||||
result.wiki =
|
||||
getWikiInfo(tagWiki, langCode, 'inspector.wiki_reference') ||
|
||||
getWikiInfo(tagWiki, langPrefix, 'inspector.wiki_reference') ||
|
||||
getWikiInfo(tagWiki, 'en', 'inspector.wiki_en_reference') ||
|
||||
getWikiInfo(keyWiki, langCode, 'inspector.wiki_reference') ||
|
||||
getWikiInfo(keyWiki, langPrefix, 'inspector.wiki_reference') ||
|
||||
getWikiInfo(keyWiki, 'en', 'inspector.wiki_en_reference');
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
function load(param) {
|
||||
function load() {
|
||||
if (!wikibase) return;
|
||||
|
||||
_button
|
||||
.classed('tag-reference-loading', true);
|
||||
|
||||
wikibase.getEntity(param, function show(err, data) {
|
||||
var docs;
|
||||
if (!err && data) {
|
||||
docs = findLocal(data);
|
||||
}
|
||||
|
||||
wikibase.getDocs(tag, function(err, docs) {
|
||||
_body.html('');
|
||||
|
||||
if (!docs || !docs.title) {
|
||||
@@ -103,17 +45,11 @@ export function uiTagReference(tag) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (docs.image) {
|
||||
var imageUrl = docs.image.url + '?' + utilQsString({
|
||||
title: docs.image.title,
|
||||
width: 100,
|
||||
height: 100,
|
||||
});
|
||||
|
||||
if (docs.imageURL) {
|
||||
_body
|
||||
.append('img')
|
||||
.attr('class', 'tag-reference-wiki-image')
|
||||
.attr('src', imageUrl)
|
||||
.attr('src', docs.imageURL)
|
||||
.on('load', function() { done(); })
|
||||
.on('error', function() { d3_select(this).remove(); done(); });
|
||||
} else {
|
||||
@@ -129,7 +65,7 @@ export function uiTagReference(tag) {
|
||||
.attr('target', '_blank')
|
||||
.attr('tabindex', -1)
|
||||
.attr('title', t('inspector.edit_reference'))
|
||||
.attr('href', 'https://wiki.openstreetmap.org/wiki/' + docs.title)
|
||||
.attr('href', docs.editURL)
|
||||
.call(svgIcon('#iD-icon-edit', 'inline'));
|
||||
|
||||
if (docs.wiki) {
|
||||
@@ -138,14 +74,14 @@ export function uiTagReference(tag) {
|
||||
.attr('class', 'tag-reference-link')
|
||||
.attr('target', '_blank')
|
||||
.attr('tabindex', -1)
|
||||
.attr('href', 'https://wiki.openstreetmap.org/wiki/' + docs.wiki.title)
|
||||
.attr('href', docs.wiki.url)
|
||||
.call(svgIcon('#iD-icon-out-link', 'inline'))
|
||||
.append('span')
|
||||
.text(docs.wiki.text);
|
||||
.text(t(docs.wiki.text));
|
||||
}
|
||||
|
||||
// Add link to info about "good changeset comments" - #2923
|
||||
if (param.key === 'comment') {
|
||||
if (tag.key === 'comment') {
|
||||
_body
|
||||
.append('a')
|
||||
.attr('class', 'tag-reference-comment-link')
|
||||
@@ -212,8 +148,7 @@ export function uiTagReference(tag) {
|
||||
} else if (_loaded) {
|
||||
done();
|
||||
} else {
|
||||
tag.langCode = utilDetect().locale.toLowerCase();
|
||||
load(tag);
|
||||
load();
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -240,9 +175,9 @@ export function uiTagReference(tag) {
|
||||
};
|
||||
|
||||
|
||||
tagReference.showing = function(_) {
|
||||
tagReference.showing = function(val) {
|
||||
if (!arguments.length) return _showing;
|
||||
_showing = _;
|
||||
_showing = val;
|
||||
return tagReference;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user