Skip to content

Commit

Permalink
Theming: Add preview for login screen
Browse files Browse the repository at this point in the history
Theming: Hide undo button on default values

Add unit tests ans small js fixes

Use IRootFolder for checking image existence

Fix CSS code style
  • Loading branch information
juliushaertl committed Aug 22, 2016
1 parent 91e8b33 commit ba6698d
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 10 deletions.
14 changes: 14 additions & 0 deletions apps/theming/css/settings-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@
div#theming_settings_msg {
margin-left: 10px;
}

#theming-preview {
width: 230px;
height: 140px;
background-size: cover;
background-position: center center;
text-align: center;
margin-left: 93px;
}
#theming-preview img {
max-width: 20%;
max-height: 20%;
margin-top: 20px;
}
49 changes: 44 additions & 5 deletions apps/theming/js/settings-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function preview(setting, value) {
textColor = "#ffffff";
icon = 'caret';
}
if (luminance>0.8) {
if (luminance > 0.8) {
elementColor = '#555555';
}

Expand All @@ -87,17 +87,47 @@ function preview(setting, value) {
'background-image: url(\'data:image/svg+xml;base64,' + generateRadioButton(elementColor) + '\'); }'
);
}

var timestamp = new Date().getTime();
if (setting === 'logoMime') {
console.log(setting);
var logos = document.getElementsByClassName('logo-icon');
var timestamp = new Date().getTime();
var previewImageLogo = document.getElementById('theming-preview-logo');
if (value !== '') {
logos[0].style.backgroundImage = "url('" + OC.generateUrl('/apps/theming/logo') + "?v" + timestamp + "')";
logos[0].style.backgroundSize = "contain";
previewImageLogo.src = OC.generateUrl('/apps/theming/logo') + "?v" + timestamp;
} else {
logos[0].style.backgroundImage = "url('" + OC.getRootPath() + '/core/img/logo-icon.svg?v' + timestamp +"')";
logos[0].style.backgroundImage = "url('" + OC.getRootPath() + '/core/img/logo-icon.svg?v' + timestamp + "')";
logos[0].style.backgroundSize = "contain";
previewImageLogo.src = OC.getRootPath() + '/core/img/logo-icon.svg?v' + timestamp;
}
}
if (setting === 'backgroundMime') {
var previewImage = document.getElementById('theming-preview');
if (value !== '') {
previewImage.style.backgroundImage = "url('" + OC.generateUrl('/apps/theming/loginbackground') + "?v" + timestamp + "')";
} else {
previewImage.style.backgroundImage = "url('" + OC.getRootPath() + '/core/img/background.jpg?v' + timestamp + "')";
}

}
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();
}
}

Expand All @@ -106,6 +136,14 @@ $(document).ready(function () {

$('html > head').append($('<style type="text/css" id="previewStyles"></style>'));

$('#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,
Expand Down Expand Up @@ -181,11 +219,12 @@ $(document).ready(function () {
if (setting === 'color') {
var colorPicker = document.getElementById('theming-color');
colorPicker.style.backgroundColor = response.data.value;
colorPicker.value = response.data.value.slice(1);
colorPicker.value = response.data.value.slice(1).toUpperCase();
} else if (setting !== 'logoMime' && setting !== 'backgroundMime') {
var input = document.getElementById('theming-'+setting);
input.value = response.data.value;
}

preview(setting, response.data.value);
OC.msg.finishedSaving('#theming_settings_msg', response);
});
Expand Down
4 changes: 4 additions & 0 deletions apps/theming/lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function getForm() {
'url' => $this->themingDefaults->getBaseUrl(),
'slogan' => $this->themingDefaults->getSlogan(),
'color' => $this->themingDefaults->getMailHeaderColor(),
'logo' => $this->themingDefaults->getLogo(),
'logoMime' => $this->config->getAppValue('theming', 'logoMime', ''),
'background' => $this->themingDefaults->getBackground(),
'backgroundMime' => $this->config->getAppValue('theming', 'backgroundMime', ''),
'uploadLogoRoute' => $path,
];

Expand Down
34 changes: 33 additions & 1 deletion apps/theming/lib/ThemingDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Files\IRootFolder;


class ThemingDefaults extends \OC_Defaults {
Expand All @@ -46,6 +47,8 @@ class ThemingDefaults extends \OC_Defaults {
private $slogan;
/** @var string */
private $color;
/** @var IRootFolder */
private $rootFolder;

/**
* ThemingDefaults constructor.
Expand All @@ -54,16 +57,19 @@ class ThemingDefaults extends \OC_Defaults {
* @param IL10N $l
* @param IURLGenerator $urlGenerator
* @param \OC_Defaults $defaults
* @param IRootFolder $rootFolder
*/
public function __construct(IConfig $config,
IL10N $l,
IURLGenerator $urlGenerator,
\OC_Defaults $defaults
\OC_Defaults $defaults,
IRootFolder $rootFolder
) {
parent::__construct();
$this->config = $config;
$this->l = $l;
$this->urlGenerator = $urlGenerator;
$this->rootFolder = $rootFolder;

$this->name = $defaults->getName();
$this->url = $defaults->getBaseUrl();
Expand Down Expand Up @@ -113,6 +119,32 @@ public function getMailHeaderColor() {
return $this->config->getAppValue('theming', 'color', $this->color);
}

/**
* Themed logo url
*
* @return string
*/
public function getLogo() {
$logo = $this->config->getAppValue('theming', 'logoMime');
if(!$logo || !$this->rootFolder->nodeExists('themedinstancelogo')) {
return $this->urlGenerator->imagePath('core','logo.svg');
} else {
return $this->urlGenerator->linkToRoute('theming.Theming.getLogo');
}
}
/**
* Themed background image url
*
* @return string
*/
public function getBackground() {
$backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime');
if(!$backgroundLogo || !$this->rootFolder->nodeExists('themedbackgroundlogo')) {
return $this->urlGenerator->imagePath('core','background.jpg');
} else {
return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground');
}
}
/**
* Increases the cache buster key
*/
Expand Down
9 changes: 7 additions & 2 deletions apps/theming/templates/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,24 @@
</p>
<p>
<form class="uploadButton" method="post" action="<?php p($_['uploadLogoRoute']) ?>">
<input type="hidden" id="current-logoMime" name="current-logoMime" value="<?php p($_['logoMime']); ?>" />
<label for="uploadlogo"><span><?php p($l->t('Logo')) ?></span></label>
<input id="uploadlogo" class="upload-logo-field" name="uploadlogo" type="file">
<input id="uploadlogo" class="upload-logo-field" name="uploadlogo" type="file" />
<label for="uploadlogo" class="button icon-upload svg" id="uploadlogo" title="<?php p($l->t('Upload new logo')) ?>"></label>
<span data-setting="logoMime" data-toggle="tooltip" data-original-title="<?php p($l->t('reset to default')); ?>" class="theme-undo icon icon-history"></span>
</form>
</p>
<p>
<form class="uploadButton" method="post" action="<?php p($_['uploadLogoRoute']) ?>">
<input type="hidden" id="current-backgroundMime" name="current-backgroundMime" value="<?php p($_['backgroundMime']); ?>" />
<label for="upload-login-background"><span><?php p($l->t('Log in image')) ?></span></label>
<input id="upload-login-background" class="upload-logo-field" name="upload-login-background" type="file">
<label for="upload-login-background" class="button icon-upload svg" id="upload-login-background" title="<?php p($l->t("Upload new login background")) ?>"></label>
<span data-setting="backgroundMime" data-toggle="tooltip" data-original-title="<?php p($l->t('reset to default')); ?>" class="theme-undo icon icon-history"></span>
</form>
</p>
</p>
<div id="theming-preview" style="background-color:<?php p($_['color']);?>; background-image:url(<?php p($_['background']); ?>);">
<img src="<?php p($_['logo']); ?>" id="theming-preview-logo" />
</div>
<?php } ?>
</div>
8 changes: 8 additions & 0 deletions apps/theming/tests/Settings/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public function testGetFormNoErrors() {
'slogan' => 'MySlogan',
'color' => '#fff',
'uploadLogoRoute' => '/my/route',
'logo' => null,
'logoMime' => null,
'background' => null,
'backgroundMime' => null,
];

$expected = new TemplateResponse('theming', 'settings-admin', $params, '');
Expand Down Expand Up @@ -139,6 +143,10 @@ public function testGetFormWithErrors() {
'slogan' => 'MySlogan',
'color' => '#fff',
'uploadLogoRoute' => '/my/route',
'logo' => null,
'logoMime' => null,
'background' => null,
'backgroundMime' => null,
];

$expected = new TemplateResponse('theming', 'settings-admin', $params, '');
Expand Down
47 changes: 46 additions & 1 deletion apps/theming/tests/ThemingDefaultsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Files\IRootFolder;
use Test\TestCase;

class ThemingDefaultsTest extends TestCase {
Expand All @@ -40,11 +41,14 @@ class ThemingDefaultsTest extends TestCase {
private $defaults;
/** @var ThemingDefaults */
private $template;
/** @var IRootFolder */
private $rootFolder;

public function setUp() {
$this->config = $this->getMock('\\OCP\\IConfig');
$this->l10n = $this->getMock('\\OCP\\IL10N');
$this->urlGenerator = $this->getMock('\\OCP\\IURLGenerator');
$this->rootFolder = $this->getMock('\\OCP\\Files\\IRootFolder');
$this->defaults = $this->getMockBuilder('\\OC_Defaults')
->disableOriginalConstructor()
->getMock();
Expand All @@ -68,7 +72,8 @@ public function setUp() {
$this->config,
$this->l10n,
$this->urlGenerator,
$this->defaults
$this->defaults,
$this->rootFolder
);

return parent::setUp();
Expand Down Expand Up @@ -368,4 +373,44 @@ public function testUndoDefaultAction() {

$this->assertSame('', $this->template->undo('defaultitem'));
}

public function testGetBackgroundDefault() {
$this->config
->expects($this->once())
->method('getAppValue')
->with('theming', 'backgroundMime')
->willReturn('');
$expected = $this->urlGenerator->imagePath('core','background.jpg');
$this->assertEquals($expected, $this->template->getBackground());
}

public function testGetBackgroundCustom() {
$this->config
->expects($this->once())
->method('getAppValue')
->with('theming', 'backgroundMime')
->willReturn('image/svg+xml');
$expected = $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground');
$this->assertEquals($expected, $this->template->getBackground());
}

public function testGetLogoDefault() {
$this->config
->expects($this->once())
->method('getAppValue')
->with('theming', 'logoMime')
->willReturn('');
$expected = $this->urlGenerator->imagePath('core','logo.svg');
$this->assertEquals($expected, $this->template->getLogo());
}

public function testGetLogoCustom() {
$this->config
->expects($this->once())
->method('getAppValue')
->with('theming', 'logoMime')
->willReturn('image/svg+xml');
$expected = $this->urlGenerator->linkToRoute('theming.Theming.getLogo');
$this->assertEquals($expected, $this->template->getLogo());
}
}
3 changes: 2 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,8 @@ public function __construct($webRoot, \OC\Config $config) {
$this->getConfig(),
$this->getL10N('theming'),
$this->getURLGenerator(),
new \OC_Defaults()
new \OC_Defaults(),
$this->getRootFolder()
);
}
return new \OC_Defaults();
Expand Down

0 comments on commit ba6698d

Please sign in to comment.