Skip to content

Commit

Permalink
Create CopyToClipboardInput view component
Browse files Browse the repository at this point in the history
  • Loading branch information
Dany Marcoux authored and eduardoj committed Sep 13, 2021
1 parent 3f57136 commit 8f4b738
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 46 deletions.
23 changes: 23 additions & 0 deletions src/api/app/components/copy_to_clipboard_input_component.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-# TODO: Relying on a form builder is a bit weird. We could perhaps directly use `text_field_tag` instead and pass an input value to the component.
= @form_builder.text_field(:string_readonly, id: 'copy-to-clipboard-readonly', class: 'form-control', readonly: true)
.input-group-append#copy-to-clipboard
%span.input-group-text
%i.fas.fa-clipboard

:javascript
function copyToClipboard() {
document.getElementById('copy-to-clipboard-readonly').select();
document.execCommand('copy');
}

- content_for :ready_function do
:plain
// Definition of the tooltip and click event handler for the "Copy to clipboard" icon
$('#copy-to-clipboard').tooltip({ title: 'Copy to clipboard' }).on('click', function () {
copyToClipboard();

// Shows "Copied!" tooltip. Later on shows the previous message 'Copy to clipboard' as tooltip.
$(this).tooltip('dispose').tooltip({ title: 'Copied!' }).tooltip('show').on('hidden.bs.tooltip', function () {
$(this).tooltip('dispose').tooltip({ title: 'Copy to clipboard' });
});
});
6 changes: 6 additions & 0 deletions src/api/app/components/copy_to_clipboard_input_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CopyToClipboardInputComponent < ApplicationComponent
def initialize(form_builder:)
super
@form_builder = form_builder
end
end
4 changes: 2 additions & 2 deletions src/api/app/views/webui/users/tokens/_create.js.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$('#flash').html("<%= escape_javascript(render(layout: false, partial: 'layouts/webui/flash', object: flash)) %>");
<% if flash[:error].blank? %>
$('#created-token').removeClass('d-none');
$('#token_string_readonly').val("<%= escape_javascript(string) %>");
$('.form-row :input').not('#token_string_readonly').prop('disabled', true);
$('#copy-to-clipboard-readonly').val("<%= escape_javascript(string) %>");
$('.form-row :input').not('#copy-to-clipboard-readonly').prop('disabled', true);
$('.actions').html("<%= escape_javascript(render partial: 'actions') %>");
<% end %>
4 changes: 2 additions & 2 deletions src/api/app/views/webui/users/tokens/_update.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ $('#flash').html("<%= escape_javascript(render(layout: false, partial: 'layouts/
<% if flash[:error].blank? %>
$('#regenerate-token-modal').modal('hide');
$('#regenerate-token-group,#scm-token-group').addClass('d-none');
$('#token_string_readonly').val("<%= escape_javascript(string) %>");
$('#copy-to-clipboard-readonly').val("<%= escape_javascript(string) %>");
$('.actions').html("<%= escape_javascript(render partial: 'actions') %>");
$('#created-token').removeClass('d-none').hide().slideDown('slow', function() {
$('#token_string_readonly').focus();
$('#copy-to-clipboard-readonly').focus();
});
<% end %>
23 changes: 1 addition & 22 deletions src/api/app/views/webui/users/tokens/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
%fieldset.form-group
= f.label(:string_readonly, 'Your new OBS Personal Access Token Secret', class: 'col-form-label col-form-label-lg')
.input-group
= f.text_field(:string_readonly, class: 'form-control', readonly: true)
.input-group-append#copy-to-clipboard
%span.input-group-text
%i.fas.fa-clipboard
= render CopyToClipboardInputComponent.new(form_builder: f)
%hr

.form-row
Expand Down Expand Up @@ -61,21 +58,3 @@
%a.btn.btn-sm.btn-outline-secondary.px-4{ data: { dismiss: 'modal' } }
Cancel
= f.submit('Regenerate', class: 'btn btn-sm btn-danger px-4', data: { disable: { with: 'Regenerate' } })

:javascript
function copyToClipboard(elementId) {
document.getElementById(elementId).select();
document.execCommand('copy');
}

- content_for :ready_function do
:plain
// Definition of the tooltip and click event handler for the "Copy to clipboard" icon
$('#copy-to-clipboard').tooltip({ title: 'Copy to clipboard' }).on('click', function () {
copyToClipboard('token_string_readonly');

// Shows "Copied!" tooltip. Later on shows the previous message 'Copy to clipboard' as tooltip.
$(this).tooltip('dispose').tooltip({ title: 'Copied!' }).tooltip('show').on('hidden.bs.tooltip', function () {
$(this).tooltip('dispose').tooltip({ title: 'Copy to clipboard' });
});
});
21 changes: 1 addition & 20 deletions src/api/app/views/webui/users/tokens/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
%fieldset.form-group
= f.label(:string_readonly, 'Your new OBS Personal Access Token Secret', class: 'col-form-label col-form-label-lg')
.input-group
= f.text_field(:string_readonly, class: 'form-control', readonly: true)
.input-group-append#copy-to-clipboard
%span.input-group-text
%i.fas.fa-clipboard
= render CopyToClipboardInputComponent.new(form_builder: f)
%hr

.form-row
Expand Down Expand Up @@ -51,12 +48,6 @@
= link_to('Cancel', tokens_path, title: 'Cancel', class: 'btn btn-outline-secondary px-4 mr-3 mt-3 mt-sm-0')
= f.submit('Create', class: 'btn btn-primary px-4 mt-3 mt-sm-0')

:javascript
function copyToClipboard(elementId) {
document.getElementById(elementId).select();
document.execCommand('copy');
}

- content_for :ready_function do
:plain
// Default type value: 'runservice'
Expand All @@ -75,13 +66,3 @@
$('#token_scm_token').attr('required', false);
}
});

// Definition of the tooltip and click event handler for the "Copy to clipboard" icon
$('#copy-to-clipboard').tooltip({ title: 'Copy to clipboard' }).on('click', function () {
copyToClipboard('token_string_readonly');

// Shows "Copied!" tooltip. Later on shows the previous message 'Copy to clipboard' as tooltip.
$(this).tooltip('dispose').tooltip({ title: 'Copied!' }).tooltip('show').on('hidden.bs.tooltip', function () {
$(this).tooltip('dispose').tooltip({ title: 'Copy to clipboard' });
});
});

0 comments on commit 8f4b738

Please sign in to comment.