This repository has been archived on 2023-02-03. You can view files and clone it, but cannot push or open issues or pull requests.
autonomic.zone/src/assets/js/util.js

587 lines
14 KiB
JavaScript
Raw Normal View History

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