pointvec/desktop/sources/scripts/picker.js

75 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-03-07 00:50:41 +00:00
function Picker()
{
this.memory = "";
this.el = document.createElement("input");
this.el.id = "picker"
this.el.setAttribute("placeholder","#ff0000")
this.el.setAttribute("maxlength","7")
this.original = null;
this.start = function()
{
dotgrid.controller.set("picker");
dotgrid.interface.el.className = "picker"
this.el.focus()
this.original = dotgrid.tool.style().color
this.el.value = ""
}
this.stop = function()
{
this.cancel();
dotgrid.controller.set();
dotgrid.interface.el.className = ""
this.el.blur()
this.el.value = ""
}
this.validate = function()
{
2018-03-21 07:10:09 +00:00
if(!is_valid(this.el.value)){ return; }
2018-03-07 00:50:41 +00:00
dotgrid.tool.style().color = this.el.value;
2018-03-07 07:43:46 +00:00
dotgrid.tool.style().fill = dotgrid.tool.style().fill != "none" ? this.el.value : "none";
2018-03-07 00:50:41 +00:00
dotgrid.draw();
dotgrid.controller.set();
dotgrid.interface.el.className = ""
this.el.blur()
}
this.cancel = function()
{
if(!this.original){ return; }
dotgrid.tool.style().color = this.original;
2018-03-07 07:43:46 +00:00
dotgrid.tool.style().fill = dotgrid.tool.style().fill != "none" ? this.original : "none";
2018-03-07 00:50:41 +00:00
dotgrid.draw();
}
this.update = function()
{
if(this.el.value.length != 4 && this.el.value.length != 7){ return; }
dotgrid.tool.style().color = this.el.value;
2018-03-07 07:43:46 +00:00
dotgrid.tool.style().fill = dotgrid.tool.style().fill != "none" ? this.el.value : "none";
2018-03-07 00:50:41 +00:00
dotgrid.draw();
}
this.listen = function(e)
{
if(e.key == "Enter"){
this.validate();
e.preventDefault();
return;
}
this.update();
}
2018-03-21 07:10:09 +00:00
function is_valid(val)
{
var re = /[0-9A-Fa-f]{6}/g;
return re.test(val)
}
2018-03-07 00:50:41 +00:00
this.el.onkeyup = function(event){ dotgrid.picker.listen(event); };
}