From d75de1369f25fb8af019d8775b4484cf9827cf92 Mon Sep 17 00:00:00 2001 From: Cristian Torres Date: Sat, 2 Apr 2016 11:20:27 -0600 Subject: [PATCH 1/2] themedScript Added A function to call javascript files just by its name as it occurs with themedCSS --- view/Requirements.php | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/view/Requirements.php b/view/Requirements.php index 76cad5ffd..f6f81e9c4 100644 --- a/view/Requirements.php +++ b/view/Requirements.php @@ -174,6 +174,23 @@ class Requirements implements Flushable { public static function themedCSS($name, $module = null, $media = null) { return self::backend()->themedCSS($name, $module, $media); } + + /** + * Registers the given themeable javascript as required. + * + * A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, + * and it that doesn't exist and the module parameter is set then a javascript file with that name in + * the module is used. + * + * @param string $name The name of the file - eg '/javascript/File.js' would have the name 'File' + * @param string $module The module to fall back to if the javascript file does not exist in the + * current theme. + * @param string $type Comma-separated list of types to use in the script tag + * (e.g. 'text/javascript,text/ecmascript') + */ + public static function themedScript($name, $module = null, $type = null) { + return self::backend()->themedScript($name, $module, $type); + } /** * Clear either a single or all requirements @@ -1331,6 +1348,38 @@ class Requirements_Backend { } } + /** + * Registers the given themeable javascript as required. + * + * A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, + * and it that doesn't exist and the module parameter is set then a javascript file with that name in + * the module is used. + * + * @param string $name The name of the file - eg '/js/File.js' would have the name 'File' + * @param string $module The module to fall back to if the javascript file does not exist in the + * current theme. + * @param string $type Comma-separated list of types to use in the script tag + * (e.g. 'text/javascript,text/ecmascript') + */ + public function themedScript($name, $module = null, $type = null) { + $theme = SSViewer::get_theme_folder(); + $project = project(); + $absbase = BASE_PATH . DIRECTORY_SEPARATOR; + $abstheme = $absbase . $theme; + $absproject = $absbase . $project; + $js = "/javascript/$name.js"; + + if(file_exists($absproject . $js)) { + $this->javascript($project . $js); + } elseif($module && file_exists($abstheme . '_' . $module.$js)) { + $this->javascript($theme . '_' . $module . $js); + } elseif(file_exists($abstheme . $js)) { + $this->javascript($theme . $js); + } elseif($module) { + $this->javascript($module . $js); + } + } + /** * Output debugging information. */ From 2f84c3431bb6cd99596bc6d22decd9424eb2ad4c Mon Sep 17 00:00:00 2001 From: Cristian Torres Date: Wed, 18 May 2016 16:47:38 -0600 Subject: [PATCH 2/2] UPDATE: Throw exceptions on file missing The class will throw exceptions whenever the css or js file is not found in any context and the site is in development mode --- view/Requirements.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/view/Requirements.php b/view/Requirements.php index f6f81e9c4..0ee811ca4 100644 --- a/view/Requirements.php +++ b/view/Requirements.php @@ -188,8 +188,8 @@ class Requirements implements Flushable { * @param string $type Comma-separated list of types to use in the script tag * (e.g. 'text/javascript,text/ecmascript') */ - public static function themedScript($name, $module = null, $type = null) { - return self::backend()->themedScript($name, $module, $type); + public static function themedJavascript($name, $module = null, $type = null) { + return self::backend()->themedJavascript($name, $module, $type); } /** @@ -1345,6 +1345,8 @@ class Requirements_Backend { $this->css($theme . $css, $media); } elseif($module) { $this->css($module . $css, $media); + } elseif (Director::isDev()) { + throw Exception("The css file doesn't exists. Please check if the file $name.css exists in any context or search for themedCSS references calling this file in your templates."); } } @@ -1361,7 +1363,7 @@ class Requirements_Backend { * @param string $type Comma-separated list of types to use in the script tag * (e.g. 'text/javascript,text/ecmascript') */ - public function themedScript($name, $module = null, $type = null) { + public function themedJavascript($name, $module = null, $type = null) { $theme = SSViewer::get_theme_folder(); $project = project(); $absbase = BASE_PATH . DIRECTORY_SEPARATOR; @@ -1377,6 +1379,8 @@ class Requirements_Backend { $this->javascript($theme . $js); } elseif($module) { $this->javascript($module . $js); + } elseif (Director::isDev()) { + throw Exception("The javascript file doesn't exists. Please check if the file $name.js exists in any context or search for themedJavascript references calling this file in your templates."); } }