-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathsettings.js
111 lines (96 loc) · 3.24 KB
/
settings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
jQuery( document ).ready( function ( $ ) {
/***** Settings Tabs *****/
const $senseiSettings = $( '#woothemes-sensei.sensei-settings' );
function hideAllSections() {
$senseiSettings.find( 'section' ).hide();
$senseiSettings.find( 'a.tab' ).removeClass( 'current' );
}
function show( sectionId = '' ) {
$senseiSettings.find( `section#${ sectionId }` ).show();
$senseiSettings
.find( `[href="#${ sectionId }"]` )
.addClass( 'current' );
sensei_log_event( 'settings_view', { view: sectionId } );
markSectionAsVisited( sectionId );
}
// Hide header and submit on page load if needed
hideSettingsFormElements();
function hideSettingsFormElements() {
const urlHashSectionId = window.location.hash?.replace( '#', '' );
if ( urlHashSectionId === 'woocommerce-settings' ) {
const formRows = $senseiSettings.find( '#woocommerce-settings tr' );
// Hide header and submit if there is not settings form in section
hideHeaderAndSubmit(
! formRows.length &&
$senseiSettings.find( '#sensei-promo-banner' )
);
} else if ( urlHashSectionId === 'sensei-content-drip-settings' ) {
const formRows = $senseiSettings.find(
'#sensei-content-drip-settings tr'
);
// Hide header and submit if there is not settings form in section
hideHeaderAndSubmit(
! formRows.length &&
$senseiSettings.find( '#sensei-promo-banner' )
);
} else {
hideHeaderAndSubmit( false );
}
}
function hideHeaderAndSubmit( shouldHide ) {
if ( shouldHide ) {
$senseiSettings.find( '#submit' ).hide();
$senseiSettings.find( 'h2' ).hide();
} else {
$senseiSettings.find( '#submit' ).show();
$senseiSettings.find( 'h2' ).show();
}
}
window.onhashchange = hideSettingsFormElements;
// Show general settings section if no section is selected in url hasn.
const defaultSectionId = 'default-settings';
const urlHashSectionId = window.location.hash?.replace( '#', '' );
hideAllSections();
if ( urlHashSectionId ) {
show( urlHashSectionId );
} else {
show( defaultSectionId );
}
$senseiSettings.find( 'a.tab' ).on( 'click', function ( e ) {
const queryString = window.location.search;
const urlParams = new URLSearchParams( queryString );
const href = $( this ).attr( 'href' );
if ( urlParams.has( 'tab' ) || ! href?.includes( '#' ) ) {
return true;
}
e.preventDefault();
const sectionId = href.split( '#' )[ 1 ];
window.location.hash = '#' + sectionId;
hideAllSections();
show( sectionId );
return false;
} );
function markSectionAsVisited( sectionId ) {
const data = new FormData();
data.append( 'action', 'sensei_settings_section_visited' );
data.append( 'section_id', sectionId );
data.append( 'nonce', window.senseiSettingsSectionVisitNonce );
fetch( ajaxurl, { method: 'POST', body: data } );
}
/***** Colour pickers *****/
jQuery( '.colorpicker' ).hide();
jQuery( '.colorpicker' ).each( function () {
jQuery( this ).farbtastic( jQuery( this ).prev( '.color' ) );
} );
jQuery( '.color' ).click( function () {
jQuery( this ).next( '.colorpicker' ).fadeIn();
} );
jQuery( document ).mousedown( function () {
jQuery( '.colorpicker' ).each( function () {
var display = jQuery( this ).css( 'display' );
if ( display == 'block' ) {
jQuery( this ).fadeOut();
}
} );
} );
} );