spc-site/data/magprog/assets/js/util.js

587 lines
16 KiB
JavaScript
Raw Permalink Normal View History

2024-03-03 18:28:58 +03:00
(function ($) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
/**
* Generate an indented list of links from a nav. Meant for use with panel().
* @return {jQuery} jQuery object.
*/
$.fn.navList = function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const $this = $(this);
$a = $this.find('a'),
b = [];
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
$a.each(function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const $this = $(this),
indent = Math.max(0, $this.parents('li').length - 1),
href = $this.attr('href'),
target = $this.attr('target');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
b.push(
'<a ' +
'class="link depth-' + indent + '"' +
((typeof target !== 'undefined' && target !== '') ? ' target="' + target + '"' : '') +
((typeof href !== 'undefined' && href !== '') ? ' href="' + href + '"' : '') +
'>' +
'<span class="indent-' + indent + '"></span>' +
$this.text() +
'</a>'
);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
return b.join('');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
};
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
/**
* Panel-ify an element.
* @param {object} userConfig User config.
* @return {jQuery} jQuery object.
*/
$.fn.panel = function (userConfig) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
let $this = $(this);
// No elements?
if (this.length === 0)
return $this;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Multiple elements?
if (this.length > 1) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
for (let i = 0; i < this.length; i++)
$(this[i]).panel(userConfig);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
return $this;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Vars.
let $body = $('body'),
$window = $(window),
id = $this.attr('id'),
config;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Config.
config = $.extend({
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Delay.
delay: 0,
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Hide panel on link click.
hideOnClick: false,
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Hide panel on escape keypress.
hideOnEscape: false,
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Hide panel on swipe.
hideOnSwipe: false,
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Reset scroll position on hide.
resetScroll: false,
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Reset forms on hide.
resetForms: false,
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Side of viewport the panel will appear.
side: null,
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Target element for "class".
target: $this,
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Class to toggle.
visibleClass: 'visible'
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}, userConfig);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Expand "target" if it's not a jQuery object already.
if (typeof config.target != 'jQuery')
config.target = $(config.target);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Panel.
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Methods.
$this._hide = function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Already hidden? Bail.
if (!config.target.hasClass(config.visibleClass))
return;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// If an event was provided, cancel it.
if (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
event.preventDefault();
event.stopPropagation();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Hide.
config.target.removeClass(config.visibleClass);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Post-hide stuff.
window.setTimeout(function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Reset scroll position.
if (config.resetScroll)
$this.scrollTop(0);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Reset forms.
if (config.resetForms)
$this.find('form').each(function () {
this.reset();
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}, config.delay);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
};
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Vendor fixes.
$this
.css('-ms-overflow-style', '-ms-autohiding-scrollbar')
.css('-webkit-overflow-scrolling', 'touch');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Hide on click.
if (config.hideOnClick) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
$this.find('a')
.css('-webkit-tap-highlight-color', 'rgba(0,0,0,0)');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
$this
.on('click', 'a', function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const $a = $(this),
href = $a.attr('href'),
target = $a.attr('target');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (!href || href === '#' || href === '' || href === '#' + id)
return;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Cancel original event.
event.preventDefault();
event.stopPropagation();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Hide panel.
$this._hide();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Redirect to href.
window.setTimeout(function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (target === '_blank')
window.open(href);
else
window.location.href = href;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}, config.delay + 10);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Event: Touch stuff.
$this.on('touchstart', function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
$this.touchPosX = event.originalEvent.touches[0].pageX;
$this.touchPosY = event.originalEvent.touches[0].pageY;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
})
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
$this.on('touchmove', function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if ($this.touchPosX === null
|| $this.touchPosY === null)
return;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const diffX = $this.touchPosX - event.originalEvent.touches[0].pageX,
diffY = $this.touchPosY - event.originalEvent.touches[0].pageY,
th = $this.outerHeight(),
ts = ($this.get(0).scrollHeight - $this.scrollTop());
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Hide on swipe?
if (config.hideOnSwipe) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
let result = false;
const boundary = 20,
delta = 50;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
switch (config.side) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
case 'left':
result = (diffY < boundary && diffY > (-1 * boundary)) && (diffX > delta);
break;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
case 'right':
result = (diffY < boundary && diffY > (-1 * boundary)) && (diffX < (-1 * delta));
break;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
case 'top':
result = (diffX < boundary && diffX > (-1 * boundary)) && (diffY > delta);
break;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
case 'bottom':
result = (diffX < boundary && diffX > (-1 * boundary)) && (diffY < (-1 * delta));
break;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
default:
break;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (result) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
$this.touchPosX = null;
$this.touchPosY = null;
$this._hide();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
return false;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Prevent vertical scrolling past the top or bottom.
if (($this.scrollTop() < 0 && diffY < 0)
|| (ts > (th - 2) && ts < (th + 2) && diffY > 0)) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
event.preventDefault();
event.stopPropagation();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Event: Prevent certain events inside the panel from bubbling.
$this.on('click touchend touchstart touchmove', function (event) {
event.stopPropagation();
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Event: Hide panel if a child anchor tag pointing to its ID is clicked.
$this.on('click', 'a[href="#' + id + '"]', function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
event.preventDefault();
event.stopPropagation();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
config.target.removeClass(config.visibleClass);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Body.
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Event: Hide panel on body click/tap.
$body.on('click touchend', function (event) {
$this._hide(event);
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Event: Toggle.
$body.on('click', 'a[href="#' + id + '"]', function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
event.preventDefault();
event.stopPropagation();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
config.target.toggleClass(config.visibleClass);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Window.
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Event: Hide on ESC.
if (config.hideOnEscape)
$window.on('keydown', function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (event.keyCode === 27)
$this._hide(event);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
return $this;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
};
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
/**
* Apply "placeholder" attribute polyfill to one or more forms.
* @return {jQuery} jQuery object.
*/
$.fn.placeholder = function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Browser natively supports placeholders? Bail.
if (typeof (document.createElement('input')).placeholder != 'undefined')
return $(this);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// No elements?
if (this.length === 0)
return $this;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Multiple elements?
if (this.length > 1) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
for (let i = 0; i < this.length; i++)
$(this[i]).placeholder();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
return $this;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Vars.
var $this = $(this);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Text, TextArea.
$this.find('input[type=text],textarea')
.each(function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const i = $(this);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.val() === ''
|| i.val() === i.attr('placeholder'))
i
.addClass('polyfill-placeholder')
.val(i.attr('placeholder'));
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
})
.on('blur', function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const i = $(this);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.attr('name').match(/-polyfill-field$/))
return;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.val() === '')
i
.addClass('polyfill-placeholder')
.val(i.attr('placeholder'));
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
})
.on('focus', function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const i = $(this);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.attr('name').match(/-polyfill-field$/))
return;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.val() === i.attr('placeholder'))
i
.removeClass('polyfill-placeholder')
.val('');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Password.
$this.find('input[type=password]')
.each(function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const i = $(this);
const x = $(
$('<div>')
.append(i.clone())
.remove()
.html()
.replace(/type="password"/i, 'type="text"')
.replace(/type=password/i, 'type=text')
);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.attr('id') !== '')
x.attr('id', i.attr('id') + '-polyfill-field');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.attr('name') !== '')
x.attr('name', i.attr('name') + '-polyfill-field');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
x.addClass('polyfill-placeholder')
.val(x.attr('placeholder')).insertAfter(i);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.val() === '')
i.hide();
else
x.hide();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
i
.on('blur', function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
event.preventDefault();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const x = i.parent().find('input[name=' + i.attr('name') + '-polyfill-field]');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.val() === '') {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
i.hide();
x.show();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
x
.on('focus', function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
event.preventDefault();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const i = x.parent().find('input[name=' + x.attr('name').replace('-polyfill-field', '') + ']');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
x.hide();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
i
.show()
.focus();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
})
.on('keypress', function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
event.preventDefault();
x.val('');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Events.
$this
.on('submit', function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
$this.find('input[type=text],input[type=password],textarea')
.each(function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const i = $(this);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.attr('name').match(/-polyfill-field$/))
i.attr('name', '');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.val() === i.attr('placeholder')) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
i.removeClass('polyfill-placeholder');
i.val('');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
})
.on('reset', function (event) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
event.preventDefault();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
$this.find('select')
.val($('option:first').val());
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
$this.find('input,textarea')
.each(function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const i = $(this);
let x;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
i.removeClass('polyfill-placeholder');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
switch (this.type) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
case 'submit':
case 'reset':
break;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
case 'password':
i.val(i.attr('defaultValue'));
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
x = i.parent().find('input[name=' + i.attr('name') + '-polyfill-field]');
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.val() === '') {
i.hide();
x.show();
} else {
i.show();
x.hide();
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
break;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
case 'checkbox':
case 'radio':
i.attr('checked', i.attr('defaultValue'));
break;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
case 'text':
case 'textarea':
i.val(i.attr('defaultValue'));
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
if (i.val() === '') {
i.addClass('polyfill-placeholder');
i.val(i.attr('placeholder'));
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
break;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
default:
i.val(i.attr('defaultValue'));
break;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
return $this;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
};
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
/**
* Moves elements to/from the first positions of their respective parents.
* @param {jQuery} $elements Elements (or selector) to move.
* @param {bool} condition If true, moves elements to the top. Otherwise, moves elements back to their original locations.
*/
$.prioritize = function ($elements, condition) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const key = '__prioritize';
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Expand $elements if it's not already a jQuery object.
if (typeof $elements != 'jQuery')
$elements = $($elements);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Step through elements.
$elements.each(function () {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
const $e = $(this);
let $p;
const $parent = $e.parent();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// No parent? Bail.
if ($parent.length === 0)
return;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Not moved? Move it.
if (!$e.data(key)) {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Condition is false? Bail.
if (!condition)
return;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Get placeholder (which will serve as our point of reference for when this element needs to move back).
$p = $e.prev();
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Couldn't find anything? Means this element's already at the top, so bail.
if ($p.length === 0)
return;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Move element to top of parent.
$e.prependTo($parent);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Mark element as moved.
$e.data(key, $p);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Moved already?
else {
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Condition is true? Bail.
if (condition)
return;
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
$p = $e.data(key);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Move element back to its original location (using our placeholder).
$e.insertAfter($p);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
// Unmark element as moved.
$e.removeData(key);
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
}
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
});
2022-04-23 10:48:53 +03:00
2024-03-03 18:28:58 +03:00
};
2022-04-23 10:48:53 +03:00
})(jQuery);