PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / assets / src / js / admin / share / dropdown-pages.js

dropdown-pages.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7, at assets/src/js/admin/share/dropdown-pages.js

130 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 ( function( $ ) {
2 function DropdownPages( el, options ) {
3 this.options = $.extend( {
4 ID: '',
5 name: 'Add new page',
6 }, options || {} );
7 const $element = $( el ),
8 $select = $element.find( 'select' ),
9 $listWrap = $element.find( '.list-pages-wrapper' ),
10 $actions = $element.find( '.quick-add-page-actions' ),
11 $form = $element.find( '.quick-add-page-inline' );
12
13 function addNewPageToList( args ) {
14 const $new_option = $( '<option value="' + args.ID + '">' + args.name + '</option>' );
15 const position = $.inArray( args.ID + '', args.positions );
16
17 $( '.learn-press-dropdown-pages select' ).each( function() {
18 const $sel = $( this ),
19 $option = $new_option.clone();
20 if ( position == 0 ) {
21 $( 'option', $sel ).each( function() {
22 if ( parseInt( $( this ).val() ) ) {
23 $option.insertBefore( $( this ) );
24 return false;
25 }
26 } );
27 } else if ( position == args.positions.length - 1 ) {
28 $sel.append( $option );
29 } else {
30 $option.insertAfter( $( 'option[value="' + args.positions[ position - 1 ] + '"]', $sel ) );
31 }
32 } );
33 }
34
35 $select.on( 'change', function() {
36 $actions.addClass( 'hide-if-js' );
37 if ( this.value !== 'add_new_page' ) {
38 if ( parseInt( this.value ) ) {
39 $actions.find( 'a.edit-page' ).attr( 'href', 'post.php?post=' + this.value + '&action=edit' );
40 $actions.find( 'a.view-page' ).attr( 'href', lpGlobalSettings.siteurl + '?page_id=' + this.value );
41 $actions.removeClass( 'hide-if-js' );
42 $select.attr( 'data-selected', this.value );
43 }
44 return;
45 }
46 $listWrap.addClass( 'hide-if-js' );
47 $form.removeClass( 'hide-if-js' ).find( 'input' ).trigger( 'focus' ).val( '' );
48 } );
49
50 // Select 2
51 $select
52 .css( 'width', $select.width() + 50 )
53 .find( 'option' ).each( function() {
54 $( this ).html( $( this ).html().replace( /&nbsp;&nbsp;&nbsp;/g, '' ) );
55 } );
56
57 $select.select2( {
58 allowClear: true,
59 } );
60
61 $select.on( 'select2:select', function( e ) {
62 const data = e.params.data;
63 } );
64
65 $element.on( 'click', '.quick-add-page-inline button', function() {
66 const $button = $( this ),
67 $input = $form.find( 'input' ),
68 page_name = $input.val();
69 if ( ! page_name ) {
70 alert( 'Please enter the name of page' );
71 $input.trigger( 'focus' );
72 return;
73 }
74 $button.prop( 'disabled', true );
75 $.ajax( {
76 url: lpGlobalSettings.ajax,
77 data: {
78 action: 'learnpress_create_page',
79 page_name,
80 },
81 type: 'post',
82 dataType: 'html',
83 success( response ) {
84 response = LP.parseJSON( response );
85 if ( response.page ) {
86 addNewPageToList( {
87 ID: response.page.ID,
88 name: response.page.post_title,
89 positions: response.positions,
90 } );
91 $select.val( response.page.ID ).trigger( 'focus' );
92 $select.val( response.page.ID ).trigger( 'change' );
93 $form.addClass( 'hide-if-js' );
94 } else if ( response.error ) {
95 alert( response.error );
96 }
97 $button.prop( 'disabled', false );
98 $listWrap.removeClass( 'hide-if-js' );
99 },
100 } );
101 } ).on( 'click', '.quick-add-page-inline a', function( e ) {
102 e.preventDefault();
103 $form.addClass( 'hide-if-js' );
104 $select.val( $select.attr( 'data-selected' ) + '' ).removeAttr( 'disabled' ).trigger( 'change' );
105 $listWrap.removeClass( 'hide-if-js' );
106 } ).on( 'click', '.button-quick-add-page', function( e ) {
107 $select.val( 'add_new_page' ).trigger( 'change' );
108 } ).on( 'keypress keydown', '.quick-add-page-inline input[type="text"]', function( e ) {
109 if ( e.key == 'Enter' && e.type == 'keypress' ) {
110 e.preventDefault();
111 $( this ).siblings( 'button' ).trigger( 'click' );
112 } else if ( e.key == 'Escape' && e.type == 'keydown' ) {
113 $( this ).siblings( 'a' ).trigger( 'click' );
114 }
115 } );
116 }
117
118 $.fn.LP( 'DropdownPages', function() {
119 return $.each( this, function() {
120 let $instance = $( this ).data( 'DropdownPages' );
121 if ( ! $instance ) {
122 $instance = new DropdownPages( this, {} );
123 $( this ).data( 'DropdownPages', $instance );
124 }
125 return $instance;
126 } );
127 } );
128 }( jQuery ) );
129
130