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

Add Reverse Option #317

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions css/ion.rangeSlider.css
Expand Up @@ -42,6 +42,9 @@
position: absolute; display: block;
top: 0; left: 0;
}
.irs-bar-edge.reverse {
left:auto; right:0;
}

.irs-shadow {
position: absolute; display: none;
Expand Down
6 changes: 6 additions & 0 deletions css/ion.rangeSlider.skinHTML5.css
Expand Up @@ -48,6 +48,12 @@
border-radius: 16px 0 0 16px;
-moz-border-radius: 16px 0 0 16px;
}
.irs-bar-edge.reverse {
margin-right: 1px;
border-width: 1px 1px 1px 0;
border-radius:0 16px 16px 0;
-moz-border-radius:0 16px 16px 0;
}

.irs-shadow {
height: 2px; top: 38px;
Expand Down
138 changes: 115 additions & 23 deletions js/ion.rangeSlider.js
@@ -1,4 +1,4 @@
// Ion.RangeSlider
// Ion.RangeSlider
// version 2.1.2 Build: 350
// © Denis Ineshin, 2015
// https://github.com/IonDen
Expand Down Expand Up @@ -311,6 +311,8 @@

disable: false,

reverse: false,

onStart: null,
onChange: null,
onFinish: null,
Expand Down Expand Up @@ -369,7 +371,9 @@

input_values_separator: $inp.data("inputValuesSeparator"),

disable: $inp.data("disable")
disable: $inp.data("disable"),

reverse: $inp.data("reverse")
};
config_from_data.values = config_from_data.values && config_from_data.values.split(",");

Expand Down Expand Up @@ -488,8 +492,15 @@

this.$cache.cont.html(base_html);
this.$cache.rs = this.$cache.cont.find(".irs");
this.$cache.min = this.$cache.cont.find(".irs-min");
this.$cache.max = this.$cache.cont.find(".irs-max");

if (this.options.reverse) {
this.$cache.min = this.$cache.cont.find(".irs-max");
this.$cache.max = this.$cache.cont.find(".irs-min");
} else {
this.$cache.min = this.$cache.cont.find(".irs-min");
this.$cache.max = this.$cache.cont.find(".irs-max");
}

this.$cache.from = this.$cache.cont.find(".irs-from");
this.$cache.to = this.$cache.cont.find(".irs-to");
this.$cache.single = this.$cache.cont.find(".irs-single");
Expand All @@ -499,7 +510,7 @@

if (this.options.type === "single") {
this.$cache.cont.append(single_html);
this.$cache.edge = this.$cache.cont.find(".irs-bar-edge");
this.$cache.edge = this.$cache.cont.find(".irs-bar-edge")
this.$cache.s_single = this.$cache.cont.find(".single");
this.$cache.from[0].style.visibility = "hidden";
this.$cache.to[0].style.visibility = "hidden";
Expand Down Expand Up @@ -534,6 +545,10 @@
if (this.options.drag_interval) {
this.$cache.bar[0].style.cursor = "ew-resize";
}

if (this.options.type === "single") {
this.$cache.edge.toggleClass('reverse', this.options.reverse);
}
},

/**
Expand Down Expand Up @@ -852,14 +867,23 @@
*/
moveByKey: function (right) {
var p = this.coords.p_pointer;

if (this.options.reverse) {
right = !right;
}

if (right) {
p += this.options.keyboard_step;
} else {
p -= this.options.keyboard_step;
}

this.coords.x_pointer = this.toFixed(this.coords.w_rs / 100 * p);
if (this.options.reverse) {
this.coords.x_pointer = this.coords.w_rs - this.toFixed(this.coords.w_rs / 100 * p);
} else {
this.coords.x_pointer = this.toFixed(this.coords.w_rs / 100 * p);
}

this.is_key = true;
this.calc();
},
Expand Down Expand Up @@ -1128,7 +1152,12 @@
this.coords.x_pointer = this.coords.w_rs;
}

this.coords.p_pointer = this.toFixed(this.coords.x_pointer / this.coords.w_rs * 100);
if (this.options.reverse) {
this.coords.p_pointer = this.toFixed(this.convertToReversePercent(this.coords.x_pointer / this.coords.w_rs * 100));
} else {
this.coords.p_pointer = this.toFixed(this.coords.x_pointer / this.coords.w_rs * 100);
}

},

convertToRealPercent: function (fake) {
Expand Down Expand Up @@ -1303,14 +1332,21 @@
if (this.old_from !== this.result.from || this.old_to !== this.result.to || this.force_redraw || this.is_key) {

this.drawLabels();

this.$cache.bar[0].style.left = this.coords.p_bar_x + "%";
if (this.options.reverse) {
this.$cache.bar[0].style.left = this.convertToReversePercent(this.coords.p_bar_x) - this.coords.p_bar_w + "%";
} else {
this.$cache.bar[0].style.left = this.coords.p_bar_x + "%";
}
this.$cache.bar[0].style.width = this.coords.p_bar_w + "%";

if (this.options.type === "single") {
this.$cache.s_single[0].style.left = this.coords.p_single_fake + "%";

this.$cache.single[0].style.left = this.labels.p_single_left + "%";
if (this.options.reverse) {
this.$cache.s_single[0].style.left = this.convertToReversePercent(this.coords.p_single_fake) - this.coords.p_handle + "%"
this.$cache.single[0].style.left = this.convertToReversePercent(this.labels.p_single_left) - this.labels.p_single_fake + "%";
} else {
this.$cache.s_single[0].style.left = this.coords.p_single_fake + "%";
this.$cache.single[0].style.left = this.labels.p_single_left + "%";
}

if (this.options.values.length) {
this.$cache.input.prop("value", this.result.from_value);
Expand All @@ -1319,17 +1355,36 @@
}
this.$cache.input.data("from", this.result.from);
} else {
this.$cache.s_from[0].style.left = this.coords.p_from_fake + "%";
this.$cache.s_to[0].style.left = this.coords.p_to_fake + "%";

if (this.options.reverse) {
this.$cache.s_from[0].style.left = this.convertToReversePercent(this.coords.p_from_fake) - this.coords.p_handle + "%";
this.$cache.s_to[0].style.left = this.convertToReversePercent(this.coords.p_to_fake) - this.coords.p_handle + "%";
} else {
this.$cache.s_from[0].style.left = this.coords.p_from_fake + "%";
this.$cache.s_to[0].style.left = this.coords.p_to_fake + "%";
}

if (this.old_from !== this.result.from || this.force_redraw) {
this.$cache.from[0].style.left = this.labels.p_from_left + "%";
if (this.options.reverse) {
this.$cache.from[0].style.left = this.convertToReversePercent(this.labels.p_from_left) - this.labels.p_from_fake + "%";
} else {
this.$cache.from[0].style.left = this.labels.p_from_left + "%";
}
}

if (this.old_to !== this.result.to || this.force_redraw) {
this.$cache.to[0].style.left = this.labels.p_to_left + "%";
if (this.options.reverse) {
this.$cache.to[0].style.left = this.convertToReversePercent(this.labels.p_to_left) - this.labels.p_to_fake + "%";
} else {
this.$cache.to[0].style.left = this.labels.p_to_left + "%";
}
}

if (this.options.reverse) {
this.$cache.single[0].style.left = this.convertToReversePercent(this.labels.p_single_left) - this.labels.p_single_fake + "%";
} else {
this.$cache.single[0].style.left = this.labels.p_single_left + "%";
}

this.$cache.single[0].style.left = this.labels.p_single_left + "%";

if (this.options.values.length) {
this.$cache.input.prop("value", this.result.from_value + this.options.input_values_separator + this.result.to_value);
Expand Down Expand Up @@ -1520,6 +1575,10 @@
from_max = this.toFixed(from_max - (this.coords.p_handle / 100 * from_max));
from_min = from_min + (this.coords.p_handle / 2);

if (this.options.reverse) {
from_min = this.convertToReversePercent(from_min) - from_max;
}

c.shad_single[0].style.display = "block";
c.shad_single[0].style.left = from_min + "%";
c.shad_single[0].style.width = from_max + "%";
Expand All @@ -1534,6 +1593,10 @@
from_max = this.toFixed(from_max - (this.coords.p_handle / 100 * from_max));
from_min = from_min + (this.coords.p_handle / 2);

if (this.options.reverse) {
from_min = this.convertToReversePercent(from_min) - from_max;
}

c.shad_from[0].style.display = "block";
c.shad_from[0].style.left = from_min + "%";
c.shad_from[0].style.width = from_max + "%";
Expand All @@ -1548,6 +1611,10 @@
to_max = this.toFixed(to_max - (this.coords.p_handle / 100 * to_max));
to_min = to_min + (this.coords.p_handle / 2);

if (this.options.reverse) {
to_min = this.convertToReversePercent(to_min) - to_max;
}

c.shad_to[0].style.display = "block";
c.shad_to[0].style.left = to_min + "%";
c.shad_to[0].style.width = to_max + "%";
Expand Down Expand Up @@ -1620,6 +1687,16 @@
return this.toFixed(percent);
},

/**
* Convert percent to its reverse
*
* @param value {Number} X in percent
* @returns {Number} X in reverse percent
*/
convertToReversePercent: function (value) {
return this.toFixed(100 - value);
},

/**
* Convert percent to real values
*
Expand Down Expand Up @@ -2095,11 +2172,18 @@
}

small_w = this.toFixed(big_w - (small_p * z));

html += '<span class="irs-grid-pol small" style="left: ' + small_w + '%"></span>';
if (this.options.reverse) {
html += '<span class="irs-grid-pol small" style="left: ' + this.convertToReversePercent(small_w) + '%"></span>';
} else {
html += '<span class="irs-grid-pol small" style="left: ' + small_w + '%"></span>';
}
}

html += '<span class="irs-grid-pol" style="left: ' + big_w + '%"></span>';
if (this.options.reverse) {
html += '<span class="irs-grid-pol" style="left: ' + this.convertToReversePercent(big_w) + '%"></span>';
} else {
html += '<span class="irs-grid-pol" style="left: ' + big_w + '%"></span>';
}

result = this.convertToValue(big_w);
if (o.values.length) {
Expand All @@ -2108,7 +2192,11 @@
result = this._prettify(result);
}

html += '<span class="irs-grid-text js-grid-text-' + i + '" style="left: ' + big_w + '%">' + result + '</span>';
if (this.options.reverse) {
html += '<span class="irs-grid-text js-grid-text-' + i + '" style="left: ' + this.convertToReversePercent(big_w) + '%">' + result + '</span>';
} else {
html += '<span class="irs-grid-text js-grid-text-' + i + '" style="left: ' + big_w + '%">' + result + '</span>';
}
}
this.coords.big_num = Math.ceil(big_num + 1);

Expand Down Expand Up @@ -2165,7 +2253,11 @@

for (i = 0; i < num; i++) {
label = this.$cache.grid_labels[i][0];
label.style.marginLeft = -this.coords.big_x[i] + "%";
if (this.options.reverse) {
label.style.marginLeft = this.coords.big_x[i] - this.coords.big_p[i] + "%";
} else {
label.style.marginLeft = -this.coords.big_x[i] + "%";
}
}
},

Expand Down