view.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**
  2. * @description Responsible to reflect data changes to the UI.
  3. */
  4. view = {}
  5. view.albums = {
  6. init: function() {
  7. view.albums.title()
  8. view.albums.content.init()
  9. },
  10. title: function() {
  11. lychee.setTitle('Albums', false)
  12. },
  13. content: {
  14. scrollPosition: 0,
  15. init: function() {
  16. let smartData = ''
  17. let albumsData = ''
  18. // Smart Albums
  19. if (lychee.publicMode===false) {
  20. albums.parse(albums.json.smartalbums.unsorted)
  21. albums.parse(albums.json.smartalbums.public)
  22. albums.parse(albums.json.smartalbums.starred)
  23. albums.parse(albums.json.smartalbums.recent)
  24. smartData = build.divider('Smart Albums') + build.album(albums.json.smartalbums.unsorted) + build.album(albums.json.smartalbums.public) + build.album(albums.json.smartalbums.starred) + build.album(albums.json.smartalbums.recent)
  25. }
  26. // Albums
  27. if (albums.json.albums && albums.json.num!==0) {
  28. $.each(albums.json.albums, function() {
  29. albums.parse(this)
  30. albumsData += build.album(this)
  31. })
  32. // Add divider
  33. if (lychee.publicMode===false) albumsData = build.divider('Albums') + albumsData
  34. }
  35. if (smartData==='' && albumsData==='') {
  36. lychee.content.html('')
  37. $('body').append(build.no_content('eye'))
  38. } else {
  39. lychee.content.html(smartData + albumsData)
  40. }
  41. // Restore scroll position
  42. if (view.albums.content.scrollPosition!=null && view.albums.content.scrollPosition!==0) {
  43. $(document).scrollTop(view.albums.content.scrollPosition)
  44. }
  45. },
  46. title: function(albumID) {
  47. let title = albums.getByID(albumID).title
  48. title = lychee.escapeHTML(title)
  49. $('.album[data-id="' + albumID + '"] .overlay h1')
  50. .html(title)
  51. .attr('title', title)
  52. },
  53. delete: function(albumID) {
  54. $('.album[data-id="' + albumID + '"]').css('opacity', 0).animate({
  55. width : 0,
  56. marginLeft : 0
  57. }, 300, function() {
  58. $(this).remove()
  59. if (albums.json.num<=0) lychee.content.find('.divider:last-child').remove()
  60. })
  61. }
  62. }
  63. }
  64. view.album = {
  65. init: function() {
  66. album.parse()
  67. view.album.sidebar()
  68. view.album.title()
  69. view.album.public()
  70. view.album.content.init()
  71. album.json.init = 1
  72. },
  73. title: function() {
  74. if ((visible.album() || !album.json.init) && !visible.photo()) {
  75. switch (album.getID()) {
  76. case 'f':
  77. lychee.setTitle('Starred', false)
  78. break
  79. case 's':
  80. lychee.setTitle('Public', false)
  81. break
  82. case 'r':
  83. lychee.setTitle('Recent', false)
  84. break
  85. case '0':
  86. lychee.setTitle('Unsorted', false)
  87. break
  88. default:
  89. if (album.json.init) sidebar.changeAttr('title', album.json.title)
  90. lychee.setTitle(album.json.title, true)
  91. break
  92. }
  93. }
  94. },
  95. content: {
  96. init: function() {
  97. let photosData = ''
  98. if (album.json.content && album.json.content!==false) {
  99. // Build photos
  100. $.each(album.json.content, function() {
  101. photosData += build.photo(this)
  102. })
  103. }
  104. // Save and reset scroll position
  105. view.albums.content.scrollPosition = $(document).scrollTop()
  106. requestAnimationFrame(() => $(document).scrollTop(0))
  107. // Add photos to view
  108. lychee.content.html(photosData)
  109. },
  110. title: function(photoID) {
  111. let title = album.json.content[photoID].title
  112. title = lychee.escapeHTML(title)
  113. $('.photo[data-id="' + photoID + '"] .overlay h1')
  114. .html(title)
  115. .attr('title', title)
  116. },
  117. star: function(photoID) {
  118. let $badge = $('.photo[data-id="' + photoID + '"] .icn-star')
  119. if (album.json.content[photoID].star==='1') $badge.addClass('badge--visible')
  120. else $badge.removeClass('badge--visible')
  121. },
  122. public: function(photoID) {
  123. let $badge = $('.photo[data-id="' + photoID + '"] .icn-share')
  124. if (album.json.content[photoID].public==='1') $badge.addClass('badge--visible')
  125. else $badge.removeClass('badge--visible')
  126. },
  127. delete: function(photoID) {
  128. $('.photo[data-id="' + photoID + '"]').css('opacity', 0).animate({
  129. width : 0,
  130. marginLeft : 0
  131. }, 300, function() {
  132. $(this).remove()
  133. // Only when search is not active
  134. if (!visible.albums()) {
  135. album.json.num--
  136. view.album.num()
  137. }
  138. })
  139. }
  140. },
  141. description: function() {
  142. sidebar.changeAttr('description', album.json.description)
  143. },
  144. num: function() {
  145. sidebar.changeAttr('images', album.json.num)
  146. },
  147. public: function() {
  148. if (album.json.public==='1') {
  149. $('#button_share_album')
  150. .addClass('active')
  151. .attr('title', 'Share Album')
  152. $('.photo .iconic-share').remove()
  153. if (album.json.init) sidebar.changeAttr('public', 'Yes')
  154. } else {
  155. $('#button_share_album')
  156. .removeClass('active')
  157. .attr('title', 'Make Public')
  158. if (album.json.init) sidebar.changeAttr('public', 'No')
  159. }
  160. },
  161. hidden: function() {
  162. if (album.json.visible==='1') sidebar.changeAttr('hidden', 'No')
  163. else sidebar.changeAttr('hidden', 'Yes')
  164. },
  165. downloadable: function() {
  166. if (album.json.downloadable==='1') sidebar.changeAttr('downloadable', 'Yes')
  167. else sidebar.changeAttr('downloadable', 'No')
  168. },
  169. password: function() {
  170. if (album.json.password==='1') sidebar.changeAttr('password', 'Yes')
  171. else sidebar.changeAttr('password', 'No')
  172. },
  173. sidebar: function() {
  174. if ((visible.album() || !album.json.init) && !visible.photo()) {
  175. let structure = sidebar.createStructure.album(album.json)
  176. let html = sidebar.render(structure)
  177. sidebar.dom('.sidebar__wrapper').html(html)
  178. sidebar.bind()
  179. }
  180. }
  181. }
  182. view.photo = {
  183. init: function() {
  184. photo.parse()
  185. view.photo.sidebar()
  186. view.photo.title()
  187. view.photo.star()
  188. view.photo.public()
  189. view.photo.photo()
  190. photo.json.init = 1
  191. },
  192. show: function() {
  193. // Change header
  194. lychee.content.addClass('view')
  195. header.setMode('photo')
  196. // Make body not scrollable
  197. $('body').css('overflow', 'hidden')
  198. // Fullscreen
  199. let timeout = null
  200. $(document).bind('mousemove', function() {
  201. clearTimeout(timeout)
  202. header.show()
  203. timeout = setTimeout(header.hide, 2500)
  204. })
  205. lychee.animate(lychee.imageview, 'fadeIn')
  206. },
  207. hide: function() {
  208. header.show()
  209. lychee.content.removeClass('view')
  210. header.setMode('album')
  211. // Make body scrollable
  212. $('body').css('overflow', 'auto')
  213. // Disable Fullscreen
  214. $(document).unbind('mousemove')
  215. // Hide Photo
  216. lychee.animate(lychee.imageview, 'fadeOut')
  217. setTimeout(() => {
  218. lychee.imageview.hide()
  219. view.album.sidebar()
  220. }, 300)
  221. },
  222. title: function() {
  223. if (photo.json.init) sidebar.changeAttr('title', photo.json.title)
  224. lychee.setTitle(photo.json.title, true)
  225. },
  226. description: function() {
  227. if (photo.json.init) sidebar.changeAttr('description', photo.json.description)
  228. },
  229. star: function() {
  230. if (photo.json.star==='1') {
  231. // Starred
  232. $('#button_star')
  233. .addClass('active')
  234. .attr('title', 'Unstar Photo')
  235. } else {
  236. // Unstarred
  237. $('#button_star').removeClass('active')
  238. $('#button_star').attr('title', 'Star Photo')
  239. }
  240. },
  241. public: function() {
  242. if (photo.json.public==='1' || photo.json.public==='2') {
  243. // Photo public
  244. $('#button_share')
  245. .addClass('active')
  246. .attr('title', 'Share Photo')
  247. if (photo.json.init) sidebar.changeAttr('public', 'Yes')
  248. } else {
  249. // Photo private
  250. $('#button_share')
  251. .removeClass('active')
  252. .attr('title', 'Make Public')
  253. if (photo.json.init) sidebar.changeAttr('public', 'No')
  254. }
  255. },
  256. tags: function() {
  257. sidebar.changeAttr('tags', build.tags(photo.json.tags), true)
  258. sidebar.bind()
  259. },
  260. photo: function() {
  261. lychee.imageview.html(build.imageview(photo.json, visible.header()))
  262. let $nextArrow = lychee.imageview.find('a#next')
  263. let $previousArrow = lychee.imageview.find('a#previous')
  264. let photoID = photo.getID()
  265. let hasNext = album.json && album.json.content && album.json.content[photoID] && album.json.content[photoID].nextPhoto!=null && album.json.content[photoID].nextPhoto!==''
  266. let hasPrevious = album.json && album.json.content && album.json.content[photoID] && album.json.content[photoID].previousPhoto!=null && album.json.content[photoID].previousPhoto!==''
  267. if (hasNext===false || lychee.viewMode===true) {
  268. $nextArrow.hide()
  269. } else {
  270. let nextPhotoID = album.json.content[photoID].nextPhoto
  271. let nextPhoto = album.json.content[nextPhotoID]
  272. $nextArrow.css('background-image', lychee.html`linear-gradient(to bottom, rgba(0, 0, 0, .4), rgba(0, 0, 0, .4)), url("$${ nextPhoto.thumbUrl }")`)
  273. }
  274. if (hasPrevious===false || lychee.viewMode===true) {
  275. $previousArrow.hide()
  276. } else {
  277. let previousPhotoID = album.json.content[photoID].previousPhoto
  278. let previousPhoto = album.json.content[previousPhotoID]
  279. $previousArrow.css('background-image', lychee.html`linear-gradient(to bottom, rgba(0, 0, 0, .4), rgba(0, 0, 0, .4)), url("$${ previousPhoto.thumbUrl }")`)
  280. }
  281. },
  282. sidebar: function() {
  283. let structure = sidebar.createStructure.photo(photo.json)
  284. let html = sidebar.render(structure)
  285. sidebar.dom('.sidebar__wrapper').html(html)
  286. sidebar.bind()
  287. }
  288. }