Compare commits

..

2 Commits

Author SHA1 Message Date
jdg
296ad3a2ce Wiki navigation bar 2022-03-27 22:01:46 +00:00
jdg
4807b7d4ae Wiki navigation bar 2022-03-27 22:00:29 +00:00
18 changed files with 491 additions and 48 deletions

View File

@ -1,6 +1,16 @@
# My Wiki
Place this app in **nextcloud/apps/**
## Disclaimer:
**This is not a real wiki**
This app allow me to display the folders where I organize some documentation within a browsable struture
Many things could be wrong (this is my first app for nextcloud), I would appreciate any comments/help
## Building the app
The app can be built by using the provided Makefile by running:

View File

@ -1,3 +1,23 @@
#hello {
color: red;
}
li[data-id="wikis"] select {
width: calc(100% - 50px);
}
.-myWikiControls {
width: 100%;
margin: .25em;
}
.-myWikiControls select {
width: 98%;
}
option.separator {
font-size: 1px;
min-height:1px;
max-height:1px;
padding:0;
background-color: #000000;
}

37
js/WikiDropdownHelper.js Normal file
View File

@ -0,0 +1,37 @@
'use strict';
class WikiDropdownHelper {
constructor(element) {
this.dd = element;
}
clear() {
while (this.dd.hasChildNodes()) {
this.dd.removeChild(this.dd.firstChild);
}
return this;
}
add(text, value, set=false) {
var option = document.createElement("option");
option.text = text;
option.value = value;
this.dd.appendChild(option);
if ( set ) {
this.set(value);
}
return this;
}
rename(value, text) {
this.dd.querySelector(`option[value="${value}"]`).innerHTML=text;
return this;
}
get() {
return {
text:this.dd.parentElement.value,
value:this.dd.parentElement.text
};
}
set(value) {
this.dd.parentElement.value=value;
// this.onSelectWiki(value);
}
}

123
js/WikiNavigation.js Normal file
View File

@ -0,0 +1,123 @@
'use strict';
class WikiNavigation {
dd = null;
onSelectWiki = null;
constructor(container, onSelectWiki){
let self = this;
this.container = container;
this.onSelectWiki = onSelectWiki;
let wikiSelector = container.getElementsByTagName('select')[0];
this.dd = new WikiDropdownHelper(wikiSelector);
wikiSelector.addEventListener('change', e=>{
if(self.onSelectWiki) {
self.onSelectWiki(+e.target.value||0);
}
});
this.loadWikis();
// Popup menu
let appNavigationMenu = container.getElementsByClassName('app-navigation-entry-menu')[0];
let button = container.querySelector('.app-navigation-entry-utils-menu-button button');
button.addEventListener('click', ()=>appNavigationMenu.classList.toggle("open") );
document.addEventListener('click', e=>{if(e.target!==button)appNavigationMenu.classList.remove("open");})
appNavigationMenu.querySelector('[data-id="add"]').addEventListener('click', ()=>self.wikiChooseFolder() );
appNavigationMenu.querySelector('[data-id="rename"]').addEventListener('click', ()=>self.wikiRename() );
appNavigationMenu.querySelector('[data-id="delete"]').addEventListener('click', ()=>self.wikiDelete() );
}
wikiRename() {
let self=this;
OC.dialogs.prompt(
t(appName, 'This allow you to rename the displayed name for the selected wiki. (The folder will remain unchanged)'),
t(appName, 'Rename Wiki'),
(ok,value)=>{
if(ok) {
value = value.trim();
if(value!='') {
let wiki = self.dd.get();
var baseUrl = OC.generateUrl('/apps/mywiki/wikis');
$.ajax({
url: baseUrl,
type: 'PUT',
contentType: 'application/json',
data: JSON.stringify({id:wiki.value, title:value})
}).done(function (response) {
console.info('JDG :: Wiki renamed', response);
// ToDo :: Rename in the dropdown
}).fail(function (response, code) {
OC.dialogs.alert('Error', t(appName,'Error renaming wiki'));
console.error('JDG :: Error renaming wiki', response);
});
}
}
},
false,
t(appName, 'New name:'),
false
);
}
loadWikis() {
let self = this;
this.dd.clear().add(t(appName, 'Loading...'), '', true);
var baseUrl = OC.generateUrl('/apps/mywiki/wikis');
$.ajax({
url: baseUrl,
type: 'GET',
contentType: 'application/json'
}).done(function (response) {
console.info('JDG :: Wikis loaded', response);
self.dd.clear().add('','');
response.forEach( x=>self.dd.add(x.title, x.id) );
}).fail(function (response, code) {
OC.dialogs.alert('Error', t(appName,'Error getting the list of wikis'));
console.error('JDG :: Error getting the wikis', response);
self.dd.clear();
});
}
wikiChooseFolder() {
let self = this;
this.dd.set('');
window.OC.dialogs.filepicker(
t(appName, 'Select Wiki folder'),
(path, type) => {
if (type === OC.dialogs.FILEPICKER_TYPE_CHOOSE) {
self.wikiAdd(path, path.split('/').pop());
}
},
false,
['httpd/unix-directory'],
true,
OC.dialogs.FILEPICKER_TYPE_CHOOSE,
'', // Path
{ allowDirectoryChooser: true }
);
}
wikiAdd(folderPath, title) {
let self = this;
var baseUrl = OC.generateUrl('/apps/mywiki/wikis');
$.ajax({
url: baseUrl,
type: 'POST',
data: JSON.stringify({title:title, folderPath:folderPath}),
contentType: 'application/json'
}).done(function (response) {
console.info('JDG :: wikiAdd :: Wiki added', response);
if ( response.id>0 ) {
self.dd.add(response.title, response.id, true);
}
}).fail(function (response, code) {
OC.dialogs.alert('Error', t(appName,'It has not been possible to add the new wiki'));
console.error('JDG :: wikiAdd :: Error adding the wiki', response);
});
}
}

View File

@ -1,10 +1,55 @@
const appName = 'MyWiki';
class WikiPages {
constructor(container){
this.wikiId = null;
}
load(wikiId) {
console.info('JDG :: Loading wiki', wikiId );
this.wikiId = wikiId;
}
getWikiId() {
return this.wikiId;
}
add(parentPageId, title) {
}
delete() {
}
rename() {
}
}
class WikiEditor {
load(wikiId, wikiPageId) {
console.info(`JDG :: Loading Wiki ${wikiId}/${wikiPageId}`);
}
}
var MyWiki = MyWiki || {};
(function(window, $, exports, undefined) {
'use strict';
$('#MyWiki-test').on('click',test);
let wikiNavigation = new WikiNavigation(document.querySelector('li[data-id="wikis"]'), onSelectWiki);
let wikiPages = new WikiPages(document.querySelector('li[data-id="pages"]'), onSelectWikiPage);
function onSelectWiki(wikiId) {
console.info(`JDG :: WikiList selected ${wikiId}` );
if ( wikiId > 0 ) {
wikiPages.load(wikiId);
}
}
function onSelectWikiPage(wikiPageId) {
console.info(`JDG :: WikiPage selected ${wikiPageId}` );
if ( wikiPageId > 0 ) {
// wikiEditor.load(wikiPage.getWikiId(), wikiPageId );
}
}
// ---------------------------------------------------------------------------------
$(`#${appName}-test`).on('click',test);
function test() {
var baseUrl = OC.generateUrl('/apps/mywiki/wikis');
$.ajax({
@ -19,13 +64,8 @@ var MyWiki = MyWiki || {};
$('output').html('<h2>'+response.statusText+'</h2><code>'+response.responseText+'</code>');
});
}
// ---------------------------------------------------------------------------------
/*
// if this function or object should be global, attach it to the namespace
exports.myGlobalFunction = function(params) {
return params;
};
*/
})(window, jQuery, MyWiki);

View File

@ -51,12 +51,12 @@
/**
* @NoAdminRequired
*
* @param string $folderPath
* @param string $title
* @param string $fileId
*/
public function create(string $title, int $fileId) {
return $this->handleReadOnly(function () use ($title, $fileId) {
return $this->service->create($title, $fileId, $this->userId);
public function create(string $folderPath, string $title) {
return $this->handleReadOnly(function () use ($folderPath, $title) {
return $this->service->create($folderPath, $title, $this->userId);
});
}

View File

@ -19,7 +19,7 @@ class Wiki extends Entity implements JsonSerializable {
return [
'id' => $this->id,
'title' => $this->title,
'file_id' => $this->file_id
'fileId' => $this->fileId
];
}
}

View File

@ -7,7 +7,7 @@ use OCP\AppFramework\Db\QBMapper;
class WikiMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'mywiki_wikis', Note::class);
parent::__construct($db, 'mywiki', Wiki::class);
}
public function usersCount(int $folderId) {
@ -31,11 +31,10 @@ class WikiMapper extends QBMapper {
)->andWhere(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId))
);
return $this->findEntity($qb);
}
public function findAll(string $userId) {
public function findAll(string $userId, mixed $filter=null) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
@ -44,6 +43,18 @@ class WikiMapper extends QBMapper {
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId))
);
if ($filter) {
if (array_key_exists('title',$filter) ) {
$qb->where(
$qb->expr()->eq('title', $qb->createNamedParameter($id))
);
}
if (array_key_exists('fileId',$filter) ) {
$qb->where(
$qb->expr()->eq('fileId', $qb->createNamedParameter($id))
);
}
}
return $this->findEntities($qb);
}

View File

@ -96,16 +96,24 @@ class WikiHelper {
return true;
}
public function initWiki(string $title) :bool {
public function initWiki(string $folderPath, string $title) :?int {
$this->wikiFolder = $this->userFolder->get($folderPath);
if ( !$this->isWiki() ) {
return null;
}
$folderId = $this->wikiFolder->getId();
if ( $this->getWikiData() === null ) {
$wiki = [
"title"=>$title,
"folderId"=>$this->wikiFolder->getId(),
"folderId"=>$folderId,
"pages"=>$this->rebuildWikiTree()
];
if ( $this->getWikiData() === null ) {
return $this->setWikiData($wiki);
if ( !$this->setWikiData($wiki) ) {
return null;
}
return true;
}
return $folderId;
}
public function add(int $parentId, string $title) {

83
lib/Helper/WikiTree.php Normal file
View File

@ -0,0 +1,83 @@
<?php
namespace OCA\MyWiki\Helper;
class WikiTree {
private array $wikiPages;
public function __construct(?array $wikiPages) {
$this->wikiPages = $wikiPages??[];
}
public function getWikiPages(): array {
return $this->wikiPages;
}
public function get($id): ?WikiTreePage {
$wikiTreePage = null;
foreach($this->wikiPages as $page) {
if ( $page['id']==$id) {
$wikiTreePage = new WikiTreePage();
$wikiTreePage->id = $page['id'];
$wikiTreePage->pid = $page['pid'];
$wikiTreePage->title = $page['title'];
$wikiTreePage->sort = $page['sort'];
break;
}
}
return $wikiTreePage;
}
private function countChilds($id): int {
$n = 0;
foreach($this->wikiPages as $page) {
if ($page['pid']==$id) {
$n++;
}
}
return $n;
}
public function set(WikiTreePage $wikiTreePage): WikiTree {
if ( $this->get($wikiTreePage->id) === null ) {
$this->add($wikiTreePage);
} else {
$this->modify($wikiTreePage);
}
return $this;
}
private function add(WikiTreePage $wikiTreePage): WikiTree {
if ($wikiTreePage->sort<=0) {
$wikiTreePage->sort = $this->countChilds($wikiTreePage->pid) + 1;
}
$this->wikiPages[] = [
"id"=>$wikiTreePage->id,
"pid"=>$wikiTreePage->pid,
"title"=>$wikiTreePage->title,
"sort"=>$wikiTreePage->sort
];
return $this;
}
private function modify(WikiTreePage $wikiTreePage): WikiTree {
foreach($this->wikiPages as &$page) {
if ( $page['id']==$wikiTreePage->id) {
$page['pid'] = $wikiTreePage->pid;
$page['title'] = $wikiTreePage->title;
$page['sort'] = $wikiTreePage->sort;
}
}
return $this;
}
public function del($id): ?WikiTree {
$pages = [];
foreach($this->wikiPages as $k => $page) {
if ($page['id']!=$id && $page['pid']!=$id) {
$pages[] = $page;
}
}
$this->wikiPages = $pages;
return $this;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace OCA\MyWiki\Helper;
class WikiTreePage {
public int $id;
public int $pid;
public string $title;
public int $sort = 0;
}

View File

@ -39,6 +39,7 @@
$table->setPrimaryKey(['id']);
$table->addIndex(['user_id'], 'mywiki_user_id_index');
$table->addIndex(['file_id'], 'mywiki_file_id_index');
}
/*

View File

@ -25,7 +25,7 @@ class WikiService {
}
public function test() {
$folderId = 381;
$folderId = 880;
return $this->wikiHelper->setFolderId($folderId)->reloadWikiTree();
return $this->wikiHelper->setFolderId($folderId)->rename(707, 'UnoMasUno');
return $this->wikiHelper->setFolderId($folderId)->rename(647, 'RenameTest3');
@ -57,17 +57,20 @@ class WikiService {
}
}
public function create(string $title, int $fileId, string $userId) {
$this->wikiHelper->setFolderId($fileId);
if ( $this->wikiHelper->isWiki() ) {
if ( !$this->wikiHelper->initWiki($title) ) {
public function create(string $folderPath, string $title, string $userId) {
$folderId = $this->wikiHelper->initWiki($folderPath, $title);
if ( $folderId === null ) {
throw new ReadOnlyException('Error creating wiki');
}
$wikis = $this->mapper->findAll($userId, ['fileId'=>$folderId]);
if ( count($wikis)>0 ) {
return $wikis[0];
}
$wiki = new Wiki();
$wiki->setTitle($title);
$wiki->setFileId($fileId);
$wiki->setFileId($folderId);
$wiki->setUserId($userId);
return $this->mapper->insert($wiki);
}

View File

@ -1,4 +1,6 @@
<?php
\OCP\Util::addScript('mywiki', 'WikiDropdownHelper');
\OCP\Util::addScript('mywiki', 'WikiNavigation');
\OCP\Util::addScript('mywiki', 'script');
\OCP\Util::addStyle('mywiki', 'style');
?>

View File

@ -1,10 +1,51 @@
<ul>
<li><a href="#">First level entry</a></li>
<li>
<a href="#">First level container</a>
<li data-id="wikis">
<select></select>
<div class="app-navigation-entry-utils">
<ul>
<li><a href="#">Second level entry</a></li>
<li><a href="#">Second level entry</a></li>
</ul>
<li class="app-navigation-entry-utils-menu-button">
<button></button>
</li>
</ul>
</div>
<div class="app-navigation-entry-menu">
<ul>
<li>
<a href="#" data-id="add">
<span class="icon-add"></span>
<span>Add</span>
</a>
</li>
<li>
<a href="#" data-id="rename">
<span class="icon-rename"></span>
<span>Edit</span>
</a>
</li>
<li>
<a href="#" data-id="delete">
<span class="icon-delete"></span>
<span>Remove</span>
</a>
</li>
</ul>
</div>
</li>
<li data-id="pages">
</li>
</ul>
<div id="myWikiTree">
no data
</div>

View File

@ -6,5 +6,10 @@
</div>
<div id="app-settings-content">
<!-- Your settings in here -->
<ul class="with-icon">
<li data-id="deleteWiki">
<a href="#" class="icon-delete svg">Delete Current Wiki</a>
</li>
</ul>
</div>
</div>

View File

@ -0,0 +1,50 @@
<?php
// sudo ./vendor/phpunit/phpunit/phpunit tests/Unit/Helper/WikiTreeHelperTest.php
namespace OCA\MyWiki\Tests\Unit\Helper;
use PHPUnit\Framework\TestCase;
use OCA\MyWiki\Helper\WikiTree;
use OCA\MyWiki\Helper\WikiTreePage;
class WikiTreeHelperTest extends TestCase {
public function testGetSetDel() {
$wikiTree = new WikiTree(null);
$wikiPage = new WikiTreePage();
$wikiPage->id = 1;
$wikiPage->pid = 0;
$wikiPage->title = 'Page1';
$wikiPage->sort = 0;
$wikiTree->set($wikiPage);
$wikiPage->id = 2;
$wikiPage->pid = 0;
$wikiPage->title = 'Page3';
$wikiPage->sort = 0;
$wikiTree->set($wikiPage);
$wikiPage->title = 'Page2';
$wikiTree->set($wikiPage);
$wikiPage->id = 11;
$wikiPage->pid = 1;
$wikiPage->title = 'Page1.1';
$wikiPage->sort = 0;
$wikiTree->set($wikiPage);
$pages = $wikiTree->getWikiPages();
$this->assertEquals(3, count($pages));
$this->assertEquals('Page2', $pages[1]['title']);
$wikiTree->del(1);
$pages = $wikiTree->getWikiPages();
$this->assertEquals(1, count($pages));
$page = $wikiTree->get(2);
$this->assertEquals(2, $page->id);
$this->assertEquals(0, $page->pid);
$this->assertEquals('Page2', $page->title);
$this->assertEquals(2, $page->sort);
}
}

View File

@ -5,7 +5,7 @@
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => 'a30a4f13ec699c6e80f32148dc3aa017511c5f5a',
'reference' => '9c6114cf8643a84a9536074942d2e7f1a8451cd0',
'name' => 'jdg/mywiki',
'dev' => true,
),
@ -25,7 +25,7 @@
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => 'a30a4f13ec699c6e80f32148dc3aa017511c5f5a',
'reference' => '9c6114cf8643a84a9536074942d2e7f1a8451cd0',
'dev_requirement' => false,
),
'myclabs/deep-copy' => array(