Merge pull request #22561 from mattwire/jobapi3
[civicrm-core.git] / ang / crmDashboard / crmDashboard.component.js
1 (function(angular, $, _) {
2
3 angular.module('crmDashboard').component('crmDashboard', {
4 templateUrl: '~/crmDashboard/Dashboard.html',
5 controller: function ($scope, $element, crmApi4, crmUiHelp, dialogService, crmStatus) {
6 var ts = $scope.ts = CRM.ts(),
7 ctrl = this;
8 this.columns = [[], []];
9 this.inactive = [];
10 this.contactDashlets = {};
11 this.sortableOptions = {
12 connectWith: '.crm-dashboard-droppable',
13 handle: '.crm-dashlet-header'
14 };
15 $scope.hs = crmUiHelp({file: 'CRM/Contact/Page/Dashboard'});
16
17 this.$onInit = function() {
18 // Sort dashlets into columns
19 _.each(CRM.crmDashboard.dashlets, function(dashlet) {
20 if (dashlet['dashboard_contact.is_active']) {
21 ctrl.columns[dashlet['dashboard_contact.column_no']].push(dashlet);
22 } else {
23 ctrl.inactive.push(dashlet);
24 }
25 });
26
27 $scope.$watchCollection('$ctrl.columns[0]', onChange);
28 $scope.$watchCollection('$ctrl.columns[1]', onChange);
29 };
30
31 var save = _.debounce(function() {
32 $scope.$apply(function() {
33 var toSave = [];
34 _.each(ctrl.inactive, function(dashlet) {
35 if (dashlet['dashboard_contact.id']) {
36 toSave.push({
37 dashboard_id: dashlet.id,
38 id: dashlet['dashboard_contact.id'],
39 is_active: false
40 });
41 }
42 });
43 _.each(ctrl.columns, function(dashlets, col) {
44 _.each(dashlets, function(dashlet, index) {
45 var item = {
46 dashboard_id: dashlet.id,
47 is_active: true,
48 column_no: col,
49 weight: index
50 };
51 if (dashlet['dashboard_contact.id']) {
52 item.id = dashlet['dashboard_contact.id'];
53 }
54 toSave.push(item);
55 });
56 });
57 crmStatus({}, crmApi4('DashboardContact', 'save', {
58 records: toSave,
59 defaults: {contact_id: 'user_contact_id'}
60 }, 'dashboard_id'))
61 .then(function(results) {
62 _.each(ctrl.columns, function(dashlets) {
63 _.each(dashlets, function(dashlet) {
64 dashlet['dashboard_contact.id'] = results[dashlet.id].id;
65 });
66 });
67 });
68 });
69 }, 2000);
70
71 this.removeDashlet = function(column, index) {
72 ctrl.inactive.push(ctrl.columns[column][index]);
73 ctrl.columns[column].splice(index, 1);
74 };
75
76 this.deleteDashlet = function(index) {
77 crmStatus(
78 {start: ts('Deleting'), success: ts('Deleted')},
79 crmApi4('Dashboard', 'delete', {where: [['id', '=', ctrl.inactive[index].id]]})
80 );
81 ctrl.inactive.splice(index, 1);
82 };
83
84 this.showFullscreen = function(dashlet) {
85 ctrl.fullscreenDashlet = true;
86 var options = CRM.utils.adjustDialogDefaults({
87 width: '90%',
88 height: '90%',
89 autoOpen: false,
90 title: dashlet.label
91 });
92 dialogService.open('fullscreenDashlet', '~/crmDashboard/FullscreenDialog.html', dashlet, options)
93 .then(function() {
94 ctrl.fullscreenDashlet = null;
95 }, function() {
96 ctrl.fullscreenDashlet = null;
97 });
98 };
99
100 function onChange(newVal, oldVal) {
101 if (oldVal !== newVal) {
102 save();
103 }
104 }
105
106 }
107 });
108
109 })(angular, CRM.$, CRM._);