diff --git a/resources/Hbs/Partials/form.hbs b/resources/Hbs/Partials/form.hbs index 6794712..9a5a872 100644 --- a/resources/Hbs/Partials/form.hbs +++ b/resources/Hbs/Partials/form.hbs @@ -41,7 +41,7 @@ {{!-- data-type="color" --}} {{#if item.placeholder}}placeholder="{{item.placeholder}}"{{else}}placeholder=" "{{/if}} {{#if item.readonly}}readonly=""{{/if}} - {{#if item.default includeZero=false}}value="{{item.default}}" default="{{item.default}}"{{/if}} + {{#if item.default includeZero=false}}value="{{#is item.default "randomHex()"}}{{colorHexRandom}}{{else}}{{item.default}}{{/is}}" default="{{item.default}}"{{/if}} {{#if item.select.[0].value includeZero=false}}min="{{item.select.[0].value}}"{{/if}} {{#if item.select.[1].value includeZero=false}}max="{{item.select.[1].value}}"{{/if}} {{#if item.required}}required=""{{/if}} @@ -64,14 +64,15 @@ autocomplete="off" {{#if item.placeholder}}placeholder="{{item.placeholder}}"{{/if}} {{#if item.disabled}}disabled=""{{else}}{{#if ../form.onready}}disabled="loading"{{/if}}{{/if}} - {{#if item._style.select.tag}}/>{{else}}>{{#if item.select}} + {{#if item.select.url}}data-select-remote="{{JSONstringify item.select}}"{{/if}} + {{#if item._style.select.tag}}/>{{else}}>{{#if item.select}}{{#if item.select.url}}{{else}} {{#each item.select as |option|}}{{/each}} - {{/if}}{{/if}} + {{/if}}{{/if}}{{/if}} {{!-- Password Input --}} {{else}}{{#is item.type "password"}} diff --git a/src/Driver/Model/Mariadb.php b/src/Driver/Model/Mariadb.php index 45f2abb..6a2936e 100644 --- a/src/Driver/Model/Mariadb.php +++ b/src/Driver/Model/Mariadb.php @@ -64,6 +64,9 @@ class Mariadb implements CrazyDriverModel { /** @var array|null $field to retrieve */ private $_fields = null; + /** @var bool $_delete */ + private $_delete = false; + /** @var null|array conditions */ private array|null $conditions = null; @@ -317,6 +320,9 @@ public function ingestData(array $data, ?array $options = null):self { */ public function pushToTrash(?array $options = null):self { + # Swith delete + $this->_delete = true; + # Return self return $this; @@ -347,7 +353,14 @@ public function run():array { ]); }else - # Insert to mongo Check schema + # Delete item by id + if($this->_delete && $this->id !== null){ + + # Insert + $result[] = $this->mariadb->deleteOneToCollection($this->arguments["table"], $this->id); + + }else + # Insert to maria db Check schema if($this->schema !== null && $this->id !== null){ # Check collection @@ -694,7 +707,7 @@ private function _ingestFields(?array $options = null):void { "table" => "", "schema" => [], "database" => [], - "pageStateProcess" => false, + "pageStateProcess" => false, ]; } \ No newline at end of file diff --git a/src/Front/Library/Crazypartial.ts b/src/Front/Library/Crazypartial.ts index 2177068..20d8d72 100644 --- a/src/Front/Library/Crazypartial.ts +++ b/src/Front/Library/Crazypartial.ts @@ -30,13 +30,16 @@ export default abstract class Crazypartial { ****************************************************** */ + /** + * Input + */ public input:RegisterPartialScanned; /** * @param html:string * Duplicate of the class name because build change name of class */ - public static readonly html:string|null|CallableFunction = null; + public html:string|null|CallableFunction = null; /** * Constructor @@ -48,12 +51,61 @@ export default abstract class Crazypartial { // Set input this.input = input; + // Check id and target + if(typeof this.input.id === "number" && this.input.target instanceof HTMLElement) + + // Set id on target + this.input.target.dataset.partialId = this.input.id.toString(); + + } + + /** Protected methods + ****************************************************** + */ + + /** + * Reload + * + * @param state:Object|null + */ + public reload = (state:Object|null = null) => { + + // Check target + if(this.input.target instanceof HTMLElement && this.html !== null){ + + // Set data + var htmlString = ""; + + // Check html + if(typeof state === "object" && typeof this.html === "function") + + // Get string + htmlString = this.html(typeof state === "object" ? state : {}); + + // Get content dom + var contentDom = document.createRange().createContextualFragment(htmlString); + + // Reload partial + this.input.target.replaceWith(contentDom); + + } + + // Execute on ready + this.onReady(); + } /** Public methods ****************************************************** */ + /** + * On Ready + */ + public onReady = () => { + + } + /** * Enable */ diff --git a/src/Front/Library/Utility/Form.ts b/src/Front/Library/Utility/Form.ts index 121bc0f..b595f65 100644 --- a/src/Front/Library/Utility/Form.ts +++ b/src/Front/Library/Utility/Form.ts @@ -174,9 +174,8 @@ export default class Form { let currentName:string; let currentType:string; - // Get all select and input on form el - let items = this._formEl.querySelectorAll("select, input"); + let items = this._formEl.querySelectorAll("select[name], input[name]"); // Check items if(items.length) @@ -193,6 +192,14 @@ export default class Form { // Get type currentType = items[i]["type"] ?? ""; + // Check if data type + // @ts-ignore + if("type" in items[i].dataset && items[i].dataset.type) + + // Override type + // @ts-ignore + currentType = items[i].dataset.type; + // Check if in values if(Object.keys(values).includes(currentName)){ @@ -458,9 +465,6 @@ export default class Form { // Get formdata let formData:FormData = this.getFormData(target); - console.log("--dev--"); - console.log(formData); - // Lock form this.lock(); @@ -1452,6 +1456,75 @@ export default class Form { } + /** + * Set Select + * + * Set select in item + * + * @param itemEl:HTMLElement + * @param value:string + * @return void + */ + private selectSet = (itemEl:HTMLElement, value:string, valuesID:string|Object|null):void => { + + console.log("toto"); + + // Check itemEl + if(["INPUT", "SELECT"].includes(itemEl.tagName)){ + + // Check if tomselect in item + if("tomselect" in itemEl && itemEl.tomselect instanceof TomSelect){ + + // Set value + itemEl.tomselect.setValue(value); + + // Set id + this._setID(valuesID, itemEl); + + }else{ + + // Set value + itemEl.setAttribute("value", value); + + // Dispatch event change + itemEl.dispatchEvent(new Event("change")); + + // Set id + this._setID(valuesID, itemEl); + + } + + } + + } + + /** + * Set Number + * + * Set number in item + * + * @param itemEl:HTMLElement + * @param value:string + * @return void + */ + private numberSet = (itemEl:HTMLElement, value:string, valuesID:string|Object|null):void => { + + // Check itemEl + if(["INPUT", "SELECT"].includes(itemEl.tagName)){ + + // Set value + itemEl.setAttribute("value", value); + + // Dispatch event change + itemEl.dispatchEvent(new Event("change")); + + // Set id + this._setID(valuesID, itemEl); + + } + + } + /** Private methods | Set value | Set Custom Data ****************************************************** */ @@ -1871,17 +1944,18 @@ export default class Form { var currentValue = inputEl.getAttribute("value"); // Check current value - if(currentValue) + if(currentValue && currentValue != "randomHex()"){ // Set default on option options.default = currentValue; - else + }else // Check if input has default if(inputEl.hasAttribute("default")){ // Get default var currentDefault = inputEl.getAttribute("default"); + // Check current value if(currentDefault === "randomHex()"){ diff --git a/src/Library/Database/Driver/Mariadb.php b/src/Library/Database/Driver/Mariadb.php index 75fc9ce..72223de 100644 --- a/src/Library/Database/Driver/Mariadb.php +++ b/src/Library/Database/Driver/Mariadb.php @@ -868,18 +868,18 @@ public function deleteOneToCollection(string $table, int $id, string $database = # Set result $result = null; - # Check input - if(!$table || empty($value) || !$database) - - # Return result - return $result; - # Check database if(!$database) # Get main database $database = $this->_getDefaultDatabase(); + # Check input + if(!$table || !$database) + + # Return result + return $result; + # Use database $this->client->exec("USE " . $database); diff --git a/src/Library/Form/Process.php b/src/Library/Form/Process.php index 1b494e5..07bc627 100644 --- a/src/Library/Form/Process.php +++ b/src/Library/Form/Process.php @@ -49,6 +49,9 @@ class Process { private array $dispatch = [ "INT" => [ ], + "SELECT" => [ + "integer", + ], "VARCHAR" => [ "trim", "clean", @@ -134,11 +137,25 @@ public function __construct(array $formResult = []){ private function _actionInt(array &$input = []):void { # Check value is same type - if(!is_int($input['value']) && !ctype_digit($input['value'])) + if(!is_int($input['value']) && !ctype_digit($input['value'])){ + + # Check requierd + if( + ( + isset($input['required']) && + $input['required'] == false + ) || + !isset($input['required']) + ) + + # Set value to null + $input['value'] = null; # Stop function return; + } + # Parse the value $input['value'] = intval($input['value']); @@ -814,6 +831,19 @@ public static function strtolower(string $input):string { } + /** + * Integer + * + * @param string + * @return int + */ + public static function integer(string $input):int { + + # Return result + return intval($input); + + } + /** * Alphanumeric * diff --git a/src/Library/Form/Validate.php b/src/Library/Form/Validate.php index 1491d1c..1e816f8 100644 --- a/src/Library/Form/Validate.php +++ b/src/Library/Form/Validate.php @@ -146,7 +146,21 @@ public function __construct(array $formResult = []){ private function _actionInt(array &$input = []):void { # Check value is same type - if(!is_int($input['value']) && !ctype_digit($input['value'])) + if( + ( + $input['value'] !== null && !is_int($input['value']) && !ctype_digit($input['value']) + ) && ( + ( + isset($input['required']) && + $input['required'] == false && + $input['value'] !== null + ) || + ( + !isset($input['required']) && + $input['value'] !== null + ) + ) + ){ # New Exception throw new CrazyException( @@ -157,6 +171,8 @@ private function _actionInt(array &$input = []):void { ] ); + } + } /**