Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
var DashboardDirectives = angular.module('corvus.dashboard.directives', []);
DashboardDirectives.directive('cvAddToDashboardModal', ['CorvusCore', 'Dashboard', 'Metabase', 'Card', '$modal', function(CorvusCore, Dashboard, Metabase, Card, $modal) {
function link(scope, element, attrs) {
var openAddToDashModal = function() {
var modalInstance = $modal.open({
templateUrl: '/app/dashboard/partials/modal_add_to_dashboard.html',
controller: ['$scope', '$modalInstance', 'CorvusAlert', 'CorvusFormService', 'card', function($scope, $modalInstance, CorvusAlert, CorvusFormService, card) {
var formName = 'addCardToDash';
// create an object for the add to dash form
$scope.addToDashForm = {};
$scope.card = card;
$scope.alerts = CorvusAlert.alerts;
var cardAdd = function(cardId, dashId) {
Dashboard.addcard({
'dashId': dashId,
'cardId': cardId
}, function(result) {
// success!!
if (typeof existingDashboardsById[dashId] != "undefined") {
CorvusAlert.alertInfo('This card has been added to the dashboard: ' + existingDashboardsById[dashId].name + '</a>');
} else {
CorvusAlert.alertInfo('This card' + ' has been added to the specified dashboard!');
}
$modalInstance.close();
}, function(error) {
CorvusAlert.alertError('Unable to add card to the specified dashboard!');
console.log(error);
});
};
//we need to create a local index of dashboard objects that can been
//queried by ID, so that the view can show the name of the
//dashboard that a card was added to
var existingDashboardsById = {};
Dashboard.list({
Allen Gilliland
committed
'filterMode': 'mine'
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
}, function(result) {
if (result && !result.error) {
$scope.dashboards = result;
for (var i = 0; i < result.length; i++) {
existingDashboardsById[result[i].id] = result[i];
}
} else {
console.log(result);
}
});
$scope.submit = function() {
// if there is an existing dash
// add the card to that dash
if ($scope.addToDashForm.existingDash) {
cardAdd($scope.card.id, $scope.addToDashForm.existingDash);
} else if ($scope.card) {
// populate a new Dash object
var newDash = {
'name': $scope.addToDashForm.newDashName,
'public_perms': 0
};
// create a new dashboard, then add the card to that
Dashboard.create(newDash, function(result) {
if (result && !result.error) {
existingDashboardsById[result.id] = result;
cardAdd(card.id, result.id);
} else {
console.log(result);
return;
}
});
} else {
console.log('error: no method for doing dashboard addition');
}
};
$scope.close = function() {
$modalInstance.dismiss('cancel');
};
}],
resolve: {
card: function() {
return scope.card;
}
}
});
};
element.bind('click', openAddToDashModal);
}
return {
restrict: 'A',
link: link,
scope: {
callback: '=',
card: '='
}
};