nextcloud/apps/theming/js/settings-admin.js

241 lines
7.4 KiB
JavaScript
Raw Normal View History

2016-06-06 18:40:38 +03:00
/**
* @author Björn Schießle <bjoern@schiessle.org>
*
* @copyright Copyright (c) 2016, Bjoern Schiessle
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
2016-06-22 14:44:33 +03:00
* published by the Free Software Foundation, either version 3 of the
2016-06-06 18:40:38 +03:00
* License, or (at your opinion) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
2016-06-09 22:46:30 +03:00
function startLoading() {
OC.msg.startSaving('#theming_settings_msg');
$('#theming_settings_loading').show();
}
function setThemingValue(setting, value) {
startLoading();
2016-06-09 22:46:30 +03:00
$.post(
OC.generateUrl('/apps/theming/ajax/updateStylesheet'), {'setting' : setting, 'value' : value}
).done(function(response) {
hideUndoButton(setting, value);
preview(setting, value, response.data.serverCssUrl);
}).fail(function(response) {
OC.msg.finishedSaving('#theming_settings_msg', response);
$('#theming_settings_loading').hide();
});
2016-06-09 22:46:30 +03:00
}
function preview(setting, value, serverCssUrl) {
OC.msg.startAction('#theming_settings_msg', t('theming', 'Loading preview…'));
var stylesheetsLoaded = 1;
var reloadStylesheets = function(cssFile) {
var queryString = '?reload=' + new Date().getTime();
var url = cssFile + queryString;
var old = $('link[href*="' + cssFile.replace("/","\/") + '"]');
var stylesheet = $("<link/>", {
rel: "stylesheet",
type: "text/css",
href: url
});
stylesheet.load(function () {
$(old).remove();
stylesheetsLoaded--;
if(stylesheetsLoaded === 0) {
$('#theming_settings_loading').hide();
var response = { status: 'success', data: {message: t('theming', 'Saved')}};
OC.msg.finishedSaving('#theming_settings_msg', response);
}
});
stylesheet.appendTo("head");
};
if (serverCssUrl !== undefined) {
stylesheetsLoaded++;
reloadStylesheets(serverCssUrl);
}
reloadStylesheets(OC.generateUrl('/apps/theming/styles'));
2016-08-24 12:33:54 +03:00
// Preview images
2016-08-24 12:33:54 +03:00
var timestamp = new Date().getTime();
if (setting === 'logoMime') {
2016-08-24 12:33:54 +03:00
var previewImageLogo = document.getElementById('theming-preview-logo');
if (value !== '') {
2016-08-24 12:33:54 +03:00
previewImageLogo.src = OC.generateUrl('/apps/theming/logo') + "?v" + timestamp;
} else {
previewImageLogo.src = OC.getRootPath() + '/core/img/logo.svg?v' + timestamp;
2016-08-24 12:33:54 +03:00
}
}
2016-08-26 19:21:05 +03:00
if (setting === 'name') {
window.document.title = t('core', 'Admin') + " - " + value;
}
hideUndoButton(setting, value);
}
function hideUndoButton(setting, value) {
var themingDefaults = {
name: 'Nextcloud',
slogan: t('lib', 'a safe home for all your data'),
url: 'https://nextcloud.com',
color: '#0082c9',
logoMime: '',
backgroundMime: ''
};
if (value === themingDefaults[setting] || value === '') {
$('.theme-undo[data-setting=' + setting + ']').hide();
} else {
$('.theme-undo[data-setting=' + setting + ']').show();
2016-06-09 22:46:30 +03:00
}
if(setting === 'backgroundMime' && value !== 'backgroundColor') {
$('.theme-remove-bg').show();
}
if(setting === 'backgroundMime' && value === 'backgroundColor') {
$('.theme-remove-bg').hide();
$('.theme-undo[data-setting=backgroundMime]').show();
}
2016-06-09 22:46:30 +03:00
}
$(document).ready(function () {
2016-06-27 21:36:23 +03:00
$('#theming [data-toggle="tooltip"]').tooltip();
2016-06-09 22:46:30 +03:00
$('#theming .theme-undo').each(function() {
var setting = $(this).data('setting');
var value = $('#theming-'+setting).val();
if(setting === 'logoMime' || setting === 'backgroundMime') {
var value = $('#current-'+setting).val();
}
hideUndoButton(setting, value);
});
var uploadParamsLogo = {
pasteZone: null,
dropZone: null,
done: function (e, response) {
preview('logoMime', response.result.data.name);
OC.msg.finishedSaving('#theming_settings_msg', response.result);
$('label#uploadlogo').addClass('icon-upload').removeClass('icon-loading-small');
$('.theme-undo[data-setting=logoMime]').show();
},
submit: function(e, response) {
startLoading();
$('label#uploadlogo').removeClass('icon-upload').addClass('icon-loading-small');
},
fail: function (e, response){
OC.msg.finishedError('#theming_settings_msg', response._response.jqXHR.responseJSON.data.message);
$('label#uploadlogo').addClass('icon-upload').removeClass('icon-loading-small');
}
};
var uploadParamsLogin = {
2016-06-09 22:46:30 +03:00
pasteZone: null,
dropZone: null,
done: function (e, response) {
preview('backgroundMime', response.result.data.name);
OC.msg.finishedSaving('#theming_settings_msg', response.result);
$('label#upload-login-background').addClass('icon-upload').removeClass('icon-loading-small');
$('.theme-undo[data-setting=backgroundMime]').show();
2016-06-09 22:46:30 +03:00
},
submit: function(e, response) {
startLoading();
$('label#upload-login-background').removeClass('icon-upload').addClass('icon-loading-small');
2016-06-09 22:46:30 +03:00
},
fail: function (e, response){
$('label#upload-login-background').removeClass('icon-loading-small').addClass('icon-upload');
OC.msg.finishedError('#theming_settings_msg', response._response.jqXHR.responseJSON.data.message);
2016-06-09 22:46:30 +03:00
}
};
2016-06-22 14:44:33 +03:00
$('#uploadlogo').fileupload(uploadParamsLogo);
$('#upload-login-background').fileupload(uploadParamsLogin);
// clicking preview should also trigger file upload dialog
$('#theming-preview-logo').on('click', function(e) {
e.stopPropagation();
$('#uploadlogo').click();
});
$('#theming-preview').on('click', function() {
$('#upload-login-background').click();
});
2016-06-09 22:46:30 +03:00
$('#theming-name').change(function(e) {
var el = $(this);
$.when(el.focusout()).then(function() {
setThemingValue('name', $(this).val());
});
2016-06-09 22:46:30 +03:00
if (e.keyCode == 13) {
setThemingValue('name', $(this).val());
}
});
$('#theming-url').change(function(e) {
var el = $(this);
$.when(el.focusout()).then(function() {
setThemingValue('url', $(this).val());
});
2016-06-09 22:46:30 +03:00
if (e.keyCode == 13) {
setThemingValue('url', $(this).val());
}
});
$('#theming-slogan').change(function(e) {
var el = $(this);
$.when(el.focusout()).then(function() {
setThemingValue('slogan', $(this).val());
});
2016-06-09 22:46:30 +03:00
if (e.keyCode == 13) {
setThemingValue('slogan', $(this).val());
}
});
$('#theming-color').change(function (e) {
setThemingValue('color', '#' + $(this).val());
});
2016-06-22 14:44:33 +03:00
2016-06-09 22:46:30 +03:00
$('.theme-undo').click(function (e) {
var setting = $(this).data('setting');
startLoading();
$('.theme-undo[data-setting=' + setting + ']').hide();
2016-06-09 22:46:30 +03:00
$.post(
OC.generateUrl('/apps/theming/ajax/undoChanges'), {'setting' : setting}
).done(function(response) {
2016-06-09 22:46:30 +03:00
if (setting === 'color') {
var colorPicker = document.getElementById('theming-color');
colorPicker.style.backgroundColor = response.data.value;
colorPicker.value = response.data.value.slice(1).toUpperCase();
} else if (setting !== 'logoMime' && setting !== 'backgroundMime') {
2016-06-09 22:46:30 +03:00
var input = document.getElementById('theming-'+setting);
input.value = response.data.value;
2016-06-09 22:46:30 +03:00
}
preview(setting, response.data.value, response.data.serverCssUrl);
2016-06-09 22:46:30 +03:00
});
});
$('.theme-remove-bg').click(function() {
startLoading();
$.post(
OC.generateUrl('/apps/theming/ajax/updateLogo'), {'backgroundColor' : true}
).done(function(response) {
preview('backgroundMime', 'backgroundColor');
}).fail(function(response) {
OC.msg.finishedSaving('#theming_settings_msg', response);
});
});
2016-06-09 22:46:30 +03:00
});