lychee/assets/js/multiselect.js

210 lines
4.7 KiB
JavaScript
Raw Normal View History

2014-01-28 16:00:55 +00:00
/**
* @name Multiselect Module
* @description Select multiple albums or photos.
* @author Tobias Reich
* @copyright 2014 by Tobias Reich
*/
multiselect = {
position: {
2014-02-17 15:22:53 +00:00
2014-08-24 20:10:21 +00:00
top: null,
right: null,
bottom: null,
left: null
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
},
show: function(e) {
2014-02-17 15:22:53 +00:00
2014-08-24 20:10:21 +00:00
if (mobileBrowser()) return false;
if (lychee.publicMode) return false;
if (visible.search()) return false;
if (visible.infobox()) return false;
if (!visible.albums()&&!visible.album) return false;
if ($('.album:hover, .photo:hover').length!==0) return false;
if (visible.multiselect()) $('#multiselect').remove();
2014-02-17 15:22:53 +00:00
2014-08-24 20:10:21 +00:00
multiselect.position.top = e.pageY;
multiselect.position.right = -1 * (e.pageX - $(document).width());
multiselect.position.bottom = -1 * (multiselect.position.top - $(window).height());
multiselect.position.left = e.pageX;
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
$('body').append(build.multiselect(multiselect.position.top, multiselect.position.left));
$(document).on('mousemove', multiselect.resize);
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
},
2014-02-17 15:22:53 +00:00
2014-08-24 20:10:21 +00:00
selectAll: function() {
2014-08-31 20:11:04 +00:00
var e,
newWidth,
newHeight;
2014-08-24 20:10:21 +00:00
if (mobileBrowser()) return false;
if (lychee.publicMode) return false;
if (visible.search()) return false;
if (visible.infobox()) return false;
if (!visible.albums()&&!visible.album) return false;
if (visible.multiselect()) $('#multiselect').remove();
multiselect.position.top = 70;
multiselect.position.right = 40;
multiselect.position.bottom = 90;
multiselect.position.left = 20;
$('body').append(build.multiselect(multiselect.position.top, multiselect.position.left));
newWidth = $(document).width() - multiselect.position.right + 2;
newHeight = $(document).height() - multiselect.position.bottom;
$('#multiselect').css({
width: newWidth,
height: newHeight
});
e = {
pageX: $(document).width() - (multiselect.position.right / 2),
pageY: $(document).height() - multiselect.position.bottom
2014-08-31 20:11:04 +00:00
};
2014-08-24 20:10:21 +00:00
multiselect.getSelection(e);
},
2014-01-28 16:00:55 +00:00
resize: function(e) {
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
var mouse_x = e.pageX,
mouse_y = e.pageY,
newHeight,
newWidth;
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
if (multiselect.position.top===null||
multiselect.position.right===null||
multiselect.position.bottom===null||
multiselect.position.left===null) return false;
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
if (mouse_y>=multiselect.position.top) {
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
// Do not leave the screen
2014-08-24 20:10:21 +00:00
newHeight = mouse_y - multiselect.position.top;
2014-01-28 16:00:55 +00:00
if ((multiselect.position.top+newHeight)>=$(document).height())
newHeight -= (multiselect.position.top + newHeight) - $(document).height() + 2;
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
$('#multiselect').css({
top: multiselect.position.top,
bottom: 'inherit',
height: newHeight
});
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
} else {
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
$('#multiselect').css({
top: 'inherit',
bottom: multiselect.position.bottom,
height: multiselect.position.top - e.pageY
});
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
}
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
if (mouse_x>=multiselect.position.left) {
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
// Do not leave the screen
2014-08-24 20:10:21 +00:00
newWidth = mouse_x - multiselect.position.left;
2014-01-28 16:00:55 +00:00
if ((multiselect.position.left+newWidth)>=$(document).width())
newWidth -= (multiselect.position.left + newWidth) - $(document).width() + 2;
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
$('#multiselect').css({
right: 'inherit',
left: multiselect.position.left,
width: newWidth
});
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
} else {
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
$('#multiselect').css({
right: multiselect.position.right,
left: 'inherit',
width: multiselect.position.left - e.pageX
});
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
}
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
},
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
stopResize: function() {
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
$(document).off('mousemove');
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
},
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
getSize: function() {
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
if (!visible.multiselect()) return false;
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
return {
2014-08-24 20:10:21 +00:00
top: $('#multiselect').offset().top,
left: $('#multiselect').offset().left,
width: parseInt($('#multiselect').css('width').replace('px', '')),
height: parseInt($('#multiselect').css('height').replace('px', ''))
2014-02-25 22:37:05 +00:00
};
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
},
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
getSelection: function(e) {
2014-02-17 15:22:53 +00:00
2014-02-01 20:50:36 +00:00
var tolerance = 150,
id,
2014-01-31 20:22:25 +00:00
ids = [],
2014-01-28 19:44:25 +00:00
offset,
size = multiselect.getSize();
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
if (visible.contextMenu()) return false;
if (!visible.multiselect()) return false;
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
$('.photo, .album').each(function() {
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
offset = $(this).offset();
2014-02-17 15:22:53 +00:00
2014-02-01 20:50:36 +00:00
if (offset.top>=(size.top-tolerance)&&
offset.left>=(size.left-tolerance)&&
(offset.top+206)<=(size.top+size.height+tolerance)&&
(offset.left+206)<=(size.left+size.width+tolerance)) {
2014-02-17 15:22:53 +00:00
2014-01-31 20:22:25 +00:00
id = $(this).data('id');
2014-02-17 15:22:53 +00:00
2014-08-31 20:11:04 +00:00
if (id!=='0'&&id!==0&&id!=='f'&&id!=='s'&&id!=='r'&&id!==null&&id!==undefined) {
2014-02-17 15:22:53 +00:00
2014-01-31 20:22:25 +00:00
ids.push(id);
$(this).addClass('active');
2014-02-17 15:22:53 +00:00
2014-01-31 20:22:25 +00:00
}
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
}
});
2014-02-17 15:22:53 +00:00
2014-02-25 22:37:05 +00:00
if (ids.length!==0&&visible.album()) contextMenu.photoMulti(ids, e);
else if (ids.length!==0&&visible.albums()) contextMenu.albumMulti(ids, e);
2014-01-28 19:44:25 +00:00
else multiselect.close();
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
},
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
close: function() {
2014-02-17 15:22:53 +00:00
2014-01-28 19:44:25 +00:00
multiselect.stopResize();
2014-02-17 15:22:53 +00:00
2014-08-24 20:10:21 +00:00
multiselect.position.top = null;
multiselect.position.right = null;
multiselect.position.bottom = null;
multiselect.position.left = null;
2014-02-17 15:22:53 +00:00
lychee.animate('#multiselect', 'fadeOut');
2014-01-28 16:00:55 +00:00
setTimeout(function() {
$('#multiselect').remove();
}, 300);
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +00:00
}
2014-02-25 22:37:05 +00:00
};