updateWeight.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @file js for changing weights of terms with Up and Down arrows
  3. */
  4. (function ($) {
  5. //object to store weights (tid => weight)
  6. var termWeightsData = new Object();
  7. Drupal.behaviors.TaxonomyManagerWeights = {
  8. attach: function(context, settings) {
  9. var weightSettings = settings.updateWeight || [];
  10. if (!$('#edit-toolbar.tm-weights-processed').length) {
  11. $('#edit-toolbar').addClass('tm-weights-processed');
  12. termWeightsData['form_token'] = $('input[name=form_token]').val();
  13. termWeightsData['form_id'] = $('input[name=form_id]').val();
  14. termWeightsData['weights'] = new Object();
  15. Drupal.attachUpdateWeightToolbar(weightSettings['up'], weightSettings['down']);
  16. Drupal.attachUpdateWeightTerms();
  17. }
  18. }
  19. }
  20. /**
  21. * adds click events for Up and Down buttons in the toolbar, which
  22. * allow the moving of selected (can be more) terms
  23. */
  24. Drupal.attachUpdateWeightToolbar = function(upButton, downButton) {
  25. var selected;
  26. var url = Drupal.settings.updateWeight['url'];
  27. $('#'+ upButton).click(function() {
  28. selected = Drupal.getSelectedTerms();
  29. for (var i=0; i < selected.length; i++) {
  30. var upTerm = selected[i];
  31. var downTerm = $(upTerm).prev();
  32. Drupal.orderTerms(upTerm, downTerm);
  33. }
  34. if (selected.length > 0) {
  35. $.post(url, termWeightsData);
  36. }
  37. });
  38. $('#'+ downButton).click(function() {
  39. selected = Drupal.getSelectedTerms();
  40. for (var i=selected.length-1; i >= 0; i--) {
  41. var downTerm = selected[i];
  42. var upTerm = $(downTerm).next();
  43. Drupal.orderTerms(upTerm, downTerm);
  44. }
  45. if (selected.length > 0) {
  46. $.post(url, termWeightsData);
  47. }
  48. });
  49. }
  50. /**
  51. * adds small up and down arrows to each term
  52. * arrows get displayed on mouseover
  53. */
  54. Drupal.attachUpdateWeightTerms = function(parent, currentIndex) {
  55. var settings = Drupal.settings.updateWeight || [];
  56. var disable = settings['disable_mouseover'];
  57. if (!disable) {
  58. var url = Drupal.settings.updateWeight['url'];
  59. var termLineClass = 'div.term-line';
  60. var termUpClass = 'img.term-up';
  61. var termDownClass = 'img.term-down';
  62. if (parent && currentIndex) {
  63. parent = $(parent).slice(currentIndex);
  64. }
  65. if (parent) {
  66. termLineClass = $(parent).find(termLineClass);
  67. termUpClass = $(parent).find(termUpClass);
  68. termDownClass = $(parent).find(termDownClass);
  69. }
  70. $(termLineClass).mouseover(function() {
  71. $(this).find('div.term-operations').show();
  72. });
  73. $(termLineClass).mouseout(function() {
  74. $(this).find('div.term-operations').hide();
  75. });
  76. $(termUpClass).click(function() {
  77. var upTerm = $(this).parents("li").eq(0);
  78. var downTerm = $(upTerm).prev();
  79. Drupal.orderTerms(upTerm, downTerm);
  80. $.post(url, termWeightsData);
  81. $(downTerm).find(termLineClass).unbind('mouseover');
  82. setTimeout(function() {
  83. $(upTerm).find('div.term-operations').hide();
  84. $(downTerm).find(termLineClass).mouseover(function() {
  85. $(this).find('div.term-operations').show();
  86. });
  87. }, 1500);
  88. });
  89. $(termDownClass).click(function() {
  90. var downTerm = $(this).parents("li").eq(0);
  91. var upTerm = $(downTerm).next();
  92. Drupal.orderTerms(upTerm, downTerm);
  93. $.post(url, termWeightsData);
  94. $(upTerm).find(termLineClass).unbind('mouseover');
  95. setTimeout(function() {
  96. $(downTerm).find('div.term-operations').hide();
  97. $(upTerm).find(termLineClass).mouseover(function() {
  98. $(this).find('div.term-operations').show();
  99. });
  100. }, 1500);
  101. });
  102. }
  103. }
  104. /**
  105. * return array of selected terms
  106. */
  107. Drupal.getSelectedTerms = function() {
  108. var terms = new Array();
  109. $('.treeview').find("input:checked").each(function() {
  110. var term = $(this).parents("li").eq(0);
  111. terms.push(term);
  112. });
  113. return terms;
  114. }
  115. /**
  116. * reorders terms
  117. * - swap list elements in DOM
  118. * - post updated weights to callback in php
  119. * - update classes of tree view
  120. */
  121. Drupal.orderTerms = function(upTerm, downTerm) {
  122. try {
  123. Drupal.getTermId(upTerm);
  124. Drupal.swapTerms(upTerm, downTerm);
  125. Drupal.swapWeights(upTerm, downTerm);
  126. Drupal.updateTree(upTerm, downTerm);
  127. } catch(e) {
  128. //no next item, because term to update is last child, continue
  129. }
  130. }
  131. /**
  132. * simple swap of two elements
  133. */
  134. Drupal.swapTerms = function(upTerm, downTerm) {
  135. $(upTerm).after(downTerm);
  136. $(downTerm).before(upTerm);
  137. }
  138. /**
  139. * updating weights of swaped terms
  140. * if two terms have different weights, then weights are being swapped
  141. * else, if both have same weights, upTerm gets decreased
  142. *
  143. * if prev/next siblings of up/down terms have same weights as current
  144. * swapped, they have to be updated by de/increasing weight (by 1) to ensure
  145. * unique position of swapped terms
  146. */
  147. Drupal.swapWeights = function(upTerm, downTerm) {
  148. var upWeight = Drupal.getWeight(upTerm);
  149. var downWeight = Drupal.getWeight(downTerm);
  150. var downTid = Drupal.getTermId(downTerm);
  151. var upTid = Drupal.getTermId(upTerm);
  152. //same weight, decrease upTerm
  153. if (upWeight == downWeight) {
  154. termWeightsData['weights'][upTid] = --upWeight;
  155. }
  156. //different weights, swap
  157. else {
  158. termWeightsData['weights'][upTid] = downWeight;
  159. termWeightsData['weights'][downTid] = upWeight;
  160. }
  161. //update prev siblings if necessary
  162. try {
  163. if (Drupal.getWeight($(upTerm).prev()) >= upWeight) {
  164. $(upTerm).prevAll().each(function() {
  165. var id = Drupal.getTermId(this);
  166. var weight = Drupal.getWeight(this);
  167. termWeightsData['weights'][id] = --weight;
  168. });
  169. }
  170. } catch(e) {
  171. //no prev
  172. }
  173. //update next siblings if necessary
  174. try {
  175. if (Drupal.getWeight($(downTerm).next()) <= downWeight) {
  176. $(downTerm).nextAll().each(function() {
  177. var id = Drupal.getTermId(this);
  178. var weight = Drupal.getWeight(this);
  179. termWeightsData['weights'][id] = ++weight;
  180. });
  181. }
  182. } catch(e) {
  183. //no next
  184. }
  185. }
  186. /**
  187. * helper to return weight of a term
  188. */
  189. Drupal.getWeight = function(li) {
  190. var id = Drupal.getTermId(li);
  191. var weight;
  192. if (termWeightsData['weights'][id] != null) {
  193. weight = termWeightsData['weights'][id];
  194. }
  195. else {
  196. weight = $(li).find("input:hidden[class=weight-form]").attr("value");
  197. }
  198. return weight;
  199. }
  200. })(jQuery);