Skip to content

Commit 2258093

Browse files
committed
Work on js
1 parent 51c7be6 commit 2258093

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

src/Factories/JavascriptFactory.php

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -71,80 +71,84 @@ public function getContainerId()
7171
}
7272

7373
/**
74-
* Produce javascript data object for ajax call.
74+
* Determine what to use - jquery or native js.
7575
*
76-
* @return string
76+
* @return bool
7777
*/
78-
protected function produceJavascriptData()
78+
protected function useJquery()
7979
{
80-
$id = WidgetId::get();
81-
$name = $this->widgetFactory->widgetName;
82-
$params = serialize($this->widgetFactory->widgetFullParams);
83-
$token = $this->widgetFactory->app->csrf_token();
84-
85-
if ($this->useJquery()) {
86-
return json_encode([
87-
'id' => $id,
88-
'name' => $name,
89-
'params' => $params,
90-
'_token' => $token,
91-
]);
92-
}
80+
return !$this->widgetFactory->app->config('laravel-widgets.disable_jquery', false);
81+
}
9382

94-
return
95-
"'id=' + encodeURIComponent('{$id}')".
96-
"'&name=' + encodeURIComponent('{$name}')".
97-
"'&params=' + encodeURIComponent('{$params}')".
98-
"'&_token=' + encodeURIComponent('{$token}')";
83+
/**
84+
* Construct ajax call for loaders.
85+
*
86+
* @return string
87+
*/
88+
protected function constructAjaxCall()
89+
{
90+
$data = [
91+
'id' => WidgetId::get(),
92+
'name' => $this->widgetFactory->widgetName,
93+
'params' => serialize($this->widgetFactory->widgetFullParams),
94+
'_token' => $this->widgetFactory->app->csrf_token(),
95+
];
96+
97+
return $this->useJquery()
98+
? $this->constructJqueryAjaxCall($data)
99+
: $this->constructNativeJsAjaxCall($data);
99100
}
100101

101102
/**
102-
* Determine what to use - jquery or native js.
103+
* Construct ajax call with jquery.
103104
*
104-
* @return bool
105+
* @param array $data
106+
*
107+
* @return string
105108
*/
106-
protected function useJquery()
109+
protected function constructJqueryAjaxCall(array $data)
107110
{
108-
return !$this->widgetFactory->app->config('laravel-widgets.disable_jquery', false);
111+
$id = WidgetId::get();
112+
113+
$jsData = json_encode($data);
114+
115+
return
116+
"var widgetTimer{$id} = setInterval(function() {".
117+
'if (window.$) {'.
118+
"$('#{$this->getContainerId()}').load('".$this->ajaxLink."', {$jsData});".
119+
"clearInterval(widgetTimer{$id});".
120+
'}'.
121+
'}, 100);';
109122
}
110123

111124
/**
112-
* Construct ajax call for loaders.
125+
* Construct ajax call without jquery.
126+
*
127+
* @param array $data
113128
*
114129
* @return string
115130
*/
116-
protected function constructAjaxCall()
131+
protected function constructNativeJsAjaxCall(array $data)
117132
{
118-
$ajaxLink = $this->ajaxLink;
119-
$containerId = $this->getContainerId();
120-
$data = $this->produceJavascriptData();
121-
122-
if ($this->useJquery()) {
123-
$id = WidgetId::get();
124-
125-
return
126-
"var widgetTimer{$id} = setInterval(function() {".
127-
'if (window.$) {'.
128-
"$('#{$containerId}').load('".$ajaxLink."', {$data});".
129-
"clearInterval(widgetTimer{$id});".
130-
'}'.
131-
'}, 100);';
132-
}
133+
$jsData = "'id=' + encodeURIComponent('{$data['id']}')".
134+
"'&name=' + encodeURIComponent('{$data['name']}')".
135+
"'&params=' + encodeURIComponent('{$data['params']}')".
136+
"'&_token=' + encodeURIComponent('{$data['token']})";
133137

134138
return
135139
'var xhr = new XMLHttpRequest();'.
136-
'var params = '.$data.';'.
137-
'var container = document.getElementById("'.$containerId.'");'.
138-
'xhr.open("POST", "'.$ajaxLink.'", true);'.
140+
'var params = '.$jsData.';'.
141+
'var container = document.getElementById("'.$this->getContainerId().'");'.
142+
'xhr.open("POST", "'.$this->ajaxLink.'", true);'.
139143
'xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");'.
140144
'xhr.send(params);'.
141145
'xhr.onreadystatechange = function() {'.
142146
'if(xhr.readyState == 4 && xhr.status == 200) {'.
143-
'var data = xhr.responseText;'.
144-
'container.innerHTML = data;'.
145-
'var scripts = container.getElementsByTagName("script");'.
146-
'for(var i=0; i < scripts.length; i++) {'.
147-
'if (window.execScript) {'.
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) {'.
148152
'window.execScript(scripts[i].text);'.
149153
'} else {'.
150154
'window["eval"].call(window, scripts[i].text);'.

0 commit comments

Comments
 (0)