init.js 4.2 KB

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