lychee.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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() {
  90. var user = $('input#username').val(),
  91. password = md5($('input#password').val()),
  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. $('#password').val('').addClass('error').focus();
  103. $('.message .button.active').removeClass('pressed');
  104. }
  105. });
  106. }
  107. lychee.loginDialog = function() {
  108. var local_username;
  109. $('body').append(build.signInModal());
  110. $('#username').focus();
  111. if (localStorage) {
  112. local_username = localStorage.getItem('lychee_username');
  113. if (local_username!==null) {
  114. if (local_username.length>0) $('#username').val(local_username);
  115. $('#password').focus();
  116. }
  117. }
  118. if (lychee.checkForUpdates==='1') lychee.getUpdate();
  119. }
  120. lychee.logout = function() {
  121. lychee.api('logout', window.location.reload);
  122. }
  123. lychee.goto = function(url) {
  124. if (url===undefined) url = '#';
  125. else url = '#' + url;
  126. history.pushState(null, null, url);
  127. lychee.load();
  128. }
  129. lychee.load = function() {
  130. var albumID = '',
  131. photoID = '',
  132. hash = document.location.hash.replace('#', '').split('/');
  133. $('.no_content').remove();
  134. contextMenu.close();
  135. multiselect.close();
  136. if (hash[0]!==undefined) albumID = hash[0];
  137. if (hash[1]!==undefined) photoID = hash[1];
  138. if (albumID&&photoID) {
  139. // Trash data
  140. photo.json = null;
  141. // Show Photo
  142. if (lychee.content.html()===''||
  143. ($('#search').length&&$('#search').val().length!==0)) {
  144. lychee.content.hide();
  145. album.load(albumID, true);
  146. }
  147. photo.load(photoID, albumID);
  148. } else if (albumID) {
  149. // Trash data
  150. photo.json = null;
  151. // Show Album
  152. if (visible.photo()) view.photo.hide();
  153. if (album.json&&albumID==album.json.id) view.album.title();
  154. else album.load(albumID);
  155. } else {
  156. // Trash albums.json when filled with search results
  157. if (search.code!=='') {
  158. albums.json = null;
  159. search.code = '';
  160. }
  161. // Trash data
  162. album.json = null;
  163. photo.json = null;
  164. // Show Albums
  165. if (visible.album()) view.album.hide();
  166. if (visible.photo()) view.photo.hide();
  167. albums.load();
  168. }
  169. }
  170. lychee.getUpdate = function() {
  171. $.ajax({
  172. url: lychee.update_path,
  173. success: function(data) { if (parseInt(data)>parseInt(lychee.version_code)) $('#version span').show(); }
  174. });
  175. }
  176. lychee.setTitle = function(title, editable) {
  177. var $title = header.dom('#title');
  178. document.title = lychee.title + ' - ' + title;
  179. if (editable) $title.addClass('editable');
  180. else $title.removeClass('editable');
  181. $title.html(title + build.iconic('caret-bottom'));
  182. }
  183. lychee.setMode = function(mode) {
  184. $('#button_settings, #button_settings, #button_search, #search, #button_trash_album, #button_share_album, .button_add, .button_divider').remove();
  185. $('#button_trash, #button_move, #button_share, #button_star').remove();
  186. $(document)
  187. .on('mouseenter', '#title.editable', function() { $(this).removeClass('editable') })
  188. .off('click', '#title.editable')
  189. .off('touchend', '#title.editable')
  190. .off('contextmenu', '.photo')
  191. .off('contextmenu', '.album')
  192. .off('drop');
  193. Mousetrap
  194. .unbind(['u', 'ctrl+u'])
  195. .unbind(['s', 'ctrl+s'])
  196. .unbind(['f', 'ctrl+f'])
  197. .unbind(['r', 'ctrl+r'])
  198. .unbind(['d', 'ctrl+d'])
  199. .unbind(['t', 'ctrl+t'])
  200. .unbind(['command+backspace', 'ctrl+backspace'])
  201. .unbind(['command+a', 'ctrl+a']);
  202. if (mode==='public') {
  203. header.dom('#button_signin, #hostedwith').show();
  204. lychee.publicMode = true;
  205. } else if (mode==='view') {
  206. Mousetrap.unbind('esc');
  207. $('#button_back, a#next, a#previous').remove();
  208. $('.no_content').remove();
  209. lychee.publicMode = true;
  210. lychee.viewMode = true;
  211. }
  212. }
  213. lychee.animate = function(obj, animation) {
  214. var animations = [
  215. ['fadeIn', 'fadeOut'],
  216. ['contentZoomIn', 'contentZoomOut']
  217. ];
  218. if (!obj.jQuery) obj = $(obj);
  219. for (var i = 0; i < animations.length; i++) {
  220. for (var x = 0; x < animations[i].length; x++) {
  221. if (animations[i][x]==animation) {
  222. obj.removeClass(animations[i][0] + ' ' + animations[i][1]).addClass(animation);
  223. return true;
  224. }
  225. }
  226. }
  227. return false;
  228. }
  229. lychee.escapeHTML = function(s) {
  230. return s.replace(/&/g, '&amp;')
  231. .replace(/"/g, '&quot;')
  232. .replace(/</g, '&lt;')
  233. .replace(/>/g, '&gt;');
  234. }
  235. lychee.loadDropbox = function(callback) {
  236. if (!lychee.dropbox&&lychee.dropboxKey) {
  237. loadingBar.show();
  238. var g = document.createElement('script'),
  239. s = document.getElementsByTagName('script')[0];
  240. g.src = 'https://www.dropbox.com/static/api/1/dropins.js';
  241. g.id = 'dropboxjs';
  242. g.type = 'text/javascript';
  243. g.async = 'true';
  244. g.setAttribute('data-app-key', lychee.dropboxKey);
  245. g.onload = g.onreadystatechange = function() {
  246. var rs = this.readyState;
  247. if (rs&&rs!=='complete'&&rs!=='loaded') return;
  248. lychee.dropbox = true;
  249. loadingBar.hide();
  250. callback();
  251. };
  252. s.parentNode.insertBefore(g, s);
  253. } else if (lychee.dropbox&&lychee.dropboxKey) {
  254. callback();
  255. } else {
  256. settings.setDropboxKey(callback);
  257. }
  258. }
  259. lychee.removeHTML = function(html) {
  260. var tmp = document.createElement('DIV');
  261. tmp.innerHTML = html;
  262. return tmp.textContent || tmp.innerText;
  263. }
  264. lychee.error = function(errorThrown, params, data) {
  265. console.error({
  266. description: errorThrown,
  267. params: params,
  268. response: data
  269. });
  270. loadingBar.show('error', errorThrown);
  271. }