Implement serverside and client side validatation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// Generated by CoffeeScript 1.6.1
|
||||
(function() {
|
||||
var $el, WSF_BUTTON_CONTROL, WSF_CHECKBOX_CONTROL, WSF_CONTROL, WSF_TEXTAREA_CONTROL, WSF_TEXT_CONTROL, controls, name, state, trigger_callback, type, typemap, _ref,
|
||||
var $el, WSF_BUTTON_CONTROL, WSF_CHECKBOX_CONTROL, WSF_CONTROL, WSF_FORM_ELEMENT_CONTROL, WSF_INPUT_CONTROL, WSF_REGEXP_VALIDATOR, WSF_TEXTAREA_CONTROL, WSF_VALIDATOR, controls, name, state, trigger_callback, type, typemap, validatormap, _ref, _ref1, _ref2,
|
||||
__hasProp = {}.hasOwnProperty,
|
||||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
|
||||
|
||||
@@ -23,12 +23,52 @@
|
||||
});
|
||||
};
|
||||
|
||||
WSF_VALIDATOR = (function() {
|
||||
|
||||
function WSF_VALIDATOR(parent_control, settings) {
|
||||
this.parent_control = parent_control;
|
||||
this.settings = settings;
|
||||
this.error = this.settings.error;
|
||||
return;
|
||||
}
|
||||
|
||||
WSF_VALIDATOR.prototype.validate = function() {
|
||||
return true;
|
||||
};
|
||||
|
||||
return WSF_VALIDATOR;
|
||||
|
||||
})();
|
||||
|
||||
WSF_REGEXP_VALIDATOR = (function(_super) {
|
||||
|
||||
__extends(WSF_REGEXP_VALIDATOR, _super);
|
||||
|
||||
function WSF_REGEXP_VALIDATOR() {
|
||||
WSF_REGEXP_VALIDATOR.__super__.constructor.apply(this, arguments);
|
||||
this.pattern = new RegExp(this.settings.expression, 'g');
|
||||
}
|
||||
|
||||
WSF_REGEXP_VALIDATOR.prototype.validate = function() {
|
||||
var res, val;
|
||||
val = this.parent_control.value();
|
||||
res = val.match(this.pattern);
|
||||
return res !== null;
|
||||
};
|
||||
|
||||
return WSF_REGEXP_VALIDATOR;
|
||||
|
||||
})(WSF_VALIDATOR);
|
||||
|
||||
validatormap = {
|
||||
"WSF_REGEXP_VALIDATOR": WSF_REGEXP_VALIDATOR
|
||||
};
|
||||
|
||||
WSF_CONTROL = (function() {
|
||||
|
||||
function WSF_CONTROL(control_name, $el) {
|
||||
this.control_name = control_name;
|
||||
this.$el = $el;
|
||||
this.attach_events();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -36,6 +76,33 @@
|
||||
|
||||
WSF_CONTROL.prototype.update = function(state) {};
|
||||
|
||||
WSF_CONTROL.prototype.on = function(name, callback, context) {
|
||||
if (this._events == null) {
|
||||
this._events = {};
|
||||
}
|
||||
if (this._events[name] == null) {
|
||||
this._events[name] = [];
|
||||
}
|
||||
this._events[name].push({
|
||||
callback: callback,
|
||||
context: context
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
WSF_CONTROL.prototype.trigger = function(name) {
|
||||
var ev, _i, _len, _ref, _ref1;
|
||||
if (((_ref = this._events) != null ? _ref[name] : void 0) == null) {
|
||||
return this;
|
||||
}
|
||||
_ref1 = this._events[name];
|
||||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
||||
ev = _ref1[_i];
|
||||
ev.callback.call(ev.context);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
return WSF_CONTROL;
|
||||
|
||||
})();
|
||||
@@ -76,15 +143,15 @@
|
||||
|
||||
})(WSF_CONTROL);
|
||||
|
||||
WSF_TEXT_CONTROL = (function(_super) {
|
||||
WSF_INPUT_CONTROL = (function(_super) {
|
||||
|
||||
__extends(WSF_TEXT_CONTROL, _super);
|
||||
__extends(WSF_INPUT_CONTROL, _super);
|
||||
|
||||
function WSF_TEXT_CONTROL() {
|
||||
return WSF_TEXT_CONTROL.__super__.constructor.apply(this, arguments);
|
||||
function WSF_INPUT_CONTROL() {
|
||||
return WSF_INPUT_CONTROL.__super__.constructor.apply(this, arguments);
|
||||
}
|
||||
|
||||
WSF_TEXT_CONTROL.prototype.attach_events = function() {
|
||||
WSF_INPUT_CONTROL.prototype.attach_events = function() {
|
||||
var self;
|
||||
self = this;
|
||||
return this.$el.change(function() {
|
||||
@@ -92,21 +159,26 @@
|
||||
});
|
||||
};
|
||||
|
||||
WSF_TEXT_CONTROL.prototype.change = function() {
|
||||
WSF_INPUT_CONTROL.prototype.change = function() {
|
||||
window.states[this.control_name]['text'] = this.$el.val();
|
||||
if (window.states[this.control_name]['callback_change']) {
|
||||
return trigger_callback(this.control_name, 'change');
|
||||
trigger_callback(this.control_name, 'change');
|
||||
}
|
||||
return this.trigger('change');
|
||||
};
|
||||
|
||||
WSF_TEXT_CONTROL.prototype.update = function(state) {
|
||||
WSF_INPUT_CONTROL.prototype.value = function() {
|
||||
return this.$el.val();
|
||||
};
|
||||
|
||||
WSF_INPUT_CONTROL.prototype.update = function(state) {
|
||||
if (state.text != null) {
|
||||
window.states[this.control_name]['text'] = state.text;
|
||||
return this.$el.val(state.text);
|
||||
}
|
||||
};
|
||||
|
||||
return WSF_TEXT_CONTROL;
|
||||
return WSF_INPUT_CONTROL;
|
||||
|
||||
})(WSF_CONTROL);
|
||||
|
||||
@@ -129,8 +201,13 @@
|
||||
WSF_TEXTAREA_CONTROL.prototype.change = function() {
|
||||
window.states[this.control_name]['text'] = this.$el.val();
|
||||
if (window.states[this.control_name]['callback_change']) {
|
||||
return trigger_callback(this.control_name, 'change');
|
||||
trigger_callback(this.control_name, 'change');
|
||||
}
|
||||
return this.trigger('change');
|
||||
};
|
||||
|
||||
WSF_TEXTAREA_CONTROL.prototype.value = function() {
|
||||
return this.$el.val();
|
||||
};
|
||||
|
||||
WSF_TEXTAREA_CONTROL.prototype.update = function(state) {
|
||||
@@ -163,8 +240,13 @@
|
||||
WSF_CHECKBOX_CONTROL.prototype.change = function() {
|
||||
window.states[this.control_name]['checked'] = this.$el.is(':checked');
|
||||
if (window.states[this.control_name]['callback_change']) {
|
||||
return trigger_callback(this.control_name, 'change');
|
||||
trigger_callback(this.control_name, 'change');
|
||||
}
|
||||
return this.trigger('change');
|
||||
};
|
||||
|
||||
WSF_CHECKBOX_CONTROL.prototype.value = function() {
|
||||
return this.$el.is(':checked');
|
||||
};
|
||||
|
||||
WSF_CHECKBOX_CONTROL.prototype.update = function(state) {
|
||||
@@ -178,11 +260,81 @@
|
||||
|
||||
})(WSF_CONTROL);
|
||||
|
||||
WSF_FORM_ELEMENT_CONTROL = (function(_super) {
|
||||
|
||||
__extends(WSF_FORM_ELEMENT_CONTROL, _super);
|
||||
|
||||
function WSF_FORM_ELEMENT_CONTROL() {
|
||||
return WSF_FORM_ELEMENT_CONTROL.__super__.constructor.apply(this, arguments);
|
||||
}
|
||||
|
||||
WSF_FORM_ELEMENT_CONTROL.prototype.attach_events = function() {
|
||||
var self, validator, _i, _len, _ref;
|
||||
self = this;
|
||||
this.value_control = controls[window.states[this.control_name]['value_control']];
|
||||
if (this.value_control != null) {
|
||||
this.value_control.on('change', this.change, this);
|
||||
}
|
||||
this.serverside_validator = false;
|
||||
this.validators = [];
|
||||
_ref = window.states[this.control_name]['validators'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
validator = _ref[_i];
|
||||
if (validatormap[validator.name] != null) {
|
||||
this.validators.push(new validatormap[validator.name](this, validator));
|
||||
} else {
|
||||
this.serverside_validator = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
WSF_FORM_ELEMENT_CONTROL.prototype.change = function() {
|
||||
var validator, _i, _len, _ref;
|
||||
_ref = this.validators;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
validator = _ref[_i];
|
||||
if (!validator.validate()) {
|
||||
this.showerror(validator.error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.showerror("");
|
||||
if (this.serverside_validator) {
|
||||
trigger_callback(this.control_name, 'validate');
|
||||
}
|
||||
};
|
||||
|
||||
WSF_FORM_ELEMENT_CONTROL.prototype.showerror = function(message) {
|
||||
var errordiv;
|
||||
this.$el.removeClass("has-error");
|
||||
this.$el.find(".validation").remove();
|
||||
if (message.length > 0) {
|
||||
this.$el.addClass("has-error");
|
||||
errordiv = $("<div />").addClass('help-block').addClass('validation').text(message);
|
||||
return this.$el.find(".col-lg-10").append(errordiv);
|
||||
}
|
||||
};
|
||||
|
||||
WSF_FORM_ELEMENT_CONTROL.prototype.update = function(state) {
|
||||
if (state.error != null) {
|
||||
return this.showerror(state.error);
|
||||
}
|
||||
};
|
||||
|
||||
WSF_FORM_ELEMENT_CONTROL.prototype.value = function() {
|
||||
return this.value_control.value();
|
||||
};
|
||||
|
||||
return WSF_FORM_ELEMENT_CONTROL;
|
||||
|
||||
})(WSF_CONTROL);
|
||||
|
||||
typemap = {
|
||||
"WSF_BUTTON_CONTROL": WSF_BUTTON_CONTROL,
|
||||
"WSF_TEXT_CONTROL": WSF_TEXT_CONTROL,
|
||||
"WSF_INPUT_CONTROL": WSF_INPUT_CONTROL,
|
||||
"WSF_TEXTAREA_CONTROL": WSF_TEXTAREA_CONTROL,
|
||||
"WSF_CHECKBOX_CONTROL": WSF_CHECKBOX_CONTROL
|
||||
"WSF_CHECKBOX_CONTROL": WSF_CHECKBOX_CONTROL,
|
||||
"WSF_FORM_ELEMENT_CONTROL": WSF_FORM_ELEMENT_CONTROL
|
||||
};
|
||||
|
||||
_ref = window.states;
|
||||
@@ -195,4 +347,12 @@
|
||||
}
|
||||
}
|
||||
|
||||
_ref1 = window.states;
|
||||
for (name in _ref1) {
|
||||
state = _ref1[name];
|
||||
if ((_ref2 = controls[name]) != null) {
|
||||
_ref2.attach_events();
|
||||
}
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
|
||||
Reference in New Issue
Block a user