init.js 4.5 KB

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