Skip to content
Browse files

add method option

  • Loading branch information...
1 parent 25a03b5 commit 850f6727695381a84599c32b2028cfe52dd45749 @aegorov aegorov committed Oct 28, 2012
Showing with 46 additions and 31 deletions.
  1. +1 −0 README.md
  2. +44 −30 lib/jquery.pageless.js
  3. +1 −1 lib/jquery.pageless.min.js
View
1 README.md
@@ -21,6 +21,7 @@ Parameters
* `params`: paramaters for the ajax query, you can pass auth_token here
* `totalPages`: total number of pages
* `url`: URL used to request more data
+* `method`: HTML method for call URL, default - get
* `scrape`: A function to modify the incoming data. (Doesn't do anything by default)
* `complete`: A function to call when a new page has been loaded (optional)
* `end`: A function to call when the last page has been loaded (optional)
View
74 lib/jquery.pageless.js
@@ -3,7 +3,7 @@
//
// Pageless is a jQuery plugin.
// As you scroll down you see more results coming back at you automatically.
-// It provides an automatic pagination in an accessible way : if javascript
+// It provides an automatic pagination in an accessible way : if javascript
// is disabled your standard pagination is supposed to work.
//
// Licensed under the MIT:
@@ -16,11 +16,12 @@
// loaderHtml: html code of the div if loader not used
// loaderImage: image inside the loader
// loaderMsg: displayed ajax message
-// pagination: selector of the paginator divs.
+// pagination: selector of the paginator divs.
// if javascript is disabled paginator is provided
// params: paramaters for the ajax query, you can pass auth_token here
// totalPages: total number of pages
// url: URL used to request more data
+// method: HTML method for call URL, default - get
//
// Callback Parameters:
// scrape: A function to modify the incoming data.
@@ -49,7 +50,7 @@
// =======================================================================
(function($) {
-
+
var FALSE = !1
, TRUE = !FALSE
, element
@@ -65,14 +66,15 @@
, params: {}
, url: location.href
, loaderImage: "/images/load.gif"
+ , method: 'get'
}
, container
, $container;
-
+
$.pageless = function(opts) {
$.isFunction(opts) ? opts.call() : init(opts);
};
-
+
$.pagelessReset = function() {
settings = { container: window
, currentPage: 1
@@ -81,45 +83,46 @@
, params: {}
, url: location.href
, loaderImage: "/images/load.gif"
+ , method: 'get'
};
stopListener();
// if there is a afterStopListener callback we call it
if (settings.end) settings.end.call();
};
-
+
var loaderHtml = function () {
return settings.loaderHtml || '\
<div id="pageless-loader" style="display:none;text-align:center;width:100%;">\
<div class="msg" style="color:#e9e9e9;font-size:2em"></div>\
<img src="' + settings.loaderImage + '" alt="loading more results" style="margin:10px auto" />\
</div>';
};
-
+
// settings params: totalPages
var init = function (opts) {
if (settings.inited) return;
settings.inited = TRUE;
-
+
if (opts) $.extend(settings, opts);
-
+
container = settings.container;
$container = $(container);
-
+
// for accessibility we can keep pagination links
- // but since we have javascript enabled we remove pagination links
+ // but since we have javascript enabled we remove pagination links
if(settings.pagination) $(settings.pagination).remove();
-
+
// start the listener
startListener();
};
-
+
$.fn.pageless = function (opts) {
var $el = $(this)
, $loader = $(opts.loader, $el);
-
+
init(opts);
element = $el;
-
+
// loader element
if (opts.loader && $loader.length) {
loader = $loader;
@@ -132,36 +135,36 @@
}
}
};
-
+
//
var loading = function (bool) {
(isLoading = bool)
? (loader && loader.fadeIn('normal'))
: (loader && loader.fadeOut('normal'));
};
-
+
// distance to end of the container
var distanceToBottom = function () {
return (container === window)
- ? $(document).height()
- - $container.scrollTop()
+ ? $(document).height()
+ - $container.scrollTop()
- $container.height()
- : $container[0].scrollHeight
- - $container.scrollTop()
+ : $container[0].scrollHeight
+ - $container.scrollTop()
- $container.height();
};
var stopListener = function() {
$container.unbind(namespace);
};
-
+
// * bind a scroll event
// * trigger is once in case of reload
var startListener = function() {
$container.bind(SCROLL+' '+RESIZE, watch)
.trigger(SCROLL);
};
-
+
var watch = function() {
// listener was stopped or we've run out of pages
if (settings.totalPages <= settings.currentPage) {
@@ -170,7 +173,7 @@
if (settings.end) settings.end.call();
return;
}
-
+
// if slider past our scroll offset, then fire a request for more data
if(!isLoading && (distanceToBottom() < settings.distance)) {
loading(TRUE);
@@ -180,15 +183,26 @@
$.extend( settings.params
, { page: settings.currentPage });
// finally ajax query
- $.get( settings.url
- , settings.params
- , function (data) {
- $.isFunction(settings.scrape) ? settings.scrape(data) : data;
- loader ? loader.before(data) : element.append(data);
- loading(FALSE);
+ if (settings.method == 'post') {
+ $.post( settings.url
+ , settings.params
+ , function (data) {
+ $.isFunction(settings.scrape) ? settings.scrape(data) : data;
+ loader ? loader.before(data) : element.append(data);
+ loading(FALSE);
// if there is a complete callback we call it
+ if (settings.complete) settings.complete.call();
+ }, 'html');
+ } else {
+ $.get( settings.url
+ , settings.params
+ , function (data) {
+ $.isFunction(settings.scrape) ? settings.scrape(data) : data;
+ loader ? loader.before(data) : element.append(data);
+ loading(FALSE);
if (settings.complete) settings.complete.call();
}, 'html');
+ }
}
};
})(jQuery);
View
2 lib/jquery.pageless.min.js
@@ -1 +1 @@
-(function(a){var b=!1,c=!b,d,e=b,f,g=".pageless",h="scroll"+g,i="resize"+g,j={container:window,currentPage:1,distance:100,pagination:".pagination",params:{},url:location.href,loaderImage:"/images/load.gif"},k,l;a.pageless=function(b){a.isFunction(b)?j.call():n(b)};var m=function(){return j.loaderHtml||'\n<div id="pageless-loader" style="display:none;text-align:center;width:100%;">\n <div class="msg" style="color:#e9e9e9;font-size:2em"></div>\n <img src="'+j.loaderImage+'" alt="loading more results" style="margin:10px auto" />\n</div>'};var n=function(b){if(j.inited)return;j.inited=c;if(b)a.extend(j,b);k=j.container;l=a(k);if(j.pagination)a(j.pagination).remove();r()};a.fn.pageless=function(b){var c=a(this),e=a(b.loader,c);n(b);d=c;if(b.loader&&e.length){f=e}else{f=a(m());c.append(f);if(!b.loaderHtml){a("#pageless-loader .msg").html(b.loaderMsg).css(b.msgStyles||{})}}};var o=function(a){(e=a)?f&&f.fadeIn("normal"):f&&f.fadeOut("normal")};var p=function(){return k===window?a(document).height()-l.scrollTop()-l.height():l[0].scrollHeight-l.scrollTop()-l.height()};var q=function(){l.unbind(g)};var r=function(){l.bind(h+" "+i,s).trigger(h)};var s=function(){if(j.totalPages<=j.currentPage){q();if(j.end)j.end.call();return}if(!e&&p()<j.distance){o(c);j.currentPage++;a.extend(j.params,{page:j.currentPage});a.get(j.url,j.params,function(c){a.isFunction(j.scrape)?j.scrape(c):c;f?f.before(c):d.append(c);o(b);if(j.complete)j.complete.call()},"html")}}})(jQuery)
+(function(a){var b=!1,c=!b,d,e=b,f,g=".pageless",h="scroll"+g,i="resize"+g,j={container:window,currentPage:1,distance:100,pagination:".pagination",params:{},url:location.href,loaderImage:"/images/load.gif",method:"get"},k,l;a.pageless=function(b){a.isFunction(b)?b.call():n(b)};a.pagelessReset=function(){j={container:window,currentPage:1,distance:100,pagination:".pagination",params:{},url:location.href,loaderImage:"/images/load.gif",method:"get"};q();if(j.end)j.end.call()};var m=function(){return j.loaderHtml||'<div id="pageless-loader" style="display:none;text-align:center;width:100%;"> <div class="msg" style="color:#e9e9e9;font-size:2em"></div> <img src="'+j.loaderImage+'" alt="loading more results" style="margin:10px auto" /></div>'};var n=function(b){if(j.inited)return;j.inited=c;if(b)a.extend(j,b);k=j.container;l=a(k);if(j.pagination)a(j.pagination).remove();r()};a.fn.pageless=function(b){var c=a(this),e=a(b.loader,c);n(b);d=c;if(b.loader&&e.length){f=e}else{f=a(m());c.append(f);if(!b.loaderHtml){a("#pageless-loader .msg").html(b.loaderMsg).css(b.msgStyles||{})}}};var o=function(a){(e=a)?f&&f.fadeIn("normal"):f&&f.fadeOut("normal")};var p=function(){return k===window?a(document).height()-l.scrollTop()-l.height():l[0].scrollHeight-l.scrollTop()-l.height()};var q=function(){l.unbind(g)};var r=function(){l.bind(h+" "+i,s).trigger(h)};var s=function(){if(j.totalPages<=j.currentPage){q();if(j.end)j.end.call();return}if(!e&&p()<j.distance){o(c);j.currentPage++;a.extend(j.params,{page:j.currentPage});if(j.method=="post"){a.post(j.url,j.params,function(c){a.isFunction(j.scrape)?j.scrape(c):c;f?f.before(c):d.append(c);o(b);if(j.complete)j.complete.call()},"html")}else{a.get(j.url,j.params,function(c){a.isFunction(j.scrape)?j.scrape(c):c;f?f.before(c):d.append(c);o(b);if(j.complete)j.complete.call()},"html")}}}})(jQuery)

0 comments on commit 850f672

Please sign in to comment.
Something went wrong with that request. Please try again.