1- import { ReactiveController , ReactiveControllerHost } from 'lit' ;
21import { Slot , SlotValue } from '@api-viewer/common/lib/index.js' ;
32import {
43 hasTemplate ,
54 TemplateTypes
65} from '@api-viewer/common/lib/templates.js' ;
6+ import {
7+ AbstractController ,
8+ AbstractControllerHost
9+ } from './abstract-controller.js' ;
710
811const capitalize = ( name : string ) : string =>
912 name [ 0 ] . toUpperCase ( ) + name . slice ( 1 ) ;
1013
1114const getSlotContent = ( name : string ) : string =>
1215 capitalize ( name === '' ? 'content' : name ) ;
1316
14- export class SlotsController implements ReactiveController {
15- host : ReactiveControllerHost ;
16-
17+ export class SlotsController extends AbstractController < SlotValue > {
1718 enabled : boolean ;
1819
19- el : HTMLElement ;
20-
21- private _slots : SlotValue [ ] = [ ] ;
22-
23- get slots ( ) : SlotValue [ ] {
24- return this . _slots ;
25- }
26-
27- set slots ( slots : SlotValue [ ] ) {
28- this . _slots = slots ;
29-
30- // Apply slots content by re-creating nodes
31- if ( this . enabled && this . el . isConnected && slots && slots . length ) {
32- this . el . innerHTML = '' ;
33- slots . forEach ( ( slot ) => {
34- let node : Element | Text ;
35- const { name, content } = slot ;
36- if ( name ) {
37- node = document . createElement ( 'div' ) ;
38- node . setAttribute ( 'slot' , name ) ;
39- node . textContent = content ;
40- } else {
41- node = document . createTextNode ( content ) ;
42- }
43- this . el . appendChild ( node ) ;
44- } ) ;
45- }
46-
47- // Update the demo snippet
48- this . host . requestUpdate ( ) ;
49- }
50-
5120 constructor (
52- host : ReactiveControllerHost ,
53- id : number ,
21+ host : AbstractControllerHost ,
5422 component : HTMLElement ,
23+ id : number ,
5524 slots : Slot [ ]
5625 ) {
57- ( this . host = host ) . addController ( this as ReactiveController ) ;
58- this . el = component ;
26+ super ( host , component ) ;
27+
5928 this . enabled = ! hasTemplate ( id , component . localName , TemplateTypes . SLOT ) ;
60- this . slots = slots
29+ this . data = slots
6130 . sort ( ( a , b ) => {
6231 if ( a . name === '' ) {
6332 return 1 ;
@@ -75,12 +44,8 @@ export class SlotsController implements ReactiveController {
7544 } ) as SlotValue [ ] ;
7645 }
7746
78- hostDisconnected ( ) {
79- this . slots = [ ] ;
80- }
81-
8247 setValue ( name : string , content : string ) {
83- this . slots = this . slots . map ( ( slot ) => {
48+ this . data = this . data . map ( ( slot ) => {
8449 return slot . name === name
8550 ? {
8651 ...slot ,
@@ -89,4 +54,25 @@ export class SlotsController implements ReactiveController {
8954 : slot ;
9055 } ) ;
9156 }
57+
58+ updateData ( data : SlotValue [ ] ) {
59+ super . updateData ( data ) ;
60+
61+ // Apply slots content by re-creating nodes
62+ if ( this . enabled && this . el . isConnected && data && data . length ) {
63+ this . el . innerHTML = '' ;
64+ data . forEach ( ( slot ) => {
65+ let node : Element | Text ;
66+ const { name, content } = slot ;
67+ if ( name ) {
68+ node = document . createElement ( 'div' ) ;
69+ node . setAttribute ( 'slot' , name ) ;
70+ node . textContent = content ;
71+ } else {
72+ node = document . createTextNode ( content ) ;
73+ }
74+ this . el . appendChild ( node ) ;
75+ } ) ;
76+ }
77+ }
9278}
0 commit comments