paginer.js 4.94 KB
/*
paginer - mapa paginowanych list na stronie, indeksowana idami
listę reprezentuje obiekt z polami:
 * type - typ listy
 * order_by - nazwa pola po którym jest porządek
 * filters - słownik/obiekt odpowiadający (mniej więcej) parametrom filter
 * lista kolumnn - potrzebna dla ajaxa (nie zmienia się)
*/

// paginer[id] ma format: [typ, parametry, akcja]
// parametry mają format: {'columns': field_list,'order_by': field, 'filters': dict}
// słownik filtrów ma format mniej więcej taki jak argumenty QuerySet.filter()
// (konkretne pola zależą od typu listy)

var page_numbers = {};

function get_page_number(id) {
  if (!page_numbers[id])
    return 1;
  return page_numbers[id];
}

function paginer_link_click() {
  change_page($(this).parents('div.paginer')[0].id,
              $('span.page_nr', this).text());
}

function bind_category_walk() {
    var container = $(this).parents('div.category-walk');
    var list_id = container.attr('id').replace('_category_walk', '');
    var category_id = $('.category-id', this).text();
    reload_category(list_id, category_id);
}

function apply_filter() {
  var list_id = this.id.replace('_filter_list', '');
  //$.each(this.elements, function() {
  //});
  change_page(list_id, 1);
  return false;
}

function perform_group_action() {
  var container = $(this).parents('div.paginer')[0];
  var id = container.id;
  var action = this.id.replace(id + '_', '');
  var ajax_url = window['ajax_' + action];
  var row_ids = $('.checkrow:checked', container).map(function() {
    return this.id.replace(id + '_', '');
  }).get();
  if (row_ids.length == 0)
    error_alert(_("Niczego nie zaznaczono"));
  $.each(row_ids, function(i, row_id) {
    $.ajaxJSON({
      method: 'post',
      url: ajax_url,
      data: {row_id: row_id},
    });
  });
  update_list(id);
}

function update_filters() {
  var list_id = this.id.replace('_filter-button', ''), val;
  $.each(this.form.elements, function() {
    if (this.name != '') {
      val = $(this).value();
      if (val !== null) {
        if (val !== '')
          paginer[list_id][1].filters[this.name] = val;
        else
          delete paginer[list_id][1].filters[this.name];
      }
    }
  });
  change_page(list_id, 1);
}

function update_order() {
  var sort_choices = this.parentNode;
  var list_id = sort_choices.id.replace('_sort-choices', '');
  var sign = $('.sort-direction', this).text() > 0? '' : '-';
  var field_name = $('.sort-field-name', this).text();
  paginer[list_id][1].order_by = sign + field_name;
  change_page(list_id, 1);
  $('.sort-direction', this).text(-(sign + '1'));
  show_order(list_id);
}

function show_order(list_id) {
  var sort_choices = $('#' + list_id + '_sort-choices');
  var current_choice, icon;
  var order_field = paginer[list_id][1].order_by;
  var icon_classes = ['ui-icon-arrow-1-s', 'ui-icon-arrow-1-n'];
  var sign = order_field[0] == '-';
  if (sign)
    order_field = order_field.substr(1);
  current_choice = $('#' + list_id + '_' + order_field + '_sort-choice');
  $('.current', sort_choices).removeClass('current');
  current_choice.addClass('current');
  if (sign)
    icon_classes.reverse();
  icon = $('.sort-icon', current_choice);
  icon.removeClass(icon_classes[0]).addClass(icon_classes[1]);
}

function reload_panel(list_id) {
  var panel = $('#' + list_id + '_filter-panel');
  var filters =  paginer[list_id][1].filters;
  if (panel.length == 0)
    return;
  $.each(panel[0].elements, function() {
    if (this.name) {
      if ($(this).is(':checkbox')) {
        if (filters[this.name])
          this.checked = 'checked';
        else
          this.checked = '';
      } else {
        if (filters[this.name] !== undefined)
          $(this).val(filters[this.name]); // źle dla radio
        else
          $(this).val('');
      }
    }
  });
}

$(function() {
  $('div.paginer a.link').click(paginer_link_click);
  $('div.category-walk a').click(bind_category_walk);
  $('.group-action').click(perform_group_action);
  $('.filter-button').click(update_filters);
  $('.sort-choices .sort-choice').click(update_order);
});

function paginer_bind_events(id) {
  $('div#'+id+' a.link').click(paginer_link_click);
  $('div#'+id+' .group-action').click(perform_group_action);
}

function paginer_ajax_callback(data, id) {
  paginer_bind_events(id);
  list_callback = paginer[id][0]+'_bind_events'; // trochę brzydkie, ale upraszcza
  if (window[list_callback])
    window[list_callback](id);
}

function change_page(id, nr, protect_hash) {
  param = {
    id: id,
    type: paginer[id][0],
    params: paginer[id][1],
    page_nr: nr,
    action: paginer[id][2],
    group_actions: paginer[id][3],
  }
  $.ajaxJSON({
    method: 'post',
    url: ajax_paginer,
    data: param,
    description: _("Pobranie strony"),
    dest: $('#'+id+'.paginer'),
    callback: paginer_ajax_callback,
    callback_args: [id],
  });
  page_numbers[id] = nr;
  if (!protect_hash)
    update_hash();
}

function update_list(id, protect_hash) {
  change_page(id, get_page_number(id), protect_hash);
}