1/** 2 * Controller for common methods in the admin section 3 * 4 * @author Alexander Gilmanov 5 * @since 18.10.2016 6 */ 7/** 8 * Define popover options 9 */ 10 11var popoverOptions = { 12 html: true, 13 template: '<div class="popover wdt-premium-popover" role="tooltip">' + 14 '<div class="arrow"></div>' + 15 '<h3 class="popover-header">' + 16 '<i class="wpdt-icon-star-full m-r-5" style="color: #FFC078;"></i>' + 17 (wdtWpDataTablesPopoverStrings.title ? wdtWpDataTablesPopoverStrings.title : 'This is a premium feature') + 18 '<button type="button" class="closePopover">' + 19 '<span aria-hidden="true"><i class="wpdt-icon-times-full"></i></span></button></h3>' + 20 '<div class="popover-body"><span>' + (wdtWpDataTablesPopoverStrings.description ? wdtWpDataTablesPopoverStrings.description : 'This feature is available only in premium version of wpDataTables') + '</span> ' + 21 '<div class="popover-footer"><a id="wdt-premium-compare-link">' + (wdtWpDataTablesPopoverStrings.compare_link ? wdtWpDataTablesPopoverStrings.compare_link : 'Compare and View Pricing') + '</a></div></div' + 22 '</div>', 23 content: function () { 24 var content = jQuery(this).attr("data-popover-content"); 25 return jQuery(content).children(".popover-body").html(); 26 }, 27 title: function () { 28 var title = jQuery(this).attr("data-popover-content"); 29 return jQuery(title).children(".popover-heading").html(); 30 } 31} 32/** 33 * Hide tooltip on button click or on mouseout event 34 */ 35var wdtInteractiveTooltipHideTimeout; 36 37var wdtHideTooltip = function () { 38 jQuery('.wdt-datatables-admin-wrap [data-toggle="tooltip"]:not(.wdt-tooltip-interactive)').on('click', function () { 39 jQuery(this).wdtBootstrapTooltip('hide'); 40 }); 41 42 jQuery('.wdt-datatables-admin-wrap [data-toggle="tooltip"]:not(.wdt-tooltip-interactive)').mouseout(function (event) { 43 var e = event.toElement || event.relatedTarget; 44 if (e != null && (e.parentNode == this || e == this)) { 45 return; 46 } 47 jQuery(this).wdtBootstrapTooltip('hide'); 48 }); 49}; 50 51/** 52 * Keep tooltips with links open while hovering over the tooltip itself 53 */ 54var wdtInteractiveTooltip = function () { 55 if (typeof jQuery.fn.wdtBootstrapTooltip === 'undefined') { 56 return; 57 } 58 59 var $tooltips = jQuery('.wdt-datatables-admin-wrap .wdt-tooltip-interactive[data-toggle="tooltip"]'); 60 61 if (!$tooltips.length) { 62 return; 63 } 64 65 $tooltips.wdtBootstrapTooltip({ 66 html: true, 67 trigger: 'manual' 68 }); 69 70 var wdtShowInteractiveTooltip = function ($el) { 71 clearTimeout(wdtInteractiveTooltipHideTimeout); 72 $tooltips.not($el).wdtBootstrapTooltip('hide'); 73 $el.wdtBootstrapTooltip('show'); 74 }; 75 76 var wdtScheduleHideInteractiveTooltip = function ($el) { 77 wdtInteractiveTooltipHideTimeout = setTimeout(function () { 78 if (!jQuery('.tooltip:hover').length && !jQuery('.tooltip').has(document.activeElement).length) { 79 $el.wdtBootstrapTooltip('hide'); 80 } 81 }, 100); 82 }; 83 84 $tooltips.on('mouseenter focusin', function () { 85 wdtShowInteractiveTooltip(jQuery(this)); 86 }).on('mouseleave focusout', function () { 87 wdtScheduleHideInteractiveTooltip(jQuery(this)); 88 }); 89 90 jQuery(document).on('mouseenter focusin', '.tooltip', function () { 91 clearTimeout(wdtInteractiveTooltipHideTimeout); 92 }).on('mouseleave focusout', '.tooltip', function () { 93 wdtInteractiveTooltipHideTimeout = setTimeout(function () { 94 if (!jQuery('.tooltip').has(document.activeElement).length) { 95 jQuery('.wdt-tooltip-interactive[data-toggle="tooltip"]').wdtBootstrapTooltip('hide'); 96 } 97 }, 100); 98 }); 99 100 // Close the open interactive tooltip on Escape for keyboard users. 101 jQuery(document).on('keydown', function (event) { 102 if (event.key === 'Escape' || event.keyCode === 27) { 103 $tooltips.wdtBootstrapTooltip('hide'); 104 } 105 }); 106}; 107 108/** 109 * Extend jQuery to use our custom function for tooltip 110 */ 111jQuery.fn.wdtBootstrapTooltip = jQuery.fn.tooltip; 112 113/** 114 * Extend jQuery to use our custom function for popover 115 */ 116jQuery.fn.wdtBootstrapPopover = jQuery.fn.popover; 117 118/** 119 * Extend jQuery to use our custom function for tabs 120 */ 121jQuery.fn.wdtBootstrapTabs = jQuery.fn.tab; 122let lastClickedElement = null; 123jQuery(document).on('click', '*', function (e) { 124 lastClickedElement = this; 125}); 126/** 127 * Extend jQuery to use our custom function for selectpicker 128 */ 129jQuery.fn.wdtBootstrapSelectPicker= jQuery.fn.selectpicker; 130/** 131 * Extend jQuery to use AnimateCSS 132 */ 133jQuery.fn.extend({ 134 animateCss: function (animationName, onEnd) { 135 var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend'; 136 jQuery(this).addClass('animated ' + animationName).one(animationEnd, function () { 137 jQuery(this).removeClass('animated ' + animationName); 138 if (typeof onEnd == 'function') { 139 onEnd(); 140 } 141 }); 142 }, 143 fadeInDown: function () { 144 jQuery(this) 145 .removeClass('hidden') 146 .show() 147 .animateCss('fadeInDown'); 148 }, 149 fadeInRight: function (onEnd) { 150 jQuery(this) 151 .removeClass('hidden') 152 .show() 153 .animateCss('fadeInRight'); 154 if (typeof onEnd == 'function') { 155 onEnd(); 156 } 157 }, 158 fadeOutDown: function () { 159 var $this = jQuery(this); 160 jQuery(this).animateCss('fadeOutDown', function () { 161 $this 162 .addClass('hidden') 163 .hide(); 164 }); 165 }, 166 fadeOutRight: function () { 167 var $this = jQuery(this); 168 if (!jQuery(lastClickedElement).is('.wdt-apply,.wdt-column-apply, .wdt-apply *')) { 169 return; 170 } 171 jQuery(this).animateCss('fadeOutRight', function () { 172 $this 173 .addClass('hidden') 174 .hide(); 175 }); 176 }, 177 animateFadeIn: function () { 178 var $this = jQuery(this); 179 jQuery(this) 180 .removeClass('hidden') 181 .show() 182 .removeClass('fadeOut') 183 .animateCss('fadeIn', function () { 184 $this 185 .removeClass('fadeIn') 186 .removeClass('hidden') 187 .show() 188 }); 189 }, 190 animateFadeOut: function (onEnd) { 191 var $this = jQuery(this); 192 jQuery(this) 193 .removeClass('fadeIn') 194 .animateCss('fadeOut', function () { 195 $this 196 .addClass('hidden') 197 .removeClass('fadeOut') 198 .hide(); 199 if (typeof onEnd == 'function') { 200 onEnd(); 201 } 202 }); 203 } 204}); 205 206/** 207 * Helper method to insert at textarea cursor position 208 */ 209jQuery.fn.extend({ 210 insertAtCaret: function (myValue) { 211 return this.each(function (i) { 212 if (document.selection) { 213 //For browsers like Internet Explorer 214 this.focus(); 215 var sel = document.selection.createRange(); 216 sel.text = myValue; 217 this.focus(); 218 } else if (this.selectionStart || this.selectionStart == '0') { 219 //For browsers like Firefox and Webkit based 220 var startPos = this.selectionStart; 221 var endPos = this.selectionEnd; 222 var scrollTop = this.scrollTop; 223 this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length); 224 this.focus(); 225 this.selectionStart = startPos + myValue.length; 226 this.selectionEnd = startPos + myValue.length; 227 this.scrollTop = scrollTop; 228 } else { 229 this.value += myValue; 230 this.focus(); 231 } 232 }); 233 } 234}); 235 236(function ($) { 237 238 $(function () { 239 240 /** 241 * Show WordPress warnings before wpDataTables data 242 */ 243 $('.card-header:eq(0) > *').not('img, h2, ul.actions, button#wdt-table-id, .clear').prependTo('div.wdt-datatables-admin-wrap'); 244 245 246 /** 247 * Attach tooltips 248 */ 249 $('.wdt-datatables-admin-wrap [data-toggle="tooltip"]:not(.wdt-tooltip-interactive)').wdtBootstrapTooltip(); 250 251 wdtHideTooltip(); 252 wdtInteractiveTooltip(); 253 254 /**
255 * Attach HTML Popovers (Hints with images) 256 */ 257 $('[data-toggle="html-popover"]').wdtBootstrapPopover({ 258 html: true, 259 content: function () { 260 var content = $(this).attr("data-popover-content"); 261 return $(content).children(".popover-body").html(); 262 }, 263 title: function () { 264 var title = $(this).attr("data-popover-content"); 265 return $(title).children(".popover-heading").html(); 266 } 267 }); 268 269 /** 270 * Apply selectpicker 271 */ 272 $('select.selectpicker').wdtBootstrapSelectPicker(); 273 274 275 /** 276 * Hide modal dialog on Esc button 277 */ 278 $(document).on('keyup', '.modal', function (e) { 279 e.preventDefault(); 280 e.stopImmediatePropagation(); 281 if (e.which == 27) { 282 $('.modal').modal('hide'); 283 } 284 }); 285 286 $(document).on('click', 'button.wdt-backend-close', function () { 287 $('#wdt-backend-close-modal').modal('show'); 288 289 $('#wdt-backend-close-button').click(function () { 290 $(location).attr('href', wdtWpDataTablesPage.browseTablesUrl); 291 }); 292 }); 293 294 $(document).on('click', 'button.wdt-backend-chart-close', function () { 295 $('#wdt-backend-close-modal').modal('show'); 296 297 $('#wdt-backend-close-button').click(function () { 298 $(location).attr('href', wdtWpDataTablesPage.browseChartsUrl); 299 }); 300 }); 301 302 /** 303 * Get only text when copy shortcode from browse 304 */ 305 306 $('.wpdt-c').on('click', '.wdt-copy-shortcode-browse', function (e) { 307 e.preventDefault(); 308 e.stopImmediatePropagation(); 309 310 var $temp = $("<input>"); 311 $($temp).insertAfter($(this)); 312 $temp.val($(this).data('shortcode')).select(); 313 document.execCommand("copy"); 314 $temp.remove(); 315 wdtNotify( 316 wpdatatables_edit_strings.success, 317 wpdatatables_edit_strings.shortcodeSaved, 318 'success' 319 ); 320 }); 321 322 /** 323 * Logic for plus and minus button on number input field 324 */ 325 $('.wdt-btn-number').on("click", function (e) { 326 e.preventDefault(); 327 328 var fieldName = $(this).attr('data-field'); 329 var type = $(this).attr('data-type'); 330 var input = $("input[name='" + fieldName + "']"); 331 var currentVal = parseInt(input.val()); 332 if (!isNaN(currentVal)) { 333 if (type == 'minus') { 334 335 if (currentVal > input.attr('min')) { 336 input.val(currentVal - 1).change(); 337 } 338 if (parseInt(input.val()) == input.attr('min')) { 339 $(this).attr('disabled', true); 340 } 341 342 } else if (type == 'plus') { 343 input.val(currentVal + 1).change(); 344 $('.wdt-button-minus').attr('disabled', false); 345 } 346 } else { 347 input.val(1).change(); 348 } 349 }); 350 $(".input-number").on("change", function (e) { 351 var inputValue = $(this).val(); 352 if (isNaN(inputValue)) { 353 e.preventDefault(); 354 } 355 }); 356 357 /** 358 * Get only text when copy shortcode 359 */ 360 $('.wpdt-c').on('click', '.wdt-copy-shortcode', function (e) { 361 e.preventDefault(); 362 e.stopImmediatePropagation(); 363 364 var $temp = $("<input>"); 365 var $shortcodeType = $(this).data('shortcode-type'); 366 $($temp).insertAfter($(this)); 367 $temp.val($('#wdt-' + $shortcodeType + '-shortcode-id').text()).select(); 368 document.execCommand("copy"); 369 $temp.remove(); 370 wdtNotify( 371 wpdatatables_edit_strings.success, 372 wpdatatables_edit_strings.shortcodeSaved, 373 'success' 374 ); 375 }); 376 377 /** 378 * Input underline animations 379 */ 380 $(".collapse")[0] && ($(".collapse").on("show.bs.collapse", function (e) { 381 $(this).closest(".panel").find(".panel-heading").addClass("active") 382 }), $(".collapse").on("hide.bs.collapse", function (e) { 383 $(this).closest(".panel").find(".panel-heading").removeClass("active") 384 }), $(".collapse.in").each(function () { 385 $(this).closest(".panel").find(".panel-heading").addClass("active") 386 })); 387 388 $(".fg-line")[0] && ($("body").on("focus", ".fg-line .form-control:not(.bootstrap-select)", function () { 389 $(this).closest(".fg-line").addClass("fg-toggled") 390 })); 391 392 $("body").on("blur", ".form-control", function () { 393 var p = $(this).closest(".form-group, .input-group") 394 , i = p.find(".form-control").val(); 395 p.hasClass("fg-float") ? 0 == i.length && $(this).closest(".fg-line").removeClass("fg-toggled") : $(this).closest(".fg-line").removeClass("fg-toggled") 396 }); 397 398 $(".fg-float")[0] && $(".fg-float .form-control").each(function () { 399 var i = $(this).val(); 400 0 == !i.length && $(this).closest(".fg-line").addClass("fg-toggled") 401 }); 402 403 /** 404 * Popover for select tables 405 */ 406 407 $('[data-toggle="html-premium-popover"]').wdtBootstrapPopover(popoverOptions).on('shown.bs.popover', function () { 408 var $popup = $(this); 409 $(this).next('.popover').find('button.closePopover').click(function (e) { 410 $popup.wdtBootstrapPopover('hide'); 411 }); 412 $('#wdt-premium-compare-link').click(function () { 413 $(location).attr('href', wdtWpDataTablesPage.liteVSPremiumUrl); 414 }); 415 }); 416 417 418 /** 419 * Close Popover on Esc 420 */ 421 $(document).keyup(function (event) { 422 if (event.which === 27) { 423 $('[data-toggle="html-premium-popover"]').wdtBootstrapPopover('hide'); 424 $('[data-toggle="html-button-premium-popover"]').wdtBootstrapPopover('hide'); 425 $('[data-toggle="html-checkbox-premium-popover"]').wdtBootstrapPopover('hide'); 426 $('[data-toggle="html-input-premium-popover"]').wdtBootstrapPopover('hide'); 427 } 428 }); 429 430 /** 431 * Popover for checkboxes 432 */ 433 $('[data-toggle="html-checkbox-premium-popover"]').wdtBootstrapPopover('destroy').wdtBootstrapPopover(popoverOptions).on('shown.bs.popover', function () { 434 var $popup = $(this); 435 $popup.next('.popover').find('button.closePopover').click(function (e) { 436 $popup.wdtBootstrapPopover('hide'); 437 $popup.siblings('input[type="checkbox"].wdt-premium-feature').prop("checked", false); 438 }); 439 $('#wdt-premium-compare-link').click(function () { 440 $(location).attr('href', wdtWpDataTablesPage.liteVSPremiumUrl); 441 }); 442 $popup.children('span').removeClass("opacity-6"); 443 }).on('hidden.bs.popover', function () { 444 var elementInput = $(this); 445 var elementSpan = $(this).children('span'); 446 if (elementSpan.hasClass('opacity-6')) { 447 elementSpan.removeClass('opacity-6'); 448 } else { 449 elementSpan.addClass('opacity-6'); 450 } 451 elementInput.siblings('input[type="checkbox"].wdt-premium-feature').prop("checked", false); 452 }) 453 454 /** 455 * Popover for input fields 456 */ 457 $('[data-toggle="html-input-premium-popover"]').on('focus', function () { 458 $('[data-toggle="html-input-premium-popover"]').wdtBootstrapPopover(popoverOptions).on('shown.bs.popover', function () { 459 var $popup = $(this); 460 $(this).next('.popover').find('button.closePopover').click(function (e) { 461 $popup.wdtBootstrapPopover('hide'); 462 }); 463 $('#wdt-premium-compare-link').click(function () { 464 $(location).attr('href', wdtWpDataTablesPage.liteVSPremiumUrl); 465 }); 466 }) 467 if ($('.wdt-premium-popover').is(':visible')) { 468 $(this).wdtBootstrapPopover('hide'); 469 } else { 470 $(this).wdtBootstrapPopover('show'); 471 } 472 473 }); 474 475 /** 476 * Popover for select elements(some options) 477 */ 478 $('#wdt-column-values, #wdt-chart-row-range-type').on("change", function () { 479 if ($(this).val() == 'foreignkey' || 480 $(this).val() == 'list' || 481 $(this).val() == 'pick_rows') {
482 popoverOnChange(this.id) 483 } else { 484 $('#' + this.id).wdtBootstrapPopover('destroy'); 485 } 486 }); 487 488 489 490 /** 491 * Popover for select elements(all options) 492 */ 493 $('#wp-render-filter, #wdt-possible-values-ajax, #horizontal-axis-direction, #vertical-axis-direction, #wdt-column-rotate-header-name').on("change", function () { 494 popoverOnChange(this.id) 495 }); 496 497 /** 498 * Popover for buttons 499 */ 500 $('[data-toggle="html-button-premium-popover"]').on("click", function (e) { 501 e.stopImmediatePropagation() 502 e.preventDefault() 503 popoverOnClick() 504 }); 505 506 $('#wpdt-views .nav-item').on('click', function (e) { 507 e.preventDefault() 508 e.stopImmediatePropagation() 509 var view = $(this).data('view'), 510 viewContainer = $('#wpdt-view-container'); 511 if (wpdatatable_config.table_type == 'simple') { 512 switch (view) { 513 case 'desktop': 514 viewContainer.width('100%'); 515 $('.wpDataTableContainerSimpleTable .wpdtSimpleTable').each(function (i) { 516 var tempID = '#' + $(this)[i].id; 517 if (wpdatatable_config.simpleResponsive) { 518 if ($(tempID).data('basictable')) 519 $(tempID).basictable('destroy'); 520 } 521 }) 522 break; 523 case 'tablet': 524 viewContainer.width('1024').css('margin', '0 auto'); 525 if (wpdatatable_config.simpleResponsive) { 526 $('.wpDataTableContainerSimpleTable .wpdtSimpleTable').each(function (i) { 527 var tempID = '#' + $(this)[i].id; 528 $(tempID).basictable({ 529 containerBreakpoint: 1024, 530 tableWrap: true, 531 header: !!$(tempID + ' thead').length 532 }); 533 }) 534 } 535 break; 536 case 'mobile': 537 viewContainer.width('400').css('margin', '0 auto'); 538 if (wpdatatable_config.simpleResponsive) { 539 $('.wpDataTableContainerSimpleTable .wpdtSimpleTable').each(function (i) { 540 var tempID = '#' + $(this)[i].id; 541 $(tempID).basictable({ 542 containerBreakpoint: 400, 543 tableWrap: true, 544 showEmptyCells: true, 545 header: !!$(tempID + ' thead').length 546 }); 547 }) 548 } 549 break; 550 default: 551 viewContainer.width('100%'); 552 break; 553 } 554 } 555 556 $(this).addClass('active').siblings().removeClass('active'); 557 }) 558 }); 559 560})(jQuery); 561 562/** 563 * Hide preloader on window load 564 */ 565jQuery(window).on('load', function () { 566 jQuery('.wdt-preload-layer').animateFadeOut(); 567}); 568 569/** 570 * Show preloader before leaving the page 571 */ 572window.onbeforeunload = function (e) { 573 jQuery('.wdt-preload-layer').animateFadeIn(); 574}; 575 576/** 577 * Popover function for select options 578 */ 579function popoverOnChange(selector) { 580 jQuery("#" + selector).wdtBootstrapPopover(popoverOptions).on('shown.bs.popover', function () { 581 var $popup = jQuery(this); 582 jQuery(this).next('.popover').find('button.closePopover').click(function (e) { 583 $popup.wdtBootstrapPopover('hide'); 584 }); 585 jQuery('#wdt-premium-compare-link').click(function () { 586 jQuery(location).attr('href', wdtWpDataTablesPage.liteVSPremiumUrl); 587 }); 588 }).wdtBootstrapPopover('show'); 589} 590 591/** 592 * Popover for button click 593 */ 594function popoverOnClick() { 595 jQuery('[data-toggle="html-button-premium-popover"]').wdtBootstrapPopover(popoverOptions).on('shown.bs.popover', function () { 596 var $popup = jQuery(this); 597 jQuery(this).next('.popover').find('button.closePopover').click(function (e) { 598 $popup.wdtBootstrapPopover('hide'); 599 });
600 jQuery('#wdt-premium-compare-link').click(function () { 601 jQuery(location).attr('href', wdtWpDataTablesPage.liteVSPremiumUrl); 602 }); 603 }).wdtBootstrapPopover('toggle'); 604} 605 606/** 607 * Growl notification in the right top corner 608 * @param title 609 * @param message 610 * @param type 611 */ 612function wdtNotify(title, message, type) { 613 614 if (typeof title == 'undefined') { 615 title = 'info'; 616 } 617 if (typeof message == 'undefined') { 618 message = 'info'; 619 } 620 if (typeof type == 'undefined') { 621 type = 'info'; 622 } 623 624 switch (type) { 625 case 'danger': 626 icon = 'wpdt-icon-exclamation-triangle'; 627 break; 628 case 'success': 629 default: 630 icon = 'wpdt-icon-check-circle-full'; 631 break; 632 } 633 634 jQuery.growl({ 635 icon: icon, 636 title: ' ' + title + ' ', 637 message: message, 638 url: '' 639 }, { 640 element: 'body', 641 type: type, 642 allow_dismiss: true, 643 placement: { 644 from: 'top', 645 align: 'right' 646 }, 647 offset: { 648 x: 20, 649 y: 40 650 }, 651 spacing: 10, 652 z_index: 100002, 653 delay: 2500, 654 timer: 1000, 655 url_target: '_blank', 656 mouse_over: false, 657 animate: { 658 enter: 'animated fadeIn', 659 exit: 'animated fadeOut' 660 }, 661 icon_type: 'class', 662 template: '<div data-growl="container" class="wpdt-c alert" role="alert">' + 663 '<button type="button" class="close" data-growl="dismiss">' + 664 '<span aria-hidden="true">×</span>' + 665 '<span class="sr-only">' + wpdatatables_edit_strings.close + '</span>' + 666 '</button>' + 667 '<span data-growl="icon"></span>' + 668 '<span data-growl="title"></span>' + 669 '<span data-growl="message"></span>' + 670 '<a href="#" data-growl="url"></a>' + 671 '</div>' 672 }); 673}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.