forked from SquareBracketAssociates/PharoByExample-english
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbe2-examples.txt
364 lines (352 loc) · 9.36 KB
/
pbe2-examples.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
===== PHARO BY EXAMPLE ==========
Below follow all the (displayed) code examples from the book "Pharo by
Example".
For details about this book, see: http://pharo-project.org/PharoByExample
The examples are provided, as is, for your convenience, in case you want
to copy and paste fragments to Pharo to try out.
Note that in almost all cases the annotation "--> ..." suggests that you
can select and apply <print it> to the previous expression and you should
obtain as a result the value following the arrow.
Many of these actually serve as test cases for the book. For more details
about testing, see the Wiki link under:
http://www.squeaksource.com/PharoByExample.html
Last update: 2010-05-16T12:50:36+02:00
===== CHAPTER: Seaside by Example ==========
-----
WAHelloWorld>>renderContentOn: html
html text: 'hello world'
-----
WAHelloWorld class>>canBeRoot
^ true
-----
html text: 'hello world'. "render a plain text string"
html html: '–'. "render an XHTML incantation"
html render: 1. "render any object"
-----
html horizontalRule.
-----
SeasideDemo>>renderContentOn: html
html heading: 'Rendering Demo'.
html heading
level: 2;
with: 'Rendering basic HTML: '.
html div
class: 'subcomponent';
with: htmlDemo.
"render the remaining components ..."
-----
SeasideDemo>>children
^ { htmlDemo . formDemo . editDemo . dialogDemo }
-----
SeasideHtmlDemo>>renderContentOn: html
self renderParagraphsOn: html.
self renderListsAndTablesOn: html.
self renderDivsAndSpansOn: html.
self renderLinkWithCallbackOn: html
-----
SeasideHtmlDemo>>renderParagraphsOn: html
html paragraph: 'A plain text paragraph.'.
html paragraph: [
html
text: 'A paragraph with plain text followed by a line break. ';
break;
emphasis: 'Emphasized text ';
text: 'followed by a horizontal rule.';
horizontalRule;
text: 'An image URI: '.
html image
url: self squeakImageUrl;
width: '50']
-----
SeasideHtmlDemo>>renderListsAndTablesOn: html
html orderedList: [
html listItem: 'An ordered list item'].
html unorderedList: [
html listItem: 'An unordered list item'].
html table: [
html tableRow: [
html tableData: 'A table with one data cell.']]
-----
SeasideHtmlDemo>>renderDivsAndSpansOn: html
html div
id: 'author';
with: [
html text: 'Raw text within a div with id ''author''. '.
html span
class: 'highlight';
with: 'A span with class ''highlight''.']
-----
SeasideHtmlDemo>>renderLinkWithCallbackOn: html
html paragraph: [
html text: 'An anchor with a local action: '.
html span with: [
html anchor
callback: [toggleValue := toggleValue not];
with: 'toggle boolean:'].
html space.
html span
class: 'boolean';
with: toggleValue ]
-----
SeasideFormDemo>>renderContentOn: html
| radioGroup |
html heading: heading.
html form: [
html span: 'Heading: '.
html textInput on: #heading of: self.
html select
list: self colors;
on: #color of: self.
radioGroup := html radioGroup.
html text: 'Radio on:'.
radioGroup radioButton
selected: radioOn;
callback: [radioOn := true].
html text: 'off:'.
radioGroup radioButton
selected: radioOn not;
callback: [radioOn := false].
html checkbox on: #checked of: self.
html submitButton
text: 'done' ]
-----
SeasideDemoWidget>>style
^ '
body {
font: 10pt Arial, Helvetica, sans-serif, Times New Roman;
}
h2 {
font-size: 12pt;
font-weight: normal;
font-style: italic;
}
table { border-collapse: collapse; }
td {
border: 2px solid #CCCCCC;
padding: 4px;
}
#author {
border: 1px solid black;
padding: 2px;
margin: 2px;
}
.subcomponent {
border: 2px solid lightblue;
padding: 2px;
margin: 2px;
}
.highlight { background-color: yellow; }
.boolean { background-color: lightgrey; }
.field { background-color: lightgrey; }
'
-----
SeasideEditCallDemo>>renderContentOn: html
html span
class: 'field';
with: self text.
html space.
html anchor
callback: [self text: (self !\underline{call:}! (SeasideEditAnswerDemo new text: self text))];
with: 'edit'
-----
SeasideEditAnswerDemo>>renderContentOn: html
html form: [
html textInput
on: #text of: self.
html submitButton
callback: [ self !\underline{answer:}! self text ];
text: 'ok'.
]
-----
SeasideDialogDemo>>renderContentOn: html
html anchor
callback: [ self request: 'edit this' label: 'done' default: 'some text' ];
with: 'self request:'.
...
-----
...
html space.
html anchor
callback: [ self inform: 'yes!' ];
with: 'self inform:'.
...
-----
...
html space.
html anchor
callback: [
(self confirm: 'Are you happy?')
ifTrue: [ self inform: ':-)' ]
ifFalse: [ self inform: ':-(' ]
];
with: 'self confirm:'.
-----
WAConvenienceTest>>go
[ self chooseCheese.
self confirmCheese ] whileFalse.
self informCheese
-----
WAConvenienceTest>>chooseCheese
cheese := self
chooseFrom: #('Greyerzer' 'Tilsiter' 'Sbrinz')
caption: 'What''s your favorite Cheese?'.
cheese isNil ifTrue: [ self chooseCheese ]
-----
WAConvenienceTest>>confirmCheese
^self confirm: 'Is ', cheese, ' your favorite cheese?'
-----
WAConvenienceTest>>informCheese
self inform: 'Your favorite cheese is ', cheese, '.'
-----
WAStore>>renderContentOn: html
"... render the title bar ..."
html div id: 'body'; with: task
-----
WAStoreTask>>go
| shipping billing creditCard |
cart := WAStoreCart new.
self isolate:
[[self fillCart.
self confirmContentsOfCart]
whileFalse].
self isolate:
[shipping := self getShippingAddress.
billing := (self useAsBillingAddress: shipping)
ifFalse: [self getBillingAddress]
ifTrue: [shipping].
creditCard := self getPaymentInfo.
self shipTo: shipping billTo: billing payWith: creditCard].
self displayConfirmation.
-----
WANestedTransaction>>go
self inform: 'Before parent txn'.
self isolate:
[self inform: 'Inside parent txn'.
self isolate: [self inform: 'Inside child txn'].
self inform: 'Outside child txn'].
self inform: 'Outside parent txn'
-----
MyStackMachine>>initialize
super initialize.
contents := OrderedCollection new.
-----
MyStackMachineTest>>testDiv
stack
push: 3;
push: 4;
div.
self assert: stack size = 1.
self assert: stack top = (4/3).
-----
MyRPNWidget>>style
^ 'table.keypad { float: left; }
td.key {
border: 1px solid grey;
background: lightgrey;
padding: 4px;
text-align: center;
}
table.stack { float: left; }
td.stackcell {
border: 2px solid white;
border-left-color: grey;
border-right-color: grey;
border-bottom-color: grey;
padding: 4px;
text-align: right;
}
td.small { font-size: 8pt; }'
-----
MyKeypad>>renderStackButton: text callback: aBlock colSpan: anInteger on: html
html tableData
class: 'key';
colSpan: anInteger;
with:
[html anchor
callback: aBlock;
with: [html html: text]]
-----
MyKeypad>>renderStackButton: text callback: aBlock on: html
self
renderStackButton: text
callback: aBlock
colSpan: 1
on: html
-----
MyKeypad>>renderContentOn: html
self ensureStackMachineNotEmpty.
html table
class: 'keypad';
with: [
html tableRow: [
self renderStackButton: '+' callback: [self stackOp: #add] on: html.
self renderStackButton: '–' callback: [self stackOp: #min] on: html.
self renderStackButton: '×' callback: [self stackOp: #mul] on: html.
self renderStackButton: '÷' callback: [self stackOp: #div] on: html.
self renderStackButton: '±' callback: [self stackOp: #neg] on: html ].
html tableRow: [
self renderStackButton: '1' callback: [self type: '1'] on: html.
self renderStackButton: '2' callback: [self type: '2'] on: html.
self renderStackButton: '3' callback: [self type: '3'] on: html.
self renderStackButton: 'Drop' callback: [self stackOp: #pop]
colSpan: 2 on: html ].
"and so on ... "
html tableRow: [
self renderStackButton: '0' callback: [self type: '0'] colSpan: 2 on: html.
self renderStackButton: 'C' callback: [self stackClearTop] on: html.
self renderStackButton: 'Enter'
callback: [self stackOp: #dup. self setClearMode]
colSpan: 2 on: html ]]
-----
MyKeypad>>type: aString
stackMachine push: (stackMachine pop asString, aString) asNumber.
-----
MyKeypad>>stackOp: op
[ stackMachine perform: op ] on: AssertionFailure do: [ ].
-----
MyKeypad>>type: aString
self inPushMode ifTrue: [
stackMachine push: stackMachine top.
self stackClearTop ].
self inClearMode ifTrue: [ self stackClearTop ].
stackMachine push: (stackMachine pop asString, aString) asNumber.
-----
html anchor
callback: [ self call: (MyDisplayStack new setMyStackMachine: stackMachine)];
with: 'open'
-----
html anchor
callback: [ self answer];
with: 'close'
-----
MyCalculator>>renderContentOn: html
html div id: 'keypad'; with: keypad.
html div id: 'display'; with: display.
-----
MyKeypad>>renderStackButton: text callback: aBlock colSpan: anInteger on: html
html tableData
class: 'key';
colSpan: anInteger;
with: [
html anchor
callback: aBlock;
onClick: "handle Javascript event"
(html updater
id: 'display';
callback: [ :r |
aBlock value.
r render: display ];
return: false);
with: [ html html: text ] ]
-----
MyCalculator class>>initialize
(self registerAsApplication: 'rpn')
addLibrary: SULibrary
-----
new Ajax.Updater(
'display',
'http://localhost/seaside/RPN+Calculator',
{'evalScripts': true,
'parameters': ['_s=zcdqfonqwbeYzkza', '_k=jMORHtqr','9'].join('&')});
return false
-----