Skip to content
This repository has been archived by the owner on Jun 4, 2023. It is now read-only.

Commit

Permalink
admin: allow to copy dashboards using drag'n'drop
Browse files Browse the repository at this point in the history
A dashboard can be dragged from one group to another and will be
copied. This also works when dragging from one group to the same
group. This will create a duplicate.

Closes #24.
  • Loading branch information
vincentbernat committed Aug 5, 2015
1 parent f1aa08f commit 553e407
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
6 changes: 6 additions & 0 deletions app/scripts/controllers/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ angular.module('dashkiosk.controllers')
.controller('GroupCtrl', function($scope) {
'use strict';

// Attach a display to this group
$scope.attachDisplay = function(name) {
$scope.group.$attach(name);
};

// Copy a dashboard to this group
$scope.copyDashboard = function(id) {
$scope.group.$copy(parseInt(id, 10));
};

// Return true if any display in the group is connected
$scope.anyConnected = function() {
return _.any($scope.group.displays, function(d) {
Expand Down
42 changes: 37 additions & 5 deletions app/scripts/services/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ angular.module('dashkiosk.services')
var self = this;
diff(self, data,
function(k) { delete self[k]; },
function(k) { self[k] = new Group(data[k]); },
function(k) { self[k] = new Group(data[k], self); },
function(k) { self[k].applyDiff(data[k]); });
};
// Add a new group
Expand All @@ -117,17 +117,24 @@ angular.module('dashkiosk.services')
return false;
});
};
// Find a dashboard with its ID
GroupCollection.prototype.$findDashboard = function(id) {
return _.find(_.map(this, function(group) {
return group.$findDashboard(id);
}));
};

// One group
function Group(data) {
function Group(data, groups) {
_.extend(this, data);
this.displays = _.mapValues(this.displays, function(d) { return new Display(d); });
this.dashboards = new DashboardCollection(this.dashboards, this);
this.groups = groups;
}
Group.prototype.applyDiff = function(data) {
var self = this;
diff(_.omit(self, ['displays', 'dashboards']),
_.omit(data, ['displays', 'dashboards']),
diff(_.omit(self, ['displays', 'dashboards', 'groups']),
_.omit(data, ['displays', 'dashboards', 'groups']),
function(k) { delete self[k]; },
function(k) { self[k] = data[k]; },
function(k) { self[k] = data[k]; });
Expand Down Expand Up @@ -170,6 +177,21 @@ angular.module('dashkiosk.services')
return false;
});
};
// Copy a dashboard from another group
Group.prototype.$copy = function(id) {
// We only have the ID, we need to find the
var dashboard = this.groups.$findDashboard(id);
if (!dashboard) {
return;
}
dashboard = angular.copy(dashboard);
delete dashboard.id;
this.dashboards.$add(dashboard);
};
// Find a dashboard with its ID
Group.prototype.$findDashboard = function(id) {
return this.dashboards.$findDashboard(id);
};
// Check if the group is empty (not any display attached)
Group.prototype.$empty = function() {
return _.keys(this.displays).length === 0;
Expand Down Expand Up @@ -236,8 +258,13 @@ angular.module('dashkiosk.services')
enumerable: false,
writable: false
});
self.$findDashboard = DashboardCollection.prototype.$findDashboard;
Object.defineProperty(self, '$findDashboard', {
enumerable: false,
writable: false
});
self.applyDiff = DashboardCollection.prototype.applyDiff;
Object.defineProperty(self, 'applydiff', {
Object.defineProperty(self, 'applyDiff', {
enumerable: false,
writable: false
});
Expand Down Expand Up @@ -276,6 +303,11 @@ angular.module('dashkiosk.services')
return false;
});
};
DashboardCollection.prototype.$findDashboard = function(id) {
return _.find(this, function(dashboard) {
return dashboard.id === id;
});
};

// One dashboard
function Dashboard(data, group) {
Expand Down
7 changes: 5 additions & 2 deletions app/views/group.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div data-ui-element="dk-group"
class="panel panel-default"
dk-droppable="{ 'text/vnd.dashkiosk.display': attachDisplay }">
dk-droppable="{ 'text/vnd.dashkiosk.display': attachDisplay,
'text/vnd.dashkiosk.dashboard': copyDashboard }">
<div class="panel-heading">
<div class="actions">
<span class="glyphicon glyphicon-trash"
Expand Down Expand Up @@ -36,7 +37,9 @@
<div class="dashboards">
<dk-dashboard
ng-repeat="dashboard in group.dashboards track by dashboard.id"
dashboard="dashboard">
dashboard="dashboard"
dk-draggable="{{ dashboard.id }}"
data-drag-type="text/vnd.dashkiosk.dashboard">
</dk-dashboard>
<div data-ui-element="dk-dashboard" class="add">
<button class="btn btn-sm"
Expand Down
5 changes: 5 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ Each group has a list of dashboards. You can reorder them by using the
up and down arrow icons on the right of each dashboard. You can add a
new dashboard by using the "*Add a new dashboard*" button.

You can also copy a dashboard from another group by using
drag'n'drop. If you drop a dashboard from one group to the same group,
you will duplicate it. It is expected to be a feature more than a
nuisance.

Dashboards
++++++++++

Expand Down

0 comments on commit 553e407

Please sign in to comment.