init.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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()) photo.setTitle([photo.getID()]);
  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(), 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, view.infobox.show);
  28. header.dom('#button_info') .on(eventName, view.infobox.show);
  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. /* Infobox */
  47. lychee.infobox.find('.header .close').on(eventName, view.infobox.hide);
  48. /* Image View */
  49. lychee.imageview
  50. .on(eventName, '.arrow_wrapper--previous', photo.previous)
  51. .on(eventName, '.arrow_wrapper--next', photo.next);
  52. /* Infobox */
  53. lychee.infobox
  54. .on(eventName, '#edit_title_album', function() { album.setTitle([album.getID()]) })
  55. .on(eventName, '#edit_description_album', function() { album.setDescription(album.getID()) })
  56. .on(eventName, '#edit_title', function() { photo.setTitle([photo.getID()]) })
  57. .on(eventName, '#edit_description', function() { photo.setDescription(photo.getID()) })
  58. .on(eventName, '#edit_tags', function() { photo.editTags([photo.getID()]) })
  59. .on(eventName, '#tags .tag span', function() { photo.deleteTag(photo.getID(), $(this).data('index')) });
  60. /* Keyboard */
  61. Mousetrap
  62. .bind('left', function() { if (visible.photo()) $('#imageview a#previous').click() })
  63. .bind('right', function() { if (visible.photo()) $('#imageview a#next').click() })
  64. .bind('u', function() { $('#upload_files').click() })
  65. .bind(['s', 'f'], function(e) {
  66. if (visible.photo()) {
  67. header.dom('#button_star').click();
  68. } else if (visible.albums()) {
  69. e.preventDefault();
  70. header.dom('#search').focus();
  71. }
  72. })
  73. .bind('r', function(e) {
  74. e.preventDefault();
  75. if (visible.album()) album.setTitle(album.getID());
  76. else if (visible.photo()) photo.setTitle([photo.getID()]);
  77. })
  78. .bind('d', function(e) {
  79. e.preventDefault();
  80. if (visible.photo()) photo.setDescription(photo.getID());
  81. else if (visible.album()) album.setDescription(album.getID());
  82. })
  83. .bind('t', function(e) {
  84. if (visible.photo()) {
  85. e.preventDefault();
  86. photo.editTags([photo.getID()]);
  87. }
  88. })
  89. .bind('i', function() {
  90. if (visible.infobox()) view.infobox.hide();
  91. else if (visible.multiselect()) return false;
  92. else if (visible.infoboxbutton()) view.infobox.show();
  93. })
  94. .bind(['command+backspace', 'ctrl+backspace'], function() {
  95. if (visible.photo()&&!visible.message()) photo.delete([photo.getID()]);
  96. else if (visible.album()&&!visible.message()) album.delete([album.getID()]);
  97. })
  98. .bind(['command+a', 'ctrl+a'], function() {
  99. if (visible.album()&&!visible.message()) multiselect.selectAll();
  100. else if (visible.albums()&&!visible.message()) multiselect.selectAll();
  101. });
  102. Mousetrap.bindGlobal('enter', function() {
  103. if (basicModal.visible()===true) basicModal.action();
  104. });
  105. Mousetrap.bindGlobal(['esc', 'command+up'], function(e) {
  106. e.preventDefault();
  107. if (basicModal.visible()===true) basicModal.cancel();
  108. else if (visible.contextMenu()) contextMenu.close();
  109. else if (visible.infobox()) view.infobox.hide();
  110. else if (visible.photo()) lychee.goto(album.getID());
  111. else if (visible.album()) lychee.goto('');
  112. else if (visible.albums()&&$('#search').val().length!==0) search.reset();
  113. });
  114. if ('ontouchend' in document.documentElement) {
  115. $(document)
  116. /* Fullscreen on mobile */
  117. .on('touchend', '#image', function(e) {
  118. if (swipe.obj===null||(swipe.offset>=-5&&swipe.offset<=5)) {
  119. if (visible.controls()) header.hide(e, 0);
  120. else header.show();
  121. }
  122. })
  123. /* Swipe on mobile */
  124. .swipe().on('swipeStart', function() { if (visible.photo()) swipe.start($('#image')) })
  125. .swipe().on('swipeMove', function(e) { if (visible.photo()) swipe.move(e.swipe) })
  126. .swipe().on('swipeEnd', function(e) { if (visible.photo()) swipe.stop(e.swipe, photo.previous, photo.next) });
  127. }
  128. /* Document */
  129. $(document)
  130. /* Login */
  131. .on('keyup', '#password', function() { if ($(this).val().length>0) $(this).removeClass('error') })
  132. /* Navigation */
  133. .on('click', '.album', function() { lychee.goto($(this).attr('data-id')) })
  134. .on('click', '.photo', function() { lychee.goto(album.getID() + '/' + $(this).attr('data-id')) })
  135. /* Context Menu */
  136. .on('contextmenu', '.photo', function(e) { contextMenu.photo(photo.getID(), e) })
  137. .on('contextmenu', '.album', function(e) { contextMenu.album(album.getID(), e) })
  138. /* Infobox */
  139. .on(eventName, '#infobox_overlay', view.infobox.hide)
  140. /* Upload */
  141. .on('change', '#upload_files', function() { basicModal.close(); upload.start.local(this.files) })
  142. .on('dragover', function(e) { e.preventDefault(); }, false)
  143. .on('drop', function(e) {
  144. e.stopPropagation();
  145. e.preventDefault();
  146. // Close open overlays or views which are correlating with the upload
  147. if (visible.photo()) lychee.goto(album.getID());
  148. if (visible.contextMenu()) contextMenu.close();
  149. // Detect if dropped item is a file or a link
  150. if (e.originalEvent.dataTransfer.files.length>0) upload.start.local(e.originalEvent.dataTransfer.files);
  151. else if (e.originalEvent.dataTransfer.getData('Text').length>3) upload.start.url(e.originalEvent.dataTransfer.getData('Text'));
  152. return true;
  153. });
  154. /* Init */
  155. lychee.init();
  156. });