| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This page is simply a container; any Angular modules defined by CiviCRM (or by CiviCRM extensions) |
| 5 | * will be activated on this page. |
| 6 | * |
| 7 | * @link https://issues.civicrm.org/jira/browse/CRM-14479 |
| 8 | */ |
| 9 | class CRM_Core_Page_Angular extends CRM_Core_Page { |
| 10 | /** |
| 11 | * The weight to assign to any Angular JS module files |
| 12 | */ |
| 13 | const DEFAULT_MODULE_WEIGHT = 200; |
| 14 | |
| 15 | function run() { |
| 16 | $this->registerResources(CRM_Core_Resources::singleton()); |
| 17 | return parent::run(); |
| 18 | } |
| 19 | |
| 20 | public function registerResources(CRM_Core_Resources $res) { |
| 21 | $modules = self::getAngularModules(); |
| 22 | |
| 23 | $res->addSettingsFactory(function () use (&$modules) { |
| 24 | // TODO optimization; client-side caching |
| 25 | return array( |
| 26 | 'resourceUrls' => CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(), |
| 27 | 'angular' => array( |
| 28 | 'modules' => array_merge(array('ngRoute'), array_keys($modules)), |
| 29 | ), |
| 30 | ); |
| 31 | }); |
| 32 | |
| 33 | $res->addScriptFile('civicrm', 'packages/bower_components/angular/angular.min.js', 100, 'html-header', FALSE); |
| 34 | $res->addScriptFile('civicrm', 'packages/bower_components/angular-route/angular-route.min.js', 110, 'html-header', FALSE); |
| 35 | foreach ($modules as $module) { |
| 36 | if (!empty($module['css'])) { |
| 37 | foreach ($module['css'] as $file) { |
| 38 | $res->addStyleFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT, 'html-header', TRUE); |
| 39 | } |
| 40 | } |
| 41 | if (!empty($module['js'])) { |
| 42 | foreach ($module['js'] as $file) { |
| 43 | $res->addScriptFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT, 'html-header', TRUE); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get a list of AngularJS modules which should be autoloaded |
| 51 | * |
| 52 | * @return array (string $name => array('ext' => string $key, 'js' => array $paths, 'css' => array $paths)) |
| 53 | */ |
| 54 | public static function getAngularModules() { |
| 55 | $angularModules = array(); |
| 56 | $angularModules['ui.utils'] = array('ext' => 'civicrm', 'js' => array('packages/bower_components/angular-ui-utils/ui-utils.min.js')); |
| 57 | $angularModules['ui.sortable'] = array('ext' => 'civicrm', 'js' => array('packages/bower_components/angular-ui-sortable/sortable.min.js')); |
| 58 | |
| 59 | foreach (CRM_Core_Component::getEnabledComponents() as $component) { |
| 60 | $angularModules = array_merge($angularModules, $component->getAngularModules()); |
| 61 | } |
| 62 | CRM_Utils_Hook::angularModules($angularModules); |
| 63 | return $angularModules; |
| 64 | } |
| 65 | |
| 66 | } |