@@ -21,20 +21,23 @@ const AjaxLoad = (($) => {
21
21
const JQUERY_NO_CONFLICT = $ . fn [ NAME ]
22
22
23
23
const ClassName = {
24
- ACTIVE : 'active' ,
25
- NAV_PILLS : 'nav-pills' ,
26
- NAV_TABS : 'nav-tabs' ,
27
- OPEN : 'open'
24
+ ACTIVE : 'active' ,
25
+ NAV_PILLS : 'nav-pills' ,
26
+ NAV_TABS : 'nav-tabs' ,
27
+ OPEN : 'open' ,
28
+ VIEW_SCRIPT : 'view-script'
28
29
}
29
30
30
31
const Event = {
31
32
CLICK : 'click'
32
33
}
33
34
34
35
const Selector = {
36
+ HEAD : 'head' ,
35
37
NAV_DROPDOWN : '.sidebar-nav .nav-dropdown' ,
36
38
NAV_LINK : '.sidebar-nav .nav-link' ,
37
- NAV_ITEM : '.sidebar-nav .nav-item'
39
+ NAV_ITEM : '.sidebar-nav .nav-item' ,
40
+ VIEW_SCRIPT : '.view-script'
38
41
}
39
42
40
43
const Default = {
@@ -74,21 +77,48 @@ const AjaxLoad = (($) => {
74
77
const element = this . _element
75
78
const config = this . _config
76
79
80
+ const loadScripts = ( src , element = 0 ) => {
81
+ const script = document . createElement ( 'script' )
82
+ script . type = 'text/javascript'
83
+ script . src = src [ element ]
84
+ script . className = ClassName . VIEW_SCRIPT
85
+ // eslint-disable-next-line no-multi-assign
86
+ script . onload = script . onreadystatechange = function ( ) {
87
+ if ( ! this . readyState || this . readyState === 'complete' ) {
88
+ if ( src . length > element + 1 ) {
89
+ loadScripts ( src , element + 1 )
90
+ }
91
+ }
92
+ }
93
+ const body = document . getElementsByTagName ( 'body' ) [ 0 ]
94
+ body . appendChild ( script )
95
+ }
96
+
77
97
$ . ajax ( {
78
98
type : 'GET' ,
79
99
url : config . subpagesDirectory + url ,
80
100
dataType : 'html' ,
81
- cache : false ,
82
- async : false ,
83
- success : function success ( ) {
101
+ beforeSend ( ) {
102
+ $ ( Selector . VIEW_SCRIPT ) . remove ( )
103
+ } ,
104
+ success ( result ) {
105
+ const wrapper = document . createElement ( 'div' )
106
+ wrapper . innerHTML = result
107
+
108
+ const scripts = Array . from ( wrapper . querySelectorAll ( 'script' ) ) . map ( ( script ) => script . attributes . getNamedItem ( 'src' ) . nodeValue )
109
+
110
+ wrapper . querySelectorAll ( 'script' ) . forEach ( ( script ) => script . parentNode . removeChild ( script ) )
111
+
84
112
$ ( 'body' ) . animate ( {
85
113
scrollTop : 0
86
114
} , 0 )
87
- $ ( element ) . load ( config . subpagesDirectory + url , null , ( ) => {
88
- window . location . hash = url
89
- } )
115
+ $ ( element ) . html ( wrapper )
116
+ if ( scripts . length ) {
117
+ loadScripts ( scripts )
118
+ }
119
+ window . location . hash = url
90
120
} ,
91
- error : function error ( ) {
121
+ error ( ) {
92
122
window . location . href = config . errorPage
93
123
}
94
124
} )
0 commit comments