init.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. $('#content') .on('mousedown', function(e) { if (e.which===1) multiselect.show(e) });
  10. $(document) .on('mouseup', function(e) { if (e.which===1) multiselect.getSelection(e) });
  11. /* Header */
  12. header.dom('#title').on(eventName, function(e) {
  13. if (!$(this).hasClass('editable')) return false;
  14. if (visible.photo()) contextMenu.photoTitle(album.getID(), photo.getID(), e);
  15. else contextMenu.albumTitle(album.getID(), e);
  16. });
  17. header.dom('#button_share').on(eventName, function(e) {
  18. if (photo.json.public==='1'||photo.json.public==='2') contextMenu.sharePhoto(photo.getID(), e);
  19. else photo.setPublic(photo.getID(), e);
  20. });
  21. header.dom('#button_share_album').on(eventName, function(e) {
  22. if (album.json.public==='1') contextMenu.shareAlbum(album.getID(), e);
  23. else album.setPublic(album.getID(), true, e);
  24. });
  25. header.dom('#button_signin') .on(eventName, lychee.loginDialog);
  26. header.dom('#button_settings') .on(eventName, contextMenu.settings);
  27. header.dom('#button_info_album') .on(eventName, sidebar.toggle);
  28. header.dom('#button_info') .on(eventName, sidebar.toggle);
  29. header.dom('.button_add') .on(eventName, contextMenu.add);
  30. header.dom('#button_more') .on(eventName, function(e) { contextMenu.photoMore(photo.getID(), e) });
  31. header.dom('#button_move') .on(eventName, function(e) { contextMenu.move([photo.getID()], e) });
  32. header.dom('#hostedwith') .on(eventName, function() { window.open(lychee.website) });
  33. header.dom('#button_trash_album') .on(eventName, function() { album.delete([album.getID()]) });
  34. header.dom('#button_trash') .on(eventName, function() { photo.delete([photo.getID()]) });
  35. header.dom('#button_archive') .on(eventName, function() { album.getArchive(album.getID()) });
  36. header.dom('#button_star') .on(eventName, function() { photo.setStar([photo.getID()]) });
  37. header.dom('#button_back_home') .on(eventName, function() { lychee.goto('') });
  38. header.dom('#button_back') .on(eventName, function() { lychee.goto(album.getID()) });
  39. /* Search */
  40. header.dom('#search').on('keyup click', function() { search.find($(this).val()) });
  41. /* Clear Search */
  42. header.dom('#clearSearch').on(eventName, function () {
  43. header.dom('#search').focus();
  44. search.reset();
  45. });
  46. /* Image View */
  47. lychee.imageview
  48. .on(eventName, '.arrow_wrapper--previous', photo.previous)
  49. .on(eventName, '.arrow_wrapper--next', photo.next);
  50. /* Keyboard */
  51. Mousetrap
  52. .bind('left', function() { if (visible.photo()) $('#imageview a#previous').click() })
  53. .bind('right', function() { if (visible.photo()) $('#imageview a#next').click() })
  54. .bind('u', function() { $('#upload_files').click() })
  55. .bind(['s', 'f'], function(e) {
  56. if (visible.photo()) {
  57. header.dom('#button_star').click();
  58. } else if (visible.albums()) {
  59. e.preventDefault();
  60. header.dom('#search').focus();
  61. }
  62. })
  63. .bind('r', function(e) {
  64. e.preventDefault();
  65. if (visible.album()) album.setTitle(album.getID());
  66. else if (visible.photo()) photo.setTitle([photo.getID()]);
  67. })
  68. .bind('d', function(e) {
  69. e.preventDefault();
  70. if (visible.photo()) photo.setDescription(photo.getID());
  71. else if (visible.album()) album.setDescription(album.getID());
  72. })
  73. .bind('t', function(e) {
  74. if (visible.photo()) {
  75. e.preventDefault();
  76. photo.editTags([photo.getID()]);
  77. }
  78. })
  79. .bind('i', function() {
  80. if (visible.multiselect()) return false;
  81. else sidebar.toggle();
  82. })
  83. .bind(['command+backspace', 'ctrl+backspace'], function() {
  84. if (visible.photo()&&!visible.message()) photo.delete([photo.getID()]);
  85. else if (visible.album()&&!visible.message()) album.delete([album.getID()]);
  86. })
  87. .bind(['command+a', 'ctrl+a'], function() {
  88. if (visible.album()&&!visible.message()) multiselect.selectAll();
  89. else if (visible.albums()&&!visible.message()) multiselect.selectAll();
  90. });
  91. Mousetrap.bindGlobal('enter', function() {
  92. if (basicModal.visible()===true) basicModal.action();
  93. });
  94. Mousetrap.bindGlobal(['esc', 'command+up'], function(e) {
  95. e.preventDefault();
  96. if (basicModal.visible()===true) basicModal.cancel();
  97. else if (visible.contextMenu()) contextMenu.close();
  98. else if (visible.photo()) lychee.goto(album.getID());
  99. else if (visible.album()) lychee.goto('');
  100. else if (visible.albums()&&$('#search').val().length!==0) search.reset();
  101. });
  102. if ('ontouchend' in document.documentElement) {
  103. $(document)
  104. /* Fullscreen on mobile */
  105. .on('touchend', '#image', function(e) {
  106. if (swipe.obj===null||(swipe.offset>=-5&&swipe.offset<=5)) {
  107. if (visible.header()) header.hide(e, 0);
  108. else header.show();
  109. }
  110. })
  111. /* Swipe on mobile */
  112. .swipe().on('swipeStart', function() { if (visible.photo()) swipe.start($('#image')) })
  113. .swipe().on('swipeMove', function(e) { if (visible.photo()) swipe.move(e.swipe) })
  114. .swipe().on('swipeEnd', function(e) { if (visible.photo()) swipe.stop(e.swipe, photo.previous, photo.next) });
  115. }
  116. /* Document */
  117. $(document)
  118. /* Login */
  119. .on('keyup', '#password', function() { if ($(this).val().length>0) $(this).removeClass('error') })
  120. /* Navigation */
  121. .on('click', '.album', function() { lychee.goto($(this).attr('data-id')) })
  122. .on('click', '.photo', function() { lychee.goto(album.getID() + '/' + $(this).attr('data-id')) })
  123. /* Context Menu */
  124. .on('contextmenu', '.photo', function(e) { contextMenu.photo(photo.getID(), e) })
  125. .on('contextmenu', '.album', function(e) { contextMenu.album(album.getID(), e) })
  126. /* Upload */
  127. .on('change', '#upload_files', function() { basicModal.close(); upload.start.local(this.files) })
  128. .on('dragover', function(e) { e.preventDefault(); }, false)
  129. .on('drop', function(e) {
  130. e.stopPropagation();
  131. e.preventDefault();
  132. // Close open overlays or views which are correlating with the upload
  133. if (visible.photo()) lychee.goto(album.getID());
  134. if (visible.contextMenu()) contextMenu.close();
  135. // Detect if dropped item is a file or a link
  136. if (e.originalEvent.dataTransfer.files.length>0) upload.start.local(e.originalEvent.dataTransfer.files);
  137. else if (e.originalEvent.dataTransfer.getData('Text').length>3) upload.start.url(e.originalEvent.dataTransfer.getData('Text'));
  138. return true;
  139. });
  140. /* Init */
  141. lychee.init();
  142. });