Refactor UndoRedo, fix initial tooltip state

Fixes #1487.
This commit is contained in:
John Firebaugh
2013-05-29 19:42:54 -07:00
parent c2fa4da23c
commit e83423c7e2
+32 -29
View File
@@ -1,47 +1,50 @@
iD.ui.UndoRedo = function(context) {
var commands = [{
id: 'undo',
cmd: iD.ui.cmd('⌘Z'),
action: context.undo,
annotation: function() { return context.history().undoAnnotation(); }
}, {
id: 'redo',
cmd: iD.ui.cmd('⌘⇧Z'),
action: context.redo,
annotation: function() { return context.history().redoAnnotation(); }
}];
return function(selection) {
var tooltip = bootstrap.tooltip()
.placement('bottom')
.html(true);
.html(true)
.title(function (d) {
return iD.ui.tooltipHtml(d.annotation() || t('nothing_to_' + d.id), d.cmd);
});
var undoButton = selection.append('button')
var buttons = selection.selectAll('button')
.data(commands)
.enter().append('button')
.attr('class', 'col6 disabled')
.html('<span class="undo icon"/>')
.on('click', context.undo)
.on('click', function(d) { return d.action(); })
.call(tooltip);
var redoButton = selection.append('button')
.attr('class', 'col6 disabled')
.html('<span class="redo icon"/>')
.on('click', context.redo)
.call(tooltip);
buttons.append('span')
.attr('class', function(d) { return 'icon ' + d.id; });
var keybinding = d3.keybinding('undo')
.on(iD.ui.cmd('⌘Z'), context.undo)
.on(iD.ui.cmd('⌘⇧Z'), context.redo);
.on(commands[0].cmd, commands[0].action)
.on(commands[1].cmd, commands[1].action);
d3.select(document)
.call(keybinding);
context.history().on('change.editor', function() {
var undo = context.history().undoAnnotation(),
redo = context.history().redoAnnotation();
function refreshTooltip(selection) {
if (selection.property('tooltipVisible')) {
selection.call(tooltip.show);
}
}
undoButton
.classed('disabled', !undo)
.attr('data-original-title', iD.ui.tooltipHtml(undo || t('nothing_to_undo'), iD.ui.cmd('⌘Z')))
.call(refreshTooltip);
redoButton
.classed('disabled', !redo)
.attr('data-original-title', iD.ui.tooltipHtml(redo || t('nothing_to_redo'), iD.ui.cmd('⌘⇧Z')))
.call(refreshTooltip);
buttons
.classed('disabled', function(d) { return !d.annotation(); })
.each(function() {
var selection = d3.select(this);
if (selection.property('tooltipVisible')) {
selection.call(tooltip.show);
}
});
});
};
};