<commit_before>import Ember from 'ember';

export default Ember.Mixin.create({
    queryParams: [ 'currentPage', 'itemCountPerPage' ],

    actions: {
        changePage: function( page ){
            var currentPage = this.get( 'currentPage' ),
                totalPages = Math.ceil( this.get( 'metaData.total' ) / this.get( 'itemCountPerPage' ) ),
                prevPage,
                validPrevPage,
                nextPage,
                validNextPage;

            switch( page ){
                case 'first':
                    return this.set( 'currentPage', 1 );
                case 'prev': 
                    validPrevPage = currentPage - 1;
                    validPrevPage = prevPage > 0;
                    return this.set( 'currentPage', validPrevPage ? validPrevPage : 1 );
                case 'next':
                    nextPage = ( currentPage + 1 );
                    validNextPage = nextPage <= totalPages;
                    return this.set( 'currentPage',  validNextPage ? nextPage : totalPages );
                case 'last':
                    return this.set( 'currentPage', totalPages );
                case 'currentPageInput':
                /* falls through */
                default:
                    page = parseInt( page );
                    page = isNaN( page ) ? 1 : page;
                    return this.set( 'currentPage', page );
            }    
        },
        changePerPage: function( perPage ){
            perPage = parseInt( perPage );
            perPage = isNaN( perPage ) ? this.get( 'perPageOptions.firstObject' ) : perPage;
            this.set( 'itemCountPerPage', perPage );
        }
    },

    arrangedContent: function(){
        var self = this,
            content = this._super(),
            currentPage = parseInt( this.get( 'currentPage' ) ),
            itemCount = parseInt( this.get( 'itemCountPerPage' ) ),
            start = ( currentPage - 1 ) * itemCount,
            end = start + itemCount;

        if( content.then && ( !content.isFulfilled && !content.isRejected ) ){
            content.then( function(){ self.notifyPropertyChange( 'content' ); } );
        }

        return content.slice( start, end );

    }.property( 'content', 
               'currentPage', 'itemCountPerPage', 
               'sortProperties.@each', 'sortAscending', 
               'filterProperties.@each'),
    
    /**
     * pagingData - holds paging data for the grid to pass to its pagination controls 
     *
     */
    pagingData: function(){
        var pageNumber = this.get( 'currentPage')-1,
            pageCount = this.getWithDefault( 'metaData.pageCount', null ),
            total = this.getWithDefault( 'metaData.total', null ),
            perPage = this.get( 'itemCountPerPage' ),
            totalPages = this.getWithDefault( 'metaData.totalPages', 1 ),
            firstRow = pageNumber * perPage + 1;

        return {
            pageFirstRow: firstRow,
            pageLastRow: firstRow + pageCount -1,
            totalRows: total,
            totalPages: totalPages,
            perPageOptions: this.get( 'perPageOptions' ), 
            itemCountPerPage: this.get( 'itemCountPerPage' ),
            currentPage: this.get( 'currentPage' ),
            modelNames: this.get( 'metaData.modelNames' )
        };       
    }.property( 'metaData', 'perPageOptions', 'itemCountPerPage', 'currentPage' ),               

    metaDataObserver: function(){
        var total = parseInt( this.get( 'metaData.total' ) ),
            itemCount = parseInt( this.get( 'itemCountPerPage' ) ),
            currentPage = parseInt( this.get( 'currentPage' ) ),
            start = ( currentPage - 1 ) * itemCount,
            end = start + itemCount,
            pageCount =  (start + end) < total ? itemCount : total - start;

        this.setProperties({
             'metaData.totalPages': Math.ceil( total / itemCount ),
             'metaData.pageCount': pageCount 
        });


    }.observes( 'metaData', 'currentPage', 'itemCountPerPage' ),

    currentPage: 1,

    perPageOptions: Ember.A([ 25, 50, 100 ]),

    perPageObserver: function(){
        this.set( 'currentPage', 1 );
    }.observes( 'itemCountPerPage' )

});<commit_msg>Add documentation, minor refactors<commit_after>import Ember from 'ember';

/**
 * @module mixins
 * @class sl-pagination-controller
 */
export default Ember.Mixin.create({

    /**
     * Controller actions hash
     *
     * @property {object} actions
     */
    actions: {

        /**
         * Change current page to the specified page
         *
         * @method actions.changePage
         */
        changePage: function( page ) {
            var currentPage = this.get( 'currentPage' ),
                nextPage,
                prevPage,
                totalPages,
                validNextPage,
                validPrevPage;

            totalPages = Math.ceil(
                this.get( 'metaData.total' ) / this.get( 'itemCountPerPage' )
            );

            switch( page ) {
                case 'first':
                    return this.set( 'currentPage', 1 );

                case 'prev':
                    validPrevPage = currentPage - 1;
                    validPrevPage = prevPage > 0;
                    return this.set( 'currentPage', validPrevPage ? validPrevPage : 1 );

                case 'next':
                    nextPage = ( currentPage + 1 );
                    validNextPage = nextPage <= totalPages;
                    return this.set( 'currentPage',  validNextPage ? nextPage : totalPages );

                case 'last':
                    return this.set( 'currentPage', totalPages );

                case 'currentPageInput':
                /* falls through */
                default:
                    page = parseInt( page );
                    page = isNaN( page ) ? 1 : page;
                    return this.set( 'currentPage', page );
            }
        },

        /**
         * Change the "per page" record limit
         *
         * @method actions.changePerPage
         */
        changePerPage: function( perPage ){
            perPage = parseInt( perPage );
            perPage = isNaN( perPage ) ? this.get( 'perPageOptions.firstObject' ) : perPage;
            this.set( 'itemCountPerPage', perPage );
        }
    },

    /**
     * An arranged copy of the content
     *
     * @property {mixed} arrangedContent
     */
    arrangedContent: function() {
        var self = this,
            content = this._super(),
            currentPage = parseInt( this.get( 'currentPage' )),
            itemCount = parseInt( this.get( 'itemCountPerPage' )),
            start = ( currentPage - 1 ) * itemCount,
            end = start + itemCount;

        if ( content.then && ( !content.isFulfilled && !content.isRejected )) {
            content.then( function() {
                self.notifyPropertyChange( 'content' );
            });
        }

        return content.slice( start, end );
    }.property(
        'content',
        'currentPage',
        'filterProperties.@each',
        'itemCountPerPage',
        'sortAscending',
        'sortProperties.@each'
    ),

    /**
     * The current page number
     *
     * @property {number} currentPage
     * @default 1
     */
    currentPage: 1,

    /**
     * Paging data for the grid to pass to its pagination controls
     *
     * @property {object} pagingData
     */
    pagingData: function() {
        var pageNumber = this.get( 'currentPage' ) - 1,
            pageCount = this.getWithDefault( 'metaData.pageCount', null ),
            total = this.getWithDefault( 'metaData.total', null ),
            perPage = this.get( 'itemCountPerPage' ),
            totalPages = this.getWithDefault( 'metaData.totalPages', 1 ),
            firstRow = pageNumber * perPage + 1;

        return {
            pageFirstRow: firstRow,
            pageLastRow: firstRow + pageCount - 1,
            totalRows: total,
            totalPages: totalPages,
            perPageOptions: this.get( 'perPageOptions' ),
            itemCountPerPage: this.get( 'itemCountPerPage' ),
            currentPage: this.get( 'currentPage' ),
            modelNames: this.get( 'metaData.modelNames' )
        };
    }.property(
        'currentPage',
        'itemCountPerPage',
        'metaData',
        'perPageOptions'
    ),

    /**
     * Options for the "per page" resource limit number
     *
     * @property {array} perPageOptions
     */
    perPageOptions: Ember.A([ 25, 50, 100 ]),

    /**
     * Accepted controller query parameters
     *
     * @property {array} queryParams
     */
    queryParams: [ 'currentPage', 'itemCountPerPage' ],

    /**
     * Reset the current page number when the itemCountPerPage is changed
     *
     * @method resetCurrentPage
     */
    resetCurrentPage: function() {
        this.set( 'currentPage', 1 );
    }.observes( 'itemCountPerPage' )

    /**
     * Update metaData properties when relevant values is changed
     *
     * @method setupMetaData
     */
    updateMetaData: function() {
        var total = parseInt( this.get( 'metaData.total' )),
            itemCount = parseInt( this.get( 'itemCountPerPage' )),
            currentPage = parseInt( this.get( 'currentPage' )),
            start = ( currentPage - 1 ) * itemCount,
            end = start + itemCount,
            pageCount = ( start + end ) < total ? itemCount : total - start;

        this.setProperties({
            'metaData.pageCount': pageCount,
            'metaData.totalPages': Math.ceil( total / itemCount )
        });
    }.observes( 'currentPage', 'itemCountPerPage', 'metaData' )

});
{'fraction_non_alphanumeric': 0.11489977268030585, 'fraction_numerical': 0.0044430667493283735, 'mean_word_length': 1.6752349364289663, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5203494', 'n_tokens_mistral': 2665, 'n_tokens_neox': 2537, 'n_words': 617}