aboutsummaryrefslogtreecommitdiffstats
path: root/app/assets/javascripts/admin/category-order.js
blob: 3be82e46f5b0ffc6d7fec1cdb3b09b1e600c1e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$(function() {
  $('.save-order').each(function(index){

    // identify the elements that will work together
    var save_button = $(this);
    var save_notice = save_button.next();
    var save_panel = save_button.parent();
    var list_element = $(save_button.data('list-id'));
    var endpoint = save_button.data('endpoint');

    // on the first list change, show that there are unsaved changes
    list_element.sortable({
        update: function (event, ui) {
          if (save_button.is('.disabled')){
            save_button.removeClass("disabled");
            save_notice.html(save_notice.data('unsaved-text'));
            save_panel.effect('highlight', {}, 2000);
          }
        }
    });
    // on save, POST to endpoint
    save_button.click(function(){
      if (!save_button.is('.disabled')){
        var data = list_element.sortable('serialize', {'attribute': 'data-id'});
        var update_call = $.ajax({ data: data, type: 'POST', url: endpoint });

        // on success, disable the save button again, and show success notice
        update_call.done(function(msg) {
          save_button.addClass('disabled');
          save_panel.effect('highlight', {}, 2000);
          save_notice.html(save_notice.data('success-text'));
        })
        // on failure, show error message
        update_call.fail(function(jqXHR, msg) {
          save_panel.effect('highlight', {'color': '#cc0000'}, 2000);
          save_notice.html(save_notice.data('error-text') + jqXHR.responseText);
        });
      }
      return false;
    })
  });
});