Skip to content

Commit

Permalink
Optionally use minified JS files for dynamic maps
Browse files Browse the repository at this point in the history
  • Loading branch information
lindseydiloreto committed Oct 18, 2023
1 parent b3a2112 commit 8c338d7
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added
- Address fields can now be added to the search index.
- Optionally use minified JavaScript files for dynamic maps on the front-end.

## 1.1.0 - 2023-10-09

Expand Down
14 changes: 13 additions & 1 deletion docs/getting-started/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ return [
// Mapbox Access Token (required)
'accessToken' => getenv('MAPBOX_ACCESSTOKEN'),

// Whether to log JS progress to the console (when a map is rendered)
// Manage JS resources (when rendering a dynamic map on the front-end)
'enableJsLogging' => true,
'minifyJsFiles' => false,

// Additional optional parameters for configuring Address fields
'fieldParams' => [
Expand Down Expand Up @@ -56,6 +57,17 @@ Whether to [log dynamic map progress](/dynamic-maps/troubleshooting/) to the Jav
'enableJsLogging' => false
```

### `minifyJsFiles`

_bool_ - Defaults to `false`.

Whether to use minified JavaScript files when rendering a dynamic map on the front-end.

```php
// Use minified JS files to render a map on the front-end
'minifyJsFiles' => true
```

### `fieldParams`

_array_ - Defaults to the following array:
Expand Down
15 changes: 15 additions & 0 deletions docs/models/settings-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ _string_ - (Required) Your unique [Mapbox Access Token](/getting-started/access-
### `enableJsLogging`

_bool_ - Whether to allow logging to the JavaScript console. Only relevant when displaying a dynamic map. Defaults to `true`.

### `minifyJsFiles`

_bool_ - Whether to use minified front-end JavaScript files. Only relevant when rendering a dynamic map. Defaults to `false`.

### `fieldParams`

_array_ - Additional optional parameters for configuring Address fields.

---
---

:::warning Additional Details
For more detailed information, check out the docs regarding the [PHP Config File...](/getting-started/config/)
:::
3 changes: 2 additions & 1 deletion src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
// Mapbox Access Token (required)
//'accessToken' => getenv('MAPBOX_ACCESSTOKEN'),

// Whether to log JS progress to the console (when a map is rendered)
// Manage JS resources (when rendering a dynamic map on the front-end)
//'enableJsLogging' => true,
//'minifyJsFiles' => false,

// Additional optional parameters for configuring Address fields
//'fieldParams' => [
Expand Down
11 changes: 9 additions & 2 deletions src/helpers/Mapbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Craft;
use craft\base\Element;
use craft\web\View;
use doublesecretagency\mapbox\MapboxPlugin;
use doublesecretagency\mapbox\models\DynamicMap;
use doublesecretagency\mapbox\models\Location;
use yii\base\Exception;
Expand Down Expand Up @@ -49,12 +50,18 @@ public static function getAssets(string $service, array $params = []): array
$manager = Craft::$app->getAssetManager();
$assets = '@doublesecretagency/mapbox/resources';

// Whether to use minified JavaScript files
$minifyJsFiles = (MapboxPlugin::$plugin->getSettings()->minifyJsFiles ?? false);

// Optionally use minified files
$min = ($minifyJsFiles ? 'min.' : '');

// Link to API URL
$files = [self::getApiUrl($service, $params)];

// Append both JS files required by plugin
$files[] = $manager->getPublishedUrl($assets, true, 'js/mapbox.js');
$files[] = $manager->getPublishedUrl($assets, true, 'js/dynamicmap.js');
$files[] = $manager->getPublishedUrl($assets, true, "js/mapbox.{$min}js");
$files[] = $manager->getPublishedUrl($assets, true, "js/dynamicmap.{$min}js");

// Return list of files
return $files;
Expand Down
5 changes: 5 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class Settings extends Model
*/
public bool $enableJsLogging = true;

/**
* @var bool Whether to use minified front-end JavaScript files. Only relevant when rendering a dynamic map.
*/
public bool $minifyJsFiles = false;

/**
* @var array Additional optional parameters for configuring Address fields.
*/
Expand Down
1 change: 1 addition & 0 deletions src/resources/js/dynamicmap.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/resources/js/mapbox.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions src/web/assets/JsApiAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace doublesecretagency\mapbox\web\assets;

use craft\web\AssetBundle;
use doublesecretagency\mapbox\MapboxPlugin;

/**
* Class JsApiAsset
Expand All @@ -32,9 +33,16 @@ public function init(): void
MapboxAsset::class,
];

// Whether to use minified JavaScript files
$minifyJsFiles = (MapboxPlugin::$plugin->getSettings()->minifyJsFiles ?? false);

// Optionally use minified files
$min = ($minifyJsFiles ? 'min.' : '');

// Load JS files
$this->js = [
'js/mapbox.js',
'js/dynamicmap.js',
"js/mapbox.{$min}js",
"js/dynamicmap.{$min}js",
];
}

Expand Down

0 comments on commit 8c338d7

Please sign in to comment.