lychee.js 8.3 KB

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