Files
iD/modules/ui/changeset_editor.js
Bryan Housel 39b3f1df68 Avoid creating comboboxes
(closes #5568)

Several strategies in here:
- Move uiCombobox() from inside the render function to class variable
- Don't render stuff like the raw tag editor when it's collapsed
- Don't show as many fields/combos on hover
- Don't instantiate fields (like universal/more) until they're actually shown
- Bind the combo on enter selection not on update selection
2018-12-11 16:07:00 -05:00

140 lines
4.1 KiB
JavaScript

import _uniqBy from 'lodash-es/uniqBy';
import { dispatch as d3_dispatch } from 'd3-dispatch';
import { t } from '../util/locale';
import { svgIcon } from '../svg';
import { uiCombobox, uiField, uiFormFields } from './index';
import { utilRebind, utilTriggerEvent } from '../util';
export function uiChangesetEditor(context) {
var dispatch = d3_dispatch('change');
var formFields = uiFormFields(context);
var commentCombo = uiCombobox(context, 'comment').caseSensitive(true);
var _fieldsArr;
var _tags;
var _changesetID;
function changesetEditor(selection) {
render(selection);
}
function render(selection) {
var initial = false;
if (!_fieldsArr) {
initial = true;
var presets = context.presets();
_fieldsArr = [
uiField(context, presets.field('comment'), null, { show: true, revert: false }),
uiField(context, presets.field('source'), null, { show: false, revert: false }),
uiField(context, presets.field('hashtags'), null, { show: false, revert: false }),
];
_fieldsArr.forEach(function(field) {
field
.on('change', function(t, onInput) {
dispatch.call('change', field, t, onInput);
});
});
}
_fieldsArr.forEach(function(field) {
field
.tags(_tags);
});
selection
.call(formFields.fieldsArr(_fieldsArr));
if (initial) {
var commentField = selection.select('#preset-input-comment');
var commentNode = commentField.node();
if (commentNode) {
commentNode.focus();
commentNode.select();
}
// trigger a 'blur' event so that comment field can be cleaned
// and checked for hashtags, even if retrieved from localstorage
utilTriggerEvent(commentField, 'blur');
var osm = context.connection();
if (osm) {
osm.userChangesets(function (err, changesets) {
if (err) return;
var comments = changesets.map(function(changeset) {
return {
title: changeset.tags.comment,
value: changeset.tags.comment
};
});
commentField
.call(commentCombo
.data(_uniqBy(comments, 'title'))
);
});
}
}
// Add warning if comment mentions Google
var hasGoogle = _tags.comment.match(/google/i);
var commentWarning = selection.select('.form-field-comment').selectAll('.comment-warning')
.data(hasGoogle ? [0] : []);
commentWarning.exit()
.transition()
.duration(200)
.style('opacity', 0)
.remove();
var commentEnter = commentWarning.enter()
.insert('div', '.tag-reference-body')
.attr('class', 'field-warning comment-warning')
.style('opacity', 0);
commentEnter
.append('a')
.attr('target', '_blank')
.attr('tabindex', -1)
.call(svgIcon('#iD-icon-alert', 'inline'))
.attr('href', t('commit.google_warning_link'))
.append('span')
.text(t('commit.google_warning'));
commentEnter
.transition()
.duration(200)
.style('opacity', 1);
}
changesetEditor.tags = function(_) {
if (!arguments.length) return _tags;
_tags = _;
// Don't reset _fieldsArr here.
return changesetEditor;
};
changesetEditor.changesetID = function(_) {
if (!arguments.length) return _changesetID;
if (_changesetID === _) return changesetEditor;
_changesetID = _;
_fieldsArr = null;
return changesetEditor;
};
return utilRebind(changesetEditor, dispatch, 'on');
}