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

[0.11] Extend entity contextmenu with options to add above/below #534

Draft
wants to merge 2 commits into
base: 0.11-feat-realtime
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## 0.11 -
### Added
- _Add Entity Above_ and _Add Entity Below_ to the Contextmenu in the entity tree

## 0.10.1
### Fixed
- Frontend errors due to wrong dayjs import
Expand Down
47 changes: 30 additions & 17 deletions app/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ class Entity extends Model implements Searchable {
];

const rules = [
'name' => 'required|string',
'entity_type_id' => 'required|integer|exists:entity_types,id',
'root_entity_id' => 'integer|exists:entities,id',
'name' => 'required|string',
'entity_type_id' => 'required|integer|exists:entity_types,id',
'root_entity_id' => 'integer|exists:entities,id',
'rank' => 'integer|min:1',
];

const patchRules = [
Expand Down Expand Up @@ -84,8 +85,8 @@ public function getAllMetadata() {
'editors' => $this->editors,
'metadata' => $this->metadata,
];
}
}

public static function getSearchCols(): array {
return array_keys(self::searchCols);
}
Expand Down Expand Up @@ -120,7 +121,7 @@ public static function getFromPath($path, $delimiter = "\\\\"): ?int {
}
}

public static function create($fields, $entityTypeId, $user, $rootEntityId = null) {
public static function create($fields, $entityTypeId, $user, $rootEntityId = null, $rank = null) {
$isChild = isset($rootEntityId);
if($isChild) {
$parentCtid = self::find($rootEntityId)->entity_type_id;
Expand All @@ -144,14 +145,27 @@ public static function create($fields, $entityTypeId, $user, $rootEntityId = nul
}

$entity = new self();
$rank;
if($isChild) {
$rank = self::where('root_entity_id', $rootEntityId)->max('rank') + 1;
if(!isset($rank)) {
$rank = self::where('root_entity_id', $rootEntityId)->max('rank') + 1;
} else {
$nextSiblings = self::where('root_entity_id', $rootEntityId)->where('rank', '>=', $rank)->get();
}
$entity->root_entity_id = $rootEntityId;
} else {
$rank = self::whereNull('root_entity_id')->max('rank') + 1;
if(!isset($rank)) {
$rank = self::whereNull('root_entity_id')->max('rank') + 1;
} else {
$nextSiblings = self::whereNull('root_entity_id')->where('rank', '>=', $rank)->get();
}
}
$entity->rank = $rank;
if(isset($nextSiblings) && count($nextSiblings) > 0) {
foreach($nextSiblings as $sibling) {
$sibling->rank = $sibling->rank + 1;
$sibling->saveQuietly();
}
}

foreach($fields as $key => $value) {
$entity->{$key} = $value;
Expand Down Expand Up @@ -180,35 +194,34 @@ public static function getEntitiesByParent($id = null) {
}
return $entities->orderBy('rank')->get();
}

private function moveOrFail(int | null $parentId) {

if(isset($parentId)) {
if($parentId == $this->id) {
throw new \Exception('Cannot move entity to itself.');
}

$parentEntity = Entity::findOrFail($parentId);
$parentEntityType = $parentEntity->entity_type;

if(!$parentEntityType->sub_entity_types->contains($this->entity_type_id)) {
throw new \Exception('This type is not an allowed sub-type.');
}

$this->root_entity_id = $parentId;
$query = self::where('root_entity_id', $parentId);
} else {
if(!$this->entity_type->is_root) {
throw new \Exception('This type is not an allowed root-type.');
}

$this->root_entity_id = null;
$query = self::whereNull('root_entity_id');
}
return $query;
}

public static function patchRanks($rank, $id, $parent, $user) {
public static function patchRanks($rank, $id, $parent, $user) {
$entity = Entity::find($id);
$oldRank = $entity->rank;
$entity->rank = $rank;
Expand All @@ -227,7 +240,7 @@ public static function patchRanks($rank, $id, $parent, $user) {
$oc->rank--;
$oc->saveQuietly();
}

try{
$query = $entity->moveOrFail($parent);
}catch(\Exception $e) {
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Controllers/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,9 @@ public function addEntity(Request $request) {
$fields = $request->only(array_keys(Entity::rules));
$etid = $request->get('entity_type_id');
$reid = $request->get('root_entity_id');
$rank = $request->get('rank');

$res = Entity::create($fields, $etid, $user, $reid);
$res = Entity::create($fields, $etid, $user, $reid, $rank);

if($res['type'] === 'entity') {
return response()->json($res['entity'], 201);
Expand Down Expand Up @@ -493,7 +494,6 @@ public function importData(Request $request) {
$entityTypeId = trim($data['entity_type_id']);
$attributesMapping = array_map(fn ($col) => trim($col), $data['attributes']);


$headerRow = null;
$hasParent = false;
$attributeIdToColumnIdxMapping = [];
Expand All @@ -506,7 +506,6 @@ public function importData(Request $request) {
$parentIdx = null;
$nameIdx = null;


// Getting headers
if(($row = fgetcsv($handle, 0, $metadata['delimiter'])) !== false) {
$row = sp_trim_array($row);
Expand Down Expand Up @@ -656,7 +655,6 @@ public function importData(Request $request) {
}

function createImportedEntity($entityName, ?string $rootEntityPath, $entityTypeId, $user) {

$rootEntityId = null;
if(isset($rootEntityPath)) {
try {
Expand Down
3 changes: 3 additions & 0 deletions resources/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ export async function addEntity(entity) {
if(!!entity.parent_id) {
data.root_entity_id = entity.parent_id;
}
if(!!entity.rank) {
data.rank = entity.rank;
}
return $httpQueue.add(
() => http.post(`/entity`, data).then(response => response.data)
);
Expand Down
4 changes: 4 additions & 0 deletions resources/js/bootstrap/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ import {
faTimes,
faTrash,
faTrashRestore,
faTurnDown,
faTurnUp,
faUnderline,
faUndo,
faUndoAlt,
Expand Down Expand Up @@ -430,6 +432,8 @@ library.add(
faTimes,
faTrash,
faTrashRestore,
faTurnDown,
faTurnUp,
faUnderline,
faUndo,
faUndoAlt,
Expand Down
20 changes: 18 additions & 2 deletions resources/js/bootstrap/stores/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,15 @@ export const useEntityStore = defineStore('entity', {
this.tree.splice(idx, 1, node);
}
} else {
this.tree.push(node);
if(this.tree.length == 0 || node.rank > this.tree.at(-1).rank) {
this.tree.push(node);
} else {
const idx = this.tree.findIndex(c => c.rank == node.rank);
this.tree.splice(idx, 0, node);
for(let i=idx+1; i<this.tree.length; i++) {
this.tree[i].rank++;
}
}
}
} else {
const doCount = !node?.already_existing;
Expand All @@ -250,7 +258,15 @@ export const useEntityStore = defineStore('entity', {
parent.children.splice(idx, 1, node);
}
} else {
parent.children.push(node);
if(node.rank > parent.children.at(-1).rank) {
parent.children.push(node);
} else {
const idx = parent.children.findIndex(c => c.rank == node.rank);
parent.children.splice(idx, 0, node);
for(let i=idx+1; i<parent.children.length; i++) {
parent.children[i].rank++;
}
}
}
}
if(doCount) {
Expand Down
38 changes: 36 additions & 2 deletions resources/js/components/tree/TreeMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@
</span>
</a>
</li>
<li>
<a
class="dropdown-item"
href="#"
@click.stop.prevent="addEntity('above')"
@dblclick.stop.prevent=""
>
<i class="fas fa-fw fa-turn-up text-success" />
<span class="ms-2">
{{ t('main.entity.tree.contextmenu.add_above') }}
</span>
</a>
</li>
<li>
<a
class="dropdown-item"
href="#"
@click.stop.prevent="addEntity('below')"
@dblclick.stop.prevent=""
>
<i class="fas fa-fw fa-turn-down text-success" />
<span class="ms-2">
{{ t('main.entity.tree.contextmenu.add_below') }}
</span>
</a>
</li>
<li>
<a
class="dropdown-item"
Expand Down Expand Up @@ -109,8 +135,16 @@
context.emit('close');
});

const addEntity = _ => {
showAddEntity(props.data);
const addEntity = where => {
if(where == 'above') {
const parent = entityStore.getEntity(props.data.root_entity_id);
showAddEntity(parent, null, props.data.rank);
} else if(where == 'below') {
const parent = entityStore.getEntity(props.data.root_entity_id);
showAddEntity(parent, null, props.data.rank + 1);
} else {
showAddEntity(props.data);
}
};
const duplicateEntity = _ => {
duplicateEntityApi(props.data).then(data => {
Expand Down
5 changes: 4 additions & 1 deletion resources/js/helpers/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ export function showLiteratureInfo(id, options) {
modal.open();
}

export function showAddEntity(parent = null, onAdded) {
export function showAddEntity(parent = null, onAdded, rank = -1) {
const uid = `AddEntity-${getTs()}`;
const modal = useModal({
component: AddEntity,
Expand All @@ -545,6 +545,9 @@ export function showAddEntity(parent = null, onAdded) {
parent_id: entity.parent_id,
name: entity.name,
};
if(rank != -1) {
entityData.rank = rank;
}

useEntityStore().create(entityData).then(node => {
if(!!onAdded) {
Expand Down
2 changes: 2 additions & 0 deletions resources/js/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@
"add": "Neue Top-Level Entität hinzufügen",
"contextmenu": {
"add": "Unterentität hinzufügen",
"add_above": "Entität darüber hinzufügen",
"add_below": "Entität darunter hinzufügen",
"duplicate": "Duplikat erzeugen",
"move": "Im Baum verschieben",
"delete": "Entität löschen"
Expand Down
2 changes: 2 additions & 0 deletions resources/js/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@
"add": "Add new Top-Level-Entity",
"contextmenu": {
"add": "Add Sub-Entity",
"add_above": "Add Entity Above",
"add_below": "Add Entity Below",
"duplicate": "Create Duplicate",
"move": "Move in Tree",
"delete": "Delete Entity"
Expand Down
Loading