1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is Fireinput Inc.
15 *
16 * Portions created by the Initial Developer are Copyright (C) 2007
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 * Olly Ja <ollyja@gmail.com>
21 *
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the MPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the MPL, the GPL or the LGPL.
33 *
34 * ***** END LICENSE BLOCK *****
35 */
36 var Pagination = {
37
38 buildPages: function(pageinfo, divId, paginateId, numPerPage)
39 {
40 this.pageinfo = pageinfo;
41 this.divId = divId;
42 this.numPerPage = numPerPage;
43 var mod = (this.pageinfo.page.length % this.numPerPage !=0) ? 1 : 0;
44 this.totalPages = parseInt(this.pageinfo.page.length / this.numPerPage) + mod;
45 this.paginateId = paginateId;
46 var initialpage = (pageinfo.selectedpage<this.totalPages)? pageinfo.selectedpage : 0;
47 this.buildPagination(initialpage)
48 this.selectPage(initialpage)
49
50 },
51
52 buildRemotePages: function(totalRemoteCount, remotePages, selectedPage, divId, paginateId, numPerPage)
53 {
54 this.divId = divId;
55 this.numPerPage = numPerPage;
56 this.remotePages = remotePages;
57 var mod = (totalRemoteCount % numPerPage != 0) ? 1 : 0;
58 this.totalRemotePages = parseInt(totalRemoteCount / numPerPage) + mod;
59 this.paginateId = paginateId;
60
61 this.startPage = 0;
62 if (!this.remotePages || this.remotePages.length <= 0)
63 var paginateHTML=""
64 else
65 { //construct pagination interface
66 var paginateHTML='<div class="pagination"><ul>\n'
67 paginateHTML+='<li><a href="#previous" rel="'+(selectedPage-1)+'"><</a></li>\n'
68 var i = (selectedPage -5) > 0 ? selectedPage -5 : 0;
69 var endi = selectedPage + 5;
70 this.startPage = i;
71 for(; i<this.totalRemotePages && i<endi; i++)
72 {
73 paginateHTML+='<li><a href="#page'+(i+1)+'" rel="'+i+'">'+(i+1)+'</a></li>\n'
74 }
75 paginateHTML+='<li><a href="#next" rel="'+(selectedPage+1)+'">></a></li>\n'
76 paginateHTML+='</ul></div>'
77 }
78
79 var paginatediv=document.getElementById(this.paginateId);
80 paginatediv._currentpage=selectedPage - this.startPage;
81 paginatediv.innerHTML=paginateHTML;
82 var pageinstance=this;
83 paginatediv.onclick=function(e){
84 var targetobj=e.target;
85 if (targetobj.tagName=="A" && targetobj.getAttribute("rel")!=""){
86 if (!/disabled/i.test(targetobj.className))
87 {
88 var ipage = parseInt(targetobj.getAttribute("rel"));
89 EmotionMgr.loadServerNextPages(ipage * pageinstance.numPerPage, ipage);
90 }
91 }
92 return false;
93 };
94
95 this.selectRemotePage(selectedPage - this.startPage);
96 },
97
98 buildPagination: function(selectedpage)
99 {
100 if (!this.pageinfo || this.pageinfo.page.length <= 0)
101 var paginateHTML=""
102 else
103 { //construct pagination interface
104 var paginateHTML='<div class="pagination"><ul>\n'
105 paginateHTML+='<li><a href="#previous" rel="'+(selectedpage-1)+'"><</a></li>\n'
106 for (var i=0; i<this.totalPages; i++)
107 {
108 paginateHTML+='<li><a href="#page'+(i+1)+'" rel="'+i+'">'+(i+1)+'</a></li>\n'
109 }
110 paginateHTML+='<li><a href="#next" rel="'+(selectedpage+1)+'">></a></li>\n'
111 paginateHTML+='</ul></div>'
112 }
113
114 var paginatediv=document.getElementById(this.paginateId);
115 paginatediv._currentpage=selectedpage;
116 paginatediv.innerHTML=paginateHTML;
117 var pageinstance=this;
118 paginatediv.onclick=function(e){
119 var targetobj=e.target;
120 if (targetobj.tagName=="A" && targetobj.getAttribute("rel")!=""){
121 if (!/disabled/i.test(targetobj.className))
122 {
123 pageinstance.selectPage(parseInt(targetobj.getAttribute("rel")));
124 }
125 }
126 return false;
127 };
128 },
129
130 selectPage: function(selectedpage)
131 {
132 if(!this.pageinfo || this.pageinfo.page.length <= 0)
133 {
134 var pagecontentdiv = document.getElementById(this.divId);
135 pagecontentdiv.innerHTML = "";
136 return;
137 }
138
139 var sindex = selectedpage * this.numPerPage;
140
141 var paginateHTML='<div>\n'
142 paginateHTML += "<div class='pagerowtitle'><span class='pageimgtitle'>网络图案(已加入)</span>";
143 paginateHTML += "<span class='pagecheckboxtitle'>选择</span></div>";
144 for(var i=sindex; i<sindex+this.numPerPage && i<this.pageinfo.page.length; i++)
145 {
146 paginateHTML += "<div class='pagerow'>";
147 paginateHTML += "<span class='pageimg' title='" + this.pageinfo.page[i].url + "' onclick='EmotionMgr.goToPage(\"" + this.pageinfo.page[i].url + "\")'>";
148 paginateHTML += "<img width='32px' height='32px' src='" + this.pageinfo.page[i].url + "'/></span>";
149 paginateHTML += "<span class='pagecheckbox'>";
150 var shouldchecked = this.pageinfo.page[i].saved ? this.pageinfo.page[i].saved : EmotionMgr.getCheckedStatus(this.pageinfo.page[i].url);
151 var checked = shouldchecked ? "checked": "";
152 paginateHTML += "<input type='checkbox' " + checked + "' onclick='EmotionMgr.updateUserEmotionList(this, \"" + this.pageinfo.page[i].url + "\")'/></span></div>";
153
154 }
155 paginateHTML += "</div>";
156
157 var prevlinkoffset=1;
158 var paginatediv=document.getElementById(this.paginateId);
159 var paginatelinks=paginatediv.getElementsByTagName("a");
160 var pagecontentdiv = document.getElementById(this.divId);
161 pagecontentdiv.innerHTML = paginateHTML;
162 if(paginatelinks.length <=0)
163 return;
164
165 paginatelinks[0].className=(selectedpage==0)? "prevnext disabled" : "prevnext";
166 paginatelinks[0].setAttribute("rel", selectedpage-1);
167 paginatelinks[paginatelinks.length-1].className=(selectedpage==this.totalPages-1)? "prevnext disabled" : "prevnext";
168 paginatelinks[paginatelinks.length-1].setAttribute("rel", selectedpage+1);
169 paginatelinks[paginatediv._currentpage+prevlinkoffset].className="";
170 paginatelinks[selectedpage+prevlinkoffset].className="currentpage";
171 paginatediv._currentpage=selectedpage;
172 },
173
174 selectRemotePage: function(selectedpage)
175 {
176 var sindex = 0;
177
178 if(!this.remotePages || this.remotePages.length <= 0)
179 {
180 var pagecontentdiv = document.getElementById(this.divId);
181 pagecontentdiv.innerHTML = "";
182 return;
183 }
184
185 var paginateHTML='<div>\n'
186 paginateHTML += "<div class='pagerowtitle'><span class='pageimgtitle'>网络图案</span>";
187 paginateHTML += "<span class='pagecheckboxtitle'>选择</span></div>";
188
189 for(var i=sindex; i<sindex+this.numPerPage && i<this.remotePages.length; i++)
190 {
191 paginateHTML += "<div class='pagerow'>";
192 paginateHTML += "<span class='pageimg' title='" + this.remotePages[i] + "' onclick='EmotionMgr.goToPage(\"" + this.remotePages[i] + "\")'>";
193 paginateHTML += "<img width='32px' height='32px' src='" + this.remotePages[i] + "'/></span>";
194 paginateHTML += "<span class='pagecheckbox'>";
195 var shouldchecked = EmotionMgr.getCheckedStatus(this.remotePages[i]);
196 var checked = shouldchecked ? "checked": "";
197 paginateHTML += "<input type='checkbox' " + checked + "' onclick='EmotionMgr.updateUserEmotionList(this, \"" + this.remotePages[i] + "\")'/></span></div>";
198
199 }
200 paginateHTML += "</div>";
201
202 var prevlinkoffset=1;
203 var paginatediv=document.getElementById(this.paginateId);
204 var paginatelinks=paginatediv.getElementsByTagName("a");
205 var pagecontentdiv = document.getElementById(this.divId);
206 pagecontentdiv.innerHTML = paginateHTML;
207 if(paginatelinks.length <=0)
208 return;
209
210 paginatelinks[0].className=(selectedpage==0)? "prevnext disabled" : "prevnext";
211 paginatelinks[0].setAttribute("rel", selectedpage + this.startPage - 1);
212 paginatelinks[paginatelinks.length-1].className=((selectedpage+this.startPage)==this.totalRemotePages-1)? "prevnext disabled" : "prevnext";
213 paginatelinks[paginatelinks.length-1].setAttribute("rel", this.startPage+selectedpage+1);
214 paginatelinks[paginatediv._currentpage+prevlinkoffset].className="";
215 paginatelinks[selectedpage+prevlinkoffset].className="currentpage";
216 paginatediv._currentpage=selectedpage;
217 }
218 };
syntax highlighted by Code2HTML, v. 0.9.1