1
1
import "../../core/jquery-ext" ; // for :scrollable for autoLoading-visible
2
2
import "regenerator-runtime/runtime" ; // needed for ``await`` support
3
+ import Base from "../../core/base" ;
3
4
import $ from "jquery" ;
4
5
import _ from "underscore" ;
5
6
import ajax from "../ajax/ajax" ;
@@ -41,18 +42,18 @@ parser.addArgument("scroll");
41
42
// to us
42
43
parser . addArgument ( "url" ) ;
43
44
44
- const inject = {
45
+ export default Base . extend ( {
45
46
name : "inject" ,
46
47
trigger :
47
48
".raptor-ui .ui-button.pat-inject, a.pat-inject, form.pat-inject, .pat-subform.pat-inject" ,
48
49
parser : parser ,
49
50
50
- init ( $el , opts ) {
51
- const cfgs = this . extractConfig ( $ el, opts ) ;
51
+ init ( ) {
52
+ const cfgs = this . extractConfig ( this . el , this . options ) ;
52
53
if ( cfgs . some ( ( e ) => e . history === "record" ) && ! ( "pushState" in history ) ) {
53
54
// if the injection shall add a history entry and HTML5 pushState
54
55
// is missing, then don't initialize the injection.
55
- return $el ;
56
+ return ;
56
57
}
57
58
$el . data ( "pat-inject" , cfgs ) ;
58
59
@@ -161,9 +162,10 @@ const inject = {
161
162
const $el = $ ( e . currentTarget ) ;
162
163
const cfgs = $el . data ( "pat-inject" ) ;
163
164
if ( $el . is ( "form" ) ) {
164
- $ ( cfgs ) . each ( ( i , v ) => {
165
- v . params = $ . param ( $el . serializeArray ( ) ) ;
166
- } ) ;
165
+ // store the params of the form in the config, to be used by history
166
+ for ( cfg of cfgs ) {
167
+ cfg . params = $ . param ( $sub . serializeArray ( ) ) ;
168
+ }
167
169
}
168
170
e . preventDefault && e . preventDefault ( ) ;
169
171
$el . trigger ( "patterns-inject-triggered" ) ;
@@ -182,9 +184,10 @@ const inject = {
182
184
const $cfg_node = $button . closest ( "[data-pat-inject]" ) ;
183
185
const cfgs = this . extractConfig ( $cfg_node , opts ) ;
184
186
185
- $ ( cfgs ) . each ( ( i , v ) => {
186
- v . params = $ . param ( $form . serializeArray ( ) ) ;
187
- } ) ;
187
+ // store the params of the form in the config, to be used by history
188
+ for ( cfg of cfgs ) {
189
+ cfg . params = $ . param ( $sub . serializeArray ( ) ) ;
190
+ }
188
191
189
192
e . preventDefault ( ) ;
190
193
$form . trigger ( "patterns-inject-triggered" ) ;
@@ -194,13 +197,13 @@ const inject = {
194
197
submitSubform ( $sub ) {
195
198
/* This method is called from pat-subform
196
199
*/
197
- const $el = $sub . parents ( "form" ) ;
198
- const cfgs = $sub . data ( "pat-inject" ) ;
200
+ const $el = $ ( $ sub[ 0 ] . closest ( "form" ) ) ;
201
+ const cfgs = this . extractConfig ( $sub [ 0 ] ) ;
199
202
200
203
// store the params of the subform in the config, to be used by history
201
- $ ( cfgs ) . each ( ( i , v ) => {
202
- v . params = $ . param ( $sub . serializeArray ( ) ) ;
203
- } ) ;
204
+ for ( cfg of cfgs ) {
205
+ cfg . params = $ . param ( $sub . serializeArray ( ) ) ;
206
+ }
204
207
205
208
try {
206
209
$el . trigger ( "patterns-inject-triggered" ) ;
@@ -210,19 +213,20 @@ const inject = {
210
213
this . execute ( cfgs , $el ) ;
211
214
} ,
212
215
213
- extractConfig ( $el , opts ) {
214
- opts = $ . extend ( { } , opts ) ;
216
+ extractConfig ( el , options = { } ) {
217
+ el = utils . jqToNode ( el ) ;
218
+ options = Object . assign ( { } , options ) ; // copy
215
219
216
- const cfgs = parser . parse ( $ el, opts , true ) ;
220
+ const cfgs = parser . parse ( el , options , true ) ;
217
221
cfgs . forEach ( ( cfg ) => {
218
- cfg . $context = $el ;
219
- // opts and cfg have priority, fall back to href/action
222
+ cfg . $context = $ ( el ) ;
223
+ // options and cfg have priority, fall back to href/action
220
224
cfg . url =
221
- opts . url ||
225
+ options . url ||
222
226
cfg . url ||
223
- $ el. attr ( "href" ) ||
224
- $ el. attr ( "action" ) ||
225
- $ el. parents ( "form" ) . attr ( "action" ) ||
227
+ el . getAttribute ( "href" ) ||
228
+ el . getAttribute ( "action" ) ||
229
+ el . closest ( "form" ) ?. getAttribute ( "action" ) ||
226
230
"" ;
227
231
228
232
// separate selector from url
@@ -1122,7 +1126,7 @@ const inject = {
1122
1126
} ,
1123
1127
} ,
1124
1128
} ,
1125
- } ;
1129
+ } ) ;
1126
1130
1127
1131
$ ( document ) . on ( "patterns-injected.inject" , ( ev , cfg , trigger , injected ) => {
1128
1132
/* Listen for the patterns-injected event.
@@ -1169,6 +1173,3 @@ if ("replaceState" in history) {
1169
1173
log . debug ( e ) ;
1170
1174
}
1171
1175
}
1172
-
1173
- registry . register ( inject ) ;
1174
- export default inject ;
0 commit comments