Making WordPress.org

Ticket #1590: 1590.patch

File 1590.patch, 6.7 KB (added by ocean90, 8 years ago)
  • trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/js/rosetta-roles.js

     
    2121                },
    2222
    2323                initialize: function() {
    24                         var subProjects = this.get( 'sub_projects' );
     24                        this.baseCollection = this.get( 'sub_projects' );
    2525                        this.unset( 'sub_projects' );
    2626
    27                         this.set( 'subProjects', new projects.model.subProjects( subProjects ) );
     27                        this.set( 'subProjects', new projects.model.subProjects( this.baseCollection, {
     28                                project: this,
     29                        } ) );
    2830                        this.set( 'checked', _.contains( projects.settings.accessList, parseInt( this.get( 'id' ), 10 ) ) );
    2931
    3032                        this.listenTo( this.get( 'subProjects' ), 'change:checked', this.updateChecked );
     
    8385                defaults: {
    8486                        id: 0,
    8587                        name: '',
    86                         checked: false,
    87                         isVisible: true
     88                        checked: false
    8889                },
    8990
    9091                initialize: function() {
     
    9899                // Search terms
    99100                terms: '',
    100101
    101                 initialize: function() {
     102                initialize: function( models, options ) {
     103                        this.project = options.project;
    102104                        this.on( 'uncheckall', this.uncheckall );
    103105                },
    104106
     
    120122                                this.search( this.terms );
    121123                        }
    122124
    123                         if ( this.terms === '' ) {
    124                                 this.each( function( project ) {
    125                                         project.set( 'isVisible', true );
    126                                 });
     125                        // If search is blank, show all projects.
     126                        if ( '' === this.terms ) {
     127                                this.reset( this.project.baseCollection );
    127128                        }
     129
     130                        // Trigger a 'projects:update' event
     131                        this.trigger( 'projects:update' );
    128132                },
    129133
    130134                // Performs a search within the collection
    131135                // @uses RegExp
    132136                search: function( term ) {
    133                         var match;
     137                        var match, results;
    134138
     139                        // Start with a full collection
     140                        this.reset( this.project.baseCollection, { silent: true } );
     141
    135142                        // Escape the term string for RegExp meta characters
    136143                        term = term.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' );
    137144
     
    140147                        term = term.replace( / /g, ')(?=.*' );
    141148                        match = new RegExp( '^(?=.*' + term + ').+', 'i' );
    142149
    143                         // Find results
    144                         this.each( function( project ) {
     150                        results = this.filter( function( project ) {
    145151                                var haystack = _.union( [ project.get( 'name' ), project.get( 'slug' ) ] );
    146                                 project.set( 'isVisible', match.test( haystack ) );
     152                                return match.test( haystack );
    147153                        });
     154
     155                        this.reset( results );
    148156                },
    149157
    150158                comparator: function( project ) {
     
    262270                        this.views.add( new projects.view.SubProjectsList({
    263271                                collection: collection
    264272                        }) );
    265                 }
     273                },
     274
    266275        });
    267276
    268277        projects.view.SubProjectsList = wp.Backbone.View.extend({
    269278                tagName: 'ul',
    270279                className: 'sub-projects-list',
    271280
    272                 initialize: function() {
    273                         var view = this;
    274                         this.collection.each( function( project ) {
    275                                 view.views.add( new projects.view.SubProject({
    276                                         model: project
    277                                 }) );
     281                // How many projects should be rendered.
     282                limit: 100,
     283
     284                initialize: function( options ) {
     285                        var self = this;
     286
     287                        this.listenTo( self.collection, 'projects:update', function() {
     288                                self.render( this );
     289                        } );
     290                },
     291
     292                render: function() {
     293                        var self = this, subProjects;
     294
     295                        self.$el.empty();
     296
     297                        subProjects = self.collection.first( self.limit );
     298                        _.each( subProjects, function( model ) {
     299                                var subProjectView = new projects.view.SubProject({
     300                                        model: model
     301                                });
     302
     303                                subProjectView.render();
     304                                self.$el.append( subProjectView.el );
    278305                        });
    279306                }
    280307        });
     
    286313                        this.views.add( new projects.view.Checkbox({
    287314                                model: this.model
    288315                        }) );
    289 
    290                         this.listenTo( this.model, 'change:isVisible', this.changeVisibility );
    291                 },
    292 
    293                 changeVisibility: function() {
    294                         if ( this.model.get( 'isVisible' ) ) {
    295                                 this.$el.removeClass( 'hidden' );
    296                         } else {
    297                                 this.$el.addClass( 'hidden' );
    298                         }
    299316                }
    300317        });
    301318
     
    332349
    333350                doSearch: _.debounce( function( event ) {
    334351                        this.collection.doSearch( event.target.value );
    335                 }, 200 )
     352                }, 500 )
    336353        });
    337354
    338         projects.init = function() {
     355        projects.initFrame = function( projectData ) {
    339356                projects.view.frame = new projects.view.Frame({
    340                         collection: new projects.model.Projects( projects.settings.data )
     357                        collection: new projects.model.Projects( projectData )
    341358                }).render();
    342359        };
    343360
     361        projects.init = function() {
     362                var projectData = localStorage.getItem( 'projectData' );
     363                if ( projectData ) {
     364                        projectData = JSON.parse( projectData );
     365                        projects.initFrame( projectData );
     366                        return;
     367                }
     368
     369                wp.ajax.post( 'rosetta-get-project-data' )
     370                        .done( function( projectData ) {
     371                                var r = localStorage.setItem( 'projectData', JSON.stringify( projectData ) );
     372                                projects.initFrame( projectData );
     373                        } );
     374        };
     375
    344376        $( projects.init );
    345377
    346378        $( '#project-all' ).on( 'click', function() {
  • trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/rosetta-roles.php

     
    7676
    7777                add_action( 'translation_editor_added', array( $this, 'update_wporg_profile_badge' ) );
    7878                add_action( 'translation_editor_removed', array( $this, 'update_wporg_profile_badge' ) );
     79
     80                add_action( 'wp_ajax_rosetta-get-project-data', array( $this, 'ajax_get_project_data' ) );
    7981        }
    8082
    8183        /**
     
    618620         * Renders the edit page.
    619621         */
    620622        private function render_edit_translation_editor( $user_id ) {
     623                $project_access_list = $this->get_users_projects( $user_id );
     624
     625                wp_localize_script( 'rosetta-roles', '_rosettaProjectsSettings', array(
     626                        'l10n' => array(
     627                                'searchPlaceholder' => esc_attr__( 'Search...', 'rosetta' ),
     628                        ),
     629                        'accessList' => $project_access_list,
     630                ) );
     631
     632                $feedback_message = $this->get_feedback_message();
     633
     634                require __DIR__ . '/views/edit-translation-editor.php';
     635        }
     636
     637        public function ajax_get_project_data() {
    621638                $projects = $this->get_translate_projects();
    622639                $project_tree = $this->get_project_tree( $projects, 0, 1 );
    623640
     
    631648                }
    632649                $project_tree = array_values( $project_tree );
    633650
    634                 $project_access_list = $this->get_users_projects( $user_id );
    635 
    636                 wp_localize_script( 'rosetta-roles', '_rosettaProjectsSettings', array(
    637                         'l10n' => array(
    638                                 'searchPlaceholder' => esc_attr__( 'Search...', 'rosetta' ),
    639                         ),
    640                         'data' => $project_tree,
    641                         'accessList' => $project_access_list,
    642                 ) );
    643 
    644                 $feedback_message = $this->get_feedback_message();
    645 
    646                 require __DIR__ . '/views/edit-translation-editor.php';
     651                wp_send_json_success( $project_tree );
    647652        }
    648653
    649654        /**