import * as React from 'react'; import { observer } from 'mobx-react'; import { observable, action, runInAction } from 'mobx'; import "./SearchBox.scss"; import "./IconBar.scss"; import * as anime from 'animejs'; import { DocTypes } from '../../documents/Documents'; import { faSearch, faFilePdf, faFilm, faImage, faObjectGroup, faStickyNote, faMusic, faLink, faChartBar, faGlobeAsia, faBan } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { library } from '@fortawesome/fontawesome-svg-core'; import * as _ from "lodash"; var classNames = require('classnames'); library.add(faSearch); library.add(faObjectGroup); library.add(faImage); library.add(faStickyNote); library.add(faFilePdf); library.add(faFilm); library.add(faMusic); library.add(faLink); library.add(faChartBar); library.add(faGlobeAsia); library.add(faBan); export interface IconBarProps { updateIcon(icons: string[]): void; getIcons(): string[]; } @observer export class IconBar extends React.Component { @observable noneRef = React.createRef(); @observable colRef = React.createRef(); @observable imgRef = React.createRef(); @observable textRef = React.createRef(); @observable pdfRef = React.createRef(); @observable vidRef = React.createRef(); @observable audioRef = React.createRef(); @observable linkRef = React.createRef(); @observable histRef = React.createRef(); @observable webRef = React.createRef(); @observable allRefs: React.RefObject[] = [this.colRef, this.imgRef, this.textRef, this.pdfRef, this.vidRef, this.audioRef, this.linkRef, this.histRef, this.webRef]; @observable originalFilteredNodes: string[] = this.props.getIcons(); componentDidMount = () => { //i KNOW this is bad i just can't get this to re render eeeeeeeek this.forceUpdate(); } //gets ref associated with given string @action.bound getRef = (value: string) => { let toReturn; switch (value) { case (DocTypes.NONE): toReturn = this.noneRef.current; break; case (DocTypes.AUDIO): toReturn = this.audioRef.current; break; case (DocTypes.COL): toReturn = this.colRef.current; break; case (DocTypes.HIST): toReturn = this.histRef.current; break; case (DocTypes.IMG): toReturn = this.imgRef.current; break; case (DocTypes.LINK): toReturn = this.linkRef.current; break; case (DocTypes.PDF): toReturn = this.pdfRef.current; break; case (DocTypes.TEXT): toReturn = this.textRef.current; break; case (DocTypes.VID): toReturn = this.vidRef.current; break; case (DocTypes.WEB): toReturn = this.webRef.current; break; default: toReturn = null; break; } return toReturn; } @action.bound unselectAllRefs() { this.allRefs.forEach(element => { if (element.current) { element.current.setAttribute("data-selected", "false"); } }); } @action.bound alternateRef(ref: any) { if (ref.getAttribute("data-selected") === "true") { ref.setAttribute("data-selected", "false"); } else { ref.setAttribute("data-selected", "true"); } } @action.bound onClick = (value: string) => { let icons: string[] = this.props.getIcons(); let ref = this.getRef(value); this.alternateRef(ref); if (value === DocTypes.NONE) { icons = []; // if its none, change the color of all the other circles this.unselectAllRefs(); } else { //if it's already selected, unselect it if (icons.includes(value)) { icons = _.without(icons, value); } //if it's not yet selected, select it else { icons.push(value); } } this.props.updateIcon(icons); //ok i know that this is bad but i dont know how else to get it to rerender and change the classname, //any help here is greatly appreciated thanks frens this.forceUpdate(); } //checks if option is selected based on the attribute data-selected @action.bound isRefSelected = (ref: React.RefObject) => { if (ref.current) { if (ref.current.getAttribute("data-selected") === "true") { return true; } return false; } } getInitialStatus = (type: string) => { if (this.originalFilteredNodes.includes(type)) { return "true"; } return "false"; } render() { return (
Filter by type of node
{ this.onClick(DocTypes.NONE); }}>
Clear
{ this.onClick(DocTypes.PDF); }}>
PDF
{ this.onClick(DocTypes.HIST); }}>
Histogram
{ this.onClick(DocTypes.COL); }}>
Collection
{ this.onClick(DocTypes.IMG); }}>
Image
{ this.onClick(DocTypes.VID); }}>
Video
{ this.onClick(DocTypes.WEB); }}>
Web
{ this.onClick(DocTypes.LINK); }}>
Link
{ this.onClick(DocTypes.AUDIO); }}>
Audio
{ this.onClick(DocTypes.TEXT); }}>
Text
); } }