Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Schongar P.VBScript unleashed.1997

.pdf
Скачиваний:
45
Добавлен:
23.08.2013
Размер:
1.59 Mб
Скачать

7:sTemp = "<A LANGUAGE=VBScript HREF=#null onClick="

8:sTemp = sTemp & Chr(34)

9:sTemp = sTemp & "Call addAppt(" & m & ", " & d & ", " & y &

")"

10:sTemp = sTemp & Chr(34)

11:sTemp = sTemp & " onMouseOver="

12:sTemp = sTemp & Chr(34)

13:sTemp = sTemp & "Call ApptStat(" & m & ", " & d & ", " & y & ")" & Chr(34)

&">"

14:sTemp = sTemp & d & "!</a>"

15:Else

16:sTemp = "<A LANGUAGE=VBScript HREF=#null onClick="

17:sTemp = sTemp & Chr(34)

18:sTemp = sTemp & "Call addAppt(" & m & ", " & d & ", " & y &

")"

19:sTemp = sTemp & Chr(34)

20:sTemp = sTemp & " onMouseOver="

21:sTemp = sTemp & Chr(34)

22:sTemp = sTemp & "Call ApptStat(" & m & ", " & d & ", " & y & ")" & Chr(34)

&">"

23:sTemp = sTemp & d & "</A>"

24:End If

25:makAP_URL = sTemp

26:End Function

Creating the Calendar

The final set of HREF functions is the makCalendar() function (see Listing 24.8), which iterates through several loops to create a calendar out of an embedded table based on the current month and year, and the GMonth() function (see Listing 24.9), which provides the month names for makCalendar().

Listing 24.8. The makCalendar() function.

1:

2:Function makCalendar()

3:m=Month(Now())

4:y=Year(Now())

5:

6:FirstDay = WeekDay(Dateserial(y,m,1))

7:DaysInMonth= Day(Dateserial(y,m+1,1)-1)

8:ThisMonth=GMonth(Month(Dateserial(y,m,1)), true) & " " & Year (Dateserial(y,m,1))

9:

10:sCalendar=""

11:sCalendar=sCalendar & "<CENTER><TABLE BORDER=1 CELLPADDING=2>"

12:sCalendar=sCalendar & "<TR><TH COLSPAN=7>" & ThisMonth & "</TH>"

13:sCalendar=sCalendar & "<TR><TH WIDTH=50 ALIGN=CENTER>Sun</TH><TH WIDTH=50

ALIGN=CENTER>Mon</TH><TH WIDTH=50 ALIGN=CENTER>Tue</TH><TH

WIDTH=50

ALIGN=CENTER>Wed</TH><TH WIDTH=50 ALIGN=CENTER>Thu</TH><TH

WIDTH=50

ALIGN=CENTER>Fri</TH><TH WIDTH=50 ALIGN=CENTER>Sat</TH></TR>"

14:

15:ictrDay=0

16:For Row=1 to 6

17:sCalendar=sCalendar & "<TR>"

18:For Col=1 to 7

19:sCalendar=sCalendar & "<TD WIDTH=50 ALIGN=CENTER>"

20:If Row=1 AND Col=FirstDay then

21: ictrDay=1

22:End If

23:If ictrDay = 0 Then

24: sCalendar=sCalendar & " "

25:End If

26:If ictrDay <> 0 Then

27:

If ictrDay <= DaysInMonth Then

28:

sCalendar=sCalendar & makAP_URL(m,ictrDay,y)

29:

End If

30:

If ictrDay > DaysInMonth Then

31:

sCalendar=sCalendar & " "

32:

End If

33:

ictrDay=ictrDay+1

34:End if

35:sCalendar=sCalendar & "</TD>"

36:Next

37:sCalendar=sCalendar & "</TR>"

38:Next

39:sCalendar=sCalendar & "</TABLE>"

40:makCalendar = sCalendar

41:End Function

42:

The makCalendar()function creates a table definition, creates the month banner and the seven weekday columns, and then loops through the remaining rows and columns, calling the makAP_URL() function from Listing 24.7 to place the date. The makAP_URL() function appends an exclamation mark to the end of a date that contains an appointment.

The GMonth() function takes a pair of arguments, the month number, and a boolean value to determine whether it should return a long month like January or a short month like Jan. Although the short month is not used, it produces a value that can be used in the expiration field of a cookie so that you can expand this example to create time-sensitive to-do's.

Listing 24.9. The GMonth() function.

1:Function GMonth(iMonth, fLongShort)

2:Select Case iMonth

3:Case 1

4:If fLongShort = true then GMonth="January" else GMonth="Jan"

5:Case 2

6:If fLongShort = true then GMonth="February" else GMonth="Feb"

7:Case 3

8:If fLongShort = true then GMonth="March" else GMonth="Mar"

9:Case 4

10:If fLongShort = true then GMonth="April" else GMonth="Apr"

11:Case 5

12:If fLongShort = true then GMonth="May" else GMonth="May"

13:Case 6

14:If fLongShort = true then GMonth="June" else GMonth="Jun"

15:Case 7

16:If fLongShort = true then GMonth="July" else GMonth="Jul"

17:Case 8

18:If fLongShort = true then GMonth="August" else GMonth="Aug"

19:Case 9

20:If fLongShort = true then GMonth="September" else GMonth="Sep"

21:Case 10

22:If fLongShort = true then GMonth="October" else GMonth="Oct"

23:Case 11

24:If fLongShort = true then GMonth="November" else GMonth="Nov"

25:Case 12

26:If fLongShort = true then GMonth="December" else GMonth="Dec"

27:End Select

28: End Function

29:

Presenting Information

An application is completely useless if it doesn't provide the user any feedback. In order to keep the WWW PIM from being completely useless, we need to add some code that creates an interface for the user to edit and view information from the page. This code is broken down into two separate groups:

The add routines

The ApptStat routine

The add Routines

The add routines in Listings 24.10, 24.11, and 24.12 provide an interface for the user to input, edit, or delete information. Each routine displays one or more message boxes and prompts the user for the necessary information. Depending on whether the user is adding, editing, or deleting information, each routine then calls either setVariable or killVariable.

The addTodo and addAddr routines both display a generic message box like the one in Figure 24.2.

Figure 24.2 : The Add/Delete Item message box.

Listing 24.10. The addTodo routine.

1:

2:Sub addTodo(iRecord)

3:Dim sTodo

4:Dim iChoice

5:

6: iChoice = MsgBox("Do you want to add or delete this item? Click yes to add or no

to delete.", 3, "Add/Delete Item")

7:Select Case iChoice

8:Case 6

9:sTodo = InputBox("What do you want to do?", "Add To-Do")

10:Call setVariable("%%TD" & Trim(iRecord), sTodo)

11:Case 7

12:killVariable "%%TD" & Trim(iRecord)

13:Case 2

14:Exit Sub

15:End Select

16:window.navigate "wwwpim.htm"

17:End Sub

18:

Listing 24.10 displays an input box that looks like Figure 24.3.

Figure 24.3 : The Add To-Do dialog.

Listing 24.11. The addAppt routine.

1:

2:Sub addAppt(m,d,y)

3:Dim sAppt

4:Dim TempAppt

5:

6: TempAppt = readVariable("%%AP" & Trim(m) & "/" & Trim(d) & "/" & Trim(y))

7:

8:If Len(Trim(TempAppt)) > 0 Then

9:sAppt = InputBox("Please enter or edit your appointment list for today",

"Add/Edit Appointment", readVariable("%%AP" & Trim(m) &

"/"

 

& Trim(d) & "/"

& Trim(y)))

10:Else

11:sAppt = InputBox("Please enter or edit your appointment list for today",

"Add/Edit Appointment")

12:End If

13:If Len(Trim(sAppt)) > 0 Then

14:Call setVariable("%%AP" & Trim(m) & "/" & Trim(d)

& "/" & Trim(y), sAppt)

15:Else

16:killVariable("%%AP" & Trim(m) & "/" & Trim(d) & "/" & Trim

(y))

17:End If

18:window.navigate "wwwpim.htm"

19:End Sub

20:

Listing 24.11 displays an input box like the one in Figure 24.4.

Figure 24.4 : The Add/Edit Appointment dialog.

Listing 24.12 displays an input box like the one in Figure 24.5.

Figure 24.5 : The Add/Delete address routine.

Listing 24.12. The addAddr routine.

1:

2:Sub addAddr(iRecord)

3:Dim Addr

4:Dim iChoice

5:iChoice = MsgBox("Do you want to add or delete this item? Click

yes to add or no

to delete.", 3,"Add/Delete Item")

6:

7:Select Case iChoice

8:Case 6

9:Addr=InputBox("What address do you want to add?", "Add Email Address")

10:Call setVariable("%%AD" & Trim(iRecord), Addr)

11:Case 7

12:killVariable "%%AD" & Trim(iRecord)

13:Case 2

14:Exit Sub

15:End Select

16:window.navigate "wwwpim.htm"

17:End Sub

18:

The ApptStat Routine

The ApptStat routine (see Listing 24.13) is called by the onMouseOver event associated with each day. When the mouse passes over a date, this routine is called and the current appointment is placed in the status bar.

Listing 24.13. The ApptStat routine.

1:

2:Sub ApptStat(m,d,y)

3:Dim sTemp

4:

 

5: sTemp

= CStr(d & " " & readVariable("%%AP" & m & "/" & d & "/"

& y))

 

6:If Len(Trim(sTemp)) > 0 AND readVariable("%%AP" & m & "/" & d & "/" & y) <> "NULL" Then

7:window.status = sTemp

8:Else

9:window.status = CStr(d)

10:End If

11:End Sub

Tying It All Together

Now that we've covered all the routines that support generation of the page, we have one last procedure to cover, the docCreate routine.

The docCreate Procedure

The docCreate procedure in Listing 24.14 encapsulates all the previous functions to generate all of the HTML for the page. It creates each column one at a time, filling in each cell with an <HREF> from one of the mak() functions, and then it calls document.write to append the new content to the document.

Listing 24.14. The docCreate procedure.

1:

2:Sub docCreate

3:Dim sDocument

4:sDocument = "<TD ALIGN=LEFT VALIGN=TOP WIDTH=35%><CENTER><B>To Do</B></CENTER>"

5:sDocument = sDocument & Chr(13) & Chr(10) & "<HR>"

6:For i = 1 to 10

7:sDocument = sDocument & "<BR>" & makTD_URL(i)

8:Next

9:sDocument = sDocument & "</TD><TD ALIGN=CENTER VALIGN=MIDDLE WIDTH=30%>"

10:sDocument = sDocument & makCalendar()

11:sDocument = sDocument & "</TD><TD ALIGN=LEFT VALIGN=TOP WIDTH=35%

>"

12:sDocument = sDocument & "<CENTER><B>ADDRESSES</B></CENTER>"

13:sDocument = sDocument & Chr(13) & Chr(10) & "<HR>"

14:For j = 1 to 10

15:sDocument = sDocument & "<BR>" & makAD_URL(j)

16:Next

17:

18:sDocument = sDocument & "</TD></TR>"

19:sDocument = sDocument & Chr(13) & Chr(10) & "</TABLE>"

21:Document.Write sDocument

23:End Sub

Review

After trying this sample, you can see how to create Web applications that can easily store and retrieve complex sets of information and display them to the user.

Here's a few challenges for you to extend your WWW Personal Information Manager:

Extend functionality by allowing you to create as many to-do's and addresses as you want, place appointments for future dates, or add more information like phone and street addresses.

Add a notification capability that tells you when appointments are due or forthcoming.

Convert the page to use ActiveX controls to give it a sleek and modern look.