| 1 |
window.vimeography = window.vimeography || {}; |
| 2 |
|
| 3 |
//http://jsfiddle.net/elijahmanor/MVNBz/22/ |
| 4 |
(function(pagination, $, undefined) { |
| 5 |
|
| 6 |
//Public Property |
| 7 |
var paging = {}; |
| 8 |
|
| 9 |
// Private Property |
| 10 |
var pages = {}; |
| 11 |
|
| 12 |
// Public Method |
| 13 |
pagination.paginate = function(element) |
| 14 |
{ |
| 15 |
if (! is_multipage()) |
| 16 |
return false; |
| 17 |
|
| 18 |
var go_to = element.data('go-to'); |
| 19 |
|
| 20 |
pagination.request.action = 'vimeography_pro_ajax_paginate'; |
| 21 |
pagination.request.endpoint = $.isEmptyObject(paging) ? pagination.endpoint : paging[go_to]; |
| 22 |
pagination.request.page = get_page( go_to ); |
| 23 |
|
| 24 |
if ( page_exists() ) |
| 25 |
{ |
| 26 |
console.log(pagination.request); |
| 27 |
|
| 28 |
var promise = $.ajax({ |
| 29 |
type: 'POST', |
| 30 |
url: pagination.request._ajax_url, |
| 31 |
data: pagination.request, |
| 32 |
dataType: 'json' |
| 33 |
}); |
| 34 |
|
| 35 |
promise.done(function(response){ |
| 36 |
console.log(response); |
| 37 |
if (response.result == 'success') |
| 38 |
{ |
| 39 |
pagination.current_page = response.page; |
| 40 |
paging = response.paging; |
| 41 |
pagination.set_pages(); |
| 42 |
pagination.set_paging_controls(); |
| 43 |
|
| 44 |
console.log(paging); |
| 45 |
return true; |
| 46 |
|
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
return false; |
| 51 |
} // end error |
| 52 |
}); |
| 53 |
|
| 54 |
return promise; |
| 55 |
|
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
console.log('Page does not exist.'); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
/** |
| 64 |
* [ description] |
| 65 |
* @return {[type]} [description] |
| 66 |
*/ |
| 67 |
pagination.set_pages = function() { |
| 68 |
pages = { |
| 69 |
first : 1, |
| 70 |
last : Math.ceil(pagination.total / pagination.per_page) |
| 71 |
}; |
| 72 |
pages.next = pagination.current_page < pages.last ? pagination.current_page + 1 : null; |
| 73 |
pages.previous = pagination.current_page > pages.first ? pagination.current_page - 1 : null; |
| 74 |
}; |
| 75 |
|
| 76 |
/** |
| 77 |
* [description] |
| 78 |
* @return {[type]} [description] |
| 79 |
*/ |
| 80 |
pagination.set_paging_controls = function() { |
| 81 |
//Private Property |
| 82 |
var disabled = 'vimeography-paging-disabled'; |
| 83 |
var container = '#vimeography-gallery-' + pagination.request.gallery_id; |
| 84 |
|
| 85 |
$('.' + disabled).removeClass( disabled ); |
| 86 |
|
| 87 |
if (pagination.per_page < pagination.total) |
| 88 |
$(container + ' .vimeography-paging-controls').css('display', 'block'); |
| 89 |
|
| 90 |
switch (pagination.current_page) |
| 91 |
{ |
| 92 |
case pages.first: |
| 93 |
$(container + ' .vimeography-previous-page, ' + container + ' .vimeography-first-page').addClass( disabled ); |
| 94 |
break; |
| 95 |
case pages.last: |
| 96 |
$(container + ' .vimeography-next-page, ' + container + ' .vimeography-last-page').addClass( disabled ); |
| 97 |
break; |
| 98 |
default: |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* [get_page description] |
| 105 |
* @param {[type]} index [description] |
| 106 |
* @return {[type]} [description] |
| 107 |
*/ |
| 108 |
function get_page( index ) |
| 109 |
{ |
| 110 |
return pages[index]; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* [is_multipage description] |
| 115 |
* @return {Boolean} [description] |
| 116 |
*/ |
| 117 |
function is_multipage() |
| 118 |
{ |
| 119 |
return pagination.per_page <= pagination.total; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Don't make the request if it is out of range. |
| 124 |
* @return {[type]} [description] |
| 125 |
*/ |
| 126 |
function page_exists() |
| 127 |
{ |
| 128 |
return pagination.request.page != null; |
| 129 |
} |
| 130 |
|
| 131 |
//Private Method |
| 132 |
function addItem( item ) { |
| 133 |
if ( item !== undefined ) { |
| 134 |
console.log( "Adding " + $.trim(item) ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
}( window.vimeography.pagination = window.vimeography.pagination || {}, jQuery )); |