vendor
6 years ago
admin.js
6 years ago
chart.js
6 years ago
wpp.js
6 years ago
wpp.min.js
6 years ago
admin.js
357 lines
| 1 | (function ($) { |
| 2 | "use strict"; |
| 3 | $(function () { |
| 4 | |
| 5 | // Stats config |
| 6 | $("#wpp-stats-config-btn, #wpp_stats_options .button-secondary").on("click", function(e){ |
| 7 | e.preventDefault(); |
| 8 | |
| 9 | // Hide custom range modal box |
| 10 | if ( $("#wpp-stats-range").is(":visible") ) { |
| 11 | $("#wpp-stats-range").hide(); |
| 12 | } |
| 13 | |
| 14 | if ( $("#wpp-stats-config").is(":visible") ) { |
| 15 | $("#wpp-stats-config").hide(); |
| 16 | } |
| 17 | else{ |
| 18 | $("#wpp-stats-config").show(); |
| 19 | } |
| 20 | }); |
| 21 | |
| 22 | // Stats range |
| 23 | $("#wpp-stats-range form").on('submit', function(e){ |
| 24 | e.preventDefault(); |
| 25 | |
| 26 | if ( $("#wpp-stats-range").is(":visible") ) { |
| 27 | $("#wpp-stats-range").hide(); |
| 28 | } |
| 29 | |
| 30 | get_chart_data( $("#wpp-time-ranges li a[data-range='custom']") ); |
| 31 | |
| 32 | $(".wpp-lightbox-tabs li:eq(0) a").trigger("click"); |
| 33 | }); |
| 34 | |
| 35 | $("#wpp-stats-range form .button-secondary").on('click', function(e){ |
| 36 | e.preventDefault(); |
| 37 | |
| 38 | if ( $("#wpp-stats-range").is(":visible") ) { |
| 39 | $("#wpp-stats-range").hide(); |
| 40 | } |
| 41 | |
| 42 | $("#stats_range_date").val(''); |
| 43 | |
| 44 | $(".wpp-lightbox-tabs li:eq(0) a").trigger("click"); |
| 45 | }); |
| 46 | |
| 47 | function get_chart_data(me) { |
| 48 | var args = { |
| 49 | action: 'wpp_update_chart', |
| 50 | nonce: wpp_admin_params.nonce, |
| 51 | range: me.data("range"), |
| 52 | time_quantity: $("#stats_range_time_quantity").val(), |
| 53 | time_unit: $("#stats_range_time_unit").val() |
| 54 | }; |
| 55 | |
| 56 | if ( '' != $("#stats_range_date").val() ){ |
| 57 | args.dates = $("#stats_range_date").val(); |
| 58 | } |
| 59 | |
| 60 | $.get( |
| 61 | ajaxurl, |
| 62 | args, |
| 63 | function( response ){ |
| 64 | if ( 'ok' == response.status ) { |
| 65 | me.parent().addClass("current").siblings().removeClass("current"); |
| 66 | |
| 67 | // Update titles |
| 68 | $("#wpp-chart-wrapper h4").html( response.data.totals.label_summary ); |
| 69 | $("#wpp-chart-wrapper h5").html( response.data.totals.label_date_range ); |
| 70 | |
| 71 | // Update chart |
| 72 | WPPChart.populate(response.data); |
| 73 | |
| 74 | $("#wpp-listing .wpp-tabbed-nav li:eq(0) a").trigger("click"); |
| 75 | |
| 76 | // Update lists |
| 77 | args = { |
| 78 | action: 'wpp_get_most_viewed', |
| 79 | nonce: wpp_admin_params.nonce, |
| 80 | items: 'most-viewed' |
| 81 | }; |
| 82 | |
| 83 | if ( '' != $("#stats_range_date").val() ){ |
| 84 | args.dates = $("#stats_range_date").val(); |
| 85 | } |
| 86 | |
| 87 | $("#wpp-listing .wpp-tab-content:eq(0)").html('<span class="spinner"></span>'); |
| 88 | |
| 89 | $.get( |
| 90 | ajaxurl, |
| 91 | args, |
| 92 | function( response ){ |
| 93 | $("#wpp-listing .wpp-tab-content:eq(0)").html(response); |
| 94 | } |
| 95 | ); |
| 96 | |
| 97 | args = { |
| 98 | action: 'wpp_get_most_commented', |
| 99 | nonce: wpp_admin_params.nonce, |
| 100 | items: 'most-commented' |
| 101 | }; |
| 102 | |
| 103 | if ( '' != $("#stats_range_date").val() ){ |
| 104 | args.dates = $("#stats_range_date").val(); |
| 105 | } |
| 106 | |
| 107 | $("#wpp-listing .wpp-tab-content:eq(1)").html('<span class="spinner"></span>'); |
| 108 | |
| 109 | $.get( |
| 110 | ajaxurl, |
| 111 | args, |
| 112 | function( response ){ |
| 113 | $("#wpp-listing .wpp-tab-content:eq(1)").html(response); |
| 114 | } |
| 115 | ); |
| 116 | |
| 117 | args = { |
| 118 | action: 'wpp_get_trending', |
| 119 | nonce: wpp_admin_params.nonce, |
| 120 | items: 'trending' |
| 121 | }; |
| 122 | |
| 123 | if ( '' != $("#stats_range_date").val() ){ |
| 124 | args.dates = $("#stats_range_date").val(); |
| 125 | } |
| 126 | |
| 127 | $("#wpp-listing .wpp-tab-content:eq(2)").html('<span class="spinner"></span>'); |
| 128 | |
| 129 | $.get( |
| 130 | ajaxurl, |
| 131 | args, |
| 132 | function( response ){ |
| 133 | $("#wpp-listing .wpp-tab-content:eq(2)").html(response); |
| 134 | } |
| 135 | ); |
| 136 | |
| 137 | // Unset date range |
| 138 | $("#stats_range_date").val(''); |
| 139 | } |
| 140 | } |
| 141 | ); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | $("#wpp-time-ranges li a").on("click", function(e){ |
| 146 | e.preventDefault(); |
| 147 | |
| 148 | var me = $(this); |
| 149 | |
| 150 | // Update chart |
| 151 | if ( WPPChart.canRender() ) { |
| 152 | if ( 'custom' != me.data("range") ) { |
| 153 | get_chart_data(me); |
| 154 | } |
| 155 | else { |
| 156 | // Hide Config modal box |
| 157 | if ( $("#wpp-stats-config").is(":visible") ) { |
| 158 | $("#wpp-stats-config").hide(); |
| 159 | } |
| 160 | |
| 161 | if ( !$("#wpp-stats-range").is(":visible") ) { |
| 162 | $("#wpp-stats-range").show(); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | }); |
| 167 | |
| 168 | $("#wpp-time-ranges li.current a").trigger("click"); |
| 169 | |
| 170 | $(".wpp-lightbox-tabs li a").on("click", function(e){ |
| 171 | e.preventDefault(); |
| 172 | |
| 173 | var me = $(this); |
| 174 | me.parent().addClass("active").siblings().removeClass("active"); |
| 175 | |
| 176 | me.closest(".wpp-lightbox").find(".wpp-lightbox-tab-content").removeClass("wpp-lightbox-tab-content-active").filter(function( index ) { |
| 177 | return me.parent().index() == index; |
| 178 | }).addClass("wpp-lightbox-tab-content-active"); |
| 179 | }); |
| 180 | |
| 181 | // Datepicker |
| 182 | $.datepicker._defaults.onAfterUpdate = null; |
| 183 | var datepicker__updateDatepicker = $.datepicker._updateDatepicker; |
| 184 | |
| 185 | $.datepicker._updateDatepicker = function( instance ){ |
| 186 | datepicker__updateDatepicker.call( this, instance ); |
| 187 | |
| 188 | var onAfterUpdate = this._get( instance, 'onAfterUpdate' ); |
| 189 | |
| 190 | if ( onAfterUpdate ) { |
| 191 | onAfterUpdate.apply( ( instance.input ? instance.input[0] : null ), [( instance.input ? instance.input.val() : '' ), instance] ); |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | var curr = -1, |
| 196 | prev = -1; |
| 197 | |
| 198 | var dp_field = $("#stats_range_date"); |
| 199 | |
| 200 | var wpp_datepicker = dp_field.datepicker({ |
| 201 | maxDate: 0, |
| 202 | dateFormat: 'yy-mm-dd', |
| 203 | showButtonPanel: true, |
| 204 | beforeShowDay: function(date){ |
| 205 | return [true, ( (date.getTime() >= Math.min(prev, curr) && date.getTime() <= Math.max(prev, curr) ) ? 'date-range-selected' : '' )] |
| 206 | }, |
| 207 | onSelect: function(dateText, instance){ |
| 208 | |
| 209 | var d1, d2; |
| 210 | |
| 211 | prev = curr; |
| 212 | |
| 213 | curr = ( new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay) ).getTime(); |
| 214 | |
| 215 | if ( |
| 216 | -1 == prev |
| 217 | || prev == curr |
| 218 | ) { |
| 219 | prev = curr; |
| 220 | dp_field.val( dateText ); |
| 221 | } |
| 222 | else { |
| 223 | |
| 224 | d1 = $.datepicker.formatDate('yy-mm-dd', new Date( Math.min(prev, curr) ), {}); |
| 225 | d2 = $.datepicker.formatDate('yy-mm-dd', new Date( Math.max(prev, curr) ), {}); |
| 226 | |
| 227 | dp_field.val( d1 + ' ~ ' + d2 ); |
| 228 | |
| 229 | } |
| 230 | |
| 231 | $(this).data('datepicker').inline = true; |
| 232 | |
| 233 | }, |
| 234 | onClose: function(){ |
| 235 | $(this).data('datepicker').inline = false; |
| 236 | }, |
| 237 | onAfterUpdate: function( instance ){ |
| 238 | var calendar = $(this); |
| 239 | |
| 240 | if ( |
| 241 | prev > -1 |
| 242 | && curr > -1 |
| 243 | ){ |
| 244 | |
| 245 | $('<button type="button" class="ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all" data-handler="hide" data-event="click">OK</button>').appendTo( $(".ui-datepicker-buttonpane") ).on('click', function(){ |
| 246 | dp_field.datepicker('hide'); |
| 247 | }); |
| 248 | } |
| 249 | } |
| 250 | }); |
| 251 | |
| 252 | // STATISTICS TABS |
| 253 | $("#wpp-listing .wpp-tabbed-nav li a").on("click", function(e){ |
| 254 | e.preventDefault(); |
| 255 | |
| 256 | var me = $(this), |
| 257 | target = me.parent().index(); |
| 258 | |
| 259 | me.parent().addClass("active").siblings().removeClass("active"); |
| 260 | |
| 261 | me.closest("#wpp-listing").children(".wpp-tab-content:eq(" + target + ")").addClass("wpp-tab-content-active").siblings().removeClass("wpp-tab-content-active"); |
| 262 | |
| 263 | }); |
| 264 | |
| 265 | // TOOLS |
| 266 | // thumb source selection |
| 267 | $("#thumb_source").change(function() { |
| 268 | if ($(this).val() == "custom_field") { |
| 269 | $("#lbl_field, #thumb_field, #row_custom_field, #row_custom_field_resize").show(); |
| 270 | } else { |
| 271 | $("#lbl_field, #thumb_field, #row_custom_field, #row_custom_field_resize").hide(); |
| 272 | } |
| 273 | }); |
| 274 | // file upload |
| 275 | $('#upload_thumb_button').click(function(e) { |
| 276 | e.preventDefault(); |
| 277 | |
| 278 | var custom_uploader = wp.media({ |
| 279 | title: 'WordPress Popular Posts', |
| 280 | library: { type : 'image' }, |
| 281 | button: { text: wpp_admin_params.label_media_upload_button }, |
| 282 | id: 'library-' + (Math.random() * 10), |
| 283 | multiple: false |
| 284 | }).on('select', function() { |
| 285 | var attachment = custom_uploader.state().get('selection').first().toJSON(); |
| 286 | $('#upload_thumb_src').val( attachment.url ); |
| 287 | |
| 288 | var img = new Image(); |
| 289 | img.onload = function() { |
| 290 | $("#thumb-review").html( this ).parent().fadeIn(); |
| 291 | } |
| 292 | img.src = attachment.url; |
| 293 | |
| 294 | }) |
| 295 | .open(); |
| 296 | |
| 297 | }); |
| 298 | // log limit |
| 299 | $("#log_limit").change(function(){ |
| 300 | var me = $(this); |
| 301 | |
| 302 | if (me.val() == 1) { |
| 303 | me.parent().children("label, .description").show(); |
| 304 | me.parent().children("br").hide(); |
| 305 | } else { |
| 306 | me.parent().children("label, .description").hide(); |
| 307 | me.parent().children("br").show(); |
| 308 | } |
| 309 | }); |
| 310 | // cache interval |
| 311 | $("#cache").change(function() { |
| 312 | if ($(this).val() == 1) { |
| 313 | $("#cache_refresh_interval").show(); |
| 314 | } else { |
| 315 | $("#cache_refresh_interval, #cache_too_long").hide(); |
| 316 | } |
| 317 | }); |
| 318 | // interval |
| 319 | $("#cache_interval_time").change(function() { |
| 320 | var value = parseInt( $("#cache_interval_value").val() ); |
| 321 | var time = $(this).val(); |
| 322 | |
| 323 | if ( time == "hour" && value > 72 ) { |
| 324 | $("#cache_too_long").show(); |
| 325 | } else if ( time == "day" && value > 3 ) { |
| 326 | $("#cache_too_long").show(); |
| 327 | } else if ( time == "week" && value > 1 ) { |
| 328 | $("#cache_too_long").show(); |
| 329 | } else if ( time == "month" && value >= 1 ) { |
| 330 | $("#cache_too_long").show(); |
| 331 | } else if ( time == "year" && value >= 1 ) { |
| 332 | $("#cache_too_long").show(); |
| 333 | } else { |
| 334 | $("#cache_too_long").hide(); |
| 335 | } |
| 336 | }); |
| 337 | |
| 338 | $("#cache_interval_value").change(function() { |
| 339 | var value = parseInt( $(this).val() ); |
| 340 | var time = $("#cache_interval_time").val(); |
| 341 | |
| 342 | if ( time == "hour" && value > 72 ) { |
| 343 | $("#cache_too_long").show(); |
| 344 | } else if ( time == "day" && value > 3 ) { |
| 345 | $("#cache_too_long").show(); |
| 346 | } else if ( time == "week" && value > 1 ) { |
| 347 | $("#cache_too_long").show(); |
| 348 | } else if ( time == "month" && value >= 1 ) { |
| 349 | $("#cache_too_long").show(); |
| 350 | } else if ( time == "year" && value >= 1 ) { |
| 351 | $("#cache_too_long").show(); |
| 352 | } else { |
| 353 | $("#cache_too_long").hide(); |
| 354 | } |
| 355 | }); |
| 356 | }); |
| 357 | }(jQuery)); |