sky.js/sky.types.js

119 lines
2.4 KiB
JavaScript

(function(SKY){
class SKYEditor{
constructor(placeholder,pattern){
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);
}
html(html){
if (arguments.length == 1)
{
this._html = $(html);
return this;
}
return this._html;
}
id(id){
this.html().attr("id", id);
return this;
}
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 SKYIntEditor extends SKYEditor {
constructor(){
super();
this._html.attr("type","number");
}
}
class SKYIPv4Editor extends SKYEditor {
constructor(){
super("ipv4");
this.html()
.attr("placeholder", "000.000.000.000")
.attr("pattern", "^[0-9]([0-9]){0,2}\.[0-9]([0-9]){0,2}\.[0-9]([0-9]){0,2}\.[0-9]([0-9]){0,2}");
}
}
class SKYType {
constructor(name){
SKY.type(name,Object.getPrototypeOf(this));
}
editor(){
return new SKYEditor();
}
render(value){
return value;
}
}
class SKYIntType extends SKYType
{
constructor(){
super("int");
}
editor(){
return new SKYIntEditor();
}
}
class SKYIPv4Type extends SKYType
{
constructor(){
super("ipv4");
}
editor(){
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}");
}
}
new SKYType(null);
new SKYIntType();
new SKYIPv4Type();
}(SKY));