Merge pull request #4961 from pratikshad/core-bugs
[civicrm-core.git] / CRM / Core / Page / Angular.php
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 /**
16 * This function takes care of all the things common to all
17 * pages. This typically involves assigning the appropriate
18 * smarty variable :)
19 *
20 * @return string
21 * The content generated by running this page
22 */
23 public function run() {
24 $this->registerResources(CRM_Core_Resources::singleton());
25 return parent::run();
26 }
27
28 /**
29 * @param CRM_Core_Resources $res
30 */
31 public function registerResources(CRM_Core_Resources $res) {
32 $modules = self::getAngularModules();
33
34 $res->addSettingsFactory(function () use (&$modules) {
35 // TODO optimization; client-side caching
36 return array(
37 'resourceUrls' => CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(),
38 'angular' => array(
39 'modules' => array_merge(array('ngRoute'), array_keys($modules)),
40 ),
41 'crmAttachment' => array(
42 'token' => CRM_Core_Page_AJAX_Attachment::createToken(),
43 ),
44 );
45 });
46
47 $res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE);
48 $res->addScriptFile('civicrm', 'bower_components/angular-route/angular-route.min.js', 110, 'html-header', FALSE);
49 $headOffset = 0;
50 foreach ($modules as $module) {
51 if (!empty($module['css'])) {
52 foreach ($module['css'] as $file) {
53 $res->addStyleFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE);
54 }
55 }
56 if (!empty($module['js'])) {
57 foreach ($module['js'] as $file) {
58 $res->addScriptFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE);
59 }
60 }
61 }
62 }
63
64 /**
65 * Get a list of AngularJS modules which should be autoloaded
66 *
67 * @return array
68 * (string $name => array('ext' => string $key, 'js' => array $paths, 'css' => array $paths))
69 */
70 public static function getAngularModules() {
71 $angularModules = array();
72 $angularModules['angularFileUpload'] = array(
73 'ext' => 'civicrm',
74 'js' => array('bower_components/angular-file-upload/angular-file-upload.min.js'),
75 );
76 $angularModules['crmApp'] = array('ext' => 'civicrm', 'js' => array('js/angular-crmApp.js'));
77 $angularModules['crmAttachment'] = array(
78 'ext' => 'civicrm',
79 'js' => array('js/angular-crmAttachment.js'),
80 'css' => array('css/angular-crmAttachment.css'),
81 );
82 $angularModules['crmUi'] = array(
83 'ext' => 'civicrm',
84 'js' => array('js/angular-crm-ui.js', 'packages/ckeditor/ckeditor.js'),
85 );
86 $angularModules['crmUtil'] = array('ext' => 'civicrm', 'js' => array('js/angular-crm-util.js'));
87 // https://github.com/jwstadler/angular-jquery-dialog-service
88 $angularModules['dialogService'] = array(
89 'ext' => 'civicrm',
90 'js' => array('bower_components/angular-jquery-dialog-service/dialog-service.js'),
91 );
92 $angularModules['ngSanitize'] = array('ext' => 'civicrm', 'js' => array('js/angular-sanitize.js'));
93 $angularModules['ui.utils'] = array(
94 'ext' => 'civicrm',
95 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'),
96 );
97 $angularModules['ui.sortable'] = array(
98 'ext' => 'civicrm',
99 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'),
100 );
101 $angularModules['unsavedChanges'] = array(
102 'ext' => 'civicrm',
103 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'),
104 );
105
106 foreach (CRM_Core_Component::getEnabledComponents() as $component) {
107 $angularModules = array_merge($angularModules, $component->getAngularModules());
108 }
109 CRM_Utils_Hook::angularModules($angularModules);
110 return $angularModules;
111 }
112
113 }