Skip to content

Commit 3ad903b

Browse files
committed
New update event signature
1 parent bf92c4e commit 3ad903b

File tree

1 file changed

+38
-44
lines changed

1 file changed

+38
-44
lines changed

js/jquery.mapael.js

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -222,25 +222,23 @@
222222
*
223223
* Update the current map
224224
* Refresh attributes and tooltips for areas and plots
225-
* @param updatedOptions options to update for plots and areas
226-
* @param newPlots new plots to add to the map
227-
* @param deletedPlots plots to delete from the map (array, or "all" to remove all plots)
228225
* @param opt option for the refresh :
229-
* opt.animDuration animation duration in ms (default = 0)
230-
* opt.resetAreas true to reset previous areas options
231-
* opt.resetPlots true to reset previous plots options
232-
* opt.resetLinks true to reset previous links options
233-
* opt.afterUpdate Hook that allows to add custom processing on the map
226+
* opt.mapOptions: options to update for plots and areas
227+
* opt.replaceOptions: whether mapsOptions should entirely replace current map options, or just extend it
228+
* opt.opt.newPlots new plots to add to the map
234229
* opt.newLinks new links to add to the map
235-
* opt.deletedLinks links to remove from the map (array, or "all" to remove all links)
230+
* opt.deletePlotKeys plots to delete from the map (array, or "all" to remove all plots)
231+
* opt.deleteLinkKeys links to remove from the map (array, or "all" to remove all links)
236232
* opt.setLegendElemsState the state of legend elements to be set : show (default) or hide
233+
* opt.animDuration animation duration in ms (default = 0)
234+
* opt.afterUpdate Hook that allows to add custom processing on the map
237235
*/
238-
$self.on("update", function(e, updatedOptions, newPlots, deletedPlots, opt) {
236+
$self.on("update", function(e, opt) {
239237
var i = 0
240-
, animDuration = 0
238+
, animDuration = (opt.animDuration) ? (opt.animDuration) : 0
241239
, elemOptions = {}
242240
// This function remove an element using animation (or not, depending on animDuration)
243-
// Used for deletedPlots and deletedLinks
241+
// Used for deletePlotKeys and deleteLinkKeys
244242
, fnRemoveElement = function(elem) {
245243
// Unset all event handlers
246244
Mapael.unsetHover(elem.mapElem, elem.textElem);
@@ -272,42 +270,38 @@
272270
}
273271
};
274272

275-
if (typeof opt != "undefined") {
276-
if (opt.resetAreas) options.areas = {};
277-
if (opt.resetPlots) options.plots = {};
278-
if (opt.resetLinks) options.links = {};
279-
if (opt.animDuration) animDuration = opt.animDuration;
273+
if (opt.mapOptions) {
274+
if (opt.replaceOptions === true) options = opt.mapOptions;
275+
else $.extend(true, options, opt.mapOptions);
280276
}
281277

282-
$.extend(true, options, updatedOptions);
283-
284-
// Delete plots by name if deletedPlots is array
285-
if (typeof deletedPlots == "object") {
286-
for (;i < deletedPlots.length; i++) {
287-
if (typeof plots[deletedPlots[i]] != "undefined") {
288-
fnRemoveElement(plots[deletedPlots[i]]);
289-
delete plots[deletedPlots[i]];
278+
// Delete plots by name if deletePlotKeys is array
279+
if (typeof opt.deletePlotKeys === "object") {
280+
for (;i < opt.deletePlotKeys.length; i++) {
281+
if (typeof plots[opt.deletePlotKeys[i]] != "undefined") {
282+
fnRemoveElement(plots[opt.deletePlotKeys[i]]);
283+
delete plots[opt.deletePlotKeys[i]];
290284
}
291285
}
292-
// Delete ALL plots if deletedPlots is set to "all"
293-
} else if (deletedPlots === "all") {
286+
// Delete ALL plots if deletePlotKeys is set to "all"
287+
} else if (opt.deletePlotKeys === "all") {
294288
$.each(plots, function(id, elem) {
295289
fnRemoveElement(elem);
296290
});
297291
// Empty plots object
298292
plots = {};
299293
}
300294

301-
// Delete links by name if deletedLinks is array
302-
if (typeof opt != "undefined" && typeof opt.deletedLinks == "object") {
303-
for (i = 0;i < opt.deletedLinks.length; i++) {
304-
if (typeof links[opt.deletedLinks[i]] != "undefined") {
305-
fnRemoveElement(links[opt.deletedLinks[i]]);
306-
delete links[opt.deletedLinks[i]];
295+
// Delete links by name if deleteLinkKeys is array
296+
if (typeof opt.deleteLinkKeys === "object") {
297+
for (i = 0;i < opt.deleteLinkKeys.length; i++) {
298+
if (typeof links[opt.deleteLinkKeys[i]] != "undefined") {
299+
fnRemoveElement(links[opt.deleteLinkKeys[i]]);
300+
delete links[opt.deleteLinkKeys[i]];
307301
}
308302
}
309-
// Delete ALL links if deletedLinks is set to "all"
310-
} else if (typeof opt != "undefined" && opt.deletedLinks === "all") {
303+
// Delete ALL links if deleteLinkKeys is set to "all"
304+
} else if (typeof opt != "undefined" && opt.deleteLinkKeys === "all") {
311305
$.each(links, function(id, elem) {
312306
fnRemoveElement(elem);
313307
});
@@ -316,10 +310,10 @@
316310
}
317311

318312
// New plots
319-
if (typeof newPlots == "object") {
320-
$.each(newPlots, function(id) {
313+
if (typeof opt.newPlots === "object") {
314+
$.each(opt.newPlots, function(id) {
321315
if (typeof plots[id] == "undefined") {
322-
options.plots[id] = newPlots[id];
316+
options.plots[id] = opt.newPlots[id];
323317
plots[id] = Mapael.drawPlot(id, options, mapConf, paper, $tooltip);
324318
if (animDuration > 0) {
325319
fnShowElement(plots[id]);
@@ -329,7 +323,7 @@
329323
}
330324

331325
// New links
332-
if (typeof opt != "undefined" && typeof opt.newLinks == "object") {
326+
if (typeof opt.newLinks === "object") {
333327
var newLinks = Mapael.drawLinksCollection(paper, options, opt.newLinks, mapConf.getCoords, $tooltip);
334328
$.extend(links, newLinks);
335329
$.extend(options.links, opt.newLinks);
@@ -387,7 +381,7 @@
387381
});
388382

389383
// Update legends
390-
if (typeof updatedOptions != "undefined" && typeof updatedOptions.legend === "object") {
384+
if (typeof opt.mapOptions.legend === "object") {
391385
Mapael.createLegends($self, options, "area", areas, 1);
392386
if (options.map.width) {
393387
Mapael.createLegends($self, options, "plot", plots, (options.map.width / mapConf.width));
@@ -400,7 +394,7 @@
400394
// Toggle (i.e. click) only if:
401395
// - slice legend is shown AND we want to hide
402396
// - slice legend is hidden AND we want to show
403-
if (typeof opt != "undefined" && typeof opt.setLegendElemsState === "object") {
397+
if (typeof opt.setLegendElemsState === "object") {
404398
// setLegendElemsState is an object listing the legend we want to hide/show
405399
$.each(opt.setLegendElemsState, function (legendCSSClass, action) {
406400
// Search for the legend
@@ -419,7 +413,7 @@
419413
} else {
420414
// setLegendElemsState is a string, or is undefined
421415
// Default : "show"
422-
var action = (typeof opt != "undefined" && opt.setLegendElemsState === "hide") ? "hide" : "show";
416+
var action = (opt.setLegendElemsState === "hide") ? "hide" : "show";
423417

424418
$("[data-type='elem']", $self).each(function(id, elem) {
425419
if (($(elem).attr('data-hidden') === "0" && action === "hide") ||
@@ -430,8 +424,8 @@
430424
});
431425
}
432426

433-
if (typeof opt != "undefined" && opt.afterUpdate)
434-
opt.afterUpdate($self, paper, areas, plots, options);
427+
if (opt.afterUpdate) opt.afterUpdate($self, paper, areas, plots, options);
428+
435429
});
436430

437431
// Handle resizing of the map

0 commit comments

Comments
 (0)