123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- /**
- * @description This module provides the basic functions of Lychee.
- * @copyright 2015 by Tobias Reich
- */
- lychee = {
- title: document.title,
- version: '3.0.3',
- version_code: '030003',
- update_path: 'http://lychee.electerious.com/version/index.php',
- updateURL: 'https://github.com/electerious/Lychee',
- website: 'http://lychee.electerious.com',
- publicMode: false,
- viewMode: false,
- debugMode: false,
- checkForUpdates:'1',
- sortingPhotos: '',
- sortingAlbums: '',
- location: '',
- dropbox: false,
- dropboxKey: '',
- content: $('#content'),
- imageview: $('#imageview')
- }
- lychee.init = function() {
- var params;
- params = {
- version: lychee.version_code
- }
- api.post('Session::init', params, function(data) {
- // Check status
- // 0 = No configuration
- // 1 = Logged out
- // 2 = Logged in
- if (data.status===2) {
- // Logged in
- lychee.sortingPhotos = data.config.sortingPhotos || '';
- lychee.sortingAlbums = data.config.sortingAlbums || '';
- lychee.dropboxKey = data.config.dropboxKey || '';
- lychee.location = data.config.location || '';
- lychee.checkForUpdates = data.config.checkForUpdates || '1';
- // Show dialog when there is no username and password
- if (data.config.login===false) settings.createLogin();
- } else if (data.status===1) {
- // Logged out
- lychee.checkForUpdates = data.config.checkForUpdates || '1';
- lychee.setMode('public');
- } else if (data.status===0) {
- // No configuration
- lychee.setMode('public');
- header.dom().hide();
- lychee.content.hide();
- $('body').append(build.no_content('cog'));
- settings.createConfig();
- return true;
- }
- $(window).bind('popstate', lychee.load);
- lychee.load();
- });
- }
- lychee.login = function(data) {
- var user = data.username,
- password = data.password,
- params;
- params = {
- user,
- password
- }
- api.post('Session::login', params, function(data) {
- if (data===true) {
- // Use 'try' to catch a thrown error when Safari is in private mode
- try { localStorage.setItem('lychee_username', user); }
- catch (err) {}
- window.location.reload();
- } else {
- // Show error and reactive button
- basicModal.error('password');
- }
- });
- }
- lychee.loginDialog = function() {
- var localUsername,
- msg = '';
- msg = `
- <p class='signIn'>
- <input class='text' name='username' autocomplete='username' type='text' value='' placeholder='username' autocapitalize='off' autocorrect='off'>
- <input class='text' name='password' autocomplete='current-password' type='password' value='' placeholder='password'>
- </p>
- <p class='version'>Lychee ${ lychee.version }<span> – <a target='_blank' href='${ lychee.updateURL }'>Update available!</a><span></p>
- `
- basicModal.show({
- body: msg,
- buttons: {
- action: {
- title: 'Sign In',
- fn: lychee.login
- },
- cancel: {
- title: 'Cancel',
- fn: basicModal.close
- }
- }
- });
- if (localStorage) {
- localUsername = localStorage.getItem('lychee_username');
- if (localUsername!==null) {
- if (localUsername.length>0) $('.basicModal input[name="username"]').val(localUsername);
- $('.basicModal input[name="password"]').focus();
- }
- }
- if (lychee.checkForUpdates==='1') lychee.getUpdate();
- }
- lychee.logout = function() {
- api.post('Session::logout', {}, function() {
- window.location.reload();
- });
- }
- lychee.goto = function(url) {
- if (url===undefined) url = '#';
- else url = '#' + url;
- history.pushState(null, null, url);
- lychee.load();
- }
- lychee.load = function() {
- var albumID = '',
- photoID = '',
- hash = document.location.hash.replace('#', '').split('/');
- $('.no_content').remove();
- contextMenu.close();
- multiselect.close();
- if (hash[0]!==undefined) albumID = hash[0];
- if (hash[1]!==undefined) photoID = hash[1];
- if (albumID&&photoID) {
- // Trash data
- photo.json = null;
- // Show Photo
- if (lychee.content.html()===''||
- ($('#search').length&&$('#search').val().length!==0)) {
- lychee.content.hide();
- album.load(albumID, true);
- }
- photo.load(photoID, albumID);
- } else if (albumID) {
- // Trash data
- photo.json = null;
- // Hide sidebar
- if (visible.sidebar()) sidebar.toggle();
- // Show Album
- if (visible.photo()) view.photo.hide();
- if (album.json&&albumID==album.json.id) view.album.title();
- else album.load(albumID);
- } else {
- // Trash albums.json when filled with search results
- if (search.hash!==null) {
- albums.json = null;
- search.hash = null;
- }
- // Trash data
- album.json = null;
- photo.json = null;
- // Hide sidebar
- if (visible.sidebar()) sidebar.toggle();
- // Show Albums
- if (visible.photo()) view.photo.hide();
- albums.load();
- }
- }
- lychee.getUpdate = function() {
- $.ajax({
- url: lychee.update_path,
- success: function(data) { if (parseInt(data)>parseInt(lychee.version_code)) $('.version span').show(); }
- });
- }
- lychee.setTitle = function(title, editable) {
- document.title = lychee.title + ' - ' + title;
- header.setEditable(editable);
- header.setTitle(title);
- }
- lychee.setMode = function(mode) {
- $('#button_settings, #button_settings, #button_search, #search, #button_trash_album, #button_share_album, .button_add, .button_divider').remove();
- $('#button_trash, #button_move, #button_share, #button_star').remove();
- $(document)
- .off('click', '#title.editable')
- .off('touchend', '#title.editable')
- .off('contextmenu', '.photo')
- .off('contextmenu', '.album')
- .off('drop');
- Mousetrap
- .unbind('u')
- .unbind('s')
- .unbind('f')
- .unbind('r')
- .unbind('d')
- .unbind('t')
- .unbind(['command+backspace', 'ctrl+backspace'])
- .unbind(['command+a', 'ctrl+a']);
- if (mode==='public') {
- header.dom('#button_signin, #hostedwith').show();
- lychee.publicMode = true;
- } else if (mode==='view') {
- Mousetrap.unbind(['esc', 'command+up']);
- $('#button_back, a#next, a#previous').remove();
- $('.no_content').remove();
- lychee.publicMode = true;
- lychee.viewMode = true;
- }
- }
- lychee.animate = function(obj, animation) {
- var animations = [
- ['fadeIn', 'fadeOut'],
- ['contentZoomIn', 'contentZoomOut']
- ];
- if (!obj.jQuery) obj = $(obj);
- for (var i = 0; i < animations.length; i++) {
- for (var x = 0; x < animations[i].length; x++) {
- if (animations[i][x]==animation) {
- obj.removeClass(animations[i][0] + ' ' + animations[i][1]).addClass(animation);
- return true;
- }
- }
- }
- return false;
- }
- lychee.escapeHTML = function(s) {
- return s.replace(/&/g, '&')
- .replace(/"/g, '"')
- .replace(/</g, '<')
- .replace(/>/g, '>');
- }
- lychee.retinize = function(path = '') {
- var pixelRatio = window.devicePixelRatio,
- extention = path.split('.').pop(),
- hasRetina = extention!=='svg';
- if ((pixelRatio!==undefined&&pixelRatio>1)&&
- (hasRetina===true)) {
- path = path.replace(/\.[^/.]+$/, '');
- path = path + '@2x' + '.' + extention;
- }
- return {
- path,
- hasRetina
- };
- }
- lychee.loadDropbox = function(callback) {
- if (!lychee.dropbox&&lychee.dropboxKey) {
- loadingBar.show();
- var g = document.createElement('script'),
- s = document.getElementsByTagName('script')[0];
- g.src = 'https://www.dropbox.com/static/api/1/dropins.js';
- g.id = 'dropboxjs';
- g.type = 'text/javascript';
- g.async = 'true';
- g.setAttribute('data-app-key', lychee.dropboxKey);
- g.onload = g.onreadystatechange = function() {
- var rs = this.readyState;
- if (rs&&rs!=='complete'&&rs!=='loaded') return;
- lychee.dropbox = true;
- loadingBar.hide();
- callback();
- };
- s.parentNode.insertBefore(g, s);
- } else if (lychee.dropbox&&lychee.dropboxKey) {
- callback();
- } else {
- settings.setDropboxKey(callback);
- }
- }
- lychee.getEventName = function() {
- let touchendSupport = (/Android|iPhone|iPad|iPod/i).test(navigator.userAgent || navigator.vendor || window.opera) && ('ontouchend' in document.documentElement),
- eventName = (touchendSupport===true ? 'touchend' : 'click');
- return eventName;
- }
- lychee.removeHTML = function(html = '') {
- if (html==='') return html;
- var tmp = document.createElement('DIV');
- tmp.innerHTML = html;
- return tmp.textContent || tmp.innerText;
- }
- lychee.error = function(errorThrown, params, data) {
- console.error({
- description: errorThrown,
- params: params,
- response: data
- });
- loadingBar.show('error', errorThrown);
- }
|