@@ -8,14 +8,10 @@ const WIDGETS = {
88 initialize : function ( ) {
99 this . url = window . location . href
1010
11- addListener . bind ( this ) ( 'phx:update_local_storage' , window , ( e ) => {
12- localStorage . setItem ( e . detail . key , e . detail . value )
13- } )
14-
15- addListener . bind ( this ) ( 'phx:update_local_storage' , this . el , ( e ) => {
11+ addListener . bind ( this ) ( 'click' , document . body , ( e ) => {
1612 const type = e . target . dataset . type || null
1713
18- if ( type && type == 'dashboard-link' ) {
14+ if ( type = == 'dashboard-link' ) {
1915 this . url = e . target . href
2016 const uri = new URL ( this . url )
2117 const path = '/' + uri . pathname . split ( '/' ) . slice ( 2 ) . join ( '/' )
@@ -54,9 +50,49 @@ const WIDGETS = {
5450 cleanup : function ( ) {
5551 removeListeners . bind ( this ) ( )
5652 }
53+ } ,
54+ tabs : {
55+ initialize : function ( ) {
56+ const domain = getDomain ( window . location . href )
57+
58+ addListener . bind ( this ) ( 'click' , this . el , ( e ) => {
59+ const button = e . target . closest ( 'button' )
60+ const tab = button && button . dataset . tab
61+
62+ if ( tab ) {
63+ const label = button . dataset . label
64+ const storageKey = button . dataset . storageKey
65+ const activeClasses = button . dataset . activeClasses
66+ const inactiveClasses = button . dataset . inactiveClasses
67+ const title = this . el
68+ . closest ( '[data-tile]' )
69+ . querySelector ( '[data-title]' )
70+
71+ title . innerText = label
72+
73+ this . el . querySelectorAll ( `button[data-tab] span` ) . forEach ( ( s ) => {
74+ s . className = inactiveClasses
75+ } )
76+
77+ button . querySelector ( 'span' ) . className = activeClasses
78+
79+ if ( storageKey ) {
80+ localStorage . setItem ( `${ storageKey } __${ domain } ` , tab )
81+ }
82+ }
83+ } )
84+ } ,
85+ cleanup : function ( ) {
86+ removeListeners . bind ( this ) ( )
87+ }
5788 }
5889}
5990
91+ function getDomain ( url ) {
92+ const uri = typeof url === 'object' ? url : new URL ( url )
93+ return uri . pathname . split ( '/' ) [ 1 ]
94+ }
95+
6096function addListener ( eventName , listener , callback ) {
6197 this . listeners = this . listeners || [ ]
6298
0 commit comments