11import "../../core/jquery-ext" ; // for :scrollable for autoLoading-visible
22import "regenerator-runtime/runtime" ; // needed for ``await`` support
3+ import Base from "../../core/base" ;
34import $ from "jquery" ;
45import _ from "underscore" ;
56import ajax from "../ajax/ajax" ;
@@ -41,18 +42,18 @@ parser.addArgument("scroll");
4142// to us
4243parser . addArgument ( "url" ) ;
4344
44- const inject = {
45+ export default Base . extend ( {
4546 name : "inject" ,
4647 trigger :
4748 ".raptor-ui .ui-button.pat-inject, a.pat-inject, form.pat-inject, .pat-subform.pat-inject" ,
4849 parser : parser ,
4950
50- init ( $el , opts ) {
51- const cfgs = this . extractConfig ( $ el, opts ) ;
51+ init ( ) {
52+ const cfgs = this . extractConfig ( this . el , this . options ) ;
5253 if ( cfgs . some ( ( e ) => e . history === "record" ) && ! ( "pushState" in history ) ) {
5354 // if the injection shall add a history entry and HTML5 pushState
5455 // is missing, then don't initialize the injection.
55- return $el ;
56+ return ;
5657 }
5758 $el . data ( "pat-inject" , cfgs ) ;
5859
@@ -161,9 +162,10 @@ const inject = {
161162 const $el = $ ( e . currentTarget ) ;
162163 const cfgs = $el . data ( "pat-inject" ) ;
163164 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+ }
167169 }
168170 e . preventDefault && e . preventDefault ( ) ;
169171 $el . trigger ( "patterns-inject-triggered" ) ;
@@ -182,9 +184,10 @@ const inject = {
182184 const $cfg_node = $button . closest ( "[data-pat-inject]" ) ;
183185 const cfgs = this . extractConfig ( $cfg_node , opts ) ;
184186
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+ }
188191
189192 e . preventDefault ( ) ;
190193 $form . trigger ( "patterns-inject-triggered" ) ;
@@ -194,13 +197,13 @@ const inject = {
194197 submitSubform ( $sub ) {
195198 /* This method is called from pat-subform
196199 */
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 ] ) ;
199202
200203 // 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+ }
204207
205208 try {
206209 $el . trigger ( "patterns-inject-triggered" ) ;
@@ -210,19 +213,20 @@ const inject = {
210213 this . execute ( cfgs , $el ) ;
211214 } ,
212215
213- extractConfig ( $el , opts ) {
214- opts = $ . extend ( { } , opts ) ;
216+ extractConfig ( el , options = { } ) {
217+ el = utils . jqToNode ( el ) ;
218+ options = Object . assign ( { } , options ) ; // copy
215219
216- const cfgs = parser . parse ( $ el, opts , true ) ;
220+ const cfgs = parser . parse ( el , options , true ) ;
217221 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
220224 cfg . url =
221- opts . url ||
225+ options . url ||
222226 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" ) ||
226230 "" ;
227231
228232 // separate selector from url
@@ -1123,7 +1127,7 @@ const inject = {
11231127 } ,
11241128 } ,
11251129 } ,
1126- } ;
1130+ } ) ;
11271131
11281132$ ( document ) . on ( "patterns-injected.inject" , ( ev , cfg , trigger , injected ) => {
11291133 /* Listen for the patterns-injected event.
@@ -1170,6 +1174,3 @@ if ("replaceState" in history) {
11701174 log . debug ( e ) ;
11711175 }
11721176}
1173-
1174- registry . register ( inject ) ;
1175- export default inject ;
0 commit comments