1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-16 14:00:58 +00:00
ESPUI/data/js/slider.js

124 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-11-29 10:31:30 +00:00
/* -----------------------------------------------------
Material Design Sliders
CodePen URL: https://codepen.io/rkchauhan/pen/xVGGpR
By: Ravikumar Chauhan
-------------------------------------------------------- */
function rkmd_rangeSlider(selector) {
2019-12-28 13:17:29 +00:00
var self, slider_width, slider_offset, curnt, sliderDiscrete, range, slider;
self = $(selector);
slider_width = self.width();
slider_offset = self.offset().left;
sliderDiscrete = self;
sliderDiscrete.each(function (i, v) {
2019-12-28 13:17:29 +00:00
curnt = $(this);
curnt.append(sliderDiscrete_tmplt());
range = curnt.find('input[type="range"]');
slider = curnt.find(".slider");
slider_fill = slider.find(".slider-fill");
slider_handle = slider.find(".slider-handle");
slider_label = slider.find(".slider-label");
var range_val = parseInt(range.val());
slider_fill.css("width", range_val + "%");
slider_handle.css("left", range_val + "%");
slider_label.find("span").text(range_val);
});
self.on("mousedown touchstart", ".slider-handle", function (e) {
2019-12-28 13:17:29 +00:00
if (e.button === 2) {
return false;
}
var parents = $(this).parents(".rkmd-slider");
var slider_width = parents.width();
var slider_offset = parents.offset().left;
var check_range = parents.find('input[type="range"]').is(":disabled");
if (check_range === true) {
return false;
}
$(this).addClass("is-active");
var moveFu = function (e) {
2019-12-28 13:17:29 +00:00
var pageX = e.pageX || e.changedTouches[0].pageX;
var slider_new_width = pageX - slider_offset;
if (slider_new_width <= slider_width && !(slider_new_width < "0")) {
slider_move(parents, slider_new_width, slider_width, true);
}
};
var upFu = function (e) {
2019-12-28 13:17:29 +00:00
$(this).off(handlers);
parents.find(".is-active").removeClass("is-active");
};
var handlers = {
mousemove: moveFu,
touchmove: moveFu,
mouseup: upFu,
touchend: upFu,
2019-12-28 13:17:29 +00:00
};
$(document).on(handlers);
});
self.on("mousedown touchstart", ".slider", function (e) {
2019-12-28 13:17:29 +00:00
if (e.button === 2) {
return false;
}
2017-11-29 10:31:30 +00:00
2019-12-28 13:17:29 +00:00
var parents = $(this).parents(".rkmd-slider");
var slider_width = parents.width();
var slider_offset = parents.offset().left;
var check_range = parents.find('input[type="range"]').is(":disabled");
2017-11-29 10:31:30 +00:00
2019-12-28 13:17:29 +00:00
if (check_range === true) {
return false;
}
2017-11-29 10:31:30 +00:00
2019-12-28 13:17:29 +00:00
var slider_new_width = e.pageX - slider_offset;
if (slider_new_width <= slider_width && !(slider_new_width < "0")) {
slider_move(parents, slider_new_width, slider_width, true);
}
var upFu = function (e) {
2019-12-28 13:17:29 +00:00
$(this).off(handlers);
};
var handlers = {
mouseup: upFu,
touchend: upFu,
2019-12-28 13:17:29 +00:00
};
$(document).on(handlers);
});
}
2017-11-29 10:31:30 +00:00
function sliderDiscrete_tmplt() {
2019-12-28 13:17:29 +00:00
var tmplt =
'<div class="slider">' +
'<div class="slider-fill"></div>' +
'<div class="slider-handle"><div class="slider-label"><span>0</span></div></div>' +
"</div>";
2017-11-29 10:31:30 +00:00
2019-12-28 13:17:29 +00:00
return tmplt;
2017-11-29 10:31:30 +00:00
}
function slider_move(parents, newW, sliderW, send) {
2019-12-28 13:17:29 +00:00
var slider_new_val = parseInt(Math.round((newW / sliderW) * 100));
var slider_fill = parents.find(".slider-fill");
var slider_handle = parents.find(".slider-handle");
var range = parents.find('input[type="range"]');
range.next().html(newW); // update value
2019-12-28 13:17:29 +00:00
slider_fill.css("width", slider_new_val + "%");
slider_handle.css({
left: slider_new_val + "%",
transition: "none",
"-webkit-transition": "none",
"-moz-transition": "none",
2019-12-28 13:17:29 +00:00
});
range.val(slider_new_val);
if (parents.find(".slider-handle span").text() != slider_new_val) {
parents.find(".slider-handle span").text(slider_new_val);
var number = parents.attr("id").substring(2);
if (send) websock.send("slvalue:" + slider_new_val + ":" + number);
}
2017-11-29 10:31:30 +00:00
}