init.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @description This module is used for bindings.
  3. * @copyright 2014 by Tobias Reich
  4. */
  5. $(document).ready(function() {
  6. /* Event Name */
  7. var event_name = (mobileBrowser()) ? 'touchend' : 'click';
  8. /* Disable ContextMenu */
  9. $(document).bind('contextmenu', function(e) { e.preventDefault() });
  10. /* Multiselect */
  11. $('#content') .on('mousedown', multiselect.show);
  12. $(document) .on('mouseup', multiselect.getSelection);
  13. /* Header */
  14. $('#button_share').on(event_name, function(e) {
  15. if (photo.json.public==1||photo.json.public==2) contextMenu.sharePhoto(photo.getID(), e);
  16. else photo.setPublic(photo.getID(), e);
  17. });
  18. $('#button_share_album').on(event_name, function(e) {
  19. if (album.json.public==1) contextMenu.shareAlbum(album.getID(), e);
  20. else album.setPublic(album.getID(), e);
  21. });
  22. $('#button_signin') .on(event_name, lychee.loginDialog);
  23. $('#button_settings') .on(event_name, contextMenu.settings);
  24. $('#button_info_album') .on(event_name, view.infobox.show);
  25. $('#button_info') .on(event_name, view.infobox.show);
  26. $('.button_add') .on(event_name, contextMenu.add);
  27. $('#button_more') .on(event_name, function(e) { contextMenu.photoMore(photo.getID(), e) });
  28. $('#button_move') .on(event_name, function(e) { contextMenu.move([photo.getID()], e) });
  29. $('#hostedwith') .on(event_name, function() { window.open(lychee.website) });
  30. $('#button_trash_album') .on(event_name, function() { album.delete([album.getID()]) });
  31. $('#button_trash') .on(event_name, function() { photo.delete([photo.getID()]) });
  32. $('#button_archive') .on(event_name, function() { album.getArchive(album.getID()) });
  33. $('#button_star') .on(event_name, function() { photo.setStar([photo.getID()]) });
  34. $('#button_back_home') .on(event_name, function() { lychee.goto('') });
  35. $('#button_back') .on(event_name, function() { lychee.goto(album.getID()) });
  36. /* Search */
  37. $('#search').on('keyup click', function() { search.find($(this).val()) });
  38. /* Clear Search */
  39. $('#clearSearch').on(event_name, function () {
  40. $('#search').focus();
  41. search.reset();
  42. });
  43. /* Image View */
  44. lychee.imageview
  45. .on(event_name, '.arrow_wrapper.previous', photo.previous)
  46. .on(event_name, '.arrow_wrapper.next', photo.next);
  47. /* Infobox */
  48. $('#infobox')
  49. .on(event_name, '.header .close', view.infobox.hide)
  50. .on(event_name, '#edit_title_album', function() { album.setTitle([album.getID()]) })
  51. .on(event_name, '#edit_description_album', function() { album.setDescription(album.getID()) })
  52. .on(event_name, '#edit_title', function() { photo.setTitle([photo.getID()]) })
  53. .on(event_name, '#edit_description', function() { photo.setDescription(photo.getID()) })
  54. .on(event_name, '#edit_tags', function() { photo.editTags([photo.getID()]) })
  55. .on(event_name, '#tags .tag span', function() { photo.deleteTag(photo.getID(), $(this).data('index')) });
  56. /* Keyboard */
  57. Mousetrap
  58. .bind('left', function() { if (visible.photo()) $('#imageview a#previous').click() })
  59. .bind('right', function() { if (visible.photo()) $('#imageview a#next').click() })
  60. .bind(['u', 'ctrl+u'], function() { $('#upload_files').click() })
  61. .bind(['s', 'ctrl+s', 'f', 'ctrl+f'], function(e) {
  62. if (visible.photo()) {
  63. $('#button_star').click();
  64. } else if (visible.albums()) {
  65. e.preventDefault();
  66. $('#search').focus();
  67. }
  68. })
  69. .bind(['r', 'ctrl+r'], function(e) {
  70. e.preventDefault();
  71. if (visible.album()) album.setTitle(album.getID());
  72. else if (visible.photo()) photo.setTitle([photo.getID()]);
  73. })
  74. .bind(['d', 'ctrl+d'], function(e) {
  75. e.preventDefault();
  76. if (visible.photo()) photo.setDescription(photo.getID());
  77. else if (visible.album()) album.setDescription(album.getID());
  78. })
  79. .bind(['t', 'ctrl+t'], function(e) {
  80. if (visible.photo()) {
  81. e.preventDefault();
  82. photo.editTags([photo.getID()]);
  83. }
  84. })
  85. .bind(['i', 'ctrl+i'], function() {
  86. if (visible.infobox()) view.infobox.hide();
  87. else if (visible.multiselect()) return false;
  88. else if (visible.infoboxbutton()) view.infobox.show();
  89. })
  90. .bind(['command+backspace', 'ctrl+backspace'], function() {
  91. if (visible.photo()&&!visible.message()) photo.delete([photo.getID()]);
  92. else if (visible.album()&&!visible.message()) album.delete([album.getID()]);
  93. })
  94. .bind(['command+a', 'ctrl+a'], function() {
  95. if (visible.album()&&!visible.message()) multiselect.selectAll();
  96. else if (visible.albums()&&!visible.message()) multiselect.selectAll();
  97. });
  98. Mousetrap.bindGlobal('enter', function() {
  99. if ($('.message .button.active').length) $('.message .button.active').addClass('pressed').click()
  100. });
  101. Mousetrap.bindGlobal(['esc', 'command+up'], function(e) {
  102. e.preventDefault();
  103. if (visible.message()&&$('.message .close').length>0) modal.close();
  104. else if (visible.contextMenu()) contextMenu.close();
  105. else if (visible.infobox()) view.infobox.hide();
  106. else if (visible.photo()) lychee.goto(album.getID());
  107. else if (visible.album()) lychee.goto('');
  108. else if (visible.albums()&&$('#search').val().length!==0) search.reset();
  109. });
  110. if (mobileBrowser()) {
  111. $(document)
  112. /* Fullscreen on mobile */
  113. .on('touchend', '#image', function(e) {
  114. if (swipe.obj===null||(swipe.offset>=-5&&swipe.offset<=5)) {
  115. if (visible.controls()) view.header.hide(e, 0);
  116. else view.header.show();
  117. }
  118. })
  119. /* Swipe on mobile */
  120. .swipe().on('swipeStart', function() { if (visible.photo()) swipe.start($('#image')) })
  121. .swipe().on('swipeMove', function(e) { if (visible.photo()) swipe.move(e.swipe) })
  122. .swipe().on('swipeEnd', function(e) { if (visible.photo()) swipe.stop(e.swipe, photo.previous, photo.next) });
  123. }
  124. /* Document */
  125. $(document)
  126. /* Login */
  127. .on('keyup', '#password', function() { if ($(this).val().length>0) $(this).removeClass('error') })
  128. /* Header */
  129. .on(event_name, '#title.editable', function() {
  130. if (visible.photo()) photo.setTitle([photo.getID()]);
  131. else album.setTitle([album.getID()]);
  132. })
  133. /* Navigation */
  134. .on('click', '.album', function() { lychee.goto($(this).attr('data-id')) })
  135. .on('click', '.photo', function() { lychee.goto(album.getID() + '/' + $(this).attr('data-id')) })
  136. /* Modal */
  137. .on(event_name, '.message .close', modal.close)
  138. .on(event_name, '.message .button:first', function() { if (modal.fns!==null) modal.fns[0](); if (!visible.signin()) modal.close() })
  139. .on(event_name, '.message .button:last', function() { if (modal.fns!==null) modal.fns[1](); if (!visible.signin()) modal.close() })
  140. /* Context Menu */
  141. .on('contextmenu', '.photo', function(e) { contextMenu.photo(photo.getID(), e) })
  142. .on('contextmenu', '.album', function(e) { contextMenu.album(album.getID(), e) })
  143. /* Infobox */
  144. .on(event_name, '#infobox_overlay', view.infobox.hide)
  145. /* Upload */
  146. .on('change', '#upload_files', function() { modal.close(); upload.start.local(this.files) })
  147. .on(event_name, '.upload_message a.close', upload.close)
  148. .on('dragover', function(e) { e.preventDefault(); }, false)
  149. .on('drop', function(e) {
  150. e.stopPropagation();
  151. e.preventDefault();
  152. // Close open overlays or views which are correlating with the upload
  153. if (visible.photo()) lychee.goto(album.getID());
  154. if (visible.contextMenu()) contextMenu.close();
  155. // Detect if dropped item is a file or a link
  156. if (e.originalEvent.dataTransfer.files.length>0) upload.start.local(e.originalEvent.dataTransfer.files);
  157. else if (e.originalEvent.dataTransfer.getData('Text').length>3) upload.start.url(e.originalEvent.dataTransfer.getData('Text'));
  158. return true;
  159. });
  160. /* Init */
  161. lychee.init();
  162. });