lychee.js 7.3 KB

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