termData.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @file js support for term editing form for ajax saving and tree updating
  3. */
  4. (function ($) {
  5. //global var that holds the current term link object
  6. var active_term = new Object();
  7. /**
  8. * attaches term data form, used after 'Saves changes' submit
  9. */
  10. Drupal.behaviors.TaxonomyManagerTermData = {
  11. attach: function(context) {
  12. if (!$('#taxonomy-term-data-replace').hasClass('processed')) {
  13. $('#taxonomy-term-data-replace').addClass('processed');
  14. Drupal.attachTermDataForm();
  15. }
  16. }
  17. }
  18. /**
  19. * attaches Term Data functionality, called by tree.js
  20. */
  21. Drupal.attachTermData = function(ul) {
  22. Drupal.attachTermDataLinks(ul);
  23. }
  24. /**
  25. * adds click events to the term links in the tree structure
  26. */
  27. Drupal.attachTermDataLinks = function(ul) {
  28. $(ul).find('a.term-data-link').click(function() {
  29. Drupal.activeTermSwapHighlight(this);
  30. var li = $(this).parents("li:first");
  31. Drupal.loadTermDataForm(Drupal.getTermId(li), false);
  32. return false;
  33. });
  34. }
  35. /**
  36. * attaches click events to next siblings
  37. */
  38. Drupal.attachTermDataToSiblings = function(all, currentIndex) {
  39. var nextSiblings = $(all).slice(currentIndex);
  40. $(nextSiblings).find('a.term-data-link').click(function() {
  41. var li = $(this).parents("li:first");
  42. Drupal.loadTermDataForm(Drupal.getTermId(li), false);
  43. return false;
  44. });
  45. }
  46. /**
  47. * adds click events to term data form, which is already open, when page gets loaded
  48. */
  49. Drupal.attachTermDataForm = function() {
  50. active_term = $('div.highlightActiveTerm').find('a');
  51. var tid = $('#taxonomy-term-data').find('input:hidden[name="tid"]').val();
  52. if (tid) {
  53. new Drupal.TermData(tid).form();
  54. }
  55. }
  56. /**
  57. * loads term data form
  58. */
  59. Drupal.loadTermDataForm = function(tid, refreshTree) {
  60. // Triggers an AJAX button
  61. $('#edit-load-tid').val(tid);
  62. if (refreshTree) {
  63. $('#edit-load-tid-refresh-tree').attr("checked", "checked");
  64. }
  65. else {
  66. $('#edit-load-tid-refresh-tree').attr("checked", "");
  67. }
  68. $('#edit-load-tid-submit').click();
  69. }
  70. /**
  71. * TermData Object
  72. */
  73. Drupal.TermData = function(tid) {
  74. this.tid = tid;
  75. this.div = $('#taxonomy-term-data');
  76. }
  77. /**
  78. * adds events to possible operations
  79. */
  80. Drupal.TermData.prototype.form = function() {
  81. var termdata = this;
  82. $(this.div).find('#term-data-close span').click(function() {
  83. termdata.div.children().hide();
  84. });
  85. $(this.div).find('a.taxonomy-term-data-name-link').click(function() {
  86. var tid = this.href.split("/").pop();
  87. Drupal.loadTermDataForm(tid, true);
  88. return false;
  89. });
  90. $(this.div).find("legend").each(function() {
  91. var staticOffsetX, staticOffsetY = null;
  92. var left, top = 0;
  93. var div = termdata.div;
  94. var pos = $(div).position();
  95. $(this).mousedown(startDrag);
  96. function startDrag(e) {
  97. if (staticOffsetX == null && staticOffsetY == null) {
  98. staticOffsetX = e.pageX;
  99. staticOffsetY = e.pageY;
  100. }
  101. $(document).mousemove(performDrag).mouseup(endDrag);
  102. return false;
  103. }
  104. function performDrag(e) {
  105. left = e.pageX - staticOffsetX;
  106. top = e.pageY - staticOffsetY;
  107. $(div).css({position: "absolute", "left": pos.left + left +"px", "top": pos.top + top +"px"});
  108. return false;
  109. }
  110. function endDrag(e) {
  111. $(document).unbind("mousemove", performDrag).unbind("mouseup", endDrag);
  112. }
  113. });
  114. }
  115. /**
  116. * hightlights current term
  117. */
  118. Drupal.activeTermSwapHighlight = function(link) {
  119. try {
  120. $(active_term).parents('div.term-line').removeClass('highlightActiveTerm');
  121. } catch(e) {}
  122. active_term = link;
  123. $(active_term).parents('div.term-line:first').addClass('highlightActiveTerm');
  124. }
  125. })(jQuery);