| 
									
										
										
										
											2022-12-17 09:29:45 -08:00
										 |  |  | import { IgniteHtml, IgniteProperty } from '../ignite-html/ignite-html.js'; | 
					
						
							| 
									
										
										
										
											2022-12-14 11:44:07 -08:00
										 |  |  | import { IgniteElement } from "../ignite-html/ignite-element.js"; | 
					
						
							| 
									
										
										
										
											2024-11-20 07:54:07 -08:00
										 |  |  | import { IgniteTemplate, slot, list, div, text, button, span } from "../ignite-html/ignite-template.js"; | 
					
						
							| 
									
										
										
										
											2022-12-14 11:44:07 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ReadMore extends IgniteElement { | 
					
						
							|  |  |  |     constructor() { | 
					
						
							|  |  |  |         super(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     get properties() { | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2024-11-20 07:54:07 -08:00
										 |  |  |             maxLength: 125, | 
					
						
							|  |  |  |             maxLines: 1, | 
					
						
							| 
									
										
										
										
											2022-12-17 09:29:45 -08:00
										 |  |  |             value: new IgniteProperty(null, { | 
					
						
							| 
									
										
										
										
											2024-11-20 07:54:07 -08:00
										 |  |  |                 onChange: () => this.collapsed = true | 
					
						
							| 
									
										
										
										
											2022-12-17 09:29:45 -08:00
										 |  |  |             }), | 
					
						
							| 
									
										
										
										
											2022-12-14 11:44:07 -08:00
										 |  |  |             collapsed: true, | 
					
						
							|  |  |  |             collapsedValue: null, | 
					
						
							| 
									
										
										
										
											2024-11-20 07:54:07 -08:00
										 |  |  |             collapseButtonClass: "btn btn-none p-0 ps-1 d-inline text-primary" | 
					
						
							| 
									
										
										
										
											2022-12-14 11:44:07 -08:00
										 |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     render() { | 
					
						
							|  |  |  |         return this.template.child( | 
					
						
							| 
									
										
										
										
											2024-11-20 07:54:07 -08:00
										 |  |  |             new span().innerText([this.collapsed, this.value, this.maxLength, this.maxLines], (collapsed, value, maxLength, maxLines) => collapsed ? this.truncate() : value), | 
					
						
							| 
									
										
										
										
											2022-12-14 11:44:07 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             new button() | 
					
						
							| 
									
										
										
										
											2024-11-20 07:54:07 -08:00
										 |  |  |                 .show([this.value, this.maxLength, this.maxLines], (value, maxLength, maxLines) => this.truncate() != value) | 
					
						
							| 
									
										
										
										
											2022-12-14 11:44:07 -08:00
										 |  |  |                 .class(this.collapseButtonClass) | 
					
						
							| 
									
										
										
										
											2023-12-24 09:51:54 -08:00
										 |  |  |                 .innerText(this.collapsed, collapsed => collapsed ? "Read more" : "Read less") | 
					
						
							| 
									
										
										
										
											2022-12-14 11:44:07 -08:00
										 |  |  |                 .onClick(() => this.collapsed = !this.collapsed) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-11-20 07:54:07 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     truncate() { | 
					
						
							|  |  |  |         //Only do this if the value isn't null.
 | 
					
						
							|  |  |  |         if (this.value) { | 
					
						
							|  |  |  |             //Speed up, if we are not restricting lines, then just check the length.
 | 
					
						
							|  |  |  |             if (this.maxLines < 0 && this.value.length > this.maxLength) { | 
					
						
							|  |  |  |                 return value.substring(0, this.maxLength).trimEnd() + "..."; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 //Check for lines and length, whichever we hit first stop.
 | 
					
						
							|  |  |  |                 var lines = 0; | 
					
						
							|  |  |  |                 for (var i = 0; i < this.value.length; i++) { | 
					
						
							|  |  |  |                     if (this.value[i] == '\n') { | 
					
						
							|  |  |  |                         lines++; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (i > this.maxLength || lines > this.maxLines) { | 
					
						
							|  |  |  |                         return this.value.substring(0, i).trimEnd() + "..."; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this.value; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-14 11:44:07 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ReadMoreTemplate extends IgniteTemplate { | 
					
						
							|  |  |  |     constructor(...children) { | 
					
						
							|  |  |  |         super("mt-read-more", children); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | IgniteHtml.register("mt-read-more", ReadMore); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export { | 
					
						
							|  |  |  |     ReadMoreTemplate as ReadMore | 
					
						
							|  |  |  | } |