Skip to content

Commit 3b1f109

Browse files
authored
index.html beautification and copying implementation (#29)
1 parent 8b3c9c1 commit 3b1f109

File tree

2 files changed

+2656
-1408
lines changed

2 files changed

+2656
-1408
lines changed

src/web/html/assets/js/alaska_main.js

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,12 @@ function show_sample_form(id, form) {
12731273
current_sample_form.off('hidden.bs.collapse');
12741274
});
12751275

1276+
// Hide all the open popovers.
1277+
for (var i = 0; i < shown_popovers.length; i++) {
1278+
var popover = shown_popovers[i];
1279+
popover.popover('hide');
1280+
}
1281+
12761282
current_sample_form.collapse('hide');
12771283
}
12781284
else {
@@ -1652,7 +1658,7 @@ function add_input_row() {
16521658
* Sets functionality for adding/removing input rows.
16531659
*/
16541660
function set_fluid_input_rows(div) {
1655-
var button = div.find('button:first');
1661+
var button = div.find('.add_btn');
16561662

16571663
// Set up the add button.
16581664
button.click(add_input_row);
@@ -4171,8 +4177,9 @@ function set_samples_meta_input() {
41714177
set_reads_table(id, new_sample_form);
41724178

41734179
// Characteristics.
4174-
set_fluid_input_rows(new_sample_form.find('.sample_specific_'
4175-
+ 'characteristics_group'));
4180+
var char_group = new_sample_form.find('.sample_specific_'
4181+
+ 'characteristics_group');
4182+
set_fluid_input_rows(char_group.find('.sample_characteristics_inputs'));
41764183

41774184
// Save changes button.
41784185
var save_changes_btn = new_sample_form.find('.save_btn');
@@ -4183,6 +4190,12 @@ function set_samples_meta_input() {
41834190
save_proj(show_saved, btn);
41844191
});
41854192

4193+
// Set up copy buttons.
4194+
var copy_btns = new_sample_form.find('.copy_btn');
4195+
copy_btns.each(function () {
4196+
set_copy_btn($(this), id);
4197+
})
4198+
41864199
// Append new form.
41874200
$('#sample_card').append(new_sample_form);
41884201
}
@@ -4195,6 +4208,107 @@ function set_samples_meta_input() {
41954208
enable_popovers_tooltips(sample_form);
41964209
}
41974210

4211+
/**
4212+
* Set up copy button to show a popover.
4213+
*/
4214+
function set_copy_btn(copy_btn, id_to_ignore) {
4215+
// Extract the data-group HTML attribute.
4216+
var group = copy_btn.data('group');
4217+
4218+
// This is the DOM element that will be placed as the content of the div.
4219+
var wrapper = $('<div>');
4220+
var content = $('<div class="btn-group-toggle" data-toggle="buttons" '
4221+
+ 'data-group=' + group + '></div>');
4222+
4223+
// This is a template of the label and input to copy.
4224+
var label = $('<label class="btn btn-sm btn-outline-primary mr-2"></label>');
4225+
var input = $('<input type="checkbox" name="copy" autocomplete="off">');
4226+
4227+
// Loop through each sample and make a button for each, except the one
4228+
// corresponding to id_to_ignore.
4229+
for (var i = 0; i < sorted_names.length; i++) {
4230+
var name = sorted_names[i];
4231+
var id = names_to_ids[name];
4232+
4233+
if (id != id_to_ignore) {
4234+
// Make a copy of the input and label and set attributes.
4235+
var label_copy = label.clone();
4236+
var input_copy = input.clone();
4237+
input_copy.val(id);
4238+
4239+
label_copy.append(input_copy);
4240+
label_copy.append(name);
4241+
content.append(label_copy);
4242+
}
4243+
}
4244+
// Add button to actually do the copying.
4245+
wrapper.append(content);
4246+
wrapper.append($('<hr>'));
4247+
wrapper.append($('<button class="btn btn-warning copy_btn" ' +
4248+
'data-group=' + group + '>Copy to selected samples</button>'))
4249+
4250+
// Set up the popover.
4251+
// Note that we need to add a listener for when the popover if shown to
4252+
// appropriately implement the actual copy functionality.
4253+
copy_btn.popover({
4254+
'title': 'Please select the samples to copy this input to',
4255+
'content': wrapper.html(),
4256+
// We can't set the trigger to be 'focus' because then the buttons within
4257+
// the popover are not clickable.
4258+
// 'trigger': 'focus',
4259+
'html': true
4260+
}).on('shown.bs.popover', function (e) {
4261+
var popover = $('#' + $(e.target).attr('aria-describedby'));
4262+
popover.find('.copy_btn').click(copy_input);
4263+
4264+
// Add this popover as one of the shown popovers.
4265+
// When the current sample form is changed, we also have to loop through
4266+
// this list to close all opened popovers.
4267+
shown_popovers.push($(this));
4268+
});
4269+
}
4270+
4271+
/**
4272+
* Helper function that actually implements the copying.
4273+
* This function is called by the copy button located WITHIN THE POPOVER.
4274+
*/
4275+
function copy_input() {
4276+
var copy_btn = $(this);
4277+
var popover_body = copy_btn.parents('.popover-body')
4278+
var group = copy_btn.data('group');
4279+
4280+
// Get the input to copy.
4281+
var input_group = current_sample_form.find('.' + group);
4282+
var group_type = sample_meta_classes_to_functions[group];
4283+
var val = getters_and_setters[group_type].get(input_group);
4284+
4285+
// Construct a list of sample ids to copy it to.
4286+
var ids_to_copy = [];
4287+
var active_labels = popover_body.find('label.active');
4288+
active_labels.each(function () {
4289+
var label = $(this);
4290+
var checkbox = label.children('input');
4291+
ids_to_copy.push(checkbox.val());
4292+
});
4293+
4294+
// Output list of selected ids for debugging.
4295+
console.log(ids_to_copy);
4296+
4297+
// Copy the value to these samples.
4298+
for (var i = 0; i < ids_to_copy.length; i++) {
4299+
var id = ids_to_copy[i];
4300+
var sample_form = sample_forms[id];
4301+
4302+
var input_group_to_copy = sample_form.find('.' + group);
4303+
getters_and_setters[group_type].set(input_group_to_copy, val);
4304+
}
4305+
4306+
// Finally, disable this button to let the user know the values have
4307+
// been copied.
4308+
copy_btn.text('Copied!');
4309+
copy_btn.prop('disabled', true);
4310+
}
4311+
41984312
/**
41994313
* Sets characteristic selection dropdown options for the control modal.
42004314
*/
@@ -5138,6 +5252,7 @@ var ftp_pw;
51385252
var raw_reads_div;
51395253
var controls_modal;
51405254
var dropdown_items = {};
5255+
var shown_popovers = [];
51415256
var proj_form;
51425257
var common_form;
51435258
var current_sample_form;

0 commit comments

Comments
 (0)