Skip to content

Commit

Permalink
Update form
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Nov 25, 2024
1 parent 2d89336 commit ef8f922
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 10 deletions.
28 changes: 28 additions & 0 deletions resources/Js/Handlebars/colorHexRandom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Handlebars Array Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @copyright 2022-2024 Kévin Zarshenas
*/



/**
* Color Hex Random
*
* Return random color hex format
*
* @param option.
*/
module.exports = (value, options) => {

// Generate a random integer between 0 and 16777215 (0xFFFFFF)
const randomColor = Math.floor(Math.random() * 16777216);

// Convert to hexadecimal and pad with leading zeros if necessary
return `#${randomColor.toString(16).padStart(6, '0')}`;

}
32 changes: 32 additions & 0 deletions resources/Js/Handlebars/roundDecimal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Handlebars Array Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @copyright 2022-2024 Kévin Zarshenas
*/

/**
* Length
*
* Returns the length of the given string or array.
*
* @param any value
* @param Object options
*
* @return number
*/
module.exports = (value, options) => {

// Check if array or string
if(typeof value === 'number')

// Return lenght
return parseFloat(value.toFixed(1));

// Return 0
return value;

}
8 changes: 8 additions & 0 deletions resources/Scss/style/dimensions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@

}

// Fit content to the window view size
.screen-fit-y {

// Dimension
align-items: stretch;

}

// Fit content at least to the window view size
.screen-fit-min {

Expand Down
9 changes: 7 additions & 2 deletions src/Driver/Model/Mariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use CrazyPHP\Library\File\Config as FileConfig;
use CrazyPHP\Interface\CrazyDriverModel;
use CrazyPHP\Exception\CrazyException;
use CrazyPHP\Library\Time\DateTime;
use CrazyPHP\Library\Model\Schema;
use CrazyPHP\Library\Array\Arrays;
use CrazyPHP\Library\Time\DateTime;

/* use PhpMyAdmin\SqlParser\Statements\SelectStatement;
use PhpMyAdmin\SqlParser\Parser; */
Expand Down Expand Up @@ -325,7 +325,7 @@ public function run():array {
if($this->id !== null){

# Set result
$result[] = $this->mariadb->find($this->arguments["table"], "", [
$result = $this->mariadb->find($this->arguments["table"], "", [
"filters" => [
"id" => $this->id
]
Expand Down Expand Up @@ -399,6 +399,11 @@ public function run():array {

}

}else{

# Set result
$result = $this->mariadb->find($this->arguments["table"]);

}

# Return result
Expand Down
156 changes: 151 additions & 5 deletions src/Front/Library/Utility/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,40 @@ export default class Form {
})
;

}else
// Check if is
if(entityAttr && onreadyAttr && /^id\/[a-zA-Z0-9]+$/.test(onreadyAttr)){

// Prepare request
let request = new Crazyrequest(
`/api/v2/${entityAttr}/${onreadyAttr.replace("id/", "")}`,
{
method: "get",
cache: false,
responseType: "json",
from: "internal"
}
);

// Fetch request
request.fetch()
.then(value => {

console.log("dev");
console.log(value);

// Unlock
this.unlock();

// Check result
if(value.results.length)

// Set values
this.setValue(value.results[0], value.results[0]._id);

})
;

// If not action, unlock form
}else

Expand Down Expand Up @@ -1149,6 +1183,35 @@ export default class Form {

}

/**
* Retrieve Color
*
* @param itemEl:HTMLElement
* @return null|Array<any>
*/
private colorRetrieve = (itemEl:HTMLElement):null|Array<any> => {

// Set result
let result:null|Array<any> = null;

// Check value
if("value" in itemEl && "name" in itemEl){

let key:string = itemEl.name as string;

// Set result
let value:string = itemEl.value as string;

// Push in result
result = [key, value];

}

// Return result
return result;

}

/**
* Password Text
*
Expand Down Expand Up @@ -1603,16 +1666,21 @@ export default class Form {
*/
private _initColorInput = (inputEl:HTMLSelectElement|HTMLInputElement):void => {

// Check maska
console.log("color init");
console.log(inputEl);

// Check pickr
if(inputEl instanceof HTMLInputElement && "colorPicker" in inputEl.dataset && inputEl.dataset.colorPicker == "pickr"){

// Create divEl
let divEl = document.createElement("div");

// Append el after el
inputEl.after(divEl);

// Hide input
inputEl.classList.add("hide");

// Prepare options
let options:Partial<Pickr.Options> = {
el: inputEl,
el: divEl,
theme: "colorTheme" in inputEl.dataset && ['classic', 'monolith', 'nano'].includes(inputEl.dataset.colorTheme as string)
? inputEl.dataset.colorTheme as Pickr.Theme
: 'classic'
Expand Down Expand Up @@ -1656,9 +1724,87 @@ export default class Form {
}
};

// Check if input has default
if(inputEl.hasAttribute("value")){

// Get default
var currentValue = inputEl.getAttribute("value");

// Check current value
if(currentValue)

// Set default on option
options.default = currentValue;

else
// Check if input has default
if(inputEl.hasAttribute("default")){

// Get default
var currentDefault = inputEl.getAttribute("default");

// Check current value
if(currentDefault === "randomHex()"){

// Generate a random integer between 0 and 16777215 (0xFFFFFF)
const randomColor = Math.floor(Math.random() * 16777216);

// Convert to hexadecimal and pad with leading zeros if necessary
options.default = `#${randomColor.toString(16).padStart(6, '0')}`;

}else
// Check currentValue
if(currentDefault)

// Set default on option
options.default = currentDefault;


}



}

// Simple example, see optional options for more configuration.
const pickr = Pickr.create(options as Pickr.Options);

// Add event on save
pickr.on("save", (color, instance) => {

// Get color
let hexa:string = color.toHEXA();

// Set value on inputEl
inputEl.value = hexa

// Close instance
instance.hide();

});

// Add event on input
inputEl.addEventListener(
"change",
(e:Event) => {

// Get targelt
let el = e.currentTarget;

// Check el
if(el instanceof HTMLInputElement){

// Get value
let value = el.value;

// Set value in pickr
pickr.setColor(value);

}

}
)

}

}
Expand Down
4 changes: 2 additions & 2 deletions src/Library/Database/Driver/Mariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ public function deleteOneToCollection(string $table, int $id, string $database =
* "limit":array
* ]
*/
public function find(string $table, string $database, array $options = []):array|null {
public function find(string $table, string $database = "", array $options = []):array|null {

# Set result
$result = null;
Expand Down Expand Up @@ -951,7 +951,7 @@ public function find(string $table, string $database, array $options = []):array
$result = $this->manager
->from($table)
->where($filters)
->fetch()
->fetchAll()
;

} catch (PDOException $e) {
Expand Down
21 changes: 21 additions & 0 deletions src/Library/String/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,25 @@ public function isLightOrDark():string {

}

/** Public static methods | Randome color
******************************************************
*/

/**
* Random Hex
*
* Return random color hex format
*
* @return string
*/
public static function randomHex():string {

# Generate a random integer between 0 and 16777215 (0xFFFFFF)
$randomColor = mt_rand(0, 16777215);

// Convert to hexadecimal and ensure it is 6 characters long
return sprintf("#%06X", $randomColor);

}

}
Loading

0 comments on commit ef8f922

Please sign in to comment.