(function() { // Get application context var app = Sammy.apps['#main']; var store = app.store; // The logic used to temporily disable transition is from // https://stackoverflow.com/a/16575811 function whichTransitionEvent(){ var t; var el = document.createElement('fakeelement'); var transitions = { 'transition':'transitionend', 'OTransition':'oTransitionEnd', 'MozTransition':'transitionend', 'WebkitTransition':'webkitTransitionEnd' } for(t in transitions){ if( el.style[t] !== undefined ){ return transitions[t]; } } }; var transitionEvent = whichTransitionEvent(); function resetSliders() { // Disable transition effects $('#slider-container').addClass('notransition'); // Delete the left/right temporary stuff only used during animation $('#slideTo').css('display', 'none'); $('#slideTo').html(""); $('#slideBack').css('display', 'none'); $('#slideBack').html(""); // Set the margin-left back to 0 $('#slider-container').css('margin-left', '0'); // c.f. the stackoverflow thread $('#slider-container')[0].offsetHeight; // Remove the binding to this event handler for next times // Re-enable transition effects $('#slider-container').removeClass('notransition'); } /** * Helpers * */ app.helpers({ // // Pacman loader management // showLoader: function() { app.loaded = false; // Not sure if that's really useful ... this is from old code with no explanation what it really does ... if ($('div.loader').length === 0) { $('#main').append('
'); } }, hideLoader: function() { app.loaded = true; // Not sure if that's really useful ... this is from old code with no explanation what it really does ... $('div.loader').remove(); }, // Flash helper to diplay instant notifications flash: function (level, message) { if (!store.get('flash')) { store.set('flash', true); } // Helper CSS class on main wrapper $('#slider').addClass('with-flashMessage'); // If the line is a bash command if (level === 'info' && message.charAt(0) === '+') { level = 'log'; } message = message.split("\n").join("' + message + '
'; } else { message = ''+message+'
'; } // Add message $('#flashMessage .messages') .prepend('helper c.prePaste(); // Run callback callback(); // Force scrollTop on page load $('html, body').scrollTop(0); }); }; // Slide back effect if (store.get('slide') == 'back') { store.clear('slide'); // Disable transition while we tweak CSS $('#slider-container').addClass('notransition'); // "Delete" the left part of the slider $('#slideBack').css('display', 'none'); // Push the slider to the left $('#slider-container').css('margin-left', '-100%'); // slideTo is the right part, and should contain the old view, // so we copypasta what's in the "center" slider (#main) $('#slideTo').show().html($('#main').html()); // leSwap will put the new view in the "center" slider (#main) leSwap(); // So now things look like: // | | // | the screen | // | | // // . #main . #slideTo . // . the new view . the old view . // ^ ^ // margin-left: -100% currently shown // // =====>>> sliiiiide =====>>> // Re-add transition effect $('#slider-container').removeClass('notransition'); // add the transition event to detect the end of the transition effect transitionEvent && $("#slider-container").off(transitionEvent) && $("#slider-container").on(transitionEvent, resetSliders); // And actually play the transition effect that will move the container from left to right $('#slider-container').css('margin-left', '0px'); } // Slide to effect else if (store.get('slide') == 'to') { // Disable transition while we tweak CSS $('#slider-container').addClass('notransition'); // "Delete" the right part of the slider $('#slideTo').css('display', 'none'); // Push the slider to the right $('#slider-container').css('margin-left', '0px'); // slideBack should contain the old view, // so we copypasta what's in the "center" slider (#main) $('#slideBack').show().html($('#main').html()); leSwap(); // So now things look like: // // | | // | the screen | // | | // // . . #slideBack . #main . // . . the old view . the new view . // ^ ^ ^ // margin-left: -100% currently shown // // <<<===== sliiiiide <<<======= // Re-add transition effect $('#slider-container').removeClass('notransition'); // add the transition event to detect the end of the transition effect var transitionEvent = whichTransitionEvent(); transitionEvent && $("#slider-container").off(transitionEvent) && $("#slider-container").on(transitionEvent, resetSliders); // And actually play the transition effect that will move the container from right to left $('#slider-container').css('margin-left', '-100%'); } // No slideing effect else { leSwap(); } }, redirect_to: function(destination, options) { c = this; options = options !== undefined ? options : {}; // If destination if the same as current url, // we don't want to display the slide animation // (or if the code explicitly state to disable slide animation) if ((c.path.split("#")[1] == destination.split("#")[1]) || (options.slide == false)) { store.clear('slide'); } // This is a copy-pasta of some of the redirect/refresh code of // sammy.js because for some reason calling the original // redirect/refresh function in some context does not work >.> // (e.g. if you're already on the page) c.trigger('redirect', {to: destination}); c.app.last_location = c.path; c.app.setLocation(destination); c.app.trigger('location-changed'); }, refresh: function() { c = this; c.redirect_to(c.path, {slide: false}); }, // // Array / object helpers // arraySortById: function(arr) { arr.sort(function(a, b){ if (a.id > b.id) { return 1; } else if (a.id < b.id) { return -1; } return 0; }); }, arrayDiff: function(arr1, arr2) { arr1 = arr1 || []; arr2 = arr2 || []; return arr1.filter(function (a) { return ((arr2.indexOf(a) == -1) && (a !== "")); }); }, // Serialize an object serialize : function(obj) { var str = []; for(var p in obj) if (obj.hasOwnProperty(p)) { str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); } return str.join("&"); }, // // Misc helpers used in views etc.. // // PasteprePaste: function() { var pasteButtons = $('button[data-paste-content],a[data-paste-content]'); pasteButtons.on('click', function(){ // Get paste content element var preElement = $($(this).data('paste-content')); c.showLoader(); // Send to paste.yunohost.org $.ajax({ type: "POST", url: 'https://paste.yunohost.org/documents', data: preElement.text(), }) .success(function(data, textStatus, jqXHR) { window.open('https://paste.yunohost.org/' + data.key, '_blank'); }) .fail(function() { c.flash('fail', y18n.t('paste_error')); }) .always(function(){ c.hideLoader(); }); }); }, force_redirect: function(to) { c = this; // This is a copy-pasta of some of the redirect/refresh code of // sammy.js because for some reason calling the origina // redirect/refresh function in some context does not work >.> // (e.g. if you're already on the page) c.trigger('redirect', {to: to}); c.app.last_location = c.path; c.app.setLocation(to); c.app.trigger('location-changed'); } }); })();