sky.js/sky.types.js

220 lines
5.1 KiB
JavaScript

(function(SKY){
class SKYEditor extends SKY.prototypes.SKYBase {
constructor(placeholder,pattern){
super();
this.html($('<input type="text">'));
if (pattern)
this.html().attr("pattern", pattern);
if (placeholder)
this.html().attr("placeholder", placeholder);
this.html().on("input", ()=>this.changed( this.html().data("value") != this.html().val() ) );
this.html().data("value", null);
}
id(id){
if (arguments.length)
{
this.html().attr("id", id);
return this;
}
return this.html().attr("id");
}
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;
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;
class SKYIntEditor extends SKYEditor {
constructor(){
super();
this._html.attr("type","number");
}
}
class SKYTimeSpanEditor extends SKYEditor {
constructor(){
super("0d 00:00:00","^([0-9]{1,}d){0,1} {0,}([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$");
}
value(value){
if (arguments.length)
{
this._html
.val(SKY.type("timespan").render(value))
.data("value", value);
return this;
}
return SKY.type("timespan").parse(this._html.val());
}
}
class SKYType {
constructor(name){
SKY.type(name,Object.getPrototypeOf(this));
}
editor(id){
return new SKYEditor().id(id);
}
selector(id){
return null;
}
render(value){
return value;
}
parse(value){
return value;
}
}
SKY.prototypes.SKYType = SKYType;
class SKYIntType extends SKYType
{
constructor(){
super("int");
}
editor(id){
return new SKYIntEditor().id(id);
}
}
class SKYIPv4Type extends SKYType
{
constructor(){
super("ipv4");
}
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);
}
}
class SKYNetwork4Type extends SKYType
{
constructor(){
super("network4");
}
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);
}
}
class SKYTimeSpanType extends SKYType
{
constructor(){
super("timespan");
}
editor(id){
return new SKYTimeSpanEditor().id(id);
}
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){
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);
}
}
new SKYType(null);
new SKYIntType();
new SKYIPv4Type();
new SKYNetwork4Type();
new SKYTimeSpanType();
}(SKY));