From f4efdc765ac530f4d94bd90a60bd1822c12bed66 Mon Sep 17 00:00:00 2001 From: Elijah Madden Date: Tue, 24 Jul 2018 17:39:36 +0900 Subject: [PATCH] Fix errors exposed by strict (#12544) * Don't need a closure, jQuery ready function is enough. Use strict. * Fix errors exposed by strict. Undeclared variables. wrong regex backreference * Fix some undeclared/wrongly declared variables * Avoid some redundant calls of $(this) * Remove some redundant (also wrong) code. --- administrator/templates/isis/js/template.js | 636 ++++++++++---------- 1 file changed, 323 insertions(+), 313 deletions(-) diff --git a/administrator/templates/isis/js/template.js b/administrator/templates/isis/js/template.js index 8a61f1a126269..d42adc59a8269 100644 --- a/administrator/templates/isis/js/template.js +++ b/administrator/templates/isis/js/template.js @@ -6,404 +6,414 @@ * @since 3.0 */ -(function($) +jQuery(function($) { - $(document).ready(function() - { - var $w = $(window); + 'use strict'; - $('*[rel=tooltip]').tooltip(); + var $w = $(window); - // Turn radios into btn-group - $('.radio.btn-group label').addClass('btn'); + $('*[rel=tooltip]').tooltip(); - $('.btn-group label:not(.active)').click(function() - { - var label = $(this); - var input = $('#' + label.attr('for')); + // Turn radios into btn-group + $('.radio.btn-group label').addClass('btn'); - if (!input.prop('checked')) - { - label.closest('.btn-group').find('label').removeClass('active btn-success btn-danger btn-primary'); + $('fieldset.btn-group').each(function() { + // Handle disabled, prevent clicks on the container, and add disabled style to each button + if ($(this).prop('disabled')) { + $(this).css('pointer-events', 'none').off('click'); + $(this).find('.btn').addClass('disabled'); + } + }); - if (label.closest('.btn-group').hasClass('btn-group-reversed')) - { - if (input.val() == '') - { - label.addClass('active btn-primary'); - } - else if (input.val() == 0) - { - label.addClass('active btn-success'); - } - else - { - label.addClass('active btn-danger'); - } - } - else - { - if (input.val() == '') - { - label.addClass('active btn-primary'); - } - else if (input.val() == 0) - { - label.addClass('active btn-danger'); - } - else - { - label.addClass('active btn-success'); - } + $('.btn-group label:not(.active)').click(function() + { + var label = $(this); + var input = $('#' + label.attr('for')); - } - input.prop('checked', true); - input.trigger('change'); - } - }); - $('.btn-group input[checked=checked]').each(function() + if (!input.prop('checked')) { - var $self = $(this); - var attrId = $self.attr('id'); + label.closest('.btn-group').find('label').removeClass('active btn-success btn-danger btn-primary'); - if ($self.parent().hasClass('btn-group-reversed')) + if (label.closest('.btn-group').hasClass('btn-group-reversed')) { - if ($self.val() == '') + if (input.val() == '') { - $('label[for=' + attrId + ']').addClass('active btn-primary'); + label.addClass('active btn-primary'); } - else if ($self.val() == 0) + else if (input.val() == 0) { - $('label[for=' + attrId + ']').addClass('active btn-success'); + label.addClass('active btn-success'); } else { - $('label[for=' + attrId + ']').addClass('active btn-danger'); + label.addClass('active btn-danger'); } } else { - if ($self.val() == '') + if (input.val() == '') { - $('label[for=' + attrId + ']').addClass('active btn-primary'); + label.addClass('active btn-primary'); } - else if ($self.val() == 0) + else if (input.val() == 0) { - $('label[for=' + attrId + ']').addClass('active btn-danger'); + label.addClass('active btn-danger'); } else { - $('label[for=' + attrId + ']').addClass('active btn-success'); + label.addClass('active btn-success'); } + } - }); - // add color classes to chosen field based on value - $('select[class^="chzn-color"], select[class*=" chzn-color"]').on('liszt:ready', function(){ - var select = $(this); - var cls = this.className.replace(/^.(chzn-color[a-z0-9-_]*)$.*/, '\1'); - var container = select.next('.chzn-container').find('.chzn-single'); - container.addClass(cls).attr('rel', 'value_' + select.val()); - select.on('change click', function() + input.prop('checked', true); + input.trigger('change'); + } + }); + $('.btn-group input[checked=checked]').each(function() + { + var $self = $(this); + var attrId = $self.attr('id'); + + if ($self.parent().hasClass('btn-group-reversed')) + { + if ($self.val() == '') { - container.attr('rel', 'value_' + select.val()); - }); + $('label[for=' + attrId + ']').addClass('active btn-primary'); + } + else if ($self.val() == 0) + { + $('label[for=' + attrId + ']').addClass('active btn-success'); + } + else + { + $('label[for=' + attrId + ']').addClass('active btn-danger'); + } + } + else + { + if ($self.val() == '') + { + $('label[for=' + attrId + ']').addClass('active btn-primary'); + } + else if ($self.val() == 0) + { + $('label[for=' + attrId + ']').addClass('active btn-danger'); + } + else + { + $('label[for=' + attrId + ']').addClass('active btn-success'); + } + } + }); + // add color classes to chosen field based on value + $('select[class^="chzn-color"], select[class*=" chzn-color"]').on('liszt:ready', function(){ + var select = $(this); + var cls = this.className.replace(/^.(chzn-color[a-z0-9-_]*)$.*/, '$1'); + var container = select.next('.chzn-container').find('.chzn-single'); + container.addClass(cls).attr('rel', 'value_' + select.val()); + select.on('change click', function() + { + container.attr('rel', 'value_' + select.val()); }); - /** - * Append submenu items to empty UL on hover allowing a scrollable dropdown - */ - if ($w.width() > 767) - { - var menuScroll = $('#menu > li > ul'), - emptyMenu = $('#nav-empty'); - - $('#menu > li').on('click mouseenter', function() { - - // Set max-height (and width if scroll) for dropdown menu, depending of window height - var $dropdownMenu = $(this).children('ul'), - windowHeight = $w.height(), - linkHeight = $(this).outerHeight(true), - statusHeight = $('#status').outerHeight(true), - menuHeight = $dropdownMenu.height(), - menuOuterHeight = $dropdownMenu.outerHeight(true), - scrollMenuWidth = $dropdownMenu.width() + 15, - maxHeight = windowHeight - (linkHeight + statusHeight + (menuOuterHeight - menuHeight) + 20); - - if (maxHeight < menuHeight) - { - $dropdownMenu.css('width', scrollMenuWidth); - } - else if (maxHeight > menuHeight) - { - $dropdownMenu.css('width', 'auto'); - } + }); - $dropdownMenu.css('max-height', maxHeight); + /** + * Append submenu items to empty UL on hover allowing a scrollable dropdown + */ + if ($w.width() > 767) + { + var menuScroll = $('#menu > li > ul'), + emptyMenu = $('#nav-empty'), + linkWidth, + menuWidth, + offsetLeft; + + $('#menu > li').on('click mouseenter', function() { + + // Set max-height (and width if scroll) for dropdown menu, depending of window height + var $self = $(this), + $dropdownMenu = $self.children('ul'), + windowHeight = $w.height(), + linkHeight = $self.outerHeight(true), + statusHeight = $('#status').outerHeight(true), + menuHeight = $dropdownMenu.height(), + menuOuterHeight = $dropdownMenu.outerHeight(true), + scrollMenuWidth = $dropdownMenu.width() + 15, + maxHeight = windowHeight - (linkHeight + statusHeight + (menuOuterHeight - menuHeight) + 20), + linkPaddingLeft = $self.children('a').css('padding-left'); + + if (maxHeight < menuHeight) + { + $dropdownMenu.css('width', scrollMenuWidth); + } + else if (maxHeight > menuHeight) + { + $dropdownMenu.css('width', 'auto'); + } - // Get the submenu position - linkWidth = $(this).outerWidth(true); - menuWidth = $dropdownMenu.width(); - linkPaddingLeft = $(this).children('a').css('padding-left'); - offsetLeft = Math.round($(this).offset().left) - parseInt(linkPaddingLeft); + $dropdownMenu.css('max-height', maxHeight); - emptyMenu.empty().hide(); + // Get the submenu position + linkWidth = $self.outerWidth(true); + menuWidth = $dropdownMenu.width(); + offsetLeft = Math.round($self.offset().left) - parseInt(linkPaddingLeft); - }); + emptyMenu.empty().hide(); - menuScroll.find('.dropdown-submenu > a').on('mouseover', function() { + }); - var $self = $(this), - dropdown = $self.next('ul'), - submenuWidth = dropdown.outerWidth(), - offsetTop = $self.offset().top, - linkPaddingTop = parseInt(dropdown.css('padding-top')) + parseInt($(this).css('padding-top')), - scroll = $w.scrollTop() + linkPaddingTop; + menuScroll.find('.dropdown-submenu > a').on('mouseover', function() { - // Set the submenu position - if ($('html').attr('dir') == 'rtl') - { - emptyMenu.css({ - top : offsetTop - scroll, - left: offsetLeft - (menuWidth - linkWidth) - submenuWidth - }); - } - else - { - emptyMenu.css({ - top : offsetTop - scroll, - left: offsetLeft + menuWidth - }); - } + var $self = $(this), + dropdown = $self.next('ul'), + submenuWidth = dropdown.outerWidth(), + offsetTop = $self.offset().top, + linkPaddingTop = parseInt(dropdown.css('padding-top')) + parseInt($self.css('padding-top')), + scroll = $w.scrollTop() + linkPaddingTop; - // Append items to empty