view.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. $('.photo[data-id="' + photoID + '"] .iconic-star').remove()
  118. if (album.json.content[photoID].star==='1') $('.photo[data-id="' + photoID + '"]').append("<a class='badge iconic-star'>" + build.iconic('star') + "</a>")
  119. },
  120. public: function(photoID) {
  121. $('.photo[data-id="' + photoID + '"] .iconic-share').remove()
  122. if (album.json.content[photoID].public==='1') $('.photo[data-id="' + photoID + '"]').append("<a class='badge iconic-share'>" + build.iconic('eye') + "</a>")
  123. },
  124. delete: function(photoID) {
  125. $('.photo[data-id="' + photoID + '"]').css('opacity', 0).animate({
  126. width : 0,
  127. marginLeft : 0
  128. }, 300, function() {
  129. $(this).remove()
  130. // Only when search is not active
  131. if (!visible.albums()) {
  132. album.json.num--
  133. view.album.num()
  134. view.album.title()
  135. }
  136. })
  137. }
  138. },
  139. description: function() {
  140. sidebar.changeAttr('description', album.json.description)
  141. },
  142. num: function() {
  143. sidebar.changeAttr('images', album.json.num)
  144. },
  145. public: function() {
  146. if (album.json.public==='1') {
  147. $('#button_share_album')
  148. .addClass('active')
  149. .attr('title', 'Share Album')
  150. $('.photo .iconic-share').remove()
  151. if (album.json.init) sidebar.changeAttr('public', 'Yes')
  152. } else {
  153. $('#button_share_album')
  154. .removeClass('active')
  155. .attr('title', 'Make Public')
  156. if (album.json.init) sidebar.changeAttr('public', 'No')
  157. }
  158. },
  159. visible: function() {
  160. if (album.json.visible==='1') sidebar.changeAttr('visible', 'Yes')
  161. else sidebar.changeAttr('visible', 'No')
  162. },
  163. downloadable: function() {
  164. if (album.json.downloadable==='1') sidebar.changeAttr('downloadable', 'Yes')
  165. else sidebar.changeAttr('downloadable', 'No')
  166. },
  167. password: function() {
  168. if (album.json.password==='1') sidebar.changeAttr('password', 'Yes')
  169. else sidebar.changeAttr('password', 'No')
  170. },
  171. sidebar: function() {
  172. if ((visible.album() || !album.json.init) && !visible.photo()) {
  173. let structure = sidebar.createStructure.album(album.json),
  174. html = sidebar.render(structure)
  175. sidebar.dom('.wrapper').html(html)
  176. sidebar.bind()
  177. }
  178. }
  179. }
  180. view.photo = {
  181. init: function() {
  182. photo.parse()
  183. view.photo.sidebar()
  184. view.photo.title()
  185. view.photo.star()
  186. view.photo.public()
  187. view.photo.photo()
  188. photo.json.init = 1
  189. },
  190. show: function() {
  191. // Change header
  192. lychee.content.addClass('view')
  193. header.setMode('photo')
  194. // Make body not scrollable
  195. $('body').css('overflow', 'hidden')
  196. // Fullscreen
  197. $(document)
  198. .bind('mouseenter', header.show)
  199. .bind('mouseleave', header.hide)
  200. lychee.animate(lychee.imageview, 'fadeIn')
  201. },
  202. hide: function() {
  203. header.show()
  204. lychee.content.removeClass('view')
  205. header.setMode('album')
  206. // Make body scrollable
  207. $('body').css('overflow', 'auto')
  208. // Disable Fullscreen
  209. $(document)
  210. .unbind('mouseenter')
  211. .unbind('mouseleave')
  212. // Hide Photo
  213. lychee.animate(lychee.imageview, 'fadeOut')
  214. setTimeout(() => {
  215. lychee.imageview.hide()
  216. view.album.sidebar()
  217. }, 300)
  218. },
  219. title: function() {
  220. if (photo.json.init) sidebar.changeAttr('title', photo.json.title)
  221. lychee.setTitle(photo.json.title, true)
  222. },
  223. description: function() {
  224. if (photo.json.init) sidebar.changeAttr('description', photo.json.description)
  225. },
  226. star: function() {
  227. if (photo.json.star==='1') {
  228. // Starred
  229. $('#button_star')
  230. .addClass('active')
  231. .attr('title', 'Unstar Photo')
  232. } else {
  233. // Unstarred
  234. $('#button_star').removeClass('active')
  235. $('#button_star').attr('title', 'Star Photo')
  236. }
  237. },
  238. public: function() {
  239. if (photo.json.public==='1' || photo.json.public==='2') {
  240. // Photo public
  241. $('#button_share')
  242. .addClass('active')
  243. .attr('title', 'Share Photo')
  244. if (photo.json.init) sidebar.changeAttr('public', 'Yes')
  245. } else {
  246. // Photo private
  247. $('#button_share')
  248. .removeClass('active')
  249. .attr('title', 'Make Public')
  250. if (photo.json.init) sidebar.changeAttr('public', 'No')
  251. }
  252. },
  253. tags: function() {
  254. sidebar.changeAttr('tags', build.tags(photo.json.tags))
  255. sidebar.bind()
  256. },
  257. photo: function() {
  258. lychee.imageview.html(build.imageview(photo.json, photo.getSize(), visible.header()))
  259. let $nextArrow = lychee.imageview.find('a#next'),
  260. $previousArrow = lychee.imageview.find('a#previous'),
  261. hasNext = album.json && album.json.content && album.json.content[photo.getID()] && album.json.content[photo.getID()].nextPhoto==='',
  262. hasPrevious = album.json && album.json.content && album.json.content[photo.getID()] && album.json.content[photo.getID()].previousPhoto===''
  263. if (hasNext || lychee.viewMode) { $nextArrow.hide() }
  264. else {
  265. let nextPhotoID = album.json.content[photo.getID()].nextPhoto,
  266. nextPhoto = album.json.content[nextPhotoID]
  267. $nextArrow.css('background-image', `linear-gradient(to bottom, rgba(0, 0, 0, .4), rgba(0, 0, 0, .4)), url("${ nextPhoto.thumbUrl }")`)
  268. }
  269. if (hasPrevious || lychee.viewMode) { $previousArrow.hide() }
  270. else {
  271. let previousPhotoID = album.json.content[photo.getID()].previousPhoto,
  272. previousPhoto = album.json.content[previousPhotoID]
  273. $previousArrow.css('background-image', `linear-gradient(to bottom, rgba(0, 0, 0, .4), rgba(0, 0, 0, .4)), url("${ previousPhoto.thumbUrl }")`)
  274. }
  275. },
  276. sidebar: function() {
  277. let structure = sidebar.createStructure.photo(photo.json),
  278. html = sidebar.render(structure)
  279. sidebar.dom('.wrapper').html(html)
  280. sidebar.bind()
  281. }
  282. }