| @@ -1,123 +1,123 @@ | ||
| 1 | -const $ = jQuery; | |
| 2 | - | |
| 3 | -function formatCourse( repo ) { | |
| 4 | - if ( repo.loading ) { | |
| 5 | - return repo.text; | |
| 6 | - } | |
| 7 | - const markup = "<div class='select2-result-course_title'>" + repo.id + ' - ' + repo.title.rendered + '</div>'; | |
| 8 | - return markup; | |
| 9 | -} | |
| 10 | - | |
| 11 | -function formatCourseSelection( repo ) { | |
| 12 | - return repo.title.rendered || repo.text; | |
| 13 | -} | |
| 14 | - | |
| 15 | -/*function autocompleteWidget( widget = null ) { | |
| 16 | - const searchs = $( '.lp-widget_select_course' ); | |
| 17 | - const wpRestUrl = searchs.data( 'rest-url' ); | |
| 18 | - const postType = searchs.data( 'post_type' ) || 'lp_course'; | |
| 19 | - | |
| 20 | - if ( postType === 'lp_course' ) { | |
| 21 | - const dataStruct = { | |
| 22 | - urlApi: Api.admin.apiSearchCourses, | |
| 23 | - dataType: 'courses', | |
| 24 | - keyGetValue: { | |
| 25 | - value: 'ID', | |
| 26 | - text: '{{post_title}} (#{{ID}})', | |
| 27 | - key_render: { | |
| 28 | - post_title: 'post_title', | |
| 29 | - ID: 'ID', | |
| 30 | - }, | |
| 31 | - }, | |
| 32 | - }; | |
| 33 | - const dataStructJson = JSON.stringify( dataStruct ); | |
| 34 | - const widgetListEl = document.querySelector( '#widget-list' ); | |
| 35 | - let nonTomSelectWidgetEls = ''; | |
| 36 | - if ( widgetListEl ) { | |
| 37 | - const tomSelectWidgetEls = Array.prototype.slice.call( widgetListEl.querySelectorAll( '.lp-widget_select_course' ) ); | |
| 38 | - nonTomSelectWidgetEls = Array.prototype.slice.call( searchs ).filter( ( el ) => ! tomSelectWidgetEls.includes( el ) ); | |
| 39 | - } | |
| 40 | - | |
| 41 | - nonTomSelectWidgetEls.forEach( ( nonTomSelectWidgetEl ) => { | |
| 42 | - $( nonTomSelectWidgetEl ).attr( 'data-struct', dataStructJson ); | |
| 43 | - initTomSelect( nonTomSelectWidgetEl ); | |
| 44 | - } ); | |
| 45 | - } else { | |
| 46 | - searchs.select2( { | |
| 47 | - ajax: { | |
| 48 | - method: 'GET', | |
| 49 | - url: wpRestUrl + 'wp/v2/' + postType, | |
| 50 | - dataType: 'json', | |
| 51 | - delay: 250, | |
| 52 | - data( params ) { | |
| 53 | - return { | |
| 54 | - search: params.term, | |
| 55 | - }; | |
| 56 | - }, | |
| 57 | - processResults( data, params ) { | |
| 58 | - params.page = params.page || 1; | |
| 59 | - | |
| 60 | - return { | |
| 61 | - results: data, | |
| 62 | - }; | |
| 63 | - }, | |
| 64 | - cache: true, | |
| 65 | - }, | |
| 66 | - escapeMarkup( markup ) { | |
| 67 | - return markup; | |
| 68 | - }, | |
| 69 | - minimumInputLength: 2, | |
| 70 | - templateResult: formatCourse, // omitted for brevity, see the source of this page | |
| 71 | - templateSelection: formatCourseSelection, // omitted for brevity, see the source of this page | |
| 72 | - } ); | |
| 73 | - } | |
| 74 | -}*/ | |
| 75 | - | |
| 76 | -document.addEventListener( 'DOMContentLoaded', function( event ) { | |
| 77 | - if ( document.querySelector( '#widgets-editor' ) ) { | |
| 78 | - $( document ).on( 'widget-added', function( event, widget ) { | |
| 79 | - //autocompleteWidget( widget ); | |
| 80 | - } ); | |
| 81 | - } else { | |
| 82 | - $( document ).on( 'learnpress/widgets/select', function() { | |
| 83 | - //autocompleteWidget(); | |
| 84 | - sortItem(); | |
| 85 | - } ); | |
| 86 | - | |
| 87 | - //autocompleteWidget(); | |
| 88 | - } | |
| 89 | -} ); | |
| 90 | - | |
| 91 | -//Sortable checkbox | |
| 92 | -document.addEventListener( 'DOMContentLoaded', function( event ) { | |
| 93 | - sortItem(); | |
| 94 | -} ); | |
| 95 | - | |
| 96 | -$( document ).on( 'widget-added widget-updated', function( event ) { | |
| 97 | - sortItem(); | |
| 98 | -} ); | |
| 99 | - | |
| 100 | -function sortItem() { | |
| 101 | - $( '.widget-content .sortable' ).sortable( { | |
| 102 | - handle: '.drag', | |
| 103 | - | |
| 104 | - start( event, ui ) { | |
| 105 | - | |
| 106 | - }, | |
| 107 | - update( event, ui ) { | |
| 108 | - const value = []; | |
| 109 | - $( this ).children().map( function() { | |
| 110 | - value.push( $( this ).find( 'input' ).val() ); | |
| 111 | - } ); | |
| 112 | - | |
| 113 | - value.join( ',' ); | |
| 114 | - | |
| 115 | - const fieldSort = $( this ).closest( '.widget-content' ).find( 'input.fields-sort' ); | |
| 116 | - fieldSort.val( value ); | |
| 117 | - fieldSort.trigger( 'change' ); | |
| 118 | - }, | |
| 119 | - stop( event, ui ) { | |
| 120 | - | |
| 121 | - }, | |
| 122 | - } ); | |
| 123 | -} | |
| 1 | +const $ = jQuery; | |
| 2 | + | |
| 3 | +function formatCourse( repo ) { | |
| 4 | + if ( repo.loading ) { | |
| 5 | + return repo.text; | |
| 6 | + } | |
| 7 | + const markup = "<div class='select2-result-course_title'>" + repo.id + ' - ' + repo.title.rendered + '</div>'; | |
| 8 | + return markup; | |
| 9 | +} | |
| 10 | + | |
| 11 | +function formatCourseSelection( repo ) { | |
| 12 | + return repo.title.rendered || repo.text; | |
| 13 | +} | |
| 14 | + | |
| 15 | +/*function autocompleteWidget( widget = null ) { | |
| 16 | + const searchs = $( '.lp-widget_select_course' ); | |
| 17 | + const wpRestUrl = searchs.data( 'rest-url' ); | |
| 18 | + const postType = searchs.data( 'post_type' ) || 'lp_course'; | |
| 19 | + | |
| 20 | + if ( postType === 'lp_course' ) { | |
| 21 | + const dataStruct = { | |
| 22 | + urlApi: Api.admin.apiSearchCourses, | |
| 23 | + dataType: 'courses', | |
| 24 | + keyGetValue: { | |
| 25 | + value: 'ID', | |
| 26 | + text: '{{post_title}} (#{{ID}})', | |
| 27 | + key_render: { | |
| 28 | + post_title: 'post_title', | |
| 29 | + ID: 'ID', | |
| 30 | + }, | |
| 31 | + }, | |
| 32 | + }; | |
| 33 | + const dataStructJson = JSON.stringify( dataStruct ); | |
| 34 | + const widgetListEl = document.querySelector( '#widget-list' ); | |
| 35 | + let nonTomSelectWidgetEls = ''; | |
| 36 | + if ( widgetListEl ) { | |
| 37 | + const tomSelectWidgetEls = Array.prototype.slice.call( widgetListEl.querySelectorAll( '.lp-widget_select_course' ) ); | |
| 38 | + nonTomSelectWidgetEls = Array.prototype.slice.call( searchs ).filter( ( el ) => ! tomSelectWidgetEls.includes( el ) ); | |
| 39 | + } | |
| 40 | + | |
| 41 | + nonTomSelectWidgetEls.forEach( ( nonTomSelectWidgetEl ) => { | |
| 42 | + $( nonTomSelectWidgetEl ).attr( 'data-struct', dataStructJson ); | |
| 43 | + initTomSelect( nonTomSelectWidgetEl ); | |
| 44 | + } ); | |
| 45 | + } else { | |
| 46 | + searchs.select2( { | |
| 47 | + ajax: { | |
| 48 | + method: 'GET', | |
| 49 | + url: wpRestUrl + 'wp/v2/' + postType, | |
| 50 | + dataType: 'json', | |
| 51 | + delay: 250, | |
| 52 | + data( params ) { | |
| 53 | + return { | |
| 54 | + search: params.term, | |
| 55 | + }; | |
| 56 | + }, | |
| 57 | + processResults( data, params ) { | |
| 58 | + params.page = params.page || 1; | |
| 59 | + | |
| 60 | + return { | |
| 61 | + results: data, | |
| 62 | + }; | |
| 63 | + }, | |
| 64 | + cache: true, | |
| 65 | + }, | |
| 66 | + escapeMarkup( markup ) { | |
| 67 | + return markup; | |
| 68 | + }, | |
| 69 | + minimumInputLength: 2, | |
| 70 | + templateResult: formatCourse, // omitted for brevity, see the source of this page | |
| 71 | + templateSelection: formatCourseSelection, // omitted for brevity, see the source of this page | |
| 72 | + } ); | |
| 73 | + } | |
| 74 | +}*/ | |
| 75 | + | |
| 76 | +document.addEventListener( 'DOMContentLoaded', function( event ) { | |
| 77 | + if ( document.querySelector( '#widgets-editor' ) ) { | |
| 78 | + $( document ).on( 'widget-added', function( event, widget ) { | |
| 79 | + //autocompleteWidget( widget ); | |
| 80 | + } ); | |
| 81 | + } else { | |
| 82 | + $( document ).on( 'learnpress/widgets/select', function() { | |
| 83 | + //autocompleteWidget(); | |
| 84 | + sortItem(); | |
| 85 | + } ); | |
| 86 | + | |
| 87 | + //autocompleteWidget(); | |
| 88 | + } | |
| 89 | +} ); | |
| 90 | + | |
| 91 | +//Sortable checkbox | |
| 92 | +document.addEventListener( 'DOMContentLoaded', function( event ) { | |
| 93 | + sortItem(); | |
| 94 | +} ); | |
| 95 | + | |
| 96 | +$( document ).on( 'widget-added widget-updated', function( event ) { | |
| 97 | + sortItem(); | |
| 98 | +} ); | |
| 99 | + | |
| 100 | +function sortItem() { | |
| 101 | + $( '.widget-content .sortable' ).sortable( { | |
| 102 | + handle: '.drag', | |
| 103 | + | |
| 104 | + start( event, ui ) { | |
| 105 | + | |
| 106 | + }, | |
| 107 | + update( event, ui ) { | |
| 108 | + const value = []; | |
| 109 | + $( this ).children().map( function() { | |
| 110 | + value.push( $( this ).find( 'input' ).val() ); | |
| 111 | + } ); | |
| 112 | + | |
| 113 | + value.join( ',' ); | |
| 114 | + | |
| 115 | + const fieldSort = $( this ).closest( '.widget-content' ).find( 'input.fields-sort' ); | |
| 116 | + fieldSort.val( value ); | |
| 117 | + fieldSort.trigger( 'change' ); | |
| 118 | + }, | |
| 119 | + stop( event, ui ) { | |
| 120 | + | |
| 121 | + }, | |
| 122 | + } ); | |
| 123 | +} | |