Commit | Line | Data |
---|---|---|
64fe847e TO |
1 | <?php |
2 | /* | |
bc77d7c0 TO |
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 | +--------------------------------------------------------------------+ | |
64fe847e TO |
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 | |
ca5cec67 | 18 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
64fe847e TO |
19 | */ |
20 | class CRM_Utils_VisualBundle { | |
21 | ||
22 | public static function register() { | |
23 | Civi::resources()->addScriptUrl(Civi::service('asset_manager')->getUrl('visual-bundle.js')); | |
1b6476e6 | 24 | Civi::resources()->addStyleUrl(Civi::service('asset_manager')->getUrl('visual-bundle.css')); |
64fe847e TO |
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 | */ | |
1b6476e6 | 34 | public static function buildAssetJs($event) { |
64fe847e TO |
35 | if ($event->asset !== 'visual-bundle.js') { |
36 | return; | |
37 | } | |
38 | ||
be2fb01f | 39 | $files = [ |
64fe847e TO |
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', | |
be2fb01f | 43 | ]; |
64fe847e | 44 | |
be2fb01f | 45 | $content = []; |
64fe847e TO |
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 | ||
1b6476e6 TO |
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 | ||
be2fb01f | 78 | $files = [ |
1b6476e6 | 79 | '[civicrm.bower]/dc-2.1.x/dc.min.css', |
be2fb01f | 80 | ]; |
1b6476e6 | 81 | |
be2fb01f | 82 | $content = []; |
1b6476e6 TO |
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 | ||
64fe847e | 92 | } |