Skip to content

Commit

Permalink
Merge pull request #74 from ba-st/73-Allow-to-configure-a-post-select…
Browse files Browse the repository at this point in the history
…ion-action-in-BootstrapTypeahedCommand

Add a post processing callback to the selection function.
  • Loading branch information
gcotelli committed Jan 25, 2019
2 parents 8795ad5 + 810f6dc commit d841abf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,25 @@ BootstrapTypeaheadCommandTest >> testEvaluateWithUsing [
assert: (lastResult at: 'name') equals: 'ab';
assert: (lastResult at: 'index') equals: 2
]

{ #category : #tests }
BootstrapTypeaheadCommandTest >> testRefreshingAnotherView [

| html identifiedView |

identifiedView := IdentifiedWebView forDivNamed: 'to-refresh' containing: 'Hi!'.

html := self
apply:
( BootstrapTypeaheadCommand
searchingWith: [ :searchTerm | #(1 2 3 4) ]
withSelectionDo: [ :selectedNumber | ]
thenRefresh: identifiedView
configuredBy: [ :typeahead | typeahead minLength: 5 ] )
toComponentDefinedBy: [ :canvas | canvas textInput ].

self
assert: html
equals:
'<input id="id7" type="text"/><script type="text/javascript">$("#id7").typeahead({"source":function(searchTerm,response){$.getJSON("/",["1","2="+encodeURIComponent(searchTerm)].join("&"),function(data,status){response(data)})},"updater":function(item){return item},"afterSelect":function(activeItem){$.ajax({"url":"/","data":["3","4="+encodeURIComponent(activeItem.index)].join("&"),"dataType":"script","complete":function(){$("#to-refresh-id5").load("/","6")}})},"minLength":5});</script>'
]
52 changes: 45 additions & 7 deletions source/Willow-Bootstrap/BootstrapTypeaheadCommand.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ Class {
#category : #'Willow-Bootstrap-Frontend'
}

{ #category : #private }
BootstrapTypeaheadCommand class >> actionToRefresh: anIdentifiedWebView [

^ [ :ajax :canvas |
anIdentifiedWebView identifyIn: canvas.
ajax
onComplete: ( ( canvas jQuery id: anIdentifiedWebView identifier ) load html: anIdentifiedWebView view )
]
]

{ #category : #private }
BootstrapTypeaheadCommand class >> defaultConfigurationBlock [

Expand All @@ -36,10 +46,22 @@ BootstrapTypeaheadCommand class >> searchingWith: aSearchAction labeledBy: aLabe
{ #category : #'Instance Creation' }
BootstrapTypeaheadCommand class >> searchingWith: aSearchAction labeledBy: aLabelsBlock withSelectionDo: aSelectionCallback configuredBy: aConfigurationBlock [

^ self
searchingWith: aSearchAction
labeledBy: aLabelsBlock
withSelectionDo: aSelectionCallback
thenDo: [ :ajax :canvas | ]
configuredBy: aConfigurationBlock
]

{ #category : #'Instance Creation' }
BootstrapTypeaheadCommand class >> searchingWith: aSearchAction labeledBy: aLabelsBlock withSelectionDo: aSelectionCallback thenDo: aPostProcessingCallback configuredBy: aConfigurationBlock [

^ self new
initializeSearchingWith: aSearchAction
labeledBy: aLabelsBlock
withSelectionDo: aSelectionCallback
thenDo: aPostProcessingCallback
configuredBy: aConfigurationBlock
]

Expand All @@ -59,16 +81,31 @@ BootstrapTypeaheadCommand class >> searchingWith: aSearchAction withSelectionDo:
configuredBy: aConfigurationBlock
]

{ #category : #'Instance Creation' }
BootstrapTypeaheadCommand class >> searchingWith: aSearchAction withSelectionDo: aSelectionCallback thenRefresh: anIdentifiedWebView configuredBy: aConfigurationBlock [

^ self
searchingWith: aSearchAction
labeledBy: self defaultLabelBlock
withSelectionDo: aSelectionCallback
thenDo: ( self actionToRefresh: anIdentifiedWebView )
configuredBy: aConfigurationBlock
]

{ #category : #private }
BootstrapTypeaheadCommand >> afterSelectFunctionEvaluate: aSelectionCallback on: aCanvas [
BootstrapTypeaheadCommand >> afterSelectFunctionEvaluate: aSelectionCallback thenDo: aPostProcessingCallback on: aCanvas [

| selectedIndexHolder ajax |

selectedIndexHolder := WAValueHolder new.
ajax := aCanvas jQuery ajax.
ajax
callback: [ :activeItemIndex | selectedIndexHolder contents: activeItemIndex greaseInteger ] value: ((aCanvas jQuery alias: 'activeItem') access: self indexProperty);
script: [ :script | aSelectionCallback value: (searchResults at: selectedIndexHolder contents) ].
callback: [ :activeItemIndex | selectedIndexHolder contents: activeItemIndex greaseInteger ]
value: ( ( aCanvas jQuery alias: 'activeItem' ) access: self indexProperty );
script: [ :script | aSelectionCallback value: ( searchResults at: selectedIndexHolder contents ) ].

aPostProcessingCallback value: ajax value: aCanvas.

^ ajax asFunction: #('activeItem')
]

Expand Down Expand Up @@ -104,7 +141,7 @@ BootstrapTypeaheadCommand >> indexProperty [
]

{ #category : #initialization }
BootstrapTypeaheadCommand >> initializeSearchingWith: aSearchAction labeledBy: aLabelsBlock withSelectionDo: aSelectionCallback configuredBy: aConfigurationBlock [
BootstrapTypeaheadCommand >> initializeSearchingWith: aSearchAction labeledBy: aLabelsBlock withSelectionDo: aSelectionCallback thenDo: aPostProcessingCallback configuredBy: aConfigurationBlock [

searchResults := #().
scriptCommand := ComponentScriptCommand
Expand All @@ -113,11 +150,12 @@ BootstrapTypeaheadCommand >> initializeSearchingWith: aSearchAction labeledBy: a

typeahead := canvas jQuery this bootstrapTypeahead.
typeahead
source: (self searchWith: aSearchAction labels: aLabelsBlock on: canvas);
source: ( self searchWith: aSearchAction labels: aLabelsBlock on: canvas );
updater: self updaterFunction;
afterSelect: (self afterSelectFunctionEvaluate: aSelectionCallback on: canvas).
afterSelect: ( self afterSelectFunctionEvaluate: aSelectionCallback thenDo: aPostProcessingCallback on: canvas ).
aConfigurationBlock cull: typeahead cull: canvas.
typeahead ]
typeahead
]
]

{ #category : #private }
Expand Down

0 comments on commit d841abf

Please sign in to comment.