Skip to content
This repository has been archived by the owner on Jan 9, 2022. It is now read-only.

Commit

Permalink
Made Ready for Production
Browse files Browse the repository at this point in the history
  • Loading branch information
Shifrin committed Jan 10, 2016
1 parent 5d2954c commit 977d1c9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
20 changes: 13 additions & 7 deletions NotyAsset.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php
/**
* Created by PhpStorm.
* User: Mohammad
* Date: 01/05/2016
* Time: 3:48 PM
* NotyAsset Class File
*
* This is a helper class which is used to register required widget assets.
*
* @author Mohammad Shifreen
* @link http://www.yiiframework.com/extension/yii2-noty/
* @copyright 2016 Mohammed Shifreen
* @license https://github.com/Shifrin/yii2-noty/blob/master/LICENSE.md
*/

namespace shifrin\noty;
Expand All @@ -16,10 +20,8 @@ class NotyAsset extends AssetBundle

public $sourcePath = '@bower/noty';
public $animateCss;
public $buttonsCss;
public $fontAwesomeCss;
public $css = [
'demo/button.css'
];
public $js = [
'js/noty/packaged/jquery.noty.packaged.min.js'
];
Expand All @@ -42,6 +44,10 @@ public function registerAssetFiles($view)
$this->css[] = 'demo/font-awesome/css/font-awesome.min.css';
}

if ($this->buttonsCss) {
$this->css[] = 'demo/buttons.css';
}

parent::registerAssetFiles($view);
}

Expand Down
72 changes: 47 additions & 25 deletions NotyWidget.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: Mohammad
* Date: 01/05/2016
* Time: 3:55 PM
* NotyWidget Class File
*
* It's a yii2 widget for alert type of messages that can be shown to the end user
* This widget build with noty jQuery plugin v2.3.7. @link http://ned.im/noty/
*
* @author Mohammad Shifreen
* @link http://www.yiiframework.com/extension/yii2-noty/
* @copyright 2016 Mohammed Shifreen
* @license https://github.com/Shifrin/yii2-noty/blob/master/LICENSE.md
*/

namespace shifrin\noty;
Expand All @@ -17,11 +22,6 @@
class NotyWidget extends Widget
{

/**
* Widget ID
* @var string defaults noty
*/
public $id = 'noty';
/**
* Noty plugin JS Options
* @var array
Expand All @@ -43,20 +43,34 @@ class NotyWidget extends Widget
* @var bool defaults true
*/
public $registerAnimateCss = true;
/**
* Register buttons.css
* If bootstrap.css or any related css already registered in your assets you can set it to false,
* otherwise this will override your buttons' styles
* @var bool defaults true
*/
public $registerButtonsCss = true;
/**
* Register font-awesome.css
* If font-awesome.css already registered in your assets you can set it to false
* @var bool defaults true
*/
public $registerFontAwesomeCss = true;

/**
* Alert types
* @var array
*/
protected $types = [
'error' => 'error',
'success' => 'success',
'information' => 'information',
'warning' => 'warning',
'alert' => 'alert'
];
/**
* Icons based on type
* @var array
*/
protected $icons = [
'error' => 'fa fa-times-circle',
'success' => 'fa fa-check-circle',
Expand All @@ -74,12 +88,10 @@ public function run()
$this->registerAssets();
$this->registerPlugin();

$view = $this->getView();
$options = !empty($this->options) ? Json::encode($this->options) : '';
$script = "var Noty = generateNoty({$options})";

if ($this->enableSessionFlash) {
$flashes = Yii::$app->session->getAllFlashes();
$view = $this->getView();
$script = "";

foreach($flashes as $type => $message) {
if (empty($message)) {
Expand All @@ -88,30 +100,38 @@ public function run()

$type = $this->verifyType($type);
$icon = $this->getIcon($type);
$text = $icon . is_array($message) ? implode(' ', $message) : $message;
$script .= "var {$type} = generateNoty({$options})";
$script .= "$.noty.setText({$type}.options.id, '{$text}')";
$script .= "$.noty.setType({$type}.options.id, '{$type}')";
$text = is_array($message) ? $icon . implode(' ', $message) : $icon . $message;
$script .= "var {$type} = Noty({$this->getId()});\r\n";
$script .= "$.noty.setText({$type}.options.id, '{$text}');\r\n";
$script .= "$.noty.setType({$type}.options.id, '{$type}');\r\n";
}
}

$view->registerJs($script);
}

/**
* Register Noty plugin
* Register Noty plugin by creating a wrapper function called 'Noty()'
* This will be available globally for use
*
* ~~~
* js: var n = Noty('id');
* $.noty.setText(n.options.id, 'Hi I am noty alert!');
* $.noty.setType(n.options.id, 'information');
* ~~~
*/
public function registerPlugin()
{
$view = $this->getView();
$options = !empty($this->options) ? Json::encode($this->options) : '';
$js = <<< JS
function generateNoty(options) {
var n = noty(options);
return n;
}
function Noty(widgetId, options) {
var finalOptions = $.extend({}, $options, options);
return noty(finalOptions);
}
JS;

$view->registerJs($js);
$view->registerJs($js, View::POS_END);
}

/**
Expand All @@ -122,11 +142,13 @@ public function registerAssets()
$view = $this->getView();
$asset = NotyAsset::register($view);
$asset->animateCss = $this->registerAnimateCss;
$asset->buttonsCss = $this->registerButtonsCss;
$asset->fontAwesomeCss = $this->enableIcon && $this->registerFontAwesomeCss;
}

/**
* Verify type, if not return defalut type
* Verify type.
* If verify unsuccessful it will return default type called 'notification'
* @param $type string
* @return string
*/
Expand Down

0 comments on commit 977d1c9

Please sign in to comment.