*/
const DEFAULT_MODULE_WEIGHT = 200;
+ /**
+ * @var CRM_Core_Resources
+ */
+ protected $res;
+
+
+ /**
+ * @var Civi\Angular\Manager
+ */
+ protected $angular;
+
+ /**
+ * @param string $title
+ * Title of the page.
+ * @param int $mode
+ * Mode of the page.
+ * @param CRM_Core_Resources|null $res
+ * Resource manager.
+ */
+ public function __construct($title = NULL, $mode = NULL, $res = NULL) {
+ parent::__construct($title, $mode);
+ $this->res = CRM_Core_Resources::singleton();
+ $this->angular = Civi\Core\Container::singleton()->get('angular');
+ }
+
/**
* This function takes care of all the things common to all
* pages. This typically involves assigning the appropriate
* smarty variable :)
*
- * @return string The content generated by running this page
+ * @return string
+ * The content generated by running this page
*/
public function run() {
- $this->registerResources(CRM_Core_Resources::singleton());
+ $this->registerResources();
return parent::run();
}
/**
- * @param CRM_Core_Resources $res
+ * Register resources required by Angular.
*/
- public function registerResources(CRM_Core_Resources $res) {
- $modules = self::getAngularModules();
+ public function registerResources() {
+ $modules = $this->angular->getModules();
- $res->addSettingsFactory(function () use (&$modules) {
+ $this->res->addSettingsFactory(function () use (&$modules) {
// TODO optimization; client-side caching
return array(
'resourceUrls' => CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(),
'angular' => array(
'modules' => array_merge(array('ngRoute'), array_keys($modules)),
+ 'cacheCode' => $this->res->getCacheCode(),
),
'crmAttachment' => array(
'token' => CRM_Core_Page_AJAX_Attachment::createToken(),
);
});
- $res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE);
- $res->addScriptFile('civicrm', 'bower_components/angular-route/angular-route.min.js', 110, 'html-header', FALSE);
+ $this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE);
+ $this->res->addScriptFile('civicrm', 'bower_components/angular-route/angular-route.min.js', 110, 'html-header', FALSE);
$headOffset = 0;
- foreach ($modules as $module) {
- if (!empty($module['css'])) {
- foreach ($module['css'] as $file) {
- $res->addStyleFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE);
- }
+ foreach ($modules as $moduleName => $module) {
+ foreach ($this->angular->getStyleUrls($moduleName) as $url) {
+ $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
}
- if (!empty($module['js'])) {
- foreach ($module['js'] as $file) {
- $res->addScriptFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE);
- }
+ foreach ($this->angular->getScriptUrls($moduleName) as $url) {
+ $this->res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
+ // addScriptUrl() bypasses the normal string-localization of addScriptFile(),
+ // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
}
}
}
-
- /**
- * Get a list of AngularJS modules which should be autoloaded
- *
- * @return array (string $name => array('ext' => string $key, 'js' => array $paths, 'css' => array $paths))
- */
- public static function getAngularModules() {
- $angularModules = array();
- $angularModules['angularFileUpload'] = array('ext' => 'civicrm', 'js' => array('bower_components/angular-file-upload/angular-file-upload.min.js'));
- $angularModules['crmApp'] = array('ext' => 'civicrm', 'js' => array('js/angular-crmApp.js'));
- $angularModules['crmAttachment'] = array('ext' => 'civicrm', 'js' => array('js/angular-crmAttachment.js'), 'css' => array('css/angular-crmAttachment.css'));
- $angularModules['crmUi'] = array('ext' => 'civicrm', 'js' => array('js/angular-crm-ui.js', 'packages/ckeditor/ckeditor.js'));
- $angularModules['crmUtil'] = array('ext' => 'civicrm', 'js' => array('js/angular-crm-util.js'));
- // https://github.com/jwstadler/angular-jquery-dialog-service
- $angularModules['dialogService'] = array('ext' => 'civicrm' , 'js' => array('bower_components/angular-jquery-dialog-service/dialog-service.js'));
- $angularModules['ngSanitize'] = array('ext' => 'civicrm', 'js' => array('js/angular-sanitize.js'));
- $angularModules['ui.utils'] = array('ext' => 'civicrm', 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'));
- $angularModules['ui.sortable'] = array('ext' => 'civicrm', 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'));
- $angularModules['unsavedChanges'] = array('ext' => 'civicrm', 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'));
-
- foreach (CRM_Core_Component::getEnabledComponents() as $component) {
- $angularModules = array_merge($angularModules, $component->getAngularModules());
- }
- CRM_Utils_Hook::angularModules($angularModules);
- return $angularModules;
- }
-
}