Skip to content

Commit

Permalink
feat(cart): only store ids & size, access
Browse files Browse the repository at this point in the history
- send 3 queries on cart page (files, cart count, ui summary)
- spinner when loading
- show 'My Project' col when logged in always
- isUserProject accepts project ids as array
- tests for isUserProject
Closes #1463
  • Loading branch information
Christine Yu committed Mar 16, 2016
1 parent 350fcce commit fc6def8
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 149 deletions.
76 changes: 31 additions & 45 deletions app/scripts/cart/cart.controllers.ts
Expand Up @@ -5,25 +5,22 @@ module ngApp.cart.controllers {
import ICoreService = ngApp.core.services.ICoreService;
import IUserService = ngApp.components.user.services.IUserService;
import ISearchService = ngApp.search.services.ISearchService;
import IPagination = ngApp.components.tables.pagination.models.IPagination;
import IParticipantsService = ngApp.participants.services.IParticipantsService;
import IFilesService = ngApp.files.services.IFilesService;

export interface ICartController {
files: IFile[];
lastModified: Moment;
getTotalSize(): number;
getFileIds(): string[];
getRelatedFileIds(): string[];
processPaging: boolean;
pagination: IPagination;
cartTableConfig: any;
projectCountChartConfig: any;
fileCountChartConfig: any;
}

class CartController implements ICartController {
lastModified: Moment;
pagination: any = {};
processPaging: boolean = true;
displayedFiles: IFile[];
numberFilesGraph: any;
sizeFilesGraph: any;
Expand All @@ -44,38 +41,25 @@ module ngApp.cart.controllers {
private CartTableModel,
private Restangular,
private SearchService: ISearchService,
private FilesService,
private FilesService: IFilesService,
private ParticipantsService: IParticipantsService,
private CartState) {
var data = $state.current.data || {};
this.CartState.setActive("tabs", data.tab);
CoreService.setPageTitle("Cart", "(" + this.files.length + ")");
this.lastModified = this.CartService.lastModified;
this.cartTableConfig = CartTableModel;

this.pagination = {
from: 1,
size: 20,
count: 10,
page: 1,
pages: Math.ceil(files.length / 10),
total: files.length,
sort: ""
};

$scope.$on("gdc-user-reset", () => {
this.files = CartService.getFiles();
this.getSummary();
});
this.refresh();

$scope.$on("undo", () => {
this.files = CartService.getFiles();
this.getSummary();
$scope.$on("$locationChangeSuccess", (event, next: string) => {
if (next.indexOf("cart") !== -1) {
this.refresh();
}
});

$scope.$on("cart-update", () => {
this.lastModified = this.CartService.lastModified;
this.files = CartService.getFiles();
this.getSummary();
$scope.$on("cart-update", (event) => {
this.refresh();
});

this.projectCountChartConfig = {
Expand All @@ -100,19 +84,17 @@ module ngApp.cart.controllers {
pluralDefaultText: "authorization levels"
};

this.getSummary();
}

getSummary() {
this.participantCount = _.unique(_.flatten(_.pluck(this.files, "caseIds"))).length;
var filters = {
op: "and",
content: [
{
op: "in",
content: {
field: "files.file_id",
value: _.pluck(this.files, "file_id")
value: this.CartService.getFileIds()
}
}
]
Expand All @@ -123,7 +105,7 @@ module ngApp.cart.controllers {
});

var UserService = this.UserService;
var authCountAndFileSizes = _.reduce(this.files, (result, file) => {
var authCountAndFileSizes = _.reduce(this.CartService.getFiles(), (result, file) => {
var canDownloadKey = UserService.userCanDownloadFile(file) ? 'authorized' : 'unauthorized';
result[canDownloadKey].count += 1;
result[canDownloadKey].file_size += file.file_size;
Expand All @@ -142,15 +124,26 @@ module ngApp.cart.controllers {
file_size: { value: authCountAndFileSizes.unauthorized.file_size }
}
], (i) => i.doc_count);

}

setState(tab: string) {
// Changing tabs and then navigating to another page
// will cause this to fire.
if (tab && (this.$state.current.name.match("cart."))) {
this.$state.go("cart." + tab, {}, {inherit: false});
}
refresh(): void {
var filters = {'content': [{'content': {'field': 'files.file_id', 'value': this.CartService.getFileIds()}, 'op': 'in'}], 'op': 'and'};
var fileOptions = {
filters: filters,
fields: ['access', 'file_name', 'file_id', 'data_type', 'data_format', 'file_size', 'annotations.annotation_id'],
expand: ['cases', 'cases.project'],
facets: ['cases.case_id'],
};
this.FilesService.getFiles(fileOptions, 'POST').then((data: IFiles) => {
this.files = this.files || {};
if (!_.isEqual(this.files.hits, data.hits)) {
this.files = data;
this.getSummary();
}
});
this.ParticipantsService.getParticipants({filters: filters}, 'POST').then((data: IParticipants) => {
this.participantCount = data.pagination.total;
});
}

getTotalSize(): number {
Expand Down Expand Up @@ -204,7 +197,6 @@ module ngApp.cart.controllers {
constructor(private CartService: ICartService) {}

addToCart(): void {
console.log(this.file.file_id);
this.CartService.addFiles([this.file], true);
}

Expand Down Expand Up @@ -277,15 +269,9 @@ module ngApp.cart.controllers {

this.FilesService.getFiles({
fields: ["access",
"file_name",
"file_id",
"file_size",
"data_type",
"data_format",
"annotations.annotation_id",
"cases.case_id",
"cases.project.project_id",
"cases.project.name"
],
filters: filters,
sort: "",
Expand Down
12 changes: 1 addition & 11 deletions app/scripts/cart/cart.directives.ts
Expand Up @@ -205,21 +205,11 @@ module ngApp.cart.directives {
},
op: "in"
});
//QueryCartService.pushAddedQuery(filters);
CartService.addFiles(this.filteredRelatedFiles.hits);
};

this.addRelatedFiles = function() {
var uuid = this.row.case_id;
//QueryCartService.pushAddedQuery({
//content: {
//field: "files.cases.case_id",
//value: [
//uuid
//]
//},
//op: "in"
//});
CartService.addFiles(this.files);
};

Expand Down Expand Up @@ -251,7 +241,7 @@ module ngApp.cart.directives {
},true);

$scope.remove = function() {
CartService.removeFiles($scope.files);
CartService.removeFiles($scope.files);
}

}
Expand Down
21 changes: 3 additions & 18 deletions app/scripts/cart/cart.services.ts
Expand Up @@ -71,7 +71,6 @@ module ngApp.cart.services {
var removedQuery = JSON.parse(this.$window.localStorage.getItem(QueryCartService.GDC_CART_REMOVED_QUERY));
//incomplete
var filters = removedQuery ? {"op":"and","content":[addedQuery, removedQuery]} : addedQuery;
console.log(filters);
if (filters) {
return this.FilesService.getFiles({
fields: ["access",
Expand Down Expand Up @@ -211,19 +210,7 @@ module ngApp.cart.services {
var alreadyIn:IFile[] = [];
_.forEach(files, (file) => {
if (!this.isInCart(file.file_id)) {
var cartItem = _.pick(file,
'access', 'file_name', 'file_id', 'file_size', 'data_type', 'data_format'
);
cartItem.annotationIds = file.annotationIds || _.pluck(file.annotations, 'annotation_id');
cartItem.caseIds = file.caseIds || _.map(file.cases, c => c.case_id);
cartItem.projects = file.projects || _.unique(_.map(file.cases, p => {
return {
project_id: p.project.project_id,
name: p.project.name
};
}), 'project_id');
cartItem.related_ids = file.related_ids || _.pluck(file.related_files, "file_id")
this.lastModifiedFiles.push(cartItem);
this.lastModifiedFiles.push(file);
} else {
alreadyIn.push(file);
}
Expand All @@ -232,7 +219,6 @@ module ngApp.cart.services {
if (addingMsgPromise) {
this.$timeout.cancel(addingMsgPromise);
}
this.$rootScope.$broadcast("cart-update");
this._sync();
this.notify.closeAll();
this.notify.config({ duration: 5000 });
Expand Down Expand Up @@ -332,8 +318,6 @@ module ngApp.cart.services {
});
this.files = remaining;
this._sync();

this.$rootScope.$broadcast("cart-update");
}

removeFiles(files: IFile[]): void {
Expand All @@ -354,9 +338,10 @@ module ngApp.cart.services {
}

_sync(): void {
this.$rootScope.$broadcast("cart-update");
this.lastModified = this.$window.moment();
this.$window.localStorage.setItem(CartService.GDC_CART_UPDATE, this.lastModified.toISOString());
this.$window.localStorage.setItem(CartService.GDC_CART_KEY, JSON.stringify(this.files));
this.$window.localStorage.setItem(CartService.GDC_CART_KEY, JSON.stringify(this.files.map(f => {return { access: f.access, file_id: f.file_id, file_size: f.file_size, projects: _.map(f.cases, c => c.project.project_id) }})));
}

}
Expand Down
31 changes: 15 additions & 16 deletions app/scripts/cart/cart.table.model.ts
Expand Up @@ -31,7 +31,7 @@ module ngApp.cart.models {
var icon = isUserProject ? 'check' : 'remove';
return '<i class="fa fa-' + icon + '"></i>';
},
inactive: $scope => !$scope.UserService.currentUser || $scope.UserService.currentUser.isFiltered,
inactive: $scope => !$scope.UserService.currentUser,
hidden: false,
tdClassName: "text-center"
}, {
Expand All @@ -53,23 +53,23 @@ module ngApp.cart.models {
id: "cases",
td: (row, $scope) => {
function getParticipants(row, $filter) {
return row.caseIds.length == 1 ?
'<a href="cases/' + row.caseIds[0] + '">1</a>' :
withFilter(row.caseIds.length, [{field: "files.file_id", value: row.file_id}], $filter);
return row.cases.length == 1 ?
'<a href="cases/' + row.cases[0].case_id + '">1</a>' :
withFilter(row.cases.length, [{field: "files.file_id", value: row.file_id}], $filter);
}

return row.caseIds.length ? getParticipants(row, $scope.$filter) : 0;
//debugger
return row.cases.length ? getParticipants(row, $scope.$filter) : 0;
},
thClassName: 'text-right',
tdClassName: 'text-right'
}, {
name: "Project",
id: "cases.project.project_id",
td: row => {
return _.map(row.projects, p => {
return ('<a href="projects/' + p.project_id +
'" data-tooltip="' + p.name +
'" data-tooltip-append-to-body="true" data-tooltip-placement="right">'+ p.project_id + '</a>');
return _.unique(row.cases, c => c.project.project_id).map(c => {
return ('<a href="projects/' + c.project.project_id +
'" data-tooltip="' + c.project.name +
'" data-tooltip-append-to-body="true" data-tooltip-placement="right">'+ c.project.project_id + '</a>');
}).join('<br>');
},
sortable: true
Expand All @@ -95,15 +95,14 @@ module ngApp.cart.models {
id: "annotations",
td: (row, $scope) => {
function getAnnotations(row, $scope) {
return row.annotationIds.length == 1 ?
'<a href="annotations/' + row.annotationIds[0] + '">' + 1 + '</a>' :
return row.annotations.length === 1 ?
'<a href="annotations/' + row.annotations[0].annotation_id + '">' + 1 + '</a>' :
withAnnotationFilter(
row.annotationIds.length,
[{field: "annotation_id", value: row.annotationIds}],
row.annotations.length,
[{field: "annotation_id", value: row.annotations.map(a => a.annotation_id)}],
$scope.$filter);
}

return row.annotationIds.length ? getAnnotations(row, $scope) : 0;
return row.annotations ? getAnnotations(row, $scope) : 0;
},
thClassName: 'text-right',
tdClassName: 'text-right'
Expand Down
34 changes: 19 additions & 15 deletions app/scripts/cart/templates/cart.html
@@ -1,11 +1,15 @@
<div class="container-fluid cart full-page">
<section class="row">
<div class="col-lg-8" data-ng-if="!cc.files.length">
<h2 data-translate>Your Cart is empty.</h2>
<div class="col-lg-8" data-ng-if="!cc.files.hits.length">
<h2 data-translate data-ng-if="!cc.CartService.getFileIds().length">Your Cart is empty.</h2>
<h2 data-translate data-ng-if="cc.CartService.getFileIds().length">
<i class="fa fa-spinner fa-spin"></i>
Your Cart is loading.
</h2>
</div>
<div data-ng-if="cc.files.length">
<div data-ng-if="cc.files.hits.length">
<div class="col-lg-3 col-sm-6">
<count-card title="Files" icon="fa-file-o" data="cc.files.length | number"></count-card>
<count-card title="Files" icon="fa-file-o" data="cc.files.pagination.total | number"></count-card>
<count-card title="Cases" icon="fa-user" data="cc.participantCount | number"></count-card>
<count-card title="File Size" icon="fa-save" data="cc.summary.fs.value | size"></count-card>
</div>
Expand Down Expand Up @@ -54,7 +58,7 @@ <h4 class="panel-title" data-translate>Notes</h4>
</div>
</section>

<section class="row items-pane" data-ng-if="cc.files.length">
<section class="row items-pane" data-ng-if="cc.files.hits.length">
<div class="col-md-12 col-lg-12 col-sm-12">
<div class="pull-right">
<a download-manifest-cart data-downloader
Expand Down Expand Up @@ -83,15 +87,15 @@ <h4 class="panel-title" data-translate>Notes</h4>
</div>
</section>


<gdc-table data-ng-if="cc.files.length"
data-data="cc.files"
data-config="cc.cartTableConfig"
data-paging="cc.pagination"
data-page="cart"
data-heading="cart items"
data-id="cart-table"
data-client-side="cc.processPaging"
class="cart-table">
<gdc-table data-ng-if="cc.files.hits.length"
data-data="cc.files.hits"
data-config="cc.cartTableConfig"
data-paging="cc.files.pagination"
data-page="files"
data-heading="cart items"
id="cart-table"
class="cart-table"
data-endpoint="files">
</gdc-table>

</div>
2 changes: 1 addition & 1 deletion app/scripts/cart/tests/cart.tests.js
Expand Up @@ -3,7 +3,7 @@ describe('Cart:', function () {
var CartService, controller, httpBackend;

// Initialization of the AngularJS application before each test case
beforeEach(module('ngProgressLite', 'ngApp.cart', 'core.filters'));
beforeEach(module('ngProgressLite', 'ngApp.cart', 'core.filters', 'ngApp.participants'));

beforeEach(module(function ($provide) {
$provide.value('RestFullResponse', {});
Expand Down
1 change: 0 additions & 1 deletion app/scripts/components/tables/pagination.controllers.ts
Expand Up @@ -41,7 +41,6 @@ module ngApp.components.tables.pagination.controllers {
return this.LocationService.setPaging(pagination);
}

this.$scope.$emit(this.$scope.page + "-paging-update", obj);
}
}

Expand Down

0 comments on commit fc6def8

Please sign in to comment.