Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add UI elements to modify navigation display #1295

Open
wants to merge 14 commits into
base: enh/1177/enable-nav-display-logic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions lib/Db/ContextMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ protected function formatResultRows(array $rows, ?string $userId) {
'display_mode_default' => (int)$item['display_mode_default'],
];
if ($userId !== null) {
if ($item['display_mode'] === null) {
$item['display_mode'] = $item['display_mode_default'];
}
$carry[$item['share_id']]['display_mode'] = (int)$item['display_mode'];
}
return $carry;
Expand Down Expand Up @@ -168,17 +171,27 @@ public function findAll(?string $userId = null): array {

return $resultEntities;
}

public function findForNavBar(string $userId): array {
$qb = $this->getFindContextBaseQuery($userId);
$qb->andWhere($qb->expr()->andX(
$qb->andWhere($qb->expr()->orX(
// default
$qb->expr()->gt('n.display_mode', $qb->createNamedParameter(Application::NAV_ENTRY_MODE_HIDDEN, IQueryBuilder::PARAM_INT)),
// user override
$qb->expr()->orX(
$qb->expr()->gt('n2.display_mode', $qb->createNamedParameter(Application::NAV_ENTRY_MODE_HIDDEN, IQueryBuilder::PARAM_INT)),
$qb->expr()->andX(
// requires lack of user overwrite, indicated by n2.display_mode
$qb->expr()->isNull('n2.display_mode'),
)
// requires a display mode also depending on the role…
$qb->expr()->orX(
// not an owner: requires RECIPIENT or ALL
$qb->expr()->andX(
// groups are not considered, yet
$qb->expr()->neq('c.owner_id', $qb->createNamedParameter($userId)),
$qb->expr()->gt('n.display_mode', $qb->createNamedParameter(Application::NAV_ENTRY_MODE_HIDDEN, IQueryBuilder::PARAM_INT)),
),
// an owner (no explicit check necessary): requires ALL
$qb->expr()->eq('n.display_mode', $qb->createNamedParameter(Application::NAV_ENTRY_MODE_ALL, IQueryBuilder::PARAM_INT)),
),
),
// user override
$qb->expr()->gt('n2.display_mode', $qb->createNamedParameter(Application::NAV_ENTRY_MODE_HIDDEN, IQueryBuilder::PARAM_INT)),
));

$result = $qb->executeQuery();
Expand Down
60 changes: 60 additions & 0 deletions lib/Db/ContextNavigationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCA\Tables\Db;

use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
Expand Down Expand Up @@ -41,4 +42,63 @@ public function setDisplayModeByShareId(int $shareId, int $displayMode, string $

return $this->insertOrUpdate($entity);
}

// we have to overwrite QBMapper`s insert() because we do not have
// an id column in this table. Sad.
public function insert(Entity $entity): Entity {
// get updated fields to save, fields have to be set using a setter to
// be saved
$properties = $entity->getUpdatedFields();

$qb = $this->db->getQueryBuilder();
$qb->insert($this->tableName);

// build the fields
foreach ($properties as $property => $updated) {
$column = $entity->propertyToColumn($property);
$getter = 'get' . ucfirst($property);
$value = $entity->$getter();

$type = $this->getParameterTypeForProperty($entity, $property);
$qb->setValue($column, $qb->createNamedParameter($value, $type));
}

$qb->executeStatement();

return $entity;
}

// we have to overwrite QBMapper`s update() because we do not have
// an id column in this table. Sad.
public function update(Entity $entity): ContextNavigation {
if (!$entity instanceof ContextNavigation) {
throw new \LogicException('Can only update context navigation entities');
}

// if entity wasn't changed it makes no sense to run a db query
$properties = $entity->getUpdatedFields();
if (\count($properties) === 0) {
return $entity;
}

$qb = $this->db->getQueryBuilder();
$qb->update($this->tableName);

// build the fields
foreach ($properties as $property => $updated) {
$column = $entity->propertyToColumn($property);
$getter = 'get' . ucfirst($property);
$value = $entity->$getter();

$type = $this->getParameterTypeForProperty($entity, $property);
$qb->set($column, $qb->createNamedParameter($value, $type));
}

$qb->where($qb->expr()->eq('share_id', $qb->createNamedParameter($entity->getShareId(), IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($entity->getUserId(), IQueryBuilder::PARAM_STR)));

$qb->executeStatement();

return $entity;
}
}
36 changes: 4 additions & 32 deletions lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
class BeforeTemplateRenderedListener implements IEventListener {
public function __construct(
protected INavigationManager $navigationManager,
protected IURLGenerator $urlGenerator,
protected IUserSession $userSession,
protected ContextService $contextService,
protected IURLGenerator $urlGenerator,
protected IUserSession $userSession,
protected ContextService $contextService,
) {
}

Expand All @@ -41,36 +41,8 @@ public function handle(Event $event): void {
return;
}

// temporarily show all
//$contexts = $this->contextService->findForNavigation($user->getUID());
$contexts = $this->contextService->findAll($user->getUID());
$contexts = $this->contextService->findForNavigation($user->getUID());
foreach ($contexts as $context) {
/* temporarily, show all
if ($context->getOwnerType() === Application::OWNER_TYPE_USER
&& $context->getOwnerId() === $user->getUID()) {


// filter out entries for owners unless it is set to be visible
$skipEntry = true;
foreach ($context->getSharing() as $shareInfo) {
// TODO: integrate into DB query in Mapper

if (isset($shareInfo['display_mode']) && $shareInfo['display_mode'] === Application::NAV_ENTRY_MODE_ALL) {
// a custom override makes it visible
$skipEntry = false;
break;
} elseif (!isset($shareInfo['display_mode']) && $shareInfo['display_mode_default'] === Application::NAV_ENTRY_MODE_ALL) {
// no custom override, and visible also for owner by default
$skipEntry = false;
break;
}
}
if ($skipEntry) {
continue;
}
}
*/

$this->navigationManager->add(function () use ($context) {
$iconRelPath = 'material/' . $context->getIcon() . '.svg';
if (file_exists(__DIR__ . '/../../img/' . $iconRelPath)) {
Expand Down
5 changes: 3 additions & 2 deletions lib/Service/ShareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,13 @@ public function updateDisplayMode(int $shareId, int $displayMode, string $userId
}
} else {
// setting user display mode override only requires access
if (!$this->permissionsService->canAccessContextById($item->getId())) {
// this does not seem to work
if (!$this->permissionsService->canAccessContextById($item->getNodeId(), $userId)) {
throw new PermissionError(sprintf('PermissionError: can not update share with id %d', $shareId));
}
}

return $this->contextNavigationMapper->setDisplayModeByShareId($shareId, $displayMode, '');
return $this->contextNavigationMapper->setDisplayModeByShareId($shareId, $displayMode, $userId);
} catch (DoesNotExistException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
Expand Down
38 changes: 36 additions & 2 deletions src/modules/modals/CreateContext.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
</div>
<NcContextResource :resources.sync="resources" :receivers.sync="receivers" />
</div>
<div class="row space-T">
<NcActionCheckbox :checked="showInNavigationDefault" @change="updateDisplayMode">
Show in app list
</NcActionCheckbox>
<p class="nav-display-subtext">
This can be overridden by a per-account preference
</p>
</div>
<div class="row space-R row space-T">
<div class="fix-col-4 end">
<NcButton type="primary" :aria-label="t('tables', 'Create application')" data-cy="createContextSubmitBtn" @click="submit">
Expand All @@ -54,13 +62,15 @@
</template>

<script>
import { NcDialog, NcButton, NcIconSvgWrapper } from '@nextcloud/vue'
import { NcDialog, NcButton, NcIconSvgWrapper, NcActionCheckbox } from '@nextcloud/vue'
import { showError } from '@nextcloud/dialogs'
import '@nextcloud/dialogs/style.css'
import NcContextResource from '../../shared/components/ncContextResource/NcContextResource.vue'
import NcIconPicker from '../../shared/components/ncIconPicker/NcIconPicker.vue'
import svgHelper from '../../shared/components/ncIconPicker/mixins/svgHelper.js'
import permissionBitmask from '../../shared/components/ncContextResource/mixins/permissionBitmask.js'
import { getCurrentUser } from '@nextcloud/auth'
import { NAV_ENTRY_MODE } from '../../shared/constants.js'

export default {
name: 'CreateContext',
Expand All @@ -70,6 +80,7 @@ export default {
NcButton,
NcIconSvgWrapper,
NcContextResource,
NcActionCheckbox,
},
mixins: [svgHelper, permissionBitmask],
props: {
Expand All @@ -90,6 +101,7 @@ export default {
description: '',
resources: [],
receivers: [],
showInNavigationDefault: false,
}
},
watch: {
Expand Down Expand Up @@ -150,18 +162,32 @@ export default {
description: this.description,
nodes: dataResources,
}
const res = await this.$store.dispatch('insertNewContext', { data, previousReceivers: [], receivers: this.receivers })
// adding share to oneself to have navigation display control
this.receivers.push(
{
id: getCurrentUser().uid,
displayName: getCurrentUser().uid,
icon: 'icon-user',
isUser: true,
key: 'user-' + getCurrentUser().uid,
})
const displayMode = this.showInNavigation ? 'NAV_ENTRY_MODE_ALL' : 'NAV_ENTRY_MODE_HIDDEN'
const res = await this.$store.dispatch('insertNewContext', { data, previousReceivers: [], receivers: this.receivers, displayMode: NAV_ENTRY_MODE[displayMode] })
if (res) {
return res.id
} else {
showError(t('tables', 'Could not create new application'))
}
},
updateDisplayMode() {
this.showInNavigation = !this.showInNavigation
},
reset() {
this.title = ''
this.errorTitle = false
this.setIcon(this.randomIcon())
this.customTitleChosen = false
this.showInNavigationDefault = false
},
},
}
Expand All @@ -175,5 +201,13 @@ export default {
display: inline-flex;
align-items: center;
}

.nav-display-subtext {
color: var(--color-text-maxcontrast)
}

li {
list-style: none;
}
}
</style>
49 changes: 44 additions & 5 deletions src/modules/modals/EditContext.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@
</div>
<NcContextResource :resources.sync="resources" :receivers.sync="receivers" />
</div>

<div class="row space-T">
<NcActionCheckbox :checked="showInNavigationDefault" @change="updateDisplayMode">
Show in app list
</NcActionCheckbox>
<p class="nav-display-subtext">
This can be overridden by a per-account preference
</p>
</div>
<div class="row space-T">
<div class="fix-col-4 space-T justify-between">
<NcButton v-if="!prepareDeleteContext" type="error" @click="prepareDeleteContext = true">
Expand All @@ -62,14 +69,14 @@
</template>

<script>
import { NcDialog, NcButton, NcIconSvgWrapper } from '@nextcloud/vue'
import { NcDialog, NcButton, NcIconSvgWrapper, NcActionCheckbox } from '@nextcloud/vue'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { getCurrentUser } from '@nextcloud/auth'
import '@nextcloud/dialogs/style.css'
import { mapGetters, mapState } from 'vuex'
import NcContextResource from '../../shared/components/ncContextResource/NcContextResource.vue'
import NcIconPicker from '../../shared/components/ncIconPicker/NcIconPicker.vue'
import { NODE_TYPE_TABLE, NODE_TYPE_VIEW, PERMISSION_READ, PERMISSION_CREATE, PERMISSION_UPDATE, PERMISSION_DELETE } from '../../shared/constants.js'
import { NODE_TYPE_TABLE, NODE_TYPE_VIEW, PERMISSION_READ, PERMISSION_CREATE, PERMISSION_UPDATE, PERMISSION_DELETE, NAV_ENTRY_MODE } from '../../shared/constants.js'
import svgHelper from '../../shared/components/ncIconPicker/mixins/svgHelper.js'
import permissionBitmask from '../../shared/components/ncContextResource/mixins/permissionBitmask.js'
import { emit } from '@nextcloud/event-bus'
Expand All @@ -83,6 +90,7 @@ export default {
NcIconPicker,
NcIconSvgWrapper,
NcContextResource,
NcActionCheckbox,
},
mixins: [svgHelper, permissionBitmask, permissionsMixin],
props: {
Expand Down Expand Up @@ -111,6 +119,7 @@ export default {
PERMISSION_UPDATE,
PERMISSION_DELETE,
prepareDeleteContext: false,
showInNavigationDefault: false,
}
},
computed: {
Expand All @@ -135,6 +144,7 @@ export default {
this.description = context.description
this.resources = context ? this.getContextResources(context) : []
this.receivers = context ? this.getContextReceivers(context) : []
this.showInNavigationDefault = this.getNavDisplay(context)
}
},
},
Expand Down Expand Up @@ -166,8 +176,17 @@ export default {
nodes: dataResources,
}
const context = this.getContext(this.contextId)

const res = await this.$store.dispatch('updateContext', { id: this.contextId, data, previousReceivers: Object.values(context.sharing), receivers: this.receivers })
// adding share to oneself to have navigation display control
this.receivers.push(
{
id: getCurrentUser().uid,
displayName: getCurrentUser().uid,
icon: 'icon-user',
isUser: true,
key: 'user-' + getCurrentUser().uid,
})
const displayMode = this.showInNavigation ? 'NAV_ENTRY_MODE_ALL' : 'NAV_ENTRY_MODE_HIDDEN'
const res = await this.$store.dispatch('updateContext', { id: this.contextId, data, previousReceivers: Object.values(context.sharing), receivers: this.receivers, displayMode: NAV_ENTRY_MODE[displayMode] })
if (res) {
showSuccess(t('tables', 'Updated application "{contextTitle}".', { contextTitle: this.title }))
this.actionCancel()
Expand All @@ -183,6 +202,15 @@ export default {
this.resources = context ? this.getContextResources(context) : []
this.receivers = context ? this.getContextReceivers(context) : []
this.prepareDeleteContext = false
this.showInNavigationDefault = this.getNavDisplay(context)
},
getNavDisplay(context) {
const shares = Object.keys(context.sharing || {})
if (shares.length) {
const displayMode = context.sharing[shares[0]].display_mode_default
return displayMode !== 0
}
return false
},
getContextReceivers(context) {
let sharing = Object.values(context.sharing)
Expand Down Expand Up @@ -240,6 +268,9 @@ export default {
}

},
updateDisplayMode() {
this.showInNavigation = !this.showInNavigation
},
actionTransfer() {
emit('tables:context:edit', null)
emit('tables:context:transfer', this.localContext)
Expand All @@ -260,4 +291,12 @@ export default {
padding-inline: 0 !important;
max-width: 100%;
}

.nav-display-subtext {
color: var(--color-text-maxcontrast)
}

li {
list-style: none;
}
</style>
Loading
Loading