lychee.js 7.3 KB

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