lychee.js 8.8 KB

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