| 1 |
( function( $ ) { |
| 2 |
$.fn.LP_Chart_Line = function( data, config ) { |
| 3 |
return $.each( this, function() { |
| 4 |
const $elem = $( this ), |
| 5 |
$canvas = $( '<canvas />' ); |
| 6 |
$elem.html( '' ); |
| 7 |
$canvas.appendTo( $elem ); |
| 8 |
var lineChart = new Chart($canvas,{ |
| 9 |
type: 'line', |
| 10 |
data: data, |
| 11 |
options: config |
| 12 |
}) |
| 13 |
} ); |
| 14 |
// |
| 15 |
}; |
| 16 |
|
| 17 |
$.fn.LP_Statistic_Users = function() { |
| 18 |
if ( parseInt( $( this ).length ) === 0 ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
return $.each( this, function() { |
| 22 |
const $buttons = $( '.chart-buttons button' ).on( 'click', function() { |
| 23 |
const $button = $( this ), |
| 24 |
type = $button.data( 'type' ); |
| 25 |
let from = '', |
| 26 |
to = ''; |
| 27 |
$buttons.not( this ).not( '[data-type="user-custom-time"]' ).prop( 'disabled', false ); |
| 28 |
if ( type == 'user-custom-time' ) { |
| 29 |
from = $( '#user-custom-time input[name="from"]' ).val(); |
| 30 |
to = $( '#user-custom-time input[name="to"]' ).val(); |
| 31 |
|
| 32 |
if ( from == '' || to == '' ) { |
| 33 |
return false; |
| 34 |
} |
| 35 |
} else { |
| 36 |
$button.prop( 'disabled', true ); |
| 37 |
} |
| 38 |
|
| 39 |
const $container = $( '#learn-press-chart' ); |
| 40 |
$container.addClass( 'loading' ); |
| 41 |
$.ajax( { |
| 42 |
url: 'admin-ajax.php', |
| 43 |
data: { |
| 44 |
action: 'learnpress_load_chart', |
| 45 |
type, |
| 46 |
range: [ from, to ], |
| 47 |
}, |
| 48 |
dataType: 'text', |
| 49 |
success( response ) { |
| 50 |
response = LP.parseJSON( response ); |
| 51 |
$container.LP_Chart_Line( response, LP_Chart_Config ); |
| 52 |
$container.removeClass( 'loading' ); |
| 53 |
}, |
| 54 |
} ); |
| 55 |
return false; |
| 56 |
} ), |
| 57 |
$inputs = $( '.chart-buttons #user-custom-time input[type="text"]' ).on( 'change', function() { |
| 58 |
const _valid_date = function() { |
| 59 |
if ( new Date( $inputs[ 0 ].value ) < new Date( $inputs[ 1 ].value ) ) { |
| 60 |
return true; |
| 61 |
} |
| 62 |
}; |
| 63 |
$buttons.filter( '[data-type="user-custom-time"]' ).prop( 'disabled', $inputs.filter( function() { |
| 64 |
return this.value == ''; |
| 65 |
} ).get().length || ! _valid_date() ); |
| 66 |
} ); |
| 67 |
} ); |
| 68 |
}; |
| 69 |
|
| 70 |
$.fn.LP_Statistic_Courses = function() { |
| 71 |
if ( parseInt( $( this ).length ) === 0 ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
return $.each( this, function() { |
| 75 |
var $buttons = $( '.chart-buttons button' ).on( 'click', function() { |
| 76 |
let $button = $( this ), |
| 77 |
type = $button.data( 'type' ), |
| 78 |
from = '', |
| 79 |
to = '', |
| 80 |
$container = $( '#learn-press-chart' ); |
| 81 |
$buttons.not( this ).not( '[data-type="course-custom-time"]' ).prop( 'disabled', false ); |
| 82 |
if ( type == 'course-custom-time' ) { |
| 83 |
from = $( '#course-custom-time input[name="from"]' ).val(); |
| 84 |
to = $( '#course-custom-time input[name="to"]' ).val(); |
| 85 |
|
| 86 |
if ( from == '' || to == '' ) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
} else { |
| 90 |
$button.prop( 'disabled', true ); |
| 91 |
} |
| 92 |
$container.addClass( 'loading' ); |
| 93 |
$.ajax( { |
| 94 |
url: 'admin-ajax.php', |
| 95 |
data: { |
| 96 |
action: 'learnpress_load_chart', |
| 97 |
type, |
| 98 |
range: [ from, to ], |
| 99 |
}, |
| 100 |
dataType: 'text', |
| 101 |
success( response ) { |
| 102 |
response = LP.parseJSON( response ); |
| 103 |
$container.LP_Chart_Line( response, LP_Chart_Config ); |
| 104 |
$container.removeClass( 'loading' ); |
| 105 |
}, |
| 106 |
} ); |
| 107 |
return false; |
| 108 |
} ), |
| 109 |
$inputs = $( '.chart-buttons #course-custom-time input[type="text"]' ).on( 'change', function() { |
| 110 |
const _valid_date = function() { |
| 111 |
if ( new Date( $inputs[ 0 ].value ) < new Date( $inputs[ 1 ].value ) ) { |
| 112 |
return true; |
| 113 |
} |
| 114 |
}; |
| 115 |
$buttons.filter( '[data-type="course-custom-time"]' ).prop( 'disabled', $inputs.filter( function() { |
| 116 |
return this.value == ''; |
| 117 |
} ).get().length || ! _valid_date() ); |
| 118 |
} ); |
| 119 |
} ); |
| 120 |
}; |
| 121 |
|
| 122 |
$.fn.LP_Statistic_Orders = function() { |
| 123 |
if ( parseInt( $( this ).length ) === 0 ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
$( '.panel_report_option' ).hide(); |
| 127 |
$( '#panel_report_sales_by_' + $( '#report_sales_by' ).val() ).show(); |
| 128 |
$( '#report_sales_by' ).on( 'change', function() { |
| 129 |
$( '.panel_report_option' ).hide(); |
| 130 |
$( '#panel_report_sales_by_' + $( this ).val() ).show(); |
| 131 |
if ( 'date' == $( this ).val() ) { |
| 132 |
LP_Statistic_Orders_Upgrade_Chart(); |
| 133 |
} |
| 134 |
} ); |
| 135 |
|
| 136 |
/** |
| 137 |
* Upgrade Chart for Order Statistics |
| 138 |
* |
| 139 |
* @return {boolean} |
| 140 |
*/ |
| 141 |
var LP_Statistic_Orders_Upgrade_Chart = function() { |
| 142 |
let type = '', |
| 143 |
from = '', |
| 144 |
to = '', |
| 145 |
report_sales_by = 'date', |
| 146 |
cat_id = 0, |
| 147 |
course_id = 0; |
| 148 |
report_sales_by = $( '#report_sales_by' ).val(); |
| 149 |
$container = $( '#learn-press-chart' ); |
| 150 |
$container.addClass( 'loading' ); |
| 151 |
// get type |
| 152 |
const $buttons = $( '.chart-buttons button:disabled' ).not( '[data-type="order-custom-time"]' ); |
| 153 |
if ( parseInt( $buttons.length ) > 0 ) { |
| 154 |
type = $( $buttons[ 0 ] ).data( 'type' ); |
| 155 |
} else { |
| 156 |
type = 'order-custom-time'; |
| 157 |
from = $( '#order-custom-time input[name="from"]' ).val(); |
| 158 |
to = $( '#order-custom-time input[name="to"]' ).val(); |
| 159 |
if ( from == '' || to == '' ) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
} |
| 163 |
if ( 'course' === report_sales_by ) { |
| 164 |
course_id = $( '#report-by-course-id' ).val(); |
| 165 |
} else if ( 'category' === report_sales_by ) { |
| 166 |
cat_id = $( '#report-by-course-category-id' ).val(); |
| 167 |
} |
| 168 |
|
| 169 |
$.ajax( { |
| 170 |
url: 'admin-ajax.php', |
| 171 |
data: { |
| 172 |
action: 'learnpress_load_chart', |
| 173 |
type, |
| 174 |
range: [ from, to ], |
| 175 |
report_sales_by, |
| 176 |
course_id, |
| 177 |
cat_id, |
| 178 |
}, |
| 179 |
dataType: 'text', |
| 180 |
success( response ) { |
| 181 |
response = LP.parseJSON( response ); |
| 182 |
$container.LP_Chart_Line( response, LP_Chart_Config ); |
| 183 |
$container.removeClass( 'loading' ); |
| 184 |
}, |
| 185 |
} ); |
| 186 |
}; |
| 187 |
|
| 188 |
$( '#report-by-course-id' ).select2( { |
| 189 |
placeholder: 'Select a course', |
| 190 |
minimumInputLength: 1, |
| 191 |
ajax: { |
| 192 |
url: ajaxurl + '?action=learnpress_search_course', |
| 193 |
dataType: 'json', |
| 194 |
quietMillis: 250, |
| 195 |
data( term, page ) { |
| 196 |
return { |
| 197 |
q: term, // search term |
| 198 |
}; |
| 199 |
}, |
| 200 |
results( data, page ) { |
| 201 |
return { results: data.items }; |
| 202 |
}, |
| 203 |
cache: true, |
| 204 |
}, |
| 205 |
} ); |
| 206 |
|
| 207 |
$( '#report-by-course-id' ).on( 'change', function() { |
| 208 |
LP_Statistic_Orders_Upgrade_Chart(); |
| 209 |
} ); |
| 210 |
|
| 211 |
$( '#report-by-course-category-id' ).select2( { |
| 212 |
placeholder: 'Select a course', |
| 213 |
minimumInputLength: 1, |
| 214 |
ajax: { |
| 215 |
url: ajaxurl + '?action=learnpress_search_course_category', |
| 216 |
dataType: 'json', |
| 217 |
quietMillis: 250, |
| 218 |
data( term, page ) { |
| 219 |
return { |
| 220 |
q: term, // search term |
| 221 |
}; |
| 222 |
}, |
| 223 |
results( data, page ) { |
| 224 |
return { results: data.items }; |
| 225 |
}, |
| 226 |
cache: true, |
| 227 |
}, |
| 228 |
} ); |
| 229 |
|
| 230 |
$( '#report-by-course-category-id' ).on( 'change', function() { |
| 231 |
LP_Statistic_Orders_Upgrade_Chart(); |
| 232 |
} ); |
| 233 |
|
| 234 |
var $buttons = $( '.chart-buttons button' ).on( 'click', function() { |
| 235 |
const $button = $( this ), |
| 236 |
type = $button.data( 'type' ), |
| 237 |
from = '', |
| 238 |
to = '', |
| 239 |
$container = $( '#learn-press-chart' ); |
| 240 |
$buttons.not( this ).not( '[data-type="order-custom-time"]' ).prop( 'disabled', false ); |
| 241 |
if ( type !== 'order-custom-time' ) { |
| 242 |
$button.prop( 'disabled', true ); |
| 243 |
$( '#order-custom-time input[name="from"]' ).val( '' ); |
| 244 |
$( '#order-custom-time input[name="to"]' ).val( '' ); |
| 245 |
} |
| 246 |
LP_Statistic_Orders_Upgrade_Chart(); |
| 247 |
return false; |
| 248 |
} ); |
| 249 |
|
| 250 |
var $inputs = $( '.chart-buttons #order-custom-time input[type="text"]' ).on( 'change', function() { |
| 251 |
const _valid_date = function() { |
| 252 |
if ( new Date( $inputs[ 0 ].value ) < new Date( $inputs[ 1 ].value ) ) { |
| 253 |
return true; |
| 254 |
} |
| 255 |
}; |
| 256 |
$buttons.filter( '[data-type="order-custom-time"]' ).prop( 'disabled', $inputs.filter( function() { |
| 257 |
return this.value == ''; |
| 258 |
} ).get().length || ! _valid_date() ); |
| 259 |
} ); |
| 260 |
}; |
| 261 |
$( function() { |
| 262 |
if ( typeof $.fn.datepicker != 'undefined' ) { |
| 263 |
$( '.date-picker' ).datepicker( { |
| 264 |
dateFormat: 'yy/mm/dd', |
| 265 |
} ); |
| 266 |
} |
| 267 |
$( '.learn-press-statistic-users' ).LP_Statistic_Users(); |
| 268 |
$( '.learn-press-statistic-courses' ).LP_Statistic_Courses(); |
| 269 |
$( '.learn-press-statistic-orders' ).LP_Statistic_Orders(); |
| 270 |
} ); |
| 271 |
return; |
| 272 |
|
| 273 |
}( jQuery ) ); |
| 274 |
|
| 275 |
|
| 276 |
|