init.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @description This module is used for bindings.
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. $(document).ready(function() {
  6. // Event Name
  7. let 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() {
  21. if (visible.photo()) { $('#imageview a#previous').click(); return false }
  22. })
  23. .bind([ 'right' ], function() {
  24. if (visible.photo()) { $('#imageview a#next').click(); return false }
  25. })
  26. .bind([ 'u' ], function() {
  27. if (!visible.photo()) { $('#upload_files').click(); return false }
  28. })
  29. .bind([ 's', 'f' ], function() {
  30. if (visible.photo()) { header.dom('#button_star').click(); return false }
  31. else if (visible.albums()) { header.dom('.header__search').focus(); return false }
  32. })
  33. .bind([ 'r' ], function() {
  34. if (visible.album()) { album.setTitle(album.getID()); return false }
  35. else if (visible.photo()) { photo.setTitle([photo.getID()]); return false }
  36. })
  37. .bind([ 'd' ], function() {
  38. if (visible.photo()) { photo.setDescription(photo.getID()); return false }
  39. else if (visible.album()) { album.setDescription(album.getID()); return false }
  40. })
  41. .bind([ 't' ], function() {
  42. if (visible.photo()) { photo.editTags([photo.getID()]); return false }
  43. })
  44. .bind([ 'i' ], function() {
  45. if (!visible.multiselect()) { sidebar.toggle(); return false }
  46. })
  47. .bind([ 'command+backspace', 'ctrl+backspace' ], function() {
  48. if (visible.photo() && basicModal.visible()===false) { photo.delete([photo.getID()]); return false }
  49. else if (visible.album() && basicModal.visible()===false) { album.delete([album.getID()]); return false }
  50. })
  51. .bind([ 'command+a', 'ctrl+a' ], function() {
  52. if (visible.album() && basicModal.visible()===false) { multiselect.selectAll(); return false }
  53. else if (visible.albums() && basicModal.visible()===false) { multiselect.selectAll(); return false }
  54. })
  55. Mousetrap.bindGlobal('enter', function() {
  56. if (basicModal.visible()===true) basicModal.action()
  57. })
  58. Mousetrap.bindGlobal([ 'esc', 'command+up' ], function() {
  59. if (basicModal.visible()===true) basicModal.cancel()
  60. else if (visible.contextMenu()) contextMenu.close()
  61. else if (visible.photo()) lychee.goto(album.getID())
  62. else if (visible.album()) lychee.goto('')
  63. else if (visible.albums() && header.dom('.header__search').val().length!==0) search.reset()
  64. return false
  65. })
  66. if (eventName==='touchend') {
  67. $(document)
  68. // Fullscreen on mobile
  69. .on('touchend', '#imageview #image', function(e) {
  70. if (swipe.obj==null || (swipe.offset>=-5&&swipe.offset<=5)) {
  71. if (visible.header()) header.hide(e, 0)
  72. else header.show()
  73. }
  74. })
  75. // Swipe on mobile
  76. .swipe().on('swipeStart', function() { if (visible.photo()) swipe.start($('#imageview #image')) })
  77. .swipe().on('swipeMove', function(e) { if (visible.photo()) swipe.move(e.swipe) })
  78. .swipe().on('swipeEnd', function(e) { if (visible.photo()) swipe.stop(e.swipe, photo.previous, photo.next) })
  79. }
  80. // Document
  81. $(document)
  82. // Navigation
  83. .on('click', '.album', function() { lychee.goto($(this).attr('data-id')) })
  84. .on('click', '.photo', function() { lychee.goto(album.getID() + '/' + $(this).attr('data-id')) })
  85. // Context Menu
  86. .on('contextmenu', '.photo', function(e) { contextMenu.photo(photo.getID(), e) })
  87. .on('contextmenu', '.album', function(e) { contextMenu.album(album.getID(), e) })
  88. // Upload
  89. .on('change', '#upload_files', function() { basicModal.close(); upload.start.local(this.files) })
  90. // Drag and Drop upload
  91. .on('dragover', function() { return false }, false)
  92. .on('drop', function(e) {
  93. // Close open overlays or views which are correlating with the upload
  94. if (visible.photo()) lychee.goto(album.getID())
  95. if (visible.contextMenu()) contextMenu.close()
  96. // Detect if dropped item is a file or a link
  97. if (e.originalEvent.dataTransfer.files.length>0) upload.start.local(e.originalEvent.dataTransfer.files)
  98. else if (e.originalEvent.dataTransfer.getData('Text').length>3) upload.start.url(e.originalEvent.dataTransfer.getData('Text'))
  99. return false
  100. })
  101. // Init
  102. lychee.init()
  103. })