rails_admin_nestable-8a33a435af9785c0a2ada85923c461341379217ee2396f5d81ece4b60c5b956f.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. (function() {
  2. jQuery(function() {
  3. var $live_update, $tree_nodes, $tree_nodes_max_depth, $tree_nodes_options, $update_button, live_update_mode, updateNodes;
  4. updateNodes = function(tree_nodes) {
  5. var serialized_tree;
  6. serialized_tree = tree_nodes.nestable('serialize');
  7. return $.ajax({
  8. url: tree_nodes.data('update-path'),
  9. type: 'POST',
  10. data: {
  11. tree_nodes: serialized_tree
  12. },
  13. success: function(data) {
  14. var $flash;
  15. $flash = $('<div>').addClass('nestable-flash alert alert-success').append($('<button>').addClass('close').data('dismiss', 'alert').html('&times;')).append($('<span>').addClass('body').html(data));
  16. $('#rails_admin_nestable').append($flash);
  17. return $flash.fadeIn(200).delay(2000).fadeOut(200, function() {
  18. return $(this).remove();
  19. });
  20. }
  21. });
  22. };
  23. $tree_nodes = $('#tree_nodes');
  24. $tree_nodes_options = {};
  25. $tree_nodes_max_depth = $tree_nodes.data('max-depth');
  26. $live_update = $('#rails_admin_nestable input[type=checkbox]');
  27. $update_button = $('#rails_admin_nestable button');
  28. live_update_mode = !$live_update.length && !$update_button.length ? true : $live_update.prop('checked');
  29. $('#rails_admin_nestable button').prop('disabled', $live_update.prop('checked'));
  30. $live_update.change(function() {
  31. live_update_mode = $(this).prop('checked');
  32. return $update_button.prop('disabled', live_update_mode);
  33. });
  34. $update_button.click(function() {
  35. return updateNodes($tree_nodes);
  36. });
  37. if ($tree_nodes_max_depth && $tree_nodes_max_depth !== 'false') {
  38. $tree_nodes_options['maxDepth'] = $tree_nodes_max_depth;
  39. }
  40. return $tree_nodes.nestable($tree_nodes_options).on({
  41. change: function(event) {
  42. if (live_update_mode) {
  43. return updateNodes($tree_nodes);
  44. }
  45. }
  46. });
  47. });
  48. }).call(this);