Skip to content

Commit 3db8a3d

Browse files
committed
Added dynamic scripts addition for custom components and widgets
1 parent 3d1cf99 commit 3db8a3d

File tree

14 files changed

+379
-1
lines changed

14 files changed

+379
-1
lines changed

src/Darryldecode/Backend/BackendServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public function bootBackend()
7171
$backendRegistrar->initViews();
7272
$backendRegistrar->initNavigation();
7373
$backendRegistrar->initPermissions();
74+
$backendRegistrar->initAddHeaderScripts();
75+
$backendRegistrar->initAddFooterScripts();
7476

7577
// load views
7678
foreach($backendRegistrar->getViewsPaths() as $view)

src/Darryldecode/Backend/Base/Etc/scaffolds/Component

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,64 @@ class Component implements ComponentInterface {
9595
'namespace' => 'App\\Backend\\Components\\{{componentNamespace}}\\Controllers',
9696
);
9797
}
98+
99+
/**
100+
* you can add scripts or css links here on header
101+
*
102+
* @return array
103+
*/
104+
public function getHeaderScripts()
105+
{
106+
/*
107+
NOTE:
108+
109+
css and js are important keys to identify whether a link is a javascript or css
110+
notice that forward slash in the beginning is present. Don't miss that!
111+
112+
example:
113+
114+
array(
115+
'css' => array(
116+
'/my-component/css/component-style.css',
117+
'/my-component/css/component-style2.css',
118+
),
119+
'js' => array(
120+
'/my-component/js/component-js.css',
121+
'/my-component/js/component-js.css',
122+
)
123+
);
124+
125+
*/
126+
127+
return array();
128+
}
129+
130+
/**
131+
* you can add scripts or css links here on footer
132+
*
133+
* @return array
134+
*/
135+
public function getFooterScripts()
136+
{
137+
/*
138+
NOTE:
139+
140+
css and js are important keys to identify whether a link is a javascript or css
141+
notice that forward slash in the beginning is present. Don't miss that!
142+
143+
example:
144+
145+
array(
146+
'js' => array(
147+
'/my-component/js/component-js.css',
148+
'/my-component/js/component-js.css',
149+
)
150+
);
151+
152+
*/
153+
154+
return array();
155+
}
98156
}
99157

100158
return new Component;

src/Darryldecode/Backend/Base/Etc/scaffolds/Widget

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,64 @@ class Widget implements WidgetInterface
4646
{
4747
return true;
4848
}
49+
50+
/**
51+
* you can add scripts or css links here on header
52+
*
53+
* @return array
54+
*/
55+
public function getHeaderScripts()
56+
{
57+
/*
58+
NOTE:
59+
60+
css and js are important keys to identify whether a link is a javascript or css
61+
notice that forward slash in the beginning is present. Don't miss that!
62+
63+
example:
64+
65+
array(
66+
'css' => array(
67+
'/my-component/css/component-style.css',
68+
'/my-component/css/component-style2.css',
69+
),
70+
'js' => array(
71+
'/my-component/js/component-js.js',
72+
'/my-component/js/component-js.js',
73+
)
74+
);
75+
76+
*/
77+
78+
return array();
79+
}
80+
81+
/**
82+
* you can add scripts or css links here on footer
83+
*
84+
* @return array
85+
*/
86+
public function getFooterScripts()
87+
{
88+
/*
89+
NOTE:
90+
91+
css and js are important keys to identify whether a link is a javascript or css
92+
notice that forward slash in the beginning is present. Don't miss that!
93+
94+
example:
95+
96+
array(
97+
'js' => array(
98+
'/my-component/js/component-js.js',
99+
'/my-component/js/component-js.js',
100+
)
101+
);
102+
103+
*/
104+
105+
return array();
106+
}
49107
}
50108

51109
return new Widget();

src/Darryldecode/Backend/Base/Registrar/ComponentInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,18 @@ public function getViewsPath();
4545
* @return array
4646
*/
4747
public function getRoutesControl();
48+
49+
/**
50+
* get component scripts for header
51+
*
52+
* @return array
53+
*/
54+
public function getHeaderScripts();
55+
56+
/**
57+
* get component scripts for footer
58+
*
59+
* @return array
60+
*/
61+
public function getFooterScripts();
4862
}

src/Darryldecode/Backend/Base/Registrar/Registrar.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Registrar {
1313
/**
1414
* the laravel backend version
1515
*/
16-
const VERSION = '1.0.18';
16+
const VERSION = '1.0.19';
1717
const VERSION_NAME = 'Alpha';
1818

1919
/**
@@ -56,6 +56,20 @@ class Registrar {
5656
*/
5757
protected $routes = array();
5858

59+
/**
60+
* the header scripts
61+
*
62+
* @var array
63+
*/
64+
protected $headerScripts = array();
65+
66+
/**
67+
* the footer scripts
68+
*
69+
* @var array
70+
*/
71+
protected $footerScripts = array();
72+
5973
public function __construct()
6074
{
6175
//
@@ -102,6 +116,8 @@ public function addWidget($widget)
102116
else
103117
{
104118
array_push($this->activeWidgets, $widget);
119+
if(count($widget->getHeaderScripts()) > 0) array_push($this->headerScripts, $widget->getHeaderScripts());
120+
if(count($widget->getFooterScripts()) > 0) array_push($this->footerScripts, $widget->getFooterScripts());
105121
}
106122

107123
return $this;
@@ -157,6 +173,28 @@ public function initViews()
157173
}
158174
}
159175

176+
/**
177+
* init added header scripts
178+
*/
179+
public function initAddHeaderScripts()
180+
{
181+
foreach($this->activeComponents as $component)
182+
{
183+
if(count($component->getHeaderScripts()) > 0) array_push($this->headerScripts, $component->getHeaderScripts());
184+
}
185+
}
186+
187+
/**
188+
* init added footer scripts
189+
*/
190+
public function initAddFooterScripts()
191+
{
192+
foreach($this->activeComponents as $component)
193+
{
194+
if(count($component->getFooterScripts()) > 0) array_push($this->footerScripts, $component->getFooterScripts());
195+
}
196+
}
197+
160198
/**
161199
* get navigations
162200
*
@@ -229,4 +267,24 @@ public function getVersion()
229267
'name' => self::VERSION_NAME,
230268
);
231269
}
270+
271+
/**
272+
* get added header scripts by all active components
273+
*
274+
* @return array
275+
*/
276+
public function getAddedHeaderScripts()
277+
{
278+
return $this->headerScripts;
279+
}
280+
281+
/**
282+
* get added footer scripts by all active components
283+
*
284+
* @return array
285+
*/
286+
public function getAddedFooterScripts()
287+
{
288+
return $this->footerScripts;
289+
}
232290
}

src/Darryldecode/Backend/Base/Registrar/WidgetInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,18 @@ public function getWidgetPosition();
3232
* @return bool
3333
*/
3434
public function isWidgetActive();
35+
36+
/**
37+
* get component scripts for header
38+
*
39+
* @return array
40+
*/
41+
public function getHeaderScripts();
42+
43+
/**
44+
* get component scripts for footer
45+
*
46+
* @return array
47+
*/
48+
public function getFooterScripts();
3549
}

src/Darryldecode/Backend/Base/Views/layouts/master.blade.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616
_CSRF = '{{csrf_token()}}';
1717
</script>
1818

19+
<!-- added header scripts-->
20+
@foreach($app['backend']->getAddedHeaderScripts() as $links)
21+
@foreach($links as $linkType => $links)
22+
@if($linkType == 'css')
23+
@foreach($links as $link)
24+
<link type="text/css" rel="stylesheet" href="{{$link}}"/>
25+
@endforeach
26+
@endif
27+
@if($linkType == 'js')
28+
@foreach($links as $link)
29+
<script src="{{$link}}" type="text/javascript"></script>
30+
@endforeach
31+
@endif
32+
@endforeach
33+
@endforeach
34+
<!-- /added header scripts-->
35+
1936
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
2037
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
2138
<!--[if lt IE 9]>
@@ -38,6 +55,23 @@
3855

3956
@include('backend::includes.scripts')
4057

58+
<!-- added footer scripts-->
59+
@foreach($app['backend']->getAddedFooterScripts() as $links)
60+
@foreach($links as $linkType => $links)
61+
@if($linkType == 'css')
62+
@foreach($links as $link)
63+
<link type="text/css" rel="stylesheet" href="{{$link}}"/>
64+
@endforeach
65+
@endif
66+
@if($linkType == 'js')
67+
@foreach($links as $link)
68+
<script src="{{$link}}" type="text/javascript"></script>
69+
@endforeach
70+
@endif
71+
@endforeach
72+
@endforeach
73+
<!-- /added footer scripts-->
74+
4175
</body>
4276

4377
</html>

src/Darryldecode/Backend/Components/Auth/Component.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@ public function getRoutesControl()
7575
'namespace' => 'Darryldecode\\Backend\\Components\\Auth\\Controllers',
7676
);
7777
}
78+
79+
/**
80+
* get component scripts for header
81+
*
82+
* @return array
83+
*/
84+
public function getHeaderScripts()
85+
{
86+
return array();
87+
}
88+
89+
/**
90+
* get component scripts for footer
91+
*
92+
* @return array
93+
*/
94+
public function getFooterScripts()
95+
{
96+
return array();
97+
}
7898
}
7999

80100
return new Component;

src/Darryldecode/Backend/Components/ContentBuilder/Component.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,26 @@ public function getRoutesControl()
154154
'namespace' => 'Darryldecode\\Backend\\Components\\ContentBuilder\\Controllers',
155155
);
156156
}
157+
158+
/**
159+
* get component scripts for header
160+
*
161+
* @return array
162+
*/
163+
public function getHeaderScripts()
164+
{
165+
return array();
166+
}
167+
168+
/**
169+
* get component scripts for footer
170+
*
171+
* @return array
172+
*/
173+
public function getFooterScripts()
174+
{
175+
return array();
176+
}
157177
}
158178

159179
return new Component;

0 commit comments

Comments
 (0)