paginer.js 6.58 KB
// paginer[id] ma format: [typ, parametry, akcja]
// parametry mają format:
// {'columns': field_list,'order_by': field, 'filters': filter_list}
// filtry mają format [field, lookup, value]

var paginer = {};
var page_numbers = {};
paginer.page_numbers = page_numbers;

function get_page_number(id) {
  "use strict";
  if (!page_numbers[id])
    return 1;
  return page_numbers[id];
}
paginer.get_page_number = get_page_number;

function get_list_id(elem) {
  "use strict";
  return elem.id.split('-')[0];
}

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

var perform_group_action = function() {
  "use strict";
  var container, id, action, ajax_url, row_ids;
  container = $(this).parents('div.paginer')[0];
  id = container.id;
  action = this.id.replace(id + '-', '');
  ajax_url = window['ajax_' + action];
  row_ids = $('.checkrow:checked', container).map(function() {
    return this.id.replace(id + '-', '');
  }).get();
  if (row_ids.length === 0)
      common.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);
};

var update_filters = function() {
  "use strict";
  var list_id = get_list_id(this);
  var params = $dj.paginer[list_id][1];
  params.filters = $.extend([], $dj.original_paginer[list_id][1].filters);
  $('#' + list_id + '-filter_panel').find('li').each(function() {
    var field_name = $(this).find('.field-choice').val();
    var lookup = $(this).find('.lookup-choice').val();
    var value = $(this).find('.value').value();
    params.filters.push([field_name, lookup, value]);
  });
  change_page(list_id, 1);
};

var update_order = function() {
  "use strict";
  var sort_choices = this.parentNode;
  var list_id = get_list_id(this);
  var sign = $('.sort-direction', this).text() > 0? '' : '-';
  var field_name = $('.sort-field-name', this).text();
  $dj.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) {
  "use strict";
  var sort_choices = $('#' + list_id + '-sort_choices');
  var current_choice, icon;
  var order_field = $dj.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]);
}
paginer.show_order = show_order;

function reload_panel(list_id) {
  "use strict";
  var panel = $('#' + list_id + '-filter_panel');
  var filters =  $dj.paginer[list_id][1].filters;
  if (panel.length === 0)
    return;
  $.each(filters, function(i, filter) {
    if (i >= $dj.original_paginer[list_id][1].filters.length) {
      var field_name = filter[0], lookup = filter[1], value = filter[2];
      new_filter_row(list_id);
      var row = panel.find('li').last();
      row.find('.field-choice').val(field_name).change();
      row.find('.lookup-choice').val(lookup);
      var value_field = row.find('.value');
      if (value_field.is(':checkbox')) {
        if (value)
          this.checked = 'checked';
        else
          this.checked = '';
      } else {
        value_field.val(value);
      }
    }
  });
}
paginer.reload_panel = reload_panel;

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

paginer.bind_events = {};
function paginer_ajax_callback(data, id) {
  "use strict";
  paginer_bind_events(id);
  var list_callback = paginer.bind_events[$dj.paginer[id][0]];
  if ($.isFunction(list_callback))
    list_callback(id);
}

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

function update_list(id, protect_hash) {
  "use strict";
  change_page(id, get_page_number(id), protect_hash);
}
paginer.update_list = update_list;

var filter_row_counter = 1; // dla niepowtarzalności idów
function new_filter_row_html(list_id) {
  "use strict";
  var new_row_html = $.ajax({
    type: 'get',
    url: $dj.ajax_new_filter_row,
    data: {list_type: $dj.paginer[list_id][0]},
    async: false
  }).responseText.replace(/NUM/g, filter_row_counter);
  filter_row_counter++;
  return new_row_html;
}

paginer.init_widget = {};
var update_filter_row = function() {
  "use strict";
  var num = this.id.split('-')[0].split('_')[2];
  var row = $(this).closest('li');
  var list_id = get_list_id($(this).closest('.filter-panel')[0]);
  var list_type = $dj.paginer[list_id][0];
  var field_name = $(this).val();
  row.find('.dynamic-field').remove();
  var dynamic_field_html = $.ajax({
    type: 'get',
    url: $dj.ajax_dynamic_field,
    data: {list_type: list_type, field_name: field_name},
    async: false
  }).responseText.replace(/NUM/g, num);
  $(dynamic_field_html).insertAfter(this);
  var init_widget = paginer.init_widget[list_type][field_name];
  if ($.isFunction(init_widget)) {
    init_widget(row);
  }
};

function new_filter_row(list_id) {
  "use strict";
  var row_html = new_filter_row_html(list_id);
  var filter_li = $('<li/>');
  filter_li.append($(row_html));
  $('#' + list_id + '-filter_panel').append(filter_li);
  filter_li.find('.field-choice').change();
}

var new_filter_click = function() {
  "use strict";
  new_filter_row(get_list_id(this));
};

$(function() {
  "use strict";
  $('div.paginer a.link').click(paginer_link_click);
  $('.group-action').click(perform_group_action);
  $('.filter-button').click(update_filters);
  $('.sort-choices .sort-choice').click(update_order);
  $('.add-filter-row').click(new_filter_click);
  $(document).on('change', '.field-choice', update_filter_row);
  $(document).on('click', '.delete-filter', function() {
    $(this).closest('li').remove();
  });
  $(window).trigger('hashchange');
});