Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
pierr committed Jun 16, 2015
2 parents b9489c4 + af858f7 commit d860f06
Show file tree
Hide file tree
Showing 34 changed files with 569 additions and 61 deletions.
46 changes: 32 additions & 14 deletions application/action-builder.js
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');
});
};
};
28 changes: 27 additions & 1 deletion application/index.js
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'
});
}
};
14 changes: 9 additions & 5 deletions application/render.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
/*global document*/
var React = require('react');
var keys = require('lodash/object/keys');
/**
* Map containing all the mounted components.
* @type {Object}
Expand All @@ -15,18 +16,21 @@ var clearComponent = require('./clear');
* @param {string} selector - A selector on a DOM node.
* @param {object} options - Options for the component rendering.
*/
module.exports = function(component, selector, options){
module.exports = function renderComponent(component, selector, options){
options = options || {};
// Clear a potential previously mounted component
clearComponent(selector);
let targetDOMContainer = document.querySelector(selector);
if(!targetDOMContainer){throw new Error(`You are trying to render a component in a DOM element which is not existing, your selector is ${selector}`); }
// Render the component
React.render(
var mountedComponent = React.render(
React.createElement(component, options.props, options.data),
document.querySelector(selector)
targetDOMContainer
);
//Save the fact that a component is mounted.
mountedComponents[selector] = true;
console.info('Mounted components : ', Object.keys(mountedComponents));
mountedComponents[selector] = mountedComponent;
console.info('Mounted components : ', keys(mountedComponents));
return mountedComponent;
};
/*
Exemple
Expand Down
3 changes: 3 additions & 0 deletions definition/formatter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
number: require('./number')
};
10 changes: 10 additions & 0 deletions definition/formatter/number.js
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);
}
};
4 changes: 3 additions & 1 deletion definition/index.js
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')
};
8 changes: 8 additions & 0 deletions definition/validator/date.js
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();
};
7 changes: 7 additions & 0 deletions definition/validator/email.js
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);
};
7 changes: 7 additions & 0 deletions definition/validator/index.js
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')
};
17 changes: 17 additions & 0 deletions definition/validator/number.js
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;
};
12 changes: 12 additions & 0 deletions definition/validator/string-length.js
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;
};
91 changes: 91 additions & 0 deletions definition/validator/validate.js
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;
22 changes: 12 additions & 10 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<!-- Place favicon.ico in the root directory -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.js"></script>
<script src='//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.9.3/lodash.min.js'></script>
<script src='http://backbonejs.org/backbone.js'></script>
<script src="https://fb.me/react-0.13.3.js"></script>
<script src="focus.js"></script>
</head>
<body>
Expand Down Expand Up @@ -41,7 +43,7 @@ <h1>Focus</h1>
}]
}
};
focus.definition.domain.container.setAll(domain);
Focus.definition.domain.container.setAll(domain);
var entities ={
"contact": {
"firstName": {
Expand All @@ -61,15 +63,15 @@ <h1>Focus</h1>
"required": false
}
}};
focus.definition.entity.container.setEntityConfiguration(entities);
var contact = focus.definition.entity.builder.getEntityInformations('contact', {firstName: {required: true, symbolique: "papa"}});
Focus.definition.entity.container.setEntityConfiguration(entities);
var contact = Focus.definition.entity.builder.getEntityInformations('contact', {firstName: {required: true, symbolique: "papa"}});
//Cas simple.
var storeSimple = new focus.store.CoreStore({
var storeSimple = new Focus.store.CoreStore({
definitionPath : 'contact'
});
storeSimple.addFirstNameChangeListener(function(data){console.log('store2 firstName: change', data)});
//Cas plus compliqué
var storeSurcharge = new focus.store.CoreStore({
var storeSurcharge = new Focus.store.CoreStore({
definitionPath : 'contact',
customDefinition: {
firstName: {
Expand All @@ -89,8 +91,8 @@ <h1>Focus</h1>
storeSurcharge.addFirstNameChangeListener(function(data){console.log('store1 firstName: change', data)});


focus.dispatcher.handleServerAction({data: {firstName: {roberto: "baggio"}}, type: "update"});
var searchStore = new focus.store.SearchStore();
Focus.dispatcher.handleServerAction({data: {firstName: {roberto: "baggio"}}, type: "update"});
var searchStore = new Focus.store.SearchStore();
searchStore.updateList(['papa', 'singe']);
console.log('list result',searchStore.getList());
function loadRedList(name){
Expand All @@ -99,8 +101,8 @@ <h1>Focus</h1>
}
}

focus.reference.config.set({'papas': loadRedList("papas"), 'singe': loadRedList('singe')})
focus.definition.entity.container.setEntityConfiguration(entities);
Focus.reference.config.set({'papas': loadRedList("papas"), 'singe': loadRedList('singe')})
Focus.definition.entity.container.setEntityConfiguration(entities);
</script>

</body>
Expand Down
3 changes: 1 addition & 2 deletions exception/CustomException.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class CustomException extends Error{
super();
if (Error.hasOwnProperty('captureStackTrace')){
Error.captureStackTrace(this, this.constructor);
}
else{
} else{
Object.defineProperty(this, 'stack', {
value: (new Error()).stack
});
Expand Down
9 changes: 7 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,20 @@ gulp.task('browserify', function(){
.transform(
{global:true},
literalify.configure({
react: 'window.React'
react: 'window.React',
backbone: 'window.Backbone',
moment: 'window.moment',
i18n: 'window.i18n'
}))
.transform(babelify)
.bundle()
//Pass desired output filename to vinyl-source-stream
//.pipe(source("focus-"+require('./package.json').version+".js"))
.pipe(source("focus.js"))
.pipe(gulp.dest('./dist/'))
.pipe(gulp.dest('../focus-components/dist/js'));
.pipe(gulp.dest('../focus-components/dist/js'))
.pipe(gulp.dest('../rodolphe-demo/ui/vendor'))
;
});


Expand Down
Loading

0 comments on commit d860f06

Please sign in to comment.