public static function jsPath($filename, $folder = '', $themeType = '')
{
if (!$themeType) {
$themeType = isMobile() ? 'mobile' : 'desktop';
}
// 1. Check for a url.
if (isUrl($filename)) {
return [$filename, $filename];
}
$paths = [];
// 2. Check for a full path.
if (strpos($filename, '/') === 0) {
$filename = ltrim($filename, '/');
// Direct path was given
$filename = "/{$filename}";
$path = PATH_ROOT . $filename;
if (file_exists($path)) {
deprecated(htmlspecialchars($path) . ": AssetModel::JsPath() with direct paths");
return [$path, $filename];
}
return false;
}
// 3. Check the theme.
$theme = Gdn::themeManager()->themeFromType($themeType);
if ($theme) {
$path = "/{$theme}/js/{$filename}";
$paths[] = [PATH_THEMES . $path, "/themes{$path}"];
}
// 4. Static, Plugin, or App relative file
if ($folder) {
if (in_array($folder, ['resources', 'static'])) {
$path = "/resources/js/{$filename}";
$paths[] = [PATH_ROOT . $path, $path];
// A plugin-relative path was given
} elseif (stringBeginsWith($folder, 'plugins/')) {
$folder = substr($folder, strlen('plugins/'));
$path = "/{$folder}/js/{$filename}";
$paths[] = [PATH_PLUGINS . $path, "/plugins{$path}"];
// Allow direct-to-file links for plugins
$paths[] = [PATH_PLUGINS . "/{$folder}/{$filename}", "/plugins/{$folder}/{$filename}", true];
// deprecated
// An app-relative path was given
} else {
// App-relative path under the theme
if ($theme) {
$path = "/{$theme}/{$folder}/js/{$filename}";
$paths[] = [PATH_THEMES . $path, "/themes{$path}"];
}
$path = "/{$folder}/js/{$filename}";
$paths[] = [PATH_APPLICATIONS . $path, "/applications{$path}"];
}
}
// 5. Check the global js folder.
$paths[] = [PATH_ROOT . "/js/{$filename}", "/js/{$filename}"];
$paths[] = [PATH_ROOT . "/js/library/{$filename}", "/js/library/{$filename}"];
foreach ($paths as $info) {
if (file_exists($info[0])) {
if (!empty($info[2])) {
// This path is deprecate. The script should be moved into a /js/ sub-directory
unset($info[2]);
deprecated("The file path '{$folder}/{$filename}'", "'{$folder}/js/{$filename}'");
}
return $info;
}
}
if (!stringEndsWith($filename, 'custom.js')) {
trace("Could not find file '{$filename}' in folder '{$folder}'.");
}
return false;
}