mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-16 05:14:35 +02:00
Use a stimulus controller to implement collectionType for specifications/parameters.
This commit is contained in:
parent
f7ce94c168
commit
537b7fad7f
6 changed files with 153 additions and 91 deletions
62
assets/controllers/elements/collection_type_controller.js
Normal file
62
assets/controllers/elements/collection_type_controller.js
Normal file
|
@ -0,0 +1,62 @@
|
|||
import {Controller} from "@hotwired/stimulus";
|
||||
|
||||
import * as bootbox from "bootbox";
|
||||
import "../../css/bootbox_extensions.css";
|
||||
|
||||
export default class extends Controller {
|
||||
static values = {
|
||||
deleteMessage: String,
|
||||
prototype: String,
|
||||
}
|
||||
|
||||
static targets = ["target"];
|
||||
|
||||
/**
|
||||
* Decodes escaped HTML entities
|
||||
* @param {string} input
|
||||
* @returns {string}
|
||||
*/
|
||||
htmlDecode(input) {
|
||||
const doc = new DOMParser().parseFromString(input, "text/html");
|
||||
return doc.documentElement.textContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a unique ID to be used for the new element
|
||||
* @returns {string}
|
||||
*/
|
||||
generateUID() {
|
||||
const long = (performance.now().toString(36)+Math.random().toString(36)).replace(/\./g,"");
|
||||
return long.slice(0, 6); // 6 characters is enough for our unique IDs here
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new entry in the target using the given prototype value
|
||||
*/
|
||||
createElement(event) {
|
||||
const targetTable = this.targetTarget;
|
||||
const prototype = this.prototypeValue
|
||||
|
||||
//Apply the index to prototype to create our element to insert
|
||||
const newElementStr = this.htmlDecode(prototype.replace(/__name__/g, this.generateUID()));
|
||||
|
||||
|
||||
//Insert new html after the last child element
|
||||
//If the table has a tbody, insert it there
|
||||
if(targetTable.tBodies[0]) {
|
||||
targetTable.tBodies[0].insertAdjacentHTML('beforeend', newElementStr);
|
||||
} else { //Otherwise just insert it
|
||||
targetTable.insertAdjacentHTML('beforeend', newElementStr);
|
||||
}
|
||||
}
|
||||
|
||||
deleteElement(event) {
|
||||
bootbox.confirm(this.deleteMessageValue, (result) => {
|
||||
if(result) {
|
||||
const target = event.target;
|
||||
//Remove the row element from the table
|
||||
target.closest("tr").remove();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue