|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Controller for the data grid view (url #/datagrid) |
| 4 | +// Todo: Trello card says Columns of interest: Title, Date, Copyright Holder, License, Modified Date, Workflow Status: {Draft, Edited, Private Published, Web Published} |
| 5 | +// Most of these are not currently available (or at least not in the sample data). |
| 6 | +// The setting scope.visibleData needs to be modified to make the actual fields we want, |
| 7 | +// and the columnDefs spec should be changed to match, and eventually we need to implement a sortBy for each column. |
| 8 | +angular.module('BloomLibraryApp.datagrid') |
| 9 | + .controller('DataGridCtrl', ['$scope', '$dialog', '$timeout', 'bookService', '$state', '$stateParams', '$location', |
| 10 | + function ($scope, $dialog, $timeout, bookService, $state, $stateParams, $location) { |
| 11 | + |
| 12 | + $scope.filterOptions = { |
| 13 | + filterText: "", // gets updated by user action in ngGrid |
| 14 | + useExternalFilter: true |
| 15 | + }; |
| 16 | + $scope.totalServerItems = 0; |
| 17 | + $scope.pagingOptions = { |
| 18 | + pageSizes: [20, 50, 100], |
| 19 | + pageSize: 20, // Gets updated by user action in ngGrid |
| 20 | + currentPage: 1 // Gets updated by user action in ngGrid |
| 21 | + }; |
| 22 | + $scope.sortInfo = { |
| 23 | + fields: [], directions: [], columns: [] |
| 24 | + }; |
| 25 | + $scope.getBookRange = function (count, currentPage, searchText) { |
| 26 | + var first = (currentPage - 1) * count; |
| 27 | + var sortField = $scope.sortInfo.fields[0]; |
| 28 | + var sortBy = null; |
| 29 | + // Todo: setting sortBy to a complex field like this does not work...no sorting happens. |
| 30 | + // It appears we will need to put a redundant top-level data field in the record for |
| 31 | + // anything we want to sort by. This is especially annoying in that it may not actually |
| 32 | + // be useful to sort by all the fields...but ng-grid allows the user to do so. |
| 33 | + // Possibly we could disallow some fields with a message and switch to some other sort. |
| 34 | + // Another reason a redundant field is probably necessary is that parse.com's built-in sorting |
| 35 | + // is case-sensitive, and their only suggestion for overcoming this is a redundant |
| 36 | + // all-lower-case field. Similarly any other sort-key field must somehow be in a form |
| 37 | + // where a plain binary sort will give the right results. |
| 38 | + if (sortField == 'title') |
| 39 | + sortBy = 'volumeInfo.title'; |
| 40 | + bookService.getFilteredBookRange(first, count, searchText, sortBy).then(function(result) { |
| 41 | + $scope.visibleBooks = result; |
| 42 | + $scope.visibleData = $scope.visibleBooks.map(function(item) { |
| 43 | + return { |
| 44 | + title: item.volumeInfo.title, |
| 45 | + published: item.volumeInfo.publishedDate, |
| 46 | + publisher: item.volumeInfo.publisher, |
| 47 | + pages: item.volumeInfo.pageCount, |
| 48 | + authors: (item.volumeInfo.authors ? item.volumeInfo.authors.join(", ") : "") |
| 49 | + }; |
| 50 | + }); |
| 51 | + }) |
| 52 | + }; |
| 53 | + $scope.getBookCount = function (searchText) { |
| 54 | + bookService.getFilteredBooksCount(searchText).then(function (count) { |
| 55 | + $scope.bookCount = count; |
| 56 | + }); |
| 57 | + }; |
| 58 | + $scope.getBookCount($scope.filterOptions.filterText); // initialize count |
| 59 | + $scope.getBookRange($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText); // init |
| 60 | + $scope.$watch('pagingOptions', function (newVal, oldVal) { |
| 61 | + if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) { |
| 62 | + $scope.getBookRange($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText); |
| 63 | + } |
| 64 | + }, true); |
| 65 | + $scope.$watch('filterOptions', function (newVal, oldVal) { |
| 66 | + if (newVal !== oldVal) { |
| 67 | + $scope.getBookCount($scope.filterOptions.filterText); |
| 68 | + $scope.getBookRange($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText); |
| 69 | + } |
| 70 | + }, true); |
| 71 | + $scope.$watch('sortInfo', function (newVal, oldVal) { |
| 72 | + if (newVal !== oldVal) { |
| 73 | + $scope.getBookRange($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText); |
| 74 | + } |
| 75 | + }, true); |
| 76 | + $scope.gridOptions = { data: 'visibleData', |
| 77 | + enableColumnResize: true, |
| 78 | + //showColumnMenu:true, |
| 79 | + //showFilter:true, |
| 80 | + enablePaging: true, |
| 81 | + showFooter: true, |
| 82 | + totalServerItems: 'bookCount', |
| 83 | + pagingOptions: $scope.pagingOptions, |
| 84 | + useExternalSorting: true, |
| 85 | + showColumnMenu: true, |
| 86 | + filterOptions: $scope.filterOptions, |
| 87 | + sortInfo: $scope.sortInfo, |
| 88 | + columnDefs: [{field:'title', displayName:'Title', width:'***'}, |
| 89 | + {field:'published', displayName:'Date', width: 80}, |
| 90 | + {field:'publisher', displayName:'Copyright', width: '**'}, |
| 91 | + {field:'pages', displayName:'Pages', width: 50}, |
| 92 | + {field: 'authors', displayName: 'Authors', width:'***'} |
| 93 | + ] |
| 94 | + }; |
| 95 | +// $scope.searchText = $stateParams["search"]; |
| 96 | +// $scope.searchTextRaw = $scope.searchText; |
| 97 | + |
| 98 | +// |
| 99 | +// // browse.tpl.html listview div configures this to be called as pageItemsFunction when user chooses a page. |
| 100 | +// // Todo: should get Filtered book range. |
| 101 | + |
| 102 | +// |
| 103 | +// $scope.foo = function(paramOne, paramTwo) { |
| 104 | +// return paramOne + paramTwo; |
| 105 | +// } |
| 106 | +// |
| 107 | +// |
| 108 | +// $scope.updatePageControl = function () { |
| 109 | +// $scope.currentPage = 1; |
| 110 | +// $scope.setPage = function (pageNo) { |
| 111 | +// $scope.currentPage = pageNo; |
| 112 | +// }; |
| 113 | +// |
| 114 | +// } |
| 115 | +// |
| 116 | +// $scope.SearchNow = function () { |
| 117 | +// // Todo: this needs to run a query on the real database and update bookCount |
| 118 | +// // and do something to make the listview invoke getBookRange (even if the bookCount |
| 119 | +// // does not change). |
| 120 | +// $scope.searchText = $scope.searchTextRaw; |
| 121 | +// $state.go('.', {search: $scope.searchText}); |
| 122 | +// } |
| 123 | + }]); |
| 124 | + |
0 commit comments