ldx-widgets
Version:
widgets
232 lines (225 loc) • 6.59 kB
JavaScript
(function() {
var Flux, Validation;
Flux = require('delorean').Flux;
Validation = Flux.createStore({
actions: {
'add-error': 'addError',
'toggle-error': 'toggleError',
'clear-error': 'clearError',
'clear-all': 'clearAll',
'force-show-all': 'forceShowAll',
'reset-force-show-all': 'resetForceShowAll',
'user-logout': 'clearData'
},
clearData: function() {
return this.resetState();
},
scheme: {
errors: {
"default": {}
},
errorsActive: {
"default": false
},
forceShowAllErrors: {
"default": false
},
pvrErrors: {
"default": {}
},
pvrErrorsActive: {
"default": false
},
pvrForceShowAllErrors: {
"default": false
}
},
/*
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 isInPopover;
isInPopover = options.isInPopover;
if (isInPopover) {
return this.addPvrError(options);
} else {
return this.addRegularError(options);
}
},
addRegularError: 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);
}
},
addPvrError: function(options) {
var anchor, error, groupId, pvrErrors;
groupId = options.groupId, error = options.error, anchor = options.anchor;
pvrErrors = this.state.pvrErrors;
pvrErrors[groupId] = error;
pvrErrors[groupId].anchor = anchor;
if (error.direction == null) {
pvrErrors[groupId].direction = 'below';
}
this.set({
pvrErrorsActive: true
});
if (error.autoHideAfter) {
return this.autoHide(error, groupId);
}
},
clearError: function(options) {
var isInPopover;
isInPopover = options.isInPopover;
if (isInPopover) {
return this.clearPvrError(options);
} else {
return this.clearRegularError(options);
}
},
clearRegularError: function(options) {
var errors, groupId;
groupId = options.groupId;
errors = this.state.errors;
if (errors[groupId] != null) {
delete errors[groupId];
return this.set({
errorsActive: Object.keys(errors).length > 0
});
}
},
clearPvrError: function(options) {
var groupId, pvrErrors;
groupId = options.groupId;
pvrErrors = this.state.pvrErrors;
if (pvrErrors[groupId] != null) {
delete pvrErrors[groupId];
return this.set({
errorsActive: Object.keys(pvrErrors).length > 0
});
}
},
clearAll: function(isInPopover) {
if (isInPopover) {
return this.clearAllPvrErrors();
} else {
return this.clearAllRegularErrors();
}
},
clearAllRegularErrors: function() {
return this.set({
errors: {},
errorsActive: false,
forceShowAllErrors: false
});
},
clearAllPvrErrors: function() {
return this.set({
pvrErrors: {},
pvrErrorsActive: false,
pvrForceShowAllErrors: false
});
},
toggleError: function(options) {
var error, errors, groupId, isInPopover, isMouseOver, pvrErrors, ref, status;
groupId = options.groupId, status = options.status, isMouseOver = options.isMouseOver, isInPopover = options.isInPopover;
ref = this.state, errors = ref.errors, pvrErrors = ref.pvrErrors;
error = isInPopover ? pvrErrors[groupId] : errors[groupId];
if (error == null) {
} else {
error.show = status != null ? status : true;
this.emit('change');
if (!isMouseOver && error.autoHideAfter) {
return this.autoHide(error, groupId);
}
}
},
forceShowAll: function(options) {
var autoHideAfter, isInPopover;
autoHideAfter = options.autoHideAfter, isInPopover = options.isInPopover;
if (isInPopover) {
return this.forceShowAllPvr(autoHideAfter);
} else {
return this.forceShowAllRegular(autoHideAfter);
}
},
forceShowAllRegular: function(autoHideAfter) {
var error, errors, groupId;
errors = this.state.errors;
for (groupId in errors) {
error = errors[groupId];
error.show = true;
}
this.set({
forceShowAllErrors: true
});
if (autoHideAfter) {
return this.autoHideAll(errors);
}
},
forceShowAllPvr: function(autoHideAfter) {
var error, groupId, pvrErrors;
pvrErrors = this.state.pvrErrors;
for (groupId in pvrErrors) {
error = pvrErrors[groupId];
error.show = true;
}
this.set({
pvrForceShowAllErrors: true
});
if (autoHideAfter) {
return this.autoHideAll(pvrErrors);
}
},
resetForceShowAll: function() {
return this.set({
forceShowAllErrors: false,
pvrForceShowAllErrors: false
});
},
hideAll: function(errors) {
var error, groupId;
for (groupId in errors) {
error = errors[groupId];
error.show = false;
}
return this.emit('change');
},
hideTimers: {},
autoHide: function(error, groupId) {
clearInterval(this.hideTimers[groupId]);
return this.hideTimers[groupId] = setTimeout((function(_this) {
return function() {
error.show = false;
return _this.emit('change');
};
})(this), error.autoHideAfter || 1500);
},
autoHideAll: function(errors) {
clearInterval(this.hideAllTimer);
return this.hideAllTimer = setTimeout((function(_this) {
return function() {
return _this.hideAll(errors);
};
})(this), 1500);
}
});
module.exports = Validation;
}).call(this);