lychee.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**
  2. * @description This module provides the basic functions of Lychee.
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. lychee = {
  6. title : document.title,
  7. version : '3.1.0',
  8. versionCode : '030100',
  9. updatePath : '//update.electerious.com/index.json',
  10. updateURL : 'https://github.com/electerious/Lychee',
  11. website : 'http://lychee.electerious.com',
  12. publicMode : false,
  13. viewMode : false,
  14. checkForUpdates : '1',
  15. sortingPhotos : '',
  16. sortingAlbums : '',
  17. location : '',
  18. dropbox : false,
  19. dropboxKey : '',
  20. content : $('.content'),
  21. imageview : $('#imageview')
  22. }
  23. lychee.init = function() {
  24. api.post('Session::init', {}, function(data) {
  25. // Check status
  26. // 0 = No configuration
  27. // 1 = Logged out
  28. // 2 = Logged in
  29. if (data.status===2) {
  30. // Logged in
  31. lychee.sortingPhotos = data.config.sortingPhotos || ''
  32. lychee.sortingAlbums = data.config.sortingAlbums || ''
  33. lychee.dropboxKey = data.config.dropboxKey || ''
  34. lychee.location = data.config.location || ''
  35. lychee.checkForUpdates = data.config.checkForUpdates || '1'
  36. // Show dialog when there is no username and password
  37. if (data.config.login===false) settings.createLogin()
  38. } else if (data.status===1) {
  39. // Logged out
  40. lychee.checkForUpdates = data.config.checkForUpdates || '1'
  41. lychee.setMode('public')
  42. } else if (data.status===0) {
  43. // No configuration
  44. lychee.setMode('public')
  45. header.dom().hide()
  46. lychee.content.hide()
  47. $('body').append(build.no_content('cog'))
  48. settings.createConfig()
  49. return true
  50. }
  51. $(window).bind('popstate', lychee.load)
  52. lychee.load()
  53. })
  54. }
  55. lychee.login = function(data) {
  56. let user = data.username
  57. let password = data.password
  58. let params = {
  59. user,
  60. password
  61. }
  62. api.post('Session::login', params, function(data) {
  63. if (data===true) {
  64. // Use 'try' to catch a thrown error when Safari is in private mode
  65. try { localStorage.setItem('lychee_username', user) }
  66. catch (err) {}
  67. window.location.reload()
  68. } else {
  69. // Show error and reactive button
  70. basicModal.error('password')
  71. }
  72. })
  73. }
  74. lychee.loginDialog = function() {
  75. let msg = lychee.html`
  76. <p class='signIn'>
  77. <input class='text' name='username' autocomplete='username' type='text' value='' placeholder='username' autocapitalize='off' autocorrect='off'>
  78. <input class='text' name='password' autocomplete='current-password' type='password' value='' placeholder='password'>
  79. </p>
  80. <p class='version'>Lychee $${ lychee.version }<span> &#8211; <a target='_blank' href='$${ lychee.updateURL }'>Update available!</a><span></p>
  81. `
  82. basicModal.show({
  83. body: msg,
  84. buttons: {
  85. action: {
  86. title: 'Sign In',
  87. fn: lychee.login
  88. },
  89. cancel: {
  90. title: 'Cancel',
  91. fn: basicModal.close
  92. }
  93. }
  94. })
  95. if (localStorage) {
  96. let localUsername = localStorage.getItem('lychee_username')
  97. if (localUsername!=null && localUsername.length>0) {
  98. $('.basicModal input[name="username"]').val(localUsername)
  99. $('.basicModal input[name="password"]').focus()
  100. }
  101. }
  102. if (lychee.checkForUpdates==='1') lychee.getUpdate()
  103. }
  104. lychee.logout = function() {
  105. api.post('Session::logout', {}, function() {
  106. window.location.reload()
  107. })
  108. }
  109. lychee.goto = function(url = '') {
  110. url = '#' + url
  111. history.pushState(null, null, url)
  112. lychee.load()
  113. }
  114. lychee.load = function() {
  115. let albumID = ''
  116. let photoID = ''
  117. let hash = document.location.hash.replace('#', '').split('/')
  118. $('.no_content').remove()
  119. contextMenu.close()
  120. multiselect.close()
  121. if (hash[0]!=null) albumID = hash[0]
  122. if (hash[1]!=null) photoID = hash[1]
  123. if (albumID && photoID) {
  124. // Trash data
  125. photo.json = null
  126. // Show Photo
  127. if (lychee.content.html()==='' || (header.dom('.header__search').length && header.dom('.header__search').val().length!==0)) {
  128. lychee.content.hide()
  129. album.load(albumID, true)
  130. }
  131. photo.load(photoID, albumID)
  132. } else if (albumID) {
  133. // Trash data
  134. photo.json = null
  135. // Show Album
  136. if (visible.photo()) view.photo.hide()
  137. if (album.json && albumID==album.json.id) view.album.title()
  138. else album.load(albumID)
  139. } else {
  140. // Trash albums.json when filled with search results
  141. if (search.hash!=null) {
  142. albums.json = null
  143. search.hash = null
  144. }
  145. // Trash data
  146. album.json = null
  147. photo.json = null
  148. // Hide sidebar
  149. if (visible.sidebar()) sidebar.toggle()
  150. // Show Albums
  151. if (visible.photo()) view.photo.hide()
  152. lychee.content.show()
  153. albums.load()
  154. }
  155. }
  156. lychee.getUpdate = function() {
  157. const success = function(data) {
  158. if (data.lychee.version>parseInt(lychee.versionCode)) $('.version span').show()
  159. }
  160. $.ajax({
  161. url : lychee.updatePath,
  162. success : success
  163. })
  164. }
  165. lychee.setTitle = function(title, editable) {
  166. document.title = lychee.title + ' - ' + title
  167. header.setEditable(editable)
  168. header.setTitle(title)
  169. }
  170. lychee.setMode = function(mode) {
  171. $('#button_settings, #button_trash_album, #button_share_album, .button_add, .header__divider').remove()
  172. $('#button_trash, #button_move, #button_share, #button_star').remove()
  173. $(document)
  174. .off('click', '.header__title--editable')
  175. .off('touchend', '.header__title--editable')
  176. .off('contextmenu', '.photo')
  177. .off('contextmenu', '.album')
  178. .off('drop')
  179. Mousetrap
  180. .unbind([ 'u' ])
  181. .unbind([ 's' ])
  182. .unbind([ 'f' ])
  183. .unbind([ 'r' ])
  184. .unbind([ 'd' ])
  185. .unbind([ 't' ])
  186. .unbind([ 'command+backspace', 'ctrl+backspace' ])
  187. .unbind([ 'command+a', 'ctrl+a' ])
  188. if (mode==='public') {
  189. lychee.publicMode = true
  190. } else if (mode==='view') {
  191. Mousetrap.unbind([ 'esc', 'command+up' ])
  192. $('#button_back, a#next, a#previous').remove()
  193. $('.no_content').remove()
  194. lychee.publicMode = true
  195. lychee.viewMode = true
  196. }
  197. }
  198. lychee.animate = function(obj, animation) {
  199. let animations = [
  200. [ 'fadeIn', 'fadeOut' ],
  201. [ 'contentZoomIn', 'contentZoomOut' ]
  202. ]
  203. if (!obj.jQuery) obj = $(obj)
  204. for (let i = 0; i < animations.length; i++) {
  205. for (let x = 0; x < animations[i].length; x++) {
  206. if (animations[i][x]==animation) {
  207. obj.removeClass(animations[i][0] + ' ' + animations[i][1]).addClass(animation)
  208. return true
  209. }
  210. }
  211. }
  212. return false
  213. }
  214. lychee.retinize = function(path = '') {
  215. let extention = path.split('.').pop()
  216. let isPhoto = extention!=='svg'
  217. if (isPhoto===true) {
  218. path = path.replace(/\.[^/.]+$/, '')
  219. path = path + '@2x' + '.' + extention
  220. }
  221. return {
  222. path,
  223. isPhoto
  224. }
  225. }
  226. lychee.loadDropbox = function(callback) {
  227. if (lychee.dropbox===false && lychee.dropboxKey!=null && lychee.dropboxKey!=='') {
  228. loadingBar.show()
  229. let g = document.createElement('script')
  230. let s = document.getElementsByTagName('script')[0]
  231. g.src = 'https://www.dropbox.com/static/api/1/dropins.js'
  232. g.id = 'dropboxjs'
  233. g.type = 'text/javascript'
  234. g.async = 'true'
  235. g.setAttribute('data-app-key', lychee.dropboxKey)
  236. g.onload = g.onreadystatechange = function() {
  237. let rs = this.readyState
  238. if (rs && rs!=='complete' && rs!=='loaded') return
  239. lychee.dropbox = true
  240. loadingBar.hide()
  241. callback()
  242. }
  243. s.parentNode.insertBefore(g, s)
  244. } else if (lychee.dropbox===true && lychee.dropboxKey!=null && lychee.dropboxKey!=='') {
  245. callback()
  246. } else {
  247. settings.setDropboxKey(callback)
  248. }
  249. }
  250. lychee.getEventName = function() {
  251. let touchendSupport = (/Android|iPhone|iPad|iPod/i).test(navigator.userAgent || navigator.vendor || window.opera) && ('ontouchend' in document.documentElement)
  252. let eventName = (touchendSupport===true ? 'touchend' : 'click')
  253. return eventName
  254. }
  255. lychee.escapeHTML = function(html = '') {
  256. // Ensure that html is a string
  257. html += ''
  258. // Escape all critical characters
  259. html = html.replace(/&/g, '&amp;')
  260. .replace(/</g, '&lt;')
  261. .replace(/>/g, '&gt;')
  262. .replace(/"/g, '&quot;')
  263. .replace(/'/g, '&#039;')
  264. .replace(/`/g, '&#96;')
  265. return html
  266. }
  267. lychee.html = function(literalSections, ...substs) {
  268. // Use raw literal sections: we don’t want
  269. // backslashes (\n etc.) to be interpreted
  270. let raw = literalSections.raw
  271. let result = ''
  272. substs.forEach((subst, i) => {
  273. // Retrieve the literal section preceding
  274. // the current substitution
  275. let lit = raw[i]
  276. // If the substitution is preceded by a dollar sign,
  277. // we escape special characters in it
  278. if (lit.slice(-1)==='$') {
  279. subst = lychee.escapeHTML(subst)
  280. lit = lit.slice(0, -1)
  281. }
  282. result += lit
  283. result += subst
  284. })
  285. // Take care of last literal section
  286. // (Never fails, because an empty template string
  287. // produces one literal section, an empty string)
  288. result += raw[raw.length - 1]
  289. return result
  290. }
  291. lychee.error = function(errorThrown, params, data) {
  292. console.error({
  293. description : errorThrown,
  294. params : params,
  295. response : data
  296. })
  297. loadingBar.show('error', errorThrown)
  298. }