lychee/assets/js/multiselect.js

171 lines
3.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-01-28 16:00:55 +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-01-31 21:28:16 +00:00
if (mobileBrowser()) return false;
if (lychee.publicMode) return false;
2014-02-09 21:23:09 +00:00
if (visible.search()) return false;
2014-02-25 22:37:05 +00:00
if ($('.album:hover, .photo:hover').length!==0) return false;
2014-01-28 16:00:55 +00:00
if (visible.multiselect()) $('#multiselect').remove();
2014-02-17 15:22:53 +00:00
2014-01-28 16:00:55 +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-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
newHeight = e.pageY - multiselect.position.top;
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
newWidth = e.pageX - multiselect.position.left;
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 {
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
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-01-28 16:00:55 +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
};