Skip to content

Commit e2524ea

Browse files
committed
add api helpers for managing array container updates
1 parent 133bf1a commit e2524ea

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/plot_api/helpers.js

+48
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,51 @@ exports.coerceTraceIndices = function(gd, traceIndices) {
430430

431431
return traceIndices;
432432
};
433+
434+
/**
435+
* Manages logic around array container item creation / deletion / update
436+
* that nested property along can't handle.
437+
*
438+
* @param {Object} np
439+
* nested property of update attribute string about trace or layout object
440+
* @param {*} newVal
441+
* update value passed to restyle / relayout / update
442+
* @param {Object} undoit
443+
* undo hash (N.B. undoit may be mutated here).
444+
*
445+
*/
446+
exports.manageArrayContainers = function(np, newVal, undoit) {
447+
var obj = np.obj,
448+
parts = np.parts,
449+
pLength = parts.length,
450+
pLast = parts[pLength - 1];
451+
452+
var pLastIsNumber = isNumeric(pLast);
453+
454+
// delete item
455+
if(pLastIsNumber && newVal === null) {
456+
457+
// Clear item in array container when new value is null
458+
var contPath = parts.slice(0, pLength - 1).join('.'),
459+
cont = Lib.nestedProperty(obj, contPath).get();
460+
cont.splice(pLast, 1);
461+
462+
// Note that nested property clears null / undefined at end of
463+
// array container, but not within them.
464+
}
465+
// create item
466+
else if(pLastIsNumber && np.get() === undefined) {
467+
468+
// When adding a new item, make sure undo command will remove it
469+
if(np.get() === undefined) undoit[np.astr] = null;
470+
471+
np.set(newVal);
472+
}
473+
// update item
474+
else {
475+
476+
// If the last part of attribute string isn't a number,
477+
// np.set is all we need.
478+
np.set(newVal);
479+
}
480+
};

0 commit comments

Comments
 (0)