export.js 5.12 KB
// straszna i zła copypasta
function selected_qualifiers_text(num, total, checked) {
  "use strict";
  if (num >= 4)
    return num + ' kwalifikatorów';
  var grouped = {};
  $.each(checked, function(i, elem) {
    var id = elem.value;
    var vocab = $dj.vocabs[id];
    if (grouped[vocab] === undefined)
      grouped[vocab] = [];
    grouped[vocab].push(elem.title);
  });
  var vocab_table = [];
  var owner = $('#owner-vocabulary').text();
  if (grouped[owner])
    vocab_table.push(owner + ': ' + grouped[owner].join(', '));
  $.each(grouped, function(vocab, qualifiers) {
    if (vocab !== owner)
      vocab_table.push(vocab + ': ' + qualifiers.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() {
  "use strict";
  var new_row_html = $.ajax({
    type: 'get',
    url: $dj.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() {
  "use strict";
  var new_row = $(get_new_qualifier_row_html());
  $('#magic-qualifiers').append(new_row);
  new_row.find('button').button();
  return new_row.prop('id');
}

var remove_qualifier_row = function() {
  "use strict";
  $(this).closest('li').remove();
};

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

function put_data(json) {
  "use strict";
  var data = $.parseJSON(json),
      qualifier_rows = {}, vocabs = [], antivocabs = [], parts;
  $('#id_refl').attr('checked', false);
  $('#id_commonness').attr('checked', false);
  $('#magic-qualifiers li').remove();
  $.each(data, function(i, item) {
    if (item.name === 'vocabs') {
      vocabs.push(item.value);
    }
    if (item.name === 'antivocabs') {
      antivocabs.push(item.value);
    }
    if (item.name === 'variant') {
      $('#id_variant').val(item.value);
    }
    if (item.name === 'refl' || item.name === 'commonness') {
      $('#id_' + item.name).attr('checked', true);
    }
    if (item.name.indexOf('magic') === 0) {
      parts = item.name.split('-');
      if (!qualifier_rows[parts[0]]) {
        qualifier_rows[parts[0]] = {};
      }
      qualifier_rows[parts[0]][parts[1]] = item.value;
    }
  });
  $('#id_vocabs').val(vocabs).multiselect2("refresh");
  $('#id_antivocabs').val(antivocabs).multiselect2("refresh");
  $.each(qualifier_rows, function(id, item) {
    var row_id = add_qualifier_row();
    $('#id_' + row_id + '-qualifier').val(item.qualifier);
    $('#id_' + row_id + '-regex').val(item.regex);
  });
}

function export_choice(ajax_data) {
  "use strict";
  var exports = ajax_data.data_list,
      list = $('#data-list');
  list.html('');
  $.each(exports, function(i, data) {
    var item = $('<li/>').addClass('ui-state-default').attr('id', data.id);
    var delete_button = $('<span/>')
      .addClass('remove ui-icon ui-icon-closethick');
    delete_button.click(function() {
      if (window.confirm('Usunąć eksport "' + data.name + '"?')) {
        $.ajaxJSON({
          method: 'post',
          url: $dj.ajax_delete_export_data,
          data: {data_id: data.id},
          description: 'Usunięcie ustawień eksportu',
          callback: function() {
            item.remove();
          }
        });
      }
      return false;
    });
    item.append(delete_button);
    item.append(data.name);
    item.click(function() {
      put_data(data.json);
      $("#load-data-dialog").dialog('close');
    });
    list.append(item);
  });
  $("#load-data-dialog").dialog('open');
}

function load_export_data() {
  "use strict";
  $.ajaxJSON({
    method: 'get',
    url: $dj.ajax_get_export_data,
    data: {},
    description: 'Pobranie zapisanych eksportów',
    callback: export_choice
  });
  return false;
}

$(function() {
  "use strict";
  var opts = {
    noneSelectedText: 'Wybierz słowniki',
    selectedText: '# słowników',
    selectedList: 4,
    header: false
  };
  $('#id_vocabs').multiselect2(opts);
  $('#id_antivocabs').multiselect2(opts);
  $('#id_excluding_qualifiers').multiselect2(qualifier_options);
  $('#add-magic-qualifier-row').click(add_qualifier_row);
  $('button.save').click(save_export_data);
  $('button.load').click(load_export_data);
  $("#load-data-dialog").dialog({
    autoOpen: false,
    height: 'auto',
    width: 'auto',
    modal: true
  });
  $(document).on('click', '#magic-qualifiers .remove', remove_qualifier_row);
});