Skip to content

Commit a2dbb84

Browse files
committed
Handle dynamic visibility of GitHub token makkup
The code shows/hides HTML elements depending on the current state of the repository's setup: - if Client ID/Secret are not set, display instructions - if they are defined, show the "Click to authorize" button - if Authorized (i.e. an app token is defined) then show the "Revoke" and "Create Webhook" buttons
1 parent 0d224cf commit a2dbb84

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

SourceGithub/files/sourcegithub.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,47 @@ SourceGithub.rest_api = function(endpoint) {
1818
};
1919

2020
jQuery(document).ready(function($) {
21+
$('#hub_app_client_id, #hub_app_secret').change(set_visibility);
2122
$('#webhook_create > button').click(webhook_create);
2223

24+
// The PHP code initially hides all token authorization elements using the.
25+
// 'hidden' class, which we need to remove so we can set visibility using
26+
// show/hide functions
27+
set_visibility();
28+
$('.sourcegithub_token, #id_secret_missing').removeClass('hidden');
29+
30+
function set_visibility() {
31+
var div_id_secret_missing = $('#id_secret_missing');
32+
var client_id = $('#hub_app_client_id');
33+
var secret = $('#hub_app_secret');
34+
35+
// If Client ID and secret are set and equal to the recorded values
36+
// for the repository, we hide the information message and display the
37+
// authorize or revoke button and authorization status as needed.
38+
if ( client_id.val() !== ''
39+
&& client_id.val() === client_id.data('original')
40+
&& secret.val() !== ''
41+
&& secret.val() === secret.data('original')
42+
) {
43+
var div_token_authorized = $('#token_authorized');
44+
var div_token_missing = $('#token_missing');
45+
var div_webhook = $('#webhook_create');
46+
var token = div_token_authorized.children('input');
47+
48+
div_id_secret_missing.hide();
49+
if (token.val() !== '') {
50+
div_token_authorized.add(div_webhook).show();
51+
div_token_missing.hide();
52+
} else {
53+
div_token_authorized.add(div_webhook).hide();
54+
div_token_missing.show();
55+
}
56+
} else {
57+
div_id_secret_missing.show();
58+
$('.sourcegithub_token').hide();
59+
}
60+
}
61+
2362
function webhook_create() {
2463
var repo_id = $('#repo_id').val();
2564
var status_icon = $('#webhook_status > i');

0 commit comments

Comments
 (0)