Paginator: Getting started with Paginator

このExampleでは「Markupで記述した文章をYUI.widget.Paginatorを使ってページ変えする」機能が実装されている。
この「Markupで記述した文章を」という部分は大切なところで、YUI.widget.Paginatorが

  • ページのコンテンツを保持するものではなく
  • 表示する対象の状態を保持する

モジュールと、スタイルであることを表している(赤部分がデフォルトの表示形式:Sam)。

YUI.widget.Paginatorの状態はstateという属性に保管されており、このstateの内容によって、Style(とStyleベースのコントロール)が制御されている。

stateのは以下の情報を保持している(こちらを参照)。

key
paginator Paginatorインスタンス
page 表示しているページ
totalRecords 全てのデータ件数
recordOffset 新しいページの最初の行のインデックス
rowsPerPage 一行あたりの行数
records 新しいページのレコードの[start index, end index]配列
before 上の現在の値が入ったオブジェクト・リテラル


このサンプルでは、Paginatorに触る(つまり、Paginatorにページ表示をリクエストする)際に発生するイベント(changeRequest)に、ハンドラを仕込んでいる。
このハンドラも、Paginatorに「ページを表示させるものではなく」、Paginatorに対してはstateを変更するだけである(ページの表示は、スタイルの変更によって行っている)。

以下にHTML全文を示す。
注釈をつける程度で、ソースの改変は殆ど行っていない。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
<title>Ajax Sample</title> 
 
<style type="text/css"> 
body {
	margin:0;
	padding:0;
}
</style> 

<link rel="stylesheet" type="text/css" href="scripts/yui/fonts/fonts-min.css" /> 
<link rel="stylesheet" type="text/css" href="scripts/yui/paginator/assets/skins/sam/paginator.css" /> 
<script type="text/javascript" src="scripts/yui/yahoo-dom-event/yahoo-dom-event.js"></script> 
<script type="text/javascript" src="scripts/yui/element/element-beta-min.js"></script> 
<script type="text/javascript" src="scripts/yui/paginator/paginator-min.js"></script> 

<style type="text/css" id="defaultstyle">
#main {
	margin: 2px;
	padding: 3px;
}

#content {
    background: #fff;
    border: 1px solid #ccc;
    color: #000;
    font-family: Times New Roman, serif;
    padding: 1em 2em;
	width: 500px;
	height: auto;
}
 
#content div {
    display: none;
}

#demo .page1 div.page1,
#demo .page2 div.page2,
#demo .page3 div.page3,
#demo .page4 div.page4,
#demo .page5 div.page5 {
    display: block;
}
</style>

<script type="text/javascript"> 
YAHOO.util.Event.onDOMReady(function () {
	 
	// YAHOO.exampleというNameSpaceを定義
	var Ex = YAHOO.namespace('example');
	 
	// YAHOO.exampleへ、id=contentのdivエレメント(文章そのもの)をcontentとして追加
	Ex.content = YAHOO.util.Dom.get('content');
	 
	// YAHOO.exampleへ、function handlePaginationとして追加
	// paginatorの"changeRequest(ページを表示せよ、というRequestで発火)"のハンドラー。
	Ex.handlePagination = function (state) {
		// Ex.content(id=contentのdivエレメント(文章そのもの))のクラス名をpage+requestされたページ番号にする。
	    // (注) スタイルの定義より、この処理によってshowしなくても表示される。
	    Ex.content.className = 'page' + state.page;

	    // paginatorのstateオブジェクトを、リクエストのstateに変更する。(pagenatorのスコープが変わる)
	    Ex.paginator.setState(state);
	};
	 
	// Paginatorインスタンスの生成
	Ex.paginator = new YAHOO.widget.Paginator({
		// 1ページあたり1行を設定
	    rowsPerPage : 1,
		// Ex.contentに含まれるdivエレメントの数が、pagingする総数
	    totalRecords : Ex.content.getElementsByTagName('div').length,
		// id=pagingのdivに生成する。
	    containers : 'paging'
	});
	 
	// "changeRequest(ページを表示せよ、というRequestで発火)"; レンダリング時は1ページ目を表示せよとなる。
	Ex.paginator.subscribe('changeRequest', Ex.handlePagination);
	 
	// paginatorを、id=pagingのdivにレンダリングする。
	Ex.paginator.render();
	 
});
</script>
</head> 
 
<body class=" yui-skin-sam">
<div id="main">
<p>markupで記述されたTextをPaginatorで表示するサンプルです</p>
<div id="demo"> 
    <h2 class="first">The Monster</h2> 
    <p>By Stephen Crane</p> 
 
    <div id="paging"></div> 
 
    <div id="content" class="page1"> 
        <div class="page1"> 
            <p>Little Jim was, for the time, engine Number 36, and he was making the run between Syracuse and Rochester. He was fourteen minutes behind time, and the throttle was wide open. In consequence, when he swung around the curve at the flower-bed, a wheel of his cart destroyed a peony. Number 36 slowed down at once and looked guiltily at his father, who was mowing the lawn. The doctor had his back to this accident, and he continued to pace slowly to and fro, pushing the mower.</p>     
            <p>Jim dropped the tongue of the cart. He looked at his father and at the broken flower. Finally he went to the peony and tried to stand it on its pins, resuscitated, but the spine of it was hurt, and it would only hang limply from his hand. Jim could do no reparation. He looked again toward his father.</p>     
        </div> 
        <div class="page2"> 
            <p>He went on to the lawn, very slowly, and kicking wretchedly at the turf. Presently his father came along with the whirring machine, while the sweet, new grass blades spun from the knives. In a low voice, Jim said, “Pa!”</p>     
            <p>The doctor was shaving this lawn as if it were a priest’s chin. All during the season he had worked at it in the coolness and peace of the evenings after supper. Even in the shadow of the cherry-trees the grass was strong and healthy. Jim raised his voice a trifle. “Pa!”</p>     
            <p>The doctor paused, and with the howl of the machine no longer occupying the sense, one could hear the robins in the cherry-trees arranging their affairs. Jim’s hands were behind his back, and sometimes his fingers clasped and unclasped. Again he said, “Pa!” The child’s fresh and rosy lip was lowered.</p>     
        </div> 
        <div class="page3"> 
            <p>The doctor stared down at his son, thrusting his head forward and frowning attentively. “What is it, Jimmie?”</p>     
            <p>“Pa!” repeated the child at length. Then he raised his finger and pointed at the flower-bed. “There!”</p>     
            <p>“What?” said the doctor, frowning more. “What is it, Jim?”</p>     
            <p>After a period of silence, during which the child may have undergone a severe mental tumult, he raised his finger and repeated his former word―“There!” The father had respected this silence with perfect courtesy. Afterward his glance carefully followed the direction indicated by the child’s finger, but he could see nothing which explained to him. “I don’t understand what you mean, Jimmie,” he said.</p>     
        </div> 
        <div class="page4"> 
            <p>It seemed that the importance of the whole thing had taken away the boy’s vocabulary. He could only reiterate, “There!”</p>     
            <p>The doctor mused upon the situation, but he could make nothing of it. At last he said, “Come, show me.”</p>     
            <p>Together they crossed the lawn toward the flower-bed. At some yards from the broken peony Jimmie began to lag. “There!” The word came almost breathlessly.</p>     
            <p>“Where?” said the doctor.</p>     
            <p>Jimmie kicked at the grass. “There!” he replied.</p>     
        </div> 
        <div class="page5"> 
            <p>The doctor was obliged to go forward alone. After some trouble he found the subject of the incident, the broken flower. Turning then, he saw the child lurking at the rear and scanning his countenance.</p>     
            <p>The father reflected. After a time he said, “Jimmie, come here.” With an infinite modesty of demeanour the child came forward. “Jimmie, how did this happen?”</p>     
            <p>The child answered, “Now&#8212;I was playin’ train&#8212;and&#8212;now&#8212;I runned over it.”</p> 
        </div> 
    </div> 
</div> 
</div>
</body> 
</html>