Files
iD/modules/ui/source_switch.js
Quincy Morgan a1af118f0e Ensure locales and presets are loaded before the UI loads (close #7406)
Consolidate localization behavior and init to a coreLocalizer function and singleton
Explicitly support `en-US` locale
Rename coreData to coreFileFetcher and export a singleton rather than using a property of coreContext
Add `apiConnections` property of coreContext to simplify adding a source switcher
Replace some init functions with re-callable, promise-supporting `ensureLoaded` functions
Make coreContext itself load the UI if a container has been specified at init time
Fix code tests
2020-03-31 12:23:31 -07:00

60 lines
1.5 KiB
JavaScript

import {
event as d3_event,
select as d3_select
} from 'd3-selection';
import { t } from '../core/localizer';
import { modeBrowse } from '../modes/browse';
export function uiSourceSwitch(context) {
var keys;
function click() {
d3_event.preventDefault();
var osm = context.connection();
if (!osm) return;
if (context.inIntro()) return;
if (context.history().hasChanges() &&
!window.confirm(t('source_switch.lose_changes'))) return;
var isLive = d3_select(this)
.classed('live');
isLive = !isLive;
context.enter(modeBrowse(context));
context.history().clearSaved(); // remove saved history
context.flush(); // remove stored data
d3_select(this)
.text(isLive ? t('source_switch.live') : t('source_switch.dev'))
.classed('live', isLive)
.classed('chip', isLive);
osm.switch(isLive ? keys[0] : keys[1]); // switch connection (warning: dispatches 'change' event)
}
var sourceSwitch = function(selection) {
selection
.append('a')
.attr('href', '#')
.text(t('source_switch.live'))
.attr('class', 'live chip')
.on('click', click);
};
sourceSwitch.keys = function(_) {
if (!arguments.length) return keys;
keys = _;
return sourceSwitch;
};
return sourceSwitch;
}