PageRenderTime 40ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/clientside/static/paginate.js

https://code.google.com/p/enanocms/
JavaScript | 418 lines | 341 code | 35 blank | 42 comment | 111 complexity | 6a0e5c7f165a2196634fd3a40e1ac7db MD5 | raw file
Possible License(s): GPL-2.0
  1. // Javascript pagination control
  2. /**
  3. * Paginates an array of data.
  4. * @param array List of objects to render
  5. * @param function Function called on each member of the array to render, should take an array member and set this.html to a string
  6. * @param int Optional. Object to start at, defaults to 0.
  7. * @param int Optional. Objects per page, defaults to 10
  8. * @param object Variable that will be passed to the renderer callback
  9. */
  10. var pagin_objects = new Object();
  11. window.paginator = function(data, callback, offset, perpage, passer, ov_num_pages, ov_flip_func)
  12. {
  13. load_component('flyin');
  14. if ( !perpage || typeof(perpage) != 'number' || ( typeof(perpage) == 'number' && perpage < 1 ) )
  15. {
  16. this.perpage = 10;
  17. }
  18. else
  19. {
  20. this.perpage = perpage;
  21. }
  22. if ( typeof(offset) != 'number' )
  23. this.offset = 0;
  24. else
  25. this.offset = offset;
  26. if ( typeof(passer) != 'undefined' )
  27. this.passer = passer;
  28. else
  29. this.passer = false;
  30. if ( ov_num_pages )
  31. {
  32. this.num_pages = ov_num_pages;
  33. this.flip_func = ov_flip_func;
  34. }
  35. else
  36. {
  37. this.num_pages = Math.ceil(data.length / perpage);
  38. this.flip_func = false;
  39. }
  40. this.random_id = 'autopagin_' + Math.floor(Math.random() * 1000000);
  41. this._build_control = _build_paginator;
  42. this.set_page = function(number)
  43. {
  44. this.offset = number * this.perpage;
  45. var html = this._build_control(number);
  46. var elements = getElementsByClassName(document.body, 'div', this.random_id + '_control');
  47. for ( var i = 0; i < elements.length; i++ )
  48. elements[i].innerHTML = html;
  49. }
  50. if ( this.num_pages > 1 )
  51. {
  52. var pg_control = '<div class="'+this.random_id+'_control">'+this._build_control(0)+'</div>';
  53. }
  54. else
  55. {
  56. var pg_control = '';
  57. }
  58. this.html = pg_control;
  59. var i = 0;
  60. while ( i < data.length )
  61. {
  62. if ( i % this.perpage == 0 )
  63. {
  64. if ( i > 0 )
  65. this.html += '</div>';
  66. var display = ( ( i * this.perpage ) == this.offset ) ? '' : 'display: none;';
  67. var thispage = Math.floor(i / this.perpage);
  68. this.html += '<div id="' + this.random_id + '_' + thispage + '" style="' + display + '">';
  69. }
  70. this.html += callback(data[i], this.passer);
  71. i++;
  72. }
  73. this.html += '</div>';
  74. this.html += pg_control;
  75. pagin_objects[this.random_id] = this;
  76. }
  77. /**
  78. * Yet another demonstration of the fact that with the right tools, any amount of Javascript can be ported from PHP.
  79. * @access private
  80. */
  81. window._build_paginator = function(this_page)
  82. {
  83. var div_styling = ( IE ) ? 'width: 1px; margin: 10px auto 10px 0;' : 'display: table; margin: 10px 0 10px auto;';
  84. var begin = '<div class="tblholder" style="'+div_styling+'"><table border="0" cellspacing="1" cellpadding="4"><tr><th>' + $lang.get('paginate_lbl_page') + '</th>';
  85. var block = '<td class="row1" style="text-align: center; white-space: nowrap;">{LINK}</td>';
  86. var end = '</tr></table></div>';
  87. var blk = new templateParser(block);
  88. var inner = '';
  89. var cls = 'row2';
  90. if ( this_page > 0 )
  91. {
  92. var url = '#page_'+(this_page);
  93. var link = "<a href=\""+url+"\" onclick=\"jspaginator_goto('"+this.random_id+"', "+(this_page-1)+"); return false;\" style='text-decoration: none;'>&laquo; " + $lang.get('paginate_btn_prev') + "</a>";
  94. cls = ( cls == 'row1' ) ? 'row2' : 'row1';
  95. blk.assign_vars({
  96. CLASS: cls,
  97. LINK: link
  98. });
  99. inner += blk.run();
  100. }
  101. if ( this.num_pages < 5 )
  102. {
  103. for ( var i = 0; i < this.num_pages; i++ )
  104. {
  105. cls = ( cls == 'row1' ) ? 'row2' : 'row1';
  106. var j = i + 1;
  107. var url = '#page_'+j;
  108. var link = ( i == this_page ) ? "<b>"+j+"</b>" : "<a href=\""+url+"\" onclick=\"jspaginator_goto('"+this.random_id+"', "+i+"); return false;\" style='text-decoration: none;'>"+j+"</a>";
  109. blk.assign_vars({
  110. CLASS: cls,
  111. LINK: link
  112. });
  113. inner += blk.run();
  114. }
  115. }
  116. else
  117. {
  118. if ( this_page + 5 > this.num_pages )
  119. {
  120. var list = new Array();
  121. var tp = this_page; // The vectors below used to be 3, 2, and 1
  122. if ( this_page + 0 == this.num_pages ) tp = tp - 2;
  123. if ( this_page + 1 == this.num_pages ) tp = tp - 1;
  124. if ( this_page + 2 == this.num_pages ) tp = tp - 0;
  125. for ( var i = tp - 1; i <= tp + 1; i++ )
  126. {
  127. list.push(i);
  128. }
  129. }
  130. else
  131. {
  132. var list = new Array();
  133. var current = this_page;
  134. var lower = ( current < 3 ) ? 1 : current - 1;
  135. for ( var i = 0; i < 3; i++ )
  136. {
  137. list.push(lower + i);
  138. }
  139. }
  140. var url = '#page_1';
  141. var link = ( 0 == this_page ) ? "<b>" + $lang.get('paginate_btn_first') + "</b>" : "<a href=\""+url+"\" onclick=\"jspaginator_goto('"+this.random_id+"', 0); return false;\" style='text-decoration: none;'>&laquo; " + $lang.get('paginate_btn_first') + "</a>";
  142. blk.assign_vars({
  143. CLASS: cls,
  144. LINK: link
  145. });
  146. inner += blk.run();
  147. // if ( !in_array(1, $list) )
  148. // {
  149. // $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
  150. // $blk->assign_vars(array('CLASS'=>$cls,'LINK'=>'...'));
  151. // $inner .= $blk->run();
  152. // }
  153. for ( var k in list )
  154. {
  155. var i = list[k];
  156. if ( i == this.num_pages )
  157. break;
  158. cls = ( cls == 'row1' ) ? 'row2' : 'row1';
  159. var j = i + 1;
  160. var url = '#page_'+j;
  161. var link = ( i == this_page ) ? "<b>"+j+"</b>" : "<a href=\""+url+"\" onclick=\"jspaginator_goto('"+this.random_id+"', "+i+"); return false;\" style='text-decoration: none;'>"+j+"</a>";
  162. blk.assign_vars({
  163. CLASS: cls,
  164. LINK: link
  165. });
  166. inner += blk.run();
  167. }
  168. if ( this_page < this.num_pages )
  169. {
  170. // $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
  171. // $blk->assign_vars(array('CLASS'=>$cls,'LINK'=>'...'));
  172. // $inner .= $blk->run();
  173. cls = ( cls == 'row1' ) ? 'row2' : 'row1';
  174. var url = '#page_' + String( this.num_pages-1 );
  175. var link = ( ( this.num_pages - 1 ) == this_page ) ? "<b>" + $lang.get('paginate_btn_last') + "</b>" : "<a href=\""+url+"\" onclick=\"jspaginator_goto('"+this.random_id+"', "+(this.num_pages-1)+"); return false;\" style='text-decoration: none;'>" + $lang.get('paginate_btn_last') + " &raquo;</a>";
  176. blk.assign_vars({
  177. CLASS: cls,
  178. LINK: link
  179. });
  180. inner += blk.run();
  181. }
  182. }
  183. if ( this_page < ( this.num_pages - 1 ) )
  184. {
  185. var url = '#page_' + String(this_page + 2);
  186. var link = "<a href=\""+url+"\" onclick=\"jspaginator_goto('"+this.random_id+"', "+(this_page+1)+"); return false;\" style='text-decoration: none;'>" + $lang.get('paginate_btn_next') + " &raquo;</a>";
  187. cls = ( cls == 'row1' ) ? 'row2' : 'row1';
  188. blk.assign_vars({
  189. CLASS: cls,
  190. LINK: link
  191. });
  192. inner += blk.run();
  193. }
  194. inner += '<td class="row2" style="cursor: pointer;" onclick="paginator_goto(this, '+this_page+', '+this.num_pages+', '+this.perpage+', {js: true, random_id: \''+this.random_id+'\'});">&darr;</td>';
  195. var paginator = "\n"+begin+inner+end+"\n";
  196. return paginator;
  197. }
  198. var __paginateLock = false;
  199. window.jspaginator_goto = function(pagin_id, jump_to)
  200. {
  201. if ( __paginateLock )
  202. return false;
  203. var theobj = pagin_objects[pagin_id];
  204. if ( theobj.flip_func )
  205. {
  206. theobj.flip_func(theobj, jump_to);
  207. __paginateLock = false;
  208. return true;
  209. }
  210. var current_div = false;
  211. var new_div = false;
  212. for ( var i = 0; i < theobj.num_pages; i++ )
  213. {
  214. var thediv = document.getElementById(pagin_id + '_' + i);
  215. if ( !thediv )
  216. {
  217. // if ( window.console )
  218. // window.console.error('jspaginator_goto(): got a bad DOM object in loop');
  219. return false;
  220. }
  221. // window.console.debug("Div "+i+' of '+(theobj.num_pages-1)+': ', thediv);
  222. if ( thediv.style.display != 'none' )
  223. current_div = thediv;
  224. else if ( i == jump_to )
  225. new_div = thediv;
  226. }
  227. if ( !new_div )
  228. {
  229. // if ( window.console )
  230. // window.console.error('jspaginator_goto(): didn\'t get new div');
  231. return false;
  232. }
  233. if ( !current_div )
  234. {
  235. // if ( window.console )
  236. // window.console.error('jspaginator_goto(): didn\'t get current div');
  237. return false;
  238. }
  239. // window.console.debug(current_div);
  240. // window.console.debug(new_div);
  241. // White-out the old div and fade in the new one
  242. if ( IE || is_Safari || aclDisableTransitionFX )
  243. {
  244. current_div.style.display = 'none';
  245. new_div.style.display = 'block';
  246. }
  247. else
  248. {
  249. __paginateLock = true;
  250. var fade_time = 375;
  251. var code = 'var old = \'' + current_div.id + '\';';
  252. code += 'var newer = \'' + new_div.id + '\';';
  253. code += 'document.getElementById(old).style.display = "none";';
  254. code += 'changeOpac(0, newer);';
  255. code += 'document.getElementById(newer).style.display = "block";';
  256. code += 'opacity(newer, 0, 100, '+fade_time+');';
  257. code += '__paginateLock = false;';
  258. // if ( window.console )
  259. // window.console.debug('metacode for fader: ', code);
  260. opacity(current_div.id, 100, 0, fade_time);
  261. setTimeout(code, (fade_time + 50));
  262. }
  263. var pg_control = theobj._build_control(jump_to);
  264. var divs = getElementsByClassName(document, 'div', pagin_id + '_control');
  265. for ( var i = 0; i < divs.length; i++ )
  266. {
  267. divs[i].innerHTML = pg_control;
  268. }
  269. }
  270. window.paginator_goto = function(parentobj, this_page, num_pages, perpage, additive, url_string)
  271. {
  272. load_component('flyin');
  273. var height = $dynano(parentobj).Height();
  274. var width = $dynano(parentobj).Width();
  275. var left = $dynano(parentobj).Left();
  276. var top = $dynano(parentobj).Top();
  277. var left_pos = left + width ;
  278. var top_pos = height + top;
  279. var div = document.createElement('div');
  280. div.style.position = 'absolute';
  281. div.style.top = top_pos + 'px';
  282. div.className = 'question-box';
  283. div.style.margin = '1px 0 0 2px';
  284. var vtmp = 'input_' + Math.floor(Math.random() * 1000000);
  285. var regex = new RegExp('\"', 'g');
  286. var submit_target = ( typeof(url_string) == 'object' ) ? ( toJSONString(url_string) ).replace(regex, '\'') : 'unescape(\'' + escape(url_string) + '\')';
  287. var onclick = 'paginator_submit(this, '+num_pages+', '+perpage+', '+additive+', '+submit_target+'); return false;';
  288. div.innerHTML = $lang.get('paginate_lbl_goto_page') + '<br /><input type="text" size="2" style="padding: 1px; font-size: 8pt;" value="'+(parseInt(this_page)+1)+'" id="'+vtmp+'" />&emsp;<a href="#" onclick="'+onclick+'" style="font-size: 14pt; text-decoration: none;">&raquo;</a>&emsp;<a href="#" onclick="var _pn = this.parentNode; setTimeout(function() { _pn.parentNode.removeChild(_pn); }, 2000); fly_out_top(this.parentNode, false, true); return false;" style="font-size: 14pt; text-decoration: none;">&times;</a>';
  289. var body = document.getElementsByTagName('body')[0];
  290. domObjChangeOpac(0, div);
  291. body.appendChild(div);
  292. document.getElementById(vtmp).onkeypress = function(e)
  293. {
  294. if ( e.keyCode == 13 )
  295. this.nextSibling.nextSibling.onclick();
  296. };
  297. document.getElementById(vtmp).focus();
  298. // fade the div
  299. /*
  300. if(!div.id) div.id = 'autofade_'+Math.floor(Math.random() * 100000);
  301. var from = '#33FF33';
  302. Fat.fade_element(div.id,30,2000,from,Fat.get_bgcolor(div.id));
  303. */
  304. fly_in_bottom(div, false, true);
  305. var divh = $dynano(div).Width();
  306. left_pos = left_pos - divh;
  307. div.style.left = left_pos + 'px';
  308. }
  309. window.paginator_submit = function(obj, max, perpage, additive, formatstring)
  310. {
  311. var userinput = obj.previousSibling.previousSibling.value;
  312. userinput = parseInt(userinput);
  313. var offset = (( userinput - 1 ) * perpage) + additive;
  314. if ( userinput > max || isNaN(userinput) || userinput < 1 )
  315. {
  316. load_component(['messagebox', 'fadefilter', 'flyin']);
  317. new MessageBox(MB_OK|MB_ICONSTOP, $lang.get('paginate_err_bad_page_title'), $lang.get('paginate_err_bad_page_body', { max: max }));
  318. return false;
  319. }
  320. if ( typeof(formatstring) == 'object' )
  321. {
  322. fly_out_top(obj.parentNode, false, true);
  323. jspaginator_goto(formatstring.random_id, ( offset / perpage ));
  324. }
  325. else
  326. {
  327. var url = sprintf(formatstring, String(offset));
  328. fly_out_top(obj.parentNode, false, true);
  329. window.location = url;
  330. }
  331. }
  332. // This code is in the public domain. Feel free to link back to http://jan.moesen.nu/
  333. function sprintf()
  334. {
  335. if (!arguments || arguments.length < 1 || !RegExp)
  336. {
  337. return;
  338. }
  339. var str = arguments[0];
  340. var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
  341. var a = b = [], numSubstitutions = 0, numMatches = 0;
  342. while (a = re.exec(str))
  343. {
  344. var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
  345. var pPrecision = a[5], pType = a[6], rightPart = a[7];
  346. //alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);
  347. numMatches++;
  348. if (pType == '%')
  349. {
  350. subst = '%';
  351. }
  352. else
  353. {
  354. numSubstitutions++;
  355. if (numSubstitutions >= arguments.length)
  356. {
  357. alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
  358. }
  359. var param = arguments[numSubstitutions];
  360. var pad = '';
  361. if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
  362. else if (pPad) pad = pPad;
  363. var justifyRight = true;
  364. if (pJustify && pJustify === "-") justifyRight = false;
  365. var minLength = -1;
  366. if (pMinLength) minLength = parseInt(pMinLength);
  367. var precision = -1;
  368. if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
  369. var subst = param;
  370. if (pType == 'b') subst = parseInt(param).toString(2);
  371. else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
  372. else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
  373. else if (pType == 'u') subst = Math.abs(param);
  374. else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
  375. else if (pType == 'o') subst = parseInt(param).toString(8);
  376. else if (pType == 's') subst = param;
  377. else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
  378. else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
  379. }
  380. str = leftpart + subst + rightPart;
  381. }
  382. return str;
  383. }