| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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-2018 |
| 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 | Civi::resources()->addStyleUrl(Civi::service('asset_manager')->getUrl('visual-bundle.css')); |
| 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 | */ |
| 52 | public static function buildAssetJs($event) { |
| 53 | if ($event->asset !== 'visual-bundle.js') { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $files = array( |
| 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', |
| 61 | ); |
| 62 | |
| 63 | $content = array(); |
| 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 | |
| 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 | |
| 96 | $files = array( |
| 97 | '[civicrm.bower]/dc-2.1.x/dc.min.css', |
| 98 | ); |
| 99 | |
| 100 | $content = array(); |
| 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 | |
| 110 | } |