lychee.js 8.4 KB

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