lychee.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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.2',
  8. versionCode : '030102',
  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 (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. }