lychee.js 8.0 KB

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