pointvec/desktop/sources/scripts/picker.js

124 lines
2.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.original = null;
this.start = function()
{
2018-07-26 08:40:06 +00:00
this.el.setAttribute("placeholder",`${dotgrid.tool.style().color}`)
2018-05-06 23:15:23 +00:00
2018-08-17 20:04:54 +00:00
try{ dotgrid.controller.set("picker"); }
catch(err){ console.log("No controller"); }
2018-03-07 00:50:41 +00:00
dotgrid.interface.el.className = "picker"
this.el.focus()
this.original = dotgrid.tool.style().color
this.el.value = ""
}
this.stop = function()
{
this.cancel();
2018-08-17 20:04:54 +00:00
try{ dotgrid.controller.set(); }
catch(err){ console.log("No controller"); }
2018-03-07 00:50:41 +00:00
dotgrid.interface.el.className = ""
this.el.blur()
this.el.value = ""
}
this.validate = function()
{
2018-08-26 19:39:15 +00:00
let parts = this.parse(this.el.value)
2018-03-21 07:10:09 +00:00
2018-05-06 23:05:25 +00:00
if(parts.color){ this.set_color(parts.color); }
2018-05-07 04:51:58 +00:00
if(parts.size){ this.set_size(parts.size); }
2018-05-06 23:05:25 +00:00
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-08-17 20:04:54 +00:00
try{ dotgrid.controller.set(); }
catch(err){ console.log("No controller"); }
2018-05-07 04:51:58 +00:00
dotgrid.interface.el.className = ""
this.el.blur()
this.el.value = ""
2018-07-26 08:40:06 +00:00
setTimeout(() => { dotgrid.interface.refresh(true); }, 500)
2018-05-06 23:05:25 +00:00
}
this.set_color = function(color)
{
dotgrid.tool.style().color = color;
dotgrid.tool.style().fill = dotgrid.tool.style().fill != "none" ? color : "none";
2018-03-07 00:50:41 +00:00
}
2018-05-06 23:05:25 +00:00
this.set_size = function(size)
{
dotgrid.set_size(size);
}
2018-03-07 00:50:41 +00:00
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-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-03-07 00:50:41 +00:00
}
this.update = function()
{
2018-08-26 19:39:15 +00:00
let parts = this.parse(this.el.value)
2018-05-07 04:51:58 +00:00
if(!parts.color){ return; }
2018-03-07 00:50:41 +00:00
2018-05-07 04:51:58 +00:00
dotgrid.tool.style().color = parts.color;
dotgrid.tool.style().fill = dotgrid.tool.style().fill != "none" ? parts.color : "none";
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-03-07 00:50:41 +00:00
}
this.listen = function(e)
{
if(e.key == "Enter"){
this.validate();
e.preventDefault();
return;
}
this.update();
}
2018-05-06 23:05:25 +00:00
this.parse = function(value)
{
2018-08-26 19:39:15 +00:00
let parts = value.split(" ");
let color = null;
let size = null;
2018-05-06 23:05:25 +00:00
2018-08-26 19:39:15 +00:00
for(let id in parts){
let part = parts[id];
2018-05-06 23:05:25 +00:00
if(is_color(part) && !color){ color = part; }
if(is_size(part) && !size){ size = { width:parseInt(part.toLowerCase().split("x")[0]),height:parseInt(part.toLowerCase().split("x")[1]) }; }
}
return {color:color,size:size}
}
function is_size(val)
{
if(val.toLowerCase().indexOf("x") < 1){ return false; }
return true
}
function is_color(val)
2018-03-21 07:10:09 +00:00
{
2018-05-06 23:05:25 +00:00
if(val.length != 4 && val.length != 7){
return false
}
2018-08-26 19:39:15 +00:00
let re = /\#[0-9A-Fa-f]/g;
2018-03-21 07:10:09 +00:00
return re.test(val)
}
2018-03-07 00:50:41 +00:00
this.el.onkeyup = function(event){ dotgrid.picker.listen(event); };
}