export.js 2.44 KB
// straszna i zła copypasta
function selected_qualifiers_text(num, total, checked) {
  if (num >= 4)
    return num + ' kwalifikatorów';
  grouped = {};
  $.each(checked, function(i, elem) {
    id = elem.value;
    vocab = vocabs[id];
    if (grouped[vocab] === undefined)
      grouped[vocab] = [];
    grouped[vocab].push(elem.title);
  });
  vocab_table = [];
  owner = $('#owner-vocabulary').text();
  if (grouped[owner])
    vocab_table.push(owner + ': ' + grouped[owner].join(', '));
  for (vocab in grouped) {
    if (vocab != owner)
      vocab_table.push(vocab + ': ' + grouped[vocab].join(', '));
  }
  return vocab_table.join('; ');
}

var qualifier_options = {
  noneSelectedText: 'Wybierz',
  selectedText: selected_qualifiers_text,
  selectedList: 4,
  header: false,
};

var new_row_counter = 1;
function get_new_qualifier_row_html() {
  var new_row_html = $.ajax({
    type: 'get',
    url: ajax_new_qualifier_row,
    async: false
  }).responseText;
  var row_html = new_row_html.replace(/NUM/g, new_row_counter);
  new_row_counter++;
  return row_html;
}

function add_qualifier_row() {
  var new_row = $(get_new_qualifier_row_html());
  $('#magic-qualifiers').append(new_row);
  new_row.find('button').button();
}

function remove_qualifier_row() {
  $(this).closest('li').remove();
}

function save_export_data() {
  var name, post_data;
  name = prompt('Wybierz nazwę')
  if(name) {
    post_data = {
      name: name,
      serialized_data: $.toJSON($('#export-form').serializeArray())
    };
    var save_data = {
      method: 'post',
      url: ajax_save_export_data,
      data: post_data,
      description: 'Zapisanie ustawień'
    }
    $.ajaxJSON($.extend(save_data, {
      callback: function(data) {
        if (data.exists && confirm('Ta nazwa jest zajęta. Nadpisać?')) {
          $.ajaxJSON($.extend(save_data, {
            data: $.extend(post_data, {force: true})
          }));
        }
      }
    }));
  }
  return false;
}

$(function() {
  var opts = {
    noneSelectedText: 'Wybierz słowniki',
    selectedText: '# słowników',
    selectedList: 4,
    header: false
  };
  $('#id_vocabs').multiselect(opts);
  $('#id_antivocabs').multiselect(opts);
  $('#id_excluding_qualifiers').multiselect(qualifier_options);
  $('#add-magic-qualifier-row').click(add_qualifier_row);
  $('button.save').click(save_export_data);
  $('button.load').click(load_export_data);
  $(document).on('click', '#magic-qualifiers .remove', remove_qualifier_row);
})