multiselect.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if (mobileBrowser()) return false;
  32. if (lychee.publicMode) return false;
  33. if (visible.search()) return false;
  34. if (visible.infobox()) return false;
  35. if (!visible.albums()&&!visible.album) return false;
  36. if (visible.multiselect()) $('#multiselect').remove();
  37. multiselect.position.top = 70;
  38. multiselect.position.right = 40;
  39. multiselect.position.bottom = 90;
  40. multiselect.position.left = 20;
  41. $('body').append(build.multiselect(multiselect.position.top, multiselect.position.left));
  42. newWidth = $(document).width() - multiselect.position.right + 2;
  43. newHeight = $(document).height() - multiselect.position.bottom;
  44. $('#multiselect').css({
  45. width: newWidth,
  46. height: newHeight
  47. });
  48. e = {
  49. pageX: $(document).width() - (multiselect.position.right / 2),
  50. pageY: $(document).height() - multiselect.position.bottom
  51. }
  52. multiselect.getSelection(e);
  53. },
  54. resize: function(e) {
  55. var mouse_x = e.pageX,
  56. mouse_y = e.pageY,
  57. newHeight,
  58. newWidth;
  59. if (multiselect.position.top===null||
  60. multiselect.position.right===null||
  61. multiselect.position.bottom===null||
  62. multiselect.position.left===null) return false;
  63. if (mouse_y>=multiselect.position.top) {
  64. // Do not leave the screen
  65. newHeight = mouse_y - multiselect.position.top;
  66. if ((multiselect.position.top+newHeight)>=$(document).height())
  67. newHeight -= (multiselect.position.top + newHeight) - $(document).height() + 2;
  68. $('#multiselect').css({
  69. top: multiselect.position.top,
  70. bottom: 'inherit',
  71. height: newHeight
  72. });
  73. } else {
  74. $('#multiselect').css({
  75. top: 'inherit',
  76. bottom: multiselect.position.bottom,
  77. height: multiselect.position.top - e.pageY
  78. });
  79. }
  80. if (mouse_x>=multiselect.position.left) {
  81. // Do not leave the screen
  82. newWidth = mouse_x - multiselect.position.left;
  83. if ((multiselect.position.left+newWidth)>=$(document).width())
  84. newWidth -= (multiselect.position.left + newWidth) - $(document).width() + 2;
  85. $('#multiselect').css({
  86. right: 'inherit',
  87. left: multiselect.position.left,
  88. width: newWidth
  89. });
  90. } else {
  91. $('#multiselect').css({
  92. right: multiselect.position.right,
  93. left: 'inherit',
  94. width: multiselect.position.left - e.pageX
  95. });
  96. }
  97. },
  98. stopResize: function() {
  99. $(document).off('mousemove');
  100. },
  101. getSize: function() {
  102. if (!visible.multiselect()) return false;
  103. return {
  104. top: $('#multiselect').offset().top,
  105. left: $('#multiselect').offset().left,
  106. width: parseInt($('#multiselect').css('width').replace('px', '')),
  107. height: parseInt($('#multiselect').css('height').replace('px', ''))
  108. };
  109. },
  110. getSelection: function(e) {
  111. var tolerance = 150,
  112. id,
  113. ids = [],
  114. offset,
  115. size = multiselect.getSize();
  116. if (visible.contextMenu()) return false;
  117. if (!visible.multiselect()) return false;
  118. $('.photo, .album').each(function() {
  119. offset = $(this).offset();
  120. if (offset.top>=(size.top-tolerance)&&
  121. offset.left>=(size.left-tolerance)&&
  122. (offset.top+206)<=(size.top+size.height+tolerance)&&
  123. (offset.left+206)<=(size.left+size.width+tolerance)) {
  124. id = $(this).data('id');
  125. if (id!=='0'&&id!==0&&id!=='f'&&id!=='s'&&id!=='r'&&id!==null&id!==undefined) {
  126. ids.push(id);
  127. $(this).addClass('active');
  128. }
  129. }
  130. });
  131. if (ids.length!==0&&visible.album()) contextMenu.photoMulti(ids, e);
  132. else if (ids.length!==0&&visible.albums()) contextMenu.albumMulti(ids, e);
  133. else multiselect.close();
  134. },
  135. close: function() {
  136. multiselect.stopResize();
  137. multiselect.position.top = null;
  138. multiselect.position.right = null;
  139. multiselect.position.bottom = null;
  140. multiselect.position.left = null;
  141. lychee.animate('#multiselect', 'fadeOut');
  142. setTimeout(function() {
  143. $('#multiselect').remove();
  144. }, 300);
  145. }
  146. };