Mercurial > hg > isophonics-drupal-site
diff core/modules/contextual/js/contextual.js @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/modules/contextual/js/contextual.js Wed Nov 29 16:09:58 2017 +0000 @@ -0,0 +1,147 @@ +/** +* DO NOT EDIT THIS FILE. +* See the following change record for more information, +* https://www.drupal.org/node/2815083 +* @preserve +**/ + +(function ($, Drupal, drupalSettings, _, Backbone, JSON, storage) { + var options = $.extend(drupalSettings.contextual, { + strings: { + open: Drupal.t('Open'), + close: Drupal.t('Close') + } + }); + + var cachedPermissionsHash = storage.getItem('Drupal.contextual.permissionsHash'); + var permissionsHash = drupalSettings.user.permissionsHash; + if (cachedPermissionsHash !== permissionsHash) { + if (typeof permissionsHash === 'string') { + _.chain(storage).keys().each(function (key) { + if (key.substring(0, 18) === 'Drupal.contextual.') { + storage.removeItem(key); + } + }); + } + storage.setItem('Drupal.contextual.permissionsHash', permissionsHash); + } + + function initContextual($contextual, html) { + var $region = $contextual.closest('.contextual-region'); + var contextual = Drupal.contextual; + + $contextual.html(html).addClass('contextual').prepend(Drupal.theme('contextualTrigger')); + + var destination = 'destination=' + Drupal.encodePath(drupalSettings.path.currentPath); + $contextual.find('.contextual-links a').each(function () { + var url = this.getAttribute('href'); + var glue = url.indexOf('?') === -1 ? '?' : '&'; + this.setAttribute('href', url + glue + destination); + }); + + var model = new contextual.StateModel({ + title: $region.find('h2').eq(0).text().trim() + }); + var viewOptions = $.extend({ el: $contextual, model: model }, options); + contextual.views.push({ + visual: new contextual.VisualView(viewOptions), + aural: new contextual.AuralView(viewOptions), + keyboard: new contextual.KeyboardView(viewOptions) + }); + contextual.regionViews.push(new contextual.RegionView($.extend({ el: $region, model: model }, options))); + + contextual.collection.add(model); + + $(document).trigger('drupalContextualLinkAdded', { + $el: $contextual, + $region: $region, + model: model + }); + + adjustIfNestedAndOverlapping($contextual); + } + + function adjustIfNestedAndOverlapping($contextual) { + var $contextuals = $contextual.parents('.contextual-region').eq(-1).find('.contextual'); + + if ($contextuals.length <= 1) { + return; + } + + var firstTop = $contextuals.eq(0).offset().top; + var secondTop = $contextuals.eq(1).offset().top; + if (firstTop === secondTop) { + var $nestedContextual = $contextuals.eq(1); + + var height = 0; + var $trigger = $nestedContextual.find('.trigger'); + + $trigger.removeClass('visually-hidden'); + height = $nestedContextual.height(); + $trigger.addClass('visually-hidden'); + + $nestedContextual.css({ top: $nestedContextual.position().top + height }); + } + } + + Drupal.behaviors.contextual = { + attach: function attach(context) { + var $context = $(context); + + var $placeholders = $context.find('[data-contextual-id]').once('contextual-render'); + if ($placeholders.length === 0) { + return; + } + + var ids = []; + $placeholders.each(function () { + ids.push($(this).attr('data-contextual-id')); + }); + + var uncachedIDs = _.filter(ids, function (contextualID) { + var html = storage.getItem('Drupal.contextual.' + contextualID); + if (html && html.length) { + window.setTimeout(function () { + initContextual($context.find('[data-contextual-id="' + contextualID + '"]'), html); + }); + return false; + } + return true; + }); + + if (uncachedIDs.length > 0) { + $.ajax({ + url: Drupal.url('contextual/render'), + type: 'POST', + data: { 'ids[]': uncachedIDs }, + dataType: 'json', + success: function success(results) { + _.each(results, function (html, contextualID) { + storage.setItem('Drupal.contextual.' + contextualID, html); + + if (html.length > 0) { + $placeholders = $context.find('[data-contextual-id="' + contextualID + '"]'); + + for (var i = 0; i < $placeholders.length; i++) { + initContextual($placeholders.eq(i), html); + } + } + }); + } + }); + } + } + }; + + Drupal.contextual = { + views: [], + + regionViews: [] + }; + + Drupal.contextual.collection = new Backbone.Collection([], { model: Drupal.contextual.StateModel }); + + Drupal.theme.contextualTrigger = function () { + return '<button class="trigger visually-hidden focusable" type="button"></button>'; + }; +})(jQuery, Drupal, drupalSettings, _, Backbone, window.JSON, window.sessionStorage); \ No newline at end of file