Debian Codegraph @

resources/src/mediawiki.htmlform/cloner.js

raw symbols SCIP
1 /*
2 * HTMLForm enhancements:
3 * Add/remove cloner clones without having to resubmit the form.
4 */
5
6 let cloneCounter = 0;
7
8 /**
9 * Appends a new row with fields to the cloner.
10 *
11 * @ignore
12 * @param {jQuery} $createButton
13 */
14 function appendToCloner( $createButton ) {
15 const $ul = $createButton.prev( 'ul.mw-htmlform-cloner-ul' ),
16
17 cloneRegex = new RegExp( mw.util.escapeRegExp( $ul.data( 'uniqueId' ) ), 'g' ),
18 // Assume the ids that need to be made unique will start with 'ooui-php-'. See T274533
19 inputIdRegex = new RegExp( /(ooui-php-[0-9]*)/, 'gm' );
20
21 ++cloneCounter;
22 const html = $ul.data( 'template' )
23 .replace( cloneRegex, 'clone' + cloneCounter )
24 .replace( inputIdRegex, '$1-clone' + cloneCounter );
25
26 const $li = $( '<li>' )
27 .addClass( 'mw-htmlform-cloner-li' )
28 .html( html )
29 .appendTo( $ul );
30
31 mw.hook( 'htmlform.enhance' ).fire( $li );
32 }
33
34 mw.hook( 'htmlform.enhance' ).add( ( $root ) => {
35 const $deleteElement = $root.find( '.mw-htmlform-cloner-delete-button' ),
36 $createElement = $root.find( '.mw-htmlform-cloner-create-button' );
37
38 $deleteElement.each( function () {
39 const $element = $( this );
40
41 // eslint-disable-next-line no-jquery/no-class-state
42 if ( $element.hasClass( 'oo-ui-widget' ) ) {
43 mw.loader.using( 'mediawiki.htmlform.ooui' ).then( () => {
44 const deleteButton = OO.ui.infuse( $element );
45 deleteButton.on( 'click', () => {
46 deleteButton.$element.closest( 'li.mw-htmlform-cloner-li' ).remove();
47 } );
48 } );
49 } else {
50 // eslint-disable-next-line no-jquery/no-sizzle
51 $element.filter( ':input' ).on( 'click', function ( e ) {
52 e.preventDefault();
53 $( this ).closest( 'li.mw-htmlform-cloner-li' ).remove();
54 } );
55 }
56 } );
57
58 $createElement.each( function () {
59 const $element = $( this );
60
61 // eslint-disable-next-line no-jquery/no-class-state
62 if ( $element.hasClass( 'oo-ui-widget' ) ) {
63 mw.loader.using( 'mediawiki.htmlform.ooui' ).then( () => {
64 const createButton = OO.ui.infuse( $element );
65 createButton.on( 'click', () => {
66 appendToCloner( createButton.$element );
67 } );
68 } );
69 } else {
70 // eslint-disable-next-line no-jquery/no-sizzle
71 $element.filter( ':input' ).on( 'click', function ( e ) {
72 e.preventDefault();
73 appendToCloner( $( this ) );
74 } );
75 }
76 } );
77 } );