Removed dep on API
[ninja.git] / application / views / js / pagination.js
blobb6a38b841a4d25c0768f16fda306fdf7dc6a9234
1 $(document).ready(function() {
2 var focused_field = false;
3 $('.show_pagination').each(function() {
4 $(this).bind('click', function() {
5 set_items_per_page();
6 });
7 });
9 $('#pagination_id_1').bind('focus', function() {
10 focused_field = $(this).attr('id');
11 });
12 $('#pagination_id_2').bind('focus', function() {
13 focused_field = $(this).attr('id');
14 });
16 $('#pagination_id_1').bind('change', function() {
17 propagate_val($(this).val());
18 });
19 $('#pagination_id_2').bind('change', function() {
20 focused_field = $(this).attr('id');
21 propagate_val($(this).val());
22 });
24 // check if we have a customized items_per_page
25 var url = location.search;
26 var cust_val = $.query.get('custom_pagination_field');
27 if (cust_val) {
28 // do we have an option with this value?
29 value_exists(cust_val); // added if not
32 $('.pagination_form').bind('submit', function() {
33 preserve_get_params($('#' + focused_field).attr('value'));
34 });
36 });
38 function set_items_per_page()
40 // submit form
41 $('.pagination_form').trigger('submit');
44 function preserve_get_params(custom_val, sel_id)
46 if (custom_val != false && typeof custom_val != 'undefined' && custom_val != 'sel') {
47 propagate_val(custom_val);
48 } else {
49 if (custom_val == 'sel') {
50 propagate_val($('#' + sel_id).val());
54 // make sure we don't loose GET variables from current query string
55 if ($.query.keys) {
56 for (var key in $.query.keys) {
57 if (key != 'items_per_page' && key!= 'custom_pagination_field' && key!='result') {
58 if("object" == typeof $.query.keys[key] && !($.query.keys[key] instanceof Array)) {
59 // only iterate true objects' properties by; 1) check type, and; 2) exclude arrays
60 // (which in javascript also are typeof === object... sigh)
61 $.each($.query.keys[key], function(index) {
62 var val = $.query.keys[key][index];
63 if (val === true)
64 val = '';
65 $('.pagination_form').append('<input type="hidden" name="' + key + '['+index+']" value="' + val + '">');
66 });
67 } else {
68 var val = $.query.keys[key];
69 if (val === true)
70 val = '';
71 $('.pagination_form').append('<input type="hidden" name="' + key + '" value="' + val + '">');
78 function propagate_val(val)
80 val = parseInt(val, '10');
81 if (isNaN(val)) {
82 var cust_val = $.query.get('custom_pagination_field');
83 cust_val = cust_val ? cust_val : 100;
84 propagate_val(cust_val);
85 return false;
87 $('.custom_pagination_field').each(function() {
88 $(this).val(val);
89 value_exists(val);
90 });
93 function value_exists(val)
95 $('.items_per_page').each(function() {
96 if (! $(this).containsOption(val) ) {
97 pagination_add_option(val);
99 $(this).selectOptions(val);
103 function pagination_add_option(val)
105 val = parseInt(val, '10');
106 if (isNaN(val)) {
107 return false;
109 if (val < 0) {
110 val = val*-1;
112 var entries_str = $('.pagination_entries_str').html();
113 $('.items_per_page').each(function() {
114 $(this).addOption(val, val + ' ' + entries_str);
115 //$(this).sortOptions(); // this is not working as of now since sorting is string based :(