Commit | Line | Data |
---|---|---|
64fe847e TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
64fe847e | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
64fe847e TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * This class defines the `visual-bundle.js` asset, which combines `dc.js`, | |
30 | * `d3.js`, and `crossfilter.js` into one asset -- and puts the services | |
31 | * in the `CRM.visual` namespace. | |
32 | * | |
33 | * @package CRM | |
6b83d5bd | 34 | * @copyright CiviCRM LLC (c) 2004-2019 |
64fe847e TO |
35 | * $Id$ |
36 | * | |
37 | */ | |
38 | class CRM_Utils_VisualBundle { | |
39 | ||
40 | public static function register() { | |
41 | Civi::resources()->addScriptUrl(Civi::service('asset_manager')->getUrl('visual-bundle.js')); | |
1b6476e6 | 42 | Civi::resources()->addStyleUrl(Civi::service('asset_manager')->getUrl('visual-bundle.css')); |
64fe847e TO |
43 | } |
44 | ||
45 | /** | |
46 | * Generate asset content (when accessed via AssetBuilder). | |
47 | * | |
48 | * @param \Civi\Core\Event\GenericHookEvent $event | |
49 | * @see CRM_Utils_hook::buildAsset() | |
50 | * @see \Civi\Core\AssetBuilder | |
51 | */ | |
1b6476e6 | 52 | public static function buildAssetJs($event) { |
64fe847e TO |
53 | if ($event->asset !== 'visual-bundle.js') { |
54 | return; | |
55 | } | |
56 | ||
be2fb01f | 57 | $files = [ |
64fe847e TO |
58 | 'crossfilter' => '[civicrm.bower]/crossfilter-1.3.x/crossfilter.min.js', |
59 | 'd3' => '[civicrm.bower]/d3-3.5.x/d3.min.js', | |
60 | 'dc' => '[civicrm.bower]/dc-2.1.x/dc.min.js', | |
be2fb01f | 61 | ]; |
64fe847e | 62 | |
be2fb01f | 63 | $content = []; |
64fe847e TO |
64 | $content[] = "(function(){"; |
65 | $content[] = "var backups = {d3: window.d3, crossfilter: window.crossfilter, dc: window.dc}"; | |
66 | $content[] = 'window.CRM = window.CRM || {};'; | |
67 | $content[] = 'CRM.visual = CRM.visual || {};'; | |
68 | foreach ($files as $var => $file) { | |
69 | $content[] = "// File: $file"; | |
70 | $content[] = file_get_contents(Civi::paths()->getPath($file)); | |
71 | } | |
72 | foreach ($files as $var => $file) { | |
73 | $content[] = "CRM.visual.$var = $var;"; | |
74 | } | |
75 | foreach ($files as $var => $file) { | |
76 | $content[] = "window.$var = backups.$var;"; | |
77 | } | |
78 | $content[] = "})();"; | |
79 | ||
80 | $event->mimeType = 'application/javascript'; | |
81 | $event->content = implode("\n", $content); | |
82 | } | |
83 | ||
1b6476e6 TO |
84 | /** |
85 | * Generate asset content (when accessed via AssetBuilder). | |
86 | * | |
87 | * @param \Civi\Core\Event\GenericHookEvent $event | |
88 | * @see CRM_Utils_hook::buildAsset() | |
89 | * @see \Civi\Core\AssetBuilder | |
90 | */ | |
91 | public static function buildAssetCss($event) { | |
92 | if ($event->asset !== 'visual-bundle.css') { | |
93 | return; | |
94 | } | |
95 | ||
be2fb01f | 96 | $files = [ |
1b6476e6 | 97 | '[civicrm.bower]/dc-2.1.x/dc.min.css', |
be2fb01f | 98 | ]; |
1b6476e6 | 99 | |
be2fb01f | 100 | $content = []; |
1b6476e6 TO |
101 | foreach ($files as $file) { |
102 | $content[] = "// File: $file"; | |
103 | $content[] = file_get_contents(Civi::paths()->getPath($file)); | |
104 | } | |
105 | ||
106 | $event->mimeType = 'text/css'; | |
107 | $event->content = implode("\n", $content); | |
108 | } | |
109 | ||
64fe847e | 110 | } |