lychee.js 8.6 KB

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