-
Notifications
You must be signed in to change notification settings - Fork 868
feat(joint-core/demo/container): render collapse button via highlighter #2972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zbynekstara
wants to merge
1
commit into
clientIO:master
Choose a base branch
from
zbynekstara:improve-container-demo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,8 @@ body { | |
margin: 0; | ||
overflow-y: hidden; | ||
} | ||
|
||
#paper .joint-paper { | ||
border: 1px solid #E2E2E2; | ||
margin: 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,94 +1,259 @@ | ||||||
(function(joint) { | ||||||
|
||||||
var graph = new joint.dia.Graph({}, { cellNamespace: joint.shapes }); | ||||||
var paper = new joint.dia.Paper({ | ||||||
el: document.getElementById('paper'), | ||||||
const HEADER_HEIGHT = 30; | ||||||
|
||||||
const cellNamespace = { | ||||||
// includes additional defined shapes from `./joint.shapes.container.js` | ||||||
...joint.shapes | ||||||
}; | ||||||
|
||||||
const graph = new joint.dia.Graph({}, { cellNamespace }); | ||||||
const paper = new joint.dia.Paper({ | ||||||
width: 800, | ||||||
height: 600, | ||||||
model: graph, | ||||||
cellViewNamespace: joint.shapes, | ||||||
cellViewNamespace: cellNamespace, | ||||||
async: true, | ||||||
background: { | ||||||
color: '#F3F7F6' | ||||||
}, | ||||||
interactive: { linkMove: false }, | ||||||
viewport: function(view) { | ||||||
var element = view.model; | ||||||
const cell = view.model; | ||||||
// Hide any element or link which is embedded inside a collapsed parent (or parent of the parent). | ||||||
var hidden = element.getAncestors().some(function(ancestor) { | ||||||
// `isCollapsed()` method is defined at `joint.shapes.container.Parent` in `./joint.shapes.container.js` | ||||||
return ancestor.isCollapsed(); | ||||||
const hidden = cell.getAncestors().some(function(ancestor) { | ||||||
if (ancestor.isCollapsed()) return true; | ||||||
if (cell.isElement()) { | ||||||
return ancestor.isChildFiltered(cell); | ||||||
} else { | ||||||
return ( | ||||||
ancestor.isChildFiltered(cell.getSourceCell()) || | ||||||
ancestor.isChildFiltered(cell.getTargetCell()) | ||||||
); | ||||||
} | ||||||
}); | ||||||
return !hidden; | ||||||
} | ||||||
}); | ||||||
|
||||||
paper.el.style.border = '1px solid #E2E2E2'; | ||||||
document.getElementById('paper').appendChild(paper.el); | ||||||
|
||||||
// Custom highlighter to render the expand/collapse button. | ||||||
const ExpandButtonHighlighter = joint.dia.HighlighterView.extend({ | ||||||
tagName: 'g', | ||||||
|
||||||
UPDATE_ATTRIBUTES: ['collapsed'], | ||||||
|
||||||
children: function() { | ||||||
return [ | ||||||
{ | ||||||
tagName: 'rect', | ||||||
selector: 'button', | ||||||
attributes: { | ||||||
fill: '#000000', | ||||||
fillOpacity: 0.2, | ||||||
stroke: '#FFFFFF', | ||||||
strokeWidth: 0.5, | ||||||
x: -7, | ||||||
y: -7, | ||||||
width: 14, | ||||||
height: 14, | ||||||
cursor: 'pointer' | ||||||
} | ||||||
}, | ||||||
{ | ||||||
tagName: 'path', | ||||||
selector: 'icon', | ||||||
attributes: { | ||||||
fill: 'none', | ||||||
stroke: '#FFFFFF', | ||||||
strokeWidth: 1, | ||||||
pointerEvents: 'none' | ||||||
} | ||||||
} | ||||||
]; | ||||||
}, | ||||||
|
||||||
events: { | ||||||
click: 'onClick' | ||||||
}, | ||||||
|
||||||
onClick() { | ||||||
this.cellView.model.toggle(); | ||||||
}, | ||||||
|
||||||
// Method called to highlight a CellView | ||||||
highlight(cellView, _node) { | ||||||
if (this.el.childNodes.length === 0) { | ||||||
this.renderChildren(); | ||||||
} | ||||||
|
||||||
const size = cellView.model.size(); | ||||||
this.el.setAttribute( | ||||||
'transform', | ||||||
`translate(${size.width - HEADER_HEIGHT / 2}, ${HEADER_HEIGHT / 2})` | ||||||
); | ||||||
|
||||||
let d; | ||||||
if (cellView.model.get('collapsed')) { | ||||||
d = 'M -4 0 4 0 M 0 -4 0 4'; | ||||||
} else { | ||||||
d = 'M -4 0 4 0'; | ||||||
} | ||||||
this.childNodes.icon.setAttribute('d', d); | ||||||
} | ||||||
}); | ||||||
|
||||||
graph.on({ | ||||||
'add': function(cell) { | ||||||
if (joint.shapes.container.Parent.isContainer(cell)) { | ||||||
// Add the expand button highlighter. | ||||||
ExpandButtonHighlighter.add(cell.findView(paper), 'root', 'expand-button'); | ||||||
} | ||||||
}, | ||||||
|
||||||
var Container = joint.shapes.container.Parent; | ||||||
var Child = joint.shapes.container.Child; | ||||||
var Link = joint.shapes.container.Link; | ||||||
'remove': function(cell) { | ||||||
if (cell.isLink()) return; | ||||||
updateContainerSize(cell.getParentCell()); | ||||||
}, | ||||||
|
||||||
'change:position': function(cell) { | ||||||
if (cell.isLink()) return; | ||||||
updateContainerSize(cell.getParentCell()); | ||||||
}, | ||||||
|
||||||
var container_a = new Container({ | ||||||
'change:size': function(cell) { | ||||||
if (cell.isLink()) return; | ||||||
updateContainerSize(cell.getParentCell()); | ||||||
}, | ||||||
|
||||||
'change:embeds': function(cell) { | ||||||
if (cell.isLink()) return; | ||||||
updateContainerSize(cell); | ||||||
}, | ||||||
|
||||||
'change:collapsed': function(cell) { | ||||||
updateContainerSize(cell); | ||||||
} | ||||||
}); | ||||||
|
||||||
function updateContainerSize(container) { | ||||||
if (!joint.shapes.container.Parent.isContainer(container)) return; | ||||||
const flags = { ignoreCommandManager: true }; | ||||||
if (container.isCollapsed()) { | ||||||
container.resize(140, 30, flags); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The hard-coded dimensions (140, 30) for the collapsed state are used directly; consider extracting these values into named constants to improve clarity and ease future adjustments.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} else { | ||||||
container.fitToChildElements(flags); | ||||||
} | ||||||
} | ||||||
|
||||||
// Show element tools on hover | ||||||
paper.on({ | ||||||
'element:mouseenter': function(elementView) { | ||||||
elementView.removeTools(); | ||||||
if (joint.shapes.container.Parent.isContainer(elementView.model)) { | ||||||
// Silently remove the children elements, then remove the container | ||||||
elementView.addTools( | ||||||
new joint.dia.ToolsView({ | ||||||
tools: [ | ||||||
new joint.elementTools.Remove({ | ||||||
useModelGeometry: true, | ||||||
y: 0, | ||||||
x: 0, | ||||||
action: function(evt, view) { | ||||||
// The children elements removal should not be added to the command manager. | ||||||
graph.removeCells(view.model.getEmbeddedCells(), { ignoreCommandManager: true }); | ||||||
view.model.remove(); | ||||||
} | ||||||
}) | ||||||
] | ||||||
}) | ||||||
); | ||||||
} else if (joint.shapes.container.Child.isChild(elementView.model)) { | ||||||
// Remove the element from the graph | ||||||
elementView.addTools( | ||||||
new joint.dia.ToolsView({ | ||||||
tools: [ | ||||||
new joint.elementTools.Remove({ | ||||||
useModelGeometry: true, | ||||||
y: 0, | ||||||
x: 0 | ||||||
}) | ||||||
] | ||||||
}) | ||||||
); | ||||||
} | ||||||
}, | ||||||
|
||||||
'element:mouseleave': function(elementView) { | ||||||
elementView.removeTools(); | ||||||
} | ||||||
}); | ||||||
|
||||||
// Example diagram | ||||||
const container_a = new joint.shapes.container.Parent({ | ||||||
z: 1, | ||||||
position: { x: 0, y: 0 }, | ||||||
size: { width: 10, height: 10 }, | ||||||
attrs: { headerText: { text: 'Container A' }} | ||||||
}); | ||||||
|
||||||
var container_b = new Container({ | ||||||
const container_b = new joint.shapes.container.Parent({ | ||||||
z: 3, | ||||||
position: { x: 0, y: 0 }, | ||||||
size: { width: 50, height: 50 }, | ||||||
attrs: { headerText: { text: 'Container B' }} | ||||||
}); | ||||||
|
||||||
var child_1 = new Child({ | ||||||
const child_1 = new joint.shapes.container.Child({ | ||||||
z: 2, | ||||||
position: { x: 250, y: 150 }, | ||||||
position: { x: 150, y: 50 }, | ||||||
attrs: { label: { text: 1 }} | ||||||
}); | ||||||
|
||||||
var child_2 = new Child({ | ||||||
const child_2 = new joint.shapes.container.Child({ | ||||||
z: 2, | ||||||
position: { x: 200, y: 250 }, | ||||||
position: { x: 100, y: 150 }, | ||||||
attrs: { label: { text: 2 }} | ||||||
}); | ||||||
|
||||||
var child_3 = new Child({ | ||||||
const child_3 = new joint.shapes.container.Child({ | ||||||
z: 2, | ||||||
position: { x: 300, y: 250 }, | ||||||
position: { x: 200, y: 150 }, | ||||||
attrs: { label: { text: 3 }} | ||||||
}); | ||||||
|
||||||
var child_4 = new Child({ | ||||||
const child_4 = new joint.shapes.container.Child({ | ||||||
z: 4, | ||||||
position: { x: 400, y: 290 }, | ||||||
position: { x: 300, y: 190 }, | ||||||
attrs: { label: { text: '4' }} | ||||||
}); | ||||||
|
||||||
var child_5 = new Child({ | ||||||
const child_5 = new joint.shapes.container.Child({ | ||||||
z: 4, | ||||||
position: { x: 500, y: 360 }, | ||||||
position: { x: 400, y: 260 }, | ||||||
attrs: { label: { text: '5' }} | ||||||
}); | ||||||
|
||||||
var link_1_2 = new Link({ | ||||||
const link_1_2 = new joint.shapes.container.Link({ | ||||||
z: 2, | ||||||
source: { id: child_1.id }, | ||||||
target: { id: child_2.id } | ||||||
}); | ||||||
|
||||||
var link_1_3 = new Link({ | ||||||
const link_1_3 = new joint.shapes.container.Link({ | ||||||
z: 2, | ||||||
source: { id: child_1.id }, | ||||||
target: { id: child_3.id } | ||||||
}); | ||||||
|
||||||
var link_4_5 = new Link({ | ||||||
const link_4_5 = new joint.shapes.container.Link({ | ||||||
z: 4, | ||||||
source: { id: child_4.id }, | ||||||
target: { id: child_5.id } | ||||||
}); | ||||||
|
||||||
var link_1_b = new Link({ | ||||||
const link_1_b = new joint.shapes.container.Link({ | ||||||
z: 4, | ||||||
source: { id: child_1.id }, | ||||||
target: { id: container_b.id } | ||||||
|
@@ -108,22 +273,4 @@ | |||||
link_4_5.reparent(); | ||||||
link_1_b.reparent(); | ||||||
|
||||||
// `toggle()` method is defined at `joint.shapes.container.Parent` in `./joint.shapes.container.js` | ||||||
container_b.toggle(false); | ||||||
container_a.toggle(false); | ||||||
|
||||||
paper.on('element:button:pointerdown', function(elementView) { | ||||||
var element = elementView.model; | ||||||
// `toggle()` method is defined at `joint.shapes.container.Parent` in `./joint.shapes.container.js` | ||||||
element.toggle(); | ||||||
// `fitAncestorElements()` method is defined at `joint.shapes.container.Base` in `./joint.shapes.container.js` | ||||||
element.fitAncestorElements(); | ||||||
}); | ||||||
|
||||||
paper.on('element:pointermove', function(elementView) { | ||||||
var element = elementView.model; | ||||||
// `fitAncestorElements()` method is defined at `joint.shapes.container.Base` in `./joint.shapes.container.js` | ||||||
element.fitAncestorElements(); | ||||||
}); | ||||||
|
||||||
})(joint); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before directly accessing this.childNodes.icon, it would be safer to check that the icon element exists to prevent potential runtime errors if the rendering process fails.
Copilot uses AI. Check for mistakes.