lychee.js 7.9 KB

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