Cache selectors when possible. (#2790)

pull/2795/head
XhmikosR 2020-06-04 21:06:38 +03:00 committed by GitHub
parent 1fbda275a9
commit 5a1ae0c35e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 121 additions and 79 deletions

View File

@ -116,13 +116,15 @@ class CardWidget {
width: this._parent.width(), width: this._parent.width(),
transition: 'all .15s' transition: 'all .15s'
}).delay(150).queue(function () { }).delay(150).queue(function () {
$(this).addClass(ClassName.MAXIMIZED) const $element = $(this)
$element.addClass(ClassName.MAXIMIZED)
$('html').addClass(ClassName.MAXIMIZED) $('html').addClass(ClassName.MAXIMIZED)
if ($(this).hasClass(ClassName.COLLAPSED)) { if ($element.hasClass(ClassName.COLLAPSED)) {
$(this).addClass(ClassName.WAS_COLLAPSED) $element.addClass(ClassName.WAS_COLLAPSED)
} }
$(this).dequeue() $element.dequeue()
}) })
this._element.trigger($.Event(Event.MAXIMIZED), this._parent) this._element.trigger($.Event(Event.MAXIMIZED), this._parent)
@ -135,17 +137,19 @@ class CardWidget {
this._parent.css('cssText', 'height:' + this._parent[0].style.height + ' !important;' + this._parent.css('cssText', 'height:' + this._parent[0].style.height + ' !important;' +
'width:' + this._parent[0].style.width + ' !important; transition: all .15s;' 'width:' + this._parent[0].style.width + ' !important; transition: all .15s;'
).delay(10).queue(function () { ).delay(10).queue(function () {
$(this).removeClass(ClassName.MAXIMIZED) const $element = $(this)
$element.removeClass(ClassName.MAXIMIZED)
$('html').removeClass(ClassName.MAXIMIZED) $('html').removeClass(ClassName.MAXIMIZED)
$(this).css({ $element.css({
height: 'inherit', height: 'inherit',
width: 'inherit' width: 'inherit'
}) })
if ($(this).hasClass(ClassName.WAS_COLLAPSED)) { if ($element.hasClass(ClassName.WAS_COLLAPSED)) {
$(this).removeClass(ClassName.WAS_COLLAPSED) $element.removeClass(ClassName.WAS_COLLAPSED)
} }
$(this).dequeue() $element.dequeue()
}) })
this._element.trigger($.Event(Event.MINIMIZED), this._parent) this._element.trigger($.Event(Event.MINIMIZED), this._parent)

View File

@ -69,42 +69,50 @@ class ControlSidebar {
// Public // Public
collapse() { collapse() {
const $body = $('body')
const $html = $('html')
// Show the control sidebar // Show the control sidebar
if (this._config.controlsidebarSlide) { if (this._config.controlsidebarSlide) {
$('html').addClass(ClassName.CONTROL_SIDEBAR_ANIMATE) $html.addClass(ClassName.CONTROL_SIDEBAR_ANIMATE)
$('body').removeClass(ClassName.CONTROL_SIDEBAR_SLIDE).delay(300).queue(function () { $body.removeClass(ClassName.CONTROL_SIDEBAR_SLIDE).delay(300).queue(function () {
$(Selector.CONTROL_SIDEBAR).hide() $(Selector.CONTROL_SIDEBAR).hide()
$('html').removeClass(ClassName.CONTROL_SIDEBAR_ANIMATE) $html.removeClass(ClassName.CONTROL_SIDEBAR_ANIMATE)
$(this).dequeue() $(this).dequeue()
}) })
} else { } else {
$('body').removeClass(ClassName.CONTROL_SIDEBAR_OPEN) $body.removeClass(ClassName.CONTROL_SIDEBAR_OPEN)
} }
$(this._element).trigger($.Event(Event.COLLAPSED)) $(this._element).trigger($.Event(Event.COLLAPSED))
} }
show() { show() {
const $body = $('body')
const $html = $('html')
// Collapse the control sidebar // Collapse the control sidebar
if (this._config.controlsidebarSlide) { if (this._config.controlsidebarSlide) {
$('html').addClass(ClassName.CONTROL_SIDEBAR_ANIMATE) $html.addClass(ClassName.CONTROL_SIDEBAR_ANIMATE)
$(Selector.CONTROL_SIDEBAR).show().delay(10).queue(function () { $(Selector.CONTROL_SIDEBAR).show().delay(10).queue(function () {
$('body').addClass(ClassName.CONTROL_SIDEBAR_SLIDE).delay(300).queue(function () { $body.addClass(ClassName.CONTROL_SIDEBAR_SLIDE).delay(300).queue(function () {
$('html').removeClass(ClassName.CONTROL_SIDEBAR_ANIMATE) $html.removeClass(ClassName.CONTROL_SIDEBAR_ANIMATE)
$(this).dequeue() $(this).dequeue()
}) })
$(this).dequeue() $(this).dequeue()
}) })
} else { } else {
$('body').addClass(ClassName.CONTROL_SIDEBAR_OPEN) $body.addClass(ClassName.CONTROL_SIDEBAR_OPEN)
} }
$(this._element).trigger($.Event(Event.EXPANDED)) $(this._element).trigger($.Event(Event.EXPANDED))
} }
toggle() { toggle() {
const shouldClose = $('body').hasClass(ClassName.CONTROL_SIDEBAR_OPEN) || $('body') const $body = $('body')
.hasClass(ClassName.CONTROL_SIDEBAR_SLIDE) const shouldClose = $body.hasClass(ClassName.CONTROL_SIDEBAR_OPEN) ||
$body.hasClass(ClassName.CONTROL_SIDEBAR_SLIDE)
if (shouldClose) { if (shouldClose) {
// Close the control sidebar // Close the control sidebar
this.collapse() this.collapse()
@ -126,14 +134,20 @@ class ControlSidebar {
}) })
$(window).scroll(() => { $(window).scroll(() => {
if ($('body').hasClass(ClassName.CONTROL_SIDEBAR_OPEN) || $('body').hasClass(ClassName.CONTROL_SIDEBAR_SLIDE)) { const $body = $('body')
const shouldClose = $body.hasClass(ClassName.CONTROL_SIDEBAR_OPEN) ||
$body.hasClass(ClassName.CONTROL_SIDEBAR_SLIDE)
if (shouldClose) {
this._fixScrollHeight() this._fixScrollHeight()
} }
}) })
} }
_fixScrollHeight() { _fixScrollHeight() {
if (!$('body').hasClass(ClassName.LAYOUT_FIXED)) { const $body = $('body')
if (!$body.hasClass(ClassName.LAYOUT_FIXED)) {
return return
} }
@ -152,11 +166,11 @@ class ControlSidebar {
let footerFixed = false let footerFixed = false
if ( if (
$('body').hasClass(ClassName.NAVBAR_FIXED) || $body.hasClass(ClassName.NAVBAR_FIXED) ||
$('body').hasClass(ClassName.NAVBAR_SM_FIXED) || $body.hasClass(ClassName.NAVBAR_SM_FIXED) ||
$('body').hasClass(ClassName.NAVBAR_MD_FIXED) || $body.hasClass(ClassName.NAVBAR_MD_FIXED) ||
$('body').hasClass(ClassName.NAVBAR_LG_FIXED) || $body.hasClass(ClassName.NAVBAR_LG_FIXED) ||
$('body').hasClass(ClassName.NAVBAR_XL_FIXED) $body.hasClass(ClassName.NAVBAR_XL_FIXED)
) { ) {
if ($(Selector.HEADER).css('position') === 'fixed') { if ($(Selector.HEADER).css('position') === 'fixed') {
navbarFixed = true navbarFixed = true
@ -164,47 +178,52 @@ class ControlSidebar {
} }
if ( if (
$('body').hasClass(ClassName.FOOTER_FIXED) || $body.hasClass(ClassName.FOOTER_FIXED) ||
$('body').hasClass(ClassName.FOOTER_SM_FIXED) || $body.hasClass(ClassName.FOOTER_SM_FIXED) ||
$('body').hasClass(ClassName.FOOTER_MD_FIXED) || $body.hasClass(ClassName.FOOTER_MD_FIXED) ||
$('body').hasClass(ClassName.FOOTER_LG_FIXED) || $body.hasClass(ClassName.FOOTER_LG_FIXED) ||
$('body').hasClass(ClassName.FOOTER_XL_FIXED) $body.hasClass(ClassName.FOOTER_XL_FIXED)
) { ) {
if ($(Selector.FOOTER).css('position') === 'fixed') { if ($(Selector.FOOTER).css('position') === 'fixed') {
footerFixed = true footerFixed = true
} }
} }
const $controlSidebar = $(Selector.CONTROL_SIDEBAR)
const $controlsidebarContent = $(Selector.CONTROL_SIDEBAR + ', ' + Selector.CONTROL_SIDEBAR + ' ' + Selector.CONTROL_SIDEBAR_CONTENT)
if (positions.top === 0 && positions.bottom === 0) { if (positions.top === 0 && positions.bottom === 0) {
$(Selector.CONTROL_SIDEBAR).css({ $controlSidebar.css({
bottom: heights.footer, bottom: heights.footer,
top: heights.header top: heights.header
}) })
$(Selector.CONTROL_SIDEBAR + ', ' + Selector.CONTROL_SIDEBAR + ' ' + Selector.CONTROL_SIDEBAR_CONTENT).css('height', heights.window - (heights.header + heights.footer)) $controlsidebarContent.css('height', heights.window - (heights.header + heights.footer))
} else if (positions.bottom <= heights.footer) { } else if (positions.bottom <= heights.footer) {
if (footerFixed === false) { if (footerFixed === false) {
$(Selector.CONTROL_SIDEBAR).css('bottom', heights.footer - positions.bottom) $controlSidebar.css('bottom', heights.footer - positions.bottom)
$(Selector.CONTROL_SIDEBAR + ', ' + Selector.CONTROL_SIDEBAR + ' ' + Selector.CONTROL_SIDEBAR_CONTENT).css('height', heights.window - (heights.footer - positions.bottom)) $controlsidebarContent.css('height', heights.window - (heights.footer - positions.bottom))
} else { } else {
$(Selector.CONTROL_SIDEBAR).css('bottom', heights.footer) $controlSidebar.css('bottom', heights.footer)
} }
} else if (positions.top <= heights.header) { } else if (positions.top <= heights.header) {
if (navbarFixed === false) { if (navbarFixed === false) {
$(Selector.CONTROL_SIDEBAR).css('top', heights.header - positions.top) $controlSidebar.css('top', heights.header - positions.top)
$(Selector.CONTROL_SIDEBAR + ', ' + Selector.CONTROL_SIDEBAR + ' ' + Selector.CONTROL_SIDEBAR_CONTENT).css('height', heights.window - (heights.header - positions.top)) $controlsidebarContent.css('height', heights.window - (heights.header - positions.top))
} else { } else {
$(Selector.CONTROL_SIDEBAR).css('top', heights.header) $controlSidebar.css('top', heights.header)
} }
} else if (navbarFixed === false) { } else if (navbarFixed === false) {
$(Selector.CONTROL_SIDEBAR).css('top', 0) $controlSidebar.css('top', 0)
$(Selector.CONTROL_SIDEBAR + ', ' + Selector.CONTROL_SIDEBAR + ' ' + Selector.CONTROL_SIDEBAR_CONTENT).css('height', heights.window) $controlsidebarContent.css('height', heights.window)
} else { } else {
$(Selector.CONTROL_SIDEBAR).css('top', heights.header) $controlSidebar.css('top', heights.header)
} }
} }
_fixHeight() { _fixHeight() {
if (!$('body').hasClass(ClassName.LAYOUT_FIXED)) { const $body = $('body')
if (!$body.hasClass(ClassName.LAYOUT_FIXED)) {
return return
} }
@ -217,21 +236,22 @@ class ControlSidebar {
let sidebarHeight = heights.window - heights.header let sidebarHeight = heights.window - heights.header
if ( if (
$('body').hasClass(ClassName.FOOTER_FIXED) || $body.hasClass(ClassName.FOOTER_FIXED) ||
$('body').hasClass(ClassName.FOOTER_SM_FIXED) || $body.hasClass(ClassName.FOOTER_SM_FIXED) ||
$('body').hasClass(ClassName.FOOTER_MD_FIXED) || $body.hasClass(ClassName.FOOTER_MD_FIXED) ||
$('body').hasClass(ClassName.FOOTER_LG_FIXED) || $body.hasClass(ClassName.FOOTER_LG_FIXED) ||
$('body').hasClass(ClassName.FOOTER_XL_FIXED) $body.hasClass(ClassName.FOOTER_XL_FIXED)
) { ) {
if ($(Selector.FOOTER).css('position') === 'fixed') { if ($(Selector.FOOTER).css('position') === 'fixed') {
sidebarHeight = heights.window - heights.header - heights.footer sidebarHeight = heights.window - heights.header - heights.footer
} }
} }
$(Selector.CONTROL_SIDEBAR + ' ' + Selector.CONTROL_SIDEBAR_CONTENT).css('height', sidebarHeight) const $controlSidebar = $(Selector.CONTROL_SIDEBAR + ' ' + Selector.CONTROL_SIDEBAR_CONTENT)
$controlSidebar.css('height', sidebarHeight)
if (typeof $.fn.overlayScrollbars !== 'undefined') { if (typeof $.fn.overlayScrollbars !== 'undefined') {
$(Selector.CONTROL_SIDEBAR + ' ' + Selector.CONTROL_SIDEBAR_CONTENT).overlayScrollbars({ $controlSidebar.overlayScrollbars({
className: this._config.scrollbarTheme, className: this._config.scrollbarTheme,
sizeAutoCapable: true, sizeAutoCapable: true,
scrollbars: { scrollbars: {

View File

@ -59,9 +59,10 @@ class Layout {
// Public // Public
fixLayoutHeight(extra = null) { fixLayoutHeight(extra = null) {
const $body = $('body')
let controlSidebar = 0 let controlSidebar = 0
if ($('body').hasClass(ClassName.CONTROL_SIDEBAR_SLIDE_OPEN) || $('body').hasClass(ClassName.CONTROL_SIDEBAR_OPEN) || extra === 'control_sidebar') { if ($body.hasClass(ClassName.CONTROL_SIDEBAR_SLIDE_OPEN) || $body.hasClass(ClassName.CONTROL_SIDEBAR_OPEN) || extra === 'control_sidebar') {
controlSidebar = $(Selector.CONTROL_SIDEBAR_CONTENT).height() controlSidebar = $(Selector.CONTROL_SIDEBAR_CONTENT).height()
} }
@ -80,26 +81,28 @@ class Layout {
offset = 0 offset = 0
} }
const $contentSelector = $(Selector.CONTENT)
if (offset !== false) { if (offset !== false) {
if (max === heights.controlSidebar) { if (max === heights.controlSidebar) {
$(Selector.CONTENT).css('min-height', (max + offset)) $contentSelector.css('min-height', (max + offset))
} else if (max === heights.window) { } else if (max === heights.window) {
$(Selector.CONTENT).css('min-height', (max + offset) - heights.header - heights.footer) $contentSelector.css('min-height', (max + offset) - heights.header - heights.footer)
} else { } else {
$(Selector.CONTENT).css('min-height', (max + offset) - heights.header) $contentSelector.css('min-height', (max + offset) - heights.header)
} }
if (this._isFooterFixed()) { if (this._isFooterFixed()) {
$(Selector.CONTENT).css('min-height', parseFloat($(Selector.CONTENT).css('min-height')) + heights.footer) $contentSelector.css('min-height', parseFloat($contentSelector.css('min-height')) + heights.footer)
} }
} }
if (!$('body').hasClass(ClassName.LAYOUT_FIXED)) { if (!$body.hasClass(ClassName.LAYOUT_FIXED)) {
return return
} }
if (offset !== false) { if (offset !== false) {
$(Selector.CONTENT).css('min-height', (max + offset) - heights.header - heights.footer) $contentSelector.css('min-height', (max + offset) - heights.header - heights.footer)
} }
if (typeof $.fn.overlayScrollbars !== 'undefined') { if (typeof $.fn.overlayScrollbars !== 'undefined') {
@ -115,13 +118,17 @@ class Layout {
} }
fixLoginRegisterHeight() { fixLoginRegisterHeight() {
if ($(Selector.LOGIN_BOX + ', ' + Selector.REGISTER_BOX).length === 0) { const $body = $('body')
$('body, html').css('height', 'auto') const $selector = $(Selector.LOGIN_BOX + ', ' + Selector.REGISTER_BOX)
} else {
const boxHeight = $(Selector.LOGIN_BOX + ', ' + Selector.REGISTER_BOX).height()
if ($('body').css('min-height') !== boxHeight) { if ($selector.length === 0) {
$('body').css('min-height', boxHeight) $body.css('height', 'auto')
$('html').css('height', 'auto')
} else {
const boxHeight = $selector.height()
if ($body.css('min-height') !== boxHeight) {
$body.css('min-height', boxHeight)
} }
} }
} }

View File

@ -62,13 +62,15 @@ class PushMenu {
// Public // Public
expand() { expand() {
const $bodySelector = $(Selector.BODY)
if (this._options.autoCollapseSize) { if (this._options.autoCollapseSize) {
if ($(window).width() <= this._options.autoCollapseSize) { if ($(window).width() <= this._options.autoCollapseSize) {
$(Selector.BODY).addClass(ClassName.OPEN) $bodySelector.addClass(ClassName.OPEN)
} }
} }
$(Selector.BODY).addClass(ClassName.IS_OPENING).removeClass(`${ClassName.COLLAPSED} ${ClassName.CLOSED}`) $bodySelector.addClass(ClassName.IS_OPENING).removeClass(`${ClassName.COLLAPSED} ${ClassName.CLOSED}`)
if (this._options.enableRemember) { if (this._options.enableRemember) {
localStorage.setItem(`remember${EVENT_KEY}`, ClassName.OPEN) localStorage.setItem(`remember${EVENT_KEY}`, ClassName.OPEN)
@ -78,13 +80,15 @@ class PushMenu {
} }
collapse() { collapse() {
const $bodySelector = $(Selector.BODY)
if (this._options.autoCollapseSize) { if (this._options.autoCollapseSize) {
if ($(window).width() <= this._options.autoCollapseSize) { if ($(window).width() <= this._options.autoCollapseSize) {
$(Selector.BODY).removeClass(ClassName.OPEN).addClass(ClassName.CLOSED) $bodySelector.removeClass(ClassName.OPEN).addClass(ClassName.CLOSED)
} }
} }
$(Selector.BODY).removeClass(ClassName.IS_OPENING).addClass(ClassName.COLLAPSED) $bodySelector.removeClass(ClassName.IS_OPENING).addClass(ClassName.COLLAPSED)
if (this._options.enableRemember) { if (this._options.enableRemember) {
localStorage.setItem(`remember${EVENT_KEY}`, ClassName.COLLAPSED) localStorage.setItem(`remember${EVENT_KEY}`, ClassName.COLLAPSED)
@ -106,14 +110,16 @@ class PushMenu {
return return
} }
const $bodySelector = $(Selector.BODY)
if ($(window).width() <= this._options.autoCollapseSize) { if ($(window).width() <= this._options.autoCollapseSize) {
if (!$(Selector.BODY).hasClass(ClassName.OPEN)) { if (!$bodySelector.hasClass(ClassName.OPEN)) {
this.collapse() this.collapse()
} }
} else if (resize === true) { } else if (resize === true) {
if ($(Selector.BODY).hasClass(ClassName.OPEN)) { if ($bodySelector.hasClass(ClassName.OPEN)) {
$(Selector.BODY).removeClass(ClassName.OPEN) $bodySelector.removeClass(ClassName.OPEN)
} else if ($(Selector.BODY).hasClass(ClassName.CLOSED)) { } else if ($bodySelector.hasClass(ClassName.CLOSED)) {
this.expand() this.expand()
} }
} }
@ -124,23 +130,25 @@ class PushMenu {
return return
} }
const $body = $('body')
const toggleState = localStorage.getItem(`remember${EVENT_KEY}`) const toggleState = localStorage.getItem(`remember${EVENT_KEY}`)
if (toggleState === ClassName.COLLAPSED) { if (toggleState === ClassName.COLLAPSED) {
if (this._options.noTransitionAfterReload) { if (this._options.noTransitionAfterReload) {
$('body').addClass('hold-transition').addClass(ClassName.COLLAPSED).delay(50).queue(function () { $body.addClass('hold-transition').addClass(ClassName.COLLAPSED).delay(50).queue(function () {
$(this).removeClass('hold-transition') $(this).removeClass('hold-transition')
$(this).dequeue() $(this).dequeue()
}) })
} else { } else {
$('body').addClass(ClassName.COLLAPSED) $body.addClass(ClassName.COLLAPSED)
} }
} else if (this._options.noTransitionAfterReload) { } else if (this._options.noTransitionAfterReload) {
$('body').addClass('hold-transition').removeClass(ClassName.COLLAPSED).delay(50).queue(function () { $body.addClass('hold-transition').removeClass(ClassName.COLLAPSED).delay(50).queue(function () {
$(this).removeClass('hold-transition') $(this).removeClass('hold-transition')
$(this).dequeue() $(this).dequeue()
}) })
} else { } else {
$('body').removeClass(ClassName.COLLAPSED) $body.removeClass(ClassName.COLLAPSED)
} }
} }

View File

@ -132,14 +132,15 @@ class Toasts {
$(this._getContainerId()).prepend(toast) $(this._getContainerId()).prepend(toast)
$('body').trigger($.Event(Event.CREATED)) const $body = $('body')
$body.trigger($.Event(Event.CREATED))
toast.toast('show') toast.toast('show')
if (this._config.autoremove) { if (this._config.autoremove) {
toast.on('hidden.bs.toast', function () { toast.on('hidden.bs.toast', function () {
$(this).delay(200).remove() $(this).delay(200).remove()
$('body').trigger($.Event(Event.REMOVED)) $body.trigger($.Event(Event.REMOVED))
}) })
} }
} }

View File

@ -69,8 +69,10 @@ class TodoList {
// Private // Private
_init() { _init() {
$(Selector.DATA_TOGGLE).find('input:checkbox:checked').parents('li').toggleClass(ClassName.TODO_LIST_DONE) const $toggleSelector = $(Selector.DATA_TOGGLE)
$(Selector.DATA_TOGGLE).on('change', 'input:checkbox', event => {
$toggleSelector.find('input:checkbox:checked').parents('li').toggleClass(ClassName.TODO_LIST_DONE)
$toggleSelector.on('change', 'input:checkbox', event => {
this.toggle($(event.target)) this.toggle($(event.target))
}) })
} }