CRM-21179 - visual-bundle.js - Generate custom build of dc.js
[civicrm-core.git] / CRM / Utils / VisualBundle.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
34 * @copyright CiviCRM LLC (c) 2004-2017
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'));
42 }
43
44 /**
45 * Generate asset content (when accessed via AssetBuilder).
46 *
47 * @param \Civi\Core\Event\GenericHookEvent $event
48 * @see CRM_Utils_hook::buildAsset()
49 * @see \Civi\Core\AssetBuilder
50 */
51 public static function buildAsset($event) {
52 if ($event->asset !== 'visual-bundle.js') {
53 return;
54 }
55
56 $files = array(
57 'crossfilter' => '[civicrm.bower]/crossfilter-1.3.x/crossfilter.min.js',
58 'd3' => '[civicrm.bower]/d3-3.5.x/d3.min.js',
59 'dc' => '[civicrm.bower]/dc-2.1.x/dc.min.js',
60 );
61
62 $content = array();
63 $content[] = "(function(){";
64 $content[] = "var backups = {d3: window.d3, crossfilter: window.crossfilter, dc: window.dc}";
65 $content[] = 'window.CRM = window.CRM || {};';
66 $content[] = 'CRM.visual = CRM.visual || {};';
67 foreach ($files as $var => $file) {
68 $content[] = "// File: $file";
69 $content[] = file_get_contents(Civi::paths()->getPath($file));
70 }
71 foreach ($files as $var => $file) {
72 $content[] = "CRM.visual.$var = $var;";
73 }
74 foreach ($files as $var => $file) {
75 $content[] = "window.$var = backups.$var;";
76 }
77 $content[] = "})();";
78
79 $event->mimeType = 'application/javascript';
80 $event->content = implode("\n", $content);
81 }
82
83 }