diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..4c65abb --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,27 @@ +# Changelog + +All notable changes to `` will be documented in this file. + +Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles. + +## [v1.1] - 2018-03-25 + +### Added +- `alert() method` +- `alert()->success() method` +- `alert()->info() method` +- `alert()->wanring() method` +- `alert()->question() method` +- `alert()->error() method` +- `alert()->html() method` +- `toast() method` +- `showConfirmButton() method` +- `showCloseButton() method` +- `showCancelButton() method` +- `persistent() method` +- `autoClose() method` +- `toToast() method` +- `footer() method` + +### Deprecated +- v1.0 diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..4810738 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,9 @@ +The Laravel SweetAlert2 code of conduct is derived from the Ruby code of conduct. Any violations of the code of conduct may be reported to Rashid Ali (realrashid05@gmail.com). + +- Participants will be tolerant of opposing views. + +- Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks. + +- When interpreting the words and actions of others, participants should always assume good intentions. + +- Behavior which can be reasonably considered harassment will not be tolerated. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..8c179e8 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,25 @@ +If you would like to contribute enhancements or fixes, please do the following: + +1. Create Laravel Project via Composer or Laravel Installer. + +2. In the project root directory create folder named as packages. + +3. Fork the `sweet-alert` repository and clone it in packages. + +4. Now goto your `composer.json` file and add this `"RealRashid\\SweetAlert\\": "packages/realrashid/sweet-alert/src"` line under `autoload` `psr-4` section + +like below + +``` json +"autoload": { + "classmap": [ + "database/seeds", + "database/factories" + ], + "psr-4": { + "App\\": "app/", + "RealRashid\\SweetAlert\\": "packages/realrashid/sweet-alert/src" + } +}, + +``` diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..437bb8f --- /dev/null +++ b/ISSUE_TEMPLATE.md @@ -0,0 +1,25 @@ + + +## Detailed description + +Provide a detailed description of the change or addition you are proposing. + +Make it clear if the issue is a bug, an enhancement or just a question. + +## Context + +Why is this change important to you? How would you use it? + +How can it benefit other users? + +## Possible implementation + +Not obligatory, but suggest an idea for implementing addition or change. + +## Your environment + +Include as many relevant details about the environment you experienced the bug in and how to reproduce it. + +* Version used (e.g. Laravel v5.6, SweetAlert2 v7.18.0): +* Operating system and version (e.g. Ubuntu 16.04, Windows 7): +* Link to your project: diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..bd8ac34 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) 2018 Rashid Ali + +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. diff --git a/composer.json b/composer.json index b42030c..fcc4fc4 100644 --- a/composer.json +++ b/composer.json @@ -1,39 +1,59 @@ { - "name": "realrashid/sweet-alert", - "description": "SweetAlert2 for Laravel 5.x by Rashid Ali", - "authors": [{ - "name": "Rashid Ali", - "email": "realrashid05@gmail.com" - }], - "license": "MIT", - "require": { - "php": ">=5.4.0", - "illuminate/support": "~5.4" - }, - "autoload": { - "psr-0": { - "RealRashid\\SweetAlert\\": "src/" + "name": "realrashid/sweet-alert", + "type": "library", + "description": "SweetAlert2 for Laravel 5.5.* by Rashid Ali", + "keywords": [ + "laravel", + "sweet-alert2", + "sweet", + "notifier", + "alert", + "noty" + ], + "homepage": "https://github.com/realrashid/sweet-alert", + "license": "MIT", + "authors": [ + { + "name": "Rashid Ali", + "email": "realrashid05@gmail.com", + "homepage": "https://realrashid.com", + "role": "Developer" + } + ], + "support": { + "email": "realrashid05@gmail.com", + "issues": "https://github.com/realrashid/sweet-alert/issues", + "source": "https://github.com/realrashid/sweet-alert", + "docs": "https://github.com/realrashid/sweet-alert/README.md" + }, + "require": { + "illuminate/support": "~5.1", + "illuminate/session": "~5.1", + "php": "~5.6|~7.0" + }, + "require-dev": { + "phpunit/phpunit": ">=5.4.3", + "squizlabs/php_codesniffer": "^2.3" }, - "files": [ - "src/RealRashid/SweetAlert/functions.php" - ] - }, - "extra": { - "branch-alias": { - "dev-master": "3.0-dev" + "autoload": { + "psr-4": { + "RealRashid\\SweetAlert\\": "src" }, + "files": [ + "src/functions.php" + ] + }, + "extra": { "laravel": { "providers": [ "RealRashid\\SweetAlert\\SweetAlertServiceProvider" ], "aliases": { - "Alert": "RealRashid\\SweetAlert\\Facades\\Alert::class" + "Alert": "RealRashid\\SweetAlert\\Facades" } } }, "config": { "sort-packages": true - }, - "minimum-stability": "dev", - "prefer-stable": true + } } diff --git a/imgs/alert/ErrorAlert.png b/imgs/alert/ErrorAlert.png new file mode 100644 index 0000000..80dec84 Binary files /dev/null and b/imgs/alert/ErrorAlert.png differ diff --git a/imgs/alert/HtmlAlert.png b/imgs/alert/HtmlAlert.png new file mode 100644 index 0000000..77352f1 Binary files /dev/null and b/imgs/alert/HtmlAlert.png differ diff --git a/imgs/alert/InfoAlert.png b/imgs/alert/InfoAlert.png new file mode 100644 index 0000000..b1bbc10 Binary files /dev/null and b/imgs/alert/InfoAlert.png differ diff --git a/imgs/alert/QuestionAlert.png b/imgs/alert/QuestionAlert.png new file mode 100644 index 0000000..b3126b7 Binary files /dev/null and b/imgs/alert/QuestionAlert.png differ diff --git a/imgs/alert/SuccessAlert.png b/imgs/alert/SuccessAlert.png new file mode 100644 index 0000000..ceeaed2 Binary files /dev/null and b/imgs/alert/SuccessAlert.png differ diff --git a/imgs/alert/SuccessAlert1.png b/imgs/alert/SuccessAlert1.png new file mode 100644 index 0000000..e3f2d6b Binary files /dev/null and b/imgs/alert/SuccessAlert1.png differ diff --git a/imgs/alert/SweetAlert.png b/imgs/alert/SweetAlert.png new file mode 100644 index 0000000..4852a9b Binary files /dev/null and b/imgs/alert/SweetAlert.png differ diff --git a/imgs/alert/WarningAlert.png b/imgs/alert/WarningAlert.png new file mode 100644 index 0000000..fe226b7 Binary files /dev/null and b/imgs/alert/WarningAlert.png differ diff --git a/imgs/auto-close.PNG b/imgs/auto-close.PNG deleted file mode 100644 index 57da7cc..0000000 Binary files a/imgs/auto-close.PNG and /dev/null differ diff --git a/imgs/basic-msg.PNG b/imgs/basic-msg.PNG deleted file mode 100644 index e83796c..0000000 Binary files a/imgs/basic-msg.PNG and /dev/null differ diff --git a/imgs/delete.PNG b/imgs/delete.PNG deleted file mode 100644 index 03b4848..0000000 Binary files a/imgs/delete.PNG and /dev/null differ diff --git a/imgs/deleted.PNG b/imgs/deleted.PNG deleted file mode 100644 index 9a18214..0000000 Binary files a/imgs/deleted.PNG and /dev/null differ diff --git a/imgs/html-elements.PNG b/imgs/html-elements.PNG deleted file mode 100644 index afa986d..0000000 Binary files a/imgs/html-elements.PNG and /dev/null differ diff --git a/imgs/intro.PNG b/imgs/intro.PNG deleted file mode 100644 index 0611f29..0000000 Binary files a/imgs/intro.PNG and /dev/null differ diff --git a/imgs/modal-with-image.PNG b/imgs/modal-with-image.PNG deleted file mode 100644 index b532509..0000000 Binary files a/imgs/modal-with-image.PNG and /dev/null differ diff --git a/imgs/rashid-coming-soon.PNG b/imgs/rashid-coming-soon.PNG deleted file mode 100644 index 2b2c8f1..0000000 Binary files a/imgs/rashid-coming-soon.PNG and /dev/null differ diff --git a/imgs/toast/ErrorToast.png b/imgs/toast/ErrorToast.png new file mode 100644 index 0000000..d4c6e55 Binary files /dev/null and b/imgs/toast/ErrorToast.png differ diff --git a/imgs/toast/InfoToast.png b/imgs/toast/InfoToast.png new file mode 100644 index 0000000..47c581c Binary files /dev/null and b/imgs/toast/InfoToast.png differ diff --git a/imgs/toast/QuestionToast.png b/imgs/toast/QuestionToast.png new file mode 100644 index 0000000..41d1c77 Binary files /dev/null and b/imgs/toast/QuestionToast.png differ diff --git a/imgs/toast/SuccessToast.png b/imgs/toast/SuccessToast.png new file mode 100644 index 0000000..7e02afe Binary files /dev/null and b/imgs/toast/SuccessToast.png differ diff --git a/imgs/toast/SuccessToast1.png b/imgs/toast/SuccessToast1.png new file mode 100644 index 0000000..57c4e2e Binary files /dev/null and b/imgs/toast/SuccessToast1.png differ diff --git a/imgs/toast/SweetToast.png b/imgs/toast/SweetToast.png new file mode 100644 index 0000000..7f0c7b7 Binary files /dev/null and b/imgs/toast/SweetToast.png differ diff --git a/imgs/toast/WarningToast.png b/imgs/toast/WarningToast.png new file mode 100644 index 0000000..c02da67 Binary files /dev/null and b/imgs/toast/WarningToast.png differ diff --git a/imgs/types/error.png b/imgs/types/error.png deleted file mode 100644 index 1428f9e..0000000 Binary files a/imgs/types/error.png and /dev/null differ diff --git a/imgs/types/info.png b/imgs/types/info.png deleted file mode 100644 index 6b3ab2a..0000000 Binary files a/imgs/types/info.png and /dev/null differ diff --git a/imgs/types/question.png b/imgs/types/question.png deleted file mode 100644 index 7db665f..0000000 Binary files a/imgs/types/question.png and /dev/null differ diff --git a/imgs/types/success.png b/imgs/types/success.png deleted file mode 100644 index a9ada96..0000000 Binary files a/imgs/types/success.png and /dev/null differ diff --git a/imgs/types/warning.png b/imgs/types/warning.png deleted file mode 100644 index 3be5765..0000000 Binary files a/imgs/types/warning.png and /dev/null differ diff --git a/readme.md b/readme.md index a000aae..183c686 100644 --- a/readme.md +++ b/readme.md @@ -2,8 +2,12 @@ Total Downloads Latest Stable Version License +Support me on Patreon +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 @@ -11,12 +15,12 @@ A BEAUTIFUL, RESPONSIVE, CUSTOMIZABLE, ACCESSIBLE (WAI-ARIA) REPLACEMENT FOR JAV ZERO DEPENDENCIES

- +

# Install -To get started with SweetAlert, use Composer to add the package to your project's dependencies: +To get started with SweetAlert2, use Composer to add the package to your project's dependencies: ``` composer require realrashid/sweet-alert @@ -24,7 +28,11 @@ composer require realrashid/sweet-alert ## Configuration -After installing the SweetAlert library, register the `RealRashid\SweetAlert\SweetAlertServiceProvider::class` in your `config/app.php` configuration file: +> 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' => [ @@ -34,292 +42,326 @@ After installing the SweetAlert library, register the `RealRashid\SweetAlert\Swe ], ``` -Also, add the `SweetAlert` facade to the `aliases` array in your `app` configuration file: +Also, add the `Alert` facade to the `aliases` array in your `app` configuration file: ```php 'Alert' => RealRashid\SweetAlert\Facades\Alert::class, ``` -> Note, there is a alert() function available, so unless you really want to use the Facade, there's no need to include it. - -### Import SweetAlert Libraries +## Import SweetAlert 2 Library -in your views - -```css - -``` +in your master layout ```javascript - + ``` -# Usage +and include sweetalert view -## Basic +`@include('sweetalert::alert')` -From your application, call the `flash` method with a message and type. +> Include the sweetalert view below the cdn link in your layout! -```php -alert()->flash('Welcome back!', 'success'); -``` +## Usage -Within a view, you can now check if a flash message exists and output it. +#### Using Facade -```php -@if (alert()->ready()) - -@endif -``` +Import Alert Facade first! -## Options +`use RealRashid\SweetAlert\Facades\Alert;` or +`Use Alert;` in your controller -You can pass additional options to the `flash` method, which are then easily accessible within your view. +* `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');` -```php -alert()->flash(title, modal type, options[]); -``` +### Using the helper function -# Alert Types +#### Alert -A basic message! +* `alert('Title','Lorem Lorem Lorem', 'success');` -in controller +* `alert()->success('Title','Lorem Lorem Lorem');` -```php -alert()->flash('Welcome to Laravel SweetAlert By Rashid Ali!'); -``` +* `alert()->info('Title','Lorem Lorem Lorem');` -in view +* `alert()->warning('Title','Lorem Lorem Lorem');` -```javascript -@if (alert()->ready()) - -@endif +* `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

- -A success message! - -in controller +#### Error Alert ```php -alert()->flash('Welcome back!', 'success', [ - 'text' => 'Welcome to Laravel SweetAlert By Rashid Ali!' + + /** + * 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')], ]); +} + ``` -in view +

+ ErrorAlert +

-```javascript -@if (alert()->ready()) - -@endif +#### 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

-A message with auto close timer! -in controller +## 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); -```php -alert()->flash('Welcome back!', 'success', [ - 'text' => 'Welcome to Laravel SweetAlert By Rashid Ali!', - 'timer' => 3000 - ]); ``` -in view +## Screenshots -```javascript -@if (alert()->ready()) - -@endif +#### Alert + +##### Success Alert + +``` php +alert()->success('SuccessAlert','Lorem ipsum dolor sit amet.'); ```

- + SuccessAlert

-Custom HTML description and buttons! +##### Info Alert -in controller - -```php -alert()->flash('HTML example', 'info',[ - 'html' => "You can use bold text, \ - links \ - and other HTML tags", - 'showCloseButton' => true - ]); +``` php +alert()->info('InfoAlert','Lorem ipsum dolor sit amet.'); ``` -in view +

+ InfoAlert +

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

- + WarningAlert

-A warning message, with a function attached to the "Confirm"-buttons! +##### Question Alert -in controller - -```php -alert()->flash('Are you sure?', 'warning',[ - 'text' => 'You won\'t be able to revert this!', - 'showCancelButton' => true, - 'confirmButtonColor' => '#3085d6', - 'cancelButtonColor' => '#d33', - 'confirmButtonText' => 'Yes, delete it!', - // if user clicked Yes, delete it! - // then this will run - 'deleted' => 'Deleted!', - 'msg' => 'Your file has been deleted.', - 'type' => 'success' - ]); +``` php +alert()->question('QuestionAlert','Lorem ipsum dolor sit amet.'); ``` -in view +

+ QuestionAlert +

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

- + ErrorAlert

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

- + HtmlAlert

+#### Toast -A message with a custom image and CSS animation disabled! +##### Success Toast -in controller +``` php +toast('Success Toast','success','top-right'); +``` -```php -alert()->flash('Sweet!', 'success',[ - 'text' => 'Modal with a custom image.', - 'imageUrl' => 'https://unsplash.it/400/200', - 'imageWidth' => 400, - 'imageHeight' => 200, - 'animation' => false - ]); +

+ SuccessToast +

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

+ InfoToast +

-```javascript -@if (alert()->ready()) - -@endif +##### Warning Toast + +``` php +toast('Warning Toast','warning','top-right'); ```

- + WarningToast

-> The above examples uses SweetAlert, but the flexibily of alert means you can easily use it with any JavaScript alert solution. +##### Question Toast -## Modal Types +``` php +toast('Question Toast','question','top-right'); +``` -`success` | `error` | `warning` | `info` | `question` ----------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- -![](https://github.com/realrashid/sweet-alert/blob/master/imgs/types/success.png) | ![](https://github.com/realrashid/sweet-alert/blob/master/imgs/types/error.png) | ![](https://github.com/realrashid/sweet-alert/blob/master/imgs/types/warning.png) | ![](https://github.com/realrashid/sweet-alert/blob/master/imgs/types/info.png) | ![](https://github.com/realrashid/sweet-alert/blob/master/imgs/types/question.png) +

+ QuestionToast +

-# Issues and Contribution +##### Error Toast -Just submit an issue or pull request through GitHub. Thanks! +``` php +toast('Error Toast','error','top-right'); +``` -## Connect with Me +

+ ErrorToast +

-- Website: http://realrashid.com -- Email: realrashid05@gmail.com -- Twitter: http://twitter.com/rashidali05 -- Facebook: https://www.facebook.com/rashidali05 -- GitHub: https://github.com/realrashid +## 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) -# License +## Connect with Me -SweetAlert is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) +* Website: http://realrashid.com +* Email: realrashid05@gmail.com +* Twitter: http://twitter.com/rashidali05 +* Facebook: https://www.facebook.com/rashidali05 +* GitHub: https://github.com/realrashid -

Made :heart: with Pakistan

+## 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/src/RealRashid/SweetAlert/Facades/Alert.php b/src/Facades/Alert.php similarity index 100% rename from src/RealRashid/SweetAlert/Facades/Alert.php rename to src/Facades/Alert.php diff --git a/src/RealRashid/SweetAlert/Storage/Session.php b/src/RealRashid/SweetAlert/Storage/Session.php deleted file mode 100644 index 6ca8d7a..0000000 --- a/src/RealRashid/SweetAlert/Storage/Session.php +++ /dev/null @@ -1,61 +0,0 @@ -session = $session; - } - - /** - * Set a session key and value. - * - * @param mixed $key - * @param string $data - * - * @return mixed - */ - public function flash($key, $data = null) - { - if (is_array($key)) { - return $this->flashMany($key); - } - - return $this->session->flash($key, $data); - } - - /** - * Flash multiple key/value pairs. - * - * @param array $data - * @return void - */ - public function flashMany(array $data) - { - foreach ($data as $key => $value) { - $this->flash($key, $value); - } - } - - /** - * Get a value from session storage. - * - * @param string $key - * @return string - */ - public function get($key) - { - return $this->session->get($key); - } -} diff --git a/src/RealRashid/SweetAlert/SweetAlertServiceProvider.php b/src/RealRashid/SweetAlert/SweetAlertServiceProvider.php deleted file mode 100644 index c552d13..0000000 --- a/src/RealRashid/SweetAlert/SweetAlertServiceProvider.php +++ /dev/null @@ -1,20 +0,0 @@ -app->singleton('alert', function ($app) { - return $this->app->make('RealRashid\SweetAlert\Toaster'); - }); - } -} diff --git a/src/RealRashid/SweetAlert/Toaster.php b/src/RealRashid/SweetAlert/Toaster.php deleted file mode 100644 index 6687f03..0000000 --- a/src/RealRashid/SweetAlert/Toaster.php +++ /dev/null @@ -1,105 +0,0 @@ -session = $session; - } - - /** - * Flash a message. - * - * @param string $message - * @param string $type - * @param array $options - * - * @return void - */ - public function flash($message, $type = null, array $options = []) - { - $this->session->flash([ - 'alert.message' => $message, - 'alert.type' => $type, - 'alert.options' => json_encode($options), - ]); - } - - /** - * Get the message - * - * @param boolean $array - * @return array - */ - public function get($array = false) - { - return [ - 'message' => $this->message(), - 'type' => $this->type(), - 'options' => $this->options($array), - ]; - } - - /** - * If a message is ready to be shown. - * - * @return bool - */ - public function ready() - { - return $this->message(); - } - - /** - * Get the stored message. - * - * @return string - */ - public function message() - { - return $this->session->get('alert.message'); - } - - /** - * Get the stored type. - * - * @return string - */ - public function type() - { - return $this->session->get('alert.type'); - } - - /** - * Get an additional stored options. - * - * @param boolean $array - * @return mixed - */ - public function options($array = false) - { - return json_decode($this->session->get('alert.options'), $array); - } - - /** - * Get a stored option. - * - * @param string $key - * @return string - */ - public function option($key, $default = null) - { - return array_get($this->options(true), $key, $default); - } -} diff --git a/src/RealRashid/SweetAlert/functions.php b/src/RealRashid/SweetAlert/functions.php deleted file mode 100644 index 0bac56b..0000000 --- a/src/RealRashid/SweetAlert/functions.php +++ /dev/null @@ -1,12 +0,0 @@ -session = $session; + } + + /** + * Set a session key and value. + * + * @param mixed $key + * @param string $data + * + * @return mixed + */ + public function flash($key, $data) + { + $this->session->flash($key, $data); + } + + // /** + // * Flash multiple key/value pairs. + // * + // * @param array $data + // * @return void + // */ + // public function flashMany(array $data) + // { + // foreach ($data as $key => $value) { + // $this->flash($key, $value); + // } + // } + // + // /** + // * Get a value from session storage. + // * + // * @param string $key + // * @return string + // */ + // public function get($key) + // { + // return $this->session->get($key); + // } +} diff --git a/src/Storage/SessionStore.php b/src/Storage/SessionStore.php new file mode 100644 index 0000000..cad496a --- /dev/null +++ b/src/Storage/SessionStore.php @@ -0,0 +1,14 @@ +registerHelpers(); + + $this->loadViewsFrom(__DIR__ . '/Views', 'sweetalert'); + + $this->publishes( + [__DIR__ . '/Views' => resource_path('views/vendor/sweetalert'), ] + ); + } + + /** + * Register bindings in the container. + * + * @return void + */ + public function register() + { + $this->app->bind( + 'RealRashid\SweetAlert\Storage\SessionStore', + 'RealRashid\SweetAlert\Storage\AlertSessionStore' + ); + $this->app->singleton('alert', function ($app) { + return $this->app->make('RealRashid\SweetAlert\Toaster'); + }); + } + + /** + * Register helpers file + */ + public function registerHelpers() + { + // Load the helpers in src/functions.php + if (file_exists($file = __DIR__ . '/functions.php')) { + require $file; + } + } +} diff --git a/src/Toaster.php b/src/Toaster.php new file mode 100644 index 0000000..504e026 --- /dev/null +++ b/src/Toaster.php @@ -0,0 +1,373 @@ +setDefaultConfig(); + + $this->session = $session; + } + + /** + * Sets all default config options for an alert. + * + * @return void + */ + protected function setDefaultConfig() + { + $this->config = [ + 'timer' => env('SWEET_ALERT_AUTOCLOSE', 3000), + 'title' => '', + 'text' => '', + 'showConfirmButton' => false, + ]; + } + + /** + * Flash a message. + * + * @param string $title + * @param string $text + * @param array $type + * + * @return void + */ + public function alert($title = '', $text = '', $type = null) + { + $this->config['title'] = $title; + + $this->config['text'] = $text; + + if (!is_null($type)) { + $this->config['type'] = $type; + } + + $this->flash(); + + return $this; + } + + /* + ** + * Display a success typed alert message with a text and a title. + * + * @param string $title + * @param string $text + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function success($title = '', $text) + { + $this->alert($title, $text, 'success'); + + return $this; + } + + /* + ** + * Display a info typed alert message with a text and a title. + * + * @param string $title + * @param string $text + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function info($title = '', $text) + { + $this->alert($title, $text, 'info'); + + return $this; + } + + /* + ** + * Display a warning typed alert message with a text and a title. + * + * @param string $title + * @param string $text + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function warning($title = '', $text) + { + $this->alert($title, $text, 'warning'); + + return $this; + } + + /* + ** + * Display a question typed alert message with a text and a title. + * + * @param string $title + * @param string $text + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function question($title = '', $text) + { + $this->alert($title, $text, 'question'); + + return $this; + } + + /* + ** + * Display a error typed alert message with a text and a title. + * + * @param string $title + * @param string $text + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function error($title = '', $text) + { + $this->alert($title, $text, 'error'); + + return $this; + } + + /* + ** + * Display a html typed alert message with html code. + * + * @param string $title + * @param string $code + * @param string $type + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function html($title, $code, $type) + { + $this->config['title'] = $title; + + $this->config['html'] = $code; + + if (!is_null($type)) { + $this->config['type'] = $type; + } + + $this->flash(); + + return $this; + } + + /* + ** + * Display a toast alert message with any typed. + * + * @param string $title + * @param string $type + * @param string $position + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function toast($title, $type, $position = 'top-right') + { + $this->config['toast'] = true; + $this->config['title'] = $title; + $this->config['showCloseButton'] = true; + $this->config['type'] = $type; + $this->config['position'] = $position; + + $this->flash(); + + return $this; + } + + /* + ** + * Convert any alert method to Toast + * + * @param string $position + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function toToast($position = 'top-right') + { + $this->config['toast'] = true; + $this->config['showCloseButton'] = false; + $this->config['position'] = $position; + + $this->flash(); + + return $this; + } + + /* + ** + * Add footer section to alert() + * + * @param string $code + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function footer($code) + { + $this->config['footer'] = $code; + + $this->flash(); + + return $this; + } + + /* + ** + * make any alert persistent + * + * @param bool $showConfirmBtn + * @param bool $showCloseBtn + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function persistent($showConfirmBtn = true, $showCloseBtn = false) + { + $this->config['allowEscapeKey'] = false; + $this->config['allowOutsideClick'] = false; + $this->removeTimer(); + + if ($showConfirmBtn) { + $this->showConfirmButton(); + } + + if ($showCloseBtn) { + $this->showCloseButton(); + } + + $this->flash(); + + return $this; + } + + /* + ** + * can any alert after param $milliseconds + * + * @param bool $milliseconds + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function autoClose($milliseconds = 5000) + { + $this->config['timer'] = $milliseconds; + + $this->flash(); + + return $this; + } + + /* + ** + * Display confirm button on alert + * + * @param string $btnText + * @param string $btnColor + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function showConfirmButton($btnText = 'Ok', $btnColor = '#3085d6') + { + $this->config['showConfirmButton'] = true; + $this->config['confirmButtonText'] = $btnText; + $this->config['confirmButtonColor'] = $btnColor; + $this->config['allowOutsideClick'] = false; + $this->removeTimer(); + $this->flash(); + + return $this; + } + + /* + ** + * Display cancel button on alert + * + * @param string $btnText + * @param string $btnColor + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function showCancelButton($btnText = 'Cancel', $btnColor = '#aaa') + { + $this->config['showCancelButton'] = true; + $this->config['cancelButtonText'] = $btnText; + $this->config['cancelButtonColor'] = $btnColor; + $this->removeTimer(); + $this->flash(); + + return $this; + } + + /* + ** + * Display close button on alert + * + * @param string $closeButtonAriaLabel + * + * @return RealRashid\SweetAlert\Toaster::alert(); + */ + public function showCloseButton($closeButtonAriaLabel = 'aria-label') + { + $this->config['showCloseButton'] = true; + $this->config['closeButtonAriaLabel'] = $closeButtonAriaLabel; + + $this->flash(); + + return $this; + } + + /** + * Remove the timer from config option. + * + * @return void + */ + protected function removeTimer() + { + if (array_key_exists('timer', $this->config)) { + unset($this->config['timer']); + } + } + + /** + * Flash the config options for alert. + * + * @return void + */ + public function flash() + { + foreach ($this->config as $key => $value) { + $this->session->flash("alert.config.{$key}", $value); + } + $this->session->flash('alert.config', $this->buildConfig()); + } + + /** + * Build Flash config options for flashing. + * + * @return void + */ + public function buildConfig() + { + $config = $this->config; + + return json_encode($config); + } +} diff --git a/src/Views/alert.blade.php b/src/Views/alert.blade.php new file mode 100644 index 0000000..9fb176b --- /dev/null +++ b/src/Views/alert.blade.php @@ -0,0 +1,5 @@ +@if (Session::has('alert.config')) + +@endif diff --git a/src/functions.php b/src/functions.php new file mode 100644 index 0000000..39cbdc7 --- /dev/null +++ b/src/functions.php @@ -0,0 +1,37 @@ +alert($title, $message, $type); + } + + return $alert; + } +} + +if (!function_exists('toast')) { + /** + * Return app instance of Toast. + * + * @return RealRashid\SweetAlert\Toaster + */ + function toast($title = '', $type = null, $position = 'bottom-right') + { + $alert = app('alert'); + + if (!is_null($title)) { + return $alert->toast($title, $type, $position); + } + + return $alert; + } +}