Merge pull request #19438 from colemanw/afformDropAttrSupport
[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 */
20 class CRM_Utils_VisualBundle {
21
22 public static function register() {
23 Civi::resources()->addScriptUrl(Civi::service('asset_manager')->getUrl('visual-bundle.js'));
24 Civi::resources()->addStyleUrl(Civi::service('asset_manager')->getUrl('visual-bundle.css'));
25 }
26
27 /**
28 * Generate asset content (when accessed via AssetBuilder).
29 *
30 * @param \Civi\Core\Event\GenericHookEvent $event
31 * @see CRM_Utils_hook::buildAsset()
32 * @see \Civi\Core\AssetBuilder
33 */
34 public static function buildAssetJs($event) {
35 if ($event->asset !== 'visual-bundle.js') {
36 return;
37 }
38
39 $files = [
40 'crossfilter' => '[civicrm.bower]/crossfilter-1.3.x/crossfilter.min.js',
41 'd3' => '[civicrm.bower]/d3-3.5.x/d3.min.js',
42 'dc' => '[civicrm.bower]/dc-2.1.x/dc.min.js',
43 ];
44
45 $content = [];
46 $content[] = "(function(){";
47 $content[] = "var backups = {d3: window.d3, crossfilter: window.crossfilter, dc: window.dc}";
48 $content[] = 'window.CRM = window.CRM || {};';
49 $content[] = 'CRM.visual = CRM.visual || {};';
50 foreach ($files as $var => $file) {
51 $content[] = "// File: $file";
52 $content[] = file_get_contents(Civi::paths()->getPath($file));
53 }
54 foreach ($files as $var => $file) {
55 $content[] = "CRM.visual.$var = $var;";
56 }
57 foreach ($files as $var => $file) {
58 $content[] = "window.$var = backups.$var;";
59 }
60 $content[] = "})();";
61
62 $event->mimeType = 'application/javascript';
63 $event->content = implode("\n", $content);
64 }
65
66 /**
67 * Generate asset content (when accessed via AssetBuilder).
68 *
69 * @param \Civi\Core\Event\GenericHookEvent $event
70 * @see CRM_Utils_hook::buildAsset()
71 * @see \Civi\Core\AssetBuilder
72 */
73 public static function buildAssetCss($event) {
74 if ($event->asset !== 'visual-bundle.css') {
75 return;
76 }
77
78 $files = [
79 '[civicrm.bower]/dc-2.1.x/dc.min.css',
80 ];
81
82 $content = [];
83 foreach ($files as $file) {
84 $content[] = "// File: $file";
85 $content[] = file_get_contents(Civi::paths()->getPath($file));
86 }
87
88 $event->mimeType = 'text/css';
89 $event->content = implode("\n", $content);
90 }
91
92 }