@@ -4,19 +4,27 @@ const { computed, createApp, onMounted, ref } = window.Vue
44const Utils = {
55 getUrl ( ) {
66 let href = location . hash . substring ( 1 )
7- let isViewSource = false
7+ let viewType = 'example' // default value
88
9- if ( href . includes ( 'view-source' ) ) {
9+ // Parse view type
10+ if ( href . includes ( '#view-' ) ) {
11+ const parts = href . split ( '#view-' )
12+
13+ viewType = parts [ 1 ] || 'example'
14+ href = parts [ 0 ] . replace ( '#' , '' )
15+ } else if ( href . includes ( 'view-source' ) ) {
16+ // Backward compatibility for legacy view-source hash
1017 href = href . replace ( '#view-source' , '' ) . replace ( 'view-source' , '' )
11- isViewSource = true
18+ viewType = 'html'
1219 }
20+
1321 return {
1422 href : href || 'welcome.html' ,
15- isViewSource
23+ viewType : [ 'example' , 'html' , 'data' ] . includes ( viewType ) ? viewType : 'example'
1624 }
1725 } ,
1826
19- loadUrl ( theme , href , isViewSource ) {
27+ loadUrl ( theme , href , viewType ) {
2028 let template = 'template'
2129
2230 if ( theme ) {
@@ -27,9 +35,12 @@ const Utils = {
2735 if ( isDebug ) {
2836 url = `${ template } .html?t=${ + new Date ( ) } &url=${ href } `
2937 }
30- if ( isViewSource ) {
31- url = `${ template } .html?v=VERSION&view-source&url=${ href } #view-source`
38+
39+ // Pass view type to iframe
40+ if ( viewType !== 'example' ) {
41+ url += `&view-type=${ viewType } #view-${ viewType } `
3242 }
43+
3344 $ ( 'iframe' ) . attr ( 'src' , url )
3445 } ,
3546
@@ -60,27 +71,32 @@ const Utils = {
6071 } , 100 )
6172 } ,
6273
63- initViewSource ( ) {
64- const isSource = / v i e w - s o u r c e $ / . test ( location . hash )
65-
66- if ( isSource ) {
67- $ ( '.view-example' ) . css ( 'display' , 'block' )
68- $ ( '.view-source' ) . css ( 'display' , 'none' )
69- } else {
70- $ ( '.view-example' ) . css ( 'display' , 'none' )
71- $ ( '.view-source' ) . css ( 'display' , 'block' )
72- }
73-
74- $ ( '.view-example, .view-source' ) . off ( 'click' ) . click ( function ( ) {
75- if ( isSource ) {
76- location . hash = location . hash . replace ( '#view-source' , '' )
77- } else if ( ! location . hash . includes ( 'view-source' ) ) {
78- location . hash += '#view-source'
79- }
80- } )
74+ initViewToggle ( ) {
75+ const { href, viewType } = Utils . getUrl ( )
76+
77+ // Update button states
78+ $ ( '.icon-buttons .view-example, .icon-buttons .view-html, .icon-buttons .view-data' ) . removeClass ( 'active' )
79+ $ ( `.view-${ viewType } ` ) . addClass ( 'active' )
80+
81+ // Bind click events using on() method and prevent default behavior
82+ $ ( '.view-example, .view-html, .view-data' )
83+ . off ( 'click.viewToggle' )
84+ . on ( 'click.viewToggle' , function ( e ) {
85+ e . preventDefault ( )
86+ const newType = $ ( this ) . hasClass ( 'view-example' ) ? 'example' :
87+ $ ( this ) . hasClass ( 'view-html' ) ? 'html' : 'data'
88+ const currentHref = location . hash . slice ( 1 ) . split ( '#' ) [ 0 ] || 'welcome.html'
89+
90+ // example is the default view, no need to add #view-example
91+ if ( newType === 'example' ) {
92+ location . hash = currentHref
93+ } else {
94+ location . hash = `${ currentHref } #view-${ newType } `
95+ }
96+ } )
8197
82- $ ( '.view-online' ) . attr ( 'href' , `https://live.bootstrap-table.com/example/ ${
83- location . hash . slice ( 1 ) . split ( '#' ) [ 0 ] || 'welcome.html' } `)
98+ // Update online editor link
99+ $ ( '.view-online' ) . attr ( 'href' , `https://live.bootstrap-table.com/example/ ${ href } `)
84100 }
85101}
86102
@@ -144,13 +160,13 @@ const setup = () => {
144160 } )
145161 }
146162 const initData = ( ) => {
147- const { href, isViewSource } = Utils . getUrl ( )
163+ const { href, viewType } = Utils . getUrl ( )
148164
149- Utils . loadUrl ( theme . value , href , isViewSource )
165+ Utils . loadUrl ( theme . value , href , viewType )
150166
151167 currentHref . value = `#${ href } `
152168
153- Utils . initViewSource ( )
169+ Utils . initViewToggle ( )
154170 }
155171 const initViews = ( ) => {
156172 $ ( '[data-bs-toggle="tooltip"]' ) . tooltip ( )
0 commit comments