manager-view.js 1.79 KB
$(function() {
  "use strict";
  $('#vocabulary-select').change(function() {
    $('#user-privileges').find('input[type=checkbox]').prop('checked', false);
    $.ajaxJSON({
      method: 'get',
      url: $dj.ajax_get_privileges,
      data: {name: $(this).value()},
      callback: function(data) {
        $.each(data.viewers, function(i, id) {
          $('#view-'+id).prop('checked', true);
        });
        $.each(data.editors, function(i, id) {
          $('#change-'+id).prop('checked', true);
        });
        $.each(data.managers, function(i, id) {
          $('#manage-'+id).prop('checked', true);
        });
      }
    });
  });
  $('#add-vocabulary').click(function() {
    var new_vocab = $('#new-vocabulary');
    var name = new_vocab.value();
    new_vocab.val('');
    $.ajaxJSON({
      method: 'post',
      url: $dj.ajax_add_vocabulary,
      data: {name: name},
      callback: function() {
        var option = $('<option/>').text(name).val(name);
        $('#vocabulary-select').append(option);
      }
    });
  });
  $('#user-privileges').find('input[type=checkbox]').change(function() {
    var $t = $(this),
      id = this.id.split('-')[1],
      type = this.id.split('-')[0],
      name = $('#vocabulary-select').value();
    $.ajaxJSON({
      method: 'post',
      url: $dj.ajax_set_privilege,
      data: {
        name: name,
        id: id,
        type: type,
        set: $t.is(':checked')
      }
    });
    var view_checkbox = $('#view-'+id), change_checkbox = $('#change-'+id);
    if (type === 'change' && $t.is(':checked') &&
        !view_checkbox.is(':checked')) {
      view_checkbox.prop('checked', true).change();
    }
    if (type === 'view' && ! $t.is(':checked') && change_checkbox.is(':checked')) {
      change_checkbox.prop('checked', false).change();
    }
  });
});