Skip to content

Commit d1bffb4

Browse files
committed
git
1 parent 6a428ec commit d1bffb4

File tree

2 files changed

+215
-27
lines changed

2 files changed

+215
-27
lines changed

Chapters/17-JQuery/jquery.pillar

Lines changed: 198 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ To use the libraries in your applications, you will need to load them in the Sea
2525

2626
!!! JQuery Basics
2727

28-
jQuery has a simple but powerful model for its interactions. It always follows the same pattern depicted in Figure *@fig:jquery-lifecycle*.
28+
jQuery has a simple but powerful model for its interactions. It always follows the same pattern depicted in Figure *@fig:jquery-lifecycle*. You basically get aJQuery and configure it, navigate its elements and activate it.
2929

30-
+jQuery Demo and Functional Test Suite.>file://figures/jquery-lifecycle.png|width=80|label=fig:jquery-lifecycle+
30+
+jQuery Lifecycle.>file://figures/jquery-lifecycle.png|width=80|label=fig:jquery-lifecycle+
3131

32-
To instantiate a ==JQueryClass== you ask a factory object for a new instance by sending the message ==jQuery==. In most cases the factory object is your ==WAHtmlCanvas==, but it can also be a ==JSScript==.
32+
To instantiate a ==JQueryClass==, you ask a factory object for a new instance by sending the message ==jQuery==. In most cases the factory object is your ==WAHtmlCanvas==, but it can also be a ==JSScript==.
3333

3434
[[[
3535
html jQuery
@@ -38,7 +38,7 @@ html jQuery
3838
While the ==JQueryClass== is conceptually a Javascript class, it is implemented as a Pharo instance. ==html jQuery== returns an instance of ==JQueryClass==.
3939

4040
!!!! Creating Queries
41-
To create a ==JQueryInstance== we specify a CSS selector that queries for certain DOM elements on the your web-page. For example, to select all HTML div tags with the CSS class ==special== one would write:
41+
To create a ==JQueryInstance==, we specify a CSS selector that queries for certain DOM elements on the your web-page. For example, to select all HTML div tags with the CSS class ==special== one would write:
4242

4343
[[[
4444
html jQuery expression: 'div.special'
@@ -70,7 +70,7 @@ Once you have identified the elements, you can specify the actions you wish to p
7070
(html jQuery: 'div.special') siblings; remove
7171
]]]
7272

73-
There are over 180 actions provided by jQuery; these can be investigated by browsing the ==JQueryInstance== class in Smalltalk, and by visiting the jQuery documentation at *http://api.jquery.com/*.
73+
There are over 180 actions provided by jQuery; these can be investigated by browsing the ==JQueryInstance== Pharo class, and by visiting the jQuery documentation at *http://api.jquery.com/*.
7474

7575
You find more details on performing actions in Section *@ref:performing-actions*.
7676

@@ -80,7 +80,6 @@ You find more details on performing actions in Section *@ref:performing-actions*
8080
If you've already used jQuery (or followed the link to the documentation), you will already be familiar with the ==$()== syntax for specifying CSS queries to select DOM elements. ==JQueryClass>>expression:== exposes this same interface, but there are also a number of shortcut forms available to you. All the constructor methods return an instance of ==JQueryInstance==.
8181

8282
!!!! $("div.hint")
83-
8483
Normally a jQuery instance is setup with a CSS selector. You can either use the long form (1) or take the shortcut (2). Of course, both forms are absolutely equivalent, in practice you will mostly encounter the shorter second form:
8584

8685
[[[
@@ -99,7 +98,7 @@ html jQuery: '#foo'. "(3)"
9998
html jQuery: #foo. "(4)"
10099
]]]
101100

102-
!! $("\*")
101+
!!!! $("\*")
103102

104103
The CSS selector to match all elements in the page is ==\*==. Again you have several equivalent possibilities to achieve the same in jQuery. The first two use a CSS selector, while the last one uses a convenience method:
105104

@@ -109,7 +108,7 @@ html jQuery: '*'.
109108
html jQuery all.
110109
]]]
111110

112-
!! $(this)
111+
!!!! $(this)
113112

114113
If you want to refer to the currently active DOM element from an event handler you can use ==new== or ==this==.
115114

@@ -120,7 +119,7 @@ html jQuery new.
120119

121120
Note that the ==new== you call here is not the one implemented in the Smalltalk class ==Behavior==, but a custom one implemented on the instance side of ==JQueryClass==. Similar to all other constructor methods it returns an instance of ==JQueryInstance==.
122121

123-
!! $("<div></div>")
122+
!!!! $("<div></div>")
124123

125124
Furthermore, jQuery provides the possibility to create new HTML code on the fly, that inserted into an existing element. Again we have different equivalent possibilities to do this. The first one uses a raw HTML string, with Seaside we want to avoid this in most cases. The second and third variation uses a block with a new renderer that we can use with the normal Seaside rendering API.
126125

@@ -130,7 +129,7 @@ html jQuery html: [ :r | r div ].
130129
html jQuery: [ :r | r div ].
131130
]]]
132131

133-
!! $(function() { alert('Hello'); })
132+
!!!! $(function() { alert('Hello'); })
134133

135134
Last but not least there is the case of the ==$()== syntax allows you to specify some action that should happen once the page is ready. This is done by attaching
136135

@@ -152,10 +151,199 @@ html jQuery: (html javascript alert: 'Hello').
152151
!!! Refining Queries
153152
@ref:refining-queries
154153

154+
After you made an initial query you can refine the result with additional operations. All existing operations are described in this section:
155+
156+
!!!! Siblings
157+
Get a set of elements containing all of the unique siblings of each of the matched set of elements.
158+
159+
[[[
160+
aQuery siblings.
161+
aQuery siblings: 'div'.
162+
]]]
163+
164+
!!!!Next Siblings
165+
Get a set of elements containing the unique next siblings of each of the given set of elements.
166+
167+
[[[
168+
aQuery next.
169+
aQuery next: 'div'.
170+
]]]
171+
172+
Or, find all sibling elements after the current element.
173+
174+
[[[
175+
aQuery nextAll.
176+
aQuery nextAll: 'div'.
177+
]]]
178+
179+
Or, find all following siblings of each element up to but not including the element matched by the selector.
180+
181+
[[[
182+
aQuery nextUntil: 'div'.
183+
]]]
184+
185+
!!!!Previous Siblings
186+
187+
Get a set of elements containing the unique previous siblings of each of the matched set of elements.
188+
189+
[[[
190+
aQuery previous.
191+
aQuery previous: 'div'.
192+
]]]
193+
194+
Or, find all sibling elements in front of the current element.
195+
196+
[[[
197+
aQuery previousAll.
198+
aQuery previousAll: 'div'.
199+
]]]
200+
201+
Or, find all previous siblings of each element up to but not including the element matched by the selector.
202+
203+
[[[
204+
aQuery previousUntil: 'div'.
205+
]]]
206+
207+
!!!!Children
208+
209+
Get a set of elements containing all of the unique immediate children of each of the matched set of elements.
210+
211+
[[[
212+
aQuery children.
213+
aQuery children: 'div'.
214+
]]]
215+
216+
Find all the child nodes inside the matched elements (including text nodes), or the content document, if the element is an iframe.
217+
218+
[[[
219+
aQuery contents.
220+
]]]
221+
222+
Searches for all elements that match the specified expression.
223+
224+
[[[
225+
aQuery find: 'div'.
226+
]]]
227+
228+
!! Parents
229+
230+
Get a set of elements containing the unique parents of the matched set of elements.
231+
232+
[[[
233+
aQuery parent.
234+
aQuery parent: 'div'.
235+
]]]
236+
237+
Or, find all following siblings of each element up to but not including the element matched by the selector.
238+
239+
[[[
240+
aQuery parents.
241+
aQuery parents: 'div'.
242+
]]]
243+
244+
Or, find all the ancestors of each element in the current set of matched elements, up to but not including the element matched by the selector.
245+
246+
[[[
247+
aQuery parentsUntil: 'div'.
248+
]]]
249+
250+
Get a set of elements containing the closest parent element that matches the specified selector, the starting element included.
251+
252+
[[[
253+
aQuery closest.
254+
aQuery closest: 'div'.
255+
]]]
256+
155257
!!! Performing Actions
156258
@ref:performing-actions
157259

260+
There is a wide variety of actions that come supported with jQuery. jQuery UI and thousands of other plugins add even more. In this section we present some of the most common actions provided by the core framework.
261+
262+
!!!! Classes
263+
264+
The following examples add, remove or toggle the CSS class ==important== given as the first argument. These methods are commonly used to change the appearance of one or more HTML elements for example to visualize a state change in the application.
265+
266+
==aQuery addClass: 'important'.
267+
==aQuery removeClass: 'important'.
268+
==aQuery toggleClass: 'important'.
269+
270+
Also you can query if a particular class is set:
271+
272+
==aQuery hasClass: 'important'.
273+
274+
!!!! Styles
275+
276+
Similarly you can change the style of one or more HTML elements. By providing a dictionary you can change multiple CSS styles at once:
277+
278+
==aQuery css: aDictionary.
279+
280+
Alternatively you can use a dictionary-like protocol to read and write specific style properties:
281+
282+
==aQuery cssAt: 'color'.
283+
==aQuery cssAt: 'color' put: '#ff0'.
284+
285+
Note that in most cases it is preferred to use CSS classes instead of hardcoding your style settings into the application code.
286+
287+
!!!! Attributes
288+
289+
While the above methods change the ==class== and ==style== attribute of one or more DOM elements, there are also accessor methods to change arbitrary HTML attributes. By providing a dictionary of key-value pairs you can change multiple attributes at once:
290+
291+
==aQuery attributes: aDictionary.
292+
293+
Alternatively you can use a dictionary-like protocol to read and write attributes:
294+
295+
==aQuery attributeAt: 'href'.
296+
==aQuery attributeAt: 'href' put: 'http://www.seaside.st/'.
297+
298+
!!!! Replace Content
299+
300+
A common operation on DOM elements is to change their contents, for example to update a view or to display additional information. To set the HTML contents of matched elements you can use the following construct that will replace the contents with ==<div></div>==:
158301

302+
[[[
303+
aQuery html: [ :r | r div ].
304+
]]]
305+
306+
Alternatively you can set the text contents of each element in the set of matched elements:
307+
308+
[[[
309+
aQuery text: 'some text'.
310+
]]]
311+
312+
Last but not least you can set the value. This is especially useful for form fields, that require different ways to set the current contents (input fields require you to change the attribute value, text areas require you to change the contents). The following code takes care of the details automatically:
313+
314+
[[[
315+
aQuery value: 'some value'.
316+
]]]
317+
318+
!!!! Insert Content
319+
320+
Alternatively to replacing the contents you can append new contents. ==before:== inserts content before each element in the set of matched elements; ==prepend:== inserts content to the beginning of each element in the set of matched elements; ==append:== inserts content to the end of each element in the set of matched elements; and ==after:== inserts content after each element in the set of matched elements.
321+
322+
323+
[[[
324+
aQuery before: [ :r | r div ].
325+
aQuery prepend: [ :r | r div ].
326+
aQuery append: [ :r | r div ].
327+
aQuery after: [ :r | r div ].
328+
]]]
329+
330+
Note that, as with ==html:==, the argument can be any renderable object: a string, a Seaside component, or a render block as in the given examples.
331+
332+
!!!! Animations
333+
334+
Showing or hiding DOM elements is one of the most common operations. While this is typically done by adding or removing a CSS class, jQuery provides a simpler way. The action ==show== makes sure that the matching DOM elements are visible. If a duration is given as a first parameter, the elements are faded-in:
335+
336+
[[[
337+
aQuery show.
338+
aQuery show: 1 second.
339+
]]]
340+
341+
The same functionality is available to hide one or more DOM elements with ==hide==:
342+
343+
[[[
344+
aQuery hide.
345+
aQuery hide: 1 second.
346+
]]]
159347

160348
!!! Adding JQuery
161349

book.pillar

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ${inputFile:Chapters/Introduction.pillar}$
44
This part shows you how to get a simple Seaside application up and running
55
in less than 15 minutes. If you’re new to Pharo, have a look at the Pharo by Example Seaside Chapter. You can also have a look at the excellent Pharo Mooc *http://mooc.pharo.org*. Watch the first video and the development session about the counter.
66

7-
${inputFile:Chapters/01-Pharo/Pharo.pillar}$
7+
%${inputFile:Chapters/01-Pharo/Pharo.pillar}$
88
%should do a pass for Zinc, redo screenshots
99

1010
!! Gemstone version
@@ -13,15 +13,15 @@ ${inputFile:Chapters/01-Pharo/Pharo.pillar}$
1313
!Fundamentals
1414
In this part we will introduce you to the manipulation of basic elements such as texts, anchors and callbacks as well as forms. It presents the notion of ''brushes'' that is central to the Seaside API. Understanding these concepts will be fundamental to your use of Seaside.
1515

16-
${inputFile:Chapters/02-RenderingComponents/RenderingComponents.pillar}$
16+
%${inputFile:Chapters/02-RenderingComponents/RenderingComponents.pillar}$
1717
%ready to review and fix figures
1818

19-
${inputFile:Chapters/03-CSS/css.pillar}$
19+
%${inputFile:Chapters/03-CSS/css.pillar}$
2020
%ready to review and fix figures
2121

22-
${inputFile:Chapters/04-Anchors/anchors.pillar}$
22+
%${inputFile:Chapters/04-Anchors/anchors.pillar}$
2323

24-
${inputFile:Chapters/05-Forms/forms.pillar}$
24+
%${inputFile:Chapters/05-Forms/forms.pillar}$
2525

2626

2727
! Using Components
@@ -34,26 +34,26 @@ You will also see how a pre-defined component can be given different behaviour a
3434

3535
Finally, you will see a discussion of ``Slime\'', which despite its name is an extremely useful library to check and validate your Seaside code.
3636

37-
${inputFile:Chapters/06-CallingComponents/callingComponents.pillar}$
38-
${inputFile:Chapters/07-EmbeddingComponents/embeddingComponents.pillar}$
39-
${inputFile:Chapters/08-Tasks/tasks.pillar}$
40-
${inputFile:Chapters/09-Slime/slime.pillar}$
37+
%${inputFile:Chapters/06-CallingComponents/callingComponents.pillar}$
38+
%${inputFile:Chapters/07-EmbeddingComponents/embeddingComponents.pillar}$
39+
%${inputFile:Chapters/08-Tasks/tasks.pillar}$
40+
%${inputFile:Chapters/09-Slime/slime.pillar}$
4141

4242
! Seaside in Action
43-
${inputFile:Chapters/04-ASimpleTodoApplication/ASimpleTodoApplication.pillar}$
44-
${inputFile:Chapters/11-Sudoko/sudoko.pillar}$
43+
%${inputFile:Chapters/04-ASimpleTodoApplication/ASimpleTodoApplication.pillar}$
44+
%${inputFile:Chapters/11-Sudoko/sudoko.pillar}$
4545
%I would group the two example together
4646

47-
${inputFile:Chapters/12-Serving/serving.pillar}$
48-
${inputFile:Chapters/13-Sessions/sessions.pillar}$
47+
%${inputFile:Chapters/12-Serving/serving.pillar}$
48+
%${inputFile:Chapters/13-Sessions/sessions.pillar}$
4949

5050

51-
${inputFile:Chapters/14-RSS/rss.pillar}$
51+
%${inputFile:Chapters/14-RSS/rss.pillar}$
5252
%We should move it close to the Simple App
53-
${inputFile:Chapters/15-REST/rest.pillar}$
54-
${inputFile:Chapters/16-Deploy/deploy.pillar}$
53+
%${inputFile:Chapters/15-REST/rest.pillar}$
54+
%${inputFile:Chapters/16-Deploy/deploy.pillar}$
5555

56-
!!! Should Migrate JQuery
56+
${inputFile:Chapters/17-JQuery/jquery.pillar}$
5757

5858
!! Wish list of new chapter
5959

0 commit comments

Comments
 (0)