mirror of
https://github.com/a2nt/silverstripe-webpack.git
synced 2024-10-22 17:05:31 +02:00
Minor code cleanups
This commit is contained in:
parent
bc685e4bfb
commit
478c5fc922
@ -12,8 +12,6 @@ class MapElementController extends ElementController
|
|||||||
{
|
{
|
||||||
parent::init();
|
parent::init();
|
||||||
|
|
||||||
DeferedRequirements::Auto();
|
|
||||||
|
|
||||||
DeferedRequirements::loadCSS('https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.css');
|
DeferedRequirements::loadCSS('https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.css');
|
||||||
DeferedRequirements::loadJS('https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.js');
|
DeferedRequirements::loadJS('https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.js');
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ use SilverStripe\ORM\ArrayList;
|
|||||||
use DNADesign\Elemental\Models\ElementContent;
|
use DNADesign\Elemental\Models\ElementContent;
|
||||||
use DNADesign\Elemental\Models\ElementalArea;
|
use DNADesign\Elemental\Models\ElementalArea;
|
||||||
use DNADesign\ElementalUserForms\Control\ElementFormController;
|
use DNADesign\ElementalUserForms\Control\ElementFormController;
|
||||||
|
use Site\Templates\DeferedRequirements;
|
||||||
|
|
||||||
class PageController extends ContentController
|
class PageController extends ContentController
|
||||||
{
|
{
|
||||||
@ -30,6 +31,13 @@ class PageController extends ContentController
|
|||||||
private $site_message;
|
private $site_message;
|
||||||
private $search_term;
|
private $search_term;
|
||||||
|
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
|
DeferedRequirements::Auto();
|
||||||
|
|
||||||
|
return parent::init();
|
||||||
|
}
|
||||||
|
|
||||||
public function index(HTTPRequest $request)
|
public function index(HTTPRequest $request)
|
||||||
{
|
{
|
||||||
$search = $request->getVar('q');
|
$search = $request->getVar('q');
|
||||||
@ -42,7 +50,7 @@ class PageController extends ContentController
|
|||||||
|
|
||||||
public function ElementalArea()
|
public function ElementalArea()
|
||||||
{
|
{
|
||||||
if ($this->CurrentElement() || $this->getAction() !== 'index') {
|
if($this->CurrentElement() || $this->getAction() !== 'index') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,10 +59,10 @@ class PageController extends ContentController
|
|||||||
|
|
||||||
public function CurrentElement()
|
public function CurrentElement()
|
||||||
{
|
{
|
||||||
$contoller_curr = Controller::curr();
|
$controller_curr = Controller::curr();
|
||||||
|
|
||||||
if (is_a($contoller_curr, ElementFormController::class)) {
|
if(is_a($controller_curr, ElementFormController::class)) {
|
||||||
return $contoller_curr;
|
return $controller_curr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -62,17 +70,25 @@ class PageController extends ContentController
|
|||||||
|
|
||||||
public function SearchForm()
|
public function SearchForm()
|
||||||
{
|
{
|
||||||
|
$config = $this->SiteConfig();
|
||||||
return Form::create(
|
return Form::create(
|
||||||
$this,
|
$this,
|
||||||
__FUNCTION__,
|
__FUNCTION__,
|
||||||
FieldList::create(
|
FieldList::create(
|
||||||
TextField::create('q', 'Search ...')
|
TextField::create('q', 'Search ...')
|
||||||
|
->setAttribute('placeholder', 'Search '.$config->getField('Title').' Website')
|
||||||
),
|
),
|
||||||
FieldList::create(
|
FieldList::create(
|
||||||
FormAction::create(
|
FormAction::create(
|
||||||
'doSearch',
|
'doSearch',
|
||||||
'Find it!'
|
'Find it!'
|
||||||
)
|
)
|
||||||
|
->setUseButtonTag(true)
|
||||||
|
->addExtraClass('btn-secondary')
|
||||||
|
->setButtonContent(
|
||||||
|
'<i class="fas fa-search"></i>'
|
||||||
|
.'<span class="sr-only">Search</span>'
|
||||||
|
)
|
||||||
),
|
),
|
||||||
RequiredFields::create(['q'])
|
RequiredFields::create(['q'])
|
||||||
)->setFormMethod('POST');
|
)->setFormMethod('POST');
|
||||||
@ -88,7 +104,7 @@ class PageController extends ContentController
|
|||||||
public function SearchResults()
|
public function SearchResults()
|
||||||
{
|
{
|
||||||
$term = $this->search_term;
|
$term = $this->search_term;
|
||||||
if (!$term) {
|
if(!$term) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +126,7 @@ class PageController extends ContentController
|
|||||||
|
|
||||||
foreach ($elements as $element) {
|
foreach ($elements as $element) {
|
||||||
$page = Page::get()->filter('ElementalAreaID', $element->getField('ParentID'))->first();
|
$page = Page::get()->filter('ElementalAreaID', $element->getField('ParentID'))->first();
|
||||||
if (!$page) {
|
if(!$page) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ class DeferedRequirements implements TemplateGlobalProvider
|
|||||||
{
|
{
|
||||||
private static $css = [];
|
private static $css = [];
|
||||||
private static $js = [];
|
private static $js = [];
|
||||||
private static $defered = false;
|
private static $deferred = false;
|
||||||
private static $static_domain;
|
private static $static_domain;
|
||||||
private static $version;
|
private static $version;
|
||||||
private static $nojquery = false;
|
private static $nojquery = false;
|
||||||
@ -38,11 +38,11 @@ class DeferedRequirements implements TemplateGlobalProvider
|
|||||||
$config = Config::inst()->get(self::class);
|
$config = Config::inst()->get(self::class);
|
||||||
$projectName = WebpackTemplateProvider::projectName();
|
$projectName = WebpackTemplateProvider::projectName();
|
||||||
$mainTheme = WebpackTemplateProvider::mainTheme();
|
$mainTheme = WebpackTemplateProvider::mainTheme();
|
||||||
$mainTheme = $mainTheme ? $mainTheme : $projectName;
|
$mainTheme = $mainTheme ?: $projectName;
|
||||||
|
|
||||||
$dir = Path::join(
|
$dir = Path::join(
|
||||||
Director::publicFolder(),
|
Director::publicFolder(),
|
||||||
ManifestFileFinder::RESOURCES_DIR,
|
RESOURCES_DIR,
|
||||||
$projectName,
|
$projectName,
|
||||||
'client',
|
'client',
|
||||||
'dist'
|
'dist'
|
||||||
@ -59,14 +59,14 @@ class DeferedRequirements implements TemplateGlobalProvider
|
|||||||
|
|
||||||
// Main libs
|
// Main libs
|
||||||
if (!$config['nojquery']) {
|
if (!$config['nojquery']) {
|
||||||
DeferedRequirements::loadJS('//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js');
|
self::loadJS('//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js');
|
||||||
}
|
}
|
||||||
// App libs
|
// App libs
|
||||||
if (!$config['nofontawesome']) {
|
if (!$config['nofontawesome']) {
|
||||||
DeferedRequirements::loadCSS('//use.fontawesome.com/releases/v5.4.0/css/all.css');
|
self::loadCSS('//use.fontawesome.com/releases/v5.4.0/css/all.css');
|
||||||
}
|
}
|
||||||
DeferedRequirements::loadCSS($mainTheme.'.css');
|
self::loadCSS($mainTheme.'.css');
|
||||||
DeferedRequirements::loadJS($mainTheme.'.js');
|
self::loadJS($mainTheme.'.js');
|
||||||
|
|
||||||
// Custom controller requirements
|
// Custom controller requirements
|
||||||
$class = get_class(Controller::curr());
|
$class = get_class(Controller::curr());
|
||||||
@ -87,17 +87,17 @@ class DeferedRequirements implements TemplateGlobalProvider
|
|||||||
$themePath = Path::join($cssPath, $mainTheme.'_'.$class . '.css');
|
$themePath = Path::join($cssPath, $mainTheme.'_'.$class . '.css');
|
||||||
$projectPath = Path::join($cssPath, $projectName.'_'.$class . '.css');
|
$projectPath = Path::join($cssPath, $projectName.'_'.$class . '.css');
|
||||||
if ($mainTheme && file_exists($themePath)) {
|
if ($mainTheme && file_exists($themePath)) {
|
||||||
DeferedRequirements::loadCSS($mainTheme.'_'.$class . '.css');
|
self::loadCSS($mainTheme.'_'.$class . '.css');
|
||||||
} elseif (file_exists($projectPath)) {
|
} elseif (file_exists($projectPath)) {
|
||||||
DeferedRequirements::loadCSS($projectName.'_'.$class . '.css');
|
self::loadCSS($projectName.'_'.$class . '.css');
|
||||||
}
|
}
|
||||||
|
|
||||||
$themePath = Path::join($jsPath, $mainTheme.'_'.$class . '.js');
|
$themePath = Path::join($jsPath, $mainTheme.'_'.$class . '.js');
|
||||||
$projectPath = Path::join($jsPath, $projectName.'_'.$class . '.js');
|
$projectPath = Path::join($jsPath, $projectName.'_'.$class . '.js');
|
||||||
if ($mainTheme && file_exists($themePath)) {
|
if ($mainTheme && file_exists($themePath)) {
|
||||||
DeferedRequirements::loadJS($mainTheme.'_'.$class . '.js');
|
self::loadJS($mainTheme.'_'.$class . '.js');
|
||||||
} elseif (file_exists($projectPath)) {
|
} elseif (file_exists($projectPath)) {
|
||||||
DeferedRequirements::loadJS($projectName.'_'.$class . '.js');
|
self::loadJS($projectName.'_'.$class . '.js');
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::forTemplate();
|
return self::forTemplate();
|
||||||
@ -105,8 +105,8 @@ class DeferedRequirements implements TemplateGlobalProvider
|
|||||||
|
|
||||||
public static function loadCSS($css)
|
public static function loadCSS($css)
|
||||||
{
|
{
|
||||||
$external = (mb_substr($css, 0, 2) === '//' || mb_substr($css, 0, 4) === 'http');
|
$external = (mb_strpos($css, '//') === 0 || mb_strpos($css, 'http') === 0);
|
||||||
if ($external || (self::$defered && !self::_webpackActive())) {
|
if ($external || (self::$deferred && !self::_webpackActive())) {
|
||||||
self::$css[] = $css;
|
self::$css[] = $css;
|
||||||
} else {
|
} else {
|
||||||
WebpackTemplateProvider::loadCSS($css);
|
WebpackTemplateProvider::loadCSS($css);
|
||||||
@ -118,7 +118,7 @@ class DeferedRequirements implements TemplateGlobalProvider
|
|||||||
/*$external = (mb_substr($js, 0, 2) === '//' || mb_substr($js, 0, 4) === 'http');
|
/*$external = (mb_substr($js, 0, 2) === '//' || mb_substr($js, 0, 4) === 'http');
|
||||||
if ($external || (self::$defered && !self::_webpackActive())) {*/
|
if ($external || (self::$defered && !self::_webpackActive())) {*/
|
||||||
// webpack supposed to load external JS
|
// webpack supposed to load external JS
|
||||||
if (self::$defered && !self::_webpackActive()) {
|
if (self::$deferred && !self::_webpackActive()) {
|
||||||
self::$js[] = $js;
|
self::$js[] = $js;
|
||||||
} else {
|
} else {
|
||||||
WebpackTemplateProvider::loadJS($js);
|
WebpackTemplateProvider::loadJS($js);
|
||||||
@ -130,9 +130,9 @@ class DeferedRequirements implements TemplateGlobalProvider
|
|||||||
return class_exists('WebpackTemplateProvider') && WebpackTemplateProvider::isActive();
|
return class_exists('WebpackTemplateProvider') && WebpackTemplateProvider::isActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function setDefered($bool)
|
public static function setDeferred($bool)
|
||||||
{
|
{
|
||||||
self::$defered = $bool;
|
self::$deferred = $bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function forTemplate()
|
public static function forTemplate()
|
||||||
@ -175,7 +175,7 @@ class DeferedRequirements implements TemplateGlobalProvider
|
|||||||
: ''; // no version defined
|
: ''; // no version defined
|
||||||
|
|
||||||
$static_domain = $config['static_domain'];
|
$static_domain = $config['static_domain'];
|
||||||
$static_domain = $static_domain ? $static_domain : '';
|
$static_domain = $static_domain ?: '';
|
||||||
|
|
||||||
return $url.$version;
|
return $url.$version;
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ class WebpackTemplateProvider implements TemplateGlobalProvider
|
|||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function get_template_global_variables()
|
public static function get_template_global_variables(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'WebpackDevServer' => 'isActive',
|
'WebpackDevServer' => 'isActive',
|
||||||
@ -49,7 +49,7 @@ class WebpackTemplateProvider implements TemplateGlobalProvider
|
|||||||
* Load CSS file
|
* Load CSS file
|
||||||
* @param $path
|
* @param $path
|
||||||
*/
|
*/
|
||||||
public static function loadCSS($path)
|
public static function loadCSS($path): void
|
||||||
{
|
{
|
||||||
if (self::isActive()) {
|
if (self::isActive()) {
|
||||||
return;
|
return;
|
||||||
@ -62,12 +62,12 @@ class WebpackTemplateProvider implements TemplateGlobalProvider
|
|||||||
* Load JS file
|
* Load JS file
|
||||||
* @param $path
|
* @param $path
|
||||||
*/
|
*/
|
||||||
public static function loadJS($path)
|
public static function loadJS($path): void
|
||||||
{
|
{
|
||||||
Requirements::javascript(self::_getPath($path));
|
Requirements::javascript(self::_getPath($path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function projectName()
|
public static function projectName(): string
|
||||||
{
|
{
|
||||||
return Config::inst()->get(ModuleManifest::class, 'project');
|
return Config::inst()->get(ModuleManifest::class, 'project');
|
||||||
}
|
}
|
||||||
@ -78,7 +78,7 @@ class WebpackTemplateProvider implements TemplateGlobalProvider
|
|||||||
return is_array($themes) && $themes[0] !== '$public' && $themes[0] !== '$default' ? $themes[0] : false;
|
return is_array($themes) && $themes[0] !== '$public' && $themes[0] !== '$default' ? $themes[0] : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function resourcesURL($link = null)
|
public static function resourcesURL($link = null): string
|
||||||
{
|
{
|
||||||
return Controller::join_links(Director::baseURL(), '/resources/'.self::projectName().'/client/dist/img/', $link);
|
return Controller::join_links(Director::baseURL(), '/resources/'.self::projectName().'/client/dist/img/', $link);
|
||||||
}
|
}
|
||||||
@ -88,41 +88,49 @@ class WebpackTemplateProvider implements TemplateGlobalProvider
|
|||||||
* Checks if dev mode is enabled and if webpack server is online
|
* Checks if dev mode is enabled and if webpack server is online
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function isActive()
|
public static function isActive(): bool
|
||||||
{
|
{
|
||||||
return Director::isDev() && !!@fsockopen(
|
$cfg = self::config();
|
||||||
Config::inst()->get(__CLASS__, 'HOSTNAME'),
|
return Director::isDev() && @fsockopen(
|
||||||
Config::inst()->get(__CLASS__, 'PORT')
|
$cfg['HOSTNAME'],
|
||||||
|
$cfg['PORT']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function _getPath($path)
|
protected static function _getPath($path): string
|
||||||
{
|
{
|
||||||
return self::isActive() && strpos($path, '//') === false ?
|
return self::isActive() && strpos($path, '//') === false ?
|
||||||
self::_toDevServerPath($path) :
|
self::_toDevServerPath($path) :
|
||||||
self::_toPublicPath($path);
|
self::_toPublicPath($path);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function _toDevServerPath($path)
|
protected static function _toDevServerPath($path): string
|
||||||
{
|
{
|
||||||
|
$cfg = self::config();
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'%s%s:%s/%s',
|
'%s%s:%s/%s',
|
||||||
Director::protocol(),
|
Director::protocol(),
|
||||||
Config::inst()->get(__CLASS__, 'HOSTNAME'),
|
$cfg['HOSTNAME'],
|
||||||
Config::inst()->get(__CLASS__, 'PORT'),
|
$cfg['PORT'],
|
||||||
basename($path)
|
basename($path)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function _toPublicPath($path)
|
protected static function _toPublicPath($path): string
|
||||||
{
|
{
|
||||||
|
$cfg = self::config();
|
||||||
return strpos($path, '//') === false ?
|
return strpos($path, '//') === false ?
|
||||||
Controller::join_links(
|
Controller::join_links(
|
||||||
self::projectName(),
|
self::projectName(),
|
||||||
Config::inst()->get(__CLASS__, 'DIST'),
|
$cfg['DIST'],
|
||||||
(strpos($path, '.css') ? 'css' : 'js'),
|
(strpos($path, '.css') ? 'css' : 'js'),
|
||||||
$path
|
$path
|
||||||
)
|
)
|
||||||
: $path;
|
: $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function config(): array
|
||||||
|
{
|
||||||
|
return Config::inst()->get(__CLASS__);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user