Commit 86717b02 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

opening layout is done as a promise

unit tests added
parent ff283844
......@@ -1008,26 +1008,30 @@ ServerConnector.getOverlayElements = function(layoutId, projectId) {
if (layoutId === undefined) {
throw new Error("Layout id must be defined");
}
return new Promise(function(resolve, reject) {
self.getToken().then(function(token) {
self.getProjectId(projectId).then(function(projectId) {
self.readFile(self.getOverlayElementsUrl(layoutId, projectId, token)).then(function(content) {
var arr = JSON.parse(content);
var result = [];
for (var i = 0; i < arr.length; i++) {
var element = arr[i];
if (element.type === "REACTION") {
result.push(new LayoutReaction(element.overlayContent));
} else if (element.type === "ALIAS") {
result.push(new LayoutAlias(element.overlayContent));
} else {
reject("Unknown element type: " + element.type);
}
}
resolve(result);
}, reject);
}, reject);
}, reject);
var token = null;
self.getToken().then(function(result) {
token = result;
return self.getProjectId(projectId);
}).then(function(result) {
projectId = result;
return self.readFile(self.getOverlayElementsUrl(layoutId, projectId, token));
}).then(function(content) {
var arr = JSON.parse(content);
var result = [];
for (var i = 0; i < arr.length; i++) {
var element = arr[i];
if (element.type === "REACTION") {
result.push(new LayoutReaction(element.overlayContent));
} else if (element.type === "ALIAS") {
result.push(new LayoutAlias(element.overlayContent));
} else {
throw new Error("Unknown element type: " + element.type);
}
}
resolve(result);
}).catch(reject);
});
};
......
......@@ -523,20 +523,24 @@ AbstractCustomMap.prototype.getTopMap = function() {
* number of layouts that are currently visualized
*/
AbstractCustomMap.prototype._resizeSelectedLayout = function(layoutId, index, length) {
// if map is not initialized then don't perform this operation
if (!this.initialized) {
logger.debug("Model " + this.getId() + " not initialized");
return;
}
logger.debug("Resize layout: " + layoutId);
// start ratio
var startX = index * (1.0 / length);
// end ratio
var endX = (index + 1) * (1.0 / length);
for (var i = 0; i < this.selectedLayoutOverlays[layoutId].length; i++) {
this.selectedLayoutOverlays[layoutId][i].setBoundsForAlias(startX, endX);
}
var self = this;
return new Promise(function(resolve, reject) {
// if map is not initialized then don't perform this operation
if (!self.initialized) {
logger.debug("Model " + self.getId() + " not initialized");
resolve();
}
logger.debug("Resize layout: " + layoutId);
// start ratio
var startX = index * (1.0 / length);
// end ratio
var endX = (index + 1) * (1.0 / length);
for (var i = 0; i < self.selectedLayoutOverlays[layoutId].length; i++) {
self.selectedLayoutOverlays[layoutId][i].setBoundsForAlias(startX, endX);
}
resolve();
});
};
/**
......@@ -554,66 +558,66 @@ AbstractCustomMap.prototype._resizeSelectedLayout = function(layoutId, index, le
* @param length
* number of layouts that are currently visualized
*/
AbstractCustomMap.prototype._showSelectedLayout = function(layoutId, index, length, onready) {
AbstractCustomMap.prototype._showSelectedLayout = function(layoutId, index, length) {
var self = this;
// if map is not initialized then don't perform this operation
if (!this.initialized) {
logger.debug("Model " + this.getId() + " not initialized");
return;
} else {
logger.debug("Showing model " + this.getId());
}
return new Promise(function(resolve, reject) {
if (!self.initialized) {
logger.debug("Model " + self.getId() + " not initialized");
resolve();
} else {
logger.debug("Showing model " + self.getId());
}
this.selectedLayoutOverlays[layoutId] = [];
self.selectedLayoutOverlays[layoutId] = [];
// start ratio
var startX = index * (1.0 / length);
// end ratio
var endX = (index + 1) * (1.0 / length);
// start ratio
var startX = index * (1.0 / length);
// end ratio
var endX = (index + 1) * (1.0 / length);
var elementsToProcess = 0;
var elementsProcessed = 0;
var elementProcessedFun = function() {
if (elementsProcessed === elementsToProcess) {
if (onready !== undefined) {
onready();
var elementsToProcess = 0;
var elementsProcessed = 0;
var elementProcessedFun = function() {
if (elementsProcessed === elementsToProcess) {
resolve();
}
}
};
this.getModel().getLayoutDataById(layoutId).then(function(layout) {
var layoutAliases = layout.getAliases();
elementsToProcess += layoutAliases.length;
for (var i = 0; i < layoutAliases.length; i++) {
};
var layoutAlias = layoutAliases[i];
self.getModel().getAliasById(layoutAlias.getId()).then(function(aliasData) {
if (aliasData === null || aliasData === undefined) {
throw new Error("Cannot find data for alias: " + layoutAlias.getId());
} else {
var overlay = AliasOverlay.create(layoutAlias, aliasData, self, startX, endX);
self.selectedLayoutOverlays[layoutId].push(overlay);
}
elementsProcessed++;
elementProcessedFun();
});
}
return self.getModel().getLayoutDataById(layoutId).then(function(layout) {
var layoutAliases = layout.getAliases();
elementsToProcess += layoutAliases.length;
for (var i = 0; i < layoutAliases.length; i++) {
var layoutAlias = layoutAliases[i];
self.getModel().getAliasById(layoutAlias.getId()).then(function(aliasData) {
if (aliasData === null || aliasData === undefined) {
throw new Error("Cannot find data for alias: " + layoutAlias.getId());
} else {
var overlay = AliasOverlay.create(layoutAlias, aliasData, self, startX, endX);
self.selectedLayoutOverlays[layoutId].push(overlay);
}
elementsProcessed++;
elementProcessedFun();
}, reject);
}
var layoutReactions = layout.getReactions();
var layoutReactions = layout.getReactions();
elementsToProcess += layoutReactions.length;
for (var j = 0; j < layoutReactions.length; j++) {
elementsToProcess += layoutReactions.length;
for (var j = 0; j < layoutReactions.length; j++) {
var layoutReaction = layoutReactions[j];
self.getModel().getReactionById(layoutReaction.getId()).then(function(reactionData) {
var reactionOverlay = new ReactionOverlay(layoutReaction, reactionData, self, length === 1);
self.selectedLayoutOverlays[layoutId].push(reactionOverlay);
elementsProcessed++;
elementProcessedFun();
});
}
elementProcessedFun();
var layoutReaction = layoutReactions[j];
self.getModel().getReactionById(layoutReaction.getId()).then(function(reactionData) {
var reactionOverlay = new ReactionOverlay(layoutReaction, reactionData, self, length === 1);
self.selectedLayoutOverlays[layoutId].push(reactionOverlay);
elementsProcessed++;
elementProcessedFun();
}, reject);
}
elementProcessedFun();
});
});
};
......
......@@ -1058,12 +1058,15 @@ CustomMap.prototype.addSelectedLayout = function(identifier) {
resolve();
} else {
self.selectedLayouts[identifier] = true;
self.getModel().initLayoutDataByLayoutId(identifier).then(function() {
self.refreshSelectedLayouts();
ServerConnector.setVisibleLayouts(JSON.stringify(self.getSelectedLayouts()));
return self.getModel().initLayoutDataByLayoutId(identifier).then(function() {
return self.refreshSelectedLayouts();
}).then(function() {
return ServerConnector.setVisibleLayouts(JSON.stringify(self.getSelectedLayouts()));
}).then(function() {
resolve();
}, reject);
}).catch(function(exception){
reject(exception);
});
}
});
};
......@@ -1110,28 +1113,31 @@ CustomMap.prototype.removeSelectedLayout = function(identifier) {
*/
CustomMap.prototype.refreshSelectedLayouts = function() {
logger.debug("Refreshing layouts");
var layouts = this.getSelectedLayouts();
var self = this;
var layouts = self.getSelectedLayouts();
// show layouts that should be visualized (resize or show them)
var promises = [];
for (var i = 0; i < layouts.length; i++) {
var layoutId = layouts[i];
if (this.layoutContainsOverlays(layoutId)) {
if (self.layoutContainsOverlays(layoutId)) {
// resize element on the map
this.resizeSelectedLayout(layoutId, i, layouts.length);
promises.push(self.resizeSelectedLayout(layoutId, i, layouts.length));
} else {
this.showSelectedLayout(layoutId, i, layouts.length);
promises.push(self.showSelectedLayout(layoutId, i, layouts.length));
}
}
// remove layouts that were
for ( var key in this.selectedLayoutOverlays) {
if (!this.selectedLayouts.hasOwnProperty(key) || this.selectedLayouts[key] === false) {
if (this.layoutContainsOverlays(key)) {
this.hideSelectedLayout(key);
return Promise.all(promises).then(function() {
// remove layouts that were
for ( var key in self.selectedLayoutOverlays) {
if (!self.selectedLayouts.hasOwnProperty(key) || self.selectedLayouts[key] === false) {
if (self.layoutContainsOverlays(key)) {
self.hideSelectedLayout(key);
}
}
}
}
this.refreshInfoWindows();
self.refreshInfoWindows();
});
};
/**
......@@ -1160,10 +1166,12 @@ CustomMap.prototype.hideSelectedLayout = function(layoutId) {
*/
CustomMap.prototype.resizeSelectedLayout = function(layoutId, index, length) {
logger.debug("Resize layout: " + layoutId);
this._resizeSelectedLayout(layoutId, index, length);
var promises = [];
promises.push(this._resizeSelectedLayout(layoutId, index, length));
for (var i = 0; i < this.submaps.length; i++) {
this.submaps[i]._resizeSelectedLayout(layoutId, index, length);
promises.push(this.submaps[i]._resizeSelectedLayout(layoutId, index, length));
}
return Promise.all(promises);
};
/**
......@@ -1178,10 +1186,13 @@ CustomMap.prototype.resizeSelectedLayout = function(layoutId, index, length) {
*/
CustomMap.prototype.showSelectedLayout = function(layoutId, index, length) {
logger.debug("Resize layout: " + layoutId);
this._showSelectedLayout(layoutId, index, length);
var promises = [];
promises.push(this._showSelectedLayout(layoutId, index, length));
for (var i = 0; i < this.submaps.length; i++) {
this.submaps[i]._showSelectedLayout(layoutId, index, length);
promises.push(this.submaps[i]._showSelectedLayout(layoutId, index, length));
}
return Promise.all(promises);
};
/**
......
......@@ -172,19 +172,26 @@ MapModel.prototype.getReactionById = function(id) {
MapModel.prototype.getMissingElements = function(elements) {
var self = this;
var layouts = this._getLayouts();
var aliasIds = [];
var reactionIds = [];
var i=0;
if (elements.reactionIds!==undefined) {
reactionIds.push.apply(reactionIds, elements.reactionIds);
for (i=0;i<reactionIds.length;i++) {
this._missingReactions[reactionIds[i]]=reactionIds[i];
}
}
if (elements.aliasIds!==undefined) {
aliasIds.push.apply(aliasIds, elements.aliasIds);
for (i=0;i<aliasIds.length;i++) {
this._missingAliases[aliasIds[i]]=aliasIds[i];
}
}
for (i=0;i<layouts.length;i++) {
var layout = layouts[i];
if (layout.isInitialized()) {
......@@ -506,9 +513,10 @@ MapModel.prototype.initLayoutDataByLayoutId = function(layoutId) {
var self = this;
return new Promise(function(resolve, reject) {
self.getLayoutDataById(layoutId).then(function(layout) {
layout.init();
return layout.init();
}).then(function(){
resolve();
}, reject);
}).catch(reject);
});
};
......
......@@ -96,7 +96,24 @@ describe('CustomMap', function() {
});
});
it("_showSelectedLayout", function(done) {
it("addSelectedLayout with data download", function() {
var map = helper.createCustomMap();
var layout = helper.createLayout();
layout.setId(102);
map.getModel().addLayout(layout);
return map.addSelectedLayout(layout.getId()).then(function() {
var vLayouts = JSON.parse(ServerConnector.getVisibleLayouts());
assert.equal(1, vLayouts.length);
assert.equal(layout.getId(), vLayouts[0]);
var overlays = map.getSelectedLayoutOverlays()[layout.getId()];
assert.equal(overlays.length, 1);
assert.ok(overlays[0] instanceof AliasOverlay);
});
});
it("_showSelectedLayout", function() {
var map = helper.createCustomMap();
var alias = helper.createAlias();
alias.setModelId(map.getId());
......@@ -117,9 +134,9 @@ describe('CustomMap', function() {
map.getModel().addLayout(layout);
map.addSelectedLayout(layout.getId());
map._showSelectedLayout(layout.getId(), 0, 1, function() {
return map.addSelectedLayout(layout.getId()).then(function() {
return map._showSelectedLayout(layout.getId(), 0, 1);
}).then(function() {
assert.equal(2, map.selectedLayoutOverlays[layout.getId()].length);
assert.ok(map.selectedLayoutOverlays[layout.getId()][0] instanceof AliasOverlay);
......@@ -129,8 +146,6 @@ describe('CustomMap', function() {
map._hideSelectedLayout(layout.getId());
assert.ok(map.selectedLayoutOverlays[layout.getId()]);
assert.equal(0, map.selectedLayoutOverlays[layout.getId()].length);
done();
});
});
......
[{"type":"ALIAS","overlayContent":{"value":1.0,"modelId":100,"idObject":"1021"}}]
\ No newline at end of file
[{"modelId":100,"bounds":{"x":683.0,"y":132.0,"width":70.0,"height":25.0},"id":1021}]
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment