Advertisement
framp

Buddypress sorting type selection - per Pollycoke

May 27th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery(document).ready(function () {
  2.     function setCookie(name, value) {
  3.         var exp = new Date();
  4.         exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30));
  5.         document.cookie = name + "=" + escape(value) + "; path=/" + ((exp == null) ? "" : "; expires=" + exp.toGMTString());
  6.     }
  7.  
  8.     function getCookie(name) {
  9.         var dc = document.cookie;
  10.         var cname = name + "=";
  11.         if (dc.length > 0) {
  12.             begin = dc.indexOf(cname);
  13.             if (begin != -1) {
  14.                 begin += cname.length;
  15.                 end = dc.indexOf(";", begin);
  16.                 if (end == -1) end = dc.length;
  17.                 return unescape(dc.substring(begin, end));
  18.             }
  19.         }
  20.         return null;
  21.     }
  22.  
  23.     var select = jQuery('<form id="ordering" method="POST" action="">' +
  24.               'Ordina per: <select name="order">' +
  25.                 '<option value="date_recorded">data</option>' +
  26.                 '<option value="favourites">preferenze</option>' +
  27.                 '<option value="comments">commenti</option>' +
  28.               '</select><select name="sort">' +
  29.                 '<option value="DESC">▼</option>' +            
  30.                 '<option value="ASC">▲</option>' +
  31.               '</select>' +
  32.               '<input type="submit" value="✓" />' +
  33.             '</form>');
  34.  
  35.     //il tasto viene aggiunto a fianco a INFO e RSS
  36.     select.appendTo(jQuery('#content>div>ul:eq(1)'));
  37.    
  38.     var order = getCookie('order');
  39.     var sort = getCookie('sort');
  40.     if (order){
  41.       setCookie('order', order);
  42.       select.children('select:eq(0)').val(order);
  43.     }else{
  44.       setCookie('order', 'date_recorded');
  45.     }
  46.  
  47.     if (sort){
  48.       setCookie('sort', sort);
  49.       select.children('select:eq(1)').val(sort);
  50.     }else{
  51.       setCookie('sort', 'DESC');
  52.     }
  53.    
  54.     select.submit(function(){
  55.       jQuery(this).children('select').each(function(){
  56.     var theSelect = jQuery(this);
  57.     setCookie(theSelect.attr("name"), theSelect.val());
  58.       });
  59.       window.location.reload();
  60.       return false;
  61.     });
  62. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement