sky.js/sky.types.js

220 lines
5.1 KiB
JavaScript
Raw Normal View History

2019-05-14 15:23:14 +02:00
(function(SKY){
2019-05-20 12:39:22 +02:00
class SKYEditor extends SKY.prototypes.SKYBase {
2019-05-14 15:23:14 +02:00
constructor(placeholder,pattern){
2019-05-20 12:39:22 +02:00
super();
2019-05-14 15:23:14 +02:00
this.html($('<input type="text">'));
if (pattern)
2019-05-20 12:39:22 +02:00
this.html().attr("pattern", pattern);
2019-05-14 15:23:14 +02:00
if (placeholder)
2019-05-20 12:39:22 +02:00
this.html().attr("placeholder", placeholder);
2019-05-14 15:23:14 +02:00
2019-05-20 12:39:22 +02:00
this.html().on("input", ()=>this.changed( this.html().data("value") != this.html().val() ) );
this.html().data("value", null);
2019-05-14 15:23:14 +02:00
}
id(id){
2019-05-20 12:39:22 +02:00
if (arguments.length)
{
this.html().attr("id", id);
return this;
}
return this.html().attr("id");
2019-05-14 15:23:14 +02:00
}
value(value){
if (arguments.length)
{
this._html
.val(value)
.data("value", value);
return this;
}
return this._html.val();
}
changed(changed){
if (arguments.length)
{
this._changed = (changed) ? true : false;
if (this._changed)
this.html().addClass("changed");
else
this.html().removeClass("changed");
return this;
}
return this._changed;
}
valid(){
return this._html[0].checkValidity();
}
}
SKY.prototypes.SKYEditor = SKYEditor;
2019-05-20 12:39:22 +02:00
class SKYSelector extends SKY.prototypes.SKYBase {
constructor(){
super();
}
value(value){
if (arguments.length){
return this;
}
return null;
}
appendTo(parent){
this.html().appendTo(parent);
return this;
}
enable(en){
if (arguments.length)
{
this.enabled = en;
if (en)
this.html().removeAttr("disabled");
else
this.html().attr("disabled", "");
return this;
}
return this.enabled;
}
}
SKY.prototypes.SKYSelector = SKYSelector;
2019-05-14 15:23:14 +02:00
class SKYIntEditor extends SKYEditor {
constructor(){
super();
2019-05-20 12:39:22 +02:00
this._html.attr("type","number");
2019-05-14 15:23:14 +02:00
}
}
2019-05-15 13:14:59 +02:00
class SKYTimeSpanEditor extends SKYEditor {
constructor(){
2019-05-20 12:39:22 +02:00
super("0d 00:00:00","^([0-9]{1,}d){0,1} {0,}([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$");
2019-05-15 13:14:59 +02:00
}
value(value){
if (arguments.length)
{
this._html
.val(SKY.type("timespan").render(value))
.data("value", value);
return this;
2019-05-20 12:39:22 +02:00
}
return SKY.type("timespan").parse(this._html.val());
2019-05-15 13:14:59 +02:00
}
}
2019-05-14 15:23:14 +02:00
class SKYType {
constructor(name){
SKY.type(name,Object.getPrototypeOf(this));
}
2019-05-23 13:38:32 +02:00
editor(id){
return new SKYEditor().id(id);
2019-05-14 15:23:14 +02:00
}
2019-05-20 12:39:22 +02:00
2019-05-23 13:38:32 +02:00
selector(id){
2019-05-20 12:39:22 +02:00
return null;
}
2019-05-14 15:23:14 +02:00
render(value){
return value;
}
2019-05-20 12:39:22 +02:00
parse(value){
return value;
}
2019-05-14 15:23:14 +02:00
}
2019-05-20 12:39:22 +02:00
SKY.prototypes.SKYType = SKYType;
2019-05-14 15:23:14 +02:00
class SKYIntType extends SKYType
{
constructor(){
super("int");
}
2019-05-23 13:38:32 +02:00
editor(id){
return new SKYIntEditor().id(id);
2019-05-14 15:23:14 +02:00
}
}
class SKYIPv4Type extends SKYType
{
constructor(){
super("ipv4");
}
2019-05-23 13:38:32 +02:00
editor(id){
return new SKYEditor("000.000.000.000","^[0-9]([0-9]){0,2}\.[0-9]([0-9]){0,2}\.[0-9]([0-9]){0,2}\.[0-9]([0-9]){0,2}").id(id);
2019-05-14 15:23:14 +02:00
}
}
2019-05-15 10:37:22 +02:00
class SKYNetwork4Type extends SKYType
{
constructor(){
super("network4");
}
2019-05-23 13:38:32 +02:00
editor(id){
return new SKYEditor("000.000.000.000/00","^[0-9]([0-9]){0,2}\.[0-9]([0-9]){0,2}\.[0-9]([0-9]){0,2}\.[0-9]([0-9]){0,2}\/[0-9]([0-9]){0,1}$").id(id);
2019-05-15 10:37:22 +02:00
}
}
2019-05-14 15:23:14 +02:00
2019-05-15 13:14:59 +02:00
class SKYTimeSpanType extends SKYType
{
constructor(){
super("timespan");
}
2019-05-23 13:38:32 +02:00
editor(id){
return new SKYTimeSpanEditor().id(id);
2019-05-15 13:14:59 +02:00
}
render(value){
var days, hours, minutes, seconds;
value = parseInt(value);
days = parseInt(value / 86400);
value %= 86400;
hours = parseInt(value / 3600);
value %= 3600;
minutes = parseInt(value / 60);
value %= 60;
seconds = parseInt(value);
var r = "";
if (days > 0)
r += `${days}d `;
r += `${SKY.tools.ntos(hours,2)}:${SKY.tools.ntos(minutes,2)}:${SKY.tools.ntos(seconds,2)}`;
return r;
}
parse(t){
2019-05-20 12:39:22 +02:00
var tokens = t.match("^([0-9]{1,}d){0,1} {0,}([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$");
var days,hours,minutes,seconds;
days = tokens[1] ? tokens[1].substr(0,tokens[1].length-1) : 0;
hours = tokens[2] || 0;
minutes = tokens[3] || 0;
seconds = tokens[4] || 0;
return parseInt(days * 86400) + parseInt(hours * 3600) + parseInt(minutes * 60) + parseInt(seconds);
2019-05-15 13:14:59 +02:00
}
}
2019-05-14 15:23:14 +02:00
new SKYType(null);
new SKYIntType();
new SKYIPv4Type();
2019-05-15 10:37:22 +02:00
new SKYNetwork4Type();
2019-05-15 13:14:59 +02:00
new SKYTimeSpanType();
2019-05-14 15:23:14 +02:00
}(SKY));