You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
changedetection.io/changedetectionio/static/js/visual-selector.js

134 lines
3.7 KiB

3 years ago
var current_selected_i;
3 years ago
var state_clicked=false;
var c = document.getElementById("selector-canvas");
// greyed out fill context
var xctx = c.getContext("2d");
// redline highlight context
var ctx = c.getContext("2d");
var current_default_xpath=$("#css_filter").val();
3 years ago
var x_scale;
var y_scale;
var selector_image = document.getElementById("selector-background");
var selector_image_rect;
3 years ago
3 years ago
function fetch_data() {
3 years ago
// Image is ready
$('.fetching-update-notice').html("Fetching element data..");
$.ajax({
url: watch_visual_selector_data_url,
context: document.body
}).done(function (data) {
$('.fetching-update-notice').html("Rendering..");
reflow_selector(data);
3 years ago
$('.fetching-update-notice').fadeOut();
3 years ago
});
3 years ago
};
3 years ago
3 years ago
$(document).on('keydown', function(event) {
if (event.key == "Escape") {
state_clicked=false;
}
});
3 years ago
3 years ago
$(window).resize(function() {
set_scale();
});
3 years ago
3 years ago
function set_scale() {
selector_image_rect = selector_image.getBoundingClientRect();
// make the canvas the same size as the image
3 years ago
$('#selector-canvas').attr('height', selector_image_rect.height);
$('#selector-canvas').attr('width', selector_image_rect.width);
3 years ago
x_scale = selector_image_rect.width / selector_image.naturalWidth;
y_scale = selector_image_rect.height / selector_image.naturalHeight;
3 years ago
3 years ago
ctx.strokeStyle = 'rgb(255,0,0, 0.8)';
ctx.lineWidth = 2;
3 years ago
}
function reflow_selector(selector_data) {
var selector_currnt_xpath_text=$("#selector-current-xpath span");
3 years ago
3 years ago
set_scale();
3 years ago
console.log(selector_data.length + " selectors found");
3 years ago
// highlight the default one if we can find it in the xPath list
// or the xpath matches the default one
for (var i = selector_data.length; i!=0; i--) {
var sel = selector_data[i-1];
if(selector_data[i - 1].xpath == current_default_xpath) {
ctx.strokeRect(sel.left * x_scale, sel.top * y_scale, sel.width * x_scale, sel.height * y_scale);
current_selected_i=i-1;
highlight_current_selected_i();
break;
}
}
3 years ago
$('#selector-canvas').bind('mousemove', function (e) {
3 years ago
if(state_clicked) {
return;
}
3 years ago
ctx.clearRect(0, 0, c.width, c.height);
current_selected_i=null;
// Reverse order - the most specific one should be deeper/"laster"
3 years ago
// Basically, find the most 'deepest'
3 years ago
for (var i = selector_data.length; i!=0; i--) {
// draw all of them? let them choose somehow?
var sel = selector_data[i-1];
3 years ago
// If we are in a bounding-box
3 years ago
if (e.offsetY > sel.top * y_scale && e.offsetY < sel.top * y_scale + sel.height * y_scale
&&
e.offsetX > sel.left * y_scale && e.offsetX < sel.left * y_scale + sel.width * y_scale
3 years ago
3 years ago
) {
3 years ago
// FOUND ONE
set_current_selected_text(sel.xpath);
3 years ago
ctx.strokeRect(sel.left * x_scale, sel.top * y_scale, sel.width * x_scale, sel.height * y_scale);
// no need to keep digging
3 years ago
// @todo or, O to go out/up, I to go in
// or double click to go up/out the selector?
current_selected_i=i-1;
3 years ago
break;
}
}
}.debounce(5));
3 years ago
function set_current_selected_text(s) {
selector_currnt_xpath_text[0].innerHTML=s;
}
function highlight_current_selected_i() {
if(state_clicked) {
state_clicked=false;
xctx.clearRect(0,0,c.width, c.height);
return;
}
var sel = selector_data[current_selected_i];
$("#css_filter").val(sel.xpath);
xctx.fillStyle = 'rgba(225,225,225,0.8)';
xctx.fillRect(0,0,c.width, c.height);
xctx.clearRect(sel.left * x_scale, sel.top * y_scale, sel.width * x_scale, sel.height * y_scale);
state_clicked=true;
set_current_selected_text(sel.xpath);
}
3 years ago
$('#selector-canvas').bind('mousedown', function (e) {
3 years ago
highlight_current_selected_i();
3 years ago
});
3 years ago
}