multiselect.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @name Multiselect Module
  3. * @description Select multiple albums or photos.
  4. * @author Tobias Reich
  5. * @copyright 2014 by Tobias Reich
  6. */
  7. multiselect = {
  8. position: {
  9. top: null,
  10. right: null,
  11. bottom: null,
  12. left: null
  13. },
  14. show: function(e) {
  15. if (mobileBrowser()) return false;
  16. if (lychee.publicMode) return false;
  17. if (visible.search()) return false;
  18. if (visible.infobox()) return false;
  19. if (!visible.albums()&&!visible.album) return false;
  20. if ($('.album:hover, .photo:hover').length!==0) return false;
  21. if (visible.multiselect()) $('#multiselect').remove();
  22. multiselect.position.top = e.pageY;
  23. multiselect.position.right = -1 * (e.pageX - $(document).width());
  24. multiselect.position.bottom = -1 * (multiselect.position.top - $(window).height());
  25. multiselect.position.left = e.pageX;
  26. $('body').append(build.multiselect(multiselect.position.top, multiselect.position.left));
  27. $(document).on('mousemove', multiselect.resize);
  28. },
  29. selectAll: function() {
  30. var e,
  31. newWidth,
  32. newHeight;
  33. if (mobileBrowser()) return false;
  34. if (lychee.publicMode) return false;
  35. if (visible.search()) return false;
  36. if (visible.infobox()) return false;
  37. if (!visible.albums()&&!visible.album) return false;
  38. if (visible.multiselect()) $('#multiselect').remove();
  39. multiselect.position.top = 70;
  40. multiselect.position.right = 40;
  41. multiselect.position.bottom = 90;
  42. multiselect.position.left = 20;
  43. $('body').append(build.multiselect(multiselect.position.top, multiselect.position.left));
  44. newWidth = $(document).width() - multiselect.position.right + 2;
  45. newHeight = $(document).height() - multiselect.position.bottom;
  46. $('#multiselect').css({
  47. width: newWidth,
  48. height: newHeight
  49. });
  50. e = {
  51. pageX: $(document).width() - (multiselect.position.right / 2),
  52. pageY: $(document).height() - multiselect.position.bottom
  53. };
  54. multiselect.getSelection(e);
  55. },
  56. resize: function(e) {
  57. var mouse_x = e.pageX,
  58. mouse_y = e.pageY,
  59. newHeight,
  60. newWidth;
  61. if (multiselect.position.top===null||
  62. multiselect.position.right===null||
  63. multiselect.position.bottom===null||
  64. multiselect.position.left===null) return false;
  65. if (mouse_y>=multiselect.position.top) {
  66. // Do not leave the screen
  67. newHeight = mouse_y - multiselect.position.top;
  68. if ((multiselect.position.top+newHeight)>=$(document).height())
  69. newHeight -= (multiselect.position.top + newHeight) - $(document).height() + 2;
  70. $('#multiselect').css({
  71. top: multiselect.position.top,
  72. bottom: 'inherit',
  73. height: newHeight
  74. });
  75. } else {
  76. $('#multiselect').css({
  77. top: 'inherit',
  78. bottom: multiselect.position.bottom,
  79. height: multiselect.position.top - e.pageY
  80. });
  81. }
  82. if (mouse_x>=multiselect.position.left) {
  83. // Do not leave the screen
  84. newWidth = mouse_x - multiselect.position.left;
  85. if ((multiselect.position.left+newWidth)>=$(document).width())
  86. newWidth -= (multiselect.position.left + newWidth) - $(document).width() + 2;
  87. $('#multiselect').css({
  88. right: 'inherit',
  89. left: multiselect.position.left,
  90. width: newWidth
  91. });
  92. } else {
  93. $('#multiselect').css({
  94. right: multiselect.position.right,
  95. left: 'inherit',
  96. width: multiselect.position.left - e.pageX
  97. });
  98. }
  99. },
  100. stopResize: function() {
  101. $(document).off('mousemove');
  102. },
  103. getSize: function() {
  104. if (!visible.multiselect()) return false;
  105. return {
  106. top: $('#multiselect').offset().top,
  107. left: $('#multiselect').offset().left,
  108. width: parseInt($('#multiselect').css('width').replace('px', '')),
  109. height: parseInt($('#multiselect').css('height').replace('px', ''))
  110. };
  111. },
  112. getSelection: function(e) {
  113. var tolerance = 150,
  114. id,
  115. ids = [],
  116. offset,
  117. size = multiselect.getSize();
  118. if (visible.contextMenu()) return false;
  119. if (!visible.multiselect()) return false;
  120. $('.photo, .album').each(function() {
  121. offset = $(this).offset();
  122. if (offset.top>=(size.top-tolerance)&&
  123. offset.left>=(size.left-tolerance)&&
  124. (offset.top+206)<=(size.top+size.height+tolerance)&&
  125. (offset.left+206)<=(size.left+size.width+tolerance)) {
  126. id = $(this).data('id');
  127. if (id!=='0'&&id!==0&&id!=='f'&&id!=='s'&&id!=='r'&&id!==null&&id!==undefined) {
  128. ids.push(id);
  129. $(this).addClass('active');
  130. }
  131. }
  132. });
  133. if (ids.length!==0&&visible.album()) contextMenu.photoMulti(ids, e);
  134. else if (ids.length!==0&&visible.albums()) contextMenu.albumMulti(ids, e);
  135. else multiselect.close();
  136. },
  137. close: function() {
  138. multiselect.stopResize();
  139. multiselect.position.top = null;
  140. multiselect.position.right = null;
  141. multiselect.position.bottom = null;
  142. multiselect.position.left = null;
  143. lychee.animate('#multiselect', 'fadeOut');
  144. setTimeout(function() {
  145. $('#multiselect').remove();
  146. }, 300);
  147. }
  148. };