lychee.js 7.9 KB

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