File indexing completed on 2024-12-22 05:34:44

0001 class CategoryTree extends React.Component {
0002   constructor(props){
0003         super(props);
0004         this.state = {
0005       categories:window.catTree,
0006       categoryId:window.categoryId,
0007       catTreeCssClass:"",
0008       selectedCategories:[],
0009       showCatTree:false,
0010       backendView:window.backendView,
0011       loading:true,
0012     };
0013     this.getSelectedCategories = this.getSelectedCategories.bind(this);
0014     this.updateDimensions = this.updateDimensions.bind(this);
0015     this.toggleCatTree = this.toggleCatTree.bind(this);
0016   }
0017 
0018   componentWillMount() {
0019     this.updateDimensions();
0020   }
0021 
0022   componentWillUnmount(){
0023     window.removeEventListener("resize", this.updateDimensions);
0024   }
0025 
0026   componentDidMount() {
0027     window.addEventListener("resize", this.updateDimensions);
0028     const urlContext = appHelpers.getUrlContext(window.location.href);
0029     this.setState({urlContext:urlContext},function(){
0030       if (this.state.categoryId && this.state.categoryId !== 0){
0031         this.getSelectedCategories(this.state.categories,this.state.categoryId);
0032       } else {
0033         this.setState({loading:false});
0034       }
0035     });
0036   }
0037 
0038   getSelectedCategories(categories,catId){
0039     const selectedCategory = appHelpers.getSelectedCategory(this.state.categories,catId);
0040     const selectedCategories = this.state.selectedCategories;
0041     if (typeof(selectedCategory) !== 'undefined'){
0042       selectedCategory.selectedIndex = selectedCategories.length;
0043       selectedCategories.push(selectedCategory);
0044     }
0045     this.setState({selectedCategories:selectedCategories},function(){
0046       if (selectedCategory && selectedCategory.parent_id){
0047         this.getSelectedCategories(categories,parseInt(selectedCategory.parent_id));
0048       } else {
0049         this.setState({loading:false});
0050       }
0051     });
0052   }
0053 
0054   updateDimensions(){
0055     const device = appHelpers.getDeviceFromWidth(window.innerWidth);
0056     this.setState({device:device});
0057   }
0058 
0059   toggleCatTree(){
0060     const showCatTree = this.state.showCatTree === true ? false : true;
0061     const catTreeCssClass = this.state.catTreeCssClass === "open" ? "" : "open";
0062     this.setState({showCatTree:showCatTree,catTreeCssClass:catTreeCssClass});
0063   }
0064 
0065   render(){
0066     let categoryTreeDisplay, selectedCategoryDisplay;
0067     if (!this.state.loading){
0068 
0069       if (this.state.device === "tablet" && this.state.selectedCategories &&  this.state.selectedCategories.length > 0){
0070         selectedCategoryDisplay = (
0071           <SelectedCategory
0072             categoryId={this.state.categoryId}
0073             selectedCategory={this.state.selectedCategories[0]}
0074             selectedCategories={this.state.selectedCategories}
0075             onCatTreeToggle={this.toggleCatTree}
0076           />
0077         );
0078       }
0079       if (this.state.device === "tablet" && this.state.showCatTree || this.state.device !== "tablet" || this.state.selectedCategories && this.state.selectedCategories.length === 0) {
0080         if (this.state.categories){
0081           const self = this;
0082           const categoryTree = this.state.categories.sort(appHelpers.sortArrayAlphabeticallyByTitle).map((cat,index) => (
0083             <CategoryItem
0084               key={index}
0085               category={cat}
0086               categoryId={self.state.categoryId}
0087               urlContext={self.state.urlContext}
0088               selectedCategories={self.state.selectedCategories}
0089               backendView={self.state.backendView}
0090             />
0091           ));
0092 
0093           const allCatItemCssClass = appHelpers.getAllCatItemCssClass(window.location.href,window.baseUrl,this.state.urlContext, this.state.categoryId);
0094           let baseUrl;
0095           if (window.baseUrl !== window.location.origin){
0096             baseUrl = window.location.origin;
0097           }
0098 
0099           const categoryItemLink = appHelpers.generateCategoryLink(window.baseUrl,this.state.urlContext,"all",window.location.href);
0100 
0101           categoryTreeDisplay = (
0102             <ul className="main-list">
0103               <li className={"cat-item" + " " + allCatItemCssClass}>
0104                 <a href={categoryItemLink}><span className="title">All</span></a>
0105               </li>
0106               {categoryTree}
0107             </ul>
0108           );
0109         }
0110       }
0111 
0112     }
0113 
0114     return(
0115       <div id="category-tree" className={this.state.device + " " + this.state.catTreeCssClass}>
0116         {selectedCategoryDisplay}
0117         {categoryTreeDisplay}
0118       </div>
0119     );
0120   }
0121 }
0122 
0123 class CategoryItem extends React.Component {
0124   constructor(props){
0125         super(props);
0126         this.state = {
0127       showSubmenu:false
0128     };
0129     this.toggleSubmenu = this.toggleSubmenu.bind(this);
0130   }
0131 
0132   toggleSubmenu(){
0133     console.log('toggle sub menu');
0134     const showSubmenu = this.state.showSubmenu === true ? false : true;
0135     console.log(showSubmenu);
0136     this.setState({showSubmenu:showSubmenu});
0137   }
0138 
0139   render(){
0140     let categoryChildrenDisplay;
0141 
0142     const categoryType = appHelpers.getCategoryType(this.props.selectedCategories,this.props.categoryId,this.props.category.id);
0143     if (this.props.category.has_children === true && categoryType && this.props.lastChild !== true ||
0144         this.props.category.has_children === true && this.props.backendView === true && this.state.showSubmenu === true){
0145 
0146       const self = this;
0147 
0148       let lastChild;
0149       if (categoryType === "selected"){
0150         lastChild = true;
0151       }
0152 
0153       const children = appHelpers.convertObjectToArray(this.props.category.children);
0154       const categoryChildren = children.sort(appHelpers.sortArrayAlphabeticallyByTitle).map((cat,index) => (
0155         <CategoryItem
0156           key={index}
0157           category={cat}
0158           categoryId={self.props.categoryId}
0159           urlContext={self.props.urlContext}
0160           selectedCategories={self.props.selectedCategories}
0161           lastChild={lastChild}
0162           parent={self.props.category}
0163           backendView={self.props.backendView}
0164         />
0165       ));
0166 
0167       categoryChildrenDisplay = (
0168         <ul>
0169           {categoryChildren}
0170         </ul>
0171       );
0172 
0173     }
0174 
0175     let categoryItemClass = "cat-item";
0176     if (this.props.categoryId === parseInt(this.props.category.id)){
0177       categoryItemClass += " active";
0178     }
0179 
0180     let productCountDisplay;
0181     if (this.props.category.product_count !== "0"){
0182       productCountDisplay = this.props.category.product_count;
0183     }
0184 
0185     const categoryItemLink = appHelpers.generateCategoryLink(window.baseUrl,this.props.urlContext,this.props.category.id,window.location.href);
0186 
0187     let catItemContentDisplay;
0188     if (this.props.backendView === true){
0189 
0190       let submenuToggleDisplay;
0191       if (this.props.category.has_children === true){
0192         console.log(this.props.category.title);
0193         console.log(this.props.category.has_children);
0194         if (this.state.showSubmenu === true){
0195           submenuToggleDisplay = "[-]";
0196         } else {
0197           submenuToggleDisplay = "[+]";
0198         }
0199       }
0200 
0201       catItemContentDisplay = (
0202         <span>
0203           <span className="title"><a href={categoryItemLink}>{this.props.category.title}</a></span>
0204           <span className="product-counter">{productCountDisplay}</span>
0205           <span className="submenu-toggle" onClick={this.toggleSubmenu}>{submenuToggleDisplay}</span>
0206         </span>
0207       );
0208     } else {
0209       catItemContentDisplay = (
0210         <a href={categoryItemLink}>
0211           <span className="title">{this.props.category.title}</span>
0212           <span className="product-counter">{productCountDisplay}</span>
0213         </a>
0214       );
0215     }
0216 
0217     return(
0218       <li id={"cat-"+this.props.category.id} className={categoryItemClass}>
0219         {catItemContentDisplay}
0220         {categoryChildrenDisplay}
0221       </li>
0222     )
0223   }
0224 }
0225 
0226 class SelectedCategory extends React.Component {
0227   constructor(props){
0228         super(props);
0229         this.state = {};
0230   }
0231 
0232   render(){
0233     let selectedCategoryDisplay;
0234     if (this.props.selectedCategory){
0235       selectedCategoryDisplay = (
0236         <a onClick={this.props.onCatTreeToggle}>{this.props.selectedCategory.title}</a>
0237       );
0238     }
0239 
0240     let selectedCategoriesDisplay;
0241     if (this.props.selectedCategories){
0242       const selectedCategoriesReverse = this.props.selectedCategories.slice(0);
0243       selectedCategoriesDisplay = selectedCategoriesReverse.reverse().map((sc,index) => (
0244         <a key={index}>{sc.title}</a>
0245       ));
0246     }
0247 
0248     return (
0249       <div onClick={this.props.onCatTreeToggle} id="selected-category-tree-item">
0250         {selectedCategoriesDisplay}
0251         <span className="selected-category-arrow-down"></span>
0252       </div>
0253     )
0254   }
0255 }
0256 
0257 function CategorySidePanelContainer(){
0258   
0259   const [ categories, setCategoies ] = React.useState(window.catTree);
0260   const [ categoryId, setCategoryId ] = React.useState(window.categoryId);
0261   const [ catTreeSccClass, setCatTreeCssClass ] = React.useState('');
0262   const [ showCatTree, setShowCatTree ] = React.useState(false);
0263   const [ backendView, setBackendView ] = React.useState(window.backendView);
0264   const [ loading, setLoading ] = React.useState(true);
0265   
0266   console.log(categories);
0267 
0268   const mainCategoriesDisplay = <CategorySidePanel categories={categories} />
0269 
0270   return(
0271     <div id="sidebar-container">
0272       <CategoryTree/>
0273       <div id="category-menu-panels-container">
0274         <div id="category-menu-panels-slider">
0275           {mainCategoriesDisplay}
0276         </div>
0277       </div>
0278     </div>
0279   )
0280 }
0281 
0282 function CategorySidePanel(){
0283   
0284 }
0285 
0286 ReactDOM.render(
0287     <CategorySidePanelContainer />,
0288     document.getElementById('category-tree-container')
0289 );