Has anyone got the Mobiscroll calendar e.g. https://demo.mobiscroll.com/javascript/range/date# working inside a Lightning Web Component (LWC)?
The version I am using is the "For JavaScript" trial.
This template:
<template>
<p>Before input</p>
<input id="xyz" class="abc" placeholder="Please Select...">
<p>After input</p>
</template>
and code:
import { LightningElement } from 'lwc';
import { loadStyle, loadScript } from 'lightning/platformResourceLoader';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import MOBISCROLL from '@salesforce/resourceUrl/mobiscroll';
export default class Mobiscroll extends LightningElement {
initialized = false;
// After component is rendered
renderedCallback() {
if (this.initialized) return;
this.initialized = true;
Promise.all([
loadScript(this, MOBISCROLL + '/js/mobiscroll.javascript.min.js'),
loadStyle(this, MOBISCROLL + '/css/mobiscroll.javascript.min.css')
])
.then(() => {
this.initialize();
})
.catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error loading Mobiscroll',
message: error.message,
variant: 'error'
})
);
});
}
initialize() {
// The D3 documentation example doesn't have "window." but needed here for some reason.
window.mobiscroll.settings = {
display: 'inline'
};
window.mobiscroll.calendar('#xyz-10');
window.mobiscroll.calendar('#xyz-7', { display: 'inline'});
window.mobiscroll.calendar('input.abc');
}
}
don't generate any errors but also don't generate any calendar output.
This message appears in the browser console:
The trial is being authenticated with remote calls made to https://trial.mobiscroll.com. This is strictly necessary for the trial. No remote calls are made from the licensed version. More info on the trial and its limitations: http://help.mobiscroll.com/trials/what-are-the-limitations-of-the-trial
though I don't see such a call being made even with that setup as a "CSP Trusted Site".
Adding lwc:dom="manual"
to the input
doesn't help either.
PS
Via some JavaScript debugging, I think I have found at least my "Cannot read property 'call' of undefined" problem.
This Mobiscroll code https://github.com/acidb/mobiscroll/blob/4b03e3d746a3fa83f07b3101eaa7ad12100e4933/src/js/core/dom.js#L1123:
var matchesSelector = element.matchesSelector || element.webkitMatchesSelector || element.mozMatchesSelector || element.msMatchesSelector;
return matchesSelector.call(element, selector);
will fail because this HTMLElementTheGoodPart
LWC code https://github.com/salesforce/lwc/blob/master/packages/lwc/types.d.ts#L16 does not implement matchesSelector
. Hence the "Cannot read property 'call' of undefined".
But HTMLElementTheGoodPart
does implement querySelectorAll
so this polyfill https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill would work.
Trying to add the polyfill to the LightningElement
, not surprisingly given the locked-down security model of LWC, results in this error:
TypeError: Cannot add property matchesSelector, object is not extensible
So it looks like the fix needs to be done in Mobiscroll. But a simple change may not be possible - see Can DOM matches logic be applied to an LWC LightingElement?.