ldx-widgets
Version:
widgets
169 lines (161 loc) • 4.51 kB
JavaScript
(function() {
var Flux, Validation, _;
Flux = require('delorean').Flux;
_ = require('lodash');
Validation = Flux.createStore({
actions: {
'add-error': 'addError',
'clear-error': 'clearError',
'toggle-error': 'toggleError',
'show-all': 'showAll',
'force-show-all': 'forceShowAll',
'hide-all': 'hideAll',
'clear-all': 'clearAll',
'raise-validation': 'raiseValidation',
'lower-validation': 'lowerValidation'
},
scheme: {
errors: {
"default": {}
},
errorsActive: {
"default": false
},
forceShowAllErrors: {
"default": false
},
zIndex: {
"default": 11
}
},
/*
Error Object Specs
Each object represents a field, and contains all the active validation issues for that field
{
show: yes/no - whether or not the message displaying in the UI
autoHideAfter: Int - ms to hide the error after
direction: 'left/right/above/below' - defaults to below - direction of the message pvr
messages: [] - array of error message strings
}
*/
addError: function(options) {
var anchor, error, errors, groupId;
groupId = options.groupId, error = options.error, anchor = options.anchor;
errors = this.state.errors;
errors[groupId] = error;
errors[groupId].anchor = anchor;
if (error.direction == null) {
errors[groupId].direction = 'below';
}
this.set({
errorsActive: true
});
if (error.autoHideAfter) {
return this.autoHide(error, groupId);
}
},
clearError: function(id) {
var errors;
errors = this.state.errors;
if (errors[id] != null) {
delete errors[id];
return this.set({
errorsActive: Object.keys(errors).length > 0
});
}
},
clearAll: function() {
return this.set({
errors: {},
errorsActive: false,
forceShowAllErrors: false,
zIndex: 11
});
},
toggleError: function(options) {
var error, errors, groupId, isMouseOver, status;
groupId = options.groupId, status = options.status, isMouseOver = options.isMouseOver;
errors = this.state.errors;
error = errors[groupId];
if (error == null) {
} else {
error.show = status != null ? status : true;
this.emit('change');
if (!isMouseOver && error.autoHideAfter) {
return this.autoHide(error, groupId);
}
}
},
showAll: function(autoHideAfter) {
var error, errors, groupId;
if (autoHideAfter == null) {
autoHideAfter = false;
}
errors = this.state.errors;
for (groupId in errors) {
error = errors[groupId];
error.show = true;
}
this.emit('change');
if (autoHideAfter) {
return this.autoHideAll();
}
},
forceShowAll: function(autoHideAfter) {
var error, errors, groupId;
if (autoHideAfter == null) {
autoHideAfter = false;
}
errors = this.state.errors;
for (groupId in errors) {
error = errors[groupId];
error.show = true;
}
this.set({
forceShowAllErrors: true
});
if (autoHideAfter) {
return this.autoHideAll();
}
},
hideAll: function() {
var error, errors, groupId;
errors = this.state.errors;
for (groupId in errors) {
error = errors[groupId];
error.show = false;
}
return this.emit('change');
},
hideTimers: {},
autoHide: function(error, groupId) {
var errors;
clearInterval(this.hideTimers[groupId]);
errors = this.state.errors;
return this.hideTimers[groupId] = setTimeout((function(_this) {
return function() {
var ref;
if ((ref = errors[groupId]) != null) {
ref.show = false;
}
return _this.emit('change');
};
})(this), error.autoHideAfter || 1500);
},
autoHideAll: function() {
clearInterval(this.hideAllTimer);
return this.hideAllTimer = setTimeout((function(_this) {
return function() {
return _this.hideAll();
};
})(this), 1500);
},
raiseValidation: function() {
return this.set('zIndex', 14);
},
lowerValidation: function() {
return this.set('zIndex', 11);
}
});
module.exports = Validation;
}).call(this);