Test and fix current layer rename.

master
Flint O'Brien 2016-05-02 18:33:45 -04:00
parent 57047b4fb1
commit 2799b4cba2
4 changed files with 61 additions and 11 deletions

View File

@ -300,10 +300,20 @@ svgedit.draw.Drawing.prototype.getCurrentLayerName = function () {
/**
* Set the current layer's name.
* @param {string} name - The new name.
* @returns {Object} If the name was changed, returns {title:SVGGElement, previousName:string}; otherwise null.
* @param {svgedit.history.HistoryRecordingService} hrService - History recording service
* @returns {string|null} The new name if changed; otherwise, null.
*/
svgedit.draw.Drawing.prototype.setCurrentLayerName = function (name) {
return this.current_layer ? this.current_layer.setName(name) : null;
svgedit.draw.Drawing.prototype.setCurrentLayerName = function (name, hrService) {
var finalName = null;
if( this.current_layer) {
var oldName = this.current_layer.getName();
finalName = this.current_layer.setName(name, hrService);
if( finalName) {
delete this.layer_map[oldName];
this.layer_map[finalName] = this.current_layer;
}
}
return finalName;
};
/**

View File

@ -155,7 +155,13 @@ Layer.prototype.getTitleElement = function() {
return null;
};
Layer.prototype.setName = function(name) {
/**
* Set the name of this layer.
* @param {string} name - The new name.
* @param {svgedit.history.HistoryRecordingService} hrService - History recording service
* @returns {string|null} The new name if changed; otherwise, null.
*/
Layer.prototype.setName = function(name, hrService) {
var previousName = this.name_;
name = svgedit.utilities.toXml(name);
// now change the underlying title element contents
@ -164,7 +170,10 @@ Layer.prototype.setName = function(name) {
while (title.firstChild) { title.removeChild(title.firstChild); }
title.textContent = name;
this.name_ = name;
return {title: title, previousName: previousName};
if( hrService) {
hrService.changeElement(title, {'#text':previousName});
}
return this.name_;
}
return null;
};

View File

@ -5058,11 +5058,9 @@ this.renameCurrentLayer = function(newname) {
var drawing = getCurrentDrawing();
var layer = drawing.getCurrentLayer();
if (layer) {
var result = drawing.setCurrentLayerName( newname);
hrService = new svgedit.history.HistoryRecordingService(this.undoMgr);
var result = drawing.setCurrentLayerName(newname, hrService);
if (result) {
var batchCmd = new svgedit.history.BatchCommand('Rename Layer');
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(result.title, {'#text':result.previousName}));
addCommandToHistory(batchCmd);
call('changed', [layer]);
return true;
}

View File

@ -376,16 +376,49 @@
equals(typeof drawing.setCurrentLayer, typeof function(){});
drawing.setCurrentLayer(LAYER2);
equals(drawing.getCurrentLayerName(LAYER2), LAYER2);
equals(drawing.getCurrentLayerName(), LAYER2);
equals(drawing.getCurrentLayer(), drawing.all_layers[1].getGroup());
drawing.setCurrentLayer(LAYER3);
equals(drawing.getCurrentLayerName(LAYER3), LAYER3);
equals(drawing.getCurrentLayerName(), LAYER3);
equals(drawing.getCurrentLayer(), drawing.all_layers[2].getGroup());
cleanupSvg(svg);
});
test('Test setCurrentLayerName()', function() {
var mockHrService = {
changeElement: function(elem, attrs){
mockHrService.elem = elem;
mockHrService.attrs = attrs;
}
}
var drawing = new svgedit.draw.Drawing(svg);
setupSvgWith3Layers(svg);
drawing.identifyLayers();
ok(drawing.setCurrentLayerName);
equals(typeof drawing.setCurrentLayerName, typeof function(){});
var oldName = drawing.getCurrentLayerName();
var newName = 'New Name'
ok( drawing.layer_map[oldName]);
equals( drawing.layer_map[newName], undefined); // newName shouldn't exist.
var result = drawing.setCurrentLayerName(newName, mockHrService);
equals(result, newName);
equals(drawing.getCurrentLayerName(), newName);
// Was the map updated?
equals( drawing.layer_map[oldName], undefined);
equals( drawing.layer_map[newName], drawing.current_layer);
// Was mockHrService called?
equals( oldName, mockHrService.attrs['#text']);
equals( newName, mockHrService.elem.textContent);
cleanupSvg(svg);
});
test('Test createLayer()', function() {
expect(7);