diff --git a/core/misc/offset/fragment.js b/core/misc/offset/fragment.js new file mode 100644 index 0000000..efcd8a3 --- /dev/null +++ b/core/misc/offset/fragment.js @@ -0,0 +1,28 @@ +/** + * @file + * Offsets the window to allow fragments navigation with fixed positioning elements. + */ +(function ($, displace) { + + "use strict"; + + function adjustOffset() { + var hash = window.location.hash; + if (hash) { + var element = document.getElementById(hash.replace('#', '')); + if (element) { + // Display the element in the viewport. + //element.scrollIntoView(); + var elementOffset = $(element).offset(); + setTimeout(function () { + window.scrollTo(0, elementOffset.top - displace.calculateOffset('top')); + }, 5); + } + } + } + + // Checks for a fragment on page load, outside Drupal behaviors. + Drupal.behaviors.adjustOffset = { attach: adjustOffset }; + $(window).on('hashchange', adjustOffset); + +})(jQuery, Drupal.displace); diff --git a/core/misc/offset/html5forms.js b/core/misc/offset/html5forms.js new file mode 100644 index 0000000..32e0d33 --- /dev/null +++ b/core/misc/offset/html5forms.js @@ -0,0 +1,13 @@ +/** + * @file + * Focus the HTML5 validation error in view. + */ +(function (displace) { + + "use strict"; + + document.addEventListener('invalid', function (event) { + setTimeout(function () { window.scrollBy(0, -displace.offsets.top); }, 5); + }, true); + +})(Drupal.displace); diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 925709c..95ec1d3 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1220,6 +1220,10 @@ function system_library_info() { 'version' => VERSION, 'js' => array( 'core/misc/drupal.js' => array('group' => JS_LIBRARY, 'weight' => -18), + 'core/misc/debounce.js' => array('group' => JS_LIBRARY), + 'core/misc/displace.js' => array('group' => JS_LIBRARY), + 'core/misc/offset/fragment.js' => array('group' => JS_LIBRARY), + 'core/misc/offset/html5forms.js' => array('group' => JS_LIBRARY), ), 'dependencies' => array( array('system', 'jquery'), @@ -1424,7 +1428,7 @@ function system_library_info() { 'title' => 'Drupal displace', 'version' => VERSION, 'js' => array( - 'core/misc/displace.js' => array('group' => JS_LIBRARY), + //'core/misc/displace.js' => array('group' => JS_LIBRARY), ), 'dependencies' => array( array('system', 'jquery'), @@ -1453,7 +1457,7 @@ function system_library_info() { 'title' => 'Drupal debounce', 'version' => VERSION, 'js' => array( - 'core/misc/debounce.js' => array('group' => JS_LIBRARY), + //'core/misc/debounce.js' => array('group' => JS_LIBRARY), ), 'dependencies' => array( // @todo remove drupal dependency.