2016-10-30 13:52:46 +00:00
|
|
|
import { map, isArray } from 'underscore';
|
|
|
|
|
|
|
|
function Dashboard($resource, $http, currentUser, Widget) {
|
|
|
|
function transformSingle(dashboard) {
|
|
|
|
dashboard.widgets = map(dashboard.widgets, row => row.map(widget => new Widget(widget)));
|
|
|
|
dashboard.publicAccessEnabled = dashboard.public_url !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const transform = $http.defaults.transformResponse.concat((data) => {
|
|
|
|
if (isArray(data)) {
|
|
|
|
data.forEach(transformSingle);
|
|
|
|
} else {
|
|
|
|
transformSingle(data);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
|
|
|
|
const resource = $resource('api/dashboards/:slug', { slug: '@slug' }, {
|
|
|
|
get: { method: 'GET', transformResponse: transform },
|
|
|
|
save: { method: 'POST', transformResponse: transform },
|
2016-11-16 13:34:11 +00:00
|
|
|
query: { method: 'GET', isArray: true, transformResponse: transform },
|
2016-10-30 13:52:46 +00:00
|
|
|
recent: {
|
|
|
|
method: 'get',
|
|
|
|
isArray: true,
|
|
|
|
url: 'api/dashboards/recent',
|
|
|
|
transformResponse: transform,
|
2016-11-21 09:31:48 +00:00
|
|
|
},
|
2016-11-16 13:05:43 +00:00
|
|
|
});
|
2016-10-30 13:52:46 +00:00
|
|
|
|
2017-02-15 17:50:04 +00:00
|
|
|
resource.prototype.canEdit = function canEdit() {
|
|
|
|
return currentUser.canEdit(this) || this.can_edit;
|
|
|
|
};
|
|
|
|
|
2016-10-30 13:52:46 +00:00
|
|
|
return resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function (ngModule) {
|
|
|
|
ngModule.factory('Dashboard', Dashboard);
|
|
|
|
}
|