Skip to content

Commit

Permalink
adding callbacks to intro js
Browse files Browse the repository at this point in the history
  • Loading branch information
Matej-ch committed May 21, 2022
1 parent a96ddcd commit bd58f06
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
![latest_tag](https://badgen.net/github/tag/Matej-ch/yii2-page-guide)
![https://github.com/Matej-ch/yii2-page-guide/wiki](https://badgen.net/badge/icon/wiki?icon=wiki&label)
![wiki](https://badgen.net/badge/icon/wiki?icon=wiki&label)

Page guide extension
====================
Expand Down Expand Up @@ -67,6 +67,16 @@ More options here [intro.js](https://introjs.com/docs/examples/customizing/html-
```php
<?= \matejch\pageGuide\widget\PageAssist::widget(['introOptions' => ['showProgress' => true] ]) ?>

```
### 5. Optional

Widget now also supports intro.js callbacks

Available callbacks are `oncomplete` `onexit` `onbeforeexit` `onchange` `onbeforechange` `onafterchange`

```php
<?= \matejch\pageGuide\widget\PageAssist::widget(['introCallbacks' => ['onchange' => ] ]) ?>

```

Usage
Expand Down
5 changes: 5 additions & 0 deletions src/web/js/assist.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ document.addEventListener('DOMContentLoaded',() => {
intro.setOption("nextLabel",window.guideLabels.nextLabel || 'Next' );
intro.setOption("skipLabel",window.guideLabels.skipLabel || 'Skip' );
intro.setOption("doneLabel",window.guideLabels.doneLabel || 'Done' );

if(window.guideCallbacks) {
console.log(window.guideCallbacks)
}

intro.start();
}

Expand Down
2 changes: 1 addition & 1 deletion src/web/js/assist.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 44 additions & 1 deletion src/widget/PageAssist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\web\View;

class PageAssist extends Widget
Expand All @@ -17,8 +18,27 @@ class PageAssist extends Widget

public $selectors = [];

/**
* Array of additional intro.js options
*
* @var array
*/
public $introOptions = [];

/**
* Array of callbacks usable in intro.js
* Available callbacks
* oncomplete
* onexit
* onbeforeexit
* onchange
* onbeforechange
* onafterchange
*
* @var array
*/
public $introCallbacks = [];

public function init()
{
parent::init();
Expand All @@ -45,7 +65,13 @@ public function run()

$options = Json::encode(ArrayHelper::merge($this->introOptions,['steps' => Json::decode($guide->rules)]));

$view->registerJs("window.guideRules=$options;window.guideLabels=$labels");
$jsString = "window.guideRules=$options;window.guideLabels=$labels;";
if(!empty($this->introCallbacks)) {
$callbacks = $this->filterCallbacks();
$jsString = "window.guideCallbacks=$callbacks";
}

$view->registerJs($jsString);
}

if(!empty($this->selectors)) {
Expand Down Expand Up @@ -79,4 +105,21 @@ public function registerTranslations()
];
}
}

private function filterCallbacks(): string
{
return Json::encode(array_intersect_key($this->introCallbacks,array_flip($this->allowedCallbacks())));
}

private function allowedCallbacks(): array
{
return [
'oncomplete',
'onexit',
'onbeforeexit',
'onchange',
'onbeforechange',
'onafterchange',
];
}
}

0 comments on commit bd58f06

Please sign in to comment.