wysiwyg_button_order.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // $Id$
  2. /**
  3. * @file
  4. * Syncronizes the buttons checkboxes with the button order field.
  5. * Adds separators, removes buttons.
  6. */
  7. (function ($) {
  8. Drupal.behaviors.WysiwygButtonOrder = {
  9. attach: function(context) {
  10. // add a button to add separators
  11. $('#buttonorder').parent().once().prepend(
  12. '<input type="button" class="form-submit" id="add-separator" value="' + Drupal.t('Add separator') + '" />'
  13. );
  14. $('#add-separator').once().click(function(e) {
  15. e.preventDefault();
  16. addDraggableRow('separator','---------------',Drupal.t('Separator'));
  17. return false;
  18. });
  19. // add events to remove links
  20. $('.removelink').once().click(function(e) {
  21. e.preventDefault();
  22. var split = $(this).parent().parent().attr('id').split('-');
  23. var group = $(this).parent().prev().prev().text();
  24. var name = split[1];
  25. if (name != 'separator') unCheckButton(name,group);
  26. removeDraggableRow($(this).parent().parent().attr('id'));
  27. return false;
  28. });
  29. // add events to sync button fieldset with order fieldset
  30. $('fieldset:nth-child(2) input.form-checkbox').once().click(function() {
  31. var split = $(this).attr('name').split('][');
  32. if (this.checked){
  33. addDraggableRow(split[1].slice(0,-1),split[0].substr(8),$(this).parent().text());
  34. } else {
  35. removeDraggableRow('order-' + split[1].slice(0,-1));
  36. }
  37. });
  38. /**
  39. * Creates draggable row for button
  40. * @args shortname: the short name of the button
  41. * @args group: the group name of the button belongs in
  42. */
  43. function addDraggableRow(name,group,fullname) {
  44. if ($('#buttonorder').length > 0) {
  45. // make a full row
  46. var row = createDraggableRow(name,group,fullname);
  47. // add row to DOM tree
  48. $('#buttonorder tbody').append(row);
  49. // make it draggable
  50. Drupal.tableDrag['buttonorder'].makeDraggable(row.get(0));
  51. }
  52. }
  53. /**
  54. * Removes draggable row for button (shortname)
  55. */
  56. function removeDraggableRow(id) {
  57. $('#'+id).remove();
  58. }
  59. // amount of separators
  60. var seps = 0;
  61. $('.separator').each( function() {
  62. seps++;
  63. });
  64. /**
  65. * Creates a draggable row ready for insertion
  66. * @return DOM element = filled row with attached events
  67. */
  68. function createDraggableRow(name,group,fullname){
  69. // if the name is a separator, make it unique
  70. var sepname = (name=='separator')? (name + '-' + ++seps) : '';
  71. // create the row(
  72. var row = $('<tr/>').addClass('draggable ' + ((sepname == '')? '' : 'separator ') + 'odd').attr('id','order-'+((sepname == '')? name : sepname));
  73. // append collumns
  74. $(row).append('<td>'+fullname+'</td>');
  75. $(row).append('<td><em>'+group+'</em></td>');
  76. $(row).append('<td class="tabledrag-hide" style="display: none;"></td>');
  77. $(row).append('<td><a href="#">Remove</a></td>');
  78. // Add "changed" to first collumn.
  79. $(row).find('td:first-child').append('<span class="warning tabledrag-changed">*</span>');
  80. // Add hidden weight component.
  81. $(row).find('td:nth-child(3)').append('<input type="hidden" name="buttonorder['+((sepname == '')? name : sepname)+'][weight]" id="edit-buttonorder-'+name+'-weight" value="1" class="buttons-weight">');
  82. // add remove link to last element, include events
  83. $(row).find('td:last a').addClass('removelink').click(function(e) {
  84. e.preventDefault();
  85. // check again if it's a separator
  86. if(sepname == '') {
  87. unCheckButton(name,group);
  88. removeDraggableRow('order-'+name);//tabledrag-hide
  89. }
  90. else {
  91. removeDraggableRow('order-'+sepname);
  92. }
  93. return false;
  94. });
  95. // return the row
  96. return row;
  97. }
  98. /**
  99. * Uncheck button in buttons fieldset
  100. */
  101. function unCheckButton(name,group){
  102. $('#edit-buttons-'+group+'-'+name).attr('checked',false);
  103. }
  104. }
  105. };
  106. }(jQuery));