Skip to content

Commit

Permalink
Add test and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mooreryan committed Feb 28, 2019
1 parent 0c28c36 commit 2b1c180
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 41 deletions.
6 changes: 6 additions & 0 deletions app/assets/javascripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ global.html.id.tree_width = "width";
global.html.id.tree_height = "height";
global.html.id.tree_padding = "padding";

// Scale bar options
global.html.id.scale_bar_show = "show-scale-bar";
global.html.id.scale_bar_offset_weight = "scale-bar-offset-weight";
global.html.id.scale_bar_autosize = "scale-bar-auto-size";
global.html.id.scale_bar_length = "scale-bar-length";

// Label options
global.html.id.inner_labels_show = "show-inner-labels";
global.html.id.inner_labels_size = "inner-label-size";
Expand Down
84 changes: 48 additions & 36 deletions app/assets/javascripts/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ var viewer = {

viewer.defaults.inner_labels_size = 12;
viewer.defaults.inner_labels_color = "#000000";
viewer.defaults.inner_labels_font = "Helvetica";
viewer.defaults.inner_labels_font = "Helvetica";

// Scale bar defaults
viewer.defaults.scale_bar_length = 1;
viewer.defaults.scale_bar_length_is_disabled = true;
viewer.defaults.scale_bar_offset_weight = 1;
viewer.defaults.scale_bar_autosize_is_checked = true;
viewer.defaults.scale_bar_show_is_checked = true;

var MAPPING_CHANGED, TREE_CHANGED;

Expand Down Expand Up @@ -218,22 +225,22 @@ var ID_LAYOUT_RECTANGULAR = "rectangular-tree",
// ID_SORT_FORWARD = "descending",
// ID_SORT_REVERSE = "ascending",
// ID_SORT_UNSORTED = "not-sorted";
var ID_SCALE_BAR_SHOW = "show-scale-bar",
ID_SCALE_BAR_OFFSET_WEIGHT = "scale-bar-offset-weight",
ID_SCALE_BAR_AUTOSIZE = "scale-bar-auto-size",
ID_SCALE_BAR_LENGTH = "scale-bar-length";
var ID_VIEWER_SIZE_FIXED = "viewer-size-fixed";
var ID_OPTIONS_ACCORDION = "options-accordion";
var ID_LEAF_LABEL_COLOR = "leaf-label-color",
ID_LEAF_LABEL_FONT = "leaf-label-font",
ID_LEAF_LABEL_PADDING = "leaf-label-padding",
ID_LEAF_LABEL_ALIGN = "align-tip-labels",
VAL_LEAF_LABEL_COLOR,
VAL_LEAF_LABEL_FONT,
VAL_LEAF_LABEL_PADDING,
VAL_LEAF_LABEL_ALIGN;

var ID_INNER_LABEL_FONT = "inner-label-font",
var //ID_SCALE_BAR_SHOW = "show-scale-bar",
//ID_SCALE_BAR_OFFSET_WEIGHT = "scale-bar-offset-weight",
ID_SCALE_BAR_AUTOSIZE = "scale-bar-auto-size";
//ID_SCALE_BAR_LENGTH = "scale-bar-length";
var ID_VIEWER_SIZE_FIXED = "viewer-size-fixed";
var ID_OPTIONS_ACCORDION = "options-accordion";
var ID_LEAF_LABEL_COLOR = "leaf-label-color",
ID_LEAF_LABEL_FONT = "leaf-label-font",
ID_LEAF_LABEL_PADDING = "leaf-label-padding",
ID_LEAF_LABEL_ALIGN = "align-tip-labels",
VAL_LEAF_LABEL_COLOR,
VAL_LEAF_LABEL_FONT,
VAL_LEAF_LABEL_PADDING,
VAL_LEAF_LABEL_ALIGN;

var ID_INNER_LABEL_FONT = "inner-label-font",
VAL_INNER_LABEL_COLOR,
VAL_INNER_LABEL_FONT;

Expand Down Expand Up @@ -848,7 +855,7 @@ function lalala(tree_input_param, mapping_input_param) {
}, TIMEOUT * 2);
});

listener(ID_SCALE_BAR_SHOW, "change", function () {
listener(global.html.id.scale_bar_show, "change", function () {
utils__set_status_msg_to_rendering();

setTimeout(function () {
Expand All @@ -859,7 +866,7 @@ function lalala(tree_input_param, mapping_input_param) {
utils__set_status_msg_to_done();
}, TIMEOUT);
});
listener(ID_SCALE_BAR_OFFSET_WEIGHT, "change", function () {
listener(global.html.id.scale_bar_offset_weight, "change", function () {
utils__set_status_msg_to_rendering();

setTimeout(function () {
Expand All @@ -870,15 +877,15 @@ function lalala(tree_input_param, mapping_input_param) {
utils__set_status_msg_to_done();
}, TIMEOUT);
});
listener(ID_SCALE_BAR_AUTOSIZE, "change", function () {
listener(global.html.id.scale_bar_autosize, "change", function () {
utils__set_status_msg_to_rendering();

setTimeout(function () {
if (document.getElementById(ID_SCALE_BAR_AUTOSIZE).checked) {
jq(ID_SCALE_BAR_LENGTH).prop("disabled", true);
if (document.getElementById(global.html.id.scale_bar_autosize).checked) {
jq(global.html.id.scale_bar_length).prop("disabled", true);
}
else {
jq(ID_SCALE_BAR_LENGTH).prop("disabled", false);
jq(global.html.id.scale_bar_length).prop("disabled", false);
}


Expand All @@ -888,7 +895,7 @@ function lalala(tree_input_param, mapping_input_param) {
utils__set_status_msg_to_done();
}, TIMEOUT);
});
listener(ID_SCALE_BAR_LENGTH, "change", function () {
listener(global.html.id.scale_bar_length, "change", function () {
utils__set_status_msg_to_rendering();

setTimeout(function () {
Expand Down Expand Up @@ -1374,9 +1381,9 @@ function lalala(tree_input_param, mapping_input_param) {
sort_function = sort_descending;
}

SHOW_SCALE_BAR = document.getElementById(ID_SCALE_BAR_SHOW).checked;
SCALE_BAR_OFFSET_WEIGHT = parseFloat(document.getElementById(ID_SCALE_BAR_OFFSET_WEIGHT).value);
SCALE_BAR_LENGTH = parseFloat(document.getElementById(ID_SCALE_BAR_LENGTH).value);
SHOW_SCALE_BAR = document.getElementById(global.html.id.scale_bar_show).checked;
SCALE_BAR_OFFSET_WEIGHT = parseFloat(document.getElementById(global.html.id.scale_bar_offset_weight).value);
SCALE_BAR_LENGTH = parseFloat(document.getElementById(global.html.id.scale_bar_length).value);


LAYOUT_CIRCLE = document.getElementById("circular-tree").selected;
Expand Down Expand Up @@ -2798,16 +2805,16 @@ function draw_scale_bar(user_changed) {
}

var scale_bar_pixels;
if (document.getElementById(ID_SCALE_BAR_AUTOSIZE).checked) {
if (document.getElementById(global.html.id.scale_bar_autosize).checked) {
scale_bar_pixels = mean_length * pixels_per_unit_length;
jq(ID_SCALE_BAR_LENGTH).val(mean_length);
jq(global.html.id.scale_bar_length).val(mean_length);
}
else {
scale_bar_pixels = jq(ID_SCALE_BAR_LENGTH).val() * pixels_per_unit_length;
scale_bar_pixels = jq(global.html.id.scale_bar_length).val() * pixels_per_unit_length;

if (isNaN(scale_bar_pixels)) {
scale_bar_pixels = mean_length * pixels_per_unit_length;
jq(ID_SCALE_BAR_LENGTH).val(mean_length);
jq(global.html.id.scale_bar_length).val(mean_length);
}
}

Expand All @@ -2824,7 +2831,7 @@ function draw_scale_bar(user_changed) {
// // Now that we have a minimum scale bar size, weight it by the slider value.
// scale_bar_pixels *= SCALE_BAR_LENGTH;
var scale_bar_label_text = fn.math.round(scale_bar_pixels / pixels_per_unit_length, ROUNDING_PRECISION);
jq(ID_SCALE_BAR_LENGTH).val(scale_bar_label_text);
jq(global.html.id.scale_bar_length).val(scale_bar_label_text);


var label_x, label_y;
Expand Down Expand Up @@ -3357,10 +3364,15 @@ function reset_all_to_defaults() {
jq(global.html.id.tree_sorting).val(global.html.id.tree_sorting_forward);

// Scale bar options
check(ID_SCALE_BAR_SHOW);
check(ID_SCALE_BAR_AUTOSIZE);
jq(ID_SCALE_BAR_LENGTH).val(1).prop("disabled", true);
$("#scale-bar-offset-weight").val(1);
jq(global.html.id.scale_bar_show)
.prop("checked", viewer.defaults.scale_bar_show_is_checked);
jq(global.html.id.scale_bar_autosize)
.prop("checked", viewer.defaults.scale_bar_autosize_is_checked);
jq(global.html.id.scale_bar_length)
.val(viewer.defaults.scale_bar_length)
.prop("disabled", viewer.defaults.scale_bar_length_is_disabled);
jq(global.html.id.scale_bar_offset_weight)
.val(viewer.defaults.scale_bar_offset_weight);

// Label options
uncheck(global.html.id.inner_labels_show);
Expand Down
8 changes: 4 additions & 4 deletions app/views/layouts/_options_panel.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@
.row
.small-12.medium-12.large-6.columns
label for="show-scale-bar" Show scale bar?
input id="show-scale-bar" type="checkbox" checked=""
input id="show-scale-bar" type="checkbox"
.small-12.medium-12.large-6.columns
label for="scale-bar-offset-weight" Offset
input id="scale-bar-offset-weight" class="slider width-100" type="range" min="1" max="2.5" value="1" step="0.1"
input id="scale-bar-offset-weight" class="slider width-100" type="range" min="1" max="2.5" step="0.1"
.row
.small-12.medium-12.large-6.columns
label for="scale-bar-auto-size" Auto size
input id="scale-bar-auto-size" type="checkbox" checked=""
input id="scale-bar-auto-size" type="checkbox"
.small-12.medium-12.large-6.columns
label for="scale-bar-length" Length
input id="scale-bar-length" disabled=""
input id="scale-bar-length"
/input id="scale-bar-length-weight" class="slider width-100" type="range" min="0.25" max="2" value="1" step="0.25"
li class="accordion-item" data-accordion-item=""
Expand Down
33 changes: 32 additions & 1 deletion spec/javascripts/viewer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,38 @@ describe("viewer", function () {
});
});

describe("label options", function () {
describe("scale bar options", function () {

it("checks show scale bar", function () {
expect(
$("#" + global.html.id.scale_bar_show).prop("checked")
).to.be.true;
});

it("sets default scale bar offset", function () {
expect(
parseFloat($("#" + global.html.id.scale_bar_offset_weight).val())
).to.equal(viewer.defaults.scale_bar_offset_weight);
});

it("checks autosize scale bar", function () {
expect(
$("#" + global.html.id.scale_bar_autosize).prop("checked")
).to.equal(viewer.defaults.scale_bar_autosize_is_checked);
});

it("sets the default scale bar length", function () {
expect(
parseFloat($("#" + global.html.id.scale_bar_length).val())
).to.equal(viewer.defaults.scale_bar_length);

expect(
$("#" + global.html.id.scale_bar_length).prop("disabled")
).to.equal(viewer.defaults.scale_bar_length_is_disabled);
});
});

describe("inner label options", function () {
it("unchecks show leaf labels", function () {
expect(
$("#" + global.html.id.inner_labels_show).prop("checked")
Expand Down

0 comments on commit 2b1c180

Please sign in to comment.