-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tree example for exploring smalltalk packages
- Loading branch information
Showing
4 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
Class { | ||
#name : #LWTreeExample, | ||
#superclass : #LWComponent, | ||
#instVars : [ | ||
'root' | ||
], | ||
#category : #'LiveWeb-Examples' | ||
} | ||
|
||
{ #category : #accessing } | ||
LWTreeExample >> children [ | ||
^ ReadStream on: { root } | ||
] | ||
|
||
{ #category : #initialization } | ||
LWTreeExample >> initialize [ | ||
super initialize. | ||
root := LWTreeNode new label: 'Smalltalk packages'; beBranch. | ||
Smalltalk packages sorted do: [ :p| | ||
| node | | ||
node := root. | ||
(p name splitOn: $-) do: [ :part | | ||
node := node branch: part. | ||
]. | ||
p classes do: [ :c | | ||
node leaf: c name | ||
] | ||
] | ||
] | ||
|
||
{ #category : #rendering } | ||
LWTreeExample >> renderOn: h [ | ||
root render: h | ||
] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
Class { | ||
#name : #LWTreeNode, | ||
#superclass : #LWComponent, | ||
#instVars : [ | ||
'label', | ||
'children', | ||
'expanded', | ||
'leaf' | ||
], | ||
#category : #'LiveWeb-Examples' | ||
} | ||
|
||
{ #category : #'as yet unclassified' } | ||
LWTreeNode >> beBranch [ | ||
leaf := false | ||
|
||
] | ||
|
||
{ #category : #'as yet unclassified' } | ||
LWTreeNode >> beLeaf [ | ||
leaf := true | ||
] | ||
|
||
{ #category : #accessing } | ||
LWTreeNode >> branch: childLabel [ | ||
^ (self child: childLabel) beBranch | ||
] | ||
|
||
{ #category : #accessing } | ||
LWTreeNode >> child: childLabel [ | ||
"Add or return child with the given label." | ||
^ children detect: [ :c | c label = childLabel ] | ||
ifNone: [ | ||
| newChild | | ||
newChild := self class new label: childLabel. | ||
children add: newChild. | ||
newChild | ||
] | ||
] | ||
|
||
{ #category : #accessing } | ||
LWTreeNode >> children [ | ||
^ReadStream on: children | ||
|
||
] | ||
|
||
{ #category : #initialization } | ||
LWTreeNode >> initialize [ | ||
super initialize. | ||
children := OrderedCollection new. | ||
expanded := false. | ||
leaf := true. | ||
] | ||
|
||
{ #category : #accessing } | ||
LWTreeNode >> label [ | ||
^ label | ||
|
||
] | ||
|
||
{ #category : #accessing } | ||
LWTreeNode >> label: aLabel [ | ||
label := aLabel | ||
] | ||
|
||
{ #category : #accessing } | ||
LWTreeNode >> leaf: childLabel [ | ||
^ (self child: childLabel) beLeaf | ||
] | ||
|
||
{ #category : #rendering } | ||
LWTreeNode >> renderOn: h [ | ||
h div: { #class -> 'treenode' } with: [ | ||
h div: { #style -> 'display: flex;' } with: [ | ||
leaf | ||
ifTrue: [ h span: '[Class]' ] | ||
ifFalse: [ | ||
h button: { #onclick -> [ self toggleExpanded ] } with: [ | ||
expanded ifTrue: [ h renderContent: '-' ] ifFalse: [ h renderContent: '+' ]] | ||
]. | ||
h span: ' ', label. | ||
]. | ||
expanded ifTrue: [ | ||
h ul: [ | ||
children do: [ :c | h li: [ c render: h ] ] | ||
] | ||
] | ||
] | ||
|
||
|
||
] | ||
|
||
{ #category : #operations } | ||
LWTreeNode >> toggleExpanded [ | ||
expanded := expanded not. | ||
self changed. | ||
|
||
] |