Merge pull request #15854 from eileenmcnaughton/test_fix
[civicrm-core.git] / CRM / Utils / VisualBundle.php
CommitLineData
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 * $Id$
20 *
21 */
22class CRM_Utils_VisualBundle {
23
24 public static function register() {
25 Civi::resources()->addScriptUrl(Civi::service('asset_manager')->getUrl('visual-bundle.js'));
1b6476e6 26 Civi::resources()->addStyleUrl(Civi::service('asset_manager')->getUrl('visual-bundle.css'));
64fe847e
TO
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 */
1b6476e6 36 public static function buildAssetJs($event) {
64fe847e
TO
37 if ($event->asset !== 'visual-bundle.js') {
38 return;
39 }
40
be2fb01f 41 $files = [
64fe847e
TO
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',
be2fb01f 45 ];
64fe847e 46
be2fb01f 47 $content = [];
64fe847e
TO
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
1b6476e6
TO
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
be2fb01f 80 $files = [
1b6476e6 81 '[civicrm.bower]/dc-2.1.x/dc.min.css',
be2fb01f 82 ];
1b6476e6 83
be2fb01f 84 $content = [];
1b6476e6
TO
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
64fe847e 94}