Merge pull request #16169 from agh1/5.21.0-releasenotes-final
[civicrm-core.git] / CRM / Utils / VisualBundle.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This class defines the `visual-bundle.js` asset, which combines `dc.js`,
14 * `d3.js`, and `crossfilter.js` into one asset -- and puts the services
15 * in the `CRM.visual` namespace.
16 *
17 * @package CRM
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * $Id$
20 *
21 */
22 class CRM_Utils_VisualBundle {
23
24 public static function register() {
25 Civi::resources()->addScriptUrl(Civi::service('asset_manager')->getUrl('visual-bundle.js'));
26 Civi::resources()->addStyleUrl(Civi::service('asset_manager')->getUrl('visual-bundle.css'));
27 }
28
29 /**
30 * Generate asset content (when accessed via AssetBuilder).
31 *
32 * @param \Civi\Core\Event\GenericHookEvent $event
33 * @see CRM_Utils_hook::buildAsset()
34 * @see \Civi\Core\AssetBuilder
35 */
36 public static function buildAssetJs($event) {
37 if ($event->asset !== 'visual-bundle.js') {
38 return;
39 }
40
41 $files = [
42 'crossfilter' => '[civicrm.bower]/crossfilter-1.3.x/crossfilter.min.js',
43 'd3' => '[civicrm.bower]/d3-3.5.x/d3.min.js',
44 'dc' => '[civicrm.bower]/dc-2.1.x/dc.min.js',
45 ];
46
47 $content = [];
48 $content[] = "(function(){";
49 $content[] = "var backups = {d3: window.d3, crossfilter: window.crossfilter, dc: window.dc}";
50 $content[] = 'window.CRM = window.CRM || {};';
51 $content[] = 'CRM.visual = CRM.visual || {};';
52 foreach ($files as $var => $file) {
53 $content[] = "// File: $file";
54 $content[] = file_get_contents(Civi::paths()->getPath($file));
55 }
56 foreach ($files as $var => $file) {
57 $content[] = "CRM.visual.$var = $var;";
58 }
59 foreach ($files as $var => $file) {
60 $content[] = "window.$var = backups.$var;";
61 }
62 $content[] = "})();";
63
64 $event->mimeType = 'application/javascript';
65 $event->content = implode("\n", $content);
66 }
67
68 /**
69 * Generate asset content (when accessed via AssetBuilder).
70 *
71 * @param \Civi\Core\Event\GenericHookEvent $event
72 * @see CRM_Utils_hook::buildAsset()
73 * @see \Civi\Core\AssetBuilder
74 */
75 public static function buildAssetCss($event) {
76 if ($event->asset !== 'visual-bundle.css') {
77 return;
78 }
79
80 $files = [
81 '[civicrm.bower]/dc-2.1.x/dc.min.css',
82 ];
83
84 $content = [];
85 foreach ($files as $file) {
86 $content[] = "// File: $file";
87 $content[] = file_get_contents(Civi::paths()->getPath($file));
88 }
89
90 $event->mimeType = 'text/css';
91 $event->content = implode("\n", $content);
92 }
93
94 }