Skip to content

Commit

Permalink
Improvement & debug
Browse files Browse the repository at this point in the history
- Fix new year bug
- Improve runner
- Add warn when try to close pas
  • Loading branch information
kekefreedog committed Jan 2, 2024
1 parent 386b7ba commit 3c5d15a
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
"object-hash": "^3.0.0",
"tslib": "^2.4.1"
}
}
}
32 changes: 32 additions & 0 deletions src/Front/Library/Crazyurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,36 @@ export default class Crazyurl{

}

/**
* To Query String
*
* Convert multidimensional object to query string
*
* @source https://stackoverflow.com/questions/26084733/convert-multidimensional-object-to-query-string
* @param obj
* @param prefix
* @returns {string}
*/
public static toQueryString = (obj:object, prefix:string = ""):string => {

var str = [], k, v;
for(var p in obj) {
if (!obj.hasOwnProperty(p)) {continue;} // skip things from the prototype
if (~p.indexOf('[')) {
k = prefix ? prefix + "[" + p.substring(0, p.indexOf('[')) + "]" + p.substring(p.indexOf('[')) : p;
// only put whatever is before the bracket into new brackets; append the rest
} else {
k = prefix ? prefix + "[" + p + "]" : p;
}
v = obj[p];
// @ts-ignore
str.push(typeof v == "object" ?
Crazyurl.toQueryString(v, k) :
encodeURI(k) + "=" + encodeURIComponent(v)
);
}
return str.join("&");

}

}
133 changes: 133 additions & 0 deletions src/Front/Library/Navigator/Client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Navigator
*
* Front TS Scrips for multiple tasks
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @copyright 2022-2023 Kévin Zarshenas
*/

/**
* Dependances
*/

/**
* Client
*
* Methods for manage client navigator
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @copyright 2022-2023 Kévin Zarshenas
*/
export default class Client {

/** Private parameters
******************************************************
*/

/** @var message:string */
private _closeMessage:string = 'Are you sure you want to leave ?';

/** @var _customCloseMessage:string */
private _customCloseMessage:string;

/** Public parameters
******************************************************
*/

/** @var isActive Check if event close is active */
public isPreventCloseActive = false;


/** Public methods
******************************************************
*/

/**
* Prevent Close
*
* @param message
* @returns {void}
*/
public preventClose = (message:string = "") => {

// Set message
this._customCloseMessage = message ? message : this._closeMessage;

// Add event listener
window.addEventListener("beforeunload", this._beforeUnloadEvent);

// Switch status
this.isPreventCloseActive = true;

}

/**
* Desable Close
*
* @param message
* @returns {void}
*/
public disablePreventClose = () => {

// Add event listener
window.removeEventListener("beforeunload", this._beforeUnloadEvent);

// Switch status
this.isPreventCloseActive = false;

}

/** Private methods
******************************************************
*/

/**
* Before Unload Event
*
* @param event
* @returns {void}
*/
private _beforeUnloadEvent = (event:Event):void => {

// Prevent default
event.preventDefault();

//
// @ts-ignore
event.returnValue = `Are you sure you want to leave?`;

/* // check if event dialog
if(
"dialog" in event &&
typeof event.dialog === "object" &&
event.dialog &&
"setMessage" in event.dialog &&
typeof event.dialog.setMessage === "function" &&
event.dialog.setMessage &&
"setButtonLabel" in event.dialog &&
typeof event.dialog.setButtonLabel === "function" &&
event.dialog.setButtonLabel
){
// Set message
event.dialog.setMessage(this._customCloseMessage);
// Set button
event.dialog.setButtonLabel("Save");
// Check show
if("show" in event.dialog && typeof event.dialog.show === "function")
// Show dialog
event.dialog.show().then(async (result) => {
if (result == "Save"){
// save the document.
}
});
} */
}

}
31 changes: 30 additions & 1 deletion src/Front/Library/Utility/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/**
* Dependances
*/
import {default as NavigatorClient} from "./../Navigator/Client";
import {default as UtilityProcess} from "./Process";

/**
Expand Down Expand Up @@ -61,6 +62,9 @@ export default class Runner {
/** @param _options */
private _options:RunnerOption;

/** @param _navigatorClient */
private _navigatorClient:NavigatorClient;

/** Public methods
******************************************************
*/
Expand All @@ -83,6 +87,9 @@ export default class Runner {
// Chain the methods in sequence using Promises
let chain = Promise.resolve(options);

// New navigator client
this._navigatorClient = new NavigatorClient();

// Initial setup before any methods
chain = chain.then(
options => {
Expand All @@ -105,6 +112,9 @@ export default class Runner {
label: UtilityProcess.capitalize(UtilityProcess.spaceBeforeCapital(method))
});

// Start prevent close
this._navigatorClient.preventClose();

// Return "abstract" class
return this.setUpBeforeClass(options);

Expand All @@ -115,13 +125,32 @@ export default class Runner {
runMethods.forEach(method => {
chain = chain
.then(options => {

// Increment current
options._info.run.current++;

// Set in progress
if(options._info.run.current>0)
options._info.status = "In Progress";

// Run setup
return this.setUpBeforeMethod(options)
})
.then(this[method])
.then(this.tearDownAfterMethod);
.then(
options => {

// Return last method
let result = this.tearDownAfterMethod(options);

// Close prevent close
this._navigatorClient.disablePreventClose();

// Return result
return result;

}
);
});

// Final teardown after all methods
Expand Down
1 change: 1 addition & 0 deletions src/Front/Types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Crazypage from "./../Library/Crazypage";
* Dependances
*/
export {default as Componentregister} from "./../Library/Componentregister";
export {default as NavigatorClient} from "./../Library/Navigator/Client";
export {default as ColorSchema} from "./../Library/Utility/ColorSchema";
export {default as UtilityProcess} from "./../Library/Utility/Process";
export {default as Crazycomponent} from "./../Library/Crazycomponent";
Expand Down
1 change: 1 addition & 0 deletions src/Front/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Dependances
*/
export {default as Componentregister} from "./Library/Componentregister";
export {default as NavigatorClient} from "./Library/Navigator/Client";
export {default as ColorSchema} from "./Library/Utility/ColorSchema";
export {default as UtilityProcess} from "./Library/Utility/Process";
export {default as Crazycomponent} from "./Library/Crazycomponent";
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Template/Handlebars.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
*/
use CrazyPHP\Library\Template\Handlebars\Helpers;
use CrazyPHP\Exception\CrazyException;
use Symfony\Component\Finder\Finder;
use CrazyPHP\Library\Form\Process;
use CrazyPHP\Library\Cache\Cache;
use CrazyPHP\Library\File\File;
use LightnCandy\LightnCandy;
use Symfony\Component\Finder\Finder;

/**
* Handlebars
Expand Down
2 changes: 1 addition & 1 deletion tests/Library/File/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testHeaderPhp():void {
$headerPhp = Header::get("php");

# Check result is equal to header generated
$this->assertEquals(html_entity_decode($headerPhp), self::RESULT);
$this->assertEquals(html_entity_decode($headerPhp), str_replace("2023", date("Y"), self::RESULT));

# Remove cache folder
$this->cache->deleteMultiple(["test-string", "test-array", "test-template"]);
Expand Down

0 comments on commit 3c5d15a

Please sign in to comment.