Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ajax In Action (2006).pdf
Скачиваний:
63
Добавлен:
17.08.2013
Размер:
8.36 Mб
Скачать

528CHAPTER 13

Building stand-alone applications with Ajax

After we set the new class, we need to create a timer to call the next step. The setTimeout emethod has two parameters. The first is the function or JavaScript statement to execute, and the second is the amount of time in milliseconds before it is executed. In this case, we are going to call our SetClass() function with the incremented state of our class. The timeout is set to our global variable, flipLength, which we declared in section 13.3.1.

The else portion of our script handles the situation when we have looped through our five CSS classes and applied them to the div. First, we remove the CSS Class f from our div. The default opacity is 100 percent and allows the div to cover the other one completely with nothing showing through from the bottom layer.

We increment the currentMessage gvariable, allowing the next message to be loaded. We check to see if that message number his greater than the number of messages contained in our array arrayMessage. If it is greater, we set the current message back to the start. The timer is restarted to load the next message iafter our set period of time. The setTimeout method calls our function, ChangeView(), and our global variable, flipLength, determines the length of time. In order for this to execute, we make sure that our global variable, bPaused, is not true. We will be coding the pause feature of this script in section 13.5.2.

The transition effect of the slideshow is now complete. We can test what we have created so far and see if it works. If everything is working correctly, we should see the page-loading counter slowly increasing as the files are being loaded into the script, and the first message should begin to fade in.

As you can see in figure 13.9, there are two different messages in the viewer since the one is slightly transparent. The current message (6) is displayed in the header, and we are able to see that in total 31 messages were loaded. Now, all we have left to do is add the pause, back, forward, and add functionality to our viewer.

13.5 Additional functionality

The code that we already developed can be used on its own, without the other features, but they can make the script more flexible for users and for us. The first feature that we want to add allows us to import other RSS feeds that are not included in the preload function. Perhaps we want to check a site once in a while for new content, or maybe we want to grab a weather RSS feed. This feature allows us to obtain the syndication feed when we need it. The other features that we can include will let us skip through messages and pause them if we want more time to read them.

Additional functionality

529

 

 

13.5.1Inserting additional feeds

Adding additional messages to our feed is easier than you may think. Take a look at figure 13.9 again. The selection list contains the names and URLs of additional feeds we want to check occasionally; we just select a name and click the Add Feed button. We have already built most of the functionality in section 13.3; all we need to do now is execute our ContentLoader, in listing 13.14, which will add the feed selected in the select element.

Listing 13.14 JavaScript function to load additional RSS feeds

function LoadNews(){

var sel = document.Form1.ddlRSS; if(sel.options[sel.selectedIndex].className!="added"){

var url = sel.options[sel.selectedIndex].value; sel.options[sel.selectedIndex].className="added"; var loader1 = new net.ContentLoader(url,

BuildXMLResults);

}

}

We create a function called LoadNews() that we initiate from our button named btnAdd. Since we are obtaining the additional RSS feeds’ URLs from a select element, we need to reference our select element, ddlRSS, so we can access its values.

Figure 13.9

The fading transition is taking place between message 5 and 6.

530CHAPTER 13

Building stand-alone applications with Ajax

When we want to add an RSS feed from the select element, we need to have some way to tell if it already has been added. One way to do this is to add a CSS class to the option element. Therefore, we need to add a check to verify that we have not added this RSS feed already. If the feed is new, we grab the value of the selected option and change the className to added.

We execute the ContentLoader with the URL of the feed and also the function BuildXMLResults(). We can use the default error message of the ContentLoader if it encounters an error. Now that we have the ability to load a document from the selection list, we need to add RSS feeds to the selection list and also add the event handler to the button, shown in listing 13.15.

Listing 13.15 HTML selection list

<select name="ddlRSS"> <option

value="http://radio.javaranch.com/frank/rss.xml">

Frank</option>

<option

value="http://radio.javaranch.com/gthought/rss.xml">

Gregg</option>

</select>

<input type="button" name="btnAdd" value="Add Feed" onclick="LoadNews()" />

In the selection list, we add URLs to RSS feeds that are not contained in our preloader RSS feed array. In this case, two additional RSS syndication feeds were added from JavaRanch’s radio blog. We add the onclick event handler to our button btnAdd so the function LoadNews() can be executed.

The last step to loading the individual feeds is to create a CSS class to add to our stylesheet. This gives an added benefit to the users by giving a visual aid that the feed has been loaded.

.added{background-color: silver;}

In the CSS class, we can add any CSS rule so we are able to distinguish the added feeds from the others. In this case, we change the background color of the option to silver so that the option stands out in the list. After we add the class, we can test our application.

As figure 13.10 shows, we have added the RSS feed of Frank since it is highlighted in silver. The feed labeled Gregg is not added since it still has the default white background color. The number of messages in our RSS reader also

Additional functionality

531

 

 

Figure 13.10

The Frank feed has been added to the Ajax reader.

increased from 31 to 54 after we added the feed. The only features remaining to add are our back, forward, and pause buttons.

13.5.2Integrating the skipping and pausing functionality

One of the most useful features that we can add is the ability to skip through messages. If we find a message that is not interesting to us, we can click a button to see the next one instead of having to wait for the timeout to execute. The pause feature allows us to have more time to read a message that is interesting or long. Since we have used global variables for our timers, pause, and the currentMessage counter, we are able to affect the current state of the RSS reader very easily. Listing 13.16 shows the code that lets the user flip through the feed.

Listing 13.16 JavaScript function to pause and skip the RSS reader feeds

function MoveFeed (xOption){

b Create function MoveFeed()

if(xOption == 0){

c Check for pause/resume action

if(!bPaused){

 

 

 

 

 

 

bPaused = true;

 

 

d Pause the reader

if(timerSwitch)

 

 

clearTimeout(timerSwitch);

 

document.getElementById("btnPause").value =

 

"RESUME";

 

 

 

}

 

 

 

532

CHAPTER 13

 

 

 

 

Building stand-alone applications with Ajax

 

 

 

 

else{

 

e Resume the

 

 

 

bPaused = false;

 

 

setTimeout("ChangeView()",300);

 

reader

 

document.getElementById("btnPause").value =

 

 

"PAUSE";

 

 

 

 

}

 

 

 

 

}

 

 

 

 

else{

 

 

 

 

 

 

 

 

if(timerSwitch)

 

 

 

 

clearTimeout(timerSwitch);

 

 

 

 

if(xOption == -1)currentMessage += -2;

f Change

 

if(currentMessage < 0)

current

 

currentMessage = arrayMessage.length

message

 

+ currentMessage;

 

 

 

 

ChangeView();

 

 

 

 

}

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

By creating a function, MoveFeed()band allowing it to accept a single parameter, we can handle all three situations; pause, skip forward, and skip backward. We can use an integer to differentiate between the different actions. To pause the reader, we pass in a 0. To skip forward, we use 1, and to skip backward we use –1.

The first functionality we check for is the pause. We verify cthat the passedin parameter is a 0. The pause button has two behaviors. The first is to enter the pause mode, which stops the transitions from executing. The second is resume, which allows for the slideshow to restart the transitions.

If the feed is not paused d, then we need to set our bPaused variable to true and check to see if our timer timerSwitch is running. If the timer is running, we need to cancel it by using the clearTimeout method. We change the button’s text to display the string “RESUME”. If the button is clicked to resume the feed, we do the opposite of pausing the feed e. We set the bPaused variable to false; we call our function ChangeView() with a slight pause in time, and we change the text of our button value back to “PAUSE”.

The pause behavior is now complete.

We have to create our skipping and backtracking functionality f. Since we are changing messages, we should remove the timer to avoid problems with skipping multiple messages. After we remove the timeout, we need to see if the action was –1. If we are moving backward, we need to subtract 2 from the currentMessage variable. This is because the variable, currentMessage, is actually holding the value of the next message since it already has been incremented. By subtracting 1 from the variable, we stay on the same message. Since we are already have the

current-

Additional functionality

533

 

 

next message variable stored in currentMessage, we do not have to do anything for the forward button.

We have to be sure that our number is not less than 0. If it is, we need to set our variable to the last message in our array. After we have changed the

Message, we can call our ChangeView() function to load our message. All we have to do is add the event handlers to the buttons (listing 13.17) so we can execute the function, MoveFeed().

Listing 13.17 onclick event handlers for button actions

value=" <BACK " onclick="MoveFeed(-1)"> value=" PAUSE " onclick="MoveFeed(0)"> value="NEXT>" onclick="MoveFeed(1)">

To initialize the function, we add onclick handlers to our buttons. The onclick handlers call our function MoveFeed(), which passes the integers of –1 to skip backwards, 0 to pause the reader, and 1 to skip forward a message. By saving the document and opening our browser to this page, we can test the last of the functionality.

Now that we have the ability to skip messages, we can advance to the messages in the middle of the RSS feed list. Figure 13.11 shows that the reader is paused since the button btnAdd’s text says RESUME. With the additional features that we

Figure 13.11

This window shows the RSS feeder being paused since the center button is now labeled RESUME.