lychee.js 8.6 KB

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