From 977d1c9c3c30a9b8060ad891f28c10a182272e5c Mon Sep 17 00:00:00 2001 From: Shifrin Date: Sun, 10 Jan 2016 10:41:42 +0300 Subject: [PATCH] Made Ready for Production --- NotyAsset.php | 20 +++++++++----- NotyWidget.php | 72 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 60 insertions(+), 32 deletions(-) diff --git a/NotyAsset.php b/NotyAsset.php index bb31962..a0ad359 100644 --- a/NotyAsset.php +++ b/NotyAsset.php @@ -1,9 +1,13 @@ css[] = 'demo/font-awesome/css/font-awesome.min.css'; } + if ($this->buttonsCss) { + $this->css[] = 'demo/buttons.css'; + } + parent::registerAssetFiles($view); } diff --git a/NotyWidget.php b/NotyWidget.php index ccf2cfd..3623682 100644 --- a/NotyWidget.php +++ b/NotyWidget.php @@ -1,9 +1,14 @@ 'error', 'success' => 'success', @@ -57,6 +67,10 @@ class NotyWidget extends Widget 'warning' => 'warning', 'alert' => 'alert' ]; + /** + * Icons based on type + * @var array + */ protected $icons = [ 'error' => 'fa fa-times-circle', 'success' => 'fa fa-check-circle', @@ -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)) { @@ -88,10 +100,10 @@ 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"; } } @@ -99,19 +111,27 @@ public function run() } /** - * 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); } /** @@ -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 */