HTML element nested in mat-grid-tile cannot be found - angular-material

I try to show a openlayers map in a mat-grid-tile of a mat-grid-list.
Template snippet:
<mat-grid-list cols="3" rowHeight="100px">[...]
<mat-grid-tile
colspan="1"
rowspan="5">
<div
id="map"
class="map">
</div>
</div>
</mat-grid-tile>[...]
</mat-grid-list>
The openlayers map is initialized in the typescript file. Snippet:
import OlTileLayer from 'ol/layer/Tile';
import OlMap from 'ol/Map';
import OlOverlay from 'ol/Overlay';
import * as proj from 'ol/proj';
import OlXYZ from 'ol/source/XYZ';
import OlView from 'ol/View';
import OlFeature from 'ol/Feature';
import OlVectorSource from 'ol/source/Vector';
import OlStyleStyle from 'ol/style/Style';
import OlIconStyle from 'ol/style/Icon';
import OlOsmSource from 'ol/source/OSM';
import OlVectorLayer from 'ol/layer/Vector';
import OlPoint from 'ol/geom/Point';
[...]
map: OlMap;
popup: OlOverlay;
source: OlXYZ;
layer: OlTileLayer;
view: OlView;
olOverlay: OlOverlay;
olFeature: OlFeature;
markerSource: OlVectorSource;
markerStyle: OlStyleStyle;
ngAfterViewInit(): void {
this.setMarkerSource();
this.setMarkerStyle();
this.setMap();
}
private setMap() {
this.map = new OlMap({
target: 'map',
layers: [
new OlTileLayer({
source: new OlOsmSource()
}),
new OlVectorLayer({
source: this.markerSource,
style: this.markerStyle
})
],
view: new OlView({
center: proj.fromLonLat([7.35077565, 49.92363955]),
zoom: 7
})
});
}
private setMarkerSource() {
this.markerSource = new OlVectorSource();
}
private setMarkerStyle() {
this.markerStyle = new OlStyleStyle({
image: new OlIconStyle(
/** #type {olx.style.IconOptions} */ ({
opacity: 0.75,
src: 'path-to-icon.png'
})
)
});
}
The issue is, that the target property cannot be set as the div with the id 'map' cannot be found.
I also tried with document.getElementById('map') but it is null.
First I had the map initialization in ngOnInit(), then I moved it to ngAfterViewInit(). Another approach was to write the div like this:
<div #mapDiv class="map></div>
and to call it in the ts file with:
#ViewChild('mapDiv')
mapDiv: ElementRef;
[...]
this.map = new OlMap({
target: this.mapDiv.nativeElement,
[...]
but it says that this.mapDiv is undefined.
If I set the map div outside the mat-grid-tile/mat-grid-list, it works just fine.
What can I do to place the map inside the mat-grid-tile?
Thanks a lot.

Thank you very much for your comment.
In fact, after stopping and restarting the app, the above code works faultlessly.
No idea what the problem was.

Related

How do I use slots with a Quasar Dialog Plugin custom component?

I want to make a custom component for the Quasar Dialog. And inside that component I want to use slots, but I'm not sure how to do that.
This is my CustomDialogComponent.vue where I have defined a cancelBtn slot and a confirmBtn slot:
<template>
<!-- notice dialogRef here -->
<q-dialog ref="dialogRef" #hide="onDialogHide">
<q-card class="q-dialog-plugin">
<q-card-section>
<strong>{{ title }}</strong>
</q-card-section>
<q-card-section>
<slot name="cancelBtn" #click="handleCancelClick"></slot>
<slot name="confirmBtn" #click="handleConfirmClick"></slot>
</q-card-section>
</q-card>
</q-dialog>
</template>
<script setup lang="ts">
import { PropType } from 'vue';
import { useDialogPluginComponent } from 'quasar';
defineProps({
title: {
type: String,
required: false,
default: 'Alert',
},
});
defineEmits([
...useDialogPluginComponent.emits,
]);
const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } =
useDialogPluginComponent();
const handleConfirmClick = () => {
console.log('Confirm Button Clicked');
onDialogOK();
};
const handleCancelClick = () => {
console.log('Cancel Button Clicked');
onDialogCancel();
};
</script>
And the Quasar docs show that I can invoke it via a $q.dialog({ ... }) Object. With props etc all set inside that object. So that would look something like this:
<template>
<div #click="showDialog">Show The Dialog</div>
</template>
<script setup lang="ts">
import { useQuasar } from 'quasar';
import CustomDialogComponent from 'src/components/CustomDialogComponent.vue'
const $q = useQuasar();
const showDialog = () => {
$q.dialog({
component: CustomDialogComponent,
// props forwarded to your custom component
componentProps: {
title: 'Alert title goes here',
},
})
};
</script>
But there are no properties inside the Dialog Object for me to pass in my slots. So where can I pass in the cancelBtn and confirmBtn slots I created in CustomDialogComponent.vue?
I asked directly and apparently there is no way to use slots at this time. They might add this functionality later.

Problem with first try simple Relay/React example

I read relay official docs and followed example.
Now, I slightly change it for practice. I replaced load query with useQueryLoader
but result is, Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
I think it is related to loadRepoQuery() part, but without it, TypeError: null is not an object (evaluating 'preloadedQuery.fetchKey') occurs.
I think it's due to my misunderstanding. Please help me.
// App.js
import './App.css';
import graphql from 'babel-plugin-relay/macro';
import {
usePreloadedQuery,
useQueryLoader,
} from 'react-relay/hooks';
// Define a query
const RepositoryNameQuery = graphql`
query AppRepositoryNameQuery {
repository(owner: "facebook", name: "react") {
name
}
}
`;
function RepoRenderer() {
const [repoQueryRef, loadRepoQuery] = useQueryLoader(RepositoryNameQuery);
loadRepoQuery();
return (
<Repo preloadedQuery={repoQueryRef}/>
)
}
function Repo(props) {
const data = usePreloadedQuery(RepositoryNameQuery, props.preloadedQuery);
return (
<div className="App">
<header className="App-header">
<p>{data.repository.name}</p>
</header>
</div>
);
}
export default function App() {
return (
<RepoRenderer/>
);
}
//
import React, { Suspense } from 'react';
import ReactDOM from 'react-dom';
import { RelayEnvironmentProvider } from 'react-relay';
import relayEnvironment from './relayEnvironment';
import App from './App';
import './index.css';
ReactDOM.render(
<React.StrictMode>
<RelayEnvironmentProvider environment={relayEnvironment}>
<Suspense fallback={'Loading...'}>
<App/>
</Suspense>
</RelayEnvironmentProvider>
</React.StrictMode>,
document.getElementById('root')
);
I kind of solve this problem. Here's code snippet for newbie like me.
See this too.
// index.js
ReactDOM.render(
<RelayEnvironmentProvider environment={relayEnvironment}>
<Suspense fallback={'Loading...'}>
<App />
</Suspense>
</RelayEnvironmentProvider>,
document.getElementById('root')
);
//app.js
import React ,{ useCallback } from 'react';
import './App.css';
import graphql from 'babel-plugin-relay/macro';
import {
usePreloadedQuery,
useQueryLoader,
} from 'react-relay/hooks';
// Define a query
const RepositoryNameQuery = graphql`
query AppRepositoryNameQuery {
repository(owner: "yujong-lee", name: "taggy") {
name
}
}
`;
function Repo({queryRef, refetch}) {
const data = usePreloadedQuery(RepositoryNameQuery, queryRef);
return (
<div className="App">
<header className="App-header">
<p>{data.repository.name}</p>
</header>
</div>
);
}
function App() {
const [repoQueryRef, loadRepoQuery] = useQueryLoader(RepositoryNameQuery);
const refetch = useCallback(() => {
loadRepoQuery();
}, [loadRepoQuery])
if(repoQueryRef !== null) {
return <Repo queryRef={repoQueryRef} refetch={refetch}/>
}
return <button type='button' onClick={() => refetch()}>Fetch</button>
}
export default App;

Quasar: how to display an image when using q-file to pick the image?

New to Quasar & Vue.
I am using q-file which allow pick file & drag to drop file.
However, how do i display the image for preview?
Q-uploader seems work but how do i change the ui of it?
Link to component from Quasar:
https://quasar.dev/vue-components/file-picker
In you template define a q-file and q-img element. Add a #change handler and updateFile function. The q-img will contain the picture you selected.
import { ref } from 'vue';
import { defineComponent } from 'vue';
<script lang="ts">
export default defineComponent({
name: 'Component Name',
components: {},
setup () {
const image = ref(null);
const image1Url = ref('')
return {
image,
image1Url,
updateFile() {
imageUrl.value = URL.createObjectURL(image.value);
}
}
}
})
</script>
<div>
<q-file
v-model="image"
label="Pick one file"
filled
style="max-width: 300px"
#change="updateFile()"
/>
</div>
<div>
<q-img
:src="imageUrl"
spinner-color="white"
style="height: 140px; max-width: 150px"
/>
</div>
Create an #change hook on q-file:
In the script set the url from the file passed in from q-file:

useEffect / useState / setInterval React on Rails components not rendering

I am trying to use useEffect, setInterval and useState in order to cycle through fontawesome icons at a timed interval. I am very new to react and I am not getting any errors my component and all of the components below it are just not rendering. I am using the react-rails gem at its most recent version
here is the code for the component:
import React, {useEffect, useState} from "react"
import PropTypes from "prop-types"
function Changing(){
const mobileData = {
icon: "fas fa-mobile-alt",
caption: "Mobile Applications",
style: ""
}
const webData = {
icon: "fas fa-desktop",
caption: "Web development",
style: ""
}
const internetData = {
icon: "fas fa-wifi",
caption: "& Everything Internet",
style: ""
}
const data = [mobileData, webData, internetData];
const [iterable, setIterable] = useState(0);
const [iconData, setIconData] = useState(mobileData);
function changeIterable(){
if(iterable === 0){
setIterable((prevIterable) => prevIterable + 1)
}
else if(iterable === 1){
setIterable((prevIterable) => prevIterable + 1)
}
else{
setIterable((prevIterable) => prevIterable - 2)
}
}
useEffect(() => {
const intervalID = setInterval(() =>{
changeIterable();
setIconData(data[iterable])
}, 4000);
return () => clearInterval(intervalID)
})
return (
<React.Fragment>
<div className="row">
<div className="col-md-6">
<i className={iconData.icon} style={iconData.style} />
</div>
<div className="col-md-6">
<h3 style={iconData.style}>{iconData.caption}</h3>
</div>
</div>
</React.Fragment>
);
}
export default Changing
and I am rendering the component with:
<%= react_component "Personal" %>
<%= react_component "Changing" %>
<%= react_component "Stack" %>
Personal and Stack components were correctly rendering, but once I added Changing every component under it would not render.
I am pretty new rails and even more of a n00b when it comes to react, I was wondering if useEffect, setInterval and useState are even supported in react-rails. Any help is welcomed, thank you!
Firstly you are setting the style to an empty string when instead you'd want to set the style to an empty object {} in iconData as this is JSX. As far as next component not rendering, there could be either CSS or logic causing it. Best way to debug is to just verify with a simple component that returns a vanilla <p>Test this</p> to see why the next component is not showing, but I have a feeling that Stack has logic somewhere that returns nothing.

How to create custom draw button for ngx-leaflet with ngx-leaflet-draw

I want to create a custom button, which enables the Polyline drawer on click. it's similar to How to click a button and start a new polygon without using the Leaflet.draw UI, but I want to do that with angular (7), ngx-leaflet and ngx-leaflet-draw.
Here is my adapted code from the link for my angular project:
// app.component.ts
import * as L from 'leaflet';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
allDrawnItems = new L.FeatureGroup();
options = {
layers: [
tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 18, attribution: '...' })
],
zoom: 5,
center: latLng(51.9487949, 7.6237527)
};
drawOptions = {
position: 'bottomright',
draw: {
circlemarker: false,
polyline: true
},
featureGroup: this.allDrawnItems
}
constructor() {}
ngOnInit() {
this.options = {
layers: [
tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {maxZoom: 18, attribution: '...' })
],
zoom: 12,
center: latLng(51.9487949, 7.6237527)
};
this.drawOptions = {
position: 'bottomright',
draw: {
circlemarker: false,
polyline: true
},
featureGroup: this.allDrawnItems
}
}
btn_drawPolygon() {
var polylineDrawer = new L.Draw.Polyline(this.map); // <-- throws error
polylineDrawer.enable();
}
onDrawReady(event) {
console.log(event.layer);
}
}
and here is my html:
// app.component.html
<div style="text-align:center; margin-top: 64px;" fxFlex>
<div fxFlex
leaflet
[leafletOptions]="options">
<div
leafletDraw
[leafletDrawOptions]="drawOptions"
(leafletDrawCreated)="onDrawReady($event)"></div>
</div>
<button (click)="btn_drawPolygon()" mat-raised-button color="primary" fxFlex style="height: 38px;">draw polyline</button>
If I click the "draw polyline" button, I get the error:
ERROR TypeError: Cannot read property 'overlayPane' of undefined
at NewClass.initialize (leaflet.draw.js:8)
at NewClass.initialize (leaflet.draw.js:8)
at new NewClass (leaflet-src.js:301)
What's wrong at my code?
alright. I forgot to bind the map using the leafletMapReady function:
// app.component.html
<div fxFlex
leaflet
[leafletOptions]="options"
(leafletMapReady)="onMapReady($event)"> <!-- added -->
<div
leafletDraw
[leafletDrawOptions]="drawOptions"
(leafletDrawCreated)="onDrawReady($event)"></div>
and after making use of the onMapReady-function and binding the map to this.map, it works like a charm:
onMapReady(map: L.Map) {
console.log("ON MAP READY CALLED");
console.log(this.map);
this.map = map;
};

Resources