From baf7e4f1539fa3053854eb96dd20f2fd9681d0bc Mon Sep 17 00:00:00 2001 From: Rashid Ali Date: Sat, 3 Nov 2018 19:25:37 +0500 Subject: [PATCH] Updated to v1.1.1 --- .github/ISSUE_TEMPLATE/1_Bug_report.md | 13 + composer.json | 2 +- docs/.nojekyll | 0 docs/README.md | 19 ++ docs/_coverpage.md | 10 + docs/_sidebar.md | 10 + docs/config.md | 41 +++ docs/configuration.md | 41 +++ docs/credits.md | 21 ++ docs/demo.md | 117 +++++++++ docs/index.html | 45 ++++ docs/install.md | 7 + docs/middleware.md | 71 +++++ docs/sw.js | 83 ++++++ docs/usage.md | 77 ++++++ readme.md | 345 +------------------------ src/SweetAlertServiceProvider.php | 11 +- src/ToSweetAlert.php | 84 ++++++ src/Toaster.php | 47 +++- src/Views/alert.blade.php | 1 + src/js/sweetalert.all.js | 267 +++++++++++++++++++ 21 files changed, 958 insertions(+), 354 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/1_Bug_report.md create mode 100644 docs/.nojekyll create mode 100644 docs/README.md create mode 100644 docs/_coverpage.md create mode 100644 docs/_sidebar.md create mode 100644 docs/config.md create mode 100644 docs/configuration.md create mode 100644 docs/credits.md create mode 100644 docs/demo.md create mode 100644 docs/index.html create mode 100644 docs/install.md create mode 100644 docs/middleware.md create mode 100644 docs/sw.js create mode 100644 docs/usage.md create mode 100644 src/ToSweetAlert.php create mode 100644 src/js/sweetalert.all.js diff --git a/.github/ISSUE_TEMPLATE/1_Bug_report.md b/.github/ISSUE_TEMPLATE/1_Bug_report.md new file mode 100644 index 0000000..fb61257 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/1_Bug_report.md @@ -0,0 +1,13 @@ +--- +name: "🐛 Bug Report" +about: Report a general package issue + +--- + +- Package Version: #.#.# +- Laravel Version: #.#.# + +### Description: + + +### Steps To Reproduce: \ No newline at end of file diff --git a/composer.json b/composer.json index fcc4fc4..decf20e 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,7 @@ "RealRashid\\SweetAlert\\SweetAlertServiceProvider" ], "aliases": { - "Alert": "RealRashid\\SweetAlert\\Facades" + "Alert": "RealRashid\\SweetAlert\\Facades\\Alert" } } }, diff --git a/docs/.nojekyll b/docs/.nojekyll new file mode 100644 index 0000000..e69de29 diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..099d057 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,19 @@ +

+Total Downloads +Latest Stable Version +License +Support me on Patreon +Support me on Liberapay +

+ +# Introduction + +A BEAUTIFUL, RESPONSIVE, CUSTOMIZABLE, ACCESSIBLE (WAI-ARIA) REPLACEMENT FOR JAVASCRIPT'S POPUP BOXES + +ZERO DEPENDENCIES + +

+ +

+ +> note: if you are using sweet-alert v1.0 you can get READMEfor v1.0 from [here](https://github.com/realrashid/sweet-alert/blob/1.0/readme.md) \ No newline at end of file diff --git a/docs/_coverpage.md b/docs/_coverpage.md new file mode 100644 index 0000000..32278a8 --- /dev/null +++ b/docs/_coverpage.md @@ -0,0 +1,10 @@ +

sweet-alert 1.1.1

+ +

By Rashid Ali

+ +> A BEAUTIFUL, RESPONSIVE, CUSTOMIZABLE, +ZERO DEPENDENCIES + + +[GitHub](https://github.com/realrashid/sweet-alert) +[Get Started](/introduction) \ No newline at end of file diff --git a/docs/_sidebar.md b/docs/_sidebar.md new file mode 100644 index 0000000..8035d36 --- /dev/null +++ b/docs/_sidebar.md @@ -0,0 +1,10 @@ + +* Getting started + + * [Installation](sweet-alert/install.md) + * [Configuration](sweet-alert/config.md) + * [Usage](sweet-alert/usage?id=usage) + * [Middleware](sweet-alert/middleware?id=middleware) + * [Demo](sweet-alert/demo) + +* [Credits](sweet-alert/credits.md) \ No newline at end of file diff --git a/docs/config.md b/docs/config.md new file mode 100644 index 0000000..df86312 --- /dev/null +++ b/docs/config.md @@ -0,0 +1,41 @@ +# Configuration + +> Optional in Laravel 5.5 or above + +After installing the sweet-alert package, register the + +```php +RealRashid\SweetAlert\SweetAlertServiceProvider::class +``` +in your `config/app.php` configuration file: + +```php +'providers' => [ + // Other service providers... + + RealRashid\SweetAlert\SweetAlertServiceProvider::class, +], +``` + +Also, add the `Alert` facade to the `aliases` array in your `app` configuration file: + +```php +'Alert' => RealRashid\SweetAlert\Facades\Alert::class, +``` + +# Include SweetAlert 2 View + +in your master layout + +```php +@include('sweetalert::alert')` +``` + +and run the below command to publish the sweetalert.all.js in your public assets. + +```bash +php artisan vendor:publish --tag=public +``` + +> note: the javascript library of sweetalert.all.js is already loaded in above included view + diff --git a/docs/configuration.md b/docs/configuration.md new file mode 100644 index 0000000..9094551 --- /dev/null +++ b/docs/configuration.md @@ -0,0 +1,41 @@ +# Methods Definition + +#### Alert Methods + +| Method | Argument | +|:---------------------: |:------------------------------: | +| `alert()` | `$title, $message, $type` | +| `alert()->success()` | `$title, $message` | +| `alert()->info()` | `$title, $message` | +| `alert()->warning()` | `$title, $message` | +| `alert()->error()` | `$title, $message` | +| `alert()->question()` | `$title, $message` | +| `alert()->html()` | `$htmltitle, $htmlCode, $type` | +| `toast()` | `$message, $type, $position` | + +#### Chain Methods + +| Chain Method | Argument | Snippet | +|:---------------------: |:------------------------------------------------: |:-----------------------------------------------------------------------------------------------: | +| `persistent()` | `$showConfirmBtn = true, $showCloseBtn = false` | `alert()->success('Alert Persistent', 'Successfully')->persistent(false,true);` | +| `autoClose()` | `$milliseconds = 5000` | `alert()->info('I am going to close after', '5 seconds')->autoClose(5000);` | +| `showConfirmButton()` | `$btnText = 'Ok', $btnColor = '#3085d6'` | `alert()->info('Info', 'Alert')->showConfirmButton('Button Text','#3085d6');` | +| `showCancelButton()` | `$btnText = 'Cencel', $btnColor = '#aaa'` | `alert()->question('Is Post Created', 'Successfully?)->showCancelButton('Button Text','#aaa');` | +| `showCloseButton()` | `$closeButtonAriaLabel = 'aria-label'` | `alert()->warning('Post Created', 'Successfully')->showCloseButton('aria-label');` | +| `footer()` | `$htmlcode` | `alert()->error('Oops...', 'Something went wrong!')->footer('Why do I have this issue?');` | +| `toToast()` | `$position = 'top-right'` | `alert()->success('Post Created', 'Successfully')->toToast();` | +| | | | +| | | | +> can also support multiple chaining + +``` php + +alert() + ->error('Oops...', 'Something went wrong!') + ->footer('Why do I have this issue?') + ->showConfirmButton() + ->showCancelButton() + ->showCloseButton() + ->autoClose(5000); + +``` diff --git a/docs/credits.md b/docs/credits.md new file mode 100644 index 0000000..21f9d29 --- /dev/null +++ b/docs/credits.md @@ -0,0 +1,21 @@ +## Credits + +* [SweetAlert2](https://github.com/sweetalert2/sweetalert2) + +# Contributing + +Please see [CONTRIBUTING](https://github.com/realrashid/sweet-alert/blob/master/CONTRIBUTING.md) and [CODE_OF_CONDUCT](https://github.com/realrashid/sweet-alert/blob/master/CODE_OF_CONDUCT.md) for details. + +# Connect with Me + +* Website: http://realrashid.com +* Email: realrashid05@gmail.com +* Twitter: http://twitter.com/rashidali05 +* Facebook: https://www.facebook.com/rashidali05 +* GitHub: https://github.com/realrashid + +# License + +SweetAlert2 is open-sourced software licensed under the MIT License (MIT). Please see [License File](LICENSE.md) for more information. + +

Made :heart: with Pakistan

diff --git a/docs/demo.md b/docs/demo.md new file mode 100644 index 0000000..c586007 --- /dev/null +++ b/docs/demo.md @@ -0,0 +1,117 @@ +## Alert Demo + +##### Success Alert + +``` php +alert()->success('SuccessAlert','Lorem ipsum dolor sit amet.'); +``` + +

+ SuccessAlert +

+ +##### Info Alert + +``` php +alert()->info('InfoAlert','Lorem ipsum dolor sit amet.'); +``` + +

+ InfoAlert +

+ +##### Warning Alert + +``` php +alert()->warning('WarningAlert','Lorem ipsum dolor sit amet.'); +``` + +

+ WarningAlert +

+ +##### Question Alert + +``` php +alert()->question('QuestionAlert','Lorem ipsum dolor sit amet.'); +``` + +

+ QuestionAlert +

+ +##### Error Alert + +``` php +alert()->error('ErrorAlert','Lorem ipsum dolor sit amet.'); +``` + +

+ ErrorAlert +

+ +##### Html Alert + +``` php +alert()->html('HTML example'," + You can use bold text, + links + and other HTML tags +",'success'); +``` + +

+ HtmlAlert +

+ +# Toast Demo + +##### Success Toast + +``` php +toast('Success Toast','success','top-right'); +``` + +

+ SuccessToast +

+ +##### Info Toast + +``` php +toast('Info Toast','info','top-right'); +``` + +

+ InfoToast +

+ +##### Warning Toast + +``` php +toast('Warning Toast','warning','top-right'); +``` + +

+ WarningToast +

+ +##### Question Toast + +``` php +toast('Question Toast','question','top-right'); +``` + +

+ QuestionToast +

+ +##### Error Toast + +``` php +toast('Error Toast','error','top-right'); +``` + +

+ ErrorToast +

\ No newline at end of file diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..db7ab35 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,45 @@ + + + + + Laravel SweetAlert by Rashid Ali + + + + + + + + + + + + + + + + +
+ + + + + + diff --git a/docs/install.md b/docs/install.md new file mode 100644 index 0000000..f651901 --- /dev/null +++ b/docs/install.md @@ -0,0 +1,7 @@ +# Install + +To get started with SweetAlert2, use Composer to add the package to your project's dependencies: + +``` +composer require realrashid/sweet-alert +``` \ No newline at end of file diff --git a/docs/middleware.md b/docs/middleware.md new file mode 100644 index 0000000..75a80f3 --- /dev/null +++ b/docs/middleware.md @@ -0,0 +1,71 @@ +# Middleware + +#### Using the Middleware + + +First thing first +Let register the middleware in web middleware groups by simply adding the middleware class + +```php +RealRashid\SweetAlert\ToSweetAlert::class +``` + +into the + +`$middlewareGroups` of your `app/Http/Kernel.php` class. + +### Examples + +Now within your controllers, just set your return message and send the proper message and proper type. + +#### Alert + +Error Alert +```php +public function FunctionName(Request $request) +{ + return redirect('login')->with('error', 'Authentication Failed!'); +} + +``` +Success Alert +```php +public function FunctionName(Request $request) +{ + return redirect('login')->with('success', 'Login Successfully!'); +} + +``` + +```php +return redirect('route')->with('type', 'message'); +``` + +All available types +`info` `warning` `question` + +#### Toast + +Error Toast +```php +public function FunctionName(Request $request) +{ + return redirect('login')->with('toast_error', 'Authentication Failed!'); +} + +``` +Success Toast +```php +public function FunctionName(Request $request) +{ + return redirect('login')->with('toast_success', 'Login Successfully!'); +} + +``` + +```php +return redirect('route')->with('type', 'message'); +``` + +All available types +`toast_info` `toast_warning` `toast_question` diff --git a/docs/sw.js b/docs/sw.js new file mode 100644 index 0000000..94461cd --- /dev/null +++ b/docs/sw.js @@ -0,0 +1,83 @@ +/* =========================================================== + * sweetaler sw.js + * =========================================================== + * Copyright 2016 @huxpro + * Licensed under Apache 2.0 + * Register service worker. + * ========================================================== */ + +const RUNTIME = 'sweetaler' +const HOSTNAME_WHITELIST = [ + self.location.hostname, + 'fonts.gstatic.com', + 'fonts.googleapis.com', + 'unpkg.com' +] + +// The Util Function to hack URLs of intercepted requests +const getFixedUrl = (req) => { + var now = Date.now() + var url = new URL(req.url) + + // 1. fixed http URL + // Just keep syncing with location.protocol + // fetch(httpURL) belongs to active mixed content. + // And fetch(httpRequest) is not supported yet. + url.protocol = self.location.protocol + + // 2. add query for caching-busting. + // Github Pages served with Cache-Control: max-age=600 + // max-age on mutable content is error-prone, with SW life of bugs can even extend. + // Until cache mode of Fetch API landed, we have to workaround cache-busting with query string. + // Cache-Control-Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=453190 + if (url.hostname === self.location.hostname) { + url.search += (url.search ? '&' : '?') + 'cache-bust=' + now + } + return url.href +} + +/** + * @Lifecycle Activate + * New one activated when old isnt being used. + * + * waitUntil(): activating ====> activated + */ +self.addEventListener('activate', event => { + event.waitUntil(self.clients.claim()) +}) + +/** + * @Functional Fetch + * All network requests are being intercepted here. + * + * void respondWith(Promise r) + */ +self.addEventListener('fetch', event => { + // Skip some of cross-origin requests, like those for Google Analytics. + if (HOSTNAME_WHITELIST.indexOf(new URL(event.request.url).hostname) > -1) { + // Stale-while-revalidate + // similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale + // Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1 + const cached = caches.match(event.request) + const fixedUrl = getFixedUrl(event.request) + const fetched = fetch(fixedUrl, { cache: 'no-store' }) + const fetchedCopy = fetched.then(resp => resp.clone()) + + // Call respondWith() with whatever we get first. + // If the fetch fails (e.g disconnected), wait for the cache. + // If there’s nothing in cache, wait for the fetch. + // If neither yields a response, return offline pages. + event.respondWith( + Promise.race([fetched.catch(_ => cached), cached]) + .then(resp => resp || fetched) + .catch(_ => { /* eat any errors */ }) + ) + + // Update the cache with the version we fetched (only for ok status) + event.waitUntil( + Promise.all([fetchedCopy, caches.open(RUNTIME)]) + .then(([response, cache]) => response.ok && cache.put(event.request, response)) + .catch(_ => { /* eat any errors */ }) + ) + } +}) \ No newline at end of file diff --git a/docs/usage.md b/docs/usage.md new file mode 100644 index 0000000..a83e4a6 --- /dev/null +++ b/docs/usage.md @@ -0,0 +1,77 @@ +# Usage + +#### Using Facade + +Import Alert Facade first! + +```php +use RealRashid\SweetAlert\Facades\Alert; +``` +or + +```php +Use Alert; +``` +in your controller + ```php + Alert::alert('Title', 'Message', 'Type'); + ``` + ```php + Alert::success('Success Title', 'Success Message'); + ``` + ```php + Alert::info('Info Title', 'Info Message'); + ``` + ```php + Alert::warning('Warning Title', 'Warning Message'); + ``` + ```php + Alert::error('Error Title', 'Error Message'); + ``` + ```php + Alert::question('Question Title', 'Question Message'); + ``` + ```php + Alert::html('Html Title', 'Html Code', 'Type'); + ``` + ```php + Alert::toast('Toast Message', 'Toast Type', 'Toast Position'); + ``` + +### Using the helper function + +#### Alert + + ```php + alert('Title','Lorem Lorem Lorem', 'success'); + ``` + + ```php + alert()->success('Title','Lorem Lorem Lorem'); + ``` + + ```php + alert()->info('Title','Lorem Lorem Lorem'); + ``` + + ```php + alert()->warning('Title','Lorem Lorem Lorem'); + ``` + + ```php + alert()->question('Title','Lorem Lorem Lorem'); + ``` + + ```php + alert()->error('Title','Lorem Lorem Lorem'); + ``` + + ```php + alert()->html('HTML example'," You can use bold text, links and other HTML tags ",'success'); + ``` + +#### Toast + + ```php + toast('Your Post as been submited!','success','top-right'); + ``` diff --git a/readme.md b/readme.md index 183c686..f9052df 100644 --- a/readme.md +++ b/readme.md @@ -6,8 +6,6 @@ Support me on Liberapay

-> note: if you are using sweet-alert v1.0 you can get READMEfor v1.0 from [here](https://github.com/realrashid/sweet-alert/blob/1.0/readme.md) - # Introduction A BEAUTIFUL, RESPONSIVE, CUSTOMIZABLE, ACCESSIBLE (WAI-ARIA) REPLACEMENT FOR JAVASCRIPT'S POPUP BOXES @@ -18,347 +16,10 @@ ZERO DEPENDENCIES

-# Install - -To get started with SweetAlert2, use Composer to add the package to your project's dependencies: - -``` -composer require realrashid/sweet-alert -``` - -## Configuration - -> Note, Optional in `Laravel 5.5 or +` - -After installing the sweet-alert package, register the -`RealRashid\SweetAlert\SweetAlertServiceProvider::class` -in your `config/app.php` configuration file: - -```php -'providers' => [ - // Other service providers... - - RealRashid\SweetAlert\SweetAlertServiceProvider::class, -], -``` - -Also, add the `Alert` facade to the `aliases` array in your `app` configuration file: - -```php -'Alert' => RealRashid\SweetAlert\Facades\Alert::class, -``` - -## Import SweetAlert 2 Library - -in your master layout - -```javascript - -``` - -and include sweetalert view - -`@include('sweetalert::alert')` - -> Include the sweetalert view below the cdn link in your layout! - -## Usage - -#### Using Facade - -Import Alert Facade first! - -`use RealRashid\SweetAlert\Facades\Alert;` or -`Use Alert;` in your controller - -* `Alert::alert('Title', 'Message', 'Type');` -* `Alert::success('Success Title', 'Success Message');` -* `Alert::info('Info Title', 'Info Message');` -* `Alert::warning('Warning Title', 'Warning Message');` -* `Alert::error('Error Title', 'Error Message');` -* `Alert::question('Question Title', 'Question Message');` -* `Alert::html('Html Title', 'Html Code', 'Type');` -* `Alert::toast('Toast Message', 'Toast Type', 'Toast Position');` - -### Using the helper function - -#### Alert - -* `alert('Title','Lorem Lorem Lorem', 'success');` - -* `alert()->success('Title','Lorem Lorem Lorem');` - -* `alert()->info('Title','Lorem Lorem Lorem');` - -* `alert()->warning('Title','Lorem Lorem Lorem');` - -* `alert()->question('Title','Lorem Lorem Lorem');` - -* `alert()->error('Title','Lorem Lorem Lorem');` - -* `alert()->html('HTML example'," You can use bold text, links and other HTML tags ",'success');` - -#### Toast - -* `toast('Your Post as been submited!','success','top-right');` - -### Demo - -#### Success Alert - -```php -/** - * Store a newly created resource in storage. - * - * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response -*/ -public function store(PostRequest $request) -{ - $post = Post::create($request->all()); - - if ($post) { - alert()->success('Post Created', 'Successfully'); - return redirect()->route('posts.index'); - } -} -``` - -

- SuccessAlert -

- -#### Error Alert - -```php - - /** - * Get the failed login response instance. - * - * @param \Illuminate\Http\Request $request -* @return \Symfony\Component\HttpFoundation\Response -* -* @throws \Illuminate\Validation\ValidationException -*/ -protected function sendFailedLoginResponse(Request $request) -{ - alert()->error('Oops...', 'Something went wrong!'); - - throw ValidationException::withMessages([ - $this->username() => [trans('auth.failed')], - ]); -} - -``` - -

- ErrorAlert -

- -#### Success Toast - -``` php -/** - * Remove the specified resource from storage. - * - * @param \App\Post $post - * @return \Illuminate\Http\Response -*/ -public function destroy($id) -{ - $post = Post::find($id); - - $post->delete(); - - if ($post) { - toast('Post Deleted Successfully','success','top-right'); - return redirect()->route('posts.index'); - } -} -``` - -

- SuccessToast -

- - -## Methods Definition - -#### Alert Methods - -| Method | Argument | -|:---------------------: |:------------------------------: | -| `alert()` | `$title, $message, $type` | -| `alert()->success()` | `$title, $message` | -| `alert()->info()` | `$title, $message` | -| `alert()->warning()` | `$title, $message` | -| `alert()->error()` | `$title, $message` | -| `alert()->question()` | `$title, $message` | -| `alert()->html()` | `$htmltitle, $htmlCode, $type` | -| `toast()` | `$message, $type, $position` | - -#### Chain Methods - -| Chain Method | Argument | Snippet | -|:---------------------: |:------------------------------------------------: |:-----------------------------------------------------------------------------------------------: | -| `persistent()` | `$showConfirmBtn = true, $showCloseBtn = false` | `alert()->success('Alert Persistent', 'Successfully')->persistent(false,true);` | -| `autoClose()` | `$milliseconds = 5000` | `alert()->info('I am going to close after', '5 seconds')->autoClose(5000);` | -| `showConfirmButton()` | `$btnText = 'Ok', $btnColor = '#3085d6'` | `alert()->info('Info', 'Alert')->showConfirmButton('Button Text','#3085d6');` | -| `showCancelButton()` | `$btnText = 'Cencel', $btnColor = '#aaa'` | `alert()->question('Is Post Created', 'Successfully?)->showCancelButton('Button Text','#aaa');` | -| `showCloseButton()` | `$closeButtonAriaLabel = 'aria-label'` | `alert()->warning('Post Created', 'Successfully')->showCloseButton('aria-label');` | -| `footer()` | `$htmlcode` | `alert()->error('Oops...', 'Something went wrong!')->footer('Why do I have this issue?');` | -| `toToast()` | `$position = 'top-right'` | `alert()->success('Post Created', 'Successfully')->toToast();` | -| | | | -| | | | -> can also support multiple chaining - -``` php - -alert() - ->error('Oops...', 'Something went wrong!') - ->footer('Why do I have this issue?') - ->showConfirmButton() - ->showCancelButton() - ->showCloseButton() - ->autoClose(5000); - -``` - -## Screenshots - -#### Alert - -##### Success Alert - -``` php -alert()->success('SuccessAlert','Lorem ipsum dolor sit amet.'); -``` - -

- SuccessAlert -

- -##### Info Alert - -``` php -alert()->info('InfoAlert','Lorem ipsum dolor sit amet.'); -``` - -

- InfoAlert -

- -##### Warning Alert - -``` php -alert()->warning('WarningAlert','Lorem ipsum dolor sit amet.'); -``` - -

- WarningAlert -

- -##### Question Alert - -``` php -alert()->question('QuestionAlert','Lorem ipsum dolor sit amet.'); -``` - -

- QuestionAlert -

- -##### Error Alert - -``` php -alert()->error('ErrorAlert','Lorem ipsum dolor sit amet.'); -``` - -

- ErrorAlert -

- -##### Html Alert - -``` php -alert()->html('HTML example'," - You can use bold text, - links - and other HTML tags -",'success'); -``` - -

- HtmlAlert -

- -#### Toast - -##### Success Toast - -``` php -toast('Success Toast','success','top-right'); -``` - -

- SuccessToast -

- -##### Info Toast - -``` php -toast('Info Toast','info','top-right'); -``` - -

- InfoToast -

- -##### Warning Toast - -``` php -toast('Warning Toast','warning','top-right'); -``` - -

- WarningToast -

- -##### Question Toast - -``` php -toast('Question Toast','question','top-right'); -``` - -

- QuestionToast -

- -##### Error Toast - -``` php -toast('Error Toast','error','top-right'); -``` - -

- ErrorToast -

- -## Contributing - -Please see [CONTRIBUTING](https://github.com/realrashid/sweet-alert/blob/master/CONTRIBUTING.md) and [CODE_OF_CONDUCT](https://github.com/realrashid/sweet-alert/blob/master/CODE_OF_CONDUCT.md) for details. - -## Credits - -* [SweetAlert2](https://github.com/sweetalert2/sweetalert2) - -## Connect with Me +

+ Documentation +

-* Website: http://realrashid.com -* Email: realrashid05@gmail.com -* Twitter: http://twitter.com/rashidali05 -* Facebook: https://www.facebook.com/rashidali05 -* GitHub: https://github.com/realrashid ## License diff --git a/src/SweetAlertServiceProvider.php b/src/SweetAlertServiceProvider.php index e5ddc33..d0bf992 100644 --- a/src/SweetAlertServiceProvider.php +++ b/src/SweetAlertServiceProvider.php @@ -18,8 +18,14 @@ public function boot() $this->loadViewsFrom(__DIR__ . '/Views', 'sweetalert'); $this->publishes( - [__DIR__ . '/Views' => resource_path('views/vendor/sweetalert'), ] + [ + __DIR__ . '/Views' => resource_path('views/vendor/sweetalert'), + ] ); + + $this->publishes([ + __DIR__.'/js' => public_path('vendor/sweetalert'), + ], 'public'); } /** @@ -31,7 +37,8 @@ public function register() { $this->app->bind( 'RealRashid\SweetAlert\Storage\SessionStore', - 'RealRashid\SweetAlert\Storage\AlertSessionStore' + 'RealRashid\SweetAlert\Storage\AlertSessionStore', + 'RealRashid\SweetAlert\ToSweetAlert' ); $this->app->singleton('alert', function ($app) { return $this->app->make('RealRashid\SweetAlert\Toaster'); diff --git a/src/ToSweetAlert.php b/src/ToSweetAlert.php new file mode 100644 index 0000000..da36986 --- /dev/null +++ b/src/ToSweetAlert.php @@ -0,0 +1,84 @@ +session()->has('success')) { + alert()->success($request->session()->get('success')); + } + + if ($request->session()->has('info')) { + alert()->info($request->session()->get('info')); + } + + if ($request->session()->has('warning')) { + alert()->warning($request->session()->get('warning')); + } + + if ($request->session()->has('question')) { + alert()->question($request->session()->get('question')); + } + + if ($request->session()->has('info')) { + alert()->info($request->session()->get('info')); + } + + if ($request->session()->has('error')) { + $error = $request->session()->get('error'); + + if (!is_string($error)) { + $error = $this->getErrors($error->getMessages()); + } + + alert()->error($error)->toHtml(); + } + + if ($request->session()->has('toast_success')) { + alert()->toast($request->session()->get('toast_success'), 'success'); + } + + if ($request->session()->has('toast_info')) { + toast($request->session()->get('toast_info'), 'info'); + } + + if ($request->session()->has('toast_warning')) { + toast($request->session()->get('toast_warning'), 'warning'); + } + + if ($request->session()->has('toast_question')) { + toast($request->session()->get('toast_question'), 'question'); + } + + if ($request->session()->has('toast_error')) { + toast($request->session()->get('toast_error'), 'error'); + } + + return $next($request); + } + + /** + * + * + * @param $errors + * + * @return string + */ + private function getErrors($errors) + { + $errors = collect($errors); + + return $errors->flatten()->implode('
'); + } +} diff --git a/src/Toaster.php b/src/Toaster.php index 504e026..ae1e957 100644 --- a/src/Toaster.php +++ b/src/Toaster.php @@ -35,7 +35,7 @@ public function __construct(SessionStore $session) protected function setDefaultConfig() { $this->config = [ - 'timer' => env('SWEET_ALERT_AUTOCLOSE', 3000), + 'timer' => env('ALERT_TIMER', 5000), 'title' => '', 'text' => '', 'showConfirmButton' => false, @@ -75,7 +75,7 @@ public function alert($title = '', $text = '', $type = null) * * @return RealRashid\SweetAlert\Toaster::alert(); */ - public function success($title = '', $text) + public function success($title = '', $text = '') { $this->alert($title, $text, 'success'); @@ -91,7 +91,7 @@ public function success($title = '', $text) * * @return RealRashid\SweetAlert\Toaster::alert(); */ - public function info($title = '', $text) + public function info($title = '', $text = '') { $this->alert($title, $text, 'info'); @@ -107,7 +107,7 @@ public function info($title = '', $text) * * @return RealRashid\SweetAlert\Toaster::alert(); */ - public function warning($title = '', $text) + public function warning($title = '', $text = '') { $this->alert($title, $text, 'warning'); @@ -123,7 +123,7 @@ public function warning($title = '', $text) * * @return RealRashid\SweetAlert\Toaster::alert(); */ - public function question($title = '', $text) + public function question($title = '', $text = '') { $this->alert($title, $text, 'question'); @@ -139,7 +139,7 @@ public function question($title = '', $text) * * @return RealRashid\SweetAlert\Toaster::alert(); */ - public function error($title = '', $text) + public function error($title = '', $text = '') { $this->alert($title, $text, 'error'); @@ -156,7 +156,7 @@ public function error($title = '', $text) * * @return RealRashid\SweetAlert\Toaster::alert(); */ - public function html($title, $code, $type) + public function html($code = '', $type = '', $title = '') { $this->config['title'] = $title; @@ -181,7 +181,7 @@ public function html($title, $code, $type) * * @return RealRashid\SweetAlert\Toaster::alert(); */ - public function toast($title, $type, $position = 'top-right') + public function toast($title = '', $type = '', $position = 'bottom-right') { $this->config['toast'] = true; $this->config['title'] = $title; @@ -196,7 +196,7 @@ public function toast($title, $type, $position = 'top-right') /* ** - * Convert any alert method to Toast + * Convert any alert modal to Toast * * @param string $position * @@ -213,6 +213,21 @@ public function toToast($position = 'top-right') return $this; } + /* + ** + * Convert any alert modal to html + * + * @return $this; + */ + public function toHtml() + { + $this->config['html'] = $this->config['text']; + unset($this->config['text']); + $this->flash(); + + return $this; + } + /* ** * Add footer section to alert() @@ -334,6 +349,20 @@ public function showCloseButton($closeButtonAriaLabel = 'aria-label') return $this; } + /* + ** + * Hide close button from alert or toast + * + * @return the $this; + */ + public function hideCloseButton() + { + $this->config['showCloseButton'] = false; + $this->flash(); + + return $this; + } + /** * Remove the timer from config option. * diff --git a/src/Views/alert.blade.php b/src/Views/alert.blade.php index 9fb176b..be53347 100644 --- a/src/Views/alert.blade.php +++ b/src/Views/alert.blade.php @@ -1,3 +1,4 @@ + @if (Session::has('alert.config'))