Making WordPress.org

Changeset 1990


Ignore:
Timestamp:
10/18/2015 06:45:08 PM (11 years ago)
Author:
ocean90
Message:

Trac: Enhance autocomplete for usernames by separating ticket and non-ticket participants.

props helen, ocean90.
See #830.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/style/trac/wp-trac.js

    r1987 r1990  
    440440
    441441                autocomplete: (function() {
    442                         var users,
    443                                 attachments,
     442                        var ticketParticipants = [],
     443                                nonTicketParticipants = [],
     444                                attachments = [],
    444445                                settings = {};
    445446
     
    454455                                        }
    455456
    456                                         users = this.getUsers();
    457                                         attachments  = this.getAttachments();
     457                                        this.initTicketParticipants();
     458                                        this.initNonTicketParticipants();
    458459
    459460                                        $( '#comment' ).atwho({
    460                                                 at:  '@',
    461                                                 data: users
     461                                                at:        '@',
     462                                                callbacks: {
     463                                                        filter:       this.filterTicketParticipants,
     464                                                        remoteFilter: this.filterNonTicketParticipants
     465                                                }
    462466                                        }).atwho({
    463                                                 at: '[att',
    464                                                 insertTpl: '${atwho-at}achment:${name}]',
     467                                                at:         '[att',
     468                                                insertTpl:  '${atwho-at}achment:${name}]',
    465469                                                displayTpl: '<li>${display}</li>',
    466                                                 data: attachments
    467                                         });
    468                                 },
    469 
    470                                 getUsers: function() {
     470                                                data:       this.getAttachments()
     471                                        });
     472                                },
     473
     474                                filterNonTicketParticipants: function( query, callback ) {
     475                                        // Bail out if the query is empty.
     476                                        if ( '' === query ) {
     477                                                return callback();
     478                                        }
     479
     480                                        var results = [],
     481                                                regex = new RegExp( '^' + query, 'ig' ); // start of string
     482
     483                                        $.each( nonTicketParticipants, function( key, value ) {
     484                                                if ( value.toLowerCase().match( regex ) ) {
     485                                                        results.push( { name: value } );
     486                                                }
     487                                        });
     488
     489                                        callback( results );
     490                                },
     491
     492                                filterTicketParticipants: function( query ) {
     493                                        // Bail out if the query is empty.
     494                                        if ( '' === query ) {
     495                                                return ticketParticipants;
     496                                        }
     497
     498                                        var results = [],
     499                                                regex = new RegExp( '^' + query, 'ig' ); // start of string
     500
     501                                        $.each( ticketParticipants, function( key, value ){
     502                                                if ( value.toLowerCase().match( regex ) ) {
     503                                                        results.push( value );
     504                                                }
     505                                        });
     506
     507                                        return results;
     508                                },
     509
     510                                initTicketParticipants: function() {
    471511                                        var users  = [], exclude = [];
    472512
     
    493533                                        }
    494534
    495                                         // Add additional users.
    496                                         if ( 'undefined' !== typeof settings.include ) {
    497                                                 $.each( settings.include, function( k, username ) {
    498                                                         if ( -1 === $.inArray( username, users ) ) {
    499                                                                 users.push( username );
    500                                                         }
    501                                                 });
    502                                         }
    503 
    504535                                        // Exclude current user.
    505536                                        if ( 'undefined' !== wpTrac.currentUser ) {
     
    509540                                        }
    510541
    511                                         return users;
     542                                        ticketParticipants = users;
     543                                },
     544
     545                                getTicketParticipants: function() {
     546                                        return ticketParticipants;
     547                                },
     548
     549                                addTicketParticipant: function( ticketParticipant ) {
     550                                        if ( -1 === $.inArray( ticketParticipant, ticketParticipants ) ) {
     551                                                $.merge( ticketParticipants, [ ticketParticipant ] );
     552                                        }
     553                                },
     554
     555                                initNonTicketParticipants: function() {
     556                                        var users  = [], exclude = [];
     557
     558                                        if ( 'undefined' !== settings.exclude ) {
     559                                                exclude = settings.exclude;
     560                                        }
     561
     562                                        if ( 'undefined' !== typeof settings.include ) {
     563                                                $.each( settings.include, function( k, username ) {
     564                                                        if ( -1 === $.inArray( username, users ) && -1 === $.inArray( username, ticketParticipants ) ) {
     565                                                                users.push( username );
     566                                                        }
     567                                                });
     568                                        }
     569
     570                                        // Exclude current user.
     571                                        if ( 'undefined' !== wpTrac.currentUser ) {
     572                                                users = $.grep( users, function( user ) {
     573                                                        return user != wpTrac.currentUser;
     574                                                });
     575                                        }
     576
     577                                        nonTicketParticipants = users;
     578                                },
     579
     580                                getNonTicketParticipants: function() {
     581                                        return nonTicketParticipants;
     582                                },
     583
     584                                addNonTicketParticipant: function( nonTicketParticipant ) {
     585                                        if ( -1 === $.inArray( nonTicketParticipant, nonTicketParticipants ) ) {
     586                                                $.merge( nonTicketParticipants, [ nonTicketParticipant ] );
     587                                        }
    512588                                },
    513589
     
    516592
    517593                                        // Most recent should show up first.
    518                                         $ ($( 'dl.attachments dt' ).get().reverse() ).each( function() {
     594                                        $( $( 'dl.attachments dt' ).get().reverse() ).each( function() {
    519595                                                attachments.push({
    520596                                                        display: $( this ).text(),
     
    908984                                                if ( data.data.maintainers ) {
    909985                                                        maintainerLabels( data.data.maintainers );
    910                                                 }
    911                                         }
    912                                 });
     986                                                //      wpTrac.autocomplete.addNonTicketParticipant( data.data.maintainers ); doesn't work yet, because ticketInit() runs before autocomplete.init()
     987                                                }
     988                                        }
     989                                });
    913990                        }
    914991
    915992                        function maintainerLabels( maintainers ) {
    916993                                var i, len, labels = {};
    917                                 for ( i = 0, len = maintainers.length; i < len; i++ ) {
    918                                 labels[ maintainers[i] ] = {
     994                                        for ( i = 0, len = maintainers.length; i < len; i++ ) {
     995                                        labels[ maintainers[i] ] = {
    919996                                                text:  'Component Maintainer',
    920997                                                title: '@' + maintainers[i] + ' maintains the ' + $.trim( $('td[headers="h_component"]').text() ) + ' component'
    921998                                        };
    922999                                }
    923                                 wpTrac.showContributorLabels( labels );
     1000                                wpTrac.showContributorLabels( labels );
    9241001                        }
    9251002
Note: See TracChangeset for help on using the changeset viewer.