Skip to content

Commit

Permalink
(v2.3.0) Allows comma-separated file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
lindseydiloreto committed Dec 18, 2020
1 parent 4e03b9b commit 3318e58
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Changelog

## 2.3.0 - 2020-12-18

### Added
- Now allows for multiple, comma-separated file paths. (thanks @nickolasjadams)

## 2.2.1 - 2020-04-04

### Changed
- Made the viewing area larger.
- Made the viewing area larger. (thanks @SayChi)

## 2.2.0 - 2019-06-24

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Easily insert additional JavaScript into the Craft Control Panel.

After you've installed the plugin, go to:

- **Settings > Plugins > Control Panel JS**
- **Settings > Control Panel JS**

Your custom JavaScript can be saved in either (or both) of two places:

**1) One or more external files in your public directory delimited by commas...**
**1) An external file in your public directory...**
![](src/resources/img/example-jsFile.png)

**2) The "Additional JavaScript" field on the settings page...**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "doublesecretagency/craft-cpjs",
"description": "Add custom JavaScript to your Control Panel.",
"type": "craft-plugin",
"version": "2.2.1",
"version": "2.3.0",
"keywords": [
"craft",
"cms",
Expand Down
2 changes: 1 addition & 1 deletion src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

{{ forms.autosuggestField({
label: "JavaScript File(s)"|t,
instructions: "Enter the path to your JavaScript file(s). Multiple filepaths may be delimited by commas."|t,
instructions: "Enter the path to a separate JS file. Multiple paths may be separated with commas. You may use aliases (ie: @web) in your filepath."|t,
id: 'jsFile',
name: 'jsFile',
suggestEnvVars: true,
Expand Down
38 changes: 24 additions & 14 deletions src/web/assets/CustomAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,43 @@ public function init()
{
parent::init();

// Requires standard CP assets to be loaded first
$this->depends = [CpAsset::class];

// Get plugin settings
$settings = CpJs::$plugin->getSettings();

$file = trim(Craft::parseEnv($settings['jsFile']));
// Get the file (or files) specified
$file = trim($settings['jsFile']);

$finalPaths = [];
// If no file was specified, bail
if (!$file) {
return;
}

if ($file) {
// Initialize a collection of paths
$paths = [];

$files = explode(',', $file);
foreach ($files as $file) {
$file = trim($file);
// Allow for comma-separated file paths
$files = explode(',', $file);

// Cache buster
if ($hash = @sha1_file($file)) {
$file .= '?e='.$hash;
}
// Loop through specified files
foreach ($files as $file) {

array_push($finalPaths, $file);
// Parse each filename for aliases
$file = Craft::parseEnv(trim($file));

// Bust the cache
if ($hash = @sha1_file($file)) {
$file .= '?e='.$hash;
}

// Load all cachebusted JS files
$this->js = $finalPaths;

// Add file to path collection
$paths[] = $file;
}

// Load all files
$this->js = $paths;
}

}

0 comments on commit 3318e58

Please sign in to comment.