PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.9
4.4.8 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 All 139 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.3.9, at assets/src/js/admin/share/dropdown-pages.js

142 lines 4.5 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 // placeholder: lpDataAdmin.i18n.select_page,
60 // } );
61
62 // $select.on( 'select2:select', function( e ) {
63 // const data = e.params.data;
64 // } );
65 //end
66
67 $element.on( 'click', '.quick-add-page-inline button', function() {
68 const $button = $( this ),
69 $input = $form.find( 'input' ),
70 page_name = $input.val();
71 if ( ! page_name ) {
72 alert( 'Please enter the name of page' );
73 $input.trigger( 'focus' );
74 return;
75 }
76 $button.prop( 'disabled', true );
77
78 const tr = $button.closest( 'tr' );
79 let field_name = '';
80 if ( tr.length ) {
81 field_name = tr.find( '.field_name' ).attr( 'name' );
82 }
83
84 $.ajax( {
85 url: lpGlobalSettings.ajax,
86 data: {
87 action: 'learnpress_create_page',
88 page_name,
89 field_name,
90 nonce: lpDataAdmin.nonce,
91 },
92 type: 'post',
93 dataType: 'html',
94 success( response ) {
95 response = LP.parseJSON( response );
96 if ( response.page ) {
97 addNewPageToList( {
98 ID: response.page.ID,
99 name: response.page.post_title,
100 positions: response.positions,
101 } );
102 /*$select.val( response.page.ID ).trigger( 'focus' );
103 $select.val( response.page.ID ).trigger( 'change' );*/
104 window.location.reload();
105 $form.addClass( 'hide-if-js' );
106 } else if ( response.error ) {
107 alert( response.error );
108 }
109 $button.prop( 'disabled', false );
110 $listWrap.removeClass( 'hide-if-js' );
111 },
112 } );
113 } ).on( 'click', '.quick-add-page-inline a', function( e ) {
114 e.preventDefault();
115 $form.addClass( 'hide-if-js' );
116 $select.val( $select.attr( 'data-selected' ) + '' ).removeAttr( 'disabled' ).trigger( 'change' );
117 $listWrap.removeClass( 'hide-if-js' );
118 } ).on( 'click', '.button-quick-add-page', function( e ) {
119 $select.val( 'add_new_page' ).trigger( 'change' );
120 } ).on( 'keypress keydown', '.quick-add-page-inline input[type="text"]', function( e ) {
121 if ( e.key == 'Enter' && e.type == 'keypress' ) {
122 e.preventDefault();
123 $( this ).siblings( 'button' ).trigger( 'click' );
124 } else if ( e.key == 'Escape' && e.type == 'keydown' ) {
125 $( this ).siblings( 'a' ).trigger( 'click' );
126 }
127 } );
128 }
129
130 $.fn.LP( 'DropdownPages', function() {
131 return $.each( this, function() {
132 let $instance = $( this ).data( 'DropdownPages' );
133 if ( ! $instance ) {
134 $instance = new DropdownPages( this, {} );
135 $( this ).data( 'DropdownPages', $instance );
136 }
137 return $instance;
138 } );
139 } );
140 }( jQuery ) );
141
142