(function() {
// Get application context
var app = Sammy.apps['#main'];
var store = app.store;
/**
* Helpers
*
*/
app.helpers({
// 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("&");
},
// 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("
");
// If the message starts with a progress bar
progressbar = message.match(/^\[#*\+*\.*\] > /);
if (progressbar)
{
progressbar = progressbar[0];
// Remove the progress bar from the mesage
message = message.replace(progressbar,"");
// Compute percent
done = (progressbar.match(/#/g)||[]).length;
ongoing = (progressbar.match(/\+/g)||[]).length;
remaining = (progressbar.match(/\./g)||[]).length;
total = done + ongoing + remaining;
done = done * 100 / total;
ongoing = ongoing * 100 / total;
// Actually build the message with the progress bar
message = '
' + 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 (enableSlide && store.get('slide') == 'back') { store.clear('slide'); $('#slideBack').css('display', 'none'); $('#slider-container').css('margin-left', '-100%'); $('#slideTo').show().html($('#main').html()); leSwap(); $('#slider-container').css('margin-left', '0px'); } // Slide to effect else if (enableSlide && store.get('slide') == 'to') { store.clear('slide'); $('#slideTo').css('display', 'none'); $('#slider-container').css('margin-left', '0px'); $('#slideBack').show().html($('#main').html()); leSwap(); $('#slider-container').css('margin-left', '-100%'); } // No slideing effect else { leSwap(); } }, confirm: function(title, content, confirmCallback, cancelCallback) { // Default callbacks confirmCallback = typeof confirmCallback !== 'undefined' ? confirmCallback : function() {}; cancelCallback = typeof cancelCallback !== 'undefined' ? cancelCallback : function() {}; // Get modal element var box = $('#modal'); // Modal title if (typeof title === 'string' && title.length) { $('.title', box).html(title); } else { box.addClass('no-title'); } // Modal content $('.content', box).html(content); // Clear any remaining click event that could still be there (e.g. // clicking outside the modal window doesn't equal to clicking // cancel... $('footer button', box).unbind( "click" ); // Handle buttons $('footer button', box) .click(function(e){ e.preventDefault(); $('#modal footer button').unbind( "click" ); // Reset & Hide modal box .removeClass('no-title') .modal('hide'); // Do corresponding callback if ($(this).data('action') == 'confirm') { confirmCallback(); } else { cancelCallback(); } }); // Show modal return box.modal('show'); }, selectAllOrNone: function () { // Remove active style from buttons $(".select_all-none input").click(function(){ $(this).toggleClass("active"); }); // Select all checkbox in this panel $(".select_all").click(function(){ $(this).parents(".panel").children(".list-group").find("input").prop("checked", true); }); // Deselect all checkbox in this panel $(".select_none").click(function(){ $(this).parents(".panel").children(".list-group").find("input").prop("checked", false); }); }, 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 !== "")); }); }, groupHooks: function(hooks, raw_infos){ var data = {}; var rules = [ { id:'configuration', isIn:function (hook) { return hook.indexOf('conf_')==0 } } ]; $.each(hooks, function(i, hook) { var group_id=hook; var hook_size=(raw_infos && raw_infos[hook] && raw_infos[hook].size)?raw_infos[hook].size:0; $.each(rules, function(i, rule) { if (rule.isIn(hook)) { group_id = 'adminjs_group_'+rule.id; return false; } }); if(group_id in data) { data[group_id] = { name:y18n.t('hook_'+group_id), value:data[group_id].value+','+hook, description:data[group_id].description+', '+y18n.t('hook_'+hook), size:data[group_id].size + hook_size }; } else { data[group_id] = { name:y18n.t('hook_'+group_id), value:hook, description:(group_id==hook)?y18n.t('hook_'+hook+'_desc'):y18n.t('hook_'+hook), size:hook_size }; } }); return data; }, ungroupHooks: function(system_parts,apps) { var data = {}; data['apps'] = apps || []; data['system'] = system_parts || []; if (data['system'].constructor !== Array) { data['system'] = [data['system']]; } if (data['apps'].constructor !== Array) { data['apps'] = [data['apps']]; } // Some hook value contains multiple hooks separated by commas var split_hooks = []; $.each(data['system'], function(i, hook) { split_hooks = split_hooks.concat(hook.split(',')); }); data['system'] = split_hooks; if (data['system'].length == 0) { delete data['system']; } if (data['apps'].length == 0) { delete data['apps']; } return data; }, // 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')); // Add pacman loader $('#main').append(''); // 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(){ // Remove pacman $('div.loader').remove(); }); }); }, 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'); } }); })();