1
Fork 0

Lint utils

This commit is contained in:
Jake Howard 2017-05-06 15:28:47 +01:00
parent b72f70384b
commit 0697e4f950
2 changed files with 281 additions and 590 deletions

View file

@ -8,9 +8,6 @@
$(function() { $(function() {
var $body = $('body'); var $body = $('body');
// Fix: Placeholder polyfill.
$('form').placeholder();
// Dropdowns. // Dropdowns.
$('#nav > ul').dropotron({ $('#nav > ul').dropotron({
alignment: 'right' alignment: 'right'

View file

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