glide icon indicating copy to clipboard operation
glide copied to clipboard

Better way to access HTML root node?

Open jcush opened this issue 6 years ago • 2 comments

Hi there,

A bit of background - I'm dynamically inserting slides on-the-fly and (due to #71) need to reboot Glide so it sets itself up properly, can be navigated, etc.

I'm trying to access the root HTML node which glide is attached to prior to calling destroy() as part of my reboot function, but can't see how to do this in the docs. The way I'm including Glide is simply using the min.js file so I'm not using ES6 module imports or anything but this is the best way I found so far:

let rootNode =  glideInstance._c.Html._r;

Where glideInstance is... as the name suggests... the mounted instance of Glide.

Thanks

jcush avatar Apr 29 '20 14:04 jcush

You can do it like this

import Glide, { Controls, Swipe } from "@glidejs/glide/dist/glide.modular.esm";

export const initSlider = (el, options) => {
    return new Glide(el).mount({
        Controls,
        Swipe,
        options
    });
};

Then where you want to use it...

import { initSlider } from "../sliders"

const sliderEl = document.querySelector(".my-slider.glide");
if (sliderEl) {
    var slider = initSliderTeam(sliderEl);
}

Then call your methods :)

slider.update({ startAt: parseInt(order) })

wickedst avatar Dec 20 '20 01:12 wickedst

Hi @wickedst,

I solved this back in April/May by creating a function I called glideReboot:

function glideReboot( glideInstance, toActive ){

	if( glideInstance ){

		if( toActive ){
			glideInstance.settings.startAt = getActiveStep( glideInstance );
		}

		let glideSettings = {
			selector: glideInstance.selector,
			options: glideInstance.settings
		};
		glideInstance.destroy();
		let newGlide = new Glide( glideSettings.selector, glideSettings.options );

		if( typeof( attachGlideEvents === 'function' ) ){
			newGlide = attachGlideEvents( newGlide );
		}

		newGlide.mount();

		return newGlide;
	
	}else{
		return null;
	}
}

This is called following any dynamic change to the existing Glide instance. It takes that instance and a bool or whether or not to automatically scroll to the active slide, creates a duplicate settings object then creates a new Glide instance and attaches any events that were added to the previous instance if there is a function called attachGlideEvents defined. It also uses a function I created called getActiveStep where necessary, which will return the step number to make active in the new instance if requested:

function getActiveStep( glideInstance ){
	let activeStep = $( glideInstance._c.Html._r ).attr( 'data-active-step' );
	if( ( typeof( activeStep ) === 'undefined' ) || ( activeStep < 1 ) ){
		activeStep = 1;
	};

	return activeStep;
}

I think this is similar to what you've done I think, but can be called from various places as is needed. Just leaving this here in case any future Googlers need an option.

jcush avatar Dec 21 '20 10:12 jcush