view.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /**
  2. * @description Responsible to reflect data changes to the UI.
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. view = {}
  6. view.albums = {
  7. init: function() {
  8. view.albums.title()
  9. view.albums.content.init()
  10. },
  11. title: function() {
  12. lychee.setTitle('Albums', false)
  13. },
  14. content: {
  15. scrollPosition: 0,
  16. init: function() {
  17. let smartData = '',
  18. albumsData = ''
  19. // Smart Albums
  20. if (lychee.publicMode===false) {
  21. albums.parse(albums.json.smartalbums.unsorted)
  22. albums.parse(albums.json.smartalbums.public)
  23. albums.parse(albums.json.smartalbums.starred)
  24. albums.parse(albums.json.smartalbums.recent)
  25. 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)
  26. }
  27. // Albums
  28. if (albums.json.albums && albums.json.num!==0) {
  29. $.each(albums.json.albums, function() {
  30. albums.parse(this)
  31. albumsData += build.album(this)
  32. })
  33. // Add divider
  34. if (lychee.publicMode===false) albumsData = build.divider('Albums') + albumsData
  35. }
  36. if (smartData==='' && albumsData==='') {
  37. lychee.content.html('')
  38. $('body').append(build.no_content('eye'))
  39. } else {
  40. lychee.content.html(smartData + albumsData)
  41. }
  42. // Restore scroll position
  43. if (view.albums.content.scrollPosition!=null && view.albums.content.scrollPosition!==0) {
  44. $(document).scrollTop(view.albums.content.scrollPosition)
  45. }
  46. },
  47. title: function(albumID) {
  48. let title = albums.getByID(albumID).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. $('.photo[data-id="' + photoID + '"] .overlay h1')
  113. .html(title)
  114. .attr('title', title)
  115. },
  116. star: function(photoID) {
  117. let $badge = $('.photo[data-id="' + photoID + '"] .iconic-star')
  118. if (album.json.content[photoID].star==='1') $badge.addClass('.badge--visible')
  119. else $badge.removeClass('.badge--visible')
  120. },
  121. public: function(photoID) {
  122. let $badge = $('.photo[data-id="' + photoID + '"] .iconic-share')
  123. if (album.json.content[photoID].public==='1') $badge.addClass('.badge--visible')
  124. else $badge.removeClass('.badge--visible')
  125. },
  126. delete: function(photoID) {
  127. $('.photo[data-id="' + photoID + '"]').css('opacity', 0).animate({
  128. width : 0,
  129. marginLeft : 0
  130. }, 300, function() {
  131. $(this).remove()
  132. // Only when search is not active
  133. if (!visible.albums()) {
  134. album.json.num--
  135. view.album.num()
  136. view.album.title()
  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. visible: function() {
  162. if (album.json.visible==='1') sidebar.changeAttr('visible', 'Yes')
  163. else sidebar.changeAttr('visible', 'No')
  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. html = sidebar.render(structure)
  177. sidebar.dom('.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. $(document)
  200. .bind('mouseenter', header.show)
  201. .bind('mouseleave', header.hide)
  202. lychee.animate(lychee.imageview, 'fadeIn')
  203. },
  204. hide: function() {
  205. header.show()
  206. lychee.content.removeClass('view')
  207. header.setMode('album')
  208. // Make body scrollable
  209. $('body').css('overflow', 'auto')
  210. // Disable Fullscreen
  211. $(document)
  212. .unbind('mouseenter')
  213. .unbind('mouseleave')
  214. // Hide Photo
  215. lychee.animate(lychee.imageview, 'fadeOut')
  216. setTimeout(() => {
  217. lychee.imageview.hide()
  218. view.album.sidebar()
  219. }, 300)
  220. },
  221. title: function() {
  222. if (photo.json.init) sidebar.changeAttr('title', photo.json.title)
  223. lychee.setTitle(photo.json.title, true)
  224. },
  225. description: function() {
  226. if (photo.json.init) sidebar.changeAttr('description', photo.json.description)
  227. },
  228. star: function() {
  229. if (photo.json.star==='1') {
  230. // Starred
  231. $('#button_star')
  232. .addClass('active')
  233. .attr('title', 'Unstar Photo')
  234. } else {
  235. // Unstarred
  236. $('#button_star').removeClass('active')
  237. $('#button_star').attr('title', 'Star Photo')
  238. }
  239. },
  240. public: function() {
  241. if (photo.json.public==='1' || photo.json.public==='2') {
  242. // Photo public
  243. $('#button_share')
  244. .addClass('active')
  245. .attr('title', 'Share Photo')
  246. if (photo.json.init) sidebar.changeAttr('public', 'Yes')
  247. } else {
  248. // Photo private
  249. $('#button_share')
  250. .removeClass('active')
  251. .attr('title', 'Make Public')
  252. if (photo.json.init) sidebar.changeAttr('public', 'No')
  253. }
  254. },
  255. tags: function() {
  256. sidebar.changeAttr('tags', build.tags(photo.json.tags))
  257. sidebar.bind()
  258. },
  259. photo: function() {
  260. lychee.imageview.html(build.imageview(photo.json, photo.getSize(), visible.header()))
  261. let $nextArrow = lychee.imageview.find('a#next'),
  262. $previousArrow = lychee.imageview.find('a#previous'),
  263. hasNext = album.json && album.json.content && album.json.content[photo.getID()] && album.json.content[photo.getID()].nextPhoto==='',
  264. hasPrevious = album.json && album.json.content && album.json.content[photo.getID()] && album.json.content[photo.getID()].previousPhoto===''
  265. if (hasNext || lychee.viewMode) { $nextArrow.hide() }
  266. else {
  267. let nextPhotoID = album.json.content[photo.getID()].nextPhoto,
  268. nextPhoto = album.json.content[nextPhotoID]
  269. $nextArrow.css('background-image', `linear-gradient(to bottom, rgba(0, 0, 0, .4), rgba(0, 0, 0, .4)), url("${ nextPhoto.thumbUrl }")`)
  270. }
  271. if (hasPrevious || lychee.viewMode) { $previousArrow.hide() }
  272. else {
  273. let previousPhotoID = album.json.content[photo.getID()].previousPhoto,
  274. previousPhoto = album.json.content[previousPhotoID]
  275. $previousArrow.css('background-image', `linear-gradient(to bottom, rgba(0, 0, 0, .4), rgba(0, 0, 0, .4)), url("${ previousPhoto.thumbUrl }")`)
  276. }
  277. },
  278. sidebar: function() {
  279. let structure = sidebar.createStructure.photo(photo.json),
  280. html = sidebar.render(structure)
  281. sidebar.dom('.wrapper').html(html)
  282. sidebar.bind()
  283. }
  284. }