Skip to content

Commit 07dc133

Browse files
committed
use native js by default
1 parent 2258093 commit 07dc133

File tree

10 files changed

+120
-96
lines changed

10 files changed

+120
-96
lines changed

src/Contracts/ApplicationWrapperContract.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ public function call($method, $params = []);
3737
*/
3838
public function config($key, $default = null);
3939

40-
/**
41-
* Wrapper around csrf_token().
42-
*
43-
* @return string
44-
*/
45-
public function csrf_token();
46-
4740
/**
4841
* Wrapper around app()->getNamespace().
4942
*

src/Factories/JavascriptFactory.php

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function getContainerId()
7777
*/
7878
protected function useJquery()
7979
{
80-
return !$this->widgetFactory->app->config('laravel-widgets.disable_jquery', false);
80+
return $this->widgetFactory->app->config('laravel-widgets.use_jquery_for_ajax_calls', false);
8181
}
8282

8383
/**
@@ -87,35 +87,34 @@ protected function useJquery()
8787
*/
8888
protected function constructAjaxCall()
8989
{
90-
$data = [
90+
$queryParams = [
9191
'id' => WidgetId::get(),
9292
'name' => $this->widgetFactory->widgetName,
9393
'params' => serialize($this->widgetFactory->widgetFullParams),
94-
'_token' => $this->widgetFactory->app->csrf_token(),
9594
];
9695

96+
$url = $this->ajaxLink."?".http_build_query($queryParams);
97+
9798
return $this->useJquery()
98-
? $this->constructJqueryAjaxCall($data)
99-
: $this->constructNativeJsAjaxCall($data);
99+
? $this->constructJqueryAjaxCall($url)
100+
: $this->constructNativeJsAjaxCall($url);
100101
}
101102

102103
/**
103104
* Construct ajax call with jquery.
104105
*
105-
* @param array $data
106+
* @param string $url
106107
*
107108
* @return string
108109
*/
109-
protected function constructJqueryAjaxCall(array $data)
110+
protected function constructJqueryAjaxCall($url)
110111
{
111112
$id = WidgetId::get();
112113

113-
$jsData = json_encode($data);
114-
115114
return
116115
"var widgetTimer{$id} = setInterval(function() {".
117116
'if (window.$) {'.
118-
"$('#{$this->getContainerId()}').load('".$this->ajaxLink."', {$jsData});".
117+
"$('#{$this->getContainerId()}').load('{$url}');".
119118
"clearInterval(widgetTimer{$id});".
120119
'}'.
121120
'}, 100);';
@@ -124,37 +123,29 @@ protected function constructJqueryAjaxCall(array $data)
124123
/**
125124
* Construct ajax call without jquery.
126125
*
127-
* @param array $data
126+
* @param string $url
128127
*
129128
* @return string
130129
*/
131-
protected function constructNativeJsAjaxCall(array $data)
130+
protected function constructNativeJsAjaxCall($url)
132131
{
133-
$jsData = "'id=' + encodeURIComponent('{$data['id']}')".
134-
"'&name=' + encodeURIComponent('{$data['name']}')".
135-
"'&params=' + encodeURIComponent('{$data['params']}')".
136-
"'&_token=' + encodeURIComponent('{$data['token']})";
137-
138132
return
139133
'var xhr = new XMLHttpRequest();'.
140-
'var params = '.$jsData.';'.
141-
'var container = document.getElementById("'.$this->getContainerId().'");'.
142-
'xhr.open("POST", "'.$this->ajaxLink.'", true);'.
143-
'xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");'.
144-
'xhr.send(params);'.
134+
'xhr.open("GET", "'.$url.'", true);'.
145135
'xhr.onreadystatechange = function() {'.
146136
'if(xhr.readyState == 4 && xhr.status == 200) {'.
147-
'var data = xhr.responseText;'.
148-
'container.innerHTML = data;'.
149-
'var scripts = container.getElementsByTagName("script");'.
150-
'for(var i=0; i < scripts.length; i++) {'.
151-
'if (window.execScript) {'.
137+
'var container = document.getElementById("'.$this->getContainerId().'");'.
138+
'container.innerHTML = xhr.responseText;'.
139+
'var scripts = container.getElementsByTagName("script");'.
140+
'for(var i=0; i < scripts.length; i++) {'.
141+
'if (window.execScript) {'.
152142
'window.execScript(scripts[i].text);'.
153143
'} else {'.
154144
'window["eval"].call(window, scripts[i].text);'.
155145
'}'.
156146
'}'.
157147
'}'.
158-
'}';
148+
'};'.
149+
'xhr.send();';
159150
}
160151
}

src/Misc/LaravelApplicationWrapper.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,6 @@ public function config($key, $default = null)
6464
return $this->app->make('config')->get($key, $default);
6565
}
6666

67-
/**
68-
* Wrapper around csrf_token().
69-
*
70-
* @return string
71-
*/
72-
public function csrf_token()
73-
{
74-
return csrf_token();
75-
}
76-
7767
/**
7868
* Wrapper around app()->getNamespace().
7969
*

src/ServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function boot()
5656
];
5757

5858
$this->app['router']->group($routeConfig, function ($router) {
59-
$router->post('load-widget', 'WidgetController@showWidget');
59+
$router->get('load-widget', 'WidgetController@showWidget');
6060
});
6161

6262
$this->registerBladeDirective('widget', '$1<?php echo app("arrilot.widget")->run$2; ?>');

src/config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
return [
44
//'default_namespace' => 'App\Widgets',
5-
'disable_jquery' => false,
5+
'use_jquery_for_ajax_calls' => false,
66
];

tests/AsyncWidgetFactoryTest.php

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Arrilot\Widgets\Test;
44

55
use Arrilot\Widgets\Factories\AsyncWidgetFactory;
6+
use Arrilot\Widgets\Test\Support\TestApplicationWrapper;
7+
use Arrilot\Widgets\Test\Support\TestCase;
68

79
class AsyncWidgetFactoryTest extends TestCase
810
{
@@ -20,65 +22,73 @@ public function testItCanRunAsyncWidget()
2022
{
2123
$output = $this->factory->run('testDefaultSlider');
2224

23-
$this->assertEquals(
25+
$expected =
2426
'<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">'.
2527
'<script type="text/javascript">'.
2628
'var widgetTimer1 = setInterval(function() {'.
2729
'if (window.$) {'.
28-
"$('#arrilot-widget-container-1').load('/arrilot/load-widget', ".$this->javascriptDataStub('TestDefaultSlider').');'.
30+
"$('#arrilot-widget-container-1').load('".$this->ajaxUrl('TestDefaultSlider')."');".
2931
'clearInterval(widgetTimer1);'.
3032
'}'.
3133
'}, 100);'.
3234
'</script>'.
33-
'</div>', $output);
35+
'</div>';
36+
37+
$this->assertEquals($expected, $output);
3438
}
3539

3640
public function testItCanRunAsyncWidgetWithPlaceholder()
3741
{
3842
$output = $this->factory->run('slider');
3943

40-
$this->assertEquals(
44+
$expected =
4145
'<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">Placeholder here!'.
4246
'<script type="text/javascript">'.
4347
'var widgetTimer1 = setInterval(function() {'.
4448
'if (window.$) {'.
45-
"$('#arrilot-widget-container-1').load('/arrilot/load-widget', ".$this->javascriptDataStub('Slider').');'.
49+
"$('#arrilot-widget-container-1').load('".$this->ajaxUrl('Slider')."');".
4650
'clearInterval(widgetTimer1);'.
4751
'}'.
4852
'}, 100);'.
4953
'</script>'.
50-
'</div>', $output);
54+
'</div>';
55+
56+
$this->assertEquals($expected, $output);
5157
}
5258

5359
public function testItCanRunMultipleAsyncWidgets()
5460
{
5561
$output = $this->factory->run('slider');
5662

57-
$this->assertEquals(
63+
$expected =
5864
'<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">Placeholder here!'.
5965
'<script type="text/javascript">'.
6066
'var widgetTimer1 = setInterval(function() {'.
6167
'if (window.$) {'.
62-
"$('#arrilot-widget-container-1').load('/arrilot/load-widget', ".$this->javascriptDataStub('Slider').');'.
68+
"$('#arrilot-widget-container-1').load('".$this->ajaxUrl('Slider')."');".
6369
'clearInterval(widgetTimer1);'.
6470
'}'.
6571
'}, 100);'.
6672
'</script>'.
67-
'</div>', $output);
73+
'</div>';
74+
75+
$this->assertEquals($expected, $output);
6876

6977
$output = $this->factory->run('testDefaultSlider');
7078

71-
$this->assertEquals(
79+
$expected =
7280
'<div id="arrilot-widget-container-2" style="display:inline" class="arrilot-widget-container">'.
7381
'<script type="text/javascript">'.
7482
'var widgetTimer2 = setInterval(function() {'.
7583
'if (window.$) {'.
76-
"$('#arrilot-widget-container-2').load('/arrilot/load-widget', ".$this->javascriptDataStub('TestDefaultSlider', [], 2).');'.
84+
"$('#arrilot-widget-container-2').load('".$this->ajaxUrl('TestDefaultSlider', [], 2)."');".
7785
'clearInterval(widgetTimer2);'.
7886
'}'.
7987
'}, 100);'.
8088
'</script>'.
81-
'</div>', $output);
89+
'</div>';
90+
91+
$this->assertEquals($expected, $output);
8292
}
8393

8494
public function testItCanRunAsyncWidgetWithAdditionalParams()
@@ -89,67 +99,108 @@ public function testItCanRunAsyncWidgetWithAdditionalParams()
8999
];
90100

91101
$output = $this->factory->run('testWidgetWithParamsInRun', [], 'parameter');
92-
$this->assertEquals(
102+
103+
$expected =
93104
'<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">Placeholder here!'.
94105
'<script type="text/javascript">'.
95106
'var widgetTimer1 = setInterval(function() {'.
96107
'if (window.$) {'.
97-
"$('#arrilot-widget-container-1').load('/arrilot/load-widget', ".$this->javascriptDataStub('TestWidgetWithParamsInRun', $params).');'.
108+
"$('#arrilot-widget-container-1').load('".$this->ajaxUrl('TestWidgetWithParamsInRun', $params)."');".
98109
'clearInterval(widgetTimer1);'.
99110
'}'.
100111
'}, 100);'.
101112
'</script>'.
102-
'</div>', $output);
113+
'</div>';
114+
115+
$this->assertEquals($expected, $output);
103116
}
104117

105118
public function testItCanRunAsyncWidgetWithMagicMethod()
106119
{
107120
$output = $this->factory->slider();
108121

109-
$this->assertEquals(
122+
$expected =
110123
'<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">Placeholder here!'.
111124
'<script type="text/javascript">'.
112125
'var widgetTimer1 = setInterval(function() {'.
113126
'if (window.$) {'.
114-
"$('#arrilot-widget-container-1').load('/arrilot/load-widget', ".$this->javascriptDataStub('Slider').');'.
127+
"$('#arrilot-widget-container-1').load('".$this->ajaxUrl('Slider')."');".
115128
'clearInterval(widgetTimer1);'.
116129
'}'.
117130
'}, 100);'.
118131
'</script>'.
119-
'</div>', $output);
132+
'</div>';
133+
134+
$this->assertEquals($expected, $output);
120135
}
121136

122137
public function testItCanRunNestedAsyncWidget()
123138
{
124139
$output = $this->factory->run('Profile\TestNamespace\TestFeed');
125140

126-
$this->assertEquals(
141+
$expected =
127142
'<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">'.
128143
'<script type="text/javascript">'.
129144
'var widgetTimer1 = setInterval(function() {'.
130145
'if (window.$) {'.
131-
"$('#arrilot-widget-container-1').load('/arrilot/load-widget', ".$this->javascriptDataStub('Profile\TestNamespace\TestFeed').');'.
146+
"$('#arrilot-widget-container-1').load('".$this->ajaxUrl('Profile\TestNamespace\TestFeed')."');".
132147
'clearInterval(widgetTimer1);'.
133148
'}'.
134149
'}, 100);'.
135150
'</script>'.
136-
'</div>', $output);
151+
'</div>';
152+
153+
$this->assertEquals($expected, $output);
137154
}
138155

139156
public function testItCanRunNestedAsyncWidgetUsingDotNotation()
140157
{
141158
$output = $this->factory->run('profile.testNamespace.testFeed');
142159

143-
$this->assertEquals(
160+
$expected =
144161
'<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">'.
145162
'<script type="text/javascript">'.
146163
'var widgetTimer1 = setInterval(function() {'.
147164
'if (window.$) {'.
148-
"$('#arrilot-widget-container-1').load('/arrilot/load-widget', ".$this->javascriptDataStub('Profile\testNamespace\testFeed').');'.
165+
"$('#arrilot-widget-container-1').load('".$this->ajaxUrl('Profile\testNamespace\testFeed')."');".
149166
'clearInterval(widgetTimer1);'.
150167
'}'.
151168
'}, 100);'.
152169
'</script>'.
153-
'</div>', $output);
170+
'</div>';
171+
172+
$this->assertEquals($expected, $output);
173+
}
174+
175+
public function testItCanRunAsyncWidgetWithoutJquery()
176+
{
177+
178+
$this->factory->app->config['laravel-widgets.use_jquery_for_ajax_calls'] = false;
179+
180+
$output = $this->factory->run('testDefaultSlider');
181+
182+
$expected =
183+
'<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">'.
184+
'<script type="text/javascript">'.
185+
'var xhr = new XMLHttpRequest();'.
186+
'xhr.open("GET", "'.$this->ajaxUrl('TestDefaultSlider').'", true);'.
187+
'xhr.onreadystatechange = function() {'.
188+
'if(xhr.readyState == 4 && xhr.status == 200) {'.
189+
'var container = document.getElementById("arrilot-widget-container-1");'.
190+
'container.innerHTML = xhr.responseText;'.
191+
'var scripts = container.getElementsByTagName("script");'.
192+
'for(var i=0; i < scripts.length; i++) {'.
193+
'if (window.execScript) {'.
194+
'window.execScript(scripts[i].text);'.
195+
'} else {'.
196+
'window["eval"].call(window, scripts[i].text);'.
197+
'}'.
198+
'}'.
199+
'}'.
200+
'};'.
201+
'xhr.send();'.
202+
'</script>'.
203+
'</div>';
204+
$this->assertEquals($expected, $output);
154205
}
155206
}

0 commit comments

Comments
 (0)