init.js 4.4 KB

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