lychee.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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' placeholder='username' autocapitalize='off' autocorrect='off'>
  78. <input class='text' name='password' autocomplete='current-password' type='password' 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 (lychee.checkForUpdates==='1') lychee.getUpdate()
  96. }
  97. lychee.logout = function() {
  98. api.post('Session::logout', {}, function() {
  99. window.location.reload()
  100. })
  101. }
  102. lychee.goto = function(url = '') {
  103. url = '#' + url
  104. history.pushState(null, null, url)
  105. lychee.load()
  106. }
  107. lychee.load = function() {
  108. let albumID = ''
  109. let photoID = ''
  110. let hash = document.location.hash.replace('#', '').split('/')
  111. $('.no_content').remove()
  112. contextMenu.close()
  113. multiselect.close()
  114. if (hash[0]!=null) albumID = hash[0]
  115. if (hash[1]!=null) photoID = hash[1]
  116. if (albumID && photoID) {
  117. // Trash data
  118. photo.json = null
  119. // Show Photo
  120. if (lychee.content.html()==='' || (header.dom('.header__search').length && header.dom('.header__search').val().length!==0)) {
  121. lychee.content.hide()
  122. album.load(albumID, true)
  123. }
  124. photo.load(photoID, albumID)
  125. } else if (albumID) {
  126. // Trash data
  127. photo.json = null
  128. // Show Album
  129. if (visible.photo()) view.photo.hide()
  130. if (album.json && albumID==album.json.id) view.album.title()
  131. else album.load(albumID)
  132. } else {
  133. // Trash albums.json when filled with search results
  134. if (search.hash!=null) {
  135. albums.json = null
  136. search.hash = null
  137. }
  138. // Trash data
  139. album.json = null
  140. photo.json = null
  141. // Hide sidebar
  142. if (visible.sidebar()) sidebar.toggle()
  143. // Show Albums
  144. if (visible.photo()) view.photo.hide()
  145. lychee.content.show()
  146. albums.load()
  147. }
  148. }
  149. lychee.getUpdate = function() {
  150. const success = function(data) {
  151. if (data.lychee.version>parseInt(lychee.versionCode)) $('.version span').show()
  152. }
  153. $.ajax({
  154. url : lychee.updatePath,
  155. success : success
  156. })
  157. }
  158. lychee.setTitle = function(title, editable) {
  159. document.title = lychee.title + ' - ' + title
  160. header.setEditable(editable)
  161. header.setTitle(title)
  162. }
  163. lychee.setMode = function(mode) {
  164. $('#button_settings, #button_trash_album, #button_share_album, .button_add, .header__divider').remove()
  165. $('#button_trash, #button_move, #button_share, #button_star').remove()
  166. $(document)
  167. .off('click', '.header__title--editable')
  168. .off('touchend', '.header__title--editable')
  169. .off('contextmenu', '.photo')
  170. .off('contextmenu', '.album')
  171. .off('drop')
  172. Mousetrap
  173. .unbind([ 'u' ])
  174. .unbind([ 's' ])
  175. .unbind([ 'f' ])
  176. .unbind([ 'r' ])
  177. .unbind([ 'd' ])
  178. .unbind([ 't' ])
  179. .unbind([ 'command+backspace', 'ctrl+backspace' ])
  180. .unbind([ 'command+a', 'ctrl+a' ])
  181. if (mode==='public') {
  182. lychee.publicMode = true
  183. } else if (mode==='view') {
  184. Mousetrap.unbind([ 'esc', 'command+up' ])
  185. $('#button_back, a#next, a#previous').remove()
  186. $('.no_content').remove()
  187. lychee.publicMode = true
  188. lychee.viewMode = true
  189. }
  190. }
  191. lychee.animate = function(obj, animation) {
  192. let animations = [
  193. [ 'fadeIn', 'fadeOut' ],
  194. [ 'contentZoomIn', 'contentZoomOut' ]
  195. ]
  196. if (!obj.jQuery) obj = $(obj)
  197. for (let i = 0; i < animations.length; i++) {
  198. for (let x = 0; x < animations[i].length; x++) {
  199. if (animations[i][x]==animation) {
  200. obj.removeClass(animations[i][0] + ' ' + animations[i][1]).addClass(animation)
  201. return true
  202. }
  203. }
  204. }
  205. return false
  206. }
  207. lychee.retinize = function(path = '') {
  208. let extention = path.split('.').pop()
  209. let isPhoto = extention!=='svg'
  210. if (isPhoto===true) {
  211. path = path.replace(/\.[^/.]+$/, '')
  212. path = path + '@2x' + '.' + extention
  213. }
  214. return {
  215. path,
  216. isPhoto
  217. }
  218. }
  219. lychee.loadDropbox = function(callback) {
  220. if (lychee.dropbox===false && lychee.dropboxKey!=null && lychee.dropboxKey!=='') {
  221. loadingBar.show()
  222. let g = document.createElement('script')
  223. let s = document.getElementsByTagName('script')[0]
  224. g.src = 'https://www.dropbox.com/static/api/1/dropins.js'
  225. g.id = 'dropboxjs'
  226. g.type = 'text/javascript'
  227. g.async = 'true'
  228. g.setAttribute('data-app-key', lychee.dropboxKey)
  229. g.onload = g.onreadystatechange = function() {
  230. let rs = this.readyState
  231. if (rs && rs!=='complete' && rs!=='loaded') return
  232. lychee.dropbox = true
  233. loadingBar.hide()
  234. callback()
  235. }
  236. s.parentNode.insertBefore(g, s)
  237. } else if (lychee.dropbox===true && lychee.dropboxKey!=null && lychee.dropboxKey!=='') {
  238. callback()
  239. } else {
  240. settings.setDropboxKey(callback)
  241. }
  242. }
  243. lychee.getEventName = function() {
  244. let touchendSupport = (/Android|iPhone|iPad|iPod/i).test(navigator.userAgent || navigator.vendor || window.opera) && ('ontouchend' in document.documentElement)
  245. let eventName = (touchendSupport===true ? 'touchend' : 'click')
  246. return eventName
  247. }
  248. lychee.escapeHTML = function(html = '') {
  249. // Ensure that html is a string
  250. html += ''
  251. // Escape all critical characters
  252. html = html.replace(/&/g, '&amp;')
  253. .replace(/</g, '&lt;')
  254. .replace(/>/g, '&gt;')
  255. .replace(/"/g, '&quot;')
  256. .replace(/'/g, '&#039;')
  257. .replace(/`/g, '&#96;')
  258. return html
  259. }
  260. lychee.html = function(literalSections, ...substs) {
  261. // Use raw literal sections: we don’t want
  262. // backslashes (\n etc.) to be interpreted
  263. let raw = literalSections.raw
  264. let result = ''
  265. substs.forEach((subst, i) => {
  266. // Retrieve the literal section preceding
  267. // the current substitution
  268. let lit = raw[i]
  269. // If the substitution is preceded by a dollar sign,
  270. // we escape special characters in it
  271. if (lit.slice(-1)==='$') {
  272. subst = lychee.escapeHTML(subst)
  273. lit = lit.slice(0, -1)
  274. }
  275. result += lit
  276. result += subst
  277. })
  278. // Take care of last literal section
  279. // (Never fails, because an empty template string
  280. // produces one literal section, an empty string)
  281. result += raw[raw.length - 1]
  282. return result
  283. }
  284. lychee.error = function(errorThrown, params, data) {
  285. console.error({
  286. description : errorThrown,
  287. params : params,
  288. response : data
  289. })
  290. loadingBar.show('error', errorThrown)
  291. }