Skip to content

Commit

Permalink
cy.data() #2407
Browse files Browse the repository at this point in the history
- Add impl.
- Add docs.
- Add basic tests.
  • Loading branch information
maxkfranz committed May 23, 2019
1 parent 71ef65d commit 812a17e
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 20 deletions.
108 changes: 108 additions & 0 deletions documentation/docmaker.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,63 @@
{
"name": "cy.destroyed",
"descr": "Get whether the instance of Cytoscape.js has been destroyed or not."
}
]
},

{
"name": "Data",

"fns": [
{
"name": "cy.data",
"pureAliases": [ "cy.attr" ],
"descr": "Read and write developer-defined data associated with the graph.",
"formatsSameFn": true,
"formats": [
{
"name": "cy.data",
"descr": "Get the entire data object."
},

{
"name": "cy.data",
"descr": "Get a particular data field.",
"args": [ { "name": "name", "descr": "The name of the field to get." } ]
},

{
"name": "cy.data",
"descr": "Set a particular data field.",
"args": [
{ "name": "name", "descr": "The name of the field to set." },
{ "name": "value", "descr": "The value to set for the field." }
]
},

{
"name": "cy.data",
"descr": "Update multiple data fields at once via an object.",
"args": [
{ "name": "obj", "descr": "The object containing name-value pairs to update data fields." }
]
}
]
},

{
"name": "cy.removeData",
"pureAliases": [ "cy.removeAttr" ],
"descr": "Remove developer-defined data associated with the elements.",
"formats": [
{ "descr": "Removes all mutable data fields for the elements." },
{
"descr": "Removes the specified mutable data fields for the elements.",
"args": [
{ "name": "names", "descr": "A space-separated list of fields to delete." }
]
}
]
},

{
Expand Down Expand Up @@ -861,6 +918,57 @@
"descr": "Remove all queued animations for the viewport."
}
]
},{
"name": "eles.data",
"pureAliases": [ "eles.attr" ],
"descr": "Read and write developer-defined data associated with the elements.",
"formatsSameFn": true,
"formats": [
{
"name": "ele.data",
"descr": "Get the entire data object."
},

{
"name": "ele.data",
"descr": "Get a particular data field for the element.",
"args": [ { "name": "name", "descr": "The name of the field to get." } ]
},

{
"name": "ele.data",
"descr": "Set a particular data field for the element.",
"args": [
{ "name": "name", "descr": "The name of the field to set." },
{ "name": "value", "descr": "The value to set for the field." }
]
},

{
"name": "ele.data",
"descr": "Update multiple data fields at once via an object.",
"args": [
{ "name": "obj", "descr": "The object containing name-value pairs to update data fields." }
]
}
],
"md": "collection/data"
},

{
"name": "eles.removeData",
"pureAliases": [ "eles.removeAttr" ],
"descr": "Remove developer-defined data associated with the elements.",
"formats": [
{ "descr": "Removes all mutable data fields for the elements." },
{
"descr": "Removes the specified mutable data fields for the elements.",
"args": [
{ "name": "names", "descr": "A space-separated list of fields to delete." }
]
}
],
"md": "collection/removeData"
},

{
Expand Down
45 changes: 45 additions & 0 deletions src/core/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import define from '../define';

const fn = {
data: define.data( {
field: 'data',
bindingEvent: 'data',
allowBinding: true,
allowSetting: true,
settingEvent: 'data',
settingTriggersEvent: true,
triggerFnName: 'trigger',
allowGetting: true
} ),

removeData: define.removeData( {
field: 'data',
event: 'data',
triggerFnName: 'trigger',
triggerEvent: true
} ),

scratch: define.data( {
field: 'scratch',
bindingEvent: 'scratch',
allowBinding: true,
allowSetting: true,
settingEvent: 'scratch',
settingTriggersEvent: true,
triggerFnName: 'trigger',
allowGetting: true
} ),

removeScratch: define.removeData( {
field: 'scratch',
event: 'scratch',
triggerFnName: 'trigger',
triggerEvent: true
} )
};

// aliases
fn.attr = fn.data;
fn.removeAttr = fn.removeData;

export default fn;
30 changes: 10 additions & 20 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as util from '../util';
import Collection from '../collection';
import * as is from '../is';
import Promise from '../promise';
import define from '../define';

import addRemove from './add-remove';
import animation from './animation';
Expand All @@ -15,6 +14,7 @@ import renderer from './renderer';
import search from './search';
import style from './style';
import viewport from './viewport';
import data from './data';

let Core = function( opts ){
let cy = this;
Expand Down Expand Up @@ -65,6 +65,7 @@ let Core = function( opts ){
elements: new Collection( this ), // elements in the graph
listeners: [], // list of listeners
aniEles: new Collection( this ), // elements being animated
data: {}, // data for the core
scratch: {}, // scratch object for core
layout: null,
renderer: null,
Expand Down Expand Up @@ -407,6 +408,10 @@ util.extend( corefn, {
}
}

if( obj.data ){
cy.data( obj.data );
}

let fields = [
'minZoom', 'maxZoom', 'zoomingEnabled', 'userZoomingEnabled',
'panningEnabled', 'userPanningEnabled',
Expand Down Expand Up @@ -449,6 +454,8 @@ util.extend( corefn, {
json.style = cy.style().json();
}

json.data = util.copy( cy.data() );

let options = _p.options;

json.zoomingEnabled = _p.zoomingEnabled;
Expand All @@ -468,25 +475,7 @@ util.extend( corefn, {

return json;
}
},

scratch: define.data( {
field: 'scratch',
bindingEvent: 'scratch',
allowBinding: true,
allowSetting: true,
settingEvent: 'scratch',
settingTriggersEvent: true,
triggerFnName: 'trigger',
allowGetting: true
} ),

removeScratch: define.removeData( {
field: 'scratch',
event: 'scratch',
triggerFnName: 'trigger',
triggerEvent: true
} )
}

} );

Expand All @@ -503,6 +492,7 @@ corefn.$id = corefn.getElementById;
search,
style,
viewport,
data
].forEach( function( props ){
util.extend( corefn, props );
} );
Expand Down
23 changes: 23 additions & 0 deletions test/core-graph-manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,29 @@ describe('Core graph manipulation', function(){
expect( cy.userPanningEnabled() ).to.equal( true );
});

it('cy.json() gets data', function(){
cy.data({ foo: 'bar' });

var json = cy.json();

expect(json.data.foo).to.equal('bar');
});

it('cy.json() sets data', function(){
cy.json({ data: { foo: 'bar' } });

expect(cy.data('foo')).to.equal('bar');
});
});

describe('cy.data()', function(){ // only basic test for now b/c shared impl w/ eles...
it('sets and gets data', function(){
cy.data({ foo: 'bar' });

expect(cy.data('foo')).to.equal('bar');
});

// TODO more tests in future
});

describe('cy.style()', function(){
Expand Down

0 comments on commit 812a17e

Please sign in to comment.