Skip to content

Commit d9c342f

Browse files
author
Jon M. Mease
committed
Completed commenting. Added some TODOs to investigate replacing some object operations with lodash functions
1 parent c7fb04f commit d9c342f

1 file changed

Lines changed: 116 additions & 85 deletions

File tree

js/src/Figure.js

Lines changed: 116 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,9 @@ var FigureView = widgets.DOMWidgetView.extend({
11031103
}
11041104
},
11051105

1106+
/**
1107+
* Handle Plotly.deleteTraces request
1108+
*/
11061109
do_deleteTraces: function () {
11071110

11081111
/** @type {Py2JsDeleteTracesMsg} */
@@ -1123,6 +1126,9 @@ var FigureView = widgets.DOMWidgetView.extend({
11231126
}
11241127
},
11251128

1129+
/**
1130+
* Handle Plotly.moveTraces request
1131+
*/
11261132
do_moveTraces: function () {
11271133

11281134
/** @type {Py2JsMoveTracesMsg} */
@@ -1144,6 +1150,9 @@ var FigureView = widgets.DOMWidgetView.extend({
11441150
}
11451151
},
11461152

1153+
/**
1154+
* Handle Plotly.restyle request
1155+
*/
11471156
do_restyle: function () {
11481157
console.log('do_restyle');
11491158

@@ -1176,6 +1185,9 @@ var FigureView = widgets.DOMWidgetView.extend({
11761185
}
11771186
},
11781187

1188+
/**
1189+
* Handle Plotly.relayout request
1190+
*/
11791191
do_relayout: function () {
11801192
console.log('FigureView: do_relayout');
11811193

@@ -1202,6 +1214,9 @@ var FigureView = widgets.DOMWidgetView.extend({
12021214
}
12031215
},
12041216

1217+
/**
1218+
* Handle Plotly.update request
1219+
*/
12051220
do_update: function () {
12061221
console.log('FigureView: do_update');
12071222

@@ -1235,6 +1250,9 @@ var FigureView = widgets.DOMWidgetView.extend({
12351250
}
12361251
},
12371252

1253+
/**
1254+
* Handle Plotly.animate request
1255+
*/
12381256
do_animate: function() {
12391257
console.log('FigureView: do_animate');
12401258

@@ -1282,6 +1300,9 @@ var FigureView = widgets.DOMWidgetView.extend({
12821300
}
12831301
},
12841302

1303+
/**
1304+
* Handle svg image request
1305+
*/
12851306
do_svgRequest: function() {
12861307
console.log('FigureView: do_svgRequest');
12871308

@@ -1291,7 +1312,6 @@ var FigureView = widgets.DOMWidgetView.extend({
12911312
if (msgData !== null) {
12921313
var req_id = msgData.request_id;
12931314
Plotly.toImage(this.el, {format:'svg'}).then(function (svg_uri) {
1294-
console.log([msgData, svg_uri]);
12951315

12961316
/** @type {Js2PySvgResponseMsg} */
12971317
var responseMsg = {
@@ -1356,90 +1376,6 @@ var FigureView = widgets.DOMWidgetView.extend({
13561376
this.model.set('_js2py_traceDeltas', traceDeltasMsg);
13571377
this.touch();
13581378
},
1359-
1360-
/**
1361-
* Return object that contains all properties in fullObj that are not
1362-
* identical to corresponding properties in removeObj
1363-
*
1364-
* Properties of fullObj and removeObj may be object arrays
1365-
*
1366-
* Returned object is a deep clone of the properties of the input objects
1367-
*
1368-
* @param {Object} fullObj
1369-
* @param {Object} removeObj
1370-
*/
1371-
createDeltaObject: function (fullObj, removeObj) {
1372-
1373-
// Initialize result as object or array
1374-
var res;
1375-
if(Array.isArray(fullObj)) {
1376-
res = new Array(fullObj.length);
1377-
} else {
1378-
res = {};
1379-
}
1380-
1381-
// Initialize removeObj to empty object if not specified
1382-
if (removeObj === null || removeObj === undefined) {
1383-
removeObj = {};
1384-
}
1385-
1386-
// Iterate over object properties or array indices
1387-
for (var p in fullObj) {
1388-
if (p[0] !== '_' && // Don't consider private properties
1389-
fullObj.hasOwnProperty(p) && // Exclude parent properties
1390-
fullObj[p] !== null // Exclude cases where fullObj doesn't
1391-
// have the property
1392-
) {
1393-
// Compute object equality
1394-
var props_equal;
1395-
props_equal = _.isEqual(removeObj[p], fullObj[p]);
1396-
1397-
// Perform recursive comparison if props are not equal
1398-
if (!props_equal || p === 'uid') { // Let uids through
1399-
1400-
// property has non-null value in fullObj that doesn't
1401-
// match the value in removeObj
1402-
var fullVal = fullObj[p];
1403-
if (removeObj.hasOwnProperty(p) && typeof fullVal === 'object') {
1404-
// Recurse over object properties
1405-
if(Array.isArray(fullVal)) {
1406-
1407-
if (fullVal.length > 0 && typeof(fullVal[0]) === 'object') {
1408-
// We have an object array
1409-
res[p] = new Array(fullVal.length);
1410-
for (var i = 0; i < fullVal.length; i++) {
1411-
if (!Array.isArray(removeObj[p]) || removeObj[p].length <= i) {
1412-
res[p][i] = fullVal[i]
1413-
} else {
1414-
res[p][i] = this.createDeltaObject(fullVal[i], removeObj[p][i]);
1415-
}
1416-
}
1417-
} else {
1418-
// We have a primitive array or typed array
1419-
res[p] = fullVal;
1420-
}
1421-
} else { // object
1422-
var full_obj = this.createDeltaObject(fullVal, removeObj[p]);
1423-
if (Object.keys(full_obj).length > 0) {
1424-
// new object is not empty
1425-
res[p] = full_obj;
1426-
}
1427-
}
1428-
} else if (typeof fullVal === 'object' && !Array.isArray(fullVal)) {
1429-
// Return 'clone' of fullVal
1430-
// We don't use a standard clone method so that we keep
1431-
// the special case handling of this method
1432-
res[p] = this.createDeltaObject(fullVal, {});
1433-
1434-
} else if (fullVal !== undefined) {
1435-
// No recursion necessary, Just keep value from fullObj
1436-
res[p] = fullVal;
1437-
}
1438-
}
1439-
}
1440-
}
1441-
return res
1442-
}
14431379
});
14441380

14451381
// Serialization
@@ -1637,6 +1573,8 @@ function flattenedKeyToObjectPath(rawKey) {
16371573
* initializing the nested layers if needed. Function returns an object
16381574
* that the last entry in keyPath can index into.
16391575
*
1576+
* TODO: Investigate replacing with lodash's set(With).
1577+
*
16401578
* Examples:
16411579
* valParent = {foo: {bar: [23]}}
16421580
* getOrInitNestedProperty(valParent, ['foo', 'bar']) -> {bar: [23]}
@@ -1850,6 +1788,8 @@ function performMoveTracesLike(parentArray, currentInds, newInds) {
18501788
* is an array of properties names or array indexes that reference a
18511789
* property to be removed
18521790
*
1791+
* TODO: Investigate replacing with lodash's unset(With).
1792+
*
18531793
* Examples:
18541794
*/
18551795
function performRemoveProps(parentObj, keyPaths) {
@@ -1875,6 +1815,97 @@ function performRemoveProps(parentObj, keyPaths) {
18751815
}
18761816
}
18771817

1818+
1819+
/**
1820+
* Return object that contains all properties in fullObj that are not
1821+
* identical to the corresponding properties in removeObj
1822+
*
1823+
* Properties of fullObj and removeObj may be objects or arrays of objects
1824+
*
1825+
* Returned object is a deep clone of the properties of the input objects
1826+
*
1827+
* @param {Object} fullObj
1828+
* @param {Object} removeObj
1829+
*
1830+
* TODO: investigate replacing with lodash's mergeWith and a customizer
1831+
* that nulls out identical properties
1832+
*
1833+
* Examples:
1834+
*
1835+
*/
1836+
function createDeltaObject (fullObj, removeObj) {
1837+
1838+
// Initialize result as object or array
1839+
var res;
1840+
if(Array.isArray(fullObj)) {
1841+
res = new Array(fullObj.length);
1842+
} else {
1843+
res = {};
1844+
}
1845+
1846+
// Initialize removeObj to empty object if not specified
1847+
if (removeObj === null || removeObj === undefined) {
1848+
removeObj = {};
1849+
}
1850+
1851+
// Iterate over object properties or array indices
1852+
for (var p in fullObj) {
1853+
if (p[0] !== '_' && // Don't consider private properties
1854+
fullObj.hasOwnProperty(p) && // Exclude parent properties
1855+
fullObj[p] !== null // Exclude cases where fullObj doesn't
1856+
// have the property
1857+
) {
1858+
// Compute object equality
1859+
var props_equal;
1860+
props_equal = _.isEqual(removeObj[p], fullObj[p]);
1861+
1862+
// Perform recursive comparison if props are not equal
1863+
if (!props_equal || p === 'uid') { // Let uids through
1864+
1865+
// property has non-null value in fullObj that doesn't
1866+
// match the value in removeObj
1867+
var fullVal = fullObj[p];
1868+
if (removeObj.hasOwnProperty(p) && typeof fullVal === 'object') {
1869+
// Recurse over object properties
1870+
if(Array.isArray(fullVal)) {
1871+
1872+
if (fullVal.length > 0 && typeof(fullVal[0]) === 'object') {
1873+
// We have an object array
1874+
res[p] = new Array(fullVal.length);
1875+
for (var i = 0; i < fullVal.length; i++) {
1876+
if (!Array.isArray(removeObj[p]) || removeObj[p].length <= i) {
1877+
res[p][i] = fullVal[i]
1878+
} else {
1879+
res[p][i] = createDeltaObject(fullVal[i], removeObj[p][i]);
1880+
}
1881+
}
1882+
} else {
1883+
// We have a primitive array or typed array
1884+
res[p] = fullVal;
1885+
}
1886+
} else { // object
1887+
var full_obj = createDeltaObject(fullVal, removeObj[p]);
1888+
if (Object.keys(full_obj).length > 0) {
1889+
// new object is not empty
1890+
res[p] = full_obj;
1891+
}
1892+
}
1893+
} else if (typeof fullVal === 'object' && !Array.isArray(fullVal)) {
1894+
// Return 'clone' of fullVal
1895+
// We don't use a standard clone method so that we keep
1896+
// the special case handling of this method
1897+
res[p] = createDeltaObject(fullVal, {});
1898+
1899+
} else if (fullVal !== undefined) {
1900+
// No recursion necessary, Just keep value from fullObj
1901+
res[p] = fullVal;
1902+
}
1903+
}
1904+
}
1905+
}
1906+
return res
1907+
}
1908+
18781909
module.exports = {
18791910
FigureView : FigureView,
18801911
FigureModel: FigureModel,

0 commit comments

Comments
 (0)