From 64fe847eab88e5b9196496478bf6177c81408eb4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 19 Sep 2017 20:14:40 -0700 Subject: [PATCH] CRM-21179 - visual-bundle.js - Generate custom build of dc.js This defines an asset named `visual-bundle.js`. It creates a custom build of `dc.js` + `crossfilter.js` + `d3.js` which loads all the services into the namespace `CRM.visual.{foo}`, which allows it to coexist with other copies of the library. --- CRM/Utils/VisualBundle.php | 83 ++++++++++++++++++++++++++++++++++++++ Civi/Core/Container.php | 1 + 2 files changed, 84 insertions(+) create mode 100644 CRM/Utils/VisualBundle.php diff --git a/CRM/Utils/VisualBundle.php b/CRM/Utils/VisualBundle.php new file mode 100644 index 0000000000..5f52b863f6 --- /dev/null +++ b/CRM/Utils/VisualBundle.php @@ -0,0 +1,83 @@ +addScriptUrl(Civi::service('asset_manager')->getUrl('visual-bundle.js')); + } + + /** + * Generate asset content (when accessed via AssetBuilder). + * + * @param \Civi\Core\Event\GenericHookEvent $event + * @see CRM_Utils_hook::buildAsset() + * @see \Civi\Core\AssetBuilder + */ + public static function buildAsset($event) { + if ($event->asset !== 'visual-bundle.js') { + return; + } + + $files = array( + 'crossfilter' => '[civicrm.bower]/crossfilter-1.3.x/crossfilter.min.js', + 'd3' => '[civicrm.bower]/d3-3.5.x/d3.min.js', + 'dc' => '[civicrm.bower]/dc-2.1.x/dc.min.js', + ); + + $content = array(); + $content[] = "(function(){"; + $content[] = "var backups = {d3: window.d3, crossfilter: window.crossfilter, dc: window.dc}"; + $content[] = 'window.CRM = window.CRM || {};'; + $content[] = 'CRM.visual = CRM.visual || {};'; + foreach ($files as $var => $file) { + $content[] = "// File: $file"; + $content[] = file_get_contents(Civi::paths()->getPath($file)); + } + foreach ($files as $var => $file) { + $content[] = "CRM.visual.$var = $var;"; + } + foreach ($files as $var => $file) { + $content[] = "window.$var = backups.$var;"; + } + $content[] = "})();"; + + $event->mimeType = 'application/javascript'; + $event->content = implode("\n", $content); + } + +} diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 23a5108f38..3adf703c9e 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -298,6 +298,7 @@ class Container { $dispatcher->addListener('hook_civicrm_eventDefs', array('\Civi\API\Events', 'hookEventDefs')); $dispatcher->addListener('hook_civicrm_eventDefs', array('\Civi\Core\Event\SystemInstallEvent', 'hookEventDefs')); $dispatcher->addListener('hook_civicrm_buildAsset', array('\Civi\Angular\Page\Modules', 'buildAngularModules')); + $dispatcher->addListener('hook_civicrm_buildAsset', array('\CRM_Utils_VisualBundle', 'buildAsset')); $dispatcher->addListener('civi.dao.postInsert', array('\CRM_Core_BAO_RecurringEntity', 'triggerInsert')); $dispatcher->addListener('civi.dao.postUpdate', array('\CRM_Core_BAO_RecurringEntity', 'triggerUpdate')); $dispatcher->addListener('civi.dao.postDelete', array('\CRM_Core_BAO_RecurringEntity', 'triggerDelete')); -- 2.25.1