-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
569 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,42 @@ | ||
var dispatcher = require('../dispatcher'); | ||
function preServiceCall(config){ | ||
dispatcher.handleViewAction({ | ||
data: {[config.node]: undefined}, | ||
type: config.type, | ||
status: {[config.node]: {name:config.preStatus}, isLoading: true} | ||
}); | ||
} | ||
function postServiceCall(config, json){ | ||
dispatcher.handleServerAction({ | ||
data: {[config.node]: json}, | ||
type: config.type, | ||
status: {[config.node]: {name:config.status}, isLoading: false} | ||
}); | ||
} | ||
|
||
module.exports = function(config){ | ||
config = config || {}; | ||
config.type = config.type || 'update'; | ||
if(!config.service){ | ||
throw new Error('You need to provide a service'); | ||
} | ||
config.preStatus = config.preStatus || 'loading'; | ||
if(!config.service){ | ||
throw new Error('You need to provide a service to call'); | ||
} | ||
|
||
if(!config.data){ | ||
throw new Error('You need to provide an action data'); | ||
if(!config.status){ | ||
throw new Error('You need to provide a status to your action'); | ||
} | ||
return config.service(config.data).then(function(jsonData){ | ||
dispatcher.handleServerAction({ | ||
data: {[config.property]: jsonData}, | ||
type: config.type | ||
/*if(!config.data){ | ||
throw new Error('You need to provide an action data'); | ||
}*/ | ||
//Exposes a function consumes by the compoennt. | ||
return function(criteria){ | ||
preServiceCall(config); | ||
//todo: add middleware see slack for more informations | ||
return config.service(criteria).then(function(jsonData){ | ||
postServiceCall(config, jsonData); | ||
}, function actionError(err){ | ||
console.warn('Error in action', err); | ||
//Get code back from a project | ||
throw new Error('An errror occurs'); | ||
}); | ||
}, function actionError(err){ | ||
console.warn('Error in action', err); | ||
throw new Error('An errror occurs'); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,32 @@ | ||
var dispatcher = require('../dispatcher'); | ||
var Empty = React.createClass({ | ||
render: function() { | ||
return <div></div>; | ||
} | ||
}); | ||
|
||
module.exports = { | ||
render: require('./render'), | ||
builtInStore: require('./built-in-store'), | ||
actionBuilder: require('./action-builder'), | ||
clear: require('./clear') | ||
clear: require('./clear'), | ||
mountedComponents: require('./mounted-components'), | ||
changeMode(newMode, previousMode){ | ||
var mode = {newMode: newMode, previousMode: previousMode}; | ||
dispatcher.handleViewAction({data: {mode: mode}, type: 'update'}); | ||
}, | ||
changeRoute(newRoute){ | ||
dispatcher.handleViewAction({data: {route: newRoute}, type: 'update'}); | ||
}, | ||
clearCartridge(){ | ||
dispatcher.handleViewAction({ | ||
data: { | ||
cartridgeComponent: {component: Empty}, | ||
barContentLeftComponent: {component: Empty}, | ||
summaryComponent: {component: Empty}, | ||
actions: {primary: [], secondary: []} | ||
}, | ||
type: 'update' | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
number: require('./number') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
let numeral = require('numeral'); | ||
|
||
const DEFAULT_FORMAT = '0,0'; | ||
|
||
module.exports = { | ||
format(number, format) { | ||
format = format || DEFAULT_FORMAT; | ||
return numeral(number).format(format); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
module.exports = { | ||
domain: require('./domain'), | ||
entity: require('./entity') | ||
entity: require('./entity'), | ||
validator: require('./validator'), | ||
formatter: require('./formatter') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
module.exports = function dateValidation(dateToValidate, options) { | ||
var moment = require('moment'); | ||
if(!moment){ | ||
console.warn('Moment library is not a part of your project, please download it : http://momentjs.com/'); | ||
} | ||
return moment(dateToValidate).isValid(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const EMAIL_REGEX = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
|
||
//Function to test an email. | ||
module.exports = function emailValidation(emailToValidate, options) { | ||
options = options || {}; | ||
return EMAIL_REGEX.test(emailToValidate); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
date: require('./date'), | ||
email: require('./email'), | ||
number: require('./number'), | ||
stringLength: require('./string-length'), | ||
validate: require('./validate') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const NUMBER_REGEX = /^-?\d+(?:\.d*)?(?:e[+\-]?\d+)?$/i; | ||
let {isUndefined, isNull} = require('lodash/lang/isUndefined'); | ||
|
||
//Function to validate that an input is a number. | ||
module.exports = function numberValidation(numberToValidate, options) { | ||
options = options || {}; | ||
if (_.isUndefined(numberToValidate) || _.isNull(numberToValidate)) { | ||
return true; | ||
} | ||
if (_.isNaN(numberToValidate)) { | ||
return false; | ||
} | ||
numberToValidate = +numberToValidate; //Cast it into a number. | ||
var isMin = options.min !== undefined ? numberToValidate >= options.min : true; | ||
var isMax = options.max !== undefined ? numberToValidate <= options.max : true; | ||
return isMin && isMax; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//Function to test the length of a string. | ||
module.exports = function stringLength(stringToTest, options) { | ||
if ('string' !== typeof stringToTest) { | ||
return false; | ||
} | ||
options = options || {}; | ||
//console.log(options); | ||
options.minLength = options.minLength || 0; | ||
var isMinLength = options.minLength !== undefined ? stringToTest.length >= options.minLength : true; | ||
var isMaxLength = options.maxLength !== undefined ? stringToTest.length <= options.maxLength : true; | ||
return isMinLength && isMaxLength; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//Dependency | ||
let DependencyException = require("../../exception").DependencyException; | ||
let i18n = require('i18n'); | ||
let assign = require('object-assign'); | ||
|
||
//Focus validators | ||
let emailValidation = require('./email'); | ||
let numberValidation = require('./number'); | ||
let stringLength = require('./string-length'); | ||
let dateValidation = require('./date'); | ||
|
||
//Validate a property, a property shoul be as follow: `{name: "field_name",value: "field_value", validators: [{...}] }` | ||
var validate = function validate(property, validators) { | ||
//console.log("validate", property, validators); | ||
var errors, res, validator, _i, _len; | ||
errors = []; | ||
if (validators) { | ||
for (_i = 0, _len = validators.length; _i < _len; _i++) { | ||
validator = validators[_i]; | ||
res = validateProperty(property, validator); | ||
if (res !== null && res !== undefined) { | ||
errors.push(res); | ||
} | ||
} | ||
} | ||
//Check what's the good type to return. | ||
return { | ||
name: property.name, | ||
value: property.value, | ||
isValid: errors.length === 0, | ||
errors: errors | ||
}; | ||
}; | ||
|
||
function validateProperty(property, validator) { | ||
var isValid; | ||
if (!validator) { | ||
return void 0; | ||
} | ||
if (!property) { | ||
return void 0; | ||
} | ||
isValid = (function () { | ||
switch (validator.type) { | ||
case "required": | ||
var prevalidString = property.value === "" ? false : true; | ||
var prevalidDate = true; | ||
return validator.value === true ? (property.value !== null && property.value !== undefined && prevalidString && prevalidDate) : true; | ||
case "regex": | ||
if (property.value === undefined || property.value === null) { | ||
return true; | ||
} | ||
return validator.value.test(property.value); | ||
case "email": | ||
if (property.value === undefined || property.value === null) { | ||
return true; | ||
} | ||
return emailValidation(property.value, validator.options); | ||
case "number": | ||
return numberValidation(property.value, validator.options); | ||
case "string": | ||
var stringToValidate = property.value || ""; | ||
return stringLength(stringToValidate, validator.options); | ||
case "date": | ||
return dateValidation(property.value, validator.options); | ||
case "function": | ||
return validator.value(property.value, validator.options); | ||
default: | ||
return void 0; | ||
} | ||
})(); | ||
if (isValid === undefined || isValid === null) { | ||
console.warn('The validator of type: ' + validator.type + ' is not defined'); //Todo: call the logger. | ||
} else if (isValid === false) { | ||
|
||
//Add the name of the property. | ||
return getErrorLalel(validator.type, property.modelName + '.' + property.name, validator.options); //"The property " + property.name + " is invalid."; | ||
} | ||
}; | ||
|
||
function getErrorLalel(type, fieldName, options) { | ||
options = options || {}; | ||
if (!i18n) { | ||
throw new DependencyException("Dependency not resolved: i18n.js"); | ||
} | ||
var translationKey = options.translationKey ? options.translationKey : "domain.validation." + type; | ||
var opts = assign({fieldName: i18n.t(fieldName)}, options); | ||
return i18n.t(translationKey, opts); | ||
} | ||
|
||
module.exports = validate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.