diff options
author | John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> | 2018-09-18 19:47:14 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2018-12-20 09:38:43 +0300 |
commit | ec9a21184695b99033ad70bceca892c1d1518560 (patch) | |
tree | ffb0d7fb0e5857707066c347cb45886326472b95 | |
parent | 6a7b73f5989a32a5ebd5b6ff341c9c6c8d4bb88e (diff) |
Fix DI error, added design base and delete handler
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
-rw-r--r-- | appinfo/routes.php | 2 | ||||
-rw-r--r-- | css/admin.scss | 51 | ||||
-rw-r--r-- | js/admin.js | 31 | ||||
-rw-r--r-- | lib/Controller/TemplatesController.php | 89 | ||||
-rw-r--r-- | lib/Settings/Admin.php | 12 | ||||
-rw-r--r-- | lib/TemplateManager.php | 98 | ||||
-rw-r--r-- | templates/admin.php | 10 |
7 files changed, 243 insertions, 50 deletions
diff --git a/appinfo/routes.php b/appinfo/routes.php index b74e2a1d..2f0c1124 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -53,7 +53,7 @@ return [ ['name' => 'assets#get', 'url' => 'assets/{token}', 'verb' => 'GET'], // templates - ['name' => 'templates#getPreview', 'url' => '/template/{templateName}', 'verb' => 'GET'], + ['name' => 'templates#getPreview', 'url' => '/template/preview/{templateName}', 'verb' => 'GET'], ['name' => 'templates#add', 'url' => '/template', 'verb' => 'POST'], ['name' => 'templates#delete', 'url' => '/template/{templateName}', 'verb' => 'DELETE'], ], diff --git a/css/admin.scss b/css/admin.scss index 9ba9b734..5965798d 100644 --- a/css/admin.scss +++ b/css/admin.scss @@ -44,4 +44,55 @@ } } } + ul { + display: flex; + li { + $size: 150px; + border-radius: var(--border-radius); + border: 1px solid var(--color-border); + margin: 10px; + position: relative; + figure { + display: flex; + flex-direction: column; + width: $size; + margin: 10px; + img { + width: $size; + height: $size; + background-color: var(--color-background-dark); + } + } + .delete-cover, + .delete-template { + width: $size; + height: $size; + top: 0; + left: 0; + position: absolute; + margin: 10px; + opacity: 0; + transition: opacity 250ms ease-in-out; + z-index: 3; + line-height: $size; + text-align: center; + font-size: 20px; + // text is set as bg + color: var(--color-background-darker); + } + .delete-cover { + // bg is set as color + background-color: var(--color-text-lighter); + z-index: 2; + } + &:hover .delete-template, + .delete-template:focus, + .delete-template.icon-loading { + opacity: 1; + + .delete-cover { + opacity: .5; + } + } + } + } }
\ No newline at end of file diff --git a/js/admin.js b/js/admin.js index 9984fd77..fb50ad98 100644 --- a/js/admin.js +++ b/js/admin.js @@ -265,7 +265,38 @@ var documentsSettings = { function initTemplateManager() { var inputElmt = document.querySelector('#add-template'); var buttonElmt = document.querySelector('.icon-add'); + var deleteElmts = document.querySelectorAll('.delete-template') + + deleteElmts.forEach(function(elmt) { + elmt.addEventListener('click', function(event) { + event.preventDefault(); + if (event.target.className.indexOf('loading') === -1 && elmt.textContent === '') { + var remote = event.target.href; + elmt.classList.add('icon-loading'); + elmt.classList.remove('icon-delete'); + + // send request + $.ajax({ + url: remote, + type: 'DELETE', + }) + .done(function() { + // Remove template + elmt.parentElement.remove(); + }) + .fail(function(e) { + elmt.textContent = t('richdocuments', 'Error'); + elmt.classList.remove('icon-loading'); + setTimeout(function() { + elmt.classList.add('icon-delete'); + elmt.textContent = ''; + }, 2000) + }) + } + }) + }) + $('#richdocuments-templates').fileupload({ dataType: 'json', url: OC.generateUrl(OC.linkTo('richdocuments', 'template')), diff --git a/lib/Controller/TemplatesController.php b/lib/Controller/TemplatesController.php index 5bb701f8..d43ba65b 100644 --- a/lib/Controller/TemplatesController.php +++ b/lib/Controller/TemplatesController.php @@ -26,10 +26,12 @@ namespace OCA\Richdocuments\Controller; use OCA\Richdocuments\TemplateManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; -use OCP\AppFramework\Http\DataDisplayResponse; +use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\NotFoundResponse; +use OCP\Files\Node; use OCP\IL10N; +use OCP\IPreview; use OCP\IRequest; use OC\Files\Filesystem; @@ -47,6 +49,9 @@ class TemplatesController extends Controller { /** @var TemplateManager */ private $manager; + /** @var IPreview */ + private $preview; + /** @var int Max template size */ private $maxSize = 20 * 1024 * 1024; @@ -57,17 +62,20 @@ class TemplatesController extends Controller { * @param IRequest $request * @param L10N $l10n * @param TemplateManager $manager + * @param IPreview $preview */ public function __construct(string $appName, IRequest $request, IL10N $l10n, - TemplateManager $manager) { + TemplateManager $manager, + IPreview $preview) { parent::__construct($appName, $request); - $this->appName = $appName; - $this->request = $request; - $this->l10n = $l10n; + $this->appName = $appName; + $this->request = $request; + $this->l10n = $l10n; $this->manager = $manager; + $this->preview = $preview; } /** @@ -77,16 +85,27 @@ class TemplatesController extends Controller { * Get preview for a specific template * * @param string $templateName The template id - * @return DataDisplayResponse|NotFoundResponse + * @return DataResponse + * @throws NotFoundResponse */ - public function getPreview(string $templateName) { - try { - $template = $this->templateManager->get($templateName); + public function getPreview(string $templateName, + int $x = 32, + int $y = 32, + bool $a = false, + bool $forceIcon = true, + string $mode = 'fill') { + + if ($template === '' || $x === 0 || $y === 0) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } - //return DataDisplayResponse($template->getPreview(), Http::STATUS_OK, ['Content-Type' => 'image/png']); + try { + $template = $this->manager->get($templateName); } catch (NotFoundException $e) { - return new NotFoundResponse(); + return new DataResponse([], Http::STATUS_NOT_FOUND); } + + return $this->fetchPreview($template, $x, $y, $a, $forceIcon, $mode); } /** @@ -94,7 +113,7 @@ class TemplatesController extends Controller { * * @return JSONResponse */ - public function add() { + public function add(): JSONResponse { $files = $this->request->getUploadedFile('files'); if (!is_null($files)) { @@ -115,10 +134,10 @@ class TemplatesController extends Controller { unlink($files['tmp_name'][0]); - $template = $this->templateManager->add($templateName, $templateFile); + $template = $this->manager->add($templateName, $templateFile); return new JSONResponse( - ['data' => ['data' => $template]], + ['data' => $template], Http::STATUS_CREATED ); } @@ -136,9 +155,9 @@ class TemplatesController extends Controller { * @param string $templateName * @return JSONResponse */ - public function delete(string $templateName) { + public function delete(string $templateName): JSONResponse { try { - $this->templateManager->delete($templateName); + $this->manager->delete($templateName); return new JSONResponse( ['data' => ['status' => 'success']], @@ -151,4 +170,42 @@ class TemplatesController extends Controller { ); } } + + /** + * @param Node $node + * @param int $x + * @param int $y + * @param bool $a + * @param bool $forceIcon + * @param string $mode + * @return DataResponse|FileDisplayResponse + */ + private function fetchPreview( + Node $node, + int $x, + int $y, + bool $a = false, + bool $forceIcon = true, + string $mode): Http\Response { + + if (!($node instanceof File) || (!$forceIcon && !$this->preview->isAvailable($node))) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + if (!$node->isReadable()) { + return new DataResponse([], Http::STATUS_FORBIDDEN); + } + + try { + $f = $this->preview->getPreview($node, $x, $y, !$a, $mode); + $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + $response->cacheFor(3600 * 24); + + return $response; + } catch (NotFoundException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } catch (\InvalidArgumentException $e) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + } } diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php index 13db7aaf..fd684f09 100644 --- a/lib/Settings/Admin.php +++ b/lib/Settings/Admin.php @@ -34,19 +34,19 @@ class Admin implements ISettings { private $config; /** @var TemplateManager */ - private $templateManager; + private $manager; /** * Admin template settings * * @param string $appName * @param IConfig $config - * @param TemplateManager $templateManager + * @param TemplateManager $manager */ public function __construct(IConfig $config, - TemplateManager $templateManager) { - $this->config = $config; - $this->templateManager = $templateManager; + TemplateManager $manager) { + $this->config = $config; + $this->manager = $manager; } /** * @return TemplateResponse @@ -62,7 +62,7 @@ class Admin implements ISettings { 'doc_format' => $this->config->getAppValue('richdocuments', 'doc_format'), 'external_apps' => $this->config->getAppValue('richdocuments', 'external_apps'), 'canonical_webroot' => $this->config->getAppValue('richdocuments', 'canonical_webroot'), - 'templates' => $this->templateManager->getGlobals() + 'templates' => $this->manager->getGlobals() ], 'blank' ); diff --git a/lib/TemplateManager.php b/lib/TemplateManager.php index 64899f12..92ff8bbd 100644 --- a/lib/TemplateManager.php +++ b/lib/TemplateManager.php @@ -25,6 +25,8 @@ namespace OCA\Richdocuments; use OCP\Files\Folder; use OCP\Files\IAppData; +use OCP\Files\IRootFolder; +use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; @@ -119,27 +121,18 @@ class TemplateManager { public function get(string $templateName) { try { // is this a global template ? - $template = $this->folder->getFile($templateName); + $templateFile = $this->folder->getFile($templateName); } catch (NotFoundException $e) { - // user defined template dir? - $templateDirID = $this->config->getUserValue($this->userId, $this->appName, 'template_dir', false); + $templateDir = $this->getUserTemplateDir(); + // finally get the template file try { - $templateDir = $this->userFolder->getById($templateDirID); + $templateFile = $templateDir->get($templateName); } catch (NotFoundException $e) { - // fallback to default template dir - try { - $templateDir = $this->userFolder->get('Templates'); - $template = $templateDir; - } catch (NotFoundException $e) { - return new NotFoundException(); - } + throw new NotFoundException($e); } } - var_dump($template); - - return true; - + return $this->formatNodeReturn($templateFile); } /** @@ -149,11 +142,7 @@ class TemplateManager { $templateFiles = $this->folder->getDirectoryListing(); return array_map(function (ISimpleFile $templateFile) { - return [ - 'name' => $templateFile->getName(), - 'preview' => $this->urlGenerator->linkToRoute('richdocuments.templates.getPreview', ['templateName' => $templateFile->getName()]), - 'ext' => $this->flipTypes[$templateFile->getMimeType()] - ]; + return $this->formatNodeReturn($templateFile); }, $templateFiles); } @@ -172,11 +161,36 @@ class TemplateManager { } $template->putContent($templateFile); - return $this->getGlobals; + return $this->get($templateName); + } + + /** + * Delete a template to the global template folder + * + * @param string $templateName + * @param string $templateFile + * @return void + */ + public function delete(string $templateName) { + try { + $template = $this->folder->getFile($templateName); + // $template->delete(); + return 1; + } catch (NotFoundException $e) { + $templateDir = $this->getUserTemplateDir(); + try { + $templateFile = $templateDir->get($templateName); + // $templateFile->delete(); + return 2; + } catch (NotFoundException $e) { + throw new NotFoundException($e); + } + } + return 0; } /** - * Flip tplTypes to retrieve types by mime + * Flip $tplTypes to retrieve types by mime * * @return array */ @@ -189,4 +203,44 @@ class TemplateManager { return $result; } + + /** + * Get the user template directory + * + * @return Node + * @throws NotFoundException + */ + private function getUserTemplateDir(): Node { + // has the user manually set a directory as the default template dir ? + $templateDirID = $this->config->getUserValue($this->userId, $this->appName, 'template_dir', false); + + if ($templateDirID !== false) { + $templateDir = $this->userFolder->getById($templateDirID); + } else { + // fallback to default template dir + try { + $templateDir = $this->userFolder->get('Templates'); + } catch (NotFoundException $e) { + throw new NotFoundException($e); + } + } + + return $templateDir; + } + + /** + * Format template file for json return object + * + * @param ISimpleFile/Node $template + * @return array + */ + private function formatNodeReturn($template): array{ + return [ + 'name' => $template->getName(), + 'preview' => 'https://dev.skjnldsv.com/remote.php/webdav/Images/2gctzk0ijmg11.jpg', + 'ext' => $this->flipTypes[$template->getMimeType()], + 'etag' => $template->getETag(), + 'delete' => $this->urlGenerator->linkToRoute('richdocuments.templates.delete', ['templateName' => $template->getName()]), + ]; + } } diff --git a/templates/admin.php b/templates/admin.php index 00db10bf..d6863074 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -61,13 +61,13 @@ script('files', 'jquery.fileupload'); <?php if (!empty($_['templates'])) { ?> <ul> <?php foreach ($_['templates'] as $template) {?> - <li> + <li data-id="<?php p($template['name']) ?>"> <figure> - <figcaption><?php p($l->t('description')) ?></figcaption> + <img src="<?php p($template['preview']) ?>" alt="<?php p($l->t('template preview')) ?>" /> + <figcaption><?php p($template['name']) ?></figcaption> </figure> - <div> - <a href="#" class="icon-delete"></a> - </div> + <a href="<?php p($template['delete']) ?>" class="delete-template icon-delete"></a> + <div class="delete-cover"></div> </li> <?php } ?> </ul> |