1/** 2 * Common scripts for Front-end 3 * 4 * @package PT_Content_Views 5 * @author PT Guy <http://www.contentviewspro.com/> 6 * @license GPL-2.0+ 7 * @link http://www.contentviewspro.com/ 8 * @copyright 2014 PT Guy 9 */ 10 11( function ( $ ) { 12 "use strict"; 13 14 $.PT_CV_Public = $.PT_CV_Public || { }; 15 PT_CV_PUBLIC = PT_CV_PUBLIC || { }; 16 var _prefix = PT_CV_PUBLIC._prefix; 17 18 $.PT_CV_Public = function ( options ) { 19 this.options = $.extend( { }, options ); 20 this.pagination(); 21 }; 22 23 $.PT_CV_Public.prototype = { 24 /** 25 * Bootstrap pagination 26 * @returns {undefined} 27 */ 28 pagination: function () { 29 var $self = this; 30 31 var fn_show_page = function ( $pager, selected_page ) { 32 $self._setup_pagination( $pager, selected_page, function () { 33 PT_CV_PUBLIC.paging = 0; 34 } ); 35 }; 36 37 $( '.' + _prefix + 'pagination.' + _prefix + 'ajax' ).each( function () { 38 var this_ = $( this ); 39 var total_pages = $( this ).attr( 'data-totalpages' ); 40 $( this ).bootstrapPaginator( { 41 bootstrapMajorVersion: 3, 42 currentPage: 1, 43 totalPages: total_pages, 44 numberOfPages: PT_CV_PUBLIC.page_to_show, 45 shouldShowPage: function ( type, page, current ) { 46 if ( total_pages && total_pages < 10 ) { 47 switch ( type ) { 48 case "first": 49 case "last": 50 return false; 51 default: 52 return true; 53 } 54 } else { 55 return true; 56 } 57 }, 58 onPageClicked: function ( e, originalEvent, type, page ) { 59 fn_show_page( this_, page ); 60 } 61 } ); 62 } ); 63 }, 64 /** 65 * Get parameters to process pagination 66 * 67 * @param {object} this_ 68 * @param {int} selected_page 69 * @param {function} callback 70 * @returns {undefined} 71 */ 72 _setup_pagination: function ( this_, selected_page, callback ) { 73 var $self = this; 74 75 PT_CV_PUBLIC.paging = PT_CV_PUBLIC.paging || 0; 76 // Prevent duplicate processing 77 if ( PT_CV_PUBLIC.paging ) { 78 return; 79 } else { 80 PT_CV_PUBLIC.paging = 1; 81 } 82 83 var session_id = this_.attr( 'data-sid' ); 84 var spinner = this_.next( '.' + _prefix + 'spinner' ); 85 86 // Get the pagination element 87 var pagination_wrapper = this_; 88 if ( this_.parent( '.' + _prefix + 'pagination-wrapper' ).length ) { 89 pagination_wrapper = this_.parent( '.' + _prefix + 'pagination-wrapper' ); 90 } 91 92 // Get the View element 93 var pages_holder = pagination_wrapper.prev( '.' + _prefix + 'view' ); 94 95 // For Timeline 96 if ( pages_holder.hasClass( _prefix + 'timeline' ) ) { 97 pages_holder = pages_holder.children( '.tl-items' ).first(); 98 } 99 100 // For Load More + Infinite loading 101 if ( pagination_wrapper.find( '.' + _prefix + 'more' ).length > 0 ) { 102 var inner_page = pages_holder.children( '.' + _prefix + 'page' ).first(); 103 if ( inner_page.length > 0 ) { 104 pages_holder = inner_page; 105 } 106 } 107 108 $self._get_page( session_id, selected_page, spinner, pages_holder, callback ); 109 }, 110 /** 111 * Get wrapper of selected page 112 * 113 * @param {string} session_id The session id of view 114 * @param {int} selected_page The page to show 115 * @param {object} spinner The jquery object of loading element 116 * @param {string} pages_holder The selector expression of wrapper of pages 117 * @param {function} callback The callback function 118 * @returns void 119 */ 120 _get_page: function ( session_id, selected_page, spinner, pages_holder, callback ) { 121 var $self = this; 122 123 // Show content of page if it existed 124 var page_existed = $self._active_page( selected_page, pages_holder, callback ); 125 // If page is loaded, exit 126 if ( page_existed ) { 127 // Run callback 128 if ( callback && typeof callback === 'function' ) { 129 callback(); 130 } 131 132 // Trigger action on finish pagination 133 $( 'body' ).trigger( _prefix + 'pagination-finished-simple' ); 134 135 return; 136 } 137 138 // Setup data 139 var data = { 140 action: 'pagination_request', 141 sid: session_id, 142 page: selected_page, 143 lang: PT_CV_PUBLIC.lang, 144 ajax_nonce: PT_CV_PUBLIC._nonce 145 }; 146 147 // Sent POST request 148 $.ajax( { 149 type: "POST", 150 url: PT_CV_PUBLIC.ajaxurl, 151 data: data, 152 beforeSend: function () { 153 // Show loading icon 154 spinner.addClass( 'active' ); 155 } 156 } ).done( function ( response ) { 157 // Hide loading icon 158 spinner.removeClass( 'active' ); 159 160 // Update content of Preview box 161 if ( response.indexOf( _prefix + 'no-post' ) < 0 ) { 162 pages_holder.append( response ); 163 } 164 165 // Active current page 166 $self._active_page( selected_page, pages_holder, callback ); 167 168 // Run callback 169 if ( callback && typeof callback === 'function' ) { 170 callback(); 171 } 172 173 // Trigger action on finish pagination 174 $( 'body' ).trigger( _prefix + 'pagination-finished', [ pages_holder, $( response ) ] ); 175 } ); 176 }, 177 /** 178 * Show content of selected page 179 * 180 * @param {int} selected_page The page to show 181 * @param {string} pages_holder The selector expression of wrapper of pages 182 * @param {function} callback The callback function 183 * @returns bool 184 */ 185 _active_page: function ( selected_page, pages_holder, callback ) { 186 var page_existed = false;
187 var page_selector = '#' + _prefix + 'page' + '-' + parseInt( selected_page ); 188 189 // If found this page, show it, scroll to its top 190 if ( pages_holder.children( page_selector ).length ) { 191 page_existed = true; 192 193 // Hide all pages 194 pages_holder.children().hide(); 195 196 // Show this page 197 pages_holder.children( page_selector ).show(); 198 199 // Scroll to this page 200 $( 'html, body' ).animate( { 201 scrollTop: pages_holder.children( page_selector ).offset().top - 160 202 }, 1000 ); 203 } 204 205 return page_existed; 206 } 207 }; 208 209 $( function () { 210 new $.PT_CV_Public(); 211 } ); 212 213}( jQuery ) );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.