Skip to content

Instantly share code, notes, and snippets.

@BenjaminVanRyseghem
Last active December 18, 2016 17:15
Show Gist options
  • Save BenjaminVanRyseghem/9974654 to your computer and use it in GitHub Desktop.
Save BenjaminVanRyseghem/9974654 to your computer and use it in GitHub Desktop.
Drag and Drop Spec Tutorial
collection1 := #(1 2 3 4 5) asOrderedCollection.
collection2 := #(a b c d e) asOrderedCollection.
list1 := ListModel new
items: collection1;
yourself.
list2 := ListModel new
items: collection2;
yourself.
list1
dragEnabled: true; "activates the drag from this model"
dropEnabled: true; "activates the drop to this model"
dragTransformationBlock: [ :e | e asInteger ]; "By default the drag passenger is the object representation. Here we want to ensure to transfer an integer"
wantDropBlock: [ :draggedItem :event :source |
"this block is used to filter the possible objects that could be dropped to this model"
draggedItem isTransferable and: [ draggedItem source = list2 ]
"the first statement is to ensure only dragged items are expected (and no dragged windows),
when the second statement allows dragged items only if they come from `list2`"
];
acceptDropBlock: [ :transfer :event :source :receiver :index |
"this block is performed when a object is effectively dropped"
| sourceList |
"This is used to retreive the model where the dropped object comes from"
sourceList := transfer source. "Note that transfer source = source"
"For each dropped element"
transfer passenger
do: [ :e |
"the element in inserted before the targeted list item"
list1 listItems add: e beforeIndex: index.
"and removed from the source list"
sourceList listItems remove: e ].
"Finally both lists are updated"
list1 updateList.
sourceList updateList ].
list2
dragEnabled: true; "activates the drag from this model"
dropEnabled: true; "activates the drop to this model"
wantDropBlock: [ :draggedItem :event :source |
"this block is used to filter the possible objects that could be dropped to this model"
draggedItem isTransferable and: [ draggedItem source = list1 ]
"the first statement is to ensure only dragged items are expected (and no dragged windows),
when the second statement allows dragged items only if they come from `list1`"
];
acceptDropBlock: [ :transfer :event :source :receiver :index |
"this block is performed when a object is effectively dropped"
| sourceList |
"This is used to retreive the model where the dropped object comes from"
sourceList := transfer source. "Note that transfer source = source"
"For each dropped element"
transfer passenger
do: [ :e |
"the element in inserted before the targeted list item"
list2 listItems add: e beforeIndex: index.
"and removed from the source list"
sourceList listItems remove: e ].
"Finally both lists are updated"
list2 updateList.
sourceList updateList ].
list1 openWithSpec.
list2 openWithSpec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment