From 7f537d684e88f60f5d2b6423f18c9dd19269c1ec Mon Sep 17 00:00:00 2001 From: Boudewijn Vahrmeijer Date: Thu, 1 Dec 2016 14:27:20 +0100 Subject: [PATCH 01/63] Custom ID generation for Pjax widgets to solve unstable ajax calls (issue #12969) Implements the solution proposed in the following comment: https://github.com/yiisoft/yii2/pull/12977#issuecomment-260123078 --- framework/widgets/Pjax.php | 23 +++++++++++++++++- tests/framework/widgets/PjaxTest.php | 35 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/framework/widgets/PjaxTest.php diff --git a/framework/widgets/Pjax.php b/framework/widgets/Pjax.php index fb4002efa27..98919e0e3ee 100644 --- a/framework/widgets/Pjax.php +++ b/framework/widgets/Pjax.php @@ -97,7 +97,11 @@ class Pjax extends Widget * [pjax project page](https://github.com/yiisoft/jquery-pjax) for available options. */ public $clientOptions; - + /** + * @var string the prefix to the automatically generated pjax widget IDs. + * @see getId() + */ + public static $pjaxAutoIdPrefix = 'w_pjax_'; /** * @inheritdoc @@ -132,6 +136,23 @@ public function init() } } + /** + * Custom ID generation for Pjax widgets + * This override is to attain a more reliable match between ID's of local and remote Pjax widgets (#12969) + * The implementation follows the solution proposed in the following comment: + * @link https://github.com/yiisoft/yii2/pull/12977#issuecomment-260123078 + * @since 2.0.11 + */ + private static $pjaxCounter = 0; + private $_pjaxId; + public function getId($autoGenerate = true) + { + if ($autoGenerate && $this->_pjaxId === null) { + $this->_pjaxId = static::$pjaxAutoIdPrefix . static::$pjaxCounter++; + } + return $this->_pjaxId; + } + /** * @inheritdoc */ diff --git a/tests/framework/widgets/PjaxTest.php b/tests/framework/widgets/PjaxTest.php new file mode 100644 index 00000000000..182b6e8b77c --- /dev/null +++ b/tests/framework/widgets/PjaxTest.php @@ -0,0 +1,35 @@ + new ArrayDataProvider()]); + ob_start(); + $pjax1 = new Pjax(); + ob_end_clean(); + $nonPjaxWidget2 = new ListView(['dataProvider' => new ArrayDataProvider()]); + ob_start(); + $pjax2 = new Pjax(); + ob_end_clean(); + + $this->assertEquals('w0', $nonPjaxWidget1->options['id']); + $this->assertEquals('w1', $nonPjaxWidget2->options['id']); + $this->assertEquals('w_pjax_0', $pjax1->options['id']); + $this->assertEquals('w_pjax_1', $pjax2->options['id']); + } + + protected function setUp() + { + parent::setUp(); + $this->mockWebApplication(); + } + +} From 9218adcec21fe2d42c1c6ddbd07ca609928041fa Mon Sep 17 00:00:00 2001 From: Boudewijn Vahrmeijer Date: Thu, 1 Dec 2016 15:34:03 +0100 Subject: [PATCH 02/63] fixes after @samdark review --- framework/widgets/Pjax.php | 28 ++++++++-------------------- tests/framework/widgets/PjaxTest.php | 4 ++-- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/framework/widgets/Pjax.php b/framework/widgets/Pjax.php index 98919e0e3ee..316d0e6ebd0 100644 --- a/framework/widgets/Pjax.php +++ b/framework/widgets/Pjax.php @@ -98,10 +98,15 @@ class Pjax extends Widget */ public $clientOptions; /** - * @var string the prefix to the automatically generated pjax widget IDs. - * @see getId() + * @inheritdoc + * Note: This variable must be redeclared to force separate counting for Pjax widgets (issue #12969) + */ + public static $counter = 0; + /** + * @inheritdoc */ - public static $pjaxAutoIdPrefix = 'w_pjax_'; + public static $autoIdPrefix = 'p'; + /** * @inheritdoc @@ -136,23 +141,6 @@ public function init() } } - /** - * Custom ID generation for Pjax widgets - * This override is to attain a more reliable match between ID's of local and remote Pjax widgets (#12969) - * The implementation follows the solution proposed in the following comment: - * @link https://github.com/yiisoft/yii2/pull/12977#issuecomment-260123078 - * @since 2.0.11 - */ - private static $pjaxCounter = 0; - private $_pjaxId; - public function getId($autoGenerate = true) - { - if ($autoGenerate && $this->_pjaxId === null) { - $this->_pjaxId = static::$pjaxAutoIdPrefix . static::$pjaxCounter++; - } - return $this->_pjaxId; - } - /** * @inheritdoc */ diff --git a/tests/framework/widgets/PjaxTest.php b/tests/framework/widgets/PjaxTest.php index 182b6e8b77c..60ccea5087d 100644 --- a/tests/framework/widgets/PjaxTest.php +++ b/tests/framework/widgets/PjaxTest.php @@ -22,8 +22,8 @@ public function testGeneratedIdByPjaxWidget() $this->assertEquals('w0', $nonPjaxWidget1->options['id']); $this->assertEquals('w1', $nonPjaxWidget2->options['id']); - $this->assertEquals('w_pjax_0', $pjax1->options['id']); - $this->assertEquals('w_pjax_1', $pjax2->options['id']); + $this->assertEquals('p0', $pjax1->options['id']); + $this->assertEquals('p1', $pjax2->options['id']); } protected function setUp() From 781ac2e139ade2397ec2d8141b7fc5b80b3b22b0 Mon Sep 17 00:00:00 2001 From: Boudewijn Vahrmeijer Date: Thu, 1 Dec 2016 16:00:06 +0100 Subject: [PATCH 03/63] add changelog --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 5f8206d1be2..d1cba34dd11 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -54,6 +54,7 @@ Yii Framework 2 Change Log - Enh #13074: Improved `\yii\log\SyslogTarget` with `$options` to be able to change the default `openlog` options. (timbeks) - Enh #13050: Added `yii\filters\HostControl` allowing protection against 'host header' attacks (klimov-paul) - Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe) +- Bug: #12969: Improved unique ID generation for `yii\widgets\Pjax` widgets (dynasource,samdark,rob006) 2.0.10 October 20, 2016 ----------------------- From 5e12a5cc3a354ea232bd9538b9c2d824b108a036 Mon Sep 17 00:00:00 2001 From: Boudewijn Vahrmeijer Date: Thu, 1 Dec 2016 16:00:31 +0100 Subject: [PATCH 04/63] added spaces --- framework/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index d1cba34dd11..c38e612eb4d 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -54,7 +54,7 @@ Yii Framework 2 Change Log - Enh #13074: Improved `\yii\log\SyslogTarget` with `$options` to be able to change the default `openlog` options. (timbeks) - Enh #13050: Added `yii\filters\HostControl` allowing protection against 'host header' attacks (klimov-paul) - Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe) -- Bug: #12969: Improved unique ID generation for `yii\widgets\Pjax` widgets (dynasource,samdark,rob006) +- Bug: #12969: Improved unique ID generation for `yii\widgets\Pjax` widgets (dynasource, samdark, rob006) 2.0.10 October 20, 2016 ----------------------- From d295812695b15f1d6c47370f3a9fd725ac29c4cc Mon Sep 17 00:00:00 2001 From: Boudewijn Vahrmeijer Date: Thu, 1 Dec 2016 17:24:41 +0100 Subject: [PATCH 05/63] upgrade file updated --- framework/UPGRADE.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/framework/UPGRADE.md b/framework/UPGRADE.md index d91e00d9d1f..f111c83d612 100644 --- a/framework/UPGRADE.md +++ b/framework/UPGRADE.md @@ -51,6 +51,13 @@ version B between A and C, you need to follow the instructions for both A and B. +Upgrade from Yii 2.0.10 +---------------------- + +* PJAX: The value of the `$autoIdPrefix` property of `yii\widgets\Pjax` has been changed to `p`. If you have any PHP or Javascript +code dependant on this property (like i.e. tests or events), you should update these to match this new value. + + Upgrade from Yii 2.0.9 ---------------------- From 7b3f4095c2184a8b0d9d24fb0c143dec59bd8bdf Mon Sep 17 00:00:00 2001 From: Boudewijn Vahrmeijer Date: Thu, 1 Dec 2016 17:57:11 +0100 Subject: [PATCH 06/63] added @since --- framework/widgets/Pjax.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/widgets/Pjax.php b/framework/widgets/Pjax.php index 316d0e6ebd0..bc702b3bccd 100644 --- a/framework/widgets/Pjax.php +++ b/framework/widgets/Pjax.php @@ -99,11 +99,13 @@ class Pjax extends Widget public $clientOptions; /** * @inheritdoc - * Note: This variable must be redeclared to force separate counting for Pjax widgets (issue #12969) + * Note: This variable is redeclared to force separate counting for Pjax widgets (issue #12969) + * @since 2.0.11 */ public static $counter = 0; /** * @inheritdoc + * @since 2.0.11 */ public static $autoIdPrefix = 'p'; From 5784dd1dbf87dd01a68345996c5acc6b9679a887 Mon Sep 17 00:00:00 2001 From: Boudewijn Vahrmeijer Date: Thu, 1 Dec 2016 22:48:40 +0100 Subject: [PATCH 07/63] fix travis --- framework/widgets/Pjax.php | 6 +++--- tests/framework/widgets/PjaxTest.php | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/framework/widgets/Pjax.php b/framework/widgets/Pjax.php index bc702b3bccd..10b6f1fa770 100644 --- a/framework/widgets/Pjax.php +++ b/framework/widgets/Pjax.php @@ -99,13 +99,13 @@ class Pjax extends Widget public $clientOptions; /** * @inheritdoc - * Note: This variable is redeclared to force separate counting for Pjax widgets (issue #12969) - * @since 2.0.11 + * @internal + * @since 2.0.11 (#12969) */ public static $counter = 0; /** * @inheritdoc - * @since 2.0.11 + * @since 2.0.11 (#12969) */ public static $autoIdPrefix = 'p'; diff --git a/tests/framework/widgets/PjaxTest.php b/tests/framework/widgets/PjaxTest.php index 60ccea5087d..2018e9df965 100644 --- a/tests/framework/widgets/PjaxTest.php +++ b/tests/framework/widgets/PjaxTest.php @@ -11,6 +11,8 @@ class PjaxTest extends TestCase { public function testGeneratedIdByPjaxWidget() { + ListView::$counter = 0; + Pjax::$counter = 0; $nonPjaxWidget1 = new ListView(['dataProvider' => new ArrayDataProvider()]); ob_start(); $pjax1 = new Pjax(); From fec8ae2e1eddccb5f468e830a35cf23eac5bbaf0 Mon Sep 17 00:00:00 2001 From: Boudewijn Vahrmeijer Date: Fri, 2 Dec 2016 09:05:10 +0100 Subject: [PATCH 08/63] removed @since --- framework/widgets/Pjax.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/framework/widgets/Pjax.php b/framework/widgets/Pjax.php index 10b6f1fa770..ee109c5a884 100644 --- a/framework/widgets/Pjax.php +++ b/framework/widgets/Pjax.php @@ -100,12 +100,10 @@ class Pjax extends Widget /** * @inheritdoc * @internal - * @since 2.0.11 (#12969) */ public static $counter = 0; /** * @inheritdoc - * @since 2.0.11 (#12969) */ public static $autoIdPrefix = 'p'; From b6e250857c68626bfe52a711b894ccf5ec51aec4 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Mon, 12 Dec 2016 23:59:31 +0100 Subject: [PATCH 09/63] Edited and extended Guide section about client scripts This section had the "under development note" for a long time and was lacking a lot of information. It should also contain info about `yii.js` but that is to be added in another PR. --- docs/guide/output-client-scripts.md | 190 ++++++++++++++++++++++------ docs/guide/runtime-responses.md | 2 +- docs/guide/structure-assets.md | 1 + 3 files changed, 154 insertions(+), 39 deletions(-) diff --git a/docs/guide/output-client-scripts.md b/docs/guide/output-client-scripts.md index 35abe574f66..f3f3feacbac 100644 --- a/docs/guide/output-client-scripts.md +++ b/docs/guide/output-client-scripts.md @@ -1,70 +1,83 @@ Working with Client Scripts =========================== -> Note: This section is under development. +Modern web applications usually not only contain static HTML pages that are +rendered and sent to the browser but also contain Javascript that is used +to modify the page in the browser by manipulating existing elements and also +loading new content via AJAX. +This section describes the methods provided by Yii for adding JavaScript and CSS to a website as well as dynamically adjusting these. -### Registering scripts +## Registering scripts -With the [[yii\web\View]] object you can register scripts. There are two dedicated methods for it: +When working with the [[yii\web\View]] object you can dynamically register frontend scripts. +There are two dedicated methods for this: [[yii\web\View::registerJs()|registerJs()]] for inline scripts and [[yii\web\View::registerJsFile()|registerJsFile()]] for external scripts. -Inline scripts are useful for configuration and dynamically generated code. + +### Registering inline scripts + +Inline scripts are useful for configuration and dynamically generated code and also for small snippets created by reusable frontend code contained in [widgets](structure-widgets.md). The method for adding these can be used as follows: ```php -$this->registerJs("var options = ".json_encode($options).";", View::POS_END, 'my-options'); +$this->registerJs( + "$('#myButton').on('click', function() { alert('Button clicked!'); });", + View::POS_READY, + 'my-button-handler' +); ``` -The first argument is the actual JS code we want to insert into the page. The second argument -determines where script should be inserted into the page. Possible values are: +The first argument is the actual JS code we want to insert into the page. +It will be wrapped into a `