Merge pull request #9 from apuc/add-categories-list
Add categories list
This commit is contained in:
		| @@ -1,4 +1,4 @@ | |||||||
| import { customStyles, createSelected, checkItemStruct } from './components/utils'; | import { customStyles, createSelected, getFormatItem } from './components/utils'; | ||||||
| import { createBreadcrumb } from './components/create-element'; | import { createBreadcrumb } from './components/create-element'; | ||||||
|  |  | ||||||
| export class DropDown { | export class DropDown { | ||||||
| @@ -29,24 +29,9 @@ export class DropDown { | |||||||
|       return false; |       return false; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const random = Math.random().toString(36).substring(2, 10); |  | ||||||
|     const index = this.#items.length; |     const index = this.#items.length; | ||||||
|  |  | ||||||
|     if (typeof item === 'object') { |     this.#items.push(getFormatItem(item, index)); | ||||||
|       item = { |  | ||||||
|         id: item.id, |  | ||||||
|         title: item.title, |  | ||||||
|         value: item.value, |  | ||||||
|       }; |  | ||||||
|     } else { |  | ||||||
|       item = { |  | ||||||
|         id: random, |  | ||||||
|         title: item, |  | ||||||
|         value: index, |  | ||||||
|       }; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     this.#items.push(item); |  | ||||||
|     this.#render(); |     this.#render(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -65,12 +50,14 @@ export class DropDown { | |||||||
|  |  | ||||||
|   selectIndex(index) { |   selectIndex(index) { | ||||||
|     const options = this.#element.querySelectorAll('.list__item'); |     const options = this.#element.querySelectorAll('.list__item'); | ||||||
|  |     // const selected = this.#element.querySelector('.selected'); | ||||||
|  |  | ||||||
|     if (index > options.length) { |     if (index > options.length) { | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const select = options[index].innerText; |     const select = options[index].innerText; | ||||||
|  |     // selected.innerText = select; | ||||||
|  |  | ||||||
|     this.#render(select); |     this.#render(select); | ||||||
|   } |   } | ||||||
| @@ -134,24 +121,18 @@ export class DropDown { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     items.forEach((dataItem, index) => { |     items.forEach((dataItem, index) => { | ||||||
|       const random = Math.random().toString(36).substring(2, 10); |       let category = ''; | ||||||
|       let item = {}; |  | ||||||
|  |  | ||||||
|       if (checkItemStruct(dataItem)) { |       if (dataItem.category && dataItem.categoryItems) { | ||||||
|         item = { |         category = dataItem.category; | ||||||
|           id: dataItem.id, |  | ||||||
|           title: dataItem.title, |         this.#items.push(category); | ||||||
|           value: index, |         dataItem.categoryItems.forEach((categoryItem, indexCategory) => { | ||||||
|         }; |           this.#items.push(getFormatItem(categoryItem, indexCategory)); | ||||||
|  |         }); | ||||||
|       } else { |       } else { | ||||||
|         item = { |         this.#items.push(getFormatItem(dataItem, index)); | ||||||
|           id: random, |  | ||||||
|           title: dataItem, |  | ||||||
|           value: index, |  | ||||||
|         }; |  | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       this.#items.push(item); |  | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -176,7 +157,8 @@ export class DropDown { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   #render(select) { |   #render(select) { | ||||||
|     const { styles, multiselect, event } = this.#options; |     const { styles, multiselect } = this.#options; | ||||||
|  |     // const { category } = this.#items; | ||||||
|  |  | ||||||
|     if (select || (select && styles)) { |     if (select || (select && styles)) { | ||||||
|       this.#initSelected(select); |       this.#initSelected(select); | ||||||
| @@ -184,7 +166,6 @@ export class DropDown { | |||||||
|     } else { |     } else { | ||||||
|       this.#initSelected(); |       this.#initSelected(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const ulList = document.createElement('ul'); |     const ulList = document.createElement('ul'); | ||||||
|  |  | ||||||
|     if (styles) { |     if (styles) { | ||||||
| @@ -198,12 +179,15 @@ export class DropDown { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     ulList.classList.add('list'); |     ulList.classList.add('list'); | ||||||
|  |  | ||||||
|     this.#element.appendChild(ulList); |     this.#element.appendChild(ulList); | ||||||
|  |  | ||||||
|     this.#items.forEach((dataItem) => { |     this.#items.forEach((dataItem) => { | ||||||
|       const liItem = document.createElement('li'); |       const liItem = document.createElement('li'); | ||||||
|  |       const strongItem = document.createElement('strong'); | ||||||
|  |  | ||||||
|       liItem.classList.add('list__item'); |       liItem.classList.add('list__item'); | ||||||
|  |       strongItem.classList.add('category'); | ||||||
|  |  | ||||||
|       if (multiselect) { |       if (multiselect) { | ||||||
|         const checkBox = document.createElement('input'); |         const checkBox = document.createElement('input'); | ||||||
| @@ -213,10 +197,24 @@ export class DropDown { | |||||||
|         liItem.appendChild(checkBox); |         liItem.appendChild(checkBox); | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       let textNode = document.createTextNode(dataItem.title); |       let textNode = ''; | ||||||
|  |  | ||||||
|       liItem.appendChild(textNode); |       if (dataItem.title) { | ||||||
|       ulList.appendChild(liItem); |         textNode = document.createTextNode(dataItem.title); | ||||||
|  |         liItem.appendChild(textNode); | ||||||
|  |         ulList.appendChild(liItem); | ||||||
|  |       } else { | ||||||
|  |         textNode = document.createTextNode(dataItem); | ||||||
|  |         strongItem.appendChild(textNode); | ||||||
|  |         ulList.appendChild(strongItem); | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     this.#items.filter((item, index) => { | ||||||
|  |       if (typeof item !== 'object') { | ||||||
|  |         this.#items.splice(index, 1); | ||||||
|  |       } | ||||||
|  |       return item; | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     this.#addOptionsBehaviour(); |     this.#addOptionsBehaviour(); | ||||||
| @@ -287,6 +285,7 @@ export class DropDown { | |||||||
|  |  | ||||||
|     const options = this.#element.querySelectorAll('.list__item'); |     const options = this.#element.querySelectorAll('.list__item'); | ||||||
|     const select = this.#element.querySelector('.selected'); |     const select = this.#element.querySelector('.selected'); | ||||||
|  |     const category = this.#element.querySelector('strong'); | ||||||
|  |  | ||||||
|     const ul = document.createElement('ul'); |     const ul = document.createElement('ul'); | ||||||
|  |  | ||||||
| @@ -298,7 +297,6 @@ export class DropDown { | |||||||
|     options.forEach((option, index) => { |     options.forEach((option, index) => { | ||||||
|       option.addEventListener('click', (event) => { |       option.addEventListener('click', (event) => { | ||||||
|         const item = this.#items[index]; |         const item = this.#items[index]; | ||||||
|  |  | ||||||
|         if (multiselect) { |         if (multiselect) { | ||||||
|           event.stopPropagation(); |           event.stopPropagation(); | ||||||
|           option.classList.toggle('active'); |           option.classList.toggle('active'); | ||||||
|   | |||||||
| @@ -59,3 +59,24 @@ export function checkItemStruct(item) { | |||||||
|  |  | ||||||
|   return item.hasOwnProperty('id') && item.hasOwnProperty('title') && item.hasOwnProperty('value'); |   return item.hasOwnProperty('id') && item.hasOwnProperty('title') && item.hasOwnProperty('value'); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | export function getFormatItem(dataItem, index) { | ||||||
|  |   const random = Math.random().toString(36).substring(2, 10); | ||||||
|  |   let item = {}; | ||||||
|  |  | ||||||
|  |   if (checkItemStruct(dataItem)) { | ||||||
|  |     item = { | ||||||
|  |       id: dataItem.id, | ||||||
|  |       title: dataItem.title, | ||||||
|  |       value: index, | ||||||
|  |     }; | ||||||
|  |   } else { | ||||||
|  |     item = { | ||||||
|  |       id: random, | ||||||
|  |       title: dataItem, | ||||||
|  |       value: index, | ||||||
|  |     }; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   return item; | ||||||
|  | } | ||||||
|   | |||||||
| @@ -11,16 +11,10 @@ | |||||||
|     <div class="container"> |     <div class="container"> | ||||||
|       <button class="cg-dropdown cg-dropdown_one"></button> |       <button class="cg-dropdown cg-dropdown_one"></button> | ||||||
|  |  | ||||||
|       <button class="cg-dropdown cg-dropdown_two"></button> |  | ||||||
|  |  | ||||||
|       <button class="cg-dropdown cg-dropdown_three"></button> |       <button class="cg-dropdown cg-dropdown_three"></button> | ||||||
|  |  | ||||||
|       <button class="cg-dropdown cg-dropdown_button" style="margin-top: 50px"></button> |       <button class="cg-dropdown cg-dropdown_button" style="margin-top: 50px"></button> | ||||||
|     </div> |     </div> | ||||||
|     <div class="buttonControlBox"> |  | ||||||
|       <button class="buttonControl button__open">Open</button> |  | ||||||
|       <button class="buttonControl button__close">Close</button> |  | ||||||
|     </div> |  | ||||||
|   </body> |   </body> | ||||||
|   <script type="module" src="index.js"></script> |   <script type="module" src="index.js"></script> | ||||||
| </html> | </html> | ||||||
|   | |||||||
							
								
								
									
										90
									
								
								src/index.js
									
									
									
									
									
								
							
							
						
						
									
										90
									
								
								src/index.js
									
									
									
									
									
								
							| @@ -1,61 +1,15 @@ | |||||||
| import { DropDown } from './cg-dropdown'; | import { DropDown } from './cg-dropdown'; | ||||||
|  |  | ||||||
|  | // ------------------------------Обычный селект-------------------- | ||||||
| const dropdown = new DropDown({ | const dropdown = new DropDown({ | ||||||
|   selector: '.cg-dropdown_one', |   selector: '.cg-dropdown_one', | ||||||
|   placeholder: 'Выберите авто', |   placeholder: 'Выберите авто', | ||||||
|   items: ['BMW', 'Opel', 'Mersedes', 'MAN', 'max'], |   items: ['BMW', 'Opel', 'Mersedes', 'MAN', 'max'], | ||||||
|  |  | ||||||
|   multiselect: true, |   multiselect: true, | ||||||
|   multiselectTag: true, |   multiselectTag: true, | ||||||
| }); | }); | ||||||
|  |  | ||||||
| dropdown.addItem('ZAZ'); | // ------------------------------URL-------------------- | ||||||
| dropdown.addItem('LADA'); |  | ||||||
| dropdown.addItem('Kamaz 258'); |  | ||||||
| dropdown.addItem('BMW'); |  | ||||||
|  |  | ||||||
| const dropdown2 = new DropDown({ |  | ||||||
|   selector: '.cg-dropdown_two', |  | ||||||
|   placeholder: 'SELECT CAR', |  | ||||||
|   items: [ |  | ||||||
|     { |  | ||||||
|       id: 'addaw21', |  | ||||||
|       title: 'BMW', |  | ||||||
|       value: 1, |  | ||||||
|     }, |  | ||||||
|     { |  | ||||||
|       id: '2414q', |  | ||||||
|       title: 'Opel', |  | ||||||
|       value: 2, |  | ||||||
|     }, |  | ||||||
|     { |  | ||||||
|       id: '24qwds', |  | ||||||
|       title: 'Kamaz 258', |  | ||||||
|       value: 3, |  | ||||||
|     }, |  | ||||||
|     { |  | ||||||
|       id: '28wds', |  | ||||||
|       title: 'MAN', |  | ||||||
|       value: 4, |  | ||||||
|     }, |  | ||||||
|     { |  | ||||||
|       id: '28qwds', |  | ||||||
|       title: 'BOOT', |  | ||||||
|       value: 5, |  | ||||||
|     }, |  | ||||||
|   ], |  | ||||||
|  |  | ||||||
|   multiselect: true, |  | ||||||
|   event: 'mouseenter', |  | ||||||
|   // multiselectTag: true, |  | ||||||
| }); |  | ||||||
|  |  | ||||||
| dropdown.disabled(false); |  | ||||||
|  |  | ||||||
| dropdown2.addItem('LADA'); |  | ||||||
|  |  | ||||||
| //ToDo: paste the desired url; |  | ||||||
|  |  | ||||||
| const dropdown3 = new DropDown({ | const dropdown3 = new DropDown({ | ||||||
|   selector: '.cg-dropdown_three', |   selector: '.cg-dropdown_three', | ||||||
|   placeholder: 'URL', |   placeholder: 'URL', | ||||||
| @@ -70,18 +24,44 @@ const dropdown3 = new DropDown({ | |||||||
|   multiselectTag: true, |   multiselectTag: true, | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | // --------------------------------Категории-------------------------- | ||||||
| const dropdown4 = new DropDown({ | const dropdown4 = new DropDown({ | ||||||
|   selector: '.cg-dropdown_button', |   selector: '.cg-dropdown_button', | ||||||
|   placeholder: 'Выберите авто', |   placeholder: 'Выберите регион', | ||||||
|   items: ['Russia', 'USA', 'England', 'Turkey', 'France'], |   items: [ | ||||||
|  |     { | ||||||
|  |       category: 'Russia', | ||||||
|  |       categoryItems: [ | ||||||
|  |         { | ||||||
|  |           id: '28qwds', | ||||||
|  |           title: 'Москва', | ||||||
|  |           value: 0, | ||||||
|  |         }, | ||||||
|  |         , | ||||||
|  |         'Ростов-на-дону', | ||||||
|  |         'Саратов', | ||||||
|  |         'Волгоград', | ||||||
|  |         'Донецк', | ||||||
|  |       ], | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  |       category: 'USA', | ||||||
|  |       categoryItems: ['Alabama', 'Texas', 'Colorado', 'Klirens', 'Los-Angeles'], | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  |       category: 'France', | ||||||
|  |       categoryItems: ['Paris'], | ||||||
|  |     }, | ||||||
|  |   ], | ||||||
|  |  | ||||||
|   multiselect: true, |   multiselect: true, | ||||||
|   multiselectTag: true, |   multiselectTag: true, | ||||||
| }); | }); | ||||||
| // dropdown4.disabled(true); |  | ||||||
|  |  | ||||||
| const buttonOpen = document.querySelector('.button__open'); | //----------------управление с помощью кнопок---------------------------------- | ||||||
| const buttonClose = document.querySelector('.button__close'); | /* const buttonOpen = document.querySelector('.button__open'); | ||||||
|  |  const buttonClose = document.querySelector('.button__close'); | ||||||
|  |  | ||||||
| dropdown4.buttonControl(buttonOpen, 'open'); |  dropdown4.buttonControl(buttonOpen, 'open'); | ||||||
| dropdown4.buttonControl(buttonClose, 'close'); |  dropdown4.buttonControl(buttonClose, 'close');  | ||||||
|  |  */ | ||||||
|   | |||||||
| @@ -16,11 +16,7 @@ body { | |||||||
|   width: 900px; |   width: 900px; | ||||||
| } | } | ||||||
|  |  | ||||||
| .content { | // ----Layout---- | ||||||
|   border: 1px solid #616161; |  | ||||||
|   height: 500px; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .cg-dropdown { | .cg-dropdown { | ||||||
|   min-width: 235px; |   min-width: 235px; | ||||||
|   margin-left: 0; |   margin-left: 0; | ||||||
| @@ -59,12 +55,6 @@ body { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| .overflow-hidden { |  | ||||||
|   overflow: hidden; |  | ||||||
|   text-overflow: ellipsis; |  | ||||||
|   white-space: nowrap; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .caret { | .caret { | ||||||
|   width: 0; |   width: 0; | ||||||
|   height: 0; |   height: 0; | ||||||
| @@ -146,11 +136,22 @@ body { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | .category { | ||||||
|  |   height: 20px; | ||||||
|  |   margin: 6px 5px 0 7px; | ||||||
|  |   font-size: 17px; | ||||||
|  |   border-bottom: 1px solid; | ||||||
|  |  | ||||||
|  |   display: flex; | ||||||
|  |   align-items: center; | ||||||
|  | } | ||||||
|  |  | ||||||
| input[type='checkbox'] { | input[type='checkbox'] { | ||||||
|   cursor: pointer; |   cursor: pointer; | ||||||
|   margin: 0 5px 0 0; |   margin: 0 5px 0 0; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // --------SVG-------- | ||||||
| .svg-icon { | .svg-icon { | ||||||
|   width: 16px; |   width: 16px; | ||||||
|   margin-left: 3px; |   margin-left: 3px; | ||||||
| @@ -164,6 +165,7 @@ input[type='checkbox'] { | |||||||
|     background-color: #ffffff; |     background-color: #ffffff; | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| .svg-icon path { | .svg-icon path { | ||||||
|   color: black; |   color: black; | ||||||
|   stroke: currentcolor; |   stroke: currentcolor; | ||||||
| @@ -171,6 +173,7 @@ input[type='checkbox'] { | |||||||
|   stroke-width: 0.5; |   stroke-width: 0.5; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | //-------Behavior-------- | ||||||
| .active { | .active { | ||||||
|   background: #8282822c; |   background: #8282822c; | ||||||
| } | } | ||||||
| @@ -179,8 +182,23 @@ input[type='checkbox'] { | |||||||
|   transition: 0.5s; |   transition: 0.5s; | ||||||
|   display: block; |   display: block; | ||||||
|   opacity: 1; |   opacity: 1; | ||||||
|  |  | ||||||
|  |   &_none { | ||||||
|  |     opacity: 0 !important; | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | .disabled { | ||||||
|  |   background-color: #8f9195 !important; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | .overflow-hidden { | ||||||
|  |   overflow: hidden; | ||||||
|  |   text-overflow: ellipsis; | ||||||
|  |   white-space: nowrap; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // ------SCROLLBAR------ | ||||||
| ::-webkit-scrollbar { | ::-webkit-scrollbar { | ||||||
|   width: 10px; |   width: 10px; | ||||||
| } | } | ||||||
| @@ -192,12 +210,3 @@ input[type='checkbox'] { | |||||||
| ::-webkit-scrollbar-thumb { | ::-webkit-scrollbar-thumb { | ||||||
|   background-color: #4d4d4d; |   background-color: #4d4d4d; | ||||||
| } | } | ||||||
|  |  | ||||||
| .disabled { |  | ||||||
|   background-color: #8f9195 !important; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| .buttonControlBox { |  | ||||||
|   margin: -25px auto 0 auto; |  | ||||||
|   width: 300px; |  | ||||||
| } |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 MaxOvs19
					MaxOvs19