CRM-16173 - crmCxn - Add UI for connecting and disconnecting
[civicrm-core.git] / ang / crmCxn / ManageCtrl.js
1 (function(angular, $, _) {
2
3 angular.module('crmCxn').controller('CrmCxnManageCtrl', function CrmCxnManageCtrl($scope, apiCalls, crmApi, crmUiAlert, crmBlocker, crmStatus, $timeout) {
4 var ts = $scope.ts = CRM.ts(null);
5 $scope.appMetas = apiCalls.appMetas.values;
6 $scope.cxns = apiCalls.cxns.values;
7 $scope.alerts = _.where(apiCalls.sysCheck.values, {name: 'checkCxnOverrides'});
8
9 $scope.filter = {};
10 var block = $scope.block = crmBlocker();
11
12 _.each($scope.alerts, function(alert){
13 crmUiAlert({text: alert.message, title: alert.title, type: 'error'});
14 });
15
16 $scope.findCxnByAppId = function(appId) {
17 var result = _.where($scope.cxns, {
18 app_guid: appId
19 });
20 switch (result.length) {
21 case 0:
22 return null;
23 case 1:
24 return result[0];
25 default:
26 throw "Error: Too many connections for appId: " + appId;
27 }
28 };
29
30 $scope.hasAvailApps = function() {
31 // This should usu return after the 1st or 2nd item, but in testing with small# apps, we may exhaust the list.
32 for (var i = 0; i< $scope.appMetas.length; i++) {
33 if (!$scope.findCxnByAppId($scope.appMetas[i].appId)) {
34 return true;
35 }
36 }
37 return false;
38 };
39
40 $scope.refreshCxns = function() {
41 crmApi('Cxn', 'get', {sequential: 1}).then(function(result) {
42 $timeout(function(){
43 $scope.cxns = result.values;
44 });
45 });
46 };
47
48 $scope.register = function(appMeta) {
49 var reg = crmApi('Cxn', 'register', {app_guid: appMeta.appId}).then($scope.refreshCxns);
50 return block(crmStatus({start: ts('Connecting...'), success: ts('Connected')}, reg));
51 };
52
53 $scope.unregister = function(appMeta) {
54 var reg = crmApi('Cxn', 'unregister', {app_guid: appMeta.appId, debug: 1}).then($scope.refreshCxns);
55 return block(crmStatus({start: ts('Disconnecting...'), success: ts('Disconnected')}, reg));
56 };
57
58 $scope.toggleCxn = function toggleCxn(cxn) {
59 var reg = crmApi('Cxn', 'create', {id: cxn.id, is_active: !cxn.is_active, debug: 1}).then(function(){
60 cxn.is_active = !cxn.is_active;
61 });
62 return block(crmStatus({start: ts('Saving...'), success: ts('Saved')}, reg));
63 };
64 });
65
66 })(angular, CRM.$, CRM._);