Control flow in Iliad

Tagged:

Today someone asked me how to do a WATask like widget in Iliad. In seaside WATask is a special presenter. Its purpose is not to build html by itself but to display other seaside components in a sequence.

Iliad doesn't provide such a widget out of the box, but it is very easy create one.

Iliad.ILWidget subclass: Sequence [

    buildContents [
        <category: 'building'>
        self shouldStart ifTrue: [self start].
        ^super buildContents
    ]

    contents [
        <category: 'building'>
        self shouldNotImplement
    ]

    start [
        <category: 'control flow'>
        "The sequencing will happen in this method"
    ]

    shouldStart [
        <category: 'testing'>
        ^decorator decoratee = self
    ]
]

The #start method will be similar to WATask>>go. Let's write a small example to see this widget in action!

Sequence subclass: SequenceExample [
    
    start [
        <category: 'control flow'>
        self 
            show: 
                (Iliad.ILCalendar new 
                    addMessage: [:e | e h1: 'Pick a date'];
                    yourself)
            onAnswer: [:date |
                self 
                    confirm: 'Do you really want to select ', date printString, '?'
                    ifTrue: [self inform: 'You selected ', date printString]]
    ]
]

You may have noticed the #inform: and #confirm: methods. Again, they are not in Iliad, but we'll write two small widgets for that:

Iliad.ILWidget subclass: InformationWidget [
    | informationString |

    informationString [
        <category: 'accessing'>
        ^informationString
    ]

    informationString: aString [
        <category: 'accessing'>
        informationString := aString
    ]

    contents [
        <category: 'building'>
        ^[:e |
            e text: self informationString.
            e form button
                text: 'Ok';
                action: [self answer]]
    ]
]

Iliad.ILWidget subclass: ConfirmationWidget [
    | confirmationString |

    confirmationString [
        <category: 'accessing'>
        ^confirmationString
    ]

    confirmationString: aString [
        <category: 'accessing'>
        confirmationString := aString
    ]

    contents [
        <category: 'building'>
        ^[:e |
            e text: self confirmationString.
            e form build: [:form |
                form button
                    text: 'Yes';
                    action: [self answer: true].
                form button
                    text: 'No';
                    action: [self answer: false]]]
    ]
]

and ILWidget's extensions:

Iliad.ILWidget extend [
    
    inform: aString [
        <category: 'control flow'>
        self show: (InformationWidget new 
            informationString: aString; 
            yourself)
    ]

    confirm: aString ifTrue: aBlock[
        <category: 'control flow'>
        self 
            show: (ConfirmationWidget new
                confirmationString: aString;
                yourself)
            onAnswer: [:boolean | boolean ifTrue: aBlock]
    ]
]

Now we can finally write an application using our sequence:

Iliad.ILApplication subclass: SequenceApplication [
    | sequence |

    SequenceApplication class >> path [^'sequence']

    index [
        <category: 'controllers'>
        ^self sequence
    ]

    sequence [
        <category: 'accessing'>
        ^sequence ifNil: [sequence  := SequenceExample new]
    ]
]

That's all :) You can see our sequence in action here.

User login