-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jap Mul Eindbaas
committed
May 13, 2015
0 parents
commit ae46852
Showing
4 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
SweetAlert Widget for Yii 2 | ||
========= | ||
- SweetAlert widget based on SweetAlert extension http://tristanedwards.me/sweetalert | ||
|
||
Installation | ||
------------ | ||
|
||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). | ||
|
||
Either run | ||
|
||
``` | ||
php composer.phar require --prefer-dist branchonline/yii2-sweetalert "*" | ||
``` | ||
|
||
or add | ||
|
||
```json | ||
"branchonline/yii2-sweetalert": "*" | ||
``` | ||
|
||
to the require section of your composer.json. | ||
|
||
Usage | ||
------------ | ||
Once the extension is installed, simply add widget to your page as follows: | ||
|
||
1) Default usage, render all flash messages stored in session flash via Yii::$app->session->setFlash(). | ||
```php | ||
echo SweetAlert::widget(); | ||
``` | ||
|
||
2) Custom usage example: | ||
```php | ||
echo SweetAlert::widget([ | ||
'useSessionFlash' => false, | ||
'options' => [ | ||
'title' => 'Success message', | ||
'type' => 'Success', | ||
'text' => "You will not be able to recover this imaginary file!", | ||
'confirmButtonText' => "Yes, delete it!", | ||
'cancelButtonText' => "No, cancel plx!" | ||
] | ||
]); | ||
``` | ||
|
||
More info | ||
---------------- | ||
More info about the SweetAlert can be found on [options page](http://tristanedwards.me/sweetalert) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "branchonline/yii2-sweetalert", | ||
"description": "SweetAlert widget for Yii2", | ||
"type": "yii2-extension", | ||
"keywords": ["yii2", "module"], | ||
"license": "Apache-2.0", | ||
"authors": [ | ||
{ | ||
"name": "Jap Mul", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"yiisoft/yii2": "*", | ||
"bower-asset/sweetalert" : "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"branchonline\\sweetalert\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace branchonline\sweetalert; | ||
|
||
use Yii; | ||
use yii\base\Widget; | ||
use yii\helpers\Json; | ||
|
||
/** | ||
* SweetAlert widget renders a message from session flash or custom messages. | ||
*/ | ||
class SweetAlert extends Widget { | ||
|
||
/** | ||
* All the flash messages stored for the session are displayed and removed from the session | ||
* Default true. | ||
* @var bool | ||
*/ | ||
public $useSessionFlash = true; | ||
|
||
/** | ||
* @var bool If set to true, the user can dismiss the modal by clicking outside it. | ||
*/ | ||
public $allowOutsideClick = true; | ||
|
||
/** | ||
* @var int Auto close timer of the modal. Set in ms (milliseconds). default - 1,5 second | ||
*/ | ||
public $timer = 2500; | ||
|
||
/** | ||
* Plugin options | ||
* @var array | ||
*/ | ||
public $options = []; | ||
|
||
/** | ||
* Initializes the widget | ||
*/ | ||
public function init() { | ||
parent::init(); | ||
|
||
if ($this->useSessionFlash) { | ||
$session = Yii::$app->getSession(); | ||
$flashes = $session->getAllFlashes(); | ||
|
||
foreach ($flashes as $type => $data) { | ||
if(is_string($data)) { | ||
$this->options['type'] = $type; | ||
$this->options['text'] = $data; | ||
} elseif(is_array($data)) { | ||
$this->options['type'] = isset($data['type']) ? $data['type'] : ''; | ||
$this->options['title'] = isset($data['title']) ? $data['title'] : ''; | ||
$this->options['text'] = isset($data['text']) ? $data['text'] : ''; | ||
} | ||
|
||
$session->removeFlash($type); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Render alert | ||
* @return string|void | ||
*/ | ||
public function run() { | ||
$this->registerAssets(); | ||
} | ||
|
||
/** | ||
* Register client assets | ||
*/ | ||
protected function registerAssets() { | ||
$view = $this->getView(); | ||
SweetAlertAsset::register($view); | ||
$js = 'sweetAlert(' . $this->getOptions() . ');'; | ||
$view->registerJs($js, $view::POS_END); | ||
} | ||
|
||
/** | ||
* Get plugin options | ||
* @return string | ||
*/ | ||
public function getOptions() { | ||
$this->options['allowOutsideClick'] = $this->allowOutsideClick; | ||
$this->options['timer'] = $this->timer; | ||
return Json::encode($this->options); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace branchonline\sweetalert; | ||
|
||
use yii\web\AssetBundle; | ||
|
||
/** | ||
* SweetAlertAsset | ||
* | ||
*/ | ||
class SweetAlertAsset extends AssetBundle { | ||
|
||
/** | ||
* @var string the directory that contains the source asset files for this asset bundle. | ||
* A source asset file is a file that is part of your source code repository of your Web application. | ||
*/ | ||
public $sourcePath = '@bower/sweetalert/lib'; | ||
|
||
/** | ||
* @var array list of JavaScript files that this bundle contains. Each JavaScript file can be | ||
* specified in one of the following formats: | ||
*/ | ||
public $js = [ | ||
'sweet-alert.min.js', | ||
]; | ||
|
||
/** | ||
* @var array list of CSS files that this bundle contains. Each CSS file can be specified | ||
* in one of the three formats as explained in [[js]]. | ||
*/ | ||
public $css = [ | ||
'sweet-alert.css' | ||
]; | ||
|
||
} |