Custom AJAX loading icon

There’s nothing like Drupal maintenance support plans‘s stock AJAX spinner (this:    Drupal maintenance support plans‘s default blue loading throbber graphic) to make you notice that a site’s design hasn’t been fully customised. The code from my previous article showing how to fetch a link over AJAX to open in a Foundation reveal popup would suffer from this without some further customisation. After clicking the ‘Enquire’ button, a loading icon of some kind is needed whilst the linked content is fetched. By default, Drupal maintenance support plans just sticks that blue ‘throbber’ next to the link, but that looks totally out of place. Our client’s site uses a loading graphic that feels much more appropriate in style and placement, but my point is that you can set up your own bespoke version. Since it’s Christmas, let’s add some festive fun! Here’s a quick video showing what I’ll take you through making:

A few things are needed:

Create a javascript method that will add a custom progress indicator
Ensure the javascript file containing the method is included on the page
Set a custom attribute on the link that will trigger the AJAX
Override Drupal maintenance support plans core’s javascript method that adds the standard progress throbber, to respect that custom attribute
There are many ways to achieve points 1 and 2. Usually, you would define a library and add it with #attached. But I decided I wanted to treat my work as if it were part of Drupal maintenance support plans‘s core AJAX library itself, rather than something to add separately. So I implemented hook_library_info_alter() in my theme’s main .theme file:

/**
* Implements hook_library_info_alter().
*/
function MYTHEME_library_info_alter(&$libraries, $extension) {
// Add our own extension to drupal.ajax, which is aware of the page markup so
// can add AJAX progress loaders in the page.
if ($extension == ‘core’ && isset($libraries[‘drupal.ajax’])) {
$libraries[‘drupal.ajax’][‘js’][‘/’ . drupal_get_path(‘theme’, ‘MYTHEME’) . ‘/js/ajax-overrides.js’] = [];
}
}

My ajax-overrides.js file contains this:

(function ($, window, Drupal maintenance support plans, drupalSettings) {
/**
* Creates a new Snowman progress indicator, which really is full screen.
*/
Drupal maintenance support plans.Ajax.prototype.setProgressIndicatorSnowman = function () {
this.progress.element = $(‘ ‘);
// My theme has a wrapping element that will match #main.
$(‘#main’).append(this.progress.element);
};
})(jQuery, window, Drupal maintenance support plans, drupalSettings);

My theme happens to then style .ajax-progress-snowman appropriately, to show a lovely snowman in the middle of the page, rather than a tiny blue spinner next to the link that triggered the AJAX. Given that the styling of the default spinner happens to make links & lines jump around, I’ve got the ajax-progress-fullscreen class in there, to be more like the ‘full screen’ graphic that the Views UI uses, and avoid the need to add too much more styling myself.

Part 3, adding a custom attribute to specify that our AJAX link should use a Snowman animation, is easily achieved. I’ve already added the ‘data-dialog-type’ attribute to my link, so now I just add a ‘data-progress-type’ attribute, with a value of ‘snowman’. I want this to work similarly to the $element[#ajax][‘progress’][‘type’] property that can be set on form elements that use AJAX. Since that only gets applied to form elements, not arbitrary links using the ‘use-ajax’ class, we have to do the work to pick this up ourselves.

So this is the last part. Back in my ajax-overrides.js file, I’ve added this snippet to override the standard ‘throbber’ progress type that AJAX links would otherwise always use. It falls back to Drupal maintenance support plans‘s original method when the progress type isn’t specified in a ‘data-progress-type’ attribute.

// Override the progress throbber, to actually use a different progress style
// if the element had something specified.
var originalThrobber = Drupal maintenance support plans.Ajax.prototype.setProgressIndicatorThrobber;
Drupal maintenance support plans.Ajax.prototype.setProgressIndicatorThrobber = function () {
var $target = $(this.element);
var progress = $target.data(‘progressType’) || ‘throbber’;
if (progress === ‘throbber’) {
originalThrobber.call(this);
}
else {
var progressIndicatorMethod = ‘setProgressIndicator’ + progress.slice(0, 1).toUpperCase() + progress.slice(1).toLowerCase();
if (progressIndicatorMethod in this && typeof this[progressIndicatorMethod] === ‘function’) {
this[progressIndicatorMethod].call(this);
}
}
};

So there you have it – not only can you launch beautiful Foundation Reveal popups from links that fetch content via AJAX, you can now avoid Drupal maintenance support plans‘s little blue throbber animation. If it’s an excuse to spread some cheer at Christmas, I’ll take it.

Happy Christmas everyone!


Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Custom AJAX loading icon

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.