From 4d207471ff7b22b7da2da4b416ddfe16a1d18199 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Tue, 14 Mar 2017 12:17:52 -0400 Subject: [PATCH] Allow raw tag editor to have readonly tags --- css/80_app.css | 9 ++++++ modules/ui/commit.js | 63 ++++++++++++++++++++++-------------- modules/ui/raw_tag_editor.js | 62 +++++++++++++++++++++++++---------- 3 files changed, 93 insertions(+), 41 deletions(-) diff --git a/css/80_app.css b/css/80_app.css index 6be523066..af3e8280d 100644 --- a/css/80_app.css +++ b/css/80_app.css @@ -1601,6 +1601,15 @@ div.combobox { clear: both; } +.tag-row.readonly, +.tag-row.readonly input.key, +.tag-row.readonly input.value, +.tag-row.readonly button.remove { + color: #777777; + background-color: #eee; + cursor: not-allowed; +} + .tag-row input { height: 31px; border: 0; diff --git a/modules/ui/commit.js b/modules/ui/commit.js index b1b3bae3c..c0627a720 100644 --- a/modules/ui/commit.js +++ b/modules/ui/commit.js @@ -18,6 +18,7 @@ import { var changeset; +var readOnlyTags = ['created_by', 'imagery_used', 'host', 'locale']; export function uiCommit(context) { @@ -318,6 +319,31 @@ export function uiCommit(context) { } + function checkComment(comment) { + // Save button disabled if there is no comment.. + d3.selectAll('.save-section .save-button') + .attr('disabled', (comment.length ? null : true)); + + // Warn if comment mentions Google.. + var googleWarning = clippyArea + .html('') + .selectAll('a') + .data(comment.match(/google/i) ? [true] : []); + + googleWarning.exit() + .remove(); + + googleWarning.enter() + .append('a') + .attr('target', '_blank') + .attr('tabindex', -1) + .call(svgIcon('#icon-alert', 'inline')) + .attr('href', t('commit.google_warning_link')) + .append('span') + .text(t('commit.google_warning')); + } + + function change(onInput) { return function() { var comment = commentField.property('value').trim(); @@ -325,36 +351,18 @@ export function uiCommit(context) { commentField.property('value', comment); } - d3.selectAll('.save-section .save-button') - .attr('disabled', (comment.length ? null : true)); - - var googleWarning = clippyArea - .html('') - .selectAll('a') - .data(comment.match(/google/i) ? [true] : []); - - googleWarning.exit() - .remove(); - - googleWarning.enter() - .append('a') - .attr('target', '_blank') - .attr('tabindex', -1) - .call(svgIcon('#icon-alert', 'inline')) - .attr('href', t('commit.google_warning_link')) - .append('span') - .text(t('commit.google_warning')); - - updateChangeset({ comment: comment }); + checkComment(comment); + var changeset = updateChangeset({ comment: comment }); var expanded = !tagSection.selectAll('a.hide-toggle.expanded').empty(); tagSection .call(rawTagEditor .expanded(expanded) + .readOnlyTags(readOnlyTags) .tags(_.clone(changeset.tags)) ); - } + }; } @@ -375,14 +383,21 @@ export function uiCommit(context) { var tags = _.clone(changeset.tags); _.forEach(changed, function(v, k) { - if (v !== undefined || tags.hasOwnProperty(k)) { - tags[k] = v; + k = k.trim(); + if (readOnlyTags.indexOf(k) !== -1) return; + + if (k !== '' && v !== undefined) { + tags[k] = v.trim(); + } else { + delete tags[k]; } }); if (!_.isEqual(changeset.tags, tags)) { changeset = changeset.update({ tags: tags }); } + + return changeset; } } diff --git a/modules/ui/raw_tag_editor.js b/modules/ui/raw_tag_editor.js index ab1d1c0df..e67674f4c 100644 --- a/modules/ui/raw_tag_editor.js +++ b/modules/ui/raw_tag_editor.js @@ -16,6 +16,7 @@ export function uiRawTagEditor(context) { var taginfo = services.taginfo, dispatch = d3.dispatch('change'), expanded = context.storage('raw_tag_editor.expanded') === 'true', + readOnlyTags = [], updatePreference = true, showBlank = false, state, @@ -82,7 +83,8 @@ export function uiRawTagEditor(context) { var enter = items.enter() .append('li') - .attr('class', 'tag-row cf'); + .attr('class', 'tag-row cf') + .classed('readonly', isReadOnly); enter .append('div') @@ -154,16 +156,26 @@ export function uiRawTagEditor(context) { items.selectAll('input.key') .attr('title', function(d) { return d.key; }) - .call(utilGetSetValue, function(d) { return d.key; }); + .call(utilGetSetValue, function(d) { return d.key; }) + .property('disabled', isReadOnly); + // .classed('deemphasize', isReadOnly); items.selectAll('input.value') .attr('title', function(d) { return d.value; }) - .call(utilGetSetValue, function(d) { return d.value; }); + .call(utilGetSetValue, function(d) { return d.value; }) + .property('disabled', isReadOnly); + // .classed('deemphasize', isReadOnly); items.selectAll('button.remove') .on('click', removeTag); + + function isReadOnly(d) { + return readOnlyTags.indexOf(d.key) !== -1; + } + + function pushMore() { if (d3.event.keyCode === 9 && !d3.event.shiftKey && list.selectAll('li:last-child input.value').node() === this) { @@ -173,20 +185,7 @@ export function uiRawTagEditor(context) { function bindTypeahead(key, value) { - - function sort(value, data) { - var sameletter = [], - other = []; - for (var i = 0; i < data.length; i++) { - if (data[i].value.substring(0, value.length) === value) { - sameletter.push(data[i]); - } else { - other.push(data[i]); - } - } - return sameletter.concat(other); - } - + if (isReadOnly({ key: key })) return; var geometry = context.geometry(id); key.call(d3combobox() @@ -211,6 +210,20 @@ export function uiRawTagEditor(context) { if (!err) callback(sort(value, data)); }); })); + + + function sort(value, data) { + var sameletter = [], + other = []; + for (var i = 0; i < data.length; i++) { + if (data[i].value.substring(0, value.length) === value) { + sameletter.push(data[i]); + } else { + other.push(data[i]); + } + } + return sameletter.concat(other); + } } @@ -230,6 +243,12 @@ export function uiRawTagEditor(context) { kNew = this.value.trim(), tag = {}; + + if (isReadOnly({ key: kNew })) { + this.value = kOld; + return; + } + if (kNew && kNew !== kOld) { var match = kNew.match(/^(.*?)(?:_(\d+))?$/), base = match[1], @@ -247,6 +266,7 @@ export function uiRawTagEditor(context) { function valueChange(d) { + if (isReadOnly(d)) return; var tag = {}; tag[d.key] = this.value; dispatch.call('change', this, tag); @@ -254,6 +274,7 @@ export function uiRawTagEditor(context) { function removeTag(d) { + if (isReadOnly(d)) return; var tag = {}; tag[d.key] = undefined; dispatch.call('change', this, tag); @@ -317,5 +338,12 @@ export function uiRawTagEditor(context) { }; + rawTagEditor.readOnlyTags = function(_) { + if (!arguments.length) return readOnlyTags; + readOnlyTags = _; + return rawTagEditor; + }; + + return utilRebind(rawTagEditor, dispatch, 'on'); }