lychee.js 8.2 KB

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