mirror of
https://github.com/FoggedLens/iD.git
synced 2026-02-13 01:02:58 +00:00
Use Promise for improveOSM comments
This commit is contained in:
@@ -325,39 +325,33 @@ export default {
|
||||
});
|
||||
},
|
||||
|
||||
getComments(d, callback) {
|
||||
getComments(item) {
|
||||
// If comments already retrieved no need to do so again
|
||||
if (d.comments) {
|
||||
if (callback) callback({}, d);
|
||||
return;
|
||||
if (item.comments) {
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
|
||||
const key = d.issueKey;
|
||||
const key = item.issueKey;
|
||||
let qParams = {};
|
||||
|
||||
if (key === 'ow') {
|
||||
qParams = d.identifier;
|
||||
qParams = item.identifier;
|
||||
} else if (key === 'mr') {
|
||||
qParams.tileX = d.identifier.x;
|
||||
qParams.tileY = d.identifier.y;
|
||||
qParams.tileX = item.identifier.x;
|
||||
qParams.tileY = item.identifier.y;
|
||||
} else if (key === 'tr') {
|
||||
qParams.targetId = d.identifier;
|
||||
qParams.targetId = item.identifier;
|
||||
}
|
||||
|
||||
const url = `${_impOsmUrls[key]}/retrieveComments?` + utilQsString(qParams);
|
||||
const after = data => {
|
||||
// Assign directly for immediate use in the callback
|
||||
const cacheComments = data => {
|
||||
// Assign directly for immediate use afterwards
|
||||
// comments are served newest to oldest
|
||||
d.comments = data.comments ? data.comments.reverse() : [];
|
||||
this.replaceItem(d);
|
||||
if (callback) callback(null, d);
|
||||
item.comments = data.comments ? data.comments.reverse() : [];
|
||||
this.replaceItem(item);
|
||||
};
|
||||
|
||||
d3_json(url)
|
||||
.then(after)
|
||||
.catch(err => {
|
||||
if (callback) callback(err.message);
|
||||
});
|
||||
return d3_json(url).then(cacheComments).then(() => item);
|
||||
},
|
||||
|
||||
postUpdate(d, callback) {
|
||||
|
||||
@@ -19,57 +19,59 @@ export function uiImproveOsmComments() {
|
||||
.merge(comments);
|
||||
|
||||
// must retrieve comments from API before they can be displayed
|
||||
services.improveOSM.getComments(_qaItem, (err, d) => {
|
||||
if (!d.comments) return; // nothing to do here
|
||||
services.improveOSM.getComments(_qaItem)
|
||||
.then(d => {
|
||||
if (!d.comments) return; // nothing to do here
|
||||
|
||||
const commentEnter = comments.selectAll('.comment')
|
||||
.data(d.comments)
|
||||
.enter()
|
||||
.append('div')
|
||||
.attr('class', 'comment');
|
||||
const commentEnter = comments.selectAll('.comment')
|
||||
.data(d.comments)
|
||||
.enter()
|
||||
.append('div')
|
||||
.attr('class', 'comment');
|
||||
|
||||
commentEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-avatar')
|
||||
.call(svgIcon('#iD-icon-avatar', 'comment-avatar-icon'));
|
||||
commentEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-avatar')
|
||||
.call(svgIcon('#iD-icon-avatar', 'comment-avatar-icon'));
|
||||
|
||||
const mainEnter = commentEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-main');
|
||||
const mainEnter = commentEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-main');
|
||||
|
||||
const metadataEnter = mainEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-metadata');
|
||||
const metadataEnter = mainEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-metadata');
|
||||
|
||||
metadataEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-author')
|
||||
.each(function(d) {
|
||||
const osm = services.osm;
|
||||
let selection = d3_select(this);
|
||||
if (osm && d.username) {
|
||||
selection = selection
|
||||
.append('a')
|
||||
.attr('class', 'comment-author-link')
|
||||
.attr('href', osm.userURL(d.username))
|
||||
.attr('tabindex', -1)
|
||||
.attr('target', '_blank');
|
||||
}
|
||||
selection
|
||||
.text(d => d.username);
|
||||
});
|
||||
metadataEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-author')
|
||||
.each(function(d) {
|
||||
const osm = services.osm;
|
||||
let selection = d3_select(this);
|
||||
if (osm && d.username) {
|
||||
selection = selection
|
||||
.append('a')
|
||||
.attr('class', 'comment-author-link')
|
||||
.attr('href', osm.userURL(d.username))
|
||||
.attr('tabindex', -1)
|
||||
.attr('target', '_blank');
|
||||
}
|
||||
selection
|
||||
.text(d => d.username);
|
||||
});
|
||||
|
||||
metadataEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-date')
|
||||
.text(d => t('note.status.commented', { when: localeDateString(d.timestamp) }));
|
||||
metadataEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-date')
|
||||
.text(d => t('note.status.commented', { when: localeDateString(d.timestamp) }));
|
||||
|
||||
mainEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-text')
|
||||
.append('p')
|
||||
.text(d => d.text);
|
||||
});
|
||||
mainEnter
|
||||
.append('div')
|
||||
.attr('class', 'comment-text')
|
||||
.append('p')
|
||||
.text(d => d.text);
|
||||
})
|
||||
.catch(err => {}); // TODO: Handle failed json request gracefully in some way
|
||||
}
|
||||
|
||||
function localeDateString(s) {
|
||||
|
||||
Reference in New Issue
Block a user