Commit af740131 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

when highlighting element check against element existence is run and proper exception is thrown

parent ce15dbbf
"use strict";
/* exported logger */
var logger = require('./logger');
function InvalidArgumentError(message) {
this.name = "InvalidArgumentError";
this.message = message;
this.stack = (new Error()).stack;
}
module.exports = InvalidArgumentError;
......@@ -7,6 +7,7 @@ var logger = require('../../logger');
var AbstractDbOverlay = require('./AbstractDbOverlay');
var IdentifiedElement = require('../data/IdentifiedElement');
var InvalidArgumentError = require('../../InvalidArgumentError');
var Promise = require("bluebird");
......@@ -36,37 +37,59 @@ UserDbOverlay.prototype.getDetailDataByIdentifiedElement = function(element) {
return model.getByIdentifiedElement(element, true);
};
UserDbOverlay.prototype.addMarker = function(params) {
UserDbOverlay.prototype._createIdentifiedElements = function(params, sourceArray) {
var self = this;
var result = [];
var promises = [];
for (var i = 0; i < params.length; i++) {
var singleElementParam = params[i];
var element = new IdentifiedElement(singleElementParam.element);
element.setIcon(singleElementParam.icon);
if (singleElementParam.options !== undefined) {
element.setColor(singleElementParam.options.color);
element.setOpacity(singleElementParam.options.opacity);
element.setLineColor(singleElementParam.options.lineColor);
element.setLineWeight(singleElementParam.options.lineWeight);
element.setLineOpacity(singleElementParam.options.lineOpacity);
}
if (sourceArray[element.getType()][element.getId()] !== undefined) {
return Promise.reject(new Error("Element already highlighted: " + element.getId()));
}
result.push(element);
promises.push(self.getMap().getSubmapById(element.getModelId()).getModel().getByIdentifiedElement(element));
}
return Promise.all(promises).then(function(elementsFromModel) {
for (var i = 0; i < elementsFromModel.length; i++) {
if (elementsFromModel[i] === undefined || elementsFromModel[i] === null) {
return Promise.reject(new InvalidArgumentError("Element doesn't exist: " + result[i].getId()));
}
}
return result;
})
};
UserDbOverlay.prototype.addMarker = function(params) {
var self = this;
self.disableAddRemoveElements();
var elements = [];
if (params.length === undefined) {
params = [ params ];
}
var i, element;
for (i = 0; i < params.length; i++) {
var singleElementParam = params[i];
element = new IdentifiedElement(singleElementParam.element);
element.setIcon(singleElementParam.icon);
if (self._markerIdentifiedElements[element.getType()][element.getId()] !== undefined) {
self.enableAddRemoveElements();
return Promise.reject(new Error("Element already highlighted: " + element.getId()));
return self._createIdentifiedElements(params, self._markerIdentifiedElements).then(function(elements) {
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
self._markerIdentifiedElements[element.getType()][element.getId()] = element;
}
elements.push(element);
}
for (i = 0; i < elements.length; i++) {
element = elements[i];
self._markerIdentifiedElements[element.getType()][element.getId()] = element;
}
return self.getIdentifiedElements().then(function(elements) {
return self.getIdentifiedElements();
}).then(function(elements) {
self.enableAddRemoveElements();
return self.callListeners("onSearch", {
fitBounds : false,
identifiedElements : elements,
});
}).then(null, function(error) {
self.enableAddRemoveElements();
return Promise.reject(error);
});
};
......@@ -74,38 +97,25 @@ UserDbOverlay.prototype.addSurface = function(params) {
var self = this;
self.disableAddRemoveElements();
var elements = [];
if (params.length === undefined) {
params = [ params ];
}
var i, element;
for (i = 0; i < params.length; i++) {
var singleElementParam = params[i];
element = new IdentifiedElement(singleElementParam.element);
if (singleElementParam.options !== undefined) {
element.setColor(singleElementParam.options.color);
element.setOpacity(singleElementParam.options.opacity);
element.setLineColor(singleElementParam.options.lineColor);
element.setLineWeight(singleElementParam.options.lineWeight);
element.setLineOpacity(singleElementParam.options.lineOpacity);
}
if (self._surfaceIdentifiedElements[element.getType()][element.getId()] !== undefined) {
self.enableAddRemoveElements();
return Promise.reject(new Error("Element already highlighted: " + element.getId()));
}
elements.push(element);
}
for (i = 0; i < elements.length; i++) {
element = elements[i];
self._surfaceIdentifiedElements[element.getType()][element.getId()] = element;
}
return self.getIdentifiedElements().then(function(elements) {
return self._createIdentifiedElements(params, self._surfaceIdentifiedElements).then(function(elements) {
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
self._surfaceIdentifiedElements[element.getType()][element.getId()] = element;
}
return self.getIdentifiedElements();
}).then(function(elements) {
self.enableAddRemoveElements();
return self.callListeners("onSearch", {
fitBounds : false,
identifiedElements : elements,
});
}).then(null, function(error) {
self.enableAddRemoveElements();
return Promise.reject(error);
});
};
......
......@@ -4,6 +4,8 @@ var Helper = require('../../Helper');
var logger = require('../../logger');
var CustomMap = require('../../../../main/js/map//CustomMap');
var InvalidArgumentError = require('../../../../main/js/InvalidArgumentError');
var UserDbOverlay = require('../../../../main/js/map/overlay/UserDbOverlay');
var assert = require('assert');
......@@ -27,30 +29,60 @@ describe('UserDbOverlay', function() {
});
it("clear", function() {
var map = helper.createCustomMap();
var oc = new UserDbOverlay({
map : map,
name : 'user'
});
return oc.addMarker({
element : {
id : 329171,
modelId : 15781,
type : "ALIAS"
},
options : {
type : "SURFACE"
}
var overlay;
return ServerConnector.getProject().then(function(project) {
var options = helper.createCustomMapOptions(project);
var map = new CustomMap(options);
overlay = new UserDbOverlay({
map : map,
name : 'user'
});
return overlay.addMarker({
element : {
id : 329171,
modelId : 15781,
type : "ALIAS"
},
options : {
type : "SURFACE"
}
});
}).then(function() {
return oc.getIdentifiedElements();
return overlay.getIdentifiedElements();
}).then(function(elements) {
assert.equal(elements.length, 1);
return oc.clear();
return overlay.clear();
}).then(function() {
return oc.getIdentifiedElements();
return overlay.getIdentifiedElements();
}).then(function(elements) {
assert.equal(elements.length, 0);
});
});
it("add unexisting object", function() {
var overlay;
return ServerConnector.getProject().then(function(project) {
var options = helper.createCustomMapOptions(project);
var map = new CustomMap(options);
overlay = new UserDbOverlay({
map : map,
name : 'user'
});
return overlay.addMarker({
element : {
id : -1,
modelId : 15781,
type : "ALIAS"
},
options : {
type : "SURFACE"
}
});
}).then(function() {
assert.ok(false, "Exception expected");
}, function(error) {
assert.ok(error instanceof InvalidArgumentError)
});
});
});
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