lychee.js 7.1 KB

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