Skip to content
Draft
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
5ddbc5e
wip
Geliogabalus Feb 19, 2025
724b1a8
wip
Geliogabalus Feb 21, 2025
044af30
layer models
Geliogabalus Feb 23, 2025
af043f2
wip
Geliogabalus Feb 23, 2025
60bbcd9
update
Geliogabalus Feb 25, 2025
5832a22
fix
Geliogabalus Feb 25, 2025
bb09418
up
Geliogabalus Feb 25, 2025
9a9aeac
up
Geliogabalus Feb 26, 2025
6249685
up
Geliogabalus Feb 28, 2025
1a208bc
up
Geliogabalus Mar 2, 2025
309edaa
wip
Geliogabalus Mar 4, 2025
099cf81
up
Geliogabalus Mar 9, 2025
2f2c482
up
Geliogabalus Mar 18, 2025
62ed851
wip
Geliogabalus Mar 18, 2025
903e922
wip
Geliogabalus Mar 18, 2025
b491ea8
fix
Geliogabalus Mar 18, 2025
1533570
up
Geliogabalus Mar 19, 2025
0752076
up
Geliogabalus May 16, 2025
163c221
wip
Geliogabalus May 16, 2025
c66280b
wip
Geliogabalus May 29, 2025
c2dc6ae
wip
Geliogabalus Jun 5, 2025
0a2f047
up
Geliogabalus Jun 5, 2025
8a03a89
up
Geliogabalus Jun 6, 2025
d058bac
reset fix
Geliogabalus Jun 6, 2025
468a6c6
wip
Geliogabalus Jun 9, 2025
5083b69
up
Geliogabalus Jun 9, 2025
dab0da8
update
Geliogabalus Jun 16, 2025
58be262
up
Geliogabalus Jun 16, 2025
15ec945
tests start
Geliogabalus Jun 17, 2025
e4786f2
fixes and tests
Geliogabalus Jun 18, 2025
1487770
update
Geliogabalus Jun 29, 2025
bf9a4c8
wip
Geliogabalus Jul 10, 2025
7864d01
update
Geliogabalus Jul 11, 2025
4d8ed05
up
Geliogabalus Jul 11, 2025
4152946
refactor
Geliogabalus Jul 14, 2025
2c6c5d9
up
Geliogabalus Jul 14, 2025
29ca501
revert
Geliogabalus Jul 14, 2025
ec9a33a
up
Geliogabalus Jul 14, 2025
505fc50
up
Geliogabalus Jul 15, 2025
6eb29f1
wip
Geliogabalus Jul 16, 2025
a94376e
async removing
Geliogabalus Jul 17, 2025
ed79f8e
wip (probably doesn't work)
Geliogabalus Jul 21, 2025
8358fa8
up
Geliogabalus Jul 23, 2025
5e4b02b
up
Geliogabalus Jul 23, 2025
8328efe
wip
Geliogabalus Jul 23, 2025
34812b6
up
Geliogabalus Jul 24, 2025
d861c35
fix
Geliogabalus Jul 24, 2025
3c6aa1d
fix
Geliogabalus Jul 25, 2025
a852825
final
Geliogabalus Jul 25, 2025
8782e66
typings
Geliogabalus Jul 26, 2025
b08e3ec
up
Geliogabalus Jul 28, 2025
49f8a85
up
Geliogabalus Jul 28, 2025
c32030d
wip
Geliogabalus Jul 29, 2025
57f18b0
update
Geliogabalus Jul 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions packages/joint-core/src/dia/Cell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export const Cell = Model.extend({
},

toFront: function(opt) {
var graph = this.graph;
const { graph } = this;
if (graph) {
opt = defaults(opt || {}, { foregroundEmbeds: true });

Expand All @@ -261,12 +261,14 @@ export const Cell = Model.extend({

const sortedCells = opt.foregroundEmbeds ? cells : sortBy(cells, cell => cell.z());

const maxZ = graph.maxZIndex();
const layerName = this.layer();

const maxZ = graph.maxZIndex(layerName);
let z = maxZ - cells.length + 1;

const collection = graph.get('cells');
const layerCells = graph.getLayerCells(layerName);

let shouldUpdate = (collection.toArray().indexOf(sortedCells[0]) !== (collection.length - cells.length));
let shouldUpdate = (layerCells.indexOf(sortedCells[0]) !== (layerCells.length - cells.length));
if (!shouldUpdate) {
shouldUpdate = sortedCells.some(function(cell, index) {
return cell.z() !== z + index;
Expand All @@ -290,7 +292,7 @@ export const Cell = Model.extend({
},

toBack: function(opt) {
var graph = this.graph;
const { graph } = this;
if (graph) {
opt = defaults(opt || {}, { foregroundEmbeds: true });

Expand All @@ -304,11 +306,13 @@ export const Cell = Model.extend({

const sortedCells = opt.foregroundEmbeds ? cells : sortBy(cells, cell => cell.z());

let z = graph.minZIndex();
const layerName = this.layer();

let z = graph.minZIndex(layerName);

var collection = graph.get('cells');
const layerCells = graph.getLayerCells(layerName);

let shouldUpdate = (collection.toArray().indexOf(sortedCells[0]) !== 0);
let shouldUpdate = (layerCells.indexOf(sortedCells[0]) !== 0);
if (!shouldUpdate) {
shouldUpdate = sortedCells.some(function(cell, index) {
return cell.z() !== z + index;
Expand Down Expand Up @@ -942,6 +946,31 @@ export const Cell = Model.extend({
.getPointRotatedAroundCenter(this.angle(), x, y)
// Transform the absolute position into relative
.difference(this.position());
},

layer: function(layerName, opt) {
// if strictly null unset the layer
if (layerName === null) {
return this.unset('layer', opt);
}

// if undefined return the current layer name
if (layerName === undefined) {
let layer = this.get('layer') || null;
// If the cell is part of a graph, use the graph's default layer.
if (layer == null && this.graph) {
layer = this.graph.getDefaultLayer().name;
}

return layer;
}

// otherwise set the layer name
if (!isString(layerName)) {
throw new Error('Layer name must be a string.');
}

return this.set('layer', layerName, opt);
}

}, {
Expand Down
111 changes: 65 additions & 46 deletions packages/joint-core/src/dia/Graph.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as util from '../util/index.mjs';
import * as g from '../g/index.mjs';

import { GraphLayer } from './layers/GraphLayer.mjs';
import { Model } from '../mvc/Model.mjs';
import { Collection } from '../mvc/Collection.mjs';
import { wrappers, wrapWith } from '../util/wrappers.mjs';
import { cloneCells } from '../util/index.mjs';
import { GraphLayersController } from './controllers/GraphLayersController.mjs';

const GraphCells = Collection.extend({

Expand All @@ -19,7 +21,6 @@ const GraphCells = Collection.extend({
/* eslint-enable no-undef */
}


this.graph = opt.graph;
},

Expand Down Expand Up @@ -55,21 +56,24 @@ const GraphCells = Collection.extend({
model.graph = null;
}
},

// `comparator` makes it easy to sort cells based on their `z` index.
comparator: function(model) {

return model.get('z') || 0;
}
});


export const Graph = Model.extend({

defaultLayerName: 'cells',

initialize: function(attrs, opt) {

opt = opt || {};

const defaultLayer = new GraphLayer({
name: this.defaultLayerName
});

this.layersController = new GraphLayersController({ graph: this });
this.layersController.addLayer(defaultLayer);

// Passing `cellModel` function in the options object to graph allows for
// setting models based on attribute objects. This is especially handy
// when processing JSON graphs that are in a different than JointJS format.
Expand All @@ -84,10 +88,6 @@ export const Graph = Model.extend({
// to the outside world.
cells.on('all', this.trigger, this);

// JointJS automatically doesn't trigger re-sort if models attributes are changed later when
// they're already in the collection. Therefore, we're triggering sort manually here.
this.on('change:z', this._sortOnChangeZ, this);

// `joint.dia.Graph` keeps an internal data structure (an adjacency list)
// for fast graph queries. All changes that affect the structure of the graph
// must be reflected in the `al` object. This object provides fast answers to
Expand Down Expand Up @@ -120,11 +120,6 @@ export const Graph = Model.extend({
cells.on('remove', this._removeCell, this);
},

_sortOnChangeZ: function() {

this.get('cells').sort();
},

_restructureOnAdd: function(cell) {

if (cell.isLink()) {
Expand Down Expand Up @@ -157,10 +152,10 @@ export const Graph = Model.extend({
}
},

_restructureOnReset: function(cells) {
_restructureOnReset: function(collection) {

// Normalize into an array of cells. The original `cells` is GraphCells mvc collection.
cells = cells.models;
// Normalize into an array of cells. The original `collection` is GraphCells mvc collection.
const cells = collection.models;

this._out = {};
this._in = {};
Expand Down Expand Up @@ -295,16 +290,12 @@ export const Graph = Model.extend({
return cell;
},

minZIndex: function() {

var firstCell = this.get('cells').first();
return firstCell ? (firstCell.get('z') || 0) : 0;
minZIndex: function(layerName) {
return this.layersController.minZIndex(layerName);
},

maxZIndex: function() {

var lastCell = this.get('cells').last();
return lastCell ? (lastCell.get('z') || 0) : 0;
maxZIndex: function(layerName) {
return this.layersController.maxZIndex(layerName);
},

addCell: function(cell, opt) {
Expand All @@ -314,17 +305,6 @@ export const Graph = Model.extend({
return this.addCells(cell, opt);
}

if (cell instanceof Model) {

if (!cell.has('z')) {
cell.set('z', this.maxZIndex() + 1);
}

} else if (cell.z === undefined) {

cell.z = this.maxZIndex() + 1;
}

this.get('cells').add(this._prepareCell(cell, opt), opt || {});

return this;
Expand All @@ -338,10 +318,10 @@ export const Graph = Model.extend({
opt.maxPosition = opt.position = cells.length - 1;

this.startBatch('add', opt);
cells.forEach(function(cell) {
cells.forEach((cell) => {
this.addCell(cell, opt);
opt.position--;
}, this);
});
this.stopBatch('add', opt);

return this;
Expand All @@ -352,11 +332,16 @@ export const Graph = Model.extend({
// Useful for bulk operations and optimizations.
resetCells: function(cells, opt) {

this.startBatch('reset', opt);

var preparedCells = util.toArray(cells).map(function(cell) {
return this._prepareCell(cell, opt);
}, this);

this.get('cells').reset(preparedCells, opt);

this.stopBatch('reset', opt);

return this;
},

Expand Down Expand Up @@ -429,35 +414,69 @@ export const Graph = Model.extend({
this.stopBatch(batchName);
},

addLayer(layer, opt) {
this.layersController.addLayer(layer, opt);
},

removeLayer(layer, opt) {
this.layersController.removeLayer(layer.name, opt);
},

getDefaultLayer() {
return this.layersController.getDefaultLayer();
},

getLayer(name) {
return this.layersController.getLayer(name);
},

hasLayer(name) {
return this.layersController.hasLayer(name);
},

getLayers() {
return this.layersController.getLayers();
},

getLayerCells(layerName) {
return this.layersController.getLayerCells(layerName);
},

// Get a cell by `id`.
getCell: function(id) {

return this.get('cells').get(id);
},

getCells: function() {

return this.get('cells').toArray();
// Preserve old order without layers
return this.layersController.getCells();
},

getElements: function() {

return this.get('cells').toArray().filter(cell => cell.isElement());
return this.getCells().filter(cell => cell.isElement());
},

getLinks: function() {

return this.get('cells').toArray().filter(cell => cell.isLink());
return this.getCells().filter(cell => cell.isLink());
},

// @deprecated
getFirstCell: function() {

return this.get('cells').first();
const cells = this.getCells();

return cells[0];
},

// @deprecated
getLastCell: function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should accept layerName now and not be deprecated


return this.get('cells').last();
const cells = this.getCells();

return cells[cells.length - 1];
},

// Get all inbound and outbound links connected to the cell `model`.
Expand Down
2 changes: 1 addition & 1 deletion packages/joint-core/src/dia/HighlighterView.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const HighlighterView = mvc.View.extend({
vGroup = V('g').addClass('highlight-transform').append(el);
}
this.transformGroup = vGroup;
paper.getLayerView(layerName).insertSortedNode(vGroup.node, options.z);
paper.getLayer(layerName).insertSortedNode(vGroup.node, options.z);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paper.geLayer() should stay paper.getLayerView() (same analogy to graph.findCellsInArea() and paper.findCellViewsInArea()

} else {
// TODO: prepend vs append
if (!el.parentNode || el.nextSibling) {
Expand Down
2 changes: 1 addition & 1 deletion packages/joint-core/src/dia/LinkView.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export const LinkView = CellView.extend({
if (!vLabels || !model.hasLabels()) return;
const { node } = vLabels;
if (options.labelsLayer) {
paper.getLayerView(options.labelsLayer).insertSortedNode(node, model.get('z'));
paper.getLayer(options.labelsLayer).insertSortedNode(node, model.get('z'));
} else {
if (node.parentNode !== el) {
el.appendChild(node);
Expand Down
Loading