learndash-course-wizard.js
5.57 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
window.onload = function() {
var createHiddenField = function( name, value ) {
var input = document.createElement( 'input' );
input.type = 'hidden';
input.name = name;
input.value = value;
return input;
};
var removeChildren = function( element ) {
while ( element.firstChild ) {
element.removeChild( element.firstChild );
}
};
var showExtraPriceTypeFields = function( priceType ) {
document.getElementById( 'ld_cw_paynow_div' ).style.display = 'none';
document.getElementById( 'ld_cw_subscribe_div' ).style.display = 'none';
if ( priceType === 'paynow' ) {
document.getElementById( 'ld_cw_paynow_div' ).style.display = 'block';
} else if ( priceType === 'subscribe' ) {
document.getElementById( 'ld_cw_subscribe_div' ).style.display = 'block';
}
};
var coursePriceSelected = document.querySelector( 'input[name="ld_cw_course_price_type"]:checked' );
if ( coursePriceSelected ) {
showExtraPriceTypeFields( coursePriceSelected.value );
}
// show extra fields when the user selects a course price type
var coursePriceTypeRadios = document.getElementsByName( 'ld_cw_course_price_type' );
if ( coursePriceTypeRadios.length > 0 ) {
for ( var i = 0; i < coursePriceTypeRadios.length; i++ ) {
coursePriceTypeRadios[i].addEventListener( 'change', function( event ) {
showExtraPriceTypeFields( event.target.value );
} );
}
}
// show services branding when the user fills down the URL field
var showButtonBranding = function( url ) {
var buttonLabel = ldCourseWizard.buttons.default.label;
var buttonImageSrc,
buttonImageAlt,
buttonImageClass = null;
var button = document.getElementById( 'ld_cw_load_data_button' );
removeChildren( button );
if ( url.includes( 'youtube.com' ) ) {
buttonLabel = ldCourseWizard.buttons.youtube.label;
buttonImageSrc = ldCourseWizard.buttons.youtube.img_src;
buttonImageAlt = ldCourseWizard.buttons.youtube.img_alt;
buttonImageClass = ldCourseWizard.buttons.youtube.img_class;
} else if ( url.includes( 'vimeo.com' ) ) {
buttonLabel = ldCourseWizard.buttons.vimeo.label;
buttonImageSrc = ldCourseWizard.buttons.vimeo.img_src;
buttonImageAlt = ldCourseWizard.buttons.vimeo.img_alt;
buttonImageClass = ldCourseWizard.buttons.vimeo.img_class;
} else if ( url.includes( 'wistia.com' ) ) {
buttonLabel = ldCourseWizard.buttons.wistia.label;
buttonImageSrc = ldCourseWizard.buttons.wistia.img_src;
buttonImageAlt = ldCourseWizard.buttons.wistia.img_alt;
buttonImageClass = ldCourseWizard.buttons.wistia.img_class;
}
// add the image if it exists
if ( buttonImageSrc ) {
var img = document.createElement( 'img' );
img.src = buttonImageSrc;
img.alt = buttonImageAlt;
img.className = buttonImageClass;
button.appendChild( img );
}
// add the button label
var label = document.createElement( 'span' );
label.textContent = buttonLabel;
button.appendChild( label );
};
var playlistUrl = document.getElementById( 'ld_cw_playlist_url' );
if ( playlistUrl ) {
playlistUrl.addEventListener( 'keyup',
function( event ) {
showButtonBranding( event.target.value );
}
);
playlistUrl.addEventListener( 'change',
function( event ) {
showButtonBranding( event.target.value );
}
);
showButtonBranding( playlistUrl.value );
}
// billing cycle control
var billingCycle = document.getElementById( 'ld_cw_course_price_billing_interval' );
var billingCycleNumber = document.getElementById( 'ld_cw_course_price_billing_number' );
var maxValue = 0;
if ( billingCycle && billingCycleNumber ) {
billingCycle.addEventListener( 'change', function( event ) {
switch ( event.target.value ) {
case 'D':
maxValue = ldCourseWizard.valid_recurring_paypal_day_max;
break;
case 'W':
maxValue = ldCourseWizard.valid_recurring_paypal_week_max;
break;
case 'M':
maxValue = ldCourseWizard.valid_recurring_paypal_month_max;
break;
case 'Y':
maxValue = ldCourseWizard.valid_recurring_paypal_year_max;
break;
default:
maxValue = 0;
break;
}
if ( billingCycleNumber.value > maxValue ) {
billingCycleNumber.value = maxValue;
}
billingCycleNumber.setAttribute( 'max', maxValue );
} );
}
// add event listener for the submit button
var createCourseButton = document.getElementById( 'ld_cw_create_course_btn' );
if ( createCourseButton ) {
createCourseButton.addEventListener( 'click', function() {
var form = document.getElementById( 'ld_cw_create_course_form' );
var courseType = document.querySelector( 'input[name="ld_cw_course_price_type"]:checked' ).value;
if ( form ) {
form.appendChild(
createHiddenField( 'course_price_type'
, courseType )
);
form.appendChild(
createHiddenField( 'course_disable_lesson_progression'
, document.querySelector( 'input[name="ld_cw_course_progression"]:checked' ).value )
);
if ( courseType === 'paynow' ) {
form.appendChild(
createHiddenField( 'course_price'
, document.getElementById( 'ld_cw_course_price_type_paynow_price' ).value )
);
} else if ( courseType === 'subscribe' ) {
form.appendChild(
createHiddenField( 'course_price'
, document.getElementById( 'ld_cw_course_price_type_subscribe_price' ).value )
);
form.appendChild(
createHiddenField( 'course_price_billing_number'
, document.getElementById( 'ld_cw_course_price_billing_number' ).value )
);
form.appendChild(
createHiddenField( 'course_price_billing_interval'
, document.getElementById( 'ld_cw_course_price_billing_interval' ).value )
);
}
form.submit();
}
} );
}
};