Files
iD/modules/ui/improveOSM_comments.js
SilentSpike 51efd5b714 Update and standardise QA implementations
- ES6ify (now using class syntax to define QAItem objects)
- Fix bug with KeepRight marker rendering not updating properly
- Use `qa-` prefix for the UI element classes to differentiate from iD
validation error related UI element classes
- Move away from "error" where possible in source
- Move away from snake_case naming where possible

Note that some function/method names have been untouched to make life
easier for v3 development. Have added note comments where appropriate.
2020-02-06 23:07:50 +00:00

92 lines
2.6 KiB
JavaScript

import { select as d3_select } from 'd3-selection';
import { t } from '../util/locale';
import { svgIcon } from '../svg/icon';
import { services } from '../services';
import { utilDetect } from '../util/detect';
export function uiImproveOsmComments() {
let _qaItem;
function issueComments(selection) {
// make the div immediately so it appears above the buttons
let comments = selection.selectAll('.comments-container')
.data([0]);
comments = comments.enter()
.append('div')
.attr('class', 'comments-container')
.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
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'));
const mainEnter = commentEnter
.append('div')
.attr('class', 'comment-main');
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-date')
.text(d => t('note.status.commented', { when: localeDateString(d.timestamp) }));
mainEnter
.append('div')
.attr('class', 'comment-text')
.append('p')
.text(d => d.text);
});
}
function localeDateString(s) {
if (!s) return null;
const detected = utilDetect();
const options = { day: 'numeric', month: 'short', year: 'numeric' };
const d = new Date(s * 1000); // timestamp is served in seconds, date takes ms
if (isNaN(d.getTime())) return null;
return d.toLocaleDateString(detected.locale, options);
}
issueComments.issue = function(val) {
if (!arguments.length) return _qaItem;
_qaItem = val;
return issueComments;
};
return issueComments;
}