Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jap Mul Eindbaas committed May 13, 2015
0 parents commit ae46852
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
49 changes: 49 additions & 0 deletions README.md
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)
22 changes: 22 additions & 0 deletions composer.json
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/"
}
}
}
91 changes: 91 additions & 0 deletions src/SweetAlert.php
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);
}

}

35 changes: 35 additions & 0 deletions src/SweetAlertAsset.php
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'
];

}

0 comments on commit ae46852

Please sign in to comment.