init.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @description This module is used for bindings.
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. $(document).ready(function() {
  6. /* Event Name */
  7. var eventName = ('ontouchend' in document.documentElement) ? 'touchend' : 'click';
  8. /* Multiselect */
  9. multiselect.bind();
  10. /* Header */
  11. header.bind();
  12. /* Image View */
  13. lychee.imageview
  14. .on(eventName, '.arrow_wrapper--previous', photo.previous)
  15. .on(eventName, '.arrow_wrapper--next', photo.next);
  16. /* Keyboard */
  17. Mousetrap
  18. .bind('left', function() { if (visible.photo()) $('#imageview a#previous').click() })
  19. .bind('right', function() { if (visible.photo()) $('#imageview a#next').click() })
  20. .bind('u', function() { $('#upload_files').click() })
  21. .bind(['s', 'f'], function(e) {
  22. if (visible.photo()) header.dom('#button_star').click();
  23. else if (visible.albums()) header.dom('#search').focus();
  24. return false;
  25. })
  26. .bind('r', function(e) {
  27. e.preventDefault();
  28. if (visible.album()) album.setTitle(album.getID());
  29. else if (visible.photo()) photo.setTitle([photo.getID()]);
  30. })
  31. .bind('d', function(e) {
  32. e.preventDefault();
  33. if (visible.photo()) photo.setDescription(photo.getID());
  34. else if (visible.album()) album.setDescription(album.getID());
  35. })
  36. .bind('t', function(e) {
  37. if (visible.photo()) {
  38. e.preventDefault();
  39. photo.editTags([photo.getID()]);
  40. }
  41. })
  42. .bind('i', function() {
  43. if (visible.multiselect()) return false;
  44. else sidebar.toggle();
  45. })
  46. .bind(['command+backspace', 'ctrl+backspace'], function() {
  47. if (visible.photo()&&!visible.message()) photo.delete([photo.getID()]);
  48. else if (visible.album()&&!visible.message()) album.delete([album.getID()]);
  49. })
  50. .bind(['command+a', 'ctrl+a'], function() {
  51. if (visible.album()&&!visible.message()) multiselect.selectAll();
  52. else if (visible.albums()&&!visible.message()) multiselect.selectAll();
  53. });
  54. Mousetrap.bindGlobal('enter', function() {
  55. if (basicModal.visible()===true) basicModal.action();
  56. });
  57. Mousetrap.bindGlobal(['esc', 'command+up'], function(e) {
  58. e.preventDefault();
  59. if (basicModal.visible()===true) basicModal.cancel();
  60. else if (visible.contextMenu()) contextMenu.close();
  61. else if (visible.photo()) lychee.goto(album.getID());
  62. else if (visible.album()) lychee.goto('');
  63. else if (visible.albums()&&$('#search').val().length!==0) search.reset();
  64. });
  65. if ('ontouchend' in document.documentElement) {
  66. $(document)
  67. /* Fullscreen on mobile */
  68. .on('touchend', '#image', function(e) {
  69. if (swipe.obj===null||(swipe.offset>=-5&&swipe.offset<=5)) {
  70. if (visible.header()) header.hide(e, 0);
  71. else header.show();
  72. }
  73. })
  74. /* Swipe on mobile */
  75. .swipe().on('swipeStart', function() { if (visible.photo()) swipe.start($('#image')) })
  76. .swipe().on('swipeMove', function(e) { if (visible.photo()) swipe.move(e.swipe) })
  77. .swipe().on('swipeEnd', function(e) { if (visible.photo()) swipe.stop(e.swipe, photo.previous, photo.next) });
  78. }
  79. /* Document */
  80. $(document)
  81. /* Login */
  82. .on('keyup', '#password', function() { if ($(this).val().length>0) $(this).removeClass('error') })
  83. /* Navigation */
  84. .on('click', '.album', function() { lychee.goto($(this).attr('data-id')) })
  85. .on('click', '.photo', function() { lychee.goto(album.getID() + '/' + $(this).attr('data-id')) })
  86. /* Context Menu */
  87. .on('contextmenu', '.photo', function(e) { contextMenu.photo(photo.getID(), e) })
  88. .on('contextmenu', '.album', function(e) { contextMenu.album(album.getID(), e) })
  89. /* Upload */
  90. .on('change', '#upload_files', function() { basicModal.close(); upload.start.local(this.files) })
  91. .on('dragover', function(e) { e.preventDefault(); }, false)
  92. .on('drop', function(e) {
  93. e.stopPropagation();
  94. e.preventDefault();
  95. // Close open overlays or views which are correlating with the upload
  96. if (visible.photo()) lychee.goto(album.getID());
  97. if (visible.contextMenu()) contextMenu.close();
  98. // Detect if dropped item is a file or a link
  99. if (e.originalEvent.dataTransfer.files.length>0) upload.start.local(e.originalEvent.dataTransfer.files);
  100. else if (e.originalEvent.dataTransfer.getData('Text').length>3) upload.start.url(e.originalEvent.dataTransfer.getData('Text'));
  101. return true;
  102. });
  103. /* Init */
  104. lychee.init();
  105. });