Skip to content

Commit

Permalink
Merge pull request #845 from hernanmd/update_class_search_options
Browse files Browse the repository at this point in the history
Update the Browser search presenter with search modes.
  • Loading branch information
Ducasse authored Sep 24, 2024
2 parents 1c2f77c + 934e121 commit d8b4a70
Showing 1 changed file with 72 additions and 16 deletions.
88 changes: 72 additions & 16 deletions src/NewTools-Core/StBrowserSearchPresenter.class.st
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"
I am a presenter that gets a list of elements and that allows one to select one (or multiple if the #multipleSelection option is enabled).
Provides a searchable list of classes, and options to configure the search mode (substring search (default), regular expression, exact). The search mode can be combined with case-sensitiveness.
Multiple classes can be selected if the #multipleSelection option is enabled.
"
Class {
#name : 'StBrowserSearchPresenter',
#superclass : 'StPresenter',
#instVars : [
'items',
'itemsList',
'searchField'
'searchField',
'searchOptions'
],
#category : 'NewTools-Core-Calypso',
#package : 'NewTools-Core',
Expand Down Expand Up @@ -41,7 +44,9 @@ StBrowserSearchPresenter class >> exampleConfiguringPresenterForMultipleSelectio
StBrowserSearchPresenter class >> open [

<script>
self new open
self new
items: self class environment allClasses;
open
]

{ #category : 'instance creation' }
Expand Down Expand Up @@ -69,7 +74,9 @@ StBrowserSearchPresenter >> connectPresenters [

super connectPresenters.

searchField whenTextChangedDo: [ self updateList ]
searchField whenTextChangedDo: [ self updateList ].

searchOptions substringBox click.
]

{ #category : 'layout' }
Expand All @@ -79,6 +86,7 @@ StBrowserSearchPresenter >> defaultLayout [
spacing: 5;
add: itemsList;
add: searchField expand: false;
add: searchOptions expand: false;
yourself
]

Expand All @@ -99,11 +107,32 @@ StBrowserSearchPresenter >> initializePresenters [

itemsList display: [ :entity | entity name ].

self
initializeSearchField;
initializeSearchOptions.
]

{ #category : 'initialization' }
StBrowserSearchPresenter >> initializeSearchField [

searchField placeholder: 'Filter...'.
searchField eventHandler whenKeyDownDo: [ :anEvent | "If we press arrow up, we should get up in the list. If we press arrow down, we should get down in the list.""31 = Arrow down"
anEvent keyValue = 31 ifTrue: [ itemsList selectIndex: (itemsList selection selectedIndex + 1 min: itemsList items size) scrollToSelection: true ].
"30 = Arrow up"
anEvent keyValue = 30 ifTrue: [ itemsList selectIndex: (itemsList selection selectedIndex - 1 max: 1) scrollToSelection: true ] ]
searchField eventHandler
whenKeyDownDo: [ :anEvent |
"If we press arrow up, we should get up in the list. If we press arrow down, we should get down in the list."

"31 = Arrow down"
anEvent keyValue = 31
ifTrue: [ itemsList selectIndex: (itemsList selection selectedIndex + 1 min: itemsList items size) scrollToSelection: true ].

"30 = Arrow up"
anEvent keyValue = 30
ifTrue: [ itemsList selectIndex: (itemsList selection selectedIndex - 1 max: 1) scrollToSelection: true ] ]
]

{ #category : 'initialization' }
StBrowserSearchPresenter >> initializeSearchOptions [

searchOptions := self instantiate: SpSearchInputFieldOptionsPresenter.
]

{ #category : 'initialization' }
Expand Down Expand Up @@ -133,6 +162,23 @@ StBrowserSearchPresenter >> multipleSelection [
itemsList beMultipleSelection
]

{ #category : 'updating' }
StBrowserSearchPresenter >> search: text [
"Main search callback method. Answer a <Collection> of results"

^ OrderedCollection streamContents: [ :stream |
items do: [ :class |
(self selectBlock value: class name value: text)
ifTrue: [ stream nextPut: class ] ] ]
]

{ #category : 'updating' }
StBrowserSearchPresenter >> searchOptions [
"Answer the receiver's search options presenter"

^ searchOptions
]

{ #category : 'api' }
StBrowserSearchPresenter >> searchValue: aString [

Expand All @@ -145,6 +191,13 @@ StBrowserSearchPresenter >> searchWithItem: anItem [
self searchValue: (itemsList display value: anItem)
]

{ #category : 'updating' }
StBrowserSearchPresenter >> selectBlock [
"Answer a <Closure> used to search each item in the receiver"

^ searchOptions selectBlock
]

{ #category : 'accessing' }
StBrowserSearchPresenter >> selectSearchPattern [

Expand All @@ -166,14 +219,17 @@ StBrowserSearchPresenter >> selectedItems [
{ #category : 'updating' }
StBrowserSearchPresenter >> updateList [

| newItems |
newItems := searchField text ifEmpty: [ items ] ifNotEmpty: [ :text |
| regex |
regex := text asRegexIgnoringCase.
items select: [ :item | regex search: (itemsList display value: item) ] ].
| newItems env |

env := self class environment.
newItems := searchField text
ifEmpty: [ items ]
ifNotEmpty: [ :text | self search: text ].

itemsList items = newItems ifFalse: [
itemsList
items: newItems;
selectFirst ]
itemsList items: newItems ].

(env hasClassNamed: searchField text)
ifTrue: [ itemsList selectItem: (env at: searchField text asSymbol) scrollToSelection: true ]
ifFalse: [ itemsList selectFirst ]
]

0 comments on commit d8b4a70

Please sign in to comment.