5.11. Combining Forces: A Custom Newsletter
To round out the discussion of dynamic content, I am going to present an application that demonstrates several aspects of dynamic content in action. Unfortunately, the Macintosh version of IE is missing some key ingredients to make this application run on that platform, so this only works on IE 5 and later for Windows and Netscape 6 and later. The example is a newsletter that adjusts its content based on the reader's filtering choices. For ease of demonstration, the newsletter arrives with a total of five stories (containing some real text and some gibberish to fill space) condensed into a single document. A controller box in the upper right corner of the page allows the reader to filter the stories so that only those stories containing specified keywords appear on the page (see Figure 5-3). Not only does the application filter the stories, it orders them based on the number of matching keywords in the stories. In a real application of this type, you might store a profile of subject keywords on the client machine as a cookie and let the document automatically perform the filtering as it loads.
Figure 5-3. A newsletter that uses DHTML to customize its content
Each story arrives inside a div element of class wrapper; each story also has a unique ID that is essentially a serial number identifying the date of the story and its number among the stories of that day. Nested inside each div element are both an h3 element (class of headline) and one or more p elements (class of story). In Example 5-14, the style sheet definition includes placeholders for assigning style rules to each of those classes. At load time, all items of the wrapper class are hidden, so they are ignored by the rendering engine.
The controller box (ID of filter) with all the checkboxes is defined as an absolute-positioned element at the top right of the page. In real life, this type of controller might be better handled as a fixed-position element (if only more browsers supported this style).
The only other noteworthy element is a div element of ID myNews (just above the first story div element). This is an empty placeholder where stories will be inserted for viewing by the user.
The onload event handler of the body element triggers the searching and sorting of stories, as does a click on any of the checkboxes in the controller box. Two global variables assist in searching and sorting. The keywords array is established at initialization time to store all the keywords from the checkboxes. The foundStories array is filled each time a new filtering task is requested. Each entry in the foundStories array is an object with two properties: id, which corresponds to the ID of a selected story, and weight, which is a numeric value that indicates how many times a keyword appears in that story.
Now skip to the filter( ) function, which is the primary function of this application. It is invoked at load time and by each click on a checkbox. The first task is to clear the myNews element by removing all child nodes if any are present. Then the function looks for each div element with a class name of wrapper, so that the div elements can be passed along to the searchAndWeigh( ) function. This is where a DOM-specific invocation of a text range object allows for extraction of just the text from the story. Because the W3C DOM text range doesn't offer the convenience of IE's findText( ) function, we use the old standby of the indexOf( ) function for the string value. By manipulating the start position of the indexOf( ) action inside a while loop, the function can count the number of matches for each keyword within the text.
For each chosen keyword match, the parent element of the current div (the element whose tags surround the matched text) is passed to the getDIVId( ) function. This function makes sure the parent element of the found item has a class associated with it (meaning that it is of the wrapper, headline, or story class). The goal is to find the wrapper class of the matched string, so getDIVId( ) works its way up the chain of parent elements until it finds a wrapper element. Now it's time to add the story belonging to the wrapper class element to the array of found stories. But since the story may have been found during an earlier match, there is a check to see if it's already in the array. If so, the array entry's weight property is incremented by one. Otherwise, the new story is added to the foundStories array.
Since it is conceivable that no story may have a matched keyword (or no keywords are selected), a short routine loads the foundStories array with information from every story in the document. Thus, if there are no matches, the stories appear in the order in which they were entered into the document. Otherwise, the foundStories array is sorted by the weight property of each array entry.
The finale of Example 5-14 is at hand. With the foundStories array as a guide, the hidden div elements are cloned (to preserve the originals untouched). The className properties of the clones are set to a different class selector whose display style property allows the element to be displayed. Then each clone is appended to the end of the myNews element. As the last step, the foundStories array is emptied, so it is ready to do it all over again when the reader clicks on another checkbox.
Example 5-14. A custom newsletter filter that uses DHTML
Some people might argue that it is a waste of bandwidth to download content that the viewer may not need. But unless you have a CGI program running on the server that can query the user's preferences and assemble a single document from matching documents, the alternative is to have the client make numerous HTTP requests for each desired story. When you want to give the user quick access to changeable content, a brief initial delay in downloading the complete content is preferable to individual delays later in the process.
Example 5-14 demonstrates that even when IE has its own way of doing things (as in its TextRange object), you can combine the proprietary DOM with W3C DOM syntax that it does support (as with the cloneNode( ) and appendNode( ) methods). This makes it easier to implement applications that change document content in both DOMs.
Copyright © 2003 O'Reilly & Associates. All rights reserved.