Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-scroll to matches in comparison pane #24

Merged
merged 1 commit into from Aug 3, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions public_html/js/index.js
Expand Up @@ -182,16 +182,33 @@
jsonpCallback: 'callback'
} ).always( function ( ret ) { // use always to handle 500s, etc.
if ( ret.detail ) { // ret.detail means we had success
var $leftPane = $( compareDiv ).find( '.compare-pane-left-body' ),
$rightPane = $( compareDiv ).find( '.compare-pane-right-body' );

// Add a class to the compare panel once we fetch the details to avoid making repetitive API requests
$( compareDiv ).find( '.compare-pane-left-body' ).html( ret.detail.article );
$( compareDiv ).find( '.compare-pane-right-body' ).html( ret.detail.source );
$leftPane.html( ret.detail.article );
$rightPane.html( ret.detail.source );

// fetch the first instance of a match and auto-scroll to it
// (-20 to account for line height and another 20 for some purty offset padding)
var leftMatch = $leftPane.find( '.cv-hl' )[0],
rightMatch = $rightPane.find( '.cv-hl' )[0];
if ( leftMatch ) {
$leftPane.scrollTop( leftMatch.offsetTop - 40 );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line-height is only 20, why use 40 here?

}
if ( rightMatch ) {
$rightPane.scrollTop( rightMatch.offsetTop - 40 );
}
} else {
// use API-provided error message, otherwise a blanket unknown error message as it could be unrelated to the API
var errorMessage = ret.error && ret.error.info ? ret.error.info : 'An unknown error occurred.';
$( compareDiv ).find( '.compare-pane-body' ).html( '<span class="text-danger">' + errorMessage + '</span>' );
}
$( compareDiv ).addClass( 'copyvios-fetched' );
} );

// add fetched class immediately, so if they close/open the pane
// it doesn't keep making requests while the initial one hasn't finished
$( compareDiv ).addClass( 'copyvios-fetched' );
}
}

Expand Down