Source:pagination.component.js

(function () {
    'use strict';
    // Pagination
    angular.module('mohistory').component('pagination', {
        templateUrl: 'app/components/pagination/pagination.component.html',
        controller: paginationCtrl,
        controllerAs: 'page',
        bindings: {
            curPage: '<',
            totalPages: '<',
            nextPage: '<',
            prevPage: '<',
            setPage: '<',
            inverted: '<',
        }
    });
    paginationCtrl.$inject = [];
    /**
     * Manages the displaying of pagination controls. Interactions follow the
     * Google model. 
     * @memberof mohistory
     * @name pagination
     * @ngdoc component
    */
    function paginationCtrl() {
        var vm = this;
        /* ----- Variables ----- */
        vm.paginationArray = []
        /* ----- Function Bindings ----- */
        vm.$onInit = onInit;
        vm.$onChanges = onChanges;
        vm.calcVisibleNums = calcVisibleNums;
        /* ----- Function Definitions ----- */
        /** 
         * Initialization code run every time the component is created, used to setup variables.
         * @function onInit
         * @memberof pagination
        */
        function onInit() {
            calcVisibleNums();
            console.log(vm.curPage)
            console.log(vm.totalPages)
            console.log(vm.paginationArray)
        }
        /** 
         * Hook triggered whenever changes are made to any values passed into
         * this component.
         * @function onChanges
         * @memberof pagination
        */
        function onChanges() {
            calcVisibleNums();
        }
        /** 
         * Initialization code run every time the component is created, used to setup variables.
         * @function calcVisibleNums
         * @memberof pagination
         * @param {number} curPage Currently selected page
         * @param {number} totalPages Total number of viable pages to access
        */
        function calcVisibleNums(curPage, totalPages) {
            var startNum = 0;
            var endNum = 0;
            if (vm.totalPages <= 10) {
                // less than 10 total pages so show all
                startNum = 1;
                endNum = vm.totalPages;
            } else {
                // more than 10 total pages so calculate start and end pages
                if (vm.curPage <= 6) {
                    startNum = 1;
                    endNum = 10;
                } else if ((vm.curPage + 4) >= vm.totalPages) {
                    startNum = vm.totalPages - 9;
                    endNum = vm.totalPages;
                } else {
                    startNum = vm.curPage - 5;
                    endNum = vm.curPage + 4;
                }
            }
            // Populate array of page numbers shown to the user
            vm.paginationArray = [];
            while (startNum <= endNum) {
                vm.paginationArray.push(startNum);
                startNum++;
            }
        }
    }
})()