1app.controller('BlogPaginationCtrl', [ '$scope', '$timeout', '$element', function ($scope, $timeout, $element) { 2 $scope.posts = {} 3 4 $scope.setJsonData = function (data, sort, itemsPerPage) { 5 if (_.isUndefined(sort) || !sort) { 6 $scope.data = $scope.orderById(data) 7 } else { 8 $scope.data = $scope.orderByCustom(data, sort); 9 } 10 11 if (!_.isUndefined(itemsPerPage) && itemsPerPage) { 12 $scope.itemsPerPage = itemsPerPage 13 } 14 15 $scope.main() 16 } 17 18 $scope.paginate = function (page, perPage) { 19 20 window.history.pushState({ url: window.location.pathname }, $('title').text(), window.location.pathname + '?page=' + page) 21 22 $scope.loading = true 23 $scope.posts = $scope.pagination(page, perPage) 24 $scope.loading = false 25 26 if ($element.find('.fade').length > 0) { 27 $element.find('.fade').removeClass('fade') 28 } 29 } 30 31 $scope.pagination = function (_page, _perPage) { 32 var page = _page || 1 33 var perPage = _perPage || 10 34 var offset = (page - 1) * perPage 35 var paginatedItems = $scope.data.slice(offset).slice(0, perPage) 36 var totalPages = Math.ceil($scope.data.length / perPage) 37 38 return { 39 page: page, 40 perPage: perPage, 41 prePage: page - 1 ? page - 1 : null, 42 nextPage: (totalPages > page) ? page + 1 : null, 43 total: $scope.data.length, 44 totalPages: totalPages, 45 data: paginatedItems 46 } 47 } 48 49 $scope.$watchCollection('[currentPage, itemsPerPage]', function (data) { 50 var currentPage = data[0] || $scope.currentPage 51 var itemsPerPage = data[1] || $scope.itemsPerPage 52 $scope.paginate(currentPage, itemsPerPage) 53 }) 54 55 $scope.orderById = function (data) { 56 data.sort(function (a, b) { 57 return a.id.toString().localeCompare(b.id) 58 }) 59 return data 60 } 61 62 /** 63 * sort by field this is like _.orderBy / _.sortByOrder (new lodash) 64 * - this old version of lodash doesn't have the above functions 65 * 66 * sort value example: 67 * [ 68 * { 69 * "field": "createdAt", 70 * "dir": "desc" 71 * } 72 * ] 73 * 74 * @param array data 75 * @param array sort 76 * @returns array 77 */ 78 $scope.orderByCustom = function (data, sort) { 79 var wrappedData = _(data).chain(); 80 _.forEach(sort, function (row) { 81 wrappedData = wrappedData.sortBy(row.field); 82 if (!_.isUndefined(row.dir) && row.dir === 'desc') { 83 wrappedData = wrappedData.reverse(); 84 } 85 }); 86 87 return wrappedData.value(); 88 }; 89 90 $scope.main = function () { 91 // init 92 $scope.totalItems = $scope.data.length 93 94 var url = new URL(window.location.href) 95 var currentPage = url.searchParams.get('page') 96 $scope.currentPage = currentPage || 1 97 } 98 99 // Simple convert object to query string 100 var objectToQS = function (object) { 101 var qsArr = [] 102 if (object) { 103 for (var key in object) { 104 if (object[key]) { 105 qsArr.push(key + '=' + object[key]) 106 } 107 } 108 } 109 return qsArr.join('&') 110 } 111 112 return $scope 113}]) 114 115app.filter('encodeUrl', function () { 116 return function (input) { 117 return window.location.protocol + '//' + window.location.hostname + input; 118 } 119})
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.