diff --git a/package.json b/package.json index b7fac64..1ad86e3 100644 --- a/package.json +++ b/package.json @@ -22,4 +22,4 @@ "object-hash": "^3.0.0", "tslib": "^2.4.1" } -} \ No newline at end of file +} diff --git a/src/Front/Library/Crazyurl.ts b/src/Front/Library/Crazyurl.ts index cc71864..793ac4d 100644 --- a/src/Front/Library/Crazyurl.ts +++ b/src/Front/Library/Crazyurl.ts @@ -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("&"); + + } + } \ No newline at end of file diff --git a/src/Front/Library/Navigator/Client.ts b/src/Front/Library/Navigator/Client.ts new file mode 100644 index 0000000..7d27a68 --- /dev/null +++ b/src/Front/Library/Navigator/Client.ts @@ -0,0 +1,133 @@ +/** + * Navigator + * + * Front TS Scrips for multiple tasks + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2023 Kévin Zarshenas + */ + +/** + * Dependances + */ + +/** + * Client + * + * Methods for manage client navigator + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @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. + } + }); + } */ + } + +} \ No newline at end of file diff --git a/src/Front/Library/Utility/Runner.ts b/src/Front/Library/Utility/Runner.ts index b29c702..6ce4311 100644 --- a/src/Front/Library/Utility/Runner.ts +++ b/src/Front/Library/Utility/Runner.ts @@ -11,6 +11,7 @@ /** * Dependances */ +import {default as NavigatorClient} from "./../Navigator/Client"; import {default as UtilityProcess} from "./Process"; /** @@ -61,6 +62,9 @@ export default class Runner { /** @param _options */ private _options:RunnerOption; + /** @param _navigatorClient */ + private _navigatorClient:NavigatorClient; + /** Public methods ****************************************************** */ @@ -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 => { @@ -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); @@ -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 diff --git a/src/Front/Types/index.d.ts b/src/Front/Types/index.d.ts index d531292..a259a6b 100644 --- a/src/Front/Types/index.d.ts +++ b/src/Front/Types/index.d.ts @@ -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"; diff --git a/src/Front/index.ts b/src/Front/index.ts index 1bb2eb1..a624568 100644 --- a/src/Front/index.ts +++ b/src/Front/index.ts @@ -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"; diff --git a/src/Library/Template/Handlebars.php b/src/Library/Template/Handlebars.php index 0209a39..4b14cc7 100644 --- a/src/Library/Template/Handlebars.php +++ b/src/Library/Template/Handlebars.php @@ -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 diff --git a/tests/Library/File/HeaderTest.php b/tests/Library/File/HeaderTest.php index 07eaf91..ccb23d1 100644 --- a/tests/Library/File/HeaderTest.php +++ b/tests/Library/File/HeaderTest.php @@ -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"]);