Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No unneeded/nested jQuery.ready() wrap #1919

Merged
merged 3 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demos/_includes/ViewTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function init(): void
$label->detail = 'success';
} else {
$this->js(true, $reload);
$this->js(true, new JsExpression('var s = Date.now(); var i = setInterval(function() { var p = Date.now() - s; var el = $[]; el.find(\'.detail\').text(p + \'ms\'); if (el.is(\'.green\')) { clearInterval(i); }}, 100)', [$label]));
$this->js(true, new JsExpression('var s = Date.now(); var i = setInterval(function () { var p = Date.now() - s; var el = $[]; el.find(\'.detail\').text(p + \'ms\'); if (el.is(\'.green\')) { clearInterval(i); }}, 100)', [$label]));
}
}
}
22 changes: 22 additions & 0 deletions demos/_unit-test/js-error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Atk4\Ui\Demos;

use Atk4\Ui\Button;
use Atk4\Ui\JsExpression;
use Atk4\Ui\Modal;
use Atk4\Ui\View;

/** @var \Atk4\Ui\App $app */
require_once __DIR__ . '/../init-app.php';

$modal = Modal::addTo($app);

$modal->set(function (View $p) {
$p->js(true, new JsExpression('$(\'<div />\').modal({onShow: () => true})'));
});

$button = Button::addTo($app)->set('Test');
$button->on('click', $modal->jsShow());
4 changes: 2 additions & 2 deletions demos/interactive/modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@

$denyApproveModal = Modal::addTo($app, ['title' => 'Deny / Approve actions']);
Message::addTo($denyApproveModal)->set('This modal is only closable via the green button');
$denyApproveModal->addDenyAction('No', new JsExpression('function() { window.alert(\'Cannot do that.\'); return false; }'));
$denyApproveModal->addApproveAction('Yes', new JsExpression('function() { window.alert(\'You are good to go!\'); }'));
$denyApproveModal->addDenyAction('No', new JsExpression('function () { window.alert(\'Cannot do that.\'); return false; }'));
$denyApproveModal->addApproveAction('Yes', new JsExpression('function () { window.alert(\'You are good to go!\'); }'));
$denyApproveModal->notClosable();

$menuBar = View::addTo($app, ['ui' => 'buttons']);
Expand Down
12 changes: 6 additions & 6 deletions demos/javascript/vue-component.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,30 @@

var myClock = {
props : { clock: Array },
data: function() {
data: function () {
return { style : this.clock, currentIdx : 0 }
},
mounted: function() {
mounted: function () {
// add a listener for changing clock style.
// this will listen to event '-clock-change-style' emit on the eventBus.
atk.eventBus.on(this.$root.$el.id + '-clock-change-style', (payload) => {
this.onChangeStyle();
});
},
computed: {
color: function() {
color: function () {
return this.style[this.currentIdx].color
},
border: function() {
border: function () {
return this.style[this.currentIdx].border
},
bg: function() {
bg: function () {
return this.style[this.currentIdx].bg
}
},
name: 'my-clock',
methods: {
onChangeStyle: function() {
onChangeStyle: function () {
this.currentIdx = this.currentIdx + 1;
if (this.currentIdx > this.style.length - 1) {
this.currentIdx = 0;
Expand Down
2 changes: 1 addition & 1 deletion demos/layout/templates/layout1.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ <h3>Nominal Acitivity Report</h3>
</div>
</div>
<script>
'use strict'; $(function() {
'use strict'; $(function () {
$('.my-dropdown').dropdown();
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion demos/layout/templates/layout1.pug
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ html
.item Last Year
.item Last Accounting Year
script.
'use strict'; $(function() {
'use strict'; $(function () {
$('.my-dropdown').dropdown();
});
.field
Expand Down
8 changes: 4 additions & 4 deletions docs/autocomplete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ You can do much more with AutoComplete form control by passing dropdown settings
'settings' => [
'allowReselection' => true,
'selectOnKeydown' => false,
'onChange' => new \Atk4\Ui\JsExpression('function(value, t, c) {
'onChange' => new \Atk4\Ui\JsExpression('function (value, t, c) {
if ($(this).data(\'value\') !== value) {
$(this).parents(\'.form\').form(\'submit\');
$(this).data(\'value\', value);
}}'),
$(this).parents(\'.form\').form(\'submit\');
$(this).data(\'value\', value);
}}'),
],
])->setModel(new Country($db));

Expand Down
12 changes: 6 additions & 6 deletions docs/js.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ The following code will show three buttons and clicking any one will hide it. On
$buttons->on('click', '.button')->hide();

// Generates:
// $('#top-element-id').on('click', '.button', function(event) {
// $('#top-element-id').on('click', '.button', function (event) {
// event.stopPropagation();
// event.preventDefault();
// $(this).hide();
Expand All @@ -265,10 +265,10 @@ You can use both actions together. The next example will allow only one button t
$buttons->on('click', '.button', $b3->js()->hide());

// Generates:
// $('#top-element-id').on('click', '.button', function(event) {
// event.stopPropagation();
// event.preventDefault();
// $('#b3-element-id').hide();
// $('#top-element-id').on('click', '.button', function (event) {
// event.stopPropagation();
// event.preventDefault();
// $('#b3-element-id').hide();
// });


Expand Down Expand Up @@ -359,7 +359,7 @@ Create a file `test.js` containing:
.. code-block:: js

function mySum(arr) {
return arr.reduce(function(a, b) {
return arr.reduce(function (a, b) {
return a + b;
}, 0);
}
Expand Down
6 changes: 3 additions & 3 deletions js/src/setup-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import AtkSidenavPlugin from './plugins/sidenav.plugin';
*
* @param {string} name Plugin name
* @param {Function} cl Plugin class
* @param {boolean} shortHand Map $.name(...) to $({}).name(...)
* @param {boolean} shorthand Map $.name(...) to $({}).name(...)
*/
atk.registerPlugin = function (name, cl, shortHand = false) {
atk.registerPlugin = function (name, cl, shorthand = false) {
const dataName = '__' + name;

// add plugin to atk namespace.
Expand All @@ -41,7 +41,7 @@ atk.registerPlugin = function (name, cl, shortHand = false) {
});
};

if (shortHand) {
if (shorthand) {
$[name] = (options) => $({})[name](options);
}
};
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,6 @@ parameters:
-
path: 'src/View.php'
message: '~^Call to an undefined method Atk4\\Ui\\JsChain::addJsonData\(\)\.$~'
-
path: 'src/View.php'
message: '~^Call to an undefined method Atk4\\Ui\\App::jsReady\(\)\.$~'
-
path: 'src/View.php'
message: '~^Call to an undefined method Atk4\\Ui\\App::getViewJS\(\)\.$~'
Expand Down
6 changes: 3 additions & 3 deletions public/js/atkjs-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -3690,10 +3690,10 @@ __webpack_require__.r(__webpack_exports__);
*
* @param {string} name Plugin name
* @param {Function} cl Plugin class
* @param {boolean} shortHand Map $.name(...) to $({}).name(...)
* @param {boolean} shorthand Map $.name(...) to $({}).name(...)
*/
atk__WEBPACK_IMPORTED_MODULE_1__["default"].registerPlugin = function (name, cl) {
let shortHand = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
let shorthand = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
const dataName = '__' + name;

// add plugin to atk namespace.
Expand All @@ -3714,7 +3714,7 @@ atk__WEBPACK_IMPORTED_MODULE_1__["default"].registerPlugin = function (name, cl)
external_jquery__WEBPACK_IMPORTED_MODULE_0___default()(this).data(dataName, new atk__WEBPACK_IMPORTED_MODULE_1__["default"][name](this, options));
});
};
if (shortHand) {
if (shorthand) {
(external_jquery__WEBPACK_IMPORTED_MODULE_0___default())[name] = options => external_jquery__WEBPACK_IMPORTED_MODULE_0___default()({})[name](options);
}
};
Expand Down
2 changes: 1 addition & 1 deletion public/js/atkjs-ui.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/atkjs-ui.min.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,9 @@ public function terminate($output = '', array $headers = []): void
if (count($ids) > 0) {
$remove_function = '$(\'.ui.dimmer.modals.page, .atk-side-panels\').find(\'' . implode(', ', $ids) . '\').remove();';
}
$output = $this->getTag('script', [], 'jQuery(function() {' . $remove_function . $output['atkjs'] . '});') . $output['html'];

$output = $this->getTag('script', [], '$(function () {' . $remove_function . $output['atkjs'] . '});')
. $output['html'];

$this->outputResponseHtml($output, $headers);
} elseif ($type === 'text/html') {
Expand Down Expand Up @@ -549,7 +551,7 @@ public function run(): void

$this->html->template->set('title', $this->title);
$this->html->renderAll();
$this->html->template->dangerouslyAppendHtml('Head', $this->getTag('script', [], $this->html->getJs()));
$this->html->template->dangerouslyAppendHtml('Head', $this->getTag('script', [], '$(function () {' . $this->html->getJs() . ';});'));
$this->isRendering = false;

if (isset($_GET[Callback::URL_QUERY_TARGET]) && $this->catchRunawayCallbacks) {
Expand Down
6 changes: 5 additions & 1 deletion src/Behat/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,12 @@ public function iPressModalButton(string $buttonLabel): void
*/
public function modalIsOpenWithText(string $text, string $tag = 'div'): void
{
$textEncoded = str_contains($text, '"')
? 'concat("' . str_replace('"', '", \'"\', "', $text) . '")'
: '"' . $text . '"';

$modal = $this->findElement(null, '.modal.visible.active.front');
$this->findElement($modal, 'xpath(//' . $tag . '[text()[normalize-space()="' . $text . '"]])');
$this->findElement($modal, 'xpath(//' . $tag . '[text()[normalize-space()=' . $textEncoded . ']])');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Lookup extends Input
* $form->addControl('field', [Form\Control\Lookup::class, 'settings' => [
* 'allowReselection' => true,
* 'selectOnKeydown' => false,
* 'onChange' => new JsExpression('function(value, t, c) {
* 'onChange' => new JsExpression('function (value, t, c) {
* if ($(this).data("value") !== value) {
* $(this).parents(\'.form\').form(\'submit\');
* $(this).data(\'value\', value);
Expand Down
18 changes: 9 additions & 9 deletions src/ItemsPerPageSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ protected function renderView(): void
$menuItems[] = ['name' => $item, 'value' => $item];
}
// set Fomantic-UI dropdown onChange function.
$function = 'function(value, text, item) {
if (value === undefined || value === \'\' || value === null) return;
$(this)
.api({
on:\'now\',
url:\'' . $this->cb->getUrl() . '\',
data:{ipp:value}
});
}';
$function = 'function (value, text, item) {
if (value === undefined || value === \'\' || value === null) return;
$(this)
.api({
on:\'now\',
url:\'' . $this->cb->getUrl() . '\',
data:{ipp:value}
});
}';

$this->js(true)->dropdown([
'values' => $menuItems,
Expand Down
2 changes: 1 addition & 1 deletion src/JsFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function jsRender(): string
$pre .= "\n" . $this->indent . ' event.stopPropagation();';
}

$output = 'function(' . implode(', ', $this->fxArgs) . ') {'
$output = 'function (' . implode(', ', $this->fxArgs) . ') {'
. $pre;
foreach ($this->fxStatements as $statement) {
if (!$statement) {
Expand Down
2 changes: 1 addition & 1 deletion src/Lister.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function addJsPaginator($ipp, $options = [], $container = null, $scrollRe
$this->model->setLimit($ipp, ($p - 1) * $ipp);

// render this View (it will count rendered records !)
$jsonArr = $this->renderToJsonArr(true, $scrollRegion);
$jsonArr = $this->renderToJsonArr($scrollRegion);

// let client know that there are no more records
$jsonArr['noMoreScrollPages'] = $this->_renderedRowsCount < $ipp;
Expand Down
4 changes: 2 additions & 2 deletions src/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*
* Modal can use Fomantic-UI predefine method onApprove or onDeny by passing
* a jsAction to Modal::addDenyAction or Modal::addApproveAction method. It will not close until the jsAction return true.
* $modal->addDenyAction('No', new JsExpression('function() { window.alert(\'Cannot do that.\'); return false; }'));
* $modal->addApproveAction('Yes', new JsExpression('function() { window.alert(\'You are good to go!\'); }'));
* $modal->addDenyAction('No', new JsExpression('function () { window.alert(\'Cannot do that.\'); return false; }'));
* $modal->addApproveAction('Yes', new JsExpression('function () { window.alert(\'You are good to go!\'); }'));
*
* You may also prevent modal from closing via the esc or dimmed area click using $modal->notClosable().
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function jsPopup()
$chain = new Jquery($name);
$chain->popup($this->popOptions);
if ($this->stopClickEvent) {
$chain->on('click', new JsExpression('function(e) { e.stopPropagation(); }'));
$chain->on('click', new JsExpression('function (e) { e.stopPropagation(); }'));
}

return $chain;
Expand Down
20 changes: 10 additions & 10 deletions src/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ public function setHeaderDropdown($items, string $icon = 'caret square down', st

$cb = Column\JsHeader::addTo($this->table);

$function = 'function(value, text, item) {
if (value === undefined || value === \'\' || value === null) { return; }
$(this)
.api({
on: \'now\',
url: \'' . $cb->getJsUrl() . '\',
data: { item: value, id: $(this).data(\'menu-id\') }
});
}';
$function = 'function (value, text, item) {
if (value === undefined || value === \'\' || value === null) { return; }
$(this)
.api({
on: \'now\',
url: \'' . $cb->getJsUrl() . '\',
data: { item: value, id: $(this).data(\'menu-id\') }
});
}';

$chain = new Jquery('#' . $id);
$chain->dropdown([
Expand All @@ -182,7 +182,7 @@ public function setHeaderDropdown($items, string $icon = 'caret square down', st
]);

// will stop grid column from being sorted.
$chain->on('click', new JsExpression('function(e) { e.stopPropagation(); }'));
$chain->on('click', new JsExpression('function (e) { e.stopPropagation(); }'));

$this->table->js(true, $chain);

Expand Down
2 changes: 1 addition & 1 deletion src/Table/Column/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Checkbox extends Table\Column
*/
public function jsChecked()
{
return new JsExpression('$(' . $this->table->jsRender() . ').find(\'.checked.' . $this->class . '\').closest(\'tr\').map(function() { '
return new JsExpression('$(' . $this->table->jsRender() . ').find(\'.checked.' . $this->class . '\').closest(\'tr\').map(function () { '
. 'return $(this).data(\'id\'); }).get().join(\',\')');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Text extends View
{
public $defaultTemplate = false;

public function render(bool $forceReturn = true): string
public function render(): string
{
return $this->content;
}
Expand Down
Loading